2 * Copyright (C) 2016 Google, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
25 #include <sys/ptrace.h>
27 #include <sys/timerfd.h>
28 #include <sys/types.h>
31 #include "../kselftest.h"
39 if (sched_setaffinity(0, sizeof(set
), &set
) != 0) {
40 ksft_print_msg("sched_setaffinity() failed: %s\n",
45 if (ptrace(PTRACE_TRACEME
, 0, NULL
, NULL
) != 0) {
46 ksft_print_msg("ptrace(PTRACE_TRACEME) failed: %s\n",
51 if (raise(SIGSTOP
) != 0) {
52 ksft_print_msg("raise(SIGSTOP) failed: %s\n", strerror(errno
));
59 bool run_test(int cpu
)
66 ksft_print_msg("fork() failed: %s\n", strerror(errno
));
72 wpid
= waitpid(pid
, &status
, __WALL
);
74 ksft_print_msg("waitpid() failed: %s\n", strerror(errno
));
77 if (!WIFSTOPPED(status
)) {
78 ksft_print_msg("child did not stop: %s\n", strerror(errno
));
81 if (WSTOPSIG(status
) != SIGSTOP
) {
82 ksft_print_msg("child did not stop with SIGSTOP: %s\n",
87 if (ptrace(PTRACE_SINGLESTEP
, pid
, NULL
, NULL
) < 0) {
90 "ptrace(PTRACE_SINGLESTEP) not supported on this architecture: %s\n",
93 ksft_print_msg("ptrace(PTRACE_SINGLESTEP) failed: %s\n",
98 wpid
= waitpid(pid
, &status
, __WALL
);
100 ksft_print_msg("waitpid() failed: $s\n", strerror(errno
));
103 if (WIFEXITED(status
)) {
104 ksft_print_msg("child did not single-step: %s\n",
108 if (!WIFSTOPPED(status
)) {
109 ksft_print_msg("child did not stop: %s\n", strerror(errno
));
112 if (WSTOPSIG(status
) != SIGTRAP
) {
113 ksft_print_msg("child did not stop with SIGTRAP: %s\n",
118 if (ptrace(PTRACE_CONT
, pid
, NULL
, NULL
) < 0) {
119 ksft_print_msg("ptrace(PTRACE_CONT) failed: %s\n",
124 wpid
= waitpid(pid
, &status
, __WALL
);
126 ksft_print_msg("waitpid() failed: %s\n", strerror(errno
));
129 if (!WIFEXITED(status
)) {
130 ksft_print_msg("child did not exit after PTRACE_CONT: %s\n",
141 struct sigevent event
= {};
144 struct itimerspec spec
= {};
147 ksft_exit_skip("Please run the test as root - Exiting.\n");
149 power_state_fd
= open("/sys/power/state", O_RDWR
);
150 if (power_state_fd
< 0)
152 "open(\"/sys/power/state\") failed %s)\n",
155 timerfd
= timerfd_create(CLOCK_BOOTTIME_ALARM
, 0);
157 ksft_exit_fail_msg("timerfd_create() failed\n");
159 spec
.it_value
.tv_sec
= 5;
160 err
= timerfd_settime(timerfd
, 0, &spec
, NULL
);
162 ksft_exit_fail_msg("timerfd_settime() failed\n");
164 if (write(power_state_fd
, "mem", strlen("mem")) != strlen("mem"))
165 ksft_exit_fail_msg("Failed to enter Suspend state\n");
168 close(power_state_fd
);
171 int main(int argc
, char **argv
)
174 bool do_suspend
= true;
175 bool succeeded
= true;
176 cpu_set_t available_cpus
;
182 while ((opt
= getopt(argc
, argv
, "n")) != -1) {
188 printf("Usage: %s [-n]\n", argv
[0]);
189 printf(" -n: do not trigger a suspend/resume cycle before the test\n");
197 err
= sched_getaffinity(0, sizeof(available_cpus
), &available_cpus
);
199 ksft_exit_fail_msg("sched_getaffinity() failed\n");
201 for (cpu
= 0; cpu
< CPU_SETSIZE
; cpu
++) {
204 if (!CPU_ISSET(cpu
, &available_cpus
))
207 test_success
= run_test(cpu
);
209 ksft_test_result_pass("CPU %d\n", cpu
);
211 ksft_test_result_fail("CPU %d\n", cpu
);