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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * pipe05.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check what happens when pipe is passed a bad file descriptor. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Issue the pipe call with a bad file descriptor. |
| 29 | * Check that we get EFAULT. |
| 30 | * |
| 31 | * USAGE: <for command-line> |
| 32 | * pipe05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 33 | * where, -c n : Run n copies concurrently. |
| 34 | * -e : Turn on errno logging. |
| 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 Ported by Wayne Boyer |
| 42 | * |
| 43 | * RESTRICTIONS |
| 44 | * None |
| 45 | */ |
| 46 | #include <fcntl.h> |
| 47 | #include <errno.h> |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 48 | #include <setjmp.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #include "test.h" |
| 50 | #include "usctest.h" |
| 51 | |
| 52 | char *TCID = "pipe05"; |
| 53 | int TST_TOTAL = 1; |
| 54 | extern int Tst_count; |
| 55 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 56 | int exp_enos[] = { EFAULT, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 58 | intptr_t pipes; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | void setup(void); |
| 60 | void cleanup(void); |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 61 | jmp_buf sig11_recover; |
| 62 | void sig11_handler(int sig); |
| 63 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 64 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | int lc; /* loop counter */ |
| 67 | char *msg; /* message returned from parse_opts */ |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 68 | struct sigaction sa, osa; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | /* parse standard options */ |
Garrett Cooper | e1f008e | 2010-12-14 00:21:59 -0800 | [diff] [blame^] | 71 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | /*NOTREACHED*/} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
| 75 | setup(); |
| 76 | |
| 77 | TEST_EXP_ENOS(exp_enos); |
| 78 | |
| 79 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 80 | |
| 81 | /* reset Tst_count in case we are looping */ |
| 82 | Tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | /* special sig11 case */ |
| 84 | sa.sa_handler = &sig11_handler; |
| 85 | sigemptyset(&sa.sa_mask); |
| 86 | sa.sa_flags = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | sigaction(SIGSEGV, NULL, &osa); |
| 89 | sigaction(SIGSEGV, &sa, NULL); |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | if (setjmp(sig11_recover)) { |
| 92 | TEST_RETURN = -1; |
| 93 | TEST_ERRNO = EFAULT; |
| 94 | } else { |
| 95 | TEST(pipe((int *)pipes)); |
| 96 | } |
| 97 | sigaction(SIGSEGV, &osa, NULL); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | if (TEST_RETURN != -1) { |
| 100 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 101 | } |
| 102 | |
| 103 | TEST_ERROR_LOG(TEST_ERRNO); |
| 104 | |
| 105 | if (TEST_ERRNO != EFAULT) { |
| 106 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 107 | "expected EMFILE", TEST_ERRNO, |
| 108 | strerror(TEST_ERRNO)); |
| 109 | } else { |
| 110 | tst_resm(TPASS, "expected failure - " |
| 111 | "errno = %d : %s", TEST_ERRNO, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | } |
| 116 | cleanup(); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 117 | return 0; |
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 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | { |
| 125 | /* capture signals */ |
| 126 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 127 | |
| 128 | /* Pause if that option was specified */ |
| 129 | TEST_PAUSE; |
| 130 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 132 | /****************************************************************** |
| 133 | * sig11_handler() - our segfault recover hack |
| 134 | ******************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | void sig11_handler(int sig) |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 136 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | longjmp(sig11_recover, 1); |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 138 | } |
| 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | /* |
| 141 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 142 | * completion or premature exit. |
| 143 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
| 146 | /* |
| 147 | * print timing stats if that option was specified. |
| 148 | * print errno log if that option was specified. |
| 149 | */ |
| 150 | TEST_CLEANUP; |
| 151 | |
| 152 | /* exit with return code appropriate for results */ |
| 153 | tst_exit(); |
| 154 | } |