"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-bind-mount.c" (15 Mar 2019, 3420 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


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. For more information about "stress-bind-mount.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.52_vs_0.09.54.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    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
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #if defined(__linux__) && \
   28     defined(MS_BIND) && \
   29     defined(MS_REC) && \
   30     defined(HAVE_CLONE) && \
   31     defined(CLONE_NEWUSER) && \
   32     defined(CLONE_NEWNS) && \
   33     defined(CLONE_VM)
   34 
   35 #define CLONE_STACK_SIZE    (128*1024)
   36 
   37 static void stress_bind_mount_child_handler(int signum)
   38 {
   39     (void)signum;
   40 
   41     _exit(0);
   42 }
   43 
   44 /*
   45  *  stress_bind_mount_child()
   46  *  aggressively perform bind mounts, this can force out of memory
   47  *  situations
   48  */
   49 static int stress_bind_mount_child(void *parg)
   50 {
   51     const args_t *args = ((pthread_args_t *)parg)->args;
   52 
   53     if (stress_sighandler(args->name, SIGALRM,
   54         stress_bind_mount_child_handler, NULL) < 0) {
   55         pr_fail_err("sighandler SIGALRM");
   56         return EXIT_FAILURE;
   57     }
   58     if (stress_sighandler(args->name, SIGSEGV,
   59         stress_bind_mount_child_handler, NULL) < 0) {
   60         pr_fail_err("sighandler SIGSEGV");
   61         return EXIT_FAILURE;
   62     }
   63     (void)setpgid(0, g_pgrp);
   64     stress_parent_died_alarm();
   65 
   66     do {
   67         int rc;
   68 
   69         rc = mount("/", "/", "", MS_BIND | MS_REC, 0);
   70         if (rc < 0) {
   71             if (errno != ENOSPC)
   72                 pr_fail_err("mount");
   73             break;
   74         }
   75         /*
   76          *  The following fails with -EBUSY, but try it anyhow
   77     `    *  just to make the kernel work harder
   78          */
   79         (void)umount("/");
   80         inc_counter(args);
   81     } while (g_keep_stressing_flag &&
   82          (!args->max_ops || get_counter(args) < args->max_ops));
   83 
   84     return 0;
   85 }
   86 
   87 /*
   88  *  stress_bind_mount()
   89  *      stress bind mounting
   90  */
   91 static int stress_bind_mount(const args_t *args)
   92 {
   93     int pid = 0, status;
   94     pthread_args_t pargs = { args, NULL };
   95     const ssize_t stack_offset =
   96         stress_get_stack_direction() *
   97         (CLONE_STACK_SIZE - 64);
   98 
   99     do {
  100         int ret;
  101         char stack[CLONE_STACK_SIZE];
  102         char *stack_top = stack + stack_offset;
  103 
  104         (void)memset(stack, 0, sizeof stack);
  105 
  106         pid = clone(stress_bind_mount_child,
  107             align_stack(stack_top),
  108             CLONE_NEWUSER | CLONE_NEWNS | CLONE_VM | SIGCHLD,
  109             (void *)&pargs, 0);
  110         if (pid < 0) {
  111             int rc = exit_status(errno);
  112 
  113             pr_fail_err("clone");
  114             return rc;
  115         }
  116         ret = waitpid(pid, &status, 0);
  117         (void)ret;
  118     } while (keep_stressing());
  119 
  120     return EXIT_SUCCESS;
  121 }
  122 
  123 stressor_info_t stress_bind_mount_info = {
  124     .stressor = stress_bind_mount,
  125     .class = CLASS_FILESYSTEM | CLASS_OS | CLASS_PATHOLOGICAL
  126 };
  127 #else
  128 stressor_info_t stress_bind_mount_info = {
  129     .stressor = stress_not_implemented,
  130     .class = CLASS_FILESYSTEM | CLASS_OS | CLASS_PATHOLOGICAL
  131 };
  132 #endif