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