1 Star 0 Fork 2

HoperunHarmony/stress-ng

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stress-ptrace.c 5.71 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#if defined(HAVE_PTRACE)
#include <sys/ptrace.h>
#endif
#if defined(HAVE_PTRACE_REQUEST)
#define shim_ptrace_request enum __ptrace_request
#else
#define shim_ptrace_request int
#endif
static const stress_help_t help[] = {
{ NULL, "ptrace N", "start N workers that trace a child using ptrace" },
{ NULL, "ptrace-ops N", "stop ptrace workers after N system calls are traced" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_PTRACE)
/*
* main syscall ptrace loop
*/
static inline bool stress_syscall_wait(
const stress_args_t *args,
const pid_t pid)
{
while (keep_stressing_flag()) {
int status;
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
if ((errno != ESRCH) && (errno != EPERM) && (errno != EACCES)) {
pr_fail("%s: ptrace failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return true;
}
}
if (shim_waitpid(pid, &status, 0) < 0) {
if ((errno != EINTR) && (errno != ECHILD))
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return true;
}
if (WIFSTOPPED(status) &&
(WSTOPSIG(status) & 0x80))
return false;
if (WIFEXITED(status))
return true;
}
return true;
}
/*
* stress_ptrace()
* stress ptracing
*/
static int stress_ptrace(const stress_args_t *args)
{
pid_t pid;
stress_set_proc_state(args->name, STRESS_STATE_RUN);
again:
pid = fork();
if (pid < 0) {
if (stress_redo_fork(errno))
goto again;
if (!keep_stressing(args))
goto finish;
pr_fail("%s: fork failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
} else if (pid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
(void)sched_settings_apply(true);
/*
* Child to be traced, we abort if we detect
* we are already being traced by someone else
* as this makes life way too complex
*/
if (ptrace(PTRACE_TRACEME) != 0) {
pr_inf_skip("%s: child cannot be traced, "
"skipping stressor: errno=%d (%s)\n",
args->name, errno, strerror(errno));
_exit(EXIT_SUCCESS);
}
/* Wait for parent to start tracing me */
(void)kill(getpid(), SIGSTOP);
/*
* A simple mix of system calls
*/
while (keep_stressing_flag()) {
pid_t pidtmp;
gid_t gidtmp;
uid_t uidtmp;
time_t ttmp;
pidtmp = getppid();
(void)pidtmp;
#if defined(HAVE_GETPGRP)
pidtmp = getpgrp();
(void)pidtmp;
#endif
gidtmp = getgid();
(void)gidtmp;
gidtmp = getegid();
(void)gidtmp;
uidtmp = getuid();
(void)uidtmp;
uidtmp = geteuid();
(void)uidtmp;
ttmp = time(NULL);
(void)ttmp;
}
_exit(0);
} else {
/* Parent to do the tracing */
int status;
int i = 0;
(void)setpgid(pid, g_pgrp);
if (shim_waitpid(pid, &status, 0) < 0) {
if ((errno != EINTR) && (errno != ECHILD)) {
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
if (ptrace(PTRACE_SETOPTIONS, pid,
0, PTRACE_O_TRACESYSGOOD) < 0) {
pr_inf_skip("%s: child cannot be traced, "
"skipping stressor: errno=%d (%s)\n",
args->name, errno, strerror(errno));
if ((errno == ESRCH) || (errno == EPERM) || (errno == EACCES)) {
/* Ensure child is really dead and reap */
(void)kill(pid, SIGKILL);
if (shim_waitpid(pid, &status, 0) < 0) {
if ((errno != EINTR) && (errno != ECHILD)) {
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
return WEXITSTATUS(status);
}
pr_fail("%s: ptrace failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
do {
/*
* We do two of the following per syscall,
* one at the start, and one at the end to catch
* the return. In this stressor we don't really
* care which is which, we just care about counting
* them
*/
if (stress_syscall_wait(args, pid))
break;
/* periodicially perform invalid ptrace calls */
if ((i & 0x1ff) == 0) {
const pid_t bad_pid = stress_get_unused_pid_racy(false);
int ret;
/* exercise invalid options */
ret = ptrace((shim_ptrace_request)~0L, pid, 0, PTRACE_O_TRACESYSGOOD);
(void)ret;
/* exercise invalid pid */
ret = ptrace(PTRACE_SETOPTIONS, bad_pid,
0, PTRACE_O_TRACESYSGOOD);
(void)ret;
}
i++;
inc_counter(args);
} while (keep_stressing(args));
/* Terminate child */
(void)kill(pid, SIGKILL);
if (shim_waitpid(pid, &status, 0) < 0)
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
finish:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return EXIT_SUCCESS;
}
stressor_info_t stress_ptrace_info = {
.stressor = stress_ptrace,
.class = CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_ptrace_info = {
.stressor = stress_not_implemented,
.class = CLASS_OS,
.help = help
};
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hoperun_harmony/stress-ng.git
git@gitee.com:hoperun_harmony/stress-ng.git
hoperun_harmony
stress-ng
stress-ng
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385