summaryrefslogtreecommitdiff
blob: 6d2bb09843ea7a06f41563cfe1827be1ef718211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
From fae349da89b9ad063f0080970558b7f02ce233c2 Mon Sep 17 00:00:00 2001
From: Daniel Lezcano <daniel.lezcano@free.fr>
Date: Thu, 26 Nov 2009 15:46:24 +0000
Subject: pass lxc_conf to the lxc_start function instead of the rcfile

The rcfile is parsed in the lxc_start function. This is not the place
to do that. Let's the caller to do that.

In the meantime, we have the lxc_conf structure filled right before
calling the lxc_start function so we can do some sanity check on the
configuration to not break the system when we launch the container.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com> 
---
diff --git a/src/lxc/commands.c b/src/lxc/commands.c
index 02239e5..4c48571 100644
--- a/src/lxc/commands.c
+++ b/src/lxc/commands.c
@@ -135,7 +135,7 @@ static int trigger_command(int fd, struct lxc_request *request,
 static void command_fd_cleanup(int fd, struct lxc_handler *handler,
 			       struct lxc_epoll_descr *descr)
 {
-	lxc_console_remove_fd(fd, &handler->conf.tty_info);
+	lxc_console_remove_fd(fd, &handler->conf->tty_info);
 	lxc_mainloop_del_handler(descr, fd);
 	close(fd);
 }
diff --git a/src/lxc/console.c b/src/lxc/console.c
index 52f6cec..96a6edd 100644
--- a/src/lxc/console.c
+++ b/src/lxc/console.c
@@ -98,7 +98,7 @@ extern int lxc_console_callback(int fd, struct lxc_request *request,
 			struct lxc_handler *handler)
 {
 	int ttynum = request->data;
-	struct lxc_tty_info *tty_info = &handler->conf.tty_info;
+	struct lxc_tty_info *tty_info = &handler->conf->tty_info;
 
 	if (ttynum > 0) {
 		if (ttynum > tty_info->nbtty)
diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h
index 66cb3b8..8cf21c1 100644
--- a/src/lxc/lxc.h
+++ b/src/lxc/lxc.h
@@ -31,6 +31,7 @@ extern "C" {
 #include <lxc/state.h>
 
 struct lxc_msg;
+struct lxc_conf;
 
 /**
  Following code is for liblxc.
@@ -44,7 +45,7 @@ struct lxc_msg;
  * @argv     : an array of char * corresponding to the commande line
  * Returns 0 on sucess, < 0 otherwise
  */
-extern int lxc_start(const char *name, char *const argv[], const char *rcfile);
+extern int lxc_start(const char *name, char *const argv[], struct lxc_conf *);
 
 /*
  * Stop the container previously started with lxc_start, all
diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c
index 846a96f..40a4b93 100644
--- a/src/lxc/lxc_execute.c
+++ b/src/lxc/lxc_execute.c
@@ -31,10 +31,11 @@
 #include <sys/stat.h>
 #include <sys/param.h>
 
-#include <lxc/log.h>
-#include <lxc/confile.h>
-#include <lxc/lxc.h>
 
+#include "lxc.h"
+#include "log.h"
+#include "conf.h"
+#include "confile.h"
 #include "arguments.h"
 #include "config.h"
 
@@ -83,6 +84,7 @@ int main(int argc, char *argv[])
 {
 	static char **args;
 	char *rcfile;
+	struct lxc_conf conf;
 
 	if (lxc_arguments_parse(&my_args, argc, argv))
 		return -1;
@@ -111,6 +113,16 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	return lxc_start(my_args.name, args, my_args.rcfile);
+	if (lxc_conf_init(&conf)) {
+		ERROR("failed to initialze configuration");
+		return -1;
+	}
+
+	if (rcfile && lxc_config_read(rcfile, &conf)) {
+		ERROR("failed to read configuration file");
+		return -1;
+	}
+
+	return lxc_start(my_args.name, args, &conf);
 }
 
diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
index cf87abf..b8d03e8 100644
--- a/src/lxc/lxc_start.c
+++ b/src/lxc/lxc_start.c
@@ -40,12 +40,13 @@
 #include <netinet/in.h>
 #include <net/if.h>
 
-#include <lxc/lxc.h>
-#include <lxc/log.h>
-#include <lxc/utils.h>
-
-#include "arguments.h"
+#include "log.h"
+#include "lxc.h"
+#include "conf.h"
+#include "utils.h"
 #include "config.h"
+#include "confile.h"
+#include "arguments.h"
 
 lxc_log_define(lxc_start, lxc);
 
@@ -132,6 +133,7 @@ int main(int argc, char *argv[])
 	};
 
 	char *rcfile = NULL;
+	struct lxc_conf conf;
 
 	if (lxc_arguments_parse(&my_args, argc, argv))
 		return err;
@@ -161,6 +163,16 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (lxc_conf_init(&conf)) {
+		ERROR("failed to initialze configuration");
+		return err;
+	}
+
+	if (rcfile && lxc_config_read(rcfile, &conf)) {
+		ERROR("failed to read configuration file");
+		return err;
+	}
+
 	if (my_args.daemonize) {
 
                 /* do not chdir as we want to open the log file,
@@ -187,7 +199,7 @@ int main(int argc, char *argv[])
 
 	save_tty(&tios);
 
-	err = lxc_start(my_args.name, args, rcfile);
+	err = lxc_start(my_args.name, args, &conf);
 
 	restore_tty(&tios);
 
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 7143421..7e9d924 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -230,7 +230,7 @@ static int console_init(char *console, size_t size)
 	return 0;
 }
 
-struct lxc_handler *lxc_init(const char *name, const char *rcfile)
+struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
 {
 	struct lxc_handler *handler;
 
@@ -240,36 +240,20 @@ struct lxc_handler *lxc_init(const char *name, const char *rcfile)
 
 	memset(handler, 0, sizeof(*handler));
 
+	handler->conf = conf;
+
 	/* Begin the set the state to STARTING*/
 	if (lxc_set_state(name, handler, STARTING)) {
 		ERROR("failed to set state '%s'", lxc_state2str(STARTING));
 		goto out_free;
 	}
 
-	if (lxc_conf_init(&handler->conf)) {
-		ERROR("failed to initialize the configuration");
-		goto out_aborting;
-	}
-
-	if (rcfile) {
-		if (access(rcfile, F_OK)) {
-			ERROR("failed to access '%s'", rcfile);
-			goto out_aborting;
-		}
-
-		if (lxc_config_read(rcfile, &handler->conf)) {
-			ERROR("failed to read '%s'", rcfile);
-			goto out_aborting;
-		}
-	}
-
-	if (console_init(handler->conf.console,
-			 sizeof(handler->conf.console))) {
+	if (console_init(conf->console, sizeof(conf->console))) {
 		ERROR("failed to initialize the console");
 		goto out_aborting;
 	}
 
-	if (lxc_create_tty(name, &handler->conf)) {
+	if (lxc_create_tty(name, conf)) {
 		ERROR("failed to create the ttys");
 		goto out_aborting;
 	}
@@ -294,7 +278,7 @@ out:
 	return handler;
 
 out_delete_tty:
-	lxc_delete_tty(&handler->conf.tty_info);
+	lxc_delete_tty(&conf->tty_info);
 out_aborting:
 	lxc_set_state(name, handler, ABORTING);
 out_free:
@@ -313,7 +297,7 @@ void lxc_fini(const char *name, struct lxc_handler *handler)
 	lxc_unlink_nsgroup(name);
 
 	if (handler) {
-		lxc_delete_tty(&handler->conf.tty_info);
+		lxc_delete_tty(&handler->conf->tty_info);
 		free(handler);
 	}
 
@@ -366,7 +350,7 @@ static int do_start(void *arg)
 	}
 
 	/* Setup the container, ip, names, utsname, ... */
-	if (lxc_setup(name, &handler->conf)) {
+	if (lxc_setup(name, handler->conf)) {
 		ERROR("failed to setup the container");
 		goto out_warn_father;
 	}
@@ -414,14 +398,14 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
 	}
 
 	clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
-	if (!lxc_list_empty(&handler->conf.network)) {
+	if (!lxc_list_empty(&handler->conf->network)) {
 
 		clone_flags |= CLONE_NEWNET;
 
 		/* that should be done before the clone because we will
 		 * fill the netdev index and use them in the child
 		 */
-		if (lxc_create_network(&handler->conf.network)) {
+		if (lxc_create_network(&handler->conf->network)) {
 			ERROR("failed to create the network");
 			goto out_close;
 		}
@@ -447,7 +431,7 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
 
 	/* Create the network configuration */
 	if (clone_flags & CLONE_NEWNET) {
-		if (lxc_assign_network(&handler->conf.network, handler->pid)) {
+		if (lxc_assign_network(&handler->conf->network, handler->pid)) {
 			ERROR("failed to create the configured network");
 			goto out_abort;
 		}
@@ -486,13 +470,13 @@ out_abort:
 	goto out_close;
 }
 
-int lxc_start(const char *name, char *const argv[], const char *rcfile)
+int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
 {
 	struct lxc_handler *handler;
 	int err = -1;
 	int status;
 
-	handler = lxc_init(name, rcfile);
+	handler = lxc_init(name, conf);
 	if (!handler) {
 		ERROR("failed to initialize the container");
 		return -1;
diff --git a/src/lxc/start.h b/src/lxc/start.h
index 3390411..ba55562 100644
--- a/src/lxc/start.h
+++ b/src/lxc/start.h
@@ -34,10 +34,10 @@ struct lxc_handler {
 	int sigfd;
 	char nsgroup[MAXPATHLEN];
 	sigset_t oldmask;
-	struct lxc_conf conf;
+	struct lxc_conf *conf;
 };
 
-extern struct lxc_handler *lxc_init(const char *name, const char *rcfile);
+extern struct lxc_handler *lxc_init(const char *name, struct lxc_conf *);
 extern int lxc_spawn(const char *name, struct lxc_handler *handler,
 		     char *const argv[]);
 
--
cgit v0.8.3