]>
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 <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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>
46 #include <lxc/lxccontainer.h>
52 #include "arguments.h"
54 lxc_log_define(lxc_start_ui
, lxc_start
);
56 static struct lxc_list defines
;
58 static int ensure_path(char **confpath
, const char *path
)
61 char *fullpath
= NULL
;
64 if (access(path
, W_OK
)) {
65 fd
= creat(path
, 0600);
66 if (fd
< 0 && errno
!= EEXIST
) {
67 SYSERROR("failed to create '%s'", path
);
74 fullpath
= realpath(path
, NULL
);
76 SYSERROR("failed to get the real path of '%s'", path
);
80 *confpath
= strdup(fullpath
);
82 ERROR("failed to dup string '%s'", fullpath
);
94 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
97 case 'c': args
->console
= arg
; break;
98 case 'L': args
->console_log
= arg
; break;
99 case 'd': args
->daemonize
= 1; args
->close_all_fds
= 1; break;
100 case 'f': args
->rcfile
= arg
; break;
101 case 'C': args
->close_all_fds
= 1; break;
102 case 's': return lxc_config_define_add(&defines
, arg
);
103 case 'p': args
->pidfile
= arg
; break;
108 static const struct option my_longopts
[] = {
109 {"daemon", no_argument
, 0, 'd'},
110 {"rcfile", required_argument
, 0, 'f'},
111 {"define", required_argument
, 0, 's'},
112 {"console", required_argument
, 0, 'c'},
113 {"console-log", required_argument
, 0, 'L'},
114 {"close-all-fds", no_argument
, 0, 'C'},
115 {"pidfile", required_argument
, 0, 'p'},
119 static struct lxc_arguments my_args
= {
120 .progname
= "lxc-start",
122 --name=NAME -- COMMAND\n\
124 lxc-start start COMMAND in specified container NAME\n\
127 -n, --name=NAME NAME for name of the container\n\
128 -d, --daemon daemonize the container\n\
129 -p, --pidfile=FILE Create a file with the process id\n\
130 -f, --rcfile=FILE Load configuration file FILE\n\
131 -c, --console=FILE Use specified FILE for the container console\n\
132 -L, --console-log=FILE Log container console output to FILE\n\
133 -C, --close-all-fds If any fds are inherited, close them\n\
134 If not specified, exit with failure instead\n\
135 Note: --daemon implies --close-all-fds\n\
136 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
137 .options
= my_longopts
,
144 int main(int argc
, char *argv
[])
147 struct lxc_conf
*conf
;
150 char *const default_args
[] = {
155 struct lxc_container
*c
;
157 lxc_list_init(&defines
);
162 if (lxc_arguments_parse(&my_args
, argc
, argv
))
170 if (lxc_log_init(my_args
.name
, my_args
.log_file
, my_args
.log_priority
,
171 my_args
.progname
, my_args
.quiet
, my_args
.lxcpath
[0]))
174 const char *lxcpath
= my_args
.lxcpath
[0];
177 * rcfile possibilities:
178 * 1. rcfile from random path specified in cli option
179 * 2. rcfile not specified, use $lxcpath/$lxcname/config
180 * 3. rcfile not specified and does not exist.
182 /* rcfile is specified in the cli option */
183 if (my_args
.rcfile
) {
184 rcfile
= (char *)my_args
.rcfile
;
185 c
= lxc_container_new(my_args
.name
, lxcpath
);
187 ERROR("Failed to create lxc_container");
190 if (!c
->load_config(c
, rcfile
)) {
191 ERROR("Failed to load rcfile");
192 lxc_container_put(c
);
198 rc
= asprintf(&rcfile
, "%s/%s/config", lxcpath
, my_args
.name
);
200 SYSERROR("failed to allocate memory");
203 INFO("using rcfile %s", rcfile
);
205 /* container configuration does not exist */
206 if (access(rcfile
, F_OK
)) {
210 c
= lxc_container_new(my_args
.name
, lxcpath
);
212 ERROR("Failed to create lxc_container");
218 * We should use set_config_item() over &defines, which would handle
219 * unset c->lxc_conf for us and let us not use lxc_config_define_load()
222 c
->lxc_conf
= lxc_conf_init();
225 if (lxc_config_define_load(&defines
, conf
))
228 if (!rcfile
&& !strcmp("/sbin/init", args
[0])) {
229 ERROR("Executing '/sbin/init' with no configuration file may crash the host");
233 if (ensure_path(&conf
->console
.path
, my_args
.console
) < 0) {
234 ERROR("failed to ensure console path '%s'", my_args
.console
);
238 if (ensure_path(&conf
->console
.log_path
, my_args
.console_log
) < 0) {
239 ERROR("failed to ensure console log '%s'", my_args
.console_log
);
243 if (my_args
.pidfile
!= NULL
) {
244 pid_fp
= fopen(my_args
.pidfile
, "w");
245 if (pid_fp
== NULL
) {
246 SYSERROR("failed to create pidfile '%s' for '%s'",
247 my_args
.pidfile
, my_args
.name
);
252 if (my_args
.daemonize
) {
253 c
->want_daemonize(c
);
256 if (pid_fp
!= NULL
) {
257 if (fprintf(pid_fp
, "%d\n", getpid()) < 0) {
258 SYSERROR("failed to write '%s'", my_args
.pidfile
);
264 if (my_args
.close_all_fds
)
265 c
->want_close_all_fds(c
);
267 err
= c
->start(c
, 0, args
) ? 0 : -1;
270 unlink(my_args
.pidfile
);
273 lxc_container_put(c
);