"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-get.c" (15 Mar 2019, 7971 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-get.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 #define _DEFAULT_SOURCE 1
   28 #define _BSD_SOURCE 1
   29 
   30 #define check_do_run()          \
   31     if (!g_keep_stressing_flag) \
   32         break;          \
   33 
   34 #define GIDS_MAX    (1024)
   35 
   36 static const int rusages[] = {
   37 #if defined(RUSAGE_SELF)
   38     RUSAGE_SELF,
   39 #endif
   40 #if defined(RUSAGE_CHILDREN)
   41     RUSAGE_CHILDREN,
   42 #endif
   43 #if defined(RUSAGE_THREAD)
   44     RUSAGE_THREAD
   45 #endif
   46 };
   47 
   48 /*
   49  *  The following produce -EINVAL for Haiku, so
   50  *  disable them
   51  */
   52 #if defined(__HAIKU__)
   53 #undef RLIMIT_CPU
   54 #undef RLIMIT_FSIZE
   55 #endif
   56 
   57 static const int rlimits[] = {
   58 #if defined(RLIMIT_AS)
   59     RLIMIT_AS,
   60 #endif
   61 #if defined(RLIMIT_CORE)
   62     RLIMIT_CORE,
   63 #endif
   64 #if defined(RLIMIT_CPU)
   65     RLIMIT_CPU,
   66 #endif
   67 #if defined(RLIMIT_DATA)
   68     RLIMIT_DATA,
   69 #endif
   70 #if defined(RLIMIT_FSIZE)
   71     RLIMIT_FSIZE,
   72 #endif
   73 #if defined(RLIMIT_MEMLOCK)
   74     RLIMIT_MEMLOCK,
   75 #endif
   76 #if defined(RLIMIT_MSGQUEUE)
   77     RLIMIT_MSGQUEUE,
   78 #endif
   79 #if defined(RLIMIT_NICE)
   80     RLIMIT_NICE,
   81 #endif
   82 #if defined(RLIMIT_NOFILE)
   83     RLIMIT_NOFILE,
   84 #endif
   85 #if defined(RLIMIT_RSS)
   86     RLIMIT_RSS,
   87 #endif
   88 #if defined(RLIMIT_RTPRIO)
   89     RLIMIT_RTPRIO,
   90 #endif
   91 #if defined(RLIMIT_RTTIME)
   92     RLIMIT_RTTIME,
   93 #endif
   94 #if defined(RLIMIT_SIGPENDING)
   95     RLIMIT_SIGPENDING,
   96 #endif
   97 #if defined(RLIMIT_STACK)
   98     RLIMIT_STACK
   99 #endif
  100 };
  101 
  102 #if defined(HAVE_GETPRIORITY)
  103 static const int priorities[] = {
  104 #if defined(PRIO_PROCESS)
  105     PRIO_PROCESS,
  106 #endif
  107 #if defined(PRIO_PGRP)
  108     PRIO_PGRP,
  109 #endif
  110 #if defined(PRIO_USER)
  111     PRIO_USER
  112 #endif
  113 };
  114 #endif
  115 
  116 /*
  117  *  stress on get*() calls
  118  *  stress system by rapid get*() system calls
  119  */
  120 static int stress_get(const args_t *args)
  121 {
  122     const bool verify = (g_opt_flags & OPT_FLAGS_VERIFY);
  123 #if defined(HAVE_SYS_TIMEX_H)
  124 #if defined(HAVE_ADJTIMEX) || defined(HAVE_ADJTIME)
  125     const bool is_root = (geteuid() == 0);
  126 #endif
  127 #endif
  128 
  129     do {
  130         char path[PATH_MAX];
  131         char *ptr;
  132         gid_t gids[GIDS_MAX];
  133         unsigned cpu, node;
  134         const pid_t mypid = getpid();
  135         int ret, n, fs_index;
  136         size_t i;
  137 #if defined(HAVE_SYS_TIMEX_H) && defined(HAVE_ADJTIME)
  138         struct timeval delta;
  139 #endif
  140         struct timeval tv;
  141         time_t t;
  142         pid_t pid;
  143         gid_t gid;
  144         uid_t uid;
  145 
  146         (void)mypid;
  147 
  148         check_do_run();
  149 
  150         pid = getppid();
  151         (void)pid;
  152         check_do_run();
  153 
  154         ptr = getcwd(path, sizeof path);
  155         if (verify && !ptr)
  156             pr_fail_err("getcwd");
  157         check_do_run();
  158 
  159         gid = getgid();
  160         (void)gid;
  161         check_do_run();
  162 
  163         gid = getegid();
  164         (void)gid;
  165         check_do_run();
  166 
  167         uid = getuid();
  168         (void)uid;
  169         check_do_run();
  170 
  171         uid = geteuid();
  172         (void)uid;
  173         check_do_run();
  174 
  175         ret = getgroups(GIDS_MAX, gids);
  176         if (verify && (ret < 0) && (errno != EINVAL))
  177             pr_fail_err("getgroups");
  178         check_do_run();
  179 
  180 #if defined(HAVE_GETPGRP)
  181         pid = getpgrp();
  182         (void)pid;
  183         check_do_run();
  184 #endif
  185 
  186 #if defined(HAVE_GETPGID)
  187         pid = getpgid(mypid);
  188         (void)pid;
  189         check_do_run();
  190 #endif
  191 
  192 #if defined(HAVE_GETPRIORITY)
  193         for (i = 0; i < SIZEOF_ARRAY(priorities); i++) {
  194             errno = 0;
  195             ret = getpriority(priorities[i], 0);
  196             if (verify && errno && (errno != EINVAL) && (ret < 0))
  197                 pr_fail_err("getpriority");
  198             check_do_run();
  199         }
  200 #endif
  201 
  202 #if defined(HAVE_GETRESGID)
  203         {
  204             gid_t rgid, egid, sgid;
  205 
  206             ret = getresgid(&rgid, &egid, &sgid);
  207             if (verify && (ret < 0))
  208                 pr_fail_err("getresgid");
  209             check_do_run();
  210         }
  211 #endif
  212 
  213 #if defined(HAVE_GETRESUID)
  214         {
  215             uid_t ruid, euid, suid;
  216 
  217             ret = getresuid(&ruid, &euid, &suid);
  218             if (verify && (ret < 0))
  219                 pr_fail_err("getresuid");
  220             check_do_run();
  221         }
  222 #endif
  223 
  224         for (i = 0; i < SIZEOF_ARRAY(rlimits); i++) {
  225             struct rlimit rlim;
  226 
  227             ret = getrlimit(rlimits[i], &rlim);
  228             if (verify && (ret < 0))
  229                 pr_fail("%s: getrlimit(%zu, ..) failed, errno=%d (%s)\n",
  230                     args->name, i, errno, strerror(errno));
  231             check_do_run();
  232         }
  233 
  234 #if defined(HAVE_PRLIMIT) && NEED_GLIBC(2,13,0) && defined(EOVERFLOW)
  235         for (i = 0; i < SIZEOF_ARRAY(rlimits); i++) {
  236             struct rlimit rlim[2];
  237 
  238             ret = prlimit(mypid, rlimits[i], NULL, &rlim[0]);
  239             if (verify && (ret < 0) && (errno != EOVERFLOW))
  240                 pr_fail("%s: prlimit(%d, %zu, ..) failed, errno=%d (%s)\n",
  241                     args->name, mypid, i, errno, strerror(errno));
  242             if (!ret) {
  243                 ret = prlimit(mypid, rlimits[i], &rlim[0], NULL);
  244                 if (verify && (ret < 0) && (errno != EOVERFLOW))
  245                     pr_fail("%s: prlimit(%d, %zu, ..) failed, errno=%d (%s)\n",
  246                         args->name, mypid, i, errno, strerror(errno));
  247                 ret = prlimit(mypid, rlimits[i], &rlim[0], &rlim[1]);
  248                 if (verify && (ret < 0) && (errno != EOVERFLOW))
  249                     pr_fail("%s: prlimit(%d, %zu, ..) failed, errno=%d (%s)\n",
  250                         args->name, mypid, i, errno, strerror(errno));
  251             }
  252             check_do_run();
  253         }
  254 #endif
  255 
  256         for (i = 0; i < SIZEOF_ARRAY(rusages); i++) {
  257             struct rusage usage;
  258 
  259             ret = getrusage(rusages[i], &usage);
  260             if (verify && (ret < 0))
  261                 pr_fail("%s: getrusage(%zu, ..) failed, errno=%d (%s)\n",
  262                     args->name, i, errno, strerror(errno));
  263             check_do_run();
  264         }
  265 
  266 #if _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
  267         ret = getsid(mypid);
  268         if (verify && (ret < 0))
  269             pr_fail_err("getsid");
  270         check_do_run();
  271 #endif
  272 
  273         (void)shim_gettid();
  274         check_do_run();
  275 
  276         (void)shim_getcpu(&cpu, &node, NULL);
  277         (void)shim_getcpu(NULL, &node, NULL);
  278         (void)shim_getcpu(&cpu, NULL, NULL);
  279         (void)shim_getcpu(NULL, NULL, NULL);
  280         check_do_run();
  281 
  282         t = time(NULL);
  283         if (verify && (t == (time_t)-1))
  284             pr_fail_err("time");
  285 
  286         ret = gettimeofday(&tv, NULL);
  287         if (verify && (ret < 0))
  288             pr_fail_err("gettimeval");
  289 #if defined(HAVE_UNAME) && defined(HAVE_SYS_UTSNAME_H)
  290         {
  291             struct utsname utsbuf;
  292 
  293             ret = uname(&utsbuf);
  294             if (verify && (ret < 0))
  295                 pr_fail_err("uname");
  296         }
  297 #endif
  298 
  299 #if defined(HAVE_GETPAGESISE)
  300         ret = getpagesize();
  301         (void)ret;
  302 #endif
  303 
  304 #if defined(HAVE_GETDTABLESIZE)
  305         ret = getdtablesize();
  306         (void)ret;
  307 #endif
  308 
  309 #if defined(HAVE_LOOKUP_DCOOKIE)
  310         {
  311             char buf[PATH_MAX];
  312 
  313             ret = syscall(__NR_lookup_dcookie, buf, sizeof(buf));
  314             (void)ret;
  315         }
  316 #endif
  317 
  318 #if defined(HAVE_SYS_TIMEX_H) && defined(HAVE_ADJTIMEX)
  319         {
  320             struct timex timexbuf;
  321 
  322             timexbuf.modes = 0;
  323             ret = adjtimex(&timexbuf);
  324             if (is_root && verify && (ret < 0))
  325                 pr_fail_err("adjtimex");
  326         }
  327 #endif
  328 
  329 #if defined(HAVE_SYS_TIMEX_H) && defined(HAVE_ADJTIME)
  330         (void)memset(&delta, 0, sizeof(delta));
  331         ret = adjtime(&delta, &tv);
  332         if (is_root && verify && (ret < 0))
  333             pr_fail_err("adjtime");
  334 #endif
  335 
  336         /* Get number of file system types */
  337         n = shim_sysfs(3);
  338         for (fs_index = 0; fs_index < n; fs_index++) {
  339             char buf[4096];
  340 
  341             ret = shim_sysfs(2, fs_index, buf);
  342             if (!ret) {
  343                 ret = shim_sysfs(1, buf);
  344                 if (verify && (ret != fs_index)) {
  345                     pr_fail("%s: sysfs(1, %s) failed, errno=%d (%s)\n",
  346                         args->name, buf, errno, strerror(errno));
  347                 }
  348             } else {
  349                 if (verify) {
  350                     pr_fail("%s: sysfs(2, %d, buf) failed, errno=%d (%s)\n",
  351                         args->name, fs_index, errno, strerror(errno));
  352                 }
  353             }
  354         }
  355         inc_counter(args);
  356     } while (keep_stressing());
  357 
  358     return EXIT_SUCCESS;
  359 }
  360 
  361 stressor_info_t stress_get_info = {
  362     .stressor = stress_get,
  363     .class = CLASS_OS
  364 };