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