"Fossies" - the Fresh Open Source Software Archive

Member "lxc-4.0.10/src/lxc/lxccontainer.c" (16 Jul 2021, 130197 Bytes) of package /linux/misc/lxc-4.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lxccontainer.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.0.9_vs_4.0.10.

    1 /* SPDX-License-Identifier: LGPL-2.1+ */
    2 
    3 #ifndef _GNU_SOURCE
    4 #define _GNU_SOURCE 1
    5 #endif
    6 #include <arpa/inet.h>
    7 #include <dirent.h>
    8 #include <errno.h>
    9 #include <fcntl.h>
   10 #include <grp.h>
   11 #include <libgen.h>
   12 #include <pthread.h>
   13 #include <sched.h>
   14 #include <stdarg.h>
   15 #include <stdint.h>
   16 #include <stdio.h>
   17 #include <stdlib.h>
   18 #include <sys/file.h>
   19 #include <sys/mman.h>
   20 #include <sys/mount.h>
   21 #include <sys/stat.h>
   22 #include <sys/syscall.h>
   23 #include <sys/sysmacros.h>
   24 #include <sys/types.h>
   25 #include <sys/wait.h>
   26 #include <unistd.h>
   27 
   28 #include "../include/netns_ifaddrs.h"
   29 #include "af_unix.h"
   30 #include "api_extensions.h"
   31 #include "attach.h"
   32 #include "cgroup.h"
   33 #include "macro.h"
   34 #include "commands.h"
   35 #include "commands_utils.h"
   36 #include "conf.h"
   37 #include "config.h"
   38 #include "confile.h"
   39 #include "confile_utils.h"
   40 #include "criu.h"
   41 #include "error.h"
   42 #include "initutils.h"
   43 #include "log.h"
   44 #include "lxc.h"
   45 #include "lxccontainer.h"
   46 #include "lxclock.h"
   47 #include "memory_utils.h"
   48 #include "monitor.h"
   49 #include "namespace.h"
   50 #include "network.h"
   51 #include "parse.h"
   52 #include "process_utils.h"
   53 #include "start.h"
   54 #include "state.h"
   55 #include "storage.h"
   56 #include "storage/btrfs.h"
   57 #include "storage/overlay.h"
   58 #include "storage_utils.h"
   59 #include "sync.h"
   60 #include "syscall_wrappers.h"
   61 #include "terminal.h"
   62 #include "utils.h"
   63 #include "version.h"
   64 
   65 #if HAVE_OPENSSL
   66 #include <openssl/evp.h>
   67 #endif
   68 
   69 /* major()/minor() */
   70 #ifdef MAJOR_IN_MKDEV
   71 #include <sys/mkdev.h>
   72 #endif
   73 
   74 #if IS_BIONIC
   75 #include <../include/lxcmntent.h>
   76 #else
   77 #include <mntent.h>
   78 #endif
   79 
   80 #ifndef HAVE_STRLCPY
   81 #include "include/strlcpy.h"
   82 #endif
   83 
   84 lxc_log_define(lxccontainer, lxc);
   85 
   86 static bool do_lxcapi_destroy(struct lxc_container *c);
   87 static const char *lxcapi_get_config_path(struct lxc_container *c);
   88 #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c)
   89 static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
   90 static bool container_destroy(struct lxc_container *c,
   91                   struct lxc_storage *storage);
   92 static bool get_snappath_dir(struct lxc_container *c, char *snappath);
   93 static bool lxcapi_snapshot_destroy_all(struct lxc_container *c);
   94 static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file);
   95 
   96 static bool config_file_exists(const char *lxcpath, const char *cname)
   97 {
   98     __do_free char *fname = NULL;
   99     int ret;
  100     size_t len;
  101 
  102     /* $lxcpath + '/' + $cname + '/config' + \0 */
  103     len = strlen(lxcpath) + 1 + strlen(cname) + 1 + strlen(LXC_CONFIG_FNAME) + 1;
  104     fname = must_realloc(NULL, len);
  105     ret = strnprintf(fname, len, "%s/%s/%s", lxcpath, cname, LXC_CONFIG_FNAME);
  106     if (ret < 0)
  107         return false;
  108 
  109     return file_exists(fname);
  110 }
  111 
  112 /*
  113  * A few functions to help detect when a container creation failed. If a
  114  * container creation was killed partway through, then trying to actually start
  115  * that container could harm the host. We detect this by creating a 'partial'
  116  * file under the container directory, and keeping an advisory lock. When
  117  * container creation completes, we remove that file.  When we load or try to
  118  * start a container, if we find that file, without a flock, we remove the
  119  * container.
  120  */
  121 enum {
  122     LXC_CREATE_FAILED = -1,
  123     LXC_CREATE_SUCCESS = 0,
  124     LXC_CREATE_ONGOING = 1,
  125     LXC_CREATE_INCOMPLETE = 2,
  126 };
  127 
  128 static int ongoing_create(struct lxc_container *c)
  129 {
  130     __do_close int fd = -EBADF;
  131     __do_free char *path = NULL;
  132     struct flock lk = {0};
  133     int ret;
  134     size_t len;
  135 
  136     len = strlen(c->config_path) + 1 + strlen(c->name) + 1 + strlen(LXC_PARTIAL_FNAME) + 1;
  137     path = must_realloc(NULL, len);
  138     ret = strnprintf(path, len, "%s/%s/%s", c->config_path, c->name, LXC_PARTIAL_FNAME);
  139     if (ret < 0)
  140         return LXC_CREATE_FAILED;
  141 
  142     fd = open(path, O_RDWR | O_CLOEXEC);
  143     if (fd < 0) {
  144         if (errno != ENOENT)
  145             return LXC_CREATE_FAILED;
  146 
  147         return LXC_CREATE_SUCCESS;
  148     }
  149 
  150     lk.l_type = F_WRLCK;
  151     lk.l_whence = SEEK_SET;
  152     /*
  153      * F_OFD_GETLK requires that l_pid be set to 0 otherwise the kernel
  154      * will EINVAL us.
  155      */
  156     lk.l_pid = 0;
  157 
  158     ret = fcntl(fd, F_OFD_GETLK, &lk);
  159     if (ret < 0 && errno == EINVAL) {
  160         ret = flock(fd, LOCK_EX | LOCK_NB);
  161         if (ret < 0 && errno == EWOULDBLOCK)
  162             ret = 0;
  163     }
  164 
  165     /* F_OFD_GETLK will not send us back a pid so don't check it. */
  166     if (ret == 0)
  167         /* Create is still ongoing. */
  168         return LXC_CREATE_ONGOING;
  169 
  170     /* Create completed but partial is still there. */
  171     return LXC_CREATE_INCOMPLETE;
  172 }
  173 
  174 static int create_partial(struct lxc_container *c)
  175 {
  176     __do_free char *path = NULL;
  177     int fd, ret;
  178     size_t len;
  179     struct flock lk = {0};
  180 
  181     /* $lxcpath + '/' + $name + '/partial' + \0 */
  182     len = strlen(c->config_path) + 1 + strlen(c->name) + 1 + strlen(LXC_PARTIAL_FNAME) + 1;
  183     path = must_realloc(NULL, len);
  184     ret = strnprintf(path, len, "%s/%s/%s", c->config_path, c->name, LXC_PARTIAL_FNAME);
  185     if (ret < 0)
  186         return -1;
  187 
  188     fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0000);
  189     if (fd < 0)
  190         return -1;
  191 
  192     lk.l_type = F_WRLCK;
  193     lk.l_whence = SEEK_SET;
  194 
  195     ret = fcntl(fd, F_OFD_SETLKW, &lk);
  196     if (ret < 0) {
  197         if (errno == EINVAL) {
  198             ret = flock(fd, LOCK_EX);
  199             if (ret == 0)
  200                 return fd;
  201         }
  202 
  203         SYSERROR("Failed to lock partial file %s", path);
  204         close(fd);
  205         return -1;
  206     }
  207 
  208     return fd;
  209 }
  210 
  211 static void remove_partial(struct lxc_container *c, int fd)
  212 {
  213     __do_free char *path = NULL;
  214     int ret;
  215     size_t len;
  216 
  217     close(fd);
  218 
  219     /* $lxcpath + '/' + $name + '/partial' + \0 */
  220     len = strlen(c->config_path) + 1 + strlen(c->name) + 1 + strlen(LXC_PARTIAL_FNAME) + 1;
  221     path = must_realloc(NULL, len);
  222     ret = strnprintf(path, len, "%s/%s/%s", c->config_path, c->name, LXC_PARTIAL_FNAME);
  223     if (ret < 0)
  224         return;
  225 
  226     ret = unlink(path);
  227     if (ret < 0)
  228         SYSERROR("Failed to remove partial file %s", path);
  229 }
  230 
  231 /* LOCKING
  232  * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
  233  * 2. container_disk_lock(c) protects the on-disk container data - in particular the
  234  *    container configuration file.
  235  *    The container_disk_lock also takes the container_mem_lock.
  236  * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
  237  * NOTHING mutexes two independent programs with their own struct
  238  * lxc_container for the same c->name, between API calls.  For instance,
  239  * c->config_read(); c->start();  Between those calls, data on disk
  240  * could change (which shouldn't bother the caller unless for instance
  241  * the rootfs get moved).  c->config_read(); update; c->config_write();
  242  * Two such updaters could race.  The callers should therefore check their
  243  * results.  Trying to prevent that would necessarily expose us to deadlocks
  244  * due to hung callers.  So I prefer to keep the locks only within our own
  245  * functions, not across functions.
  246  *
  247  * If you're going to clone while holding a lxccontainer, increment
  248  * c->numthreads (under privlock) before forking.  When deleting,
  249  * decrement numthreads under privlock, then if it hits 0 you can delete.
  250  * Do not ever use a lxccontainer whose numthreads you did not bump.
  251  */
  252 static void lxc_container_free(struct lxc_container *c)
  253 {
  254     if (!c)
  255         return;
  256 
  257     free(c->configfile);
  258     c->configfile = NULL;
  259 
  260     free(c->error_string);
  261     c->error_string = NULL;
  262 
  263     if (c->slock) {
  264         lxc_putlock(c->slock);
  265         c->slock = NULL;
  266     }
  267 
  268     if (c->privlock) {
  269         lxc_putlock(c->privlock);
  270         c->privlock = NULL;
  271     }
  272 
  273     free(c->name);
  274     c->name = NULL;
  275 
  276     if (c->lxc_conf) {
  277         lxc_conf_free(c->lxc_conf);
  278         c->lxc_conf = NULL;
  279     }
  280 
  281     free(c->config_path);
  282     c->config_path = NULL;
  283 
  284     free(c);
  285 }
  286 
  287 /* Consider the following case:
  288  *
  289  * |====================================================================|
  290  * | freer                         |    racing get()er                  |
  291  * |====================================================================|
  292  * | lxc_container_put()           |   lxc_container_get()              |
  293  * | \ lxclock(c->privlock)        |   c->numthreads < 1? (no)          |
  294  * | \ c->numthreads = 0           |   \ lxclock(c->privlock) -> waits  |
  295  * | \ lxcunlock()                 |   \                                |
  296  * | \ lxc_container_free()        |   \ lxclock() returns              |
  297  * |                               |   \ c->numthreads < 1 -> return 0  |
  298  * | \ \ (free stuff)              |                                    |
  299  * | \ \ sem_destroy(privlock)     |                                    |
  300  * |_______________________________|____________________________________|
  301  *
  302  * When the get()er checks numthreads the first time, one of the following
  303  * is true:
  304  * 1. freer has set numthreads = 0.  get() returns 0
  305  * 2. freer is between lxclock and setting numthreads to 0.  get()er will
  306  *    sem_wait on privlock, get lxclock after freer() drops it, then see
  307  *    numthreads is 0 and exit without touching lxclock again..
  308  * 3. freer has not yet locked privlock.  If get()er runs first, then put()er
  309  *    will see --numthreads = 1 and not call lxc_container_free().
  310 */
  311 
  312 int lxc_container_get(struct lxc_container *c)
  313 {
  314     if (!c)
  315         return 0;
  316 
  317     /* If someone else has already started freeing the container, don't try
  318      * to take the lock, which may be invalid.
  319      */
  320     if (c->numthreads < 1)
  321         return 0;
  322 
  323     if (container_mem_lock(c))
  324         return 0;
  325 
  326     /* Bail without trying to unlock, bc the privlock is now probably in
  327      * freed memory.
  328      */
  329     if (c->numthreads < 1)
  330         return 0;
  331 
  332     c->numthreads++;
  333     container_mem_unlock(c);
  334 
  335     return 1;
  336 }
  337 
  338 int lxc_container_put(struct lxc_container *c)
  339 {
  340     if (!c)
  341         return -1;
  342 
  343     if (container_mem_lock(c))
  344         return -1;
  345 
  346     c->numthreads--;
  347 
  348     if (c->numthreads < 1) {
  349         container_mem_unlock(c);
  350         lxc_container_free(c);
  351         return 1;
  352     }
  353 
  354     container_mem_unlock(c);
  355     return 0;
  356 }
  357 
  358 static bool do_lxcapi_is_defined(struct lxc_container *c)
  359 {
  360     int statret;
  361     struct stat statbuf;
  362     bool ret = false;
  363 
  364     if (!c)
  365         return false;
  366 
  367     if (container_mem_lock(c))
  368         return false;
  369 
  370     if (!c->configfile)
  371         goto on_error;
  372 
  373     statret = stat(c->configfile, &statbuf);
  374     if (statret != 0)
  375         goto on_error;
  376 
  377     ret = true;
  378 
  379 on_error:
  380     container_mem_unlock(c);
  381     return ret;
  382 }
  383 
  384 #define WRAP_API(rettype, fnname)                   \
  385 static rettype fnname(struct lxc_container *c)              \
  386 {                                   \
  387     rettype ret;                            \
  388     bool reset_config = false;                  \
  389                                     \
  390     if (!current_config && c && c->lxc_conf) {          \
  391         current_config = c->lxc_conf;               \
  392         reset_config = true;                    \
  393     }                               \
  394                                     \
  395     ret = do_##fnname(c);                       \
  396     if (reset_config)                       \
  397         current_config = NULL;                  \
  398                                     \
  399     return ret;                         \
  400 }
  401 
  402 #define WRAP_API_1(rettype, fnname, t1)                 \
  403 static rettype fnname(struct lxc_container *c, t1 a1)           \
  404 {                                   \
  405     rettype ret;                            \
  406     bool reset_config = false;                  \
  407                                     \
  408     if (!current_config && c && c->lxc_conf) {          \
  409         current_config = c->lxc_conf;               \
  410         reset_config = true;                    \
  411     }                               \
  412                                     \
  413     ret = do_##fnname(c, a1);                   \
  414     if (reset_config)                       \
  415         current_config = NULL;                  \
  416                                     \
  417     return ret;                         \
  418 }
  419 
  420 #define WRAP_API_2(rettype, fnname, t1, t2)             \
  421 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2)        \
  422 {                                   \
  423     rettype ret;                            \
  424     bool reset_config = false;                  \
  425                                     \
  426     if (!current_config && c && c->lxc_conf) {          \
  427         current_config = c->lxc_conf;               \
  428         reset_config = true;                    \
  429     }                               \
  430                                     \
  431     ret = do_##fnname(c, a1, a2);                   \
  432     if (reset_config)                       \
  433         current_config = NULL;                  \
  434                                     \
  435     return ret;                         \
  436 }
  437 
  438 #define WRAP_API_3(rettype, fnname, t1, t2, t3)             \
  439 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \
  440 {                                   \
  441     rettype ret;                            \
  442     bool reset_config = false;                  \
  443                                     \
  444     if (!current_config && c && c->lxc_conf) {          \
  445         current_config = c->lxc_conf;               \
  446         reset_config = true;                    \
  447     }                               \
  448                                     \
  449     ret = do_##fnname(c, a1, a2, a3);               \
  450     if (reset_config)                       \
  451         current_config = NULL;                  \
  452                                     \
  453     return ret;                         \
  454 }
  455 
  456 #define WRAP_API_6(rettype, fnname, t1, t2, t3, t4, t5, t6)             \
  457 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3, \
  458                         t4 a4, t5 a5, t6 a6)    \
  459 {                                   \
  460     rettype ret;                            \
  461     bool reset_config = false;                  \
  462                                     \
  463     if (!current_config && c && c->lxc_conf) {          \
  464         current_config = c->lxc_conf;               \
  465         reset_config = true;                    \
  466     }                               \
  467                                     \
  468     ret = do_##fnname(c, a1, a2, a3, a4, a5, a6);               \
  469     if (reset_config)                       \
  470         current_config = NULL;                  \
  471                                     \
  472     return ret;                         \
  473 }
  474 
  475 WRAP_API(bool, lxcapi_is_defined)
  476 
  477 static const char *do_lxcapi_state(struct lxc_container *c)
  478 {
  479     lxc_state_t s;
  480 
  481     if (!c)
  482         return NULL;
  483 
  484     s = lxc_getstate(c->name, c->config_path);
  485     return lxc_state2str(s);
  486 }
  487 
  488 WRAP_API(const char *, lxcapi_state)
  489 
  490 static bool is_stopped(struct lxc_container *c)
  491 {
  492     lxc_state_t s;
  493 
  494     s = lxc_getstate(c->name, c->config_path);
  495     return (s == STOPPED);
  496 }
  497 
  498 static bool do_lxcapi_is_running(struct lxc_container *c)
  499 {
  500     if (!c)
  501         return false;
  502 
  503     return !is_stopped(c);
  504 }
  505 
  506 WRAP_API(bool, lxcapi_is_running)
  507 
  508 static bool do_lxcapi_freeze(struct lxc_container *c)
  509 {
  510     int ret = 0;
  511     lxc_state_t s;
  512 
  513     if (!c || !c->lxc_conf)
  514         return false;
  515 
  516     s = lxc_getstate(c->name, c->config_path);
  517     if (s != FROZEN) {
  518         ret = cgroup_freeze(c->name, c->config_path, -1);
  519         if (ret == -ENOCGROUP2)
  520             ret = lxc_freeze(c->lxc_conf, c->name, c->config_path);
  521     }
  522 
  523     return ret == 0;
  524 }
  525 
  526 WRAP_API(bool, lxcapi_freeze)
  527 
  528 static bool do_lxcapi_unfreeze(struct lxc_container *c)
  529 {
  530     int ret = 0;
  531     lxc_state_t s;
  532 
  533     if (!c || !c->lxc_conf)
  534         return false;
  535 
  536     s = lxc_getstate(c->name, c->config_path);
  537     if (s == FROZEN) {
  538         ret = cgroup_unfreeze(c->name, c->config_path, -1);
  539         if (ret == -ENOCGROUP2)
  540             ret = lxc_unfreeze(c->lxc_conf, c->name, c->config_path);
  541     }
  542 
  543 
  544     return ret == 0;
  545 }
  546 
  547 WRAP_API(bool, lxcapi_unfreeze)
  548 
  549 static int do_lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *ptxfd)
  550 {
  551     if (!c)
  552         return -1;
  553 
  554     return lxc_terminal_getfd(c, ttynum, ptxfd);
  555 }
  556 
  557 WRAP_API_2(int, lxcapi_console_getfd, int *, int *)
  558 
  559 static int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd,
  560               int stdoutfd, int stderrfd, int escape)
  561 {
  562     int ret;
  563 
  564     if (!c)
  565         return -1;
  566 
  567     current_config = c->lxc_conf;
  568     ret = lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape);
  569     current_config = NULL;
  570 
  571     return ret;
  572 }
  573 
  574 static int do_lxcapi_console_log(struct lxc_container *c, struct lxc_console_log *log)
  575 {
  576     int ret;
  577 
  578     if (!c)
  579         return -EINVAL;
  580 
  581     ret = lxc_cmd_console_log(c->name, do_lxcapi_get_config_path(c), log);
  582     if (ret < 0) {
  583         if (ret == -ENODATA)
  584             NOTICE("The console log is empty");
  585         else if (ret == -EFAULT)
  586             NOTICE("The container does not keep a console log");
  587         else if (ret == -ENOENT)
  588             NOTICE("The container does not keep a console log file");
  589         else if (ret == -EIO)
  590             NOTICE("Failed to write console log to log file");
  591         else
  592             ERROR("Failed to retrieve console log");
  593     }
  594 
  595     return ret;
  596 }
  597 
  598 WRAP_API_1(int, lxcapi_console_log, struct lxc_console_log *)
  599 
  600 static pid_t do_lxcapi_init_pid(struct lxc_container *c)
  601 {
  602     if (!c)
  603         return -1;
  604 
  605     return lxc_cmd_get_init_pid(c->name, c->config_path);
  606 }
  607 
  608 WRAP_API(pid_t, lxcapi_init_pid)
  609 
  610 static int do_lxcapi_init_pidfd(struct lxc_container *c)
  611 {
  612     if (!c)
  613         return ret_errno(EBADF);
  614 
  615     return lxc_cmd_get_init_pidfd(c->name, c->config_path);
  616 }
  617 
  618 WRAP_API(int, lxcapi_init_pidfd)
  619 
  620 static int do_lxcapi_devpts_fd(struct lxc_container *c)
  621 {
  622     if (!c)
  623         return ret_errno(EBADF);
  624 
  625     return lxc_cmd_get_devpts_fd(c->name, c->config_path);
  626 }
  627 
  628 WRAP_API(int, lxcapi_devpts_fd)
  629 
  630 static bool load_config_locked(struct lxc_container *c, const char *fname)
  631 {
  632     if (!c->lxc_conf)
  633         c->lxc_conf = lxc_conf_init();
  634 
  635     if (!c->lxc_conf)
  636         return false;
  637 
  638     if (lxc_config_read(fname, c->lxc_conf, false) != 0)
  639         return false;
  640 
  641     c->lxc_conf->name = c->name;
  642     return true;
  643 }
  644 
  645 static bool do_lxcapi_load_config(struct lxc_container *c, const char *alt_file)
  646 {
  647     int lret;
  648     const char *fname;
  649     bool need_disklock = false, ret = false;
  650 
  651     if (!c)
  652         return false;
  653 
  654     fname = c->configfile;
  655 
  656     if (alt_file)
  657         fname = alt_file;
  658 
  659     if (!fname)
  660         return false;
  661 
  662     /* If we're reading something other than the container's config, we only
  663      * need to lock the in-memory container. If loading the container's
  664      * config file, take the disk lock.
  665      */
  666     if (strequal(fname, c->configfile))
  667         need_disklock = true;
  668 
  669     if (need_disklock)
  670         lret = container_disk_lock(c);
  671     else
  672         lret = container_mem_lock(c);
  673     if (lret)
  674         return false;
  675 
  676     ret = load_config_locked(c, fname);
  677 
  678     if (need_disklock)
  679         container_disk_unlock(c);
  680     else
  681         container_mem_unlock(c);
  682 
  683     return ret;
  684 }
  685 
  686 WRAP_API_1(bool, lxcapi_load_config, const char *)
  687 
  688 static bool do_lxcapi_want_daemonize(struct lxc_container *c, bool state)
  689 {
  690     if (!c || !c->lxc_conf)
  691         return false;
  692 
  693     if (container_mem_lock(c))
  694         return false;
  695 
  696     c->daemonize = state;
  697 
  698     container_mem_unlock(c);
  699 
  700     return true;
  701 }
  702 
  703 WRAP_API_1(bool, lxcapi_want_daemonize, bool)
  704 
  705 static bool do_lxcapi_want_close_all_fds(struct lxc_container *c, bool state)
  706 {
  707     if (!c || !c->lxc_conf)
  708         return false;
  709 
  710     if (container_mem_lock(c))
  711         return false;
  712 
  713     c->lxc_conf->close_all_fds = state;
  714 
  715     container_mem_unlock(c);
  716 
  717     return true;
  718 }
  719 
  720 WRAP_API_1(bool, lxcapi_want_close_all_fds, bool)
  721 
  722 static bool do_lxcapi_wait(struct lxc_container *c, const char *state,
  723                int timeout)
  724 {
  725     int ret;
  726 
  727     if (!c)
  728         return false;
  729 
  730     ret = lxc_wait(c->name, state, timeout, c->config_path);
  731     return ret == 0;
  732 }
  733 
  734 WRAP_API_2(bool, lxcapi_wait, const char *, int)
  735 
  736 static bool am_single_threaded(void)
  737 {
  738     __do_closedir DIR *dir = NULL;
  739     struct dirent *direntp;
  740     int count = 0;
  741 
  742     dir = opendir("/proc/self/task");
  743     if (!dir)
  744         return false;
  745 
  746     while ((direntp = readdir(dir))) {
  747         if (strequal(direntp->d_name, "."))
  748             continue;
  749 
  750         if (strequal(direntp->d_name, ".."))
  751             continue;
  752 
  753         count++;
  754         if (count > 1)
  755             break;
  756     }
  757 
  758     return count == 1;
  759 }
  760 
  761 static void push_arg(char ***argp, char *arg, int *nargs)
  762 {
  763     char *copy;
  764     char **argv;
  765 
  766     copy = must_copy_string(arg);
  767 
  768     do {
  769         argv = realloc(*argp, (*nargs + 2) * sizeof(char *));
  770     } while (!argv);
  771 
  772     *argp = argv;
  773     argv[*nargs] = copy;
  774     (*nargs)++;
  775     argv[*nargs] = NULL;
  776 }
  777 
  778 static char **split_init_cmd(const char *incmd)
  779 {
  780     __do_free char *copy = NULL;
  781     char *p;
  782     char **argv;
  783     int nargs = 0;
  784 
  785     if (!incmd)
  786         return NULL;
  787 
  788     copy = must_copy_string(incmd);
  789 
  790     do {
  791         argv = malloc(sizeof(char *));
  792     } while (!argv);
  793 
  794     argv[0] = NULL;
  795     lxc_iterate_parts (p, copy, " ")
  796         push_arg(&argv, p, &nargs);
  797 
  798     if (nargs == 0) {
  799         free(argv);
  800         return NULL;
  801     }
  802 
  803     return argv;
  804 }
  805 
  806 static void free_init_cmd(char **argv)
  807 {
  808     int i = 0;
  809 
  810     if (!argv)
  811         return;
  812 
  813     while (argv[i])
  814         free(argv[i++]);
  815 
  816     free(argv);
  817 }
  818 
  819 static int lxc_rcv_status(int state_socket)
  820 {
  821     int ret;
  822     int state = -1;
  823 
  824 again:
  825     /* Receive container state. */
  826     ret = lxc_abstract_unix_rcv_credential(state_socket, &state, sizeof(int));
  827     if (ret <= 0) {
  828         if (errno != EINTR)
  829             return -1;
  830 
  831         TRACE("Caught EINTR; retrying");
  832         goto again;
  833     }
  834 
  835     return state;
  836 }
  837 
  838 static bool wait_on_daemonized_start(struct lxc_handler *handler, int pid)
  839 {
  840     int ret, state;
  841 
  842     /* The first child is going to fork() again and then exits. So we reap
  843      * the first child here.
  844      */
  845     ret = wait_for_pid(pid);
  846     if (ret < 0)
  847         DEBUG("Failed waiting on first child %d", pid);
  848     else
  849         DEBUG("First child %d exited", pid);
  850 
  851     /* Close write end of the socket pair. */
  852     close_prot_errno_disarm(handler->state_socket_pair[1]);
  853 
  854     state = lxc_rcv_status(handler->state_socket_pair[0]);
  855 
  856     /* Close read end of the socket pair. */
  857     close_prot_errno_disarm(handler->state_socket_pair[0]);
  858 
  859     if (state < 0) {
  860         SYSERROR("Failed to receive the container state");
  861         return false;
  862     }
  863 
  864     /* If we receive anything else then running we know that the container
  865      * failed to start.
  866      */
  867     if (state != RUNNING) {
  868         ERROR("Received container state \"%s\" instead of \"RUNNING\"",
  869               lxc_state2str(state));
  870         return false;
  871     }
  872 
  873     TRACE("Container is in \"RUNNING\" state");
  874     return true;
  875 }
  876 
  877 static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
  878 {
  879     int ret;
  880     struct lxc_handler *handler;
  881     struct lxc_conf *conf;
  882     char *default_args[] = {
  883         "/sbin/init",
  884         NULL,
  885     };
  886     char **init_cmd = NULL;
  887 
  888     /* container does exist */
  889     if (!c)
  890         return false;
  891 
  892     /* If anything fails before we set error_num, we want an error in there.
  893      */
  894     c->error_num = 1;
  895 
  896     /* Container has not been setup. */
  897     if (!c->lxc_conf)
  898         return false;
  899 
  900     ret = ongoing_create(c);
  901     switch (ret) {
  902     case LXC_CREATE_FAILED:
  903         ERROR("Failed checking for incomplete container creation");
  904         return false;
  905     case LXC_CREATE_ONGOING:
  906         ERROR("Ongoing container creation detected");
  907         return false;
  908     case LXC_CREATE_INCOMPLETE:
  909         ERROR("Failed to create container");
  910         do_lxcapi_destroy(c);
  911         return false;
  912     }
  913 
  914     if (container_mem_lock(c))
  915         return false;
  916 
  917     conf = c->lxc_conf;
  918 
  919     /* initialize handler */
  920     handler = lxc_init_handler(NULL, c->name, conf, c->config_path, c->daemonize);
  921 
  922     container_mem_unlock(c);
  923     if (!handler)
  924         return false;
  925 
  926     if (!argv) {
  927         if (useinit && conf->execute_cmd)
  928             argv = init_cmd = split_init_cmd(conf->execute_cmd);
  929         else
  930             argv = init_cmd = split_init_cmd(conf->init_cmd);
  931     }
  932 
  933     /* ... otherwise use default_args. */
  934     if (!argv) {
  935         if (useinit) {
  936             ERROR("No valid init detected");
  937             lxc_put_handler(handler);
  938             return false;
  939         }
  940         argv = default_args;
  941     }
  942 
  943     /* I'm not sure what locks we want here.Any? Is liblxc's locking enough
  944      * here to protect the on disk container?  We don't want to exclude
  945      * things like lxc_info while the container is running.
  946      */
  947     if (c->daemonize) {
  948         bool started;
  949         char title[2048];
  950         pid_t pid_first, pid_second;
  951 
  952         pid_first = fork();
  953         if (pid_first < 0) {
  954             free_init_cmd(init_cmd);
  955             lxc_put_handler(handler);
  956             return false;
  957         }
  958 
  959         /* first parent */
  960         if (pid_first != 0) {
  961             /* Set to NULL because we don't want father unlink
  962              * the PID file, child will do the free and unlink.
  963              */
  964             c->pidfile = NULL;
  965 
  966             /* Wait for container to tell us whether it started
  967              * successfully.
  968              */
  969             started = wait_on_daemonized_start(handler, pid_first);
  970 
  971             free_init_cmd(init_cmd);
  972             lxc_put_handler(handler);
  973             return started;
  974         }
  975 
  976         /* first child */
  977 
  978         /* We don't really care if this doesn't print all the
  979          * characters. All that it means is that the proctitle will be
  980          * ugly. Similarly, we also don't care if setproctitle() fails.
  981          */
  982         ret = strnprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name);
  983         if (ret > 0) {
  984             ret = setproctitle(title);
  985             if (ret < 0)
  986                 INFO("Failed to set process title to %s", title);
  987             else
  988                 INFO("Set process title to %s", title);
  989         }
  990 
  991         /* We fork() a second time to be reparented to init. Like
  992          * POSIX's daemon() function we change to "/" and redirect
  993          * std{in,out,err} to /dev/null.
  994          */
  995         pid_second = fork();
  996         if (pid_second < 0) {
  997             SYSERROR("Failed to fork first child process");
  998             _exit(EXIT_FAILURE);
  999         }
 1000 
 1001         /* second parent */
 1002         if (pid_second != 0) {
 1003             free_init_cmd(init_cmd);
 1004             lxc_put_handler(handler);
 1005             _exit(EXIT_SUCCESS);
 1006         }
 1007 
 1008         /* second child */
 1009 
 1010         /* change to / directory */
 1011         ret = chdir("/");
 1012         if (ret < 0) {
 1013             SYSERROR("Failed to change to \"/\" directory");
 1014             _exit(EXIT_FAILURE);
 1015         }
 1016 
 1017         ret = inherit_fds(handler, true);
 1018         if (ret < 0)
 1019             _exit(EXIT_FAILURE);
 1020 
 1021         /* redirect std{in,out,err} to /dev/null */
 1022         ret = null_stdfds();
 1023         if (ret < 0) {
 1024             ERROR("Failed to redirect std{in,out,err} to /dev/null");
 1025             _exit(EXIT_FAILURE);
 1026         }
 1027 
 1028         /* become session leader */
 1029         ret = setsid();
 1030         if (ret < 0)
 1031             TRACE("Process %d is already process group leader", lxc_raw_getpid());
 1032     } else if (!am_single_threaded()) {
 1033         ERROR("Cannot start non-daemonized container when threaded");
 1034         free_init_cmd(init_cmd);
 1035         lxc_put_handler(handler);
 1036         return false;
 1037     }
 1038 
 1039     /* We need to write PID file after daemonize, so we always write the
 1040      * right PID.
 1041      */
 1042     if (c->pidfile) {
 1043         int w;
 1044         char pidstr[INTTYPE_TO_STRLEN(pid_t)];
 1045 
 1046         w = strnprintf(pidstr, sizeof(pidstr), "%d", lxc_raw_getpid());
 1047         if (w < 0) {
 1048             free_init_cmd(init_cmd);
 1049             lxc_put_handler(handler);
 1050 
 1051             SYSERROR("Failed to write monitor pid to \"%s\"", c->pidfile);
 1052 
 1053             if (c->daemonize)
 1054                 _exit(EXIT_FAILURE);
 1055 
 1056             return false;
 1057         }
 1058 
 1059         ret = lxc_write_to_file(c->pidfile, pidstr, w, false, 0600);
 1060         if (ret < 0) {
 1061             free_init_cmd(init_cmd);
 1062             lxc_put_handler(handler);
 1063 
 1064             SYSERROR("Failed to write monitor pid to \"%s\"", c->pidfile);
 1065 
 1066             if (c->daemonize)
 1067                 _exit(EXIT_FAILURE);
 1068 
 1069             return false;
 1070         }
 1071     }
 1072 
 1073     conf->reboot = REBOOT_NONE;
 1074 
 1075     /* Unshare the mount namespace if requested */
 1076     if (conf->monitor_unshare) {
 1077         ret = unshare(CLONE_NEWNS);
 1078         if (ret < 0) {
 1079             SYSERROR("Failed to unshare mount namespace");
 1080             lxc_put_handler(handler);
 1081             ret = 1;
 1082             goto on_error;
 1083         }
 1084 
 1085         ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
 1086         if (ret < 0) {
 1087             SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
 1088             lxc_put_handler(handler);
 1089             ret = 1;
 1090             goto on_error;
 1091         }
 1092     }
 1093 
 1094 reboot:
 1095     if (conf->reboot == REBOOT_INIT) {
 1096         /* initialize handler */
 1097         handler = lxc_init_handler(handler, c->name, conf, c->config_path, c->daemonize);
 1098         if (!handler) {
 1099             ret = 1;
 1100             goto on_error;
 1101         }
 1102     }
 1103 
 1104     ret = inherit_fds(handler, c->daemonize);
 1105     if (ret < 0) {
 1106         lxc_put_handler(handler);
 1107         ret = 1;
 1108         goto on_error;
 1109     }
 1110 
 1111     if (useinit)
 1112         ret = lxc_execute(c->name, argv, 1, handler, c->config_path,
 1113                   c->daemonize, &c->error_num);
 1114     else
 1115         ret = lxc_start(argv, handler, c->config_path, c->daemonize,
 1116                 &c->error_num);
 1117 
 1118     if (conf->reboot == REBOOT_REQ) {
 1119         INFO("Container requested reboot");
 1120         conf->reboot = REBOOT_INIT;
 1121         goto reboot;
 1122     }
 1123 
 1124 on_error:
 1125     if (c->pidfile) {
 1126         unlink(c->pidfile);
 1127         free(c->pidfile);
 1128         c->pidfile = NULL;
 1129     }
 1130     free_init_cmd(init_cmd);
 1131 
 1132     if (c->daemonize && ret != 0)
 1133         _exit(EXIT_FAILURE);
 1134     else if (c->daemonize)
 1135         _exit(EXIT_SUCCESS);
 1136 
 1137     if (ret != 0)
 1138         return false;
 1139 
 1140     return true;
 1141 }
 1142 
 1143 static bool lxcapi_start(struct lxc_container *c, int useinit,
 1144              char *const argv[])
 1145 {
 1146     bool ret;
 1147 
 1148     current_config = c ? c->lxc_conf : NULL;
 1149     ret = do_lxcapi_start(c, useinit, argv);
 1150     current_config = NULL;
 1151 
 1152     return ret;
 1153 }
 1154 
 1155 /* Note, there MUST be an ending NULL. */
 1156 static bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
 1157 {
 1158     va_list ap;
 1159     char **inargs = NULL;
 1160     bool bret = false;
 1161 
 1162     /* container exists */
 1163     if (!c)
 1164         return false;
 1165 
 1166     current_config = c->lxc_conf;
 1167 
 1168     va_start(ap, useinit);
 1169     inargs = lxc_va_arg_list_to_argv(ap, 0, 1);
 1170     va_end(ap);
 1171     if (!inargs)
 1172         goto on_error;
 1173 
 1174     /* pass NULL if no arguments were supplied */
 1175     bret = do_lxcapi_start(c, useinit, *inargs ? inargs : NULL);
 1176 
 1177 on_error:
 1178     if (inargs) {
 1179         char **arg;
 1180 
 1181         for (arg = inargs; *arg; arg++)
 1182             free(*arg);
 1183         free(inargs);
 1184     }
 1185 
 1186     current_config = NULL;
 1187 
 1188     return bret;
 1189 }
 1190 
 1191 static bool do_lxcapi_stop(struct lxc_container *c)
 1192 {
 1193     int ret;
 1194 
 1195     if (!c)
 1196         return false;
 1197 
 1198     ret = lxc_cmd_stop(c->name, c->config_path);
 1199 
 1200     return ret == 0;
 1201 }
 1202 
 1203 WRAP_API(bool, lxcapi_stop)
 1204 
 1205 static int do_create_container_dir(const char *path, struct lxc_conf *conf)
 1206 {
 1207     int lasterr;
 1208     int ret = -1;
 1209 
 1210     mode_t mask = umask(0002);
 1211     ret = mkdir(path, 0770);
 1212     lasterr = errno;
 1213     umask(mask);
 1214     errno = lasterr;
 1215     if (ret) {
 1216         if (errno != EEXIST)
 1217             return -1;
 1218 
 1219         ret = 0;
 1220     }
 1221 
 1222     if (!lxc_list_empty(&conf->id_map)) {
 1223         ret = chown_mapped_root(path, conf);
 1224         if (ret < 0)
 1225             ret = -1;
 1226     }
 1227 
 1228     return ret;
 1229 }
 1230 
 1231 /* Create the standard expected container dir. */
 1232 static bool create_container_dir(struct lxc_container *c)
 1233 {
 1234     __do_free char *s = NULL;
 1235     int ret;
 1236     size_t len;
 1237 
 1238     len = strlen(c->config_path) + strlen(c->name) + 2;
 1239     s = malloc(len);
 1240     if (!s)
 1241         return false;
 1242 
 1243     ret = strnprintf(s, len, "%s/%s", c->config_path, c->name);
 1244     if (ret < 0)
 1245         return false;
 1246 
 1247     return do_create_container_dir(s, c->lxc_conf) == 0;
 1248 }
 1249 
 1250 /* do_storage_create: thin wrapper around storage_create(). Like
 1251  * storage_create(), it returns a mounted bdev on success, NULL on error.
 1252  */
 1253 static struct lxc_storage *do_storage_create(struct lxc_container *c,
 1254                          const char *type,
 1255                          struct bdev_specs *specs)
 1256 {
 1257     __do_free char *dest = NULL;
 1258     int ret;
 1259     size_t len;
 1260     struct lxc_storage *bdev;
 1261 
 1262     /* rootfs.path or lxcpath/lxcname/rootfs */
 1263     if (c->lxc_conf->rootfs.path &&
 1264         (access(c->lxc_conf->rootfs.path, F_OK) == 0)) {
 1265         const char *rpath = c->lxc_conf->rootfs.path;
 1266         len = strlen(rpath) + 1;
 1267         dest = must_realloc(NULL, len);
 1268         ret = strnprintf(dest, len, "%s", rpath);
 1269     } else {
 1270         const char *lxcpath = do_lxcapi_get_config_path(c);
 1271         len = strlen(c->name) + 1 + strlen(lxcpath) + 1 + strlen(LXC_ROOTFS_DNAME) + 1;
 1272         dest = must_realloc(NULL, len);
 1273         ret = strnprintf(dest, len, "%s/%s/%s", lxcpath, c->name, LXC_ROOTFS_DNAME);
 1274     }
 1275     if (ret < 0)
 1276         return NULL;
 1277 
 1278     bdev = storage_create(dest, type, c->name, specs, c->lxc_conf);
 1279     if (!bdev) {
 1280         ERROR("Failed to create \"%s\" storage", type);
 1281         return NULL;
 1282     }
 1283 
 1284     if (!c->set_config_item(c, "lxc.rootfs.path", bdev->src)) {
 1285         ERROR("Failed to set \"lxc.rootfs.path = %s\"", bdev->src);
 1286         storage_put(bdev);
 1287         return NULL;
 1288     }
 1289 
 1290     /* If we are not root, chown the rootfs dir to root in the target user
 1291      * namespace.
 1292      */
 1293     if (am_guest_unpriv() || !lxc_list_empty(&c->lxc_conf->id_map)) {
 1294         ret = chown_mapped_root(bdev->dest, c->lxc_conf);
 1295         if (ret < 0) {
 1296             ERROR("Error chowning \"%s\" to container root", bdev->dest);
 1297             suggest_default_idmap();
 1298             storage_put(bdev);
 1299             return NULL;
 1300         }
 1301     }
 1302 
 1303     return bdev;
 1304 }
 1305 
 1306 /* Strip path and return name of file for argv[0] passed to execvp */
 1307 static char *lxctemplatefilename(char *tpath)
 1308 {
 1309     char *p;
 1310 
 1311     p = tpath + strlen(tpath) - 1;
 1312     while ( (p-1) >= tpath && *(p-1) != '/')
 1313         p--;
 1314 
 1315     return p;
 1316 }
 1317 
 1318 static bool create_run_template(struct lxc_container *c, char *tpath,
 1319                 bool need_null_stdfds, char *const argv[])
 1320 {
 1321     int ret;
 1322     pid_t pid;
 1323 
 1324     if (!tpath)
 1325         return true;
 1326 
 1327     pid = fork();
 1328     if (pid < 0) {
 1329         SYSERROR("Failed to fork task for container creation template");
 1330         return false;
 1331     }
 1332 
 1333     if (pid == 0) { /* child */
 1334         int i, len;
 1335         char *namearg, *patharg, *rootfsarg;
 1336         char **newargv;
 1337         int nargs = 0;
 1338         struct lxc_storage *bdev = NULL;
 1339         struct lxc_conf *conf = c->lxc_conf;
 1340         uid_t euid;
 1341 
 1342         if (need_null_stdfds) {
 1343             ret = null_stdfds();
 1344             if (ret < 0)
 1345                 _exit(EXIT_FAILURE);
 1346         }
 1347 
 1348         ret = lxc_storage_prepare(conf);
 1349         if (ret) {
 1350             ERROR("Failed to initialize storage");
 1351             _exit(EXIT_FAILURE);
 1352         }
 1353         bdev = conf->rootfs.storage;
 1354 
 1355         euid = geteuid();
 1356         if (euid == 0) {
 1357             ret = unshare(CLONE_NEWNS);
 1358             if (ret < 0) {
 1359                 ERROR("Failed to unshare CLONE_NEWNS");
 1360                 _exit(EXIT_FAILURE);
 1361             }
 1362 
 1363             if (detect_shared_rootfs() && mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL))
 1364                 SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
 1365         }
 1366 
 1367         if (!strequal(bdev->type, "dir") && !strequal(bdev->type, "btrfs")) {
 1368             if (euid != 0) {
 1369                 ERROR("Unprivileged users can only create "
 1370                       "btrfs and directory-backed containers");
 1371                 _exit(EXIT_FAILURE);
 1372             }
 1373 
 1374             if (strequal(bdev->type, "overlay") ||
 1375                 strequal(bdev->type, "overlayfs")) {
 1376                 /* If we create an overlay container we need to
 1377                  * rsync the contents into
 1378                  * <container-path>/<container-name>/rootfs.
 1379                  * However, the overlay mount function will
 1380                  * mount
 1381                  * <container-path>/<container-name>/delta0
 1382                  * over
 1383                  * <container-path>/<container-name>/rootfs
 1384                  * which means we would rsync the rootfs into
 1385                  * the delta directory. That doesn't make sense
 1386                  * since the delta directory only exists to
 1387                  * record the differences to
 1388                  * <container-path>/<container-name>/rootfs. So
 1389                  * let's simply bind-mount here and then rsync
 1390                  * directly into
 1391                  * <container-path>/<container-name>/rootfs.
 1392                  */
 1393                 char *src;
 1394 
 1395                 src = ovl_get_rootfs(bdev->src, &(size_t){0});
 1396                 if (!src) {
 1397                     ERROR("Failed to get rootfs");
 1398                     _exit(EXIT_FAILURE);
 1399                 }
 1400 
 1401                 ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC, NULL);
 1402                 if (ret < 0) {
 1403                     ERROR("Failed to mount rootfs");
 1404                     _exit(EXIT_FAILURE);
 1405                 }
 1406             } else {
 1407                 ret = bdev->ops->mount(bdev);
 1408                 if (ret < 0) {
 1409                     ERROR("Failed to mount rootfs");
 1410                     _exit(EXIT_FAILURE);
 1411                 }
 1412             }
 1413         } else { /* TODO come up with a better way here! */
 1414             const char *src;
 1415             free(bdev->dest);
 1416             src = lxc_storage_get_path(bdev->src, bdev->type);
 1417             bdev->dest = strdup(src);
 1418         }
 1419 
 1420         /* Create our new array, pre-pend the template name and base
 1421          * args.
 1422          */
 1423         if (argv)
 1424             for (nargs = 0; argv[nargs]; nargs++)
 1425                 ;
 1426 
 1427         /* template, path, rootfs and name args */
 1428         nargs += 4;
 1429 
 1430         newargv = malloc(nargs * sizeof(*newargv));
 1431         if (!newargv)
 1432             _exit(EXIT_FAILURE);
 1433         newargv[0] = lxctemplatefilename(tpath);
 1434 
 1435         /* --path */
 1436         len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
 1437         patharg = malloc(len);
 1438         if (!patharg)
 1439             _exit(EXIT_FAILURE);
 1440 
 1441         ret = strnprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
 1442         if (ret < 0)
 1443             _exit(EXIT_FAILURE);
 1444         newargv[1] = patharg;
 1445 
 1446         /* --name */
 1447         len = strlen("--name=") + strlen(c->name) + 1;
 1448         namearg = malloc(len);
 1449         if (!namearg)
 1450             _exit(EXIT_FAILURE);
 1451 
 1452         ret = strnprintf(namearg, len, "--name=%s", c->name);
 1453         if (ret < 0)
 1454             _exit(EXIT_FAILURE);
 1455         newargv[2] = namearg;
 1456 
 1457         /* --rootfs */
 1458         len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
 1459         rootfsarg = malloc(len);
 1460         if (!rootfsarg)
 1461             _exit(EXIT_FAILURE);
 1462 
 1463         ret = strnprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
 1464         if (ret < 0)
 1465             _exit(EXIT_FAILURE);
 1466         newargv[3] = rootfsarg;
 1467 
 1468         /* add passed-in args */
 1469         if (argv)
 1470             for (i = 4; i < nargs; i++)
 1471                 newargv[i] = argv[i - 4];
 1472 
 1473         /* add trailing NULL */
 1474         nargs++;
 1475         newargv = realloc(newargv, nargs * sizeof(*newargv));
 1476         if (!newargv)
 1477             _exit(EXIT_FAILURE);
 1478         newargv[nargs - 1] = NULL;
 1479 
 1480         /* If we're running the template in a mapped userns, then we
 1481          * prepend the template command with: lxc-usernsexec <-m map1>
 1482          * ... <-m mapn> -- and we append "--mapped-uid x", where x is
 1483          * the mapped uid for our geteuid()
 1484          */
 1485         if (!lxc_list_empty(&conf->id_map)) {
 1486             int extraargs, hostuid_mapped, hostgid_mapped;
 1487             char **n2;
 1488             char txtuid[20], txtgid[20];
 1489             struct lxc_list *it;
 1490             struct id_map *map;
 1491             int n2args = 1;
 1492 
 1493             n2 = malloc(n2args * sizeof(*n2));
 1494             if (!n2)
 1495                 _exit(EXIT_FAILURE);
 1496 
 1497             newargv[0] = tpath;
 1498             tpath = "lxc-usernsexec";
 1499             n2[0] = "lxc-usernsexec";
 1500 
 1501             lxc_list_for_each(it, &conf->id_map) {
 1502                 map = it->elem;
 1503                 n2args += 2;
 1504                 n2 = realloc(n2, n2args * sizeof(char *));
 1505                 if (!n2)
 1506                     _exit(EXIT_FAILURE);
 1507 
 1508                 n2[n2args - 2] = "-m";
 1509                 n2[n2args - 1] = malloc(200);
 1510                 if (!n2[n2args - 1])
 1511                     _exit(EXIT_FAILURE);
 1512 
 1513                 ret = strnprintf(n2[n2args - 1], 200, "%c:%lu:%lu:%lu",
 1514                            map->idtype == ID_TYPE_UID ? 'u' : 'g',
 1515                            map->nsid, map->hostid, map->range);
 1516                 if (ret < 0)
 1517                     _exit(EXIT_FAILURE);
 1518             }
 1519 
 1520             hostuid_mapped = mapped_hostid(geteuid(), conf, ID_TYPE_UID);
 1521             extraargs = hostuid_mapped >= 0 ? 1 : 3;
 1522 
 1523             n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *));
 1524             if (!n2)
 1525                 _exit(EXIT_FAILURE);
 1526 
 1527             if (hostuid_mapped < 0) {
 1528                 hostuid_mapped = find_unmapped_nsid(conf, ID_TYPE_UID);
 1529                 n2[n2args++] = "-m";
 1530                 if (hostuid_mapped < 0) {
 1531                     ERROR("Failed to find free uid to map");
 1532                     _exit(EXIT_FAILURE);
 1533                 }
 1534 
 1535                 n2[n2args++] = malloc(200);
 1536                 if (!n2[n2args - 1]) {
 1537                     SYSERROR("out of memory");
 1538                     _exit(EXIT_FAILURE);
 1539                 }
 1540 
 1541                 ret = strnprintf(n2[n2args - 1], 200, "u:%d:%d:1",
 1542                            hostuid_mapped, geteuid());
 1543                 if (ret < 0)
 1544                     _exit(EXIT_FAILURE);
 1545             }
 1546 
 1547             hostgid_mapped = mapped_hostid(getegid(), conf, ID_TYPE_GID);
 1548             extraargs = hostgid_mapped >= 0 ? 1 : 3;
 1549 
 1550             n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *));
 1551             if (!n2)
 1552                 _exit(EXIT_FAILURE);
 1553 
 1554             if (hostgid_mapped < 0) {
 1555                 hostgid_mapped = find_unmapped_nsid(conf, ID_TYPE_GID);
 1556                 n2[n2args++] = "-m";
 1557                 if (hostgid_mapped < 0) {
 1558                     ERROR("Failed to find free gid to map");
 1559                     _exit(EXIT_FAILURE);
 1560                 }
 1561 
 1562                 n2[n2args++] = malloc(200);
 1563                 if (!n2[n2args - 1]) {
 1564                     SYSERROR("out of memory");
 1565                     _exit(EXIT_FAILURE);
 1566                 }
 1567 
 1568                 ret = strnprintf(n2[n2args - 1], 200, "g:%d:%d:1",
 1569                            hostgid_mapped, getegid());
 1570                 if (ret < 0)
 1571                     _exit(EXIT_FAILURE);
 1572             }
 1573 
 1574             n2[n2args++] = "--";
 1575 
 1576             for (i = 0; i < nargs; i++)
 1577                 n2[i + n2args] = newargv[i];
 1578             n2args += nargs;
 1579 
 1580             /* Finally add "--mapped-uid $uid" to tell template what
 1581              * to chown cached images to.
 1582              */
 1583             n2args += 4;
 1584             n2 = realloc(n2, n2args * sizeof(char *));
 1585             if (!n2)
 1586                 _exit(EXIT_FAILURE);
 1587 
 1588             /* note n2[n2args-1] is NULL */
 1589             n2[n2args - 5] = "--mapped-uid";
 1590 
 1591             ret = strnprintf(txtuid, 20, "%d", hostuid_mapped);
 1592             if (ret < 0) {
 1593                 free(newargv);
 1594                 free(n2);
 1595                 _exit(EXIT_FAILURE);
 1596             }
 1597 
 1598             n2[n2args - 4] = txtuid;
 1599             n2[n2args - 3] = "--mapped-gid";
 1600 
 1601             ret = strnprintf(txtgid, 20, "%d", hostgid_mapped);
 1602             if (ret < 0) {
 1603                 free(newargv);
 1604                 free(n2);
 1605                 _exit(EXIT_FAILURE);
 1606             }
 1607 
 1608             n2[n2args - 2] = txtgid;
 1609             n2[n2args - 1] = NULL;
 1610             free(newargv);
 1611             newargv = n2;
 1612         }
 1613 
 1614         execvp(tpath, newargv);
 1615         SYSERROR("Failed to execute template %s", tpath);
 1616         _exit(EXIT_FAILURE);
 1617     }
 1618 
 1619     ret = wait_for_pid(pid);
 1620     if (ret != 0) {
 1621         ERROR("Failed to create container from template");
 1622         return false;
 1623     }
 1624 
 1625     return true;
 1626 }
 1627 
 1628 static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
 1629 {
 1630     long flen;
 1631     size_t len;
 1632     char *contents;
 1633     FILE *f;
 1634     int ret = -1;
 1635 #if HAVE_OPENSSL
 1636     int i;
 1637     unsigned int md_len = 0;
 1638     unsigned char md_value[EVP_MAX_MD_SIZE];
 1639     char *tpath;
 1640 #endif
 1641 
 1642     f = fopen(path, "re");
 1643     if (f == NULL)
 1644         return false;
 1645 
 1646     ret = fseek(f, 0, SEEK_END);
 1647     if (ret < 0)
 1648         goto out_error;
 1649 
 1650     ret = -1;
 1651     flen = ftell(f);
 1652     if (flen < 0)
 1653         goto out_error;
 1654 
 1655     ret = fseek(f, 0, SEEK_SET);
 1656     if (ret < 0)
 1657         goto out_error;
 1658 
 1659     ret = fseek(f, 0, SEEK_SET);
 1660     if (ret < 0)
 1661         goto out_error;
 1662 
 1663     ret = -1;
 1664     contents = malloc(flen + 1);
 1665     if (!contents)
 1666         goto out_error;
 1667 
 1668     len = fread(contents, 1, flen, f);
 1669     if (len != flen)
 1670         goto out_free_contents;
 1671 
 1672     contents[flen] = '\0';
 1673 
 1674     ret = fclose(f);
 1675     f = NULL;
 1676     if (ret < 0)
 1677         goto out_free_contents;
 1678 
 1679 #if HAVE_OPENSSL
 1680     tpath = get_template_path(t);
 1681     if (!tpath) {
 1682         ERROR("Invalid template \"%s\" specified", t);
 1683         goto out_free_contents;
 1684     }
 1685 
 1686     ret = sha1sum_file(tpath, md_value, &md_len);
 1687     if (ret < 0) {
 1688         ERROR("Failed to get sha1sum of %s", tpath);
 1689         free(tpath);
 1690         goto out_free_contents;
 1691     }
 1692     free(tpath);
 1693 #endif
 1694 
 1695     f = fopen(path, "we");
 1696     if (f == NULL) {
 1697         SYSERROR("Reopening config for writing");
 1698         free(contents);
 1699         return false;
 1700     }
 1701 
 1702     fprintf(f, "# Template used to create this container: %s\n", t);
 1703     if (argv) {
 1704         fprintf(f, "# Parameters passed to the template:");
 1705         while (*argv) {
 1706             fprintf(f, " %s", *argv);
 1707             argv++;
 1708         }
 1709         fprintf(f, "\n");
 1710     }
 1711 
 1712 #if HAVE_OPENSSL
 1713     fprintf(f, "# Template script checksum (SHA-1): ");
 1714     for (i=0; i<md_len; i++)
 1715         fprintf(f, "%02x", md_value[i]);
 1716     fprintf(f, "\n");
 1717 #endif
 1718     fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n");
 1719     fprintf(f, "\n# Uncomment the following line to support nesting containers:\n");
 1720     fprintf(f, "#lxc.include = " LXCTEMPLATECONFIG "/nesting.conf\n");
 1721     fprintf(f, "# (Be aware this has security implications)\n\n");
 1722     if (fwrite(contents, 1, flen, f) != flen) {
 1723         SYSERROR("Writing original contents");
 1724         free(contents);
 1725         fclose(f);
 1726         return false;
 1727     }
 1728 
 1729     ret = 0;
 1730 
 1731 out_free_contents:
 1732     free(contents);
 1733 
 1734 out_error:
 1735     if (f) {
 1736         int newret;
 1737         newret = fclose(f);
 1738         if (ret == 0)
 1739             ret = newret;
 1740     }
 1741 
 1742     if (ret < 0) {
 1743         SYSERROR("Error prepending header");
 1744         return false;
 1745     }
 1746 
 1747     return true;
 1748 }
 1749 
 1750 static void lxcapi_clear_config(struct lxc_container *c)
 1751 {
 1752     if (!c || !c->lxc_conf)
 1753         return;
 1754 
 1755     lxc_conf_free(c->lxc_conf);
 1756     c->lxc_conf = NULL;
 1757 }
 1758 
 1759 #define do_lxcapi_clear_config(c) lxcapi_clear_config(c)
 1760 
 1761 /*
 1762  * lxcapi_create:
 1763  * create a container with the given parameters.
 1764  * @c: container to be created.  It has the lxcpath, name, and a starting
 1765  *     configuration already set
 1766  * @t: the template to execute to instantiate the root filesystem and
 1767  *     adjust the configuration.
 1768  * @bdevtype: backing store type to use.  If NULL, dir will be used.
 1769  * @specs: additional parameters for the backing store, i.e. LVM vg to
 1770  *         use.
 1771  *
 1772  * @argv: the arguments to pass to the template, terminated by NULL.  If no
 1773  * arguments, you can just pass NULL.
 1774  */
 1775 static bool do_lxcapi_create(struct lxc_container *c, const char *t,
 1776                  const char *bdevtype, struct bdev_specs *specs,
 1777                  int flags, char *const argv[])
 1778 {
 1779     __do_free char *path_template = NULL;
 1780     int partial_fd;
 1781     mode_t mask;
 1782     pid_t pid;
 1783     bool ret = false, rootfs_managed = true;
 1784 
 1785     if (!c)
 1786         return false;
 1787 
 1788     if (t) {
 1789         path_template = get_template_path(t);
 1790         if (!path_template)
 1791             return syserror_set(ENOENT, "Template \"%s\" not found", t);
 1792     }
 1793 
 1794     /* If a template is passed in, and the rootfs already is defined in the
 1795      * container config and exists, then the caller is trying to create an
 1796      * existing container. Return an error, but do NOT delete the container.
 1797      */
 1798     if (do_lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path &&
 1799         access(c->lxc_conf->rootfs.path, F_OK) == 0 && path_template)
 1800         return syserror_set(EEXIST, "Container \"%s\" already exists in \"%s\"", c->name, c->config_path);
 1801 
 1802     if (!c->lxc_conf &&
 1803         !do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config")))
 1804         return syserror_set(EINVAL, "Failed to load default configuration file %s",
 1805                     lxc_global_config_value("lxc.default_config"));
 1806 
 1807     if (!create_container_dir(c))
 1808         return syserror_set(EINVAL, "Failed to create container %s", c->name);
 1809 
 1810     if (c->lxc_conf->rootfs.path)
 1811         rootfs_managed = false;
 1812 
 1813     /* If both template and rootfs.path are set, template is setup as
 1814      * rootfs.path. The container is already created if we have a config and
 1815      * rootfs.path is accessible
 1816      */
 1817     if (!c->lxc_conf->rootfs.path && !path_template) {
 1818         /* No template passed in and rootfs does not exist. */
 1819         if (!c->save_config(c, NULL)) {
 1820             ERROR("Failed to save initial config for \"%s\"", c->name);
 1821             goto out;
 1822         }
 1823         ret = true;
 1824         goto out;
 1825     }
 1826 
 1827     /* Rootfs passed into configuration, but does not exist. */
 1828     if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0)
 1829         goto out;
 1830 
 1831     if (do_lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !path_template) {
 1832         /* Rootfs already existed, user just wanted to save the loaded
 1833          * configuration.
 1834          */
 1835         if (!c->save_config(c, NULL))
 1836             ERROR("Failed to save initial config for \"%s\"", c->name);
 1837 
 1838         ret = true;
 1839         goto out;
 1840     }
 1841 
 1842     /* Mark that this container is being created */
 1843     partial_fd = create_partial(c);
 1844     if (partial_fd < 0)
 1845         goto out;
 1846 
 1847     /* No need to get disk lock bc we have the partial lock. */
 1848 
 1849     mask = umask(0022);
 1850 
 1851     /* Create the storage.
 1852      * Note we can't do this in the same task as we use to execute the
 1853      * template because of the way zfs works.
 1854      * After you 'zfs create', zfs mounts the fs only in the initial
 1855      * namespace.
 1856      */
 1857     pid = fork();
 1858     if (pid < 0) {
 1859         SYSERROR("Failed to fork task for container creation template");
 1860         goto out_unlock;
 1861     }
 1862 
 1863     if (pid == 0) { /* child */
 1864         struct lxc_storage *bdev = NULL;
 1865 
 1866         bdev = do_storage_create(c, bdevtype, specs);
 1867         if (!bdev) {
 1868             ERROR("Failed to create %s storage for %s",
 1869                   bdevtype ? bdevtype : "(none)", c->name);
 1870             _exit(EXIT_FAILURE);
 1871         }
 1872 
 1873         /* Save config file again to store the new rootfs location. */
 1874         if (!do_lxcapi_save_config(c, NULL)) {
 1875             ERROR("Failed to save initial config for %s", c->name);
 1876             /* Parent task won't see the storage driver in the
 1877              * config so we delete it.
 1878              */
 1879             bdev->ops->umount(bdev);
 1880             bdev->ops->destroy(bdev);
 1881             _exit(EXIT_FAILURE);
 1882         }
 1883 
 1884         _exit(EXIT_SUCCESS);
 1885     }
 1886 
 1887     if (wait_for_pid(pid) != 0)
 1888         goto out_unlock;
 1889 
 1890     /* Reload config to get the rootfs. */
 1891     lxc_conf_free(c->lxc_conf);
 1892     c->lxc_conf = NULL;
 1893 
 1894     if (!load_config_locked(c, c->configfile))
 1895         goto out_unlock;
 1896 
 1897     if (!create_run_template(c, path_template, !!(flags & LXC_CREATE_QUIET), argv))
 1898         goto out_unlock;
 1899 
 1900     /* Now clear out the lxc_conf we have, reload from the created
 1901      * container.
 1902      */
 1903     do_lxcapi_clear_config(c);
 1904 
 1905     if (t) {
 1906         if (!prepend_lxc_header(c->configfile, path_template, argv)) {
 1907             ERROR("Failed to prepend header to config file");
 1908             goto out_unlock;
 1909         }
 1910     }
 1911 
 1912     ret = load_config_locked(c, c->configfile);
 1913 
 1914 out_unlock:
 1915     umask(mask);
 1916     remove_partial(c, partial_fd);
 1917 
 1918 out:
 1919     if (!ret) {
 1920         bool reset_managed = c->lxc_conf->rootfs.managed;
 1921 
 1922         /*
 1923          * Ensure that we don't destroy storage we didn't create
 1924          * ourselves.
 1925          */
 1926         if (!rootfs_managed)
 1927             c->lxc_conf->rootfs.managed = false;
 1928         container_destroy(c, NULL);
 1929         c->lxc_conf->rootfs.managed = reset_managed;
 1930     }
 1931 
 1932     return ret;
 1933 }
 1934 
 1935 static bool lxcapi_create(struct lxc_container *c, const char *t,
 1936               const char *bdevtype, struct bdev_specs *specs,
 1937               int flags, char *const argv[])
 1938 {
 1939     bool ret;
 1940 
 1941     current_config = c ? c->lxc_conf : NULL;
 1942 
 1943     ret = do_lxcapi_create(c, t, bdevtype, specs, flags, argv);
 1944     current_config = NULL;
 1945     return ret;
 1946 }
 1947 
 1948 static bool do_lxcapi_reboot(struct lxc_container *c)
 1949 {
 1950     __do_close int pidfd = -EBADF;
 1951     pid_t pid = -1;
 1952     int ret;
 1953     int rebootsignal = SIGINT;
 1954 
 1955     if (!c)
 1956         return false;
 1957 
 1958     if (!do_lxcapi_is_running(c))
 1959         return false;
 1960 
 1961     pidfd = do_lxcapi_init_pidfd(c);
 1962     if (pidfd < 0) {
 1963         pid = do_lxcapi_init_pid(c);
 1964         if (pid <= 0)
 1965             return false;
 1966     }
 1967 
 1968     if (c->lxc_conf && c->lxc_conf->rebootsignal)
 1969         rebootsignal = c->lxc_conf->rebootsignal;
 1970 
 1971     if (pidfd >= 0)
 1972         ret = lxc_raw_pidfd_send_signal(pidfd, rebootsignal, NULL, 0);
 1973     else
 1974         ret = kill(pid, rebootsignal);
 1975     if (ret < 0)
 1976         return log_warn(false, "Failed to send signal %d to pid %d",
 1977                 rebootsignal, pid);
 1978 
 1979     return true;
 1980 }
 1981 
 1982 WRAP_API(bool, lxcapi_reboot)
 1983 
 1984 static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout)
 1985 {
 1986     __do_close int pidfd = -EBADF, state_client_fd = -EBADF;
 1987     int rebootsignal = SIGINT;
 1988     pid_t pid = -1;
 1989     lxc_state_t states[MAX_STATE] = {0};
 1990     int killret, ret;
 1991 
 1992     if (!c)
 1993         return false;
 1994 
 1995     if (!do_lxcapi_is_running(c))
 1996         return true;
 1997 
 1998     pidfd = do_lxcapi_init_pidfd(c);
 1999     if (pidfd < 0) {
 2000         pid = do_lxcapi_init_pid(c);
 2001         if (pid <= 0)
 2002             return true;
 2003     }
 2004 
 2005     if (c->lxc_conf && c->lxc_conf->rebootsignal)
 2006         rebootsignal = c->lxc_conf->rebootsignal;
 2007 
 2008     /* Add a new state client before sending the shutdown signal so that we
 2009      * don't miss a state.
 2010      */
 2011     if (timeout != 0) {
 2012         states[RUNNING] = 2;
 2013         ret = lxc_cmd_add_state_client(c->name, c->config_path, states,
 2014                            &state_client_fd);
 2015         if (ret < 0)
 2016             return false;
 2017 
 2018         if (state_client_fd < 0)
 2019             return false;
 2020 
 2021         if (ret == RUNNING)
 2022             return true;
 2023 
 2024         if (ret < MAX_STATE)
 2025             return false;
 2026     }
 2027 
 2028     /* Send reboot signal to container. */
 2029     if (pidfd >= 0)
 2030         killret = lxc_raw_pidfd_send_signal(pidfd, rebootsignal, NULL, 0);
 2031     else
 2032         killret = kill(pid, rebootsignal);
 2033     if (killret < 0)
 2034         return log_warn(false, "Failed to send signal %d to pidfd(%d)/pid(%d)", rebootsignal, pidfd, pid);
 2035     TRACE("Sent signal %d to pidfd(%d)/pid(%d)", rebootsignal, pidfd, pid);
 2036 
 2037     if (timeout == 0)
 2038         return true;
 2039 
 2040     ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout);
 2041     if (ret < 0)
 2042         return false;
 2043 
 2044     TRACE("Received state \"%s\"", lxc_state2str(ret));
 2045     if (ret != RUNNING)
 2046         return false;
 2047 
 2048     return true;
 2049 }
 2050 
 2051 WRAP_API_1(bool, lxcapi_reboot2, int)
 2052 
 2053 static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout)
 2054 {
 2055     __do_close int pidfd = -EBADF, state_client_fd = -EBADF;
 2056     int haltsignal = SIGPWR;
 2057     pid_t pid = -1;
 2058     lxc_state_t states[MAX_STATE] = {0};
 2059     int killret, ret;
 2060 
 2061     if (!c)
 2062         return false;
 2063 
 2064     if (!do_lxcapi_is_running(c))
 2065         return true;
 2066 
 2067     pidfd = do_lxcapi_init_pidfd(c);
 2068     pid = do_lxcapi_init_pid(c);
 2069     if (pid <= 0)
 2070         return true;
 2071 
 2072     /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */
 2073     if (c->lxc_conf && c->lxc_conf->haltsignal)
 2074         haltsignal = c->lxc_conf->haltsignal;
 2075     else if (task_blocks_signal(pid, (SIGRTMIN + 3)))
 2076         haltsignal = (SIGRTMIN + 3);
 2077 
 2078 
 2079     /*
 2080      * Add a new state client before sending the shutdown signal so
 2081      * that we don't miss a state.
 2082      */
 2083     if (timeout != 0) {
 2084         states[STOPPED] = 1;
 2085         ret = lxc_cmd_add_state_client(c->name, c->config_path, states,
 2086                            &state_client_fd);
 2087         if (ret < 0)
 2088             return false;
 2089 
 2090         if (state_client_fd < 0)
 2091             return false;
 2092 
 2093         if (ret == STOPPED)
 2094             return true;
 2095 
 2096         if (ret < MAX_STATE)
 2097             return false;
 2098     }
 2099 
 2100     if (pidfd >= 0) {
 2101         struct pollfd pidfd_poll = {
 2102             .events = POLLIN,
 2103             .fd = pidfd,
 2104         };
 2105 
 2106         killret = lxc_raw_pidfd_send_signal(pidfd, haltsignal,
 2107                             NULL, 0);
 2108         if (killret < 0)
 2109             return log_warn(false, "Failed to send signal %d to pidfd %d",
 2110                     haltsignal, pidfd);
 2111 
 2112         TRACE("Sent signal %d to pidfd %d", haltsignal, pidfd);
 2113 
 2114         /*
 2115          * No need for going through all of the state server
 2116          * complications anymore. We can just poll on pidfds. :)
 2117          */
 2118 
 2119         if (timeout != 0) {
 2120             ret = poll(&pidfd_poll, 1, timeout * 1000);
 2121             if (ret < 0 || !(pidfd_poll.revents & POLLIN))
 2122                 return false;
 2123 
 2124             TRACE("Pidfd polling detected container exit");
 2125         }
 2126     } else {
 2127         killret = kill(pid, haltsignal);
 2128         if (killret < 0)
 2129             return log_warn(false, "Failed to send signal %d to pid %d",
 2130                     haltsignal, pid);
 2131 
 2132         TRACE("Sent signal %d to pid %d", haltsignal, pid);
 2133     }
 2134 
 2135     if (timeout == 0)
 2136         return true;
 2137 
 2138     ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout);
 2139     if (ret < 0)
 2140         return false;
 2141 
 2142     TRACE("Received state \"%s\"", lxc_state2str(ret));
 2143     if (ret != STOPPED)
 2144         return false;
 2145 
 2146     return true;
 2147 }
 2148 
 2149 WRAP_API_1(bool, lxcapi_shutdown, int)
 2150 
 2151 static bool lxcapi_createl(struct lxc_container *c, const char *t,
 2152         const char *bdevtype, struct bdev_specs *specs, int flags, ...)
 2153 {
 2154     bool bret = false;
 2155     char **args = NULL;
 2156     va_list ap;
 2157 
 2158     if (!c)
 2159         return false;
 2160 
 2161     current_config = c->lxc_conf;
 2162 
 2163     /*
 2164      * since we're going to wait for create to finish, I don't think we
 2165      * need to get a copy of the arguments.
 2166      */
 2167     va_start(ap, flags);
 2168     args = lxc_va_arg_list_to_argv(ap, 0, 0);
 2169     va_end(ap);
 2170     if (!args) {
 2171         ERROR("Failed to allocate memory");
 2172         goto out;
 2173     }
 2174 
 2175     bret = do_lxcapi_create(c, t, bdevtype, specs, flags, args);
 2176 
 2177 out:
 2178     free(args);
 2179     current_config = NULL;
 2180     return bret;
 2181 }
 2182 
 2183 static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key)
 2184 {
 2185     if (strequal(key, "lxc.cgroup"))
 2186         return clear_unexp_config_line(conf, key, true);
 2187 
 2188     if (strequal(key, "lxc.network"))
 2189         return clear_unexp_config_line(conf, key, true);
 2190 
 2191     if (strequal(key, "lxc.net"))
 2192         return clear_unexp_config_line(conf, key, true);
 2193 
 2194     /* Clear a network with a specific index. */
 2195     if (strnequal(key, "lxc.net.", 8)) {
 2196         int ret;
 2197         const char *idx;
 2198 
 2199         idx = key + 8;
 2200         ret = lxc_safe_uint(idx, &(unsigned int){0});
 2201         if (!ret)
 2202             return clear_unexp_config_line(conf, key, true);
 2203     }
 2204 
 2205     if (strequal(key, "lxc.hook"))
 2206         return clear_unexp_config_line(conf, key, true);
 2207 
 2208     return clear_unexp_config_line(conf, key, false);
 2209 }
 2210 
 2211 static bool do_lxcapi_clear_config_item(struct lxc_container *c,
 2212                     const char *key)
 2213 {
 2214     int ret = 1;
 2215     struct lxc_config_t *config;
 2216 
 2217     if (!c || !c->lxc_conf)
 2218         return false;
 2219 
 2220     if (container_mem_lock(c))
 2221         return false;
 2222 
 2223     config = lxc_get_config(key);
 2224 
 2225     ret = config->clr(key, c->lxc_conf, NULL);
 2226     if (!ret)
 2227         do_clear_unexp_config_line(c->lxc_conf, key);
 2228 
 2229     container_mem_unlock(c);
 2230     return ret == 0;
 2231 }
 2232 
 2233 WRAP_API_1(bool, lxcapi_clear_config_item, const char *)
 2234 
 2235 static inline bool enter_net_ns(struct lxc_container *c)
 2236 {
 2237     pid_t pid = do_lxcapi_init_pid(c);
 2238 
 2239     if (pid < 0)
 2240         return false;
 2241 
 2242     if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) &&
 2243         (access("/proc/self/ns/user", F_OK) == 0))
 2244         if (!switch_to_ns(pid, "user"))
 2245             return false;
 2246 
 2247     return switch_to_ns(pid, "net");
 2248 }
 2249 
 2250 /* Used by qsort and bsearch functions for comparing names. */
 2251 static inline int string_cmp(char **first, char **second)
 2252 {
 2253     return strcmp(*first, *second);
 2254 }
 2255 
 2256 /* Used by qsort and bsearch functions for comparing container names. */
 2257 static inline int container_cmp(struct lxc_container **first,
 2258                 struct lxc_container **second)
 2259 {
 2260     return strcmp((*first)->name, (*second)->name);
 2261 }
 2262 
 2263 static bool add_to_array(char ***names, char *cname, int pos)
 2264 {
 2265     char **newnames = (char**)realloc(*names, (pos+1) * sizeof(char *));
 2266     if (!newnames) {
 2267         ERROR("Out of memory");
 2268         return false;
 2269     }
 2270 
 2271     *names = newnames;
 2272     newnames[pos] = strdup(cname);
 2273     if (!newnames[pos])
 2274         return false;
 2275 
 2276     /* Sort the array as we will use binary search on it. */
 2277     qsort(newnames, pos + 1, sizeof(char *),
 2278           (int (*)(const void *, const void *))string_cmp);
 2279 
 2280     return true;
 2281 }
 2282 
 2283 static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c,
 2284              int pos, bool sort)
 2285 {
 2286     struct lxc_container **newlist = realloc(*list, (pos + 1) * sizeof(struct lxc_container *));
 2287     if (!newlist) {
 2288         ERROR("Out of memory");
 2289         return false;
 2290     }
 2291 
 2292     *list = newlist;
 2293     newlist[pos] = c;
 2294 
 2295     /* Sort the array as we will use binary search on it. */
 2296     if (sort)
 2297         qsort(newlist, pos + 1, sizeof(struct lxc_container *),
 2298               (int (*)(const void *, const void *))container_cmp);
 2299 
 2300     return true;
 2301 }
 2302 
 2303 static char** get_from_array(char ***names, char *cname, int size)
 2304 {
 2305     if (!*names)
 2306         return NULL;
 2307 
 2308     return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp);
 2309 }
 2310 
 2311 static bool array_contains(char ***names, char *cname, int size)
 2312 {
 2313     if(get_from_array(names, cname, size) != NULL)
 2314         return true;
 2315 
 2316     return false;
 2317 }
 2318 
 2319 static bool remove_from_array(char ***names, char *cname, int size)
 2320 {
 2321     char **result = get_from_array(names, cname, size);
 2322     if (result != NULL) {
 2323         size_t i = result - *names;
 2324         free(*result);
 2325         memmove(*names+i, *names+i+1, (size-i-1) * sizeof(char*));
 2326         char **newnames = (char**)realloc(*names, (size-1) * sizeof(char *));
 2327         if (!newnames) {
 2328             ERROR("Out of memory");
 2329             return true;
 2330         }
 2331 
 2332         *names = newnames;
 2333         return true;
 2334     }
 2335 
 2336     return false;
 2337 }
 2338 
 2339 static char **do_lxcapi_get_interfaces(struct lxc_container *c)
 2340 {
 2341     pid_t pid;
 2342     int i, count = 0, pipefd[2];
 2343     char **interfaces = NULL;
 2344     char interface[IFNAMSIZ];
 2345 
 2346     if (pipe2(pipefd, O_CLOEXEC))
 2347         return log_error_errno(NULL, errno, "Failed to create pipe");
 2348 
 2349     pid = fork();
 2350     if (pid < 0) {
 2351         close(pipefd[0]);
 2352         close(pipefd[1]);
 2353         return log_error_errno(NULL, errno, "Failed to fork task to get interfaces information");
 2354     }
 2355 
 2356     if (pid == 0) {
 2357         call_cleaner(netns_freeifaddrs) struct netns_ifaddrs *ifaddrs = NULL;
 2358         struct netns_ifaddrs *ifa = NULL;
 2359         int ret = 1;
 2360         int nbytes;
 2361 
 2362         /* close the read-end of the pipe */
 2363         close(pipefd[0]);
 2364 
 2365         if (!enter_net_ns(c)) {
 2366             SYSERROR("Failed to enter network namespace");
 2367             goto out;
 2368         }
 2369 
 2370         /* Grab the list of interfaces */
 2371         if (netns_getifaddrs(&ifaddrs, -1, &(bool){false})) {
 2372             SYSERROR("Failed to get interfaces list");
 2373             goto out;
 2374         }
 2375 
 2376         /* Iterate through the interfaces */
 2377         for (ifa = ifaddrs; ifa != NULL;
 2378              ifa = ifa->ifa_next) {
 2379             nbytes = lxc_write_nointr(pipefd[1], ifa->ifa_name, IFNAMSIZ);
 2380             if (nbytes < 0)
 2381                 goto out;
 2382 
 2383             count++;
 2384         }
 2385 
 2386         ret = 0;
 2387 
 2388     out:
 2389         /* close the write-end of the pipe, thus sending EOF to the reader */
 2390         close(pipefd[1]);
 2391         _exit(ret);
 2392     }
 2393 
 2394     /* close the write-end of the pipe */
 2395     close(pipefd[1]);
 2396 
 2397     while (lxc_read_nointr(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) {
 2398         interface[IFNAMSIZ - 1] = '\0';
 2399 
 2400         if (array_contains(&interfaces, interface, count))
 2401             continue;
 2402 
 2403         if (!add_to_array(&interfaces, interface, count))
 2404             ERROR("Failed to add \"%s\" to array", interface);
 2405 
 2406         count++;
 2407     }
 2408 
 2409     if (wait_for_pid(pid)) {
 2410         for (i = 0; i < count; i++)
 2411             free(interfaces[i]);
 2412 
 2413         free(interfaces);
 2414         interfaces = NULL;
 2415     }
 2416 
 2417     /* close the read-end of the pipe */
 2418     close(pipefd[0]);
 2419 
 2420     /* Append NULL to the array */
 2421     if (interfaces)
 2422         interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);
 2423 
 2424     return interfaces;
 2425 }
 2426 
 2427 WRAP_API(char **, lxcapi_get_interfaces)
 2428 
 2429 static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
 2430                 const char *family, int scope)
 2431 {
 2432     int i, ret;
 2433     pid_t pid;
 2434     int pipefd[2];
 2435     char address[INET6_ADDRSTRLEN];
 2436     int count = 0;
 2437     char **addresses = NULL;
 2438 
 2439     ret = pipe2(pipefd, O_CLOEXEC);
 2440     if (ret < 0)
 2441         return log_error_errno(NULL, errno, "Failed to create pipe");
 2442 
 2443     pid = fork();
 2444     if (pid < 0) {
 2445         SYSERROR("Failed to create new process");
 2446         close(pipefd[0]);
 2447         close(pipefd[1]);
 2448         return NULL;
 2449     }
 2450 
 2451     if (pid == 0) {
 2452         call_cleaner(netns_freeifaddrs) struct netns_ifaddrs *ifaddrs = NULL;
 2453         struct netns_ifaddrs *ifa = NULL;
 2454         ssize_t nbytes;
 2455         char addressOutputBuffer[INET6_ADDRSTRLEN];
 2456         char *address_ptr = NULL;
 2457         void *address_ptr_tmp = NULL;
 2458 
 2459         /* close the read-end of the pipe */
 2460         close(pipefd[0]);
 2461 
 2462         if (!enter_net_ns(c)) {
 2463             SYSERROR("Failed to attach to network namespace");
 2464             goto out;
 2465         }
 2466 
 2467         /* Grab the list of interfaces */
 2468         if (netns_getifaddrs(&ifaddrs, -1, &(bool){false})) {
 2469             SYSERROR("Failed to get interfaces list");
 2470             goto out;
 2471         }
 2472 
 2473         /* Iterate through the interfaces */
 2474         for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
 2475             if (ifa->ifa_addr == NULL)
 2476                 continue;
 2477 
 2478 #pragma GCC diagnostic push
 2479 #pragma GCC diagnostic ignored "-Wcast-align"
 2480 
 2481             if (ifa->ifa_addr->sa_family == AF_INET) {
 2482                 if (family && !strequal(family, "inet"))
 2483                     continue;
 2484 
 2485                 address_ptr_tmp = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
 2486             } else {
 2487                 if (family && !strequal(family, "inet6"))
 2488                     continue;
 2489 
 2490                 if (((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id != scope)
 2491                     continue;
 2492 
 2493                 address_ptr_tmp = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
 2494             }
 2495 
 2496 #pragma GCC diagnostic pop
 2497 
 2498             if (interface && !strequal(interface, ifa->ifa_name))
 2499                 continue;
 2500             else if (!interface && strequal("lo", ifa->ifa_name))
 2501                 continue;
 2502 
 2503             address_ptr = (char *)inet_ntop(ifa->ifa_addr->sa_family, address_ptr_tmp,
 2504                             addressOutputBuffer,
 2505                             sizeof(addressOutputBuffer));
 2506             if (!address_ptr)
 2507                 continue;
 2508 
 2509             nbytes = lxc_write_nointr(pipefd[1], address_ptr, INET6_ADDRSTRLEN);
 2510             if (nbytes != INET6_ADDRSTRLEN) {
 2511                 SYSERROR("Failed to send ipv6 address \"%s\"", address_ptr);
 2512                 goto out;
 2513             }
 2514 
 2515             count++;
 2516         }
 2517 
 2518         ret = 0;
 2519 
 2520     out:
 2521         /* close the write-end of the pipe, thus sending EOF to the reader */
 2522         close(pipefd[1]);
 2523         _exit(ret);
 2524     }
 2525 
 2526     /* close the write-end of the pipe */
 2527     close(pipefd[1]);
 2528 
 2529     while (lxc_read_nointr(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) {
 2530         address[INET6_ADDRSTRLEN - 1] = '\0';
 2531 
 2532         if (!add_to_array(&addresses, address, count))
 2533             ERROR("PARENT: add_to_array failed");
 2534 
 2535         count++;
 2536     }
 2537 
 2538     if (wait_for_pid(pid)) {
 2539         for (i = 0; i < count; i++)
 2540             free(addresses[i]);
 2541 
 2542         free(addresses);
 2543         addresses = NULL;
 2544     }
 2545 
 2546     /* close the read-end of the pipe */
 2547     close(pipefd[0]);
 2548 
 2549     /* Append NULL to the array */
 2550     if (addresses)
 2551         addresses = (char **)lxc_append_null_to_array((void **)addresses, count);
 2552 
 2553     return addresses;
 2554 }
 2555 
 2556 WRAP_API_3(char **, lxcapi_get_ips, const char *, const char *, int)
 2557 
 2558 static int do_lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
 2559 {
 2560     int ret = -1;
 2561     struct lxc_config_t *config;
 2562 
 2563     if (!c || !c->lxc_conf)
 2564         return -1;
 2565 
 2566     if (container_mem_lock(c))
 2567         return -1;
 2568 
 2569     config = lxc_get_config(key);
 2570 
 2571     ret = config->get(key, retv, inlen, c->lxc_conf, NULL);
 2572 
 2573     container_mem_unlock(c);
 2574     return ret;
 2575 }
 2576 
 2577 WRAP_API_3(int, lxcapi_get_config_item, const char *, char *, int)
 2578 
 2579 static char* do_lxcapi_get_running_config_item(struct lxc_container *c, const char *key)
 2580 {
 2581     char *ret;
 2582 
 2583     if (!c || !c->lxc_conf)
 2584         return NULL;
 2585 
 2586     if (container_mem_lock(c))
 2587         return NULL;
 2588 
 2589     ret = lxc_cmd_get_config_item(c->name, key, do_lxcapi_get_config_path(c));
 2590     container_mem_unlock(c);
 2591     return ret;
 2592 }
 2593 
 2594 WRAP_API_1(char *, lxcapi_get_running_config_item, const char *)
 2595 
 2596 static int do_lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
 2597 {
 2598     int ret = -1;
 2599 
 2600     /* List all config items. */
 2601     if (!key)
 2602         return lxc_list_config_items(retv, inlen);
 2603 
 2604     if (!c || !c->lxc_conf)
 2605         return -1;
 2606 
 2607     if (container_mem_lock(c))
 2608         return -1;
 2609 
 2610     /* Support 'lxc.net.<idx>', i.e. 'lxc.net.0'
 2611      * This is an intelligent result to show which keys are valid given the
 2612      * type of nic it is.
 2613      */
 2614     if (strnequal(key, "lxc.net.", 8))
 2615         ret = lxc_list_net(c->lxc_conf, key, retv, inlen);
 2616     else
 2617         ret = lxc_list_subkeys(c->lxc_conf, key, retv, inlen);
 2618 
 2619     container_mem_unlock(c);
 2620     return ret;
 2621 }
 2622 
 2623 WRAP_API_3(int, lxcapi_get_keys, const char *, char *, int)
 2624 
 2625 static bool do_lxcapi_save_config(struct lxc_container *c, const char *alt_file)
 2626 {
 2627     int fd, lret;
 2628     bool ret = false, need_disklock = false;
 2629 
 2630     if (!alt_file)
 2631         alt_file = c->configfile;
 2632 
 2633     if (!alt_file)
 2634         return false;
 2635 
 2636     /* If we haven't yet loaded a config, load the stock config. */
 2637     if (!c->lxc_conf) {
 2638         if (!do_lxcapi_load_config(c, lxc_global_config_value("lxc.default_config"))) {
 2639             ERROR("Error loading default configuration file %s "
 2640                   "while saving %s",
 2641                   lxc_global_config_value("lxc.default_config"),
 2642                   c->name);
 2643             return false;
 2644         }
 2645     }
 2646 
 2647     if (!create_container_dir(c))
 2648         return false;
 2649 
 2650     /* If we're writing to the container's config file, take the disk lock.
 2651      * Otherwise just take the memlock to protect the struct lxc_container
 2652      * while we're traversing it.
 2653      */
 2654     if (strequal(c->configfile, alt_file))
 2655         need_disklock = true;
 2656 
 2657     if (need_disklock)
 2658         lret = container_disk_lock(c);
 2659     else
 2660         lret = container_mem_lock(c);
 2661     if (lret)
 2662         return false;
 2663 
 2664     fd = open(alt_file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
 2665           S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
 2666     if (fd < 0)
 2667         goto on_error;
 2668 
 2669     lret = write_config(fd, c->lxc_conf);
 2670     close(fd);
 2671     if (lret < 0)
 2672         goto on_error;
 2673 
 2674     ret = true;
 2675 
 2676 on_error:
 2677     if (need_disklock)
 2678         container_disk_unlock(c);
 2679     else
 2680         container_mem_unlock(c);
 2681 
 2682     return ret;
 2683 }
 2684 
 2685 WRAP_API_1(bool, lxcapi_save_config, const char *)
 2686 
 2687 
 2688 static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc)
 2689 {
 2690     FILE *f1;
 2691     struct stat fbuf;
 2692     void *buf = NULL;
 2693     char *del = NULL;
 2694     char path[PATH_MAX];
 2695     char newpath[PATH_MAX];
 2696     int fd, ret, n = 0, v = 0;
 2697     bool bret = false;
 2698     size_t len = 0, bytes = 0;
 2699 
 2700     if (container_disk_lock(c0))
 2701         return false;
 2702 
 2703     ret = strnprintf(path, sizeof(path), "%s/%s/lxc_snapshots", c0->config_path, c0->name);
 2704     if (ret < 0)
 2705         goto out;
 2706 
 2707     ret = strnprintf(newpath, sizeof(newpath), "%s\n%s\n", c->config_path, c->name);
 2708     if (ret < 0)
 2709         goto out;
 2710 
 2711     /* If we find an lxc-snapshot file using the old format only listing the
 2712      * number of snapshots we will keep using it. */
 2713     f1 = fopen(path, "re");
 2714     if (f1) {
 2715         n = fscanf(f1, "%d", &v);
 2716         fclose(f1);
 2717         if (n == 1 && v == 0) {
 2718             ret = remove(path);
 2719             if (ret < 0)
 2720                 SYSERROR("Failed to remove \"%s\"", path);
 2721 
 2722             n = 0;
 2723         }
 2724     }
 2725 
 2726     if (n == 1) {
 2727         v += inc ? 1 : -1;
 2728         f1 = fopen(path, "we");
 2729         if (!f1)
 2730             goto out;
 2731 
 2732         if (fprintf(f1, "%d\n", v) < 0) {
 2733             ERROR("Error writing new snapshots value");
 2734             fclose(f1);
 2735             goto out;
 2736         }
 2737 
 2738         ret = fclose(f1);
 2739         if (ret != 0) {
 2740             SYSERROR("Error writing to or closing snapshots file");
 2741             goto out;
 2742         }
 2743     } else {
 2744         /* Here we know that we have or can use an lxc-snapshot file
 2745          * using the new format. */
 2746         if (inc) {
 2747             f1 = fopen(path, "ae");
 2748             if (!f1)
 2749                 goto out;
 2750 
 2751             if (fprintf(f1, "%s", newpath) < 0) {
 2752                 ERROR("Error writing new snapshots entry");
 2753                 ret = fclose(f1);
 2754                 if (ret != 0)
 2755                     SYSERROR("Error writing to or closing snapshots file");
 2756                 goto out;
 2757             }
 2758 
 2759             ret = fclose(f1);
 2760             if (ret != 0) {
 2761                 SYSERROR("Error writing to or closing snapshots file");
 2762                 goto out;
 2763             }
 2764         } else if (!inc) {
 2765             if ((fd = open(path, O_RDWR | O_CLOEXEC)) < 0)
 2766                 goto out;
 2767 
 2768             if (fstat(fd, &fbuf) < 0) {
 2769                 close(fd);
 2770                 goto out;
 2771             }
 2772 
 2773             if (fbuf.st_size != 0) {
 2774                 buf = lxc_strmmap(NULL, fbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 2775                 if (buf == MAP_FAILED) {
 2776                     SYSERROR("Failed to create mapping %s", path);
 2777                     close(fd);
 2778                     goto out;
 2779                 }
 2780 
 2781                 len = strlen(newpath);
 2782                 while ((del = strstr((char *)buf, newpath))) {
 2783                     memmove(del, del + len, strlen(del) - len + 1);
 2784                     bytes += len;
 2785                 }
 2786 
 2787                 lxc_strmunmap(buf, fbuf.st_size);
 2788                 if (ftruncate(fd, fbuf.st_size - bytes) < 0) {
 2789                     SYSERROR("Failed to truncate file %s", path);
 2790                     close(fd);
 2791                     goto out;
 2792                 }
 2793             }
 2794 
 2795             close(fd);
 2796         }
 2797 
 2798         /* If the lxc-snapshot file is empty, remove it. */
 2799         if (stat(path, &fbuf) < 0)
 2800             goto out;
 2801 
 2802         if (!fbuf.st_size) {
 2803             ret = remove(path);
 2804             if (ret < 0)
 2805                 SYSERROR("Failed to remove \"%s\"", path);
 2806         }
 2807     }
 2808 
 2809     bret = true;
 2810 
 2811 out:
 2812     container_disk_unlock(c0);
 2813     return bret;
 2814 }
 2815 
 2816 void mod_all_rdeps(struct lxc_container *c, bool inc)
 2817 {
 2818     __do_free char *lxcpath = NULL, *lxcname = NULL;
 2819     __do_fclose FILE *f = NULL;
 2820     size_t pathlen = 0, namelen = 0;
 2821     struct lxc_container *p;
 2822     char path[PATH_MAX];
 2823     int ret;
 2824 
 2825     ret = strnprintf(path, sizeof(path), "%s/%s/lxc_rdepends",
 2826         c->config_path, c->name);
 2827     if (ret < 0) {
 2828         ERROR("Path name too long");
 2829         return;
 2830     }
 2831 
 2832     f = fopen(path, "re");
 2833     if (!f)
 2834         return;
 2835 
 2836     while (getline(&lxcpath, &pathlen, f) != -1) {
 2837         if (getline(&lxcname, &namelen, f) == -1) {
 2838             ERROR("badly formatted file %s", path);
 2839             return;
 2840         }
 2841 
 2842         remove_trailing_newlines(lxcpath);
 2843         remove_trailing_newlines(lxcname);
 2844 
 2845         if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) {
 2846             ERROR("Unable to find dependent container %s:%s",
 2847                 lxcpath, lxcname);
 2848             continue;
 2849         }
 2850 
 2851         if (!mod_rdep(p, c, inc))
 2852             ERROR("Failed to update snapshots file for %s:%s",
 2853                 lxcpath, lxcname);
 2854 
 2855         lxc_container_put(p);
 2856     }
 2857 }
 2858 
 2859 static bool has_fs_snapshots(struct lxc_container *c)
 2860 {
 2861     __do_fclose FILE *f = NULL;
 2862     char path[PATH_MAX];
 2863     int ret, v;
 2864     struct stat fbuf;
 2865 
 2866     ret = strnprintf(path, sizeof(path), "%s/%s/lxc_snapshots", c->config_path,
 2867             c->name);
 2868     if (ret < 0)
 2869         return false;
 2870 
 2871     /* If the file doesn't exist there are no snapshots. */
 2872     if (stat(path, &fbuf) < 0)
 2873         return false;
 2874 
 2875     v = fbuf.st_size;
 2876     if (v != 0) {
 2877         f = fopen(path, "re");
 2878         if (!f)
 2879             return false;
 2880 
 2881         ret = fscanf(f, "%d", &v);
 2882         if (ret != 1)
 2883             INFO("Container uses new lxc-snapshots format %s", path);
 2884     }
 2885 
 2886     return v != 0;
 2887 }
 2888 
 2889 static bool has_snapshots(struct lxc_container *c)
 2890 {
 2891     __do_closedir DIR *dir = NULL;
 2892     char path[PATH_MAX];
 2893     struct dirent *direntp;
 2894     int count = 0;
 2895 
 2896     if (!get_snappath_dir(c, path))
 2897         return false;
 2898 
 2899     dir = opendir(path);
 2900     if (!dir)
 2901         return false;
 2902 
 2903     while ((direntp = readdir(dir))) {
 2904         if (strequal(direntp->d_name, "."))
 2905             continue;
 2906 
 2907         if (strequal(direntp->d_name, ".."))
 2908             continue;
 2909         count++;
 2910         break;
 2911     }
 2912 
 2913     return count > 0;
 2914 }
 2915 
 2916 static bool do_destroy_container(struct lxc_conf *conf) {
 2917     int ret;
 2918 
 2919     if (am_guest_unpriv()) {
 2920         ret = userns_exec_full(conf, storage_destroy_wrapper, conf,
 2921                        "storage_destroy_wrapper");
 2922         if (ret < 0)
 2923             return false;
 2924 
 2925         return true;
 2926     }
 2927 
 2928     return storage_destroy(conf);
 2929 }
 2930 
 2931 static int lxc_rmdir_onedev_wrapper(void *data)
 2932 {
 2933     char *arg = (char *) data;
 2934     return lxc_rmdir_onedev(arg, "snaps");
 2935 }
 2936 
 2937 static int lxc_unlink_exec_wrapper(void *data)
 2938 {
 2939     char *arg = data;
 2940     return unlink(arg);
 2941 }
 2942 
 2943 static bool container_destroy(struct lxc_container *c,
 2944                   struct lxc_storage *storage)
 2945 {
 2946     const char *p1;
 2947     size_t len;
 2948     struct lxc_conf *conf;
 2949     char *path = NULL;
 2950     bool bret = false;
 2951     int ret = 0;
 2952 
 2953     if (!c || !do_lxcapi_is_defined(c))
 2954         return false;
 2955 
 2956     conf = c->lxc_conf;
 2957     if (container_disk_lock(c))
 2958         return false;
 2959 
 2960     if (!is_stopped(c)) {
 2961         /* We should queue some sort of error - in c->error_string? */
 2962         ERROR("container %s is not stopped", c->name);
 2963         goto out;
 2964     }
 2965 
 2966     if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) {
 2967         /* Start of environment variable setup for hooks */
 2968         if (setenv("LXC_NAME", c->name, 1))
 2969             SYSERROR("Failed to set environment variable for container name");
 2970 
 2971         if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1))
 2972             SYSERROR("Failed to set environment variable for config path");
 2973 
 2974         if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1))
 2975             SYSERROR("Failed to set environment variable for rootfs mount");
 2976 
 2977         if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1))
 2978             SYSERROR("Failed to set environment variable for rootfs mount");
 2979 
 2980         if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1))
 2981             SYSERROR("Failed to set environment variable for console path");
 2982 
 2983         if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1))
 2984             SYSERROR("Failed to set environment variable for console log");
 2985         /* End of environment variable setup for hooks */
 2986 
 2987         if (run_lxc_hooks(c->name, "destroy", conf, NULL)) {
 2988             ERROR("Failed to execute clone hook for \"%s\"", c->name);
 2989             goto out;
 2990         }
 2991     }
 2992 
 2993     if (current_config && conf == current_config) {
 2994         current_config = NULL;
 2995 
 2996         if (conf->logfd != -1) {
 2997             close(conf->logfd);
 2998             conf->logfd = -1;
 2999         }
 3000     }
 3001 
 3002     /* LXC is not managing the storage of the container. */
 3003     if (conf && !conf->rootfs.managed)
 3004         goto on_success;
 3005 
 3006     if (conf && conf->rootfs.path && conf->rootfs.mount) {
 3007         if (!do_destroy_container(conf)) {
 3008             ERROR("Error destroying rootfs for %s", c->name);
 3009             goto out;
 3010         }
 3011         INFO("Destroyed rootfs for %s", c->name);
 3012     }
 3013 
 3014     mod_all_rdeps(c, false);
 3015 
 3016     p1 = do_lxcapi_get_config_path(c);
 3017     /* strlen(p1)
 3018      * +
 3019      * /
 3020      * +
 3021      * strlen(c->name)
 3022      * +
 3023      * /
 3024      * +
 3025      * strlen("config") = 6
 3026      * +
 3027      * \0
 3028      */
 3029     len = strlen(p1) + 1 + strlen(c->name) + 1 + strlen(LXC_CONFIG_FNAME) + 1;
 3030     path = malloc(len);
 3031     if (!path) {
 3032         ERROR("Failed to allocate memory");
 3033         goto out;
 3034     }
 3035 
 3036     /* For an overlay container the rootfs is considered immutable and
 3037      * cannot be removed when restoring from a snapshot.
 3038      */
 3039     if (storage && (strequal(storage->type, "overlay") ||
 3040             strequal(storage->type, "overlayfs")) &&
 3041         (storage->flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)) {
 3042         ret = strnprintf(path, len, "%s/%s/%s", p1, c->name, LXC_CONFIG_FNAME);
 3043         if (ret < 0)
 3044             goto out;
 3045 
 3046         if (am_guest_unpriv())
 3047             ret = userns_exec_1(conf, lxc_unlink_exec_wrapper, path,
 3048                         "lxc_unlink_exec_wrapper");
 3049         else
 3050             ret = unlink(path);
 3051         if (ret < 0) {
 3052             SYSERROR("Failed to destroy config file \"%s\" for \"%s\"",
 3053                      path, c->name);
 3054             goto out;
 3055         }
 3056         INFO("Destroyed config file \"%s\" for \"%s\"", path, c->name);
 3057 
 3058         bret = true;
 3059         goto out;
 3060     }
 3061 
 3062     ret = strnprintf(path, len, "%s/%s", p1, c->name);
 3063     if (ret < 0)
 3064         goto out;
 3065 
 3066     if (am_guest_unpriv())
 3067         ret = userns_exec_full(conf, lxc_rmdir_onedev_wrapper, path,
 3068                        "lxc_rmdir_onedev_wrapper");
 3069     else
 3070         ret = lxc_rmdir_onedev(path, "snaps");
 3071     if (ret < 0) {
 3072         ERROR("Failed to destroy directory \"%s\" for \"%s\"", path,
 3073               c->name);
 3074         goto out;
 3075     }
 3076     INFO("Destroyed directory \"%s\" for \"%s\"", path, c->name);
 3077 
 3078 on_success:
 3079     bret = true;
 3080 
 3081 out:
 3082     if (path)
 3083         free(path);
 3084 
 3085     container_disk_unlock(c);
 3086     return bret;
 3087 }
 3088 
 3089 static bool do_lxcapi_destroy(struct lxc_container *c)
 3090 {
 3091     if (!c || !lxcapi_is_defined(c))
 3092         return false;
 3093 
 3094     if (c->lxc_conf && c->lxc_conf->rootfs.managed) {
 3095         if (has_snapshots(c)) {
 3096             ERROR("Container %s has snapshots;  not removing", c->name);
 3097             return false;
 3098         }
 3099 
 3100         if (has_fs_snapshots(c)) {
 3101             ERROR("container %s has snapshots on its rootfs", c->name);
 3102             return false;
 3103         }
 3104     }
 3105 
 3106     return container_destroy(c, NULL);
 3107 }
 3108 
 3109 WRAP_API(bool, lxcapi_destroy)
 3110 
 3111 static bool do_lxcapi_destroy_with_snapshots(struct lxc_container *c)
 3112 {
 3113     if (!c || !lxcapi_is_defined(c))
 3114         return false;
 3115 
 3116     if (!lxcapi_snapshot_destroy_all(c)) {
 3117         ERROR("Error deleting all snapshots");
 3118         return false;
 3119     }
 3120 
 3121     return lxcapi_destroy(c);
 3122 }
 3123 
 3124 WRAP_API(bool, lxcapi_destroy_with_snapshots)
 3125 
 3126 int lxc_set_config_item_locked(struct lxc_conf *conf, const char *key,
 3127                    const char *v)
 3128 {
 3129     int ret;
 3130     struct lxc_config_t *config;
 3131     bool bret = true;
 3132 
 3133     config = lxc_get_config(key);
 3134 
 3135     ret = config->set(key, v, conf, NULL);
 3136     if (ret < 0)
 3137         return -EINVAL;
 3138 
 3139     if (lxc_config_value_empty(v))
 3140         do_clear_unexp_config_line(conf, key);
 3141     else
 3142         bret = do_append_unexp_config_line(conf, key, v);
 3143     if (!bret)
 3144         return -ENOMEM;
 3145 
 3146     return 0;
 3147 }
 3148 
 3149 static bool do_set_config_item_locked(struct lxc_container *c, const char *key,
 3150                       const char *v)
 3151 {
 3152     int ret;
 3153 
 3154     if (!c->lxc_conf)
 3155         c->lxc_conf = lxc_conf_init();
 3156 
 3157     if (!c->lxc_conf)
 3158         return false;
 3159 
 3160     ret = lxc_set_config_item_locked(c->lxc_conf, key, v);
 3161     if (ret < 0)
 3162         return false;
 3163 
 3164     return true;
 3165 }
 3166 
 3167 static bool do_lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
 3168 {
 3169     bool b = false;
 3170 
 3171     if (!c)
 3172         return false;
 3173 
 3174     if (container_mem_lock(c))
 3175         return false;
 3176 
 3177     b = do_set_config_item_locked(c, key, v);
 3178 
 3179     container_mem_unlock(c);
 3180     return b;
 3181 }
 3182 
 3183 WRAP_API_2(bool, lxcapi_set_config_item, const char *, const char *)
 3184 
 3185 static char *lxcapi_config_file_name(struct lxc_container *c)
 3186 {
 3187     if (!c || !c->configfile)
 3188         return NULL;
 3189 
 3190     return strdup(c->configfile);
 3191 }
 3192 
 3193 static const char *lxcapi_get_config_path(struct lxc_container *c)
 3194 {
 3195     if (!c || !c->config_path)
 3196         return NULL;
 3197 
 3198     return (const char *)(c->config_path);
 3199 }
 3200 
 3201 /*
 3202  * not for export
 3203  * Just recalculate the c->configfile based on the
 3204  * c->config_path, which must be set.
 3205  * The lxc_container must be locked or not yet public.
 3206  */
 3207 static bool set_config_filename(struct lxc_container *c)
 3208 {
 3209     char *newpath;
 3210     int len, ret;
 3211 
 3212     if (!c->config_path)
 3213         return false;
 3214 
 3215     /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
 3216     len = strlen(c->config_path) + 1 + strlen(c->name) + 1 + strlen(LXC_CONFIG_FNAME) + 1;
 3217     newpath = malloc(len);
 3218     if (!newpath)
 3219         return false;
 3220 
 3221     ret = strnprintf(newpath, len, "%s/%s/%s", c->config_path, c->name, LXC_CONFIG_FNAME);
 3222     if (ret < 0) {
 3223         fprintf(stderr, "Error printing out config file name\n");
 3224         free(newpath);
 3225         return false;
 3226     }
 3227 
 3228     free(c->configfile);
 3229     c->configfile = newpath;
 3230 
 3231     return true;
 3232 }
 3233 
 3234 static bool do_lxcapi_set_config_path(struct lxc_container *c, const char *path)
 3235 {
 3236     char *p;
 3237     bool b = false;
 3238     char *oldpath = NULL;
 3239 
 3240     if (!c)
 3241         return b;
 3242 
 3243     if (container_mem_lock(c))
 3244         return b;
 3245 
 3246     p = strdup(path);
 3247     if (!p) {
 3248         ERROR("Out of memory setting new lxc path");
 3249         goto err;
 3250     }
 3251 
 3252     b = true;
 3253     if (c->config_path)
 3254         oldpath = c->config_path;
 3255     c->config_path = p;
 3256 
 3257     /* Since we've changed the config path, we have to change the
 3258      * config file name too */
 3259     if (!set_config_filename(c)) {
 3260         ERROR("Out of memory setting new config filename");
 3261         b = false;
 3262         free(c->config_path);
 3263         c->config_path = oldpath;
 3264         oldpath = NULL;
 3265     }
 3266 
 3267 err:
 3268     free(oldpath);
 3269     container_mem_unlock(c);
 3270     return b;
 3271 }
 3272 
 3273 WRAP_API_1(bool, lxcapi_set_config_path, const char *)
 3274 
 3275 static bool do_lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
 3276 {
 3277     call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
 3278     int ret;
 3279 
 3280     if (!c)
 3281         return false;
 3282 
 3283     if (is_stopped(c))
 3284         return false;
 3285 
 3286     ret = cgroup_set(c->name, c->config_path, subsys, value);
 3287     if (ret < 0 && ERRNO_IS_NOT_SUPPORTED(ret)) {
 3288         cgroup_ops = cgroup_init(c->lxc_conf);
 3289         if (!cgroup_ops)
 3290             return false;
 3291 
 3292         ret = cgroup_ops->set(cgroup_ops, subsys, value, c->name, c->config_path);
 3293     }
 3294 
 3295     return ret == 0;
 3296 }
 3297 
 3298 WRAP_API_2(bool, lxcapi_set_cgroup_item, const char *, const char *)
 3299 
 3300 static int do_lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
 3301 {
 3302     call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
 3303     int ret;
 3304 
 3305     if (!c)
 3306         return -1;
 3307 
 3308     if (is_stopped(c))
 3309         return -1;
 3310 
 3311     ret = cgroup_get(c->name, c->config_path, subsys, retv, inlen);
 3312     if (ret < 0 && ERRNO_IS_NOT_SUPPORTED(ret)) {
 3313         cgroup_ops = cgroup_init(c->lxc_conf);
 3314         if (!cgroup_ops)
 3315             return -1;
 3316 
 3317         return cgroup_ops->get(cgroup_ops, subsys, retv, inlen, c->name, c->config_path);
 3318     }
 3319 
 3320     return ret;
 3321 }
 3322 
 3323 WRAP_API_3(int, lxcapi_get_cgroup_item, const char *, char *, int)
 3324 
 3325 const char *lxc_get_global_config_item(const char *key)
 3326 {
 3327     return lxc_global_config_value(key);
 3328 }
 3329 
 3330 const char *lxc_get_version(void)
 3331 {
 3332     return LXC_VERSION;
 3333 }
 3334 
 3335 static int copy_file(const char *old, const char *new)
 3336 {
 3337     int in, out;
 3338     ssize_t len, ret;
 3339     char buf[8096];
 3340     struct stat sbuf;
 3341 
 3342     if (file_exists(new)) {
 3343         ERROR("copy destination %s exists", new);
 3344         return -1;
 3345     }
 3346 
 3347     ret = stat(old, &sbuf);
 3348     if (ret < 0) {
 3349         INFO("Error stat'ing %s", old);
 3350         return -1;
 3351     }
 3352 
 3353     in = open(old, O_RDONLY);
 3354     if (in < 0) {
 3355         SYSERROR("Error opening original file %s", old);
 3356         return -1;
 3357     }
 3358 
 3359     out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
 3360     if (out < 0) {
 3361         SYSERROR("Error opening new file %s", new);
 3362         close(in);
 3363         return -1;
 3364     }
 3365 
 3366     for (;;) {
 3367         len = lxc_read_nointr(in, buf, 8096);
 3368         if (len < 0) {
 3369             SYSERROR("Error reading old file %s", old);
 3370             goto err;
 3371         }
 3372 
 3373         if (len == 0)
 3374             break;
 3375 
 3376         ret = lxc_write_nointr(out, buf, len);
 3377         if (ret < len) { /* should we retry? */
 3378             SYSERROR("Error: write to new file %s was interrupted", new);
 3379             goto err;
 3380         }
 3381     }
 3382 
 3383     close(in);
 3384     close(out);
 3385 
 3386     /* We set mode, but not owner/group. */
 3387     ret = chmod(new, sbuf.st_mode);
 3388     if (ret) {
 3389         SYSERROR("Error setting mode on %s", new);
 3390         return -1;
 3391     }
 3392 
 3393     return 0;
 3394 
 3395 err:
 3396     close(in);
 3397     close(out);
 3398     return -1;
 3399 }
 3400 
 3401 static int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
 3402 {
 3403     __do_free char *cpath = NULL;
 3404     int i, len, ret;
 3405     struct lxc_list *it;
 3406 
 3407     len = strlen(oldc->config_path) + strlen(oldc->name) + 3;
 3408     cpath = must_realloc(NULL, len);
 3409     ret = strnprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name);
 3410     if (ret < 0)
 3411         return -1;
 3412 
 3413     for (i=0; i<NUM_LXC_HOOKS; i++) {
 3414         lxc_list_for_each(it, &c->lxc_conf->hooks[i]) {
 3415             char *hookname = it->elem;
 3416             char *fname = strrchr(hookname, '/');
 3417             char tmppath[PATH_MAX];
 3418             if (!fname) /* relative path - we don't support, but maybe we should */
 3419                 return 0;
 3420 
 3421             if (!strnequal(hookname, cpath, len - 1)) {
 3422                 /* this hook is public - ignore */
 3423                 continue;
 3424             }
 3425 
 3426             /* copy the script, and change the entry in confile */
 3427             ret = strnprintf(tmppath, sizeof(tmppath), "%s/%s/%s",
 3428                     c->config_path, c->name, fname+1);
 3429             if (ret < 0)
 3430                 return -1;
 3431 
 3432             ret = copy_file(it->elem, tmppath);
 3433             if (ret < 0)
 3434                 return -1;
 3435 
 3436             free(it->elem);
 3437 
 3438             it->elem = strdup(tmppath);
 3439             if (!it->elem) {
 3440                 ERROR("out of memory copying hook path");
 3441                 return -1;
 3442             }
 3443         }
 3444     }
 3445 
 3446     if (!clone_update_unexp_hooks(c->lxc_conf, oldc->config_path,
 3447             c->config_path, oldc->name, c->name)) {
 3448         ERROR("Error saving new hooks in clone");
 3449         return -1;
 3450     }
 3451 
 3452     do_lxcapi_save_config(c, NULL);
 3453     return 0;
 3454 }
 3455 
 3456 
 3457 static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
 3458 {
 3459     char newpath[PATH_MAX];
 3460     char *oldpath = oldc->lxc_conf->fstab;
 3461     int ret;
 3462 
 3463     if (!oldpath)
 3464         return 0;
 3465 
 3466     clear_unexp_config_line(c->lxc_conf, "lxc.mount.fstab", false);
 3467 
 3468     char *p = strrchr(oldpath, '/');
 3469     if (!p)
 3470         return -1;
 3471 
 3472     ret = strnprintf(newpath, sizeof(newpath), "%s/%s%s",
 3473             c->config_path, c->name, p);
 3474     if (ret < 0) {
 3475         ERROR("error printing new path for %s", oldpath);
 3476         return -1;
 3477     }
 3478 
 3479     if (file_exists(newpath)) {
 3480         ERROR("error: fstab file %s exists", newpath);
 3481         return -1;
 3482     }
 3483 
 3484     if (copy_file(oldpath, newpath) < 0) {
 3485         ERROR("error: copying %s to %s", oldpath, newpath);
 3486         return -1;
 3487     }
 3488 
 3489     free(c->lxc_conf->fstab);
 3490 
 3491     c->lxc_conf->fstab = strdup(newpath);
 3492     if (!c->lxc_conf->fstab) {
 3493         ERROR("error: allocating pathname");
 3494         return -1;
 3495     }
 3496 
 3497     if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount.fstab", newpath)) {
 3498         ERROR("error saving new lxctab");
 3499         return -1;
 3500     }
 3501 
 3502     return 0;
 3503 }
 3504 
 3505 static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0)
 3506 {
 3507     char path0[PATH_MAX], path1[PATH_MAX];
 3508     int ret;
 3509 
 3510     ret = strnprintf(path0, sizeof(path0), "%s/%s/lxc_rdepends", c0->config_path,
 3511         c0->name);
 3512     if (ret < 0) {
 3513         WARN("Error copying reverse dependencies");
 3514         return;
 3515     }
 3516 
 3517     ret = strnprintf(path1, sizeof(path1), "%s/%s/lxc_rdepends", c->config_path,
 3518         c->name);
 3519     if (ret < 0) {
 3520         WARN("Error copying reverse dependencies");
 3521         return;
 3522     }
 3523 
 3524     if (copy_file(path0, path1) < 0) {
 3525         INFO("Error copying reverse dependencies");
 3526         return;
 3527     }
 3528 }
 3529 
 3530 static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0)
 3531 {
 3532     __do_fclose FILE *f = NULL;
 3533     int ret;
 3534     char path[PATH_MAX];
 3535 
 3536     ret = strnprintf(path, sizeof(path), "%s/%s/lxc_rdepends", c->config_path, c->name);
 3537     if (ret < 0)
 3538         return false;
 3539 
 3540     f = fopen(path, "ae");
 3541     if (!f)
 3542         return false;
 3543 
 3544     /* If anything goes wrong, just return an error. */
 3545     return fprintf(f, "%s\n%s\n", c0->config_path, c0->name) > 0;
 3546 }
 3547 
 3548 /*
 3549  * If the fs natively supports snapshot clones with no penalty,
 3550  * then default to those even if not requested.
 3551  * Currently we only do this for btrfs.
 3552  */
 3553 static bool should_default_to_snapshot(struct lxc_container *c0,
 3554                 struct lxc_container *c1)
 3555 {
 3556     __do_free char *p0 = NULL, *p1 = NULL;
 3557     int ret;
 3558     size_t l0 = strlen(c0->config_path) + strlen(c0->name) + 2;
 3559     size_t l1 = strlen(c1->config_path) + strlen(c1->name) + 2;
 3560     char *rootfs = c0->lxc_conf->rootfs.path;
 3561 
 3562     p0 = must_realloc(NULL, l0 + 1);
 3563     p1 = must_realloc(NULL, l1 + 1);
 3564     ret = strnprintf(p0, l0, "%s/%s", c0->config_path, c0->name);
 3565     if (ret < 0)
 3566         return false;
 3567 
 3568     ret = strnprintf(p1, l1, "%s/%s", c1->config_path, c1->name);
 3569     if (ret < 0)
 3570         return false;
 3571 
 3572     if (!is_btrfs_fs(p0) || !is_btrfs_fs(p1))
 3573         return false;
 3574 
 3575     if (is_btrfs_subvol(rootfs) <= 0)
 3576         return false;
 3577 
 3578     return btrfs_same_fs(p0, p1) == 0;
 3579 }
 3580 
 3581 static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
 3582             const char *newtype, int flags, const char *bdevdata,
 3583             uint64_t newsize)
 3584 {
 3585     struct lxc_storage *bdev;
 3586     bool need_rdep;
 3587 
 3588     if (should_default_to_snapshot(c0, c))
 3589         flags |= LXC_CLONE_SNAPSHOT;
 3590 
 3591     bdev = storage_copy(c0, c->name, c->config_path, newtype, flags,
 3592                 bdevdata, newsize, &need_rdep);
 3593     if (!bdev) {
 3594         ERROR("Error copying storage.");
 3595         return -1;
 3596     }
 3597 
 3598     /* Set new rootfs. */
 3599     free(c->lxc_conf->rootfs.path);
 3600     c->lxc_conf->rootfs.path = strdup(bdev->src);
 3601     storage_put(bdev);
 3602 
 3603     if (!c->lxc_conf->rootfs.path) {
 3604         ERROR("Out of memory while setting storage path.");
 3605         return -1;
 3606     }
 3607 
 3608     /* Append a new lxc.rootfs.path entry to the unexpanded config. */
 3609     clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", false);
 3610     if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs.path",
 3611                      c->lxc_conf->rootfs.path)) {
 3612         ERROR("Error saving new rootfs to cloned config.");
 3613         return -1;
 3614     }
 3615 
 3616     if (flags & LXC_CLONE_SNAPSHOT)
 3617         copy_rdepends(c, c0);
 3618 
 3619     if (need_rdep) {
 3620         if (!add_rdepends(c, c0))
 3621             WARN("Error adding reverse dependency from %s to %s",
 3622                  c->name, c0->name);
 3623     }
 3624 
 3625     mod_all_rdeps(c, true);
 3626 
 3627     return 0;
 3628 }
 3629 
 3630 struct clone_update_data {
 3631     struct lxc_container *c0;
 3632     struct lxc_container *c1;
 3633     int flags;
 3634     char **hookargs;
 3635 };
 3636 
 3637 static int clone_update_rootfs(struct clone_update_data *data)
 3638 {
 3639     struct lxc_container *c0 = data->c0;
 3640     struct lxc_container *c = data->c1;
 3641     int flags = data->flags;
 3642     char **hookargs = data->hookargs;
 3643     int ret = -1;
 3644     char path[PATH_MAX];
 3645     struct lxc_storage *bdev;
 3646     FILE *fout;
 3647     struct lxc_conf *conf = c->lxc_conf;
 3648 
 3649     /* update hostname in rootfs */
 3650     /* we're going to mount, so run in a clean namespace to simplify cleanup */
 3651 
 3652     (void)lxc_drop_groups();
 3653 
 3654     if (setgid(0) < 0) {
 3655         ERROR("Failed to setgid to 0");
 3656         return -1;
 3657     }
 3658 
 3659     if (setuid(0) < 0) {
 3660         ERROR("Failed to setuid to 0");
 3661         return -1;
 3662     }
 3663 
 3664     if (unshare(CLONE_NEWNS) < 0)
 3665         return -1;
 3666 
 3667     ret = lxc_storage_prepare(conf);
 3668     if (ret)
 3669         return -1;
 3670     bdev = conf->rootfs.storage;
 3671 
 3672     if (!strequal(bdev->type, "dir")) {
 3673         if (unshare(CLONE_NEWNS) < 0) {
 3674             ERROR("error unsharing mounts");
 3675             lxc_storage_put(conf);
 3676             return -1;
 3677         }
 3678 
 3679         if (detect_shared_rootfs() && mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL))
 3680             SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
 3681 
 3682         if (bdev->ops->mount(bdev) < 0) {
 3683             lxc_storage_put(conf);
 3684             return -1;
 3685         }
 3686     } else { /* TODO come up with a better way */
 3687         free(bdev->dest);
 3688         bdev->dest = strdup(lxc_storage_get_path(bdev->src, bdev->type));
 3689     }
 3690 
 3691     if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
 3692         /* Start of environment variable setup for hooks */
 3693         if (c0->name && setenv("LXC_SRC_NAME", c0->name, 1))
 3694             SYSERROR("failed to set environment variable for source container name");
 3695 
 3696         if (setenv("LXC_NAME", c->name, 1))
 3697             SYSERROR("failed to set environment variable for container name");
 3698 
 3699         if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1))
 3700             SYSERROR("failed to set environment variable for config path");
 3701 
 3702         if (bdev->dest && setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1))
 3703             SYSERROR("failed to set environment variable for rootfs mount");
 3704 
 3705         if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1))
 3706             SYSERROR("failed to set environment variable for rootfs mount");
 3707 
 3708         if (run_lxc_hooks(c->name, "clone", conf, hookargs)) {
 3709             ERROR("Error executing clone hook for %s", c->name);
 3710             lxc_storage_put(conf);
 3711             return -1;
 3712         }
 3713     }
 3714 
 3715     if (!(flags & LXC_CLONE_KEEPNAME)) {
 3716         ret = strnprintf(path, sizeof(path), "%s/etc/hostname", bdev->dest);
 3717         lxc_storage_put(conf);
 3718 
 3719         if (ret < 0)
 3720             return -1;
 3721 
 3722         if (!file_exists(path))
 3723             return 0;
 3724 
 3725         if (!(fout = fopen(path, "we"))) {
 3726             SYSERROR("unable to open %s: ignoring", path);
 3727             return 0;
 3728         }
 3729 
 3730         if (fprintf(fout, "%s", c->name) < 0) {
 3731             fclose(fout);
 3732             return -1;
 3733         }
 3734 
 3735         if (fclose(fout) < 0)
 3736             return -1;
 3737     } else {
 3738         lxc_storage_put(conf);
 3739     }
 3740 
 3741     return 0;
 3742 }
 3743 
 3744 static int clone_update_rootfs_wrapper(void *data)
 3745 {
 3746     struct clone_update_data *arg = (struct clone_update_data *) data;
 3747     return clone_update_rootfs(arg);
 3748 }
 3749 
 3750 /*
 3751  * We want to support:
 3752 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
 3753         -p|--lvprefix lvprefix -t|--fstype fstype  -B backingstore
 3754 
 3755 -s [ implies overlay]
 3756 -s -B overlay
 3757 
 3758 only rootfs gets converted (copied/snapshotted) on clone.
 3759 */
 3760 
 3761 static int create_file_dirname(char *path, struct lxc_conf *conf)
 3762 {
 3763     char *p = strrchr(path, '/');
 3764     int ret = -1;
 3765 
 3766     if (!p)
 3767         return -1;
 3768 
 3769     *p = '\0';
 3770     ret = do_create_container_dir(path, conf);
 3771     *p = '/';
 3772 
 3773     return ret;
 3774 }
 3775 
 3776 static struct lxc_container *do_lxcapi_clone(struct lxc_container *c, const char *newname,
 3777         const char *lxcpath, int flags,
 3778         const char *bdevtype, const char *bdevdata, uint64_t newsize,
 3779         char **hookargs)
 3780 {
 3781     char newpath[PATH_MAX];
 3782     int fd, ret;
 3783     struct clone_update_data data;
 3784     size_t saved_unexp_len;
 3785     pid_t pid;
 3786     int storage_copied = 0;
 3787     char *origroot = NULL, *saved_unexp_conf = NULL;
 3788     struct lxc_container *c2 = NULL;
 3789 
 3790     if (!c || !do_lxcapi_is_defined(c))
 3791         return NULL;
 3792 
 3793     if (container_mem_lock(c))
 3794         return NULL;
 3795     if (!is_stopped(c) && !(flags & LXC_CLONE_ALLOW_RUNNING)) {
 3796         ERROR("error: Original container (%s) is running. Use --allowrunning if you want to force a snapshot of the running container.", c->name);
 3797         goto out;
 3798     }
 3799 
 3800     /* Make sure the container doesn't yet exist. */
 3801     if (!newname)
 3802         newname = c->name;
 3803 
 3804     if (!lxcpath)
 3805         lxcpath = do_lxcapi_get_config_path(c);
 3806 
 3807     ret = strnprintf(newpath, sizeof(newpath), "%s/%s/%s", lxcpath, newname, LXC_CONFIG_FNAME);
 3808     if (ret < 0) {
 3809         SYSERROR("clone: failed making config pathname");
 3810         goto out;
 3811     }
 3812 
 3813     if (file_exists(newpath)) {
 3814         ERROR("error: clone: %s exists", newpath);
 3815         goto out;
 3816     }
 3817 
 3818     ret = create_file_dirname(newpath, c->lxc_conf);
 3819     if (ret < 0 && errno != EEXIST) {
 3820         ERROR("Error creating container dir for %s", newpath);
 3821         goto out;
 3822     }
 3823 
 3824     /* Copy the configuration. Tweak it as needed. */
 3825     if (c->lxc_conf->rootfs.path) {
 3826         origroot = c->lxc_conf->rootfs.path;
 3827         c->lxc_conf->rootfs.path = NULL;
 3828     }
 3829 
 3830     fd = open(newpath, O_WRONLY | O_CREAT | O_CLOEXEC,
 3831           S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
 3832     if (fd < 0) {
 3833         SYSERROR("Failed to open \"%s\"", newpath);
 3834         goto out;
 3835     }
 3836 
 3837     saved_unexp_conf = c->lxc_conf->unexpanded_config;
 3838     saved_unexp_len = c->lxc_conf->unexpanded_len;
 3839     c->lxc_conf->unexpanded_config = strdup(saved_unexp_conf);
 3840     if (!c->lxc_conf->unexpanded_config) {
 3841         close(fd);
 3842         goto out;
 3843     }
 3844 
 3845     clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.path", false);
 3846     write_config(fd, c->lxc_conf);
 3847     close(fd);
 3848 
 3849     c->lxc_conf->rootfs.path = origroot;
 3850 
 3851     free(c->lxc_conf->unexpanded_config);
 3852     c->lxc_conf->unexpanded_config = saved_unexp_conf;
 3853     saved_unexp_conf = NULL;
 3854     c->lxc_conf->unexpanded_len = saved_unexp_len;
 3855 
 3856     ret = strnprintf(newpath, sizeof(newpath), "%s/%s/%s", lxcpath, newname, LXC_ROOTFS_DNAME);
 3857     if (ret < 0) {
 3858         SYSERROR("clone: failed making rootfs pathname");
 3859         goto out;
 3860     }
 3861 
 3862     ret = mkdir(newpath, 0755);
 3863     if (ret < 0) {
 3864         /* For an overlay container the rootfs is considered immutable
 3865          * and will not have been removed when restoring from a
 3866          * snapshot.
 3867          */
 3868         if (errno != ENOENT &&
 3869             !(flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)) {
 3870             SYSERROR("Failed to create directory \"%s\"", newpath);
 3871             goto out;
 3872         }
 3873     }
 3874 
 3875     if (am_guest_unpriv()) {
 3876         if (chown_mapped_root(newpath, c->lxc_conf) < 0) {
 3877             ERROR("Error chowning %s to container root", newpath);
 3878             goto out;
 3879         }
 3880     }
 3881 
 3882     c2 = lxc_container_new(newname, lxcpath);
 3883     if (!c2) {
 3884         ERROR("clone: failed to create new container (%s %s)", newname,
 3885                 lxcpath);
 3886         goto out;
 3887     }
 3888 
 3889     /* copy/snapshot rootfs's */
 3890     ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
 3891     if (ret < 0)
 3892         goto out;
 3893 
 3894     /* update utsname */
 3895     if (!(flags & LXC_CLONE_KEEPNAME)) {
 3896         clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false);
 3897         clear_unexp_config_line(c2->lxc_conf, "lxc.uts.name", false);
 3898 
 3899         if (!do_set_config_item_locked(c2, "lxc.uts.name", newname)) {
 3900             ERROR("Error setting new hostname");
 3901             goto out;
 3902         }
 3903     }
 3904 
 3905     /* copy hooks */
 3906     ret = copyhooks(c, c2);
 3907     if (ret < 0) {
 3908         ERROR("error copying hooks");
 3909         goto out;
 3910     }
 3911 
 3912     if (copy_fstab(c, c2) < 0) {
 3913         ERROR("error copying fstab");
 3914         goto out;
 3915     }
 3916 
 3917     /* update macaddrs */
 3918     if (!(flags & LXC_CLONE_KEEPMACADDR)) {
 3919         if (!network_new_hwaddrs(c2->lxc_conf)) {
 3920             ERROR("Error updating mac addresses");
 3921             goto out;
 3922         }
 3923     }
 3924 
 3925     /* Update absolute paths for overlay mount directories. */
 3926     if (ovl_update_abs_paths(c2->lxc_conf, c->config_path, c->name, lxcpath, newname) < 0)
 3927         goto out;
 3928 
 3929     /* We've now successfully created c2's storage, so clear it out if we
 3930      * fail after this.
 3931      */
 3932     storage_copied = 1;
 3933 
 3934     if (!c2->save_config(c2, NULL))
 3935         goto out;
 3936 
 3937     if ((pid = fork()) < 0) {
 3938         SYSERROR("fork");
 3939         goto out;
 3940     }
 3941 
 3942     if (pid > 0) {
 3943         ret = wait_for_pid(pid);
 3944         if (ret)
 3945             goto out;
 3946 
 3947         container_mem_unlock(c);
 3948         return c2;
 3949     }
 3950 
 3951     data.c0 = c;
 3952     data.c1 = c2;
 3953     data.flags = flags;
 3954     data.hookargs = hookargs;
 3955 
 3956     if (am_guest_unpriv())
 3957         ret = userns_exec_full(c->lxc_conf, clone_update_rootfs_wrapper,
 3958                        &data, "clone_update_rootfs_wrapper");
 3959     else
 3960         ret = clone_update_rootfs(&data);
 3961     if (ret < 0)
 3962         _exit(EXIT_FAILURE);
 3963 
 3964     container_mem_unlock(c);
 3965     _exit(EXIT_SUCCESS);
 3966 
 3967 out:
 3968     container_mem_unlock(c);
 3969     if (c2) {
 3970         if (!storage_copied)
 3971             c2->lxc_conf->rootfs.path = NULL;
 3972 
 3973         c2->destroy(c2);
 3974         lxc_container_put(c2);
 3975     }
 3976 
 3977     return NULL;
 3978 }
 3979 
 3980 static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
 3981         const char *lxcpath, int flags,
 3982         const char *bdevtype, const char *bdevdata, uint64_t newsize,
 3983         char **hookargs)
 3984 {
 3985     struct lxc_container * ret;
 3986 
 3987     current_config = c ? c->lxc_conf : NULL;
 3988     ret = do_lxcapi_clone(c, newname, lxcpath, flags, bdevtype, bdevdata, newsize, hookargs);
 3989     current_config = NULL;
 3990 
 3991     return ret;
 3992 }
 3993 
 3994 static bool do_lxcapi_rename(struct lxc_container *c, const char *newname)
 3995 {
 3996     struct lxc_storage *bdev;
 3997     struct lxc_container *newc;
 3998 
 3999     if (!c || !c->name || !c->config_path || !c->lxc_conf)
 4000         return false;
 4001 
 4002     if (has_fs_snapshots(c) || has_snapshots(c)) {
 4003         ERROR("Renaming a container with snapshots is not supported");
 4004         return false;
 4005     }
 4006 
 4007     if (lxc_storage_prepare(c->lxc_conf)) {
 4008         ERROR("Failed to find original backing store type");
 4009         return false;
 4010     }
 4011     bdev = c->lxc_conf->rootfs.storage;
 4012 
 4013     newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL);
 4014     lxc_storage_put(c->lxc_conf);
 4015     if (!newc) {
 4016         lxc_container_put(newc);
 4017         return false;
 4018     }
 4019 
 4020     if (newc && lxcapi_is_defined(newc))
 4021         lxc_container_put(newc);
 4022 
 4023     if (!container_destroy(c, NULL)) {
 4024         ERROR("Could not destroy existing container %s", c->name);
 4025         return false;
 4026     }
 4027 
 4028     return true;
 4029 }
 4030 
 4031 WRAP_API_1(bool, lxcapi_rename, const char *)
 4032 
 4033 static int lxcapi_attach(struct lxc_container *c,
 4034              lxc_attach_exec_t exec_function, void *exec_payload,
 4035              lxc_attach_options_t *options, pid_t *attached_process)
 4036 {
 4037     int ret;
 4038 
 4039     if (!c)
 4040         return -1;
 4041 
 4042     current_config = c->lxc_conf;
 4043 
 4044     ret = lxc_attach(c, exec_function, exec_payload, options,
 4045              attached_process);
 4046     current_config = NULL;
 4047     return ret;
 4048 }
 4049 
 4050 static int do_lxcapi_attach_run_wait(struct lxc_container *c,
 4051                      lxc_attach_options_t *options,
 4052                      const char *program,
 4053                      const char *const argv[])
 4054 {
 4055     lxc_attach_command_t command;
 4056     pid_t pid;
 4057     int ret;
 4058 
 4059     if (!c)
 4060         return -1;
 4061 
 4062     command.program = (char *)program;
 4063     command.argv = (char **)argv;
 4064 
 4065     ret = lxc_attach(c, lxc_attach_run_command, &command, options, &pid);
 4066     if (ret < 0)
 4067         return ret;
 4068 
 4069     return lxc_wait_for_pid_status(pid);
 4070 }
 4071 
 4072 static int lxcapi_attach_run_wait(struct lxc_container *c,
 4073                   lxc_attach_options_t *options,
 4074                   const char *program, const char *const argv[])
 4075 {
 4076     int ret;
 4077 
 4078     current_config = c ? c->lxc_conf : NULL;
 4079     ret = do_lxcapi_attach_run_wait(c, options, program, argv);
 4080     current_config = NULL;
 4081 
 4082     return ret;
 4083 }
 4084 
 4085 static int get_next_index(const char *lxcpath, char *cname)
 4086 {
 4087     __do_free char *fname = NULL;
 4088     struct stat sb;
 4089     int i = 0, ret;
 4090 
 4091     fname = must_realloc(NULL, strlen(lxcpath) + 20);
 4092 
 4093     for (;;) {
 4094         sprintf(fname, "%s/snap%d", lxcpath, i);
 4095 
 4096         ret = stat(fname, &sb);
 4097         if (ret != 0)
 4098             return i;
 4099 
 4100         i++;
 4101     }
 4102 }
 4103 
 4104 static bool get_snappath_dir(struct lxc_container *c, char *snappath)
 4105 {
 4106     int ret;
 4107 
 4108     /*
 4109      * If the old style snapshot path exists, use it
 4110      * /var/lib/lxc -> /var/lib/lxcsnaps
 4111      */
 4112     ret = strnprintf(snappath, PATH_MAX, "%ssnaps", c->config_path);
 4113     if (ret < 0)
 4114         return false;
 4115 
 4116     if (dir_exists(snappath)) {
 4117         ret = strnprintf(snappath, PATH_MAX, "%ssnaps/%s", c->config_path, c->name);
 4118         if (ret < 0)
 4119             return false;
 4120 
 4121         return true;
 4122     }
 4123 
 4124     /*
 4125      * Use the new style path
 4126      * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
 4127      */
 4128     ret = strnprintf(snappath, PATH_MAX, "%s/%s/snaps", c->config_path, c->name);
 4129     if (ret < 0)
 4130         return false;
 4131 
 4132     return true;
 4133 }
 4134 
 4135 static int do_lxcapi_snapshot(struct lxc_container *c, const char *commentfile)
 4136 {
 4137     __do_free char *dfnam = NULL;
 4138         int len;
 4139     int i, flags, ret;
 4140     time_t timer;
 4141     struct tm tm_info;
 4142     struct lxc_container *c2;
 4143     char snappath[PATH_MAX], newname[20];
 4144     char buffer[25];
 4145     FILE *f;
 4146 
 4147     if (!c || !lxcapi_is_defined(c))
 4148         return -1;
 4149 
 4150     if (!storage_can_backup(c->lxc_conf)) {
 4151         ERROR("%s's backing store cannot be backed up", c->name);
 4152         ERROR("Your container must use another backing store type");
 4153         return -1;
 4154     }
 4155 
 4156     if (!get_snappath_dir(c, snappath))
 4157         return -1;
 4158 
 4159     i = get_next_index(snappath, c->name);
 4160 
 4161     if (mkdir_p(snappath, 0755) < 0) {
 4162         ERROR("Failed to create snapshot directory %s", snappath);
 4163         return -1;
 4164     }
 4165 
 4166     ret = strnprintf(newname, 20, "snap%d", i);
 4167     if (ret < 0)
 4168         return -1;
 4169 
 4170     /*
 4171      * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
 4172      * created in the original container
 4173      */
 4174     flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME |
 4175         LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT;
 4176     if (storage_is_dir(c->lxc_conf)) {
 4177         ERROR("Snapshot of directory-backed container requested");
 4178         ERROR("Making a copy-clone.  If you do want snapshots, then");
 4179         ERROR("please create overlay clone first, snapshot that");
 4180         ERROR("and keep the original container pristine");
 4181         flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
 4182     }
 4183 
 4184     c2 = do_lxcapi_clone(c, newname, snappath, flags, NULL, NULL, 0, NULL);
 4185     if (!c2) {
 4186         ERROR("Failed to clone of %s:%s", c->config_path, c->name);
 4187         return -1;
 4188     }
 4189 
 4190     lxc_container_put(c2);
 4191 
 4192     /* Now write down the creation time. */
 4193     time(&timer);
 4194 
 4195     if (!localtime_r(&timer, &tm_info)) {
 4196         ERROR("Failed to get localtime");
 4197         return -1;
 4198     }
 4199 
 4200     strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", &tm_info);
 4201 
 4202     len = strlen(snappath) + 1 + strlen(newname) + 1 + strlen(LXC_TIMESTAMP_FNAME) + 1;
 4203     dfnam = must_realloc(NULL, len);
 4204     ret = strnprintf(dfnam, len, "%s/%s/%s", snappath, newname, LXC_TIMESTAMP_FNAME);
 4205     if (ret < 0)
 4206         return -1;
 4207     f = fopen(dfnam, "we");
 4208     if (!f) {
 4209         ERROR("Failed to open %s", dfnam);
 4210         return -1;
 4211     }
 4212 
 4213     if (fprintf(f, "%s", buffer) < 0) {
 4214         SYSERROR("Writing timestamp");
 4215         fclose(f);
 4216         return -1;
 4217     }
 4218 
 4219     ret = fclose(f);
 4220     if (ret != 0) {
 4221         SYSERROR("Writing timestamp");
 4222         return -1;
 4223     }
 4224 
 4225     if (commentfile) {
 4226         __do_free char *path = NULL;
 4227         /* $p / $name / comment \0 */
 4228         len = strlen(snappath) + 1 + strlen(newname) + 1 + strlen(LXC_COMMENT_FNAME) + 1;
 4229 
 4230         path = must_realloc(NULL, len);
 4231         ret = strnprintf(path, len, "%s/%s/%s", snappath, newname, LXC_COMMENT_FNAME);
 4232         if (ret < 0)
 4233             return -1;
 4234         return copy_file(commentfile, path) < 0 ? -1 : i;
 4235     }
 4236 
 4237     return i;
 4238 }
 4239 
 4240 WRAP_API_1(int, lxcapi_snapshot, const char *)
 4241 
 4242 static void lxcsnap_free(struct lxc_snapshot *s)
 4243 {
 4244     free(s->name);
 4245     free(s->comment_pathname);
 4246     free(s->timestamp);
 4247     free(s->lxcpath);
 4248 }
 4249 
 4250 static char *get_snapcomment_path(char *snappath, char *name)
 4251 {
 4252     __do_free char *s = NULL;
 4253     /* $snappath/$name/comment */
 4254     int ret, len = strlen(snappath) + strlen(name) + 10;
 4255 
 4256     s = malloc(len);
 4257     if (!s)
 4258         return NULL;
 4259 
 4260     ret = strnprintf(s, len, "%s/%s/comment", snappath, name);
 4261     if (ret < 0)
 4262         return NULL;
 4263 
 4264     return move_ptr(s);
 4265 }
 4266 
 4267 static char *get_timestamp(char* snappath, char *name)
 4268 {
 4269     __do_free char *s = NULL;
 4270     __do_fclose FILE *fin = NULL;
 4271     char path[PATH_MAX];
 4272     int ret, len;
 4273 
 4274     ret = strnprintf(path, sizeof(path), "%s/%s/ts", snappath, name);
 4275     if (ret < 0)
 4276         return NULL;
 4277 
 4278     fin = fopen(path, "re");
 4279     if (!fin)
 4280         return NULL;
 4281 
 4282     (void) fseek(fin, 0, SEEK_END);
 4283     len = ftell(fin);
 4284     (void) fseek(fin, 0, SEEK_SET);
 4285     if (len > 0) {
 4286         s = malloc(len+1);
 4287         if (s) {
 4288             s[len] = '\0';
 4289             if (fread(s, 1, len, fin) != len)
 4290                 return log_error_errno(NULL, errno, "reading timestamp");
 4291         }
 4292     }
 4293 
 4294     return move_ptr(s);
 4295 }
 4296 
 4297 static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps)
 4298 {
 4299     __do_closedir DIR *dir = NULL;
 4300     char snappath[PATH_MAX], path2[PATH_MAX];
 4301     int count = 0, ret;
 4302     struct dirent *direntp;
 4303     struct lxc_snapshot *snaps =NULL, *nsnaps;
 4304 
 4305     if (!c || !lxcapi_is_defined(c))
 4306         return -1;
 4307 
 4308     if (!get_snappath_dir(c, snappath)) {
 4309         ERROR("path name too long");
 4310         return -1;
 4311     }
 4312 
 4313     dir = opendir(snappath);
 4314     if (!dir) {
 4315         INFO("Failed to open %s - assuming no snapshots", snappath);
 4316         return 0;
 4317     }
 4318 
 4319     while ((direntp = readdir(dir))) {
 4320         if (strequal(direntp->d_name, "."))
 4321             continue;
 4322 
 4323         if (strequal(direntp->d_name, ".."))
 4324             continue;
 4325 
 4326         ret = strnprintf(path2, sizeof(path2), "%s/%s/%s", snappath, direntp->d_name, LXC_CONFIG_FNAME);
 4327         if (ret < 0) {
 4328             ERROR("pathname too long");
 4329             goto out_free;
 4330         }
 4331 
 4332         if (!file_exists(path2))
 4333             continue;
 4334 
 4335         nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps));
 4336         if (!nsnaps) {
 4337             SYSERROR("Out of memory");
 4338             goto out_free;
 4339         }
 4340 
 4341         snaps = nsnaps;
 4342         snaps[count].free = lxcsnap_free;
 4343         snaps[count].name = strdup(direntp->d_name);
 4344         if (!snaps[count].name)
 4345             goto out_free;
 4346 
 4347         snaps[count].lxcpath = strdup(snappath);
 4348         if (!snaps[count].lxcpath) {
 4349             free(snaps[count].name);
 4350             goto out_free;
 4351         }
 4352 
 4353         snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name);
 4354         snaps[count].timestamp = get_timestamp(snappath, direntp->d_name);
 4355         count++;
 4356     }
 4357 
 4358     *ret_snaps = snaps;
 4359     return count;
 4360 
 4361 out_free:
 4362     if (snaps) {
 4363         for (int i = 0; i < count; i++)
 4364             lxcsnap_free(&snaps[i]);
 4365 
 4366         free(snaps);
 4367     }
 4368 
 4369     return -1;
 4370 }
 4371 
 4372 WRAP_API_1(int, lxcapi_snapshot_list, struct lxc_snapshot **)
 4373 
 4374 static bool do_lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname)
 4375 {
 4376     char clonelxcpath[PATH_MAX];
 4377     int flags = 0;
 4378     struct lxc_container *snap, *rest;
 4379     struct lxc_storage *bdev;
 4380     bool b = false;
 4381 
 4382     if (!c || !c->name || !c->config_path)
 4383         return false;
 4384 
 4385     if (has_fs_snapshots(c)) {
 4386         ERROR("container rootfs has dependent snapshots");
 4387         return false;
 4388     }
 4389 
 4390     if (lxc_storage_prepare(c->lxc_conf)) {
 4391         ERROR("Failed to find original backing store type");
 4392         return false;
 4393     }
 4394     bdev = c->lxc_conf->rootfs.storage;
 4395 
 4396     /* For an overlay container the rootfs is considered immutable
 4397      * and cannot be removed when restoring from a snapshot. We pass this
 4398      * internal flag along to communicate this to various parts of the
 4399      * codebase.
 4400      */
 4401     if (strequal(bdev->type, "overlay") || strequal(bdev->type, "overlayfs"))
 4402         bdev->flags |= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE;
 4403 
 4404     if (!newname)
 4405         newname = c->name;
 4406 
 4407     if (!get_snappath_dir(c, clonelxcpath)) {
 4408         lxc_storage_put(c->lxc_conf);
 4409         return false;
 4410     }
 4411     /* how should we lock this? */
 4412 
 4413     snap = lxc_container_new(snapname, clonelxcpath);
 4414     if (!snap || !lxcapi_is_defined(snap)) {
 4415         ERROR("Could not open snapshot %s", snapname);
 4416 
 4417         if (snap)
 4418             lxc_container_put(snap);
 4419 
 4420         lxc_storage_put(c->lxc_conf);
 4421         return false;
 4422     }
 4423 
 4424     if (strequal(c->name, newname)) {
 4425         if (!container_destroy(c, bdev)) {
 4426             ERROR("Could not destroy existing container %s", newname);
 4427             lxc_container_put(snap);
 4428             lxc_storage_put(c->lxc_conf);
 4429             return false;
 4430         }
 4431     }
 4432 
 4433     if (!strequal(bdev->type, "dir") && !strequal(bdev->type, "loop"))
 4434         flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
 4435 
 4436     if (strequal(bdev->type, "overlay") || strequal(bdev->type, "overlayfs"))
 4437         flags |= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE;
 4438 
 4439     rest = lxcapi_clone(snap, newname, c->config_path, flags, bdev->type,
 4440                 NULL, 0, NULL);
 4441     lxc_storage_put(c->lxc_conf);
 4442     if (rest && lxcapi_is_defined(rest))
 4443         b = true;
 4444 
 4445     if (rest)
 4446         lxc_container_put(rest);
 4447 
 4448     lxc_container_put(snap);
 4449     return b;
 4450 }
 4451 
 4452 WRAP_API_2(bool, lxcapi_snapshot_restore, const char *, const char *)
 4453 
 4454 static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath)
 4455 {
 4456     struct lxc_container *snap = NULL;
 4457     bool bret = false;
 4458 
 4459     snap = lxc_container_new(snapname, clonelxcpath);
 4460     if (!snap) {
 4461         ERROR("Could not find snapshot %s", snapname);
 4462         goto err;
 4463     }
 4464 
 4465     if (!do_lxcapi_destroy(snap)) {
 4466         ERROR("Could not destroy snapshot %s", snapname);
 4467         goto err;
 4468     }
 4469 
 4470     bret = true;
 4471 
 4472 err:
 4473     if (snap)
 4474         lxc_container_put(snap);
 4475 
 4476     return bret;
 4477 }
 4478 
 4479 static bool remove_all_snapshots(const char *path)
 4480 {
 4481     __do_closedir DIR *dir = NULL;
 4482     struct dirent *direntp;
 4483     bool bret = true;
 4484 
 4485     dir = opendir(path);
 4486     if (!dir) {
 4487         SYSERROR("opendir on snapshot path %s", path);
 4488         return false;
 4489     }
 4490 
 4491     while ((direntp = readdir(dir))) {
 4492         if (strequal(direntp->d_name, "."))
 4493             continue;
 4494 
 4495         if (strequal(direntp->d_name, ".."))
 4496             continue;
 4497 
 4498         if (!do_snapshot_destroy(direntp->d_name, path)) {
 4499             bret = false;
 4500             continue;
 4501         }
 4502     }
 4503 
 4504     if (rmdir(path))
 4505         SYSERROR("Error removing directory %s", path);
 4506 
 4507     return bret;
 4508 }
 4509 
 4510 static bool do_lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname)
 4511 {
 4512     char clonelxcpath[PATH_MAX];
 4513 
 4514     if (!c || !c->name || !c->config_path || !snapname)
 4515         return false;
 4516 
 4517     if (!get_snappath_dir(c, clonelxcpath))
 4518         return false;
 4519 
 4520     return do_snapshot_destroy(snapname, clonelxcpath);
 4521 }
 4522 
 4523 WRAP_API_1(bool, lxcapi_snapshot_destroy, const char *)
 4524 
 4525 static bool do_lxcapi_snapshot_destroy_all(struct lxc_container *c)
 4526 {
 4527     char clonelxcpath[PATH_MAX];
 4528 
 4529     if (!c || !c->name || !c->config_path)
 4530         return false;
 4531 
 4532     if (!get_snappath_dir(c, clonelxcpath))
 4533         return false;
 4534 
 4535     return remove_all_snapshots(clonelxcpath);
 4536 }
 4537 
 4538 WRAP_API(bool, lxcapi_snapshot_destroy_all)
 4539 
 4540 static bool do_lxcapi_may_control(struct lxc_container *c)
 4541 {
 4542     if (!c)
 4543         return false;
 4544 
 4545     return lxc_try_cmd(c->name, c->config_path) == 0;
 4546 }
 4547 
 4548 WRAP_API(bool, lxcapi_may_control)
 4549 
 4550 static bool do_add_remove_node(pid_t init_pid, const char *path, bool add,
 4551                    struct stat *st)
 4552 {
 4553     int ret;
 4554     char *tmp;
 4555     pid_t pid;
 4556     char chrootpath[PATH_MAX];
 4557     char *directory_path = NULL;
 4558 
 4559     pid = fork();
 4560     if (pid < 0) {
 4561         SYSERROR("Failed to fork()");
 4562         return false;
 4563     }
 4564 
 4565     if (pid) {
 4566         ret = wait_for_pid(pid);
 4567         if (ret != 0) {
 4568             ERROR("Failed to create device node");
 4569             return false;
 4570         }
 4571 
 4572         return true;
 4573     }
 4574 
 4575     /* prepare the path */
 4576     ret = strnprintf(chrootpath, sizeof(chrootpath), "/proc/%d/root", init_pid);
 4577     if (ret < 0)
 4578         return false;
 4579 
 4580     ret = chroot(chrootpath);
 4581     if (ret < 0)
 4582         _exit(EXIT_FAILURE);
 4583 
 4584     ret = chdir("/");
 4585     if (ret < 0)
 4586         _exit(EXIT_FAILURE);
 4587 
 4588     /* remove path if it exists */
 4589     ret = faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW);
 4590     if(ret == 0) {
 4591         ret = unlink(path);
 4592         if (ret < 0) {
 4593             SYSERROR("Failed to remove \"%s\"", path);
 4594             _exit(EXIT_FAILURE);
 4595         }
 4596     }
 4597 
 4598     if (!add)
 4599         _exit(EXIT_SUCCESS);
 4600 
 4601     /* create any missing directories */
 4602     tmp = strdup(path);
 4603     if (!tmp)
 4604         _exit(EXIT_FAILURE);
 4605 
 4606     directory_path = dirname(tmp);
 4607     ret = mkdir_p(directory_path, 0755);
 4608     if (ret < 0 && errno != EEXIST) {
 4609         SYSERROR("Failed to create path \"%s\"", directory_path);
 4610         free(tmp);
 4611         _exit(EXIT_FAILURE);
 4612     }
 4613 
 4614     /* create the device node */
 4615     ret = mknod(path, st->st_mode, st->st_rdev);
 4616     free(tmp);
 4617     if (ret < 0) {
 4618         SYSERROR("Failed to create device node at \"%s\"", path);
 4619         _exit(EXIT_FAILURE);
 4620     }
 4621 
 4622     _exit(EXIT_SUCCESS);
 4623 }
 4624 
 4625 static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add)
 4626 {
 4627     int ret;
 4628     struct stat st;
 4629     char value[LXC_MAX_BUFFER];
 4630     const char *p;
 4631     pid_t init_pid;
 4632 
 4633     /* make sure container is running */
 4634     if (!do_lxcapi_is_running(c)) {
 4635         ERROR("container is not running");
 4636         return false;
 4637     }
 4638 
 4639     /* use src_path if dest_path is NULL otherwise use dest_path */
 4640     p = dest_path ? dest_path : src_path;
 4641 
 4642     /* make sure we can access p */
 4643     if(access(p, F_OK) < 0 || stat(p, &st) < 0)
 4644         return false;
 4645 
 4646     /* continue if path is character device or block device */
 4647     if (S_ISCHR(st.st_mode))
 4648         ret = strnprintf(value, sizeof(value), "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev));
 4649     else if (S_ISBLK(st.st_mode))
 4650         ret = strnprintf(value, sizeof(value), "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev));
 4651     else
 4652         return false;
 4653     if (ret < 0)
 4654         return false;
 4655 
 4656     init_pid = do_lxcapi_init_pid(c);
 4657     if (init_pid < 0) {
 4658         ERROR("Failed to get init pid");
 4659         return false;
 4660     }
 4661 
 4662     if (!do_add_remove_node(init_pid, p, add, &st))
 4663         return false;
 4664 
 4665     /* add or remove device to/from cgroup access list */
 4666     if (add) {
 4667         if (!do_lxcapi_set_cgroup_item(c, "devices.allow", value)) {
 4668             ERROR("set_cgroup_item failed while adding the device node");
 4669             return false;
 4670         }
 4671     } else {
 4672         if (!do_lxcapi_set_cgroup_item(c, "devices.deny", value)) {
 4673             ERROR("set_cgroup_item failed while removing the device node");
 4674             return false;
 4675         }
 4676     }
 4677 
 4678     return true;
 4679 }
 4680 
 4681 static bool do_lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
 4682 {
 4683     // cannot mknod if we're not privileged wrt init_user_ns
 4684     if (am_host_unpriv()) {
 4685         ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__);
 4686         return false;
 4687     }
 4688 
 4689     return add_remove_device_node(c, src_path, dest_path, true);
 4690 }
 4691 
 4692 WRAP_API_2(bool, lxcapi_add_device_node, const char *, const char *)
 4693 
 4694 static bool do_lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
 4695 {
 4696     if (am_guest_unpriv()) {
 4697         ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__);
 4698         return false;
 4699     }
 4700 
 4701     return add_remove_device_node(c, src_path, dest_path, false);
 4702 }
 4703 
 4704 WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *)
 4705 
 4706 static bool do_lxcapi_attach_interface(struct lxc_container *c,
 4707                        const char *ifname,
 4708                        const char *dst_ifname)
 4709 {
 4710     pid_t init_pid;
 4711     int ret = 0;
 4712 
 4713     if (am_guest_unpriv()) {
 4714         ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__);
 4715         return false;
 4716     }
 4717 
 4718     if (!ifname) {
 4719         ERROR("No source interface name given");
 4720         return false;
 4721     }
 4722 
 4723     ret = lxc_netdev_isup(ifname);
 4724     if (ret > 0) {
 4725         /* netdev of ifname is up. */
 4726         ret = lxc_netdev_down(ifname);
 4727         if (ret)
 4728             goto err;
 4729     }
 4730 
 4731     init_pid = do_lxcapi_init_pid(c);
 4732     if (init_pid < 0) {
 4733         ERROR("Failed to get init pid");
 4734         goto err;
 4735     }
 4736 
 4737     ret = lxc_netdev_move_by_name(ifname, init_pid, dst_ifname);
 4738     if (ret)
 4739         goto err;
 4740 
 4741     INFO("Moved network device \"%s\" to network namespace of %d", ifname, init_pid);
 4742     return true;
 4743 
 4744 err:
 4745     return false;
 4746 }
 4747 
 4748 WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *)
 4749 
 4750 static bool do_lxcapi_detach_interface(struct lxc_container *c,
 4751                        const char *ifname,
 4752                        const char *dst_ifname)
 4753 {
 4754     int ret;
 4755     pid_t pid, pid_outside;
 4756     __do_free char *physname = NULL;
 4757 
 4758     /*
 4759      * TODO - if this is a physical device, then we need am_host_unpriv.
 4760      * But for other types guest privilege suffices.
 4761      */
 4762     if (am_guest_unpriv()) {
 4763         ERROR(LXC_UNPRIV_EOPNOTSUPP, __FUNCTION__);
 4764         return false;
 4765     }
 4766 
 4767     if (!ifname) {
 4768         ERROR("No source interface name given");
 4769         return false;
 4770     }
 4771 
 4772     pid_outside = lxc_raw_getpid();
 4773     pid = fork();
 4774     if (pid < 0) {
 4775         ERROR("Failed to fork");
 4776         return false;
 4777     }
 4778 
 4779     if (pid == 0) { /* child */
 4780         pid_t init_pid;
 4781 
 4782         init_pid = do_lxcapi_init_pid(c);
 4783         if (init_pid < 0) {
 4784             ERROR("Failed to get init pid");
 4785             _exit(EXIT_FAILURE);
 4786         }
 4787         if (!switch_to_ns(init_pid, "net")) {
 4788             ERROR("Failed to enter network namespace");
 4789             _exit(EXIT_FAILURE);
 4790         }
 4791 
 4792         /* create new mount namespace for use with remounting /sys and is_wlan() below. */
 4793         ret = unshare(CLONE_NEWNS);
 4794         if (ret < 0) {
 4795             ERROR("Failed to unshare mount namespace");
 4796             _exit(EXIT_FAILURE);
 4797         }
 4798 
 4799         /* set / recursively as private so that mount propagation doesn't affect us.  */
 4800         if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0) < 0) {
 4801             ERROR("Failed to recursively set / as private in mount namespace");
 4802             _exit(EXIT_FAILURE);
 4803         }
 4804 
 4805         ret = lxc_netdev_isup(ifname);
 4806         if (ret < 0) {
 4807             ERROR("Failed to determine whether network device \"%s\" is up", ifname);
 4808             _exit(EXIT_FAILURE);
 4809         }
 4810 
 4811         /* netdev of ifname is up. */
 4812         if (ret) {
 4813             ret = lxc_netdev_down(ifname);
 4814             if (ret) {
 4815                 ERROR("Failed to set network device \"%s\" down", ifname);
 4816                 _exit(EXIT_FAILURE);
 4817             }
 4818         }
 4819 
 4820         /* remount /sys so is_wlan() can check if this device is a wlan device. */
 4821         lxc_attach_remount_sys_proc();
 4822         physname = is_wlan(ifname);
 4823         if (physname)
 4824             ret = lxc_netdev_move_wlan(physname, ifname, pid_outside, dst_ifname);
 4825         else
 4826             ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname);
 4827 
 4828         /* -EINVAL means there is no netdev named as ifname. */
 4829         if (ret < 0) {
 4830             if (ret == -EINVAL)
 4831                 ERROR("Network device \"%s\" not found", ifname);
 4832             else
 4833                 ERROR("Failed to remove network device \"%s\"", ifname);
 4834 
 4835             _exit(EXIT_FAILURE);
 4836         }
 4837 
 4838         _exit(EXIT_SUCCESS);
 4839     }
 4840 
 4841     ret = wait_for_pid(pid);
 4842     if (ret != 0)
 4843         return false;
 4844 
 4845     INFO("Moved network device \"%s\" to network namespace of %d", ifname, pid_outside);
 4846     return true;
 4847 }
 4848 
 4849 WRAP_API_2(bool, lxcapi_detach_interface, const char *, const char *)
 4850 
 4851 static int do_lxcapi_migrate(struct lxc_container *c, unsigned int cmd,
 4852                  struct migrate_opts *opts, unsigned int size)
 4853 {
 4854     int ret = -1;
 4855     struct migrate_opts *valid_opts = opts;
 4856     uint64_t features_to_check = 0;
 4857 
 4858     /* If the caller has a bigger (newer) struct migrate_opts, let's make
 4859      * sure that the stuff on the end is zero, i.e. that they didn't ask us
 4860      * to do anything special.
 4861      */
 4862     if (size > sizeof(*opts)) {
 4863         unsigned char *addr;
 4864         unsigned char *end;
 4865 
 4866         addr = (void *)opts + sizeof(*opts);
 4867         end  = (void *)opts + size;
 4868 
 4869         for (; addr < end; addr++)
 4870             if (*addr)
 4871                 return -E2BIG;
 4872     }
 4873 
 4874     /* If the caller has a smaller struct, let's zero out the end for them
 4875      * so we don't accidentally use bits of it that they didn't know about
 4876      * to initialize.
 4877      */
 4878     if (size < sizeof(*opts)) {
 4879         valid_opts = malloc(sizeof(*opts));
 4880         if (!valid_opts)
 4881             return -ENOMEM;
 4882 
 4883         memset(valid_opts, 0, sizeof(*opts));
 4884         memcpy(valid_opts, opts, size);
 4885     }
 4886 
 4887     switch (cmd) {
 4888     case MIGRATE_PRE_DUMP:
 4889         if (!do_lxcapi_is_running(c)) {
 4890             ERROR("container is not running");
 4891             goto on_error;
 4892         }
 4893 
 4894         ret = !__criu_pre_dump(c, valid_opts);
 4895         break;
 4896     case MIGRATE_DUMP:
 4897         if (!do_lxcapi_is_running(c)) {
 4898             ERROR("container is not running");
 4899             goto on_error;
 4900         }
 4901 
 4902         ret = !__criu_dump(c, valid_opts);
 4903         break;
 4904     case MIGRATE_RESTORE:
 4905         if (do_lxcapi_is_running(c)) {
 4906             ERROR("container is already running");
 4907             goto on_error;
 4908         }
 4909 
 4910         ret = !__criu_restore(c, valid_opts);
 4911         break;
 4912     case MIGRATE_FEATURE_CHECK:
 4913         features_to_check = valid_opts->features_to_check;
 4914         ret = !__criu_check_feature(&features_to_check);
 4915         if (ret) {
 4916             /* Something went wrong. Let's let the caller
 4917              * know which feature checks failed. */
 4918             valid_opts->features_to_check = features_to_check;
 4919         }
 4920         break;
 4921     default:
 4922         ERROR("invalid migrate command %u", cmd);
 4923         ret = -EINVAL;
 4924     }
 4925 
 4926 on_error:
 4927     if (size < sizeof(*opts))
 4928         free(valid_opts);
 4929 
 4930     return ret;
 4931 }
 4932 
 4933 WRAP_API_3(int, lxcapi_migrate, unsigned int, struct migrate_opts *, unsigned int)
 4934 
 4935 static bool do_lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose)
 4936 {
 4937     struct migrate_opts opts;
 4938 
 4939     memset(&opts, 0, sizeof(opts));
 4940 
 4941     opts.directory = directory;
 4942     opts.stop = stop;
 4943     opts.verbose = verbose;
 4944 
 4945     return !do_lxcapi_migrate(c, MIGRATE_DUMP, &opts, sizeof(opts));
 4946 }
 4947 
 4948 WRAP_API_3(bool, lxcapi_checkpoint, char *, bool, bool)
 4949 
 4950 static bool do_lxcapi_restore(struct lxc_container *c, char *directory, bool verbose)
 4951 {
 4952     struct migrate_opts opts;
 4953 
 4954     memset(&opts, 0, sizeof(opts));
 4955 
 4956     opts.directory = directory;
 4957     opts.verbose = verbose;
 4958 
 4959     return !do_lxcapi_migrate(c, MIGRATE_RESTORE, &opts, sizeof(opts));
 4960 }
 4961 
 4962 WRAP_API_2(bool, lxcapi_restore, char *, bool)
 4963 
 4964 /* @st_mode is the st_mode field of the stat(source) return struct */
 4965 static int create_mount_target(const char *dest, mode_t st_mode)
 4966 {
 4967     char *dirdup, *destdirname;
 4968     int ret;
 4969 
 4970     dirdup = strdup(dest);
 4971     if (!dirdup) {
 4972         SYSERROR("Failed to duplicate target name \"%s\"", dest);
 4973         return -1;
 4974     }
 4975     destdirname = dirname(dirdup);
 4976 
 4977     ret = mkdir_p(destdirname, 0755);
 4978     if (ret < 0) {
 4979         SYSERROR("Failed to create \"%s\"", destdirname);
 4980         free(dirdup);
 4981         return ret;
 4982     }
 4983     free(dirdup);
 4984 
 4985     (void)remove(dest);
 4986 
 4987     if (S_ISDIR(st_mode))
 4988         ret = mkdir(dest, 0000);
 4989     else
 4990         ret = mknod(dest, S_IFREG | 0000, 0);
 4991 
 4992     if (ret == 0)
 4993         TRACE("Created mount target \"%s\"", dest);
 4994     else if (ret < 0 && errno != EEXIST) {
 4995         SYSERROR("Failed to create mount target \"%s\"", dest);
 4996         return -1;
 4997     }
 4998 
 4999     return 0;
 5000 }
 5001 
 5002 static int do_lxcapi_mount(struct lxc_container *c, const char *source,
 5003                const char *target, const char *filesystemtype,
 5004                unsigned long mountflags, const void *data,
 5005                struct lxc_mount *mnt)
 5006 {
 5007     char *suff, *sret;
 5008     char template[PATH_MAX], path[PATH_MAX];
 5009     pid_t pid, init_pid;
 5010     struct stat sb;
 5011     bool is_dir;
 5012     int ret = -1, fd = -EBADF;
 5013 
 5014     if (!c || !c->lxc_conf) {
 5015         ERROR("Container or configuration is NULL");
 5016         return -EINVAL;
 5017     }
 5018 
 5019     if (!c->lxc_conf->shmount.path_host) {
 5020         ERROR("Host path to shared mountpoint must be specified in the config\n");
 5021         return -EINVAL;
 5022     }
 5023 
 5024     ret = strnprintf(template, sizeof(template), "%s/.lxcmount_XXXXXX", c->lxc_conf->shmount.path_host);
 5025     if (ret < 0) {
 5026         SYSERROR("Error writing shmounts tempdir name");
 5027         goto out;
 5028     }
 5029 
 5030     /* Create a temporary file / dir under the shared mountpoint */
 5031     if (!source || strequal(source, "")) {
 5032         /* If source is not specified, maybe we want to mount a filesystem? */
 5033         sb.st_mode = S_IFDIR;
 5034     } else {
 5035         ret = stat(source, &sb);
 5036         if (ret < 0) {
 5037             SYSERROR("Error getting stat info about the source \"%s\"", source);
 5038             goto out;
 5039         }
 5040     }
 5041 
 5042     is_dir = (S_ISDIR(sb.st_mode) != 0);
 5043     if (is_dir) {
 5044         sret = mkdtemp(template);
 5045         if (!sret) {
 5046             SYSERROR("Could not create shmounts temporary dir");
 5047             goto out;
 5048         }
 5049     } else {
 5050         fd = lxc_make_tmpfile(template, false);
 5051         if (fd < 0) {
 5052             SYSERROR("Could not create shmounts temporary file");
 5053             goto out;
 5054         }
 5055     }
 5056 
 5057     /* Do the fork */
 5058     pid = fork();
 5059     if (pid < 0) {
 5060         SYSERROR("Could not fork");
 5061         goto out;
 5062     }
 5063 
 5064     if (pid == 0) {
 5065         /* Do the mount */
 5066         ret = mount(source, template, filesystemtype, mountflags, data);
 5067         if (ret < 0) {
 5068             SYSERROR("Failed to mount onto \"%s\"", template);
 5069             _exit(EXIT_FAILURE);
 5070         }
 5071         TRACE("Mounted \"%s\" onto \"%s\"", source, template);
 5072 
 5073         init_pid = do_lxcapi_init_pid(c);
 5074         if (init_pid < 0) {
 5075             ERROR("Failed to obtain container's init pid");
 5076             _exit(EXIT_FAILURE);
 5077         }
 5078 
 5079         /* Enter the container namespaces */
 5080         if (!lxc_list_empty(&c->lxc_conf->id_map)) {
 5081             if (!switch_to_ns(init_pid, "user")) {
 5082                 ERROR("Failed to enter user namespace");
 5083                 _exit(EXIT_FAILURE);
 5084             }
 5085 
 5086             if (!lxc_switch_uid_gid(0, 0))
 5087                 _exit(EXIT_FAILURE);
 5088         }
 5089 
 5090         if (!switch_to_ns(init_pid, "mnt")) {
 5091             ERROR("Failed to enter mount namespace");
 5092             _exit(EXIT_FAILURE);
 5093         }
 5094 
 5095         ret = create_mount_target(target, sb.st_mode);
 5096         if (ret < 0)
 5097             _exit(EXIT_FAILURE);
 5098 
 5099         suff = strrchr(template, '/');
 5100         if (!suff)
 5101             goto cleanup_target_in_child;
 5102 
 5103         ret = strnprintf(path, sizeof(path), "%s%s", c->lxc_conf->shmount.path_cont, suff);
 5104         if (ret < 0) {
 5105             SYSERROR("Error writing container mountpoint name");
 5106             goto cleanup_target_in_child;
 5107         }
 5108 
 5109         ret = mount(path, target, NULL, MS_MOVE | MS_REC, NULL);
 5110         if (ret < 0) {
 5111             SYSERROR("Failed to move the mount from \"%s\" to \"%s\"", path, target);
 5112             goto cleanup_target_in_child;
 5113         }
 5114         TRACE("Moved mount from \"%s\" to \"%s\"", path, target);
 5115 
 5116         _exit(EXIT_SUCCESS);
 5117 
 5118     cleanup_target_in_child:
 5119         (void)remove(target);
 5120         _exit(EXIT_FAILURE);
 5121     }
 5122 
 5123     ret = wait_for_pid(pid);
 5124     if (ret < 0)
 5125         SYSERROR("Wait for the child with pid %ld failed", (long)pid);
 5126     else
 5127         ret = 0;
 5128 
 5129     if (umount2(template, MNT_DETACH))
 5130         SYSWARN("Failed to remove temporary mount \"%s\"", template);
 5131 
 5132     if (is_dir)
 5133         (void)rmdir(template);
 5134     else
 5135         (void)unlink(template);
 5136 
 5137 out:
 5138     if (fd >= 0)
 5139         close(fd);
 5140 
 5141     return ret;
 5142 }
 5143 
 5144 WRAP_API_6(int, lxcapi_mount, const char *, const char *, const char *,
 5145        unsigned long, const void *, struct lxc_mount *)
 5146 
 5147 static int do_lxcapi_umount(struct lxc_container *c, const char *target,
 5148                 unsigned long flags, struct lxc_mount *mnt)
 5149 {
 5150     pid_t pid, init_pid;
 5151     int ret = -1;
 5152 
 5153     if (!c || !c->lxc_conf) {
 5154         ERROR("Container or configuration is NULL");
 5155         return -EINVAL;
 5156     }
 5157 
 5158     /* Do the fork */
 5159     pid = fork();
 5160     if (pid < 0) {
 5161         SYSERROR("Could not fork");
 5162         return -1;
 5163     }
 5164 
 5165     if (pid == 0) {
 5166         init_pid = do_lxcapi_init_pid(c);
 5167         if (init_pid < 0) {
 5168             ERROR("Failed to obtain container's init pid");
 5169             _exit(EXIT_FAILURE);
 5170         }
 5171 
 5172         /* Enter the container namespaces */
 5173         if (!lxc_list_empty(&c->lxc_conf->id_map)) {
 5174             if (!switch_to_ns(init_pid, "user")) {
 5175                 ERROR("Failed to enter user namespace");
 5176                 _exit(EXIT_FAILURE);
 5177             }
 5178         }
 5179 
 5180         if (!switch_to_ns(init_pid, "mnt")) {
 5181             ERROR("Failed to enter mount namespace");
 5182             _exit(EXIT_FAILURE);
 5183         }
 5184 
 5185         /* Do the unmount */
 5186         ret = umount2(target, flags);
 5187         if (ret < 0) {
 5188             SYSERROR("Failed to umount \"%s\"", target);
 5189             _exit(EXIT_FAILURE);
 5190         }
 5191 
 5192         _exit(EXIT_SUCCESS);
 5193     }
 5194 
 5195     ret = wait_for_pid(pid);
 5196     if (ret < 0) {
 5197         SYSERROR("Wait for the child with pid %ld failed", (long)pid);
 5198         return -ret;
 5199     }
 5200 
 5201     return 0;
 5202 }
 5203 
 5204 WRAP_API_3(int, lxcapi_umount, const char *, unsigned long, struct lxc_mount*)
 5205 
 5206 static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
 5207 {
 5208     va_list ap;
 5209     const char **argv;
 5210     int ret;
 5211 
 5212     if (!c)
 5213         return -1;
 5214 
 5215     current_config = c->lxc_conf;
 5216 
 5217     va_start(ap, arg);
 5218     argv = lxc_va_arg_list_to_argv_const(ap, 1);
 5219     va_end(ap);
 5220 
 5221     if (!argv) {
 5222         ERROR("Memory allocation error.");
 5223         ret = -1;
 5224         goto out;
 5225     }
 5226     argv[0] = arg;
 5227 
 5228     ret = do_lxcapi_attach_run_wait(c, options, program, (const char * const *)argv);
 5229     free((void*)argv);
 5230 
 5231 out:
 5232     current_config = NULL;
 5233     return ret;
 5234 }
 5235 
 5236 static int do_lxcapi_seccomp_notify_fd(struct lxc_container *c)
 5237 {
 5238     if (!c || !c->lxc_conf)
 5239         return ret_set_errno(-1, -EINVAL);
 5240 
 5241     return lxc_seccomp_get_notify_fd(&c->lxc_conf->seccomp);
 5242 }
 5243 
 5244 WRAP_API(int, lxcapi_seccomp_notify_fd)
 5245 
 5246 static int do_lxcapi_seccomp_notify_fd_active(struct lxc_container *c)
 5247 {
 5248     if (!c || !c->lxc_conf)
 5249         return ret_set_errno(-1, -EINVAL);
 5250 
 5251     return lxc_cmd_get_seccomp_notify_fd(c->name, c->config_path);
 5252 }
 5253 
 5254 WRAP_API(int, lxcapi_seccomp_notify_fd_active)
 5255 
 5256 struct lxc_container *lxc_container_new(const char *name, const char *configpath)
 5257 {
 5258     struct lxc_container *c;
 5259     size_t len;
 5260     int rc;
 5261 
 5262     if (!name)
 5263         return NULL;
 5264 
 5265     c = malloc(sizeof(*c));
 5266     if (!c) {
 5267         fprintf(stderr, "Failed to allocate memory for %s\n", name);
 5268         return NULL;
 5269     }
 5270     memset(c, 0, sizeof(*c));
 5271 
 5272     if (configpath)
 5273         c->config_path = strdup(configpath);
 5274     else
 5275         c->config_path = strdup(lxc_global_config_value("lxc.lxcpath"));
 5276     if (!c->config_path) {
 5277         fprintf(stderr, "Failed to allocate memory for %s\n", name);
 5278         goto err;
 5279     }
 5280 
 5281     remove_trailing_slashes(c->config_path);
 5282 
 5283     len = strlen(name);
 5284     c->name = malloc(len + 1);
 5285     if (!c->name) {
 5286         fprintf(stderr, "Failed to allocate memory for %s\n", name);
 5287         goto err;
 5288     }
 5289     (void)strlcpy(c->name, name, len + 1);
 5290 
 5291     c->numthreads = 1;
 5292     c->slock = lxc_newlock(c->config_path, name);
 5293     if (!c->slock) {
 5294         fprintf(stderr, "Failed to create lock for %s\n", name);
 5295         goto err;
 5296     }
 5297 
 5298     c->privlock = lxc_newlock(NULL, NULL);
 5299     if (!c->privlock) {
 5300         fprintf(stderr, "Failed to create private lock for %s\n", name);
 5301         goto err;
 5302     }
 5303 
 5304     if (!set_config_filename(c)) {
 5305         fprintf(stderr, "Failed to create config file name for %s\n", name);
 5306         goto err;
 5307     }
 5308 
 5309     if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL)) {
 5310         fprintf(stderr, "Failed to load config for %s\n", name);
 5311         goto err;
 5312     }
 5313 
 5314     rc = ongoing_create(c);
 5315     switch (rc) {
 5316     case LXC_CREATE_INCOMPLETE:
 5317         SYSERROR("Failed to complete container creation for %s", c->name);
 5318         container_destroy(c, NULL);
 5319         lxcapi_clear_config(c);
 5320         break;
 5321     case LXC_CREATE_ONGOING:
 5322         /* container creation going on */
 5323         break;
 5324     case LXC_CREATE_FAILED:
 5325         /* container creation failed */
 5326         if (errno != EACCES && errno != EPERM) {
 5327             /* insufficient privileges */
 5328             SYSERROR("Failed checking for incomplete container %s creation", c->name);
 5329             goto err;
 5330         }
 5331         break;
 5332     }
 5333 
 5334     c->daemonize = true;
 5335     c->pidfile = NULL;
 5336 
 5337     /* Assign the member functions. */
 5338     c->is_defined = lxcapi_is_defined;
 5339     c->state = lxcapi_state;
 5340     c->is_running = lxcapi_is_running;
 5341     c->freeze = lxcapi_freeze;
 5342     c->unfreeze = lxcapi_unfreeze;
 5343     c->console = lxcapi_console;
 5344     c->console_getfd = lxcapi_console_getfd;
 5345     c->devpts_fd = lxcapi_devpts_fd;
 5346     c->init_pid = lxcapi_init_pid;
 5347     c->init_pidfd = lxcapi_init_pidfd;
 5348     c->load_config = lxcapi_load_config;
 5349     c->want_daemonize = lxcapi_want_daemonize;
 5350     c->want_close_all_fds = lxcapi_want_close_all_fds;
 5351     c->start = lxcapi_start;
 5352     c->startl = lxcapi_startl;
 5353     c->stop = lxcapi_stop;
 5354     c->config_file_name = lxcapi_config_file_name;
 5355     c->wait = lxcapi_wait;
 5356     c->set_config_item = lxcapi_set_config_item;
 5357     c->destroy = lxcapi_destroy;
 5358     c->destroy_with_snapshots = lxcapi_destroy_with_snapshots;
 5359     c->rename = lxcapi_rename;
 5360     c->save_config = lxcapi_save_config;
 5361     c->get_keys = lxcapi_get_keys;
 5362     c->create = lxcapi_create;
 5363     c->createl = lxcapi_createl;
 5364     c->shutdown = lxcapi_shutdown;
 5365     c->reboot = lxcapi_reboot;
 5366     c->reboot2 = lxcapi_reboot2;
 5367     c->clear_config = lxcapi_clear_config;
 5368     c->clear_config_item = lxcapi_clear_config_item;
 5369     c->get_config_item = lxcapi_get_config_item;
 5370     c->get_running_config_item = lxcapi_get_running_config_item;
 5371     c->get_cgroup_item = lxcapi_get_cgroup_item;
 5372     c->set_cgroup_item = lxcapi_set_cgroup_item;
 5373     c->get_config_path = lxcapi_get_config_path;
 5374     c->set_config_path = lxcapi_set_config_path;
 5375     c->clone = lxcapi_clone;
 5376     c->get_interfaces = lxcapi_get_interfaces;
 5377     c->get_ips = lxcapi_get_ips;
 5378     c->attach = lxcapi_attach;
 5379     c->attach_run_wait = lxcapi_attach_run_wait;
 5380     c->attach_run_waitl