]>
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 | |
77075659 | 54 | lxc_log_define(lxc_start_ui, lxc_start); |
5e97c3fc | 55 | |
33ba4ad7 CLG |
56 | static struct lxc_list defines; |
57 | ||
596a818d DE |
58 | static int ensure_path(char **confpath, const char *path) |
59 | { | |
60 | int err = -1, fd; | |
61 | char *fullpath = NULL; | |
62 | ||
63 | if (path) { | |
64 | if (access(path, W_OK)) { | |
65 | fd = creat(path, 0600); | |
84bdfb2b | 66 | if (fd < 0 && errno != EEXIST) { |
596a818d DE |
67 | SYSERROR("failed to create '%s'", path); |
68 | goto err; | |
69 | } | |
84bdfb2b SH |
70 | if (fd >= 0) |
71 | close(fd); | |
596a818d DE |
72 | } |
73 | ||
74 | fullpath = realpath(path, NULL); | |
75 | if (!fullpath) { | |
76 | SYSERROR("failed to get the real path of '%s'", path); | |
77 | goto err; | |
78 | } | |
79 | ||
80 | *confpath = strdup(fullpath); | |
81 | if (!*confpath) { | |
82 | ERROR("failed to dup string '%s'", fullpath); | |
83 | goto err; | |
84 | } | |
85 | } | |
86 | err = 0; | |
87 | ||
88 | err: | |
89 | if (fullpath) | |
90 | free(fullpath); | |
91 | return err; | |
92 | } | |
93 | ||
c36583c3 DL |
94 | static int my_parser(struct lxc_arguments* args, int c, char* arg) |
95 | { | |
96 | switch (c) { | |
829dd918 | 97 | case 'c': args->console = arg; break; |
596a818d | 98 | case 'L': args->console_log = arg; break; |
b119f362 | 99 | case 'd': args->daemonize = 1; args->close_all_fds = 1; break; |
48862401 | 100 | case 'f': args->rcfile = arg; break; |
b119f362 | 101 | case 'C': args->close_all_fds = 1; break; |
33ba4ad7 | 102 | case 's': return lxc_config_define_add(&defines, arg); |
3114c982 | 103 | case 'p': args->pidfile = arg; break; |
c36583c3 DL |
104 | } |
105 | return 0; | |
106 | } | |
107 | ||
9618063c | 108 | static const struct option my_longopts[] = { |
c36583c3 | 109 | {"daemon", no_argument, 0, 'd'}, |
48862401 | 110 | {"rcfile", required_argument, 0, 'f'}, |
33ba4ad7 | 111 | {"define", required_argument, 0, 's'}, |
829dd918 | 112 | {"console", required_argument, 0, 'c'}, |
596a818d | 113 | {"console-log", required_argument, 0, 'L'}, |
b119f362 | 114 | {"close-all-fds", no_argument, 0, 'C'}, |
3114c982 | 115 | {"pidfile", required_argument, 0, 'p'}, |
9618063c MN |
116 | LXC_COMMON_OPTIONS |
117 | }; | |
118 | ||
119 | static struct lxc_arguments my_args = { | |
120 | .progname = "lxc-start", | |
121 | .help = "\ | |
122 | --name=NAME -- COMMAND\n\ | |
123 | \n\ | |
124 | lxc-start start COMMAND in specified container NAME\n\ | |
125 | \n\ | |
126 | Options :\n\ | |
596a818d DE |
127 | -n, --name=NAME NAME for name of the container\n\ |
128 | -d, --daemon daemonize the container\n\ | |
129 | -p, --pidfile=FILE Create a file with the process id\n\ | |
130 | -f, --rcfile=FILE Load configuration file FILE\n\ | |
131 | -c, --console=FILE Use specified FILE for the container console\n\ | |
132 | -L, --console-log=FILE Log container console output to FILE\n\ | |
133 | -C, --close-all-fds If any fds are inherited, close them\n\ | |
134 | If not specified, exit with failure instead\n\ | |
135 | Note: --daemon implies --close-all-fds\n\ | |
136 | -s, --define KEY=VAL Assign VAL to configuration variable KEY\n", | |
c36583c3 DL |
137 | .options = my_longopts, |
138 | .parser = my_parser, | |
139 | .checker = NULL, | |
140 | .daemonize = 0, | |
3114c982 | 141 | .pidfile = NULL, |
9618063c | 142 | }; |
5e97c3fc | 143 | |
144 | int main(int argc, char *argv[]) | |
145 | { | |
e043236e | 146 | int err = -1; |
91480a0f DL |
147 | struct lxc_conf *conf; |
148 | char *const *args; | |
149 | char *rcfile = NULL; | |
9618063c | 150 | char *const default_args[] = { |
b2b6c597 | 151 | "/sbin/init", |
152 | '\0', | |
153 | }; | |
3114c982 | 154 | FILE *pid_fp = NULL; |
79622932 | 155 | struct lxc_container *c; |
5e97c3fc | 156 | |
33ba4ad7 CLG |
157 | lxc_list_init(&defines); |
158 | ||
0ed9cc8b DL |
159 | if (lxc_caps_init()) |
160 | return err; | |
161 | ||
e043236e MN |
162 | if (lxc_arguments_parse(&my_args, argc, argv)) |
163 | return err; | |
5e97c3fc | 164 | |
9618063c | 165 | if (!my_args.argc) |
f79d43bb | 166 | args = default_args; |
9618063c MN |
167 | else |
168 | args = my_args.argv; | |
5e97c3fc | 169 | |
5e1e7aaf | 170 | if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority, |
8d06bd13 | 171 | my_args.progname, my_args.quiet, my_args.lxcpath[0])) |
e043236e | 172 | return err; |
51cab631 | 173 | |
69733b5d SH |
174 | const char *lxcpath = my_args.lxcpath[0]; |
175 | ||
79622932 SH |
176 | /* |
177 | * rcfile possibilities: | |
178 | * 1. rcfile from random path specified in cli option | |
179 | * 2. rcfile not specified, use $lxcpath/$lxcname/config | |
180 | * 3. rcfile not specified and does not exist. | |
181 | */ | |
96c210bb | 182 | /* rcfile is specified in the cli option */ |
79622932 | 183 | if (my_args.rcfile) { |
96c210bb | 184 | rcfile = (char *)my_args.rcfile; |
69733b5d | 185 | c = lxc_container_new(my_args.name, lxcpath); |
79622932 SH |
186 | if (!c) { |
187 | ERROR("Failed to create lxc_container"); | |
188 | return err; | |
189 | } | |
190 | if (!c->load_config(c, rcfile)) { | |
191 | ERROR("Failed to load rcfile"); | |
192 | lxc_container_put(c); | |
193 | return err; | |
194 | } | |
195 | } else { | |
fa9ab205 NL |
196 | int rc; |
197 | ||
79622932 | 198 | rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name); |
fa9ab205 | 199 | if (rc == -1) { |
96c210bb DL |
200 | SYSERROR("failed to allocate memory"); |
201 | return err; | |
202 | } | |
444f3ca2 | 203 | INFO("using rcfile %s", rcfile); |
96c210bb DL |
204 | |
205 | /* container configuration does not exist */ | |
206 | if (access(rcfile, F_OK)) { | |
207 | free(rcfile); | |
208 | rcfile = NULL; | |
79622932 SH |
209 | } |
210 | c = lxc_container_new(my_args.name, lxcpath); | |
211 | if (!c) { | |
212 | ERROR("Failed to create lxc_container"); | |
213 | return err; | |
96c210bb DL |
214 | } |
215 | } | |
216 | ||
79622932 SH |
217 | /* |
218 | * We should use set_config_item() over &defines, which would handle | |
219 | * unset c->lxc_conf for us and let us not use lxc_config_define_load() | |
220 | */ | |
221 | if (!c->lxc_conf) | |
222 | c->lxc_conf = lxc_conf_init(); | |
223 | conf = c->lxc_conf; | |
fae349da | 224 | |
33ba4ad7 | 225 | if (lxc_config_define_load(&defines, conf)) |
2d4bcb96 | 226 | goto out; |
33ba4ad7 | 227 | |
f2ae79a0 | 228 | if (!rcfile && !strcmp("/sbin/init", args[0])) { |
79622932 | 229 | ERROR("Executing '/sbin/init' with no configuration file may crash the host"); |
2d4bcb96 | 230 | goto out; |
f2ae79a0 AN |
231 | } |
232 | ||
596a818d DE |
233 | if (ensure_path(&conf->console.path, my_args.console) < 0) { |
234 | ERROR("failed to ensure console path '%s'", my_args.console); | |
2d4bcb96 | 235 | goto out; |
596a818d | 236 | } |
829dd918 | 237 | |
596a818d DE |
238 | if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) { |
239 | ERROR("failed to ensure console log '%s'", my_args.console_log); | |
2d4bcb96 | 240 | goto out; |
829dd918 DL |
241 | } |
242 | ||
3114c982 NC |
243 | if (my_args.pidfile != NULL) { |
244 | pid_fp = fopen(my_args.pidfile, "w"); | |
245 | if (pid_fp == NULL) { | |
c3752c0b SG |
246 | SYSERROR("failed to create pidfile '%s' for '%s'", |
247 | my_args.pidfile, my_args.name); | |
2d4bcb96 | 248 | goto out; |
3114c982 NC |
249 | } |
250 | } | |
251 | ||
b4df0a1e | 252 | if (my_args.daemonize) { |
79622932 | 253 | c->want_daemonize(c); |
c36583c3 DL |
254 | } |
255 | ||
3114c982 NC |
256 | if (pid_fp != NULL) { |
257 | if (fprintf(pid_fp, "%d\n", getpid()) < 0) { | |
258 | SYSERROR("failed to write '%s'", my_args.pidfile); | |
2d4bcb96 | 259 | goto out; |
3114c982 NC |
260 | } |
261 | fclose(pid_fp); | |
262 | } | |
263 | ||
b119f362 | 264 | if (my_args.close_all_fds) |
130a1888 | 265 | c->want_close_all_fds(c); |
b119f362 | 266 | |
79622932 | 267 | err = c->start(c, 0, args) ? 0 : -1; |
91480a0f | 268 | |
3114c982 NC |
269 | if (my_args.pidfile) |
270 | unlink(my_args.pidfile); | |
79622932 | 271 | |
2d4bcb96 | 272 | out: |
79622932 | 273 | lxc_container_put(c); |
b0a33c1e | 274 | return err; |
5e97c3fc | 275 | } |
276 |