]>
Commit | Line | Data |
---|---|---|
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 | |
9f30a190 | 53 | #define OPT_SHARE_NET OPT_USAGE+1 |
3c93577b | 54 | #define OPT_SHARE_IPC OPT_USAGE+2 |
6c544cb3 | 55 | #define OPT_SHARE_UTS OPT_USAGE+3 |
9f30a190 | 56 | |
6ea518f6 | 57 | lxc_log_define(lxc_start_ui, lxc); |
5e97c3fc | 58 | |
33ba4ad7 CLG |
59 | static struct lxc_list defines; |
60 | ||
596a818d DE |
61 | static int ensure_path(char **confpath, const char *path) |
62 | { | |
63 | int err = -1, fd; | |
64 | char *fullpath = NULL; | |
65 | ||
66 | if (path) { | |
67 | if (access(path, W_OK)) { | |
68 | fd = creat(path, 0600); | |
84bdfb2b | 69 | if (fd < 0 && errno != EEXIST) { |
596a818d DE |
70 | SYSERROR("failed to create '%s'", path); |
71 | goto err; | |
72 | } | |
84bdfb2b SH |
73 | if (fd >= 0) |
74 | close(fd); | |
596a818d DE |
75 | } |
76 | ||
77 | fullpath = realpath(path, NULL); | |
78 | if (!fullpath) { | |
79 | SYSERROR("failed to get the real path of '%s'", path); | |
80 | goto err; | |
81 | } | |
82 | ||
83 | *confpath = strdup(fullpath); | |
84 | if (!*confpath) { | |
85 | ERROR("failed to dup string '%s'", fullpath); | |
86 | goto err; | |
87 | } | |
88 | } | |
89 | err = 0; | |
90 | ||
91 | err: | |
92 | if (fullpath) | |
93 | free(fullpath); | |
94 | return err; | |
95 | } | |
96 | ||
02b4f2e1 MM |
97 | static int pid_from_lxcname(const char *lxcname_or_pid, const char *lxcpath) { |
98 | char *eptr; | |
99 | int pid = strtol(lxcname_or_pid, &eptr, 10); | |
100 | if (*eptr != '\0' || pid < 1) { | |
101 | struct lxc_container *s; | |
102 | s = lxc_container_new(lxcname_or_pid, lxcpath); | |
103 | if (!s) { | |
104 | SYSERROR("'%s' is not a valid pid nor a container name", lxcname_or_pid); | |
105 | return -1; | |
106 | } | |
107 | ||
108 | if (!s->may_control(s)) { | |
109 | SYSERROR("Insufficient privileges to control container '%s'", s->name); | |
f8059880 | 110 | lxc_container_put(s); |
02b4f2e1 MM |
111 | return -1; |
112 | } | |
113 | ||
114 | pid = s->init_pid(s); | |
115 | if (pid < 1) { | |
116 | SYSERROR("Is container '%s' running?", s->name); | |
f8059880 | 117 | lxc_container_put(s); |
02b4f2e1 MM |
118 | return -1; |
119 | } | |
f8059880 MM |
120 | |
121 | lxc_container_put(s); | |
02b4f2e1 MM |
122 | } |
123 | if (kill(pid, 0) < 0) { | |
124 | SYSERROR("Can't send signal to pid %d", pid); | |
125 | return -1; | |
126 | } | |
f8059880 | 127 | |
02b4f2e1 MM |
128 | return pid; |
129 | } | |
130 | ||
131 | static int open_ns(int pid, const char *ns_proc_name) { | |
132 | int fd; | |
133 | char path[MAXPATHLEN]; | |
f8059880 | 134 | snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns_proc_name); |
02b4f2e1 MM |
135 | |
136 | fd = open(path, O_RDONLY); | |
137 | if (fd < 0) { | |
138 | SYSERROR("failed to open %s", path); | |
139 | return -1; | |
140 | } | |
141 | return fd; | |
142 | } | |
143 | ||
c36583c3 DL |
144 | static int my_parser(struct lxc_arguments* args, int c, char* arg) |
145 | { | |
146 | switch (c) { | |
829dd918 | 147 | case 'c': args->console = arg; break; |
596a818d | 148 | case 'L': args->console_log = arg; break; |
83758ed0 | 149 | case 'd': args->daemonize = 1; break; |
48862401 | 150 | case 'f': args->rcfile = arg; break; |
b119f362 | 151 | case 'C': args->close_all_fds = 1; break; |
33ba4ad7 | 152 | case 's': return lxc_config_define_add(&defines, arg); |
3114c982 | 153 | case 'p': args->pidfile = arg; break; |
46926165 MM |
154 | case OPT_SHARE_NET: args->share_ns[LXC_NS_NET] = arg; break; |
155 | case OPT_SHARE_IPC: args->share_ns[LXC_NS_IPC] = arg; break; | |
6c544cb3 | 156 | case OPT_SHARE_UTS: args->share_ns[LXC_NS_UTS] = arg; break; |
c36583c3 DL |
157 | } |
158 | return 0; | |
159 | } | |
160 | ||
9618063c | 161 | static const struct option my_longopts[] = { |
c36583c3 | 162 | {"daemon", no_argument, 0, 'd'}, |
48862401 | 163 | {"rcfile", required_argument, 0, 'f'}, |
33ba4ad7 | 164 | {"define", required_argument, 0, 's'}, |
829dd918 | 165 | {"console", required_argument, 0, 'c'}, |
596a818d | 166 | {"console-log", required_argument, 0, 'L'}, |
b119f362 | 167 | {"close-all-fds", no_argument, 0, 'C'}, |
3114c982 | 168 | {"pidfile", required_argument, 0, 'p'}, |
9f30a190 | 169 | {"share-net", required_argument, 0, OPT_SHARE_NET}, |
3c93577b | 170 | {"share-ipc", required_argument, 0, OPT_SHARE_IPC}, |
6c544cb3 | 171 | {"share-uts", required_argument, 0, OPT_SHARE_UTS}, |
9618063c MN |
172 | LXC_COMMON_OPTIONS |
173 | }; | |
174 | ||
175 | static struct lxc_arguments my_args = { | |
176 | .progname = "lxc-start", | |
177 | .help = "\ | |
178 | --name=NAME -- COMMAND\n\ | |
179 | \n\ | |
180 | lxc-start start COMMAND in specified container NAME\n\ | |
181 | \n\ | |
182 | Options :\n\ | |
596a818d DE |
183 | -n, --name=NAME NAME for name of the container\n\ |
184 | -d, --daemon daemonize the container\n\ | |
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\ | |
191 | Note: --daemon implies --close-all-fds\n\ | |
9f30a190 | 192 | -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\ |
304dc8b3 | 193 | --share-[net|ipc|uts]=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, | |
198 | .daemonize = 0, | |
3114c982 | 199 | .pidfile = NULL, |
9618063c | 200 | }; |
5e97c3fc | 201 | |
202 | int main(int argc, char *argv[]) | |
203 | { | |
a7440c15 | 204 | int err = 1; |
91480a0f DL |
205 | struct lxc_conf *conf; |
206 | char *const *args; | |
207 | char *rcfile = NULL; | |
9618063c | 208 | char *const default_args[] = { |
b2b6c597 | 209 | "/sbin/init", |
13aad0ae | 210 | NULL, |
b2b6c597 | 211 | }; |
79622932 | 212 | struct lxc_container *c; |
5e97c3fc | 213 | |
33ba4ad7 CLG |
214 | lxc_list_init(&defines); |
215 | ||
0ed9cc8b DL |
216 | if (lxc_caps_init()) |
217 | return err; | |
218 | ||
e043236e MN |
219 | if (lxc_arguments_parse(&my_args, argc, argv)) |
220 | return err; | |
5e97c3fc | 221 | |
9618063c | 222 | if (!my_args.argc) |
f79d43bb | 223 | args = default_args; |
9618063c MN |
224 | else |
225 | args = my_args.argv; | |
5e97c3fc | 226 | |
5e1e7aaf | 227 | if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority, |
8d06bd13 | 228 | my_args.progname, my_args.quiet, my_args.lxcpath[0])) |
e043236e | 229 | return err; |
6edbfc86 | 230 | lxc_log_options_no_override(); |
51cab631 | 231 | |
69733b5d SH |
232 | const char *lxcpath = my_args.lxcpath[0]; |
233 | ||
79622932 SH |
234 | /* |
235 | * rcfile possibilities: | |
236 | * 1. rcfile from random path specified in cli option | |
237 | * 2. rcfile not specified, use $lxcpath/$lxcname/config | |
238 | * 3. rcfile not specified and does not exist. | |
239 | */ | |
96c210bb | 240 | /* rcfile is specified in the cli option */ |
79622932 | 241 | if (my_args.rcfile) { |
96c210bb | 242 | rcfile = (char *)my_args.rcfile; |
69733b5d | 243 | c = lxc_container_new(my_args.name, lxcpath); |
79622932 SH |
244 | if (!c) { |
245 | ERROR("Failed to create lxc_container"); | |
246 | return err; | |
247 | } | |
4df7f012 | 248 | c->clear_config(c); |
79622932 SH |
249 | if (!c->load_config(c, rcfile)) { |
250 | ERROR("Failed to load rcfile"); | |
251 | lxc_container_put(c); | |
252 | return err; | |
253 | } | |
254 | } else { | |
fa9ab205 NL |
255 | int rc; |
256 | ||
79622932 | 257 | rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name); |
fa9ab205 | 258 | if (rc == -1) { |
96c210bb DL |
259 | SYSERROR("failed to allocate memory"); |
260 | return err; | |
261 | } | |
444f3ca2 | 262 | INFO("using rcfile %s", rcfile); |
96c210bb DL |
263 | |
264 | /* container configuration does not exist */ | |
265 | if (access(rcfile, F_OK)) { | |
266 | free(rcfile); | |
267 | rcfile = NULL; | |
79622932 SH |
268 | } |
269 | c = lxc_container_new(my_args.name, lxcpath); | |
270 | if (!c) { | |
271 | ERROR("Failed to create lxc_container"); | |
272 | return err; | |
96c210bb DL |
273 | } |
274 | } | |
275 | ||
79622932 SH |
276 | /* |
277 | * We should use set_config_item() over &defines, which would handle | |
278 | * unset c->lxc_conf for us and let us not use lxc_config_define_load() | |
279 | */ | |
280 | if (!c->lxc_conf) | |
281 | c->lxc_conf = lxc_conf_init(); | |
282 | conf = c->lxc_conf; | |
fae349da | 283 | |
33ba4ad7 | 284 | if (lxc_config_define_load(&defines, conf)) |
2d4bcb96 | 285 | goto out; |
33ba4ad7 | 286 | |
f2ae79a0 | 287 | if (!rcfile && !strcmp("/sbin/init", args[0])) { |
79622932 | 288 | ERROR("Executing '/sbin/init' with no configuration file may crash the host"); |
2d4bcb96 | 289 | goto out; |
f2ae79a0 AN |
290 | } |
291 | ||
596a818d DE |
292 | if (ensure_path(&conf->console.path, my_args.console) < 0) { |
293 | ERROR("failed to ensure console path '%s'", my_args.console); | |
2d4bcb96 | 294 | goto out; |
596a818d | 295 | } |
829dd918 | 296 | |
596a818d DE |
297 | if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) { |
298 | ERROR("failed to ensure console log '%s'", my_args.console_log); | |
2d4bcb96 | 299 | goto out; |
829dd918 DL |
300 | } |
301 | ||
3114c982 | 302 | if (my_args.pidfile != NULL) { |
72cf75fa QH |
303 | if (ensure_path(&c->pidfile, my_args.pidfile) < 0) { |
304 | ERROR("failed to ensure pidfile '%s'", my_args.pidfile); | |
305 | goto out; | |
306 | } | |
3114c982 NC |
307 | } |
308 | ||
46926165 MM |
309 | int i; |
310 | for (i = 0; i < LXC_NS_MAX; i++) { | |
311 | if (my_args.share_ns[i] == NULL) | |
312 | continue; | |
9f30a190 | 313 | |
46926165 | 314 | int pid = pid_from_lxcname(my_args.share_ns[i], lxcpath); |
3c93577b MM |
315 | if (pid < 1) |
316 | goto out; | |
317 | ||
46926165 | 318 | int fd = open_ns(pid, ns_info[i].proc_name); |
3c93577b MM |
319 | if (fd < 0) |
320 | goto out; | |
46926165 | 321 | conf->inherit_ns_fd[i] = fd; |
3c93577b MM |
322 | } |
323 | ||
c8ad5f46 SH |
324 | if (!my_args.daemonize) { |
325 | c->want_daemonize(c, false); | |
c36583c3 DL |
326 | } |
327 | ||
b119f362 | 328 | if (my_args.close_all_fds) |
540f932a | 329 | c->want_close_all_fds(c, true); |
b119f362 | 330 | |
a7440c15 | 331 | err = c->start(c, 0, args) ? 0 : 1; |
307ab05d SG |
332 | |
333 | if (err) { | |
334 | ERROR("The container failed to start."); | |
335 | if (my_args.daemonize) | |
336 | ERROR("To get more details, run the container in foreground mode."); | |
337 | ERROR("Additional information can be obtained by setting the " | |
338 | "--logfile and --log-priority options."); | |
339 | } | |
340 | ||
2d4bcb96 | 341 | out: |
79622932 | 342 | lxc_container_put(c); |
b0a33c1e | 343 | return err; |
5e97c3fc | 344 | } |