]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxccontainer.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
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.
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.
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.
22 #include "lxccontainer.h"
30 #include <sys/types.h>
34 lxc_log_define(lxc_container
, lxc
);
37 * c->privlock protects the struct lxc_container from multiple threads.
38 * c->slock protects the on-disk container data
39 * NOTHING mutexes two independent programs with their own struct
40 * lxc_container for the same c->name, between API calls. For instance,
41 * c->config_read(); c->start(); Between those calls, data on disk
42 * could change (which shouldn't bother the caller unless for instance
43 * the rootfs get moved). c->config_read(); update; c->config_write();
44 * Two such updaters could race. The callers should therefore check their
45 * results. Trying to prevent that would necessarily expose us to deadlocks
46 * due to hung callers. So I prefer to keep the locks only within our own
47 * functions, not across functions.
49 * If you're going to fork while holding a lxccontainer, increment
50 * c->numthreads (under privlock) before forking. When deleting,
51 * decrement numthreads under privlock, then if it hits 0 you can delete.
52 * Do not ever use a lxccontainer whose numthreads you did not bump.
55 static void lxc_container_free(struct lxc_container
*c
)
64 if (c
->error_string
) {
65 free(c
->error_string
);
66 c
->error_string
= NULL
;
73 sem_destroy(c
->privlock
);
82 lxc_conf_free(c
->lxc_conf
);
88 int lxc_container_get(struct lxc_container
*c
)
93 if (lxclock(c
->privlock
, 0))
95 if (c
->numthreads
< 1) {
96 // bail without trying to unlock, bc the privlock is now probably
101 lxcunlock(c
->privlock
);
105 int lxc_container_put(struct lxc_container
*c
)
109 if (lxclock(c
->privlock
, 0))
111 if (--c
->numthreads
< 1) {
112 lxcunlock(c
->privlock
);
113 lxc_container_free(c
);
116 lxcunlock(c
->privlock
);
120 static bool file_exists(char *f
)
124 return stat(f
, &statbuf
) == 0;
127 static bool lxcapi_is_defined(struct lxc_container
*c
)
136 if (lxclock(c
->privlock
, 0))
140 statret
= stat(c
->configfile
, &statbuf
);
146 lxcunlock(c
->privlock
);
150 static const char *lxcapi_state(struct lxc_container
*c
)
157 if (lxclock(c
->slock
, 0))
159 s
= lxc_getstate(c
->name
);
160 ret
= lxc_state2str(s
);
166 static bool is_stopped_nolock(struct lxc_container
*c
)
169 s
= lxc_getstate(c
->name
);
170 return (s
== STOPPED
);
173 static bool lxcapi_is_running(struct lxc_container
*c
)
180 if (!s
|| strcmp(s
, "STOPPED") == 0)
185 static bool lxcapi_freeze(struct lxc_container
*c
)
191 if (lxclock(c
->slock
, 0))
193 ret
= lxc_freeze(c
->name
);
200 static bool lxcapi_unfreeze(struct lxc_container
*c
)
206 if (lxclock(c
->slock
, 0))
208 ret
= lxc_unfreeze(c
->name
);
215 static pid_t
lxcapi_init_pid(struct lxc_container
*c
)
221 if (lxclock(c
->slock
, 0))
223 ret
= get_init_pid(c
->name
);
228 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
231 c
->lxc_conf
= lxc_conf_init();
232 if (c
->lxc_conf
&& !lxc_config_read(fname
, c
->lxc_conf
))
237 static bool lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
244 fname
= c
->configfile
;
249 if (lxclock(c
->slock
, 0))
251 ret
= load_config_locked(c
, fname
);
256 static void lxcapi_want_daemonize(struct lxc_container
*c
)
263 static bool lxcapi_wait(struct lxc_container
*c
, const char *state
, int timeout
)
270 ret
= lxc_wait(c
->name
, state
, timeout
);
275 static bool wait_on_daemonized_start(struct lxc_container
*c
)
277 /* we'll probably want to make this timeout configurable? */
278 int timeout
= 5, ret
, status
;
281 * our child is going to fork again, then exit. reap the
285 if (ret
== -1 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
286 DEBUG("failed waiting for first dual-fork child");
287 return lxcapi_wait(c
, "RUNNING", timeout
);
291 * I can't decide if it'd be more convenient for callers if we accept '...',
292 * or a null-terminated array (i.e. execl vs execv)
294 static bool lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
297 struct lxc_conf
*conf
;
299 char *default_args
[] = {
304 /* container exists */
307 /* container has been setup */
311 /* is this app meant to be run through lxcinit, as in lxc-execute? */
312 if (useinit
&& !argv
)
315 if (lxclock(c
->privlock
, 0))
318 daemonize
= c
->daemonize
;
319 lxcunlock(c
->privlock
);
322 ret
= lxc_execute(c
->name
, argv
, 1, conf
);
323 return ret
== 0 ? true : false;
330 * say, I'm not sure - what locks do we want here? Any?
331 * Is liblxc's locking enough here to protect the on disk
332 * container? We don't want to exclude things like lxc_info
333 * while container is running...
336 if (!lxc_container_get(c
))
340 lxc_container_put(c
);
344 return wait_on_daemonized_start(c
);
345 /* second fork to be reparented by init */
348 SYSERROR("Error doing dual-fork");
353 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
355 SYSERROR("Error chdir()ing to /.");
361 open("/dev/null", O_RDONLY
);
362 open("/dev/null", O_RDWR
);
363 open("/dev/null", O_RDWR
);
367 if (putenv("container=lxc")) {
368 fprintf(stderr
, "failed to set environment variable");
370 lxc_container_put(c
);
379 ret
= lxc_start(c
->name
, argv
, conf
);
382 INFO("container requested reboot");
384 if (conf
->maincmd_fd
)
385 close(conf
->maincmd_fd
);
386 conf
->maincmd_fd
= 0;
391 lxc_container_put(c
);
392 exit (ret
== 0 ? true : false);
394 return (ret
== 0 ? true : false);
399 * note there MUST be an ending NULL
401 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
404 char **inargs
= NULL
, **temp
;
408 /* container exists */
412 /* build array of arguments if any */
413 va_start(ap
, useinit
);
416 arg
= va_arg(ap
, char *);
420 temp
= realloc(inargs
, n_inargs
* sizeof(*inargs
));
424 inargs
[n_inargs
- 1] = strdup(arg
); // not sure if it's safe not to copy
428 /* add trailing NULL */
431 temp
= realloc(inargs
, n_inargs
* sizeof(*inargs
));
435 inargs
[n_inargs
- 1] = NULL
;
438 bret
= lxcapi_start(c
, useinit
, inargs
);
443 for (i
= 0; i
< n_inargs
; i
++) {
453 static bool lxcapi_stop(struct lxc_container
*c
)
460 ret
= lxc_stop(c
->name
);
465 static bool valid_template(char *t
)
470 statret
= stat(t
, &statbuf
);
477 * create the standard expected container dir
479 static bool create_container_dir(struct lxc_container
*c
)
484 len
= strlen(LXCPATH
) + strlen(c
->name
) + 2;
488 ret
= snprintf(s
, len
, "%s/%s", LXCPATH
, c
->name
);
489 if (ret
< 0 || ret
>= len
) {
493 ret
= mkdir(s
, 0755);
498 SYSERROR("failed to create container path for %s\n", c
->name
);
505 * backing stores not (yet) supported
506 * for ->create, argv contains the arguments to pass to the template,
507 * terminated by NULL. If no arguments, you can just pass NULL.
509 static bool lxcapi_create(struct lxc_container
*c
, char *t
, char *const argv
[])
521 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
525 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
526 if (ret
< 0 || ret
>= len
)
528 if (!valid_template(tpath
)) {
529 ERROR("bad template: %s\n", t
);
533 if (!create_container_dir(c
))
536 if (!c
->save_config(c
, NULL
)) {
537 ERROR("failed to save starting configuration for %s\n", c
->name
);
541 /* we're going to fork. but since we'll wait for our child, we
542 don't need to lxc_container_get */
544 if (lxclock(c
->slock
, 0)) {
545 ERROR("failed to grab global container lock for %s\n", c
->name
);
551 SYSERROR("failed to fork task for container creation template\n");
555 if (pid
== 0) { // child
556 char *patharg
, *namearg
;
562 open("/dev/null", O_RDONLY
);
563 open("/dev/null", O_RDWR
);
564 open("/dev/null", O_RDWR
);
567 * create our new array, pre-pend the template name and
571 for (; argv
[nargs
]; nargs
++) ;
572 nargs
+= 3; // template, path and name args
573 newargv
= malloc(nargs
* sizeof(*newargv
));
578 len
= strlen(LXCPATH
) + strlen(c
->name
) + strlen("--path=") + 2;
579 patharg
= malloc(len
);
582 ret
= snprintf(patharg
, len
, "--path=%s/%s", LXCPATH
, c
->name
);
583 if (ret
< 0 || ret
>= len
)
585 newargv
[1] = patharg
;
586 len
= strlen("--name=") + strlen(c
->name
) + 1;
587 namearg
= malloc(len
);
590 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
591 if (ret
< 0 || ret
>= len
)
593 newargv
[2] = namearg
;
595 /* add passed-in args */
597 for (i
= 3; i
< nargs
; i
++)
598 newargv
[i
] = argv
[i
-3];
600 /* add trailing NULL */
602 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
605 newargv
[nargs
- 1] = NULL
;
608 ret
= execv(tpath
, newargv
);
609 SYSERROR("failed to execute template %s", tpath
);
614 ret
= waitpid(pid
, &status
, 0);
618 SYSERROR("waitpid failed");
623 if (!WIFEXITED(status
)) { // did not exit normally
624 // we could set an error code and string inside the
625 // container_struct here if we like
626 ERROR("container creation template exited abnormally\n");
630 if (WEXITSTATUS(status
) != 0) {
631 ERROR("container creation template for %s exited with %d\n",
632 c
->name
, WEXITSTATUS(status
));
636 // now clear out the lxc_conf we have, reload from the created
639 lxc_conf_free(c
->lxc_conf
);
641 bret
= load_config_locked(c
, c
->configfile
);
651 static bool lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
661 if (!c
->is_running(c
))
663 pid
= c
->init_pid(c
);
667 retv
= c
->wait(c
, "STOPPED", timeout
);
668 if (!retv
&& timeout
> 0) {
670 retv
= c
->wait(c
, "STOPPED", 0); // 0 means don't wait
675 static bool lxcapi_createl(struct lxc_container
*c
, char *t
, ...)
678 char **args
= NULL
, **temp
;
686 * since we're going to wait for create to finish, I don't think we
687 * need to get a copy of the arguments.
692 arg
= va_arg(ap
, char *);
696 temp
= realloc(args
, (nargs
+1) * sizeof(*args
));
700 args
[nargs
- 1] = arg
;
705 bret
= c
->create(c
, t
, args
);
713 static bool lxcapi_clear_config_item(struct lxc_container
*c
, const char *key
)
717 if (!c
|| !c
->lxc_conf
)
719 if (lxclock(c
->privlock
, 0)) {
722 ret
= lxc_clear_config_item(c
->lxc_conf
, key
);
723 lxcunlock(c
->privlock
);
727 static int lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
731 if (!c
|| !c
->lxc_conf
)
733 if (lxclock(c
->privlock
, 0)) {
736 ret
= lxc_get_config_item(c
->lxc_conf
, key
, retv
, inlen
);
737 lxcunlock(c
->privlock
);
741 static int lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
744 return lxc_listconfigs(retv
, inlen
);
746 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
747 * This is an intelligent result to show which keys are valid given
748 * the type of nic it is
750 if (!c
|| !c
->lxc_conf
)
752 if (lxclock(c
->privlock
, 0))
755 if (strncmp(key
, "lxc.network.", 12) == 0)
756 ret
= lxc_list_nicconfigs(c
->lxc_conf
, key
, retv
, inlen
);
757 lxcunlock(c
->privlock
);
762 /* default config file - should probably come through autoconf */
763 #define LXC_DEFAULT_CONFIG "/etc/lxc/lxc.conf"
764 static bool lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
767 alt_file
= c
->configfile
;
769 return false; // should we write to stdout if no file is specified?
771 if (!c
->load_config(c
, LXC_DEFAULT_CONFIG
)) {
772 ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG
, c
->name
);
776 FILE *fout
= fopen(alt_file
, "w");
779 if (lxclock(c
->privlock
, 0)) {
783 write_config(fout
, c
->lxc_conf
);
785 lxcunlock(c
->privlock
);
789 static bool lxcapi_destroy(struct lxc_container
*c
)
800 if (pid
== 0) { // child
801 ret
= execlp("lxc-destroy", "lxc-destroy", "-n", c
->name
, NULL
);
807 ret
= waitpid(pid
, &status
, 0);
816 if (!WIFEXITED(status
)) { // did not exit normally
817 // we could set an error code and string inside the
818 // container_struct here if we like
822 return WEXITSTATUS(status
) == 0;
825 static bool lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
829 struct lxc_config_t
*config
;
834 if (lxclock(c
->privlock
, 0))
838 c
->lxc_conf
= lxc_conf_init();
841 config
= lxc_getconfig(key
);
844 ret
= config
->cb(key
, v
, c
->lxc_conf
);
849 lxcunlock(c
->privlock
);
853 static char *lxcapi_config_file_name(struct lxc_container
*c
)
855 if (!c
|| !c
->configfile
)
857 return strdup(c
->configfile
);
860 static bool lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
868 if (lxclock(c
->privlock
, 0))
871 if (is_stopped_nolock(c
))
874 ret
= lxc_cgroup_set(c
->name
, subsys
, value
);
878 lxcunlock(c
->privlock
);
882 static int lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
886 if (!c
|| !c
->lxc_conf
)
889 if (lxclock(c
->privlock
, 0))
892 if (is_stopped_nolock(c
))
895 ret
= lxc_cgroup_get(c
->name
, subsys
, retv
, inlen
);
898 lxcunlock(c
->privlock
);
903 struct lxc_container
*lxc_container_new(const char *name
)
905 struct lxc_container
*c
;
908 c
= malloc(sizeof(*c
));
910 fprintf(stderr
, "failed to malloc lxc_container\n");
913 memset(c
, 0, sizeof(*c
));
915 c
->name
= malloc(strlen(name
)+1);
917 fprintf(stderr
, "Error allocating lxc_container name\n");
920 strcpy(c
->name
, name
);
923 c
->slock
= lxc_newlock(name
);
925 fprintf(stderr
, "failed to create lock\n");
929 c
->privlock
= lxc_newlock(NULL
);
931 fprintf(stderr
, "failed to alloc privlock\n");
935 len
= strlen(LXCPATH
)+strlen(c
->name
)+strlen("/config")+2;
936 c
->configfile
= malloc(len
);
937 if (!c
->configfile
) {
938 fprintf(stderr
, "Error allocating config file pathname\n");
941 ret
= snprintf(c
->configfile
, len
, "%s/%s/config", LXCPATH
, c
->name
);
942 if (ret
< 0 || ret
>= len
) {
943 fprintf(stderr
, "Error printing out config file name\n");
947 if (file_exists(c
->configfile
))
948 lxcapi_load_config(c
, NULL
);
950 // assign the member functions
951 c
->is_defined
= lxcapi_is_defined
;
952 c
->state
= lxcapi_state
;
953 c
->is_running
= lxcapi_is_running
;
954 c
->freeze
= lxcapi_freeze
;
955 c
->unfreeze
= lxcapi_unfreeze
;
956 c
->init_pid
= lxcapi_init_pid
;
957 c
->load_config
= lxcapi_load_config
;
958 c
->want_daemonize
= lxcapi_want_daemonize
;
959 c
->start
= lxcapi_start
;
960 c
->startl
= lxcapi_startl
;
961 c
->stop
= lxcapi_stop
;
962 c
->config_file_name
= lxcapi_config_file_name
;
963 c
->wait
= lxcapi_wait
;
964 c
->set_config_item
= lxcapi_set_config_item
;
965 c
->destroy
= lxcapi_destroy
;
966 c
->save_config
= lxcapi_save_config
;
967 c
->get_keys
= lxcapi_get_keys
;
968 c
->create
= lxcapi_create
;
969 c
->createl
= lxcapi_createl
;
970 c
->shutdown
= lxcapi_shutdown
;
971 c
->clear_config_item
= lxcapi_clear_config_item
;
972 c
->get_config_item
= lxcapi_get_config_item
;
973 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
974 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
976 /* we'll allow the caller to update these later */
977 if (lxc_log_init(NULL
, NULL
, "lxc_container", 0)) {
978 fprintf(stderr
, "failed to open log\n");
983 * default configuration file is $LXCPATH/$NAME/config
989 lxc_container_free(c
);
993 int lxc_get_wait_states(const char **states
)
998 for (i
=0; i
<MAX_STATE
; i
++)
999 states
[i
] = lxc_state2str(i
);