"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-sigfd.c" (15 Mar 2019, 3055 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-sigfd.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_SIGNALFD_H) &&     \
   28     NEED_GLIBC(2,8,0) &&        \
   29     defined(HAVE_SIGQUEUE)
   30 
   31 /*
   32  *  stress_sigfd
   33  *  stress signalfd reads
   34  */
   35 static int stress_sigfd(const args_t *args)
   36 {
   37     pid_t pid, ppid = args->pid;
   38     int sfd;
   39     sigset_t mask;
   40 
   41     (void)sigemptyset(&mask);
   42     (void)sigaddset(&mask, SIGRTMIN);
   43     if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
   44         pr_fail_dbg("sigprocmask");
   45         return EXIT_FAILURE;
   46     }
   47     sfd = signalfd(-1, &mask, 0);
   48     if (sfd < 0) {
   49         pr_fail_dbg("signalfd");
   50         return EXIT_FAILURE;
   51     }
   52 
   53 again:
   54     pid = fork();
   55     if (pid < 0) {
   56         if (g_keep_stressing_flag && (errno == EAGAIN))
   57             goto again;
   58         pr_fail_dbg("fork");
   59         return EXIT_FAILURE;
   60     } else if (pid == 0) {
   61         (void)setpgid(0, g_pgrp);
   62         stress_parent_died_alarm();
   63 
   64         while (g_keep_stressing_flag) {
   65             union sigval s;
   66             int ret;
   67 
   68             (void)memset(&s, 0, sizeof(s));
   69             s.sival_int = 0;
   70             ret = sigqueue(ppid, SIGRTMIN, s);
   71             if (ret < 0)
   72                 break;
   73         }
   74         (void)close(sfd);
   75         _exit(0);
   76     } else {
   77         /* Parent */
   78         int status;
   79 
   80         (void)setpgid(pid, g_pgrp);
   81         do {
   82             int ret;
   83             struct signalfd_siginfo fdsi;
   84 
   85             ret = read(sfd, &fdsi, sizeof(fdsi));
   86             if ((ret < 0) || (ret != sizeof(fdsi))) {
   87                 if ((errno == EAGAIN) || (errno == EINTR))
   88                     continue;
   89                 if (errno) {
   90                     pr_fail_dbg("signalfd read");
   91                     (void)close(sfd);
   92                     _exit(EXIT_FAILURE);
   93                 }
   94                 continue;
   95             }
   96             if (ret == 0)
   97                 break;
   98             if (g_opt_flags & OPT_FLAGS_VERIFY) {
   99                 if (fdsi.ssi_signo != (uint32_t)SIGRTMIN) {
  100                     pr_fail("%s: unexpected signal %d",
  101                         args->name, fdsi.ssi_signo);
  102                     break;
  103                 }
  104             }
  105             inc_counter(args);
  106         } while (keep_stressing());
  107 
  108         /* terminal child */
  109         (void)kill(pid, SIGKILL);
  110         (void)waitpid(pid, &status, 0);
  111     }
  112     return EXIT_SUCCESS;
  113 }
  114 
  115 stressor_info_t stress_sigfd_info = {
  116     .stressor = stress_sigfd,
  117     .class = CLASS_INTERRUPT | CLASS_OS
  118 };
  119 #else
  120 stressor_info_t stress_sigfd_info = {
  121     .stressor = stress_not_implemented,
  122     .class = CLASS_INTERRUPT | CLASS_OS
  123 };
  124 #endif