plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | */ |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 25 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <wait.h> |
| 30 | |
| 31 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 33 | char *TCID = "pause02"; |
| 34 | int TST_TOTAL = 1; |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 35 | static pid_t cpid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 37 | static void do_child(void); |
| 38 | static void setup(void); |
| 39 | static void cleanup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 40 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 43 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 44 | const char *msg; |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 45 | int status; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 47 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 48 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 49 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 50 | #ifdef UCLINUX |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 51 | maybe_run_child(&do_child, ""); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | setup(); |
| 55 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 57 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 59 | cpid = FORK_OR_VFORK(); |
| 60 | switch (cpid) { |
| 61 | case -1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | tst_brkm(TBROK, cleanup, "fork() failed"); |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 63 | break; |
| 64 | case 0: |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 65 | #ifdef UCLINUX |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 66 | if (self_exec(av[0], "") < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 67 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 68 | #else |
| 69 | do_child(); |
| 70 | #endif |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 71 | break; |
| 72 | default: |
| 73 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Cyril Hrubis | 40b465a | 2012-12-11 18:57:52 +0100 | [diff] [blame] | 76 | /* |
| 77 | * Wait for child to enter pause(). |
| 78 | */ |
| 79 | TST_PROCESS_STATE_WAIT(cleanup, cpid, 'S'); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Send the SIGINT signal now, so that child |
| 83 | * returns from pause and resumes execution. |
| 84 | */ |
| 85 | kill(cpid, SIGINT); |
| 86 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | wait(&status); |
| 88 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 89 | if (WIFEXITED(status)) { |
| 90 | if (WEXITSTATUS(status) == 0) |
| 91 | tst_resm(TPASS, "pause was interrupted correctly"); |
| 92 | else |
Cyril Hrubis | 40b465a | 2012-12-11 18:57:52 +0100 | [diff] [blame] | 93 | tst_resm(TFAIL, "pause was interrupted but the " |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 94 | "retval and/or errno was wrong"); |
Cyril Hrubis | 40b465a | 2012-12-11 18:57:52 +0100 | [diff] [blame] | 95 | continue; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | } |
Cyril Hrubis | 40b465a | 2012-12-11 18:57:52 +0100 | [diff] [blame] | 97 | |
| 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 111 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 114 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 115 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 117 | static void sig_handle(int sig) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 118 | { |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void do_child(void) |
| 122 | { |
Cyril Hrubis | 40b465a | 2012-12-11 18:57:52 +0100 | [diff] [blame] | 123 | /* avoid LTP framework to do whatever it likes */ |
| 124 | signal(SIGALRM, SIG_DFL); |
| 125 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 126 | if (signal(SIGINT, sig_handle) == SIG_ERR) { |
| 127 | fprintf(stderr, "Child: Failed to setup signal handler\n"); |
| 128 | exit(1); |
| 129 | } |
| 130 | |
Cyril Hrubis | 40b465a | 2012-12-11 18:57:52 +0100 | [diff] [blame] | 131 | /* Commit suicide after 10 seconds */ |
| 132 | alarm(10); |
| 133 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 134 | TEST(pause()); |
| 135 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 136 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 137 | 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); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 143 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 144 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 145 | fprintf(stderr, "Child: Pause returned %ld\n", TEST_RETURN); |
| 146 | exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 149 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Cyril Hrubis | 421e251 | 2012-11-28 17:02:17 +0100 | [diff] [blame] | 156 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 158 | } |