]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/execute.c
utils: add LXC_PROC_PID_FD_LEN
[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{
cd90db2c 41 int j, i = 0, log = -1;
0ae4f887 42 struct execute_args *my_args = data;
cd90db2c 43 char **argv, *logfd;
e0b0b533 44 int argc = 0, argc_add;
0ae4f887
GK
45
46 while (my_args->argv[argc++]);
47
858faf70
SH
48 /* lxc-init -n name -- [argc] NULL -> 5 */
49 argc_add = 5;
e0b0b533
DE
50 if (my_args->quiet)
51 argc_add++;
858faf70
SH
52 if (!handler->conf->rootfs.path)
53 argc_add += 2;
54 if (lxc_log_has_valid_level())
55 argc_add += 2;
e0b0b533
DE
56
57 argv = malloc((argc + argc_add) * sizeof(*argv));
0ae4f887 58 if (!argv)
e0b0b533 59 goto out1;
0ae4f887 60
794248d0 61 if (!my_args->init_path)
e0b0b533 62 goto out2;
794248d0
CB
63
64 argv[i++] = my_args->init_path;
a6f151a7
CB
65
66 argv[i++] = "-n";
67 argv[i++] = (char *)handler->name;
68
69 if (lxc_log_has_valid_level()) {
70 argv[i++] = "-l";
71 argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
72 }
73
cd90db2c
TA
74 if (current_config->logfd != -1 || lxc_log_fd != -1) {
75 int to_dup = current_config->logfd;
76
77 if (current_config->logfd == -1)
78 to_dup = lxc_log_fd;
79
80 log = dup(to_dup);
81 if (log < 0) {
82 SYSERROR("dup of log fd failed");
83 goto out2;
84 }
85
86 if (asprintf(&logfd, "/proc/1/fd/%d", log) < 0) {
87 ERROR("Couldn't allocate memory for log string");
88 goto out3;
89 }
90
8f98ac7b 91 argv[i++] = "-o";
cd90db2c 92 argv[i++] = logfd;
8f98ac7b
CB
93 }
94
0ae4f887
GK
95 if (my_args->quiet)
96 argv[i++] = "--quiet";
a6f151a7 97
e0b0b533 98 if (!handler->conf->rootfs.path) {
a6f151a7 99 argv[i++] = "-P";
e0b0b533 100 argv[i++] = (char *)handler->lxcpath;
e0b0b533 101 }
a6f151a7 102
0ae4f887
GK
103 argv[i++] = "--";
104 for (j = 0; j < argc; j++)
105 argv[i++] = my_args->argv[j];
106 argv[i++] = NULL;
107
a6f151a7 108 NOTICE("Exec'ing \"%s\"", my_args->argv[0]);
0ae4f887
GK
109
110 execvp(argv[0], argv);
a6f151a7 111 SYSERROR("Failed to exec %s", argv[0]);
794248d0 112
cd90db2c
TA
113 free(logfd);
114out3:
115 close(log);
e0b0b533
DE
116out2:
117 free(argv);
118out1:
0ae4f887
GK
119 return 1;
120}
121
122static int execute_post_start(struct lxc_handler *handler, void* data)
123{
124 struct execute_args *my_args = data;
125 NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid);
126 return 0;
127}
128
129static struct lxc_operations execute_start_ops = {
130 .start = execute_start,
131 .post_start = execute_post_start
132};
133
134int lxc_execute(const char *name, char *const argv[], int quiet,
aa460476 135 struct lxc_handler *handler, const char *lxcpath,
a3b4f3d6 136 bool backgrounded, int *error_num)
0ae4f887 137{
aa460476 138 struct execute_args args = {.argv = argv, .quiet = quiet};
0ae4f887 139
47a46cf1 140 if (lxc_check_inherited(handler->conf, false, &handler->conf->maincmd_fd, 1))
0ae4f887
GK
141 return -1;
142
aa460476
CB
143 handler->conf->is_execute = 1;
144 return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
a3b4f3d6 145 backgrounded, error_num);
0ae4f887 146}