]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_start.c
add a macro to wrap a privilegied function
[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
DL
43#include "log.h"
44#include "lxc.h"
45#include "conf.h"
63376d7d 46#include "cgroup.h"
fae349da 47#include "utils.h"
96c210bb 48#include "config.h"
fae349da
DL
49#include "confile.h"
50#include "arguments.h"
36eb9bde 51
77075659 52lxc_log_define(lxc_start_ui, lxc_start);
5e97c3fc 53
33ba4ad7
CLG
54static struct lxc_list defines;
55
c36583c3
DL
56static int my_parser(struct lxc_arguments* args, int c, char* arg)
57{
58 switch (c) {
59 case 'd': args->daemonize = 1; break;
48862401 60 case 'f': args->rcfile = arg; break;
33ba4ad7 61 case 's': return lxc_config_define_add(&defines, arg);
c36583c3
DL
62 }
63 return 0;
64}
65
9618063c 66static const struct option my_longopts[] = {
c36583c3 67 {"daemon", no_argument, 0, 'd'},
48862401 68 {"rcfile", required_argument, 0, 'f'},
33ba4ad7 69 {"define", required_argument, 0, 's'},
9618063c
MN
70 LXC_COMMON_OPTIONS
71};
72
73static struct lxc_arguments my_args = {
74 .progname = "lxc-start",
75 .help = "\
76--name=NAME -- COMMAND\n\
77\n\
78lxc-start start COMMAND in specified container NAME\n\
79\n\
80Options :\n\
c36583c3 81 -n, --name=NAME NAME for name of the container\n\
48862401 82 -d, --daemon daemonize the container\n\
33ba4ad7
CLG
83 -f, --rcfile=FILE Load configuration file FILE\n\
84 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
c36583c3
DL
85 .options = my_longopts,
86 .parser = my_parser,
87 .checker = NULL,
88 .daemonize = 0,
9618063c 89};
5e97c3fc 90
91int main(int argc, char *argv[])
92{
e043236e 93 int err = -1;
91480a0f
DL
94 struct lxc_conf *conf;
95 char *const *args;
96 char *rcfile = NULL;
9618063c 97 char *const default_args[] = {
b2b6c597 98 "/sbin/init",
99 '\0',
100 };
5e97c3fc 101
33ba4ad7
CLG
102 lxc_list_init(&defines);
103
e043236e
MN
104 if (lxc_arguments_parse(&my_args, argc, argv))
105 return err;
5e97c3fc 106
9618063c 107 if (!my_args.argc)
b2b6c597 108 args = default_args;
9618063c
MN
109 else
110 args = my_args.argv;
5e97c3fc 111
9618063c
MN
112 if (lxc_log_init(my_args.log_file, my_args.log_priority,
113 my_args.progname, my_args.quiet))
e043236e 114 return err;
51cab631 115
96c210bb
DL
116 /* rcfile is specified in the cli option */
117 if (my_args.rcfile)
118 rcfile = (char *)my_args.rcfile;
119 else {
fa9ab205
NL
120 int rc;
121
122 rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name);
123 if (rc == -1) {
96c210bb
DL
124 SYSERROR("failed to allocate memory");
125 return err;
126 }
127
128 /* container configuration does not exist */
129 if (access(rcfile, F_OK)) {
130 free(rcfile);
131 rcfile = NULL;
132 }
133 }
134
7b379ab3
MN
135 conf = lxc_conf_init();
136 if (!conf) {
137 ERROR("failed to initialize configuration");
fae349da
DL
138 return err;
139 }
140
7b379ab3 141 if (rcfile && lxc_config_read(rcfile, conf)) {
fae349da
DL
142 ERROR("failed to read configuration file");
143 return err;
144 }
145
33ba4ad7
CLG
146 if (lxc_config_define_load(&defines, conf))
147 return err;
148
f2ae79a0
AN
149 if (!rcfile && !strcmp("/sbin/init", args[0])) {
150 ERROR("no configuration file for '/sbin/init' (may crash the host)");
151 return err;
152 }
153
b8f57738
DL
154 if (my_args.daemonize) {
155
156 /* do not chdir as we want to open the log file,
157 * change the directory right after.
158 * do not close 0, 1, 2, we want to do that
159 * ourself because we don't want /dev/null
160 * being reopened.
161 */
af795875 162 if (daemon(1, 1)) {
b8f57738
DL
163 SYSERROR("failed to daemonize '%s'", my_args.name);
164 return err;
165 }
166
80090207
CLG
167 close(0);
168 close(1);
169 close(2);
b8f57738
DL
170
171 if (my_args.log_file) {
172 open(my_args.log_file, O_WRONLY | O_CLOEXEC);
173 open(my_args.log_file, O_RDONLY | O_CLOEXEC);
174 open(my_args.log_file, O_RDONLY | O_CLOEXEC);
175 }
c36583c3
DL
176 }
177
7b379ab3 178 err = lxc_start(my_args.name, args, conf);
5e97c3fc 179
91480a0f
DL
180 /*
181 * exec ourself, that requires to have all opened fd
182 * with the close-on-exec flag set
183 */
184 if (conf->reboot) {
185 INFO("rebooting container");
186 execvp(argv[0], argv);
187 SYSERROR("failed to exec");
188 err = -1;
189 }
190
b0a33c1e 191 return err;
5e97c3fc 192}
193