]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/state.c
Merge pull request #1615 from 0x0916/expose-lxc_log_init
[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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5e97c3fc 22 */
940ef906
CB
23
24#include <errno.h>
25#include <fcntl.h>
5e97c3fc 26#include <stdio.h>
12a50cc6 27#include <stdlib.h>
0ad19a3f 28#include <string.h>
940ef906 29#include <time.h>
5e97c3fc 30#include <unistd.h>
940ef906 31#include <sys/file.h>
0ad19a3f 32#include <sys/param.h>
940ef906 33#include <sys/socket.h>
0ad19a3f 34#include <sys/stat.h>
940ef906 35#include <sys/types.h>
5e97c3fc 36
f2363e38 37#include "cgroup.h"
e98fe68b 38#include "commands.h"
881450bb 39#include "config.h"
940ef906
CB
40#include "log.h"
41#include "lxc.h"
42#include "monitor.h"
43#include "start.h"
36eb9bde
CLG
44
45lxc_log_define(lxc_state, lxc);
0ad19a3f 46
74a3920a 47static const char * const strstate[] = {
0ad19a3f 48 "STOPPED", "STARTING", "RUNNING", "STOPPING",
fa082227 49 "ABORTING", "FREEZING", "FROZEN", "THAWED",
0ad19a3f 50};
51
52const char *lxc_state2str(lxc_state_t state)
53{
54 if (state < STOPPED || state > MAX_STATE - 1)
55 return NULL;
56 return strstate[state];
57}
5e97c3fc 58
0ad19a3f 59lxc_state_t lxc_str2state(const char *state)
5e97c3fc 60{
e6a19d26
DE
61 size_t len;
62 lxc_state_t i;
0ad19a3f 63 len = sizeof(strstate)/sizeof(strstate[0]);
64 for (i = 0; i < len; i++)
65 if (!strcmp(strstate[i], state))
66 return i;
3ab87b66 67
439358bf 68 ERROR("invalid state '%s'", state);
0ad19a3f 69 return -1;
5e97c3fc 70}
71
13f5be62 72lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
0ad19a3f 73{
4fb3cba5
DE
74 extern lxc_state_t freezer_state(const char *name, const char *lxcpath);
75
e6a19d26 76 lxc_state_t state = freezer_state(name, lxcpath);
0ad19a3f 77 if (state != FROZEN && state != FREEZING)
ef6e34ee 78 state = lxc_cmd_get_state(name, lxcpath);
0ad19a3f 79 return state;
80}
e98fe68b 81
12a50cc6 82static int fillwaitedstates(const char *strstates, int *states)
72d0e1cb
SG
83{
84 char *token, *saveptr = NULL;
12a50cc6 85 char *strstates_dup = strdup(strstates);
72d0e1cb
SG
86 int state;
87
12a50cc6
DE
88 if (!strstates_dup)
89 return -1;
90
91 token = strtok_r(strstates_dup, "|", &saveptr);
72d0e1cb
SG
92 while (token) {
93
94 state = lxc_str2state(token);
12a50cc6
DE
95 if (state < 0) {
96 free(strstates_dup);
72d0e1cb 97 return -1;
12a50cc6 98 }
72d0e1cb
SG
99
100 states[state] = 1;
101
102 token = strtok_r(NULL, "|", &saveptr);
103 }
12a50cc6 104 free(strstates_dup);
72d0e1cb
SG
105 return 0;
106}
107
940ef906
CB
108extern int lxc_wait(const char *lxcname, const char *states, int timeout,
109 const char *lxcpath)
72d0e1cb
SG
110{
111 struct lxc_msg msg;
112 int state, ret;
940ef906 113 int s[MAX_STATE] = {0}, fd;
72d0e1cb
SG
114
115 if (fillwaitedstates(states, s))
116 return -1;
117
f485f377
DE
118 if (lxc_monitord_spawn(lxcpath))
119 return -1;
120
9123e471 121 fd = lxc_monitor_open(lxcpath);
72d0e1cb
SG
122 if (fd < 0)
123 return -1;
124
125 /*
126 * if container present,
127 * then check if already in requested state
128 */
129 ret = -1;
13f5be62 130 state = lxc_getstate(lxcname, lxcpath);
72d0e1cb
SG
131 if (state < 0) {
132 goto out_close;
133 } else if ((state >= 0) && (s[state])) {
134 ret = 0;
135 goto out_close;
136 }
137
138 for (;;) {
940ef906
CB
139 int64_t elapsed_time, curtime = 0;
140 struct timespec tspec;
72d0e1cb
SG
141 int stop = 0;
142 int retval;
143
144 if (timeout != -1) {
940ef906 145 retval = clock_gettime(CLOCK_REALTIME, &tspec);
72d0e1cb
SG
146 if (retval)
147 goto out_close;
940ef906 148 curtime = tspec.tv_sec;
72d0e1cb 149 }
0a9362f5
ÇO
150 if (lxc_monitor_read_timeout(fd, &msg, timeout) < 0) {
151 /* try again if select interrupted by signal */
152 if (errno != EINTR)
153 goto out_close;
154 }
72d0e1cb
SG
155
156 if (timeout != -1) {
940ef906 157 retval = clock_gettime(CLOCK_REALTIME, &tspec);
72d0e1cb
SG
158 if (retval)
159 goto out_close;
940ef906 160 elapsed_time = tspec.tv_sec - curtime;
72d0e1cb
SG
161 if (timeout - elapsed_time <= 0)
162 stop = 1;
163 timeout -= elapsed_time;
164 }
165
166 if (strcmp(lxcname, msg.name)) {
167 if (stop) {
168 ret = -2;
169 goto out_close;
170 }
171 continue;
172 }
173
174 switch (msg.type) {
175 case lxc_msg_state:
940ef906 176 if (msg.value < 0 || msg.value >= MAX_STATE)
72d0e1cb 177 goto out_close;
72d0e1cb
SG
178
179 if (s[msg.value]) {
180 ret = 0;
181 goto out_close;
182 }
183 break;
184 default:
185 if (stop) {
186 ret = -2;
187 goto out_close;
188 }
189 /* just ignore garbage */
190 break;
191 }
192 }
193
194out_close:
195 lxc_monitor_close(fd);
196 return ret;
197}