xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pause/pause02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  * Copyright (c) 2012 Cyril Hrubis <[email protected]>
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Verify that, pause() returns -1 and sets errno to EINTR after receipt of a
22  * signal which is caught by the calling process. Also, verify that the calling
23  * process will resume execution from the point of suspension.
24  */
25 
26 #include <unistd.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/wait.h>
30 
31 #include "test.h"
32 
33 char *TCID = "pause02";
34 int TST_TOTAL = 1;
35 static pid_t cpid;
36 
37 static void do_child(void);
38 static void setup(void);
39 static void cleanup(void);
40 
main(int ac,char ** av)41 int main(int ac, char **av)
42 {
43 	int lc;
44 	int status;
45 
46 	tst_parse_opts(ac, av, NULL, NULL);
47 
48 	setup();
49 
50 	for (lc = 0; TEST_LOOPING(lc); lc++) {
51 		tst_count = 0;
52 
53 		cpid = tst_fork();
54 		switch (cpid) {
55 		case -1:
56 			tst_brkm(TBROK, cleanup, "fork() failed");
57 		break;
58 		case 0:
59 			do_child();
60 		break;
61 		default:
62 		break;
63 		}
64 
65 		/*
66 		 * Wait for child to enter pause().
67 		 */
68 		TST_PROCESS_STATE_WAIT(cleanup, cpid, 'S');
69 
70 		/*
71 		 * Send the SIGINT signal now, so that child
72 		 * returns from pause and resumes execution.
73 		 */
74 		kill(cpid, SIGINT);
75 
76 		wait(&status);
77 
78 		if (WIFEXITED(status)) {
79 			if (WEXITSTATUS(status) == 0)
80 				tst_resm(TPASS, "pause was interrupted correctly");
81 			else
82 				tst_resm(TFAIL, "pause was interrupted but the "
83 				                "retval and/or errno was wrong");
84 			continue;
85 		}
86 
87 		if (WIFSIGNALED(status)) {
88 			switch (WTERMSIG(status)) {
89 			case SIGALRM:
90 				tst_resm(TFAIL, "Timeout: SIGINT wasn't received by child");
91 			break;
92 			default:
93 				tst_resm(TFAIL, "Child killed by signal");
94 			}
95 
96 			continue;
97 		}
98 
99 		tst_resm(TFAIL, "Pause was not interrupted");
100 	}
101 
102 	cleanup();
103 	tst_exit();
104 }
105 
sig_handle(int sig)106 static void sig_handle(int sig)
107 {
108 }
109 
do_child(void)110 static void do_child(void)
111 {
112 	/* avoid LTP framework to do whatever it likes */
113 	signal(SIGALRM, SIG_DFL);
114 
115 	if (signal(SIGINT, sig_handle) == SIG_ERR) {
116 		fprintf(stderr, "Child: Failed to setup signal handler\n");
117 		exit(1);
118 	}
119 
120 	/* Commit suicide after 10 seconds */
121 	alarm(10);
122 
123 	TEST(pause());
124 
125 	if (TEST_RETURN == -1) {
126 		if (TEST_ERRNO == EINTR)
127 			exit(0);
128 
129 		fprintf(stderr, "Child: Pause returned -1 but errno is %d (%s)\n",
130 		        TEST_ERRNO, strerror(TEST_ERRNO));
131 		exit(1);
132 	}
133 
134 	fprintf(stderr, "Child: Pause returned %ld\n", TEST_RETURN);
135 	exit(1);
136 }
137 
setup(void)138 static void setup(void)
139 {
140 	tst_sig(FORK, DEF_HANDLER, cleanup);
141 
142 	TEST_PAUSE;
143 }
144 
cleanup(void)145 static void cleanup(void)
146 {
147 }
148