]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/execute.c
move lxc-init to /sbin/init.lxc
[mirror_lxc.git] / src / lxc / execute.c
CommitLineData
0ae4f887
GK
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ae4f887
GK
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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ae4f887
GK
22 */
23
a0a2066d
SH
24#include <sys/types.h>
25#include <sys/stat.h>
0ae4f887
GK
26#include <errno.h>
27#include <unistd.h>
28#include <stdlib.h>
29
e0b0b533 30#include "conf.h"
0ae4f887
GK
31#include "log.h"
32#include "start.h"
33
34lxc_log_define(lxc_execute, lxc_start);
35
36struct execute_args {
37 char *const *argv;
38 int quiet;
39};
40
718e4f48
SH
41/* historically lxc-init has been under /usr/lib/lxc and under
42 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
a0a2066d
SH
43 */
44static char *choose_init(void)
45{
46 char *retv = malloc(PATH_MAX);
47 int ret;
48 struct stat mystat;
49 if (!retv)
50 return NULL;
51
718e4f48
SH
52 ret = snprintf(retv, PATH_MAX, SBINDIR "/init.lxc");
53 if (ret < 0 || ret >= PATH_MAX) {
54 ERROR("pathname too long");
55 goto out1;
56 }
57 ret = stat(retv, &mystat);
58 if (ret == 0)
59 return retv;
60
9ba8130c
SH
61 ret = snprintf(retv, PATH_MAX, LXCINITDIR "/lxc/lxc-init");
62 if (ret < 0 || ret >= PATH_MAX) {
63 ERROR("pathname too long");
8950ee8e 64 goto out1;
9ba8130c
SH
65 }
66
a0a2066d
SH
67 ret = stat(retv, &mystat);
68 if (ret == 0)
69 return retv;
9ba8130c
SH
70
71 ret = snprintf(retv, PATH_MAX, "/usr/lib/lxc/lxc-init");
72 if (ret < 0 || ret >= PATH_MAX) {
73 ERROR("pathname too long");
8950ee8e 74 goto out1;
9ba8130c 75 }
a0a2066d
SH
76 ret = stat(retv, &mystat);
77 if (ret == 0)
78 return retv;
9ba8130c
SH
79 ret = snprintf(retv, PATH_MAX, "/sbin/lxc-init");
80 if (ret < 0 || ret >= PATH_MAX) {
81 ERROR("pathname too long");
8950ee8e 82 goto out1;
9ba8130c 83 }
a0a2066d
SH
84 ret = stat(retv, &mystat);
85 if (ret == 0)
86 return retv;
8950ee8e
DE
87out1:
88 free(retv);
a0a2066d
SH
89 return NULL;
90}
91
0ae4f887
GK
92static int execute_start(struct lxc_handler *handler, void* data)
93{
94 int j, i = 0;
95 struct execute_args *my_args = data;
96 char **argv;
e0b0b533 97 int argc = 0, argc_add;
a0a2066d 98 char *initpath;
0ae4f887
GK
99
100 while (my_args->argv[argc++]);
101
e0b0b533
DE
102 argc_add = 4;
103 if (my_args->quiet)
104 argc_add++;
fabf7361
QH
105 if (!handler->conf->rootfs.path) {
106 argc_add += 4;
107 if (lxc_log_has_valid_level())
108 argc_add += 2;
109 }
e0b0b533
DE
110
111 argv = malloc((argc + argc_add) * sizeof(*argv));
0ae4f887 112 if (!argv)
e0b0b533 113 goto out1;
0ae4f887 114
a0a2066d
SH
115 initpath = choose_init();
116 if (!initpath) {
117 ERROR("Failed to find an lxc-init");
e0b0b533 118 goto out2;
a0a2066d
SH
119 }
120 argv[i++] = initpath;
0ae4f887
GK
121 if (my_args->quiet)
122 argv[i++] = "--quiet";
e0b0b533
DE
123 if (!handler->conf->rootfs.path) {
124 argv[i++] = "--name";
125 argv[i++] = (char *)handler->name;
126 argv[i++] = "--lxcpath";
127 argv[i++] = (char *)handler->lxcpath;
fabf7361
QH
128
129 if (lxc_log_has_valid_level()) {
130 argv[i++] = "--logpriority";
131 argv[i++] = (char *)
132 lxc_log_priority_to_string(lxc_log_get_level());
133 }
e0b0b533 134 }
0ae4f887
GK
135 argv[i++] = "--";
136 for (j = 0; j < argc; j++)
137 argv[i++] = my_args->argv[j];
138 argv[i++] = NULL;
139
140 NOTICE("exec'ing '%s'", my_args->argv[0]);
141
142 execvp(argv[0], argv);
143 SYSERROR("failed to exec %s", argv[0]);
e0b0b533
DE
144 free(initpath);
145out2:
146 free(argv);
147out1:
0ae4f887
GK
148 return 1;
149}
150
151static int execute_post_start(struct lxc_handler *handler, void* data)
152{
153 struct execute_args *my_args = data;
154 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
155 return 0;
156}
157
158static struct lxc_operations execute_start_ops = {
159 .start = execute_start,
160 .post_start = execute_post_start
161};
162
163int lxc_execute(const char *name, char *const argv[], int quiet,
13f5be62 164 struct lxc_conf *conf, const char *lxcpath)
0ae4f887
GK
165{
166 struct execute_args args = {
167 .argv = argv,
168 .quiet = quiet
169 };
170
b119f362 171 if (lxc_check_inherited(conf, -1))
0ae4f887
GK
172 return -1;
173
37903589 174 conf->is_execute = 1;
13f5be62 175 return __lxc_start(name, conf, &execute_start_ops, &args, lxcpath);
0ae4f887 176}