getdents02.c revision ec19461cecdac81c48bbbe4783624167754349a2
1/* 2 * 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20/* 21 * NAME 22 * getdents02.c 23 * 24 * DESCRIPTION 25 * getdents02 - check that we get a failure with a bad file descriptor 26 * 27 * ALGORITHM 28 * loop if that option was specified 29 * issue the system call using a bad file descriptor 30 * check the errno value 31 * issue a PASS message if we get EBADF - errno 9 32 * otherwise, the tests fails 33 * issue a FAIL message 34 * break any remaining tests 35 * call cleanup 36 * 37 * USAGE: <for command-line> 38 * getdents02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 39 * where, -c n : Run n copies concurrently. 40 * -e : Turn on errno logging. 41 * -i n : Execute test n times. 42 * -I x : Execute test for x seconds. 43 * -P x : Pause for x seconds between iterations. 44 * -t : Turn on syscall timing. 45 * 46 * HISTORY 47 * 03/2001 - Written by Wayne Boyer 48 * 49 * RESTRICTIONS 50 * none 51 */ 52 53#include "getdents.h" 54#include "test.h" 55#include "usctest.h" 56 57#include <errno.h> 58#include <fcntl.h> 59#include <linux/types.h> 60#include <dirent.h> 61#include <linux/unistd.h> 62#include <unistd.h> 63 64void cleanup(void); 65void setup(void); 66 67char *TCID = "getdents02"; 68int TST_TOTAL = 1; 69 70int exp_enos[] = { EBADF, 0 }; /* 0 terminated list of expected errnos */ 71 72int main(int ac, char **av) 73{ 74 int lc; 75 char *msg; 76 int rval, fd; 77 int count; 78 size_t size = 0; 79 char *dir_name = NULL; 80 struct dirent *dirp; 81 82 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 83 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 84 85 setup(); 86 87 for (lc = 0; TEST_LOOPING(lc); lc++) { 88 Tst_count = 0; 89 90 if ((dir_name = getcwd(dir_name, size)) == NULL) 91 tst_brkm(TBROK, cleanup, "Can not get current " 92 "directory name"); 93 94 if ((dirp = malloc(sizeof(struct dirent))) == NULL) 95 tst_brkm(TBROK, cleanup, "malloc failed"); 96 97 count = (int)sizeof(struct dirent); 98 99 /* set up a bad file descriptor */ 100 101 fd = -5; 102 103 rval = getdents(fd, dirp, count); 104 105 /* 106 * Hopefully we get an error due to the bad file descriptor. 107 */ 108 if (rval < 0) { 109 TEST_ERROR_LOG(errno); 110 111 switch (errno) { 112 case EBADF: 113 tst_resm(TPASS, 114 "failed as expected with EBADF"); 115 break; 116 default: 117 tst_resm(TFAIL|TERRNO, 118 "getdents failed unexpectedly"); 119 break; 120 } 121 } else 122 tst_resm(TFAIL, "call succeeded unexpectedly"); 123 124 free(dir_name); 125 dir_name = NULL; 126 127 free(dirp); 128 } 129 130 cleanup(); 131 132 tst_exit(); 133} 134 135void setup(void) 136{ 137 138 tst_sig(NOFORK, DEF_HANDLER, cleanup); 139 140 tst_tmpdir(); 141 142 TEST_EXP_ENOS(exp_enos); 143 144 TEST_PAUSE; 145} 146 147void cleanup(void) 148{ 149 150 TEST_CLEANUP; 151 152 tst_rmdir(); 153} 154