"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-dup.c" (15 Mar 2019, 2714 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-dup.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_dup()
   29  *  stress system by rapid dup/close calls
   30  */
   31 static int stress_dup(const args_t *args)
   32 {
   33     int fds[STRESS_FD_MAX];
   34     size_t max_fd = stress_get_file_limit();
   35     size_t i;
   36 #if defined(HAVE_DUP3)
   37     bool do_dup3 = true;
   38 #endif
   39 
   40     if (max_fd > SIZEOF_ARRAY(fds))
   41         max_fd =  SIZEOF_ARRAY(fds);
   42 
   43     fds[0] = open("/dev/zero", O_RDONLY);
   44     if (fds[0] < 0) {
   45         pr_fail_dbg("open on /dev/zero");
   46         return EXIT_FAILURE;
   47     }
   48 
   49     do {
   50         size_t n;
   51 
   52         for (n = 1; n < max_fd; n++) {
   53             int tmp;
   54 
   55             fds[n] = dup(fds[0]);
   56             if (fds[n] < 0)
   57                 break;
   58 #if defined(HAVE_DUP3)
   59             if (do_dup3 && mwc1()) {
   60                 int fd;
   61 
   62                 fd = shim_dup3(fds[0], fds[n], O_CLOEXEC);
   63                 /* No dup3 support? then fallback to dup2 */
   64                 if ((fd < 0) && (errno == ENOSYS)) {
   65                     fd = dup2(fds[0], fds[n]);
   66                     do_dup3 = false;
   67                 }
   68                 fds[n] = fd;
   69             } else {
   70                 fds[n] = dup2(fds[0], fds[n]);
   71             }
   72 #else
   73             fds[n] = dup2(fds[0], fds[n]);
   74 #endif
   75             if (fds[n] < 0)
   76                 break;
   77 
   78             /* dup2 on the same fd should be a no-op */
   79             tmp = dup2(fds[n], fds[n]);
   80             if (tmp != fds[n]) {
   81                 pr_fail_err("dup2 with same fds");
   82                 break;
   83             }
   84 #if defined(F_DUPFD)
   85             /* POSIX.1-2001 fcntl() */
   86 
   87             (void)close(fds[n]);
   88             fds[n] = fcntl(fds[0], F_DUPFD, fds[0]);
   89             if (fds[n] < 0)
   90                 break;
   91 #endif
   92 
   93             if (!keep_stressing())
   94                 break;
   95             inc_counter(args);
   96         }
   97         for (i = 1; i < n; i++) {
   98             if (fds[i] < 0)
   99                 break;
  100             if (!g_keep_stressing_flag)
  101                 break;
  102             (void)close(fds[i]);
  103         }
  104     } while (keep_stressing());
  105     (void)close(fds[0]);
  106 
  107     return EXIT_SUCCESS;
  108 }
  109 
  110 stressor_info_t stress_dup_info = {
  111     .stressor = stress_dup,
  112     .class = CLASS_FILESYSTEM | CLASS_OS
  113 };