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