]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_start.c
rename network type enum
[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"
46#include "utils.h"
96c210bb 47#include "config.h"
fae349da
DL
48#include "confile.h"
49#include "arguments.h"
36eb9bde 50
77075659 51lxc_log_define(lxc_start_ui, lxc_start);
5e97c3fc 52
33ba4ad7
CLG
53static struct lxc_list defines;
54
c36583c3
DL
55static int my_parser(struct lxc_arguments* args, int c, char* arg)
56{
57 switch (c) {
58 case 'd': args->daemonize = 1; break;
48862401 59 case 'f': args->rcfile = arg; break;
33ba4ad7 60 case 's': return lxc_config_define_add(&defines, arg);
c36583c3
DL
61 }
62 return 0;
63}
64
9618063c 65static const struct option my_longopts[] = {
c36583c3 66 {"daemon", no_argument, 0, 'd'},
48862401 67 {"rcfile", required_argument, 0, 'f'},
33ba4ad7 68 {"define", required_argument, 0, 's'},
9618063c
MN
69 LXC_COMMON_OPTIONS
70};
71
72static struct lxc_arguments my_args = {
73 .progname = "lxc-start",
74 .help = "\
75--name=NAME -- COMMAND\n\
76\n\
77lxc-start start COMMAND in specified container NAME\n\
78\n\
79Options :\n\
c36583c3 80 -n, --name=NAME NAME for name of the container\n\
48862401 81 -d, --daemon daemonize the container\n\
33ba4ad7
CLG
82 -f, --rcfile=FILE Load configuration file FILE\n\
83 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
c36583c3
DL
84 .options = my_longopts,
85 .parser = my_parser,
86 .checker = NULL,
87 .daemonize = 0,
9618063c 88};
5e97c3fc 89
f8e09a0b
DL
90static int save_tty(struct termios *tios)
91{
92 if (!isatty(0))
93 return 0;
94
95 if (tcgetattr(0, tios))
96 WARN("failed to get current terminal settings : %s",
97 strerror(errno));
98
99 return 0;
100}
101
102static int restore_tty(struct termios *tios)
103{
104 struct termios current_tios;
105 void (*oldhandler)(int);
106 int ret;
107
108 if (!isatty(0))
109 return 0;
110
111 if (tcgetattr(0, &current_tios)) {
112 ERROR("failed to get current terminal settings : %s",
113 strerror(errno));
114 return -1;
115 }
116
117 if (!memcmp(tios, &current_tios, sizeof(*tios)))
118 return 0;
119
f8e09a0b
DL
120 oldhandler = signal(SIGTTOU, SIG_IGN);
121 ret = tcsetattr(0, TCSADRAIN, tios);
122 if (ret)
123 ERROR("failed to restore terminal attributes");
124 signal(SIGTTOU, oldhandler);
125
126 return ret;
127}
128
5e97c3fc 129int main(int argc, char *argv[])
130{
9618063c 131 char *const *args;
e043236e 132 int err = -1;
b0a33c1e 133 struct termios tios;
134
9618063c 135 char *const default_args[] = {
b2b6c597 136 "/sbin/init",
137 '\0',
138 };
5e97c3fc 139
96c210bb 140 char *rcfile = NULL;
7b379ab3 141 struct lxc_conf *conf;
96c210bb 142
33ba4ad7
CLG
143 lxc_list_init(&defines);
144
e043236e
MN
145 if (lxc_arguments_parse(&my_args, argc, argv))
146 return err;
5e97c3fc 147
9618063c 148 if (!my_args.argc)
b2b6c597 149 args = default_args;
9618063c
MN
150 else
151 args = my_args.argv;
5e97c3fc 152
9618063c
MN
153 if (lxc_log_init(my_args.log_file, my_args.log_priority,
154 my_args.progname, my_args.quiet))
e043236e 155 return err;
51cab631 156
96c210bb
DL
157 /* rcfile is specified in the cli option */
158 if (my_args.rcfile)
159 rcfile = (char *)my_args.rcfile;
160 else {
7418c9ce 161 if (!asprintf(&rcfile, LXCPATH "/%s/config", my_args.name)) {
96c210bb
DL
162 SYSERROR("failed to allocate memory");
163 return err;
164 }
165
166 /* container configuration does not exist */
167 if (access(rcfile, F_OK)) {
168 free(rcfile);
169 rcfile = NULL;
170 }
171 }
172
7b379ab3
MN
173 conf = lxc_conf_init();
174 if (!conf) {
175 ERROR("failed to initialize configuration");
fae349da
DL
176 return err;
177 }
178
7b379ab3 179 if (rcfile && lxc_config_read(rcfile, conf)) {
fae349da
DL
180 ERROR("failed to read configuration file");
181 return err;
182 }
183
33ba4ad7
CLG
184 if (lxc_config_define_load(&defines, conf))
185 return err;
186
f2ae79a0
AN
187 if (!rcfile && !strcmp("/sbin/init", args[0])) {
188 ERROR("no configuration file for '/sbin/init' (may crash the host)");
189 return err;
190 }
191
b8f57738
DL
192 if (my_args.daemonize) {
193
194 /* do not chdir as we want to open the log file,
195 * change the directory right after.
196 * do not close 0, 1, 2, we want to do that
197 * ourself because we don't want /dev/null
198 * being reopened.
199 */
af795875 200 if (daemon(1, 1)) {
b8f57738
DL
201 SYSERROR("failed to daemonize '%s'", my_args.name);
202 return err;
203 }
204
af795875
DL
205 lxc_close_inherited_fd(0);
206 lxc_close_inherited_fd(1);
207 lxc_close_inherited_fd(2);
b8f57738
DL
208
209 if (my_args.log_file) {
210 open(my_args.log_file, O_WRONLY | O_CLOEXEC);
211 open(my_args.log_file, O_RDONLY | O_CLOEXEC);
212 open(my_args.log_file, O_RDONLY | O_CLOEXEC);
213 }
c36583c3
DL
214 }
215
f8e09a0b 216 save_tty(&tios);
b0a33c1e 217
7b379ab3 218 err = lxc_start(my_args.name, args, conf);
5e97c3fc 219
f8e09a0b 220 restore_tty(&tios);
b0a33c1e 221
222 return err;
5e97c3fc 223}
224