]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/state.c
state: additional check in lxc_wait to prevent OOB
[mirror_lxc.git] / src / lxc / state.c
index 31c6590ab9665316aa7585060efc35873d0053ac..b4de0ea50b70755ed4a420be24fcfdbf30b3d611 100644 (file)
@@ -1,69 +1,52 @@
-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <daniel.lezcano at free.fr>
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <time.h>
-#include <unistd.h>
 #include <sys/file.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
 
 #include "cgroup.h"
 #include "commands.h"
-#include "config.h"
+#include "commands_utils.h"
 #include "log.h"
 #include "lxc.h"
 #include "monitor.h"
 #include "start.h"
+#include "utils.h"
 
-lxc_log_define(lxc_state, lxc);
+lxc_log_define(state, lxc);
 
-static const char * const strstate[] = {
-       "STOPPED", "STARTING", "RUNNING", "STOPPING",
-       "ABORTING", "FREEZING", "FROZEN", "THAWED",
+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)
 {
        size_t len;
-       lxc_state_t i;
-       len = sizeof(strstate)/sizeof(strstate[0]);
-       for (i = 0; i < len; i++)
-               if (!strcmp(strstate[i], state))
+
+       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;
@@ -71,26 +54,20 @@ lxc_state_t lxc_str2state(const char *state)
 
 lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
 {
-       extern lxc_state_t freezer_state(const char *name, const char *lxcpath);
-
-       lxc_state_t state = freezer_state(name, lxcpath);
-       if (state != FROZEN && state != FREEZING)
-               state = lxc_cmd_get_state(name, lxcpath);
-       return state;
+       return lxc_cmd_get_state(name, lxcpath);
 }
 
 static int fillwaitedstates(const char *strstates, lxc_state_t *states)
 {
-       char *token, *saveptr = NULL;
-       char *strstates_dup = strdup(strstates);
+       char *token;
+       char *strstates_dup;
        int state;
 
+       strstates_dup = strdup(strstates);
        if (!strstates_dup)
                return -1;
 
-       token = strtok_r(strstates_dup, "|", &saveptr);
-       while (token) {
-
+       lxc_iterate_parts(token, strstates_dup, "|") {
                state = lxc_str2state(token);
                if (state < 0) {
                        free(strstates_dup);
@@ -98,30 +75,44 @@ static int fillwaitedstates(const char *strstates, lxc_state_t *states)
                }
 
                states[state] = 1;
-
-               token = strtok_r(NULL, "|", &saveptr);
        }
        free(strstates_dup);
        return 0;
 }
 
-extern int lxc_wait(const char *lxcname, const char *states, int timeout,
-                   const char *lxcpath)
+int lxc_wait(const char *lxcname, const char *states, int timeout,
+            const char *lxcpath)
 {
-       int state;
+       int state = -1;
        lxc_state_t s[MAX_STATE] = {0};
 
        if (fillwaitedstates(states, s))
                return -1;
 
-       state = lxc_cmd_state_server(lxcname, lxcpath, s);
-       if (state < 0) {
-               SYSERROR("failed to receive state from monitor");
-               return -1;
+       for (;;) {
+               struct timespec onesec = {
+                   .tv_sec = 1,
+                   .tv_nsec = 0,
+               };
+
+               state = lxc_cmd_sock_get_state(lxcname, lxcpath, s, timeout);
+               if (state >= 0)
+                       break;
+
+               if (errno != ECONNREFUSED)
+                       return log_error_errno(-1, errno, "Failed to receive state from monitor");
+
+               if (timeout > 0)
+                       timeout--;
+
+               if (timeout == 0)
+                       return -1;
+
+               (void)nanosleep(&onesec, NULL);
        }
 
-       TRACE("retrieved state of container %s", lxc_state2str(state));
-       if (!s[state])
+       TRACE("Retrieved state of container %s", lxc_state2str(state));
+       if ((state < STOPPED || state > MAX_STATE - 1) || !s[state])
                return -1;
 
        return 0;