plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * NAME |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 3 | * fcntl01.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
| 5 | * DESCRIPTION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 6 | * Test F_DUPFD, F_SETFL cmds of fcntl |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 7 | * |
| 8 | * CALLS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 9 | * fcntl |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | * |
| 11 | * ALGORITHM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 12 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 13 | * 1. Testing F_DUPFD cmd with arg less than, equal to, and greater |
| 14 | * than the next available file descriptor. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 15 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 16 | * 2. Checking F_SETFL cmd with each valid flag (O_NDELAY, O_APPEND). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 18 | * 3. Checking, setting and reading `close on exec' flag. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | * |
| 20 | * USAGE |
| 21 | * fcntl01 |
| 22 | * |
| 23 | * HISTORY |
| 24 | * 07/2001 Ported by Wayne Boyer |
plars | 7d1952f | 2002-09-25 18:04:41 +0000 | [diff] [blame] | 25 | * 09/2002 added fd2 array to remove statid fds |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * RESTRICTIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 28 | * None |
plars | 7d1952f | 2002-09-25 18:04:41 +0000 | [diff] [blame] | 29 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include <fcntl.h> |
| 33 | #include <errno.h> |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
| 38 | void setup(void); |
| 39 | void cleanup(void); |
| 40 | |
| 41 | char *TCID = "fcntl01"; |
| 42 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 44 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | { |
| 46 | int flags; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | char fname[40]; |
plars | 7d1952f | 2002-09-25 18:04:41 +0000 | [diff] [blame] | 48 | int fd[10], fd2[10]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | int mypid, i; |
| 50 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 51 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 53 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 54 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | setup(); |
| 57 | |
| 58 | /* check for looping state if -i option is given */ |
| 59 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 60 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 61 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | mypid = getpid(); |
| 64 | for (i = 0; i < 8; i++) { |
| 65 | sprintf(fname, "./fcntl%d.%d", i, mypid); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | if ((fd[i] = |
| 67 | open(fname, O_WRONLY | O_CREAT, 0666)) == -1) |
| 68 | tst_resm(TBROK | TERRNO, "open failed"); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 69 | fd2[i] = fd[i]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | } |
| 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | close(fd[2]); |
| 73 | close(fd[3]); |
| 74 | close(fd[4]); |
| 75 | close(fd[5]); |
| 76 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 77 | if ((fd[2] = fcntl(fd[1], F_DUPFD, 1)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | tst_resm(TFAIL | TERRNO, "fcntl(.., 1) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 80 | if (fd[2] < fd2[2]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | tst_resm(TFAIL, "new fd has unexpected value: " |
robbiew | d7cf515 | 2001-09-11 16:48:19 +0000 | [diff] [blame] | 82 | "got %d, expected greater than %d", fd[2], 5); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 84 | if ((fd[4] = fcntl(fd[1], F_DUPFD, fd2[3])) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | tst_resm(TFAIL | TERRNO, "fcntl(.., fd2[3]) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 87 | if (fd[4] < fd2[3]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | tst_resm(TFAIL, "new fd has unexpected value, got %d, " |
plars | 7d1952f | 2002-09-25 18:04:41 +0000 | [diff] [blame] | 89 | "expect greater than %d", fd[4], fd2[3]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 91 | if ((fd[8] = fcntl(fd[1], F_DUPFD, fd2[5])) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | tst_resm(TFAIL | TERRNO, "fcntl(.., fd2[5]) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 94 | if (fd[8] != fd2[5]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | tst_resm(TFAIL, "new fd has unexpected value: " |
plars | 7d1952f | 2002-09-25 18:04:41 +0000 | [diff] [blame] | 96 | "got %d, expected %d", fd[8], fd2[5]); |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 97 | /* //block1: */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | flags = fcntl(fd[2], F_GETFL, 0); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 99 | if ((flags & O_WRONLY) == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x", |
| 101 | flags, O_WRONLY); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
| 103 | /* Check setting of no_delay flag */ |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 104 | if (fcntl(fd[2], F_SETFL, O_NDELAY) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | tst_resm(TBROK | TERRNO, "fcntl(.., O_NDELAY) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
| 107 | flags = fcntl(fd[2], F_GETFL, 0); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | if ((flags & (O_NDELAY | O_WRONLY)) == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | flags, O_NDELAY | O_WRONLY); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
| 112 | /* Check of setting append flag */ |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 113 | if (fcntl(fd[2], F_SETFL, O_APPEND) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_resm(TFAIL | TERRNO, "fcntl(.., O_APPEND) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
| 116 | flags = fcntl(fd[2], F_GETFL, 0); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 117 | if ((flags & (O_APPEND | O_WRONLY)) == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | tst_resm(TFAIL, "unexpected flag ox%x, expected 0x%x", |
| 119 | flags, O_APPEND | O_WRONLY); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
| 121 | /* Check setting flags together */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | if (fcntl(fd[2], F_SETFL, O_NDELAY | O_APPEND) < 0) |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 123 | tst_resm(TFAIL, "fcntl(.., O_NDELAY|O_APPEND) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
| 125 | flags = fcntl(fd[2], F_GETFL, 0); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 126 | if ((flags & (O_NDELAY | O_APPEND | O_WRONLY)) == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | flags, |
| 129 | O_NDELAY | O_APPEND | O_SYNC | O_WRONLY); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
| 131 | /* Check that flags are not cummulative */ |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 132 | if (fcntl(fd[2], F_SETFL, 0) == -1) |
| 133 | tst_resm(TFAIL, "fcntl(.., 0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
| 135 | flags = fcntl(fd[2], F_GETFL, 0); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 136 | if ((flags & O_WRONLY) == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | tst_resm(TFAIL, "unexpected flag 0x%x, expected 0x%x", |
| 138 | flags, O_WRONLY); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 140 | /* //block2: */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | /* |
| 142 | * Check ability to set (F_SETFD) the close on exec flag |
| 143 | */ |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 144 | if ((flags = fcntl(fd[2], F_GETFD, 0)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | tst_resm(TFAIL | TERRNO, |
| 146 | "fcntl(.., F_GETFD, ..) #1 failed"); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 147 | if (flags != 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_resm(TFAIL, "unexpected flags got 0x%x expected " |
| 149 | "0x%x", flags, 0); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 150 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | tst_resm(TFAIL | TERRNO, |
| 154 | "fcntl(.., F_GETFD, ..) #2 failed"); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 155 | if (flags != 1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | tst_resm(TFAIL, "unexpected flags, got 0x%x, " |
| 157 | "expected 0x%x", flags, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 159 | for (i = 0; i < ARRAY_SIZE(fd); i++) |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 160 | close(fd[i]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | for (i = 0; i < 8; i++) { |
| 162 | sprintf(fname, "./fcntl%d.%d", i, mypid); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 163 | if ((unlink(fname)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_resm(TFAIL | TERRNO, |
| 165 | "unlinking %s failed", fname); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 169 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | * setup |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | * performs all ONE TIME setup for this test |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | umask(0); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 180 | TEST_PAUSE; |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 181 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 183 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | tst_rmdir(); |
| 187 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 188 | } |