"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-chdir.c" (15 Mar 2019, 3676 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-chdir.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.49_vs_0.09.50.

    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 /*
   28  *  stress_set_chdir_dirs()
   29  *  set number of chdir directories from given option string
   30  */
   31 int stress_set_chdir_dirs(const char *opt)
   32 {
   33     uint64_t chdir_dirs;
   34 
   35     chdir_dirs = get_uint64(opt);
   36     check_range("chdir-dirs", chdir_dirs,
   37         MIN_CHDIR_DIRS, MAX_CHDIR_DIRS);
   38     return set_setting("chdir-dirs", TYPE_ID_UINT64, &chdir_dirs);
   39 }
   40 
   41 /*
   42  *  stress_chdir
   43  *  stress chdir calls
   44  */
   45 static int stress_chdir(const args_t *args)
   46 {
   47     uint64_t i, chdir_dirs = DEFAULT_CHDIR_DIRS;
   48     char path[PATH_MAX], cwd[PATH_MAX];
   49     int fd = -1, rc, ret = EXIT_FAILURE;
   50     char **paths;
   51 
   52     (void)get_setting("chdir-dirs", &chdir_dirs);
   53     paths = calloc(chdir_dirs, sizeof(*paths));
   54     if (!paths) {
   55         pr_err("%s: out of memory allocating paths\n", args->name);
   56         return EXIT_NO_RESOURCE;
   57     }
   58 
   59     if (getcwd(cwd, sizeof(cwd)) == NULL) {
   60         pr_fail_err("getcwd");
   61         goto err;
   62     }
   63 
   64     rc = stress_temp_dir_mk_args(args);
   65     if (rc < 0) {
   66         ret = exit_status(-rc);
   67         goto err;
   68     }
   69 
   70     /* Populate */
   71     for (i = 0; i < chdir_dirs; i++) {
   72         uint64_t gray_code = (i >> 1) ^ i;
   73 
   74         (void)stress_temp_filename_args(args,
   75             path, sizeof(path), gray_code);
   76         paths[i] = strdup(path);
   77         if (paths[i] == NULL)
   78             goto abort;
   79         rc = mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
   80         if (rc < 0) {
   81             ret = exit_status(errno);
   82             pr_fail_err("mkdir");
   83             goto abort;
   84         }
   85         if (!g_keep_stressing_flag)
   86             goto done;
   87     }
   88 
   89 #if defined(O_DIRECTORY)
   90     fd = open(paths[0], O_DIRECTORY | O_RDONLY);
   91 #else
   92     fd = open(paths[0], O_RDONLY);
   93 #endif
   94 
   95     do {
   96         for (i = 0; i < chdir_dirs; i++) {
   97             if (!keep_stressing())
   98                 goto done;
   99             if (chdir(paths[i]) < 0) {
  100                 if (errno != ENOMEM) {
  101                     pr_fail_err("chdir");
  102                     goto abort;
  103                 }
  104             }
  105 
  106 
  107             if ((fd >= 0) && (fchdir(fd) < 0)) {
  108                 if (errno != ENOMEM) {
  109                     pr_fail_err("fchdir");
  110                     goto abort;
  111                 }
  112             }
  113 redo1:
  114             if (!keep_stressing())
  115                 goto done;
  116             /* We need chdir to cwd to always succeed */
  117             if (chdir(cwd) < 0) {
  118                 /* Maybe low memory, force retry */
  119                 if (errno == ENOMEM)
  120                     goto redo1;
  121                 pr_fail_err("chdir");
  122                 goto abort;
  123             }
  124         }
  125         inc_counter(args);
  126     } while (keep_stressing());
  127 done:
  128     ret = EXIT_SUCCESS;
  129 abort:
  130     if (chdir(cwd) < 0)
  131         pr_fail_err("chdir");
  132 
  133     if (fd >= 0)
  134         (void)close(fd);
  135 
  136     /* force unlink of all files */
  137     pr_tidy("%s: removing %" PRIu64 " directories\n",
  138         args->name, chdir_dirs);
  139 
  140     for (i = 0; (i < chdir_dirs) && paths[i] ; i++) {
  141         (void)rmdir(paths[i]);
  142         free(paths[i]);
  143     }
  144     (void)stress_temp_dir_rm_args(args);
  145 err:
  146     free(paths);
  147 
  148     return ret;
  149 }
  150 
  151 stressor_info_t stress_chdir_info = {
  152     .stressor = stress_chdir,
  153     .class = CLASS_FILESYSTEM | CLASS_OS
  154 };