"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-getrandom.c" (15 Mar 2019, 2602 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-getrandom.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(__OpenBSD__) ||     \
   28     defined(__APPLE__) ||   \
   29     (defined(__linux__) && defined(__NR_getrandom))
   30 
   31 #if defined(__OpenBSD__) || \
   32     defined(__APPLE__)
   33 #define RANDOM_BUFFER_SIZE  (256)
   34 #else
   35 #define RANDOM_BUFFER_SIZE  (8192)
   36 #endif
   37 
   38 /*
   39  *  stress_getrandom_supported()
   40  *      check if getrandom is supported
   41  */
   42 static int stress_getrandom_supported(void)
   43 {
   44     int ret;
   45     char buffer[RANDOM_BUFFER_SIZE];
   46 
   47     ret = shim_getrandom(buffer, sizeof(buffer), 0);
   48     if ((ret < 0) && (errno == ENOSYS)) {
   49         pr_inf("getrandom stressor will be skipped, getrandom() not supported\n");
   50         return -1;
   51     }
   52     return 0;
   53 }
   54 
   55 /*
   56  *  stress_getrandom
   57  *  stress reading random values using getrandom()
   58  */
   59 static int stress_getrandom(const args_t *args)
   60 {
   61     do {
   62         char buffer[RANDOM_BUFFER_SIZE];
   63 
   64         ssize_t ret;
   65 
   66         ret = shim_getrandom(buffer, sizeof(buffer), 0);
   67         if (ret < 0) {
   68             if ((errno == EAGAIN) || (errno == EINTR))
   69                 continue;
   70             if (errno == ENOSYS) {
   71                 /* Should not happen.. */
   72                 pr_inf("%s: stressor will be skipped, "
   73                     "getrandom() not supported\n",
   74                     args->name);
   75                 return EXIT_NOT_IMPLEMENTED;
   76             }
   77             pr_fail_err("getrandom");
   78             return EXIT_FAILURE;
   79         }
   80         inc_counter(args);
   81     } while (keep_stressing());
   82 
   83     return EXIT_SUCCESS;
   84 }
   85 
   86 stressor_info_t stress_getrandom_info = {
   87     .stressor = stress_getrandom,
   88     .supported = stress_getrandom_supported,
   89     .class = CLASS_OS | CLASS_CPU
   90 };
   91 #else
   92 stressor_info_t stress_getrandom_info = {
   93     .stressor = stress_not_implemented,
   94     .class = CLASS_OS | CLASS_CPU
   95 };
   96 #endif