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