]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/param.h>
12 #include <sys/socket.h>
14 #include <sys/types.h>
20 #include "commands_utils.h"
27 lxc_log_define(state
, lxc
);
29 static const char *const strstate
[] = {
30 "STOPPED", "STARTING", "RUNNING", "STOPPING",
31 "ABORTING", "FREEZING", "FROZEN", "THAWED",
34 const char *lxc_state2str(lxc_state_t state
)
36 if (state
< STOPPED
|| state
> MAX_STATE
- 1)
37 return "INVALID STATE";
38 return strstate
[state
];
41 lxc_state_t
lxc_str2state(const char *state
)
45 len
= sizeof(strstate
) / sizeof(strstate
[0]);
46 for (lxc_state_t i
= 0; i
< len
; i
++) {
47 if (strequal(strstate
[i
], state
))
51 ERROR("invalid state '%s'", state
);
55 lxc_state_t
lxc_getstate(const char *name
, const char *lxcpath
)
57 return lxc_cmd_get_state(name
, lxcpath
);
60 static int fillwaitedstates(const char *strstates
, lxc_state_t
*states
)
66 strstates_dup
= strdup(strstates
);
70 lxc_iterate_parts(token
, strstates_dup
, "|") {
71 state
= lxc_str2state(token
);
83 int lxc_wait(const char *lxcname
, const char *states
, int timeout
,
87 lxc_state_t s
[MAX_STATE
] = {0};
89 if (fillwaitedstates(states
, s
))
93 struct timespec onesec
= {
98 state
= lxc_cmd_sock_get_state(lxcname
, lxcpath
, s
, timeout
);
102 if (errno
!= ECONNREFUSED
)
103 return log_error_errno(-1, errno
, "Failed to receive state from monitor");
111 (void)nanosleep(&onesec
, NULL
);
114 TRACE("Retrieved state of container %s", lxc_state2str(state
));
115 if ((state
< STOPPED
|| state
> MAX_STATE
- 1) || !s
[state
])