]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxccontainer.c
lxc-start-ephemeral: handle the overlayfs workdir option (v2)
[mirror_lxc.git] / src / lxc / lxccontainer.c
CommitLineData
72d0e1cb
SG
1/* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
5 *
d75462e4
SH
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
72d0e1cb
SG
19 */
20
9be53773 21#define _GNU_SOURCE
148a9d27 22#include <assert.h>
a0e93eeb 23#include <stdarg.h>
71454076 24#include <pthread.h>
9be53773
SH
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/wait.h>
4de2791f 28#include <sys/mount.h>
9be53773 29#include <errno.h>
93dc5327 30#include <fcntl.h>
9be53773 31#include <sched.h>
f5dd1d53 32#include <dirent.h>
f2363e38
ÇO
33#include <sched.h>
34#include <arpa/inet.h>
35#include <libgen.h>
d659597e 36#include <stdint.h>
c476bdce 37#include <grp.h>
5f7eba0b 38#include <sys/syscall.h>
f2363e38
ÇO
39
40#include <lxc/lxccontainer.h>
41#include <lxc/version.h>
e58fae8f 42#include <lxc/network.h>
f2363e38 43
9be53773 44#include "config.h"
72d0e1cb
SG
45#include "lxc.h"
46#include "state.h"
72d0e1cb 47#include "conf.h"
72d0e1cb 48#include "confile.h"
b5159817 49#include "console.h"
72d0e1cb
SG
50#include "cgroup.h"
51#include "commands.h"
52#include "log.h"
9be53773 53#include "bdev.h"
6a44839f 54#include "utils.h"
a0e93eeb 55#include "attach.h"
f2363e38
ÇO
56#include "monitor.h"
57#include "namespace.h"
fed29fad 58#include "network.h"
95ee490b 59#include "lxclock.h"
735f2c6e 60#include "sync.h"
4ba0d9af
SG
61
62#if HAVE_IFADDRS_H
9c83a661 63#include <ifaddrs.h>
4ba0d9af
SG
64#else
65#include <../include/ifaddrs.h>
66#endif
72d0e1cb 67
a9a0ed90
ÇO
68#define MAX_BUFFER 4096
69
c868b261
ÇO
70#define NOT_SUPPORTED_ERROR "the requested function %s is not currently supported with unprivileged containers"
71
5f7eba0b
SG
72/* Define faccessat() if missing from the C library */
73#ifndef HAVE_FACCESSAT
74static int faccessat(int __fd, const char *__file, int __type, int __flag)
75{
76#ifdef __NR_faccessat
77return syscall(__NR_faccessat, __fd, __file, __type, __flag);
78#else
79errno = ENOSYS;
80return -1;
81#endif
82}
83#endif
84
85
72d0e1cb
SG
86lxc_log_define(lxc_container, lxc);
87
a41f104b
SH
88static bool config_file_exists(const char *lxcpath, const char *cname)
89{
90 /* $lxcpath + '/' + $cname + '/config' + \0 */
91 int ret, len = strlen(lxcpath) + strlen(cname) + 9;
92 char *fname = alloca(len);
93
94 ret = snprintf(fname, len, "%s/%s/config", lxcpath, cname);
95 if (ret < 0 || ret >= len)
96 return false;
97
98 return file_exists(fname);
99}
100
3e625e2d
SH
101/*
102 * A few functions to help detect when a container creation failed.
103 * If a container creation was killed partway through, then trying
104 * to actually start that container could harm the host. We detect
105 * this by creating a 'partial' file under the container directory,
106 * and keeping an advisory lock. When container creation completes,
107 * we remove that file. When we load or try to start a container, if
108 * we find that file, without a flock, we remove the container.
109 */
74a3920a 110static int ongoing_create(struct lxc_container *c)
3e625e2d
SH
111{
112 int len = strlen(c->config_path) + strlen(c->name) + 10;
113 char *path = alloca(len);
114 int fd, ret;
93dc5327
SH
115 struct flock lk;
116
3e625e2d
SH
117 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
118 if (ret < 0 || ret >= len) {
119 ERROR("Error writing partial pathname");
120 return -1;
121 }
122
123 if (!file_exists(path))
124 return 0;
025ed0f3 125 fd = open(path, O_RDWR);
025ed0f3 126 if (fd < 0) {
3e625e2d
SH
127 // give benefit of the doubt
128 SYSERROR("Error opening partial file");
3e625e2d
SH
129 return 0;
130 }
93dc5327
SH
131 lk.l_type = F_WRLCK;
132 lk.l_whence = SEEK_SET;
133 lk.l_start = 0;
134 lk.l_len = 0;
135 lk.l_pid = -1;
136 if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) {
3e625e2d
SH
137 // create is still ongoing
138 close(fd);
3e625e2d
SH
139 return 1;
140 }
141 // create completed but partial is still there.
142 close(fd);
3e625e2d
SH
143 return 2;
144}
145
74a3920a 146static int create_partial(struct lxc_container *c)
3e625e2d
SH
147{
148 // $lxcpath + '/' + $name + '/partial' + \0
149 int len = strlen(c->config_path) + strlen(c->name) + 10;
150 char *path = alloca(len);
151 int fd, ret;
93dc5327
SH
152 struct flock lk;
153
3e625e2d
SH
154 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
155 if (ret < 0 || ret >= len) {
156 ERROR("Error writing partial pathname");
157 return -1;
158 }
93dc5327 159 if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) {
3e625e2d 160 SYSERROR("Erorr creating partial file");
3e625e2d
SH
161 return -1;
162 }
93dc5327
SH
163 lk.l_type = F_WRLCK;
164 lk.l_whence = SEEK_SET;
165 lk.l_start = 0;
166 lk.l_len = 0;
167 if (fcntl(fd, F_SETLKW, &lk) < 0) {
3e625e2d
SH
168 SYSERROR("Error locking partial file %s", path);
169 close(fd);
3e625e2d
SH
170 return -1;
171 }
3e625e2d
SH
172
173 return fd;
174}
175
74a3920a 176static void remove_partial(struct lxc_container *c, int fd)
3e625e2d
SH
177{
178 // $lxcpath + '/' + $name + '/partial' + \0
179 int len = strlen(c->config_path) + strlen(c->name) + 10;
180 char *path = alloca(len);
181 int ret;
182
183 close(fd);
184 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
185 if (ret < 0 || ret >= len) {
186 ERROR("Error writing partial pathname");
187 return;
188 }
3e625e2d
SH
189 if (unlink(path) < 0)
190 SYSERROR("Error unlink partial file %s", path);
3e625e2d
SH
191}
192
72d0e1cb 193/* LOCKING
3bc449ed
SH
194 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
195 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
196 * container configuration file.
197 * The container_disk_lock also takes the container_mem_lock.
198 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
72d0e1cb
SG
199 * NOTHING mutexes two independent programs with their own struct
200 * lxc_container for the same c->name, between API calls. For instance,
201 * c->config_read(); c->start(); Between those calls, data on disk
202 * could change (which shouldn't bother the caller unless for instance
203 * the rootfs get moved). c->config_read(); update; c->config_write();
204 * Two such updaters could race. The callers should therefore check their
205 * results. Trying to prevent that would necessarily expose us to deadlocks
206 * due to hung callers. So I prefer to keep the locks only within our own
207 * functions, not across functions.
208 *
3bc449ed 209 * If you're going to clone while holding a lxccontainer, increment
72d0e1cb
SG
210 * c->numthreads (under privlock) before forking. When deleting,
211 * decrement numthreads under privlock, then if it hits 0 you can delete.
212 * Do not ever use a lxccontainer whose numthreads you did not bump.
213 */
214
215static void lxc_container_free(struct lxc_container *c)
216{
217 if (!c)
218 return;
219
220 if (c->configfile) {
221 free(c->configfile);
222 c->configfile = NULL;
223 }
224 if (c->error_string) {
225 free(c->error_string);
226 c->error_string = NULL;
227 }
d95db067 228 if (c->slock) {
df271a59 229 lxc_putlock(c->slock);
d95db067
DE
230 c->slock = NULL;
231 }
72d0e1cb 232 if (c->privlock) {
df271a59 233 lxc_putlock(c->privlock);
72d0e1cb
SG
234 c->privlock = NULL;
235 }
236 if (c->name) {
237 free(c->name);
238 c->name = NULL;
239 }
d95db067
DE
240 if (c->lxc_conf) {
241 lxc_conf_free(c->lxc_conf);
242 c->lxc_conf = NULL;
243 }
2a59a681
SH
244 if (c->config_path) {
245 free(c->config_path);
246 c->config_path = NULL;
247 }
72cf75fa 248
72d0e1cb
SG
249 free(c);
250}
251
43d1aa34
SH
252/*
253 * Consider the following case:
254freer | racing get()er
255==================================================================
256lxc_container_put() | lxc_container_get()
257\ lxclock(c->privlock) | c->numthreads < 1? (no)
258\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
259\ lxcunlock() | \
260\ lxc_container_free() | \ lxclock() returns
261 | \ c->numthreads < 1 -> return 0
262\ \ (free stuff) |
263\ \ sem_destroy(privlock) |
264
265 * When the get()er checks numthreads the first time, one of the following
266 * is true:
267 * 1. freer has set numthreads = 0. get() returns 0
268 * 2. freer is between lxclock and setting numthreads to 0. get()er will
269 * sem_wait on privlock, get lxclock after freer() drops it, then see
270 * numthreads is 0 and exit without touching lxclock again..
271 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
272 * will see --numthreads = 1 and not call lxc_container_free().
273*/
274
72d0e1cb
SG
275int lxc_container_get(struct lxc_container *c)
276{
277 if (!c)
278 return 0;
279
43d1aa34
SH
280 // if someone else has already started freeing the container, don't
281 // try to take the lock, which may be invalid
282 if (c->numthreads < 1)
283 return 0;
284
5cee8c50 285 if (container_mem_lock(c))
72d0e1cb
SG
286 return 0;
287 if (c->numthreads < 1) {
288 // bail without trying to unlock, bc the privlock is now probably
289 // in freed memory
290 return 0;
291 }
292 c->numthreads++;
5cee8c50 293 container_mem_unlock(c);
72d0e1cb
SG
294 return 1;
295}
296
297int lxc_container_put(struct lxc_container *c)
298{
299 if (!c)
300 return -1;
5cee8c50 301 if (container_mem_lock(c))
72d0e1cb
SG
302 return -1;
303 if (--c->numthreads < 1) {
5cee8c50 304 container_mem_unlock(c);
72d0e1cb
SG
305 lxc_container_free(c);
306 return 1;
307 }
5cee8c50 308 container_mem_unlock(c);
72d0e1cb
SG
309 return 0;
310}
311
72d0e1cb
SG
312static bool lxcapi_is_defined(struct lxc_container *c)
313{
314 struct stat statbuf;
315 bool ret = false;
316 int statret;
317
318 if (!c)
319 return false;
320
5cee8c50 321 if (container_mem_lock(c))
72d0e1cb
SG
322 return false;
323 if (!c->configfile)
324 goto out;
325 statret = stat(c->configfile, &statbuf);
326 if (statret != 0)
327 goto out;
328 ret = true;
329
330out:
5cee8c50 331 container_mem_unlock(c);
72d0e1cb
SG
332 return ret;
333}
334
335static const char *lxcapi_state(struct lxc_container *c)
336{
72d0e1cb
SG
337 lxc_state_t s;
338
339 if (!c)
340 return NULL;
13f5be62 341 s = lxc_getstate(c->name, c->config_path);
39dc698c 342 return lxc_state2str(s);
72d0e1cb
SG
343}
344
39dc698c 345static bool is_stopped(struct lxc_container *c)
794dd120
SH
346{
347 lxc_state_t s;
13f5be62 348 s = lxc_getstate(c->name, c->config_path);
794dd120
SH
349 return (s == STOPPED);
350}
351
72d0e1cb
SG
352static bool lxcapi_is_running(struct lxc_container *c)
353{
354 const char *s;
355
356 if (!c)
357 return false;
358 s = lxcapi_state(c);
359 if (!s || strcmp(s, "STOPPED") == 0)
360 return false;
361 return true;
362}
363
364static bool lxcapi_freeze(struct lxc_container *c)
365{
366 int ret;
367 if (!c)
368 return false;
369
9123e471 370 ret = lxc_freeze(c->name, c->config_path);
72d0e1cb
SG
371 if (ret)
372 return false;
373 return true;
374}
375
376static bool lxcapi_unfreeze(struct lxc_container *c)
377{
378 int ret;
379 if (!c)
380 return false;
381
9123e471 382 ret = lxc_unfreeze(c->name, c->config_path);
72d0e1cb
SG
383 if (ret)
384 return false;
385 return true;
386}
387
b5159817 388static int lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd)
0115f8fd
DE
389{
390 int ttyfd;
391 if (!c)
392 return -1;
393
b5159817 394 ttyfd = lxc_console_getfd(c, ttynum, masterfd);
0115f8fd
DE
395 return ttyfd;
396}
397
b5159817
DE
398static int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd,
399 int stdoutfd, int stderrfd, int escape)
400{
401 return lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape);
402}
403
72d0e1cb
SG
404static pid_t lxcapi_init_pid(struct lxc_container *c)
405{
72d0e1cb
SG
406 if (!c)
407 return -1;
408
5cee8c50 409 return lxc_cmd_get_init_pid(c->name, c->config_path);
72d0e1cb
SG
410}
411
12a50cc6 412static bool load_config_locked(struct lxc_container *c, const char *fname)
8eb5694b
SH
413{
414 if (!c->lxc_conf)
415 c->lxc_conf = lxc_conf_init();
6b0d5538
SH
416 if (!c->lxc_conf)
417 return false;
d08779d4
SH
418 if (lxc_config_read(fname, c->lxc_conf, false) != 0)
419 return false;
d08779d4 420 return true;
8eb5694b
SH
421}
422
12a50cc6 423static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
72d0e1cb 424{
39dc698c
SH
425 bool ret = false, need_disklock = false;
426 int lret;
12a50cc6 427 const char *fname;
72d0e1cb
SG
428 if (!c)
429 return false;
430
431 fname = c->configfile;
432 if (alt_file)
433 fname = alt_file;
434 if (!fname)
435 return false;
39dc698c
SH
436 /*
437 * If we're reading something other than the container's config,
438 * we only need to lock the in-memory container. If loading the
439 * container's config file, take the disk lock.
440 */
441 if (strcmp(fname, c->configfile) == 0)
442 need_disklock = true;
443
444 if (need_disklock)
445 lret = container_disk_lock(c);
446 else
447 lret = container_mem_lock(c);
448 if (lret)
72d0e1cb 449 return false;
39dc698c 450
8eb5694b 451 ret = load_config_locked(c, fname);
39dc698c
SH
452
453 if (need_disklock)
454 container_disk_unlock(c);
455 else
456 container_mem_unlock(c);
72d0e1cb
SG
457 return ret;
458}
459
540f932a 460static bool lxcapi_want_daemonize(struct lxc_container *c, bool state)
72d0e1cb 461{
497a2995 462 if (!c || !c->lxc_conf)
540f932a 463 return false;
f02abefe 464 if (container_mem_lock(c)) {
3bc449ed 465 ERROR("Error getting mem lock");
540f932a 466 return false;
3bc449ed 467 }
a2739df5 468 c->daemonize = state;
3bc449ed 469 container_mem_unlock(c);
540f932a 470 return true;
72d0e1cb
SG
471}
472
540f932a 473static bool lxcapi_want_close_all_fds(struct lxc_container *c, bool state)
130a1888
ÇO
474{
475 if (!c || !c->lxc_conf)
49badbbe 476 return false;
130a1888
ÇO
477 if (container_mem_lock(c)) {
478 ERROR("Error getting mem lock");
49badbbe 479 return false;
130a1888 480 }
540f932a 481 c->lxc_conf->close_all_fds = state;
130a1888 482 container_mem_unlock(c);
49badbbe 483 return true;
130a1888
ÇO
484}
485
12a50cc6 486static bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
7a44c8b4
SG
487{
488 int ret;
489
490 if (!c)
491 return false;
492
67e571de 493 ret = lxc_wait(c->name, state, timeout, c->config_path);
7a44c8b4
SG
494 return ret == 0;
495}
496
497
03f064ff 498static bool wait_on_daemonized_start(struct lxc_container *c, int pid)
7a44c8b4
SG
499{
500 /* we'll probably want to make this timeout configurable? */
697fa639 501 int timeout = 5, ret, status;
7a44c8b4 502
697fa639
SH
503 /*
504 * our child is going to fork again, then exit. reap the
505 * child
506 */
03f064ff 507 ret = waitpid(pid, &status, 0);
697fa639
SH
508 if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
509 DEBUG("failed waiting for first dual-fork child");
7a44c8b4
SG
510 return lxcapi_wait(c, "RUNNING", timeout);
511}
512
2d834aa8
SH
513static bool am_single_threaded(void)
514{
515 struct dirent dirent, *direntp;
516 DIR *dir;
517 int count=0;
518
2d834aa8 519 dir = opendir("/proc/self/task");
2d834aa8
SH
520 if (!dir) {
521 INFO("failed to open /proc/self/task");
522 return false;
523 }
524
525 while (!readdir_r(dir, &dirent, &direntp)) {
526 if (!direntp)
527 break;
528
529 if (!strcmp(direntp->d_name, "."))
530 continue;
531
532 if (!strcmp(direntp->d_name, ".."))
533 continue;
534 if (++count > 1)
535 break;
536 }
2d834aa8 537 closedir(dir);
2d834aa8
SH
538 return count == 1;
539}
540
72d0e1cb
SG
541/*
542 * I can't decide if it'd be more convenient for callers if we accept '...',
543 * or a null-terminated array (i.e. execl vs execv)
544 */
12a50cc6 545static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
72d0e1cb
SG
546{
547 int ret;
548 struct lxc_conf *conf;
540f932a 549 bool daemonize = false;
6eaac303 550 FILE *pid_fp = NULL;
72d0e1cb
SG
551 char *default_args[] = {
552 "/sbin/init",
13aad0ae 553 NULL,
72d0e1cb 554 };
67c660d0 555 char *init_cmd[2];
72d0e1cb
SG
556
557 /* container exists */
558 if (!c)
559 return false;
560 /* container has been setup */
561 if (!c->lxc_conf)
562 return false;
563
3e625e2d
SH
564 if ((ret = ongoing_create(c)) < 0) {
565 ERROR("Error checking for incomplete creation");
566 return false;
567 }
568 if (ret == 2) {
569 ERROR("Error: %s creation was not completed", c->name);
570 c->destroy(c);
571 return false;
572 } else if (ret == 1) {
573 ERROR("Error: creation of %s is ongoing", c->name);
574 return false;
575 }
576
72d0e1cb
SG
577 /* is this app meant to be run through lxcinit, as in lxc-execute? */
578 if (useinit && !argv)
579 return false;
580
5cee8c50 581 if (container_mem_lock(c))
72d0e1cb
SG
582 return false;
583 conf = c->lxc_conf;
584 daemonize = c->daemonize;
5cee8c50 585 container_mem_unlock(c);
72d0e1cb
SG
586
587 if (useinit) {
13f5be62 588 ret = lxc_execute(c->name, argv, 1, conf, c->config_path);
72d0e1cb
SG
589 return ret == 0 ? true : false;
590 }
591
67c660d0
SG
592 if (!argv) {
593 if (conf->init_cmd) {
594 init_cmd[0] = conf->init_cmd;
595 init_cmd[1] = NULL;
596 argv = init_cmd;
597 }
598 else
599 argv = default_args;
600 }
72d0e1cb
SG
601
602 /*
603 * say, I'm not sure - what locks do we want here? Any?
604 * Is liblxc's locking enough here to protect the on disk
605 * container? We don't want to exclude things like lxc_info
606 * while container is running...
607 */
608 if (daemonize) {
db2930e1 609 conf->close_all_fds = 1;
e51d4895 610 lxc_monitord_spawn(c->config_path);
71454076 611
72d0e1cb 612 pid_t pid = fork();
844f7a38 613 if (pid < 0)
72d0e1cb 614 return false;
6eaac303
QH
615
616 if (pid != 0) {
617 /* Set to NULL because we don't want father unlink
618 * the PID file, child will do the free and unlink.
619 */
620 c->pidfile = NULL;
03f064ff 621 return wait_on_daemonized_start(c, pid);
6eaac303 622 }
025ed0f3 623
697fa639
SH
624 /* second fork to be reparented by init */
625 pid = fork();
626 if (pid < 0) {
627 SYSERROR("Error doing dual-fork");
628 return false;
629 }
630 if (pid != 0)
631 exit(0);
72d0e1cb 632 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
c278cef2
SH
633 if (chdir("/")) {
634 SYSERROR("Error chdir()ing to /.");
635 return false;
636 }
9581b4b7 637 lxc_check_inherited(conf, -1);
72d0e1cb
SG
638 close(0);
639 close(1);
640 close(2);
eddaaafd 641 open("/dev/zero", O_RDONLY);
72d0e1cb
SG
642 open("/dev/null", O_RDWR);
643 open("/dev/null", O_RDWR);
644 setsid();
2d834aa8
SH
645 } else {
646 if (!am_single_threaded()) {
647 ERROR("Cannot start non-daemonized container when threaded");
648 return false;
649 }
72d0e1cb
SG
650 }
651
6eaac303
QH
652 /* We need to write PID file after daeminize, so we always
653 * write the right PID.
654 */
655 if (c->pidfile) {
656 pid_fp = fopen(c->pidfile, "w");
657 if (pid_fp == NULL) {
658 SYSERROR("Failed to create pidfile '%s' for '%s'",
659 c->pidfile, c->name);
660 return false;
661 }
662
663 if (fprintf(pid_fp, "%d\n", getpid()) < 0) {
664 SYSERROR("Failed to write '%s'", c->pidfile);
665 fclose(pid_fp);
666 pid_fp = NULL;
667 return false;
668 }
669
670 fclose(pid_fp);
671 pid_fp = NULL;
672 }
673
72d0e1cb
SG
674reboot:
675 conf->reboot = 0;
13f5be62 676 ret = lxc_start(c->name, argv, conf, c->config_path);
d4ef230c 677 c->error_num = ret;
72d0e1cb
SG
678
679 if (conf->reboot) {
680 INFO("container requested reboot");
681 conf->reboot = 0;
72d0e1cb
SG
682 goto reboot;
683 }
684
487d8008
QH
685 if (c->pidfile) {
686 unlink(c->pidfile);
687 free(c->pidfile);
688 c->pidfile = NULL;
689 }
690
844f7a38 691 if (daemonize)
05e5d7dc 692 exit (ret == 0 ? true : false);
844f7a38 693 else
05e5d7dc 694 return (ret == 0 ? true : false);
72d0e1cb
SG
695}
696
697/*
698 * note there MUST be an ending NULL
699 */
700static bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
701{
702 va_list ap;
a0e93eeb 703 char **inargs = NULL;
72d0e1cb
SG
704 bool bret = false;
705
706 /* container exists */
707 if (!c)
708 return false;
709
72d0e1cb 710 va_start(ap, useinit);
a0e93eeb 711 inargs = lxc_va_arg_list_to_argv(ap, 0, 1);
72d0e1cb
SG
712 va_end(ap);
713
a0e93eeb
CS
714 if (!inargs) {
715 ERROR("Memory allocation error.");
716 goto out;
72d0e1cb
SG
717 }
718
a0e93eeb
CS
719 /* pass NULL if no arguments were supplied */
720 bret = lxcapi_start(c, useinit, *inargs ? inargs : NULL);
72d0e1cb
SG
721
722out:
723 if (inargs) {
4e03ae57
DE
724 char **arg;
725 for (arg = inargs; *arg; arg++)
726 free(*arg);
72d0e1cb
SG
727 free(inargs);
728 }
729
730 return bret;
731}
732
733static bool lxcapi_stop(struct lxc_container *c)
734{
735 int ret;
736
737 if (!c)
738 return false;
739
ef6e34ee 740 ret = lxc_cmd_stop(c->name, c->config_path);
72d0e1cb
SG
741
742 return ret == 0;
72d0e1cb
SG
743}
744
d5752559
SH
745static int do_create_container_dir(const char *path, struct lxc_conf *conf)
746{
747 int ret = -1, lasterr;
748 char *p = alloca(strlen(path)+1);
749 mode_t mask = umask(0002);
750 ret = mkdir(path, 0770);
751 lasterr = errno;
752 umask(mask);
753 errno = lasterr;
754 if (ret) {
755 if (errno == EEXIST)
756 ret = 0;
757 else {
758 SYSERROR("failed to create container path %s", path);
759 return -1;
760 }
761 }
762 strcpy(p, path);
763 if (!lxc_list_empty(&conf->id_map) && chown_mapped_root(p, conf) != 0) {
764 ERROR("Failed to chown container dir");
765 ret = -1;
766 }
767 return ret;
768}
769
72d0e1cb
SG
770/*
771 * create the standard expected container dir
772 */
773static bool create_container_dir(struct lxc_container *c)
774{
775 char *s;
776 int len, ret;
777
2a59a681 778 len = strlen(c->config_path) + strlen(c->name) + 2;
72d0e1cb
SG
779 s = malloc(len);
780 if (!s)
781 return false;
2a59a681 782 ret = snprintf(s, len, "%s/%s", c->config_path, c->name);
72d0e1cb
SG
783 if (ret < 0 || ret >= len) {
784 free(s);
785 return false;
786 }
d5752559 787 ret = do_create_container_dir(s, c->lxc_conf);
72d0e1cb
SG
788 free(s);
789 return ret == 0;
790}
791
1897e3bc
SH
792static const char *lxcapi_get_config_path(struct lxc_container *c);
793static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
794
72d0e1cb 795/*
1897e3bc
SH
796 * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
797 * it returns a mounted bdev on success, NULL on error.
72d0e1cb 798 */
1897e3bc
SH
799static struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
800 struct bdev_specs *specs)
801{
802 char *dest;
1897e3bc
SH
803 size_t len;
804 struct bdev *bdev;
805 int ret;
806
cd219ae6
SY
807 /* rootfs.path or lxcpath/lxcname/rootfs */
808 if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0) {
cf465fe4
SH
809 const char *rpath = c->lxc_conf->rootfs.path;
810 len = strlen(rpath) + 1;
cd219ae6 811 dest = alloca(len);
cf465fe4 812 ret = snprintf(dest, len, "%s", rpath);
cd219ae6 813 } else {
cf465fe4 814 const char *lxcpath = lxcapi_get_config_path(c);
cd219ae6
SY
815 len = strlen(c->name) + strlen(lxcpath) + 9;
816 dest = alloca(len);
817 ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name);
818 }
1897e3bc
SH
819 if (ret < 0 || ret >= len)
820 return NULL;
821
822 bdev = bdev_create(dest, type, c->name, specs);
d44e88c2 823 if (!bdev) {
959aee9c 824 ERROR("Failed to create backing store type %s", type);
1897e3bc 825 return NULL;
d44e88c2
SH
826 }
827
1897e3bc 828 lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
cf3ef16d
SH
829
830 /* if we are not root, chown the rootfs dir to root in the
831 * target uidmap */
832
0e6e3a41 833 if (geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) {
c4d10a05 834 if (chown_mapped_root(bdev->dest, c->lxc_conf) < 0) {
959aee9c 835 ERROR("Error chowning %s to container root", bdev->dest);
97e9cfa0 836 suggest_default_idmap();
cf3ef16d
SH
837 bdev_put(bdev);
838 return NULL;
839 }
840 }
841
1897e3bc
SH
842 return bdev;
843}
844
cbee8106
SH
845/*
846 * Given the '-t' template option to lxc-create, figure out what to
847 * do. If the template is a full executable path, use that. If it
85db5535
DE
848 * is something like 'sshd', then return $templatepath/lxc-sshd.
849 * On success return the template, on error return NULL.
cbee8106 850 */
85db5535 851static char *get_template_path(const char *t)
cbee8106
SH
852{
853 int ret, len;
854 char *tpath;
855
cbee8106
SH
856 if (t[0] == '/' && access(t, X_OK) == 0) {
857 tpath = strdup(t);
cbee8106
SH
858 return tpath;
859 }
860
861 len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
862 tpath = malloc(len);
863 if (!tpath)
85db5535 864 return NULL;
cbee8106
SH
865 ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
866 if (ret < 0 || ret >= len) {
867 free(tpath);
85db5535 868 return NULL;
cbee8106
SH
869 }
870 if (access(tpath, X_OK) < 0) {
959aee9c 871 SYSERROR("bad template: %s", t);
cbee8106 872 free(tpath);
85db5535 873 return NULL;
cbee8106
SH
874 }
875
876 return tpath;
877}
878
96b3cb40 879static char *lxcbasename(char *path)
72d0e1cb 880{
96b3cb40
SH
881 char *p = path + strlen(path) - 1;
882 while (*p != '/' && p > path)
883 p--;
884 return p;
885}
72d0e1cb 886
dc23c1c8 887static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
96b3cb40
SH
888 char *const argv[])
889{
890 pid_t pid;
72d0e1cb 891
72d0e1cb 892 if (!tpath)
96b3cb40 893 return true;
72d0e1cb
SG
894
895 pid = fork();
896 if (pid < 0) {
959aee9c 897 SYSERROR("failed to fork task for container creation template");
96b3cb40 898 return false;
72d0e1cb
SG
899 }
900
901 if (pid == 0) { // child
1897e3bc
SH
902 char *patharg, *namearg, *rootfsarg, *src;
903 struct bdev *bdev = NULL;
72d0e1cb 904 int i;
96b3cb40
SH
905 int ret, len, nargs = 0;
906 char **newargv;
cf3ef16d 907 struct lxc_conf *conf = c->lxc_conf;
72d0e1cb 908
dc23c1c8
SH
909 if (quiet) {
910 close(0);
911 close(1);
912 close(2);
913 open("/dev/zero", O_RDONLY);
914 open("/dev/null", O_RDWR);
915 open("/dev/null", O_RDWR);
916 }
1897e3bc
SH
917
918 src = c->lxc_conf->rootfs.path;
919 /*
1f92162d 920 * for an overlay create, what the user wants is the template to fill
1897e3bc
SH
921 * in what will become the readonly lower layer. So don't mount for
922 * the template
923 */
1f92162d
SG
924 if (strncmp(src, "overlayfs:", 10) == 0)
925 src = overlay_getlower(src+10);
926 if (strncmp(src, "aufs:", 5) == 0)
927 src = overlay_getlower(src+5);
928
76a26f55 929 bdev = bdev_init(c->lxc_conf, src, c->lxc_conf->rootfs.mount, NULL);
1897e3bc
SH
930 if (!bdev) {
931 ERROR("Error opening rootfs");
932 exit(1);
933 }
934
4de2791f 935 if (geteuid() == 0) {
cf3ef16d
SH
936 if (unshare(CLONE_NEWNS) < 0) {
937 ERROR("error unsharing mounts");
938 exit(1);
939 }
4de2791f 940 if (detect_shared_rootfs()) {
c597baa8 941 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
4de2791f
SH
942 SYSERROR("Failed to make / rslave to run template");
943 ERROR("Continuing...");
944 }
945 }
946 }
2659c7cb 947 if (strcmp(bdev->type, "dir") && strcmp(bdev->type, "btrfs")) {
4de2791f 948 if (geteuid() != 0) {
2659c7cb 949 ERROR("non-root users can only create btrfs and directory-backed containers");
4de2791f
SH
950 exit(1);
951 }
cf3ef16d
SH
952 if (bdev->ops->mount(bdev) < 0) {
953 ERROR("Error mounting rootfs");
954 exit(1);
955 }
956 } else { // TODO come up with a better way here!
957 if (bdev->dest)
958 free(bdev->dest);
959 bdev->dest = strdup(bdev->src);
1897e3bc
SH
960 }
961
72d0e1cb
SG
962 /*
963 * create our new array, pre-pend the template name and
964 * base args
965 */
966 if (argv)
1897e3bc 967 for (nargs = 0; argv[nargs]; nargs++) ;
6849cb5b 968 nargs += 4; // template, path, rootfs and name args
cf3ef16d 969
72d0e1cb
SG
970 newargv = malloc(nargs * sizeof(*newargv));
971 if (!newargv)
972 exit(1);
96b3cb40 973 newargv[0] = lxcbasename(tpath);
72d0e1cb 974
2a59a681 975 len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
72d0e1cb
SG
976 patharg = malloc(len);
977 if (!patharg)
978 exit(1);
2a59a681 979 ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
72d0e1cb
SG
980 if (ret < 0 || ret >= len)
981 exit(1);
982 newargv[1] = patharg;
983 len = strlen("--name=") + strlen(c->name) + 1;
984 namearg = malloc(len);
985 if (!namearg)
986 exit(1);
987 ret = snprintf(namearg, len, "--name=%s", c->name);
988 if (ret < 0 || ret >= len)
989 exit(1);
990 newargv[2] = namearg;
991
1897e3bc
SH
992 len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
993 rootfsarg = malloc(len);
994 if (!rootfsarg)
995 exit(1);
996 ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
997 if (ret < 0 || ret >= len)
998 exit(1);
999 newargv[3] = rootfsarg;
1000
72d0e1cb
SG
1001 /* add passed-in args */
1002 if (argv)
1897e3bc
SH
1003 for (i = 4; i < nargs; i++)
1004 newargv[i] = argv[i-4];
72d0e1cb
SG
1005
1006 /* add trailing NULL */
1007 nargs++;
1008 newargv = realloc(newargv, nargs * sizeof(*newargv));
1009 if (!newargv)
1010 exit(1);
1011 newargv[nargs - 1] = NULL;
1012
cf3ef16d
SH
1013 /*
1014 * If we're running the template in a mapped userns, then
1015 * we prepend the template command with:
1016 * lxc-usernsexec <-m map1> ... <-m mapn> --
57d116ab
SH
1017 * and we append "--mapped-uid x", where x is the mapped uid
1018 * for our geteuid()
cf3ef16d 1019 */
0e6e3a41 1020 if (!lxc_list_empty(&conf->id_map)) {
cf3ef16d 1021 int n2args = 1;
57d116ab 1022 char txtuid[20];
2133f58c 1023 char txtgid[20];
cf3ef16d
SH
1024 char **n2 = malloc(n2args * sizeof(*n2));
1025 struct lxc_list *it;
1026 struct id_map *map;
1027
57d116ab
SH
1028 if (!n2) {
1029 SYSERROR("out of memory");
1030 exit(1);
1031 }
cf3ef16d
SH
1032 newargv[0] = tpath;
1033 tpath = "lxc-usernsexec";
1034 n2[0] = "lxc-usernsexec";
1035 lxc_list_for_each(it, &conf->id_map) {
1036 map = it->elem;
1037 n2args += 2;
57d116ab 1038 n2 = realloc(n2, n2args * sizeof(char *));
cf3ef16d
SH
1039 if (!n2)
1040 exit(1);
1041 n2[n2args-2] = "-m";
1042 n2[n2args-1] = malloc(200);
1043 if (!n2[n2args-1])
1044 exit(1);
1045 ret = snprintf(n2[n2args-1], 200, "%c:%lu:%lu:%lu",
1046 map->idtype == ID_TYPE_UID ? 'u' : 'g',
1047 map->nsid, map->hostid, map->range);
1048 if (ret < 0 || ret >= 200)
1049 exit(1);
1050 }
2133f58c 1051 int hostid_mapped = mapped_hostid(geteuid(), conf, ID_TYPE_UID);
6849cb5b 1052 int extraargs = hostid_mapped >= 0 ? 1 : 3;
57d116ab 1053 n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *));
cf3ef16d
SH
1054 if (!n2)
1055 exit(1);
57d116ab 1056 if (hostid_mapped < 0) {
2133f58c 1057 hostid_mapped = find_unmapped_nsuid(conf, ID_TYPE_UID);
cf3ef16d 1058 n2[n2args++] = "-m";
57d116ab 1059 if (hostid_mapped < 0) {
cf3ef16d
SH
1060 ERROR("Could not find free uid to map");
1061 exit(1);
1062 }
1063 n2[n2args++] = malloc(200);
1064 if (!n2[n2args-1]) {
1065 SYSERROR("out of memory");
1066 exit(1);
1067 }
1068 ret = snprintf(n2[n2args-1], 200, "u:%d:%d:1",
57d116ab 1069 hostid_mapped, geteuid());
cf3ef16d
SH
1070 if (ret < 0 || ret >= 200) {
1071 ERROR("string too long");
1072 exit(1);
1073 }
1074 }
2133f58c
SH
1075 int hostgid_mapped = mapped_hostid(getegid(), conf, ID_TYPE_GID);
1076 extraargs = hostgid_mapped >= 0 ? 1 : 3;
1077 n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *));
1078 if (!n2)
1079 exit(1);
1080 if (hostgid_mapped < 0) {
1081 hostgid_mapped = find_unmapped_nsuid(conf, ID_TYPE_GID);
1082 n2[n2args++] = "-m";
1083 if (hostgid_mapped < 0) {
1084 ERROR("Could not find free uid to map");
1085 exit(1);
1086 }
1087 n2[n2args++] = malloc(200);
1088 if (!n2[n2args-1]) {
1089 SYSERROR("out of memory");
1090 exit(1);
1091 }
1092 ret = snprintf(n2[n2args-1], 200, "g:%d:%d:1",
1093 hostgid_mapped, getegid());
1094 if (ret < 0 || ret >= 200) {
1095 ERROR("string too long");
1096 exit(1);
1097 }
1098 }
cf3ef16d
SH
1099 n2[n2args++] = "--";
1100 for (i = 0; i < nargs; i++)
1101 n2[i + n2args] = newargv[i];
57d116ab
SH
1102 n2args += nargs;
1103 // Finally add "--mapped-uid $uid" to tell template what to chown
1104 // cached images to
2133f58c 1105 n2args += 4;
57d116ab
SH
1106 n2 = realloc(n2, n2args * sizeof(char *));
1107 if (!n2) {
1108 SYSERROR("out of memory");
1109 exit(1);
1110 }
1111 // note n2[n2args-1] is NULL
2133f58c 1112 n2[n2args-5] = "--mapped-uid";
57d116ab 1113 snprintf(txtuid, 20, "%d", hostid_mapped);
2133f58c
SH
1114 n2[n2args-4] = txtuid;
1115 n2[n2args-3] = "--mapped-gid";
1116 snprintf(txtgid, 20, "%d", hostgid_mapped);
1117 n2[n2args-2] = txtgid;
57d116ab 1118 n2[n2args-1] = NULL;
cf3ef16d
SH
1119 free(newargv);
1120 newargv = n2;
1121 }
72d0e1cb 1122 /* execute */
cf3ef16d 1123 execvp(tpath, newargv);
72d0e1cb
SG
1124 SYSERROR("failed to execute template %s", tpath);
1125 exit(1);
1126 }
1127
9be53773 1128 if (wait_for_pid(pid) != 0) {
959aee9c 1129 ERROR("container creation template for %s failed", c->name);
96b3cb40
SH
1130 return false;
1131 }
1132
1133 return true;
1134}
1135
74a3920a 1136static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
3ce74686 1137{
1fd9bd50 1138 long flen;
b4569e93 1139 char *contents;
3ce74686 1140 FILE *f;
025ed0f3 1141 int ret = -1;
52026772 1142#if HAVE_LIBGNUTLS
025ed0f3 1143 int i;
3ce74686 1144 unsigned char md_value[SHA_DIGEST_LENGTH];
b4569e93 1145 char *tpath;
52026772 1146#endif
3ce74686 1147
025ed0f3 1148 f = fopen(path, "r");
025ed0f3 1149 if (f == NULL)
3ce74686 1150 return false;
025ed0f3
SH
1151
1152 if (fseek(f, 0, SEEK_END) < 0)
1153 goto out_error;
1154 if ((flen = ftell(f)) < 0)
1155 goto out_error;
1156 if (fseek(f, 0, SEEK_SET) < 0)
1157 goto out_error;
1158 if ((contents = malloc(flen + 1)) == NULL)
1159 goto out_error;
1160 if (fread(contents, 1, flen, f) != flen)
1161 goto out_free_contents;
1162
3ce74686 1163 contents[flen] = '\0';
025ed0f3 1164 ret = fclose(f);
025ed0f3
SH
1165 f = NULL;
1166 if (ret < 0)
1167 goto out_free_contents;
3ce74686 1168
b4569e93 1169#if HAVE_LIBGNUTLS
01efd4d3 1170 tpath = get_template_path(t);
85db5535 1171 if (!tpath) {
959aee9c 1172 ERROR("bad template: %s", t);
025ed0f3 1173 goto out_free_contents;
3ce74686
SH
1174 }
1175
85db5535
DE
1176 ret = sha1sum_file(tpath, md_value);
1177 if (ret < 0) {
1178 ERROR("Error getting sha1sum of %s", tpath);
3ce74686 1179 free(tpath);
85db5535 1180 goto out_free_contents;
3ce74686 1181 }
85db5535 1182 free(tpath);
3ce74686
SH
1183#endif
1184
025ed0f3 1185 f = fopen(path, "w");
025ed0f3 1186 if (f == NULL) {
3ce74686
SH
1187 SYSERROR("reopening config for writing");
1188 free(contents);
1189 return false;
1190 }
1191 fprintf(f, "# Template used to create this container: %s\n", t);
1192 if (argv) {
1193 fprintf(f, "# Parameters passed to the template:");
1194 while (*argv) {
1195 fprintf(f, " %s", *argv);
1196 argv++;
1197 }
1198 fprintf(f, "\n");
1199 }
1200#if HAVE_LIBGNUTLS
56698177
SH
1201 fprintf(f, "# Template script checksum (SHA-1): ");
1202 for (i=0; i<SHA_DIGEST_LENGTH; i++)
1203 fprintf(f, "%02x", md_value[i]);
1204 fprintf(f, "\n");
3ce74686 1205#endif
0520c252 1206 fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n");
3ce74686
SH
1207 if (fwrite(contents, 1, flen, f) != flen) {
1208 SYSERROR("Writing original contents");
1209 free(contents);
1210 fclose(f);
1211 return false;
1212 }
025ed0f3
SH
1213 ret = 0;
1214out_free_contents:
3ce74686 1215 free(contents);
025ed0f3
SH
1216out_error:
1217 if (f) {
1218 int newret;
025ed0f3 1219 newret = fclose(f);
025ed0f3
SH
1220 if (ret == 0)
1221 ret = newret;
1222 }
1223 if (ret < 0) {
1224 SYSERROR("Error prepending header");
3ce74686
SH
1225 return false;
1226 }
1227 return true;
1228}
1229
4df7f012
SH
1230static void lxcapi_clear_config(struct lxc_container *c)
1231{
f979ac15
SH
1232 if (c) {
1233 if (c->lxc_conf) {
1234 lxc_conf_free(c->lxc_conf);
1235 c->lxc_conf = NULL;
1236 }
4df7f012
SH
1237 }
1238}
1239
96b3cb40 1240static bool lxcapi_destroy(struct lxc_container *c);
18aa217b
SH
1241static bool container_destroy(struct lxc_container *c);
1242static bool get_snappath_dir(struct lxc_container *c, char *snappath);
96b3cb40
SH
1243/*
1244 * lxcapi_create:
1245 * create a container with the given parameters.
1246 * @c: container to be created. It has the lxcpath, name, and a starting
1247 * configuration already set
1248 * @t: the template to execute to instantiate the root filesystem and
1249 * adjust the configuration.
1250 * @bdevtype: backing store type to use. If NULL, dir will be used.
1251 * @specs: additional parameters for the backing store, i.e. LVM vg to
1252 * use.
1253 *
1254 * @argv: the arguments to pass to the template, terminated by NULL. If no
1255 * arguments, you can just pass NULL.
1256 */
1257static bool lxcapi_create(struct lxc_container *c, const char *t,
dc23c1c8 1258 const char *bdevtype, struct bdev_specs *specs, int flags,
96b3cb40
SH
1259 char *const argv[])
1260{
a69aad27 1261 bool ret = false;
96b3cb40 1262 pid_t pid;
85db5535 1263 char *tpath = NULL;
cbee8106 1264 int partial_fd;
96b3cb40
SH
1265
1266 if (!c)
1267 return false;
1268
85db5535
DE
1269 if (t) {
1270 tpath = get_template_path(t);
1271 if (!tpath) {
959aee9c 1272 ERROR("bad template: %s", t);
85db5535
DE
1273 goto out;
1274 }
96b3cb40
SH
1275 }
1276
cf465fe4
SH
1277 /*
1278 * If a template is passed in, and the rootfs already is defined in
1279 * the container config and exists, then * caller is trying to create
1280 * an existing container. Return an error, but do NOT delete the
1281 * container.
1282 */
1283 if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path &&
1284 access(c->lxc_conf->rootfs.path, F_OK) == 0 && tpath) {
1285 ERROR("Container %s:%s already exists", c->config_path, c->name);
6c6892b5 1286 goto free_tpath;
cf465fe4
SH
1287 }
1288
6c6892b5 1289 if (!c->lxc_conf) {
dad87e3b 1290 if (!c->load_config(c, lxc_global_config_value("lxc.default_config"))) {
959aee9c 1291 ERROR("Error loading default configuration file %s", lxc_global_config_value("lxc.default_config"));
6c6892b5
DE
1292 goto free_tpath;
1293 }
96b3cb40
SH
1294 }
1295
6c6892b5
DE
1296 if (!create_container_dir(c))
1297 goto free_tpath;
1298
0590e82c
SH
1299 /*
1300 * either template or rootfs.path should be set.
1301 * if both template and rootfs.path are set, template is setup as rootfs.path.
1302 * container is already created if we have a config and rootfs.path is accessible
1303 */
1304 if (!c->lxc_conf->rootfs.path && !tpath)
1305 /* no template passed in and rootfs does not exist: error */
1306 goto out;
1307 if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0)
1308 /* rootfs passed into configuration, but does not exist: error */
1309 goto out;
1310 if (lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !tpath) {
1311 /* Rootfs already existed, user just wanted to save the
1312 * loaded configuration */
1313 ret = true;
1314 goto out;
a69aad27 1315 }
96b3cb40
SH
1316
1317 /* Mark that this container is being created */
1318 if ((partial_fd = create_partial(c)) < 0)
1319 goto out;
1320
1321 /* no need to get disk lock bc we have the partial locked */
1322
1323 /*
1324 * Create the backing store
1325 * Note we can't do this in the same task as we use to execute the
1326 * template because of the way zfs works.
1327 * After you 'zfs create', zfs mounts the fs only in the initial
1328 * namespace.
1329 */
1330 pid = fork();
1331 if (pid < 0) {
959aee9c 1332 SYSERROR("failed to fork task for container creation template");
8eb5694b
SH
1333 goto out_unlock;
1334 }
1335
96b3cb40
SH
1336 if (pid == 0) { // child
1337 struct bdev *bdev = NULL;
1338
1339 if (!(bdev = do_bdev_create(c, bdevtype, specs))) {
1340 ERROR("Error creating backing store type %s for %s",
1341 bdevtype ? bdevtype : "(none)", c->name);
1342 exit(1);
1343 }
1344
1345 /* save config file again to store the new rootfs location */
1346 if (!c->save_config(c, NULL)) {
959aee9c 1347 ERROR("failed to save starting configuration for %s", c->name);
96b3cb40
SH
1348 // parent task won't see bdev in config so we delete it
1349 bdev->ops->umount(bdev);
1350 bdev->ops->destroy(bdev);
1351 exit(1);
1352 }
1353 exit(0);
1354 }
1355 if (wait_for_pid(pid) != 0)
a09295f8 1356 goto out_unlock;
96b3cb40
SH
1357
1358 /* reload config to get the rootfs */
a3b47c09 1359 lxc_conf_free(c->lxc_conf);
96b3cb40
SH
1360 c->lxc_conf = NULL;
1361 if (!load_config_locked(c, c->configfile))
a09295f8 1362 goto out_unlock;
96b3cb40 1363
dc23c1c8 1364 if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv))
96b3cb40
SH
1365 goto out_unlock;
1366
8eb5694b
SH
1367 // now clear out the lxc_conf we have, reload from the created
1368 // container
4df7f012 1369 lxcapi_clear_config(c);
3ce74686 1370
9d65a487
KY
1371 if (t) {
1372 if (!prepend_lxc_header(c->configfile, tpath, argv)) {
1373 ERROR("Error prepending header to configuration file");
1374 goto out_unlock;
1375 }
3ce74686 1376 }
a69aad27 1377 ret = load_config_locked(c, c->configfile);
72d0e1cb
SG
1378
1379out_unlock:
3e625e2d
SH
1380 if (partial_fd >= 0)
1381 remove_partial(c, partial_fd);
72d0e1cb 1382out:
a69aad27 1383 if (!ret && c)
18aa217b 1384 container_destroy(c);
6c6892b5
DE
1385free_tpath:
1386 if (tpath)
1387 free(tpath);
a69aad27 1388 return ret;
72d0e1cb
SG
1389}
1390
3e625e2d
SH
1391static bool lxcapi_reboot(struct lxc_container *c)
1392{
1393 pid_t pid;
1394
1395 if (!c)
1396 return false;
1397 if (!c->is_running(c))
1398 return false;
1399 pid = c->init_pid(c);
1400 if (pid <= 0)
1401 return false;
1402 if (kill(pid, SIGINT) < 0)
1403 return false;
1404 return true;
1405
1406}
1407
72d0e1cb
SG
1408static bool lxcapi_shutdown(struct lxc_container *c, int timeout)
1409{
1410 bool retv;
1411 pid_t pid;
f0f1d8c0 1412 int haltsignal = SIGPWR;
72d0e1cb
SG
1413
1414 if (!c)
1415 return false;
1416
72d0e1cb
SG
1417 if (!c->is_running(c))
1418 return true;
1419 pid = c->init_pid(c);
1420 if (pid <= 0)
1421 return true;
b0227444 1422 if (c->lxc_conf && c->lxc_conf->haltsignal)
f0f1d8c0
DE
1423 haltsignal = c->lxc_conf->haltsignal;
1424 kill(pid, haltsignal);
72d0e1cb 1425 retv = c->wait(c, "STOPPED", timeout);
72d0e1cb
SG
1426 return retv;
1427}
1428
1897e3bc 1429static bool lxcapi_createl(struct lxc_container *c, const char *t,
dc23c1c8 1430 const char *bdevtype, struct bdev_specs *specs, int flags, ...)
72d0e1cb
SG
1431{
1432 bool bret = false;
a0e93eeb 1433 char **args = NULL;
72d0e1cb 1434 va_list ap;
72d0e1cb
SG
1435
1436 if (!c)
1437 return false;
1438
1439 /*
1440 * since we're going to wait for create to finish, I don't think we
1441 * need to get a copy of the arguments.
1442 */
dc23c1c8 1443 va_start(ap, flags);
a0e93eeb 1444 args = lxc_va_arg_list_to_argv(ap, 0, 0);
72d0e1cb 1445 va_end(ap);
a0e93eeb
CS
1446 if (!args) {
1447 ERROR("Memory allocation error.");
1448 goto out;
1449 }
72d0e1cb 1450
dc23c1c8 1451 bret = c->create(c, t, bdevtype, specs, flags, args);
72d0e1cb
SG
1452
1453out:
a0e93eeb 1454 free(args);
72d0e1cb
SG
1455 return bret;
1456}
1457
6b0d5538
SH
1458static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key)
1459{
1460 if (strcmp(key, "lxc.cgroup") == 0)
1461 clear_unexp_config_line(conf, key, true);
1462 else if (strcmp(key, "lxc.network") == 0)
1463 clear_unexp_config_line(conf, key, true);
1464 else if (strcmp(key, "lxc.hook") == 0)
1465 clear_unexp_config_line(conf, key, true);
1466 else
1467 clear_unexp_config_line(conf, key, false);
1468 if (!do_append_unexp_config_line(conf, key, ""))
1469 WARN("Error clearing configuration for %s", key);
1470}
1471
12a50cc6 1472static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
72d0e1cb
SG
1473{
1474 int ret;
1475
1476 if (!c || !c->lxc_conf)
1477 return false;
5cee8c50 1478 if (container_mem_lock(c))
72d0e1cb 1479 return false;
72d0e1cb 1480 ret = lxc_clear_config_item(c->lxc_conf, key);
6b0d5538
SH
1481 if (!ret)
1482 do_clear_unexp_config_line(c->lxc_conf, key);
5cee8c50 1483 container_mem_unlock(c);
72d0e1cb
SG
1484 return ret == 0;
1485}
1486
e0f59189 1487static inline bool enter_net_ns(struct lxc_container *c)
51d0854c
DY
1488{
1489 pid_t pid = c->init_pid(c);
ae22a220 1490
0e6e3a41 1491 if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) {
51d0854c
DY
1492 if (!switch_to_ns(pid, "user"))
1493 return false;
9c83a661 1494 }
51d0854c 1495 return switch_to_ns(pid, "net");
799f29ab
ÇO
1496}
1497
9c88ff1f
ÇO
1498// used by qsort and bsearch functions for comparing names
1499static inline int string_cmp(char **first, char **second)
1500{
1501 return strcmp(*first, *second);
1502}
1503
1504// used by qsort and bsearch functions for comparing container names
1505static inline int container_cmp(struct lxc_container **first, struct lxc_container **second)
1506{
1507 return strcmp((*first)->name, (*second)->name);
1508}
1509
1510static bool add_to_array(char ***names, char *cname, int pos)
1511{
1512 char **newnames = realloc(*names, (pos+1) * sizeof(char *));
1513 if (!newnames) {
1514 ERROR("Out of memory");
1515 return false;
1516 }
1517
1518 *names = newnames;
1519 newnames[pos] = strdup(cname);
1520 if (!newnames[pos])
1521 return false;
1522
1523 // sort the arrray as we will use binary search on it
1524 qsort(newnames, pos + 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp);
1525
1526 return true;
1527}
1528
2871830a 1529static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, int pos, bool sort)
9c88ff1f
ÇO
1530{
1531 struct lxc_container **newlist = realloc(*list, (pos+1) * sizeof(struct lxc_container *));
1532 if (!newlist) {
1533 ERROR("Out of memory");
1534 return false;
1535 }
1536
1537 *list = newlist;
1538 newlist[pos] = c;
1539
1540 // sort the arrray as we will use binary search on it
2871830a
DE
1541 if (sort)
1542 qsort(newlist, pos + 1, sizeof(struct lxc_container *), (int (*)(const void *,const void *))container_cmp);
9c88ff1f
ÇO
1543
1544 return true;
1545}
1546
1547static char** get_from_array(char ***names, char *cname, int size)
1548{
1549 return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp);
1550}
1551
1552
1553static bool array_contains(char ***names, char *cname, int size) {
1554 if(get_from_array(names, cname, size) != NULL)
1555 return true;
1556 return false;
1557}
1558
1559static bool remove_from_array(char ***names, char *cname, int size)
1560{
1561 char **result = get_from_array(names, cname, size);
1562 if (result != NULL) {
1563 free(result);
1564 return true;
1565 }
1566 return false;
1567}
1568
799f29ab
ÇO
1569static char** lxcapi_get_interfaces(struct lxc_container *c)
1570{
ae22a220
ÇO
1571 pid_t pid;
1572 int i, count = 0, pipefd[2];
9c88ff1f 1573 char **interfaces = NULL;
ae22a220 1574 char interface[IFNAMSIZ];
799f29ab 1575
ae22a220
ÇO
1576 if(pipe(pipefd) < 0) {
1577 SYSERROR("pipe failed");
1578 return NULL;
c868b261
ÇO
1579 }
1580
ae22a220
ÇO
1581 pid = fork();
1582 if (pid < 0) {
959aee9c 1583 SYSERROR("failed to fork task to get interfaces information");
ae22a220
ÇO
1584 close(pipefd[0]);
1585 close(pipefd[1]);
1586 return NULL;
1587 }
799f29ab 1588
ae22a220
ÇO
1589 if (pid == 0) { // child
1590 int ret = 1, nbytes;
1591 struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
1592
1593 /* close the read-end of the pipe */
1594 close(pipefd[0]);
1595
e0f59189 1596 if (!enter_net_ns(c)) {
ae22a220
ÇO
1597 SYSERROR("failed to enter namespace");
1598 goto out;
1599 }
1600
1601 /* Grab the list of interfaces */
1602 if (getifaddrs(&interfaceArray)) {
1603 SYSERROR("failed to get interfaces list");
1604 goto out;
1605 }
1606
1607 /* Iterate through the interfaces */
1608 for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
1609 nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ);
1610 if (nbytes < 0) {
1611 ERROR("write failed");
1612 goto out;
1613 }
1614 count++;
1615 }
1616 ret = 0;
1617
1618 out:
1619 if (interfaceArray)
1620 freeifaddrs(interfaceArray);
1621
1622 /* close the write-end of the pipe, thus sending EOF to the reader */
1623 close(pipefd[1]);
1624 exit(ret);
799f29ab
ÇO
1625 }
1626
ae22a220
ÇO
1627 /* close the write-end of the pipe */
1628 close(pipefd[1]);
1629
358afd84 1630 while (read(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) {
ae22a220
ÇO
1631 if (array_contains(&interfaces, interface, count))
1632 continue;
799f29ab 1633
ae22a220
ÇO
1634 if(!add_to_array(&interfaces, interface, count))
1635 ERROR("PARENT: add_to_array failed");
9c88ff1f
ÇO
1636 count++;
1637 }
799f29ab 1638
ae22a220
ÇO
1639 if (wait_for_pid(pid) != 0) {
1640 for(i=0;i<count;i++)
1641 free(interfaces[i]);
1642 free(interfaces);
1643 interfaces = NULL;
1644 }
9c88ff1f 1645
ae22a220
ÇO
1646 /* close the read-end of the pipe */
1647 close(pipefd[0]);
799f29ab 1648
9c88ff1f
ÇO
1649 /* Append NULL to the array */
1650 if(interfaces)
1651 interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);
799f29ab 1652
9c88ff1f 1653 return interfaces;
799f29ab
ÇO
1654}
1655
f0ca2726 1656static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope)
799f29ab 1657{
ae22a220
ÇO
1658 pid_t pid;
1659 int i, count = 0, pipefd[2];
9c88ff1f 1660 char **addresses = NULL;
ae22a220 1661 char address[INET6_ADDRSTRLEN];
799f29ab 1662
ae22a220
ÇO
1663 if(pipe(pipefd) < 0) {
1664 SYSERROR("pipe failed");
1665 return NULL;
c868b261
ÇO
1666 }
1667
ae22a220
ÇO
1668 pid = fork();
1669 if (pid < 0) {
959aee9c 1670 SYSERROR("failed to fork task to get container ips");
ae22a220
ÇO
1671 close(pipefd[0]);
1672 close(pipefd[1]);
1673 return NULL;
9c83a661
SG
1674 }
1675
ae22a220
ÇO
1676 if (pid == 0) { // child
1677 int ret = 1, nbytes;
1678 struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
1679 char addressOutputBuffer[INET6_ADDRSTRLEN];
1680 void *tempAddrPtr = NULL;
1681 char *address = NULL;
fe218ca3 1682
ae22a220
ÇO
1683 /* close the read-end of the pipe */
1684 close(pipefd[0]);
1685
e0f59189 1686 if (!enter_net_ns(c)) {
ae22a220
ÇO
1687 SYSERROR("failed to enter namespace");
1688 goto out;
9c83a661 1689 }
ae22a220
ÇO
1690
1691 /* Grab the list of interfaces */
1692 if (getifaddrs(&interfaceArray)) {
1693 SYSERROR("failed to get interfaces list");
1694 goto out;
1695 }
1696
1697 /* Iterate through the interfaces */
1698 for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
1699 if (tempIfAddr->ifa_addr == NULL)
9c83a661
SG
1700 continue;
1701
ae22a220
ÇO
1702 if(tempIfAddr->ifa_addr->sa_family == AF_INET) {
1703 if (family && strcmp(family, "inet"))
1704 continue;
1705 tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr;
1706 }
1707 else {
1708 if (family && strcmp(family, "inet6"))
1709 continue;
1710
1711 if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope)
1712 continue;
1713
1714 tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr;
1715 }
1716
1717 if (interface && strcmp(interface, tempIfAddr->ifa_name))
1718 continue;
1719 else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)
9c83a661
SG
1720 continue;
1721
ae22a220
ÇO
1722 address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,
1723 tempAddrPtr,
1724 addressOutputBuffer,
1725 sizeof(addressOutputBuffer));
1726 if (!address)
1727 continue;
1728
1729 nbytes = write(pipefd[1], address, INET6_ADDRSTRLEN);
1730 if (nbytes < 0) {
1731 ERROR("write failed");
1732 goto out;
1733 }
1734 count++;
9c83a661 1735 }
ae22a220 1736 ret = 0;
9c83a661 1737
ae22a220
ÇO
1738 out:
1739 if(interfaceArray)
1740 freeifaddrs(interfaceArray);
9c83a661 1741
ae22a220
ÇO
1742 /* close the write-end of the pipe, thus sending EOF to the reader */
1743 close(pipefd[1]);
1744 exit(ret);
6849cb5b 1745 }
9c83a661 1746
ae22a220
ÇO
1747 /* close the write-end of the pipe */
1748 close(pipefd[1]);
1749
358afd84 1750 while (read(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) {
9c88ff1f 1751 if(!add_to_array(&addresses, address, count))
ae22a220 1752 ERROR("PARENT: add_to_array failed");
9c88ff1f 1753 count++;
9c83a661
SG
1754 }
1755
ae22a220
ÇO
1756 if (wait_for_pid(pid) != 0) {
1757 for(i=0;i<count;i++)
1758 free(addresses[i]);
1759 free(addresses);
1760 addresses = NULL;
1761 }
9c83a661 1762
ae22a220
ÇO
1763 /* close the read-end of the pipe */
1764 close(pipefd[0]);
9c83a661
SG
1765
1766 /* Append NULL to the array */
9c88ff1f
ÇO
1767 if(addresses)
1768 addresses = (char **)lxc_append_null_to_array((void **)addresses, count);
9c83a661
SG
1769
1770 return addresses;
1771}
1772
12a50cc6 1773static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
72d0e1cb
SG
1774{
1775 int ret;
1776
1777 if (!c || !c->lxc_conf)
1778 return -1;
5cee8c50 1779 if (container_mem_lock(c))
72d0e1cb 1780 return -1;
72d0e1cb 1781 ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen);
5cee8c50 1782 container_mem_unlock(c);
72d0e1cb
SG
1783 return ret;
1784}
1785
8ac18377
ÇO
1786static char* lxcapi_get_running_config_item(struct lxc_container *c, const char *key)
1787{
1788 char *ret;
1789
1790 if (!c || !c->lxc_conf)
1791 return NULL;
1792 if (container_mem_lock(c))
1793 return NULL;
1794 ret = lxc_cmd_get_config_item(c->name, key, c->get_config_path(c));
1795 container_mem_unlock(c);
1796 return ret;
1797}
1798
12a50cc6 1799static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
72d0e1cb
SG
1800{
1801 if (!key)
1802 return lxc_listconfigs(retv, inlen);
1803 /*
1804 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
1805 * This is an intelligent result to show which keys are valid given
1806 * the type of nic it is
1807 */
1808 if (!c || !c->lxc_conf)
1809 return -1;
5cee8c50 1810 if (container_mem_lock(c))
72d0e1cb
SG
1811 return -1;
1812 int ret = -1;
1813 if (strncmp(key, "lxc.network.", 12) == 0)
6849cb5b 1814 ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen);
5cee8c50 1815 container_mem_unlock(c);
72d0e1cb
SG
1816 return ret;
1817}
1818
12a50cc6 1819static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
72d0e1cb 1820{
39dc698c
SH
1821 FILE *fout;
1822 bool ret = false, need_disklock = false;
1823 int lret;
1824
72d0e1cb
SG
1825 if (!alt_file)
1826 alt_file = c->configfile;
1827 if (!alt_file)
6849cb5b 1828 return false; // should we write to stdout if no file is specified?
39dc698c
SH
1829
1830 // If we haven't yet loaded a config, load the stock config
1831 if (!c->lxc_conf) {
dad87e3b 1832 if (!c->load_config(c, lxc_global_config_value("lxc.default_config"))) {
959aee9c 1833 ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("lxc.default_config"), c->name);
72d0e1cb
SG
1834 return false;
1835 }
39dc698c 1836 }
72d0e1cb 1837
5a3d2e1e
SG
1838 if (!create_container_dir(c))
1839 return false;
1840
39dc698c
SH
1841 /*
1842 * If we're writing to the container's config file, take the
1843 * disk lock. Otherwise just take the memlock to protect the
1844 * struct lxc_container while we're traversing it.
1845 */
1846 if (strcmp(c->configfile, alt_file) == 0)
1847 need_disklock = true;
1848
1849 if (need_disklock)
1850 lret = container_disk_lock(c);
1851 else
1852 lret = container_mem_lock(c);
1853
1854 if (lret)
72d0e1cb 1855 return false;
39dc698c
SH
1856
1857 fout = fopen(alt_file, "w");
1858 if (!fout)
1859 goto out;
6b0d5538 1860 write_config(fout, c->lxc_conf);
72d0e1cb 1861 fclose(fout);
39dc698c
SH
1862 ret = true;
1863
1864out:
1865 if (need_disklock)
1866 container_disk_unlock(c);
1867 else
1868 container_mem_unlock(c);
1869 return ret;
72d0e1cb
SG
1870}
1871
dfb31b25
SH
1872static bool mod_rdep(struct lxc_container *c, bool inc)
1873{
1874 char path[MAXPATHLEN];
1875 int ret, v = 0;
1876 FILE *f;
1877 bool bret = false;
1878
1879 if (container_disk_lock(c))
1880 return false;
1881 ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path,
1882 c->name);
1883 if (ret < 0 || ret > MAXPATHLEN)
1884 goto out;
1885 f = fopen(path, "r");
1886 if (f) {
1887 ret = fscanf(f, "%d", &v);
1888 fclose(f);
1889 if (ret != 1) {
1890 ERROR("Corrupted file %s", path);
1891 goto out;
1892 }
1893 }
1894 v += inc ? 1 : -1;
1895 f = fopen(path, "w");
1896 if (!f)
1897 goto out;
1898 if (fprintf(f, "%d\n", v) < 0) {
1899 ERROR("Error writing new snapshots value");
1900 fclose(f);
1901 goto out;
1902 }
025ed0f3 1903 ret = fclose(f);
025ed0f3 1904 if (ret != 0) {
dfb31b25
SH
1905 SYSERROR("Error writing to or closing snapshots file");
1906 goto out;
1907 }
1908
1909 bret = true;
1910
1911out:
1912 container_disk_unlock(c);
1913 return bret;
1914}
1915
1916static void strip_newline(char *p)
1917{
1918 size_t len = strlen(p);
1919 if (len < 1)
1920 return;
1921 if (p[len-1] == '\n')
1922 p[len-1] = '\0';
1923}
1924
1925static void mod_all_rdeps(struct lxc_container *c, bool inc)
1926{
1927 struct lxc_container *p;
1928 char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN];
1929 size_t pathlen = 0, namelen = 0;
1930 FILE *f;
1931 int ret;
1932
1933 ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends",
1934 c->config_path, c->name);
1935 if (ret < 0 || ret >= MAXPATHLEN) {
1936 ERROR("Path name too long");
1937 return;
1938 }
025ed0f3 1939 f = fopen(path, "r");
025ed0f3 1940 if (f == NULL)
dfb31b25
SH
1941 return;
1942 while (getline(&lxcpath, &pathlen, f) != -1) {
1943 if (getline(&lxcname, &namelen, f) == -1) {
959aee9c 1944 ERROR("badly formatted file %s", path);
dfb31b25
SH
1945 goto out;
1946 }
1947 strip_newline(lxcpath);
1948 strip_newline(lxcname);
1949 if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) {
1950 ERROR("Unable to find dependent container %s:%s",
1951 lxcpath, lxcname);
1952 continue;
1953 }
1954 if (!mod_rdep(p, inc))
1955 ERROR("Failed to increase numsnapshots for %s:%s",
1956 lxcpath, lxcname);
1957 lxc_container_put(p);
1958 }
1959out:
1960 if (lxcpath) free(lxcpath);
1961 if (lxcname) free(lxcname);
1962 fclose(f);
1963}
1964
18aa217b 1965static bool has_fs_snapshots(struct lxc_container *c)
dfb31b25
SH
1966{
1967 char path[MAXPATHLEN];
1968 int ret, v;
1969 FILE *f;
1970 bool bret = false;
1971
1972 ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path,
1973 c->name);
1974 if (ret < 0 || ret > MAXPATHLEN)
1975 goto out;
1976 f = fopen(path, "r");
1977 if (!f)
1978 goto out;
1979 ret = fscanf(f, "%d", &v);
1980 fclose(f);
1981 if (ret != 1)
1982 goto out;
1983 bret = v != 0;
1984
1985out:
1986 return bret;
1987}
1988
18aa217b
SH
1989static bool has_snapshots(struct lxc_container *c)
1990{
1991 char path[MAXPATHLEN];
1992 struct dirent dirent, *direntp;
1993 int count=0;
1994 DIR *dir;
1995
1996 if (!get_snappath_dir(c, path))
1997 return false;
1998 dir = opendir(path);
1999 if (!dir)
2000 return false;
2001 while (!readdir_r(dir, &dirent, &direntp)) {
2002 if (!direntp)
2003 break;
2004
2005 if (!strcmp(direntp->d_name, "."))
2006 continue;
2007
2008 if (!strcmp(direntp->d_name, ".."))
2009 continue;
2010 count++;
2011 break;
2012 }
2013 closedir(dir);
2014 return count > 0;
2015}
2016
4355ab5f
SH
2017static int lxc_rmdir_onedev_wrapper(void *data)
2018{
2019 char *arg = (char *) data;
18aa217b 2020 return lxc_rmdir_onedev(arg, "snaps");
4355ab5f
SH
2021}
2022
44a706bd
SH
2023static int do_bdev_destroy(struct lxc_conf *conf)
2024{
2025 struct bdev *r;
2026 int ret = 0;
2027
76a26f55 2028 r = bdev_init(conf, conf->rootfs.path, conf->rootfs.mount, NULL);
44a706bd
SH
2029 if (!r)
2030 return -1;
2031
2032 if (r->ops->destroy(r) < 0)
2033 ret = -1;
2034 bdev_put(r);
2035 return ret;
2036}
2037
2038static int bdev_destroy_wrapper(void *data)
2039{
2040 struct lxc_conf *conf = data;
2041
2042 if (setgid(0) < 0) {
2043 ERROR("Failed to setgid to 0");
2044 return -1;
2045 }
2046 if (setgroups(0, NULL) < 0)
2047 WARN("Failed to clear groups");
2048 if (setuid(0) < 0) {
2049 ERROR("Failed to setuid to 0");
2050 return -1;
2051 }
2052 return do_bdev_destroy(conf);
2053}
2054
18aa217b 2055static bool container_destroy(struct lxc_container *c)
72d0e1cb 2056{
c868b261 2057 bool bret = false;
fef48dc9 2058 int ret;
72d0e1cb 2059
1897e3bc 2060 if (!c || !lxcapi_is_defined(c))
5a3d2e1e
SG
2061 return false;
2062
3bc449ed 2063 if (container_disk_lock(c))
72d0e1cb 2064 return false;
72d0e1cb 2065
39dc698c 2066 if (!is_stopped(c)) {
60bf62d4
SH
2067 // we should queue some sort of error - in c->error_string?
2068 ERROR("container %s is not stopped", c->name);
2069 goto out;
72d0e1cb
SG
2070 }
2071
44a706bd
SH
2072 if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount) {
2073 if (am_unpriv())
2074 ret = userns_exec_1(c->lxc_conf, bdev_destroy_wrapper, c->lxc_conf);
2075 else
2076 ret = do_bdev_destroy(c->lxc_conf);
2077 if (ret < 0) {
2078 ERROR("Error destroying rootfs for %s", c->name);
2079 goto out;
60bf62d4
SH
2080 }
2081 }
2082
dfb31b25
SH
2083 mod_all_rdeps(c, false);
2084
60bf62d4
SH
2085 const char *p1 = lxcapi_get_config_path(c);
2086 char *path = alloca(strlen(p1) + strlen(c->name) + 2);
2087 sprintf(path, "%s/%s", p1, c->name);
c868b261 2088 if (am_unpriv())
4355ab5f
SH
2089 ret = userns_exec_1(c->lxc_conf, lxc_rmdir_onedev_wrapper, path);
2090 else
18aa217b 2091 ret = lxc_rmdir_onedev(path, "snaps");
4355ab5f 2092 if (ret < 0) {
60bf62d4
SH
2093 ERROR("Error destroying container directory for %s", c->name);
2094 goto out;
2095 }
fef48dc9 2096 bret = true;
60bf62d4
SH
2097
2098out:
3bc449ed 2099 container_disk_unlock(c);
fef48dc9 2100 return bret;
72d0e1cb
SG
2101}
2102
18aa217b
SH
2103static bool lxcapi_destroy(struct lxc_container *c)
2104{
2105 if (!c || !lxcapi_is_defined(c))
2106 return false;
2107 if (has_snapshots(c)) {
2108 ERROR("Container %s has snapshots; not removing", c->name);
2109 return false;
2110 }
2111
2112 if (has_fs_snapshots(c)) {
2113 ERROR("container %s has snapshots on its rootfs", c->name);
2114 return false;
2115 }
2116
2117 return container_destroy(c);
2118}
2119
2120static bool lxcapi_snapshot_destroy_all(struct lxc_container *c);
2121
2122static bool lxcapi_destroy_with_snapshots(struct lxc_container *c)
2123{
2124 if (!c || !lxcapi_is_defined(c))
2125 return false;
2126 if (!lxcapi_snapshot_destroy_all(c)) {
2127 ERROR("Error deleting all snapshots");
2128 return false;
2129 }
2130 return lxcapi_destroy(c);
2131}
2132
96532523
SH
2133static bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v)
2134{
2135 struct lxc_config_t *config;
2136
2137 if (!c->lxc_conf)
2138 c->lxc_conf = lxc_conf_init();
6b0d5538 2139 if (!c->lxc_conf)
96532523
SH
2140 return false;
2141 config = lxc_getconfig(key);
2142 if (!config)
2143 return false;
6b0d5538 2144 if (config->cb(key, v, c->lxc_conf) != 0)
f979ac15 2145 return false;
6b0d5538 2146 return do_append_unexp_config_line(c->lxc_conf, key, v);
96532523
SH
2147}
2148
12a50cc6 2149static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
72d0e1cb 2150{
72d0e1cb 2151 bool b = false;
72d0e1cb
SG
2152
2153 if (!c)
2154 return false;
2155
5cee8c50 2156 if (container_mem_lock(c))
72d0e1cb
SG
2157 return false;
2158
96532523 2159 b = set_config_item_locked(c, key, v);
72d0e1cb 2160
5cee8c50 2161 container_mem_unlock(c);
72d0e1cb
SG
2162 return b;
2163}
2164
2165static char *lxcapi_config_file_name(struct lxc_container *c)
2166{
2167 if (!c || !c->configfile)
2168 return NULL;
2169 return strdup(c->configfile);
2170}
2171
2a59a681
SH
2172static const char *lxcapi_get_config_path(struct lxc_container *c)
2173{
2174 if (!c || !c->config_path)
2175 return NULL;
2176 return (const char *)(c->config_path);
2177}
2178
afeecbba
SH
2179/*
2180 * not for export
2181 * Just recalculate the c->configfile based on the
2182 * c->config_path, which must be set.
2183 * The lxc_container must be locked or not yet public.
2184 */
2185static bool set_config_filename(struct lxc_container *c)
2186{
2187 char *newpath;
2188 int len, ret;
2189
2190 if (!c->config_path)
2191 return false;
2192
2193 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
2194 len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3;
2195 newpath = malloc(len);
2196 if (!newpath)
2197 return false;
2198
2199 ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name);
2200 if (ret < 0 || ret >= len) {
2201 fprintf(stderr, "Error printing out config file name\n");
2202 free(newpath);
2203 return false;
2204 }
2205
2206 if (c->configfile)
2207 free(c->configfile);
2208 c->configfile = newpath;
2209
2210 return true;
2211}
2212
2a59a681
SH
2213static bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
2214{
2215 char *p;
2216 bool b = false;
afeecbba 2217 char *oldpath = NULL;
2a59a681
SH
2218
2219 if (!c)
2220 return b;
2221
5cee8c50 2222 if (container_mem_lock(c))
2a59a681
SH
2223 return b;
2224
2225 p = strdup(path);
afeecbba
SH
2226 if (!p) {
2227 ERROR("Out of memory setting new lxc path");
2a59a681 2228 goto err;
afeecbba
SH
2229 }
2230
2a59a681
SH
2231 b = true;
2232 if (c->config_path)
afeecbba 2233 oldpath = c->config_path;
2a59a681 2234 c->config_path = p;
afeecbba
SH
2235
2236 /* Since we've changed the config path, we have to change the
2237 * config file name too */
2238 if (!set_config_filename(c)) {
2239 ERROR("Out of memory setting new config filename");
2240 b = false;
2241 free(c->config_path);
2242 c->config_path = oldpath;
2243 oldpath = NULL;
2244 }
2a59a681 2245err:
afeecbba
SH
2246 if (oldpath)
2247 free(oldpath);
5cee8c50 2248 container_mem_unlock(c);
2a59a681
SH
2249 return b;
2250}
2251
2252
794dd120
SH
2253static bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
2254{
2255 int ret;
794dd120
SH
2256
2257 if (!c)
2258 return false;
2259
3bc449ed 2260 if (is_stopped(c))
794dd120
SH
2261 return false;
2262
3bc449ed
SH
2263 if (container_disk_lock(c))
2264 return false;
794dd120 2265
33ad9f1a 2266 ret = lxc_cgroup_set(subsys, value, c->name, c->config_path);
3bc449ed
SH
2267
2268 container_disk_unlock(c);
2269 return ret == 0;
794dd120
SH
2270}
2271
2272static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
2273{
3bc449ed 2274 int ret;
794dd120 2275
6502006a 2276 if (!c)
794dd120
SH
2277 return -1;
2278
3bc449ed 2279 if (is_stopped(c))
794dd120
SH
2280 return -1;
2281
3bc449ed
SH
2282 if (container_disk_lock(c))
2283 return -1;
794dd120 2284
33ad9f1a 2285 ret = lxc_cgroup_get(subsys, retv, inlen, c->name, c->config_path);
794dd120 2286
3bc449ed 2287 container_disk_unlock(c);
794dd120
SH
2288 return ret;
2289}
2290
593e8478 2291const char *lxc_get_global_config_item(const char *key)
83c98d82 2292{
593e8478 2293 return lxc_global_config_value(key);
a8428dfa
SH
2294}
2295
b6b918a1
SG
2296const char *lxc_get_version(void)
2297{
95ee490b 2298 return LXC_VERSION;
b6b918a1
SG
2299}
2300
f0ca2726 2301static int copy_file(const char *old, const char *new)
9be53773
SH
2302{
2303 int in, out;
2304 ssize_t len, ret;
2305 char buf[8096];
2306 struct stat sbuf;
2307
2308 if (file_exists(new)) {
2309 ERROR("copy destination %s exists", new);
2310 return -1;
2311 }
2312 ret = stat(old, &sbuf);
2313 if (ret < 0) {
dfb31b25 2314 INFO("Error stat'ing %s", old);
9be53773
SH
2315 return -1;
2316 }
2317
2318 in = open(old, O_RDONLY);
2319 if (in < 0) {
dfb31b25 2320 SYSERROR("Error opening original file %s", old);
9be53773
SH
2321 return -1;
2322 }
2323 out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
2324 if (out < 0) {
dfb31b25 2325 SYSERROR("Error opening new file %s", new);
9be53773
SH
2326 close(in);
2327 return -1;
2328 }
2329
2330 while (1) {
2331 len = read(in, buf, 8096);
2332 if (len < 0) {
dfb31b25 2333 SYSERROR("Error reading old file %s", old);
9be53773
SH
2334 goto err;
2335 }
2336 if (len == 0)
2337 break;
2338 ret = write(out, buf, len);
6849cb5b 2339 if (ret < len) { // should we retry?
dfb31b25 2340 SYSERROR("Error: write to new file %s was interrupted", new);
9be53773
SH
2341 goto err;
2342 }
2343 }
2344 close(in);
2345 close(out);
2346
2347 // we set mode, but not owner/group
2348 ret = chmod(new, sbuf.st_mode);
2349 if (ret) {
dfb31b25 2350 SYSERROR("Error setting mode on %s", new);
9be53773
SH
2351 return -1;
2352 }
2353
2354 return 0;
2355
2356err:
2357 close(in);
2358 close(out);
2359 return -1;
2360}
2361
9be53773
SH
2362static int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
2363{
619256b5 2364 int i, len, ret;
9be53773 2365 struct lxc_list *it;
619256b5
ÇO
2366 char *cpath;
2367
2368 len = strlen(oldc->config_path) + strlen(oldc->name) + 3;
2369 cpath = alloca(len);
2370 ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name);
2371 if (ret < 0 || ret >= len)
2372 return -1;
9be53773
SH
2373
2374 for (i=0; i<NUM_LXC_HOOKS; i++) {
2375 lxc_list_for_each(it, &c->lxc_conf->hooks[i]) {
2376 char *hookname = it->elem;
c32981c3 2377 char *fname = strrchr(hookname, '/');
9be53773
SH
2378 char tmppath[MAXPATHLEN];
2379 if (!fname) // relative path - we don't support, but maybe we should
2380 return 0;
619256b5
ÇO
2381 if (strncmp(hookname, cpath, len - 1) != 0) {
2382 // this hook is public - ignore
2383 continue;
2384 }
9be53773
SH
2385 // copy the script, and change the entry in confile
2386 ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s",
2387 c->config_path, c->name, fname+1);
2388 if (ret < 0 || ret >= MAXPATHLEN)
2389 return -1;
2390 ret = copy_file(it->elem, tmppath);
2391 if (ret < 0)
2392 return -1;
2393 free(it->elem);
2394 it->elem = strdup(tmppath);
2395 if (!it->elem) {
2396 ERROR("out of memory copying hook path");
2397 return -1;
2398 }
9be53773
SH
2399 }
2400 }
2401
67702c21
SH
2402 if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path,
2403 c->config_path, oldc->name, c->name)) {
6b0d5538
SH
2404 ERROR("Error saving new hooks in clone");
2405 return -1;
2406 }
9be53773
SH
2407 c->save_config(c, NULL);
2408 return 0;
2409}
2410
9be53773
SH
2411
2412static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
2413{
2414 char newpath[MAXPATHLEN];
2415 char *oldpath = oldc->lxc_conf->fstab;
2416 int ret;
2417
2418 if (!oldpath)
2419 return 0;
2420
6b0d5538
SH
2421 clear_unexp_config_line(c->lxc_conf, "lxc.mount", false);
2422
c32981c3 2423 char *p = strrchr(oldpath, '/');
9be53773
SH
2424 if (!p)
2425 return -1;
2426 ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s",
2427 c->config_path, c->name, p);
2428 if (ret < 0 || ret >= MAXPATHLEN) {
2429 ERROR("error printing new path for %s", oldpath);
2430 return -1;
2431 }
2432 if (file_exists(newpath)) {
2433 ERROR("error: fstab file %s exists", newpath);
2434 return -1;
2435 }
2436
2437 if (copy_file(oldpath, newpath) < 0) {
2438 ERROR("error: copying %s to %s", oldpath, newpath);
2439 return -1;
2440 }
2441 free(c->lxc_conf->fstab);
2442 c->lxc_conf->fstab = strdup(newpath);
2443 if (!c->lxc_conf->fstab) {
2444 ERROR("error: allocating pathname");
2445 return -1;
2446 }
6b0d5538
SH
2447 if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount", newpath)) {
2448 ERROR("error saving new lxctab");
2449 return -1;
2450 }
9be53773
SH
2451
2452 return 0;
2453}
2454
dfb31b25
SH
2455static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0)
2456{
2457 char path0[MAXPATHLEN], path1[MAXPATHLEN];
2458 int ret;
2459
2460 ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path,
2461 c0->name);
2462 if (ret < 0 || ret >= MAXPATHLEN) {
2463 WARN("Error copying reverse dependencies");
2464 return;
2465 }
2466 ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path,
2467 c->name);
2468 if (ret < 0 || ret >= MAXPATHLEN) {
2469 WARN("Error copying reverse dependencies");
2470 return;
2471 }
2472 if (copy_file(path0, path1) < 0) {
2473 INFO("Error copying reverse dependencies");
2474 return;
2475 }
2476}
2477
2478static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0)
2479{
2480 int ret;
2481 char path[MAXPATHLEN];
2482 FILE *f;
2483 bool bret;
2484
2485 ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path,
2486 c->name);
2487 if (ret < 0 || ret >= MAXPATHLEN)
2488 return false;
2489 f = fopen(path, "a");
2490 if (!f)
2491 return false;
2492 bret = true;
2493 // if anything goes wrong, just return an error
2494 if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0)
2495 bret = false;
2496 if (fclose(f) != 0)
2497 bret = false;
2498 return bret;
2499}
2500
9be53773 2501static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
d659597e 2502 const char *newtype, int flags, const char *bdevdata, uint64_t newsize)
9be53773
SH
2503{
2504 struct bdev *bdev;
dfb31b25 2505 int need_rdep;
9be53773 2506
1354955b 2507 bdev = bdev_copy(c0, c->name, c->config_path, newtype, flags,
dfb31b25 2508 bdevdata, newsize, &need_rdep);
9be53773 2509 if (!bdev) {
dfb31b25 2510 ERROR("Error copying storage");
9be53773
SH
2511 return -1;
2512 }
2513 free(c->lxc_conf->rootfs.path);
2514 c->lxc_conf->rootfs.path = strdup(bdev->src);
2515 bdev_put(bdev);
dfb31b25
SH
2516 if (!c->lxc_conf->rootfs.path) {
2517 ERROR("Out of memory while setting storage path");
9be53773 2518 return -1;
dfb31b25 2519 }
6b0d5538
SH
2520 // We will simply append a new lxc.rootfs entry to the unexpanded config
2521 clear_unexp_config_line(c->lxc_conf, "lxc.rootfs", false);
2522 if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs", c->lxc_conf->rootfs.path)) {
2523 ERROR("Error saving new rootfs to cloend config");
d0218321
SH
2524 return -1;
2525 }
eee59f94
SH
2526 if (flags & LXC_CLONE_SNAPSHOT)
2527 copy_rdepends(c, c0);
dfb31b25
SH
2528 if (need_rdep) {
2529 if (!add_rdepends(c, c0))
2530 WARN("Error adding reverse dependency from %s to %s",
2531 c->name, c0->name);
2532 }
2533
2534 mod_all_rdeps(c, true);
2535
9be53773
SH
2536 return 0;
2537}
2538
1354955b
SH
2539struct clone_update_data {
2540 struct lxc_container *c0;
2541 struct lxc_container *c1;
2542 int flags;
2543 char **hookargs;
2544};
2545
2546static int clone_update_rootfs(struct clone_update_data *data)
9be53773 2547{
1354955b
SH
2548 struct lxc_container *c0 = data->c0;
2549 struct lxc_container *c = data->c1;
2550 int flags = data->flags;
2551 char **hookargs = data->hookargs;
9be53773
SH
2552 int ret = -1;
2553 char path[MAXPATHLEN];
2554 struct bdev *bdev;
2555 FILE *fout;
148e91f5 2556 struct lxc_conf *conf = c->lxc_conf;
9be53773
SH
2557
2558 /* update hostname in rootfs */
2559 /* we're going to mount, so run in a clean namespace to simplify cleanup */
2560
1354955b
SH
2561 if (setgid(0) < 0) {
2562 ERROR("Failed to setgid to 0");
2563 return -1;
2564 }
2565 if (setuid(0) < 0) {
2566 ERROR("Failed to setuid to 0");
9be53773 2567 return -1;
1354955b 2568 }
c476bdce
SH
2569 if (setgroups(0, NULL) < 0)
2570 WARN("Failed to clear groups");
9be53773 2571
1354955b
SH
2572 if (unshare(CLONE_NEWNS) < 0)
2573 return -1;
76a26f55 2574 bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
9be53773 2575 if (!bdev)
1354955b 2576 return -1;
cf3ef16d
SH
2577 if (strcmp(bdev->type, "dir") != 0) {
2578 if (unshare(CLONE_NEWNS) < 0) {
2579 ERROR("error unsharing mounts");
e7de366c 2580 bdev_put(bdev);
1354955b 2581 return -1;
cf3ef16d 2582 }
2c6f3fc9
SH
2583 if (detect_shared_rootfs()) {
2584 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
2585 SYSERROR("Failed to make / rslave");
2586 ERROR("Continuing...");
2587 }
2588 }
e7de366c
SG
2589 if (bdev->ops->mount(bdev) < 0) {
2590 bdev_put(bdev);
1354955b 2591 return -1;
e7de366c 2592 }
cf3ef16d
SH
2593 } else { // TODO come up with a better way
2594 if (bdev->dest)
2595 free(bdev->dest);
2596 bdev->dest = strdup(bdev->src);
2597 }
148e91f5
SH
2598
2599 if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
2600 /* Start of environment variable setup for hooks */
1143ed39
DE
2601 if (setenv("LXC_SRC_NAME", c0->name, 1)) {
2602 SYSERROR("failed to set environment variable for source container name");
2603 }
148e91f5
SH
2604 if (setenv("LXC_NAME", c->name, 1)) {
2605 SYSERROR("failed to set environment variable for container name");
2606 }
2607 if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
2608 SYSERROR("failed to set environment variable for config path");
2609 }
24ef39f4 2610 if (setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) {
148e91f5
SH
2611 SYSERROR("failed to set environment variable for rootfs mount");
2612 }
2613 if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
2614 SYSERROR("failed to set environment variable for rootfs mount");
2615 }
2616
283678ed 2617 if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) {
148e91f5 2618 ERROR("Error executing clone hook for %s", c->name);
e7de366c 2619 bdev_put(bdev);
1354955b 2620 return -1;
148e91f5
SH
2621 }
2622 }
2623
2624 if (!(flags & LXC_CLONE_KEEPNAME)) {
2625 ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest);
e7de366c
SG
2626 bdev_put(bdev);
2627
148e91f5 2628 if (ret < 0 || ret >= MAXPATHLEN)
1354955b 2629 return -1;
8058be39 2630 if (!file_exists(path))
1354955b 2631 return 0;
148e91f5 2632 if (!(fout = fopen(path, "w"))) {
959aee9c 2633 SYSERROR("unable to open %s: ignoring", path);
1354955b 2634 return 0;
148e91f5 2635 }
a684f0b7
ÇO
2636 if (fprintf(fout, "%s", c->name) < 0) {
2637 fclose(fout);
1354955b 2638 return -1;
6849cb5b 2639 }
148e91f5 2640 if (fclose(fout) < 0)
1354955b 2641 return -1;
9be53773 2642 }
e7de366c
SG
2643 else
2644 bdev_put(bdev);
2645
1354955b
SH
2646 return 0;
2647}
2648
2649static int clone_update_rootfs_wrapper(void *data)
2650{
2651 struct clone_update_data *arg = (struct clone_update_data *) data;
2652 return clone_update_rootfs(arg);
9be53773
SH
2653}
2654
2655/*
2656 * We want to support:
2657sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
2658 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
2659
2660-s [ implies overlayfs]
2661-s -B overlayfs
2662-s -B aufs
2663
2664only rootfs gets converted (copied/snapshotted) on clone.
2665*/
2666
d5752559 2667static int create_file_dirname(char *path, struct lxc_conf *conf)
9be53773 2668{
c32981c3 2669 char *p = strrchr(path, '/');
d5752559 2670 int ret = -1;
9be53773
SH
2671
2672 if (!p)
2673 return -1;
2674 *p = '\0';
d5752559 2675 ret = do_create_container_dir(path, conf);
9be53773
SH
2676 *p = '/';
2677 return ret;
2678}
2679
74a3920a 2680static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
9be53773 2681 const char *lxcpath, int flags,
d659597e 2682 const char *bdevtype, const char *bdevdata, uint64_t newsize,
148e91f5 2683 char **hookargs)
9be53773
SH
2684{
2685 struct lxc_container *c2 = NULL;
2686 char newpath[MAXPATHLEN];
176d9acb 2687 int ret, storage_copied = 0;
8d2efe40 2688 char *origroot = NULL;
1354955b 2689 struct clone_update_data data;
9be53773 2690 FILE *fout;
1354955b 2691 pid_t pid;
9be53773
SH
2692
2693 if (!c || !c->is_defined(c))
2694 return NULL;
2695
5cee8c50 2696 if (container_mem_lock(c))
9be53773
SH
2697 return NULL;
2698
39dc698c 2699 if (!is_stopped(c)) {
9be53773
SH
2700 ERROR("error: Original container (%s) is running", c->name);
2701 goto out;
2702 }
2703
2704 // Make sure the container doesn't yet exist.
05d53f4c
SH
2705 if (!newname)
2706 newname = c->name;
2707 if (!lxcpath)
2708 lxcpath = c->get_config_path(c);
2709 ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", lxcpath, newname);
6849cb5b 2710 if (ret < 0 || ret >= MAXPATHLEN) {
9be53773
SH
2711 SYSERROR("clone: failed making config pathname");
2712 goto out;
2713 }
2714 if (file_exists(newpath)) {
2715 ERROR("error: clone: %s exists", newpath);
2716 goto out;
2717 }
2718
d5752559 2719 ret = create_file_dirname(newpath, c->lxc_conf);
96532523 2720 if (ret < 0 && errno != EEXIST) {
9be53773
SH
2721 ERROR("Error creating container dir for %s", newpath);
2722 goto out;
2723 }
2724
2725 // copy the configuration, tweak it as needed,
8d2efe40
SH
2726 if (c->lxc_conf->rootfs.path) {
2727 origroot = c->lxc_conf->rootfs.path;
2728 c->lxc_conf->rootfs.path = NULL;
2729 }
9be53773
SH
2730 fout = fopen(newpath, "w");
2731 if (!fout) {
2732 SYSERROR("open %s", newpath);
2733 goto out;
2734 }
6b0d5538 2735 write_config(fout, c->lxc_conf);
9be53773 2736 fclose(fout);
8d2efe40 2737 c->lxc_conf->rootfs.path = origroot;
9be53773 2738
05d53f4c 2739 sprintf(newpath, "%s/%s/rootfs", lxcpath, newname);
9be53773
SH
2740 if (mkdir(newpath, 0755) < 0) {
2741 SYSERROR("error creating %s", newpath);
2742 goto out;
2743 }
2744
1354955b
SH
2745 if (am_unpriv()) {
2746 if (chown_mapped_root(newpath, c->lxc_conf) < 0) {
959aee9c 2747 ERROR("Error chowning %s to container root", newpath);
1354955b
SH
2748 goto out;
2749 }
2750 }
2751
05d53f4c 2752 c2 = lxc_container_new(newname, lxcpath);
375c2258 2753 if (!c2) {
05d53f4c
SH
2754 ERROR("clone: failed to create new container (%s %s)", newname,
2755 lxcpath);
9be53773
SH
2756 goto out;
2757 }
8d2efe40
SH
2758
2759 // copy/snapshot rootfs's
2760 ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
2761 if (ret < 0)
2762 goto out;
9be53773 2763
6b0d5538
SH
2764 clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false);
2765
96532523
SH
2766 // update utsname
2767 if (!set_config_item_locked(c2, "lxc.utsname", newname)) {
2768 ERROR("Error setting new hostname");
2769 goto out;
2770 }
2771
619256b5
ÇO
2772 // copy hooks
2773 ret = copyhooks(c, c2);
2774 if (ret < 0) {
2775 ERROR("error copying hooks");
2776 goto out;
9be53773
SH
2777 }
2778
2779 if (copy_fstab(c, c2) < 0) {
2780 ERROR("error copying fstab");
9be53773
SH
2781 goto out;
2782 }
2783
2784 // update macaddrs
6b0d5538 2785 if (!(flags & LXC_CLONE_KEEPMACADDR)) {
67702c21
SH
2786 if (!network_new_hwaddrs(c2->lxc_conf)) {
2787 ERROR("Error updating mac addresses");
6b0d5538
SH
2788 goto out;
2789 }
2790 }
9be53773 2791
176d9acb
SH
2792 // We've now successfully created c2's storage, so clear it out if we
2793 // fail after this
2794 storage_copied = 1;
2795
375c2258 2796 if (!c2->save_config(c2, NULL))
9be53773 2797 goto out;
9be53773 2798
1354955b
SH
2799 if ((pid = fork()) < 0) {
2800 SYSERROR("fork");
9be53773 2801 goto out;
1354955b
SH
2802 }
2803 if (pid > 0) {
2804 ret = wait_for_pid(pid);
2805 if (ret)
2806 goto out;
2807 container_mem_unlock(c);
2808 return c2;
2809 }
2810 data.c0 = c;
2811 data.c1 = c2;
2812 data.flags = flags;
2813 data.hookargs = hookargs;
2814 if (am_unpriv())
2815 ret = userns_exec_1(c->lxc_conf, clone_update_rootfs_wrapper,
2816 &data);
2817 else
2818 ret = clone_update_rootfs(&data);
2819 if (ret < 0)
2820 exit(1);
9be53773 2821
5cee8c50 2822 container_mem_unlock(c);
1354955b 2823 exit(0);
9be53773
SH
2824
2825out:
5cee8c50 2826 container_mem_unlock(c);
375c2258 2827 if (c2) {
176d9acb
SH
2828 if (!storage_copied)
2829 c2->lxc_conf->rootfs.path = NULL;
375c2258 2830 c2->destroy(c2);
9be53773 2831 lxc_container_put(c2);
375c2258 2832 }
9be53773
SH
2833
2834 return NULL;
2835}
2836
06e5650e
ÇO
2837static bool lxcapi_rename(struct lxc_container *c, const char *newname)
2838{
2839 struct bdev *bdev;
2840 struct lxc_container *newc;
06e5650e 2841
d693cf93 2842 if (!c || !c->name || !c->config_path || !c->lxc_conf)
06e5650e
ÇO
2843 return false;
2844
18aa217b
SH
2845 if (has_fs_snapshots(c) || has_snapshots(c)) {
2846 ERROR("Renaming a container with snapshots is not supported");
2847 return false;
2848 }
76a26f55 2849 bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
06e5650e
ÇO
2850 if (!bdev) {
2851 ERROR("Failed to find original backing store type");
2852 return false;
2853 }
2854
619256b5 2855 newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL);
06e5650e
ÇO
2856 bdev_put(bdev);
2857 if (!newc) {
2858 lxc_container_put(newc);
2859 return false;
2860 }
2861
2862 if (newc && lxcapi_is_defined(newc))
2863 lxc_container_put(newc);
2864
18aa217b 2865 if (!container_destroy(c)) {
06e5650e
ÇO
2866 ERROR("Could not destroy existing container %s", c->name);
2867 return false;
2868 }
2869 return true;
2870}
2871
a0e93eeb
CS
2872static int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process)
2873{
2874 if (!c)
2875 return -1;
2876
2877 return lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process);
2878}
2879
2880static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[])
2881{
2882 lxc_attach_command_t command;
2883 pid_t pid;
2884 int r;
2885
2886 if (!c)
2887 return -1;
2888
2889 command.program = (char*)program;
2890 command.argv = (char**)argv;
2891 r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid);
2892 if (r < 0) {
2893 ERROR("ups");
2894 return r;
2895 }
2896 return lxc_wait_for_pid_status(pid);
2897}
2898
74a3920a 2899static int get_next_index(const char *lxcpath, char *cname)
f5dd1d53
SH
2900{
2901 char *fname;
2902 struct stat sb;
2903 int i = 0, ret;
2904
2905 fname = alloca(strlen(lxcpath) + 20);
2906 while (1) {
2907 sprintf(fname, "%s/snap%d", lxcpath, i);
2908 ret = stat(fname, &sb);
2909 if (ret != 0)
2910 return i;
2911 i++;
2912 }
2913}
2914
18aa217b
SH
2915static bool get_snappath_dir(struct lxc_container *c, char *snappath)
2916{
2917 int ret;
2918 /*
2919 * If the old style snapshot path exists, use it
2920 * /var/lib/lxc -> /var/lib/lxcsnaps
2921 */
2922 ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path);
2923 if (ret < 0 || ret >= MAXPATHLEN)
2924 return false;
2925 if (dir_exists(snappath)) {
2926 ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name);
2927 if (ret < 0 || ret >= MAXPATHLEN)
2928 return false;
2929 return true;
2930 }
2931
2932 /*
2933 * Use the new style path
2934 * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
2935 */
2936 ret = snprintf(snappath, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
2937 if (ret < 0 || ret >= MAXPATHLEN)
2938 return false;
2939 return true;
2940}
2941
f0ca2726 2942static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile)
f5dd1d53
SH
2943{
2944 int i, flags, ret;
2945 struct lxc_container *c2;
2946 char snappath[MAXPATHLEN], newname[20];
2947
840f05df
SH
2948 if (!c || !lxcapi_is_defined(c))
2949 return -1;
2950
cdd01be2
SH
2951 if (!bdev_can_backup(c->lxc_conf)) {
2952 ERROR("%s's backing store cannot be backed up.", c->name);
2953 ERROR("Your container must use another backing store type.");
2954 return -1;
2955 }
2956
18aa217b 2957 if (!get_snappath_dir(c, snappath))
f5dd1d53 2958 return -1;
18aa217b 2959
f5dd1d53
SH
2960 i = get_next_index(snappath, c->name);
2961
2962 if (mkdir_p(snappath, 0755) < 0) {
2963 ERROR("Failed to create snapshot directory %s", snappath);
2964 return -1;
2965 }
2966
2967 ret = snprintf(newname, 20, "snap%d", i);
2968 if (ret < 0 || ret >= 20)
2969 return -1;
2970
0a83cbbb
SH
2971 /*
2972 * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
2973 * created in the original container
2974 */
2975 flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME |
2976 LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT;
76a26f55 2977 if (bdev_is_dir(c->lxc_conf, c->lxc_conf->rootfs.path)) {
8c39f7a4
SH
2978 ERROR("Snapshot of directory-backed container requested.");
2979 ERROR("Making a copy-clone. If you do want snapshots, then");
1f92162d 2980 ERROR("please create an aufs or overlayfs clone first, snapshot that");
8c39f7a4
SH
2981 ERROR("and keep the original container pristine.");
2982 flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
2983 }
f5dd1d53
SH
2984 c2 = c->clone(c, newname, snappath, flags, NULL, NULL, 0, NULL);
2985 if (!c2) {
959aee9c 2986 ERROR("clone of %s:%s failed", c->config_path, c->name);
f5dd1d53
SH
2987 return -1;
2988 }
2989
2990 lxc_container_put(c2);
2991
2992 // Now write down the creation time
2993 time_t timer;
2994 char buffer[25];
2995 struct tm* tm_info;
025ed0f3 2996 FILE *f;
f5dd1d53
SH
2997
2998 time(&timer);
2999 tm_info = localtime(&timer);
3000
3001 strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info);
3002
3003 char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5);
3004 sprintf(dfnam, "%s/%s/ts", snappath, newname);
025ed0f3 3005 f = fopen(dfnam, "w");
f5dd1d53 3006 if (!f) {
959aee9c 3007 ERROR("Failed to open %s", dfnam);
f5dd1d53
SH
3008 return -1;
3009 }
3010 if (fprintf(f, "%s", buffer) < 0) {
3011 SYSERROR("Writing timestamp");
3012 fclose(f);
3013 return -1;
3014 }
025ed0f3 3015 ret = fclose(f);
025ed0f3 3016 if (ret != 0) {
f5dd1d53
SH
3017 SYSERROR("Writing timestamp");
3018 return -1;
3019 }
3020
3021 if (commentfile) {
3022 // $p / $name / comment \0
3023 int len = strlen(snappath) + strlen(newname) + 10;
3024 char *path = alloca(len);
3025 sprintf(path, "%s/%s/comment", snappath, newname);
3026 return copy_file(commentfile, path) < 0 ? -1 : i;
3027 }
3028
3029 return i;
3030}
3031
3032static void lxcsnap_free(struct lxc_snapshot *s)
3033{
3034 if (s->name)
3035 free(s->name);
3036 if (s->comment_pathname)
3037 free(s->comment_pathname);
3038 if (s->timestamp)
3039 free(s->timestamp);
3040 if (s->lxcpath)
3041 free(s->lxcpath);
3042}
3043
3044static char *get_snapcomment_path(char* snappath, char *name)
3045{
3046 // $snappath/$name/comment
3047 int ret, len = strlen(snappath) + strlen(name) + 10;
3048 char *s = malloc(len);
3049
3050 if (s) {
3051 ret = snprintf(s, len, "%s/%s/comment", snappath, name);
3052 if (ret < 0 || ret >= len) {
3053 free(s);
3054 s = NULL;
3055 }
3056 }
3057 return s;
3058}
3059
3060static char *get_timestamp(char* snappath, char *name)
3061{
3062 char path[MAXPATHLEN], *s = NULL;
3063 int ret, len;
3064 FILE *fin;
3065
3066 ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name);
3067 if (ret < 0 || ret >= MAXPATHLEN)
3068 return NULL;
025ed0f3 3069 fin = fopen(path, "r");
025ed0f3 3070 if (!fin)
f5dd1d53
SH
3071 return NULL;
3072 (void) fseek(fin, 0, SEEK_END);
3073 len = ftell(fin);
3074 (void) fseek(fin, 0, SEEK_SET);
3075 if (len > 0) {
3076 s = malloc(len+1);
3077 if (s) {
3078 s[len] = '\0';
3079 if (fread(s, 1, len, fin) != len) {
3080 SYSERROR("reading timestamp");
3081 free(s);
3082 s = NULL;
3083 }
3084 }
3085 }
3086 fclose(fin);
3087 return s;
3088}
3089
3090static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps)
3091{
3092 char snappath[MAXPATHLEN], path2[MAXPATHLEN];
18aa217b 3093 int count = 0, ret;
f5dd1d53
SH
3094 struct dirent dirent, *direntp;
3095 struct lxc_snapshot *snaps =NULL, *nsnaps;
3096 DIR *dir;
3097
3098 if (!c || !lxcapi_is_defined(c))
3099 return -1;
c868b261 3100
18aa217b 3101 if (!get_snappath_dir(c, snappath)) {
f5dd1d53
SH
3102 ERROR("path name too long");
3103 return -1;
3104 }
025ed0f3 3105 dir = opendir(snappath);
025ed0f3 3106 if (!dir) {
f5dd1d53
SH
3107 INFO("failed to open %s - assuming no snapshots", snappath);
3108 return 0;
3109 }
3110
3111 while (!readdir_r(dir, &dirent, &direntp)) {
3112 if (!direntp)
3113 break;
3114
3115 if (!strcmp(direntp->d_name, "."))
3116 continue;
3117
3118 if (!strcmp(direntp->d_name, ".."))
3119 continue;
3120
3121 ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name);
3122 if (ret < 0 || ret >= MAXPATHLEN) {
3123 ERROR("pathname too long");
3124 goto out_free;
3125 }
3126 if (!file_exists(path2))
3127 continue;
3128 nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps));
3129 if (!nsnaps) {
3130 SYSERROR("Out of memory");
3131 goto out_free;
3132 }
3133 snaps = nsnaps;
3134 snaps[count].free = lxcsnap_free;
3135 snaps[count].name = strdup(direntp->d_name);
3136 if (!snaps[count].name)
3137 goto out_free;
3138 snaps[count].lxcpath = strdup(snappath);
3139 if (!snaps[count].lxcpath) {
3140 free(snaps[count].name);
3141 goto out_free;
3142 }
3143 snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name);
3144 snaps[count].timestamp = get_timestamp(snappath, direntp->d_name);
3145 count++;
3146 }
3147
3148 if (closedir(dir))
3149 WARN("failed to close directory");
3150
3151 *ret_snaps = snaps;
3152 return count;
3153
3154out_free:
3155 if (snaps) {
3156 int i;
3157 for (i=0; i<count; i++)
3158 lxcsnap_free(&snaps[i]);
3159 free(snaps);
3160 }
9baa57bd
SH
3161 if (closedir(dir))
3162 WARN("failed to close directory");
f5dd1d53
SH
3163 return -1;
3164}
3165
f0ca2726 3166static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname)
f5dd1d53
SH
3167{
3168 char clonelxcpath[MAXPATHLEN];
18aa217b 3169 int flags = 0;
f5dd1d53
SH
3170 struct lxc_container *snap, *rest;
3171 struct bdev *bdev;
3172 bool b = false;
3173
3174 if (!c || !c->name || !c->config_path)
3175 return false;
3176
18aa217b
SH
3177 if (has_fs_snapshots(c)) {
3178 ERROR("container rootfs has dependent snapshots");
3179 return false;
3180 }
3181
76a26f55 3182 bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
f5dd1d53
SH
3183 if (!bdev) {
3184 ERROR("Failed to find original backing store type");
3185 return false;
3186 }
3187
3188 if (!newname)
3189 newname = c->name;
7e36f87e 3190
18aa217b 3191 if (!get_snappath_dir(c, clonelxcpath)) {
f5dd1d53
SH
3192 bdev_put(bdev);
3193 return false;
3194 }
3195 // how should we lock this?
3196
3197 snap = lxc_container_new(snapname, clonelxcpath);
3198 if (!snap || !lxcapi_is_defined(snap)) {
3199 ERROR("Could not open snapshot %s", snapname);
3200 if (snap) lxc_container_put(snap);
3201 bdev_put(bdev);
3202 return false;
3203 }
3204
7e36f87e 3205 if (strcmp(c->name, newname) == 0) {
18aa217b 3206 if (!container_destroy(c)) {
7e36f87e
ÇO
3207 ERROR("Could not destroy existing container %s", newname);
3208 lxc_container_put(snap);
3209 bdev_put(bdev);
3210 return false;
3211 }
3212 }
3213
de269ee8
SH
3214 if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0)
3215 flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
3216 rest = lxcapi_clone(snap, newname, c->config_path, flags,
3217 bdev->type, NULL, 0, NULL);
f5dd1d53
SH
3218 bdev_put(bdev);
3219 if (rest && lxcapi_is_defined(rest))
3220 b = true;
3221 if (rest)
3222 lxc_container_put(rest);
3223 lxc_container_put(snap);
3224 return b;
3225}
3226
18aa217b 3227static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath)
771d96b3 3228{
771d96b3 3229 struct lxc_container *snap = NULL;
18aa217b 3230 bool bret = false;
771d96b3
ÇO
3231
3232 snap = lxc_container_new(snapname, clonelxcpath);
18aa217b 3233 if (!snap) {
771d96b3
ÇO
3234 ERROR("Could not find snapshot %s", snapname);
3235 goto err;
3236 }
3237
3238 if (!lxcapi_destroy(snap)) {
3239 ERROR("Could not destroy snapshot %s", snapname);
3240 goto err;
3241 }
18aa217b 3242 bret = true;
771d96b3 3243
771d96b3
ÇO
3244err:
3245 if (snap)
3246 lxc_container_put(snap);
18aa217b
SH
3247 return bret;
3248}
3249
3250static bool remove_all_snapshots(const char *path)
3251{
3252 DIR *dir;
3253 struct dirent dirent, *direntp;
3254 bool bret = true;
3255
3256 dir = opendir(path);
3257 if (!dir) {
3258 SYSERROR("opendir on snapshot path %s", path);
3259 return false;
3260 }
3261 while (!readdir_r(dir, &dirent, &direntp)) {
3262 if (!direntp)
3263 break;
3264 if (!strcmp(direntp->d_name, "."))
3265 continue;
3266 if (!strcmp(direntp->d_name, ".."))
3267 continue;
3268 if (!do_snapshot_destroy(direntp->d_name, path)) {
3269 bret = false;
3270 continue;
3271 }
3272 }
3273
3274 closedir(dir);
3275
3276 if (rmdir(path))
3277 SYSERROR("Error removing directory %s", path);
3278
3279 return bret;
3280}
3281
3282static bool lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname)
3283{
3284 char clonelxcpath[MAXPATHLEN];
3285
3286 if (!c || !c->name || !c->config_path || !snapname)
3287 return false;
3288
3289 if (!get_snappath_dir(c, clonelxcpath))
3290 return false;
3291
3292 return do_snapshot_destroy(snapname, clonelxcpath);
3293}
3294
3295static bool lxcapi_snapshot_destroy_all(struct lxc_container *c)
3296{
3297 char clonelxcpath[MAXPATHLEN];
3298
3299 if (!c || !c->name || !c->config_path)
3300 return false;
3301
3302 if (!get_snappath_dir(c, clonelxcpath))
3303 return false;
3304
3305 return remove_all_snapshots(clonelxcpath);
771d96b3
ÇO
3306}
3307
b494d2dd
SH
3308static bool lxcapi_may_control(struct lxc_container *c)
3309{
3310 return lxc_try_cmd(c->name, c->config_path) == 0;
3311}
3312
d5aa23e6
SH
3313static bool do_add_remove_node(pid_t init_pid, const char *path, bool add,
3314 struct stat *st)
3315{
3316 char chrootpath[MAXPATHLEN];
3317 char *directory_path = NULL;
3318 pid_t pid;
3319 int ret;
3320
3321 if ((pid = fork()) < 0) {
3322 SYSERROR("failed to fork a child helper");
3323 return false;
3324 }
3325 if (pid) {
3326 if (wait_for_pid(pid) != 0) {
3327 ERROR("Failed to create note in guest");
3328 return false;
3329 }
3330 return true;
3331 }
3332
3333 /* prepare the path */
3334 ret = snprintf(chrootpath, MAXPATHLEN, "/proc/%d/root", init_pid);
3335 if (ret < 0 || ret >= MAXPATHLEN)
3336 return false;
3337
6b9324bd 3338 if (chroot(chrootpath) < 0)
d5aa23e6 3339 exit(1);
6b9324bd 3340 if (chdir("/") < 0)
d5aa23e6
SH
3341 exit(1);
3342 /* remove path if it exists */
3343 if(faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW) == 0) {
3344 if (unlink(path) < 0) {
3345 ERROR("unlink failed");
3346 exit(1);
3347 }
3348 }
3349 if (!add)
3350 exit(0);
3351
3352 /* create any missing directories */
3353 directory_path = dirname(strdup(path));
3354 if (mkdir_p(directory_path, 0755) < 0 && errno != EEXIST) {
3355 ERROR("failed to create directory");
3356 exit(1);
3357 }
3358
3359 /* create the device node */
3360 if (mknod(path, st->st_mode, st->st_rdev) < 0) {
3361 ERROR("mknod failed");
3362 exit(1);
3363 }
3364
3365 exit(0);
3366}
3367
f0ca2726 3368static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add)
a9a0ed90
ÇO
3369{
3370 int ret;
3371 struct stat st;
a9a0ed90 3372 char value[MAX_BUFFER];
f0ca2726 3373 const char *p;
a9a0ed90
ÇO
3374
3375 /* make sure container is running */
3376 if (!c->is_running(c)) {
3377 ERROR("container is not running");
d5aa23e6 3378 return false;
a9a0ed90
ÇO
3379 }
3380
3381 /* use src_path if dest_path is NULL otherwise use dest_path */
3382 p = dest_path ? dest_path : src_path;
3383
a9a0ed90
ÇO
3384 /* make sure we can access p */
3385 if(access(p, F_OK) < 0 || stat(p, &st) < 0)
d5aa23e6 3386 return false;
a9a0ed90
ÇO
3387
3388 /* continue if path is character device or block device */
c6a9b0d7 3389 if (S_ISCHR(st.st_mode))
a9a0ed90 3390 ret = snprintf(value, MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev));
c6a9b0d7 3391 else if (S_ISBLK(st.st_mode))
a9a0ed90
ÇO
3392 ret = snprintf(value, MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev));
3393 else
d5aa23e6 3394 return false;
a9a0ed90
ÇO
3395
3396 /* check snprintf return code */
3397 if (ret < 0 || ret >= MAX_BUFFER)
d5aa23e6 3398 return false;
a9a0ed90 3399
d5aa23e6
SH
3400 if (!do_add_remove_node(c->init_pid(c), p, add, &st))
3401 return false;
a9a0ed90 3402
d5aa23e6 3403 /* add or remove device to/from cgroup access list */
a9a0ed90 3404 if (add) {
a9a0ed90
ÇO
3405 if (!c->set_cgroup_item(c, "devices.allow", value)) {
3406 ERROR("set_cgroup_item failed while adding the device node");
d5aa23e6 3407 return false;
a9a0ed90
ÇO
3408 }
3409 } else {
a9a0ed90
ÇO
3410 if (!c->set_cgroup_item(c, "devices.deny", value)) {
3411 ERROR("set_cgroup_item failed while removing the device node");
d5aa23e6 3412 return false;
a9a0ed90
ÇO
3413 }
3414 }
d5aa23e6 3415
a9a0ed90 3416 return true;
a9a0ed90
ÇO
3417}
3418
f0ca2726 3419static bool lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
a9a0ed90 3420{
c868b261
ÇO
3421 if (am_unpriv()) {
3422 ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
3423 return false;
3424 }
a9a0ed90
ÇO
3425 return add_remove_device_node(c, src_path, dest_path, true);
3426}
3427
f0ca2726 3428static bool lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
a9a0ed90 3429{
c868b261
ÇO
3430 if (am_unpriv()) {
3431 ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
3432 return false;
3433 }
a9a0ed90
ÇO
3434 return add_remove_device_node(c, src_path, dest_path, false);
3435}
3436
e58fae8f
DY
3437static bool lxcapi_attach_interface(struct lxc_container *c, const char *ifname,
3438 const char *dst_ifname)
3439{
3440 int ret = 0;
3441 if (am_unpriv()) {
3442 ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
3443 return false;
3444 }
3445
3446 if (!ifname) {
3447 ERROR("No source interface name given");
3448 return false;
3449 }
3450
3451 ret = lxc_netdev_isup(ifname);
e58fae8f 3452
e5848d39
SH
3453 if (ret > 0) {
3454 /* netdev of ifname is up. */
e58fae8f
DY
3455 ret = lxc_netdev_down(ifname);
3456 if (ret)
3457 goto err;
3458 }
3459
3460 ret = lxc_netdev_move_by_name(ifname, c->init_pid(c), dst_ifname);
3461 if (ret)
3462 goto err;
3463
3464 return true;
e5848d39 3465
e58fae8f 3466err:
e58fae8f
DY
3467 return false;
3468}
3469
3470static bool lxcapi_detach_interface(struct lxc_container *c, const char *ifname,
3471 const char *dst_ifname)
3472{
3473 pid_t pid, pid_outside;
3474
3475 if (am_unpriv()) {
3476 ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
3477 return false;
3478 }
3479
3480 if (!ifname) {
3481 ERROR("No source interface name given");
3482 return false;
3483 }
3484
3485 pid_outside = getpid();
3486 pid = fork();
3487 if (pid < 0) {
3488 ERROR("failed to fork task to get interfaces information");
3489 return false;
3490 }
3491
3492 if (pid == 0) { // child
3493 int ret = 0;
e0f59189 3494 if (!enter_net_ns(c)) {
e58fae8f
DY
3495 ERROR("failed to enter namespace");
3496 exit(-1);
3497 }
3498
3499 ret = lxc_netdev_isup(ifname);
3500 if (ret < 0)
3501 exit(ret);
3502
3503 /* netdev of ifname is up. */
3504 if (ret) {
3505 ret = lxc_netdev_down(ifname);
3506 if (ret)
3507 exit(ret);
3508 }
3509
3510 ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname);
3511
3512 /* -EINVAL means there is no netdev named as ifanme. */
3513 if (ret == -EINVAL) {
3514 ERROR("No network device named as %s.", ifname);
3515 }
3516 exit(ret);
3517 }
3518
3519 if (wait_for_pid(pid) != 0)
3520 return false;
3521
3522 return true;
3523}
3524
735f2c6e
TA
3525struct criu_opts {
3526 /* The type of criu invocation, one of "dump" or "restore" */
3527 char *action;
3528
3529 /* The directory to pass to criu */
3530 char *directory;
3531
3532 /* The container to dump */
3533 struct lxc_container *c;
3534
3535 /* Enable criu verbose mode? */
3536 bool verbose;
3537
3538 /* dump: stop the container or not after dumping? */
3539 bool stop;
3540
3541 /* restore: the file to write the init process' pid into */
3542 char *pidfile;
2ba7a429 3543 const char *cgroup_path;
735f2c6e
TA
3544};
3545
735f2c6e
TA
3546static void exec_criu(struct criu_opts *opts)
3547{
fed29fad 3548 char **argv, log[PATH_MAX], buf[257];
7c8f5230 3549 int static_args = 14, argc = 0, i, ret;
fed29fad
TA
3550 int netnr = 0;
3551 struct lxc_list *it;
735f2c6e
TA
3552
3553 /* The command line always looks like:
7c8f5230
TA
3554 * criu $(action) --tcp-established --file-locks --link-remap --force-irmap \
3555 * --manage-cgroups action-script foo.sh -D $(directory) \
3556 * -o $(directory)/$(action).log
735f2c6e
TA
3557 * +1 for final NULL */
3558
3559 if (strcmp(opts->action, "dump") == 0) {
3560 /* -t pid */
3561 static_args += 2;
3562
3563 /* --leave-running */
3564 if (!opts->stop)
3565 static_args++;
3566 } else if (strcmp(opts->action, "restore") == 0) {
2ba7a429
TA
3567 /* --root $(lxc_mount_point) --restore-detached
3568 * --restore-sibling --pidfile $foo --cgroup-root $foo */
3569 static_args += 8;
735f2c6e
TA
3570 } else {
3571 return;
3572 }
3573
3574 if (opts->verbose)
3575 static_args++;
3576
3577 ret = snprintf(log, PATH_MAX, "%s/%s.log", opts->directory, opts->action);
3578 if (ret < 0 || ret >= PATH_MAX) {
3579 ERROR("logfile name too long\n");
3580 return;
3581 }
3582
3583 argv = malloc(static_args * sizeof(*argv));
3584 if (!argv)
3585 return;
3586
3587 memset(argv, 0, static_args * sizeof(*argv));
3588
2566a145
TA
3589#define DECLARE_ARG(arg) \
3590 do { \
3591 if (arg == NULL) { \
3592 ERROR("Got NULL argument for criu"); \
3593 goto err; \
3594 } \
3595 argv[argc++] = strdup(arg); \
3596 if (!argv[argc-1]) \
3597 goto err; \
735f2c6e
TA
3598 } while (0)
3599
3600 argv[argc++] = on_path("criu", NULL);
3601 if (!argv[argc-1]) {
3602 ERROR("Couldn't find criu binary\n");
3603 goto err;
3604 }
3605
3606 DECLARE_ARG(opts->action);
3607 DECLARE_ARG("--tcp-established");
3608 DECLARE_ARG("--file-locks");
3609 DECLARE_ARG("--link-remap");
7c8f5230 3610 DECLARE_ARG("--force-irmap");
735f2c6e
TA
3611 DECLARE_ARG("--manage-cgroups");
3612 DECLARE_ARG("--action-script");
0080bebf 3613 DECLARE_ARG(DATADIR "/lxc/lxc-restore-net");
735f2c6e
TA
3614 DECLARE_ARG("-D");
3615 DECLARE_ARG(opts->directory);
3616 DECLARE_ARG("-o");
3617 DECLARE_ARG(log);
3618
3619 if (opts->verbose)
3620 DECLARE_ARG("-vvvvvv");
3621
3622 if (strcmp(opts->action, "dump") == 0) {
3623 char pid[32];
3624
3625 if (sprintf(pid, "%d", lxcapi_init_pid(opts->c)) < 0)
3626 goto err;
3627
3628 DECLARE_ARG("-t");
3629 DECLARE_ARG(pid);
3630 if (!opts->stop)
3631 DECLARE_ARG("--leave-running");
3632 } else if (strcmp(opts->action, "restore") == 0) {
735f2c6e
TA
3633 DECLARE_ARG("--root");
3634 DECLARE_ARG(opts->c->lxc_conf->rootfs.mount);
3635 DECLARE_ARG("--restore-detached");
74bcefea 3636 DECLARE_ARG("--restore-sibling");
735f2c6e
TA
3637 DECLARE_ARG("--pidfile");
3638 DECLARE_ARG(opts->pidfile);
2ba7a429
TA
3639 DECLARE_ARG("--cgroup-root");
3640 DECLARE_ARG(opts->cgroup_path);
735f2c6e
TA
3641
3642 lxc_list_for_each(it, &opts->c->lxc_conf->network) {
fed29fad 3643 char eth[128], *veth;
735f2c6e 3644 void *m;
fed29fad
TA
3645 struct lxc_netdev *n = it->elem;
3646
3647 if (n->name) {
e311a562 3648 if (strlen(n->name) >= sizeof(eth))
fed29fad 3649 goto err;
e311a562 3650 strncpy(eth, n->name, sizeof(eth));
fed29fad
TA
3651 } else
3652 sprintf(eth, "eth%d", netnr);
3653
3654 veth = n->priv.veth_attr.pair;
735f2c6e 3655
e311a562
TA
3656 ret = snprintf(buf, sizeof(buf), "%s=%s", eth, veth);
3657 if (ret < 0 || ret >= sizeof(buf))
735f2c6e
TA
3658 goto err;
3659
fed29fad 3660 /* final NULL and --veth-pair eth0=vethASDF */
735f2c6e
TA
3661 m = realloc(argv, (argc + 1 + 2) * sizeof(*argv));
3662 if (!m)
3663 goto err;
3664 argv = m;
3665
3666 DECLARE_ARG("--veth-pair");
3667 DECLARE_ARG(buf);
3668 argv[argc] = NULL;
3669
735f2c6e
TA
3670 }
3671 }
3672
fed29fad
TA
3673 netnr = 0;
3674 lxc_list_for_each(it, &opts->c->lxc_conf->network) {
3675 struct lxc_netdev *n = it->elem;
3676 char veth[128];
3677
3678 /*
3679 * Here, we set some parameters that lxc-restore-net
3680 * will examine to figure out the right network to
3681 * restore.
3682 */
3683 snprintf(buf, sizeof(buf), "LXC_CRIU_BRIDGE%d", netnr);
3684 if (setenv(buf, n->link, 1))
3685 goto err;
3686
3687 if (strcmp("restore", opts->action) == 0)
3688 strncpy(veth, n->priv.veth_attr.pair, sizeof(veth));
3689 else {
3690 char *tmp;
3691 ret = snprintf(buf, sizeof(buf), "lxc.network.%d.veth.pair", netnr);
3692 if (ret < 0 || ret >= sizeof(buf))
3693 goto err;
3694 tmp = lxcapi_get_running_config_item(opts->c, buf);
3695 strncpy(veth, tmp, sizeof(veth));
3696 free(tmp);
3697 }
3698
3699 snprintf(buf, sizeof(buf), "LXC_CRIU_VETH%d", netnr);
3700 if (setenv(buf, veth, 1))
3701 goto err;
735f2c6e 3702
fed29fad
TA
3703 netnr++;
3704 }
3705
3706#undef DECLARE_ARG
735f2c6e
TA
3707 execv(argv[0], argv);
3708err:
3709 for (i = 0; argv[i]; i++)
3710 free(argv[i]);
3711 free(argv);
3712}
3713
3714/* Check and make sure the container has a configuration that we know CRIU can
3715 * dump. */
3716static bool criu_ok(struct lxc_container *c)
3717{
3718 struct lxc_list *it;
3719 bool found_deny_rule = false;
3720
3721 if (geteuid()) {
3722 ERROR("Must be root to checkpoint\n");
3723 return false;
3724 }
3725
3726 /* We only know how to restore containers with veth networks. */
3727 lxc_list_for_each(it, &c->lxc_conf->network) {
3728 struct lxc_netdev *n = it->elem;
3729 if (n->type != LXC_NET_VETH && n->type != LXC_NET_NONE) {
3730 ERROR("Found network that is not VETH or NONE\n");
3731 return false;
3732 }
3733 }
3734
3735 // These requirements come from http://criu.org/LXC
3736 if (c->lxc_conf->console.path &&
3737 strcmp(c->lxc_conf->console.path, "none") != 0) {
3738 ERROR("lxc.console must be none\n");
3739 return false;
3740 }
3741
3742 if (c->lxc_conf->tty != 0) {
3743 ERROR("lxc.tty must be 0\n");
3744 return false;
3745 }
3746
3747 lxc_list_for_each(it, &c->lxc_conf->cgroup) {
3748 struct lxc_cgroup *cg = it->elem;
3749 if (strcmp(cg->subsystem, "devices.deny") == 0 &&
3750 strcmp(cg->value, "c 5:1 rwm") == 0) {
3751
3752 found_deny_rule = true;
3753 break;
3754 }
3755 }
3756
3757 if (!found_deny_rule) {
3758 ERROR("couldn't find devices.deny = c 5:1 rwm");
3759 return false;
3760 }
3761
3762 return true;
3763}
3764
bbd4e13e 3765static bool dump_net_info(struct lxc_container *c, char *directory)
735f2c6e 3766{
bbd4e13e 3767 int netnr;
735f2c6e 3768 struct lxc_list *it;
735f2c6e
TA
3769
3770 netnr = 0;
3771 lxc_list_for_each(it, &c->lxc_conf->network) {
3772 char *veth = NULL, *bridge = NULL, veth_path[PATH_MAX], eth[128];
3773 struct lxc_netdev *n = it->elem;
bbd4e13e 3774 bool has_error = true;
735f2c6e
TA
3775 int pret;
3776
3777 pret = snprintf(veth_path, PATH_MAX, "lxc.network.%d.veth.pair", netnr);
bbd4e13e 3778 if (pret < 0 || pret >= PATH_MAX)
735f2c6e 3779 goto out;
735f2c6e
TA
3780
3781 veth = lxcapi_get_running_config_item(c, veth_path);
3782 if (!veth) {
3783 /* criu_ok() checks that all interfaces are
3784 * LXC_NET{VETH,NONE}, and VETHs should have this
3785 * config */
3786 assert(n->type == LXC_NET_NONE);
3787 break;
3788 }
3789
735f2c6e 3790 bridge = lxcapi_get_running_config_item(c, veth_path);
bbd4e13e 3791 if (!bridge)
735f2c6e 3792 goto out;
735f2c6e
TA
3793
3794 pret = snprintf(veth_path, PATH_MAX, "%s/veth%d", directory, netnr);
bbd4e13e 3795 if (pret < 0 || pret >= PATH_MAX || print_to_file(veth_path, veth) < 0)
735f2c6e 3796 goto out;
735f2c6e 3797
735f2c6e 3798 if (n->name) {
bbd4e13e 3799 if (strlen(n->name) >= 128)
735f2c6e 3800 goto out;
735f2c6e
TA
3801 strncpy(eth, n->name, 128);
3802 } else
3803 sprintf(eth, "eth%d", netnr);
3804
bbd4e13e 3805 has_error = false;
735f2c6e 3806out:
bbd4e13e
TA
3807 if (veth)
3808 free(veth);
cc4ae28d 3809 if (bridge)
bbd4e13e
TA
3810 free(bridge);
3811 if (has_error)
735f2c6e
TA
3812 return false;
3813 }
3814
bbd4e13e
TA
3815 return true;
3816}
3817
3818static bool lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose)
3819{
3820 pid_t pid;
3821 int status;
3822
3823 if (!criu_ok(c))
3824 return false;
3825
3826 if (mkdir(directory, 0700) < 0 && errno != EEXIST)
3827 return false;
3828
3829 if (!dump_net_info(c, directory))
3830 return false;
3831
735f2c6e
TA
3832 pid = fork();
3833 if (pid < 0)
3834 return false;
3835
3836 if (pid == 0) {
3837 struct criu_opts os;
3838
3839 os.action = "dump";
3840 os.directory = directory;
3841 os.c = c;
3842 os.stop = stop;
3843 os.verbose = verbose;
3844
3845 /* exec_criu() returning is an error */
3846 exec_criu(&os);
3847 exit(1);
3848 } else {
3849 pid_t w = waitpid(pid, &status, 0);
3850 if (w == -1) {
3851 perror("waitpid");
3852 return false;
3853 }
3854
3855 if (WIFEXITED(status)) {
3856 return !WEXITSTATUS(status);
3857 }
3858
3859 return false;
3860 }
3861}
3862
fed29fad 3863static bool restore_net_info(struct lxc_container *c)
bbd4e13e
TA
3864{
3865 struct lxc_list *it;
3866 bool has_error = true;
bbd4e13e
TA
3867
3868 if (container_mem_lock(c))
3869 return false;
3870
3871 lxc_list_for_each(it, &c->lxc_conf->network) {
bbd4e13e 3872 struct lxc_netdev *netdev = it->elem;
fed29fad
TA
3873 char template[IFNAMSIZ];
3874 snprintf(template, sizeof(template), "vethXXXXXX");
bbd4e13e 3875
fed29fad
TA
3876 if (!netdev->priv.veth_attr.pair)
3877 netdev->priv.veth_attr.pair = lxc_mkifname(template);
bbd4e13e 3878
bbd4e13e
TA
3879 if (!netdev->priv.veth_attr.pair)
3880 goto out_unlock;
bbd4e13e
TA
3881 }
3882
3883 has_error = false;
3884
3885out_unlock:
3886 container_mem_unlock(c);
3887 return !has_error;
3888}
3889
735f2c6e
TA
3890static bool lxcapi_restore(struct lxc_container *c, char *directory, bool verbose)
3891{
3892 pid_t pid;
735f2c6e
TA
3893 struct lxc_rootfs *rootfs;
3894 char pidfile[L_tmpnam];
dbb51a43
TA
3895 struct lxc_handler *handler;
3896 bool has_error = true;
735f2c6e
TA
3897
3898 if (!criu_ok(c))
3899 return false;
3900
3901 if (geteuid()) {
3902 ERROR("Must be root to restore\n");
3903 return false;
3904 }
3905
3906 if (!tmpnam(pidfile))
3907 return false;
3908
dbb51a43
TA
3909 handler = lxc_init(c->name, c->lxc_conf, c->config_path);
3910 if (!handler)
3911 return false;
3912
3913 if (!cgroup_init(handler)) {
3914 ERROR("failed initing cgroups");
3915 goto out_fini_handler;
3916 }
3917
2ba7a429
TA
3918 if (!cgroup_create(handler)) {
3919 ERROR("failed creating groups");
3920 goto out_fini_handler;
3921 }
3922
fed29fad
TA
3923 if (!restore_net_info(c)) {
3924 ERROR("failed restoring network info");
3925 goto out_fini_handler;
3926 }
3927
735f2c6e
TA
3928 pid = fork();
3929 if (pid < 0)
dbb51a43 3930 goto out_fini_handler;
735f2c6e
TA
3931
3932 if (pid == 0) {
3933 struct criu_opts os;
3934
3935 if (unshare(CLONE_NEWNS))
6d5b330d 3936 exit(1);
735f2c6e
TA
3937
3938 /* CRIU needs the lxc root bind mounted so that it is the root of some
3939 * mount. */
3940 rootfs = &c->lxc_conf->rootfs;
3941
3942 if (rootfs_is_blockdev(c->lxc_conf)) {
3943 if (do_rootfs_setup(c->lxc_conf, c->name, c->config_path) < 0)
6d5b330d 3944 exit(1);
735f2c6e
TA
3945 }
3946 else {
3947 if (mkdir(rootfs->mount, 0755) < 0 && errno != EEXIST)
6d5b330d 3948 exit(1);
735f2c6e
TA
3949
3950 if (mount(rootfs->path, rootfs->mount, NULL, MS_BIND, NULL) < 0) {
3951 rmdir(rootfs->mount);
6d5b330d 3952 exit(1);
735f2c6e
TA
3953 }
3954 }
3955
3956 os.action = "restore";
3957 os.directory = directory;
3958 os.c = c;
3959 os.pidfile = pidfile;
3960 os.verbose = verbose;
2ba7a429 3961 os.cgroup_path = cgroup_canonical_path(handler);
735f2c6e
TA
3962
3963 /* exec_criu() returning is an error */
3964 exec_criu(&os);
3965 umount(rootfs->mount);
3966 rmdir(rootfs->mount);
3967 exit(1);
3968 } else {
3969 int status;
3b72c4a0 3970
735f2c6e
TA
3971 pid_t w = waitpid(pid, &status, 0);
3972
3973 if (w == -1) {
3974 perror("waitpid");
dbb51a43 3975 goto out_fini_handler;
735f2c6e
TA
3976 }
3977
3978 if (WIFEXITED(status)) {
3979 if (WEXITSTATUS(status)) {
3b72c4a0 3980 goto out_fini_handler;
735f2c6e
TA
3981 }
3982 else {
bbd4e13e 3983 int ret;
735f2c6e
TA
3984 FILE *f = fopen(pidfile, "r");
3985 if (!f) {
3986 perror("reading pidfile");
3987 ERROR("couldn't read restore's init pidfile %s\n", pidfile);
3b72c4a0 3988 goto out_fini_handler;
735f2c6e
TA
3989 }
3990
3991 ret = fscanf(f, "%d", (int*) &handler->pid);
3992 fclose(f);
3993 if (ret != 1) {
3994 ERROR("reading restore pid failed");
3b72c4a0 3995 goto out_fini_handler;
735f2c6e
TA
3996 }
3997
dbb51a43 3998 if (lxc_set_state(c->name, handler, RUNNING))
3b72c4a0 3999 goto out_fini_handler;
735f2c6e 4000 }
c49ecd78
TA
4001 } else {
4002 ERROR("CRIU was killed with signal %d\n", WTERMSIG(status));
c49ecd78 4003 goto out_fini_handler;
735f2c6e
TA
4004 }
4005
4006 if (lxc_poll(c->name, handler)) {
4007 lxc_abort(c->name, handler);
dbb51a43 4008 goto out_fini_handler;
735f2c6e 4009 }
dbb51a43
TA
4010 }
4011
4012 has_error = false;
735f2c6e 4013
3b72c4a0 4014out_fini_handler:
dbb51a43
TA
4015 lxc_fini(c->name, handler);
4016 return !has_error;
735f2c6e
TA
4017}
4018
a0e93eeb
CS
4019static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
4020{
4021 va_list ap;
4022 const char **argv;
4023 int ret;
4024
4025 if (!c)
4026 return -1;
4027
4028 va_start(ap, arg);
4029 argv = lxc_va_arg_list_to_argv_const(ap, 1);
4030 va_end(ap);
4031
4032 if (!argv) {
4033 ERROR("Memory allocation error.");
4034 return -1;
4035 }
4036 argv[0] = arg;
4037
4038 ret = lxcapi_attach_run_wait(c, options, program, (const char * const *)argv);
4039 free((void*)argv);
4040 return ret;
4041}
4042
afeecbba 4043struct lxc_container *lxc_container_new(const char *name, const char *configpath)
72d0e1cb
SG
4044{
4045 struct lxc_container *c;
72d0e1cb 4046
18aa217b
SH
4047 if (!name)
4048 return NULL;
4049
72d0e1cb
SG
4050 c = malloc(sizeof(*c));
4051 if (!c) {
4052 fprintf(stderr, "failed to malloc lxc_container\n");
4053 return NULL;
4054 }
4055 memset(c, 0, sizeof(*c));
4056
afeecbba
SH
4057 if (configpath)
4058 c->config_path = strdup(configpath);
4059 else
593e8478 4060 c->config_path = strdup(lxc_global_config_value("lxc.lxcpath"));
afeecbba 4061
2a59a681 4062 if (!c->config_path) {
03fadd16 4063 fprintf(stderr, "Out of memory\n");
2a59a681
SH
4064 goto err;
4065 }
4066
f5dd1d53 4067 remove_trailing_slashes(c->config_path);
72d0e1cb
SG
4068 c->name = malloc(strlen(name)+1);
4069 if (!c->name) {
4070 fprintf(stderr, "Error allocating lxc_container name\n");
4071 goto err;
4072 }
4073 strcpy(c->name, name);
4074
4075 c->numthreads = 1;
df271a59 4076 if (!(c->slock = lxc_newlock(c->config_path, name))) {
72d0e1cb
SG
4077 fprintf(stderr, "failed to create lock\n");
4078 goto err;
4079 }
4080
df271a59 4081 if (!(c->privlock = lxc_newlock(NULL, NULL))) {
72d0e1cb
SG
4082 fprintf(stderr, "failed to alloc privlock\n");
4083 goto err;
4084 }
4085
afeecbba 4086 if (!set_config_filename(c)) {
72d0e1cb
SG
4087 fprintf(stderr, "Error allocating config file pathname\n");
4088 goto err;
4089 }
72d0e1cb 4090
bac806d1
SH
4091 if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL))
4092 goto err;
72d0e1cb 4093
3e625e2d
SH
4094 if (ongoing_create(c) == 2) {
4095 ERROR("Error: %s creation was not completed", c->name);
18aa217b 4096 container_destroy(c);
4df7f012 4097 lxcapi_clear_config(c);
3e625e2d 4098 }
a2739df5 4099 c->daemonize = true;
72cf75fa 4100 c->pidfile = NULL;
3e625e2d 4101
72d0e1cb
SG
4102 // assign the member functions
4103 c->is_defined = lxcapi_is_defined;
4104 c->state = lxcapi_state;
4105 c->is_running = lxcapi_is_running;
4106 c->freeze = lxcapi_freeze;
4107 c->unfreeze = lxcapi_unfreeze;
0115f8fd 4108 c->console = lxcapi_console;
b5159817 4109 c->console_getfd = lxcapi_console_getfd;
72d0e1cb
SG
4110 c->init_pid = lxcapi_init_pid;
4111 c->load_config = lxcapi_load_config;
4112 c->want_daemonize = lxcapi_want_daemonize;
130a1888 4113 c->want_close_all_fds = lxcapi_want_close_all_fds;
72d0e1cb
SG
4114 c->start = lxcapi_start;
4115 c->startl = lxcapi_startl;
4116 c->stop = lxcapi_stop;
4117 c->config_file_name = lxcapi_config_file_name;
4118 c->wait = lxcapi_wait;
4119 c->set_config_item = lxcapi_set_config_item;
4120 c->destroy = lxcapi_destroy;
18aa217b 4121 c->destroy_with_snapshots = lxcapi_destroy_with_snapshots;
06e5650e 4122 c->rename = lxcapi_rename;
72d0e1cb
SG
4123 c->save_config = lxcapi_save_config;
4124 c->get_keys = lxcapi_get_keys;
4125 c->create = lxcapi_create;
4126 c->createl = lxcapi_createl;
4127 c->shutdown = lxcapi_shutdown;
3e625e2d 4128 c->reboot = lxcapi_reboot;
4df7f012 4129 c->clear_config = lxcapi_clear_config;
72d0e1cb
SG
4130 c->clear_config_item = lxcapi_clear_config_item;
4131 c->get_config_item = lxcapi_get_config_item;
8ac18377 4132 c->get_running_config_item = lxcapi_get_running_config_item;
794dd120
SH
4133 c->get_cgroup_item = lxcapi_get_cgroup_item;
4134 c->set_cgroup_item = lxcapi_set_cgroup_item;
2a59a681
SH
4135 c->get_config_path = lxcapi_get_config_path;
4136 c->set_config_path = lxcapi_set_config_path;
9be53773 4137 c->clone = lxcapi_clone;
799f29ab 4138 c->get_interfaces = lxcapi_get_interfaces;
9c83a661 4139 c->get_ips = lxcapi_get_ips;
a0e93eeb
CS
4140 c->attach = lxcapi_attach;
4141 c->attach_run_wait = lxcapi_attach_run_wait;
4142 c->attach_run_waitl = lxcapi_attach_run_waitl;
f5dd1d53
SH
4143 c->snapshot = lxcapi_snapshot;
4144 c->snapshot_list = lxcapi_snapshot_list;
4145 c->snapshot_restore = lxcapi_snapshot_restore;
771d96b3 4146 c->snapshot_destroy = lxcapi_snapshot_destroy;
18aa217b 4147 c->snapshot_destroy_all = lxcapi_snapshot_destroy_all;
b494d2dd 4148 c->may_control = lxcapi_may_control;
a9a0ed90
ÇO
4149 c->add_device_node = lxcapi_add_device_node;
4150 c->remove_device_node = lxcapi_remove_device_node;
e58fae8f
DY
4151 c->attach_interface = lxcapi_attach_interface;
4152 c->detach_interface = lxcapi_detach_interface;
735f2c6e
TA
4153 c->checkpoint = lxcapi_checkpoint;
4154 c->restore = lxcapi_restore;
72d0e1cb
SG
4155
4156 /* we'll allow the caller to update these later */
ab1bf971 4157 if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) {
72d0e1cb
SG
4158 fprintf(stderr, "failed to open log\n");
4159 goto err;
4160 }
4161
72d0e1cb
SG
4162 return c;
4163
4164err:
4165 lxc_container_free(c);
4166 return NULL;
4167}
4168
4a7c7daa 4169int lxc_get_wait_states(const char **states)
72d0e1cb
SG
4170{
4171 int i;
4172
4173 if (states)
4174 for (i=0; i<MAX_STATE; i++)
4175 states[i] = lxc_state2str(i);
4176 return MAX_STATE;
4177}
a41f104b 4178
a41f104b
SH
4179/*
4180 * These next two could probably be done smarter with reusing a common function
4181 * with different iterators and tests...
4182 */
4183int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret)
4184{
4185 DIR *dir;
4186 int i, cfound = 0, nfound = 0;
4187 struct dirent dirent, *direntp;
4188 struct lxc_container *c;
4189
4190 if (!lxcpath)
593e8478 4191 lxcpath = lxc_global_config_value("lxc.lxcpath");
a41f104b 4192
a41f104b 4193 dir = opendir(lxcpath);
a41f104b
SH
4194 if (!dir) {
4195 SYSERROR("opendir on lxcpath");
4196 return -1;
4197 }
4198
4199 if (cret)
4200 *cret = NULL;
4201 if (names)
4202 *names = NULL;
4203
4204 while (!readdir_r(dir, &dirent, &direntp)) {
4205 if (!direntp)
4206 break;
4207 if (!strcmp(direntp->d_name, "."))
4208 continue;
4209 if (!strcmp(direntp->d_name, ".."))
4210 continue;
4211
4212 if (!config_file_exists(lxcpath, direntp->d_name))
4213 continue;
4214
4215 if (names) {
9c88ff1f 4216 if (!add_to_array(names, direntp->d_name, cfound))
a41f104b
SH
4217 goto free_bad;
4218 }
4219 cfound++;
4220
4221 if (!cret) {
4222 nfound++;
4223 continue;
4224 }
4225
4226 c = lxc_container_new(direntp->d_name, lxcpath);
4227 if (!c) {
4228 INFO("Container %s:%s has a config but could not be loaded",
4229 lxcpath, direntp->d_name);
4230 if (names)
9c88ff1f
ÇO
4231 if(!remove_from_array(names, direntp->d_name, cfound--))
4232 goto free_bad;
a41f104b
SH
4233 continue;
4234 }
4235 if (!lxcapi_is_defined(c)) {
4236 INFO("Container %s:%s has a config but is not defined",
4237 lxcpath, direntp->d_name);
4238 if (names)
9c88ff1f
ÇO
4239 if(!remove_from_array(names, direntp->d_name, cfound--))
4240 goto free_bad;
a41f104b
SH
4241 lxc_container_put(c);
4242 continue;
4243 }
4244
2871830a 4245 if (!add_to_clist(cret, c, nfound, true)) {
a41f104b
SH
4246 lxc_container_put(c);
4247 goto free_bad;
4248 }
4249 nfound++;
4250 }
4251
a41f104b 4252 closedir(dir);
a41f104b
SH
4253 return nfound;
4254
4255free_bad:
4256 if (names && *names) {
4257 for (i=0; i<cfound; i++)
4258 free((*names)[i]);
4259 free(*names);
4260 }
4261 if (cret && *cret) {
4262 for (i=0; i<nfound; i++)
4263 lxc_container_put((*cret)[i]);
4264 free(*cret);
4265 }
a41f104b 4266 closedir(dir);
a41f104b
SH
4267 return -1;
4268}
4269
148a9d27
DE
4270int list_active_containers(const char *lxcpath, char ***nret,
4271 struct lxc_container ***cret)
a41f104b 4272{
148a9d27 4273 int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0;
a41f104b
SH
4274 int lxcpath_len;
4275 char *line = NULL;
148a9d27 4276 char **ct_name = NULL;
a41f104b
SH
4277 size_t len = 0;
4278 struct lxc_container *c;
88556fd7 4279 bool is_hashed;
a41f104b
SH
4280
4281 if (!lxcpath)
593e8478 4282 lxcpath = lxc_global_config_value("lxc.lxcpath");
a41f104b
SH
4283 lxcpath_len = strlen(lxcpath);
4284
4285 if (cret)
4286 *cret = NULL;
148a9d27
DE
4287 if (nret)
4288 *nret = NULL;
a41f104b 4289
a41f104b 4290 FILE *f = fopen("/proc/net/unix", "r");
a41f104b
SH
4291 if (!f)
4292 return -1;
4293
4294 while (getline(&line, &len, f) != -1) {
88556fd7 4295
0f8f9c8a 4296 char *p = strrchr(line, ' '), *p2;
a41f104b
SH
4297 if (!p)
4298 continue;
4299 p++;
4300 if (*p != 0x40)
4301 continue;
4302 p++;
88556fd7
ÇO
4303
4304 is_hashed = false;
4305 if (strncmp(p, lxcpath, lxcpath_len) == 0) {
4306 p += lxcpath_len;
4307 } else if (strncmp(p, "lxc/", 4) == 0) {
4308 p += 4;
4309 is_hashed = true;
4310 } else {
a41f104b 4311 continue;
88556fd7
ÇO
4312 }
4313
a41f104b
SH
4314 while (*p == '/')
4315 p++;
4316
4317 // Now p is the start of lxc_name
4318 p2 = index(p, '/');
4319 if (!p2 || strncmp(p2, "/command", 8) != 0)
4320 continue;
4321 *p2 = '\0';
4322
88556fd7
ÇO
4323 if (is_hashed) {
4324 if (strncmp(lxcpath, lxc_cmd_get_lxcpath(p), lxcpath_len) != 0)
4325 continue;
4326 p = lxc_cmd_get_name(p);
4327 }
4328
148a9d27 4329 if (array_contains(&ct_name, p, ct_name_cnt))
9c88ff1f
ÇO
4330 continue;
4331
148a9d27
DE
4332 if (!add_to_array(&ct_name, p, ct_name_cnt))
4333 goto free_cret_list;
9c88ff1f 4334
148a9d27 4335 ct_name_cnt++;
a41f104b 4336
148a9d27 4337 if (!cret)
a41f104b 4338 continue;
a41f104b
SH
4339
4340 c = lxc_container_new(p, lxcpath);
4341 if (!c) {
4342 INFO("Container %s:%s is running but could not be loaded",
4343 lxcpath, p);
148a9d27 4344 remove_from_array(&ct_name, p, ct_name_cnt--);
a41f104b
SH
4345 continue;
4346 }
4347
4348 /*
4349 * If this is an anonymous container, then is_defined *can*
4350 * return false. So we don't do that check. Count on the
4351 * fact that the command socket exists.
4352 */
4353
148a9d27 4354 if (!add_to_clist(cret, c, cret_cnt, true)) {
a41f104b 4355 lxc_container_put(c);
148a9d27 4356 goto free_cret_list;
a41f104b 4357 }
148a9d27 4358 cret_cnt++;
a41f104b
SH
4359 }
4360
148a9d27
DE
4361 assert(!nret || !cret || cret_cnt == ct_name_cnt);
4362 ret = ct_name_cnt;
4363 if (nret)
4364 *nret = ct_name;
4365 else
4366 goto free_ct_name;
4367 goto out;
a41f104b 4368
148a9d27 4369free_cret_list:
a41f104b 4370 if (cret && *cret) {
148a9d27 4371 for (i = 0; i < cret_cnt; i++)
a41f104b
SH
4372 lxc_container_put((*cret)[i]);
4373 free(*cret);
4374 }
148a9d27
DE
4375
4376free_ct_name:
4377 if (ct_name) {
4378 for (i = 0; i < ct_name_cnt; i++)
4379 free(ct_name[i]);
4380 free(ct_name);
4381 }
4382
4383out:
e853a32d
ÇO
4384 if (line)
4385 free(line);
4386
a41f104b 4387 fclose(f);
148a9d27 4388 return ret;
a41f104b 4389}
2871830a
DE
4390
4391int list_all_containers(const char *lxcpath, char ***nret,
4392 struct lxc_container ***cret)
4393{
4394 int i, ret, active_cnt, ct_cnt, ct_list_cnt;
4395 char **active_name;
4396 char **ct_name;
4397 struct lxc_container **ct_list = NULL;
4398
4399 ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL);
4400 if (ct_cnt < 0)
4401 return ct_cnt;
4402
4403 active_cnt = list_active_containers(lxcpath, &active_name, NULL);
4404 if (active_cnt < 0) {
4405 ret = active_cnt;
4406 goto free_ct_name;
4407 }
4408
4409 for (i = 0; i < active_cnt; i++) {
4410 if (!array_contains(&ct_name, active_name[i], ct_cnt)) {
4411 if (!add_to_array(&ct_name, active_name[i], ct_cnt)) {
4412 ret = -1;
4413 goto free_active_name;
4414 }
4415 ct_cnt++;
4416 }
4417 free(active_name[i]);
4418 active_name[i] = NULL;
4419 }
4420 free(active_name);
4421 active_name = NULL;
4422 active_cnt = 0;
4423
4424 for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) {
4425 struct lxc_container *c;
4426
4427 c = lxc_container_new(ct_name[i], lxcpath);
4428 if (!c) {
4429 WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]);
4430 remove_from_array(&ct_name, ct_name[i], ct_cnt--);
4431 continue;
4432 }
4433
4434 if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) {
4435 lxc_container_put(c);
4436 ret = -1;
4437 goto free_ct_list;
4438 }
4439 ct_list_cnt++;
4440 }
4441
4442 if (cret)
4443 *cret = ct_list;
4444
4445 if (nret)
4446 *nret = ct_name;
4447 else {
4448 ret = ct_cnt;
4449 goto free_ct_name;
4450 }
4451 return ct_cnt;
4452
4453free_ct_list:
4454 for (i = 0; i < ct_list_cnt; i++) {
4455 lxc_container_put(ct_list[i]);
4456 }
4457 if (ct_list)
4458 free(ct_list);
4459
4460free_active_name:
4461 for (i = 0; i < active_cnt; i++) {
4462 if (active_name[i])
4463 free(active_name[i]);
4464 }
4465 if (active_name)
4466 free(active_name);
4467
4468free_ct_name:
4469 for (i = 0; i < ct_cnt; i++) {
4470 free(ct_name[i]);
4471 }
4472 free(ct_name);
4473 return ret;
4474}