1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Google, Inc.
16 #include <sys/ptrace.h>
18 #include <sys/timerfd.h>
19 #include <sys/types.h>
22 #include "../kselftest.h"
30 if (sched_setaffinity(0, sizeof(set), &set) != 0) {
31 ksft_print_msg("sched_setaffinity() failed: %s\n",
36 if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
37 ksft_print_msg("ptrace(PTRACE_TRACEME) failed: %s\n",
42 if (raise(SIGSTOP) != 0) {
43 ksft_print_msg("raise(SIGSTOP) failed: %s\n", strerror(errno));
57 ksft_print_msg("fork() failed: %s\n", strerror(errno));
63 wpid = waitpid(pid, &status, __WALL);
65 ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
68 if (!WIFSTOPPED(status)) {
69 ksft_print_msg("child did not stop: %s\n", strerror(errno));
72 if (WSTOPSIG(status) != SIGSTOP) {
73 ksft_print_msg("child did not stop with SIGSTOP: %s\n",
78 if (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) {
81 "ptrace(PTRACE_SINGLESTEP) not supported on this architecture: %s\n",
85 ksft_print_msg("ptrace(PTRACE_SINGLESTEP) failed: %s\n",
90 wpid = waitpid(pid, &status, __WALL);
92 ksft_print_msg("waitpid() failed: $s\n", strerror(errno));
95 if (WIFEXITED(status)) {
96 ksft_print_msg("child did not single-step: %s\n",
100 if (!WIFSTOPPED(status)) {
101 ksft_print_msg("child did not stop: %s\n", strerror(errno));
104 if (WSTOPSIG(status) != SIGTRAP) {
105 ksft_print_msg("child did not stop with SIGTRAP: %s\n",
110 if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
111 ksft_print_msg("ptrace(PTRACE_CONT) failed: %s\n",
116 wpid = waitpid(pid, &status, __WALL);
118 ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
121 if (!WIFEXITED(status)) {
122 ksft_print_msg("child did not exit after PTRACE_CONT: %s\n",
133 struct sigevent event = {};
136 struct itimerspec spec = {};
139 ksft_exit_skip("Please run the test as root - Exiting.\n");
141 power_state_fd = open("/sys/power/state", O_RDWR);
142 if (power_state_fd < 0)
144 "open(\"/sys/power/state\") failed %s)\n",
147 timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
149 ksft_exit_fail_msg("timerfd_create() failed\n");
151 spec.it_value.tv_sec = 5;
152 err = timerfd_settime(timerfd, 0, &spec, NULL);
154 ksft_exit_fail_msg("timerfd_settime() failed\n");
156 if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem"))
157 ksft_exit_fail_msg("Failed to enter Suspend state\n");
160 close(power_state_fd);
163 int main(int argc, char **argv)
166 bool do_suspend = true;
167 bool succeeded = true;
168 unsigned int tests = 0;
169 cpu_set_t available_cpus;
175 while ((opt = getopt(argc, argv, "n")) != -1) {
181 printf("Usage: %s [-n]\n", argv[0]);
182 printf(" -n: do not trigger a suspend/resume cycle before the test\n");
187 err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus);
189 ksft_exit_fail_msg("sched_getaffinity() failed\n");
191 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
192 if (!CPU_ISSET(cpu, &available_cpus))
200 ksft_set_plan(tests);
201 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
204 if (!CPU_ISSET(cpu, &available_cpus))
207 test_success = run_test(cpu);
208 switch (test_success) {
210 ksft_test_result_pass("CPU %d\n", cpu);
213 ksft_test_result_skip("CPU %d\n", cpu);
216 ksft_test_result_fail("CPU %d\n", cpu);