"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-urandom.c" (15 Mar 2019, 2745 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-urandom.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 /*
   28  *  stress_urandom
   29  *  stress reading of /dev/urandom and /dev/random
   30  */
   31 static int stress_urandom(const args_t *args)
   32 {
   33     int fd_urnd, fd_rnd, rc = EXIT_FAILURE;
   34 
   35     if ((fd_urnd = open("/dev/urandom", O_RDONLY)) < 0) {
   36         if (errno != ENOENT) {
   37             pr_fail_err("open");
   38             return EXIT_FAILURE;
   39         }
   40     }
   41     if ((fd_rnd = open("/dev/random", O_RDONLY | O_NONBLOCK)) < 0) {
   42         if (errno != ENOENT) {
   43             pr_fail_err("open");
   44             (void)close(fd_urnd);
   45             return EXIT_FAILURE;
   46         }
   47     }
   48 
   49     if ((fd_urnd < 0) && (fd_rnd < 0)) {
   50         pr_inf("%s: random device(s) do not exist, skipping stressor\n",
   51             args->name);
   52         return EXIT_NOT_IMPLEMENTED;
   53     }
   54 
   55     do {
   56         char buffer[8192];
   57         ssize_t ret;
   58 
   59         if (fd_urnd >= 0) {
   60             ret = read(fd_urnd, buffer, sizeof(buffer));
   61             if (ret < 0) {
   62                 if ((errno != EAGAIN) && (errno != EINTR)) {
   63                     pr_fail_err("read");
   64                     goto err;
   65                 }
   66             }
   67         }
   68 
   69         /*
   70          * Fetch entropy pool count, not considered fatal
   71          * this fails, just skip this part of the stressor
   72          */
   73         if (fd_rnd >= 0) {
   74 #if defined(RNDGETENTCNT)
   75             unsigned long val;
   76 
   77             if (ioctl(fd_rnd, RNDGETENTCNT, &val) < 0)
   78                 goto next;
   79             /* Try to avoid emptying entropy pool */
   80             if (val < 128)
   81                 goto next;
   82 #endif
   83             ret = read(fd_rnd, buffer, 1);
   84             if (ret < 0) {
   85                 if ((errno != EAGAIN) && (errno != EINTR)) {
   86                     pr_fail_err("read");
   87                     goto err;
   88                 }
   89             }
   90         }
   91 #if defined(RNDGETENTCNT)
   92 next:
   93 #endif
   94         inc_counter(args);
   95     } while (keep_stressing());
   96 
   97     rc = EXIT_SUCCESS;
   98 err:
   99     if (fd_urnd >= 0)
  100         (void)close(fd_urnd);
  101     if (fd_rnd >= 0)
  102         (void)close(fd_rnd);
  103 
  104     return rc;
  105 }
  106 stressor_info_t stress_urandom_info = {
  107     .stressor = stress_urandom,
  108     .class = CLASS_DEV | CLASS_OS
  109 };