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 | * dup204.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of dup2(2). |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * attempt to call dup2() on read/write ends of a pipe |
| 29 | * |
| 30 | * USAGE: <for command-line> |
| 31 | * dup204 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 32 | * where, -c n : Run n copies concurrently. |
| 33 | * -f : Turn off functionality Testing. |
| 34 | * -i n : Execute test n times. |
| 35 | * -I x : Execute test for x seconds. |
| 36 | * -P x : Pause for x seconds between iterations. |
| 37 | * -t : Turn on syscall timing. |
| 38 | * |
| 39 | * RESTRICTION |
| 40 | * NONE |
| 41 | */ |
| 42 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 43 | #ifndef _GNU_SOURCE |
| 44 | #define _GNU_SOURCE |
| 45 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <sys/fcntl.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <errno.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | #include <signal.h> |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 51 | #include <string.h> |
| 52 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
vapier | aa35472 | 2006-05-26 06:26:37 +0000 | [diff] [blame] | 54 | void setup(); |
| 55 | void cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 57 | char *TCID = "dup204"; |
| 58 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 60 | int fd[2]; |
| 61 | int nfd[2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +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; |
Garrett Cooper | 8bb5865 | 2010-12-18 10:35:38 -0800 | [diff] [blame] | 67 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | struct stat oldbuf, newbuf; |
| 69 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 70 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 71 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | setup(); |
| 74 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 76 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 77 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | /* loop through the test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | for (i = 0; i < TST_TOTAL; i++) { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 81 | TEST(dup2(fd[i], nfd[i])); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 83 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 85 | continue; |
| 86 | } |
| 87 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 88 | if (fstat(fd[i], &oldbuf) == -1) |
| 89 | tst_brkm(TBROK, cleanup, "fstat() #1 " |
| 90 | "failed"); |
| 91 | if (fstat(nfd[i], &newbuf) == -1) |
| 92 | tst_brkm(TBROK, cleanup, "fstat() #2 " |
| 93 | "failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 95 | if (oldbuf.st_ino != newbuf.st_ino) |
| 96 | tst_resm(TFAIL, "original and duped " |
| 97 | "inodes do not match"); |
| 98 | else |
| 99 | tst_resm(TPASS, "original and duped " |
| 100 | "inodes are the same"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 102 | if (close(TEST_RETURN) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | tst_brkm(TBROK | TERRNO, cleanup, |
| 104 | "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 108 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 109 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 112 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 114 | fd[0] = -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | TEST_PAUSE; |
| 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | tst_tmpdir(); |
| 121 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 122 | if (pipe(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | tst_brkm(TBROK | TERRNO, cleanup, "pipe failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 126 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 128 | int i; |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 129 | |
Garrett Cooper | 6bb4fda | 2011-01-21 09:36:16 -0800 | [diff] [blame] | 130 | for (i = 0; i < (sizeof(fd) / sizeof(fd[0])); i++) { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 131 | close(fd[i]); |
| 132 | close(nfd[i]); |
| 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_rmdir(); |
Garrett Cooper | 6bb4fda | 2011-01-21 09:36:16 -0800 | [diff] [blame] | 136 | } |