]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_start.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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
34 #include <sys/param.h>
35 #include <sys/utsname.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <arpa/inet.h>
40 #include <netinet/in.h>
49 #include "arguments.h"
51 lxc_log_define(lxc_start
, lxc
);
53 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
56 case 'd': args
->daemonize
= 1; break;
57 case 'f': args
->rcfile
= arg
; break;
62 static const struct option my_longopts
[] = {
63 {"daemon", no_argument
, 0, 'd'},
64 {"rcfile", required_argument
, 0, 'f'},
68 static struct lxc_arguments my_args
= {
69 .progname
= "lxc-start",
71 --name=NAME -- COMMAND\n\
73 lxc-start start COMMAND in specified container NAME\n\
76 -n, --name=NAME NAME for name of the container\n\
77 -d, --daemon daemonize the container\n\
78 -f, --rcfile=FILE Load configuration file FILE\n",
79 .options
= my_longopts
,
85 static int save_tty(struct termios
*tios
)
90 if (tcgetattr(0, tios
))
91 WARN("failed to get current terminal settings : %s",
97 static int restore_tty(struct termios
*tios
)
99 struct termios current_tios
;
100 void (*oldhandler
)(int);
106 if (tcgetattr(0, ¤t_tios
)) {
107 ERROR("failed to get current terminal settings : %s",
112 if (!memcmp(tios
, ¤t_tios
, sizeof(*tios
)))
115 oldhandler
= signal(SIGTTOU
, SIG_IGN
);
116 ret
= tcsetattr(0, TCSADRAIN
, tios
);
118 ERROR("failed to restore terminal attributes");
119 signal(SIGTTOU
, oldhandler
);
124 int main(int argc
, char *argv
[])
130 char *const default_args
[] = {
136 struct lxc_conf
*conf
;
138 if (lxc_arguments_parse(&my_args
, argc
, argv
))
146 if (lxc_log_init(my_args
.log_file
, my_args
.log_priority
,
147 my_args
.progname
, my_args
.quiet
))
150 /* rcfile is specified in the cli option */
152 rcfile
= (char *)my_args
.rcfile
;
154 if (!asprintf(&rcfile
, LXCPATH
"/%s/config", my_args
.name
)) {
155 SYSERROR("failed to allocate memory");
159 /* container configuration does not exist */
160 if (access(rcfile
, F_OK
)) {
166 conf
= lxc_conf_init();
168 ERROR("failed to initialize configuration");
172 if (rcfile
&& lxc_config_read(rcfile
, conf
)) {
173 ERROR("failed to read configuration file");
177 if (!rcfile
&& !strcmp("/sbin/init", args
[0])) {
178 ERROR("no configuration file for '/sbin/init' (may crash the host)");
182 if (my_args
.daemonize
) {
184 /* do not chdir as we want to open the log file,
185 * change the directory right after.
186 * do not close 0, 1, 2, we want to do that
187 * ourself because we don't want /dev/null
191 SYSERROR("failed to daemonize '%s'", my_args
.name
);
195 lxc_close_inherited_fd(0);
196 lxc_close_inherited_fd(1);
197 lxc_close_inherited_fd(2);
199 if (my_args
.log_file
) {
200 open(my_args
.log_file
, O_WRONLY
| O_CLOEXEC
);
201 open(my_args
.log_file
, O_RDONLY
| O_CLOEXEC
);
202 open(my_args
.log_file
, O_RDONLY
| O_CLOEXEC
);
208 err
= lxc_start(my_args
.name
, args
, conf
);