• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) International Business Machines  Corp., 2002
3  *    ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
4  *   Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
5  *
6  *   This program is free software;  you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  *   the GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program;  if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22   WHAT:  Does dup return -1 on the 21st file?
23   HOW:   Create up to _NFILE (20) files and check for -1 return on the
24          next attempt
25          Should check NOFILE as well as _NFILE.  19-Jun-84 Dale.
26 */
27 
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include "test.h"
36 
37 char *TCID = "dup06";
38 int TST_TOTAL = 1;
39 
cnt_free_fds(int maxfd)40 static int cnt_free_fds(int maxfd)
41 {
42 	int freefds = 0;
43 
44 	for (maxfd--; maxfd >= 0; maxfd--)
45 		if (fcntl(maxfd, F_GETFD) == -1 && errno == EBADF)
46 			freefds++;
47 
48 	return (freefds);
49 }
50 
51 static void setup(void);
52 static void cleanup(void);
53 
main(int ac,char ** av)54 int main(int ac, char **av)
55 {
56 	int *fildes, i;
57 	int min;
58 	int freefds;
59 	int lc;
60 	const char *pfilname = "dup06";
61 
62 	tst_parse_opts(ac, av, NULL, NULL);
63 
64 	setup();
65 
66 	min = getdtablesize();
67 	freefds = cnt_free_fds(min);
68 	fildes = malloc((min + 5) * sizeof(int));
69 
70 	for (i = 0; i < min + 5; i++)
71 		fildes[i] = 0;
72 
73 	for (lc = 0; TEST_LOOPING(lc); lc++) {
74 		unlink(pfilname);
75 
76 		if ((fildes[0] = creat(pfilname, 0666)) == -1) {
77 			tst_resm(TFAIL, "Cannot open first file");
78 		} else {
79 			for (i = 1; i < min + 5; i++) {
80 				if ((fildes[i] = dup(fildes[i - 1])) == -1)
81 					break;
82 			}
83 			if (i < freefds) {
84 				tst_resm(TFAIL, "Not enough files duped");
85 			} else if (i > freefds) {
86 				tst_resm(TFAIL, "Too many files duped");
87 			} else {
88 				tst_resm(TPASS, "Test passed.");
89 			}
90 		}
91 
92 		unlink(pfilname);
93 
94 		for (i = 0; i < min + 5; i++) {
95 			if (fildes[i] != 0 && fildes[i] != -1)
96 				close(fildes[i]);
97 
98 			fildes[i] = 0;
99 		}
100 	}
101 
102 	cleanup();
103 	tst_exit();
104 }
105 
setup(void)106 static void setup(void)
107 {
108 	tst_tmpdir();
109 }
110 
cleanup(void)111 static void cleanup(void)
112 {
113 	tst_rmdir();
114 }
115