]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/tools/lxc_start.c
Add support share pid namespace
[mirror_lxc.git] / src / lxc / tools / lxc_start.c
CommitLineData
5e97c3fc 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>
5e97c3fc 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
5e97c3fc 22 */
d06245b8
NC
23#include "config.h"
24
5e97c3fc 25#include <stdio.h>
26#include <libgen.h>
96c210bb 27#include <stdlib.h>
5e97c3fc 28#include <unistd.h>
29#include <string.h>
b0a33c1e 30#include <errno.h>
c36583c3 31#include <fcntl.h>
f8e09a0b 32#include <signal.h>
5e97c3fc 33#include <sys/param.h>
34#include <sys/utsname.h>
35#include <sys/types.h>
36#include <sys/socket.h>
c36583c3 37#include <sys/stat.h>
5e97c3fc 38#include <arpa/inet.h>
39#include <netinet/in.h>
40#include <net/if.h>
41
f2363e38
ÇO
42#include <lxc/lxccontainer.h>
43
fae349da 44#include "log.h"
0ed9cc8b 45#include "caps.h"
fae349da
DL
46#include "lxc.h"
47#include "conf.h"
63376d7d 48#include "cgroup.h"
fae349da 49#include "utils.h"
fae349da
DL
50#include "confile.h"
51#include "arguments.h"
36eb9bde 52
eab15c1e
CB
53#define OPT_SHARE_NET OPT_USAGE + 1
54#define OPT_SHARE_IPC OPT_USAGE + 2
55#define OPT_SHARE_UTS OPT_USAGE + 3
03df7ab5 56#define OPT_SHARE_PID OPT_USAGE + 4
9f30a190 57
6ea518f6 58lxc_log_define(lxc_start_ui, lxc);
5e97c3fc 59
33ba4ad7
CLG
60static struct lxc_list defines;
61
596a818d
DE
62static int ensure_path(char **confpath, const char *path)
63{
64 int err = -1, fd;
65 char *fullpath = NULL;
66
67 if (path) {
68 if (access(path, W_OK)) {
69 fd = creat(path, 0600);
84bdfb2b 70 if (fd < 0 && errno != EEXIST) {
596a818d
DE
71 SYSERROR("failed to create '%s'", path);
72 goto err;
73 }
84bdfb2b
SH
74 if (fd >= 0)
75 close(fd);
596a818d
DE
76 }
77
78 fullpath = realpath(path, NULL);
79 if (!fullpath) {
80 SYSERROR("failed to get the real path of '%s'", path);
81 goto err;
82 }
83
e58e6e11 84 *confpath = fullpath;
596a818d 85 }
040f39c4 86 err = EXIT_SUCCESS;
596a818d
DE
87
88err:
596a818d
DE
89 return err;
90}
91
02b4f2e1
MM
92static int pid_from_lxcname(const char *lxcname_or_pid, const char *lxcpath) {
93 char *eptr;
94 int pid = strtol(lxcname_or_pid, &eptr, 10);
95 if (*eptr != '\0' || pid < 1) {
96 struct lxc_container *s;
97 s = lxc_container_new(lxcname_or_pid, lxcpath);
98 if (!s) {
99 SYSERROR("'%s' is not a valid pid nor a container name", lxcname_or_pid);
100 return -1;
101 }
102
103 if (!s->may_control(s)) {
104 SYSERROR("Insufficient privileges to control container '%s'", s->name);
f8059880 105 lxc_container_put(s);
02b4f2e1
MM
106 return -1;
107 }
108
109 pid = s->init_pid(s);
110 if (pid < 1) {
111 SYSERROR("Is container '%s' running?", s->name);
f8059880 112 lxc_container_put(s);
02b4f2e1
MM
113 return -1;
114 }
f8059880
MM
115
116 lxc_container_put(s);
02b4f2e1
MM
117 }
118 if (kill(pid, 0) < 0) {
119 SYSERROR("Can't send signal to pid %d", pid);
120 return -1;
121 }
f8059880 122
02b4f2e1
MM
123 return pid;
124}
125
126static int open_ns(int pid, const char *ns_proc_name) {
127 int fd;
128 char path[MAXPATHLEN];
f8059880 129 snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns_proc_name);
02b4f2e1
MM
130
131 fd = open(path, O_RDONLY);
132 if (fd < 0) {
133 SYSERROR("failed to open %s", path);
134 return -1;
135 }
136 return fd;
137}
138
c36583c3
DL
139static int my_parser(struct lxc_arguments* args, int c, char* arg)
140{
141 switch (c) {
829dd918 142 case 'c': args->console = arg; break;
596a818d 143 case 'L': args->console_log = arg; break;
83758ed0 144 case 'd': args->daemonize = 1; break;
476d302c 145 case 'F': args->daemonize = 0; break;
48862401 146 case 'f': args->rcfile = arg; break;
b119f362 147 case 'C': args->close_all_fds = 1; break;
33ba4ad7 148 case 's': return lxc_config_define_add(&defines, arg);
3114c982 149 case 'p': args->pidfile = arg; break;
46926165
MM
150 case OPT_SHARE_NET: args->share_ns[LXC_NS_NET] = arg; break;
151 case OPT_SHARE_IPC: args->share_ns[LXC_NS_IPC] = arg; break;
6c544cb3 152 case OPT_SHARE_UTS: args->share_ns[LXC_NS_UTS] = arg; break;
03df7ab5 153 case OPT_SHARE_PID: args->share_ns[LXC_NS_PID] = arg; break;
c36583c3
DL
154 }
155 return 0;
156}
157
9618063c 158static const struct option my_longopts[] = {
c36583c3 159 {"daemon", no_argument, 0, 'd'},
476d302c 160 {"foreground", no_argument, 0, 'F'},
48862401 161 {"rcfile", required_argument, 0, 'f'},
33ba4ad7 162 {"define", required_argument, 0, 's'},
829dd918 163 {"console", required_argument, 0, 'c'},
596a818d 164 {"console-log", required_argument, 0, 'L'},
b119f362 165 {"close-all-fds", no_argument, 0, 'C'},
3114c982 166 {"pidfile", required_argument, 0, 'p'},
9f30a190 167 {"share-net", required_argument, 0, OPT_SHARE_NET},
3c93577b 168 {"share-ipc", required_argument, 0, OPT_SHARE_IPC},
6c544cb3 169 {"share-uts", required_argument, 0, OPT_SHARE_UTS},
03df7ab5 170 {"share-pid", required_argument, 0, OPT_SHARE_PID},
9618063c
MN
171 LXC_COMMON_OPTIONS
172};
173
174static struct lxc_arguments my_args = {
175 .progname = "lxc-start",
176 .help = "\
177--name=NAME -- COMMAND\n\
178\n\
179lxc-start start COMMAND in specified container NAME\n\
180\n\
181Options :\n\
5e8757ed 182 -n, --name=NAME NAME of the container\n\
c00f3f36
SG
183 -d, --daemon Daemonize the container (default)\n\
184 -F, --foreground Start with the current tty attached to /dev/console\n\
596a818d
DE
185 -p, --pidfile=FILE Create a file with the process id\n\
186 -f, --rcfile=FILE Load configuration file FILE\n\
187 -c, --console=FILE Use specified FILE for the container console\n\
188 -L, --console-log=FILE Log container console output to FILE\n\
189 -C, --close-all-fds If any fds are inherited, close them\n\
190 If not specified, exit with failure instead\n\
d028235d 191 Note: --daemon implies --close-all-fds\n\
9f30a190 192 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\
03df7ab5 193 --share-[net|ipc|uts|pid]=NAME Share a namespace with another container or pid\n\
9f30a190 194",
c36583c3
DL
195 .options = my_longopts,
196 .parser = my_parser,
197 .checker = NULL,
c00f3f36 198 .daemonize = 1,
3114c982 199 .pidfile = NULL,
9618063c 200};
5e97c3fc 201
202int main(int argc, char *argv[])
203{
b52b0595 204 int err = EXIT_FAILURE;
91480a0f 205 struct lxc_conf *conf;
73b910a3 206 struct lxc_log log;
91480a0f
DL
207 char *const *args;
208 char *rcfile = NULL;
9618063c 209 char *const default_args[] = {
b2b6c597 210 "/sbin/init",
13aad0ae 211 NULL,
b2b6c597 212 };
79622932 213 struct lxc_container *c;
5e97c3fc 214
33ba4ad7
CLG
215 lxc_list_init(&defines);
216
0ed9cc8b 217 if (lxc_caps_init())
b52b0595 218 exit(err);
0ed9cc8b 219
e043236e 220 if (lxc_arguments_parse(&my_args, argc, argv))
b52b0595 221 exit(err);
5e97c3fc 222
9618063c 223 if (!my_args.argc)
f79d43bb 224 args = default_args;
9618063c
MN
225 else
226 args = my_args.argv;
5e97c3fc 227
73b910a3 228 log.name = my_args.name;
229 log.file = my_args.log_file;
4b73005c 230 log.level = my_args.log_priority;
73b910a3 231 log.prefix = my_args.progname;
232 log.quiet = my_args.quiet;
233 log.lxcpath = my_args.lxcpath[0];
234
235 if (lxc_log_init(&log))
b52b0595 236 exit(err);
6edbfc86 237 lxc_log_options_no_override();
51cab631 238
37180208 239 if (access(my_args.lxcpath[0], O_RDONLY) < 0) {
040f39c4
CB
240 if (!my_args.quiet)
241 fprintf(stderr, "You lack access to %s\n", my_args.lxcpath[0]);
242 exit(err);
243 }
244
69733b5d
SH
245 const char *lxcpath = my_args.lxcpath[0];
246
79622932
SH
247 /*
248 * rcfile possibilities:
249 * 1. rcfile from random path specified in cli option
250 * 2. rcfile not specified, use $lxcpath/$lxcname/config
251 * 3. rcfile not specified and does not exist.
252 */
96c210bb 253 /* rcfile is specified in the cli option */
79622932 254 if (my_args.rcfile) {
96c210bb 255 rcfile = (char *)my_args.rcfile;
69733b5d 256 c = lxc_container_new(my_args.name, lxcpath);
79622932
SH
257 if (!c) {
258 ERROR("Failed to create lxc_container");
b52b0595 259 exit(err);
79622932 260 }
4df7f012 261 c->clear_config(c);
79622932
SH
262 if (!c->load_config(c, rcfile)) {
263 ERROR("Failed to load rcfile");
264 lxc_container_put(c);
b52b0595 265 exit(err);
79622932 266 }
b586db43
WB
267 c->configfile = strdup(my_args.rcfile);
268 if (!c->configfile) {
269 ERROR("Out of memory setting new config filename");
270 goto out;
271 }
79622932 272 } else {
fa9ab205
NL
273 int rc;
274
79622932 275 rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
fa9ab205 276 if (rc == -1) {
96c210bb 277 SYSERROR("failed to allocate memory");
b52b0595 278 exit(err);
96c210bb 279 }
444f3ca2 280 INFO("using rcfile %s", rcfile);
96c210bb
DL
281
282 /* container configuration does not exist */
283 if (access(rcfile, F_OK)) {
284 free(rcfile);
285 rcfile = NULL;
79622932
SH
286 }
287 c = lxc_container_new(my_args.name, lxcpath);
288 if (!c) {
289 ERROR("Failed to create lxc_container");
b52b0595 290 exit(err);
96c210bb
DL
291 }
292 }
293
72c78e0e
CB
294 /* We do not check here whether the container is defined, because we
295 * support volatile containers. Which means the container does not need
296 * to be created for it to be started. You can just pass a configuration
297 * file as argument and start the container right away.
298 */
040f39c4
CB
299
300 if (!c->may_control(c)) {
301 fprintf(stderr, "Insufficent privileges to control %s\n", c->name);
302 goto out;
303 }
304
312f9c5d
DY
305 if (c->is_running(c)) {
306 ERROR("Container is already running.");
040f39c4 307 err = EXIT_SUCCESS;
312f9c5d
DY
308 goto out;
309 }
79622932
SH
310 /*
311 * We should use set_config_item() over &defines, which would handle
312 * unset c->lxc_conf for us and let us not use lxc_config_define_load()
313 */
314 if (!c->lxc_conf)
315 c->lxc_conf = lxc_conf_init();
316 conf = c->lxc_conf;
fae349da 317
33ba4ad7 318 if (lxc_config_define_load(&defines, conf))
2d4bcb96 319 goto out;
33ba4ad7 320
f2ae79a0 321 if (!rcfile && !strcmp("/sbin/init", args[0])) {
79622932 322 ERROR("Executing '/sbin/init' with no configuration file may crash the host");
2d4bcb96 323 goto out;
f2ae79a0
AN
324 }
325
596a818d
DE
326 if (ensure_path(&conf->console.path, my_args.console) < 0) {
327 ERROR("failed to ensure console path '%s'", my_args.console);
2d4bcb96 328 goto out;
596a818d 329 }
829dd918 330
596a818d
DE
331 if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) {
332 ERROR("failed to ensure console log '%s'", my_args.console_log);
2d4bcb96 333 goto out;
829dd918
DL
334 }
335
3114c982 336 if (my_args.pidfile != NULL) {
72cf75fa
QH
337 if (ensure_path(&c->pidfile, my_args.pidfile) < 0) {
338 ERROR("failed to ensure pidfile '%s'", my_args.pidfile);
339 goto out;
340 }
3114c982
NC
341 }
342
46926165
MM
343 int i;
344 for (i = 0; i < LXC_NS_MAX; i++) {
345 if (my_args.share_ns[i] == NULL)
346 continue;
9f30a190 347
46926165 348 int pid = pid_from_lxcname(my_args.share_ns[i], lxcpath);
3c93577b
MM
349 if (pid < 1)
350 goto out;
351
46926165 352 int fd = open_ns(pid, ns_info[i].proc_name);
3c93577b
MM
353 if (fd < 0)
354 goto out;
46926165 355 conf->inherit_ns_fd[i] = fd;
3c93577b
MM
356 }
357
c8ad5f46
SH
358 if (!my_args.daemonize) {
359 c->want_daemonize(c, false);
c36583c3
DL
360 }
361
b119f362 362 if (my_args.close_all_fds)
540f932a 363 c->want_close_all_fds(c, true);
b119f362 364
67c660d0 365 if (args == default_args)
b52b0595 366 err = c->start(c, 0, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
67c660d0 367 else
b52b0595 368 err = c->start(c, 0, args) ? EXIT_SUCCESS : EXIT_FAILURE;
307ab05d
SG
369
370 if (err) {
371 ERROR("The container failed to start.");
372 if (my_args.daemonize)
373 ERROR("To get more details, run the container in foreground mode.");
374 ERROR("Additional information can be obtained by setting the "
a90842e4 375 "--logfile and --logpriority options.");
d4ef230c
RV
376 err = c->error_num;
377 lxc_container_put(c);
b52b0595 378 exit(err);
307ab05d
SG
379 }
380
2d4bcb96 381out:
79622932 382 lxc_container_put(c);
b52b0595 383 exit(err);
5e97c3fc 384}