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