]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/execute.c
init: become session leader
[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"
8afb3e61 33#include "utils.h"
0ae4f887
GK
34
35lxc_log_define(lxc_execute, lxc_start);
36
37struct execute_args {
38 char *const *argv;
39 int quiet;
40};
41
42static 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;
e0b0b533 47 int argc = 0, argc_add;
a0a2066d 48 char *initpath;
0ae4f887
GK
49
50 while (my_args->argv[argc++]);
51
e0b0b533
DE
52 argc_add = 4;
53 if (my_args->quiet)
54 argc_add++;
fabf7361
QH
55 if (!handler->conf->rootfs.path) {
56 argc_add += 4;
57 if (lxc_log_has_valid_level())
58 argc_add += 2;
59 }
e0b0b533
DE
60
61 argv = malloc((argc + argc_add) * sizeof(*argv));
0ae4f887 62 if (!argv)
e0b0b533 63 goto out1;
0ae4f887 64
9d9c111c 65 initpath = choose_init(NULL);
a0a2066d 66 if (!initpath) {
1e1d1dca 67 ERROR("Failed to find an lxc-init or init.lxc");
e0b0b533 68 goto out2;
a0a2066d
SH
69 }
70 argv[i++] = initpath;
0ae4f887
GK
71 if (my_args->quiet)
72 argv[i++] = "--quiet";
e0b0b533
DE
73 if (!handler->conf->rootfs.path) {
74 argv[i++] = "--name";
75 argv[i++] = (char *)handler->name;
76 argv[i++] = "--lxcpath";
77 argv[i++] = (char *)handler->lxcpath;
fabf7361
QH
78
79 if (lxc_log_has_valid_level()) {
80 argv[i++] = "--logpriority";
81 argv[i++] = (char *)
82 lxc_log_priority_to_string(lxc_log_get_level());
83 }
e0b0b533 84 }
0ae4f887
GK
85 argv[i++] = "--";
86 for (j = 0; j < argc; j++)
87 argv[i++] = my_args->argv[j];
88 argv[i++] = NULL;
89
90 NOTICE("exec'ing '%s'", my_args->argv[0]);
91
92 execvp(argv[0], argv);
93 SYSERROR("failed to exec %s", argv[0]);
e0b0b533
DE
94 free(initpath);
95out2:
96 free(argv);
97out1:
0ae4f887
GK
98 return 1;
99}
100
101static int execute_post_start(struct lxc_handler *handler, void* data)
102{
103 struct execute_args *my_args = data;
104 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
105 return 0;
106}
107
108static struct lxc_operations execute_start_ops = {
109 .start = execute_start,
110 .post_start = execute_post_start
111};
112
113int lxc_execute(const char *name, char *const argv[], int quiet,
aa460476
CB
114 struct lxc_handler *handler, const char *lxcpath,
115 bool backgrounded)
0ae4f887 116{
aa460476 117 struct execute_args args = {.argv = argv, .quiet = quiet};
0ae4f887 118
47a46cf1 119 if (lxc_check_inherited(handler->conf, false, &handler->conf->maincmd_fd, 1))
0ae4f887
GK
120 return -1;
121
aa460476
CB
122 handler->conf->is_execute = 1;
123 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
124 backgrounded);
0ae4f887 125}