]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/rsync.c
fd: only add valid fd to mainloop
[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 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <grp.h>
28 #include <sched.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/mount.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include "config.h"
38 #include "log.h"
39 #include "rsync.h"
40 #include "storage.h"
41 #include "syscall_wrappers.h"
42 #include "utils.h"
43
44 lxc_log_define(rsync, lxc);
45
46 int lxc_storage_rsync_exec_wrapper(void *data)
47 {
48 struct rsync_data *arg = data;
49 return lxc_rsync(arg);
50 }
51
52 int lxc_rsync_exec_wrapper(void *data)
53 {
54 struct rsync_data_char *args = data;
55
56 if (!lxc_switch_uid_gid(0, 0))
57 return -1;
58
59 if (!lxc_setgroups(0, NULL))
60 return -1;
61
62 return lxc_rsync_exec(args->src, args->dest);
63 }
64
65 int lxc_rsync_exec(const char *src, const char *dest)
66 {
67 int ret;
68 size_t l;
69 char *s;
70
71 l = strlen(src) + 2;
72 s = malloc(l);
73 if (!s)
74 return -1;
75
76 ret = snprintf(s, l, "%s", src);
77 if (ret < 0 || (size_t)ret >= l) {
78 free(s);
79 return -1;
80 }
81
82 s[l - 2] = '/';
83 s[l - 1] = '\0';
84
85 execlp("rsync", "rsync", "-aHXS", "--delete", s, dest, (char *)NULL);
86 free(s);
87 return -1;
88 }
89
90 int lxc_rsync(struct rsync_data *data)
91 {
92 int ret;
93 const char *dest, *src;
94 struct lxc_storage *orig = data->orig, *new = data->new;
95
96 ret = unshare(CLONE_NEWNS);
97 if (ret < 0) {
98 SYSERROR("Failed to unshare CLONE_NEWNS");
99 return -1;
100 }
101
102 ret = detect_shared_rootfs();
103 if (ret) {
104 ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
105 if (ret < 0)
106 SYSERROR("Failed to make \"/\" a slave mount");
107 }
108
109 ret = orig->ops->mount(orig);
110 if (ret < 0) {
111 ERROR("Failed mounting \"%s\" on \"%s\"", orig->src, orig->dest);
112 return -1;
113 }
114
115 ret = new->ops->mount(new);
116 if (ret < 0) {
117 ERROR("Failed mounting \"%s\" onto \"%s\"", new->src, new->dest);
118 return -1;
119 }
120
121 if (!lxc_switch_uid_gid(0, 0))
122 return -1;
123
124 if (!lxc_setgroups(0, NULL))
125 return -1;
126
127 src = lxc_storage_get_path(orig->dest, orig->type);
128 dest = lxc_storage_get_path(new->dest, new->type);
129
130 ret = lxc_rsync_exec(src, dest);
131 if (ret < 0) {
132 ERROR("Failed to rsync from \"%s\" into \"%s\"", src, dest);
133 return -1;
134 }
135
136 return 0;
137 }