"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-set.c" (15 Mar 2019, 4643 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-set.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 #if defined(HOST_NAME_MAX)
   31 #define STRESS_HOST_NAME_LEN    (HOST_NAME_MAX + 1)
   32 #else
   33 #define STRESS_HOST_NAME_LEN    (256)
   34 #endif
   35 
   36 #define check_do_run()          \
   37     if (!g_keep_stressing_flag) \
   38         break;          \
   39 
   40 #define GIDS_MAX    (1024)
   41 
   42 typedef struct {
   43     int id;
   44     int ret;
   45     struct rlimit rlim;
   46 } rlimit_info_t;
   47 
   48 static rlimit_info_t rlimits[] = {
   49 #if defined(RLIMIT_AS)
   50     { RLIMIT_AS, 0, { 0, 0 } },
   51 #endif
   52 #if defined(RLIMIT_CORE)
   53     { RLIMIT_CORE, 0, { 0, 0 } },
   54 #endif
   55 #if defined(RLIMIT_CPU)
   56     { RLIMIT_CPU, 0, { 0, 0 } },
   57 #endif
   58 #if defined(RLIMIT_DATA)
   59     { RLIMIT_DATA, 0, { 0, 0 } },
   60 #endif
   61 #if defined(RLIMIT_FSIZE)
   62     { RLIMIT_FSIZE, 0, { 0, 0 } },
   63 #endif
   64 #if defined(RLIMIT_MEMLOCK)
   65     { RLIMIT_MEMLOCK, 0, { 0, 0 } },
   66 #endif
   67 #if defined(RLIMIT_MSGQUEUE)
   68     { RLIMIT_MSGQUEUE, 0, { 0, 0 } },
   69 #endif
   70 #if defined(RLIMIT_NICE)
   71     { RLIMIT_NICE, 0, { 0, 0 } },
   72 #endif
   73 #if defined(RLIMIT_NOFILE)
   74     { RLIMIT_NOFILE, 0, { 0, 0 } },
   75 #endif
   76 #if defined(RLIMIT_RSS)
   77     { RLIMIT_RSS, 0, { 0, 0 } },
   78 #endif
   79 #if defined(RLIMIT_RTPRIO)
   80     { RLIMIT_RTPRIO, 0, { 0, 0 } },
   81 #endif
   82 #if defined(RLIMIT_RTTIME)
   83     { RLIMIT_RTTIME, 0, { 0, 0 } },
   84 #endif
   85 #if defined(RLIMIT_SIGPENDING)
   86     { RLIMIT_SIGPENDING, 0, { 0, 0 } },
   87 #endif
   88 #if defined(RLIMIT_STACK)
   89     { RLIMIT_STACK, 0, { 0, 0 } },
   90 #endif
   91 };
   92 
   93 /*
   94  *  stress on set*() calls
   95  *  stress system by rapid get*() system calls
   96  */
   97 static int stress_set(const args_t *args)
   98 {
   99     size_t i;
  100     int ret_hostname;
  101     char hostname[STRESS_HOST_NAME_LEN];
  102 #if defined(HAVE_GETPGID) && defined(HAVE_SETPGID)
  103     const pid_t mypid = getpid();
  104 #endif
  105 
  106     for (i = 0; i < SIZEOF_ARRAY(rlimits); i++) {
  107         rlimits[i].ret = getrlimit(rlimits[i].id, &rlimits[i].rlim);
  108     }
  109 
  110     (void)memset(hostname, 0, sizeof(hostname));
  111     ret_hostname = gethostname(hostname, sizeof(hostname) - 1);
  112     if (ret_hostname == 0)
  113         hostname[sizeof(hostname) - 1 ] = '\0';
  114 
  115     do {
  116         int ret;
  117         pid_t pid;
  118         gid_t gid;
  119         uid_t uid;
  120 
  121         /* setsid will fail, ignore return */
  122         pid = setsid();
  123         (void)pid;
  124         check_do_run();
  125 
  126         /* getgid always succeeds */
  127         gid = getgid();
  128         ret = setgid(gid);
  129         (void)ret;
  130         check_do_run();
  131 
  132         if (ret_hostname == 0) {
  133             ret = sethostname(hostname, sizeof(hostname) - 1);
  134             (void)ret;
  135         }
  136 
  137 #if defined(HAVE_GETPGID) && defined(HAVE_SETPGID)
  138         pid = getpgid(mypid);
  139         if (pid != -1) {
  140             ret = setpgid(mypid, pid);
  141             (void)ret;
  142             check_do_run();
  143         }
  144 #endif
  145 
  146 #if defined(HAVE_GETPGRP) && defined(HAVE_SETPGRP)
  147         /* getpgrp always succeeds */
  148         pid = getpgrp();
  149         if (pid != -1) {
  150             ret = setpgrp();
  151             (void)ret;
  152             check_do_run();
  153         }
  154 #endif
  155 
  156         /* getuid always succeeds */
  157         uid = getuid();
  158         ret = setuid(uid);
  159         (void)ret;
  160         check_do_run();
  161 
  162 #if defined(HAVE_GRP_H)
  163         ret = getgroups(0, NULL);
  164         if (ret > 0) {
  165             gid_t groups[GIDS_MAX];
  166 
  167             ret = STRESS_MINIMUM(ret, (int)SIZEOF_ARRAY(groups));
  168             ret = getgroups(ret, groups);
  169             if (ret > 0) {
  170                 ret = setgroups(ret, groups);
  171                 (void)ret;
  172             }
  173         }
  174 #endif
  175 
  176 #if defined(HAVE_SETREUID)
  177         ret = setreuid(-1, -1);
  178         (void)ret;
  179 #endif
  180 #if defined(HAVE_SETREGID)
  181         ret = setregid(-1, -1);
  182         (void)ret;
  183 #endif
  184 #if defined(HAVE_SETRESUID)
  185         ret = setresuid(-1, -1, -1);
  186         (void)ret;
  187 #endif
  188 #if defined(HAVE_SETRESGID)
  189         ret = setresgid(-1, -1, -1);
  190         (void)ret;
  191 #endif
  192 
  193         for (i = 0; i < SIZEOF_ARRAY(rlimits); i++) {
  194             if (rlimits[i].ret == 0) {
  195                 ret = setrlimit(rlimits[i].id, &rlimits[i].rlim);
  196                 (void)ret;
  197             }
  198         }
  199         inc_counter(args);
  200     } while (keep_stressing());
  201 
  202     return EXIT_SUCCESS;
  203 }
  204 
  205 stressor_info_t stress_set_info = {
  206     .stressor = stress_set,
  207     .class = CLASS_OS
  208 };