plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 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 | /* |
| 21 | * NAME |
| 22 | * waitpid01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that when a child kills itself by generating an alarm |
| 26 | * exception, the waiting parent is correctly notified. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * Fork a child that sets an alarm. When the alarm goes off, causing |
| 30 | * the death of the child, the parent checks that SIG_ALRM was returned |
| 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * waitpid01 [-c n] [-i n] [-I x] [-P x] [-t] |
| 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -i n : Execute test n times. |
| 36 | * -I x : Execute test for x seconds. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * History |
| 41 | * 07/2001 John George |
| 42 | * -Ported |
| 43 | * |
| 44 | * Restrictions |
| 45 | * None |
| 46 | */ |
| 47 | |
| 48 | #include <sys/signal.h> |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/wait.h> |
| 51 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 52 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 54 | static void setup(void); |
| 55 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
| 57 | char *TCID = "waitpid01"; |
| 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 60 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 62 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 63 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | int pid, npid, sig, nsig; |
| 66 | int exno, nexno, status; |
| 67 | |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 68 | msg = parse_opts(argc, argv, NULL, NULL); |
| 69 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | setup(); |
| 73 | |
| 74 | /* check for looping state if -i option is given */ |
| 75 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 76 | /* reset tst_count in case we are looping */ |
| 77 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | exno = 1; |
| 80 | sig = 14; |
| 81 | |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 82 | pid = FORK_OR_VFORK(); |
| 83 | if (pid < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_brkm(TFAIL, cleanup, "Fork Failed"); |
| 85 | } else if (pid == 0) { |
| 86 | alarm(2); |
| 87 | pause(); |
| 88 | exit(exno); |
| 89 | } else { |
| 90 | errno = 0; |
| 91 | while (((npid = waitpid(pid, &status, 0)) != -1) || |
| 92 | (errno == EINTR)) { |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 93 | if (errno == EINTR) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | continue; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | if (npid != pid) { |
| 97 | tst_resm(TFAIL, "waitpid error: " |
| 98 | "unexpected pid returned"); |
| 99 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | tst_resm(TPASS, |
| 101 | "recieved expected pid"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | nsig = WTERMSIG(status); |
| 105 | |
| 106 | /* |
| 107 | * nsig is the signal number returned by |
| 108 | * waitpid |
| 109 | */ |
| 110 | if (nsig != sig) { |
| 111 | tst_resm(TFAIL, "waitpid error: " |
| 112 | "unexpected signal " |
| 113 | "returned"); |
| 114 | } else { |
| 115 | tst_resm(TPASS, "recieved expected " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | "signal"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * nexno is the exit number returned by |
| 121 | * waitpid |
| 122 | */ |
| 123 | nexno = WEXITSTATUS(status); |
| 124 | if (nexno != 0) { |
| 125 | tst_resm(TFAIL, "signal error: " |
| 126 | "unexpected exit number " |
| 127 | "returned"); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 134 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 137 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | TEST_PAUSE; |
| 140 | } |
| 141 | |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 142 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | tst_sig(FORK, DEF_HANDLER, cleanup); |
Wanlong Gao | 23e8983 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 145 | } |