]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/state.c
coverity: #1426130
[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 23
d38dd64a
CB
24#ifndef _GNU_SOURCE
25#define _GNU_SOURCE 1
26#endif
940ef906
CB
27#include <errno.h>
28#include <fcntl.h>
5e97c3fc 29#include <stdio.h>
12a50cc6 30#include <stdlib.h>
0ad19a3f 31#include <string.h>
940ef906 32#include <sys/file.h>
0ad19a3f 33#include <sys/param.h>
940ef906 34#include <sys/socket.h>
0ad19a3f 35#include <sys/stat.h>
940ef906 36#include <sys/types.h>
d38dd64a
CB
37#include <time.h>
38#include <unistd.h>
5e97c3fc 39
f2363e38 40#include "cgroup.h"
e98fe68b 41#include "commands.h"
92e35018 42#include "commands_utils.h"
881450bb 43#include "config.h"
940ef906
CB
44#include "log.h"
45#include "lxc.h"
46#include "monitor.h"
47#include "start.h"
89aca5a5 48#include "utils.h"
36eb9bde 49
ac2cecc4 50lxc_log_define(state, lxc);
0ad19a3f 51
d39b10eb
CB
52static const char *const strstate[] = {
53 "STOPPED", "STARTING", "RUNNING", "STOPPING",
54 "ABORTING", "FREEZING", "FROZEN", "THAWED",
0ad19a3f 55};
56
57const char *lxc_state2str(lxc_state_t state)
58{
59 if (state < STOPPED || state > MAX_STATE - 1)
60 return NULL;
61 return strstate[state];
62}
5e97c3fc 63
0ad19a3f 64lxc_state_t lxc_str2state(const char *state)
5e97c3fc 65{
e6a19d26
DE
66 size_t len;
67 lxc_state_t i;
0ad19a3f 68 len = sizeof(strstate)/sizeof(strstate[0]);
69 for (i = 0; i < len; i++)
70 if (!strcmp(strstate[i], state))
71 return i;
3ab87b66 72
439358bf 73 ERROR("invalid state '%s'", state);
0ad19a3f 74 return -1;
5e97c3fc 75}
76
13f5be62 77lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
0ad19a3f 78{
dd66700c 79 return lxc_cmd_get_state(name, lxcpath);
0ad19a3f 80}
e98fe68b 81
dbc9832d 82static int fillwaitedstates(const char *strstates, lxc_state_t *states)
72d0e1cb 83{
89aca5a5
CB
84 char *token;
85 char *strstates_dup;
72d0e1cb
SG
86 int state;
87
89aca5a5 88 strstates_dup = strdup(strstates);
12a50cc6
DE
89 if (!strstates_dup)
90 return -1;
91
89aca5a5 92 lxc_iterate_parts(token, strstates_dup, "|") {
72d0e1cb 93 state = lxc_str2state(token);
12a50cc6
DE
94 if (state < 0) {
95 free(strstates_dup);
72d0e1cb 96 return -1;
12a50cc6 97 }
72d0e1cb
SG
98
99 states[state] = 1;
72d0e1cb 100 }
12a50cc6 101 free(strstates_dup);
72d0e1cb
SG
102 return 0;
103}
104
746559f4
CB
105int lxc_wait(const char *lxcname, const char *states, int timeout,
106 const char *lxcpath)
72d0e1cb 107{
b695eea2 108 int state = -1;
dbc9832d 109 lxc_state_t s[MAX_STATE] = {0};
72d0e1cb
SG
110
111 if (fillwaitedstates(states, s))
112 return -1;
113
f577e061 114 for (;;) {
746559f4
CB
115 struct timespec onesec = {
116 .tv_sec = 1,
117 .tv_nsec = 0,
118 };
119
f577e061
CB
120 state = lxc_cmd_sock_get_state(lxcname, lxcpath, s, timeout);
121 if (state >= 0)
122 break;
123
124 if (errno != ECONNREFUSED) {
125 SYSERROR("Failed to receive state from monitor");
126 return -1;
127 }
128
f577e061
CB
129 if (timeout > 0)
130 timeout--;
131
132 if (timeout == 0)
133 return -1;
974a8aba 134
746559f4 135 (void)nanosleep(&onesec, NULL);
dbc9832d 136 }
aa460476 137
f577e061 138 TRACE("Retrieved state of container %s", lxc_state2str(state));
dbc9832d 139 if (!s[state])
aa460476
CB
140 return -1;
141
dbc9832d 142 return 0;
72d0e1cb 143}