]>
Commit | Line | Data |
---|---|---|
0ae4f887 GK |
1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | ||
a0a2066d SH |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | |
0ae4f887 GK |
26 | #include <errno.h> |
27 | #include <unistd.h> | |
28 | #include <stdlib.h> | |
29 | ||
a0a2066d | 30 | |
0ae4f887 GK |
31 | #include "log.h" |
32 | #include "start.h" | |
33 | ||
34 | lxc_log_define(lxc_execute, lxc_start); | |
35 | ||
36 | struct execute_args { | |
37 | char *const *argv; | |
38 | int quiet; | |
39 | }; | |
40 | ||
a0a2066d SH |
41 | /* historically lxc-init has been under /usr/lib/lxc. Now with |
42 | * multi-arch it can be under /usr/lib/$ARCH/lxc. Serge thinks | |
43 | * it makes more sense to put it under /sbin. | |
44 | * If /usr/lib/$ARCH/lxc exists and is used, then LXCINITDIR will | |
45 | * point to it. | |
46 | */ | |
47 | static char *choose_init(void) | |
48 | { | |
49 | char *retv = malloc(PATH_MAX); | |
50 | int ret; | |
51 | struct stat mystat; | |
52 | if (!retv) | |
53 | return NULL; | |
54 | ||
9ba8130c SH |
55 | ret = snprintf(retv, PATH_MAX, LXCINITDIR "/lxc/lxc-init"); |
56 | if (ret < 0 || ret >= PATH_MAX) { | |
57 | ERROR("pathname too long"); | |
58 | return NULL; | |
59 | } | |
60 | ||
a0a2066d SH |
61 | ret = stat(retv, &mystat); |
62 | if (ret == 0) | |
63 | return retv; | |
9ba8130c SH |
64 | |
65 | ret = snprintf(retv, PATH_MAX, "/usr/lib/lxc/lxc-init"); | |
66 | if (ret < 0 || ret >= PATH_MAX) { | |
67 | ERROR("pathname too long"); | |
68 | return NULL; | |
69 | } | |
a0a2066d SH |
70 | ret = stat(retv, &mystat); |
71 | if (ret == 0) | |
72 | return retv; | |
9ba8130c SH |
73 | ret = snprintf(retv, PATH_MAX, "/sbin/lxc-init"); |
74 | if (ret < 0 || ret >= PATH_MAX) { | |
75 | ERROR("pathname too long"); | |
76 | return NULL; | |
77 | } | |
a0a2066d SH |
78 | ret = stat(retv, &mystat); |
79 | if (ret == 0) | |
80 | return retv; | |
81 | return NULL; | |
82 | } | |
83 | ||
0ae4f887 GK |
84 | static int execute_start(struct lxc_handler *handler, void* data) |
85 | { | |
86 | int j, i = 0; | |
87 | struct execute_args *my_args = data; | |
88 | char **argv; | |
89 | int argc = 0; | |
a0a2066d | 90 | char *initpath; |
0ae4f887 GK |
91 | |
92 | while (my_args->argv[argc++]); | |
93 | ||
94 | argv = malloc((argc + my_args->quiet ? 5 : 4) * sizeof(*argv)); | |
95 | if (!argv) | |
96 | return 1; | |
97 | ||
a0a2066d SH |
98 | initpath = choose_init(); |
99 | if (!initpath) { | |
100 | ERROR("Failed to find an lxc-init"); | |
101 | return 1; | |
102 | } | |
103 | argv[i++] = initpath; | |
0ae4f887 GK |
104 | if (my_args->quiet) |
105 | argv[i++] = "--quiet"; | |
106 | argv[i++] = "--"; | |
107 | for (j = 0; j < argc; j++) | |
108 | argv[i++] = my_args->argv[j]; | |
109 | argv[i++] = NULL; | |
110 | ||
111 | NOTICE("exec'ing '%s'", my_args->argv[0]); | |
112 | ||
113 | execvp(argv[0], argv); | |
114 | SYSERROR("failed to exec %s", argv[0]); | |
115 | return 1; | |
116 | } | |
117 | ||
118 | static int execute_post_start(struct lxc_handler *handler, void* data) | |
119 | { | |
120 | struct execute_args *my_args = data; | |
121 | NOTICE("'%s' started with pid '%d'", my_args->argv[0], handler->pid); | |
122 | return 0; | |
123 | } | |
124 | ||
125 | static struct lxc_operations execute_start_ops = { | |
126 | .start = execute_start, | |
127 | .post_start = execute_post_start | |
128 | }; | |
129 | ||
130 | int lxc_execute(const char *name, char *const argv[], int quiet, | |
131 | struct lxc_conf *conf) | |
132 | { | |
133 | struct execute_args args = { | |
134 | .argv = argv, | |
135 | .quiet = quiet | |
136 | }; | |
137 | ||
b119f362 | 138 | if (lxc_check_inherited(conf, -1)) |
0ae4f887 GK |
139 | return -1; |
140 | ||
141 | return __lxc_start(name, conf, &execute_start_ops, &args); | |
142 | } |