-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <dlezcano at fr.ibm.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/types.h>
+#include <sys/file.h>
#include <sys/param.h>
+#include <sys/socket.h>
#include <sys/stat.h>
-#include <sys/file.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
-#include <lxc/lxc.h>
-#include <lxc/log.h>
+#include "cgroup.h"
#include "commands.h"
-
-lxc_log_define(lxc_state, lxc);
-
-static char *strstate[] = {
- "STOPPED", "STARTING", "RUNNING", "STOPPING",
- "ABORTING", "FREEZING", "FROZEN",
+#include "commands_utils.h"
+#include "log.h"
+#include "lxc.h"
+#include "monitor.h"
+#include "start.h"
+#include "utils.h"
+
+lxc_log_define(state, lxc);
+
+static const char *const strstate[] = {
+ "STOPPED", "STARTING", "RUNNING", "STOPPING",
+ "ABORTING", "FREEZING", "FROZEN", "THAWED",
};
const char *lxc_state2str(lxc_state_t state)
{
if (state < STOPPED || state > MAX_STATE - 1)
- return NULL;
+ return "INVALID STATE";
return strstate[state];
}
lxc_state_t lxc_str2state(const char *state)
{
- int i, len;
- len = sizeof(strstate)/sizeof(strstate[0]);
- for (i = 0; i < len; i++)
- if (!strcmp(strstate[i], state))
+ size_t len;
+
+ len = sizeof(strstate) / sizeof(strstate[0]);
+ for (lxc_state_t i = 0; i < len; i++) {
+ if (strequal(strstate[i], state))
return i;
+ }
ERROR("invalid state '%s'", state);
return -1;
}
-int lxc_rmstate(const char *name)
+lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
{
- char file[MAXPATHLEN];
- snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
- unlink(file);
- return 0;
+ return lxc_cmd_get_state(name, lxcpath);
}
-static int freezer_state(const char *name)
+static int fillwaitedstates(const char *strstates, lxc_state_t *states)
{
- char freezer[MAXPATHLEN];
- char status[MAXPATHLEN];
- FILE *file;
- int err;
-
- snprintf(freezer, MAXPATHLEN,
- LXCPATH "/%s/freezer.state", name);
+ char *token;
+ char *strstates_dup;
+ int state;
- file = fopen(freezer, "r");
- if (!file)
+ strstates_dup = strdup(strstates);
+ if (!strstates_dup)
return -1;
- err = fscanf(file, "%s", status);
- fclose(file);
+ lxc_iterate_parts(token, strstates_dup, "|") {
+ state = lxc_str2state(token);
+ if (state < 0) {
+ free(strstates_dup);
+ return -1;
+ }
- if (err == EOF) {
- SYSERROR("failed to read %s", freezer);
- return -1;
+ states[state] = 1;
}
-
- return lxc_str2state(status);
+ free(strstates_dup);
+ return 0;
}
-lxc_state_t lxc_getstate(const char *name)
+int lxc_wait(const char *lxcname, const char *states, int timeout,
+ const char *lxcpath)
{
- struct lxc_command command = {
- .request = { .type = LXC_COMMAND_STATE },
- };
+ int state = -1;
+ lxc_state_t s[MAX_STATE] = {0};
- int ret;
-
- ret = lxc_command(name, &command);
- if (ret < 0) {
- ERROR("failed to send command");
+ if (fillwaitedstates(states, s))
return -1;
- }
- if (!ret) {
- WARN("'%s' has stopped before sending its state", name);
- return -1;
- }
+ for (;;) {
+ struct timespec onesec = {
+ .tv_sec = 1,
+ .tv_nsec = 0,
+ };
- if (command.answer.ret < 0) {
- ERROR("failed to get state for '%s': %s",
- name, strerror(-command.answer.ret));
- return -1;
- }
+ state = lxc_cmd_sock_get_state(lxcname, lxcpath, s, timeout);
+ if (state >= 0)
+ break;
- DEBUG("'%s' is in '%s' state", name, lxc_state2str(command.answer.ret));
+ if (errno != ECONNREFUSED)
+ return log_error_errno(-1, errno, "Failed to receive state from monitor");
- return command.answer.ret;
-}
+ if (timeout > 0)
+ timeout--;
-lxc_state_t lxc_state(const char *name)
-{
- int state = freezer_state(name);
- if (state != FROZEN && state != FREEZING)
- state = lxc_getstate(name);
- return state;
-}
-
-/*----------------------------------------------------------------------------
- * functions used by lxc-start mainloop
- * to handle above command request.
- *--------------------------------------------------------------------------*/
-extern int lxc_state_callback(int fd, struct lxc_request *request,
- struct lxc_handler *handler)
-{
- struct lxc_answer answer;
- int ret;
-
- answer.ret = handler->state;
+ if (timeout == 0)
+ return -1;
- ret = send(fd, &answer, sizeof(answer), 0);
- if (ret < 0) {
- WARN("failed to send answer to the peer");
- goto out;
+ (void)nanosleep(&onesec, NULL);
}
- if (ret != sizeof(answer)) {
- ERROR("partial answer sent");
- goto out;
- }
+ TRACE("Retrieved state of container %s", lxc_state2str(state));
+ if ((state < STOPPED || state > MAX_STATE - 1) || !s[state])
+ return -1;
-out:
- return ret;
+ return 0;
}
-