1 /* 2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 24 * Mountain View, CA 94043, or: 25 * 26 * http://www.sgi.com 27 * 28 * For further information regarding this notice, see: 29 * 30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ 31 * 32 */ 33 /* $Id: dup03.c,v 1.5 2009/10/13 14:00:46 subrata_modak Exp $ */ 34 /********************************************************** 35 * 36 * OS Test - Silicon Graphics, Inc. 37 * 38 * TEST IDENTIFIER : dup03 39 * 40 * EXECUTED BY : anyone 41 * 42 * TEST TITLE : Negative test for dup(2) (too many fds) 43 * 44 * PARENT DOCUMENT : usctpl01 45 * 46 * TEST CASE TOTAL : 1 47 * 48 * WALL CLOCK TIME : 1 49 * 50 * CPU TYPES : ALL 51 * 52 * 53 * DATE STARTED : 06/94 54 * 55 * INITIAL RELEASE : UNICOS 7.0 56 * 57 * TEST CASES 58 * 59 * 1.) dup(2) returns...(See Description) 60 * 61 * INPUT SPECIFICATIONS 62 * The standard options for system call tests are accepted. 63 * (See the parse_opts(3) man page). 64 * 65 * OUTPUT SPECIFICATIONS 66 *$ 67 * DURATION 68 * Terminates - with frequency and infinite modes. 69 * 70 * SIGNALS 71 * Uses SIGUSR1 to pause before test if option set. 72 * (See the parse_opts(3) man page). 73 * 74 * RESOURCES 75 * None 76 * 77 * ENVIRONMENTAL NEEDS 78 * No run-time environmental needs. 79 * 80 * SPECIAL PROCEDURAL REQUIREMENTS 81 * None 82 * 83 * INTERCASE DEPENDENCIES 84 * None 85 * 86 * DETAILED DESCRIPTION 87 * 88 * Setup: 89 * Setup signal handling. 90 * Pause for SIGUSR1 if option specified. 91 * 92 * Test: 93 * Loop if the proper options are given. 94 * Execute system call 95 * Check return code, if system call failed (return=-1) 96 * Log the errno and Issue a FAIL message. 97 * Otherwise, Issue a PASS message. 98 * 99 * Cleanup: 100 * Print errno log and/or timing stats if options given 101 * 102 * 103 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ 104 105 #ifndef _GNU_SOURCE 106 #define _GNU_SOURCE 107 #endif 108 #include <sys/types.h> 109 #include <fcntl.h> 110 #include <errno.h> 111 #include <string.h> 112 #include <signal.h> 113 #include <stdlib.h> 114 #include "test.h" 115 #include "safe_macros.h" 116 117 void setup(); 118 void cleanup(); 119 120 char *TCID = "dup03"; 121 int TST_TOTAL = 1; 122 123 char filename[255]; 124 int *fd = NULL; 125 int nfds = 0; 126 127 int main(int ac, char **av) 128 { 129 int lc; 130 131 tst_parse_opts(ac, av, NULL, NULL); 132 133 setup(); 134 135 for (lc = 0; TEST_LOOPING(lc); lc++) { 136 137 tst_count = 0; 138 139 TEST(dup(fd[0])); 140 141 if (TEST_RETURN == -1) { 142 if (TEST_ERRNO == EMFILE) 143 tst_resm(TPASS, 144 "dup failed as expected with " 145 "EMFILE"); 146 else 147 tst_resm(TFAIL | TTERRNO, 148 "dup failed unexpectedly"); 149 } else { 150 tst_resm(TFAIL, "dup succeeded unexpectedly"); 151 152 SAFE_CLOSE(cleanup, TEST_RETURN); 153 } 154 155 } 156 157 cleanup(); 158 tst_exit(); 159 } 160 161 void setup(void) 162 { 163 long maxfds; 164 165 maxfds = sysconf(_SC_OPEN_MAX); 166 /* 167 * Read the errors section if you're so inclined to determine 168 * why == -1 matters for errno. 169 */ 170 if (maxfds < 1) 171 tst_brkm((maxfds == -1 ? TBROK | TERRNO : TBROK), NULL, 172 "sysconf(_SC_OPEN_MAX) failed"); 173 174 fd = malloc(maxfds * sizeof(int)); 175 if (fd == NULL) 176 tst_brkm(TBROK | TERRNO, NULL, "malloc failed"); 177 fd[0] = -1; 178 179 tst_sig(FORK, DEF_HANDLER, cleanup); 180 181 TEST_PAUSE; 182 183 tst_tmpdir(); 184 185 sprintf(filename, "dupfile"); 186 for (nfds = 1; nfds <= maxfds; nfds++) 187 if ((fd[nfds - 1] = 188 open(filename, O_RDWR | O_CREAT, 0700)) == -1) { 189 if (errno == EMFILE) 190 break; 191 else 192 tst_brkm(TBROK | TBROK, cleanup, "open failed"); 193 nfds--; 194 } 195 196 if (nfds == 0) 197 tst_brkm(TBROK, cleanup, "unable to open at least one file"); 198 if (nfds > maxfds) 199 tst_brkm(TBROK, cleanup, 200 "unable to open enough files to use all file descriptors, " 201 "tried %ld", maxfds); 202 } 203 204 void cleanup(void) 205 { 206 int i; 207 208 for (i = 0; i <= nfds; i++) 209 close(fd[i]); 210 211 tst_rmdir(); 212 } 213