"Fossies" - the Fresh Open Source Software Archive

Member "lxc-4.0.10/src/tests/api_reboot.c" (16 Jul 2021, 4295 Bytes) of package /linux/misc/lxc-4.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "api_reboot.c": 4.0.9_vs_4.0.10.

    1 /* liblxcapi
    2  *
    3  * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
    4  *
    5  * This program is free software; you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License version 2, as
    7  * published by the Free Software Foundation.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License along
   15  * with this program; if not, write to the Free Software Foundation, Inc.,
   16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   17  */
   18 
   19 #include <alloca.h>
   20 #include <stdio.h>
   21 #include <sched.h>
   22 #include <unistd.h>
   23 #include <signal.h>
   24 #include <errno.h>
   25 #include <string.h>
   26 #include <sys/reboot.h>
   27 #include <sys/types.h>
   28 #include <sys/wait.h>
   29 
   30 #include "lxc/lxccontainer.h"
   31 #include "lxctest.h"
   32 #include "utils.h"
   33 
   34 #ifndef HAVE_STRLCPY
   35 #include "include/strlcpy.h"
   36 #endif
   37 
   38 #define TSTNAME "lxc-api-reboot"
   39 
   40 int main(int argc, char *argv[])
   41 {
   42     int i;
   43     struct lxc_container *c;
   44     int ret = EXIT_FAILURE;
   45     struct lxc_log log;
   46     char template[sizeof(P_tmpdir"/reboot_XXXXXX")];
   47 
   48     (void)strlcpy(template, P_tmpdir"/reboot_XXXXXX", sizeof(template));
   49 
   50     i = lxc_make_tmpfile(template, false);
   51     if (i < 0) {
   52         lxc_error("Failed to create temporary log file for container %s\n", TSTNAME);
   53         exit(EXIT_FAILURE);
   54     } else {
   55         lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, TSTNAME);
   56         close(i);
   57     }
   58 
   59     log.name = TSTNAME;
   60     log.file = template;
   61     log.level = "TRACE";
   62     log.prefix = "reboot";
   63     log.quiet = false;
   64     log.lxcpath = NULL;
   65     if (lxc_log_init(&log))
   66         exit(ret);
   67 
   68     /* Test that the reboot() API function properly waits for containers to
   69      * restart.
   70      */
   71     c = lxc_container_new(TSTNAME, NULL);
   72     if (!c) {
   73         lxc_error("%s", "Failed to create container \"reboot\"");
   74         exit(ret);
   75     }
   76 
   77     if (c->is_defined(c)) {
   78         lxc_error("%s\n", "Container \"reboot\" is defined");
   79         goto on_error_put;
   80     }
   81 
   82     if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
   83         lxc_error("%s\n", "Failed to create busybox container \"reboot\"");
   84         goto on_error_put;
   85     }
   86 
   87     if (!c->is_defined(c)) {
   88         lxc_error("%s\n", "Container \"reboot\" is not defined");
   89         goto on_error_put;
   90     }
   91 
   92     c->clear_config(c);
   93 
   94     if (!c->load_config(c, NULL)) {
   95         lxc_error("%s\n", "Failed to load config for container \"reboot\"");
   96         goto on_error_stop;
   97     }
   98 
   99     if (!c->want_daemonize(c, true)) {
  100         lxc_error("%s\n", "Failed to mark container \"reboot\" daemonized");
  101         goto on_error_stop;
  102     }
  103 
  104     if (!c->startl(c, 0, NULL)) {
  105         lxc_error("%s\n", "Failed to start container \"reboot\" daemonized");
  106         goto on_error_stop;
  107     }
  108 
  109     /* reboot 10 times */
  110     for (i = 0; i < 10; i++) {
  111         /* Give the init system some time to setup it's signal handlers
  112          * otherwise we will wait indefinitely.
  113          */
  114         sleep(5);
  115 
  116         if (!c->reboot2(c, 60 * 5)) {
  117             lxc_error("%s\n", "Failed to reboot container \"reboot\"");
  118             goto on_error_stop;
  119         }
  120 
  121         if (!c->is_running(c)) {
  122             lxc_error("%s\n", "Failed to reboot container \"reboot\"");
  123             goto on_error_stop;
  124         }
  125         lxc_debug("%s\n", "Container \"reboot\" rebooted successfully");
  126     }
  127 
  128     /* Give the init system some time to setup it's signal handlers
  129      * otherwise we will wait indefinitely.
  130      */
  131     sleep(5);
  132 
  133     /* Test non-blocking reboot2() */
  134     if (!c->reboot2(c, 0)) {
  135         lxc_error("%s\n", "Failed to request non-blocking reboot of container \"reboot\"");
  136         goto on_error_stop;
  137     }
  138     lxc_debug("%s\n", "Non-blocking reboot of container \"reboot\" succeeded");
  139 
  140     ret = EXIT_SUCCESS;
  141 
  142 on_error_stop:
  143     if (c->is_running(c) && !c->stop(c))
  144         lxc_error("%s\n", "Failed to stop container \"reboot\"");
  145 
  146     if (!c->destroy(c))
  147         lxc_error("%s\n", "Failed to destroy container \"reboot\"");
  148 
  149 on_error_put:
  150     lxc_container_put(c);
  151 
  152     if (ret == EXIT_SUCCESS) {
  153         lxc_debug("%s\n", "All reboot tests passed");
  154     } else {
  155         int fd;
  156 
  157         fd = open(template, O_RDONLY);
  158         if (fd >= 0) {
  159             char buf[4096];
  160             ssize_t buflen;
  161             while ((buflen = read(fd, buf, 1024)) > 0) {
  162                 buflen = write(STDERR_FILENO, buf, buflen);
  163                 if (buflen <= 0)
  164                     break;
  165             }
  166             close(fd);
  167         }
  168     }
  169     (void)unlink(template);
  170 
  171     exit(ret);
  172 }