]>
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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
5e97c3fc | 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 | { |
2acf7795 | 72 | char *cgabspath = NULL; |
0ad19a3f | 73 | char freezer[MAXPATHLEN]; |
74 | char status[MAXPATHLEN]; | |
75 | FILE *file; | |
2acf7795 | 76 | int ret; |
35d2c3e7 | 77 | |
2acf7795 DE |
78 | cgabspath = lxc_cgroup_path_get("freezer", name, lxcpath); |
79 | if (!cgabspath) | |
80 | return -1; | |
fa082227 | 81 | |
2acf7795 DE |
82 | ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", cgabspath); |
83 | if (ret < 0 || ret >= MAXPATHLEN) | |
84 | goto out; | |
0ad19a3f | 85 | |
86 | file = fopen(freezer, "r"); | |
2acf7795 DE |
87 | if (!file) { |
88 | ret = -1; | |
89 | goto out; | |
90 | } | |
0ad19a3f | 91 | |
2acf7795 | 92 | ret = fscanf(file, "%s", status); |
0ad19a3f | 93 | fclose(file); |
94 | ||
2acf7795 | 95 | if (ret == EOF) { |
36eb9bde | 96 | SYSERROR("failed to read %s", freezer); |
2acf7795 DE |
97 | ret = -1; |
98 | goto out; | |
0ad19a3f | 99 | } |
100 | ||
2acf7795 | 101 | ret = lxc_str2state(status); |
fd37327f | 102 | |
2acf7795 DE |
103 | out: |
104 | free(cgabspath); | |
105 | return ret; | |
0ad19a3f | 106 | } |
107 | ||
13f5be62 | 108 | lxc_state_t lxc_getstate(const char *name, const char *lxcpath) |
0ad19a3f | 109 | { |
e6a19d26 | 110 | lxc_state_t state = freezer_state(name, lxcpath); |
0ad19a3f | 111 | if (state != FROZEN && state != FREEZING) |
ef6e34ee | 112 | state = lxc_cmd_get_state(name, lxcpath); |
0ad19a3f | 113 | return state; |
114 | } | |
e98fe68b | 115 | |
12a50cc6 | 116 | static int fillwaitedstates(const char *strstates, int *states) |
72d0e1cb SG |
117 | { |
118 | char *token, *saveptr = NULL; | |
12a50cc6 | 119 | char *strstates_dup = strdup(strstates); |
72d0e1cb SG |
120 | int state; |
121 | ||
12a50cc6 DE |
122 | if (!strstates_dup) |
123 | return -1; | |
124 | ||
125 | token = strtok_r(strstates_dup, "|", &saveptr); | |
72d0e1cb SG |
126 | while (token) { |
127 | ||
128 | state = lxc_str2state(token); | |
12a50cc6 DE |
129 | if (state < 0) { |
130 | free(strstates_dup); | |
72d0e1cb | 131 | return -1; |
12a50cc6 | 132 | } |
72d0e1cb SG |
133 | |
134 | states[state] = 1; | |
135 | ||
136 | token = strtok_r(NULL, "|", &saveptr); | |
137 | } | |
12a50cc6 | 138 | free(strstates_dup); |
72d0e1cb SG |
139 | return 0; |
140 | } | |
141 | ||
67e571de | 142 | extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath) |
72d0e1cb SG |
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 | ||
f485f377 DE |
151 | if (lxc_monitord_spawn(lxcpath)) |
152 | return -1; | |
153 | ||
9123e471 | 154 | fd = lxc_monitor_open(lxcpath); |
72d0e1cb SG |
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; | |
13f5be62 | 163 | state = lxc_getstate(lxcname, lxcpath); |
72d0e1cb SG |
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 (;;) { | |
8dff643f | 172 | int elapsed_time, curtime = 0; |
72d0e1cb SG |
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 | } | |
0a9362f5 ÇO |
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 | } | |
72d0e1cb SG |
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 | } |