]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/execute.c
Merge pull request #2568 from brauner/2018-08-22/ifaddrs
[mirror_lxc.git] / src / lxc / execute.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #define _GNU_SOURCE
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "conf.h"
33 #include "log.h"
34 #include "start.h"
35 #include "utils.h"
36
37 lxc_log_define(execute, start);
38
39 static int execute_start(struct lxc_handler *handler, void* data)
40 {
41 int argc_add, j;
42 char **argv;
43 int argc = 0, i = 0, logfd = -1;
44 struct execute_args *my_args = data;
45 char logfile[LXC_PROC_PID_FD_LEN];
46
47 while (my_args->argv[argc++]);
48
49 /* lxc-init -n name -- [argc] NULL -> 5 */
50 argc_add = 5;
51 if (my_args->quiet)
52 argc_add++;
53
54 if (!handler->conf->rootfs.path)
55 argc_add += 2;
56
57 if (lxc_log_has_valid_level())
58 argc_add += 2;
59
60 if (current_config->logfd != -1 || lxc_log_fd != -1)
61 argc_add += 2;
62
63 argv = malloc((argc + argc_add) * sizeof(*argv));
64 if (!argv) {
65 SYSERROR("Allocating init args failed");
66 goto out1;
67 }
68
69 if (my_args->init_path)
70 argv[i++] = my_args->init_path;
71 else
72 argv[i++] = "lxc-init";
73
74 argv[i++] = "-n";
75 argv[i++] = (char *)handler->name;
76
77 if (lxc_log_has_valid_level()) {
78 argv[i++] = "-l";
79 argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
80 }
81
82 if (current_config->logfd != -1 || lxc_log_fd != -1) {
83 int ret;
84 int to_dup = current_config->logfd;
85
86 if (current_config->logfd == -1)
87 to_dup = lxc_log_fd;
88
89 logfd = dup(to_dup);
90 if (logfd < 0) {
91 SYSERROR("Failed to duplicate log file descriptor");
92 goto out2;
93 }
94
95 ret = snprintf(logfile, sizeof(logfile), "/proc/1/fd/%d", logfd);
96 if (ret < 0 || (size_t)ret >= sizeof(logfile))
97 goto out3;
98
99 argv[i++] = "-o";
100 argv[i++] = logfile;
101 }
102
103 if (my_args->quiet)
104 argv[i++] = "--quiet";
105
106 if (!handler->conf->rootfs.path) {
107 argv[i++] = "-P";
108 argv[i++] = (char *)handler->lxcpath;
109 }
110
111 argv[i++] = "--";
112 for (j = 0; j < argc; j++)
113 argv[i++] = my_args->argv[j];
114 argv[i++] = NULL;
115
116 NOTICE("Exec'ing \"%s\"", my_args->argv[0]);
117
118 if (my_args->init_fd >= 0)
119 #ifdef __NR_execveat
120 syscall(__NR_execveat, my_args->init_fd, "", argv, environ, AT_EMPTY_PATH);
121 #else
122 ERROR("System seems to be missing execveat syscall number");
123 #endif
124 else
125 execvp(argv[0], argv);
126 SYSERROR("Failed to exec %s", argv[0]);
127
128 out3:
129 close(logfd);
130 out2:
131 free(argv);
132 out1:
133 return 1;
134 }
135
136 static int execute_post_start(struct lxc_handler *handler, void* data)
137 {
138 struct execute_args *my_args = data;
139 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
140 return 0;
141 }
142
143 static struct lxc_operations execute_start_ops = {
144 .start = execute_start,
145 .post_start = execute_post_start
146 };
147
148 int lxc_execute(const char *name, char *const argv[], int quiet,
149 struct lxc_handler *handler, const char *lxcpath,
150 bool backgrounded, int *error_num)
151 {
152 struct execute_args args = {.argv = argv, .quiet = quiet};
153
154 TRACE("Doing lxc_execute");
155 handler->conf->is_execute = true;
156 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
157 backgrounded, error_num);
158 }