]>
Commit | Line | Data |
---|---|---|
5e97c3fc | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
5e97c3fc | 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 | #include <stdio.h> | |
12a50cc6 | 24 | #include <stdlib.h> |
0ad19a3f | 25 | #include <string.h> |
26 | #include <fcntl.h> | |
27 | #include <errno.h> | |
5e97c3fc | 28 | #include <unistd.h> |
5e97c3fc | 29 | #include <sys/types.h> |
90b59fd0 | 30 | #include <sys/socket.h> |
0ad19a3f | 31 | #include <sys/param.h> |
32 | #include <sys/stat.h> | |
33 | #include <sys/file.h> | |
5e97c3fc | 34 | |
72d0e1cb | 35 | #include <lxc/lxc.h> |
36eb9bde | 36 | #include <lxc/log.h> |
00b3c2e2 | 37 | #include <lxc/start.h> |
108ed092 | 38 | #include <lxc/cgroup.h> |
72d0e1cb | 39 | #include <lxc/monitor.h> |
e98fe68b | 40 | #include "commands.h" |
881450bb | 41 | #include "config.h" |
36eb9bde CLG |
42 | |
43 | lxc_log_define(lxc_state, lxc); | |
0ad19a3f | 44 | |
45 | static char *strstate[] = { | |
46 | "STOPPED", "STARTING", "RUNNING", "STOPPING", | |
fa082227 | 47 | "ABORTING", "FREEZING", "FROZEN", "THAWED", |
0ad19a3f | 48 | }; |
49 | ||
50 | const char *lxc_state2str(lxc_state_t state) | |
51 | { | |
52 | if (state < STOPPED || state > MAX_STATE - 1) | |
53 | return NULL; | |
54 | return strstate[state]; | |
55 | } | |
5e97c3fc | 56 | |
0ad19a3f | 57 | lxc_state_t lxc_str2state(const char *state) |
5e97c3fc | 58 | { |
e6a19d26 DE |
59 | size_t len; |
60 | lxc_state_t i; | |
0ad19a3f | 61 | len = sizeof(strstate)/sizeof(strstate[0]); |
62 | for (i = 0; i < len; i++) | |
63 | if (!strcmp(strstate[i], state)) | |
64 | return i; | |
3ab87b66 | 65 | |
439358bf | 66 | ERROR("invalid state '%s'", state); |
0ad19a3f | 67 | return -1; |
5e97c3fc | 68 | } |
69 | ||
e6a19d26 | 70 | static lxc_state_t freezer_state(const char *name, const char *lxcpath) |
0ad19a3f | 71 | { |
fa082227 | 72 | char *nsgroup; |
0ad19a3f | 73 | char freezer[MAXPATHLEN]; |
74 | char status[MAXPATHLEN]; | |
75 | FILE *file; | |
76 | int err; | |
35d2c3e7 | 77 | |
ae5c8b8e | 78 | err = lxc_cgroup_path_get(&nsgroup, "freezer", name, lxcpath); |
fa082227 MN |
79 | if (err) |
80 | return -1; | |
81 | ||
9ba8130c SH |
82 | err = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", nsgroup); |
83 | if (err < 0 || err >= MAXPATHLEN) | |
84 | return -1; | |
0ad19a3f | 85 | |
86 | file = fopen(freezer, "r"); | |
87 | if (!file) | |
88 | return -1; | |
89 | ||
90 | err = fscanf(file, "%s", status); | |
91 | fclose(file); | |
92 | ||
93 | if (err == EOF) { | |
36eb9bde | 94 | SYSERROR("failed to read %s", freezer); |
0ad19a3f | 95 | return -1; |
96 | } | |
97 | ||
98 | return lxc_str2state(status); | |
99 | } | |
100 | ||
13f5be62 | 101 | static lxc_state_t __lxc_getstate(const char *name, const char *lxcpath) |
e98fe68b DL |
102 | { |
103 | struct lxc_command command = { | |
104 | .request = { .type = LXC_COMMAND_STATE }, | |
105 | }; | |
106 | ||
d97b36f8 DL |
107 | int ret, stopped = 0; |
108 | ||
13f5be62 | 109 | ret = lxc_command(name, &command, &stopped, lxcpath); |
d97b36f8 DL |
110 | if (ret < 0 && stopped) |
111 | return STOPPED; | |
e98fe68b | 112 | |
e98fe68b DL |
113 | if (ret < 0) { |
114 | ERROR("failed to send command"); | |
115 | return -1; | |
116 | } | |
117 | ||
118 | if (!ret) { | |
119 | WARN("'%s' has stopped before sending its state", name); | |
120 | return -1; | |
121 | } | |
122 | ||
123 | if (command.answer.ret < 0) { | |
124 | ERROR("failed to get state for '%s': %s", | |
125 | name, strerror(-command.answer.ret)); | |
126 | return -1; | |
127 | } | |
128 | ||
129 | DEBUG("'%s' is in '%s' state", name, lxc_state2str(command.answer.ret)); | |
130 | ||
131 | return command.answer.ret; | |
132 | } | |
133 | ||
13f5be62 | 134 | lxc_state_t lxc_getstate(const char *name, const char *lxcpath) |
0ad19a3f | 135 | { |
e6a19d26 | 136 | lxc_state_t state = freezer_state(name, lxcpath); |
0ad19a3f | 137 | if (state != FROZEN && state != FREEZING) |
13f5be62 | 138 | state = __lxc_getstate(name, lxcpath); |
0ad19a3f | 139 | return state; |
140 | } | |
e98fe68b DL |
141 | |
142 | /*---------------------------------------------------------------------------- | |
143 | * functions used by lxc-start mainloop | |
144 | * to handle above command request. | |
145 | *--------------------------------------------------------------------------*/ | |
146 | extern int lxc_state_callback(int fd, struct lxc_request *request, | |
147 | struct lxc_handler *handler) | |
148 | { | |
149 | struct lxc_answer answer; | |
150 | int ret; | |
151 | ||
ae5c8b8e | 152 | memset(&answer, 0, sizeof(answer)); |
e98fe68b DL |
153 | answer.ret = handler->state; |
154 | ||
155 | ret = send(fd, &answer, sizeof(answer), 0); | |
156 | if (ret < 0) { | |
157 | WARN("failed to send answer to the peer"); | |
158 | goto out; | |
159 | } | |
160 | ||
161 | if (ret != sizeof(answer)) { | |
162 | ERROR("partial answer sent"); | |
163 | goto out; | |
164 | } | |
165 | ||
166 | out: | |
167 | return ret; | |
168 | } | |
169 | ||
12a50cc6 | 170 | static int fillwaitedstates(const char *strstates, int *states) |
72d0e1cb SG |
171 | { |
172 | char *token, *saveptr = NULL; | |
12a50cc6 | 173 | char *strstates_dup = strdup(strstates); |
72d0e1cb SG |
174 | int state; |
175 | ||
12a50cc6 DE |
176 | if (!strstates_dup) |
177 | return -1; | |
178 | ||
179 | token = strtok_r(strstates_dup, "|", &saveptr); | |
72d0e1cb SG |
180 | while (token) { |
181 | ||
182 | state = lxc_str2state(token); | |
12a50cc6 DE |
183 | if (state < 0) { |
184 | free(strstates_dup); | |
72d0e1cb | 185 | return -1; |
12a50cc6 | 186 | } |
72d0e1cb SG |
187 | |
188 | states[state] = 1; | |
189 | ||
190 | token = strtok_r(NULL, "|", &saveptr); | |
191 | } | |
12a50cc6 | 192 | free(strstates_dup); |
72d0e1cb SG |
193 | return 0; |
194 | } | |
195 | ||
67e571de | 196 | extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath) |
72d0e1cb SG |
197 | { |
198 | struct lxc_msg msg; | |
199 | int state, ret; | |
200 | int s[MAX_STATE] = { }, fd; | |
201 | ||
202 | if (fillwaitedstates(states, s)) | |
203 | return -1; | |
204 | ||
9123e471 | 205 | fd = lxc_monitor_open(lxcpath); |
72d0e1cb SG |
206 | if (fd < 0) |
207 | return -1; | |
208 | ||
209 | /* | |
210 | * if container present, | |
211 | * then check if already in requested state | |
212 | */ | |
213 | ret = -1; | |
13f5be62 | 214 | state = lxc_getstate(lxcname, lxcpath); |
72d0e1cb SG |
215 | if (state < 0) { |
216 | goto out_close; | |
217 | } else if ((state >= 0) && (s[state])) { | |
218 | ret = 0; | |
219 | goto out_close; | |
220 | } | |
221 | ||
222 | for (;;) { | |
8dff643f | 223 | int elapsed_time, curtime = 0; |
72d0e1cb SG |
224 | struct timeval tv; |
225 | int stop = 0; | |
226 | int retval; | |
227 | ||
228 | if (timeout != -1) { | |
229 | retval = gettimeofday(&tv, NULL); | |
230 | if (retval) | |
231 | goto out_close; | |
232 | curtime = tv.tv_sec; | |
233 | } | |
234 | if (lxc_monitor_read_timeout(fd, &msg, timeout) < 0) | |
235 | goto out_close; | |
236 | ||
237 | if (timeout != -1) { | |
238 | retval = gettimeofday(&tv, NULL); | |
239 | if (retval) | |
240 | goto out_close; | |
241 | elapsed_time = tv.tv_sec - curtime; | |
242 | if (timeout - elapsed_time <= 0) | |
243 | stop = 1; | |
244 | timeout -= elapsed_time; | |
245 | } | |
246 | ||
247 | if (strcmp(lxcname, msg.name)) { | |
248 | if (stop) { | |
249 | ret = -2; | |
250 | goto out_close; | |
251 | } | |
252 | continue; | |
253 | } | |
254 | ||
255 | switch (msg.type) { | |
256 | case lxc_msg_state: | |
257 | if (msg.value < 0 || msg.value >= MAX_STATE) { | |
258 | ERROR("Receive an invalid state number '%d'", | |
259 | msg.value); | |
260 | goto out_close; | |
261 | } | |
262 | ||
263 | if (s[msg.value]) { | |
264 | ret = 0; | |
265 | goto out_close; | |
266 | } | |
267 | break; | |
268 | default: | |
269 | if (stop) { | |
270 | ret = -2; | |
271 | goto out_close; | |
272 | } | |
273 | /* just ignore garbage */ | |
274 | break; | |
275 | } | |
276 | } | |
277 | ||
278 | out_close: | |
279 | lxc_monitor_close(fd); | |
280 | return ret; | |
281 | } |