]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 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 | ||
24 | #define _GNU_SOURCE | |
25 | #include <stdio.h> | |
26 | #undef _GNU_SOURCE | |
27 | #include <string.h> | |
28 | #include <stdlib.h> | |
29 | #include <dirent.h> | |
30 | #include <errno.h> | |
31 | #include <unistd.h> | |
32 | #include <signal.h> | |
0ad19a3f | 33 | #include <sys/param.h> |
34 | #include <sys/file.h> | |
f4d507d5 | 35 | #include <sys/mount.h> |
0ad19a3f | 36 | #include <sys/types.h> |
0ad19a3f | 37 | #include <sys/prctl.h> |
42ff343d | 38 | #include <sys/capability.h> |
0ad19a3f | 39 | #include <sys/wait.h> |
0ad19a3f | 40 | |
e2bcd7db | 41 | #include "error.h" |
42 | ||
b113348e | 43 | #include <lxc/lxc.h> |
0ad19a3f | 44 | |
45 | LXC_TTY_HANDLER(SIGINT); | |
46 | LXC_TTY_HANDLER(SIGQUIT); | |
47 | ||
05f05512 | 48 | int lxc_start(const char *name, char *argv[]) |
0ad19a3f | 49 | { |
22ebac19 | 50 | char init[MAXPATHLEN]; |
51 | char *val = NULL; | |
f4d507d5 | 52 | char ttyname[MAXPATHLEN]; |
e2bcd7db | 53 | int fd, lock, sv[2], sync = 0, err = -LXC_ERROR_INTERNAL; |
0ad19a3f | 54 | pid_t pid; |
55 | int clone_flags; | |
f4d507d5 | 56 | |
0ad19a3f | 57 | lock = lxc_get_lock(name); |
58 | if (!lock) { | |
59 | lxc_log_error("'%s' is busy", name); | |
e2bcd7db | 60 | return -LXC_ERROR_BUSY; |
0ad19a3f | 61 | } |
62 | ||
63 | if (lock < 0) { | |
64 | lxc_log_error("failed to acquire lock on '%s':%s", | |
65 | name, strerror(-lock)); | |
e2bcd7db | 66 | return -LXC_ERROR_INTERNAL; |
0ad19a3f | 67 | } |
68 | ||
0ad19a3f | 69 | /* Begin the set the state to STARTING*/ |
70 | if (lxc_setstate(name, STARTING)) { | |
71 | lxc_log_error("failed to set state %s", lxc_state2str(STARTING)); | |
72 | goto out; | |
73 | } | |
74 | ||
f4d507d5 | 75 | if (readlink("/proc/self/fd/0", ttyname, sizeof(ttyname)) < 0) { |
76 | lxc_log_syserror("failed to read '/proc/self/fd/0'"); | |
77 | goto out; | |
78 | } | |
79 | ||
80 | ||
0ad19a3f | 81 | /* Synchro socketpair */ |
82 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { | |
83 | lxc_log_syserror("failed to create communication socketpair"); | |
f4d507d5 | 84 | goto out; |
0ad19a3f | 85 | } |
86 | ||
87 | /* Avoid signals from terminal */ | |
88 | LXC_TTY_ADD_HANDLER(SIGINT); | |
89 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
90 | ||
f4d507d5 | 91 | clone_flags = CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
0ad19a3f | 92 | if (conf_has_utsname(name)) |
93 | clone_flags |= CLONE_NEWUTS; | |
94 | if (conf_has_network(name)) | |
95 | clone_flags |= CLONE_NEWNET; | |
96 | ||
97 | /* Create a process in a new set of namespaces */ | |
98 | pid = fork_ns(clone_flags); | |
99 | if (pid < 0) { | |
100 | lxc_log_syserror("failed to fork into a new namespace"); | |
101 | goto err_fork_ns; | |
102 | } | |
103 | ||
104 | if (!pid) { | |
105 | ||
106 | close(sv[1]); | |
107 | ||
108 | /* Be sure we don't inherit this after the exec */ | |
109 | fcntl(sv[0], F_SETFD, FD_CLOEXEC); | |
110 | ||
111 | /* Tell our father he can begin to configure the container */ | |
112 | if (write(sv[0], &sync, sizeof(sync)) < 0) { | |
113 | lxc_log_syserror("failed to write socket"); | |
57545890 | 114 | goto out_child; |
0ad19a3f | 115 | } |
116 | ||
117 | /* Wait for the father to finish the configuration */ | |
118 | if (read(sv[0], &sync, sizeof(sync)) < 0) { | |
119 | lxc_log_syserror("failed to read socket"); | |
57545890 | 120 | goto out_child; |
0ad19a3f | 121 | } |
122 | ||
123 | /* Setup the container, ip, names, utsname, ... */ | |
124 | if (lxc_setup(name)) { | |
125 | lxc_log_error("failed to setup the container"); | |
126 | if (write(sv[0], &sync, sizeof(sync)) < 0) | |
127 | lxc_log_syserror("failed to write the socket"); | |
57545890 | 128 | goto out_child; |
0ad19a3f | 129 | } |
130 | ||
f4d507d5 | 131 | if (mount(ttyname, "/dev/console", "none", MS_BIND, 0)) { |
132 | lxc_log_syserror("failed to mount '/dev/console'"); | |
57545890 | 133 | goto out_child; |
f4d507d5 | 134 | } |
135 | ||
42ff343d | 136 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
137 | lxc_log_syserror("failed to remove CAP_SYS_BOOT capability"); | |
138 | goto out_child; | |
139 | } | |
140 | ||
0ad19a3f | 141 | execvp(argv[0], argv); |
142 | lxc_log_syserror("failed to exec %s", argv[0]); | |
143 | ||
144 | /* If the exec fails, tell that to our father */ | |
145 | if (write(sv[0], &sync, sizeof(sync)) < 0) | |
146 | lxc_log_syserror("failed to write the socket"); | |
147 | ||
57545890 | 148 | out_child: |
0ad19a3f | 149 | exit(1); |
150 | } | |
151 | ||
152 | close(sv[0]); | |
153 | ||
154 | /* Wait for the child to be ready */ | |
155 | if (read(sv[1], &sync, sizeof(sync)) < 0) { | |
156 | lxc_log_syserror("failed to read the socket"); | |
157 | goto err_pipe_read; | |
158 | } | |
159 | ||
218d4250 | 160 | if (lxc_link_nsgroup(name, pid)) |
161 | lxc_log_warning("cgroupfs not found: cgroup disabled"); | |
162 | ||
0ad19a3f | 163 | /* Create the network configuration */ |
164 | if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) { | |
165 | lxc_log_error("failed to create the configured network"); | |
166 | goto err_create_network; | |
167 | } | |
168 | ||
169 | /* Tell the child to continue its initialization */ | |
170 | if (write(sv[1], &sync, sizeof(sync)) < 0) { | |
171 | lxc_log_syserror("failed to write the socket"); | |
172 | goto err_pipe_write; | |
173 | } | |
174 | ||
175 | /* Wait for the child to exec or returning an error */ | |
176 | err = read(sv[1], &sync, sizeof(sync)); | |
177 | if (err < 0) { | |
178 | lxc_log_error("failed to read the socket"); | |
179 | goto err_pipe_read2; | |
180 | } | |
181 | ||
182 | if (err > 0) { | |
183 | lxc_log_error("something went wrong with %d", pid); | |
184 | /* TODO : check status etc ... */ | |
185 | waitpid(pid, NULL, 0); | |
186 | goto err_child_failed; | |
187 | } | |
188 | ||
189 | asprintf(&val, "%d\n", pid); | |
22ebac19 | 190 | |
191 | snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name); | |
192 | ||
0ad19a3f | 193 | fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); |
194 | if (fd < 0) { | |
195 | lxc_log_syserror("failed to open '%s'", init); | |
196 | goto err_write; | |
197 | } | |
198 | ||
199 | if (write(fd, val, strlen(val)) < 0) { | |
200 | lxc_log_syserror("failed to write the init pid"); | |
201 | goto err_write; | |
202 | } | |
203 | ||
204 | close(fd); | |
205 | ||
0ad19a3f | 206 | if (lxc_setstate(name, RUNNING)) { |
207 | lxc_log_error("failed to set state to %s", | |
208 | lxc_state2str(RUNNING)); | |
209 | goto err_state_failed; | |
210 | } | |
211 | ||
212 | wait_again: | |
213 | if (waitpid(pid, NULL, 0) < 0) { | |
214 | if (errno == EINTR) | |
215 | goto wait_again; | |
216 | lxc_log_syserror("failed to wait the pid %d", pid); | |
217 | goto err_waitpid_failed; | |
218 | } | |
219 | ||
220 | if (lxc_setstate(name, STOPPING)) | |
221 | lxc_log_error("failed to set state %s", lxc_state2str(STOPPING)); | |
222 | ||
b7c9b199 | 223 | #ifdef NETWORK_DESTROY |
0ad19a3f | 224 | if (clone_flags & CLONE_NEWNET && conf_destroy_network(name)) |
225 | lxc_log_error("failed to destroy the network"); | |
b7c9b199 | 226 | #endif |
0ad19a3f | 227 | |
228 | err = 0; | |
229 | out: | |
230 | if (lxc_setstate(name, STOPPED)) | |
231 | lxc_log_error("failed to set state %s", lxc_state2str(STOPPED)); | |
232 | ||
233 | lxc_unlink_nsgroup(name); | |
234 | unlink(init); | |
0ad19a3f | 235 | free(val); |
236 | lxc_put_lock(lock); | |
536b97f0 | 237 | LXC_TTY_DEL_HANDLER(SIGQUIT); |
238 | LXC_TTY_DEL_HANDLER(SIGINT); | |
0ad19a3f | 239 | |
240 | return err; | |
241 | ||
242 | err_write: | |
243 | close(fd); | |
244 | ||
245 | err_state_failed: | |
246 | err_child_failed: | |
247 | err_pipe_read2: | |
248 | err_pipe_write: | |
b7c9b199 | 249 | #ifdef NETWORK_DESTROY |
0ad19a3f | 250 | if (clone_flags & CLONE_NEWNET) |
251 | conf_destroy_network(name); | |
b7c9b199 | 252 | #endif |
0ad19a3f | 253 | err_create_network: |
254 | err_pipe_read: | |
255 | err_waitpid_failed: | |
256 | if (lxc_setstate(name, ABORTING)) | |
257 | lxc_log_error("failed to set state %s", lxc_state2str(STOPPED)); | |
258 | ||
259 | kill(pid, SIGKILL); | |
260 | err_fork_ns: | |
0ad19a3f | 261 | close(sv[0]); |
262 | close(sv[1]); | |
0ad19a3f | 263 | goto out; |
264 | } |