]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_start.c
Revert "use a default per-container logfile"
[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:
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 */
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
DL
45#include "lxc.h"
46#include "conf.h"
63376d7d 47#include "cgroup.h"
fae349da 48#include "utils.h"
96c210bb 49#include "config.h"
fae349da
DL
50#include "confile.h"
51#include "arguments.h"
36eb9bde 52
77075659 53lxc_log_define(lxc_start_ui, lxc_start);
5e97c3fc 54
33ba4ad7
CLG
55static struct lxc_list defines;
56
596a818d
DE
57static int ensure_path(char **confpath, const char *path)
58{
59 int err = -1, fd;
60 char *fullpath = NULL;
61
62 if (path) {
63 if (access(path, W_OK)) {
64 fd = creat(path, 0600);
65 if (fd < 0) {
66 SYSERROR("failed to create '%s'", path);
67 goto err;
68 }
69 close(fd);
70 }
71
72 fullpath = realpath(path, NULL);
73 if (!fullpath) {
74 SYSERROR("failed to get the real path of '%s'", path);
75 goto err;
76 }
77
78 *confpath = strdup(fullpath);
79 if (!*confpath) {
80 ERROR("failed to dup string '%s'", fullpath);
81 goto err;
82 }
83 }
84 err = 0;
85
86err:
87 if (fullpath)
88 free(fullpath);
89 return err;
90}
91
c36583c3
DL
92static int my_parser(struct lxc_arguments* args, int c, char* arg)
93{
94 switch (c) {
829dd918 95 case 'c': args->console = arg; break;
596a818d 96 case 'L': args->console_log = arg; break;
b119f362 97 case 'd': args->daemonize = 1; args->close_all_fds = 1; break;
48862401 98 case 'f': args->rcfile = arg; break;
b119f362 99 case 'C': args->close_all_fds = 1; break;
33ba4ad7 100 case 's': return lxc_config_define_add(&defines, arg);
3114c982 101 case 'p': args->pidfile = arg; break;
c36583c3
DL
102 }
103 return 0;
104}
105
9618063c 106static const struct option my_longopts[] = {
c36583c3 107 {"daemon", no_argument, 0, 'd'},
48862401 108 {"rcfile", required_argument, 0, 'f'},
33ba4ad7 109 {"define", required_argument, 0, 's'},
829dd918 110 {"console", required_argument, 0, 'c'},
596a818d 111 {"console-log", required_argument, 0, 'L'},
b119f362 112 {"close-all-fds", no_argument, 0, 'C'},
3114c982 113 {"pidfile", required_argument, 0, 'p'},
9618063c
MN
114 LXC_COMMON_OPTIONS
115};
116
117static struct lxc_arguments my_args = {
118 .progname = "lxc-start",
119 .help = "\
120--name=NAME -- COMMAND\n\
121\n\
122lxc-start start COMMAND in specified container NAME\n\
123\n\
124Options :\n\
596a818d
DE
125 -n, --name=NAME NAME for name of the container\n\
126 -d, --daemon daemonize the container\n\
127 -p, --pidfile=FILE Create a file with the process id\n\
128 -f, --rcfile=FILE Load configuration file FILE\n\
129 -c, --console=FILE Use specified FILE for the container console\n\
130 -L, --console-log=FILE Log container console output to FILE\n\
131 -C, --close-all-fds If any fds are inherited, close them\n\
132 If not specified, exit with failure instead\n\
133 Note: --daemon implies --close-all-fds\n\
134 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
c36583c3
DL
135 .options = my_longopts,
136 .parser = my_parser,
137 .checker = NULL,
138 .daemonize = 0,
3114c982 139 .pidfile = NULL,
9618063c 140};
5e97c3fc 141
142int main(int argc, char *argv[])
143{
e043236e 144 int err = -1;
91480a0f
DL
145 struct lxc_conf *conf;
146 char *const *args;
147 char *rcfile = NULL;
9618063c 148 char *const default_args[] = {
b2b6c597 149 "/sbin/init",
150 '\0',
151 };
3114c982 152 FILE *pid_fp = NULL;
5e97c3fc 153
33ba4ad7
CLG
154 lxc_list_init(&defines);
155
0ed9cc8b
DL
156 if (lxc_caps_init())
157 return err;
158
e043236e
MN
159 if (lxc_arguments_parse(&my_args, argc, argv))
160 return err;
5e97c3fc 161
9618063c 162 if (!my_args.argc)
f79d43bb 163 args = default_args;
9618063c
MN
164 else
165 args = my_args.argv;
5e97c3fc 166
e6cde741 167 if (lxc_log_init(my_args.log_file, my_args.log_priority,
9618063c 168 my_args.progname, my_args.quiet))
e043236e 169 return err;
51cab631 170
96c210bb
DL
171 /* rcfile is specified in the cli option */
172 if (my_args.rcfile)
173 rcfile = (char *)my_args.rcfile;
174 else {
fa9ab205
NL
175 int rc;
176
177 rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name);
178 if (rc == -1) {
96c210bb
DL
179 SYSERROR("failed to allocate memory");
180 return err;
181 }
182
183 /* container configuration does not exist */
184 if (access(rcfile, F_OK)) {
185 free(rcfile);
186 rcfile = NULL;
187 }
188 }
189
7b379ab3
MN
190 conf = lxc_conf_init();
191 if (!conf) {
192 ERROR("failed to initialize configuration");
fae349da
DL
193 return err;
194 }
195
7b379ab3 196 if (rcfile && lxc_config_read(rcfile, conf)) {
fae349da
DL
197 ERROR("failed to read configuration file");
198 return err;
199 }
200
33ba4ad7
CLG
201 if (lxc_config_define_load(&defines, conf))
202 return err;
203
f2ae79a0
AN
204 if (!rcfile && !strcmp("/sbin/init", args[0])) {
205 ERROR("no configuration file for '/sbin/init' (may crash the host)");
206 return err;
207 }
208
596a818d
DE
209 if (ensure_path(&conf->console.path, my_args.console) < 0) {
210 ERROR("failed to ensure console path '%s'", my_args.console);
211 return err;
212 }
829dd918 213
596a818d
DE
214 if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) {
215 ERROR("failed to ensure console log '%s'", my_args.console_log);
216 return err;
829dd918
DL
217 }
218
3114c982
NC
219 if (my_args.pidfile != NULL) {
220 pid_fp = fopen(my_args.pidfile, "w");
221 if (pid_fp == NULL) {
c3752c0b
SG
222 SYSERROR("failed to create pidfile '%s' for '%s'",
223 my_args.pidfile, my_args.name);
3114c982
NC
224 return err;
225 }
226 }
227
b4df0a1e
SH
228 if (my_args.daemonize) {
229 /* do an early check for needed privs, since otherwise the
230 * user won't see the error */
231
232 if (!lxc_caps_check()) {
233 ERROR("Not running with sufficient privilege");
234 return err;
235 }
236
237 if (daemon(0, 0)) {
238 SYSERROR("failed to daemonize '%s'", my_args.name);
239 return err;
240 }
c36583c3
DL
241 }
242
3114c982
NC
243 if (pid_fp != NULL) {
244 if (fprintf(pid_fp, "%d\n", getpid()) < 0) {
245 SYSERROR("failed to write '%s'", my_args.pidfile);
246 return err;
247 }
248 fclose(pid_fp);
249 }
250
b119f362
SH
251 if (my_args.close_all_fds)
252 conf->close_all_fds = 1;
253
7b379ab3 254 err = lxc_start(my_args.name, args, conf);
5e97c3fc 255
91480a0f
DL
256 /*
257 * exec ourself, that requires to have all opened fd
258 * with the close-on-exec flag set
259 */
260 if (conf->reboot) {
261 INFO("rebooting container");
262 execvp(argv[0], argv);
263 SYSERROR("failed to exec");
264 err = -1;
265 }
266
3114c982
NC
267 if (my_args.pidfile)
268 unlink(my_args.pidfile);
269
b0a33c1e 270 return err;
5e97c3fc 271}
272