]>
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 | */ |
96c210bb | 23 | #define _GNU_SOURCE |
5e97c3fc | 24 | #include <stdio.h> |
96c210bb | 25 | #undef _GNU_SOURCE |
5e97c3fc | 26 | #include <libgen.h> |
96c210bb | 27 | #include <stdlib.h> |
5e97c3fc | 28 | #include <unistd.h> |
29 | #include <string.h> | |
b0a33c1e | 30 | #include <termios.h> |
31 | #include <errno.h> | |
c36583c3 | 32 | #include <fcntl.h> |
f8e09a0b | 33 | #include <signal.h> |
5e97c3fc | 34 | #include <sys/param.h> |
35 | #include <sys/utsname.h> | |
36 | #include <sys/types.h> | |
37 | #include <sys/socket.h> | |
c36583c3 | 38 | #include <sys/stat.h> |
5e97c3fc | 39 | #include <arpa/inet.h> |
40 | #include <netinet/in.h> | |
41 | #include <net/if.h> | |
42 | ||
fae349da | 43 | #include "log.h" |
0ed9cc8b | 44 | #include "caps.h" |
fae349da | 45 | #include "lxc.h" |
948955a2 | 46 | #include <lxc/lxccontainer.h> |
fae349da | 47 | #include "conf.h" |
63376d7d | 48 | #include "cgroup.h" |
fae349da | 49 | #include "utils.h" |
96c210bb | 50 | #include "config.h" |
fae349da DL |
51 | #include "confile.h" |
52 | #include "arguments.h" | |
36eb9bde | 53 | |
9f30a190 | 54 | #define OPT_SHARE_NET OPT_USAGE+1 |
3c93577b | 55 | #define OPT_SHARE_IPC OPT_USAGE+2 |
9f30a190 | 56 | |
77075659 | 57 | lxc_log_define(lxc_start_ui, lxc_start); |
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; | |
c36583c3 DL |
156 | } |
157 | return 0; | |
158 | } | |
159 | ||
9618063c | 160 | static const struct option my_longopts[] = { |
c36583c3 | 161 | {"daemon", no_argument, 0, 'd'}, |
48862401 | 162 | {"rcfile", required_argument, 0, 'f'}, |
33ba4ad7 | 163 | {"define", required_argument, 0, 's'}, |
829dd918 | 164 | {"console", required_argument, 0, 'c'}, |
596a818d | 165 | {"console-log", required_argument, 0, 'L'}, |
b119f362 | 166 | {"close-all-fds", no_argument, 0, 'C'}, |
3114c982 | 167 | {"pidfile", required_argument, 0, 'p'}, |
9f30a190 | 168 | {"share-net", required_argument, 0, OPT_SHARE_NET}, |
3c93577b | 169 | {"share-ipc", required_argument, 0, OPT_SHARE_IPC}, |
9618063c MN |
170 | LXC_COMMON_OPTIONS |
171 | }; | |
172 | ||
173 | static struct lxc_arguments my_args = { | |
174 | .progname = "lxc-start", | |
175 | .help = "\ | |
176 | --name=NAME -- COMMAND\n\ | |
177 | \n\ | |
178 | lxc-start start COMMAND in specified container NAME\n\ | |
179 | \n\ | |
180 | Options :\n\ | |
596a818d DE |
181 | -n, --name=NAME NAME for name of the container\n\ |
182 | -d, --daemon daemonize the container\n\ | |
183 | -p, --pidfile=FILE Create a file with the process id\n\ | |
184 | -f, --rcfile=FILE Load configuration file FILE\n\ | |
185 | -c, --console=FILE Use specified FILE for the container console\n\ | |
186 | -L, --console-log=FILE Log container console output to FILE\n\ | |
187 | -C, --close-all-fds If any fds are inherited, close them\n\ | |
188 | If not specified, exit with failure instead\n\ | |
189 | Note: --daemon implies --close-all-fds\n\ | |
9f30a190 | 190 | -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\ |
02b4f2e1 | 191 | --share-net=NAME Share a network namespace with another container or pid\n\ |
3c93577b | 192 | --share-ipc=NAME Share an IPC namespace with another container or pid\n\ |
9f30a190 | 193 | ", |
c36583c3 DL |
194 | .options = my_longopts, |
195 | .parser = my_parser, | |
196 | .checker = NULL, | |
197 | .daemonize = 0, | |
3114c982 | 198 | .pidfile = NULL, |
9618063c | 199 | }; |
5e97c3fc | 200 | |
201 | int main(int argc, char *argv[]) | |
202 | { | |
e043236e | 203 | int err = -1; |
91480a0f DL |
204 | struct lxc_conf *conf; |
205 | char *const *args; | |
206 | char *rcfile = NULL; | |
9618063c | 207 | char *const default_args[] = { |
b2b6c597 | 208 | "/sbin/init", |
209 | '\0', | |
210 | }; | |
3114c982 | 211 | FILE *pid_fp = NULL; |
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; |
51cab631 | 230 | |
69733b5d SH |
231 | const char *lxcpath = my_args.lxcpath[0]; |
232 | ||
79622932 SH |
233 | /* |
234 | * rcfile possibilities: | |
235 | * 1. rcfile from random path specified in cli option | |
236 | * 2. rcfile not specified, use $lxcpath/$lxcname/config | |
237 | * 3. rcfile not specified and does not exist. | |
238 | */ | |
96c210bb | 239 | /* rcfile is specified in the cli option */ |
79622932 | 240 | if (my_args.rcfile) { |
96c210bb | 241 | rcfile = (char *)my_args.rcfile; |
69733b5d | 242 | c = lxc_container_new(my_args.name, lxcpath); |
79622932 SH |
243 | if (!c) { |
244 | ERROR("Failed to create lxc_container"); | |
245 | return err; | |
246 | } | |
247 | if (!c->load_config(c, rcfile)) { | |
248 | ERROR("Failed to load rcfile"); | |
249 | lxc_container_put(c); | |
250 | return err; | |
251 | } | |
252 | } else { | |
fa9ab205 NL |
253 | int rc; |
254 | ||
79622932 | 255 | rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name); |
fa9ab205 | 256 | if (rc == -1) { |
96c210bb DL |
257 | SYSERROR("failed to allocate memory"); |
258 | return err; | |
259 | } | |
444f3ca2 | 260 | INFO("using rcfile %s", rcfile); |
96c210bb DL |
261 | |
262 | /* container configuration does not exist */ | |
263 | if (access(rcfile, F_OK)) { | |
264 | free(rcfile); | |
265 | rcfile = NULL; | |
79622932 SH |
266 | } |
267 | c = lxc_container_new(my_args.name, lxcpath); | |
268 | if (!c) { | |
269 | ERROR("Failed to create lxc_container"); | |
270 | return err; | |
96c210bb DL |
271 | } |
272 | } | |
273 | ||
79622932 SH |
274 | /* |
275 | * We should use set_config_item() over &defines, which would handle | |
276 | * unset c->lxc_conf for us and let us not use lxc_config_define_load() | |
277 | */ | |
278 | if (!c->lxc_conf) | |
279 | c->lxc_conf = lxc_conf_init(); | |
280 | conf = c->lxc_conf; | |
fae349da | 281 | |
33ba4ad7 | 282 | if (lxc_config_define_load(&defines, conf)) |
2d4bcb96 | 283 | goto out; |
33ba4ad7 | 284 | |
f2ae79a0 | 285 | if (!rcfile && !strcmp("/sbin/init", args[0])) { |
79622932 | 286 | ERROR("Executing '/sbin/init' with no configuration file may crash the host"); |
2d4bcb96 | 287 | goto out; |
f2ae79a0 AN |
288 | } |
289 | ||
596a818d DE |
290 | if (ensure_path(&conf->console.path, my_args.console) < 0) { |
291 | ERROR("failed to ensure console path '%s'", my_args.console); | |
2d4bcb96 | 292 | goto out; |
596a818d | 293 | } |
829dd918 | 294 | |
596a818d DE |
295 | if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) { |
296 | ERROR("failed to ensure console log '%s'", my_args.console_log); | |
2d4bcb96 | 297 | goto out; |
829dd918 DL |
298 | } |
299 | ||
3114c982 NC |
300 | if (my_args.pidfile != NULL) { |
301 | pid_fp = fopen(my_args.pidfile, "w"); | |
302 | if (pid_fp == NULL) { | |
c3752c0b SG |
303 | SYSERROR("failed to create pidfile '%s' for '%s'", |
304 | my_args.pidfile, my_args.name); | |
2d4bcb96 | 305 | goto out; |
3114c982 NC |
306 | } |
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 | ||
b4df0a1e | 324 | if (my_args.daemonize) { |
79622932 | 325 | c->want_daemonize(c); |
c36583c3 DL |
326 | } |
327 | ||
3114c982 NC |
328 | if (pid_fp != NULL) { |
329 | if (fprintf(pid_fp, "%d\n", getpid()) < 0) { | |
330 | SYSERROR("failed to write '%s'", my_args.pidfile); | |
2d4bcb96 | 331 | goto out; |
3114c982 NC |
332 | } |
333 | fclose(pid_fp); | |
334 | } | |
335 | ||
b119f362 | 336 | if (my_args.close_all_fds) |
130a1888 | 337 | c->want_close_all_fds(c); |
b119f362 | 338 | |
79622932 | 339 | err = c->start(c, 0, args) ? 0 : -1; |
91480a0f | 340 | |
3114c982 NC |
341 | if (my_args.pidfile) |
342 | unlink(my_args.pidfile); | |
79622932 | 343 | |
2d4bcb96 | 344 | out: |
79622932 | 345 | lxc_container_put(c); |
b0a33c1e | 346 | return err; |
5e97c3fc | 347 | } |
348 |