/
lxc_start.c
335 lines (297 loc) · 8.3 KB
/
lxc_start.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <net/if.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <lxc/lxccontainer.h>
#include "arguments.h"
#include "tool_list.h"
#include "tool_utils.h"
static struct lxc_list defines;
static int ensure_path(char **confpath, const char *path)
{
int err = -1, fd;
char *fullpath = NULL;
if (path) {
if (access(path, W_OK)) {
fd = creat(path, 0600);
if (fd < 0 && errno != EEXIST) {
fprintf(stderr, "failed to create '%s'\n", path);
goto err;
}
if (fd >= 0)
close(fd);
}
fullpath = realpath(path, NULL);
if (!fullpath) {
fprintf(stderr, "failed to get the real path of '%s'\n", path);
goto err;
}
*confpath = fullpath;
}
err = EXIT_SUCCESS;
err:
return err;
}
static int my_parser(struct lxc_arguments *args, int c, char *arg)
{
switch (c) {
case 'c':
args->console = arg;
break;
case 'L':
args->console_log = arg;
break;
case 'd':
args->daemonize = 1;
break;
case 'F':
args->daemonize = 0;
break;
case 'f':
args->rcfile = arg;
break;
case 'C':
args->close_all_fds = 1;
break;
case 's':
return lxc_config_define_add(&defines, arg);
case 'p':
args->pidfile = arg;
break;
case OPT_SHARE_NET:
args->share_ns[LXC_NS_NET] = arg;
break;
case OPT_SHARE_IPC:
args->share_ns[LXC_NS_IPC] = arg;
break;
case OPT_SHARE_UTS:
args->share_ns[LXC_NS_UTS] = arg;
break;
case OPT_SHARE_PID:
args->share_ns[LXC_NS_PID] = arg;
break;
}
return 0;
}
static const struct option my_longopts[] = {
{"daemon", no_argument, 0, 'd'},
{"foreground", no_argument, 0, 'F'},
{"rcfile", required_argument, 0, 'f'},
{"define", required_argument, 0, 's'},
{"console", required_argument, 0, 'c'},
{"console-log", required_argument, 0, 'L'},
{"close-all-fds", no_argument, 0, 'C'},
{"pidfile", required_argument, 0, 'p'},
{"share-net", required_argument, 0, OPT_SHARE_NET},
{"share-ipc", required_argument, 0, OPT_SHARE_IPC},
{"share-uts", required_argument, 0, OPT_SHARE_UTS},
{"share-pid", required_argument, 0, OPT_SHARE_PID},
LXC_COMMON_OPTIONS
};
static struct lxc_arguments my_args = {
.progname = "lxc-start",
.help = "\
--name=NAME -- COMMAND\n\
\n\
lxc-start start COMMAND in specified container NAME\n\
\n\
Options :\n\
-n, --name=NAME NAME of the container\n\
-d, --daemon Daemonize the container (default)\n\
-F, --foreground Start with the current tty attached to /dev/console\n\
-p, --pidfile=FILE Create a file with the process id\n\
-f, --rcfile=FILE Load configuration file FILE\n\
-c, --console=FILE Use specified FILE for the container console\n\
-L, --console-log=FILE Log container console output to FILE\n\
-C, --close-all-fds If any fds are inherited, close them\n\
If not specified, exit with failure instead\n\
Note: --daemon implies --close-all-fds\n\
-s, --define KEY=VAL Assign VAL to configuration variable KEY\n\
--share-[net|ipc|uts|pid]=NAME Share a namespace with another container or pid\n\
",
.options = my_longopts,
.parser = my_parser,
.checker = NULL,
.daemonize = 1,
.pidfile = NULL,
};
int main(int argc, char *argv[])
{
const char *lxcpath;
char *const *args;
struct lxc_container *c;
struct lxc_log log;
int err = EXIT_FAILURE;
char *rcfile = NULL;
char *const default_args[] = {
"/sbin/init",
NULL,
};
lxc_list_init(&defines);
if (lxc_caps_init())
exit(err);
if (lxc_arguments_parse(&my_args, argc, argv))
exit(err);
if (!my_args.argc)
args = default_args;
else
args = my_args.argv;
log.name = my_args.name;
log.file = my_args.log_file;
log.level = my_args.log_priority;
log.prefix = my_args.progname;
log.quiet = my_args.quiet;
log.lxcpath = my_args.lxcpath[0];
if (lxc_log_init(&log))
exit(err);
lxcpath = my_args.lxcpath[0];
if (access(lxcpath, O_RDONLY) < 0) {
if (!my_args.quiet)
fprintf(stderr, "You lack access to %s\n", lxcpath);
exit(err);
}
/* REMOVE IN LXC 3.0 */
setenv("LXC_UPDATE_CONFIG_FORMAT", "1", 0);
/*
* rcfile possibilities:
* 1. rcfile from random path specified in cli option
* 2. rcfile not specified, use $lxcpath/$lxcname/config
* 3. rcfile not specified and does not exist.
*/
/* rcfile is specified in the cli option */
if (my_args.rcfile) {
rcfile = (char *)my_args.rcfile;
c = lxc_container_new(my_args.name, lxcpath);
if (!c) {
fprintf(stderr, "Failed to create lxc_container\n");
exit(err);
}
c->clear_config(c);
if (!c->load_config(c, rcfile)) {
fprintf(stderr, "Failed to load rcfile\n");
lxc_container_put(c);
exit(err);
}
c->configfile = strdup(my_args.rcfile);
if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n");
goto out;
}
} else {
int rc;
rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
if (rc == -1) {
fprintf(stderr, "failed to allocate memory\n");
exit(err);
}
/* container configuration does not exist */
if (access(rcfile, F_OK)) {
free(rcfile);
rcfile = NULL;
}
c = lxc_container_new(my_args.name, lxcpath);
if (!c) {
fprintf(stderr, "Failed to create lxc_container\n");
exit(err);
}
}
/* We do not check here whether the container is defined, because we
* support volatile containers. Which means the container does not need
* to be created for it to be started. You can just pass a configuration
* file as argument and start the container right away.
*/
if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s\n", c->name);
goto out;
}
if (c->is_running(c)) {
fprintf(stderr, "Container is already running.\n");
err = EXIT_SUCCESS;
goto out;
}
/*
* We should use set_config_item() over &defines, which would handle
* unset c->lxc_conf for us and let us not use lxc_config_define_load()
*/
if (!c->lxc_conf) {
fprintf(stderr, "No container config specified\n");
goto out;
}
if (lxc_config_define_load(&defines, c))
goto out;
if (!rcfile && !strcmp("/sbin/init", args[0])) {
fprintf(stderr, "Executing '/sbin/init' with no configuration file may crash the host\n");
goto out;
}
if (my_args.pidfile != NULL) {
if (ensure_path(&c->pidfile, my_args.pidfile) < 0) {
fprintf(stderr, "failed to ensure pidfile '%s'\n", my_args.pidfile);
goto out;
}
}
if (my_args.console)
if (!c->set_config_item(c, "lxc.console.path", my_args.console))
goto out;
if (my_args.console_log)
if (!c->set_config_item(c, "lxc.console.logfile", my_args.console_log))
goto out;
if (!lxc_setup_shared_ns(&my_args, c))
goto out;
if (!my_args.daemonize) {
c->want_daemonize(c, false);
}
if (my_args.close_all_fds)
c->want_close_all_fds(c, true);
if (args == default_args)
err = c->start(c, 0, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
else
err = c->start(c, 0, args) ? EXIT_SUCCESS : EXIT_FAILURE;
if (err) {
fprintf(stderr, "The container failed to start.\n");
if (my_args.daemonize)
fprintf(stderr, "To get more details, run the container in foreground mode.\n");
fprintf(stderr, "Additional information can be obtained by setting the "
"--logfile and --logpriority options.\n");
err = c->error_num;
lxc_container_put(c);
exit(err);
}
out:
lxc_container_put(c);
exit(err);
}