"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-mknod.c" (15 Mar 2019, 3071 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-mknod.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 
   29 static const int modes[] = {
   30 #if defined(S_IFIFO)
   31     S_IFIFO,    /* FIFO */
   32 #endif
   33 #if defined(S_IFREG)
   34     S_IFREG,    /* Regular file */
   35 #endif
   36 #if defined(S_IFSOCK)
   37     S_IFSOCK    /* named socket */
   38 #endif
   39 };
   40 
   41 /*
   42  *  stress_mknod_tidy()
   43  *  remove all files
   44  */
   45 static void stress_mknod_tidy(
   46     const args_t *args,
   47     const uint64_t n)
   48 {
   49     uint64_t i;
   50 
   51     for (i = 0; i < n; i++) {
   52         char path[PATH_MAX];
   53         const uint64_t gray_code = (i >> 1) ^ i;
   54 
   55         (void)stress_temp_filename_args(args,
   56             path, sizeof(path), gray_code);
   57         (void)unlink(path);
   58     }
   59 }
   60 
   61 /*
   62  *  stress_mknod
   63  *  stress mknod creates
   64  */
   65 static int stress_mknod(const args_t *args)
   66 {
   67     const size_t num_nodes = SIZEOF_ARRAY(modes);
   68     int ret;
   69 
   70     if (num_nodes == 0) {
   71         pr_err("%s: aborting, no valid mknod modes.\n",
   72             args->name);
   73         return EXIT_FAILURE;
   74     }
   75     ret = stress_temp_dir_mk_args(args);
   76     if (ret < 0)
   77         return exit_status(-ret);
   78 
   79     do {
   80         uint64_t i, n = DEFAULT_DIRS;
   81 
   82         for (i = 0; i < n; i++) {
   83             char path[PATH_MAX];
   84             const uint64_t gray_code = (i >> 1) ^ i;
   85             int mode = modes[mwc32() % num_nodes];
   86 
   87             (void)stress_temp_filename_args(args,
   88                 path, sizeof(path), gray_code);
   89             if (mknod(path, mode | S_IRUSR | S_IWUSR, 0) < 0) {
   90                 if ((errno == ENOSPC) || (errno == ENOMEM))
   91                     continue;   /* Try again */
   92                 pr_fail_err("mknod");
   93                 n = i;
   94                 break;
   95             }
   96 
   97             if (!keep_stressing())
   98                 goto abort;
   99 
  100             inc_counter(args);
  101         }
  102         stress_mknod_tidy(args, n);
  103         if (!g_keep_stressing_flag)
  104             break;
  105         (void)sync();
  106     } while (keep_stressing());
  107 
  108 abort:
  109     /* force unlink of all files */
  110     pr_tidy("%s: removing %" PRIu32 " nodes\n", args->name, DEFAULT_DIRS);
  111     stress_mknod_tidy(args, DEFAULT_DIRS);
  112     (void)stress_temp_dir_rm_args(args);
  113 
  114     return EXIT_SUCCESS;
  115 }
  116 
  117 stressor_info_t stress_mknod_info = {
  118     .stressor = stress_mknod,
  119     .class = CLASS_FILESYSTEM | CLASS_OS
  120 };
  121 #else
  122 stressor_info_t stress_mknod_info = {
  123         .stressor = stress_not_implemented,
  124         .class = CLASS_FILESYSTEM | CLASS_OS
  125 };
  126 #endif