]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/execute.c
Joined liblxc and lxc directory
[mirror_lxc.git] / src / lxc / 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 (conf_has_network(name))
125 if (mount("sysfs", "/sys", "sysfs", 0, NULL)) {
126 lxc_log_syserror("failed to mount '/sys'");
127 /* continue: non fatal error until sysfs not per
128 namespace */
129 }
130
131 if (preexec)
132 if (preexec(name, argc, argv, data)) {
133 lxc_log_error("preexec callback has failed");
134 return -1;
135 }
136
137 execvp(argv[0], argv);
138 lxc_log_syserror("failed to exec %s", argv[0]);
139 error:
140 if (write(sv[0], &sync, sizeof(sync)) < 0)
141 lxc_log_syserror("failed to write the socket");
142
143 exit(1);
144 }
145
146 setsid();
147 close(0);
148 close(1);
149 close(2);
150
151 if (prctl(PR_SET_NAME, vinit, 0, 0, 0))
152 lxc_log_syserror("failed to set process name");
153
154 close(sv[0]);
155 close(sv[1]);
156
157 for (;;) {
158 int status;
159 if (wait(&status) < 0) {
160 if (errno == ECHILD)
161 return 0;
162 if (errno == EINTR)
163 continue;
164 lxc_log_syserror("failed to wait child");
165 return 1;
166 }
167 }
168 }
169
170 close(sv[0]);
171
172 if (read(sv[1], &sync, sizeof(sync)) < 0) {
173 lxc_log_syserror("failed to read the socket");
174 goto err_pipe_read;
175 }
176
177 if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) {
178 lxc_log_error("failed to create the configured network");
179 goto err_create_network;
180 }
181
182 if (write(sv[1], &sync, sizeof(sync)) < 0) {
183 lxc_log_syserror("failed to write the socket");
184 goto err_pipe_write;
185 }
186
187 err = read(sv[1], &sync, sizeof(sync));
188 if (err < 0) {
189 lxc_log_error("failed to read the socket");
190 goto err_pipe_read2;
191 }
192
193 if (err > 0) {
194 lxc_log_error("something went wrong with %d", pid);
195 /* TODO : check status etc ... */
196 waitpid(pid, NULL, 0);
197 goto err_child_failed;
198 }
199
200 asprintf(&init, LXCPATH "/%s/init", name);
201 fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
202 if (fd < 0) {
203 lxc_log_syserror("failed to open %s", init);
204 goto err_open;
205 }
206
207 asprintf(&val, "%d", pid);
208 if (write(fd, val, strlen(val)) < 0) {
209 lxc_log_syserror("failed to write init pid");
210 goto err_write;
211 }
212
213 if (lxc_link_nsgroup(name, pid))
214 lxc_log_warning("cgroupfs not found: cgroup disabled");
215
216 if (lxc_setstate(name, RUNNING)) {
217 lxc_log_error("failed to set state to %s", lxc_state2str(RUNNING));
218 goto err_state_failed;
219 }
220
221 wait_again:
222 if (waitpid(pid, NULL, 0) < 0) {
223 if (errno == EINTR)
224 goto wait_again;
225 lxc_log_syserror("failed to wait the pid %d", pid);
226 goto err_waitpid_failed;
227 }
228
229 if (lxc_setstate(name, STOPPING))
230 lxc_log_error("failed to set state %s", lxc_state2str(STOPPING));
231
232 if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
233 lxc_log_error("failed to destroy the network");
234
235 err = 0;
236 out:
237 if (lxc_setstate(name, STOPPED))
238 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
239
240 lxc_unlink_nsgroup(name);
241 unlink(init);
242 free(init);
243 free(val);
244 lxc_put_lock(lock);
245
246 return err;
247
248 err_write:
249 close(fd);
250
251 err_state_failed:
252 err_child_failed:
253 err_pipe_read2:
254 err_pipe_write:
255 conf_destroy_network(name);
256 err_create_network:
257 err_pipe_read:
258 err_open:
259 err_waitpid_failed:
260 if (lxc_setstate(name, ABORTING))
261 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
262
263 kill(pid, SIGKILL);
264 err_fork_ns:
265 LXC_TTY_DEL_HANDLER(SIGQUIT);
266 LXC_TTY_DEL_HANDLER(SIGINT);
267 close(sv[0]);
268 close(sv[1]);
269 err:
270 goto out;
271 }