]>
Commit | Line | Data |
---|---|---|
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 | ||
d38dd64a CB |
24 | #ifndef _GNU_SOURCE |
25 | #define _GNU_SOURCE 1 | |
26 | #endif | |
0ae4f887 | 27 | #include <errno.h> |
cd90db2c | 28 | #include <stdio.h> |
d38dd64a CB |
29 | #include <stdlib.h> |
30 | #include <sys/stat.h> | |
31 | #include <sys/types.h> | |
32 | #include <unistd.h> | |
0ae4f887 | 33 | |
e0b0b533 | 34 | #include "conf.h" |
d38dd64a | 35 | #include "config.h" |
0ae4f887 GK |
36 | #include "log.h" |
37 | #include "start.h" | |
13be2733 | 38 | #include "raw_syscalls.h" |
8afb3e61 | 39 | #include "utils.h" |
0ae4f887 | 40 | |
ac2cecc4 | 41 | lxc_log_define(execute, start); |
0ae4f887 | 42 | |
0ae4f887 GK |
43 | static int execute_start(struct lxc_handler *handler, void* data) |
44 | { | |
321614a5 CB |
45 | int argc_add, j; |
46 | char **argv; | |
47 | int argc = 0, i = 0, logfd = -1; | |
0ae4f887 | 48 | struct execute_args *my_args = data; |
321614a5 | 49 | char logfile[LXC_PROC_PID_FD_LEN]; |
5c618243 | 50 | bool is_privileged = lxc_list_empty(&handler->conf->id_map); |
0ae4f887 GK |
51 | |
52 | while (my_args->argv[argc++]); | |
53 | ||
858faf70 SH |
54 | /* lxc-init -n name -- [argc] NULL -> 5 */ |
55 | argc_add = 5; | |
e0b0b533 DE |
56 | if (my_args->quiet) |
57 | argc_add++; | |
321614a5 | 58 | |
858faf70 SH |
59 | if (!handler->conf->rootfs.path) |
60 | argc_add += 2; | |
321614a5 | 61 | |
5c618243 CB |
62 | if (is_privileged) { |
63 | if (lxc_log_has_valid_level()) | |
64 | argc_add += 2; | |
e0b0b533 | 65 | |
5c618243 CB |
66 | if (current_config->logfd != -1 || lxc_log_fd != -1) |
67 | argc_add += 2; | |
68 | } | |
9c40b2d9 | 69 | |
e0b0b533 | 70 | argv = malloc((argc + argc_add) * sizeof(*argv)); |
b2efeb0b TA |
71 | if (!argv) { |
72 | SYSERROR("Allocating init args failed"); | |
e0b0b533 | 73 | goto out1; |
b2efeb0b | 74 | } |
0ae4f887 | 75 | |
4b5b3a2a TA |
76 | if (my_args->init_path) |
77 | argv[i++] = my_args->init_path; | |
78 | else | |
79 | argv[i++] = "lxc-init"; | |
a6f151a7 CB |
80 | |
81 | argv[i++] = "-n"; | |
82 | argv[i++] = (char *)handler->name; | |
83 | ||
84 | if (lxc_log_has_valid_level()) { | |
85 | argv[i++] = "-l"; | |
86 | argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level()); | |
87 | } | |
88 | ||
5c618243 | 89 | if (is_privileged && (current_config->logfd != -1 || lxc_log_fd != -1)) { |
321614a5 | 90 | int ret; |
cd90db2c TA |
91 | int to_dup = current_config->logfd; |
92 | ||
93 | if (current_config->logfd == -1) | |
94 | to_dup = lxc_log_fd; | |
95 | ||
321614a5 CB |
96 | logfd = dup(to_dup); |
97 | if (logfd < 0) { | |
98 | SYSERROR("Failed to duplicate log file descriptor"); | |
cd90db2c TA |
99 | goto out2; |
100 | } | |
101 | ||
bf58a980 | 102 | ret = snprintf(logfile, sizeof(logfile), "/proc/self/fd/%d", logfd); |
321614a5 | 103 | if (ret < 0 || (size_t)ret >= sizeof(logfile)) |
cd90db2c | 104 | goto out3; |
cd90db2c | 105 | |
8f98ac7b | 106 | argv[i++] = "-o"; |
321614a5 | 107 | argv[i++] = logfile; |
8f98ac7b CB |
108 | } |
109 | ||
0ae4f887 GK |
110 | if (my_args->quiet) |
111 | argv[i++] = "--quiet"; | |
a6f151a7 | 112 | |
e0b0b533 | 113 | if (!handler->conf->rootfs.path) { |
a6f151a7 | 114 | argv[i++] = "-P"; |
e0b0b533 | 115 | argv[i++] = (char *)handler->lxcpath; |
e0b0b533 | 116 | } |
a6f151a7 | 117 | |
0ae4f887 GK |
118 | argv[i++] = "--"; |
119 | for (j = 0; j < argc; j++) | |
120 | argv[i++] = my_args->argv[j]; | |
121 | argv[i++] = NULL; | |
122 | ||
a6f151a7 | 123 | NOTICE("Exec'ing \"%s\"", my_args->argv[0]); |
0ae4f887 | 124 | |
4b5b3a2a | 125 | if (my_args->init_fd >= 0) |
13be2733 | 126 | lxc_raw_execveat(my_args->init_fd, "", argv, environ, AT_EMPTY_PATH); |
4b5b3a2a TA |
127 | else |
128 | execvp(argv[0], argv); | |
a6f151a7 | 129 | SYSERROR("Failed to exec %s", argv[0]); |
794248d0 | 130 | |
cd90db2c | 131 | out3: |
321614a5 | 132 | close(logfd); |
e0b0b533 DE |
133 | out2: |
134 | free(argv); | |
135 | out1: | |
0ae4f887 GK |
136 | return 1; |
137 | } | |
138 | ||
139 | static int execute_post_start(struct lxc_handler *handler, void* data) | |
140 | { | |
141 | struct execute_args *my_args = data; | |
142 | NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid); | |
143 | return 0; | |
144 | } | |
145 | ||
146 | static struct lxc_operations execute_start_ops = { | |
147 | .start = execute_start, | |
148 | .post_start = execute_post_start | |
149 | }; | |
150 | ||
151 | int lxc_execute(const char *name, char *const argv[], int quiet, | |
aa460476 | 152 | struct lxc_handler *handler, const char *lxcpath, |
bb955810 | 153 | bool daemonize, int *error_num) |
0ae4f887 | 154 | { |
aa460476 | 155 | struct execute_args args = {.argv = argv, .quiet = quiet}; |
0ae4f887 | 156 | |
b2efeb0b | 157 | TRACE("Doing lxc_execute"); |
07c4ea31 | 158 | handler->conf->is_execute = true; |
aa460476 | 159 | return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath, |
bb955810 | 160 | daemonize, error_num); |
0ae4f887 | 161 | } |