blob: 2c9af6e594c2d8b8ae36748ba066d64f95c2e5cc [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis400b6222015-02-19 12:59:27 +01002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis400b6222015-02-19 12:59:27 +01005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis400b6222015-02-19 12:59:27 +010010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis400b6222015-02-19 12:59:27 +010015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * 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.
plars865695b2001-08-27 22:15:12 +000027 */
Cyril Hrubis400b6222015-02-19 12:59:27 +010028
plars865695b2001-08-27 22:15:12 +000029#include <errno.h>
30#include <unistd.h>
31#include <fcntl.h>
robbiew01be3332003-03-26 23:48:41 +000032#include <time.h>
33#include <wait.h>
plars865695b2001-08-27 22:15:12 +000034
35#include "test.h"
plars865695b2001-08-27 22:15:12 +000036
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020037char *TCID = "nanosleep03";
38int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000039
Cyril Hrubis400b6222015-02-19 12:59:27 +010040static void do_child(void);
41static void setup(void);
42static void sig_handler();
plars865695b2001-08-27 22:15:12 +000043
subrata_modak56207ce2009-03-23 13:35:39 +000044int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000045{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020046 int lc;
Cyril Hrubis400b6222015-02-19 12:59:27 +010047 pid_t cpid;
48 int status;
subrata_modak56207ce2009-03-23 13:35:39 +000049
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010050 tst_parse_opts(ac, av, NULL, NULL);
Cyril Hrubis400b6222015-02-19 12:59:27 +010051
robbiewd34d5812005-07-11 22:28:09 +000052#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
plars865695b2001-08-27 22:15:12 +000057 setup();
58
plars865695b2001-08-27 22:15:12 +000059 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080060 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000061
62 /*
63 * Creat a child process and suspend its
64 * execution using nanosleep()
65 */
Cyril Hrubis400b6222015-02-19 12:59:27 +010066 if ((cpid = FORK_OR_VFORK()) == -1)
67 tst_brkm(TBROK, NULL, "fork() failed");
plars865695b2001-08-27 22:15:12 +000068
Cyril Hrubis400b6222015-02-19 12:59:27 +010069 if (cpid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000070#ifdef UCLINUX
71 if (self_exec(av[0], "dddd",
72 timereq.tv_sec, timereq.tv_nsec,
73 timerem.tv_sec, timerem.tv_nsec) < 0) {
Cyril Hrubis400b6222015-02-19 12:59:27 +010074 tst_brkm(TBROK, NULL, "self_exec failed");
plars865695b2001-08-27 22:15:12 +000075 }
robbiewd34d5812005-07-11 22:28:09 +000076#else
77 do_child();
78#endif
plars865695b2001-08-27 22:15:12 +000079 }
80
plars865695b2001-08-27 22:15:12 +000081 sleep(1);
82
83 /* Now send signal to child */
84 if (kill(cpid, SIGINT) < 0) {
Cyril Hrubis400b6222015-02-19 12:59:27 +010085 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +000086 "kill() fails send signal to child");
87 }
88
89 /* Wait for child to execute */
90 wait(&status);
yaberauneyaf00ed7a2009-12-22 17:35:13 +000091 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
92 tst_resm(TPASS, "nanosleep() failed, interrupted"
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 " by signal (%d) as expected", EINTR);
yaberauneyaf00ed7a2009-12-22 17:35:13 +000094 } else {
95 tst_resm(TFAIL, "child process exited abnormally; "
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 "status = %d", status);
plars865695b2001-08-27 22:15:12 +000097 }
Garrett Cooper2c282152010-12-16 00:55:50 -080098 }
plars865695b2001-08-27 22:15:12 +000099
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800100 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800101}
plars865695b2001-08-27 22:15:12 +0000102
Cyril Hrubis400b6222015-02-19 12:59:27 +0100103static void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000104{
Cyril Hrubis400b6222015-02-19 12:59:27 +0100105 struct timespec timereq = {.tv_sec = 5, .tv_nsec = 9999};
106 struct timespec timerem;
107
subrata_modak4bb656a2009-02-26 12:02:09 +0000108 /*
robbiewd34d5812005-07-11 22:28:09 +0000109 * 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
robbiewd34d5812005-07-11 22:28:09 +0000117 if (TEST_RETURN == -1) {
118
robbiewd34d5812005-07-11 22:28:09 +0000119 /* Check for expected errno is set */
120 if (TEST_ERRNO != EINTR) {
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000121 tst_resm(TFAIL | TTERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 "nanosleep() failed; expected errno: %d",
123 EINTR);
robbiewd34d5812005-07-11 22:28:09 +0000124 exit(1);
125 }
126 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000127 tst_resm(TFAIL, "nanosleep() returns %ld, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 "expected -1, errno:%d", TEST_RETURN, EINTR);
robbiewd34d5812005-07-11 22:28:09 +0000129 exit(1);
130 }
131
robbiewd34d5812005-07-11 22:28:09 +0000132 exit(0);
133}
134
Cyril Hrubis400b6222015-02-19 12:59:27 +0100135static void setup(void)
plars865695b2001-08-27 22:15:12 +0000136{
Cyril Hrubis400b6222015-02-19 12:59:27 +0100137 tst_sig(FORK, DEF_HANDLER, NULL);
plars865695b2001-08-27 22:15:12 +0000138
plars865695b2001-08-27 22:15:12 +0000139 TEST_PAUSE;
140
plars865695b2001-08-27 22:15:12 +0000141 if (signal(SIGINT, sig_handler) == SIG_ERR) {
Cyril Hrubis400b6222015-02-19 12:59:27 +0100142 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +0000143 "signal() fails to setup signal handler");
144 }
145
plars865695b2001-08-27 22:15:12 +0000146}
147
Cyril Hrubis400b6222015-02-19 12:59:27 +0100148static void sig_handler(void)
plars865695b2001-08-27 22:15:12 +0000149{
150}