]>
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
39 #include <sys/types.h>
43 #include <lxc/lxccontainer.h>
44 #include <lxc/version.h>
49 #include "initutils.h"
50 #include "memory_utils.h"
52 #include "raw_syscalls.h"
53 #include "string_utils.h"
55 /* option keys for long only options */
56 #define OPT_USAGE 0x1000
57 #define OPT_VERSION (OPT_USAGE - 1)
59 #define QUOTE(macro) #macro
60 #define QUOTEVAL(macro) QUOTE(macro)
62 static sig_atomic_t was_interrupted
;
64 static void interrupt_handler(int sig
)
67 was_interrupted
= sig
;
70 static struct option long_options
[] = {
71 { "name", required_argument
, 0, 'n' },
72 { "help", no_argument
, 0, 'h' },
73 { "usage", no_argument
, 0, OPT_USAGE
},
74 { "version", no_argument
, 0, OPT_VERSION
},
75 { "quiet", no_argument
, 0, 'q' },
76 { "lxcpath", required_argument
, 0, 'P' },
79 static const char short_options
[] = "n:hqo:l:P:";
82 const struct option
*options
;
83 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)
104 __do_free
char *line
= NULL
;
105 __do_fclose
FILE *f
= NULL
;
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
)) {
141 fprintf(stderr
, "Failed to create string\n");
145 fd
= open(path
, O_WRONLY
);
148 fprintf(stderr
, "Failed to open \"%s\"\n", path
);
152 ret
= write(fd
, "1", 1);
153 if (ret
!= 1 && !my_args
.quiet
)
154 fprintf(stderr
, "Failed to write to \"%s\"\n", path
);
161 static void kill_children(pid_t pid
)
163 __do_fclose
FILE *f
= NULL
;
167 ret
= snprintf(path
, sizeof(path
), "/proc/%d/task/%d/children", pid
, pid
);
168 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
170 fprintf(stderr
, "Failed to create string\n");
174 f
= fopen(path
, "r");
177 fprintf(stderr
, "Failed to open %s\n", path
);
184 if (fscanf(f
, "%d ", &find_pid
) != 1) {
186 fprintf(stderr
, "Failed to retrieve pid\n");
190 (void)kill_children(find_pid
);
191 (void)kill(find_pid
, SIGKILL
);
195 static void remove_self(void)
199 char path
[PATH_MAX
] = {0};
201 n
= readlink("/proc/self/exe", path
, sizeof(path
));
202 if (n
< 0 || n
>= PATH_MAX
)
206 ret
= umount2(path
, MNT_DETACH
);
215 int main(int argc
, char *argv
[])
219 struct sigaction act
;
220 sigset_t mask
, omask
;
221 int have_status
= 0, exit_with
= 1, shutdown
= 0;
223 if (arguments_parse(&my_args
, argc
, argv
))
228 fprintf(stderr
, "Please specify a command to execute\n");
232 /* Mask all the signals so we are safe to install a signal handler and
235 ret
= sigfillset(&mask
);
239 ret
= sigdelset(&mask
, SIGILL
);
243 ret
= sigdelset(&mask
, SIGSEGV
);
247 ret
= sigdelset(&mask
, SIGBUS
);
251 ret
= pthread_sigmask(SIG_SETMASK
, &mask
, &omask
);
255 ret
= sigfillset(&act
.sa_mask
);
259 ret
= sigdelset(&act
.sa_mask
, SIGILL
);
263 ret
= sigdelset(&act
.sa_mask
, SIGSEGV
);
267 ret
= sigdelset(&act
.sa_mask
, SIGBUS
);
271 ret
= sigdelset(&act
.sa_mask
, SIGSTOP
);
275 ret
= sigdelset(&act
.sa_mask
, SIGKILL
);
280 act
.sa_handler
= interrupt_handler
;
282 for (i
= 1; i
< NSIG
; i
++) {
283 /* Exclude some signals: ILL, SEGV and BUS are likely to reveal
284 * a bug and we want a core. STOP and KILL cannot be handled
285 * anyway: they're here for documentation. 32 and 33 are not
288 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
289 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
292 ret
= sigaction(i
, &act
, NULL
);
298 fprintf(stderr
, "Failed to change signal action\n");
310 /* restore default signal handlers */
311 for (i
= 1; i
< NSIG
; i
++) {
314 if (i
== SIGILL
|| i
== SIGSEGV
|| i
== SIGBUS
||
315 i
== SIGSTOP
|| i
== SIGKILL
|| i
== 32 || i
== 33)
318 sigerr
= signal(i
, SIG_DFL
);
319 if (sigerr
== SIG_ERR
&& !my_args
.quiet
)
320 fprintf(stderr
, "Failed to reset to default action for signal \"%d\": %d\n", i
, pid
);
323 ret
= pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
326 fprintf(stderr
, "Failed to set signal mask\n");
332 (void)ioctl(STDIN_FILENO
, TIOCSCTTY
, 0);
334 ret
= execvp(my_args
.argv
[0], my_args
.argv
);
336 fprintf(stderr
, "Failed to exec \"%s\"\n", my_args
.argv
[0]);
339 logfd
= open("/dev/console", O_WRONLY
| O_NOCTTY
| O_CLOEXEC
);
341 ret
= dup3(logfd
, STDERR_FILENO
, O_CLOEXEC
);
346 (void)setproctitle("init");
348 /* Let's process the signals now. */
349 ret
= sigdelset(&omask
, SIGALRM
);
353 ret
= pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
356 fprintf(stderr
, "Failed to set signal mask\n");
360 /* No need of other inherited fds but stderr. */
362 close(STDOUT_FILENO
);
368 switch (was_interrupted
) {
370 /* Some applications send SIGHUP in order to get init to reload
371 * its configuration. We don't want to forward this onto the
372 * application itself, because it probably isn't expecting this
373 * signal since it was expecting init to do something with it.
375 * Instead, let's explicitly ignore it here. The actual
376 * terminal case is handled in the monitor's handler, which
377 * sends this task a SIGTERM in the case of a SIGHUP, which is
385 pid_t mypid
= lxc_raw_getpid();
390 kill_children(mypid
);
392 ret
= kill(-1, SIGTERM
);
393 if (ret
< 0 && !my_args
.quiet
)
394 fprintf(stderr
, "Failed to send SIGTERM to all children\n");
400 pid_t mypid
= lxc_raw_getpid();
404 kill_children(mypid
);
406 ret
= kill(-1, SIGKILL
);
407 if (ret
< 0 && !my_args
.quiet
)
408 fprintf(stderr
, "Failed to send SIGTERM to all children\n");
413 ret
= kill(pid
, was_interrupted
);
419 waited_pid
= wait(&status
);
420 if (waited_pid
< 0) {
428 fprintf(stderr
, "Failed to wait on child %d\n", pid
);
433 /* Reset timer each time a process exited. */
437 /* Keep the exit code of the started application (not wrapped
438 * pid) and continue to wait for the end of the orphan group.
440 if (waited_pid
== pid
&& !have_status
) {
441 exit_with
= lxc_error_set_and_log(waited_pid
, status
);
451 __noreturn
static void print_usage_exit(const struct option longopts
[])
454 fprintf(stderr
, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
455 [-q|--quiet] [-P|--lxcpath=LXCPATH]\n");
459 __noreturn
static void print_version_exit(void)
461 printf("%s\n", LXC_VERSION
);
465 static void print_help(void)
468 Usage: lxc-init --name=NAME -- COMMAND\n\
470 lxc-init start a COMMAND as PID 2 inside a container\n\
473 -n, --name=NAME NAME of the container\n\
474 -q, --quiet Don't produce any output\n\
475 -P, --lxcpath=PATH Use specified container path\n\
476 -?, --help Give this help list\n\
477 --usage Give a short usage message\n\
478 --version Print the version number\n\
480 Mandatory or optional arguments to long options are also mandatory or optional\n\
481 for any corresponding short options.\n\
483 See the lxc-init man page for further information.\n\n");
486 static int arguments_parse(struct arguments
*args
, int argc
,
493 c
= getopt_long(argc
, argv
, args
->shortopts
, args
->options
, &index
);
508 remove_trailing_slashes(optarg
);
509 args
->lxcpath
= optarg
;
512 print_usage_exit(args
->options
);
514 print_version_exit();
525 * Reclaim the remaining command arguments
527 args
->argv
= &argv
[optind
];
528 args
->argc
= argc
- optind
;
530 /* If no lxcpath was given, use default */
532 args
->lxcpath
= lxc_global_config_value("lxc.lxcpath");
534 /* Check the command options */
537 fprintf(stderr
, "lxc-init: missing container name, use --name option\n");