"Fossies" - the Fresh Open Source Software Archive

Member "lxc-4.0.10/src/tests/sys_mixed.c" (16 Jul 2021, 3580 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.

    1 /* liblxcapi
    2  *
    3  * Copyright © 2021 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 "config.h"
   20 
   21 #define __STDC_FORMAT_MACROS
   22 
   23 #include <errno.h>
   24 #include <fcntl.h>
   25 #include <inttypes.h>
   26 #include <signal.h>
   27 #include <stdio.h>
   28 #include <stdlib.h>
   29 #include <string.h>
   30 #include <unistd.h>
   31 #include <sys/stat.h>
   32 #include <sys/types.h>
   33 #include <sys/wait.h>
   34 
   35 #include <lxc/lxccontainer.h>
   36 #include <lxc/attach_options.h>
   37 
   38 #ifdef HAVE_STATVFS
   39 #include <sys/statvfs.h>
   40 #endif
   41 
   42 #include "lxctest.h"
   43 #include "utils.h"
   44 
   45 static int is_read_only(const char *path)
   46 {
   47 #ifdef HAVE_STATVFS
   48     int ret;
   49     struct statvfs sb;
   50 
   51     ret = statvfs(path, &sb);
   52     if (ret < 0)
   53         return -errno;
   54 
   55     return (sb.f_flag & MS_RDONLY) > 0;
   56 #else
   57     return -EOPNOTSUPP;
   58 #endif
   59 }
   60 
   61 static int sys_mixed(void *payload)
   62 {
   63     int ret;
   64 
   65     ret = is_read_only("/sys");
   66     if (ret == -EOPNOTSUPP)
   67         return 0;
   68 
   69     if (ret <= 0)
   70         return -1;
   71 
   72     if (is_read_only("/sys/devices/virtual/net"))
   73         return -1;
   74 
   75     return 0;
   76 }
   77 
   78 int main(int argc, char *argv[])
   79 {
   80     int fret = EXIT_FAILURE;
   81     lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
   82     int ret;
   83     pid_t pid;
   84     struct lxc_container *c;
   85 
   86     c = lxc_container_new("sys-mixed", NULL);
   87     if (!c) {
   88         lxc_error("%s", "Failed to create container \"sys-mixed\"");
   89         exit(fret);
   90     }
   91 
   92     if (c->is_defined(c)) {
   93         lxc_error("%s\n", "Container \"sys-mixed\" is defined");
   94         goto on_error_put;
   95     }
   96 
   97     if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
   98         lxc_error("%s\n", "Failed to create busybox container \"sys-mixed\"");
   99         goto on_error_put;
  100     }
  101 
  102     if (!c->is_defined(c)) {
  103         lxc_error("%s\n", "Container \"sys-mixed\" is not defined");
  104         goto on_error_put;
  105     }
  106 
  107     c->clear_config(c);
  108 
  109     if (!c->set_config_item(c, "lxc.mount.auto", "sys:mixed")) {
  110         lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=sys:mixed\"");
  111         goto on_error_put;
  112     }
  113 
  114     if (!c->load_config(c, NULL)) {
  115         lxc_error("%s\n", "Failed to load config for container \"sys-mixed\"");
  116         goto on_error_stop;
  117     }
  118 
  119     if (!c->want_daemonize(c, true)) {
  120         lxc_error("%s\n", "Failed to mark container \"sys-mixed\" daemonized");
  121         goto on_error_stop;
  122     }
  123 
  124     if (!c->startl(c, 0, NULL)) {
  125         lxc_error("%s\n", "Failed to start container \"sys-mixed\" daemonized");
  126         goto on_error_stop;
  127     }
  128 
  129     /* Leave some time for the container to write something to the log. */
  130     sleep(2);
  131 
  132     ret = c->attach(c, sys_mixed, NULL, &attach_options, &pid);
  133     if (ret < 0) {
  134         lxc_error("%s\n", "Failed to run function in container \"sys-mixed\"");
  135         goto on_error_stop;
  136     }
  137 
  138     ret = wait_for_pid(pid);
  139     if (ret < 0) {
  140         lxc_error("%s\n", "Failed to run function in container \"sys-mixed\"");
  141         goto on_error_stop;
  142     }
  143 
  144     fret = 0;
  145 
  146 on_error_stop:
  147     if (c->is_running(c) && !c->stop(c))
  148         lxc_error("%s\n", "Failed to stop container \"sys-mixed\"");
  149 
  150     if (!c->destroy(c))
  151         lxc_error("%s\n", "Failed to destroy container \"sys-mixed\"");
  152 
  153 on_error_put:
  154     lxc_container_put(c);
  155     exit(fret);
  156 }