"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.10.00/stress-cap.c" (8 Jul 2019, 3169 Bytes) of package /linux/privat/stress-ng-0.10.00.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.57_vs_0.09.58.

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