]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_start.c
api_create: undo unneeded chunk in previous commit
[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 <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <lxc/lxccontainer.h>
47 #include "conf.h"
48 #include "cgroup.h"
49 #include "utils.h"
50 #include "config.h"
51 #include "confile.h"
52 #include "arguments.h"
53
54 lxc_log_define(lxc_start_ui, lxc_start);
55
56 static struct lxc_list defines;
57
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);
66 if (fd < 0 && errno != EEXIST) {
67 SYSERROR("failed to create '%s'", path);
68 goto err;
69 }
70 if (fd >= 0)
71 close(fd);
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
94 static int my_parser(struct lxc_arguments* args, int c, char* arg)
95 {
96 switch (c) {
97 case 'c': args->console = arg; break;
98 case 'L': args->console_log = arg; break;
99 case 'd': args->daemonize = 1; args->close_all_fds = 1; break;
100 case 'f': args->rcfile = arg; break;
101 case 'C': args->close_all_fds = 1; break;
102 case 's': return lxc_config_define_add(&defines, arg);
103 case 'p': args->pidfile = arg; break;
104 }
105 return 0;
106 }
107
108 static const struct option my_longopts[] = {
109 {"daemon", no_argument, 0, 'd'},
110 {"rcfile", required_argument, 0, 'f'},
111 {"define", required_argument, 0, 's'},
112 {"console", required_argument, 0, 'c'},
113 {"console-log", required_argument, 0, 'L'},
114 {"close-all-fds", no_argument, 0, 'C'},
115 {"pidfile", required_argument, 0, 'p'},
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\
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",
137 .options = my_longopts,
138 .parser = my_parser,
139 .checker = NULL,
140 .daemonize = 0,
141 .pidfile = NULL,
142 };
143
144 int main(int argc, char *argv[])
145 {
146 int err = -1;
147 struct lxc_conf *conf;
148 char *const *args;
149 char *rcfile = NULL;
150 char *const default_args[] = {
151 "/sbin/init",
152 '\0',
153 };
154 FILE *pid_fp = NULL;
155 struct lxc_container *c;
156
157 lxc_list_init(&defines);
158
159 if (lxc_caps_init())
160 return err;
161
162 if (lxc_arguments_parse(&my_args, argc, argv))
163 return err;
164
165 if (!my_args.argc)
166 args = default_args;
167 else
168 args = my_args.argv;
169
170 if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
171 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
172 return err;
173
174 const char *lxcpath = my_args.lxcpath[0];
175
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 */
182 /* rcfile is specified in the cli option */
183 if (my_args.rcfile) {
184 rcfile = (char *)my_args.rcfile;
185 c = lxc_container_new(my_args.name, lxcpath);
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 {
196 int rc;
197
198 rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
199 if (rc == -1) {
200 SYSERROR("failed to allocate memory");
201 return err;
202 }
203 INFO("using rcfile %s", rcfile);
204
205 /* container configuration does not exist */
206 if (access(rcfile, F_OK)) {
207 free(rcfile);
208 rcfile = NULL;
209 }
210 c = lxc_container_new(my_args.name, lxcpath);
211 if (!c) {
212 ERROR("Failed to create lxc_container");
213 return err;
214 }
215 }
216
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;
224
225 if (lxc_config_define_load(&defines, conf))
226 goto out;
227
228 if (!rcfile && !strcmp("/sbin/init", args[0])) {
229 ERROR("Executing '/sbin/init' with no configuration file may crash the host");
230 goto out;
231 }
232
233 if (ensure_path(&conf->console.path, my_args.console) < 0) {
234 ERROR("failed to ensure console path '%s'", my_args.console);
235 goto out;
236 }
237
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);
240 goto out;
241 }
242
243 if (my_args.pidfile != NULL) {
244 pid_fp = fopen(my_args.pidfile, "w");
245 if (pid_fp == NULL) {
246 SYSERROR("failed to create pidfile '%s' for '%s'",
247 my_args.pidfile, my_args.name);
248 goto out;
249 }
250 }
251
252 if (my_args.daemonize) {
253 c->want_daemonize(c);
254 }
255
256 if (pid_fp != NULL) {
257 if (fprintf(pid_fp, "%d\n", getpid()) < 0) {
258 SYSERROR("failed to write '%s'", my_args.pidfile);
259 goto out;
260 }
261 fclose(pid_fp);
262 }
263
264 if (my_args.close_all_fds)
265 c->want_close_all_fds(c);
266
267 err = c->start(c, 0, args) ? 0 : -1;
268
269 if (my_args.pidfile)
270 unlink(my_args.pidfile);
271
272 out:
273 lxc_container_put(c);
274 return err;
275 }
276