]>
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
34 #include <sys/types.h>
38 #include <lxc/lxccontainer.h>
39 #include <lxc/version.h>
42 #include "initutils.h"
44 #include "namespace.h"
47 /* option keys for long only options */
48 #define OPT_USAGE 0x1000
49 #define OPT_VERSION OPT_USAGE - 1
51 #define QUOTE(macro) #macro
52 #define QUOTEVAL(macro) QUOTE(macro)
54 lxc_log_define(lxc_init
, lxc
);
56 static sig_atomic_t was_interrupted
= 0;
58 static void interrupt_handler(int sig
)
61 was_interrupted
= sig
;
64 static struct option long_options
[] = {
65 { "name", required_argument
, 0, 'n' },
66 { "help", no_argument
, 0, 'h' },
67 { "usage", no_argument
, 0, OPT_USAGE
},
68 { "version", no_argument
, 0, OPT_VERSION
},
69 { "quiet", no_argument
, 0, 'q' },
70 { "logfile", required_argument
, 0, 'o' },
71 { "logpriority", required_argument
, 0, 'l' },
72 { "lxcpath", required_argument
, 0, 'P' },
75 static char short_options
[] = "n:hqo:l:P:";
78 const struct option
*options
;
79 const char *shortopts
;
87 /* remaining arguments */
92 static int arguments_parse(struct arguments
*my_args
, int argc
,
95 static struct arguments my_args
= {
96 .options
= long_options
,
97 .shortopts
= short_options
100 static void prevent_forking(void)
105 char path
[MAXPATHLEN
];
107 f
= fopen("/proc/self/cgroup", "r");
111 while (getline(&line
, &len
, f
) != -1) {
115 p
= strchr(line
, ':');
124 /* This is a cgroup v2 entry. Skip it. */
128 if (strcmp(p
, "pids") != 0)
132 p2
+= lxc_char_left_gc(p2
, strlen(p2
));
133 p2
[lxc_char_right_gc(p2
, strlen(p2
))] = '\0';
135 ret
= snprintf(path
, sizeof(path
),
136 "/sys/fs/cgroup/pids/%s/pids.max", p2
);
137 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
138 ERROR("Failed to create string");
142 fd
= open(path
, O_WRONLY
);
144 SYSERROR("Failed to open \"%s\"", path
);
148 ret
= write(fd
, "1", 1);
150 SYSERROR("Failed to write to \"%s\"", path
);
161 static void kill_children(pid_t pid
)
167 ret
= snprintf(path
, sizeof(path
), "/proc/%d/task/%d/children", pid
, pid
);
168 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
169 ERROR("Failed to create string");
173 f
= fopen(path
, "r");
175 SYSERROR("Failed to open %s", path
);
182 if (fscanf(f
, "%d ", &pid
) != 1) {
183 ERROR("Failed to retrieve pid");
195 static void remove_self(void)
199 char path
[MAXPATHLEN
] = {0};
201 n
= readlink("/proc/self/exe", path
, sizeof(path
));
202 if (n
< 0 || n
>= MAXPATHLEN
) {
203 SYSERROR("Failed to readlink \"/proc/self/exe\"");
208 ret
= umount2(path
, MNT_DETACH
);
210 SYSERROR("Failed to unmount \"%s\"", path
);
216 SYSERROR("Failed to unlink \"%s\"", path
);
221 int main(int argc
, char *argv
[])
225 struct sigaction act
;
227 sigset_t mask
, omask
;
228 int have_status
= 0, exit_with
= 1, shutdown
= 0;
230 if (arguments_parse(&my_args
, argc
, argv
))
233 log
.prefix
= "lxc-init";
234 log
.name
= my_args
.name
;
235 log
.file
= my_args
.log_file
;
236 log
.level
= my_args
.log_priority
;
237 log
.quiet
= my_args
.quiet
;
238 log
.lxcpath
= my_args
.lxcpath
;
240 ret
= lxc_log_init(&log
);
243 lxc_log_options_no_override();
246 ERROR("Please specify a command to execute");
250 /* Mask all the signals so we are safe to install a signal handler and
253 ret
= sigfillset(&mask
);
257 ret
= sigdelset(&mask
, SIGILL
);
261 ret
= sigdelset(&mask
, SIGSEGV
);
265 ret
= sigdelset(&mask
, SIGBUS
);
269 ret
= sigprocmask(SIG_SETMASK
, &mask
, &omask
);
273 ret
= sigfillset(&act
.sa_mask
);
277 ret
= sigdelset(&act
.sa_mask
, SIGILL
);
281 ret
= sigdelset(&act
.sa_mask
, SIGSEGV
);
285 ret
= sigdelset(&act
.sa_mask
, SIGBUS
);
289 ret
= sigdelset(&act
.sa_mask
, SIGSTOP
);
293 ret
= sigdelset(&act
.sa_mask
, SIGKILL
);
298 act
.sa_handler
= interrupt_handler
;
300 for (i
= 1; i
< NSIG
; i
++) {
301 /* Exclude some signals: ILL, SEGV and BUS are likely to reveal
302 * a bug and we want a core. STOP and KILL cannot be handled
303 * anyway: they're here for documentation. 32 and 33 are not
306 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
307 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
310 ret
= sigaction(i
, &act
, NULL
);
315 SYSERROR("Failed to change signal action");
327 /* restore default signal handlers */
328 for (i
= 1; i
< NSIG
; i
++) {
330 sigerr
= signal(i
, SIG_DFL
);
331 if (sigerr
== SIG_ERR
) {
332 DEBUG("%s - Failed to reset to default action "
333 "for signal \"%d\": %d", strerror(errno
),
338 ret
= sigprocmask(SIG_SETMASK
, &omask
, NULL
);
340 SYSERROR("Failed to set signal mask");
346 DEBUG("Failed to make child session leader");
348 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 0) < 0)
349 DEBUG("Failed to set controlling terminal");
351 NOTICE("Exec'ing \"%s\"", my_args
.argv
[0]);
353 ret
= execvp(my_args
.argv
[0], my_args
.argv
);
354 ERROR("%s - Failed to exec \"%s\"", strerror(errno
), my_args
.argv
[0]);
358 INFO("Attempting to set proc title to \"init\"");
359 setproctitle("init");
361 /* Let's process the signals now. */
362 ret
= sigdelset(&omask
, SIGALRM
);
366 ret
= sigprocmask(SIG_SETMASK
, &omask
, NULL
);
368 SYSERROR("Failed to set signal mask");
372 /* No need of other inherited fds but stderr. */
374 close(STDOUT_FILENO
);
380 switch (was_interrupted
) {
382 /* Some applications send SIGHUP in order to get init to reload
383 * its configuration. We don't want to forward this onto the
384 * application itself, because it probably isn't expecting this
385 * signal since it was expecting init to do something with it.
387 * Instead, let's explicitly ignore it here. The actual
388 * terminal case is handled in the monitor's handler, which
389 * sends this task a SIGTERM in the case of a SIGHUP, which is
397 pid_t mypid
= lxc_raw_getpid();
402 kill_children(mypid
);
404 ret
= kill(-1, SIGTERM
);
406 DEBUG("%s - Failed to send SIGTERM to "
407 "all children", strerror(errno
));
413 pid_t mypid
= lxc_raw_getpid();
417 kill_children(mypid
);
419 ret
= kill(-1, SIGKILL
);
421 DEBUG("%s - Failed to send SIGTERM to "
422 "all children", strerror(errno
));
427 ret
= kill(pid
, was_interrupted
);
429 DEBUG("%s - Failed to send signal \"%d\" to "
430 "%d", strerror(errno
), was_interrupted
, pid
);
436 waited_pid
= wait(&status
);
437 if (waited_pid
< 0) {
444 ERROR("%s - Failed to wait on child %d",
445 strerror(errno
), pid
);
449 /* Reset timer each time a process exited. */
453 /* Keep the exit code of the started application (not wrapped
454 * pid) and continue to wait for the end of the orphan group.
456 if (waited_pid
== pid
&& !have_status
) {
457 exit_with
= lxc_error_set_and_log(waited_pid
, status
);
467 static void print_usage(const struct option longopts
[])
470 fprintf(stderr
, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version] \n\
471 [-q|--quiet] [-o|--logfile=LOGFILE] [-l|--logpriority=LOGPRIORITY] [-P|--lxcpath=LXCPATH]\n");
475 static void print_version()
477 printf("%s\n", LXC_VERSION
);
481 static void print_help()
484 Usage: lxc-init --name=NAME -- COMMAND\n\
486 lxc-init start a COMMAND as PID 2 inside a container\n\
489 -n, --name=NAME NAME of the container\n\
490 -o, --logfile=FILE Output log to FILE instead of stderr\n\
491 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
492 -q, --quiet Don't produce any output\n\
493 -P, --lxcpath=PATH Use specified container path\n\
494 -?, --help Give this help list\n\
495 --usage Give a short usage message\n\
496 --version Print the version number\n\
498 Mandatory or optional arguments to long options are also mandatory or optional\n\
499 for any corresponding short options.\n\
501 See the lxc-init man page for further information.\n\n");
505 static int arguments_parse(struct arguments
*args
, int argc
,
512 c
= getopt_long(argc
, argv
, args
->shortopts
, args
->options
, &index
);
520 args
->log_file
= optarg
;
523 args
->log_priority
= optarg
;
529 remove_trailing_slashes(optarg
);
530 args
->lxcpath
= optarg
;
533 print_usage(args
->options
);
546 * Reclaim the remaining command arguments
548 args
->argv
= &argv
[optind
];
549 args
->argc
= argc
- optind
;
551 /* If no lxcpath was given, use default */
552 if (!args
->lxcpath
) {
553 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");