"Fossies" - the Fresh Open Source Software Archive

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

    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(HAVE_FLOCK) &&  \
   28     defined(LOCK_EX) &&     \
   29     defined(LOCK_UN)
   30 
   31 #define MAX_FLOCK_STRESSORS (3)
   32 
   33 static void stress_flock_child(const args_t *args, const int fd)
   34 {
   35     bool cont;
   36 
   37     for (;;) {
   38         if (flock(fd, LOCK_EX) == 0) {
   39             cont = keep_stressing();
   40             if (cont)
   41                 inc_counter(args);
   42             (void)flock(fd, LOCK_UN);
   43             if (!cont)
   44                 break;
   45         }
   46 
   47 #if defined(LOCK_NB)
   48         if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
   49             cont = keep_stressing();
   50             if (cont)
   51                 inc_counter(args);
   52             (void)flock(fd, LOCK_UN);
   53             if (!cont)
   54                 break;
   55         }
   56 #endif
   57 
   58 #if defined(LOCK_SH)
   59         if (!keep_stressing())
   60             break;
   61         if (flock(fd, LOCK_SH) == 0) {
   62             cont = keep_stressing();
   63             if (cont)
   64                 inc_counter(args);
   65             (void)flock(fd, LOCK_UN);
   66             if (!cont)
   67                 break;
   68         }
   69 #endif
   70 
   71 #if defined(LOCK_SH) && defined(LOCK_NB)
   72         if (!keep_stressing())
   73             break;
   74         if (flock(fd, LOCK_SH | LOCK_NB) == 0) {
   75             cont = keep_stressing();
   76             if (cont)
   77                 inc_counter(args);
   78             (void)flock(fd, LOCK_UN);
   79             if (!cont)
   80                 break;
   81         }
   82 #endif
   83     }
   84 }
   85 
   86 /*
   87  *  stress_flock
   88  *  stress file locking
   89  */
   90 static int stress_flock(const args_t *args)
   91 {
   92     int fd, ret, rc = EXIT_FAILURE;
   93     size_t i;
   94     pid_t pids[MAX_FLOCK_STRESSORS];
   95     char filename[PATH_MAX];
   96 
   97     ret = stress_temp_dir_mk_args(args);
   98     if (ret < 0)
   99         return exit_status(-ret);
  100 
  101     (void)stress_temp_filename_args(args,
  102         filename, sizeof(filename), mwc32());
  103     fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  104     if (fd < 0) {
  105         pr_err("%s: failed to create %s: errno=%d (%s)\n",
  106             args->name, filename, errno, strerror(errno));
  107         goto err;
  108     }
  109 
  110     (void)memset(pids, 0, sizeof(pids));
  111     for (i = 0; i < MAX_FLOCK_STRESSORS; i++) {
  112         pids[i] = fork();
  113         if (pids[i] < 0) {
  114             goto reap;
  115         } else if (pids[i] == 0) {
  116             (void)setpgid(0, g_pgrp);
  117             stress_parent_died_alarm();
  118 
  119             stress_flock_child(args, fd);
  120             _exit(EXIT_SUCCESS);
  121         }
  122     }
  123 
  124     stress_flock_child(args, fd);
  125     rc = EXIT_SUCCESS;
  126 reap:
  127     (void)close(fd);
  128 
  129     for (i = 0; i < MAX_FLOCK_STRESSORS; i++) {
  130         if (pids[i] > 0) {
  131             int status;
  132 
  133             (void)kill(pids[i], SIGKILL);
  134             (void)waitpid(pids[i], &status, 0);
  135         }
  136     }
  137 
  138     (void)unlink(filename);
  139 err:
  140     (void)stress_temp_dir_rm_args(args);
  141 
  142     return rc;
  143 }
  144 
  145 stressor_info_t stress_flock_info = {
  146     .stressor = stress_flock,
  147     .class = CLASS_FILESYSTEM | CLASS_OS
  148 };
  149 #else
  150 stressor_info_t stress_flock_info = {
  151     .stressor = stress_not_implemented,
  152     .class = CLASS_FILESYSTEM | CLASS_OS
  153 };
  154 #endif