]> git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/start.c
Added C++ compatibility, change to libtool, improve monitoring
[mirror_lxc.git] / src / liblxc / start.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #undef _GNU_SOURCE
27 #include <string.h>
28 #include <stdlib.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <mntent.h>
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/prctl.h>
39 #include <sys/wait.h>
40 #include <netinet/in.h>
41 #include <net/if.h>
42
43 #include <lxc.h>
44
45 LXC_TTY_HANDLER(SIGINT);
46 LXC_TTY_HANDLER(SIGQUIT);
47
48 int lxc_start(const char *name, int argc, char *argv[],
49 lxc_callback_t prestart, void *data)
50 {
51 char *init = NULL, *val = NULL;
52 int fd, lock, sv[2], sync = 0, err = -1;
53 pid_t pid;
54 int clone_flags;
55
56 lock = lxc_get_lock(name);
57 if (!lock) {
58 lxc_log_error("'%s' is busy", name);
59 return -1;
60 }
61
62 if (lock < 0) {
63 lxc_log_error("failed to acquire lock on '%s':%s",
64 name, strerror(-lock));
65 return -1;
66 }
67
68 fcntl(lock, F_SETFD, FD_CLOEXEC);
69
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));
73 goto out;
74 }
75
76 /* Synchro socketpair */
77 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
78 lxc_log_syserror("failed to create communication socketpair");
79 goto err;
80 }
81
82 /* Avoid signals from terminal */
83 LXC_TTY_ADD_HANDLER(SIGINT);
84 LXC_TTY_ADD_HANDLER(SIGQUIT);
85
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;
93
94 /* Create a process in a new set of namespaces */
95 pid = fork_ns(clone_flags);
96 if (pid < 0) {
97 lxc_log_syserror("failed to fork into a new namespace");
98 goto err_fork_ns;
99 }
100
101 if (!pid) {
102
103 close(sv[1]);
104
105 /* Be sure we don't inherit this after the exec */
106 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
107
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");
111 return 1;
112 }
113
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");
117 return 1;
118 }
119
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");
125 return -1;
126 }
127
128 /* If a callback has been passed, call it before doing exec */
129 if (prestart)
130 if (prestart(name, argc, argv, data)) {
131 lxc_log_error("prestart callback has failed");
132 return -1;
133 }
134
135 execvp(argv[0], argv);
136 lxc_log_syserror("failed to exec %s", argv[0]);
137
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");
141
142 return 1;
143 }
144
145 close(sv[0]);
146
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");
150 goto err_pipe_read;
151 }
152
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;
157 }
158
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");
162 goto err_pipe_write;
163 }
164
165 /* Wait for the child to exec or returning an error */
166 err = read(sv[1], &sync, sizeof(sync));
167 if (err < 0) {
168 lxc_log_error("failed to read the socket");
169 goto err_pipe_read2;
170 }
171
172 if (err > 0) {
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;
177 }
178
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);
182 if (fd < 0) {
183 lxc_log_syserror("failed to open '%s'", init);
184 goto err_write;
185 }
186
187 if (write(fd, val, strlen(val)) < 0) {
188 lxc_log_syserror("failed to write the init pid");
189 goto err_write;
190 }
191
192 close(fd);
193
194 if (lxc_link_nsgroup(name, pid))
195 lxc_log_warning("cgroupfs not found: cgroup disabled");
196
197 if (lxc_setstate(name, RUNNING)) {
198 lxc_log_error("failed to set state to %s", lxc_state2str(RUNNING));
199 goto err_state_failed;
200 }
201
202 wait_again:
203 if (waitpid(pid, NULL, 0) < 0) {
204 if (errno == EINTR)
205 goto wait_again;
206 lxc_log_syserror("failed to wait the pid %d", pid);
207 goto err_waitpid_failed;
208 }
209
210 if (lxc_setstate(name, STOPPING))
211 lxc_log_error("failed to set state %s", lxc_state2str(STOPPING));
212
213 if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
214 lxc_log_error("failed to destroy the network");
215
216 err = 0;
217 out:
218 if (lxc_setstate(name, STOPPED))
219 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
220
221 lxc_unlink_nsgroup(name);
222 unlink(init);
223 free(init);
224 free(val);
225 lxc_put_lock(lock);
226
227 return err;
228
229 err_write:
230 close(fd);
231
232 err_state_failed:
233 err_child_failed:
234 err_pipe_read2:
235 err_pipe_write:
236 if (clone_flags & CLONE_NEWNET)
237 conf_destroy_network(name);
238 err_create_network:
239 err_pipe_read:
240 err_waitpid_failed:
241 if (lxc_setstate(name, ABORTING))
242 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
243
244 kill(pid, SIGKILL);
245 err_fork_ns:
246 LXC_TTY_DEL_HANDLER(SIGQUIT);
247 LXC_TTY_DEL_HANDLER(SIGINT);
248 close(sv[0]);
249 close(sv[1]);
250 err:
251 goto out;
252 }