]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cmd/lxc_init.c
362f5b495911c8a0475e63b2513620e0aa0e3b19
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>
43 #include "initutils.h"
45 #include "namespace.h"
47 #include "string_utils.h"
49 /* option keys for long only options */
50 #define OPT_USAGE 0x1000
51 #define OPT_VERSION (OPT_USAGE - 1)
53 #define QUOTE(macro) #macro
54 #define QUOTEVAL(macro) QUOTE(macro)
56 lxc_log_define(lxc_init
, lxc
);
58 static sig_atomic_t was_interrupted
;
60 static void interrupt_handler(int sig
)
63 was_interrupted
= sig
;
66 static struct option long_options
[] = {
67 { "name", required_argument
, 0, 'n' },
68 { "help", no_argument
, 0, 'h' },
69 { "usage", no_argument
, 0, OPT_USAGE
},
70 { "version", no_argument
, 0, OPT_VERSION
},
71 { "quiet", no_argument
, 0, 'q' },
72 { "logfile", required_argument
, 0, 'o' },
73 { "logpriority", required_argument
, 0, 'l' },
74 { "lxcpath", required_argument
, 0, 'P' },
77 static const char short_options
[] = "n:hqo:l:P:";
80 const struct option
*options
;
81 const char *shortopts
;
89 /* remaining arguments */
94 static int arguments_parse(struct arguments
*my_args
, int argc
,
97 static struct arguments my_args
= {
98 .options
= long_options
,
99 .shortopts
= short_options
102 static void prevent_forking(void)
107 char path
[MAXPATHLEN
];
109 f
= fopen("/proc/self/cgroup", "r");
113 while (getline(&line
, &len
, f
) != -1) {
117 p
= strchr(line
, ':');
126 /* This is a cgroup v2 entry. Skip it. */
130 if (strcmp(p
, "pids") != 0)
134 p2
+= lxc_char_left_gc(p2
, strlen(p2
));
135 p2
[lxc_char_right_gc(p2
, strlen(p2
))] = '\0';
137 ret
= snprintf(path
, sizeof(path
),
138 "/sys/fs/cgroup/pids/%s/pids.max", p2
);
139 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
140 ERROR("Failed to create string");
144 fd
= open(path
, O_WRONLY
);
146 SYSERROR("Failed to open \"%s\"", path
);
150 ret
= write(fd
, "1", 1);
152 SYSERROR("Failed to write to \"%s\"", path
);
163 static void kill_children(pid_t pid
)
169 ret
= snprintf(path
, sizeof(path
), "/proc/%d/task/%d/children", pid
, pid
);
170 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
171 ERROR("Failed to create string");
175 f
= fopen(path
, "r");
177 SYSERROR("Failed to open %s", path
);
184 if (fscanf(f
, "%d ", &pid
) != 1) {
185 ERROR("Failed to retrieve pid");
197 static void remove_self(void)
201 char path
[MAXPATHLEN
] = {0};
203 n
= readlink("/proc/self/exe", path
, sizeof(path
));
204 if (n
< 0 || n
>= MAXPATHLEN
) {
205 SYSDEBUG("Failed to readlink \"/proc/self/exe\"");
210 ret
= umount2(path
, MNT_DETACH
);
212 SYSDEBUG("Failed to unmount \"%s\"", path
);
218 SYSDEBUG("Failed to unlink \"%s\"", path
);
223 int main(int argc
, char *argv
[])
227 struct sigaction act
;
229 sigset_t mask
, omask
;
230 int have_status
= 0, exit_with
= 1, shutdown
= 0;
232 if (arguments_parse(&my_args
, argc
, argv
))
235 log
.prefix
= "lxc-init";
236 log
.name
= my_args
.name
;
237 log
.file
= my_args
.log_file
;
238 log
.level
= my_args
.log_priority
;
239 log
.quiet
= my_args
.quiet
;
240 log
.lxcpath
= my_args
.lxcpath
;
242 ret
= lxc_log_init(&log
);
245 lxc_log_options_no_override();
248 ERROR("Please specify a command to execute");
252 /* Mask all the signals so we are safe to install a signal handler and
255 ret
= sigfillset(&mask
);
259 ret
= sigdelset(&mask
, SIGILL
);
263 ret
= sigdelset(&mask
, SIGSEGV
);
267 ret
= sigdelset(&mask
, SIGBUS
);
271 ret
= pthread_sigmask(SIG_SETMASK
, &mask
, &omask
);
275 ret
= sigfillset(&act
.sa_mask
);
279 ret
= sigdelset(&act
.sa_mask
, SIGILL
);
283 ret
= sigdelset(&act
.sa_mask
, SIGSEGV
);
287 ret
= sigdelset(&act
.sa_mask
, SIGBUS
);
291 ret
= sigdelset(&act
.sa_mask
, SIGSTOP
);
295 ret
= sigdelset(&act
.sa_mask
, SIGKILL
);
300 act
.sa_handler
= interrupt_handler
;
302 for (i
= 1; i
< NSIG
; i
++) {
303 /* Exclude some signals: ILL, SEGV and BUS are likely to reveal
304 * a bug and we want a core. STOP and KILL cannot be handled
305 * anyway: they're here for documentation. 32 and 33 are not
308 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
309 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
312 ret
= sigaction(i
, &act
, NULL
);
317 SYSERROR("Failed to change signal action");
329 /* restore default signal handlers */
330 for (i
= 1; i
< NSIG
; i
++) {
333 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
334 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
337 sigerr
= signal(i
, SIG_DFL
);
338 if (sigerr
== SIG_ERR
) {
339 SYSDEBUG("Failed to reset to default action "
340 "for signal \"%d\": %d", i
, pid
);
344 ret
= pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
346 SYSERROR("Failed to set signal mask");
352 DEBUG("Failed to make child session leader");
354 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 0) < 0)
355 DEBUG("Failed to set controlling terminal");
357 NOTICE("Exec'ing \"%s\"", my_args
.argv
[0]);
359 ret
= execvp(my_args
.argv
[0], my_args
.argv
);
360 SYSERROR("Failed to exec \"%s\"", my_args
.argv
[0]);
364 INFO("Attempting to set proc title to \"init\"");
365 setproctitle("init");
367 /* Let's process the signals now. */
368 ret
= sigdelset(&omask
, SIGALRM
);
372 ret
= pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
374 SYSERROR("Failed to set signal mask");
378 /* No need of other inherited fds but stderr. */
380 close(STDOUT_FILENO
);
386 switch (was_interrupted
) {
388 /* Some applications send SIGHUP in order to get init to reload
389 * its configuration. We don't want to forward this onto the
390 * application itself, because it probably isn't expecting this
391 * signal since it was expecting init to do something with it.
393 * Instead, let's explicitly ignore it here. The actual
394 * terminal case is handled in the monitor's handler, which
395 * sends this task a SIGTERM in the case of a SIGHUP, which is
403 pid_t mypid
= lxc_raw_getpid();
408 kill_children(mypid
);
410 ret
= kill(-1, SIGTERM
);
412 SYSDEBUG("Failed to send SIGTERM to all children");
418 pid_t mypid
= lxc_raw_getpid();
422 kill_children(mypid
);
424 ret
= kill(-1, SIGKILL
);
426 SYSDEBUG("Failed to send SIGTERM to all children");
431 ret
= kill(pid
, was_interrupted
);
433 SYSDEBUG("Failed to send signal \"%d\" to %d", was_interrupted
, pid
);
439 waited_pid
= wait(&status
);
440 if (waited_pid
< 0) {
447 SYSERROR("Failed to wait on child %d", pid
);
451 /* Reset timer each time a process exited. */
455 /* Keep the exit code of the started application (not wrapped
456 * pid) and continue to wait for the end of the orphan group.
458 if (waited_pid
== pid
&& !have_status
) {
459 exit_with
= lxc_error_set_and_log(waited_pid
, status
);
469 static void print_usage(const struct option longopts
[])
472 fprintf(stderr
, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
473 [-q|--quiet] [-o|--logfile=LOGFILE] [-l|--logpriority=LOGPRIORITY] [-P|--lxcpath=LXCPATH]\n");
477 static void print_version(void)
479 printf("%s\n", LXC_VERSION
);
483 static void print_help(void)
486 Usage: lxc-init --name=NAME -- COMMAND\n\
488 lxc-init start a COMMAND as PID 2 inside a container\n\
491 -n, --name=NAME NAME of the container\n\
492 -o, --logfile=FILE Output log to FILE instead of stderr\n\
493 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
494 -q, --quiet Don't produce any output\n\
495 -P, --lxcpath=PATH Use specified container path\n\
496 -?, --help Give this help list\n\
497 --usage Give a short usage message\n\
498 --version Print the version number\n\
500 Mandatory or optional arguments to long options are also mandatory or optional\n\
501 for any corresponding short options.\n\
503 See the lxc-init man page for further information.\n\n");
506 static int arguments_parse(struct arguments
*args
, int argc
,
513 c
= getopt_long(argc
, argv
, args
->shortopts
, args
->options
, &index
);
521 args
->log_file
= optarg
;
524 args
->log_priority
= optarg
;
530 remove_trailing_slashes(optarg
);
531 args
->lxcpath
= optarg
;
534 print_usage(args
->options
);
547 * Reclaim the remaining command arguments
549 args
->argv
= &argv
[optind
];
550 args
->argc
= argc
- optind
;
552 /* If no lxcpath was given, use default */
554 args
->lxcpath
= lxc_global_config_value("lxc.lxcpath");
556 /* Check the command options */
559 fprintf(stderr
, "lxc-init: missing container name, use --name option\n");