]>
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>
51 #include "arguments.h"
53 lxc_log_define(lxc_start_ui
, lxc_start
);
55 static struct lxc_list defines
;
57 static int ensure_path(char **confpath
, const char *path
)
60 char *fullpath
= NULL
;
63 if (access(path
, W_OK
)) {
64 fd
= creat(path
, 0600);
66 SYSERROR("failed to create '%s'", path
);
72 fullpath
= realpath(path
, NULL
);
74 SYSERROR("failed to get the real path of '%s'", path
);
78 *confpath
= strdup(fullpath
);
80 ERROR("failed to dup string '%s'", fullpath
);
92 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
95 case 'c': args
->console
= arg
; break;
96 case 'L': args
->console_log
= arg
; break;
97 case 'd': args
->daemonize
= 1; args
->close_all_fds
= 1; break;
98 case 'f': args
->rcfile
= arg
; break;
99 case 'C': args
->close_all_fds
= 1; break;
100 case 's': return lxc_config_define_add(&defines
, arg
);
101 case 'p': args
->pidfile
= arg
; break;
106 static const struct option my_longopts
[] = {
107 {"daemon", no_argument
, 0, 'd'},
108 {"rcfile", required_argument
, 0, 'f'},
109 {"define", required_argument
, 0, 's'},
110 {"console", required_argument
, 0, 'c'},
111 {"console-log", required_argument
, 0, 'L'},
112 {"close-all-fds", no_argument
, 0, 'C'},
113 {"pidfile", required_argument
, 0, 'p'},
117 static struct lxc_arguments my_args
= {
118 .progname
= "lxc-start",
120 --name=NAME -- COMMAND\n\
122 lxc-start start COMMAND in specified container NAME\n\
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",
135 .options
= my_longopts
,
142 int main(int argc
, char *argv
[])
145 struct lxc_conf
*conf
;
148 char *const default_args
[] = {
154 lxc_list_init(&defines
);
159 if (lxc_arguments_parse(&my_args
, argc
, argv
))
167 if (lxc_log_init(my_args
.log_file
, my_args
.log_priority
,
168 my_args
.progname
, my_args
.quiet
))
171 /* rcfile is specified in the cli option */
173 rcfile
= (char *)my_args
.rcfile
;
177 rc
= asprintf(&rcfile
, LXCPATH
"/%s/config", my_args
.name
);
179 SYSERROR("failed to allocate memory");
183 /* container configuration does not exist */
184 if (access(rcfile
, F_OK
)) {
190 conf
= lxc_conf_init();
192 ERROR("failed to initialize configuration");
196 if (rcfile
&& lxc_config_read(rcfile
, conf
)) {
197 ERROR("failed to read configuration file");
201 if (lxc_config_define_load(&defines
, conf
))
204 if (!rcfile
&& !strcmp("/sbin/init", args
[0])) {
205 ERROR("no configuration file for '/sbin/init' (may crash the host)");
209 if (ensure_path(&conf
->console
.path
, my_args
.console
) < 0) {
210 ERROR("failed to ensure console path '%s'", my_args
.console
);
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
);
219 if (my_args
.pidfile
!= NULL
) {
220 pid_fp
= fopen(my_args
.pidfile
, "w");
221 if (pid_fp
== NULL
) {
222 SYSERROR("failed to create pidfile '%s' for '%s'",
223 my_args
.pidfile
, my_args
.name
);
228 if (my_args
.daemonize
) {
229 /* do an early check for needed privs, since otherwise the
230 * user won't see the error */
232 if (!lxc_caps_check()) {
233 ERROR("Not running with sufficient privilege");
238 SYSERROR("failed to daemonize '%s'", my_args
.name
);
243 if (pid_fp
!= NULL
) {
244 if (fprintf(pid_fp
, "%d\n", getpid()) < 0) {
245 SYSERROR("failed to write '%s'", my_args
.pidfile
);
251 if (my_args
.close_all_fds
)
252 conf
->close_all_fds
= 1;
254 err
= lxc_start(my_args
.name
, args
, conf
);
257 * exec ourself, that requires to have all opened fd
258 * with the close-on-exec flag set
261 INFO("rebooting container");
262 execvp(argv
[0], argv
);
263 SYSERROR("failed to exec");
268 unlink(my_args
.pidfile
);