]>
git.proxmox.com Git - mirror_lxc.git/blob - src/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
33 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/types.h>
37 #include <sys/prctl.h>
42 LXC_TTY_HANDLER(SIGINT
);
43 LXC_TTY_HANDLER(SIGQUIT
);
45 int opentty(const char *ttyname
)
49 fd
= open(ttyname
, O_RDWR
| O_NONBLOCK
);
51 lxc_log_syserror("open '%s'", ttyname
);
55 flags
= fcntl(fd
, F_GETFL
);
57 fcntl(fd
, F_SETFL
, flags
);
59 for (i
= 0; i
< fd
; i
++)
61 for (i
= 0; i
< 3; i
++)
70 int lxc_start(const char *name
, int argc
, char *argv
[],
71 lxc_callback_t prestart
, void *data
)
73 char *init
= NULL
, *val
= NULL
;
74 char ttyname
[MAXPATHLEN
];
75 int fd
, lock
, sv
[2], sync
= 0, err
= -1;
79 lock
= lxc_get_lock(name
);
81 lxc_log_error("'%s' is busy", name
);
86 lxc_log_error("failed to acquire lock on '%s':%s",
87 name
, strerror(-lock
));
91 /* Begin the set the state to STARTING*/
92 if (lxc_setstate(name
, STARTING
)) {
93 lxc_log_error("failed to set state %s", lxc_state2str(STARTING
));
97 if (readlink("/proc/self/fd/0", ttyname
, sizeof(ttyname
)) < 0) {
98 lxc_log_syserror("failed to read '/proc/self/fd/0'");
103 /* Synchro socketpair */
104 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
105 lxc_log_syserror("failed to create communication socketpair");
109 /* Avoid signals from terminal */
110 LXC_TTY_ADD_HANDLER(SIGINT
);
111 LXC_TTY_ADD_HANDLER(SIGQUIT
);
113 clone_flags
= CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
114 if (conf_has_utsname(name
))
115 clone_flags
|= CLONE_NEWUTS
;
116 if (conf_has_network(name
))
117 clone_flags
|= CLONE_NEWNET
;
119 /* Create a process in a new set of namespaces */
120 pid
= fork_ns(clone_flags
);
122 lxc_log_syserror("failed to fork into a new namespace");
130 /* Be sure we don't inherit this after the exec */
131 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
133 /* Tell our father he can begin to configure the container */
134 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
135 lxc_log_syserror("failed to write socket");
139 /* Wait for the father to finish the configuration */
140 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
141 lxc_log_syserror("failed to read socket");
145 /* Setup the container, ip, names, utsname, ... */
146 if (lxc_setup(name
)) {
147 lxc_log_error("failed to setup the container");
148 if (write(sv
[0], &sync
, sizeof(sync
)) < 0)
149 lxc_log_syserror("failed to write the socket");
154 if (opentty(ttyname
)) {
155 lxc_log_syserror("failed to open the tty");
159 if (mount(ttyname
, "/dev/console", "none", MS_BIND
, 0)) {
160 lxc_log_syserror("failed to mount '/dev/console'");
164 /* If a callback has been passed, call it before doing exec */
166 if (prestart(name
, argc
, argv
, data
)) {
167 lxc_log_error("prestart callback has failed");
171 execvp(argv
[0], argv
);
172 lxc_log_syserror("failed to exec %s", argv
[0]);
174 /* If the exec fails, tell that to our father */
175 if (write(sv
[0], &sync
, sizeof(sync
)) < 0)
176 lxc_log_syserror("failed to write the socket");
183 /* Wait for the child to be ready */
184 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
185 lxc_log_syserror("failed to read the socket");
189 /* Create the network configuration */
190 if (clone_flags
& CLONE_NEWNET
&& conf_create_network(name
, pid
)) {
191 lxc_log_error("failed to create the configured network");
192 goto err_create_network
;
195 /* Tell the child to continue its initialization */
196 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
197 lxc_log_syserror("failed to write the socket");
201 /* Wait for the child to exec or returning an error */
202 err
= read(sv
[1], &sync
, sizeof(sync
));
204 lxc_log_error("failed to read the socket");
209 lxc_log_error("something went wrong with %d", pid
);
210 /* TODO : check status etc ... */
211 waitpid(pid
, NULL
, 0);
212 goto err_child_failed
;
215 asprintf(&val
, "%d\n", pid
);
216 asprintf(&init
, LXCPATH
"/%s/init", name
);
217 fd
= open(init
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRUSR
|S_IWUSR
);
219 lxc_log_syserror("failed to open '%s'", init
);
223 if (write(fd
, val
, strlen(val
)) < 0) {
224 lxc_log_syserror("failed to write the init pid");
230 if (lxc_link_nsgroup(name
, pid
))
231 lxc_log_warning("cgroupfs not found: cgroup disabled");
233 if (lxc_setstate(name
, RUNNING
)) {
234 lxc_log_error("failed to set state to %s",
235 lxc_state2str(RUNNING
));
236 goto err_state_failed
;
240 if (waitpid(pid
, NULL
, 0) < 0) {
243 lxc_log_syserror("failed to wait the pid %d", pid
);
244 goto err_waitpid_failed
;
247 if (lxc_setstate(name
, STOPPING
))
248 lxc_log_error("failed to set state %s", lxc_state2str(STOPPING
));
250 if (clone_flags
& CLONE_NEWNET
&& conf_destroy_network(name
))
251 lxc_log_error("failed to destroy the network");
255 if (lxc_setstate(name
, STOPPED
))
256 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED
));
258 lxc_unlink_nsgroup(name
);
273 if (clone_flags
& CLONE_NEWNET
)
274 conf_destroy_network(name
);
278 if (lxc_setstate(name
, ABORTING
))
279 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED
));
283 LXC_TTY_DEL_HANDLER(SIGQUIT
);
284 LXC_TTY_DEL_HANDLER(SIGINT
);