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 | * 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
| 51 | char *TCID = "pipe05"; |
| 52 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 54 | intptr_t pipes; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | void setup(void); |
| 56 | void cleanup(void); |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 57 | jmp_buf sig11_recover; |
| 58 | void sig11_handler(int sig); |
| 59 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 60 | int main(int ac, char **av) |
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; |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 64 | struct sigaction sa, osa; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 66 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 67 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
| 69 | setup(); |
| 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 72 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 73 | /* reset tst_count in case we are looping */ |
| 74 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | /* special sig11 case */ |
| 76 | sa.sa_handler = &sig11_handler; |
| 77 | sigemptyset(&sa.sa_mask); |
| 78 | sa.sa_flags = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | sigaction(SIGSEGV, NULL, &osa); |
| 81 | sigaction(SIGSEGV, &sa, NULL); |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | if (setjmp(sig11_recover)) { |
| 84 | TEST_RETURN = -1; |
| 85 | TEST_ERRNO = EFAULT; |
| 86 | } else { |
| 87 | TEST(pipe((int *)pipes)); |
| 88 | } |
| 89 | sigaction(SIGSEGV, &osa, NULL); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | if (TEST_RETURN != -1) { |
| 92 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 93 | } |
| 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | if (TEST_ERRNO != EFAULT) { |
| 96 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 97 | "expected EMFILE", TEST_ERRNO, |
| 98 | strerror(TEST_ERRNO)); |
| 99 | } else { |
| 100 | tst_resm(TPASS, "expected failure - " |
| 101 | "errno = %d : %s", TEST_ERRNO, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } |
| 106 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 107 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
| 112 | * setup() - performs all ONE TIME setup for this test. |
| 113 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 114 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | TEST_PAUSE; |
| 120 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 122 | /****************************************************************** |
| 123 | * sig11_handler() - our segfault recover hack |
| 124 | ******************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | void sig11_handler(int sig) |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 126 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | longjmp(sig11_recover, 1); |
robbiew | 0c87cd8 | 2003-04-29 15:01:27 +0000 | [diff] [blame] | 128 | } |
| 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | /* |
| 131 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 132 | * completion or premature exit. |
| 133 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 134 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 136 | } |