]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
github: Update for main branch
[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
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/file.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39
40 #include "cgroup.h"
41 #include "commands.h"
42 #include "commands_utils.h"
43 #include "config.h"
44 #include "log.h"
45 #include "lxc.h"
46 #include "monitor.h"
47 #include "start.h"
48 #include "utils.h"
49
50 lxc_log_define(state, lxc);
51
52 static const char *const strstate[] = {
53 "STOPPED", "STARTING", "RUNNING", "STOPPING",
54 "ABORTING", "FREEZING", "FROZEN", "THAWED",
55 };
56
57 const char *lxc_state2str(lxc_state_t state)
58 {
59 if (state < STOPPED || state > MAX_STATE - 1)
60 return NULL;
61 return strstate[state];
62 }
63
64 lxc_state_t lxc_str2state(const char *state)
65 {
66 size_t len;
67 lxc_state_t i;
68 len = sizeof(strstate)/sizeof(strstate[0]);
69 for (i = 0; i < len; i++)
70 if (!strcmp(strstate[i], state))
71 return i;
72
73 ERROR("invalid state '%s'", state);
74 return -1;
75 }
76
77 lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
78 {
79 return lxc_cmd_get_state(name, lxcpath);
80 }
81
82 static int fillwaitedstates(const char *strstates, lxc_state_t *states)
83 {
84 char *token;
85 char *strstates_dup;
86 int state;
87
88 strstates_dup = strdup(strstates);
89 if (!strstates_dup)
90 return -1;
91
92 lxc_iterate_parts(token, strstates_dup, "|") {
93 state = lxc_str2state(token);
94 if (state < 0) {
95 free(strstates_dup);
96 return -1;
97 }
98
99 states[state] = 1;
100 }
101 free(strstates_dup);
102 return 0;
103 }
104
105 int lxc_wait(const char *lxcname, const char *states, int timeout,
106 const char *lxcpath)
107 {
108 int state = -1;
109 lxc_state_t s[MAX_STATE] = {0};
110
111 if (fillwaitedstates(states, s))
112 return -1;
113
114 for (;;) {
115 struct timespec onesec = {
116 .tv_sec = 1,
117 .tv_nsec = 0,
118 };
119
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
129 if (timeout > 0)
130 timeout--;
131
132 if (timeout == 0)
133 return -1;
134
135 (void)nanosleep(&onesec, NULL);
136 }
137
138 TRACE("Retrieved state of container %s", lxc_state2str(state));
139 if (!s[state])
140 return -1;
141
142 return 0;
143 }