blob: 76099c86afde98ba8bf8685ca8a93c0a223cd3e5 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis421e2512012-11-28 17:02:17 +01002 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis421e2512012-11-28 17:02:17 +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 Hrubis421e2512012-11-28 17:02:17 +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 Hrubis421e2512012-11-28 17:02:17 +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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
Cyril Hrubis421e2512012-11-28 17:02:17 +010021 * 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.
plars865695b2001-08-27 22:15:12 +000024 */
Cyril Hrubis421e2512012-11-28 17:02:17 +010025
plars865695b2001-08-27 22:15:12 +000026#include <unistd.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <wait.h>
30
31#include "test.h"
plars865695b2001-08-27 22:15:12 +000032
Cyril Hrubis421e2512012-11-28 17:02:17 +010033char *TCID = "pause02";
34int TST_TOTAL = 1;
Cyril Hrubis421e2512012-11-28 17:02:17 +010035static pid_t cpid;
plars865695b2001-08-27 22:15:12 +000036
Cyril Hrubis421e2512012-11-28 17:02:17 +010037static void do_child(void);
38static void setup(void);
39static void cleanup(void);
robbiewd34d5812005-07-11 22:28:09 +000040
subrata_modak56207ce2009-03-23 13:35:39 +000041int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000042{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020043 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020044 const char *msg;
Cyril Hrubis421e2512012-11-28 17:02:17 +010045 int status;
subrata_modak56207ce2009-03-23 13:35:39 +000046
Garrett Coopera9e49f12010-12-16 10:54:03 -080047 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080048 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Cyril Hrubis421e2512012-11-28 17:02:17 +010049
robbiewd34d5812005-07-11 22:28:09 +000050#ifdef UCLINUX
Cyril Hrubis421e2512012-11-28 17:02:17 +010051 maybe_run_child(&do_child, "");
robbiewd34d5812005-07-11 22:28:09 +000052#endif
53
plars865695b2001-08-27 22:15:12 +000054 setup();
55
plars865695b2001-08-27 22:15:12 +000056 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080057 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000058
Cyril Hrubis421e2512012-11-28 17:02:17 +010059 cpid = FORK_OR_VFORK();
60 switch (cpid) {
61 case -1:
plars865695b2001-08-27 22:15:12 +000062 tst_brkm(TBROK, cleanup, "fork() failed");
Cyril Hrubis421e2512012-11-28 17:02:17 +010063 break;
64 case 0:
robbiewd34d5812005-07-11 22:28:09 +000065#ifdef UCLINUX
Cyril Hrubis421e2512012-11-28 17:02:17 +010066 if (self_exec(av[0], "") < 0)
robbiewd34d5812005-07-11 22:28:09 +000067 tst_brkm(TBROK, cleanup, "self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +000068#else
69 do_child();
70#endif
Cyril Hrubis421e2512012-11-28 17:02:17 +010071 break;
72 default:
73 break;
plars865695b2001-08-27 22:15:12 +000074 }
75
Cyril Hrubis40b465a2012-12-11 18:57:52 +010076 /*
77 * Wait for child to enter pause().
78 */
79 TST_PROCESS_STATE_WAIT(cleanup, cpid, 'S');
plars865695b2001-08-27 22:15:12 +000080
81 /*
82 * Send the SIGINT signal now, so that child
83 * returns from pause and resumes execution.
84 */
85 kill(cpid, SIGINT);
86
plars865695b2001-08-27 22:15:12 +000087 wait(&status);
88
Cyril Hrubis421e2512012-11-28 17:02:17 +010089 if (WIFEXITED(status)) {
90 if (WEXITSTATUS(status) == 0)
91 tst_resm(TPASS, "pause was interrupted correctly");
92 else
Cyril Hrubis40b465a2012-12-11 18:57:52 +010093 tst_resm(TFAIL, "pause was interrupted but the "
Cyril Hrubis421e2512012-11-28 17:02:17 +010094 "retval and/or errno was wrong");
Cyril Hrubis40b465a2012-12-11 18:57:52 +010095 continue;
plars865695b2001-08-27 22:15:12 +000096 }
Cyril Hrubis40b465a2012-12-11 18:57:52 +010097
98 if (WIFSIGNALED(status)) {
99 switch (WTERMSIG(status)) {
100 case SIGALRM:
101 tst_resm(TFAIL, "Timeout: SIGINT wasn't recieved by child");
102 break;
103 default:
104 tst_resm(TFAIL, "Child killed by signal");
105 }
106
107 continue;
108 }
109
110 tst_resm(TFAIL, "Pause was not interrupted");
Garrett Cooper2c282152010-12-16 00:55:50 -0800111 }
plars865695b2001-08-27 22:15:12 +0000112
plars865695b2001-08-27 22:15:12 +0000113 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800114 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800115}
plars865695b2001-08-27 22:15:12 +0000116
Cyril Hrubis421e2512012-11-28 17:02:17 +0100117static void sig_handle(int sig)
robbiewd34d5812005-07-11 22:28:09 +0000118{
Cyril Hrubis421e2512012-11-28 17:02:17 +0100119}
120
121static void do_child(void)
122{
Cyril Hrubis40b465a2012-12-11 18:57:52 +0100123 /* avoid LTP framework to do whatever it likes */
124 signal(SIGALRM, SIG_DFL);
125
Cyril Hrubis421e2512012-11-28 17:02:17 +0100126 if (signal(SIGINT, sig_handle) == SIG_ERR) {
127 fprintf(stderr, "Child: Failed to setup signal handler\n");
128 exit(1);
129 }
130
Cyril Hrubis40b465a2012-12-11 18:57:52 +0100131 /* Commit suicide after 10 seconds */
132 alarm(10);
133
robbiewd34d5812005-07-11 22:28:09 +0000134 TEST(pause());
135
robbiewd34d5812005-07-11 22:28:09 +0000136 if (TEST_RETURN == -1) {
Cyril Hrubis421e2512012-11-28 17:02:17 +0100137 if (TEST_ERRNO == EINTR)
138 exit(0);
139
140 fprintf(stderr, "Child: Pause returned -1 but errno is %d (%s)\n",
141 TEST_ERRNO, strerror(TEST_ERRNO));
142 exit(1);
robbiewd34d5812005-07-11 22:28:09 +0000143 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000144
Cyril Hrubis421e2512012-11-28 17:02:17 +0100145 fprintf(stderr, "Child: Pause returned %ld\n", TEST_RETURN);
146 exit(1);
robbiewd34d5812005-07-11 22:28:09 +0000147}
148
Cyril Hrubis421e2512012-11-28 17:02:17 +0100149static void setup(void)
plars865695b2001-08-27 22:15:12 +0000150{
plars865695b2001-08-27 22:15:12 +0000151 tst_sig(FORK, DEF_HANDLER, cleanup);
152
plars865695b2001-08-27 22:15:12 +0000153 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000154}
155
Cyril Hrubis421e2512012-11-28 17:02:17 +0100156static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000157{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700158}