]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_init.c
Remove annoying warnings and fix tty for restart
[mirror_lxc.git] / src / lxc / lxc_init.c
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 #include <stdio.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/mount.h>
31 #define _GNU_SOURCE
32 #include <getopt.h>
33
34 static int mount_sysfs;
35 static int mount_procfs;
36
37 static struct option options[] = {
38 { "mount-sysfs", no_argument, &mount_sysfs, 1 },
39 { "mount-procfs", no_argument, &mount_procfs, 1 },
40 };
41
42 int main(int argc, char *argv[])
43 {
44 pid_t pid;
45
46 int nbargs = 0;
47 char **aargv;
48
49 while (1) {
50 int ret = getopt_long_only(argc, argv, "", options, NULL);
51 if (ret == -1)
52 break;
53 if (ret == '?')
54 exit(1);
55 nbargs++;
56 }
57
58 if (!argv[optind]) {
59 fprintf(stderr, "missing command to launch\n");
60 exit(1);
61 }
62
63 aargv = &argv[optind];
64 argc -= nbargs;
65
66 pid = fork();
67
68 if (pid < 0)
69 exit(1);
70
71 if (!pid) {
72
73 if (mount_sysfs && mount("sysfs", "/sys", "sysfs", 0, NULL)) {
74 fprintf(stderr, "failed to mount '/sys'\n");
75 exit(1);
76 }
77
78 if (mount_procfs && mount("proc", "/proc", "proc", 0, NULL)) {
79 fprintf(stderr, "failed to mount '/proc'\n");
80 exit(1);
81 }
82
83 execvp(aargv[0], aargv);
84 fprintf(stderr, "failed to exec: %s\n", aargv[0]);
85 exit(1);
86 }
87
88 for (;;) {
89 int status;
90 if (wait(&status) < 0) {
91 if (errno == ECHILD)
92 exit(0);
93 if (errno == EINTR)
94 continue;
95 fprintf(stderr, "failed to wait child\n");
96 return 1;
97 }
98 }
99
100 return 0;
101 }