]>
Commit | Line | Data |
---|---|---|
5e97c3fc | 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 | */ | |
96c210bb | 23 | #define _GNU_SOURCE |
5e97c3fc | 24 | #include <stdio.h> |
96c210bb | 25 | #undef _GNU_SOURCE |
5e97c3fc | 26 | #include <libgen.h> |
96c210bb | 27 | #include <stdlib.h> |
5e97c3fc | 28 | #include <unistd.h> |
29 | #include <string.h> | |
b0a33c1e | 30 | #include <termios.h> |
31 | #include <errno.h> | |
c36583c3 | 32 | #include <fcntl.h> |
f8e09a0b | 33 | #include <signal.h> |
5e97c3fc | 34 | #include <sys/param.h> |
35 | #include <sys/utsname.h> | |
36 | #include <sys/types.h> | |
37 | #include <sys/socket.h> | |
c36583c3 | 38 | #include <sys/stat.h> |
5e97c3fc | 39 | #include <arpa/inet.h> |
40 | #include <netinet/in.h> | |
41 | #include <net/if.h> | |
42 | ||
fae349da DL |
43 | #include "log.h" |
44 | #include "lxc.h" | |
45 | #include "conf.h" | |
63376d7d | 46 | #include "cgroup.h" |
fae349da | 47 | #include "utils.h" |
96c210bb | 48 | #include "config.h" |
fae349da DL |
49 | #include "confile.h" |
50 | #include "arguments.h" | |
36eb9bde | 51 | |
77075659 | 52 | lxc_log_define(lxc_start_ui, lxc_start); |
5e97c3fc | 53 | |
33ba4ad7 CLG |
54 | static struct lxc_list defines; |
55 | ||
c36583c3 DL |
56 | static int my_parser(struct lxc_arguments* args, int c, char* arg) |
57 | { | |
58 | switch (c) { | |
59 | case 'd': args->daemonize = 1; break; | |
48862401 | 60 | case 'f': args->rcfile = arg; break; |
33ba4ad7 | 61 | case 's': return lxc_config_define_add(&defines, arg); |
c36583c3 DL |
62 | } |
63 | return 0; | |
64 | } | |
65 | ||
9618063c | 66 | static const struct option my_longopts[] = { |
c36583c3 | 67 | {"daemon", no_argument, 0, 'd'}, |
48862401 | 68 | {"rcfile", required_argument, 0, 'f'}, |
33ba4ad7 | 69 | {"define", required_argument, 0, 's'}, |
9618063c MN |
70 | LXC_COMMON_OPTIONS |
71 | }; | |
72 | ||
73 | static struct lxc_arguments my_args = { | |
74 | .progname = "lxc-start", | |
75 | .help = "\ | |
76 | --name=NAME -- COMMAND\n\ | |
77 | \n\ | |
78 | lxc-start start COMMAND in specified container NAME\n\ | |
79 | \n\ | |
80 | Options :\n\ | |
c36583c3 | 81 | -n, --name=NAME NAME for name of the container\n\ |
48862401 | 82 | -d, --daemon daemonize the container\n\ |
33ba4ad7 CLG |
83 | -f, --rcfile=FILE Load configuration file FILE\n\ |
84 | -s, --define KEY=VAL Assign VAL to configuration variable KEY\n", | |
c36583c3 DL |
85 | .options = my_longopts, |
86 | .parser = my_parser, | |
87 | .checker = NULL, | |
88 | .daemonize = 0, | |
9618063c | 89 | }; |
5e97c3fc | 90 | |
91 | int main(int argc, char *argv[]) | |
92 | { | |
9618063c | 93 | char *const *args; |
e043236e | 94 | int err = -1; |
b0a33c1e | 95 | |
9618063c | 96 | char *const default_args[] = { |
b2b6c597 | 97 | "/sbin/init", |
98 | '\0', | |
99 | }; | |
5e97c3fc | 100 | |
96c210bb | 101 | char *rcfile = NULL; |
7b379ab3 | 102 | struct lxc_conf *conf; |
96c210bb | 103 | |
33ba4ad7 CLG |
104 | lxc_list_init(&defines); |
105 | ||
e043236e MN |
106 | if (lxc_arguments_parse(&my_args, argc, argv)) |
107 | return err; | |
5e97c3fc | 108 | |
9618063c | 109 | if (!my_args.argc) |
b2b6c597 | 110 | args = default_args; |
9618063c MN |
111 | else |
112 | args = my_args.argv; | |
5e97c3fc | 113 | |
9618063c MN |
114 | if (lxc_log_init(my_args.log_file, my_args.log_priority, |
115 | my_args.progname, my_args.quiet)) | |
e043236e | 116 | return err; |
51cab631 | 117 | |
96c210bb DL |
118 | /* rcfile is specified in the cli option */ |
119 | if (my_args.rcfile) | |
120 | rcfile = (char *)my_args.rcfile; | |
121 | else { | |
7418c9ce | 122 | if (!asprintf(&rcfile, LXCPATH "/%s/config", my_args.name)) { |
96c210bb DL |
123 | SYSERROR("failed to allocate memory"); |
124 | return err; | |
125 | } | |
126 | ||
127 | /* container configuration does not exist */ | |
128 | if (access(rcfile, F_OK)) { | |
129 | free(rcfile); | |
130 | rcfile = NULL; | |
131 | } | |
132 | } | |
133 | ||
7b379ab3 MN |
134 | conf = lxc_conf_init(); |
135 | if (!conf) { | |
136 | ERROR("failed to initialize configuration"); | |
fae349da DL |
137 | return err; |
138 | } | |
139 | ||
7b379ab3 | 140 | if (rcfile && lxc_config_read(rcfile, conf)) { |
fae349da DL |
141 | ERROR("failed to read configuration file"); |
142 | return err; | |
143 | } | |
144 | ||
33ba4ad7 CLG |
145 | if (lxc_config_define_load(&defines, conf)) |
146 | return err; | |
147 | ||
f2ae79a0 AN |
148 | if (!rcfile && !strcmp("/sbin/init", args[0])) { |
149 | ERROR("no configuration file for '/sbin/init' (may crash the host)"); | |
150 | return err; | |
151 | } | |
152 | ||
b8f57738 DL |
153 | if (my_args.daemonize) { |
154 | ||
155 | /* do not chdir as we want to open the log file, | |
156 | * change the directory right after. | |
157 | * do not close 0, 1, 2, we want to do that | |
158 | * ourself because we don't want /dev/null | |
159 | * being reopened. | |
160 | */ | |
af795875 | 161 | if (daemon(1, 1)) { |
b8f57738 DL |
162 | SYSERROR("failed to daemonize '%s'", my_args.name); |
163 | return err; | |
164 | } | |
165 | ||
80090207 CLG |
166 | close(0); |
167 | close(1); | |
168 | close(2); | |
b8f57738 DL |
169 | |
170 | if (my_args.log_file) { | |
171 | open(my_args.log_file, O_WRONLY | O_CLOEXEC); | |
172 | open(my_args.log_file, O_RDONLY | O_CLOEXEC); | |
173 | open(my_args.log_file, O_RDONLY | O_CLOEXEC); | |
174 | } | |
c36583c3 DL |
175 | } |
176 | ||
7b379ab3 | 177 | err = lxc_start(my_args.name, args, conf); |
5e97c3fc | 178 | |
b0a33c1e | 179 | return err; |
5e97c3fc | 180 | } |
181 |