fcntl09.c revision e4d0d27ec3829d826a7a4499e3a7ad662d14bd5a
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 */ 33/* $Id: fcntl09.c,v 1.2 2002/09/09 17:17:06 robbiew Exp $ */ 34/********************************************************** 35 * 36 * OS Test - Silicon Graphics, Inc. 37 * 38 * TEST IDENTIFIER : fcntl09 39 * 40 * EXECUTED BY : anyone 41 * 42 * TEST TITLE : Basic test for fcntl(2) using F_SETLK argument. 43 * 44 * PARENT DOCUMENT : usctpl01 45 * 46 * TEST CASE TOTAL : 2 47 * 48 * WALL CLOCK TIME : 1 49 * 50 * CPU TYPES : ALL 51 * 52 * AUTHOR : William Roske 53 * 54 * CO-PILOT : Dave Fenner 55 * 56 * DATE STARTED : 03/30/92 57 * 58 * INITIAL RELEASE : UNICOS 7.0 59 * 60 * TEST CASES 61 * 62 * 1.) fcntl(2) returns...(See Description) 63 * 64 * INPUT SPECIFICATIONS 65 * The standard options for system call tests are accepted. 66 * (See the parse_opts(3) man page). 67 * 68 * OUTPUT SPECIFICATIONS 69 * 70 * DURATION 71 * Terminates - with frequency and infinite modes. 72 * 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 79 * 80 * ENVIRONMENTAL NEEDS 81 * No run-time environmental needs. 82 * 83 * SPECIAL PROCEDURAL REQUIREMENTS 84 * None 85 * 86 * INTERCASE DEPENDENCIES 87 * None 88 * 89 * DETAILED DESCRIPTION 90 * This is a Phase I test for the fcntl(2) system call. It is intended 91 * to provide a limited exposure of the system call, for now. It 92 * should/will be extended when full functional tests are written for 93 * fcntl(2). 94 * 95 * Setup: 96 * Setup signal handling. 97 * Pause for SIGUSR1 if option specified. 98 * 99 * Test: 100 * Loop if the proper options are given. 101 * Execute system call 102 * Check return code, if system call failed (return=-1) 103 * Log the errno and Issue a FAIL message. 104 * Otherwise, Issue a PASS message. 105 * 106 * Cleanup: 107 * Print errno log and/or timing stats if options given 108 * 109 * 110 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ 111 112#include <sys/types.h> 113#include <sys/fcntl.h> 114#include <sys/stat.h> 115#include <errno.h> 116#include <string.h> 117#include <signal.h> 118#include "test.h" 119#include "usctest.h" 120 121extern void setup(); 122extern void cleanup(); 123 124 125 126char *TCID="fcntl09"; /* Test program identifier. */ 127int TST_TOTAL=2; /* Total number of test cases. */ 128extern int Tst_count; /* Test Case counter for tst_* routines */ 129 130int exp_enos[]={0, 0}; 131 132char fname[255]; 133int fd; 134struct flock flocks; 135 136int 137main(int ac, char **av) 138{ 139 int lc; /* loop counter */ 140 char *msg; /* message returned from parse_opts */ 141 142 /*************************************************************** 143 * parse standard options 144 ***************************************************************/ 145 if ( (msg=parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *) NULL ) 146 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); 147 148 /*************************************************************** 149 * perform global setup for test 150 ***************************************************************/ 151 setup(); 152 153 /* set the expected errnos... */ 154 TEST_EXP_ENOS(exp_enos); 155 156 /*************************************************************** 157 * check looping state if -c option given 158 ***************************************************************/ 159 for (lc=0; TEST_LOOPING(lc); lc++) { 160 int type; 161 for (type = 0; type < 2; type ++) { 162 163 /* reset Tst_count in case we are looping. */ 164 Tst_count=0; 165 166 flocks.l_type = type ? F_RDLCK : F_WRLCK; 167 168 /* 169 * Call fcntl(2) with F_SETLK argument on fname 170 */ 171 TEST(fcntl(fd, F_SETLK, &flocks)); 172 173 /* check return code */ 174 if ( TEST_RETURN == -1 ) { 175 TEST_ERROR_LOG(TEST_ERRNO); 176 tst_resm(TFAIL, 177 "fcntl(%s, F_SETLK, &flocks) flocks.l_type = %s Failed, errno=%d : %s", 178 fname, type ? "F_RDLCK" : "F_WRLCK", TEST_ERRNO, strerror(TEST_ERRNO)); 179 } else { 180 181 /*************************************************************** 182 * only perform functional verification if flag set (-f not given) 183 ***************************************************************/ 184 if ( STD_FUNCTIONAL_TEST ) { 185 /* No Verification test, yet... */ 186 tst_resm(TPASS, 187 "fcntl(%s, F_SETLK, &flocks) flocks.l_type = %s returned %d", 188 fname, type ? "F_RDLCK" : "F_WRLCK" ,TEST_RETURN); 189 } 190 } 191 192 flocks.l_type = F_UNLCK; 193 /* 194 * Call fcntl(2) with F_SETLK argument on fname 195 */ 196 TEST(fcntl(fd, F_SETLK, &flocks)); 197 198 /* check return code */ 199 if ( TEST_RETURN == -1 ) { 200 TEST_ERROR_LOG(TEST_ERRNO); 201 tst_resm(TFAIL, 202 "fcntl(%s, F_SETLK, &flocks) flocks.l_type = F_UNLCK Failed, errno=%d : %s", 203 fname, TEST_ERRNO, strerror(TEST_ERRNO)); 204 } else { 205 206 /*************************************************************** 207 * only perform functional verification if flag set (-f not given) 208 ***************************************************************/ 209 if ( STD_FUNCTIONAL_TEST ) { 210 /* No Verification test, yet... */ 211 tst_resm(TPASS, 212 "fcntl(%s, F_SETLK, &flocks) flocks.l_type = F_UNLCK returned %d", 213 fname, TEST_RETURN); 214 } 215 } 216 } 217 218 } /* End for TEST_LOOPING */ 219 220 /*************************************************************** 221 * cleanup and exit 222 ***************************************************************/ 223 cleanup(); 224 225 return 0; 226} /* End main */ 227 228/*************************************************************** 229 * setup() - performs all ONE TIME setup for this test. 230 ***************************************************************/ 231void 232setup() 233{ 234 /* capture signals */ 235 tst_sig(NOFORK, DEF_HANDLER, cleanup); 236 237 /* Pause if that option was specified */ 238 TEST_PAUSE; 239 240 /* make a temp directory and cd to it */ 241 tst_tmpdir(); 242 243 sprintf(fname,"./file_%d",getpid()); 244 if ((fd=creat(fname, 0644)) == -1) { 245 tst_brkm(TBROK, cleanup, "creat(%s, 0644) Failed, errno=%d : %s", fname, errno, strerror(errno)); 246 } else if (close(fd) == -1) { 247 tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s", fname, errno, strerror(errno)); 248 } else if ((fd = open(fname,O_RDWR,0700)) == -1) { 249 tst_brkm(TBROK, cleanup, "open(%s, O_RDWR,0700) Failed, errno=%d : %s", fname, errno, strerror(errno)); 250 } 251 flocks.l_whence=1; 252 flocks.l_start=0; 253 flocks.l_len=0; 254 flocks.l_pid=getpid(); 255} /* End setup() */ 256 257 258/*************************************************************** 259 * cleanup() - performs all ONE TIME cleanup for this test at 260 * completion or premature exit. 261 ***************************************************************/ 262void 263cleanup() 264{ 265 /* 266 * print timing stats if that option was specified. 267 * print errno log if that option was specified. 268 */ 269 TEST_CLEANUP; 270 271 if (close(fd) == -1) { 272 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno, strerror(errno)); 273 } else if (unlink(fname) == -1) { 274 tst_resm(TWARN, "unlink(%s) Failed, errno=%d : %s", fname, errno, strerror(errno)); 275 276 } 277 278 /* Remove tmp dir and all files in it */ 279 tst_rmdir(); 280 281 /* exit with return code appropriate for results */ 282 tst_exit(); 283} /* End cleanup() */ 284