"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-ptrace.c" (15 Mar 2019, 3767 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-ptrace.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_PTRACE)
   28 
   29 /*
   30  *  main syscall ptrace loop
   31  */
   32 static inline bool stress_syscall_wait(
   33     const args_t *args,
   34     const pid_t pid)
   35 {
   36     while (g_keep_stressing_flag) {
   37         int status;
   38 
   39         if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
   40             pr_fail_dbg("ptrace");
   41             return true;
   42         }
   43         if (waitpid(pid, &status, 0) < 0) {
   44             if (errno != EINTR)
   45                 pr_fail_dbg("waitpid");
   46             return true;
   47         }
   48 
   49         if (WIFSTOPPED(status) &&
   50             (WSTOPSIG(status) & 0x80))
   51             return false;
   52         if (WIFEXITED(status))
   53             return true;
   54     }
   55     return true;
   56 }
   57 
   58 /*
   59  *  stress_ptrace()
   60  *  stress ptracing
   61  */
   62 static int stress_ptrace(const args_t *args)
   63 {
   64     pid_t pid;
   65 
   66     pid = fork();
   67     if (pid < 0) {
   68         pr_fail_dbg("fork");
   69         return EXIT_FAILURE;
   70     } else if (pid == 0) {
   71         (void)setpgid(0, g_pgrp);
   72         stress_parent_died_alarm();
   73 
   74         /*
   75          * Child to be traced, we abort if we detect
   76          * we are already being traced by someone else
   77          * as this makes life way too complex
   78          */
   79         if (ptrace(PTRACE_TRACEME) != 0) {
   80             pr_fail("%s: ptrace child being traced "
   81                 "already, aborting\n", args->name);
   82             _exit(0);
   83         }
   84         /* Wait for parent to start tracing me */
   85         (void)kill(getpid(), SIGSTOP);
   86 
   87         /*
   88          *  A simple mix of system calls
   89          */
   90         while (g_keep_stressing_flag) {
   91             pid_t pidtmp;
   92             gid_t gidtmp;
   93             uid_t uidtmp;
   94             time_t ttmp;
   95 
   96             pidtmp = getppid();
   97             (void)pidtmp;
   98 
   99 #if defined(HAVE_GETPGRP)
  100             pidtmp = getpgrp();
  101             (void)pidtmp;
  102 #endif
  103 
  104             gidtmp = getgid();
  105             (void)gidtmp;
  106 
  107             gidtmp = getegid();
  108             (void)gidtmp;
  109 
  110             uidtmp = getuid();
  111             (void)uidtmp;
  112 
  113             uidtmp = geteuid();
  114             (void)uidtmp;
  115 
  116             ttmp = time(NULL);
  117             (void)ttmp;
  118         }
  119         _exit(0);
  120     } else {
  121         /* Parent to do the tracing */
  122         int status;
  123 
  124         (void)setpgid(pid, g_pgrp);
  125 
  126         if (waitpid(pid, &status, 0) < 0) {
  127             if (errno != EINTR) {
  128                 pr_fail_dbg("waitpid");
  129                 return EXIT_FAILURE;
  130             }
  131             return EXIT_SUCCESS;
  132         }
  133         if (ptrace(PTRACE_SETOPTIONS, pid,
  134             0, PTRACE_O_TRACESYSGOOD) < 0) {
  135             pr_fail_dbg("ptrace");
  136             return EXIT_FAILURE;
  137         }
  138 
  139         do {
  140             /*
  141              *  We do two of the following per syscall,
  142              *  one at the start, and one at the end to catch
  143              *  the return.  In this stressor we don't really
  144              *  care which is which, we just care about counting
  145              *  them
  146              */
  147             if (stress_syscall_wait(args, pid))
  148                 break;
  149             inc_counter(args);
  150         } while (keep_stressing());
  151 
  152         /* Terminate child */
  153         (void)kill(pid, SIGKILL);
  154         if (waitpid(pid, &status, 0) < 0)
  155             pr_fail_dbg("waitpid");
  156     }
  157     return EXIT_SUCCESS;
  158 }
  159 
  160 stressor_info_t stress_ptrace_info = {
  161     .stressor = stress_ptrace,
  162     .class = CLASS_OS
  163 };
  164 #else
  165 stressor_info_t stress_ptrace_info = {
  166     .stressor = stress_not_implemented,
  167     .class = CLASS_OS
  168 };
  169 #endif