1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
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 Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * Test Description:
22 * Verify that nanosleep() will fail to suspend the execution
23 * of a process for a specified time if interrupted by a non-blocked signal.
24 *
25 * Expected Result:
26 * nanosleep() should return with -1 value and sets errno to EINTR.
27 */
28
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <wait.h>
34
35 #include "test.h"
36
37 char *TCID = "nanosleep03";
38 int TST_TOTAL = 1;
39
40 static void do_child(void);
41 static void setup(void);
42 static void sig_handler();
43
main(int ac,char ** av)44 int main(int ac, char **av)
45 {
46 int lc;
47 pid_t cpid;
48 int status;
49
50 tst_parse_opts(ac, av, NULL, NULL);
51
52 #ifdef UCLINUX
53 maybe_run_child(&do_child, "dddd", &timereq.tv_sec, &timereq.tv_nsec,
54 &timerem.tv_sec, &timerem.tv_nsec);
55 #endif
56
57 setup();
58
59 for (lc = 0; TEST_LOOPING(lc); lc++) {
60 tst_count = 0;
61
62 /*
63 * Creat a child process and suspend its
64 * execution using nanosleep()
65 */
66 if ((cpid = FORK_OR_VFORK()) == -1)
67 tst_brkm(TBROK, NULL, "fork() failed");
68
69 if (cpid == 0) {
70 #ifdef UCLINUX
71 if (self_exec(av[0], "dddd",
72 timereq.tv_sec, timereq.tv_nsec,
73 timerem.tv_sec, timerem.tv_nsec) < 0) {
74 tst_brkm(TBROK, NULL, "self_exec failed");
75 }
76 #else
77 do_child();
78 #endif
79 }
80
81 sleep(1);
82
83 /* Now send signal to child */
84 if (kill(cpid, SIGINT) < 0) {
85 tst_brkm(TBROK, NULL,
86 "kill() fails send signal to child");
87 }
88
89 /* Wait for child to execute */
90 wait(&status);
91 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
92 tst_resm(TPASS, "nanosleep() failed, interrupted"
93 " by signal (%d) as expected", EINTR);
94 } else {
95 tst_resm(TFAIL, "child process exited abnormally; "
96 "status = %d", status);
97 }
98 }
99
100 tst_exit();
101 }
102
do_child(void)103 static void do_child(void)
104 {
105 struct timespec timereq = {.tv_sec = 5, .tv_nsec = 9999};
106 struct timespec timerem;
107
108 /*
109 * Call nanosleep() to suspend child process
110 * for specified time 'tv_sec'.
111 * Call should return before suspending execution
112 * for the specified time due to receipt of signal
113 * from Parent.
114 */
115 TEST(nanosleep(&timereq, &timerem));
116
117 if (TEST_RETURN == -1) {
118
119 /* Check for expected errno is set */
120 if (TEST_ERRNO != EINTR) {
121 tst_resm(TFAIL | TTERRNO,
122 "nanosleep() failed; expected errno: %d",
123 EINTR);
124 exit(1);
125 }
126 } else {
127 tst_resm(TFAIL, "nanosleep() returns %ld, "
128 "expected -1, errno:%d", TEST_RETURN, EINTR);
129 exit(1);
130 }
131
132 exit(0);
133 }
134
setup(void)135 static void setup(void)
136 {
137 tst_sig(FORK, DEF_HANDLER, NULL);
138
139 TEST_PAUSE;
140
141 if (signal(SIGINT, sig_handler) == SIG_ERR) {
142 tst_brkm(TBROK, NULL,
143 "signal() fails to setup signal handler");
144 }
145
146 }
147
sig_handler(void)148 static void sig_handler(void)
149 {
150 }
151