"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-cap.c" (15 Mar 2019, 2949 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-cap.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 #if defined(HAVE_SYS_CAPABILITY_H)
   28 
   29 static int stress_capgetset_pid(
   30     const args_t *args,
   31     const pid_t pid,
   32     const bool do_set,
   33     const bool exists)
   34 {
   35     int ret;
   36     struct __user_cap_header_struct uch;
   37     struct __user_cap_data_struct ucd[_LINUX_CAPABILITY_U32S_3];
   38 
   39     (void)memset(&uch, 0, sizeof uch);
   40     (void)memset(ucd, 0, sizeof ucd);
   41 
   42     uch.version = _LINUX_CAPABILITY_VERSION_3;
   43     uch.pid = pid;
   44 
   45     ret = capget(&uch, ucd);
   46     if (ret < 0) {
   47         if (((errno == ESRCH) && exists) ||
   48             (errno != ESRCH)) {
   49             pr_fail("%s: capget on pid %d failed: "
   50                 "errno=%d (%s)\n",
   51                 args->name, pid, errno, strerror(errno));
   52         }
   53     }
   54 
   55     if (do_set) {
   56         ret = capset(&uch, ucd);
   57         if (ret < 0) {
   58             if (((errno == ESRCH) && exists) ||
   59                 (errno != ESRCH)) {
   60                 pr_fail("%s: capget on pid %d failed: "
   61                     "errno=%d (%s)\n",
   62                     args->name, pid, errno, strerror(errno));
   63             }
   64         }
   65     }
   66 
   67     inc_counter(args);
   68 
   69     return ret;
   70 }
   71 
   72 /*
   73  *  stress_cap
   74  *  stress capabilities (trivial)
   75  */
   76 static int stress_cap(const args_t *args)
   77 {
   78     do {
   79         DIR *dir;
   80 
   81         stress_capgetset_pid(args, 1, false, true);
   82         if (!keep_stressing())
   83             break;
   84         stress_capgetset_pid(args, args->pid, true, true);
   85         if (!keep_stressing())
   86             break;
   87         stress_capgetset_pid(args, args->ppid, false, false);
   88         if (!keep_stressing())
   89             break;
   90 
   91         dir = opendir("/proc");
   92         if (dir) {
   93             struct dirent *d;
   94 
   95             while ((d = readdir(dir)) != NULL) {
   96                 pid_t p;
   97 
   98                 if (!isdigit(d->d_name[0]))
   99                     continue;
  100                 if (sscanf(d->d_name, "%d", &p) != 1)
  101                     continue;
  102                 stress_capgetset_pid(args, p, false, false);
  103                 if (!keep_stressing())
  104                     break;
  105             }
  106             (void)closedir(dir);
  107         }
  108     } while (keep_stressing());
  109 
  110     return EXIT_SUCCESS;
  111 }
  112 
  113 stressor_info_t stress_cap_info = {
  114     .stressor = stress_cap,
  115     .class = CLASS_OS
  116 };
  117 #else
  118 stressor_info_t stress_cap_info = {
  119     .stressor = stress_not_implemented,
  120     .class = CLASS_OS
  121 };
  122 #endif