blob: 0f2f7c5f94e08cb18dc8b046c3be4315c959fe6a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +00003 * fcntl01.c
plars865695b2001-08-27 22:15:12 +00004 *
5 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +00006 * Test F_DUPFD, F_SETFL cmds of fcntl
plars865695b2001-08-27 22:15:12 +00007 *
8 * CALLS
subrata_modak56207ce2009-03-23 13:35:39 +00009 * fcntl
plars865695b2001-08-27 22:15:12 +000010 *
11 * ALGORITHM
subrata_modak4bb656a2009-02-26 12:02:09 +000012 *
subrata_modak56207ce2009-03-23 13:35:39 +000013 * 1. Testing F_DUPFD cmd with arg less than, equal to, and greater
14 * than the next available file descriptor.
plars865695b2001-08-27 22:15:12 +000015 *
subrata_modak56207ce2009-03-23 13:35:39 +000016 * 2. Checking F_SETFL cmd with each valid flag (O_NDELAY, O_APPEND).
plars865695b2001-08-27 22:15:12 +000017 *
subrata_modak56207ce2009-03-23 13:35:39 +000018 * 3. Checking, setting and reading `close on exec' flag.
plars865695b2001-08-27 22:15:12 +000019 *
20 * USAGE
21 * fcntl01
22 *
23 * HISTORY
24 * 07/2001 Ported by Wayne Boyer
plars7d1952f2002-09-25 18:04:41 +000025 * 09/2002 added fd2 array to remove statid fds
plars865695b2001-08-27 22:15:12 +000026 *
27 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000028 * None
plars7d1952f2002-09-25 18:04:41 +000029 *
plars865695b2001-08-27 22:15:12 +000030 */
31
32#include <fcntl.h>
33#include <errno.h>
robbiew4cf80962003-03-25 21:53:41 +000034#include <sys/types.h>
35#include <sys/stat.h>
36#include "test.h"
plars865695b2001-08-27 22:15:12 +000037
38void setup(void);
39void cleanup(void);
40
41char *TCID = "fcntl01";
42int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000043
robbiew4cf80962003-03-25 21:53:41 +000044int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000045{
46 int flags;
plars865695b2001-08-27 22:15:12 +000047 char fname[40];
plars7d1952f2002-09-25 18:04:41 +000048 int fd[10], fd2[10];
plars865695b2001-08-27 22:15:12 +000049 int mypid, i;
50 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020051 const char *msg;
plars865695b2001-08-27 22:15:12 +000052
Garrett Cooperc737f2f2010-12-18 19:58:34 -080053 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080054 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000055
56 setup();
57
58 /* check for looping state if -i option is given */
59 for (lc = 0; TEST_LOOPING(lc); lc++) {
60
Caspar Zhangd59a6592013-03-07 14:59:12 +080061 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000062
plars865695b2001-08-27 22:15:12 +000063 mypid = getpid();
64 for (i = 0; i < 8; i++) {
65 sprintf(fname, "./fcntl%d.%d", i, mypid);
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 if ((fd[i] =
67 open(fname, O_WRONLY | O_CREAT, 0666)) == -1)
68 tst_resm(TBROK | TERRNO, "open failed");
Garrett Cooperc737f2f2010-12-18 19:58:34 -080069 fd2[i] = fd[i];
plars865695b2001-08-27 22:15:12 +000070 }
71
plars865695b2001-08-27 22:15:12 +000072 close(fd[2]);
73 close(fd[3]);
74 close(fd[4]);
75 close(fd[5]);
76
Garrett Cooperc737f2f2010-12-18 19:58:34 -080077 if ((fd[2] = fcntl(fd[1], F_DUPFD, 1)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 tst_resm(TFAIL | TERRNO, "fcntl(.., 1) failed");
plars865695b2001-08-27 22:15:12 +000079
Garrett Cooperc737f2f2010-12-18 19:58:34 -080080 if (fd[2] < fd2[2])
plars865695b2001-08-27 22:15:12 +000081 tst_resm(TFAIL, "new fd has unexpected value: "
robbiewd7cf5152001-09-11 16:48:19 +000082 "got %d, expected greater than %d", fd[2], 5);
plars865695b2001-08-27 22:15:12 +000083
Garrett Cooperc737f2f2010-12-18 19:58:34 -080084 if ((fd[4] = fcntl(fd[1], F_DUPFD, fd2[3])) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 tst_resm(TFAIL | TERRNO, "fcntl(.., fd2[3]) failed");
plars865695b2001-08-27 22:15:12 +000086
Garrett Cooperc737f2f2010-12-18 19:58:34 -080087 if (fd[4] < fd2[3])
plars865695b2001-08-27 22:15:12 +000088 tst_resm(TFAIL, "new fd has unexpected value, got %d, "
plars7d1952f2002-09-25 18:04:41 +000089 "expect greater than %d", fd[4], fd2[3]);
plars865695b2001-08-27 22:15:12 +000090
Garrett Cooperc737f2f2010-12-18 19:58:34 -080091 if ((fd[8] = fcntl(fd[1], F_DUPFD, fd2[5])) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 tst_resm(TFAIL | TERRNO, "fcntl(.., fd2[5]) failed");
plars865695b2001-08-27 22:15:12 +000093
Garrett Cooperc737f2f2010-12-18 19:58:34 -080094 if (fd[8] != fd2[5])
plars865695b2001-08-27 22:15:12 +000095 tst_resm(TFAIL, "new fd has unexpected value: "
plars7d1952f2002-09-25 18:04:41 +000096 "got %d, expected %d", fd[8], fd2[5]);
mridgedb639212005-01-04 21:04:11 +000097/* //block1: */
plars865695b2001-08-27 22:15:12 +000098 flags = fcntl(fd[2], F_GETFL, 0);
Garrett Cooperc737f2f2010-12-18 19:58:34 -080099 if ((flags & O_WRONLY) == 0)
plars865695b2001-08-27 22:15:12 +0000100 tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x",
101 flags, O_WRONLY);
plars865695b2001-08-27 22:15:12 +0000102
103 /* Check setting of no_delay flag */
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800104 if (fcntl(fd[2], F_SETFL, O_NDELAY) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 tst_resm(TBROK | TERRNO, "fcntl(.., O_NDELAY) failed");
plars865695b2001-08-27 22:15:12 +0000106
107 flags = fcntl(fd[2], F_GETFL, 0);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 if ((flags & (O_NDELAY | O_WRONLY)) == 0)
plars865695b2001-08-27 22:15:12 +0000109 tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 flags, O_NDELAY | O_WRONLY);
plars865695b2001-08-27 22:15:12 +0000111
112 /* Check of setting append flag */
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800113 if (fcntl(fd[2], F_SETFL, O_APPEND) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_resm(TFAIL | TERRNO, "fcntl(.., O_APPEND) failed");
plars865695b2001-08-27 22:15:12 +0000115
116 flags = fcntl(fd[2], F_GETFL, 0);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 if ((flags & (O_APPEND | O_WRONLY)) == 0)
plars865695b2001-08-27 22:15:12 +0000118 tst_resm(TFAIL, "unexpected flag ox%x, expected 0x%x",
119 flags, O_APPEND | O_WRONLY);
plars865695b2001-08-27 22:15:12 +0000120
121 /* Check setting flags together */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 if (fcntl(fd[2], F_SETFL, O_NDELAY | O_APPEND) < 0)
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800123 tst_resm(TFAIL, "fcntl(.., O_NDELAY|O_APPEND) failed");
plars865695b2001-08-27 22:15:12 +0000124
125 flags = fcntl(fd[2], F_GETFL, 0);
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800126 if ((flags & (O_NDELAY | O_APPEND | O_WRONLY)) == 0)
plars865695b2001-08-27 22:15:12 +0000127 tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 flags,
129 O_NDELAY | O_APPEND | O_SYNC | O_WRONLY);
plars865695b2001-08-27 22:15:12 +0000130
131 /* Check that flags are not cummulative */
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800132 if (fcntl(fd[2], F_SETFL, 0) == -1)
133 tst_resm(TFAIL, "fcntl(.., 0) failed");
plars865695b2001-08-27 22:15:12 +0000134
135 flags = fcntl(fd[2], F_GETFL, 0);
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800136 if ((flags & O_WRONLY) == 0)
plars865695b2001-08-27 22:15:12 +0000137 tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x",
138 flags, O_WRONLY);
plars865695b2001-08-27 22:15:12 +0000139
mridgedb639212005-01-04 21:04:11 +0000140/* //block2: */
plars865695b2001-08-27 22:15:12 +0000141 /*
142 * Check ability to set (F_SETFD) the close on exec flag
143 */
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800144 if ((flags = fcntl(fd[2], F_GETFD, 0)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_resm(TFAIL | TERRNO,
146 "fcntl(.., F_GETFD, ..) #1 failed");
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800147 if (flags != 0)
plars865695b2001-08-27 22:15:12 +0000148 tst_resm(TFAIL, "unexpected flags got 0x%x expected "
149 "0x%x", flags, 0);
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800150 if ((flags = fcntl(fd[2], F_SETFD, 1)) == -1)
151 tst_resm(TFAIL, "fcntl(.., F_SETFD, ..) failed");
152 if ((flags = fcntl(fd[2], F_GETFD, 0)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 tst_resm(TFAIL | TERRNO,
154 "fcntl(.., F_GETFD, ..) #2 failed");
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800155 if (flags != 1)
plars865695b2001-08-27 22:15:12 +0000156 tst_resm(TFAIL, "unexpected flags, got 0x%x, "
157 "expected 0x%x", flags, 1);
plars865695b2001-08-27 22:15:12 +0000158
Cyril Hrubisb863a0b2014-09-24 13:15:29 +0200159 for (i = 0; i < ARRAY_SIZE(fd); i++)
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800160 close(fd[i]);
plars865695b2001-08-27 22:15:12 +0000161 for (i = 0; i < 8; i++) {
162 sprintf(fname, "./fcntl%d.%d", i, mypid);
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800163 if ((unlink(fname)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_resm(TFAIL | TERRNO,
165 "unlinking %s failed", fname);
plars865695b2001-08-27 22:15:12 +0000166 }
167 }
168 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800169 tst_exit();
plars865695b2001-08-27 22:15:12 +0000170}
171
172/*
173 * setup
subrata_modak56207ce2009-03-23 13:35:39 +0000174 * performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +0000175 */
subrata_modak56207ce2009-03-23 13:35:39 +0000176void setup(void)
plars865695b2001-08-27 22:15:12 +0000177{
Garrett Cooper2c282152010-12-16 00:55:50 -0800178 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000179 umask(0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800180 TEST_PAUSE;
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800181 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000182}
subrata_modakbdbaec52009-02-26 12:14:51 +0000183
subrata_modak56207ce2009-03-23 13:35:39 +0000184void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000185{
plars865695b2001-08-27 22:15:12 +0000186 tst_rmdir();
187
Chris Dearmanec6edca2012-10-17 19:54:01 -0700188}