]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
extend command processor to handle generic data
[mirror_lxc.git] / src / lxc / state.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/file.h>
34
35 #include <lxc/lxc.h>
36 #include <lxc/log.h>
37 #include <lxc/start.h>
38 #include <lxc/cgroup.h>
39 #include <lxc/monitor.h>
40 #include "commands.h"
41 #include "config.h"
42
43 lxc_log_define(lxc_state, lxc);
44
45 static char *strstate[] = {
46 "STOPPED", "STARTING", "RUNNING", "STOPPING",
47 "ABORTING", "FREEZING", "FROZEN", "THAWED",
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 }
56
57 lxc_state_t lxc_str2state(const char *state)
58 {
59 size_t len;
60 lxc_state_t i;
61 len = sizeof(strstate)/sizeof(strstate[0]);
62 for (i = 0; i < len; i++)
63 if (!strcmp(strstate[i], state))
64 return i;
65
66 ERROR("invalid state '%s'", state);
67 return -1;
68 }
69
70 static lxc_state_t freezer_state(const char *name, const char *lxcpath)
71 {
72 char *nsgroup = NULL;
73 char freezer[MAXPATHLEN];
74 char status[MAXPATHLEN];
75 FILE *file;
76 int err;
77
78 err = lxc_cgroup_path_get(&nsgroup, "freezer", name, lxcpath);
79 if (err)
80 goto fail;
81
82 err = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", nsgroup);
83 if (err < 0 || err >= MAXPATHLEN)
84 goto fail;
85
86 file = fopen(freezer, "r");
87 if (!file)
88 goto fail;
89
90 err = fscanf(file, "%s", status);
91 fclose(file);
92
93 if (err == EOF) {
94 SYSERROR("failed to read %s", freezer);
95 goto fail;
96 }
97
98 return lxc_str2state(status);
99
100 fail:
101 if (nsgroup)
102 free(nsgroup);
103 return -1;
104 }
105
106 lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
107 {
108 lxc_state_t state = freezer_state(name, lxcpath);
109 if (state != FROZEN && state != FREEZING)
110 state = lxc_cmd_get_state(name, lxcpath);
111 return state;
112 }
113
114 static int fillwaitedstates(const char *strstates, int *states)
115 {
116 char *token, *saveptr = NULL;
117 char *strstates_dup = strdup(strstates);
118 int state;
119
120 if (!strstates_dup)
121 return -1;
122
123 token = strtok_r(strstates_dup, "|", &saveptr);
124 while (token) {
125
126 state = lxc_str2state(token);
127 if (state < 0) {
128 free(strstates_dup);
129 return -1;
130 }
131
132 states[state] = 1;
133
134 token = strtok_r(NULL, "|", &saveptr);
135 }
136 free(strstates_dup);
137 return 0;
138 }
139
140 extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath)
141 {
142 struct lxc_msg msg;
143 int state, ret;
144 int s[MAX_STATE] = { }, fd;
145
146 if (fillwaitedstates(states, s))
147 return -1;
148
149 if (lxc_monitord_spawn(lxcpath))
150 return -1;
151
152 fd = lxc_monitor_open(lxcpath);
153 if (fd < 0)
154 return -1;
155
156 /*
157 * if container present,
158 * then check if already in requested state
159 */
160 ret = -1;
161 state = lxc_getstate(lxcname, lxcpath);
162 if (state < 0) {
163 goto out_close;
164 } else if ((state >= 0) && (s[state])) {
165 ret = 0;
166 goto out_close;
167 }
168
169 for (;;) {
170 int elapsed_time, curtime = 0;
171 struct timeval tv;
172 int stop = 0;
173 int retval;
174
175 if (timeout != -1) {
176 retval = gettimeofday(&tv, NULL);
177 if (retval)
178 goto out_close;
179 curtime = tv.tv_sec;
180 }
181 if (lxc_monitor_read_timeout(fd, &msg, timeout) < 0) {
182 /* try again if select interrupted by signal */
183 if (errno != EINTR)
184 goto out_close;
185 }
186
187 if (timeout != -1) {
188 retval = gettimeofday(&tv, NULL);
189 if (retval)
190 goto out_close;
191 elapsed_time = tv.tv_sec - curtime;
192 if (timeout - elapsed_time <= 0)
193 stop = 1;
194 timeout -= elapsed_time;
195 }
196
197 if (strcmp(lxcname, msg.name)) {
198 if (stop) {
199 ret = -2;
200 goto out_close;
201 }
202 continue;
203 }
204
205 switch (msg.type) {
206 case lxc_msg_state:
207 if (msg.value < 0 || msg.value >= MAX_STATE) {
208 ERROR("Receive an invalid state number '%d'",
209 msg.value);
210 goto out_close;
211 }
212
213 if (s[msg.value]) {
214 ret = 0;
215 goto out_close;
216 }
217 break;
218 default:
219 if (stop) {
220 ret = -2;
221 goto out_close;
222 }
223 /* just ignore garbage */
224 break;
225 }
226 }
227
228 out_close:
229 lxc_monitor_close(fd);
230 return ret;
231 }