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