1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * Description:
9*49cdfc7eSAndroid Build Coastguard Worker * Set CPU time limit for a process and check its behavior
10*49cdfc7eSAndroid Build Coastguard Worker * after reaching CPU time limit.
11*49cdfc7eSAndroid Build Coastguard Worker * 1) Process got SIGXCPU after reaching soft limit of CPU time.
12*49cdfc7eSAndroid Build Coastguard Worker * 2) Process got SIGKILL after reaching hard limit of CPU time.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * Note:
15*49cdfc7eSAndroid Build Coastguard Worker * This is also a regression test for the following kernel bug:
16*49cdfc7eSAndroid Build Coastguard Worker * 'c3bca5d450b62 ("posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated")'
17*49cdfc7eSAndroid Build Coastguard Worker */
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker static int *end;
32*49cdfc7eSAndroid Build Coastguard Worker
sighandler(int sig)33*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker *end = sig;
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker
setup(void)38*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGNAL(SIGXCPU, sighandler);
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker end = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE,
43*49cdfc7eSAndroid Build Coastguard Worker MAP_SHARED | MAP_ANONYMOUS, -1, 0);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker if (end)
49*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(end, sizeof(int));
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
verify_setrlimit(void)52*49cdfc7eSAndroid Build Coastguard Worker static void verify_setrlimit(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker int status;
55*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker *end = 0;
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
60*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
61*49cdfc7eSAndroid Build Coastguard Worker struct rlimit rlim = {
62*49cdfc7eSAndroid Build Coastguard Worker .rlim_cur = 1,
63*49cdfc7eSAndroid Build Coastguard Worker .rlim_max = 2,
64*49cdfc7eSAndroid Build Coastguard Worker };
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker TEST(setrlimit(RLIMIT_CPU, &rlim));
67*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
69*49cdfc7eSAndroid Build Coastguard Worker "setrlimit(RLIMIT_CPU) failed");
70*49cdfc7eSAndroid Build Coastguard Worker exit(1);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker alarm(20);
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker while (1);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, 0);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
81*49cdfc7eSAndroid Build Coastguard Worker return;
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker if (WIFSIGNALED(status)) {
84*49cdfc7eSAndroid Build Coastguard Worker if (WTERMSIG(status) == SIGKILL && *end == SIGXCPU) {
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
86*49cdfc7eSAndroid Build Coastguard Worker "Got SIGXCPU then SIGKILL after reaching both limit");
87*49cdfc7eSAndroid Build Coastguard Worker return;
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker if (WTERMSIG(status) == SIGKILL && !*end) {
91*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
92*49cdfc7eSAndroid Build Coastguard Worker "Got only SIGKILL after reaching both limit");
93*49cdfc7eSAndroid Build Coastguard Worker return;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker if (WTERMSIG(status) == SIGALRM && *end == SIGXCPU) {
97*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
98*49cdfc7eSAndroid Build Coastguard Worker "Got only SIGXCPU after reaching both limit");
99*49cdfc7eSAndroid Build Coastguard Worker return;
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (WTERMSIG(status) == SIGALRM && !*end) {
103*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
104*49cdfc7eSAndroid Build Coastguard Worker "Got no signal after reaching both limit");
105*49cdfc7eSAndroid Build Coastguard Worker return;
106*49cdfc7eSAndroid Build Coastguard Worker }
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child %s", tst_strstatus(status));
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
113*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_setrlimit,
114*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
115*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
116*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
117*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
118*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "c3bca5d450b62"},
119*49cdfc7eSAndroid Build Coastguard Worker {}
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker };
122