]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include <sys/param.h>
13 #include <sys/socket.h>
15 #include <sys/types.h>
21 #include "commands_utils.h"
29 lxc_log_define(state
, lxc
);
31 static const char *const strstate
[] = {
32 "STOPPED", "STARTING", "RUNNING", "STOPPING",
33 "ABORTING", "FREEZING", "FROZEN", "THAWED",
36 const char *lxc_state2str(lxc_state_t state
)
38 if (state
< STOPPED
|| state
> MAX_STATE
- 1)
39 return "INVALID STATE";
40 return strstate
[state
];
43 lxc_state_t
lxc_str2state(const char *state
)
47 len
= sizeof(strstate
) / sizeof(strstate
[0]);
48 for (lxc_state_t i
= 0; i
< len
; i
++) {
49 if (strequal(strstate
[i
], state
))
53 ERROR("invalid state '%s'", state
);
57 lxc_state_t
lxc_getstate(const char *name
, const char *lxcpath
)
59 return lxc_cmd_get_state(name
, lxcpath
);
62 static int fillwaitedstates(const char *strstates
, lxc_state_t
*states
)
68 strstates_dup
= strdup(strstates
);
72 lxc_iterate_parts(token
, strstates_dup
, "|") {
73 state
= lxc_str2state(token
);
85 int lxc_wait(const char *lxcname
, const char *states
, int timeout
,
89 lxc_state_t s
[MAX_STATE
] = {0};
91 if (fillwaitedstates(states
, s
))
95 struct timespec onesec
= {
100 state
= lxc_cmd_sock_get_state(lxcname
, lxcpath
, s
, timeout
);
104 if (errno
!= ECONNREFUSED
)
105 return log_error_errno(-1, errno
, "Failed to receive state from monitor");
113 (void)nanosleep(&onesec
, NULL
);
116 TRACE("Retrieved state of container %s", lxc_state2str(state
));