]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/execute.c
tree-wide: s/strncpy()/strlcpy()/g
[mirror_lxc.git] / src / lxc / execute.c
CommitLineData
0ae4f887
GK
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ae4f887
GK
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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ae4f887
GK
22 */
23
cd90db2c 24#define _GNU_SOURCE
a0a2066d
SH
25#include <sys/types.h>
26#include <sys/stat.h>
0ae4f887
GK
27#include <errno.h>
28#include <unistd.h>
29#include <stdlib.h>
cd90db2c 30#include <stdio.h>
0ae4f887 31
e0b0b533 32#include "conf.h"
0ae4f887
GK
33#include "log.h"
34#include "start.h"
8afb3e61 35#include "utils.h"
0ae4f887
GK
36
37lxc_log_define(lxc_execute, lxc_start);
38
0ae4f887
GK
39static int execute_start(struct lxc_handler *handler, void* data)
40{
321614a5
CB
41 int argc_add, j;
42 char **argv;
43 int argc = 0, i = 0, logfd = -1;
0ae4f887 44 struct execute_args *my_args = data;
321614a5 45 char logfile[LXC_PROC_PID_FD_LEN];
0ae4f887
GK
46
47 while (my_args->argv[argc++]);
48
858faf70
SH
49 /* lxc-init -n name -- [argc] NULL -> 5 */
50 argc_add = 5;
e0b0b533
DE
51 if (my_args->quiet)
52 argc_add++;
321614a5 53
858faf70
SH
54 if (!handler->conf->rootfs.path)
55 argc_add += 2;
321614a5 56
858faf70
SH
57 if (lxc_log_has_valid_level())
58 argc_add += 2;
e0b0b533 59
9c40b2d9
TA
60 if (current_config->logfd != -1 || lxc_log_fd != -1)
61 argc_add += 2;
62
e0b0b533 63 argv = malloc((argc + argc_add) * sizeof(*argv));
b2efeb0b
TA
64 if (!argv) {
65 SYSERROR("Allocating init args failed");
e0b0b533 66 goto out1;
b2efeb0b 67 }
0ae4f887 68
b2efeb0b
TA
69 if (!my_args->init_path) {
70 ERROR("Init path missing");
e0b0b533 71 goto out2;
b2efeb0b 72 }
794248d0
CB
73
74 argv[i++] = my_args->init_path;
a6f151a7
CB
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
cd90db2c 84 if (current_config->logfd != -1 || lxc_log_fd != -1) {
321614a5 85 int ret;
cd90db2c
TA
86 int to_dup = current_config->logfd;
87
88 if (current_config->logfd == -1)
89 to_dup = lxc_log_fd;
90
321614a5
CB
91 logfd = dup(to_dup);
92 if (logfd < 0) {
93 SYSERROR("Failed to duplicate log file descriptor");
cd90db2c
TA
94 goto out2;
95 }
96
321614a5
CB
97 ret = snprintf(logfile, sizeof(logfile), "/proc/1/fd/%d", logfd);
98 if (ret < 0 || (size_t)ret >= sizeof(logfile))
cd90db2c 99 goto out3;
cd90db2c 100
8f98ac7b 101 argv[i++] = "-o";
321614a5 102 argv[i++] = logfile;
8f98ac7b
CB
103 }
104
0ae4f887
GK
105 if (my_args->quiet)
106 argv[i++] = "--quiet";
a6f151a7 107
e0b0b533 108 if (!handler->conf->rootfs.path) {
a6f151a7 109 argv[i++] = "-P";
e0b0b533 110 argv[i++] = (char *)handler->lxcpath;
e0b0b533 111 }
a6f151a7 112
0ae4f887
GK
113 argv[i++] = "--";
114 for (j = 0; j < argc; j++)
115 argv[i++] = my_args->argv[j];
116 argv[i++] = NULL;
117
a6f151a7 118 NOTICE("Exec'ing \"%s\"", my_args->argv[0]);
0ae4f887
GK
119
120 execvp(argv[0], argv);
a6f151a7 121 SYSERROR("Failed to exec %s", argv[0]);
794248d0 122
cd90db2c 123out3:
321614a5 124 close(logfd);
e0b0b533
DE
125out2:
126 free(argv);
127out1:
0ae4f887
GK
128 return 1;
129}
130
131static 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
138static struct lxc_operations execute_start_ops = {
139 .start = execute_start,
140 .post_start = execute_post_start
141};
142
143int lxc_execute(const char *name, char *const argv[], int quiet,
aa460476 144 struct lxc_handler *handler, const char *lxcpath,
a3b4f3d6 145 bool backgrounded, int *error_num)
0ae4f887 146{
aa460476 147 struct execute_args args = {.argv = argv, .quiet = quiet};
0ae4f887 148
b2efeb0b 149 TRACE("Doing lxc_execute");
aa460476
CB
150 handler->conf->is_execute = 1;
151 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
a3b4f3d6 152 backgrounded, error_num);
0ae4f887 153}