2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
24 #include <sys/types.h>
35 lxc_log_define(lxc_execute
, lxc_start
);
42 /* historically lxc-init has been under /usr/lib/lxc and under
43 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
45 static char *choose_init(void)
51 if (!getenv("PATH")) {
52 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
53 SYSERROR("Failed to setenv");
57 retv
= on_path("init.lxc");
61 SYSERROR("Failed to unsetenv");
67 retv
= malloc(PATH_MAX
);
71 ret
= snprintf(retv
, PATH_MAX
, SBINDIR
"/init.lxc");
72 if (ret
< 0 || ret
>= PATH_MAX
) {
73 ERROR("pathname too long");
77 ret
= stat(retv
, &mystat
);
81 ret
= snprintf(retv
, PATH_MAX
, LXCINITDIR
"/lxc/lxc-init");
82 if (ret
< 0 || ret
>= PATH_MAX
) {
83 ERROR("pathname too long");
87 ret
= stat(retv
, &mystat
);
91 ret
= snprintf(retv
, PATH_MAX
, "/usr/lib/lxc/lxc-init");
92 if (ret
< 0 || ret
>= PATH_MAX
) {
93 ERROR("pathname too long");
96 ret
= stat(retv
, &mystat
);
99 ret
= snprintf(retv
, PATH_MAX
, "/sbin/lxc-init");
100 if (ret
< 0 || ret
>= PATH_MAX
) {
101 ERROR("pathname too long");
104 ret
= stat(retv
, &mystat
);
112 static int execute_start(struct lxc_handler
*handler
, void* data
)
115 struct execute_args
*my_args
= data
;
117 int argc
= 0, argc_add
;
120 while (my_args
->argv
[argc
++]);
125 if (!handler
->conf
->rootfs
.path
) {
127 if (lxc_log_has_valid_level())
131 argv
= malloc((argc
+ argc_add
) * sizeof(*argv
));
135 initpath
= choose_init();
137 ERROR("Failed to find an lxc-init");
140 argv
[i
++] = initpath
;
142 argv
[i
++] = "--quiet";
143 if (!handler
->conf
->rootfs
.path
) {
144 argv
[i
++] = "--name";
145 argv
[i
++] = (char *)handler
->name
;
146 argv
[i
++] = "--lxcpath";
147 argv
[i
++] = (char *)handler
->lxcpath
;
149 if (lxc_log_has_valid_level()) {
150 argv
[i
++] = "--logpriority";
152 lxc_log_priority_to_string(lxc_log_get_level());
156 for (j
= 0; j
< argc
; j
++)
157 argv
[i
++] = my_args
->argv
[j
];
160 NOTICE("exec'ing '%s'", my_args
->argv
[0]);
162 execvp(argv
[0], argv
);
163 SYSERROR("failed to exec %s", argv
[0]);
171 static int execute_post_start(struct lxc_handler
*handler
, void* data
)
173 struct execute_args
*my_args
= data
;
174 NOTICE("'%s' started with pid '%d'", my_args
->argv
[0], handler
->pid
);
178 static struct lxc_operations execute_start_ops
= {
179 .start
= execute_start
,
180 .post_start
= execute_post_start
183 int lxc_execute(const char *name
, char *const argv
[], int quiet
,
184 struct lxc_conf
*conf
, const char *lxcpath
)
186 struct execute_args args
= {
191 if (lxc_check_inherited(conf
, -1))
194 conf
->is_execute
= 1;
195 return __lxc_start(name
, conf
, &execute_start_ops
, &args
, lxcpath
);