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 | * pipe08.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that a SIGPIPE signal is generated when a write is |
| 26 | * attempted on an empty pipe. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * 1. Write to a pipe after closing the read side. |
| 30 | * 2. Check for the signal SIGPIPE to be received. |
| 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * pipe08 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -f : Turn off functionality Testing. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
| 41 | * USAGE |
| 42 | * pipe08 |
| 43 | * |
| 44 | * HISTORY |
| 45 | * 07/2001 Ported by Wayne Boyer |
| 46 | * |
| 47 | * RESTRICTIONS |
| 48 | * None |
| 49 | */ |
| 50 | #include <errno.h> |
| 51 | #include <unistd.h> |
| 52 | #include <signal.h> |
robbiew | bc3c43e | 2003-07-28 15:05:38 +0000 | [diff] [blame] | 53 | #include <string.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | char *TCID = "pipe08"; |
| 57 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | void setup(void); |
| 60 | void cleanup(void); |
| 61 | void sighandler(int); |
| 62 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 63 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 65 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 66 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | int pipefd[2]; /* fds for pipe read/write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | char wrbuf[BUFSIZ]; |
| 70 | int written, length; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int close_stat; /* exit status of close(read fd) */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 73 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 74 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | setup(); |
| 77 | |
| 78 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 79 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 80 | /* reset tst_count in case we are looping */ |
| 81 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
| 83 | TEST(pipe(pipefd)); |
| 84 | |
| 85 | if (TEST_RETURN != 0) { |
| 86 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if ((close_stat = close(pipefd[0])) == -1) { |
| 91 | tst_brkm(TBROK, cleanup, "close of read side failed"); |
| 92 | } |
| 93 | |
| 94 | strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz\0"); |
| 95 | length = strlen(wrbuf); |
| 96 | |
| 97 | /* |
| 98 | * the SIGPIPE signal will be caught here or else |
| 99 | * the program will dump core when the signal is |
| 100 | * sent |
| 101 | */ |
| 102 | written = write(pipefd[1], wrbuf, length); |
| 103 | } |
| 104 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 105 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * sighandler - catch signals and look for SIGPIPE |
| 111 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 114 | if (sig != SIGPIPE) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | tst_resm(TFAIL, "expected SIGPIPE, got %d", sig); |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 116 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | tst_resm(TPASS, "got expected SIGPIPE signal"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * setup() - performs all ONE TIME setup for this test. |
| 122 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 123 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | tst_sig(NOFORK, sighandler, cleanup); |
| 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | TEST_PAUSE; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 133 | * completion or premature exit. |
| 134 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 135 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | { |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 137 | } |