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