]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/rsync.c
tree-wide: fix includes to fix bionic builds
[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 "utils.h"
42
43 lxc_log_define(rsync, lxc);
44
45 int lxc_storage_rsync_exec_wrapper(void *data)
46 {
47 struct rsync_data *arg = data;
48 return lxc_rsync(arg);
49 }
50
51 int lxc_rsync_exec_wrapper(void *data)
52 {
53 struct rsync_data_char *args = data;
54
55 if (!lxc_switch_uid_gid(0, 0))
56 return -1;
57
58 if (!lxc_setgroups(0, NULL))
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 free(s);
78 return -1;
79 }
80
81 s[l - 2] = '/';
82 s[l - 1] = '\0';
83
84 execlp("rsync", "rsync", "-aHXS", "--delete", s, dest, (char *)NULL);
85 free(s);
86 return -1;
87 }
88
89 int lxc_rsync(struct rsync_data *data)
90 {
91 int ret;
92 const char *dest, *src;
93 struct lxc_storage *orig = data->orig, *new = data->new;
94
95 ret = unshare(CLONE_NEWNS);
96 if (ret < 0) {
97 SYSERROR("Failed to unshare CLONE_NEWNS");
98 return -1;
99 }
100
101 ret = detect_shared_rootfs();
102 if (ret) {
103 ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
104 if (ret < 0)
105 SYSERROR("Failed to make \"/\" a slave mount");
106 }
107
108 ret = orig->ops->mount(orig);
109 if (ret < 0) {
110 ERROR("Failed mounting \"%s\" on \"%s\"", orig->src, orig->dest);
111 return -1;
112 }
113
114 ret = new->ops->mount(new);
115 if (ret < 0) {
116 ERROR("Failed mounting \"%s\" onto \"%s\"", new->src, new->dest);
117 return -1;
118 }
119
120 if (!lxc_switch_uid_gid(0, 0))
121 return -1;
122
123 if (!lxc_setgroups(0, NULL))
124 return -1;
125
126 src = lxc_storage_get_path(orig->dest, orig->type);
127 dest = lxc_storage_get_path(new->dest, new->type);
128
129 ret = lxc_rsync_exec(src, dest);
130 if (ret < 0) {
131 ERROR("Failed to rsync from \"%s\" into \"%s\"", src, dest);
132 return -1;
133 }
134
135 return 0;
136 }