blob: 96234729a9f2ca4da9e9544f5c23124d6e57dfdd [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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 Cooper8afc5e22010-12-17 02:56:19 -080043#ifndef _GNU_SOURCE
44#define _GNU_SOURCE
45#endif
plars865695b2001-08-27 22:15:12 +000046#include <sys/types.h>
47#include <sys/fcntl.h>
48#include <sys/stat.h>
49#include <errno.h>
plars865695b2001-08-27 22:15:12 +000050#include <signal.h>
Garrett Cooper8afc5e22010-12-17 02:56:19 -080051#include <string.h>
52#include "test.h"
plars865695b2001-08-27 22:15:12 +000053
vapieraa354722006-05-26 06:26:37 +000054void setup();
55void cleanup();
plars865695b2001-08-27 22:15:12 +000056
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020057char *TCID = "dup204";
58int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000059
Garrett Cooper83599b92010-12-18 10:29:05 -080060int fd[2];
61int nfd[2];
plars865695b2001-08-27 22:15:12 +000062
robbiew57ce7e52003-03-25 18:44:20 +000063int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000064{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020065 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
Garrett Cooper8bb58652010-12-18 10:35:38 -080067 int i;
plars865695b2001-08-27 22:15:12 +000068 struct stat oldbuf, newbuf;
69
Garrett Cooper8afc5e22010-12-17 02:56:19 -080070 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080071 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000072
73 setup();
74
plars865695b2001-08-27 22:15:12 +000075 for (lc = 0; TEST_LOOPING(lc); lc++) {
76
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
79 /* loop through the test cases */
subrata_modak56207ce2009-03-23 13:35:39 +000080 for (i = 0; i < TST_TOTAL; i++) {
Garrett Cooper83599b92010-12-18 10:29:05 -080081 TEST(dup2(fd[i], nfd[i]));
plars865695b2001-08-27 22:15:12 +000082
Garrett Cooper83599b92010-12-18 10:29:05 -080083 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +000084 tst_resm(TFAIL, "call failed unexpectedly");
85 continue;
86 }
87
Cyril Hrubise38b9612014-06-02 17:20:57 +020088 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");
plars865695b2001-08-27 22:15:12 +000094
Cyril Hrubise38b9612014-06-02 17:20:57 +020095 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");
plars865695b2001-08-27 22:15:12 +0000101
Garrett Cooper83599b92010-12-18 10:29:05 -0800102 if (close(TEST_RETURN) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 tst_brkm(TBROK | TERRNO, cleanup,
104 "close failed");
plars865695b2001-08-27 22:15:12 +0000105 }
106 }
plars865695b2001-08-27 22:15:12 +0000107
Cyril Hrubise38b9612014-06-02 17:20:57 +0200108 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800109 tst_exit();
plars865695b2001-08-27 22:15:12 +0000110}
111
Mike Frysingerc57fba52014-04-09 18:56:30 -0400112void setup(void)
plars865695b2001-08-27 22:15:12 +0000113{
Garrett Cooper83599b92010-12-18 10:29:05 -0800114 fd[0] = -1;
plars865695b2001-08-27 22:15:12 +0000115
plars865695b2001-08-27 22:15:12 +0000116 tst_sig(FORK, DEF_HANDLER, cleanup);
117
plars865695b2001-08-27 22:15:12 +0000118 TEST_PAUSE;
119
plars865695b2001-08-27 22:15:12 +0000120 tst_tmpdir();
121
Garrett Cooper83599b92010-12-18 10:29:05 -0800122 if (pipe(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 tst_brkm(TBROK | TERRNO, cleanup, "pipe failed");
plars865695b2001-08-27 22:15:12 +0000124}
125
Mike Frysingerc57fba52014-04-09 18:56:30 -0400126void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000127{
Garrett Cooper83599b92010-12-18 10:29:05 -0800128 int i;
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800129
Garrett Cooper6bb4fda2011-01-21 09:36:16 -0800130 for (i = 0; i < (sizeof(fd) / sizeof(fd[0])); i++) {
Garrett Cooper83599b92010-12-18 10:29:05 -0800131 close(fd[i]);
132 close(nfd[i]);
133 }
plars865695b2001-08-27 22:15:12 +0000134
plars865695b2001-08-27 22:15:12 +0000135 tst_rmdir();
Garrett Cooper6bb4fda2011-01-21 09:36:16 -0800136}