]> git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/execute.c
Added C++ compatibility, change to libtool, improve monitoring
[mirror_lxc.git] / src / liblxc / execute.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 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <sys/prctl.h>
36 #include <sys/wait.h>
37 #include <sys/file.h>
38 #include <sys/mount.h>
39 #include <netinet/in.h>
40 #include <net/if.h>
41
42 #include <lxc.h>
43
44 LXC_TTY_HANDLER(SIGINT);
45 LXC_TTY_HANDLER(SIGQUIT);
46
47 int lxc_execute(const char *name, int argc, char *argv[],
48 lxc_callback_t preexec, void *data)
49 {
50 char *init = NULL, *val = NULL, *vinit = "[vinit]";
51 int fd, lock, sv[2], sync = 0, err = -1;
52 pid_t pid;
53 int clone_flags;
54
55 lock = lxc_get_lock(name);
56 if (!lock) {
57 lxc_log_error("'%s' is busy", name);
58 return -1;
59 }
60
61 if (lock < 0) {
62 lxc_log_error("failed to acquire lock on '%s':%s",
63 name, strerror(-lock));
64 return -1;
65 }
66
67 if (lxc_setstate(name, STARTING)) {
68 lxc_log_error("failed to set state %s", lxc_state2str(STARTING));
69 goto out;
70 }
71
72 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
73 lxc_log_syserror("failed to create communication socketpair");
74 goto err;
75 }
76
77 LXC_TTY_ADD_HANDLER(SIGINT);
78 LXC_TTY_ADD_HANDLER(SIGQUIT);
79
80 clone_flags = CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
81 if (conf_has_utsname(name))
82 clone_flags |= CLONE_NEWUTS;
83 if (conf_has_network(name))
84 clone_flags |= CLONE_NEWNET;
85
86 pid = fork_ns(clone_flags);
87 if (pid < 0) {
88 lxc_log_syserror("failed to fork into a new namespace");
89 goto err_fork_ns;
90 }
91
92 if (!pid) {
93
94 pid = fork();
95 if (pid < 0) {
96 lxc_log_syserror("failed to fork");
97 return 1;
98 }
99
100 if (!pid) {
101 close(sv[1]);
102 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
103
104 if (write(sv[0], &sync, sizeof(sync)) < 0) {
105 lxc_log_syserror("failed to write socket");
106 return 1;
107 }
108
109 if (read(sv[0], &sync, sizeof(sync)) < 0) {
110 lxc_log_syserror("failed to read socket");
111 return 1;
112 }
113
114 if (lxc_setup(name)) {
115 lxc_log_error("failed to setup the container");
116 goto error;
117 }
118
119 if (mount("proc", "/proc", "proc", 0, NULL)) {
120 lxc_log_syserror("failed to mount '/proc'");
121 goto error;
122 }
123
124 if (mount("sysfs", "/sys", "sysfs", 0, NULL)) {
125 lxc_log_syserror("failed to mount '/sys'");
126 /* continue: non fatal error until sysfs not per
127 namespace */
128 }
129
130 if (preexec)
131 if (preexec(name, argc, argv, data)) {
132 lxc_log_error("preexec callback has failed");
133 return -1;
134 }
135
136 execvp(argv[0], argv);
137 error:
138 lxc_log_syserror("failed to exec %s", argv[0]);
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 setsid();
146 close(0);
147 close(1);
148 close(2);
149
150 if (prctl(PR_SET_NAME, vinit, 0, 0, 0))
151 lxc_log_syserror("failed to set process name");
152
153 close(sv[0]);
154 close(sv[1]);
155
156 for (;;) {
157 int status;
158 if (wait(&status) < 0) {
159 if (errno == ECHILD)
160 return 0;
161 if (errno == EINTR)
162 continue;
163 lxc_log_syserror("failed to wait child");
164 return 1;
165 }
166 }
167 }
168
169 close(sv[0]);
170
171 if (read(sv[1], &sync, sizeof(sync)) < 0) {
172 lxc_log_syserror("failed to read the socket");
173 goto err_pipe_read;
174 }
175
176 if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) {
177 lxc_log_error("failed to create the configured network");
178 goto err_create_network;
179 }
180
181 if (write(sv[1], &sync, sizeof(sync)) < 0) {
182 lxc_log_syserror("failed to write the socket");
183 goto err_pipe_write;
184 }
185
186 err = read(sv[1], &sync, sizeof(sync));
187 if (err < 0) {
188 lxc_log_error("failed to read the socket");
189 goto err_pipe_read2;
190 }
191
192 if (err > 0) {
193 lxc_log_error("something went wrong with %d", pid);
194 /* TODO : check status etc ... */
195 waitpid(pid, NULL, 0);
196 goto err_child_failed;
197 }
198
199 asprintf(&init, LXCPATH "/%s/init", name);
200 fd = open(init, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
201 if (fd < 0) {
202 lxc_log_syserror("failed to open %s", init);
203 goto err_open;
204 }
205
206 asprintf(&val, "%d", pid);
207 if (write(fd, val, strlen(val)) < 0) {
208 lxc_log_syserror("failed to write init pid");
209 goto err_write;
210 }
211
212 if (lxc_link_nsgroup(name, pid))
213 lxc_log_warning("cgroupfs not found: cgroup disabled");
214
215 if (lxc_setstate(name, RUNNING)) {
216 lxc_log_error("failed to set state to %s", lxc_state2str(RUNNING));
217 goto err_state_failed;
218 }
219
220 wait_again:
221 if (waitpid(pid, NULL, 0) < 0) {
222 if (errno == EINTR)
223 goto wait_again;
224 lxc_log_syserror("failed to wait the pid %d", pid);
225 goto err_waitpid_failed;
226 }
227
228 if (lxc_setstate(name, STOPPING))
229 lxc_log_error("failed to set state %s", lxc_state2str(STOPPING));
230
231 if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
232 lxc_log_error("failed to destroy the network");
233
234 err = 0;
235 out:
236 if (lxc_setstate(name, STOPPED))
237 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
238
239 lxc_unlink_nsgroup(name);
240 unlink(init);
241 free(init);
242 free(val);
243 lxc_put_lock(lock);
244
245 return err;
246
247 err_write:
248 close(fd);
249
250 err_state_failed:
251 err_child_failed:
252 err_pipe_read2:
253 err_pipe_write:
254 conf_destroy_network(name);
255 err_create_network:
256 err_pipe_read:
257 err_open:
258 err_waitpid_failed:
259 if (lxc_setstate(name, ABORTING))
260 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
261
262 kill(pid, SIGKILL);
263 err_fork_ns:
264 LXC_TTY_DEL_HANDLER(SIGQUIT);
265 LXC_TTY_DEL_HANDLER(SIGINT);
266 close(sv[0]);
267 close(sv[1]);
268 err:
269 goto out;
270 }