plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, 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 | */ |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 33 | /* $Id: dup03.c,v 1.5 2009/10/13 14:00:46 subrata_modak Exp $ */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 35 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | * OS Test - Silicon Graphics, Inc. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 37 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * TEST IDENTIFIER : dup03 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 39 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 41 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * TEST TITLE : Negative test for dup(2) (too many fds) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 43 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * PARENT DOCUMENT : usctpl01 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 45 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * TEST CASE TOTAL : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 47 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * WALL CLOCK TIME : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 49 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | * CPU TYPES : ALL |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 51 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | * AUTHOR : Richard Logan |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 53 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * CO-PILOT : William Roske |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 55 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | * DATE STARTED : 06/94 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 57 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | * INITIAL RELEASE : UNICOS 7.0 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | * TEST CASES |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 61 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | * 1.) dup(2) returns...(See Description) |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 63 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | * INPUT SPECIFICATIONS |
| 65 | * The standard options for system call tests are accepted. |
| 66 | * (See the parse_opts(3) man page). |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 67 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | * OUTPUT SPECIFICATIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | *$ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | * DURATION |
| 71 | * Terminates - with frequency and infinite modes. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 72 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | * SIGNALS |
| 74 | * Uses SIGUSR1 to pause before test if option set. |
| 75 | * (See the parse_opts(3) man page). |
| 76 | * |
| 77 | * RESOURCES |
| 78 | * None |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 79 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | * ENVIRONMENTAL NEEDS |
| 81 | * No run-time environmental needs. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 82 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | * SPECIAL PROCEDURAL REQUIREMENTS |
| 84 | * None |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 85 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | * INTERCASE DEPENDENCIES |
| 87 | * None |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 88 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | * DETAILED DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 90 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | * Setup: |
| 92 | * Setup signal handling. |
| 93 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 94 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | * Test: |
| 96 | * Loop if the proper options are given. |
| 97 | * Execute system call |
| 98 | * Check return code, if system call failed (return=-1) |
| 99 | * Log the errno and Issue a FAIL message. |
| 100 | * Otherwise, Issue a PASS message. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 101 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | * Cleanup: |
| 103 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 104 | * |
| 105 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
| 107 | |
| 108 | #include <sys/types.h> |
| 109 | #include <sys/fcntl.h> |
| 110 | #include <errno.h> |
| 111 | #include <string.h> |
| 112 | #include <signal.h> |
| 113 | #include <stdlib.h> |
| 114 | #include "test.h" |
| 115 | #include "usctest.h" |
| 116 | |
| 117 | void setup(); |
| 118 | void cleanup(); |
| 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | char *TCID = "dup03"; /* Test program identifier. */ |
| 121 | int TST_TOTAL = 1; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | char Fname[255]; |
| 125 | int *Fd = NULL; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | int Nfds = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
| 128 | /*********************************************************************** |
| 129 | * Main |
| 130 | ***********************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | int lc; /* loop counter */ |
| 134 | char *msg; /* message returned from parse_opts */ |
| 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | /*************************************************************** |
| 137 | * parse standard options |
| 138 | ***************************************************************/ |
Garrett Cooper | e1f008e | 2010-12-14 00:21:59 -0800 | [diff] [blame^] | 139 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 141 | tst_exit(); |
| 142 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
| 144 | /*************************************************************** |
| 145 | * perform global setup for test |
| 146 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
| 149 | /*************************************************************** |
| 150 | * check looping state if -c option given |
| 151 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | /* reset Tst_count in case we are looping. */ |
| 155 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Call dup(2) |
| 159 | */ |
| 160 | TEST(dup(Fd[0])); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 161 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | /* check return code */ |
| 163 | if (TEST_RETURN == -1) { |
| 164 | if (STD_FUNCTIONAL_TEST) { |
| 165 | if (TEST_ERRNO == EMFILE) { |
| 166 | tst_resm(TPASS, |
| 167 | "dup(%d) Failed, errno=%d : %s", |
| 168 | Fd[0], TEST_ERRNO, |
| 169 | strerror(TEST_ERRNO)); |
| 170 | } else { |
| 171 | tst_resm(TFAIL, |
| 172 | "dup(%d) Failed, errno=%d %s, expected %d (EMFILE)", |
| 173 | Fd[0], TEST_ERRNO, |
| 174 | strerror(TEST_ERRNO), EMFILE); |
| 175 | } |
| 176 | } |
| 177 | } else { |
| 178 | tst_resm(TFAIL, |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 179 | "dup(%d) returned %ld, expected -1, errno:%d (EMFILE)", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | Fd[0], TEST_RETURN, EMFILE); |
| 181 | |
| 182 | /* close the new file so loops do not open too many files */ |
| 183 | if (close(TEST_RETURN) == -1) { |
| 184 | tst_brkm(TBROK, cleanup, |
| 185 | "close(%s) Failed, errno=%d : %s", |
| 186 | Fname, errno, strerror(errno)); |
| 187 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | } /* End for TEST_LOOPING */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
| 192 | /*************************************************************** |
| 193 | * cleanup and exit |
| 194 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | } /* End main */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
| 200 | /*************************************************************** |
| 201 | * setup() - performs all ONE TIME setup for this test. |
| 202 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | long maxfds; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Initialize Fd in case we get a quick signal |
| 209 | */ |
| 210 | maxfds = sysconf(_SC_OPEN_MAX); |
| 211 | if (maxfds < 1) { |
| 212 | tst_brkm(TBROK, cleanup, |
| 213 | "sysconf(_SC_OPEN_MAX) Failed, errno=%d : %s", |
| 214 | errno, strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | Fd = (int *)malloc(maxfds * sizeof(int)); |
| 218 | Fd[0] = -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | /* capture signals */ |
| 221 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 222 | |
| 223 | /* Pause if that option was specified */ |
| 224 | TEST_PAUSE; |
| 225 | |
| 226 | /* make a temp directory and cd to it */ |
| 227 | tst_tmpdir(); |
| 228 | |
| 229 | /* |
| 230 | * open the file as many times as it takes to use up all fds |
| 231 | */ |
| 232 | sprintf(Fname, "dupfile"); |
| 233 | for (Nfds = 1; Nfds <= maxfds; Nfds++) { |
| 234 | if ((Fd[Nfds - 1] = open(Fname, O_RDWR | O_CREAT, 0700)) == -1) { |
| 235 | |
| 236 | Nfds--; /* on a open failure, decrement the counter */ |
| 237 | if (errno == EMFILE) { |
| 238 | break; |
| 239 | } else { /* open failed for some other reason */ |
| 240 | tst_brkm(TBROK, cleanup, |
| 241 | "open(%s, O_RDWR|O_CREAT,0700) Failed, errno=%d : %s", |
| 242 | Fname, errno, strerror(errno)); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * make sure at least one was open and that all fds were opened. |
| 249 | */ |
| 250 | if (Nfds == 0) { |
| 251 | tst_brkm(TBROK, cleanup, "Unable to open at least one file"); |
| 252 | } |
| 253 | if (Nfds > maxfds) { |
| 254 | tst_brkm(TBROK, cleanup, |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 255 | "Unable to open enough files to use all file descriptors, tried %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 256 | maxfds); |
| 257 | } |
| 258 | } /* End setup() */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 259 | |
| 260 | /*************************************************************** |
| 261 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 262 | * completion or premature exit. |
| 263 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 265 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 266 | /* |
| 267 | * print timing stats if that option was specified. |
| 268 | * print errno log if that option was specified. |
| 269 | */ |
| 270 | TEST_CLEANUP; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 272 | /* close the open file we've been dup'ing */ |
| 273 | if (Fd) { |
| 274 | for (; Nfds > 0; Nfds--) { |
| 275 | if (close(Fd[Nfds - 1]) == -1) { |
| 276 | tst_resm(TWARN, |
| 277 | "close(%s) Failed, errno=%d : %s", |
| 278 | Fname, errno, strerror(errno)); |
| 279 | } |
| 280 | Fd[Nfds] = -1; |
| 281 | } |
| 282 | free(Fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 283 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 285 | /* Remove tmp dir and all files in it */ |
| 286 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 287 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 288 | /* exit with return code appropriate for results */ |
| 289 | tst_exit(); |
| 290 | } /* End cleanup() */ |