]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cmd/lxc_init.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/types.h>
39 #include <lxc/lxccontainer.h>
40 #include <lxc/version.h>
44 #include "initutils.h"
46 #include "namespace.h"
48 #include "string_utils.h"
50 /* option keys for long only options */
51 #define OPT_USAGE 0x1000
52 #define OPT_VERSION (OPT_USAGE - 1)
54 #define QUOTE(macro) #macro
55 #define QUOTEVAL(macro) QUOTE(macro)
57 lxc_log_define(lxc_init
, lxc
);
59 static sig_atomic_t was_interrupted
;
61 static void interrupt_handler(int sig
)
64 was_interrupted
= sig
;
67 static struct option long_options
[] = {
68 { "name", required_argument
, 0, 'n' },
69 { "help", no_argument
, 0, 'h' },
70 { "usage", no_argument
, 0, OPT_USAGE
},
71 { "version", no_argument
, 0, OPT_VERSION
},
72 { "quiet", no_argument
, 0, 'q' },
73 { "logfile", required_argument
, 0, 'o' },
74 { "logpriority", required_argument
, 0, 'l' },
75 { "lxcpath", required_argument
, 0, 'P' },
78 static const char short_options
[] = "n:hqo:l:P:";
81 const struct option
*options
;
82 const char *shortopts
;
90 /* remaining arguments */
95 static int arguments_parse(struct arguments
*my_args
, int argc
,
98 static struct arguments my_args
= {
99 .options
= long_options
,
100 .shortopts
= short_options
103 static void prevent_forking(void)
108 char path
[MAXPATHLEN
];
110 f
= fopen("/proc/self/cgroup", "r");
114 while (getline(&line
, &len
, f
) != -1) {
118 p
= strchr(line
, ':');
127 /* This is a cgroup v2 entry. Skip it. */
131 if (strcmp(p
, "pids") != 0)
135 p2
+= lxc_char_left_gc(p2
, strlen(p2
));
136 p2
[lxc_char_right_gc(p2
, strlen(p2
))] = '\0';
138 ret
= snprintf(path
, sizeof(path
),
139 "/sys/fs/cgroup/pids/%s/pids.max", p2
);
140 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
141 ERROR("Failed to create string");
145 fd
= open(path
, O_WRONLY
);
147 SYSERROR("Failed to open \"%s\"", path
);
151 ret
= write(fd
, "1", 1);
153 SYSERROR("Failed to write to \"%s\"", path
);
164 static void kill_children(pid_t pid
)
170 ret
= snprintf(path
, sizeof(path
), "/proc/%d/task/%d/children", pid
, pid
);
171 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
172 ERROR("Failed to create string");
176 f
= fopen(path
, "r");
178 SYSERROR("Failed to open %s", path
);
185 if (fscanf(f
, "%d ", &pid
) != 1) {
186 ERROR("Failed to retrieve pid");
198 static void remove_self(void)
202 char path
[MAXPATHLEN
] = {0};
204 n
= readlink("/proc/self/exe", path
, sizeof(path
));
205 if (n
< 0 || n
>= MAXPATHLEN
) {
206 SYSDEBUG("Failed to readlink \"/proc/self/exe\"");
211 ret
= umount2(path
, MNT_DETACH
);
213 SYSDEBUG("Failed to unmount \"%s\"", path
);
219 SYSDEBUG("Failed to unlink \"%s\"", path
);
224 int main(int argc
, char *argv
[])
228 struct sigaction act
;
230 sigset_t mask
, omask
;
231 int have_status
= 0, exit_with
= 1, shutdown
= 0;
233 if (arguments_parse(&my_args
, argc
, argv
))
236 log
.prefix
= "lxc-init";
237 log
.name
= my_args
.name
;
238 log
.file
= my_args
.log_file
;
239 log
.level
= my_args
.log_priority
;
240 log
.quiet
= my_args
.quiet
;
241 log
.lxcpath
= my_args
.lxcpath
;
243 ret
= lxc_log_init(&log
);
246 lxc_log_options_no_override();
249 ERROR("Please specify a command to execute");
253 /* Mask all the signals so we are safe to install a signal handler and
256 ret
= sigfillset(&mask
);
260 ret
= sigdelset(&mask
, SIGILL
);
264 ret
= sigdelset(&mask
, SIGSEGV
);
268 ret
= sigdelset(&mask
, SIGBUS
);
272 ret
= pthread_sigmask(SIG_SETMASK
, &mask
, &omask
);
276 ret
= sigfillset(&act
.sa_mask
);
280 ret
= sigdelset(&act
.sa_mask
, SIGILL
);
284 ret
= sigdelset(&act
.sa_mask
, SIGSEGV
);
288 ret
= sigdelset(&act
.sa_mask
, SIGBUS
);
292 ret
= sigdelset(&act
.sa_mask
, SIGSTOP
);
296 ret
= sigdelset(&act
.sa_mask
, SIGKILL
);
301 act
.sa_handler
= interrupt_handler
;
303 for (i
= 1; i
< NSIG
; i
++) {
304 /* Exclude some signals: ILL, SEGV and BUS are likely to reveal
305 * a bug and we want a core. STOP and KILL cannot be handled
306 * anyway: they're here for documentation. 32 and 33 are not
309 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
310 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
313 ret
= sigaction(i
, &act
, NULL
);
318 SYSERROR("Failed to change signal action");
330 /* restore default signal handlers */
331 for (i
= 1; i
< NSIG
; i
++) {
334 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
335 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
338 sigerr
= signal(i
, SIG_DFL
);
339 if (sigerr
== SIG_ERR
) {
340 SYSDEBUG("Failed to reset to default action "
341 "for signal \"%d\": %d", i
, pid
);
345 ret
= pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
347 SYSERROR("Failed to set signal mask");
353 DEBUG("Failed to make child session leader");
355 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 0) < 0)
356 DEBUG("Failed to set controlling terminal");
358 NOTICE("Exec'ing \"%s\"", my_args
.argv
[0]);
360 ret
= execvp(my_args
.argv
[0], my_args
.argv
);
361 SYSERROR("Failed to exec \"%s\"", my_args
.argv
[0]);
365 INFO("Attempting to set proc title to \"init\"");
366 setproctitle("init");
368 /* Let's process the signals now. */
369 ret
= sigdelset(&omask
, SIGALRM
);
373 ret
= pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
375 SYSERROR("Failed to set signal mask");
379 /* No need of other inherited fds but stderr. */
381 close(STDOUT_FILENO
);
387 switch (was_interrupted
) {
389 /* Some applications send SIGHUP in order to get init to reload
390 * its configuration. We don't want to forward this onto the
391 * application itself, because it probably isn't expecting this
392 * signal since it was expecting init to do something with it.
394 * Instead, let's explicitly ignore it here. The actual
395 * terminal case is handled in the monitor's handler, which
396 * sends this task a SIGTERM in the case of a SIGHUP, which is
404 pid_t mypid
= lxc_raw_getpid();
409 kill_children(mypid
);
411 ret
= kill(-1, SIGTERM
);
413 SYSDEBUG("Failed to send SIGTERM to all children");
419 pid_t mypid
= lxc_raw_getpid();
423 kill_children(mypid
);
425 ret
= kill(-1, SIGKILL
);
427 SYSDEBUG("Failed to send SIGTERM to all children");
432 ret
= kill(pid
, was_interrupted
);
434 SYSDEBUG("Failed to send signal \"%d\" to %d", was_interrupted
, pid
);
440 waited_pid
= wait(&status
);
441 if (waited_pid
< 0) {
448 SYSERROR("Failed to wait on child %d", pid
);
452 /* Reset timer each time a process exited. */
456 /* Keep the exit code of the started application (not wrapped
457 * pid) and continue to wait for the end of the orphan group.
459 if (waited_pid
== pid
&& !have_status
) {
460 exit_with
= lxc_error_set_and_log(waited_pid
, status
);
470 __noreturn__
static void print_usage_exit(const struct option longopts
[])
473 fprintf(stderr
, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
474 [-q|--quiet] [-o|--logfile=LOGFILE] [-l|--logpriority=LOGPRIORITY] [-P|--lxcpath=LXCPATH]\n");
478 __noreturn__
static void print_version_exit(void)
480 printf("%s\n", LXC_VERSION
);
484 static void print_help(void)
487 Usage: lxc-init --name=NAME -- COMMAND\n\
489 lxc-init start a COMMAND as PID 2 inside a container\n\
492 -n, --name=NAME NAME of the container\n\
493 -o, --logfile=FILE Output log to FILE instead of stderr\n\
494 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
495 -q, --quiet Don't produce any output\n\
496 -P, --lxcpath=PATH Use specified container path\n\
497 -?, --help Give this help list\n\
498 --usage Give a short usage message\n\
499 --version Print the version number\n\
501 Mandatory or optional arguments to long options are also mandatory or optional\n\
502 for any corresponding short options.\n\
504 See the lxc-init man page for further information.\n\n");
507 static int arguments_parse(struct arguments
*args
, int argc
,
514 c
= getopt_long(argc
, argv
, args
->shortopts
, args
->options
, &index
);
522 args
->log_file
= optarg
;
525 args
->log_priority
= optarg
;
531 remove_trailing_slashes(optarg
);
532 args
->lxcpath
= optarg
;
535 print_usage_exit(args
->options
);
537 print_version_exit();
548 * Reclaim the remaining command arguments
550 args
->argv
= &argv
[optind
];
551 args
->argc
= argc
- optind
;
553 /* If no lxcpath was given, use default */
555 args
->lxcpath
= lxc_global_config_value("lxc.lxcpath");
557 /* Check the command options */
560 fprintf(stderr
, "lxc-init: missing container name, use --name option\n");