"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-daemon.c" (15 Mar 2019, 3642 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-daemon.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.52_vs_0.09.54.

    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(NSIG)
   28 #define MAX_SIGNUM      NSIG
   29 #elif defined(_NSIG)
   30 #define MAX_SIGNUM      _NSIG
   31 #else
   32 #define MAX_SIGNUM      256
   33 #endif
   34 
   35 /*
   36  *  daemons()
   37  *  fork off a child and let the parent die
   38  */
   39 static void daemons(const args_t *args, const int fd)
   40 {
   41     int fds[3];
   42     int i;
   43     sigset_t set;
   44 
   45     if (stress_sig_stop_stressing(args->name, SIGALRM) < 0)
   46         goto err;
   47     if (setsid() < 0)
   48         goto err;
   49 
   50     (void)close(0);
   51     (void)close(1);
   52     (void)close(2);
   53 
   54     for (i = 0; i < MAX_SIGNUM; i++)
   55         (void)signal(i, SIG_DFL);
   56 
   57     (void)sigemptyset(&set);
   58     (void)sigprocmask(SIG_SETMASK, &set, NULL);
   59 #if defined(HAVE_CLEARENV)
   60     (void)clearenv();
   61 #endif
   62 
   63     if ((fds[0] = open("/dev/null", O_RDWR)) < 0)
   64         goto err;
   65     if ((fds[1] = dup(0)) < 0)
   66         goto err0;
   67     if ((fds[2] = dup(0)) < 0)
   68         goto err1;
   69 
   70     while (g_keep_stressing_flag) {
   71         pid_t pid;
   72 
   73         pid = fork();
   74         if (pid < 0) {
   75             /* A slow init? no pids or memory, retry */
   76             if ((errno == EAGAIN) || (errno == ENOMEM))
   77                 continue;
   78             goto tidy;
   79         } else if (pid == 0) {
   80             /* Child */
   81             char buf[1] = { 0xff };
   82             ssize_t sz;
   83             int ret;
   84 
   85             if (chdir("/") < 0)
   86                 goto err2;
   87             (void)umask(0);
   88             ret = stress_drop_capabilities(args->name);
   89             (void)ret;
   90 
   91             sz = write(fd, buf, sizeof(buf));
   92             if (sz != sizeof(buf))
   93                 goto err2;
   94 
   95         } else {
   96             /* Parent, will be reaped by init */
   97             break;
   98         }
   99     }
  100 
  101 tidy:
  102     (void)close(fds[2]);
  103     (void)close(fds[1]);
  104     (void)close(fds[0]);
  105     return;
  106 
  107 err2:   (void)close(fds[2]);
  108 err1:   (void)close(fds[1]);
  109 err0:   (void)close(fds[0]);
  110 err:    (void)close(fd);
  111 }
  112 
  113 /*
  114  *  stress_daemon()
  115  *  stress by multiple daemonizing forks
  116  */
  117 static int stress_daemon(const args_t *args)
  118 {
  119     int fds[2];
  120     pid_t pid;
  121 
  122     if (stress_sig_stop_stressing(args->name, SIGALRM) < 0)
  123         return EXIT_FAILURE;
  124 
  125     if (pipe(fds) < 0) {
  126         pr_fail_dbg("pipe");
  127         return EXIT_FAILURE;
  128     }
  129     pid = fork();
  130     if (pid < 0) {
  131         pr_fail_dbg("fork");
  132         (void)close(fds[0]);
  133         (void)close(fds[1]);
  134         return EXIT_FAILURE;
  135     } else if (pid == 0) {
  136         /* Children */
  137         (void)close(fds[0]);
  138         daemons(args, fds[1]);
  139         (void)close(fds[1]);
  140     } else {
  141         /* Parent */
  142         (void)close(fds[1]);
  143         do {
  144             ssize_t n;
  145             char buf[1];
  146 
  147             n = read(fds[0], buf, sizeof(buf));
  148             if (n < 0) {
  149                 (void)close(fds[0]);
  150                 if (errno != EINTR) {
  151                     pr_dbg("%s: read failed: "
  152                         "errno=%d (%s)\n",
  153                         args->name,
  154                         errno, strerror(errno));
  155                 }
  156                 break;
  157             }
  158             inc_counter(args);
  159         } while (keep_stressing());
  160     }
  161 
  162     return EXIT_SUCCESS;
  163 }
  164 
  165 stressor_info_t stress_daemon_info = {
  166     .stressor = stress_daemon,
  167     .class = CLASS_SCHEDULER | CLASS_OS
  168 };