]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/execute.c
mainloop: capture output of short-lived init procs
[mirror_lxc.git] / src / lxc / execute.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29
30 #include "conf.h"
31 #include "log.h"
32 #include "start.h"
33 #include "utils.h"
34
35 lxc_log_define(lxc_execute, lxc_start);
36
37 struct execute_args {
38 char *const *argv;
39 int quiet;
40 };
41
42 static int execute_start(struct lxc_handler *handler, void* data)
43 {
44 int j, i = 0;
45 struct execute_args *my_args = data;
46 char **argv;
47 int argc = 0, argc_add;
48 char *initpath;
49
50 while (my_args->argv[argc++]);
51
52 /* lxc-init -n name -- [argc] NULL -> 5 */
53 argc_add = 5;
54 if (my_args->quiet)
55 argc_add++;
56 if (!handler->conf->rootfs.path)
57 argc_add += 2;
58 if (lxc_log_has_valid_level())
59 argc_add += 2;
60
61 argv = malloc((argc + argc_add) * sizeof(*argv));
62 if (!argv)
63 goto out1;
64
65 initpath = choose_init(NULL);
66 if (!initpath) {
67 ERROR("Failed to find an init.lxc or init.lxc.static");
68 goto out2;
69 }
70 argv[i++] = initpath;
71
72 argv[i++] = "-n";
73 argv[i++] = (char *)handler->name;
74
75 if (lxc_log_has_valid_level()) {
76 argv[i++] = "-l";
77 argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
78 }
79
80 if (handler->conf->logfile) {
81 argv[i++] = "-o";
82 argv[i++] = (char *)handler->conf->logfile;
83 }
84
85 if (my_args->quiet)
86 argv[i++] = "--quiet";
87
88 if (!handler->conf->rootfs.path) {
89 argv[i++] = "-P";
90 argv[i++] = (char *)handler->lxcpath;
91 }
92
93 argv[i++] = "--";
94 for (j = 0; j < argc; j++)
95 argv[i++] = my_args->argv[j];
96 argv[i++] = NULL;
97
98 NOTICE("Exec'ing \"%s\"", my_args->argv[0]);
99
100 execvp(argv[0], argv);
101 SYSERROR("Failed to exec %s", argv[0]);
102 free(initpath);
103 out2:
104 free(argv);
105 out1:
106 return 1;
107 }
108
109 static int execute_post_start(struct lxc_handler *handler, void* data)
110 {
111 struct execute_args *my_args = data;
112 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
113 return 0;
114 }
115
116 static struct lxc_operations execute_start_ops = {
117 .start = execute_start,
118 .post_start = execute_post_start
119 };
120
121 int lxc_execute(const char *name, char *const argv[], int quiet,
122 struct lxc_handler *handler, const char *lxcpath,
123 bool backgrounded)
124 {
125 struct execute_args args = {.argv = argv, .quiet = quiet};
126
127 if (lxc_check_inherited(handler->conf, false, &handler->conf->maincmd_fd, 1))
128 return -1;
129
130 handler->conf->is_execute = 1;
131 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
132 backgrounded);
133 }