]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 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 | ||
ff218c25 | 24 | #include "../config.h" |
0ad19a3f | 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> | |
b0a33c1e | 33 | #include <fcntl.h> |
34 | #include <termios.h> | |
0ad19a3f | 35 | #include <sys/param.h> |
36 | #include <sys/file.h> | |
f4d507d5 | 37 | #include <sys/mount.h> |
0ad19a3f | 38 | #include <sys/types.h> |
0ad19a3f | 39 | #include <sys/prctl.h> |
42ff343d | 40 | #include <sys/capability.h> |
0ad19a3f | 41 | #include <sys/wait.h> |
b0a33c1e | 42 | #include <sys/un.h> |
43 | #include <sys/poll.h> | |
ff218c25 | 44 | |
45 | #ifdef HAVE_SYS_SIGNALFD_H | |
8ca61733 | 46 | # include <sys/signalfd.h> |
ff218c25 | 47 | #else |
8ca61733 MJ |
48 | # ifndef __NR_signalfd4 |
49 | /* assume kernel headers are too old */ | |
50 | # if __i386__ | |
51 | # define __NR_signalfd4 327 | |
52 | # elif __x86_64__ | |
53 | # define __NR_signalfd4 289 | |
bfa38025 MH |
54 | # elif __powerpc__ |
55 | # define __NR_signalfd4 313 | |
47f38330 SH |
56 | # elif __s390x__ |
57 | # define __NR_signalfd4 322 | |
8ca61733 MJ |
58 | # endif |
59 | #endif | |
60 | ||
61 | # ifndef __NR_signalfd | |
62 | /* assume kernel headers are too old */ | |
63 | # if __i386__ | |
64 | # define __NR_signalfd 321 | |
65 | # elif __x86_64__ | |
66 | # define __NR_signalfd 282 | |
bfa38025 MH |
67 | # elif __powerpc__ |
68 | # define __NR_signalfd 305 | |
47f38330 SH |
69 | # elif __s390x__ |
70 | # define __NR_signalfd 316 | |
8ca61733 MJ |
71 | # endif |
72 | #endif | |
73 | ||
74 | int signalfd(int fd, const sigset_t *mask, int flags) | |
75 | { | |
76 | int retval; | |
77 | ||
78 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
79 | if (errno == ENOSYS && flags == 0) | |
80 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
81 | return retval; | |
82 | } | |
ff218c25 | 83 | #endif |
0ad19a3f | 84 | |
656994bb MH |
85 | #if !HAVE_DECL_PR_CAPBSET_DROP |
86 | #define PR_CAPBSET_DROP 24 | |
87 | #endif | |
88 | ||
e2bcd7db | 89 | #include "error.h" |
b0a33c1e | 90 | #include "af_unix.h" |
91 | #include "mainloop.h" | |
e2bcd7db | 92 | |
b113348e | 93 | #include <lxc/lxc.h> |
36eb9bde CLG |
94 | #include <lxc/log.h> |
95 | ||
96 | lxc_log_define(lxc_start, lxc); | |
97 | ||
0ad19a3f | 98 | |
99 | LXC_TTY_HANDLER(SIGINT); | |
100 | LXC_TTY_HANDLER(SIGQUIT); | |
101 | ||
b0a33c1e | 102 | static int setup_sigchld_fd(sigset_t *oldmask) |
103 | { | |
104 | sigset_t mask; | |
105 | int fd; | |
106 | ||
107 | if (sigprocmask(SIG_BLOCK, NULL, &mask)) { | |
36eb9bde | 108 | SYSERROR("failed to get mask signal"); |
b0a33c1e | 109 | return -1; |
110 | } | |
111 | ||
112 | if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
36eb9bde | 113 | SYSERROR("failed to set mask signal"); |
b0a33c1e | 114 | return -1; |
115 | } | |
116 | ||
117 | fd = signalfd(-1, &mask, 0); | |
118 | if (fd < 0) { | |
36eb9bde | 119 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 120 | return -1; |
121 | } | |
122 | ||
123 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 124 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 125 | close(fd); |
126 | return -1; | |
127 | } | |
128 | ||
129 | return fd; | |
130 | } | |
131 | ||
132 | static int setup_tty_service(const char *name, int *ttyfd) | |
133 | { | |
134 | int fd; | |
135 | struct sockaddr_un addr = { 0 }; | |
136 | char *offset = &addr.sun_path[1]; | |
137 | ||
138 | strcpy(offset, name); | |
139 | addr.sun_path[0] = '\0'; | |
140 | ||
141 | fd = lxc_af_unix_open(addr.sun_path, SOCK_STREAM, 0); | |
142 | if (fd < 0) | |
143 | return -1; | |
144 | ||
145 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 146 | SYSERROR("failed to close-on-exec flag"); |
b0a33c1e | 147 | close(fd); |
148 | return -1; | |
149 | } | |
150 | ||
151 | *ttyfd = fd; | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | static int sigchld_handler(int fd, void *data, | |
157 | struct lxc_epoll_descr *descr) | |
158 | { | |
159 | pid_t *pid = data; | |
160 | ||
161 | waitpid(*pid, NULL, 0); | |
162 | ||
163 | return 1; | |
164 | } | |
165 | ||
166 | static int ttyclient_handler(int fd, void *data, | |
167 | struct lxc_epoll_descr *descr) | |
168 | { | |
169 | int i; | |
170 | struct lxc_tty_info *tty_info = data; | |
171 | ||
172 | for (i = 0; i < tty_info->nbtty; i++) { | |
173 | ||
174 | if (tty_info->pty_info[i].busy != fd) | |
175 | continue; | |
176 | ||
177 | lxc_mainloop_del_handler(descr, fd); | |
178 | tty_info->pty_info[i].busy = 0; | |
179 | close(fd); | |
180 | } | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | static int ttyservice_handler(int fd, void *data, | |
186 | struct lxc_epoll_descr *descr) | |
187 | { | |
188 | int conn, ttynum, val = 1, ret = -1; | |
189 | struct lxc_tty_info *tty_info = data; | |
190 | ||
191 | conn = accept(fd, NULL, 0); | |
192 | if (conn < 0) { | |
36eb9bde | 193 | SYSERROR("failed to accept tty client"); |
b0a33c1e | 194 | return -1; |
195 | } | |
196 | ||
197 | if (setsockopt(conn, SOL_SOCKET, SO_PASSCRED, &val, sizeof(val))) { | |
36eb9bde | 198 | SYSERROR("failed to enable credential on socket"); |
b0a33c1e | 199 | goto out_close; |
200 | } | |
201 | ||
202 | if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum))) | |
203 | goto out_close; | |
204 | ||
205 | if (ttynum <= 0 || ttynum > tty_info->nbtty) | |
206 | goto out_close; | |
207 | ||
208 | /* fixup index array (eg. tty1 is index 0) */ | |
209 | ttynum--; | |
210 | ||
211 | if (tty_info->pty_info[ttynum].busy) | |
212 | goto out_close; | |
213 | ||
214 | if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum].master, | |
215 | NULL, 0) < 0) { | |
36eb9bde | 216 | ERROR("failed to send tty to client"); |
b0a33c1e | 217 | goto out_close; |
218 | } | |
219 | ||
220 | if (lxc_mainloop_add_handler(descr, conn, | |
221 | ttyclient_handler, tty_info)) { | |
36eb9bde | 222 | ERROR("failed to add tty client handler"); |
b0a33c1e | 223 | goto out_close; |
224 | } | |
225 | ||
226 | tty_info->pty_info[ttynum].busy = conn; | |
227 | ||
228 | ret = 0; | |
229 | ||
230 | out: | |
231 | return ret; | |
232 | out_close: | |
233 | close(conn); | |
234 | goto out; | |
235 | } | |
236 | ||
237 | static int mainloop(const char *name, pid_t pid, int sigfd, | |
238 | const struct lxc_tty_info *tty_info) | |
239 | { | |
240 | int nfds, ttyfd = -1, ret = -1; | |
241 | struct lxc_epoll_descr descr; | |
242 | ||
243 | if (tty_info->nbtty && setup_tty_service(name, &ttyfd)) { | |
36eb9bde | 244 | ERROR("failed to create the tty service point"); |
b0a33c1e | 245 | goto out_sigfd; |
246 | } | |
247 | ||
248 | /* sigfd + nb tty + tty service | |
249 | * if tty is enabled */ | |
250 | nfds = tty_info->nbtty + 1 + tty_info->nbtty ? 1 : 0; | |
251 | ||
252 | if (lxc_mainloop_open(nfds, &descr)) { | |
36eb9bde | 253 | ERROR("failed to create mainloop"); |
b0a33c1e | 254 | goto out_ttyfd; |
255 | } | |
256 | ||
257 | if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) { | |
36eb9bde | 258 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 259 | goto out_mainloop_open; |
260 | } | |
261 | ||
262 | if (tty_info->nbtty) { | |
263 | if (lxc_mainloop_add_handler(&descr, ttyfd, | |
264 | ttyservice_handler, | |
265 | (void *)tty_info)) { | |
36eb9bde | 266 | ERROR("failed to add handler for the tty"); |
b0a33c1e | 267 | goto out_mainloop_open; |
268 | } | |
269 | } | |
270 | ||
271 | ret = lxc_mainloop(&descr); | |
272 | ||
273 | out: | |
274 | return ret; | |
275 | ||
276 | out_mainloop_open: | |
277 | lxc_mainloop_close(&descr); | |
278 | out_ttyfd: | |
279 | close(ttyfd); | |
280 | out_sigfd: | |
281 | close(sigfd); | |
282 | goto out; | |
283 | } | |
284 | ||
05f05512 | 285 | int lxc_start(const char *name, char *argv[]) |
0ad19a3f | 286 | { |
b0a33c1e | 287 | struct lxc_tty_info tty_info = { 0 }; |
288 | sigset_t oldmask; | |
22ebac19 | 289 | char init[MAXPATHLEN]; |
939229eb | 290 | char tty[MAXPATHLEN]; |
22ebac19 | 291 | char *val = NULL; |
b0a33c1e | 292 | int fd, sigfd, lock, sv[2], sync = 0, err = -LXC_ERROR_INTERNAL; |
0ad19a3f | 293 | pid_t pid; |
294 | int clone_flags; | |
f4d507d5 | 295 | |
0ad19a3f | 296 | lock = lxc_get_lock(name); |
b0a33c1e | 297 | if (lock < 0) |
298 | return lock; | |
0ad19a3f | 299 | |
0ad19a3f | 300 | /* Begin the set the state to STARTING*/ |
301 | if (lxc_setstate(name, STARTING)) { | |
36eb9bde | 302 | ERROR("failed to set state '%s'", |
8b8b04f8 | 303 | lxc_state2str(STARTING)); |
0ad19a3f | 304 | goto out; |
305 | } | |
306 | ||
caf249f4 | 307 | /* If we are not attached to a tty, disable it */ |
308 | if (ttyname_r(0, tty, sizeof(tty))) | |
939229eb | 309 | tty[0] = '\0'; |
939229eb | 310 | |
b0a33c1e | 311 | if (lxc_create_tty(name, &tty_info)) { |
36eb9bde | 312 | ERROR("failed to create the ttys"); |
b0a33c1e | 313 | goto out; |
314 | } | |
315 | ||
316 | /* the signal fd has to be created before forking otherwise | |
317 | * if the child process exits before we setup the signal fd, | |
318 | * the event will be lost and the command will be stuck */ | |
319 | sigfd = setup_sigchld_fd(&oldmask); | |
320 | if (sigfd < 0) { | |
36eb9bde | 321 | ERROR("failed to set sigchild fd handler"); |
b0a33c1e | 322 | return -1; |
323 | } | |
324 | ||
0ad19a3f | 325 | /* Synchro socketpair */ |
326 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { | |
36eb9bde | 327 | SYSERROR("failed to create communication socketpair"); |
f4d507d5 | 328 | goto out; |
0ad19a3f | 329 | } |
330 | ||
331 | /* Avoid signals from terminal */ | |
332 | LXC_TTY_ADD_HANDLER(SIGINT); | |
333 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
334 | ||
f4d507d5 | 335 | clone_flags = CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
0ad19a3f | 336 | if (conf_has_utsname(name)) |
337 | clone_flags |= CLONE_NEWUTS; | |
338 | if (conf_has_network(name)) | |
339 | clone_flags |= CLONE_NEWNET; | |
340 | ||
341 | /* Create a process in a new set of namespaces */ | |
342 | pid = fork_ns(clone_flags); | |
343 | if (pid < 0) { | |
36eb9bde | 344 | SYSERROR("failed to fork into a new namespace"); |
0ad19a3f | 345 | goto err_fork_ns; |
346 | } | |
347 | ||
348 | if (!pid) { | |
349 | ||
b0a33c1e | 350 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL)) { |
36eb9bde | 351 | SYSERROR("failed to set sigprocmask"); |
b0a33c1e | 352 | return -1; |
353 | } | |
354 | ||
0ad19a3f | 355 | close(sv[1]); |
356 | ||
357 | /* Be sure we don't inherit this after the exec */ | |
358 | fcntl(sv[0], F_SETFD, FD_CLOEXEC); | |
359 | ||
360 | /* Tell our father he can begin to configure the container */ | |
361 | if (write(sv[0], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 362 | SYSERROR("failed to write socket"); |
57545890 | 363 | goto out_child; |
0ad19a3f | 364 | } |
365 | ||
366 | /* Wait for the father to finish the configuration */ | |
367 | if (read(sv[0], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 368 | SYSERROR("failed to read socket"); |
57545890 | 369 | goto out_child; |
0ad19a3f | 370 | } |
371 | ||
372 | /* Setup the container, ip, names, utsname, ... */ | |
b0a33c1e | 373 | err = lxc_setup(name, tty, &tty_info); |
e5bda9ee | 374 | if (err) { |
36eb9bde | 375 | ERROR("failed to setup the container"); |
e5bda9ee | 376 | if (write(sv[0], &err, sizeof(err)) < 0) |
36eb9bde | 377 | SYSERROR("failed to write the socket"); |
57545890 | 378 | goto out_child; |
0ad19a3f | 379 | } |
380 | ||
42ff343d | 381 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
36eb9bde | 382 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); |
42ff343d | 383 | goto out_child; |
384 | } | |
385 | ||
0ad19a3f | 386 | execvp(argv[0], argv); |
36eb9bde | 387 | SYSERROR("failed to exec %s", argv[0]); |
0ad19a3f | 388 | |
b3223262 | 389 | err = LXC_ERROR_WRONG_COMMAND; |
0ad19a3f | 390 | /* If the exec fails, tell that to our father */ |
e5bda9ee | 391 | if (write(sv[0], &err, sizeof(err)) < 0) |
36eb9bde | 392 | SYSERROR("failed to write the socket"); |
0ad19a3f | 393 | |
57545890 | 394 | out_child: |
e5bda9ee | 395 | exit(err); |
0ad19a3f | 396 | } |
397 | ||
398 | close(sv[0]); | |
399 | ||
400 | /* Wait for the child to be ready */ | |
401 | if (read(sv[1], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 402 | SYSERROR("failed to read the socket"); |
0ad19a3f | 403 | goto err_pipe_read; |
404 | } | |
405 | ||
218d4250 | 406 | if (lxc_link_nsgroup(name, pid)) |
36eb9bde | 407 | WARN("cgroupfs not found: cgroup disabled"); |
218d4250 | 408 | |
0ad19a3f | 409 | /* Create the network configuration */ |
410 | if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) { | |
36eb9bde | 411 | ERROR("failed to create the configured network"); |
0ad19a3f | 412 | goto err_create_network; |
413 | } | |
414 | ||
415 | /* Tell the child to continue its initialization */ | |
416 | if (write(sv[1], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 417 | SYSERROR("failed to write the socket"); |
0ad19a3f | 418 | goto err_pipe_write; |
419 | } | |
420 | ||
421 | /* Wait for the child to exec or returning an error */ | |
422 | err = read(sv[1], &sync, sizeof(sync)); | |
423 | if (err < 0) { | |
36eb9bde | 424 | ERROR("failed to read the socket"); |
0ad19a3f | 425 | goto err_pipe_read2; |
426 | } | |
427 | ||
428 | if (err > 0) { | |
e5bda9ee | 429 | err = sync; |
0ad19a3f | 430 | waitpid(pid, NULL, 0); |
431 | goto err_child_failed; | |
432 | } | |
433 | ||
3f21c114 | 434 | if (!asprintf(&val, "%d\n", pid)) { |
36eb9bde | 435 | SYSERROR("failed to allocate memory"); |
3f21c114 RT |
436 | goto err_child_failed; |
437 | } | |
22ebac19 | 438 | |
439 | snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name); | |
440 | ||
0ad19a3f | 441 | fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); |
442 | if (fd < 0) { | |
36eb9bde | 443 | SYSERROR("failed to open '%s'", init); |
0ad19a3f | 444 | goto err_write; |
445 | } | |
446 | ||
447 | if (write(fd, val, strlen(val)) < 0) { | |
36eb9bde | 448 | SYSERROR("failed to write the init pid"); |
0ad19a3f | 449 | goto err_write; |
450 | } | |
451 | ||
452 | close(fd); | |
453 | ||
0ad19a3f | 454 | if (lxc_setstate(name, RUNNING)) { |
36eb9bde | 455 | ERROR("failed to set state to %s", |
0ad19a3f | 456 | lxc_state2str(RUNNING)); |
457 | goto err_state_failed; | |
458 | } | |
459 | ||
b0a33c1e | 460 | if (mainloop(name, pid, sigfd, &tty_info)) { |
36eb9bde | 461 | ERROR("mainloop exited with an error"); |
b0a33c1e | 462 | goto err_mailoop_failed; |
0ad19a3f | 463 | } |
464 | ||
465 | if (lxc_setstate(name, STOPPING)) | |
36eb9bde | 466 | ERROR("failed to set state %s", lxc_state2str(STOPPING)); |
0ad19a3f | 467 | |
468 | if (clone_flags & CLONE_NEWNET && conf_destroy_network(name)) | |
36eb9bde | 469 | ERROR("failed to destroy the network"); |
0ad19a3f | 470 | |
471 | err = 0; | |
472 | out: | |
473 | if (lxc_setstate(name, STOPPED)) | |
36eb9bde | 474 | ERROR("failed to set state %s", lxc_state2str(STOPPED)); |
0ad19a3f | 475 | |
b0a33c1e | 476 | lxc_delete_tty(&tty_info); |
0ad19a3f | 477 | lxc_unlink_nsgroup(name); |
478 | unlink(init); | |
0ad19a3f | 479 | free(val); |
480 | lxc_put_lock(lock); | |
536b97f0 | 481 | LXC_TTY_DEL_HANDLER(SIGQUIT); |
482 | LXC_TTY_DEL_HANDLER(SIGINT); | |
0ad19a3f | 483 | |
484 | return err; | |
485 | ||
486 | err_write: | |
487 | close(fd); | |
488 | ||
489 | err_state_failed: | |
490 | err_child_failed: | |
491 | err_pipe_read2: | |
492 | err_pipe_write: | |
493 | if (clone_flags & CLONE_NEWNET) | |
494 | conf_destroy_network(name); | |
495 | err_create_network: | |
496 | err_pipe_read: | |
b0a33c1e | 497 | err_mailoop_failed: |
0ad19a3f | 498 | if (lxc_setstate(name, ABORTING)) |
36eb9bde | 499 | ERROR("failed to set state %s", lxc_state2str(STOPPED)); |
0ad19a3f | 500 | |
501 | kill(pid, SIGKILL); | |
502 | err_fork_ns: | |
0ad19a3f | 503 | close(sv[0]); |
504 | close(sv[1]); | |
0ad19a3f | 505 | goto out; |
506 | } |