]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
Merge git://github.com/lxc/lxc
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 *cgabspath = NULL;
73 char freezer[MAXPATHLEN];
74 char status[MAXPATHLEN];
75 FILE *file;
76 int ret;
77
78 cgabspath = lxc_cgroup_path_get("freezer", name, lxcpath);
79 if (!cgabspath)
80 return -1;
81
82 ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", cgabspath);
83 if (ret < 0 || ret >= MAXPATHLEN)
84 goto out;
85
86 file = fopen(freezer, "r");
87 if (!file) {
88 ret = -1;
89 goto out;
90 }
91
92 ret = fscanf(file, "%s", status);
93 fclose(file);
94
95 if (ret == EOF) {
96 SYSERROR("failed to read %s", freezer);
97 ret = -1;
98 goto out;
99 }
100
101 ret = lxc_str2state(status);
102
103 out:
104 free(cgabspath);
105 return ret;
106 }
107
108 lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
109 {
110 lxc_state_t state = freezer_state(name, lxcpath);
111 if (state != FROZEN && state != FREEZING)
112 state = lxc_cmd_get_state(name, lxcpath);
113 return state;
114 }
115
116 static int fillwaitedstates(const char *strstates, int *states)
117 {
118 char *token, *saveptr = NULL;
119 char *strstates_dup = strdup(strstates);
120 int state;
121
122 if (!strstates_dup)
123 return -1;
124
125 token = strtok_r(strstates_dup, "|", &saveptr);
126 while (token) {
127
128 state = lxc_str2state(token);
129 if (state < 0) {
130 free(strstates_dup);
131 return -1;
132 }
133
134 states[state] = 1;
135
136 token = strtok_r(NULL, "|", &saveptr);
137 }
138 free(strstates_dup);
139 return 0;
140 }
141
142 extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath)
143 {
144 struct lxc_msg msg;
145 int state, ret;
146 int s[MAX_STATE] = { }, fd;
147
148 if (fillwaitedstates(states, s))
149 return -1;
150
151 if (lxc_monitord_spawn(lxcpath))
152 return -1;
153
154 fd = lxc_monitor_open(lxcpath);
155 if (fd < 0)
156 return -1;
157
158 /*
159 * if container present,
160 * then check if already in requested state
161 */
162 ret = -1;
163 state = lxc_getstate(lxcname, lxcpath);
164 if (state < 0) {
165 goto out_close;
166 } else if ((state >= 0) && (s[state])) {
167 ret = 0;
168 goto out_close;
169 }
170
171 for (;;) {
172 int elapsed_time, curtime = 0;
173 struct timeval tv;
174 int stop = 0;
175 int retval;
176
177 if (timeout != -1) {
178 retval = gettimeofday(&tv, NULL);
179 if (retval)
180 goto out_close;
181 curtime = tv.tv_sec;
182 }
183 if (lxc_monitor_read_timeout(fd, &msg, timeout) < 0) {
184 /* try again if select interrupted by signal */
185 if (errno != EINTR)
186 goto out_close;
187 }
188
189 if (timeout != -1) {
190 retval = gettimeofday(&tv, NULL);
191 if (retval)
192 goto out_close;
193 elapsed_time = tv.tv_sec - curtime;
194 if (timeout - elapsed_time <= 0)
195 stop = 1;
196 timeout -= elapsed_time;
197 }
198
199 if (strcmp(lxcname, msg.name)) {
200 if (stop) {
201 ret = -2;
202 goto out_close;
203 }
204 continue;
205 }
206
207 switch (msg.type) {
208 case lxc_msg_state:
209 if (msg.value < 0 || msg.value >= MAX_STATE) {
210 ERROR("Receive an invalid state number '%d'",
211 msg.value);
212 goto out_close;
213 }
214
215 if (s[msg.value]) {
216 ret = 0;
217 goto out_close;
218 }
219 break;
220 default:
221 if (stop) {
222 ret = -2;
223 goto out_close;
224 }
225 /* just ignore garbage */
226 break;
227 }
228 }
229
230 out_close:
231 lxc_monitor_close(fd);
232 return ret;
233 }