]>
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
;
69 sem_destroy(c
->privlock
);
79 * note, c->lxc_conf is going to have to be freed, but the fn
80 * to do that hasn't been written yet near as I can tell
85 int lxc_container_get(struct lxc_container
*c
)
90 if (lxclock(c
->privlock
, 0))
92 if (c
->numthreads
< 1) {
93 // bail without trying to unlock, bc the privlock is now probably
98 lxcunlock(c
->privlock
);
102 int lxc_container_put(struct lxc_container
*c
)
106 if (lxclock(c
->privlock
, 0))
108 if (--c
->numthreads
< 1) {
109 lxcunlock(c
->privlock
);
110 lxc_container_free(c
);
113 lxcunlock(c
->privlock
);
117 static bool file_exists(char *f
)
121 return stat(f
, &statbuf
) == 0;
124 static bool lxcapi_is_defined(struct lxc_container
*c
)
133 if (lxclock(c
->privlock
, 0))
137 statret
= stat(c
->configfile
, &statbuf
);
143 lxcunlock(c
->privlock
);
147 static const char *lxcapi_state(struct lxc_container
*c
)
154 if (lxclock(c
->slock
, 0))
156 s
= lxc_getstate(c
->name
);
157 ret
= lxc_state2str(s
);
163 static bool lxcapi_is_running(struct lxc_container
*c
)
170 if (!s
|| strcmp(s
, "STOPPED") == 0)
175 static bool lxcapi_freeze(struct lxc_container
*c
)
181 if (lxclock(c
->slock
, 0))
183 ret
= lxc_freeze(c
->name
);
190 static bool lxcapi_unfreeze(struct lxc_container
*c
)
196 if (lxclock(c
->slock
, 0))
198 ret
= lxc_unfreeze(c
->name
);
205 static pid_t
lxcapi_init_pid(struct lxc_container
*c
)
211 if (lxclock(c
->slock
, 0))
213 ret
= get_init_pid(c
->name
);
218 static bool lxcapi_load_config(struct lxc_container
*c
, char *alt_file
)
225 fname
= c
->configfile
;
230 if (lxclock(c
->slock
, 0))
233 c
->lxc_conf
= lxc_conf_init();
234 if (c
->lxc_conf
&& !lxc_config_read(fname
, c
->lxc_conf
))
240 static void lxcapi_want_daemonize(struct lxc_container
*c
)
247 static bool lxcapi_wait(struct lxc_container
*c
, char *state
, int timeout
)
254 ret
= lxc_wait(c
->name
, state
, timeout
);
259 static bool wait_on_daemonized_start(struct lxc_container
*c
)
261 /* we'll probably want to make this timeout configurable? */
264 return lxcapi_wait(c
, "RUNNING", timeout
);
268 * I can't decide if it'd be more convenient for callers if we accept '...',
269 * or a null-terminated array (i.e. execl vs execv)
271 static bool lxcapi_start(struct lxc_container
*c
, int useinit
, char ** argv
)
274 struct lxc_conf
*conf
;
276 char *default_args
[] = {
281 /* container exists */
284 /* container has been setup */
288 /* is this app meant to be run through lxcinit, as in lxc-execute? */
289 if (useinit
&& !argv
)
292 if (lxclock(c
->privlock
, 0))
295 daemonize
= c
->daemonize
;
296 lxcunlock(c
->privlock
);
299 ret
= lxc_execute(c
->name
, argv
, 1, conf
);
300 return ret
== 0 ? true : false;
307 * say, I'm not sure - what locks do we want here? Any?
308 * Is liblxc's locking enough here to protect the on disk
309 * container? We don't want to exclude things like lxc_info
310 * while container is running...
313 if (!lxc_container_get(c
))
317 lxc_container_put(c
);
321 return wait_on_daemonized_start(c
);
322 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
327 open("/dev/null", O_RDONLY
);
328 open("/dev/null", O_RDWR
);
329 open("/dev/null", O_RDWR
);
333 if (putenv("container=lxc")) {
334 fprintf(stderr
, "failed to set environment variable");
336 lxc_container_put(c
);
345 ret
= lxc_start(c
->name
, argv
, conf
);
348 INFO("container requested reboot");
350 if (conf
->maincmd_fd
)
351 close(conf
->maincmd_fd
);
352 conf
->maincmd_fd
= 0;
357 lxc_container_put(c
);
358 exit (ret
== 0 ? true : false);
360 return (ret
== 0 ? true : false);
365 * note there MUST be an ending NULL
367 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
370 char **inargs
= NULL
, **temp
;
374 /* container exists */
378 /* build array of arguments if any */
379 va_start(ap
, useinit
);
382 arg
= va_arg(ap
, char *);
386 temp
= realloc(inargs
, n_inargs
* sizeof(*inargs
));
390 inargs
[n_inargs
- 1] = strdup(arg
); // not sure if it's safe not to copy
394 /* add trailing NULL */
397 temp
= realloc(inargs
, n_inargs
* sizeof(*inargs
));
401 inargs
[n_inargs
- 1] = NULL
;
404 bret
= lxcapi_start(c
, useinit
, inargs
);
409 for (i
= 0; i
< n_inargs
; i
++) {
419 static bool lxcapi_stop(struct lxc_container
*c
)
426 ret
= lxc_stop(c
->name
);
431 static bool valid_template(char *t
)
436 statret
= stat(t
, &statbuf
);
443 * create the standard expected container dir
445 static bool create_container_dir(struct lxc_container
*c
)
450 len
= strlen(LXCPATH
) + strlen(c
->name
) + 2;
454 ret
= snprintf(s
, len
, "%s/%s", LXCPATH
, c
->name
);
455 if (ret
< 0 || ret
>= len
) {
459 ret
= mkdir(s
, 0755);
464 SYSERROR("failed to create container path for %s\n", c
->name
);
471 * backing stores not (yet) supported
472 * for ->create, argv contains the arguments to pass to the template,
473 * terminated by NULL. If no arguments, you can just pass NULL.
475 static bool lxcapi_create(struct lxc_container
*c
, char *t
, char **argv
)
487 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
491 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
492 if (ret
< 0 || ret
>= len
)
494 if (!valid_template(tpath
)) {
495 ERROR("bad template: %s\n", t
);
499 if (!create_container_dir(c
))
502 if (!c
->save_config(c
, NULL
)) {
503 ERROR("failed to save starting configuration for %s\n", c
->name
);
507 /* we're going to fork. but since we'll wait for our child, we
508 don't need to lxc_container_get */
510 if (lxclock(c
->slock
, 0)) {
511 ERROR("failed to grab global container lock for %s\n", c
->name
);
517 SYSERROR("failed to fork task for container creation template\n");
521 if (pid
== 0) { // child
522 char *patharg
, *namearg
;
528 open("/dev/null", O_RDONLY
);
529 open("/dev/null", O_RDWR
);
530 open("/dev/null", O_RDWR
);
533 * create our new array, pre-pend the template name and
537 for (; argv
[nargs
]; nargs
++) ;
538 nargs
+= 3; // template, path and name args
539 newargv
= malloc(nargs
* sizeof(*newargv
));
544 len
= strlen(LXCPATH
) + strlen(c
->name
) + strlen("--path=") + 2;
545 patharg
= malloc(len
);
548 ret
= snprintf(patharg
, len
, "--path=%s/%s", LXCPATH
, c
->name
);
549 if (ret
< 0 || ret
>= len
)
551 newargv
[1] = patharg
;
552 len
= strlen("--name=") + strlen(c
->name
) + 1;
553 namearg
= malloc(len
);
556 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
557 if (ret
< 0 || ret
>= len
)
559 newargv
[2] = namearg
;
561 /* add passed-in args */
563 for (i
= 3; i
< nargs
; i
++)
564 newargv
[i
] = argv
[i
-3];
566 /* add trailing NULL */
568 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
571 newargv
[nargs
- 1] = NULL
;
574 ret
= execv(tpath
, newargv
);
575 SYSERROR("failed to execute template %s", tpath
);
580 ret
= waitpid(pid
, &status
, 0);
584 SYSERROR("waitpid failed");
589 if (!WIFEXITED(status
)) { // did not exit normally
590 // we could set an error code and string inside the
591 // container_struct here if we like
592 ERROR("container creation template exited abnormally\n");
596 if (WEXITSTATUS(status
) != 0)
597 ERROR("container creation template for %s exited with %d\n",
598 c
->name
, WEXITSTATUS(status
));
610 static bool lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
620 if (!c
->is_running(c
))
622 pid
= c
->init_pid(c
);
626 retv
= c
->wait(c
, "STOPPED", timeout
);
629 retv
= c
->wait(c
, "STOPPED", 0); // 0 means don't wait
634 static bool lxcapi_createl(struct lxc_container
*c
, char *t
, ...)
637 char **args
= NULL
, **temp
;
645 * since we're going to wait for create to finish, I don't think we
646 * need to get a copy of the arguments.
651 arg
= va_arg(ap
, char *);
655 temp
= realloc(args
, nargs
* sizeof(*args
));
659 args
[nargs
- 1] = arg
;
663 bret
= c
->create(c
, t
, args
);
671 static bool lxcapi_clear_config_item(struct lxc_container
*c
, char *key
)
675 if (!c
|| !c
->lxc_conf
)
677 if (lxclock(c
->privlock
, 0)) {
680 ret
= lxc_clear_config_item(c
->lxc_conf
, key
);
681 lxcunlock(c
->privlock
);
685 static int lxcapi_get_config_item(struct lxc_container
*c
, char *key
, char *retv
, int inlen
)
689 if (!c
|| !c
->lxc_conf
)
691 if (lxclock(c
->privlock
, 0)) {
694 ret
= lxc_get_config_item(c
->lxc_conf
, key
, retv
, inlen
);
695 lxcunlock(c
->privlock
);
699 static int lxcapi_get_keys(struct lxc_container
*c
, char *key
, char *retv
, int inlen
)
702 return lxc_listconfigs(retv
, inlen
);
704 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
705 * This is an intelligent result to show which keys are valid given
706 * the type of nic it is
708 if (!c
|| !c
->lxc_conf
)
710 if (lxclock(c
->privlock
, 0))
713 if (strncmp(key
, "lxc.network.", 12) == 0)
714 ret
= lxc_list_nicconfigs(c
->lxc_conf
, key
, retv
, inlen
);
715 lxcunlock(c
->privlock
);
720 /* default config file - should probably come through autoconf */
721 #define LXC_DEFAULT_CONFIG "/etc/lxc/lxc.conf"
722 static bool lxcapi_save_config(struct lxc_container
*c
, char *alt_file
)
725 alt_file
= c
->configfile
;
727 return false; // should we write to stdout if no file is specified?
729 if (!c
->load_config(c
, LXC_DEFAULT_CONFIG
)) {
730 ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG
, c
->name
);
734 FILE *fout
= fopen(alt_file
, "w");
737 if (lxclock(c
->privlock
, 0)) {
741 write_config(fout
, c
->lxc_conf
);
743 lxcunlock(c
->privlock
);
747 static bool lxcapi_destroy(struct lxc_container
*c
)
758 if (pid
== 0) { // child
759 ret
= execlp("lxc-destroy", "lxc-destroy", "-n", c
->name
, NULL
);
765 ret
= waitpid(pid
, &status
, 0);
774 if (!WIFEXITED(status
)) { // did not exit normally
775 // we could set an error code and string inside the
776 // container_struct here if we like
780 return WEXITSTATUS(status
) == 0;
783 static bool lxcapi_set_config_item(struct lxc_container
*c
, char *key
, char *v
)
787 struct lxc_config_t
*config
;
792 if (lxclock(c
->privlock
, 0))
796 c
->lxc_conf
= lxc_conf_init();
799 config
= lxc_getconfig(key
);
802 ret
= config
->cb(key
, v
, c
->lxc_conf
);
807 lxcunlock(c
->privlock
);
811 static char *lxcapi_config_file_name(struct lxc_container
*c
)
813 if (!c
|| !c
->configfile
)
815 return strdup(c
->configfile
);
818 struct lxc_container
*lxc_container_new(char *name
)
820 struct lxc_container
*c
;
823 c
= malloc(sizeof(*c
));
825 fprintf(stderr
, "failed to malloc lxc_container\n");
828 memset(c
, 0, sizeof(*c
));
830 c
->name
= malloc(strlen(name
)+1);
832 fprintf(stderr
, "Error allocating lxc_container name\n");
835 strcpy(c
->name
, name
);
838 c
->slock
= lxc_newlock(name
);
840 fprintf(stderr
, "failed to create lock\n");
844 c
->privlock
= lxc_newlock(NULL
);
846 fprintf(stderr
, "failed to alloc privlock\n");
850 len
= strlen(LXCDIR
)+strlen(c
->name
)+strlen("/config")+2;
851 c
->configfile
= malloc(len
);
852 if (!c
->configfile
) {
853 fprintf(stderr
, "Error allocating config file pathname\n");
856 ret
= snprintf(c
->configfile
, len
, "%s/%s/config", LXCDIR
, c
->name
);
857 if (ret
< 0 || ret
>= len
) {
858 fprintf(stderr
, "Error printing out config file name\n");
862 if (file_exists(c
->configfile
))
863 lxcapi_load_config(c
, NULL
);
865 // assign the member functions
866 c
->is_defined
= lxcapi_is_defined
;
867 c
->state
= lxcapi_state
;
868 c
->is_running
= lxcapi_is_running
;
869 c
->freeze
= lxcapi_freeze
;
870 c
->unfreeze
= lxcapi_unfreeze
;
871 c
->init_pid
= lxcapi_init_pid
;
872 c
->load_config
= lxcapi_load_config
;
873 c
->want_daemonize
= lxcapi_want_daemonize
;
874 c
->start
= lxcapi_start
;
875 c
->startl
= lxcapi_startl
;
876 c
->stop
= lxcapi_stop
;
877 c
->config_file_name
= lxcapi_config_file_name
;
878 c
->wait
= lxcapi_wait
;
879 c
->set_config_item
= lxcapi_set_config_item
;
880 c
->destroy
= lxcapi_destroy
;
881 c
->save_config
= lxcapi_save_config
;
882 c
->get_keys
= lxcapi_get_keys
;
883 c
->create
= lxcapi_create
;
884 c
->createl
= lxcapi_createl
;
885 c
->shutdown
= lxcapi_shutdown
;
886 c
->clear_config_item
= lxcapi_clear_config_item
;
887 c
->get_config_item
= lxcapi_get_config_item
;
889 /* we'll allow the caller to update these later */
890 if (lxc_log_init("/var/log/lxccontainer.log", "trace", "lxc_container", 0)) {
891 fprintf(stderr
, "failed to open log\n");
896 * default configuration file is $LXCDIR/$NAME/config
902 lxc_container_free(c
);
906 int lxc_get_wait_states(char **states
)
911 for (i
=0; i
<MAX_STATE
; i
++)
912 states
[i
] = lxc_state2str(i
);