]>
Commit | Line | Data |
---|---|---|
5e97c3fc | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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 | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
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 DL |
45 | #include "lxc.h" |
46 | #include "conf.h" | |
63376d7d | 47 | #include "cgroup.h" |
fae349da | 48 | #include "utils.h" |
96c210bb | 49 | #include "config.h" |
fae349da DL |
50 | #include "confile.h" |
51 | #include "arguments.h" | |
36eb9bde | 52 | |
77075659 | 53 | lxc_log_define(lxc_start_ui, lxc_start); |
5e97c3fc | 54 | |
33ba4ad7 CLG |
55 | static struct lxc_list defines; |
56 | ||
c36583c3 DL |
57 | static int my_parser(struct lxc_arguments* args, int c, char* arg) |
58 | { | |
59 | switch (c) { | |
829dd918 | 60 | case 'c': args->console = arg; break; |
b119f362 | 61 | case 'd': args->daemonize = 1; args->close_all_fds = 1; break; |
48862401 | 62 | case 'f': args->rcfile = arg; break; |
b119f362 | 63 | case 'C': args->close_all_fds = 1; break; |
33ba4ad7 | 64 | case 's': return lxc_config_define_add(&defines, arg); |
3114c982 | 65 | case 'p': args->pidfile = arg; break; |
c36583c3 DL |
66 | } |
67 | return 0; | |
68 | } | |
69 | ||
9618063c | 70 | static const struct option my_longopts[] = { |
c36583c3 | 71 | {"daemon", no_argument, 0, 'd'}, |
48862401 | 72 | {"rcfile", required_argument, 0, 'f'}, |
33ba4ad7 | 73 | {"define", required_argument, 0, 's'}, |
829dd918 | 74 | {"console", required_argument, 0, 'c'}, |
b119f362 | 75 | {"close-all-fds", no_argument, 0, 'C'}, |
3114c982 | 76 | {"pidfile", required_argument, 0, 'p'}, |
9618063c MN |
77 | LXC_COMMON_OPTIONS |
78 | }; | |
79 | ||
80 | static struct lxc_arguments my_args = { | |
81 | .progname = "lxc-start", | |
82 | .help = "\ | |
83 | --name=NAME -- COMMAND\n\ | |
84 | \n\ | |
85 | lxc-start start COMMAND in specified container NAME\n\ | |
86 | \n\ | |
87 | Options :\n\ | |
c36583c3 | 88 | -n, --name=NAME NAME for name of the container\n\ |
48862401 | 89 | -d, --daemon daemonize the container\n\ |
3114c982 | 90 | -p, --pidfile=FILE Create a file with the process id\n\ |
33ba4ad7 | 91 | -f, --rcfile=FILE Load configuration file FILE\n\ |
829dd918 | 92 | -c, --console=FILE Set the file output for the container console\n\ |
b119f362 SH |
93 | -C, --close-all-fds If any fds are inherited, close them\n\ |
94 | If not specified, exit with failure instead\n\ | |
95 | Note: --daemon implies --close-all-fds\n\ | |
33ba4ad7 | 96 | -s, --define KEY=VAL Assign VAL to configuration variable KEY\n", |
c36583c3 DL |
97 | .options = my_longopts, |
98 | .parser = my_parser, | |
99 | .checker = NULL, | |
100 | .daemonize = 0, | |
3114c982 | 101 | .pidfile = NULL, |
9618063c | 102 | }; |
5e97c3fc | 103 | |
104 | int main(int argc, char *argv[]) | |
105 | { | |
e043236e | 106 | int err = -1; |
91480a0f DL |
107 | struct lxc_conf *conf; |
108 | char *const *args; | |
109 | char *rcfile = NULL; | |
9618063c | 110 | char *const default_args[] = { |
b2b6c597 | 111 | "/sbin/init", |
112 | '\0', | |
113 | }; | |
3114c982 | 114 | FILE *pid_fp = NULL; |
5e97c3fc | 115 | |
33ba4ad7 CLG |
116 | lxc_list_init(&defines); |
117 | ||
0ed9cc8b DL |
118 | if (lxc_caps_init()) |
119 | return err; | |
120 | ||
e043236e MN |
121 | if (lxc_arguments_parse(&my_args, argc, argv)) |
122 | return err; | |
5e97c3fc | 123 | |
9618063c | 124 | if (!my_args.argc) |
b2b6c597 | 125 | args = default_args; |
9618063c MN |
126 | else |
127 | args = my_args.argv; | |
5e97c3fc | 128 | |
9618063c MN |
129 | if (lxc_log_init(my_args.log_file, my_args.log_priority, |
130 | my_args.progname, my_args.quiet)) | |
e043236e | 131 | return err; |
51cab631 | 132 | |
3244e750 DL |
133 | if (putenv("container=lxc")) { |
134 | SYSERROR("failed to set environment variable"); | |
135 | return err; | |
136 | } | |
137 | ||
96c210bb DL |
138 | /* rcfile is specified in the cli option */ |
139 | if (my_args.rcfile) | |
140 | rcfile = (char *)my_args.rcfile; | |
141 | else { | |
fa9ab205 NL |
142 | int rc; |
143 | ||
144 | rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name); | |
145 | if (rc == -1) { | |
96c210bb DL |
146 | SYSERROR("failed to allocate memory"); |
147 | return err; | |
148 | } | |
149 | ||
150 | /* container configuration does not exist */ | |
151 | if (access(rcfile, F_OK)) { | |
152 | free(rcfile); | |
153 | rcfile = NULL; | |
154 | } | |
155 | } | |
156 | ||
7b379ab3 MN |
157 | conf = lxc_conf_init(); |
158 | if (!conf) { | |
159 | ERROR("failed to initialize configuration"); | |
fae349da DL |
160 | return err; |
161 | } | |
162 | ||
7b379ab3 | 163 | if (rcfile && lxc_config_read(rcfile, conf)) { |
fae349da DL |
164 | ERROR("failed to read configuration file"); |
165 | return err; | |
166 | } | |
167 | ||
33ba4ad7 CLG |
168 | if (lxc_config_define_load(&defines, conf)) |
169 | return err; | |
170 | ||
f2ae79a0 AN |
171 | if (!rcfile && !strcmp("/sbin/init", args[0])) { |
172 | ERROR("no configuration file for '/sbin/init' (may crash the host)"); | |
173 | return err; | |
174 | } | |
175 | ||
829dd918 | 176 | if (my_args.console) { |
b8f57738 | 177 | |
829dd918 DL |
178 | char *console, fd; |
179 | ||
180 | if (access(my_args.console, W_OK)) { | |
181 | ||
182 | fd = creat(my_args.console, 0600); | |
183 | if (fd < 0) { | |
184 | SYSERROR("failed to touch file '%s'", | |
185 | my_args.console); | |
186 | return err; | |
187 | } | |
188 | close(fd); | |
b8f57738 DL |
189 | } |
190 | ||
829dd918 DL |
191 | console = realpath(my_args.console, NULL); |
192 | if (!console) { | |
193 | SYSERROR("failed to get the real path of '%s'", | |
194 | my_args.console); | |
195 | return err; | |
196 | } | |
b8f57738 | 197 | |
829dd918 DL |
198 | conf->console.path = strdup(console); |
199 | if (!conf->console.path) { | |
200 | ERROR("failed to dup string '%s'", console); | |
201 | return err; | |
b8f57738 | 202 | } |
829dd918 DL |
203 | |
204 | free(console); | |
205 | } | |
206 | ||
3114c982 NC |
207 | if (my_args.pidfile != NULL) { |
208 | pid_fp = fopen(my_args.pidfile, "w"); | |
209 | if (pid_fp == NULL) { | |
c3752c0b SG |
210 | SYSERROR("failed to create pidfile '%s' for '%s'", |
211 | my_args.pidfile, my_args.name); | |
3114c982 NC |
212 | return err; |
213 | } | |
214 | } | |
215 | ||
b4df0a1e SH |
216 | if (my_args.daemonize) { |
217 | /* do an early check for needed privs, since otherwise the | |
218 | * user won't see the error */ | |
219 | ||
220 | if (!lxc_caps_check()) { | |
221 | ERROR("Not running with sufficient privilege"); | |
222 | return err; | |
223 | } | |
224 | ||
225 | if (daemon(0, 0)) { | |
226 | SYSERROR("failed to daemonize '%s'", my_args.name); | |
227 | return err; | |
228 | } | |
c36583c3 DL |
229 | } |
230 | ||
3114c982 NC |
231 | if (pid_fp != NULL) { |
232 | if (fprintf(pid_fp, "%d\n", getpid()) < 0) { | |
233 | SYSERROR("failed to write '%s'", my_args.pidfile); | |
234 | return err; | |
235 | } | |
236 | fclose(pid_fp); | |
237 | } | |
238 | ||
b119f362 SH |
239 | if (my_args.close_all_fds) |
240 | conf->close_all_fds = 1; | |
241 | ||
7b379ab3 | 242 | err = lxc_start(my_args.name, args, conf); |
5e97c3fc | 243 | |
91480a0f DL |
244 | /* |
245 | * exec ourself, that requires to have all opened fd | |
246 | * with the close-on-exec flag set | |
247 | */ | |
248 | if (conf->reboot) { | |
249 | INFO("rebooting container"); | |
250 | execvp(argv[0], argv); | |
251 | SYSERROR("failed to exec"); | |
252 | err = -1; | |
253 | } | |
254 | ||
3114c982 NC |
255 | if (my_args.pidfile) |
256 | unlink(my_args.pidfile); | |
257 | ||
b0a33c1e | 258 | return err; |
5e97c3fc | 259 | } |
260 |