]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/rsync.c
storage: constify where possible
[mirror_lxc.git] / src / lxc / storage / rsync.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _GNU_SOURCE
25 #include <grp.h>
26 #include <sched.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/mount.h>
34
35 #include "log.h"
36 #include "rsync.h"
37 #include "storage.h"
38 #include "utils.h"
39
40 lxc_log_define(rsync, lxc);
41
42 int lxc_storage_rsync_exec_wrapper(void *data)
43 {
44 struct rsync_data *arg = data;
45 return lxc_rsync(arg);
46 }
47
48 int lxc_rsync_exec_wrapper(void *data)
49 {
50 int ret;
51 struct rsync_data_char *args = data;
52
53 ret = lxc_switch_uid_gid(0, 0);
54 if (ret < 0)
55 return -1;
56
57 ret = lxc_setgroups(0, NULL);
58 if (ret < 0)
59 return -1;
60
61 return lxc_rsync_exec(args->src, args->dest);
62 }
63
64 int lxc_rsync_exec(const char *src, const char *dest)
65 {
66 int ret;
67 size_t l;
68 char *s;
69
70 l = strlen(src) + 2;
71 s = malloc(l);
72 if (!s)
73 return -1;
74
75 ret = snprintf(s, l, "%s", src);
76 if (ret < 0 || (size_t)ret >= l)
77 return -1;
78
79 s[l - 2] = '/';
80 s[l - 1] = '\0';
81
82 execlp("rsync", "rsync", "-aHXS", "--delete", s, dest, (char *)NULL);
83 return -1;
84 }
85
86 int lxc_rsync(struct rsync_data *data)
87 {
88 int ret;
89 const char *dest, *src;
90 struct lxc_storage *orig = data->orig, *new = data->new;
91
92 ret = unshare(CLONE_NEWNS);
93 if (ret < 0) {
94 SYSERROR("Failed to unshare CLONE_NEWNS");
95 return -1;
96 }
97
98 ret = detect_shared_rootfs();
99 if (ret) {
100 ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
101 if (ret < 0)
102 SYSERROR("Failed to make \"/\" a slave mount");
103 }
104
105 ret = orig->ops->mount(orig);
106 if (ret < 0) {
107 ERROR("Failed mounting \"%s\" on \"%s\"", orig->src, orig->dest);
108 return -1;
109 }
110
111 ret = new->ops->mount(new);
112 if (ret < 0) {
113 ERROR("Failed mounting \"%s\" onto \"%s\"", new->src, new->dest);
114 return -1;
115 }
116
117 ret = lxc_switch_uid_gid(0, 0);
118 if (ret < 0)
119 return -1;
120
121 ret = lxc_setgroups(0, NULL);
122 if (ret < 0)
123 return -1;
124
125 src = lxc_storage_get_path(orig->dest, orig->type);
126 dest = lxc_storage_get_path(new->dest, new->type);
127
128 ret = lxc_rsync_exec(src, dest);
129 if (ret < 0) {
130 ERROR("Failed to rsync from \"%s\" into \"%s\"", src, dest);
131 return -1;
132 }
133
134 return 0;
135 }