]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_execute.c
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
32 #include <sys/param.h>
34 #include <sys/types.h>
38 #include <lxc/lxccontainer.h>
40 #include "arguments.h"
47 lxc_log_define(lxc_execute
, lxc
);
49 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
);
50 static bool set_argv(struct lxc_container
*c
, struct lxc_arguments
*args
);
52 static struct lxc_list defines
;
54 static const struct option my_longopts
[] = {
55 {"daemon", no_argument
, 0, 'd'},
56 {"rcfile", required_argument
, 0, 'f'},
57 {"define", required_argument
, 0, 's'},
58 {"uid", required_argument
, 0, 'u'},
59 {"gid", required_argument
, 0, 'g'},
60 {"share-net", required_argument
, 0, OPT_SHARE_NET
},
61 {"share-ipc", required_argument
, 0, OPT_SHARE_IPC
},
62 {"share-uts", required_argument
, 0, OPT_SHARE_UTS
},
63 {"share-pid", required_argument
, 0, OPT_SHARE_PID
},
67 static struct lxc_arguments my_args
= {
68 .progname
= "lxc-execute",
70 --name=NAME -- COMMAND\n\
72 lxc-execute creates a container with the identifier NAME\n\
73 and execs COMMAND into this container.\n\
76 -n, --name=NAME NAME of the container\n\
77 -d, --daemon Daemonize the container\n\
78 -f, --rcfile=FILE Load configuration file FILE\n\
79 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\
80 -u, --uid=UID Execute COMMAND with UID inside the container\n\
81 -g, --gid=GID Execute COMMAND with GID inside the container\n",
82 .options
= my_longopts
,
84 .log_priority
= "ERROR",
89 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
)
101 ret
= lxc_config_define_add(&defines
, arg
);
103 lxc_config_define_free(&defines
);
106 if (lxc_safe_uint(arg
, &args
->uid
) < 0)
110 if (lxc_safe_uint(arg
, &args
->gid
) < 0)
114 args
->share_ns
[LXC_NS_NET
] = arg
;
117 args
->share_ns
[LXC_NS_IPC
] = arg
;
120 args
->share_ns
[LXC_NS_UTS
] = arg
;
123 args
->share_ns
[LXC_NS_PID
] = arg
;
129 static bool set_argv(struct lxc_container
*c
, struct lxc_arguments
*args
)
132 char buf
[MAXPATHLEN
];
133 char **components
, **p
;
136 ret
= c
->get_config_item(c
, "lxc.execute.cmd", buf
, MAXPATHLEN
);
140 components
= lxc_string_split_quoted(buf
);
144 args
->argv
= components
;
146 for (p
= components
; *p
; p
++)
152 int main(int argc
, char *argv
[])
154 struct lxc_container
*c
;
156 int err
= EXIT_FAILURE
;
160 lxc_list_init(&defines
);
165 if (lxc_arguments_parse(&my_args
, argc
, argv
))
168 log
.name
= my_args
.name
;
169 log
.file
= my_args
.log_file
;
170 log
.level
= my_args
.log_priority
;
171 log
.prefix
= my_args
.progname
;
172 log
.quiet
= my_args
.quiet
;
173 log
.lxcpath
= my_args
.lxcpath
[0];
175 if (lxc_log_init(&log
))
178 c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
180 ERROR("Failed to create lxc_container");
184 if (my_args
.rcfile
) {
187 if (!c
->load_config(c
, my_args
.rcfile
)) {
188 ERROR("Failed to load rcfile");
192 c
->configfile
= strdup(my_args
.rcfile
);
193 if (!c
->configfile
) {
194 ERROR("Out of memory setting new config filename");
200 ERROR("Executing a container with no configuration file may crash the host");
204 if (my_args
.argc
== 0)
205 if (!set_argv(c
, &my_args
)) {
206 ERROR("Missing command to execute!");
210 bret
= lxc_config_define_load(&defines
, c
);
217 ret
= snprintf(buf
, 256, "%d", my_args
.uid
);
218 if (ret
< 0 || (size_t)ret
>= 256)
221 bret
= c
->set_config_item(c
, "lxc.init.uid", buf
);
229 ret
= snprintf(buf
, 256, "%d", my_args
.gid
);
230 if (ret
< 0 || (size_t)ret
>= 256)
233 bret
= c
->set_config_item(c
, "lxc.init.gid", buf
);
238 if (!lxc_setup_shared_ns(&my_args
, c
))
241 c
->daemonize
= my_args
.daemonize
== 1;
243 bret
= c
->start(c
, 1, my_args
.argv
);
245 ERROR("Failed run an application inside container");
252 if (WIFEXITED(c
->error_num
))
253 err
= WEXITSTATUS(c
->error_num
);
255 /* Try to die with the same signal the task did. */
256 kill(0, WTERMSIG(c
->error_num
));
260 lxc_container_put(c
);