]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxccontainer.c
avoid very unlikely race due to EEXIST
[mirror_lxc.git] / src / lxc / lxccontainer.c
CommitLineData
72d0e1cb
SG
1/* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
9be53773 20#define _GNU_SOURCE
a0e93eeb 21#include <stdarg.h>
71454076 22#include <pthread.h>
9be53773
SH
23#include <unistd.h>
24#include <sys/types.h>
25#include <sys/wait.h>
26#include <errno.h>
93dc5327 27#include <fcntl.h>
9be53773
SH
28#include <sched.h>
29#include "config.h"
72d0e1cb
SG
30#include "lxc.h"
31#include "state.h"
32#include "lxccontainer.h"
33#include "conf.h"
72d0e1cb 34#include "confile.h"
b5159817 35#include "console.h"
72d0e1cb
SG
36#include "cgroup.h"
37#include "commands.h"
b6b918a1 38#include "version.h"
72d0e1cb 39#include "log.h"
9be53773 40#include "bdev.h"
6a44839f 41#include "utils.h"
a0e93eeb 42#include "attach.h"
2a59a681 43#include <lxc/utils.h>
e51d4895 44#include <lxc/monitor.h>
e768f9c0 45#include <lxc/namespace.h>
9c83a661 46#include <sched.h>
9c83a661 47#include <arpa/inet.h>
4ba0d9af
SG
48
49#if HAVE_IFADDRS_H
9c83a661 50#include <ifaddrs.h>
4ba0d9af
SG
51#else
52#include <../include/ifaddrs.h>
53#endif
72d0e1cb
SG
54
55lxc_log_define(lxc_container, lxc);
56
3e625e2d
SH
57static bool file_exists(char *f)
58{
59 struct stat statbuf;
60
61 return stat(f, &statbuf) == 0;
62}
63
64/*
65 * A few functions to help detect when a container creation failed.
66 * If a container creation was killed partway through, then trying
67 * to actually start that container could harm the host. We detect
68 * this by creating a 'partial' file under the container directory,
69 * and keeping an advisory lock. When container creation completes,
70 * we remove that file. When we load or try to start a container, if
71 * we find that file, without a flock, we remove the container.
72 */
73int ongoing_create(struct lxc_container *c)
74{
75 int len = strlen(c->config_path) + strlen(c->name) + 10;
76 char *path = alloca(len);
77 int fd, ret;
93dc5327
SH
78 struct flock lk;
79
3e625e2d
SH
80 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
81 if (ret < 0 || ret >= len) {
82 ERROR("Error writing partial pathname");
83 return -1;
84 }
85
86 if (!file_exists(path))
87 return 0;
88 if (process_lock())
89 return -1;
90 if ((fd = open(path, O_RDWR)) < 0) {
91 // give benefit of the doubt
92 SYSERROR("Error opening partial file");
93 process_unlock();
94 return 0;
95 }
93dc5327
SH
96 lk.l_type = F_WRLCK;
97 lk.l_whence = SEEK_SET;
98 lk.l_start = 0;
99 lk.l_len = 0;
100 lk.l_pid = -1;
101 if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) {
3e625e2d
SH
102 // create is still ongoing
103 close(fd);
104 process_unlock();
105 return 1;
106 }
107 // create completed but partial is still there.
108 close(fd);
109 process_unlock();
110 return 2;
111}
112
113int create_partial(struct lxc_container *c)
114{
115 // $lxcpath + '/' + $name + '/partial' + \0
116 int len = strlen(c->config_path) + strlen(c->name) + 10;
117 char *path = alloca(len);
118 int fd, ret;
93dc5327
SH
119 struct flock lk;
120
3e625e2d
SH
121 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
122 if (ret < 0 || ret >= len) {
123 ERROR("Error writing partial pathname");
124 return -1;
125 }
1897e3bc 126 if (process_lock())
3e625e2d 127 return -1;
93dc5327 128 if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) {
3e625e2d
SH
129 SYSERROR("Erorr creating partial file");
130 process_unlock();
131 return -1;
132 }
93dc5327
SH
133 lk.l_type = F_WRLCK;
134 lk.l_whence = SEEK_SET;
135 lk.l_start = 0;
136 lk.l_len = 0;
137 if (fcntl(fd, F_SETLKW, &lk) < 0) {
3e625e2d
SH
138 SYSERROR("Error locking partial file %s", path);
139 close(fd);
140 process_unlock();
141 return -1;
142 }
143 process_unlock();
144
145 return fd;
146}
147
148void remove_partial(struct lxc_container *c, int fd)
149{
150 // $lxcpath + '/' + $name + '/partial' + \0
151 int len = strlen(c->config_path) + strlen(c->name) + 10;
152 char *path = alloca(len);
153 int ret;
154
155 close(fd);
156 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
157 if (ret < 0 || ret >= len) {
158 ERROR("Error writing partial pathname");
159 return;
160 }
161 if (process_lock())
162 return;
163 if (unlink(path) < 0)
164 SYSERROR("Error unlink partial file %s", path);
165 process_unlock();
166}
167
72d0e1cb 168/* LOCKING
3bc449ed
SH
169 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
170 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
171 * container configuration file.
172 * The container_disk_lock also takes the container_mem_lock.
173 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
72d0e1cb
SG
174 * NOTHING mutexes two independent programs with their own struct
175 * lxc_container for the same c->name, between API calls. For instance,
176 * c->config_read(); c->start(); Between those calls, data on disk
177 * could change (which shouldn't bother the caller unless for instance
178 * the rootfs get moved). c->config_read(); update; c->config_write();
179 * Two such updaters could race. The callers should therefore check their
180 * results. Trying to prevent that would necessarily expose us to deadlocks
181 * due to hung callers. So I prefer to keep the locks only within our own
182 * functions, not across functions.
183 *
3bc449ed 184 * If you're going to clone while holding a lxccontainer, increment
72d0e1cb
SG
185 * c->numthreads (under privlock) before forking. When deleting,
186 * decrement numthreads under privlock, then if it hits 0 you can delete.
187 * Do not ever use a lxccontainer whose numthreads you did not bump.
188 */
189
190static void lxc_container_free(struct lxc_container *c)
191{
192 if (!c)
193 return;
194
195 if (c->configfile) {
196 free(c->configfile);
197 c->configfile = NULL;
198 }
199 if (c->error_string) {
200 free(c->error_string);
201 c->error_string = NULL;
202 }
d95db067 203 if (c->slock) {
df271a59 204 lxc_putlock(c->slock);
d95db067
DE
205 c->slock = NULL;
206 }
72d0e1cb 207 if (c->privlock) {
df271a59 208 lxc_putlock(c->privlock);
72d0e1cb
SG
209 c->privlock = NULL;
210 }
211 if (c->name) {
212 free(c->name);
213 c->name = NULL;
214 }
d95db067
DE
215 if (c->lxc_conf) {
216 lxc_conf_free(c->lxc_conf);
217 c->lxc_conf = NULL;
218 }
2a59a681
SH
219 if (c->config_path) {
220 free(c->config_path);
221 c->config_path = NULL;
222 }
72d0e1cb
SG
223 free(c);
224}
225
43d1aa34
SH
226/*
227 * Consider the following case:
228freer | racing get()er
229==================================================================
230lxc_container_put() | lxc_container_get()
231\ lxclock(c->privlock) | c->numthreads < 1? (no)
232\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
233\ lxcunlock() | \
234\ lxc_container_free() | \ lxclock() returns
235 | \ c->numthreads < 1 -> return 0
236\ \ (free stuff) |
237\ \ sem_destroy(privlock) |
238
239 * When the get()er checks numthreads the first time, one of the following
240 * is true:
241 * 1. freer has set numthreads = 0. get() returns 0
242 * 2. freer is between lxclock and setting numthreads to 0. get()er will
243 * sem_wait on privlock, get lxclock after freer() drops it, then see
244 * numthreads is 0 and exit without touching lxclock again..
245 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
246 * will see --numthreads = 1 and not call lxc_container_free().
247*/
248
72d0e1cb
SG
249int lxc_container_get(struct lxc_container *c)
250{
251 if (!c)
252 return 0;
253
43d1aa34
SH
254 // if someone else has already started freeing the container, don't
255 // try to take the lock, which may be invalid
256 if (c->numthreads < 1)
257 return 0;
258
5cee8c50 259 if (container_mem_lock(c))
72d0e1cb
SG
260 return 0;
261 if (c->numthreads < 1) {
262 // bail without trying to unlock, bc the privlock is now probably
263 // in freed memory
264 return 0;
265 }
266 c->numthreads++;
5cee8c50 267 container_mem_unlock(c);
72d0e1cb
SG
268 return 1;
269}
270
271int lxc_container_put(struct lxc_container *c)
272{
273 if (!c)
274 return -1;
5cee8c50 275 if (container_mem_lock(c))
72d0e1cb
SG
276 return -1;
277 if (--c->numthreads < 1) {
5cee8c50 278 container_mem_unlock(c);
72d0e1cb
SG
279 lxc_container_free(c);
280 return 1;
281 }
5cee8c50 282 container_mem_unlock(c);
72d0e1cb
SG
283 return 0;
284}
285
72d0e1cb
SG
286static bool lxcapi_is_defined(struct lxc_container *c)
287{
288 struct stat statbuf;
289 bool ret = false;
290 int statret;
291
292 if (!c)
293 return false;
294
5cee8c50 295 if (container_mem_lock(c))
72d0e1cb
SG
296 return false;
297 if (!c->configfile)
298 goto out;
299 statret = stat(c->configfile, &statbuf);
300 if (statret != 0)
301 goto out;
302 ret = true;
303
304out:
5cee8c50 305 container_mem_unlock(c);
72d0e1cb
SG
306 return ret;
307}
308
309static const char *lxcapi_state(struct lxc_container *c)
310{
72d0e1cb
SG
311 lxc_state_t s;
312
313 if (!c)
314 return NULL;
13f5be62 315 s = lxc_getstate(c->name, c->config_path);
39dc698c 316 return lxc_state2str(s);
72d0e1cb
SG
317}
318
39dc698c 319static bool is_stopped(struct lxc_container *c)
794dd120
SH
320{
321 lxc_state_t s;
13f5be62 322 s = lxc_getstate(c->name, c->config_path);
794dd120
SH
323 return (s == STOPPED);
324}
325
72d0e1cb
SG
326static bool lxcapi_is_running(struct lxc_container *c)
327{
328 const char *s;
329
330 if (!c)
331 return false;
332 s = lxcapi_state(c);
333 if (!s || strcmp(s, "STOPPED") == 0)
334 return false;
335 return true;
336}
337
338static bool lxcapi_freeze(struct lxc_container *c)
339{
340 int ret;
341 if (!c)
342 return false;
343
9123e471 344 ret = lxc_freeze(c->name, c->config_path);
72d0e1cb
SG
345 if (ret)
346 return false;
347 return true;
348}
349
350static bool lxcapi_unfreeze(struct lxc_container *c)
351{
352 int ret;
353 if (!c)
354 return false;
355
9123e471 356 ret = lxc_unfreeze(c->name, c->config_path);
72d0e1cb
SG
357 if (ret)
358 return false;
359 return true;
360}
361
b5159817 362static int lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd)
0115f8fd
DE
363{
364 int ttyfd;
365 if (!c)
366 return -1;
367
b5159817 368 ttyfd = lxc_console_getfd(c, ttynum, masterfd);
0115f8fd
DE
369 return ttyfd;
370}
371
b5159817
DE
372static int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd,
373 int stdoutfd, int stderrfd, int escape)
374{
375 return lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape);
376}
377
72d0e1cb
SG
378static pid_t lxcapi_init_pid(struct lxc_container *c)
379{
72d0e1cb
SG
380 if (!c)
381 return -1;
382
5cee8c50 383 return lxc_cmd_get_init_pid(c->name, c->config_path);
72d0e1cb
SG
384}
385
12a50cc6 386static bool load_config_locked(struct lxc_container *c, const char *fname)
8eb5694b
SH
387{
388 if (!c->lxc_conf)
389 c->lxc_conf = lxc_conf_init();
390 if (c->lxc_conf && !lxc_config_read(fname, c->lxc_conf))
391 return true;
392 return false;
393}
394
12a50cc6 395static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
72d0e1cb 396{
39dc698c
SH
397 bool ret = false, need_disklock = false;
398 int lret;
12a50cc6 399 const char *fname;
72d0e1cb
SG
400 if (!c)
401 return false;
402
403 fname = c->configfile;
404 if (alt_file)
405 fname = alt_file;
406 if (!fname)
407 return false;
39dc698c
SH
408 /*
409 * If we're reading something other than the container's config,
410 * we only need to lock the in-memory container. If loading the
411 * container's config file, take the disk lock.
412 */
413 if (strcmp(fname, c->configfile) == 0)
414 need_disklock = true;
415
416 if (need_disklock)
417 lret = container_disk_lock(c);
418 else
419 lret = container_mem_lock(c);
420 if (lret)
72d0e1cb 421 return false;
39dc698c 422
8eb5694b 423 ret = load_config_locked(c, fname);
39dc698c
SH
424
425 if (need_disklock)
426 container_disk_unlock(c);
427 else
428 container_mem_unlock(c);
72d0e1cb
SG
429 return ret;
430}
431
432static void lxcapi_want_daemonize(struct lxc_container *c)
433{
434 if (!c)
435 return;
f02abefe 436 if (container_mem_lock(c)) {
3bc449ed
SH
437 ERROR("Error getting mem lock");
438 return;
439 }
72d0e1cb 440 c->daemonize = 1;
3bc449ed 441 container_mem_unlock(c);
72d0e1cb
SG
442}
443
12a50cc6 444static bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
7a44c8b4
SG
445{
446 int ret;
447
448 if (!c)
449 return false;
450
67e571de 451 ret = lxc_wait(c->name, state, timeout, c->config_path);
7a44c8b4
SG
452 return ret == 0;
453}
454
455
456static bool wait_on_daemonized_start(struct lxc_container *c)
457{
458 /* we'll probably want to make this timeout configurable? */
697fa639 459 int timeout = 5, ret, status;
7a44c8b4 460
697fa639
SH
461 /*
462 * our child is going to fork again, then exit. reap the
463 * child
464 */
465 ret = wait(&status);
466 if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
467 DEBUG("failed waiting for first dual-fork child");
7a44c8b4
SG
468 return lxcapi_wait(c, "RUNNING", timeout);
469}
470
72d0e1cb
SG
471/*
472 * I can't decide if it'd be more convenient for callers if we accept '...',
473 * or a null-terminated array (i.e. execl vs execv)
474 */
12a50cc6 475static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
72d0e1cb
SG
476{
477 int ret;
478 struct lxc_conf *conf;
479 int daemonize = 0;
480 char *default_args[] = {
481 "/sbin/init",
482 '\0',
483 };
484
485 /* container exists */
486 if (!c)
487 return false;
488 /* container has been setup */
489 if (!c->lxc_conf)
490 return false;
491
3e625e2d
SH
492 if ((ret = ongoing_create(c)) < 0) {
493 ERROR("Error checking for incomplete creation");
494 return false;
495 }
496 if (ret == 2) {
497 ERROR("Error: %s creation was not completed", c->name);
498 c->destroy(c);
499 return false;
500 } else if (ret == 1) {
501 ERROR("Error: creation of %s is ongoing", c->name);
502 return false;
503 }
504
72d0e1cb
SG
505 /* is this app meant to be run through lxcinit, as in lxc-execute? */
506 if (useinit && !argv)
507 return false;
508
5cee8c50 509 if (container_mem_lock(c))
72d0e1cb
SG
510 return false;
511 conf = c->lxc_conf;
512 daemonize = c->daemonize;
5cee8c50 513 container_mem_unlock(c);
72d0e1cb
SG
514
515 if (useinit) {
13f5be62 516 ret = lxc_execute(c->name, argv, 1, conf, c->config_path);
72d0e1cb
SG
517 return ret == 0 ? true : false;
518 }
519
520 if (!argv)
521 argv = default_args;
522
523 /*
524 * say, I'm not sure - what locks do we want here? Any?
525 * Is liblxc's locking enough here to protect the on disk
526 * container? We don't want to exclude things like lxc_info
527 * while container is running...
528 */
529 if (daemonize) {
530 if (!lxc_container_get(c))
531 return false;
e51d4895 532 lxc_monitord_spawn(c->config_path);
71454076 533
5cee8c50 534 if (process_lock())
71454076 535 return false;
72d0e1cb
SG
536 pid_t pid = fork();
537 if (pid < 0) {
538 lxc_container_put(c);
5cee8c50 539 process_unlock();
72d0e1cb
SG
540 return false;
541 }
71454076
DE
542 if (pid != 0) {
543 ret = wait_on_daemonized_start(c);
5cee8c50 544 process_unlock();
71454076
DE
545 return ret;
546 }
5cee8c50 547 process_unlock();
697fa639
SH
548 /* second fork to be reparented by init */
549 pid = fork();
550 if (pid < 0) {
551 SYSERROR("Error doing dual-fork");
552 return false;
553 }
554 if (pid != 0)
555 exit(0);
72d0e1cb 556 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
c278cef2
SH
557 if (chdir("/")) {
558 SYSERROR("Error chdir()ing to /.");
559 return false;
560 }
72d0e1cb
SG
561 close(0);
562 close(1);
563 close(2);
eddaaafd 564 open("/dev/zero", O_RDONLY);
72d0e1cb
SG
565 open("/dev/null", O_RDWR);
566 open("/dev/null", O_RDWR);
567 setsid();
568 }
569
72d0e1cb
SG
570reboot:
571 conf->reboot = 0;
13f5be62 572 ret = lxc_start(c->name, argv, conf, c->config_path);
72d0e1cb
SG
573
574 if (conf->reboot) {
575 INFO("container requested reboot");
576 conf->reboot = 0;
72d0e1cb
SG
577 goto reboot;
578 }
579
580 if (daemonize) {
581 lxc_container_put(c);
582 exit (ret == 0 ? true : false);
583 } else {
584 return (ret == 0 ? true : false);
585 }
586}
587
588/*
589 * note there MUST be an ending NULL
590 */
591static bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
592{
593 va_list ap;
a0e93eeb 594 char **inargs = NULL;
72d0e1cb
SG
595 bool bret = false;
596
597 /* container exists */
598 if (!c)
599 return false;
600
72d0e1cb 601 va_start(ap, useinit);
a0e93eeb 602 inargs = lxc_va_arg_list_to_argv(ap, 0, 1);
72d0e1cb
SG
603 va_end(ap);
604
a0e93eeb
CS
605 if (!inargs) {
606 ERROR("Memory allocation error.");
607 goto out;
72d0e1cb
SG
608 }
609
a0e93eeb
CS
610 /* pass NULL if no arguments were supplied */
611 bret = lxcapi_start(c, useinit, *inargs ? inargs : NULL);
72d0e1cb
SG
612
613out:
614 if (inargs) {
a0e93eeb
CS
615 char *arg;
616 for (arg = *inargs; arg; arg++)
617 free(arg);
72d0e1cb
SG
618 free(inargs);
619 }
620
621 return bret;
622}
623
624static bool lxcapi_stop(struct lxc_container *c)
625{
626 int ret;
627
628 if (!c)
629 return false;
630
ef6e34ee 631 ret = lxc_cmd_stop(c->name, c->config_path);
72d0e1cb
SG
632
633 return ret == 0;
72d0e1cb
SG
634}
635
72d0e1cb
SG
636/*
637 * create the standard expected container dir
638 */
639static bool create_container_dir(struct lxc_container *c)
640{
641 char *s;
642 int len, ret;
643
2a59a681 644 len = strlen(c->config_path) + strlen(c->name) + 2;
72d0e1cb
SG
645 s = malloc(len);
646 if (!s)
647 return false;
2a59a681 648 ret = snprintf(s, len, "%s/%s", c->config_path, c->name);
72d0e1cb
SG
649 if (ret < 0 || ret >= len) {
650 free(s);
651 return false;
652 }
653 ret = mkdir(s, 0755);
654 if (ret) {
655 if (errno == EEXIST)
656 ret = 0;
657 else
658 SYSERROR("failed to create container path for %s\n", c->name);
659 }
660 free(s);
661 return ret == 0;
662}
663
1897e3bc
SH
664static const char *lxcapi_get_config_path(struct lxc_container *c);
665static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
666
72d0e1cb 667/*
1897e3bc
SH
668 * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
669 * it returns a mounted bdev on success, NULL on error.
72d0e1cb 670 */
1897e3bc
SH
671static struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
672 struct bdev_specs *specs)
673{
674 char *dest;
675 const char *lxcpath = lxcapi_get_config_path(c);
676 size_t len;
677 struct bdev *bdev;
678 int ret;
679
680 /* lxcpath/lxcname/rootfs */
681 len = strlen(c->name) + strlen(lxcpath) + 9;
682 dest = alloca(len);
683 ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name);
684 if (ret < 0 || ret >= len)
685 return NULL;
686
687 bdev = bdev_create(dest, type, c->name, specs);
d44e88c2
SH
688 if (!bdev) {
689 ERROR("Failed to create backing store type %s\n", type);
1897e3bc 690 return NULL;
d44e88c2
SH
691 }
692
1897e3bc
SH
693 lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
694 return bdev;
695}
696
cbee8106
SH
697/*
698 * Given the '-t' template option to lxc-create, figure out what to
699 * do. If the template is a full executable path, use that. If it
700 * is something like 'sshd', then return $templatepath/lxc-sshd. If
701 * no template was passed in, return NULL (this is ok).
702 * On error return (char *) -1.
703 */
704char *get_template_path(const char *t)
705{
706 int ret, len;
707 char *tpath;
708
709 if (!t)
710 return NULL;
711
712 if (t[0] == '/' && access(t, X_OK) == 0) {
713 tpath = strdup(t);
714 if (!tpath)
715 return (char *) -1;
716 return tpath;
717 }
718
719 len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
720 tpath = malloc(len);
721 if (!tpath)
722 return (char *) -1;
723 ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
724 if (ret < 0 || ret >= len) {
725 free(tpath);
726 return (char *) -1;
727 }
728 if (access(tpath, X_OK) < 0) {
729 SYSERROR("bad template: %s\n", t);
730 free(tpath);
731 return (char *) -1;
732 }
733
734 return tpath;
735}
736
96b3cb40 737static char *lxcbasename(char *path)
72d0e1cb 738{
96b3cb40
SH
739 char *p = path + strlen(path) - 1;
740 while (*p != '/' && p > path)
741 p--;
742 return p;
743}
72d0e1cb 744
dc23c1c8 745static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
96b3cb40
SH
746 char *const argv[])
747{
748 pid_t pid;
72d0e1cb 749
72d0e1cb 750 if (!tpath)
96b3cb40 751 return true;
72d0e1cb
SG
752
753 pid = fork();
754 if (pid < 0) {
755 SYSERROR("failed to fork task for container creation template\n");
96b3cb40 756 return false;
72d0e1cb
SG
757 }
758
759 if (pid == 0) { // child
1897e3bc
SH
760 char *patharg, *namearg, *rootfsarg, *src;
761 struct bdev *bdev = NULL;
72d0e1cb 762 int i;
96b3cb40
SH
763 int ret, len, nargs = 0;
764 char **newargv;
72d0e1cb 765
dc23c1c8
SH
766 if (quiet) {
767 close(0);
768 close(1);
769 close(2);
770 open("/dev/zero", O_RDONLY);
771 open("/dev/null", O_RDWR);
772 open("/dev/null", O_RDWR);
773 }
1897e3bc
SH
774 if (unshare(CLONE_NEWNS) < 0) {
775 ERROR("error unsharing mounts");
776 exit(1);
777 }
778
779 src = c->lxc_conf->rootfs.path;
780 /*
781 * for an overlayfs create, what the user wants is the template to fill
782 * in what will become the readonly lower layer. So don't mount for
783 * the template
784 */
785 if (strncmp(src, "overlayfs:", 10) == 0) {
786 src = overlayfs_getlower(src+10);
787 }
788 bdev = bdev_init(src, c->lxc_conf->rootfs.mount, NULL);
789 if (!bdev) {
790 ERROR("Error opening rootfs");
791 exit(1);
792 }
793
794 if (bdev->ops->mount(bdev) < 0) {
795 ERROR("Error mounting rootfs");
796 exit(1);
797 }
798
72d0e1cb
SG
799 /*
800 * create our new array, pre-pend the template name and
801 * base args
802 */
803 if (argv)
1897e3bc
SH
804 for (nargs = 0; argv[nargs]; nargs++) ;
805 nargs += 4; // template, path, rootfs and name args
72d0e1cb
SG
806 newargv = malloc(nargs * sizeof(*newargv));
807 if (!newargv)
808 exit(1);
96b3cb40 809 newargv[0] = lxcbasename(tpath);
72d0e1cb 810
2a59a681 811 len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
72d0e1cb
SG
812 patharg = malloc(len);
813 if (!patharg)
814 exit(1);
2a59a681 815 ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
72d0e1cb
SG
816 if (ret < 0 || ret >= len)
817 exit(1);
818 newargv[1] = patharg;
819 len = strlen("--name=") + strlen(c->name) + 1;
820 namearg = malloc(len);
821 if (!namearg)
822 exit(1);
823 ret = snprintf(namearg, len, "--name=%s", c->name);
824 if (ret < 0 || ret >= len)
825 exit(1);
826 newargv[2] = namearg;
827
1897e3bc
SH
828 len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
829 rootfsarg = malloc(len);
830 if (!rootfsarg)
831 exit(1);
832 ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
833 if (ret < 0 || ret >= len)
834 exit(1);
835 newargv[3] = rootfsarg;
836
72d0e1cb
SG
837 /* add passed-in args */
838 if (argv)
1897e3bc
SH
839 for (i = 4; i < nargs; i++)
840 newargv[i] = argv[i-4];
72d0e1cb
SG
841
842 /* add trailing NULL */
843 nargs++;
844 newargv = realloc(newargv, nargs * sizeof(*newargv));
845 if (!newargv)
846 exit(1);
847 newargv[nargs - 1] = NULL;
848
849 /* execute */
e6a19d26 850 execv(tpath, newargv);
72d0e1cb
SG
851 SYSERROR("failed to execute template %s", tpath);
852 exit(1);
853 }
854
9be53773
SH
855 if (wait_for_pid(pid) != 0) {
856 ERROR("container creation template for %s failed\n", c->name);
96b3cb40
SH
857 return false;
858 }
859
860 return true;
861}
862
3ce74686
SH
863bool prepend_lxc_header(char *path, const char *t, char *const argv[])
864{
1fd9bd50 865 long flen;
b4569e93 866 char *contents;
3ce74686 867 FILE *f;
52026772
SH
868#if HAVE_LIBGNUTLS
869 int i, ret;
3ce74686 870 unsigned char md_value[SHA_DIGEST_LENGTH];
b4569e93 871 char *tpath;
3ce74686 872 bool have_tpath = false;
52026772 873#endif
3ce74686
SH
874
875 if ((f = fopen(path, "r")) == NULL) {
876 SYSERROR("Opening old config");
877 return false;
878 }
879 if (fseek(f, 0, SEEK_END) < 0) {
880 SYSERROR("Seeking to end of old config file");
881 fclose(f);
882 return false;
883 }
884 if ((flen = ftell(f)) < 0) {
885 SYSERROR("telling size of old config");
886 fclose(f);
887 return false;
888 }
889 if (fseek(f, 0, SEEK_SET) < 0) {
890 SYSERROR("rewinding old config");
891 fclose(f);
892 return false;
893 }
894 if ((contents = malloc(flen + 1)) == NULL) {
895 SYSERROR("out of memory");
896 fclose(f);
897 return false;
898 }
899 if (fread(contents, 1, flen, f) != flen) {
900 SYSERROR("Reading old config");
901 free(contents);
902 fclose(f);
903 return false;
904 }
905 contents[flen] = '\0';
906 if (fclose(f) < 0) {
907 SYSERROR("closing old config");
908 free(contents);
909 return false;
910 }
911
b4569e93 912#if HAVE_LIBGNUTLS
01efd4d3
SH
913 tpath = get_template_path(t);
914 if (tpath == (char *) -1) {
3ce74686
SH
915 ERROR("bad template: %s\n", t);
916 free(contents);
917 return false;
918 }
919
3ce74686
SH
920 if (tpath) {
921 have_tpath = true;
922 ret = sha1sum_file(tpath, md_value);
923 if (ret < 0) {
924 ERROR("Error getting sha1sum of %s", tpath);
925 free(contents);
926 return false;
927 }
928 free(tpath);
929 }
930#endif
931
932 if ((f = fopen(path, "w")) == NULL) {
933 SYSERROR("reopening config for writing");
934 free(contents);
935 return false;
936 }
937 fprintf(f, "# Template used to create this container: %s\n", t);
938 if (argv) {
939 fprintf(f, "# Parameters passed to the template:");
940 while (*argv) {
941 fprintf(f, " %s", *argv);
942 argv++;
943 }
944 fprintf(f, "\n");
945 }
946#if HAVE_LIBGNUTLS
947 if (have_tpath) {
948 fprintf(f, "# Template script checksum (SHA-1): ");
949 for (i=0; i<SHA_DIGEST_LENGTH; i++)
950 fprintf(f, "%02x", md_value[i]);
951 fprintf(f, "\n");
952 }
953#endif
954 if (fwrite(contents, 1, flen, f) != flen) {
955 SYSERROR("Writing original contents");
956 free(contents);
957 fclose(f);
958 return false;
959 }
960 free(contents);
961 if (fclose(f) < 0) {
962 SYSERROR("Closing config file after write");
963 return false;
964 }
965 return true;
966}
967
96b3cb40
SH
968static bool lxcapi_destroy(struct lxc_container *c);
969/*
970 * lxcapi_create:
971 * create a container with the given parameters.
972 * @c: container to be created. It has the lxcpath, name, and a starting
973 * configuration already set
974 * @t: the template to execute to instantiate the root filesystem and
975 * adjust the configuration.
976 * @bdevtype: backing store type to use. If NULL, dir will be used.
977 * @specs: additional parameters for the backing store, i.e. LVM vg to
978 * use.
979 *
980 * @argv: the arguments to pass to the template, terminated by NULL. If no
981 * arguments, you can just pass NULL.
982 */
983static bool lxcapi_create(struct lxc_container *c, const char *t,
dc23c1c8 984 const char *bdevtype, struct bdev_specs *specs, int flags,
96b3cb40
SH
985 char *const argv[])
986{
987 bool bret = false;
988 pid_t pid;
cbee8106
SH
989 char *tpath;
990 int partial_fd;
96b3cb40
SH
991
992 if (!c)
993 return false;
994
01efd4d3
SH
995 tpath = get_template_path(t);
996 if (tpath == (char *) -1) {
96b3cb40
SH
997 ERROR("bad template: %s\n", t);
998 goto out;
999 }
1000
1001 if (!c->save_config(c, NULL)) {
1002 ERROR("failed to save starting configuration for %s\n", c->name);
1003 goto out;
1004 }
1005
1006 /* container is already created if we have a config and rootfs.path is accessible */
1007 if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
1008 goto out;
1009
1010 /* Mark that this container is being created */
1011 if ((partial_fd = create_partial(c)) < 0)
1012 goto out;
1013
1014 /* no need to get disk lock bc we have the partial locked */
1015
1016 /*
1017 * Create the backing store
1018 * Note we can't do this in the same task as we use to execute the
1019 * template because of the way zfs works.
1020 * After you 'zfs create', zfs mounts the fs only in the initial
1021 * namespace.
1022 */
1023 pid = fork();
1024 if (pid < 0) {
1025 SYSERROR("failed to fork task for container creation template\n");
8eb5694b
SH
1026 goto out_unlock;
1027 }
1028
96b3cb40
SH
1029 if (pid == 0) { // child
1030 struct bdev *bdev = NULL;
1031
1032 if (!(bdev = do_bdev_create(c, bdevtype, specs))) {
1033 ERROR("Error creating backing store type %s for %s",
1034 bdevtype ? bdevtype : "(none)", c->name);
1035 exit(1);
1036 }
1037
1038 /* save config file again to store the new rootfs location */
1039 if (!c->save_config(c, NULL)) {
1040 ERROR("failed to save starting configuration for %s\n", c->name);
1041 // parent task won't see bdev in config so we delete it
1042 bdev->ops->umount(bdev);
1043 bdev->ops->destroy(bdev);
1044 exit(1);
1045 }
1046 exit(0);
1047 }
1048 if (wait_for_pid(pid) != 0)
a09295f8 1049 goto out_unlock;
96b3cb40
SH
1050
1051 /* reload config to get the rootfs */
1052 if (c->lxc_conf)
1053 lxc_conf_free(c->lxc_conf);
1054 c->lxc_conf = NULL;
1055 if (!load_config_locked(c, c->configfile))
a09295f8 1056 goto out_unlock;
96b3cb40 1057
dc23c1c8 1058 if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv))
96b3cb40
SH
1059 goto out_unlock;
1060
8eb5694b
SH
1061 // now clear out the lxc_conf we have, reload from the created
1062 // container
1063 if (c->lxc_conf)
1064 lxc_conf_free(c->lxc_conf);
1065 c->lxc_conf = NULL;
3ce74686
SH
1066
1067 if (!prepend_lxc_header(c->configfile, tpath, argv)) {
1068 ERROR("Error prepending header to configuration file");
1069 goto out_unlock;
1070 }
8eb5694b 1071 bret = load_config_locked(c, c->configfile);
72d0e1cb
SG
1072
1073out_unlock:
3e625e2d
SH
1074 if (partial_fd >= 0)
1075 remove_partial(c, partial_fd);
72d0e1cb
SG
1076out:
1077 if (tpath)
1078 free(tpath);
1897e3bc
SH
1079 if (!bret && c)
1080 lxcapi_destroy(c);
72d0e1cb
SG
1081 return bret;
1082}
1083
3e625e2d
SH
1084static bool lxcapi_reboot(struct lxc_container *c)
1085{
1086 pid_t pid;
1087
1088 if (!c)
1089 return false;
1090 if (!c->is_running(c))
1091 return false;
1092 pid = c->init_pid(c);
1093 if (pid <= 0)
1094 return false;
1095 if (kill(pid, SIGINT) < 0)
1096 return false;
1097 return true;
1098
1099}
1100
72d0e1cb
SG
1101static bool lxcapi_shutdown(struct lxc_container *c, int timeout)
1102{
1103 bool retv;
1104 pid_t pid;
1105
1106 if (!c)
1107 return false;
1108
1109 if (!timeout)
1110 timeout = -1;
1111 if (!c->is_running(c))
1112 return true;
1113 pid = c->init_pid(c);
1114 if (pid <= 0)
1115 return true;
1116 kill(pid, SIGPWR);
1117 retv = c->wait(c, "STOPPED", timeout);
f6144ed4 1118 if (!retv && timeout > 0) {
72d0e1cb
SG
1119 c->stop(c);
1120 retv = c->wait(c, "STOPPED", 0); // 0 means don't wait
1121 }
1122 return retv;
1123}
1124
1897e3bc 1125static bool lxcapi_createl(struct lxc_container *c, const char *t,
dc23c1c8 1126 const char *bdevtype, struct bdev_specs *specs, int flags, ...)
72d0e1cb
SG
1127{
1128 bool bret = false;
a0e93eeb 1129 char **args = NULL;
72d0e1cb 1130 va_list ap;
72d0e1cb
SG
1131
1132 if (!c)
1133 return false;
1134
1135 /*
1136 * since we're going to wait for create to finish, I don't think we
1137 * need to get a copy of the arguments.
1138 */
dc23c1c8 1139 va_start(ap, flags);
a0e93eeb 1140 args = lxc_va_arg_list_to_argv(ap, 0, 0);
72d0e1cb 1141 va_end(ap);
a0e93eeb
CS
1142 if (!args) {
1143 ERROR("Memory allocation error.");
1144 goto out;
1145 }
72d0e1cb 1146
dc23c1c8 1147 bret = c->create(c, t, bdevtype, specs, flags, args);
72d0e1cb
SG
1148
1149out:
a0e93eeb 1150 free(args);
72d0e1cb
SG
1151 return bret;
1152}
1153
12a50cc6 1154static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
72d0e1cb
SG
1155{
1156 int ret;
1157
1158 if (!c || !c->lxc_conf)
1159 return false;
5cee8c50 1160 if (container_mem_lock(c))
72d0e1cb 1161 return false;
72d0e1cb 1162 ret = lxc_clear_config_item(c->lxc_conf, key);
5cee8c50 1163 container_mem_unlock(c);
72d0e1cb
SG
1164 return ret == 0;
1165}
1166
9c83a661
SG
1167char** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, int scope)
1168{
1169 int count = 0;
1170 struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
1171 char addressOutputBuffer[INET6_ADDRSTRLEN];
1172 void *tempAddrPtr = NULL;
1173 char **addresses = NULL, **temp;
1174 char *address = NULL;
1175 char new_netns_path[MAXPATHLEN];
1176 int old_netns = -1, new_netns = -1, ret = 0;
1177
1178 if (!c->is_running(c))
1179 goto out;
1180
1181 /* Save reference to old netns */
1182 old_netns = open("/proc/self/ns/net", O_RDONLY);
1183 if (old_netns < 0) {
1184 SYSERROR("failed to open /proc/self/ns/net");
1185 goto out;
1186 }
1187
1188 /* Switch to new netns */
1189 ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", c->init_pid(c));
1190 if (ret < 0 || ret >= MAXPATHLEN)
1191 goto out;
1192
1193 new_netns = open(new_netns_path, O_RDONLY);
1194 if (new_netns < 0) {
1195 SYSERROR("failed to open %s", new_netns_path);
1196 goto out;
1197 }
1198
1199 if (setns(new_netns, CLONE_NEWNET)) {
1200 SYSERROR("failed to setns");
1201 goto out;
1202 }
1203
1204 /* Grab the list of interfaces */
1205 if (getifaddrs(&interfaceArray)) {
1206 SYSERROR("failed to get interfaces list");
1207 goto out;
1208 }
1209
1210 /* Iterate through the interfaces */
1211 for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
1212 if(tempIfAddr->ifa_addr->sa_family == AF_INET) {
1213 if (family && strcmp(family, "inet"))
1214 continue;
1215 tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr;
1216 }
1217 else {
1218 if (family && strcmp(family, "inet6"))
1219 continue;
1220
1221 if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope)
1222 continue;
1223
1224 tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr;
1225 }
1226
1227 if (interface && strcmp(interface, tempIfAddr->ifa_name))
1228 continue;
1229 else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)
1230 continue;
1231
1232 address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,
1233 tempAddrPtr,
1234 addressOutputBuffer,
1235 sizeof(addressOutputBuffer));
1236 if (!address)
1237 continue;
1238
1239 count += 1;
1240 temp = realloc(addresses, count * sizeof(*addresses));
1241 if (!temp) {
1242 count--;
1243 goto out;
1244 }
1245 addresses = temp;
1246 addresses[count - 1] = strdup(address);
1247 }
1248
1249out:
1250 if(interfaceArray)
1251 freeifaddrs(interfaceArray);
1252
1253 /* Switch back to original netns */
1254 if (old_netns >= 0 && setns(old_netns, CLONE_NEWNET))
1255 SYSERROR("failed to setns");
1256 if (new_netns >= 0)
1257 close(new_netns);
1258 if (old_netns >= 0)
1259 close(old_netns);
1260
1261 /* Append NULL to the array */
1262 if (count) {
1263 count++;
1264 temp = realloc(addresses, count * sizeof(*addresses));
1265 if (!temp) {
fca3080f
DE
1266 int i;
1267 for (i = 0; i < count-1; i++)
9c83a661
SG
1268 free(addresses[i]);
1269 free(addresses);
1270 return NULL;
1271 }
1272 addresses = temp;
1273 addresses[count - 1] = NULL;
1274 }
1275
1276 return addresses;
1277}
1278
12a50cc6 1279static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
72d0e1cb
SG
1280{
1281 int ret;
1282
1283 if (!c || !c->lxc_conf)
1284 return -1;
5cee8c50 1285 if (container_mem_lock(c))
72d0e1cb 1286 return -1;
72d0e1cb 1287 ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen);
5cee8c50 1288 container_mem_unlock(c);
72d0e1cb
SG
1289 return ret;
1290}
1291
12a50cc6 1292static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
72d0e1cb
SG
1293{
1294 if (!key)
1295 return lxc_listconfigs(retv, inlen);
1296 /*
1297 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
1298 * This is an intelligent result to show which keys are valid given
1299 * the type of nic it is
1300 */
1301 if (!c || !c->lxc_conf)
1302 return -1;
5cee8c50 1303 if (container_mem_lock(c))
72d0e1cb
SG
1304 return -1;
1305 int ret = -1;
1306 if (strncmp(key, "lxc.network.", 12) == 0)
1307 ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen);
5cee8c50 1308 container_mem_unlock(c);
72d0e1cb
SG
1309 return ret;
1310}
1311
12a50cc6 1312static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
72d0e1cb 1313{
39dc698c
SH
1314 FILE *fout;
1315 bool ret = false, need_disklock = false;
1316 int lret;
1317
72d0e1cb
SG
1318 if (!alt_file)
1319 alt_file = c->configfile;
1320 if (!alt_file)
1321 return false; // should we write to stdout if no file is specified?
39dc698c
SH
1322
1323 // If we haven't yet loaded a config, load the stock config
1324 if (!c->lxc_conf) {
72d0e1cb
SG
1325 if (!c->load_config(c, LXC_DEFAULT_CONFIG)) {
1326 ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG, c->name);
1327 return false;
1328 }
39dc698c 1329 }
72d0e1cb 1330
5a3d2e1e
SG
1331 if (!create_container_dir(c))
1332 return false;
1333
39dc698c
SH
1334 /*
1335 * If we're writing to the container's config file, take the
1336 * disk lock. Otherwise just take the memlock to protect the
1337 * struct lxc_container while we're traversing it.
1338 */
1339 if (strcmp(c->configfile, alt_file) == 0)
1340 need_disklock = true;
1341
1342 if (need_disklock)
1343 lret = container_disk_lock(c);
1344 else
1345 lret = container_mem_lock(c);
1346
1347 if (lret)
72d0e1cb 1348 return false;
39dc698c
SH
1349
1350 fout = fopen(alt_file, "w");
1351 if (!fout)
1352 goto out;
72d0e1cb
SG
1353 write_config(fout, c->lxc_conf);
1354 fclose(fout);
39dc698c
SH
1355 ret = true;
1356
1357out:
1358 if (need_disklock)
1359 container_disk_unlock(c);
1360 else
1361 container_mem_unlock(c);
1362 return ret;
72d0e1cb
SG
1363}
1364
60bf62d4 1365// do we want the api to support --force, or leave that to the caller?
72d0e1cb
SG
1366static bool lxcapi_destroy(struct lxc_container *c)
1367{
1897e3bc 1368 struct bdev *r = NULL;
60bf62d4 1369 bool ret = false;
72d0e1cb 1370
1897e3bc 1371 if (!c || !lxcapi_is_defined(c))
5a3d2e1e
SG
1372 return false;
1373
3bc449ed 1374 if (container_disk_lock(c))
72d0e1cb 1375 return false;
72d0e1cb 1376
39dc698c 1377 if (!is_stopped(c)) {
60bf62d4
SH
1378 // we should queue some sort of error - in c->error_string?
1379 ERROR("container %s is not stopped", c->name);
1380 goto out;
72d0e1cb
SG
1381 }
1382
3bc449ed 1383 if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount)
1897e3bc 1384 r = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
60bf62d4
SH
1385 if (r) {
1386 if (r->ops->destroy(r) < 0) {
1387 ERROR("Error destroying rootfs for %s", c->name);
1388 goto out;
1389 }
1390 }
1391
1392 const char *p1 = lxcapi_get_config_path(c);
1393 char *path = alloca(strlen(p1) + strlen(c->name) + 2);
1394 sprintf(path, "%s/%s", p1, c->name);
1395 if (lxc_rmdir_onedev(path) < 0) {
1396 ERROR("Error destroying container directory for %s", c->name);
1397 goto out;
1398 }
1399 ret = true;
1400
1401out:
3bc449ed 1402 container_disk_unlock(c);
60bf62d4 1403 return ret;
72d0e1cb
SG
1404}
1405
96532523
SH
1406static bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v)
1407{
1408 struct lxc_config_t *config;
1409
1410 if (!c->lxc_conf)
1411 c->lxc_conf = lxc_conf_init();
1412 if (!c->lxc_conf)
1413 return false;
1414 config = lxc_getconfig(key);
1415 if (!config)
1416 return false;
1417 return (0 == config->cb(key, v, c->lxc_conf));
1418}
1419
12a50cc6 1420static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
72d0e1cb 1421{
72d0e1cb 1422 bool b = false;
72d0e1cb
SG
1423
1424 if (!c)
1425 return false;
1426
5cee8c50 1427 if (container_mem_lock(c))
72d0e1cb
SG
1428 return false;
1429
96532523 1430 b = set_config_item_locked(c, key, v);
72d0e1cb 1431
5cee8c50 1432 container_mem_unlock(c);
72d0e1cb
SG
1433 return b;
1434}
1435
1436static char *lxcapi_config_file_name(struct lxc_container *c)
1437{
1438 if (!c || !c->configfile)
1439 return NULL;
1440 return strdup(c->configfile);
1441}
1442
2a59a681
SH
1443static const char *lxcapi_get_config_path(struct lxc_container *c)
1444{
1445 if (!c || !c->config_path)
1446 return NULL;
1447 return (const char *)(c->config_path);
1448}
1449
afeecbba
SH
1450/*
1451 * not for export
1452 * Just recalculate the c->configfile based on the
1453 * c->config_path, which must be set.
1454 * The lxc_container must be locked or not yet public.
1455 */
1456static bool set_config_filename(struct lxc_container *c)
1457{
1458 char *newpath;
1459 int len, ret;
1460
1461 if (!c->config_path)
1462 return false;
1463
1464 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
1465 len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3;
1466 newpath = malloc(len);
1467 if (!newpath)
1468 return false;
1469
1470 ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name);
1471 if (ret < 0 || ret >= len) {
1472 fprintf(stderr, "Error printing out config file name\n");
1473 free(newpath);
1474 return false;
1475 }
1476
1477 if (c->configfile)
1478 free(c->configfile);
1479 c->configfile = newpath;
1480
1481 return true;
1482}
1483
2a59a681
SH
1484static bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
1485{
1486 char *p;
1487 bool b = false;
afeecbba 1488 char *oldpath = NULL;
2a59a681
SH
1489
1490 if (!c)
1491 return b;
1492
5cee8c50 1493 if (container_mem_lock(c))
2a59a681
SH
1494 return b;
1495
1496 p = strdup(path);
afeecbba
SH
1497 if (!p) {
1498 ERROR("Out of memory setting new lxc path");
2a59a681 1499 goto err;
afeecbba
SH
1500 }
1501
2a59a681
SH
1502 b = true;
1503 if (c->config_path)
afeecbba 1504 oldpath = c->config_path;
2a59a681 1505 c->config_path = p;
afeecbba
SH
1506
1507 /* Since we've changed the config path, we have to change the
1508 * config file name too */
1509 if (!set_config_filename(c)) {
1510 ERROR("Out of memory setting new config filename");
1511 b = false;
1512 free(c->config_path);
1513 c->config_path = oldpath;
1514 oldpath = NULL;
1515 }
2a59a681 1516err:
afeecbba
SH
1517 if (oldpath)
1518 free(oldpath);
5cee8c50 1519 container_mem_unlock(c);
2a59a681
SH
1520 return b;
1521}
1522
1523
794dd120
SH
1524static bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
1525{
1526 int ret;
794dd120
SH
1527
1528 if (!c)
1529 return false;
1530
3bc449ed 1531 if (is_stopped(c))
794dd120
SH
1532 return false;
1533
3bc449ed
SH
1534 if (container_disk_lock(c))
1535 return false;
794dd120 1536
ef091cef 1537 ret = lxc_cgroup_set(c->name, subsys, value, c->config_path);
3bc449ed
SH
1538
1539 container_disk_unlock(c);
1540 return ret == 0;
794dd120
SH
1541}
1542
1543static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
1544{
3bc449ed 1545 int ret;
794dd120
SH
1546
1547 if (!c || !c->lxc_conf)
1548 return -1;
1549
3bc449ed 1550 if (is_stopped(c))
794dd120
SH
1551 return -1;
1552
3bc449ed
SH
1553 if (container_disk_lock(c))
1554 return -1;
794dd120 1555
ae5c8b8e 1556 ret = lxc_cgroup_get(c->name, subsys, retv, inlen, c->config_path);
794dd120 1557
3bc449ed 1558 container_disk_unlock(c);
794dd120
SH
1559 return ret;
1560}
1561
67e571de 1562const char *lxc_get_default_config_path(void)
83c98d82
DE
1563{
1564 return default_lxc_path();
1565}
794dd120 1566
a8428dfa
SH
1567const char *lxc_get_default_lvm_vg(void)
1568{
1569 return default_lvm_vg();
1570}
1571
1572const char *lxc_get_default_zfs_root(void)
1573{
1574 return default_zfs_root();
1575}
1576
b6b918a1
SG
1577const char *lxc_get_version(void)
1578{
1579 return lxc_version();
1580}
1581
9be53773
SH
1582static int copy_file(char *old, char *new)
1583{
1584 int in, out;
1585 ssize_t len, ret;
1586 char buf[8096];
1587 struct stat sbuf;
1588
1589 if (file_exists(new)) {
1590 ERROR("copy destination %s exists", new);
1591 return -1;
1592 }
1593 ret = stat(old, &sbuf);
1594 if (ret < 0) {
1595 SYSERROR("stat'ing %s", old);
1596 return -1;
1597 }
1598
1599 in = open(old, O_RDONLY);
1600 if (in < 0) {
1601 SYSERROR("opening original file %s", old);
1602 return -1;
1603 }
1604 out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
1605 if (out < 0) {
1606 SYSERROR("opening new file %s", new);
1607 close(in);
1608 return -1;
1609 }
1610
1611 while (1) {
1612 len = read(in, buf, 8096);
1613 if (len < 0) {
1614 SYSERROR("reading old file %s", old);
1615 goto err;
1616 }
1617 if (len == 0)
1618 break;
1619 ret = write(out, buf, len);
1620 if (ret < len) { // should we retry?
1621 SYSERROR("write to new file %s was interrupted", new);
1622 goto err;
1623 }
1624 }
1625 close(in);
1626 close(out);
1627
1628 // we set mode, but not owner/group
1629 ret = chmod(new, sbuf.st_mode);
1630 if (ret) {
1631 SYSERROR("setting mode on %s", new);
1632 return -1;
1633 }
1634
1635 return 0;
1636
1637err:
1638 close(in);
1639 close(out);
1640 return -1;
1641}
1642
9be53773
SH
1643static int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
1644{
1645 int i;
1646 int ret;
1647 struct lxc_list *it;
1648
1649 for (i=0; i<NUM_LXC_HOOKS; i++) {
1650 lxc_list_for_each(it, &c->lxc_conf->hooks[i]) {
1651 char *hookname = it->elem;
c32981c3 1652 char *fname = strrchr(hookname, '/');
9be53773
SH
1653 char tmppath[MAXPATHLEN];
1654 if (!fname) // relative path - we don't support, but maybe we should
1655 return 0;
1656 // copy the script, and change the entry in confile
1657 ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s",
1658 c->config_path, c->name, fname+1);
1659 if (ret < 0 || ret >= MAXPATHLEN)
1660 return -1;
1661 ret = copy_file(it->elem, tmppath);
1662 if (ret < 0)
1663 return -1;
1664 free(it->elem);
1665 it->elem = strdup(tmppath);
1666 if (!it->elem) {
1667 ERROR("out of memory copying hook path");
1668 return -1;
1669 }
9be53773
SH
1670 }
1671 }
1672
1673 c->save_config(c, NULL);
1674 return 0;
1675}
1676
1677static void new_hwaddr(char *hwaddr)
1678{
1679 FILE *f = fopen("/dev/urandom", "r");
1680 if (f) {
1681 unsigned int seed;
1682 int ret = fread(&seed, sizeof(seed), 1, f);
1683 if (ret != 1)
1684 seed = time(NULL);
1685 fclose(f);
1686 srand(seed);
1687 } else
1688 srand(time(NULL));
1689 snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
1690 rand() % 255, rand() % 255, rand() % 255);
1691}
1692
1693static void network_new_hwaddrs(struct lxc_container *c)
1694{
1695 struct lxc_list *it;
1696
1697 lxc_list_for_each(it, &c->lxc_conf->network) {
1698 struct lxc_netdev *n = it->elem;
1699 if (n->hwaddr)
1700 new_hwaddr(n->hwaddr);
1701 }
1702}
1703
1704static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
1705{
1706 char newpath[MAXPATHLEN];
1707 char *oldpath = oldc->lxc_conf->fstab;
1708 int ret;
1709
1710 if (!oldpath)
1711 return 0;
1712
c32981c3 1713 char *p = strrchr(oldpath, '/');
9be53773
SH
1714 if (!p)
1715 return -1;
1716 ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s",
1717 c->config_path, c->name, p);
1718 if (ret < 0 || ret >= MAXPATHLEN) {
1719 ERROR("error printing new path for %s", oldpath);
1720 return -1;
1721 }
1722 if (file_exists(newpath)) {
1723 ERROR("error: fstab file %s exists", newpath);
1724 return -1;
1725 }
1726
1727 if (copy_file(oldpath, newpath) < 0) {
1728 ERROR("error: copying %s to %s", oldpath, newpath);
1729 return -1;
1730 }
1731 free(c->lxc_conf->fstab);
1732 c->lxc_conf->fstab = strdup(newpath);
1733 if (!c->lxc_conf->fstab) {
1734 ERROR("error: allocating pathname");
1735 return -1;
1736 }
1737
1738 return 0;
1739}
1740
1741static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
1742 const char *newtype, int flags, const char *bdevdata, unsigned long newsize)
1743{
1744 struct bdev *bdev;
1745
1746 bdev = bdev_copy(c0->lxc_conf->rootfs.path, c0->name, c->name,
1747 c0->config_path, c->config_path, newtype, !!(flags & LXC_CLONE_SNAPSHOT),
1748 bdevdata, newsize);
1749 if (!bdev) {
1750 ERROR("error copying storage");
1751 return -1;
1752 }
1753 free(c->lxc_conf->rootfs.path);
1754 c->lxc_conf->rootfs.path = strdup(bdev->src);
1755 bdev_put(bdev);
1756 if (!c->lxc_conf->rootfs.path)
1757 return -1;
1758 // here we could also update all lxc.mount.entries or even
1759 // items in the lxc.mount fstab list. As discussed on m-l,
1760 // we could do either any source paths starting with the
1761 // lxcpath/oldname, or simply anythign which is not a virtual
1762 // fs or a bind mount.
1763 return 0;
1764}
1765
1143ed39
DE
1766static int clone_update_rootfs(struct lxc_container *c0,
1767 struct lxc_container *c, int flags,
1768 char **hookargs)
9be53773
SH
1769{
1770 int ret = -1;
1771 char path[MAXPATHLEN];
1772 struct bdev *bdev;
1773 FILE *fout;
1774 pid_t pid;
148e91f5 1775 struct lxc_conf *conf = c->lxc_conf;
9be53773
SH
1776
1777 /* update hostname in rootfs */
1778 /* we're going to mount, so run in a clean namespace to simplify cleanup */
1779
1780 pid = fork();
1781 if (pid < 0)
1782 return -1;
1783 if (pid > 0)
1784 return wait_for_pid(pid);
1785
1786 if (unshare(CLONE_NEWNS) < 0) {
1787 ERROR("error unsharing mounts");
1788 exit(1);
1789 }
1790 bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
1791 if (!bdev)
1792 exit(1);
1793 if (bdev->ops->mount(bdev) < 0)
1794 exit(1);
148e91f5
SH
1795
1796 if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
1797 /* Start of environment variable setup for hooks */
1143ed39
DE
1798 if (setenv("LXC_SRC_NAME", c0->name, 1)) {
1799 SYSERROR("failed to set environment variable for source container name");
1800 }
148e91f5
SH
1801 if (setenv("LXC_NAME", c->name, 1)) {
1802 SYSERROR("failed to set environment variable for container name");
1803 }
1804 if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
1805 SYSERROR("failed to set environment variable for config path");
1806 }
1807 if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
1808 SYSERROR("failed to set environment variable for rootfs mount");
1809 }
1810 if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
1811 SYSERROR("failed to set environment variable for rootfs mount");
1812 }
1813
283678ed 1814 if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) {
148e91f5
SH
1815 ERROR("Error executing clone hook for %s", c->name);
1816 exit(1);
1817 }
1818 }
1819
1820 if (!(flags & LXC_CLONE_KEEPNAME)) {
1821 ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest);
1822 if (ret < 0 || ret >= MAXPATHLEN)
1823 exit(1);
8058be39
DE
1824 if (!file_exists(path))
1825 exit(0);
148e91f5
SH
1826 if (!(fout = fopen(path, "w"))) {
1827 SYSERROR("unable to open %s: ignoring\n", path);
1828 exit(0);
1829 }
1830 if (fprintf(fout, "%s", c->name) < 0)
1831 exit(1);
1832 if (fclose(fout) < 0)
1833 exit(1);
9be53773 1834 }
9be53773
SH
1835 exit(0);
1836}
1837
1838/*
1839 * We want to support:
1840sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
1841 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
1842
1843-s [ implies overlayfs]
1844-s -B overlayfs
1845-s -B aufs
1846
1847only rootfs gets converted (copied/snapshotted) on clone.
1848*/
1849
1850static int create_file_dirname(char *path)
1851{
c32981c3 1852 char *p = strrchr(path, '/');
9be53773
SH
1853 int ret;
1854
1855 if (!p)
1856 return -1;
1857 *p = '\0';
1858 ret = mkdir(path, 0755);
1859 if (ret && errno != EEXIST)
1860 SYSERROR("creating container path %s\n", path);
1861 *p = '/';
1862 return ret;
1863}
1864
1865struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
1866 const char *lxcpath, int flags,
148e91f5
SH
1867 const char *bdevtype, const char *bdevdata, unsigned long newsize,
1868 char **hookargs)
9be53773
SH
1869{
1870 struct lxc_container *c2 = NULL;
1871 char newpath[MAXPATHLEN];
176d9acb 1872 int ret, storage_copied = 0;
9be53773
SH
1873 const char *n, *l;
1874 FILE *fout;
1875
1876 if (!c || !c->is_defined(c))
1877 return NULL;
1878
5cee8c50 1879 if (container_mem_lock(c))
9be53773
SH
1880 return NULL;
1881
39dc698c 1882 if (!is_stopped(c)) {
9be53773
SH
1883 ERROR("error: Original container (%s) is running", c->name);
1884 goto out;
1885 }
1886
1887 // Make sure the container doesn't yet exist.
1888 n = newname ? newname : c->name;
1889 l = lxcpath ? lxcpath : c->get_config_path(c);
1890 ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", l, n);
1891 if (ret < 0 || ret >= MAXPATHLEN) {
1892 SYSERROR("clone: failed making config pathname");
1893 goto out;
1894 }
1895 if (file_exists(newpath)) {
1896 ERROR("error: clone: %s exists", newpath);
1897 goto out;
1898 }
1899
96532523
SH
1900 ret = create_file_dirname(newpath);
1901 if (ret < 0 && errno != EEXIST) {
9be53773
SH
1902 ERROR("Error creating container dir for %s", newpath);
1903 goto out;
1904 }
1905
1906 // copy the configuration, tweak it as needed,
1907 fout = fopen(newpath, "w");
1908 if (!fout) {
1909 SYSERROR("open %s", newpath);
1910 goto out;
1911 }
1912 write_config(fout, c->lxc_conf);
1913 fclose(fout);
1914
9be53773
SH
1915 sprintf(newpath, "%s/%s/rootfs", l, n);
1916 if (mkdir(newpath, 0755) < 0) {
1917 SYSERROR("error creating %s", newpath);
1918 goto out;
1919 }
1920
1921 c2 = lxc_container_new(n, l);
375c2258 1922 if (!c2) {
9be53773
SH
1923 ERROR("clone: failed to create new container (%s %s)", n, l);
1924 goto out;
1925 }
1926
96532523
SH
1927 // update utsname
1928 if (!set_config_item_locked(c2, "lxc.utsname", newname)) {
1929 ERROR("Error setting new hostname");
1930 goto out;
1931 }
1932
1933
9be53773
SH
1934 // copy hooks if requested
1935 if (flags & LXC_CLONE_COPYHOOKS) {
1936 ret = copyhooks(c, c2);
1937 if (ret < 0) {
1938 ERROR("error copying hooks");
9be53773
SH
1939 goto out;
1940 }
1941 }
1942
1943 if (copy_fstab(c, c2) < 0) {
1944 ERROR("error copying fstab");
9be53773
SH
1945 goto out;
1946 }
1947
1948 // update macaddrs
1949 if (!(flags & LXC_CLONE_KEEPMACADDR))
1950 network_new_hwaddrs(c2);
1951
1952 // copy/snapshot rootfs's
1953 ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
375c2258 1954 if (ret < 0)
9be53773 1955 goto out;
9be53773 1956
176d9acb
SH
1957 // We've now successfully created c2's storage, so clear it out if we
1958 // fail after this
1959 storage_copied = 1;
1960
375c2258 1961 if (!c2->save_config(c2, NULL))
9be53773 1962 goto out;
9be53773 1963
1143ed39 1964 if (clone_update_rootfs(c, c2, flags, hookargs) < 0)
9be53773 1965 goto out;
9be53773
SH
1966
1967 // TODO: update c's lxc.snapshot = count
5cee8c50 1968 container_mem_unlock(c);
9be53773
SH
1969 return c2;
1970
1971out:
5cee8c50 1972 container_mem_unlock(c);
375c2258 1973 if (c2) {
176d9acb
SH
1974 if (!storage_copied)
1975 c2->lxc_conf->rootfs.path = NULL;
375c2258 1976 c2->destroy(c2);
9be53773 1977 lxc_container_put(c2);
375c2258 1978 }
9be53773
SH
1979
1980 return NULL;
1981}
1982
a0e93eeb
CS
1983static int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process)
1984{
1985 if (!c)
1986 return -1;
1987
1988 return lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process);
1989}
1990
1991static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[])
1992{
1993 lxc_attach_command_t command;
1994 pid_t pid;
1995 int r;
1996
1997 if (!c)
1998 return -1;
1999
2000 command.program = (char*)program;
2001 command.argv = (char**)argv;
2002 r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid);
2003 if (r < 0) {
2004 ERROR("ups");
2005 return r;
2006 }
2007 return lxc_wait_for_pid_status(pid);
2008}
2009
2010static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
2011{
2012 va_list ap;
2013 const char **argv;
2014 int ret;
2015
2016 if (!c)
2017 return -1;
2018
2019 va_start(ap, arg);
2020 argv = lxc_va_arg_list_to_argv_const(ap, 1);
2021 va_end(ap);
2022
2023 if (!argv) {
2024 ERROR("Memory allocation error.");
2025 return -1;
2026 }
2027 argv[0] = arg;
2028
2029 ret = lxcapi_attach_run_wait(c, options, program, (const char * const *)argv);
2030 free((void*)argv);
2031 return ret;
2032}
2033
afeecbba 2034struct lxc_container *lxc_container_new(const char *name, const char *configpath)
72d0e1cb
SG
2035{
2036 struct lxc_container *c;
72d0e1cb
SG
2037
2038 c = malloc(sizeof(*c));
2039 if (!c) {
2040 fprintf(stderr, "failed to malloc lxc_container\n");
2041 return NULL;
2042 }
2043 memset(c, 0, sizeof(*c));
2044
afeecbba
SH
2045 if (configpath)
2046 c->config_path = strdup(configpath);
2047 else
67e571de 2048 c->config_path = strdup(default_lxc_path());
afeecbba 2049
2a59a681
SH
2050 if (!c->config_path) {
2051 fprintf(stderr, "Out of memory");
2052 goto err;
2053 }
2054
72d0e1cb
SG
2055 c->name = malloc(strlen(name)+1);
2056 if (!c->name) {
2057 fprintf(stderr, "Error allocating lxc_container name\n");
2058 goto err;
2059 }
2060 strcpy(c->name, name);
2061
2062 c->numthreads = 1;
df271a59 2063 if (!(c->slock = lxc_newlock(c->config_path, name))) {
72d0e1cb
SG
2064 fprintf(stderr, "failed to create lock\n");
2065 goto err;
2066 }
2067
df271a59 2068 if (!(c->privlock = lxc_newlock(NULL, NULL))) {
72d0e1cb
SG
2069 fprintf(stderr, "failed to alloc privlock\n");
2070 goto err;
2071 }
2072
afeecbba 2073 if (!set_config_filename(c)) {
72d0e1cb
SG
2074 fprintf(stderr, "Error allocating config file pathname\n");
2075 goto err;
2076 }
72d0e1cb
SG
2077
2078 if (file_exists(c->configfile))
2079 lxcapi_load_config(c, NULL);
2080
3e625e2d
SH
2081 if (ongoing_create(c) == 2) {
2082 ERROR("Error: %s creation was not completed", c->name);
1897e3bc
SH
2083 lxcapi_destroy(c);
2084 lxc_conf_free(c->lxc_conf);
2085 c->lxc_conf = NULL;
3e625e2d
SH
2086 }
2087
72d0e1cb
SG
2088 // assign the member functions
2089 c->is_defined = lxcapi_is_defined;
2090 c->state = lxcapi_state;
2091 c->is_running = lxcapi_is_running;
2092 c->freeze = lxcapi_freeze;
2093 c->unfreeze = lxcapi_unfreeze;
0115f8fd 2094 c->console = lxcapi_console;
b5159817 2095 c->console_getfd = lxcapi_console_getfd;
72d0e1cb
SG
2096 c->init_pid = lxcapi_init_pid;
2097 c->load_config = lxcapi_load_config;
2098 c->want_daemonize = lxcapi_want_daemonize;
2099 c->start = lxcapi_start;
2100 c->startl = lxcapi_startl;
2101 c->stop = lxcapi_stop;
2102 c->config_file_name = lxcapi_config_file_name;
2103 c->wait = lxcapi_wait;
2104 c->set_config_item = lxcapi_set_config_item;
2105 c->destroy = lxcapi_destroy;
2106 c->save_config = lxcapi_save_config;
2107 c->get_keys = lxcapi_get_keys;
2108 c->create = lxcapi_create;
2109 c->createl = lxcapi_createl;
2110 c->shutdown = lxcapi_shutdown;
3e625e2d 2111 c->reboot = lxcapi_reboot;
72d0e1cb
SG
2112 c->clear_config_item = lxcapi_clear_config_item;
2113 c->get_config_item = lxcapi_get_config_item;
794dd120
SH
2114 c->get_cgroup_item = lxcapi_get_cgroup_item;
2115 c->set_cgroup_item = lxcapi_set_cgroup_item;
2a59a681
SH
2116 c->get_config_path = lxcapi_get_config_path;
2117 c->set_config_path = lxcapi_set_config_path;
9be53773 2118 c->clone = lxcapi_clone;
9c83a661 2119 c->get_ips = lxcapi_get_ips;
a0e93eeb
CS
2120 c->attach = lxcapi_attach;
2121 c->attach_run_wait = lxcapi_attach_run_wait;
2122 c->attach_run_waitl = lxcapi_attach_run_waitl;
72d0e1cb
SG
2123
2124 /* we'll allow the caller to update these later */
ab1bf971 2125 if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) {
72d0e1cb
SG
2126 fprintf(stderr, "failed to open log\n");
2127 goto err;
2128 }
2129
72d0e1cb
SG
2130 return c;
2131
2132err:
2133 lxc_container_free(c);
2134 return NULL;
2135}
2136
4a7c7daa 2137int lxc_get_wait_states(const char **states)
72d0e1cb
SG
2138{
2139 int i;
2140
2141 if (states)
2142 for (i=0; i<MAX_STATE; i++)
2143 states[i] = lxc_state2str(i);
2144 return MAX_STATE;
2145}