2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
25 #include <sys/types.h>
37 lxc_log_define(lxc_execute
, lxc_start
);
39 static int execute_start(struct lxc_handler
*handler
, void* data
)
43 int argc
= 0, i
= 0, logfd
= -1;
44 struct execute_args
*my_args
= data
;
45 char logfile
[LXC_PROC_PID_FD_LEN
];
47 while (my_args
->argv
[argc
++]);
49 /* lxc-init -n name -- [argc] NULL -> 5 */
54 if (!handler
->conf
->rootfs
.path
)
57 if (lxc_log_has_valid_level())
60 if (current_config
->logfd
!= -1 || lxc_log_fd
!= -1)
63 argv
= malloc((argc
+ argc_add
) * sizeof(*argv
));
65 SYSERROR("Allocating init args failed");
69 if (!my_args
->init_path
) {
70 ERROR("Init path missing");
74 argv
[i
++] = my_args
->init_path
;
77 argv
[i
++] = (char *)handler
->name
;
79 if (lxc_log_has_valid_level()) {
81 argv
[i
++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
84 if (current_config
->logfd
!= -1 || lxc_log_fd
!= -1) {
86 int to_dup
= current_config
->logfd
;
88 if (current_config
->logfd
== -1)
93 SYSERROR("Failed to duplicate log file descriptor");
97 ret
= snprintf(logfile
, sizeof(logfile
), "/proc/1/fd/%d", logfd
);
98 if (ret
< 0 || (size_t)ret
>= sizeof(logfile
))
106 argv
[i
++] = "--quiet";
108 if (!handler
->conf
->rootfs
.path
) {
110 argv
[i
++] = (char *)handler
->lxcpath
;
114 for (j
= 0; j
< argc
; j
++)
115 argv
[i
++] = my_args
->argv
[j
];
118 NOTICE("Exec'ing \"%s\"", my_args
->argv
[0]);
120 execvp(argv
[0], argv
);
121 SYSERROR("Failed to exec %s", argv
[0]);
131 static int execute_post_start(struct lxc_handler
*handler
, void* data
)
133 struct execute_args
*my_args
= data
;
134 NOTICE("'%s' started with pid '%d'", my_args
->argv
[0], handler
->pid
);
138 static struct lxc_operations execute_start_ops
= {
139 .start
= execute_start
,
140 .post_start
= execute_post_start
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
)
147 struct execute_args args
= {.argv
= argv
, .quiet
= quiet
};
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
);