]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/execute.c
tree-wide: s/strncpy()/strlcpy()/g
[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(lxc_execute, lxc_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 ERROR("Init path missing");
71 goto out2;
72 }
73
74 argv[i++] = my_args->init_path;
75
76 argv[i++] = "-n";
77 argv[i++] = (char *)handler->name;
78
79 if (lxc_log_has_valid_level()) {
80 argv[i++] = "-l";
81 argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
82 }
83
84 if (current_config->logfd != -1 || lxc_log_fd != -1) {
85 int ret;
86 int to_dup = current_config->logfd;
87
88 if (current_config->logfd == -1)
89 to_dup = lxc_log_fd;
90
91 logfd = dup(to_dup);
92 if (logfd < 0) {
93 SYSERROR("Failed to duplicate log file descriptor");
94 goto out2;
95 }
96
97 ret = snprintf(logfile, sizeof(logfile), "/proc/1/fd/%d", logfd);
98 if (ret < 0 || (size_t)ret >= sizeof(logfile))
99 goto out3;
100
101 argv[i++] = "-o";
102 argv[i++] = logfile;
103 }
104
105 if (my_args->quiet)
106 argv[i++] = "--quiet";
107
108 if (!handler->conf->rootfs.path) {
109 argv[i++] = "-P";
110 argv[i++] = (char *)handler->lxcpath;
111 }
112
113 argv[i++] = "--";
114 for (j = 0; j < argc; j++)
115 argv[i++] = my_args->argv[j];
116 argv[i++] = NULL;
117
118 NOTICE("Exec'ing \"%s\"", my_args->argv[0]);
119
120 execvp(argv[0], argv);
121 SYSERROR("Failed to exec %s", argv[0]);
122
123 out3:
124 close(logfd);
125 out2:
126 free(argv);
127 out1:
128 return 1;
129 }
130
131 static int execute_post_start(struct lxc_handler *handler, void* data)
132 {
133 struct execute_args *my_args = data;
134 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
135 return 0;
136 }
137
138 static struct lxc_operations execute_start_ops = {
139 .start = execute_start,
140 .post_start = execute_post_start
141 };
142
143 int lxc_execute(const char *name, char *const argv[], int quiet,
144 struct lxc_handler *handler, const char *lxcpath,
145 bool backgrounded, int *error_num)
146 {
147 struct execute_args args = {.argv = argv, .quiet = quiet};
148
149 TRACE("Doing lxc_execute");
150 handler->conf->is_execute = 1;
151 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
152 backgrounded, error_num);
153 }