]>
git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/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>
36 #include <sys/types.h>
38 #include <sys/prctl.h>
40 #include <netinet/in.h>
45 LXC_TTY_HANDLER(SIGINT
);
46 LXC_TTY_HANDLER(SIGQUIT
);
48 int lxc_start(const char *name
, int argc
, char *argv
[],
49 lxc_callback_t prestart
, void *data
)
51 char *init
= NULL
, *val
= NULL
;
52 int fd
, lock
, sv
[2], sync
= 0, err
= -1;
56 lock
= lxc_get_lock(name
);
58 lxc_log_error("'%s' is busy", name
);
63 lxc_log_error("failed to acquire lock on '%s':%s",
64 name
, strerror(-lock
));
68 fcntl(lock
, F_SETFD
, FD_CLOEXEC
);
70 /* Begin the set the state to STARTING*/
71 if (lxc_setstate(name
, STARTING
)) {
72 lxc_log_error("failed to set state %s", lxc_state2str(STARTING
));
76 /* Synchro socketpair */
77 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
78 lxc_log_syserror("failed to create communication socketpair");
82 /* Avoid signals from terminal */
83 LXC_TTY_ADD_HANDLER(SIGINT
);
84 LXC_TTY_ADD_HANDLER(SIGQUIT
);
86 clone_flags
= CLONE_NEWPID
|CLONE_NEWIPC
;
87 if (conf_has_fstab(name
))
88 clone_flags
|= CLONE_NEWNS
;
89 if (conf_has_utsname(name
))
90 clone_flags
|= CLONE_NEWUTS
;
91 if (conf_has_network(name
))
92 clone_flags
|= CLONE_NEWNET
;
94 /* Create a process in a new set of namespaces */
95 pid
= fork_ns(clone_flags
);
97 lxc_log_syserror("failed to fork into a new namespace");
105 /* Be sure we don't inherit this after the exec */
106 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
108 /* Tell our father he can begin to configure the container */
109 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
110 lxc_log_syserror("failed to write socket");
114 /* Wait for the father to finish the configuration */
115 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
116 lxc_log_syserror("failed to read socket");
120 /* Setup the container, ip, names, utsname, ... */
121 if (lxc_setup(name
)) {
122 lxc_log_error("failed to setup the container");
123 if (write(sv
[0], &sync
, sizeof(sync
)) < 0)
124 lxc_log_syserror("failed to write the socket");
128 /* If a callback has been passed, call it before doing exec */
130 if (prestart(name
, argc
, argv
, data
)) {
131 lxc_log_error("prestart callback has failed");
135 execvp(argv
[0], argv
);
136 lxc_log_syserror("failed to exec %s", argv
[0]);
138 /* If the exec fails, tell that to our father */
139 if (write(sv
[0], &sync
, sizeof(sync
)) < 0)
140 lxc_log_syserror("failed to write the socket");
147 /* Wait for the child to be ready */
148 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
149 lxc_log_syserror("failed to read the socket");
153 /* Create the network configuration */
154 if (clone_flags
& CLONE_NEWNET
&& conf_create_network(name
, pid
)) {
155 lxc_log_error("failed to create the configured network");
156 goto err_create_network
;
159 /* Tell the child to continue its initialization */
160 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
161 lxc_log_syserror("failed to write the socket");
165 /* Wait for the child to exec or returning an error */
166 err
= read(sv
[1], &sync
, sizeof(sync
));
168 lxc_log_error("failed to read the socket");
173 lxc_log_error("something went wrong with %d", pid
);
174 /* TODO : check status etc ... */
175 waitpid(pid
, NULL
, 0);
176 goto err_child_failed
;
179 asprintf(&val
, "%d\n", pid
);
180 asprintf(&init
, LXCPATH
"/%s/init", name
);
181 fd
= open(init
, O_WRONLY
|O_CREAT
, S_IRUSR
|S_IWUSR
);
183 lxc_log_syserror("failed to open '%s'", init
);
187 if (write(fd
, val
, strlen(val
)) < 0) {
188 lxc_log_syserror("failed to write the init pid");
194 if (lxc_link_nsgroup(name
, pid
))
195 lxc_log_warning("cgroupfs not found: cgroup disabled");
197 if (lxc_setstate(name
, RUNNING
)) {
198 lxc_log_error("failed to set state to %s", lxc_state2str(RUNNING
));
199 goto err_state_failed
;
203 if (waitpid(pid
, NULL
, 0) < 0) {
206 lxc_log_syserror("failed to wait the pid %d", pid
);
207 goto err_waitpid_failed
;
210 if (lxc_setstate(name
, STOPPING
))
211 lxc_log_error("failed to set state %s", lxc_state2str(STOPPING
));
213 if (clone_flags
& CLONE_NEWNET
&& conf_destroy_network(name
))
214 lxc_log_error("failed to destroy the network");
218 if (lxc_setstate(name
, STOPPED
))
219 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED
));
221 lxc_unlink_nsgroup(name
);
236 if (clone_flags
& CLONE_NEWNET
)
237 conf_destroy_network(name
);
241 if (lxc_setstate(name
, ABORTING
))
242 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED
));
246 LXC_TTY_DEL_HANDLER(SIGQUIT
);
247 LXC_TTY_DEL_HANDLER(SIGINT
);