]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0ae4f887 | 2 | |
d38dd64a CB |
3 | #ifndef _GNU_SOURCE |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
0ae4f887 | 6 | #include <errno.h> |
cd90db2c | 7 | #include <stdio.h> |
d38dd64a CB |
8 | #include <stdlib.h> |
9 | #include <sys/stat.h> | |
10 | #include <sys/types.h> | |
11 | #include <unistd.h> | |
0ae4f887 | 12 | |
e0b0b533 | 13 | #include "conf.h" |
d38dd64a | 14 | #include "config.h" |
0ae4f887 GK |
15 | #include "log.h" |
16 | #include "start.h" | |
f40988c7 | 17 | #include "process_utils.h" |
8afb3e61 | 18 | #include "utils.h" |
0ae4f887 | 19 | |
ac2cecc4 | 20 | lxc_log_define(execute, start); |
0ae4f887 | 21 | |
0ae4f887 GK |
22 | static int execute_start(struct lxc_handler *handler, void* data) |
23 | { | |
321614a5 CB |
24 | int argc_add, j; |
25 | char **argv; | |
b30b7eea | 26 | int argc = 0, i = 0; |
0ae4f887 | 27 | struct execute_args *my_args = data; |
0ae4f887 GK |
28 | |
29 | while (my_args->argv[argc++]); | |
30 | ||
858faf70 SH |
31 | /* lxc-init -n name -- [argc] NULL -> 5 */ |
32 | argc_add = 5; | |
e0b0b533 DE |
33 | if (my_args->quiet) |
34 | argc_add++; | |
321614a5 | 35 | |
858faf70 SH |
36 | if (!handler->conf->rootfs.path) |
37 | argc_add += 2; | |
321614a5 | 38 | |
e0b0b533 | 39 | argv = malloc((argc + argc_add) * sizeof(*argv)); |
b2efeb0b TA |
40 | if (!argv) { |
41 | SYSERROR("Allocating init args failed"); | |
e0b0b533 | 42 | goto out1; |
b2efeb0b | 43 | } |
0ae4f887 | 44 | |
4b5b3a2a TA |
45 | if (my_args->init_path) |
46 | argv[i++] = my_args->init_path; | |
47 | else | |
48 | argv[i++] = "lxc-init"; | |
a6f151a7 CB |
49 | |
50 | argv[i++] = "-n"; | |
51 | argv[i++] = (char *)handler->name; | |
52 | ||
0ae4f887 GK |
53 | if (my_args->quiet) |
54 | argv[i++] = "--quiet"; | |
a6f151a7 | 55 | |
e0b0b533 | 56 | if (!handler->conf->rootfs.path) { |
a6f151a7 | 57 | argv[i++] = "-P"; |
e0b0b533 | 58 | argv[i++] = (char *)handler->lxcpath; |
e0b0b533 | 59 | } |
a6f151a7 | 60 | |
0ae4f887 GK |
61 | argv[i++] = "--"; |
62 | for (j = 0; j < argc; j++) | |
63 | argv[i++] = my_args->argv[j]; | |
64 | argv[i++] = NULL; | |
65 | ||
a6f151a7 | 66 | NOTICE("Exec'ing \"%s\"", my_args->argv[0]); |
0ae4f887 | 67 | |
4b5b3a2a | 68 | if (my_args->init_fd >= 0) |
f40988c7 | 69 | execveat(my_args->init_fd, "", argv, environ, AT_EMPTY_PATH); |
4b5b3a2a TA |
70 | else |
71 | execvp(argv[0], argv); | |
a6f151a7 | 72 | SYSERROR("Failed to exec %s", argv[0]); |
794248d0 | 73 | |
e0b0b533 DE |
74 | free(argv); |
75 | out1: | |
0ae4f887 GK |
76 | return 1; |
77 | } | |
78 | ||
79 | static int execute_post_start(struct lxc_handler *handler, void* data) | |
80 | { | |
81 | struct execute_args *my_args = data; | |
82 | NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid); | |
83 | return 0; | |
84 | } | |
85 | ||
86 | static struct lxc_operations execute_start_ops = { | |
87 | .start = execute_start, | |
88 | .post_start = execute_post_start | |
89 | }; | |
90 | ||
91 | int lxc_execute(const char *name, char *const argv[], int quiet, | |
aa460476 | 92 | struct lxc_handler *handler, const char *lxcpath, |
bb955810 | 93 | bool daemonize, int *error_num) |
0ae4f887 | 94 | { |
aa460476 | 95 | struct execute_args args = {.argv = argv, .quiet = quiet}; |
0ae4f887 | 96 | |
b2efeb0b | 97 | TRACE("Doing lxc_execute"); |
07c4ea31 | 98 | handler->conf->is_execute = true; |
7bdf97d2 | 99 | return __lxc_start(handler, &execute_start_ops, &args, lxcpath, |
bb955810 | 100 | daemonize, error_num); |
0ae4f887 | 101 | } |