]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_start.c
4b6b1f75b14bdac94970ef14d444a6e614b72242
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>
43 #include <lxc/lxccontainer.h>
53 #include "arguments.h"
55 #define OPT_SHARE_NET OPT_USAGE+1
56 #define OPT_SHARE_IPC OPT_USAGE+2
57 #define OPT_SHARE_UTS OPT_USAGE+3
59 lxc_log_define(lxc_start_ui
, lxc_start
);
61 static struct lxc_list defines
;
63 static int ensure_path(char **confpath
, const char *path
)
66 char *fullpath
= NULL
;
69 if (access(path
, W_OK
)) {
70 fd
= creat(path
, 0600);
71 if (fd
< 0 && errno
!= EEXIST
) {
72 SYSERROR("failed to create '%s'", path
);
79 fullpath
= realpath(path
, NULL
);
81 SYSERROR("failed to get the real path of '%s'", path
);
85 *confpath
= strdup(fullpath
);
87 ERROR("failed to dup string '%s'", fullpath
);
99 static int pid_from_lxcname(const char *lxcname_or_pid
, const char *lxcpath
) {
101 int pid
= strtol(lxcname_or_pid
, &eptr
, 10);
102 if (*eptr
!= '\0' || pid
< 1) {
103 struct lxc_container
*s
;
104 s
= lxc_container_new(lxcname_or_pid
, lxcpath
);
106 SYSERROR("'%s' is not a valid pid nor a container name", lxcname_or_pid
);
110 if (!s
->may_control(s
)) {
111 SYSERROR("Insufficient privileges to control container '%s'", s
->name
);
112 lxc_container_put(s
);
116 pid
= s
->init_pid(s
);
118 SYSERROR("Is container '%s' running?", s
->name
);
119 lxc_container_put(s
);
123 lxc_container_put(s
);
125 if (kill(pid
, 0) < 0) {
126 SYSERROR("Can't send signal to pid %d", pid
);
133 static int open_ns(int pid
, const char *ns_proc_name
) {
135 char path
[MAXPATHLEN
];
136 snprintf(path
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns_proc_name
);
138 fd
= open(path
, O_RDONLY
);
140 SYSERROR("failed to open %s", path
);
146 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
149 case 'c': args
->console
= arg
; break;
150 case 'L': args
->console_log
= arg
; break;
151 case 'd': args
->daemonize
= 1; break;
152 case 'f': args
->rcfile
= arg
; break;
153 case 'C': args
->close_all_fds
= 1; break;
154 case 's': return lxc_config_define_add(&defines
, arg
);
155 case 'p': args
->pidfile
= arg
; break;
156 case OPT_SHARE_NET
: args
->share_ns
[LXC_NS_NET
] = arg
; break;
157 case OPT_SHARE_IPC
: args
->share_ns
[LXC_NS_IPC
] = arg
; break;
158 case OPT_SHARE_UTS
: args
->share_ns
[LXC_NS_UTS
] = arg
; break;
163 static const struct option my_longopts
[] = {
164 {"daemon", no_argument
, 0, 'd'},
165 {"rcfile", required_argument
, 0, 'f'},
166 {"define", required_argument
, 0, 's'},
167 {"console", required_argument
, 0, 'c'},
168 {"console-log", required_argument
, 0, 'L'},
169 {"close-all-fds", no_argument
, 0, 'C'},
170 {"pidfile", required_argument
, 0, 'p'},
171 {"share-net", required_argument
, 0, OPT_SHARE_NET
},
172 {"share-ipc", required_argument
, 0, OPT_SHARE_IPC
},
173 {"share-uts", required_argument
, 0, OPT_SHARE_UTS
},
177 static struct lxc_arguments my_args
= {
178 .progname
= "lxc-start",
180 --name=NAME -- COMMAND\n\
182 lxc-start start COMMAND in specified container NAME\n\
185 -n, --name=NAME NAME for name of the container\n\
186 -d, --daemon daemonize the container\n\
187 -p, --pidfile=FILE Create a file with the process id\n\
188 -f, --rcfile=FILE Load configuration file FILE\n\
189 -c, --console=FILE Use specified FILE for the container console\n\
190 -L, --console-log=FILE Log container console output to FILE\n\
191 -C, --close-all-fds If any fds are inherited, close them\n\
192 If not specified, exit with failure instead\n\
193 Note: --daemon implies --close-all-fds\n\
194 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\
195 --share-[net|ipc|uts]=NAME Share a namespace with another container or pid\n\
197 .options
= my_longopts
,
204 int main(int argc
, char *argv
[])
207 struct lxc_conf
*conf
;
210 char *const default_args
[] = {
214 struct lxc_container
*c
;
216 lxc_list_init(&defines
);
221 if (lxc_arguments_parse(&my_args
, argc
, argv
))
229 if (lxc_log_init(my_args
.name
, my_args
.log_file
, my_args
.log_priority
,
230 my_args
.progname
, my_args
.quiet
, my_args
.lxcpath
[0]))
233 const char *lxcpath
= my_args
.lxcpath
[0];
236 * rcfile possibilities:
237 * 1. rcfile from random path specified in cli option
238 * 2. rcfile not specified, use $lxcpath/$lxcname/config
239 * 3. rcfile not specified and does not exist.
241 /* rcfile is specified in the cli option */
242 if (my_args
.rcfile
) {
243 rcfile
= (char *)my_args
.rcfile
;
244 c
= lxc_container_new(my_args
.name
, lxcpath
);
246 ERROR("Failed to create lxc_container");
250 if (!c
->load_config(c
, rcfile
)) {
251 ERROR("Failed to load rcfile");
252 lxc_container_put(c
);
258 rc
= asprintf(&rcfile
, "%s/%s/config", lxcpath
, my_args
.name
);
260 SYSERROR("failed to allocate memory");
263 INFO("using rcfile %s", rcfile
);
265 /* container configuration does not exist */
266 if (access(rcfile
, F_OK
)) {
270 c
= lxc_container_new(my_args
.name
, lxcpath
);
272 ERROR("Failed to create lxc_container");
278 * We should use set_config_item() over &defines, which would handle
279 * unset c->lxc_conf for us and let us not use lxc_config_define_load()
282 c
->lxc_conf
= lxc_conf_init();
285 if (lxc_config_define_load(&defines
, conf
))
288 if (!rcfile
&& !strcmp("/sbin/init", args
[0])) {
289 ERROR("Executing '/sbin/init' with no configuration file may crash the host");
293 if (ensure_path(&conf
->console
.path
, my_args
.console
) < 0) {
294 ERROR("failed to ensure console path '%s'", my_args
.console
);
298 if (ensure_path(&conf
->console
.log_path
, my_args
.console_log
) < 0) {
299 ERROR("failed to ensure console log '%s'", my_args
.console_log
);
303 if (my_args
.pidfile
!= NULL
) {
304 if (ensure_path(&c
->pidfile
, my_args
.pidfile
) < 0) {
305 ERROR("failed to ensure pidfile '%s'", my_args
.pidfile
);
311 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
312 if (my_args
.share_ns
[i
] == NULL
)
315 int pid
= pid_from_lxcname(my_args
.share_ns
[i
], lxcpath
);
319 int fd
= open_ns(pid
, ns_info
[i
].proc_name
);
322 conf
->inherit_ns_fd
[i
] = fd
;
325 if (!my_args
.daemonize
) {
326 c
->want_daemonize(c
, false);
329 if (my_args
.close_all_fds
)
330 c
->want_close_all_fds(c
, true);
332 err
= c
->start(c
, 0, args
) ? 0 : -1;
334 lxc_container_put(c
);