]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/execute.c
Merge pull request #3059 from brauner/2019-06-21/seccomp_notify
[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 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include "conf.h"
35 #include "config.h"
36 #include "log.h"
37 #include "start.h"
38 #include "raw_syscalls.h"
39 #include "utils.h"
40
41 lxc_log_define(execute, start);
42
43 static int execute_start(struct lxc_handler *handler, void* data)
44 {
45 int argc_add, j;
46 char **argv;
47 int argc = 0, i = 0;
48 struct execute_args *my_args = data;
49
50 while (my_args->argv[argc++]);
51
52 /* lxc-init -n name -- [argc] NULL -> 5 */
53 argc_add = 5;
54 if (my_args->quiet)
55 argc_add++;
56
57 if (!handler->conf->rootfs.path)
58 argc_add += 2;
59
60 argv = malloc((argc + argc_add) * sizeof(*argv));
61 if (!argv) {
62 SYSERROR("Allocating init args failed");
63 goto out1;
64 }
65
66 if (my_args->init_path)
67 argv[i++] = my_args->init_path;
68 else
69 argv[i++] = "lxc-init";
70
71 argv[i++] = "-n";
72 argv[i++] = (char *)handler->name;
73
74 if (my_args->quiet)
75 argv[i++] = "--quiet";
76
77 if (!handler->conf->rootfs.path) {
78 argv[i++] = "-P";
79 argv[i++] = (char *)handler->lxcpath;
80 }
81
82 argv[i++] = "--";
83 for (j = 0; j < argc; j++)
84 argv[i++] = my_args->argv[j];
85 argv[i++] = NULL;
86
87 NOTICE("Exec'ing \"%s\"", my_args->argv[0]);
88
89 if (my_args->init_fd >= 0)
90 lxc_raw_execveat(my_args->init_fd, "", argv, environ, AT_EMPTY_PATH);
91 else
92 execvp(argv[0], argv);
93 SYSERROR("Failed to exec %s", argv[0]);
94
95 free(argv);
96 out1:
97 return 1;
98 }
99
100 static int execute_post_start(struct lxc_handler *handler, void* data)
101 {
102 struct execute_args *my_args = data;
103 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
104 return 0;
105 }
106
107 static struct lxc_operations execute_start_ops = {
108 .start = execute_start,
109 .post_start = execute_post_start
110 };
111
112 int lxc_execute(const char *name, char *const argv[], int quiet,
113 struct lxc_handler *handler, const char *lxcpath,
114 bool daemonize, int *error_num)
115 {
116 struct execute_args args = {.argv = argv, .quiet = quiet};
117
118 TRACE("Doing lxc_execute");
119 handler->conf->is_execute = true;
120 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
121 daemonize, error_num);
122 }