]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_start.c
Makefile.am: use right .h file name for seccomp
[mirror_lxc.git] / src / lxc / lxc_start.c
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 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <libgen.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <sys/param.h>
35 #include <sys/utsname.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <arpa/inet.h>
40 #include <netinet/in.h>
41 #include <net/if.h>
42
43 #include "log.h"
44 #include "caps.h"
45 #include "lxc.h"
46 #include "conf.h"
47 #include "cgroup.h"
48 #include "utils.h"
49 #include "config.h"
50 #include "confile.h"
51 #include "arguments.h"
52
53 lxc_log_define(lxc_start_ui, lxc_start);
54
55 static struct lxc_list defines;
56
57 static int my_parser(struct lxc_arguments* args, int c, char* arg)
58 {
59 switch (c) {
60 case 'c': args->console = arg; break;
61 case 'd': args->daemonize = 1; args->close_all_fds = 1; break;
62 case 'f': args->rcfile = arg; break;
63 case 'C': args->close_all_fds = 1; break;
64 case 's': return lxc_config_define_add(&defines, arg);
65 }
66 return 0;
67 }
68
69 static const struct option my_longopts[] = {
70 {"daemon", no_argument, 0, 'd'},
71 {"rcfile", required_argument, 0, 'f'},
72 {"define", required_argument, 0, 's'},
73 {"console", required_argument, 0, 'c'},
74 {"close-all-fds", no_argument, 0, 'C'},
75 LXC_COMMON_OPTIONS
76 };
77
78 static struct lxc_arguments my_args = {
79 .progname = "lxc-start",
80 .help = "\
81 --name=NAME -- COMMAND\n\
82 \n\
83 lxc-start start COMMAND in specified container NAME\n\
84 \n\
85 Options :\n\
86 -n, --name=NAME NAME for name of the container\n\
87 -d, --daemon daemonize the container\n\
88 -f, --rcfile=FILE Load configuration file FILE\n\
89 -c, --console=FILE Set the file output for the container console\n\
90 -C, --close-all-fds If any fds are inherited, close them\n\
91 If not specified, exit with failure instead\n\
92 Note: --daemon implies --close-all-fds\n\
93 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
94 .options = my_longopts,
95 .parser = my_parser,
96 .checker = NULL,
97 .daemonize = 0,
98 };
99
100 int main(int argc, char *argv[])
101 {
102 int err = -1;
103 struct lxc_conf *conf;
104 char *const *args;
105 char *rcfile = NULL;
106 char *const default_args[] = {
107 "/sbin/init",
108 '\0',
109 };
110
111 lxc_list_init(&defines);
112
113 if (lxc_caps_init())
114 return err;
115
116 if (lxc_arguments_parse(&my_args, argc, argv))
117 return err;
118
119 if (!my_args.argc)
120 args = default_args;
121 else
122 args = my_args.argv;
123
124 if (lxc_log_init(my_args.log_file, my_args.log_priority,
125 my_args.progname, my_args.quiet))
126 return err;
127
128 if (putenv("container=lxc")) {
129 SYSERROR("failed to set environment variable");
130 return err;
131 }
132
133 /* rcfile is specified in the cli option */
134 if (my_args.rcfile)
135 rcfile = (char *)my_args.rcfile;
136 else {
137 int rc;
138
139 rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name);
140 if (rc == -1) {
141 SYSERROR("failed to allocate memory");
142 return err;
143 }
144
145 /* container configuration does not exist */
146 if (access(rcfile, F_OK)) {
147 free(rcfile);
148 rcfile = NULL;
149 }
150 }
151
152 conf = lxc_conf_init();
153 if (!conf) {
154 ERROR("failed to initialize configuration");
155 return err;
156 }
157
158 if (rcfile && lxc_config_read(rcfile, conf)) {
159 ERROR("failed to read configuration file");
160 return err;
161 }
162
163 if (lxc_config_define_load(&defines, conf))
164 return err;
165
166 if (!rcfile && !strcmp("/sbin/init", args[0])) {
167 ERROR("no configuration file for '/sbin/init' (may crash the host)");
168 return err;
169 }
170
171 if (my_args.console) {
172
173 char *console, fd;
174
175 if (access(my_args.console, W_OK)) {
176
177 fd = creat(my_args.console, 0600);
178 if (fd < 0) {
179 SYSERROR("failed to touch file '%s'",
180 my_args.console);
181 return err;
182 }
183 close(fd);
184 }
185
186 console = realpath(my_args.console, NULL);
187 if (!console) {
188 SYSERROR("failed to get the real path of '%s'",
189 my_args.console);
190 return err;
191 }
192
193 conf->console.path = strdup(console);
194 if (!conf->console.path) {
195 ERROR("failed to dup string '%s'", console);
196 return err;
197 }
198
199 free(console);
200 }
201
202 if (my_args.daemonize) {
203 /* do an early check for needed privs, since otherwise the
204 * user won't see the error */
205
206 if (!lxc_caps_check()) {
207 ERROR("Not running with sufficient privilege");
208 return err;
209 }
210
211 if (daemon(0, 0)) {
212 SYSERROR("failed to daemonize '%s'", my_args.name);
213 return err;
214 }
215 }
216
217 if (my_args.close_all_fds)
218 conf->close_all_fds = 1;
219
220 err = lxc_start(my_args.name, args, conf);
221
222 /*
223 * exec ourself, that requires to have all opened fd
224 * with the close-on-exec flag set
225 */
226 if (conf->reboot) {
227 INFO("rebooting container");
228 execvp(argv[0], argv);
229 SYSERROR("failed to exec");
230 err = -1;
231 }
232
233 return err;
234 }
235