plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 3 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 7 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 11 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | /* |
| 33 | * This program will grow a list of files. |
| 34 | * Each file will grow by grow_incr before the same |
| 35 | * file grows twice. Each file is open and closed before next file is opened. |
| 36 | * |
| 37 | * To just verify file contents: growfiles -g 0 -c 1 filename |
| 38 | * |
| 39 | * See help and prt_examples functions below. |
| 40 | * |
| 41 | * Basic code layout |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 42 | * process cmdline |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * print debug message about options used |
| 44 | * setup signal handlers |
| 45 | * return control to user (if wanted - default action) |
| 46 | * fork number of desired childern (if wanted) |
| 47 | * re-exec self (if wanted) |
| 48 | * Determine number of files |
| 49 | * malloc space or i/o buffer |
| 50 | * Loop until stop is set |
| 51 | * Determine if hit iteration, time, max errors or num bytes reached |
| 52 | * Loop through each file |
| 53 | * open file |
| 54 | * fstat file - to determine if file if a fifo |
| 55 | * prealloc file space (if wanted) |
| 56 | * growfile |
| 57 | * check last write |
| 58 | * check whole file |
| 59 | * shrink file |
| 60 | * close file |
| 61 | * delay (if wanted) |
| 62 | * End loop |
| 63 | * End loop |
| 64 | * remove all files (if wanted) |
| 65 | * |
| 66 | * Author: Richard Logan |
| 67 | * |
| 68 | */ |
| 69 | #include <stdio.h> |
robbiew | a70576c | 2003-03-04 18:33:41 +0000 | [diff] [blame] | 70 | #include <errno.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | #include <stdlib.h> |
| 72 | #include <ctype.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | #include <sys/types.h> |
mridge | 2422745 | 2004-05-17 22:06:02 +0000 | [diff] [blame] | 74 | #include <time.h> |
mridge | 1f61f0d | 2004-05-18 17:08:11 +0000 | [diff] [blame] | 75 | #include <sys/file.h> |
mridge | 2422745 | 2004-05-17 22:06:02 +0000 | [diff] [blame] | 76 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | #include <sys/stat.h> |
| 78 | #include <sys/time.h> |
| 79 | #include <sys/param.h> |
| 80 | #include <sys/signal.h> |
mridge | 1f61f0d | 2004-05-18 17:08:11 +0000 | [diff] [blame] | 81 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | #include <errno.h> |
| 83 | #include <string.h> |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 84 | #include <inttypes.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | #include "dataascii.h" |
| 86 | #include "random_range.h" |
| 87 | #include "databin.h" |
| 88 | |
| 89 | #ifdef CRAY |
| 90 | #include <sys/panic.h> |
| 91 | #include <sys/category.h> |
| 92 | #endif |
| 93 | |
robbiew | 92b688b | 2004-03-01 22:36:38 +0000 | [diff] [blame] | 94 | #include "test.h" |
| 95 | |
| 96 | /* LTP status reporting */ |
| 97 | char *TCID="growfiles"; /* Default test program identifier. */ |
| 98 | int TST_TOTAL=1; /* Total number of test cases. */ |
| 99 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 100 | |
| 101 | /* To avoid extensive modifications to the code, use this bodge */ |
| 102 | #define exit(x) myexit(x) |
| 103 | void |
| 104 | myexit (int x) |
| 105 | { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 106 | if (x) |
robbiew | 92b688b | 2004-03-01 22:36:38 +0000 | [diff] [blame] | 107 | tst_resm (TFAIL, "Test failed"); |
| 108 | else |
| 109 | tst_resm (TPASS, "Test passed"); |
| 110 | tst_exit(); |
| 111 | } |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | extern char *openflags2symbols(); |
| 114 | |
| 115 | extern int parse_open_flags(); |
| 116 | extern int background(); |
| 117 | extern int forker(); |
| 118 | extern int datapidgen(); |
| 119 | extern void databingen(); |
| 120 | extern int datapidchk(); |
| 121 | extern int databinchk(); |
| 122 | extern int file_lock(); |
| 123 | |
| 124 | int file_size(); |
| 125 | int check_write(); |
| 126 | int shrinkfile(); |
| 127 | int check_file(); |
| 128 | int growfile(); |
| 129 | int cleanup(); |
| 130 | int handle_error(); |
| 131 | int lkfile(); |
| 132 | void usage(); |
| 133 | void help(); |
| 134 | void prt_examples(); |
| 135 | int set_sig(); |
| 136 | void sig_handler(); |
| 137 | static void notify_others(); |
| 138 | #ifndef linux |
| 139 | int pre_alloc(); |
| 140 | #endif |
| 141 | |
| 142 | |
| 143 | #define NEWIO 1 /* Use the tlibio.c functions */ |
| 144 | |
| 145 | #ifndef NEWIO |
| 146 | #define NEWIO 0 /* specifies to use original iowrite.c */ |
| 147 | /* functions instead of tlibio.c functions */ |
| 148 | /* Once it is proven tlibio.c functions work properly, */ |
| 149 | /* only tlibio.c functions will be used */ |
| 150 | #else |
| 151 | #include "tlibio.h" |
| 152 | #endif |
| 153 | |
| 154 | #ifndef PATH_MAX |
| 155 | #define PATH_MAX 1023 |
| 156 | #endif |
| 157 | |
| 158 | |
| 159 | #define DEF_DIR "." |
| 160 | #define DEF_FILE "gf" |
| 161 | |
| 162 | char *Progname; |
vapier | 93205ff | 2006-02-15 06:53:26 +0000 | [diff] [blame] | 163 | int Debug = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
| 165 | int Pid=0; |
| 166 | |
| 167 | int io_type = 0; /* I/O type -sync */ |
robbiew | 85b1b03 | 2004-04-20 15:13:20 +0000 | [diff] [blame] | 168 | |
| 169 | #ifdef O_LARGEFILE |
| 170 | int open_flags = O_RDWR|O_CREAT|O_LARGEFILE; /* open flags */ |
| 171 | #else |
| 172 | #warning O_LARGEFILE is not defined! |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | int open_flags = O_RDWR|O_CREAT; /* open flags */ |
robbiew | 85b1b03 | 2004-04-20 15:13:20 +0000 | [diff] [blame] | 174 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
| 176 | #define MAX_FC_READ 196608 /* 4096 * 48 - 48 blocks */ |
| 177 | |
| 178 | #define PATTERN_ASCII 1 /* repeating alphabet letter pattern */ |
| 179 | /* allows multiple writers and to be checked */ |
| 180 | #define PATTERN_PID 2 /* <pid><words byte offset><pid> */ |
| 181 | /* Assumes 64 bit word. Only allows single */ |
| 182 | /* process to write and check */ |
| 183 | /* |
| 184 | * 1234567890123456789012345678901234567890123456789012345678901234 |
| 185 | * ________________________________________________________________ |
| 186 | * < pid >< offset in file of this word >< pid > |
| 187 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 188 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | #define PATTERN_OFFSET 3 /* Like PATTERN_PID but has a fixed number */ |
| 190 | /* (STATIC_NUM) instead of pid. */ |
| 191 | /* Allows multiple processes to write/read */ |
| 192 | #define PATTERN_ALT 4 /* alternating bit pattern (i.e. 0x5555555...) */ |
| 193 | #define PATTERN_CHKER 5 /* checkerboard pattern (i.e. 0xff00ff00ff00...) */ |
| 194 | #define PATTERN_CNTING 6 /* counting pattern (i.e. 0 - 07, 0 - 07, ...) */ |
| 195 | #define PATTERN_ONES 7 /* all bits set (i.e. 0xffffffffffffff...) */ |
| 196 | #define PATTERN_ZEROS 8 /* all bits cleared (i.e. 0x000000000...) */ |
| 197 | #define PATTERN_RANDOM 9 /* random integers - can not be checked */ |
| 198 | #define STATIC_NUM 221849 /* used instead of pid when PATTERN_OFFSET */ |
| 199 | |
| 200 | #define MODE_RAND_SIZE 1 /* random write and trunc */ |
| 201 | #define MODE_RAND_LSEEK 2 /* random lseek before write */ |
| 202 | #define MODE_GROW_BY_LSEEK 4 /* lseek beyond end of file then write a byte */ |
| 203 | #define RANDOM_OPEN 999876 /* if Open_flags set to this value, open flags */ |
| 204 | /* will be randomly choosen from Open_flags[] */ |
| 205 | #define MODE_FIFO S_IFIFO /* defined in stat.h 0010000 */ |
| 206 | |
| 207 | int num_files = 0; /* num_auto_files + cmd line files */ |
| 208 | char *filenames; /* pointer to space containing filenames */ |
| 209 | int remove_files = 0; /* if set, cleanup default is not to cleanup */ |
| 210 | int bytes_consumed = 0; /* total bytes consumed, all files */ |
| 211 | int bytes_to_consume = 0; /* non-zero if -B was specified, total bytes */ |
| 212 | int Maxerrs = 100; /* Max number errors before forced exit */ |
| 213 | int Errors = 0; /* number of encountered errors */ |
| 214 | int Upanic_on_error = 0; /* call upanic if error and this variable set */ |
| 215 | |
| 216 | /* The *_size variables are only used when random iosize option (-r) is used */ |
| 217 | int max_size=5000; |
| 218 | int min_size=1; /* also set in option parsing */ |
| 219 | int mult_size=1; /* when random iosz, iosz must be mult of mult_size */ |
| 220 | /* the *_lseek variables are only used when radon lseek option (-R) is used */ |
| 221 | int min_lseek=0; /* also set in option parsing */ |
| 222 | int max_lseek=-1; /* -1 means size of file */ |
| 223 | #ifdef CRAY |
| 224 | int Pattern=PATTERN_OFFSET; /* This pattern is 64 bit word based */ |
| 225 | #else |
| 226 | int Pattern=PATTERN_ASCII; |
| 227 | #endif |
| 228 | int Seed=-1; /* random number seed, < 0 == uninitialized */ |
| 229 | int Nseeds=0; /* Number of seed specified by the user */ |
| 230 | int *Seeds; /* malloc'ed arrary of ints holding user spec seeds */ |
| 231 | |
| 232 | int using_random=0; /* flag indicating randomization is being used */ |
| 233 | float delaysecs=0.0; /* delay between iterations (in seconds) */ |
| 234 | int delaytime; /* delay between iterations in clocks/uses */ |
| 235 | int lockfile=0; /* if set, do file locking */ |
| 236 | /* 1 = do file locking around write, trunc */ |
| 237 | /* and reads. */ |
| 238 | /* 2 = write lock around all file operations */ |
| 239 | |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 240 | off_t Woffset=0; /* offset before last write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | int Grow_incr=4096; /* sz of last write */ |
| 242 | int Mode=0; /* bitmask of write/trunc mode */ |
| 243 | /* also knows if dealing with fifo */ |
| 244 | char *Buffer = NULL; /* buffer used by write and write check */ |
| 245 | int Alignment=0; /* if non word multiple, io will not be word aligned */ |
| 246 | int Opid=0; /* original pid */ |
| 247 | |
| 248 | int Sync_with_others = 0; /* Flag indicating to stop other if we stop before DONE */ |
| 249 | int Iter_cnt = 0; /* contains current iteration count value */ |
| 250 | char TagName[40]; /* name of this growfiles (see Monster) */ |
| 251 | |
| 252 | struct fileinfo_t { |
| 253 | char *filename; |
| 254 | int fd; |
| 255 | int openflags; |
| 256 | int mode; |
| 257 | } Fileinfo; |
| 258 | |
| 259 | /* |
| 260 | * Define open flags that will be used when '-o random' option is used. |
| 261 | * Note: If there is more than one growfiles doing its thing to the same |
| 262 | * file, O_TRUNC will cause data mismatches. How you ask? |
| 263 | * timing of events, example: |
| 264 | * Process one Process two |
| 265 | * --------------- ------------- |
| 266 | * get write lock |
| 267 | * fstat file |
| 268 | * lseek |
| 269 | * generate pattern |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 270 | * open with O_TRUNC |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | * write with wrong pattern |
| 272 | * because offset is wrong |
| 273 | * |
| 274 | * The second process truncated the file after the pattern was |
| 275 | * determined, thus the pattern is wrong for the file location. |
| 276 | * |
| 277 | * There can also be a timing problem with open flag O_APPEND if |
| 278 | * file locks are not being used (-l option). Things could happen |
| 279 | * between the fstat and the write. Thus, writing the wrong pattern. |
| 280 | * If all processes observe the file locks, O_APPEND should be ok |
| 281 | * to use. |
| 282 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 283 | int Open_flags[] = { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | #ifdef CRAY |
| 285 | O_RDWR|O_CREAT, |
| 286 | O_RDWR|O_CREAT|O_RAW, |
| 287 | O_RDWR|O_CREAT|O_BIG, |
| 288 | O_RDWR|O_CREAT|O_APPEND, |
| 289 | O_RDWR|O_CREAT|O_NDELAY, |
| 290 | O_RDWR|O_CREAT|O_PLACE, |
| 291 | O_RDWR|O_CREAT|O_SYNC, |
| 292 | O_RDWR|O_CREAT|O_RAW|O_SYNC, |
| 293 | O_RDWR|O_CREAT|O_NDELAY|O_SYNC, |
| 294 | O_RDWR|O_CREAT|O_NDELAY|O_SYNC|O_BIG, |
| 295 | O_RDWR|O_CREAT|O_RAW, |
| 296 | O_RDWR|O_CREAT|O_RAW|O_APPEND, |
| 297 | O_RDWR|O_CREAT|O_RAW|O_BIG, |
| 298 | O_RDWR|O_CREAT|O_RAW|O_APPEND|O_BIG, |
| 299 | /*** |
| 300 | * O_WELLFORMED makes -o random require well formed i/o |
| 301 | ***/ |
| 302 | #if ALLOW_O_WELLFORMED |
| 303 | #if O_PARALLEL |
| 304 | O_RDWR|O_CREAT|O_PARALLEL|O_WELLFORMED|O_RAW, |
| 305 | O_RDWR|O_CREAT|O_PARALLEL|O_WELLFORMED|O_RAW|O_TRUNC, |
| 306 | #endif /* O_PARALLEL */ |
| 307 | #endif |
| 308 | |
| 309 | #else /* CRAY */ |
| 310 | O_RDWR|O_CREAT, |
| 311 | O_RDWR|O_CREAT|O_APPEND, |
| 312 | O_RDWR|O_CREAT|O_NDELAY, |
| 313 | O_RDWR|O_CREAT|O_SYNC, |
| 314 | O_RDWR|O_CREAT|O_SYNC|O_NDELAY, |
| 315 | O_RDWR|O_CREAT|O_APPEND|O_NDELAY, |
| 316 | |
| 317 | #endif /* CRAY */ |
| 318 | }; |
| 319 | |
| 320 | #define REXEC_INIT 0 /* don't do re-exec of childern */ |
| 321 | #define REXEC_DOIT 1 /* Do re-exec of childern */ |
| 322 | #define REXEC_DONE 2 /* We've already been re-exec'ed */ |
| 323 | |
| 324 | #ifndef BSIZE |
| 325 | #ifdef CRAY |
| 326 | #define BSIZE 1024 |
| 327 | #else |
| 328 | #define BSIZE 512 |
| 329 | #endif /* CRAY */ |
| 330 | #endif /* BSIZE */ |
| 331 | |
| 332 | #define USECS_PER_SEC 1000000 /* microseconds per second */ |
| 333 | |
| 334 | /* |
| 335 | * Define marcos used when dealing with file locks. |
| 336 | */ |
| 337 | #define LKLVL0 1 /* file lock around write/read/trunc */ |
| 338 | #define LKLVL1 2 /* file lock after open to before close */ |
| 339 | |
| 340 | /* |
| 341 | * Define special max lseek values |
| 342 | */ |
| 343 | #define LSK_EOF -1 /* set fptr up to EOF */ |
| 344 | #define LSK_EOFPLUSGROW -2 /* set fptr up to EOF + grow - leave whole */ |
| 345 | #define LSK_EOFMINUSGROW -3 /* set fptr up to EOF-grow - no grow */ |
| 346 | |
| 347 | |
| 348 | /*********************************************************************** |
| 349 | * MAIN |
| 350 | ***********************************************************************/ |
| 351 | int |
| 352 | main(argc, argv) |
| 353 | int argc; |
| 354 | char **argv; |
| 355 | { |
| 356 | extern char *optarg; /* used by getopt */ |
| 357 | extern int optind; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 358 | |
| 359 | int ind; |
| 360 | int first_file_ind = 0; |
| 361 | int num_auto_files = 0; /* files created by tool */ |
| 362 | int seq_auto_files = 0; /* auto files created by tool created by tool */ |
| 363 | char *auto_dir = DEF_DIR; |
| 364 | char *auto_file = DEF_FILE; |
| 365 | int grow_incr = 4096; |
| 366 | int trunc_incr = 4096; |
| 367 | int trunc_inter = 0; /* 0 means none, */ |
| 368 | int unlink_inter = 0; /* 0 means none, 1 means always unlink */ |
| 369 | int unlink_inter_ran = -1; /* -1 -use unlink_inter, otherwise randomly choose */ |
| 370 | /* between unlink_inter and unlink_inter_ran */ |
| 371 | int file_check_inter = 0; /* 0 means never, 1 means always */ |
| 372 | int write_check_inter = 1; /* 0 means never, 1 means always */ |
| 373 | int iterations = 1; /* number of increments to be added */ |
| 374 | int no_file_check = 0; /* if set, no whole file checking will be done */ |
| 375 | int num; |
| 376 | int fd; /* file descriptor */ |
| 377 | int stop = 0; /* loop stopper if set */ |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 378 | |
| 379 | long unsigned curr_size = 0; /* BUG:14136 (keep track of file size) */ |
| 380 | const long unsigned ext2_limit = 2147483647; /* BUG:14136 (2GB ext2 filesize limit) */ |
| 381 | |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 382 | off_t tmp; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 383 | char chr; |
| 384 | int ret; |
| 385 | int pre_alloc_space = 0; |
| 386 | #ifndef linux |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 387 | long total_grow_value; /* used in pre-allocations */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 388 | #endif |
| 389 | int backgrnd = 1; /* return control to user */ |
| 390 | struct stat statbuf; |
| 391 | int time_iterval = -1; |
| 392 | time_t start_time; |
subrata_modak | c6b731c | 2009-11-09 16:51:32 +0000 | [diff] [blame] | 393 | char reason[128]; /* reason for loop termination */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 394 | int num_procs=1; |
| 395 | int forker_mode=0; |
| 396 | int reexec=REXEC_INIT; /* reexec info */ |
| 397 | char *exec_path=NULL; |
| 398 | |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 399 | /*char *strrchr();*/ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 400 | |
| 401 | char *filename; /* name of file specified by user */ |
| 402 | char *cptr; /* temp char pointer */ |
| 403 | extern int Forker_npids; /* num of forked pid, defined in forker.c */ |
robbiew | aead6ba | 2003-04-17 16:49:18 +0000 | [diff] [blame] | 404 | struct timeval tv1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 405 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 406 | if (argv[0][0] == '-') |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 407 | reexec=REXEC_DONE; |
| 408 | /* |
| 409 | * Determine name of file used to invoke this program |
| 410 | */ |
| 411 | if ((Progname=strrchr(argv[0], '/')) != NULL) |
| 412 | Progname++; |
| 413 | else |
| 414 | Progname=argv[0]; |
| 415 | |
| 416 | TagName[0] = '\0'; |
| 417 | |
| 418 | /* |
| 419 | * Process options |
| 420 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 421 | while ((ind=getopt(argc, argv, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 422 | "hB:C:c:bd:D:e:Ef:g:H:I:i:lL:n:N:O:o:pP:q:wt:r:R:s:S:T:uU:W:xy")) != EOF) { |
| 423 | switch(ind) { |
| 424 | |
| 425 | case 'h' : |
| 426 | help(); |
subrata_modak | 999fb4a | 2008-08-25 14:32:09 +0000 | [diff] [blame] | 427 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 428 | |
| 429 | case 'B': |
| 430 | switch (sscanf(optarg, "%i%c", |
| 431 | &bytes_to_consume, &chr)) { |
| 432 | case 1: /* noop */ |
| 433 | break; |
| 434 | |
| 435 | case 2: |
| 436 | if (chr == 'b') { |
| 437 | bytes_to_consume *= BSIZE; |
| 438 | } else { |
| 439 | fprintf(stderr, |
| 440 | "%s%s: --B option arg invalid\n", |
| 441 | Progname, TagName); |
| 442 | usage(); |
| 443 | exit(1); |
| 444 | } |
| 445 | break; |
| 446 | |
| 447 | default: |
| 448 | fprintf(stderr, "%s%s: --B option arg invalid\n", |
| 449 | Progname, TagName); |
| 450 | usage(); |
| 451 | exit(1); |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | break; |
| 456 | |
| 457 | case 'E' : |
| 458 | prt_examples(stdout); |
| 459 | exit(0); |
| 460 | |
| 461 | case 'b' : /* batch */ |
| 462 | backgrnd=0; |
| 463 | break; |
| 464 | |
| 465 | case 'C': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 466 | if (sscanf(optarg, "%i", &write_check_inter) != 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 467 | fprintf(stderr, "%s%s: --c option arg invalid\n", |
| 468 | Progname, TagName); |
| 469 | usage(); |
| 470 | exit(1); |
| 471 | } |
| 472 | break; |
| 473 | |
| 474 | case 'c': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 475 | if (sscanf(optarg, "%i", &file_check_inter) != 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 476 | fprintf(stderr, "%s%s: --c option arg invalid\n", |
| 477 | Progname, TagName); |
| 478 | usage(); |
| 479 | exit(1); |
| 480 | } |
| 481 | break; |
| 482 | |
| 483 | |
| 484 | case 'd': |
| 485 | auto_dir=optarg; |
| 486 | #ifdef CRAY |
| 487 | unsetenv("TMPDIR"); /* force the use of auto_dir */ |
| 488 | #endif |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 489 | if (stat(auto_dir, &statbuf) == -1) { |
| 490 | if (mkdir(auto_dir, 0777) == -1) { |
| 491 | if (errno != EEXIST) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 492 | fprintf(stderr, |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 493 | "%s%s: Unable to make dir %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 494 | Progname, TagName, auto_dir); |
| 495 | exit(1); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | else { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 500 | if (! (statbuf.st_mode & S_IFDIR)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 501 | fprintf(stderr, |
| 502 | "%s%s: %s already exists and is not a directory\n", |
| 503 | Progname, TagName, auto_dir); |
| 504 | exit(1); |
| 505 | } |
| 506 | } |
| 507 | break; |
| 508 | |
| 509 | case 'D': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 510 | if (sscanf(optarg, "%i", &Debug) != 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 511 | fprintf(stderr, "%s%s: --D option arg invalid\n", |
| 512 | Progname, TagName); |
| 513 | usage(); |
| 514 | exit(1); |
| 515 | } |
| 516 | break; |
| 517 | |
| 518 | case 'e': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 519 | if (sscanf(optarg, "%i", &Maxerrs) != 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 520 | fprintf(stderr, "%s%s: --e option arg invalid\n", |
| 521 | Progname, TagName); |
| 522 | usage(); |
| 523 | exit(1); |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case 'f': |
| 528 | auto_file=optarg; |
| 529 | break; |
| 530 | |
| 531 | case 'g': |
| 532 | if ((ret=sscanf(optarg, "%i%c", &grow_incr, &chr)) < 1 || |
| 533 | grow_incr < 0 ) { |
| 534 | |
| 535 | fprintf(stderr, "%s%s: --g option arg invalid\n", |
| 536 | Progname, TagName); |
| 537 | usage(); |
| 538 | exit(1); |
| 539 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 540 | if (ret == 2) { |
| 541 | if (chr == 'b' || chr == 'B') |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 542 | grow_incr *= 4096; |
| 543 | else { |
| 544 | fprintf(stderr, |
| 545 | "%s%s: --g option arg invalid\n", |
| 546 | Progname, TagName); |
| 547 | usage(); |
| 548 | exit(1); |
| 549 | } |
| 550 | } |
| 551 | break; |
| 552 | |
| 553 | case 'H': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 554 | if (sscanf(optarg, "%f", &delaysecs) != 1 || delaysecs < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 555 | |
| 556 | fprintf(stderr, "%s%s: --H option arg invalid\n", |
| 557 | Progname, TagName); |
| 558 | usage(); |
| 559 | exit(1); |
| 560 | } |
| 561 | break; |
| 562 | |
| 563 | case 'i': |
| 564 | if (sscanf(optarg, "%i", &iterations) != 1 || |
| 565 | iterations < 0 ) { |
| 566 | |
| 567 | fprintf(stderr, "%s%s: --i option arg invalid\n", |
| 568 | Progname, TagName); |
| 569 | usage(); |
| 570 | exit(1); |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | case 'I': |
| 575 | #if NEWIO |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 576 | if ((io_type=lio_parse_io_arg1(optarg)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 577 | fprintf(stderr, |
| 578 | "%s%s: --I arg is invalid, must be s, p, f, a, l, L or r.\n", |
| 579 | Progname, TagName); |
| 580 | exit(1); |
| 581 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 582 | if (io_type & LIO_RANDOM) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 583 | using_random++; |
| 584 | #else |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 585 | if ((io_type=parse_io_arg(optarg)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 586 | fprintf(stderr, |
| 587 | "%s%s: --I arg is invalid, must be s, p, f, a, l, L or r.\n", |
| 588 | Progname, TagName); |
| 589 | exit(1); |
| 590 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 591 | if (io_type == 99) /* hold-over until tlibio.h */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 592 | using_random++; |
| 593 | #endif |
| 594 | break; |
| 595 | |
| 596 | case 'l': |
| 597 | lockfile++; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 598 | if (lockfile > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 599 | lockfile=2; /* lockfile can only be 1 or 2 */ |
| 600 | break; |
| 601 | |
| 602 | case 'L': |
| 603 | if (sscanf(optarg, "%i", &time_iterval) != 1 || |
| 604 | time_iterval < 0 ) { |
| 605 | fprintf(stderr, "%s%s: --L option arg invalid\n", |
| 606 | Progname, TagName); |
| 607 | usage(); |
| 608 | exit(1); |
| 609 | } |
| 610 | break; |
| 611 | |
| 612 | case 'n': |
| 613 | if (sscanf(optarg, "%i:%i", &num_procs, &forker_mode) < 1 || |
| 614 | num_procs < 0 ) { |
| 615 | |
| 616 | fprintf(stderr, "%s%s: --n option arg invalid\n", |
| 617 | Progname, TagName); |
| 618 | usage(); |
| 619 | exit(1); |
| 620 | } |
| 621 | |
| 622 | break; |
| 623 | |
| 624 | case 'N': |
| 625 | if (sscanf(optarg, "%i", &num_auto_files) != 1 || |
| 626 | num_auto_files < 0 ) { |
| 627 | |
| 628 | fprintf(stderr, "%s%s: --N option arg invalid\n", |
| 629 | Progname, TagName); |
| 630 | usage(); |
| 631 | exit(1); |
| 632 | } |
| 633 | break; |
| 634 | |
| 635 | case 'O': |
| 636 | if (sscanf(optarg, "%i", &Alignment) != 1 || |
| 637 | num_auto_files < 0 ) { |
| 638 | |
| 639 | fprintf(stderr, "%s%s: --O option arg invalid\n", |
| 640 | Progname, TagName); |
| 641 | usage(); |
| 642 | exit(1); |
| 643 | } |
| 644 | break; |
| 645 | |
| 646 | case 'o': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 647 | if (strcmp(optarg, "random") == 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 648 | open_flags=RANDOM_OPEN; |
| 649 | using_random++; |
| 650 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 651 | } else if ((open_flags=parse_open_flags(optarg, NULL)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 652 | fprintf(stderr, "%s%s: --o arg contains invalid flag\n", |
| 653 | Progname, TagName); |
| 654 | exit(1); |
| 655 | } |
| 656 | break; |
| 657 | |
| 658 | |
| 659 | case 'p' : /* pre allocate space */ |
| 660 | #ifdef linux |
| 661 | printf("%s%s: --p is illegal option on linux system\n", |
| 662 | Progname, TagName); |
| 663 | exit(1); |
| 664 | #else |
| 665 | pre_alloc_space++; |
| 666 | #endif |
| 667 | break; |
| 668 | |
| 669 | case 'P': |
| 670 | #ifdef CRAY |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 671 | if (strcmp(optarg, "PANIC") != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 672 | fprintf(stderr, "%s%s: --P arg must be PANIC\n", Progname, TagName); |
| 673 | exit(1); |
| 674 | } |
| 675 | Upanic_on_error++; |
| 676 | printf("%s: Will call upanic after writes\n"); |
| 677 | #else |
| 678 | printf("%s%s: --P is illegal option on non-cray system\n", |
| 679 | Progname, TagName); |
| 680 | exit(1); |
| 681 | #endif |
| 682 | break; |
| 683 | |
| 684 | case 'q': /* file content or pattern */ |
| 685 | switch(optarg[0]) { |
| 686 | case 'A': |
| 687 | Pattern = PATTERN_ALT; |
| 688 | break; |
| 689 | case 'a': |
| 690 | Pattern = PATTERN_ASCII; |
| 691 | break; |
| 692 | case 'p': |
| 693 | Pattern = PATTERN_PID; |
| 694 | break; |
| 695 | case 'o': |
| 696 | Pattern = PATTERN_OFFSET; |
| 697 | break; |
| 698 | case 'c': |
| 699 | Pattern = PATTERN_CHKER; |
| 700 | break; |
| 701 | case 'C': |
| 702 | Pattern = PATTERN_CNTING; |
| 703 | break; |
| 704 | case 'r': |
| 705 | Pattern = PATTERN_RANDOM; |
| 706 | using_random++; |
| 707 | break; |
| 708 | case 'z': |
| 709 | Pattern = PATTERN_ZEROS; |
| 710 | break; |
| 711 | case 'O': |
| 712 | Pattern = PATTERN_ONES; |
| 713 | break; |
| 714 | default: |
| 715 | fprintf(stderr, |
| 716 | "%s%s: --C option arg invalid, A, a, p, o, c, C, r, z, or 0\n", |
| 717 | Progname, TagName); |
| 718 | usage(); |
| 719 | exit(1); |
| 720 | } |
| 721 | break; |
| 722 | |
| 723 | case 'R': /* random lseek before write arg: [min-]max*/ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 724 | if (sscanf(optarg, "%i-%i", &min_lseek, &max_lseek) != 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 725 | min_lseek=1; /* same as default in define */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 726 | if (sscanf(optarg, "%i%c", &max_lseek, &chr) != 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 727 | fprintf(stderr, "%s%s: --R option arg invalid: [min-]max\n", |
| 728 | Progname, TagName); |
| 729 | exit(1); |
| 730 | } |
| 731 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 732 | if (max_lseek < LSK_EOFMINUSGROW) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 733 | fprintf(stderr, "%s%s: --R option, max_lseek is invalid\n", |
| 734 | Progname, TagName); |
| 735 | exit(1); |
| 736 | } |
| 737 | Mode |= MODE_RAND_LSEEK; |
| 738 | using_random++; |
| 739 | break; |
| 740 | |
| 741 | case 'r': /* random io size arg: [min-]max[:mult] */ |
| 742 | |
| 743 | /* min-max:mult format */ |
| 744 | if (sscanf(optarg, "%i-%i:%i%c", &min_size, &max_size, |
| 745 | &mult_size, &chr) != 3 ) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 746 | min_size=1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 747 | /* max:mult format */ |
| 748 | if (sscanf(optarg, "%i:%i%c", &max_size, |
| 749 | &mult_size, &chr) != 2 ) { |
| 750 | /* min-max format */ |
| 751 | if (sscanf(optarg, "%i-%i%c", &min_size, |
| 752 | &max_size, &chr) != 2 ) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 753 | min_size=1; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 754 | if (sscanf(optarg, "%i%c", &max_size, &chr) != 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 755 | fprintf(stderr, |
| 756 | "%s%s: --r option arg invalid: [min-]max[:mult]\n", |
| 757 | Progname, TagName); |
| 758 | exit(1); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 764 | if (max_size < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 765 | fprintf(stderr, "%s%s: --r option, max_size is invalid\n", |
| 766 | Progname, TagName); |
| 767 | exit(1); |
| 768 | } |
| 769 | /* |
| 770 | * If min and max are the same, no randomness |
| 771 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 772 | if (min_size != max_size) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 773 | Mode |= MODE_RAND_SIZE; |
| 774 | using_random++; |
| 775 | } |
| 776 | break; |
| 777 | |
| 778 | case 'S': |
| 779 | if (sscanf(optarg, "%i", &seq_auto_files) != 1 || |
| 780 | seq_auto_files < 0 ) { |
| 781 | |
| 782 | fprintf(stderr, "%s%s: --S option arg invalid\n", |
| 783 | Progname, TagName); |
| 784 | usage(); |
| 785 | exit(1); |
| 786 | } |
| 787 | break; |
| 788 | |
| 789 | case 's': /* format: seed[,seed...] */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 790 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 791 | /* count the number of seeds */ |
| 792 | cptr=optarg; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 793 | for (Nseeds=1; *cptr ; Nseeds++) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 794 | if ((filename=strchr(cptr, ',')) == NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 795 | break; |
| 796 | cptr=filename; |
| 797 | cptr++; |
| 798 | } |
| 799 | Seeds=(int *)malloc(Nseeds*sizeof(int)); |
| 800 | |
| 801 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 802 | * check that each seed is valid and put them in |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 803 | * the newly malloc'ed Seeds arrary. |
| 804 | */ |
| 805 | filename=cptr=optarg; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 806 | for (Nseeds=0; *cptr; Nseeds++) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 807 | if ((filename=strchr(cptr, ',')) == NULL) { |
| 808 | if (sscanf(cptr, "%i", &Seeds[Nseeds]) < 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 809 | fprintf(stderr, "%s%s: --s option arg %s invalid\n", |
| 810 | Progname, TagName, cptr); |
| 811 | usage(); |
| 812 | exit(1); |
| 813 | } |
| 814 | Nseeds++; |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | *filename='\0'; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 819 | if (sscanf(cptr, "%i", &Seeds[Nseeds]) < 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 820 | fprintf(stderr, "%s%s: --s option arg %s invalid\n", |
| 821 | Progname, TagName, cptr); |
| 822 | usage(); |
| 823 | exit(1); |
| 824 | } |
| 825 | *filename=','; /* restore string */ |
| 826 | cptr=filename; |
| 827 | cptr++; |
| 828 | } |
| 829 | break; |
| 830 | |
| 831 | case 't': |
| 832 | if ((ret=sscanf(optarg, "%i%c", &trunc_incr, &chr)) < 1 || |
| 833 | trunc_incr < 0 ) { |
| 834 | |
| 835 | fprintf(stderr, "%s%s: --t option arg invalid\n", |
| 836 | Progname, TagName); |
| 837 | usage(); |
| 838 | exit(1); |
| 839 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 840 | if (ret == 2) { |
| 841 | if (chr == 'b' || chr == 'B') |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 842 | trunc_incr *= 4096; |
| 843 | else { |
| 844 | fprintf(stderr, |
| 845 | "%s%s: --t option arg invalid\n", |
| 846 | Progname, TagName); |
| 847 | usage(); |
| 848 | exit(1); |
| 849 | } |
| 850 | } |
| 851 | break; |
| 852 | |
| 853 | case 'T': /* truncate interval */ |
| 854 | if (sscanf(optarg, "%i%c", &trunc_inter, &chr) != 1 || |
| 855 | trunc_inter < 0 ) { |
| 856 | |
| 857 | fprintf(stderr, "%s%s: --T option arg invalid\n", |
| 858 | Progname, TagName); |
| 859 | usage(); |
| 860 | exit(1); |
| 861 | } |
| 862 | break; |
| 863 | |
| 864 | case 'u': |
| 865 | remove_files++; |
| 866 | break; |
| 867 | |
| 868 | case 'U': /* how often to unlink file */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 869 | /* |
| 870 | * formats: |
| 871 | * A-B - randomly pick interval between A and B |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 872 | * X - unlink file every X iteration |
| 873 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 874 | if (sscanf(optarg, "%i-%i", &unlink_inter, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 875 | &unlink_inter_ran) == 2 ) { |
| 876 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 877 | if (unlink_inter < 0 || unlink_inter_ran < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 878 | fprintf(stderr, "%s%s: --U option arg invalid\n", |
| 879 | Progname, TagName); |
| 880 | usage(); |
| 881 | exit(1); |
| 882 | } |
| 883 | /* ensure unlink_inter contains smaller value */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 884 | if (unlink_inter > unlink_inter_ran) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 885 | tmp=unlink_inter_ran; |
| 886 | unlink_inter_ran=unlink_inter; |
| 887 | unlink_inter=tmp; |
| 888 | } |
| 889 | using_random++; |
| 890 | |
| 891 | } else if (sscanf(optarg, "%i%c", &unlink_inter, &chr) != 1 || |
| 892 | unlink_inter < 0 ) { |
| 893 | |
| 894 | fprintf(stderr, "%s%s: --U option arg invalid\n", |
| 895 | Progname, TagName); |
| 896 | usage(); |
| 897 | exit(1); |
| 898 | } |
| 899 | break; |
| 900 | |
| 901 | case 'x': |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 902 | if (reexec != REXEC_DONE) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 903 | reexec=REXEC_DOIT; |
| 904 | break; |
| 905 | |
| 906 | case 'w': |
| 907 | Mode |= MODE_GROW_BY_LSEEK; |
| 908 | break; |
| 909 | |
| 910 | case 'W': |
robbiew | 92b688b | 2004-03-01 22:36:38 +0000 | [diff] [blame] | 911 | TCID = optarg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 912 | sprintf( TagName, "(%.39s)", optarg ); |
| 913 | break; |
| 914 | |
| 915 | case 'y': |
| 916 | Sync_with_others=1; |
| 917 | break; |
| 918 | |
| 919 | case '?': |
| 920 | usage(); |
| 921 | exit(1); |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 926 | if (Debug == 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 927 | cptr = getenv("TOUTPUT"); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 928 | if ((cptr != NULL) && (strcmp( cptr, "NOPASS" ) == 0)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 929 | Debug = 0; |
| 930 | } |
| 931 | } |
| 932 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 933 | if (Pattern == PATTERN_RANDOM) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 934 | no_file_check=1; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 935 | if (write_check_inter || file_check_inter) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 936 | printf("%s%s: %d Using random pattern - no data checking will be performed!\n", |
| 937 | Progname, TagName, getpid()); |
| 938 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 939 | else if (max_lseek == LSK_EOFPLUSGROW || Mode & MODE_GROW_BY_LSEEK) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 940 | no_file_check=1; |
| 941 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 942 | if (file_check_inter) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 943 | printf("%s%s: %d Using random lseek beyond EOF or lseek grow,\n\ |
| 944 | no whole file checking will be performed!\n", Progname, TagName, getpid()); |
| 945 | |
| 946 | } |
| 947 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 948 | if (Mode & MODE_RAND_SIZE) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 949 | grow_incr=max_size; |
| 950 | |
| 951 | set_sig(); |
| 952 | |
| 953 | Opid=getpid(); |
| 954 | Pid=Opid; |
| 955 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 956 | if (backgrnd) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 957 | if (Debug > 1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 958 | printf("%s: %d DEBUG2 forking, returning control to the user\n", |
| 959 | Progname, Opid); |
| 960 | background(Progname); /* give user their prompt back */ |
| 961 | } |
| 962 | |
| 963 | #if CRAY |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 964 | if (Sync_with_others) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 965 | setpgrp(); |
| 966 | #endif |
| 967 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 968 | if (Debug > 3) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 969 | #if NEWIO |
| 970 | lio_set_debug(Debug-3); |
| 971 | #else |
| 972 | set_iowrite_debug(Debug-3); |
| 973 | #endif |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Print some program information here if debug is turned on to |
| 978 | * level 3 or higher. |
| 979 | */ |
| 980 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 981 | if (Debug > 2) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 982 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 983 | if (Mode & MODE_GROW_BY_LSEEK) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 984 | printf("%s: %d DEBUG lseeking past end of file, writting a \"w\"\n", |
| 985 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 986 | else if (Pattern == PATTERN_OFFSET) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 987 | printf("%s: %d DEBUG3 %d<byteoffset>%d per word pattern multi-writers.\n", |
| 988 | Progname, Pid, STATIC_NUM, STATIC_NUM); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 989 | else if (Pattern == PATTERN_PID) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 990 | printf("%s: %d DEBUG3 <pid><byteoffset><pid> per word pattern - 1 writer\n", |
| 991 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 992 | else if (Pattern == PATTERN_ASCII) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 993 | printf("%s: %d DEBUG3 ascii pattern (vi'able)- allows multiple writers\n", |
| 994 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 995 | else if (Pattern == PATTERN_ALT) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 996 | printf("%s: %d DEBUG3 alt bit pattern - allows multiple writers\n", |
| 997 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 998 | else if (Pattern == PATTERN_CHKER) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 999 | printf("%s: %d DEBUG3 checkerboard pattern - allows multiple writers\n", |
| 1000 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1001 | else if (Pattern == PATTERN_CNTING) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1002 | printf("%s: %d DEBUG3 counting pattern - allows multiple writers\n", |
| 1003 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1004 | else if (Pattern == PATTERN_RANDOM) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1005 | printf("%s: %d DEBUG3 random integer pattern - no write/file checking\n", |
| 1006 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1007 | else if (Pattern == PATTERN_ONES) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1008 | printf("%s: %d DEBUG3 all ones pattern - allows multiple writers\n", |
| 1009 | Progname, Pid); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1010 | else if (Pattern == PATTERN_ZEROS) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1011 | printf("%s: %d DEBUG3 all zeros pattern - allows multiple writers\n", |
| 1012 | Progname, Pid); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1013 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1014 | else |
| 1015 | printf("%s: %d DEBUG3 unknown pattern\n", |
| 1016 | Progname, Pid); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1017 | if (bytes_to_consume) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1018 | printf("%s: %d DEBUG3 bytes_to_consume = %d\n", |
| 1019 | Progname, Pid, bytes_to_consume); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1020 | printf("%s: %d DEBUG3 Maxerrs = %d, pre_alloc_space = %d, filelocking = %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1021 | Progname, Pid, Maxerrs, pre_alloc_space, lockfile); |
| 1022 | |
| 1023 | printf("%s: %d DEBUG3 Debug = %d, remove files in cleanup : %d\n", |
| 1024 | Progname, Pid, Debug, remove_files); |
| 1025 | |
| 1026 | printf("%s: %d DEBUG3 Mode = %#o\n", Progname, Pid, Mode); |
| 1027 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1028 | if (open_flags == RANDOM_OPEN) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1029 | printf("%s: %d DEBUG3 open_flags = (random), io_type = %#o\n", Progname, |
| 1030 | Pid, io_type); |
| 1031 | else |
| 1032 | printf("%s: %d DEBUG3 open_flags = %#o, io_type = %#o\n", Progname, |
| 1033 | Pid, open_flags, io_type); |
| 1034 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1035 | if (Mode & MODE_RAND_SIZE) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1036 | printf("%s: %d DEBUG3 random write/trunc: min=%d, max=%d, mult = %d\n", |
| 1037 | Progname, Pid, min_size, max_size, mult_size); |
| 1038 | } |
| 1039 | else { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1040 | printf("%s: %d DEBUG3 grow_incr = %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1041 | Progname, Pid, grow_incr); |
| 1042 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1043 | if (Mode & MODE_RAND_LSEEK) { |
| 1044 | if (max_lseek == LSK_EOF) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1045 | printf("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile>\n", |
| 1046 | Progname, Pid, min_lseek); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1047 | else if (max_lseek == LSK_EOFPLUSGROW) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1048 | printf("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile+iosize>\n", |
| 1049 | Progname, Pid, min_lseek); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1050 | else if (max_lseek == LSK_EOFMINUSGROW) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1051 | printf("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile-iosize>\n", |
| 1052 | Progname, Pid, min_lseek); |
| 1053 | else |
| 1054 | printf("%s: %d DEBUG3 random lseek: min=%d, max=%d\n", |
| 1055 | Progname, Pid, min_lseek, max_lseek); |
| 1056 | } |
| 1057 | |
| 1058 | printf("%s: %d DEBUG3 check write interval = %d, check file interval = %d\n", |
| 1059 | Progname, Pid, write_check_inter, file_check_inter); |
| 1060 | |
| 1061 | printf("%s: %d DEBUG3 trunc interval = %d, trunc_incr = %d\n", |
| 1062 | Progname, Pid, trunc_inter, trunc_incr); |
| 1063 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1064 | if (no_file_check) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1065 | printf("%s: %d DEBUG3 no whole file checking will be done\n", |
| 1066 | Progname, Pid); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1067 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1068 | if (unlink_inter_ran == -1) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1069 | printf("%s: %d DEBUG3 unlink_inter = %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1070 | Progname, Pid, unlink_inter); |
| 1071 | } else { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1072 | printf("%s: %d DEBUG3 unlink_inter = %d, unlink_inter_ran = %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1073 | Progname, Pid, unlink_inter, unlink_inter_ran); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1074 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1075 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1076 | if (Debug > 8) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1077 | num=sizeof(Open_flags)/sizeof(int); |
| 1078 | printf("%s: %d DEBUG9 random open flags values:\n", Progname, Pid); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1079 | for (ind=0; ind<num; ind++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1080 | printf("\t%#o\n", Open_flags[ind]); |
| 1081 | } |
| 1082 | } |
| 1083 | } /* end of DEBUG > 2 */ |
| 1084 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1085 | if (Debug > 1 && num_procs > 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1086 | printf("%s: %d DEBUG2 about to fork %d more copies\n", Progname, |
| 1087 | Opid, num_procs-1); |
| 1088 | } |
| 1089 | |
| 1090 | fflush(stdout); /* ensure pending i/o is flushed before forking */ |
| 1091 | fflush(stderr); |
| 1092 | |
| 1093 | forker(num_procs, forker_mode, Progname); |
| 1094 | |
| 1095 | Pid=getpid(); /* reset after the forks */ |
| 1096 | /* |
| 1097 | * If user specified random seed(s), get that random seed value. |
| 1098 | * get random seed if it was not specified by the user. |
| 1099 | * This is done after the forks, because pid is used to get the seed. |
| 1100 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1101 | if (Nseeds == 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1102 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1103 | * If only one seed specified, all processes will get that seed. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1104 | */ |
| 1105 | Seed=Seeds[0]; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1106 | } else if (Nseeds > 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1107 | /* |
| 1108 | * More than one seed was specified. |
| 1109 | * The original process gets the first seed. Each |
| 1110 | * process will be get the next seed in the specified list. |
| 1111 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1112 | if (Opid == Pid) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1113 | Seed=Seeds[0]; |
| 1114 | } else { |
| 1115 | /* |
| 1116 | * If user didn't specify enough seeds, use default method. |
| 1117 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1118 | if (Forker_npids >= Nseeds) |
subrata_modak | 90d1e3c | 2008-08-21 13:07:02 +0000 | [diff] [blame] | 1119 | { |
| 1120 | struct timeval ts; |
| 1121 | gettimeofday(&ts,NULL); |
| 1122 | Seed=ts.tv_sec + Pid; /* default random seed */ |
| 1123 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1124 | else { |
| 1125 | Seed=Seeds[Forker_npids]; |
| 1126 | } |
| 1127 | } |
| 1128 | } else { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1129 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1130 | * Generate a random seed based on time and pid. |
| 1131 | * It has a good chance of being unique for each pid. |
| 1132 | */ |
subrata_modak | 90d1e3c | 2008-08-21 13:07:02 +0000 | [diff] [blame] | 1133 | struct timeval ts; |
| 1134 | gettimeofday(&ts,NULL); |
| 1135 | Seed=ts.tv_sec + Pid; /* default random seed */ |
| 1136 | //Seed=time(0) + Pid; /* default random seed */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | random_range_seed(Seed); |
| 1141 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1142 | if (using_random && Debug > 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1143 | printf("%s%s: %d DEBUG1 Using random seed of %d\n", |
| 1144 | Progname, TagName, Pid, Seed); |
| 1145 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1146 | if (unlink_inter_ran > 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * Find unlinking file interval. This must be done after |
| 1149 | * the seed was set. This allows multiple copies to |
| 1150 | * get different intervals. |
| 1151 | */ |
| 1152 | tmp=unlink_inter; |
| 1153 | unlink_inter=random_range(tmp, unlink_inter_ran, 1, NULL); |
| 1154 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1155 | if (Debug > 2) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 1156 | printf("%s: %d DEBUG3 Unlink interval is %d (random %ld - %d)\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1157 | Progname, Pid, unlink_inter, tmp, unlink_inter_ran); |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * re-exec all childern if reexec is set to REXEC_DOIT. |
| 1162 | * This is useful on MPP systems to get the |
| 1163 | * child process on another PE. |
| 1164 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1165 | if (reexec == REXEC_DOIT && Opid != Pid) { |
| 1166 | if (exec_path == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1167 | exec_path = argv[0]; |
| 1168 | /* Get space for cmd (2 extra, 1 for - and 1 fro NULL */ |
| 1169 | argv[0] = (char *)malloc(strlen(exec_path) + 2); |
| 1170 | sprintf(argv[0], "-%s", exec_path); |
| 1171 | } |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1172 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1173 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1174 | printf("%s: %d DEBUG3 %s/%d: execvp(%s, argv)\n", |
| 1175 | Progname, Pid, __FILE__, __LINE__, argv[0]); |
| 1176 | |
| 1177 | execvp(argv[0], argv); |
| 1178 | } |
| 1179 | |
| 1180 | /*** begin filename stuff here *****/ |
| 1181 | /* |
| 1182 | * Determine the number of files to be dealt with |
| 1183 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1184 | if (optind == argc) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | * no cmd line files, therfore, set |
| 1187 | * the default number of auto created files |
| 1188 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1189 | if (! num_auto_files && ! seq_auto_files) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1190 | num_auto_files=1; |
| 1191 | } |
| 1192 | else { |
| 1193 | first_file_ind=optind; |
| 1194 | num_files += argc-optind; |
| 1195 | } |
| 1196 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1197 | if (num_auto_files) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1198 | num_files += num_auto_files; |
| 1199 | } |
| 1200 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1201 | if (seq_auto_files) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1202 | num_files += seq_auto_files; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * get space for file names |
| 1207 | */ |
| 1208 | if ((filenames=(char *)malloc(num_files*PATH_MAX)) == NULL) { |
| 1209 | fprintf(stderr, "%s%s: %d %s/%d: malloc(%d) failed: %s\n", |
| 1210 | Progname, TagName, Pid, __FILE__, __LINE__, num_files*PATH_MAX, |
| 1211 | strerror(errno)); |
| 1212 | exit(1); |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * fill in filename cmd files then auto files. |
| 1217 | */ |
| 1218 | |
| 1219 | num=0; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1220 | if (first_file_ind) { |
| 1221 | for (ind=first_file_ind; ind<argc; ind++, num++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1222 | strcpy((char *)filenames+(num*PATH_MAX), argv[ind]); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * construct auto filename and insert them into filenames space |
| 1228 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1229 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1230 | for (ind=0;ind<num_auto_files; ind++, num++) { |
robbiew | aead6ba | 2003-04-17 16:49:18 +0000 | [diff] [blame] | 1231 | gettimeofday(&tv1,NULL); |
robbiew | 79ce8da | 2003-04-17 16:51:24 +0000 | [diff] [blame] | 1232 | sprintf((char *)filenames+(num*PATH_MAX), "%s/%s%ld%ld%d.%d", |
| 1233 | auto_dir, auto_file, (long)tv1.tv_sec, (long)tv1.tv_usec, rand(), ind ); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * construct auto seq filenames |
| 1238 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1239 | for (ind=1; ind<=seq_auto_files; ind++, num++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1240 | sprintf((char *)filenames+(num*PATH_MAX), "%s/%s%d", |
| 1241 | auto_dir, auto_file, ind); |
| 1242 | } |
| 1243 | |
| 1244 | /**** end filename stuff ****/ |
| 1245 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1246 | if (time_iterval > 0) |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1247 | { |
subrata_modak | 90d1e3c | 2008-08-21 13:07:02 +0000 | [diff] [blame] | 1248 | struct timeval ts; |
| 1249 | gettimeofday(&ts,NULL); |
| 1250 | start_time=ts.tv_sec; |
| 1251 | //start_time=time(0); |
| 1252 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1253 | |
| 1254 | /* |
| 1255 | * get space for I/O buffer |
| 1256 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1257 | if (grow_incr) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1258 | if ((Buffer=(char *)malloc(grow_incr+Alignment)) == NULL) { |
| 1259 | fprintf(stderr, "%s%s: %d %s/%d: malloc(%d) failed: %s\n", |
| 1260 | Progname, TagName, Pid, __FILE__, __LINE__, grow_incr, strerror(errno)); |
| 1261 | exit(1); |
| 1262 | } |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1263 | if (Alignment) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1264 | Buffer = Buffer + Alignment; |
| 1265 | |
| 1266 | } |
| 1267 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1268 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1269 | printf("%s: %d DEBUG3 num_files = %d\n", |
| 1270 | Progname, Pid, num_files); |
| 1271 | } |
| 1272 | |
| 1273 | #ifndef linux |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1274 | if (pre_alloc_space) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1275 | if (iterations == 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1276 | fprintf(stderr, "%s%s: %d %s/%d: can NOT pre-alloc and grow forever\n", |
| 1277 | Progname, TagName, Pid, __FILE__, __LINE__); |
| 1278 | exit(1); |
| 1279 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1280 | if (Mode & MODE_RAND_SIZE) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1281 | fprintf(stderr, |
| 1282 | "%s%s: %d %s/%d: can NOT pre-alloc and do random io size\n", |
| 1283 | Progname, TagName, Pid, __FILE__, __LINE__); |
| 1284 | exit(1); |
| 1285 | } |
| 1286 | |
| 1287 | total_grow_value=grow_incr * iterations; |
| 1288 | |
| 1289 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1290 | * attempt to limit |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1291 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1292 | if (bytes_to_consume && bytes_to_consume < total_grow_value) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1293 | total_grow_value=bytes_to_consume; |
| 1294 | } |
| 1295 | } |
| 1296 | #endif |
| 1297 | |
| 1298 | /* |
| 1299 | * If delaying between iterations, get amount time to |
| 1300 | * delaysecs in clocks or usecs. |
| 1301 | * If on the CRAY, delaytime is in clocks since |
| 1302 | * _rtc() will be used, which does not have the overhead |
| 1303 | * of gettimeofday(2). |
| 1304 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1305 | if (delaysecs) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1306 | #if CRAY |
| 1307 | int hz; |
| 1308 | hz=sysconf(_SC_CLK_TCK); |
| 1309 | delaytime=(int)((float)hz * delaysecs); |
| 1310 | #else |
| 1311 | delaytime=(int)((float)USECS_PER_SEC * delaysecs); |
| 1312 | #endif |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * This is the main iteration loop. |
| 1317 | * Each iteration, all files can be opened, written to, |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1318 | * read to check the write, check the whole file, |
| 1319 | * truncated, and closed. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1320 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1321 | for (Iter_cnt=1; ! stop ; Iter_cnt++) { |
subrata_modak | 90d1e3c | 2008-08-21 13:07:02 +0000 | [diff] [blame] | 1322 | struct timeval ts; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1323 | if (iterations && (Iter_cnt >= iterations+1)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1324 | strcpy(reason, "Hit iteration value"); |
| 1325 | stop=1; |
| 1326 | continue; |
| 1327 | } |
subrata_modak | 90d1e3c | 2008-08-21 13:07:02 +0000 | [diff] [blame] | 1328 | gettimeofday(&ts,NULL); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1329 | if ((time_iterval > 0) && (start_time + time_iterval < ts.tv_sec)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1330 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1331 | sprintf(reason, "Hit time value of %d", time_iterval); |
| 1332 | stop=1; |
| 1333 | continue; |
| 1334 | } |
| 1335 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1336 | if (bytes_to_consume && bytes_consumed >= bytes_to_consume) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1337 | sprintf(reason, "Hit bytes consumed value of %d", bytes_to_consume); |
| 1338 | stop=1; |
| 1339 | continue; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * This loop will loop through all files. |
| 1344 | * Each iteration, a single file can be opened, written to, |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1345 | * read to check the write, check the whole file, |
| 1346 | * truncated, and closed. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1347 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1348 | for (ind=0; ind<num_files; ind++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1349 | |
| 1350 | fflush(stdout); |
| 1351 | fflush(stderr); |
| 1352 | |
| 1353 | filename=(char *)filenames+(ind*PATH_MAX); |
| 1354 | Fileinfo.filename=(char *)filenames+(ind*PATH_MAX); |
| 1355 | |
| 1356 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1357 | if (open_flags == RANDOM_OPEN) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1358 | ret=Open_flags[random_range(0, sizeof(Open_flags)/sizeof(int)-1, 1, NULL)]; |
| 1359 | } |
| 1360 | |
| 1361 | else |
| 1362 | ret=open_flags; |
| 1363 | |
| 1364 | Fileinfo.openflags=ret; |
| 1365 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1366 | if (Debug > 3) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1367 | printf("%s: %d DEBUG3 %s/%d: %d Open filename = %s, open flags = %#o %s\n", |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1368 | Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename, ret, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1369 | openflags2symbols(ret, ",", NULL)); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1370 | } else if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1371 | printf("%s: %d DEBUG3 %s/%d: %d filename = %s, open flags = %#o\n", |
| 1372 | Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename, ret); |
| 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | * open file with desired flags. |
| 1377 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1378 | if ((fd=open(filename, ret, 0777)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1379 | fprintf(stderr, |
| 1380 | "%s%s: %d %s/%d: open(%s, %#o, 0777) returned -1, errno:%d %s\n", |
| 1381 | Progname, TagName, Pid, __FILE__, __LINE__, filename, ret, errno, strerror(errno)); |
| 1382 | handle_error(); |
| 1383 | continue; |
| 1384 | } |
| 1385 | |
| 1386 | Fileinfo.fd=fd; |
| 1387 | |
| 1388 | lkfile(fd, LOCK_EX, LKLVL1); /* lock if lockfile is LKLVL1 */ |
| 1389 | |
| 1390 | #ifndef linux |
| 1391 | /* |
| 1392 | * preallocation is only done once, if specified. |
| 1393 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1394 | if (pre_alloc_space) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1395 | if (pre_alloc(fd, total_grow_value) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1396 | cleanup(); |
| 1397 | exit(2); |
| 1398 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1399 | if (Debug > 1) { |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 1400 | printf("%s: %d DEBUG2 %s/%d: pre_allocated %ld for file %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1401 | Progname, Pid, __FILE__, __LINE__, total_grow_value, filename); |
| 1402 | } |
| 1403 | lkfile(fd, LOCK_UN, LKLVL1); /* release lock */ |
| 1404 | close(fd); |
| 1405 | Iter_cnt=0; /* reset outside loop to restart from one */ |
| 1406 | continue; |
| 1407 | } |
| 1408 | #endif |
| 1409 | |
| 1410 | /* |
| 1411 | * grow file by desired amount. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1412 | * growfile() will set the Grow_incr variable and |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1413 | * possiblly update the Mode variable indicating |
| 1414 | * if we are dealing with a FIFO file. |
| 1415 | */ |
| 1416 | |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 1417 | /* BUG:14136 (don't go past ext2's filesize limit) */ |
subrata_modak | 243eecf | 2007-11-09 12:12:44 +0000 | [diff] [blame] | 1418 | curr_size = file_size(fd); |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 1419 | if (curr_size+grow_incr>=ext2_limit) { |
| 1420 | lkfile(fd, LOCK_UN, LKLVL1); /* release lock */ |
| 1421 | close(fd); |
| 1422 | sprintf(reason, "Reached %ld filesize which is almost %ld limit.",curr_size, ext2_limit); |
| 1423 | stop=1; |
| 1424 | continue; |
| 1425 | } |
| 1426 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1427 | if (growfile(fd, filename, grow_incr, Buffer, &curr_size) != 0) { /* BUG:14136 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1428 | handle_error(); |
| 1429 | lkfile(fd, LOCK_UN, LKLVL1); /* release lock */ |
| 1430 | close(fd); |
| 1431 | continue; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * check if last write is not corrupted |
| 1436 | */ |
| 1437 | if ( check_write(fd, write_check_inter, filename, |
| 1438 | Mode) != 0 ) { |
| 1439 | handle_error(); |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * Check that whole file is not corrupted. |
| 1444 | */ |
| 1445 | if ( check_file(fd, file_check_inter, filename, |
| 1446 | no_file_check) != 0 ) { |
| 1447 | handle_error(); |
| 1448 | } |
| 1449 | |
| 1450 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1451 | * shrink file by desired amount if it is time |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1452 | */ |
| 1453 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1454 | if (shrinkfile(fd, filename, trunc_incr, trunc_inter, Mode) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1455 | handle_error(); |
| 1456 | } |
| 1457 | |
| 1458 | lkfile(fd, LOCK_UN, LKLVL1); /* release lock */ |
| 1459 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1460 | if (Debug > 4) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1461 | printf("%s: %d DEBUG5 %s/%d: %d Closing file %s fd:%d \n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1462 | Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename, fd); |
| 1463 | close(fd); |
| 1464 | |
| 1465 | /* |
| 1466 | * Unlink the file if that is desired |
| 1467 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1468 | if (unlink_inter && (Iter_cnt % unlink_inter == 0)) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1469 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1470 | if (Debug > 4) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1471 | printf("%s: %d DEBUG5 %s/%d: %d Unlinking file %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1472 | Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename); |
| 1473 | |
| 1474 | unlink(filename); |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * delay while staying active for "delaysecs" seconds. |
| 1479 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1480 | if (delaytime) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1481 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1482 | int ct, end; |
| 1483 | #ifdef CRAY |
| 1484 | ct=_rtc(); |
| 1485 | end=ct+delaytime; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1486 | while (ct < end) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1487 | ct = _rtc(); |
| 1488 | } |
| 1489 | #else |
| 1490 | struct timeval curtime; |
| 1491 | gettimeofday(&curtime, NULL); |
| 1492 | ct=curtime.tv_sec*USECS_PER_SEC + curtime.tv_usec; |
| 1493 | end=ct+delaytime; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1494 | while (ct < end) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1495 | |
| 1496 | gettimeofday(&curtime, NULL); |
| 1497 | ct=curtime.tv_sec*USECS_PER_SEC + curtime.tv_usec; |
| 1498 | } |
| 1499 | #endif |
| 1500 | } |
| 1501 | } |
| 1502 | #ifndef linux |
| 1503 | /* |
| 1504 | * if Iter_cnt == 0, then we pre allocated space to all files |
| 1505 | * and we are starting outside loop over. Set pre_alloc_space |
| 1506 | * to zero otherwise we get in infinite loop |
| 1507 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1508 | if (Iter_cnt == 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1509 | pre_alloc_space=0; |
| 1510 | } |
| 1511 | #endif |
| 1512 | |
| 1513 | |
| 1514 | } /* end iteration for loop */ |
| 1515 | |
| 1516 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1517 | if (Debug) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1518 | printf("%s%s: %d %s/%d: DONE %d iterations to %d files. %s\n", |
| 1519 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, num_files, reason); |
| 1520 | } |
| 1521 | fflush(stdout); |
| 1522 | fflush(stderr); |
| 1523 | |
| 1524 | cleanup(); |
| 1525 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1526 | if (Errors) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1527 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1528 | printf("%s%s: %d DEBUG3 %d error(s) encountered\n", |
| 1529 | Progname, TagName, Pid, Errors); |
| 1530 | printf("%s%s: %d DEBUG3 %s/%d: exiting with value of 1\n", Progname, TagName, Pid, __FILE__, __LINE__); |
| 1531 | } |
| 1532 | exit(1); |
| 1533 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1534 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1535 | printf("%s%s: %d DEBUG3 %s/%d: no errors, exiting with value of 0\n", Progname, TagName, Pid, __FILE__, __LINE__); |
mridge | 2422745 | 2004-05-17 22:06:02 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1538 | exit(0); |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 1539 | return 0; /* to keep compiler happy */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /*********************************************************************** |
| 1543 | * |
| 1544 | ***********************************************************************/ |
| 1545 | int |
| 1546 | set_sig() |
| 1547 | { |
| 1548 | int sig; |
| 1549 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1550 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1551 | /* |
| 1552 | * now loop through all signals and set the handlers |
| 1553 | */ |
| 1554 | |
| 1555 | for (sig = 1; sig < NSIG; sig++) { |
| 1556 | switch (sig) { |
| 1557 | case SIGKILL: |
| 1558 | case SIGSTOP: |
| 1559 | case SIGCONT: |
| 1560 | #ifdef CRAY |
| 1561 | case SIGINFO: |
| 1562 | case SIGRECOVERY: |
| 1563 | #endif /* CRAY */ |
| 1564 | #ifdef SIGCKPT |
| 1565 | case SIGCKPT: |
| 1566 | #endif /* SIGCKPT */ |
| 1567 | #ifdef SIGRESTART |
| 1568 | case SIGRESTART: |
| 1569 | #endif /* SIGRESTART */ |
| 1570 | case SIGCLD: |
| 1571 | break; |
| 1572 | |
| 1573 | default: |
| 1574 | #ifdef sgi |
| 1575 | sigset( sig, sig_handler ); |
| 1576 | #else |
| 1577 | /* linux and cray */ |
| 1578 | signal(sig, sig_handler); |
| 1579 | #endif |
| 1580 | break; |
| 1581 | } |
| 1582 | } /* endfor */ |
| 1583 | |
| 1584 | |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
| 1588 | /*********************************************************************** |
| 1589 | * |
| 1590 | ***********************************************************************/ |
| 1591 | void |
| 1592 | sig_handler(sig) |
| 1593 | int sig; |
| 1594 | { |
| 1595 | int exit_stat = 2; |
| 1596 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1597 | if (sig == SIGUSR2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1598 | fprintf(stdout, "%s%s: %d %s/%d: received SIGUSR2 (%d) - stopping.\n", |
| 1599 | Progname, TagName, Pid, __FILE__, __LINE__, sig); |
| 1600 | #ifndef sgi |
| 1601 | signal(sig, sig_handler); /* allow us to get this signal more than once */ |
| 1602 | #endif |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1603 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1604 | } else if (sig == SIGINT) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1605 | /* The user has told us to cleanup, don't pretend it's an error. */ |
| 1606 | exit_stat=0; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1607 | if (Debug != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1608 | fprintf(stderr, "%s%s: %d %s/%d: received unexpected signal: %d\n", Progname, TagName, |
| 1609 | Pid, __FILE__, __LINE__, sig); |
| 1610 | } |
| 1611 | } else { |
| 1612 | fprintf(stderr, "%s%s: %d %s/%d: received unexpected signal: %d\n", Progname, TagName, |
| 1613 | Pid, __FILE__, __LINE__, sig); |
| 1614 | } |
| 1615 | |
| 1616 | notify_others(); |
| 1617 | cleanup(); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1618 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1619 | printf("%s%s: %d DEBUG3 %s/%d: Exiting with a value of %d\n", |
| 1620 | Progname, TagName, Pid, __FILE__, __LINE__, exit_stat); |
| 1621 | } |
| 1622 | exit(exit_stat); |
| 1623 | } |
| 1624 | |
| 1625 | /*********************************************************************** |
| 1626 | * this function attempts to send SIGUSR2 to other growfiles processes |
| 1627 | * telling them to stop. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1628 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1629 | ***********************************************************************/ |
| 1630 | static void |
| 1631 | notify_others() |
| 1632 | { |
| 1633 | static int send_signals = 0; |
| 1634 | int ind; |
| 1635 | extern int Forker_pids[]; |
| 1636 | extern int Forker_npids; |
| 1637 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1638 | if (Sync_with_others && send_signals == 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1639 | |
| 1640 | #if CRAY |
| 1641 | send_signals=1; /* only send signals once */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1642 | if (Debug > 1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1643 | printf("%s%s: %d DEBUG2 %s/%d: Sending SIGUSR2 to pgrp\n", |
| 1644 | Progname, TagName, Pid, __FILE__, __LINE__); |
| 1645 | killm(C_PGRP, getpgrp(), SIGUSR2); |
| 1646 | #else |
| 1647 | send_signals=1; /* only send signals once */ |
| 1648 | |
| 1649 | for (ind=0; ind< Forker_npids; ind++) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1650 | if (Forker_pids[ind] != Pid) |
| 1651 | if (Debug > 1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1652 | printf("%s%s: %d DEBUG2 %s/%d: Sending SIGUSR2 to pid %d\n", |
| 1653 | Progname, TagName, Pid, __FILE__, __LINE__, Forker_pids[ind]); |
| 1654 | kill(Forker_pids[ind], SIGUSR2); |
| 1655 | } |
| 1656 | #endif |
| 1657 | } |
| 1658 | |
| 1659 | } |
| 1660 | |
| 1661 | /*********************************************************************** |
| 1662 | * this function will count the number of errors encountered. |
| 1663 | * This function will call upanic if wanted or cleanup and |
| 1664 | * and exit is Maxerrs were encountered. |
| 1665 | ***********************************************************************/ |
| 1666 | int |
| 1667 | handle_error() |
| 1668 | { |
| 1669 | Errors++; |
| 1670 | |
| 1671 | #ifdef CRAY |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1672 | if (Errors & Upanic_on_error) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1673 | upanic(PA_PANIC); |
| 1674 | } |
| 1675 | #endif |
| 1676 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1677 | if (Maxerrs && Errors >= Maxerrs) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1678 | printf("%s%s: %d %s/%d: %d Hit max errors value of %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1679 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, Maxerrs); |
| 1680 | notify_others(); |
| 1681 | cleanup(); |
| 1682 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1683 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1684 | printf("%s%s: %d DEBUG3 %d error(s) encountered\n", |
| 1685 | Progname, TagName, Pid, Errors); |
| 1686 | printf("%s%s: %d DEBUG3 %s/%d: exiting with value of 1\n", Progname, TagName, Pid, __FILE__, __LINE__); |
| 1687 | } |
| 1688 | |
| 1689 | exit(1); |
| 1690 | } |
| 1691 | |
| 1692 | return 0; |
| 1693 | } |
| 1694 | |
| 1695 | /*********************************************************************** |
| 1696 | * |
| 1697 | ***********************************************************************/ |
| 1698 | int |
| 1699 | cleanup() |
| 1700 | { |
| 1701 | int ind; |
| 1702 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1703 | if (remove_files) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1704 | if (Debug > 2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1705 | printf("%s: %d DEBUG3 Removing all %d files\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1706 | Progname, Pid, num_files); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1707 | for (ind=0; ind<=num_files; ind++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1708 | unlink(filenames+(ind*PATH_MAX)); |
| 1709 | } |
| 1710 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1711 | if (using_random && Debug > 1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1712 | printf("%s%s: %d DEBUG2 Used random seed: %d\n", |
| 1713 | Progname, TagName, Pid, Seed); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /*********************************************************************** |
| 1718 | * |
| 1719 | ***********************************************************************/ |
| 1720 | void |
| 1721 | usage() |
| 1722 | { |
| 1723 | fprintf(stderr, |
| 1724 | "Usage: %s%s [-bhEluy][[-g grow_incr][-i num][-t trunc_incr][-T trunc_inter]\n", |
| 1725 | Progname, TagName ); |
| 1726 | fprintf(stderr, |
| 1727 | "[-d auto_dir][-e maxerrs][-f auto_file][-N num_files][-w][-c chk_inter][-D debug]\n"); |
| 1728 | fprintf(stderr, |
| 1729 | "[-s seed][-S seq_auto_files][-p][-P PANIC][-I io_type][-o open_flags][-B maxbytes]\n"); |
| 1730 | fprintf(stderr, |
| 1731 | "[-r iosizes][-R lseeks][-U unlk_inter][-W tagname] [files]\n"); |
| 1732 | |
| 1733 | return; |
| 1734 | |
| 1735 | } /* end of usage */ |
| 1736 | |
| 1737 | /*********************************************************************** |
| 1738 | * |
| 1739 | ***********************************************************************/ |
| 1740 | void |
| 1741 | help() |
| 1742 | { |
| 1743 | usage(); |
| 1744 | |
| 1745 | fprintf(stdout, "\ |
| 1746 | -h Specfied to print this help and exit.\n\ |
| 1747 | -b Specfied to execute in sync mode.(def async mode)\n\ |
| 1748 | -B maxbytes Max bytes to consume by all files. growfiles exits when more\n\ |
| 1749 | than maxbytes have been consumed. (def no chk) If maxbytes ends\n\ |
| 1750 | with the letter 'b', maxbytes is multiplied by BSIZE\n\ |
| 1751 | -C write_chk Specifies how often to check the last write (default 1)\n\ |
| 1752 | -c file_chk Specifies how often to check whole file (default 0)\n\ |
| 1753 | -d auto_dir Specifies the directory to auto created files. (default .)\n\ |
| 1754 | -D debug_lvl Specifies the debug level (default 1)\n\ |
| 1755 | -E Print examples and exit\n\ |
| 1756 | -e errs The number errors that will terminate this program (def 100)\n\ |
| 1757 | -f auto_file Specifies the base filename files created. (default \"gf\")\n\ |
| 1758 | -g grow_incr Specfied to grow by incr for each num. (default 4096)\n\ |
| 1759 | grow_incr may end in b for blocks\n\ |
| 1760 | If -r option is used, this option is ignored and size is random\n\ |
| 1761 | -H delay Amount of time to delay between each file (default 0.0)\n\ |
| 1762 | -I io_type Specifies io type: s - sync, p - polled async, a - async (def s)\n\ |
| 1763 | l - listio sync, L - listio async, r - random\n\ |
| 1764 | -i iteration Specfied to grow each file num times. 0 means forever (default 1)\n\ |
| 1765 | -l Specfied to do file locking around write/read/trunc\n\ |
| 1766 | If specified twice, file locking after open to just before close\n\ |
| 1767 | -L time Specfied to exit after time secs, must be used with -i.\n\ |
| 1768 | -N num_files Specifies the number of files to be created.\n\ |
| 1769 | The default is zero if cmd line files.\n\ |
| 1770 | The default is one if no cmd line files.\n\ |
| 1771 | -n num_procs Specifies the number of copies of this cmd.\n\ |
| 1772 | -o op_type Specifies open flages: (def O_RDWR,O_CREAT) op_type can be 'random'\n\ |
| 1773 | -O offset adjust i/o buffer alignment by offset bytes\n\ |
| 1774 | -P PANIC Specifies to call upanic on error.\n\ |
| 1775 | -p Specifies to pre-allocate space\n\ |
| 1776 | -q pattern pattern can be a - ascii, p - pid with boff, o boff (def)\n\ |
| 1777 | A - Alternating bits, r - random, O - all ones, z - all zeros,\n\ |
| 1778 | c - checkboard, C - counting\n\ |
| 1779 | -R [min-]max random lseek before write and trunc, max of -1 means filesz,\n\ |
| 1780 | -2 means filesz+grow, -3 filesz-grow. (min def is 0)\n\ |
| 1781 | -r [min-]max random io write size (min def is 1)\n\ |
| 1782 | -S seq_auto_files Specifies the number of seqental auto files (default 0)\n\ |
| 1783 | -s seed[,seed...] Specifies the random number seed (default time(0)+pid)\n\ |
| 1784 | -t trunc_incr Specfied the amount to shrink file. (default 4096)\n\ |
| 1785 | trunc_inter may end in b for blocks\n\ |
| 1786 | If -R option is used, this option is ignored and trunc is random\n\ |
| 1787 | -T trunc_inter Specfied the how many grows happen before shrink. (default 0)\n\ |
| 1788 | -u unlink files before exit\n\ |
| 1789 | -U ui[-ui2] Unlink files each ui iteration (def 0)\n\ |
| 1790 | -w Specfied to grow via lseek instead of writes.\n\ |
| 1791 | -W tag-name Who-am-i. My Monster tag name. (used by Monster).\n\ |
| 1792 | -x Re-exec children before continuing - useful on MPP systems\n\ |
| 1793 | -y Attempt to sync copies - if one fails it will send sigusr2 to others\n\ |
| 1794 | Action to each file every iteration is open, write, write check\n\ |
| 1795 | file check, trunc and closed.\n"); |
| 1796 | |
| 1797 | return; |
| 1798 | } |
| 1799 | |
| 1800 | /*********************************************************************** |
| 1801 | * |
| 1802 | ***********************************************************************/ |
| 1803 | void |
| 1804 | prt_examples(FILE *stream) |
| 1805 | { |
| 1806 | /* This example creates 200 files in directory dir1. It writes */ |
| 1807 | /* 4090 bytes 100 times then truncates 408990 bytes off the file */ |
| 1808 | /* The file contents are checked every 1000 grow. */ |
| 1809 | fprintf(stream, |
| 1810 | "# run forever: writes of 4090 bytes then on every 100 iterval\n\ |
| 1811 | # truncate file by 408990 bytes. Done to 200 files in dir1.\n\ |
| 1812 | %s -i 0 -g 4090 -T 100 -t 408990 -l -C 10 -c 1000 -d dir1 -S 200\n\n", Progname); |
| 1813 | |
| 1814 | /* same as above with 5000 byte grow and a 499990 byte tuncate */ |
| 1815 | fprintf(stream, |
| 1816 | "# same as above with writes of 5000 bytes and truncs of 499990\n\ |
| 1817 | %s -i 0 -g 5000 -T 100 -t 499990 -l -C 10 -c 1000 -d dir2 -S 200\n\n", Progname); |
| 1818 | |
| 1819 | /* This example beats on opens and closes */ |
| 1820 | fprintf(stream, |
| 1821 | "# runs forever: beats on opens and closes of file ocfile - no io\n\ |
| 1822 | %s -i 0 -g 0 -c 0 -C 0 ocfile\n\n", Progname); |
| 1823 | |
| 1824 | fprintf(stream, |
| 1825 | "# writes 4096 to files until 50 blocks are written\n\ |
| 1826 | %s -i 0 -g 4096 -B 50b file1 file2\n\n", Progname); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1827 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1828 | fprintf(stream, |
| 1829 | "# write one byte to 750 files in gdir then unlinks them\n\ |
| 1830 | %s -g 1 -C 0 -d gdir -u -S 750\n\n", Progname); |
| 1831 | |
| 1832 | fprintf(stream, |
| 1833 | "# run 30 secs: random iosize, random lseek up to eof\n\ |
| 1834 | %s -r 1-5000 -R 0--1 -i 0 -L 30 -C 1 g_rand1 g_rand2\n\n", Progname); |
| 1835 | |
| 1836 | fprintf(stream, |
| 1837 | "# run 30 secs: grow by lseek then write single byte, trunc every 10 itervals\n\ |
| 1838 | %s -g 5000 -wlu -i 0 -L 30 -C 1 -T 10 g_sleek1 g_lseek2\n\n", Progname); |
| 1839 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1840 | fprintf(stream, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1841 | "# run forever: 5 copies of random iosize, random lseek to beyond eof,\n\ |
| 1842 | # rand io types doing a trunc every 5 iterations, with unlinks.\n\ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1843 | %s -i0 -r 1-50000 -R 0--2 -I r -C1 -l -n5 -u -U 100-200 gf_rana gf_ranb\n\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1844 | Progname); |
| 1845 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1846 | fprintf(stream, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1847 | "# run forever: 5 copies of random iosize, random lseek to beyond eof,\n\ |
| 1848 | # random open flags, rand io types doing a trunc every 10 iterations.\n\ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1849 | %s -i0 -r 1-50000 -R 0--2 -o random -I r -C0 -l -T 20 -uU100-200 -n 5 gf_rand1 gf_rand2\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1850 | Progname); |
| 1851 | |
| 1852 | |
| 1853 | return; |
| 1854 | } |
| 1855 | |
| 1856 | /*********************************************************************** |
| 1857 | * |
| 1858 | * The file descriptor current offset is assumed to be the end of the |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1859 | * file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1860 | * Woffset will be set to the offset before the write. |
| 1861 | * Grow_incr will be set to the size of the write or lseek write. |
| 1862 | ***********************************************************************/ |
| 1863 | int |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 1864 | growfile(fd, file, grow_incr, buf, curr_size_ptr) /* BUG:14136 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1865 | int fd; |
| 1866 | char *file; |
| 1867 | int grow_incr; |
| 1868 | char *buf; |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 1869 | long *curr_size_ptr; /* BUG:14136 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1870 | { |
robbiew | dd19842 | 2004-11-22 17:13:21 +0000 | [diff] [blame] | 1871 | off_t noffset; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1872 | int ret; |
| 1873 | int cur_offset; |
| 1874 | char *errmsg; |
robbiew | dd19842 | 2004-11-22 17:13:21 +0000 | [diff] [blame] | 1875 | off_t fsize; /* current size of file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1876 | int size_grew; /* size the file grew */ |
| 1877 | struct stat stbuf; |
robbiew | dd19842 | 2004-11-22 17:13:21 +0000 | [diff] [blame] | 1878 | off_t tmp = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1879 | |
| 1880 | /* |
| 1881 | * Do a stat on the open file. |
| 1882 | * If the file is a fifo, set the bit in Mode variable. |
| 1883 | * This fifo check must be done prior to growfile() returning. |
| 1884 | * Also get the current size of the file. |
| 1885 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1886 | if (fstat(fd, &stbuf) != -1) { |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 1887 | if (S_ISFIFO(stbuf.st_mode)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1888 | Fileinfo.mode |= MODE_FIFO; |
| 1889 | Mode |= MODE_FIFO; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1890 | if (Debug > 3) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1891 | printf("%s: %d DEBUG4 %s/%d: file is a fifo - no lseek or truncs,\n", |
| 1892 | Progname, Pid, __FILE__, __LINE__); |
| 1893 | } |
| 1894 | fsize = stbuf.st_size; |
| 1895 | |
| 1896 | } else { |
| 1897 | fprintf(stderr, "%s%s: %d %s/%d: Unable to fstat(%d, &buf), errno:%d %s\n", |
| 1898 | Progname, TagName, Pid, __FILE__, __LINE__, fd, errno, strerror(errno)); |
| 1899 | |
| 1900 | return -1; |
| 1901 | } |
| 1902 | |
| 1903 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1904 | if (grow_incr <= 0) { /* don't attempt i/o if grow_incr <= 0 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1905 | |
| 1906 | Grow_incr=grow_incr; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1907 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1908 | printf("%s: %d DEBUG3 %s/%d: Not attempting to grow, growsize == %d\n", |
| 1909 | Progname, Pid, __FILE__, __LINE__, grow_incr); |
| 1910 | return grow_incr; |
| 1911 | } |
| 1912 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1913 | if (Mode & MODE_RAND_SIZE) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1914 | grow_incr=random_range(min_size, max_size, mult_size, &errmsg); |
| 1915 | if (errmsg != NULL) { |
| 1916 | fprintf(stderr, "%s%s: %d %s/%d: random_range() failed - %s\n", Progname, TagName, Pid, __FILE__, __LINE__, errmsg); |
| 1917 | return -1; |
| 1918 | } |
| 1919 | Grow_incr=grow_incr; |
| 1920 | } |
| 1921 | else |
| 1922 | Grow_incr=grow_incr; |
| 1923 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1924 | if (! (Mode & MODE_FIFO)) { |
| 1925 | if ((cur_offset=lseek(fd,0,SEEK_CUR)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1926 | fprintf(stderr, "%s%s: %d %s/%d: tell failed: %s\n", |
| 1927 | Progname, TagName, Pid, __FILE__, __LINE__, strerror(errno)); |
| 1928 | return -1; |
| 1929 | } |
| 1930 | } |
| 1931 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1932 | if (Mode & MODE_GROW_BY_LSEEK) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1933 | Woffset=fsize; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1934 | if (Debug > 2) { |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 1935 | printf("%s: %d DEBUG3 %s/%d: Current size of file is %ld\n", Progname, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1936 | Pid, __FILE__, __LINE__, Woffset); |
| 1937 | printf("%s: %d DEBUG3 %s/%d: lseeking to %d byte with SEEK_END\n", Progname, |
| 1938 | Pid, __FILE__, __LINE__, grow_incr-1); |
| 1939 | } |
| 1940 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1941 | if ((noffset=lseek(fd, grow_incr-1, SEEK_END)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1942 | fprintf(stderr, "%s%s: %s/%d: lseek(fd, %d, SEEK_END) failed: %s\n", |
| 1943 | Progname, TagName, __FILE__, __LINE__, grow_incr-1, strerror(errno)); |
| 1944 | return -1; |
| 1945 | } |
| 1946 | |
| 1947 | lkfile(fd, LOCK_EX, LKLVL0); /* get exclusive lock */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1948 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1949 | #if NEWIO |
| 1950 | ret=lio_write_buffer(fd, io_type, "w", 1, SIGUSR1, &errmsg,0); |
| 1951 | #else |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1952 | ret=write_buffer(fd, io_type, "w", 1, 0, &errmsg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1953 | #endif |
| 1954 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1955 | if (ret != 1) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1956 | fprintf(stderr, "%s%s: %d %s/%d: %d %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1957 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1958 | if (ret == -ENOSPC) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1959 | cleanup(); |
| 1960 | exit(2); |
| 1961 | } |
| 1962 | } |
| 1963 | /*** |
| 1964 | write(fd, "w", 1); |
| 1965 | ****/ |
| 1966 | |
| 1967 | lkfile(fd, LOCK_UN, LKLVL0); |
| 1968 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1969 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1970 | printf("%s: %d DEBUG3 %s/%d: %d wrote 1 byte to file\n", |
| 1971 | Progname, Pid, __FILE__, __LINE__, Iter_cnt); |
| 1972 | |
| 1973 | } else { /* end of grow by lseek */ |
| 1974 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1975 | if (Fileinfo.openflags & O_APPEND) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1976 | /* |
| 1977 | * Deal with special case of the open flag containing O_APPEND. |
| 1978 | * If it does, the current offset does not matter since the write |
| 1979 | * will be done end of the file. |
| 1980 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1981 | if (Debug > 4) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1982 | printf("%s: %d DEBUG5 %s/%d: dealing with O_APPEND condition\n", |
| 1983 | Progname, Pid, __FILE__, __LINE__ ); |
| 1984 | lkfile(fd, LOCK_EX, LKLVL0); /* get exclusive lock */ |
| 1985 | |
| 1986 | /* |
| 1987 | * do fstat again to get size of the file. |
| 1988 | * This is done inside a file lock (if locks are being used). |
| 1989 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1990 | if (fstat(fd, &stbuf) != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1991 | Woffset = stbuf.st_size; |
| 1992 | } else { |
| 1993 | fprintf(stderr, "%s%s: %d %s/%d: Unable to fstat(%d, &buf), errno:%d %s\n", |
| 1994 | Progname, TagName, Pid, __FILE__, __LINE__, fd, errno, strerror(errno)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 1995 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1996 | lkfile(fd, LOCK_UN, LKLVL0); /* release lock */ |
| 1997 | return -1; |
| 1998 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 1999 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2000 | printf("%s: %d DEBUG3 %s/%d: dealing with O_APPEND condition (offset:fsz:%d)\n", |
| 2001 | Progname, Pid, __FILE__, __LINE__, (int)stbuf.st_size); |
| 2002 | |
| 2003 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2004 | } else if (Mode & MODE_RAND_LSEEK) { |
| 2005 | if (max_lseek == LSK_EOF) { /* within file size */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2006 | noffset=random_range(min_lseek, fsize, 1, NULL); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2007 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2008 | else if (max_lseek == LSK_EOFPLUSGROW) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2009 | /* max to beyond file size */ |
| 2010 | noffset=random_range(min_lseek, fsize+grow_incr, 1, NULL); |
| 2011 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2012 | else if (max_lseek == LSK_EOFMINUSGROW) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2013 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2014 | * Attempt to not grow the file. |
| 2015 | * If the i/o will fit from min_lseek to EOF, |
| 2016 | * pick offset to allow it to fit. |
| 2017 | * Otherwise, pick the min_lseek offset and grow |
| 2018 | * file by smallest amount. |
| 2019 | * If min_lseek is != 0, there will be a problem |
| 2020 | * with whole file checking if file is ever smaller |
| 2021 | * than min_lseek. |
| 2022 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2023 | if (fsize <= min_lseek + grow_incr) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2024 | noffset=min_lseek; /* file will still grow */ |
| 2025 | else |
| 2026 | noffset=random_range(min_lseek, fsize-grow_incr, 1, NULL); |
| 2027 | } |
| 2028 | else { |
| 2029 | noffset=random_range(min_lseek, max_lseek, 1, NULL); |
| 2030 | } |
| 2031 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2032 | if ((Woffset=lseek(fd, noffset, SEEK_SET)) == -1) { |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2033 | fprintf(stderr, "%s%s: %d %s/%d: lseek(%d, %ld, SEEK_SET) l2 failed: %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2034 | Progname, TagName, Pid, __FILE__, __LINE__, fd, noffset, strerror(errno)); |
| 2035 | return -1; |
| 2036 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2037 | else if (Debug > 2) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2038 | printf("%s: %d DEBUG3 %s/%d: lseeked to random offset %ld (fsz:%d)\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2039 | Progname, Pid, __FILE__, __LINE__, Woffset, |
| 2040 | (int)stbuf.st_size); |
| 2041 | |
| 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * lseek to end of file only if not fifo |
| 2046 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2047 | else if (! (Mode & MODE_FIFO)) { |
| 2048 | if ((Woffset=lseek(fd, 0, SEEK_END)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2049 | fprintf(stderr, "%s%s: %d %s/%d: lseek(fd, 0, SEEK_END) failed: %s\n", |
| 2050 | Progname, TagName, Pid, __FILE__, __LINE__, strerror(errno)); |
| 2051 | return -1; |
| 2052 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2053 | else if (Debug > 2) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2054 | printf("%s: %d DEBUG3 %s/%d: lseeked to end of file, offset %ld\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2055 | Progname, Pid, __FILE__, __LINE__, Woffset); |
| 2056 | } |
| 2057 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2058 | if (Pattern == PATTERN_OFFSET) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2059 | datapidgen(STATIC_NUM, buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2060 | else if (Pattern == PATTERN_PID) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2061 | datapidgen(Pid, buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2062 | else if (Pattern == PATTERN_ASCII) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2063 | dataasciigen(NULL, buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2064 | else if (Pattern == PATTERN_RANDOM) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2065 | databingen('r', buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2066 | else if (Pattern == PATTERN_ALT) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2067 | databingen('a', buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2068 | else if (Pattern == PATTERN_CHKER) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2069 | databingen('c', buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2070 | else if (Pattern == PATTERN_CNTING) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2071 | databingen('C', buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2072 | else if (Pattern == PATTERN_ZEROS) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2073 | databingen('z', buf, grow_incr, Woffset); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2074 | else if (Pattern == PATTERN_ONES) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2075 | databingen('o', buf, grow_incr, Woffset); |
| 2076 | else |
| 2077 | dataasciigen(NULL, buf, grow_incr, Woffset); |
| 2078 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2079 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2080 | printf("%s: %d DEBUG3 %s/%d: attempting to write %d bytes\n", |
| 2081 | Progname, Pid, __FILE__, __LINE__, grow_incr); |
| 2082 | |
| 2083 | lkfile(fd, LOCK_EX, LKLVL0); /* get exclusive lock */ |
| 2084 | |
| 2085 | /***** |
| 2086 | ret=write(fd, buf, grow_incr); |
| 2087 | |
| 2088 | tmp=tell(fd); |
| 2089 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2090 | lkfile(fd, LOCK_UN, LKLVL0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2091 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2092 | if (ret != grow_incr) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2093 | fprintf(stderr, "%s: %s/%d: write failed: %s\n", |
| 2094 | Progname, __FILE__, __LINE__, strerror(errno)); |
| 2095 | return -1; |
| 2096 | } |
| 2097 | *****/ |
| 2098 | |
| 2099 | #if NEWIO |
| 2100 | ret=lio_write_buffer(fd, io_type, buf, grow_incr, |
| 2101 | SIGUSR1, &errmsg,0); |
| 2102 | #else |
| 2103 | ret=write_buffer(fd, io_type, buf, grow_incr, 0, &errmsg); |
| 2104 | #endif |
| 2105 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2106 | if (Mode & MODE_FIFO) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2107 | /* If it is a fifo then just pretend the file |
| 2108 | * offset is where we think it should be. |
| 2109 | */ |
| 2110 | tmp = Woffset + grow_incr; |
| 2111 | } |
| 2112 | else{ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2113 | if ((tmp=lseek(fd,0,SEEK_CUR)) < 0) { /* get offset after the write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2114 | fprintf(stderr, "%s%s: %s/%d: tell(2) failed: %d %s\n", |
| 2115 | Progname, TagName, __FILE__, __LINE__, errno, strerror(errno) ); |
| 2116 | return -1; |
| 2117 | } |
| 2118 | #if NEWIO |
subrata_modak | 142cfbb | 2009-04-03 06:58:18 +0000 | [diff] [blame] | 2119 | #if defined(sgi) || defined(__linux__) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2120 | /* If this is POSIX I/O and it is via aio_{read,write} |
| 2121 | * or lio_listio then after completion of the I/O the |
| 2122 | * value of the file offset for the file is |
| 2123 | * unspecified--which means we cannot trust what |
| 2124 | * tell() told us. Fudge it here. |
| 2125 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2126 | if ((io_type & LIO_IO_ASYNC_TYPES) || (io_type & LIO_RANDOM)) { |
| 2127 | if (tmp != Woffset + grow_incr) { |
| 2128 | if (Debug > 5) { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 2129 | printf("%s: %d DEBUG6 %s/%d: posix fudge, forcing tmp (%"PRId64") to match Woffset+grow_incr (%"PRId64")\n", |
| 2130 | Progname, Pid, __FILE__, __LINE__, (int64_t)tmp, (int64_t)Woffset+grow_incr); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2131 | } |
| 2132 | tmp = Woffset + grow_incr; |
| 2133 | } |
| 2134 | } |
subrata_modak | 142cfbb | 2009-04-03 06:58:18 +0000 | [diff] [blame] | 2135 | #endif /* sgi __linux__ */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2136 | #endif |
| 2137 | } |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 2138 | *curr_size_ptr=tmp; /* BUG:14136 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2139 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2140 | lkfile(fd, LOCK_UN, LKLVL0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2141 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2142 | if (ret != grow_incr) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2143 | fprintf(stderr, "%s%s: %d %s/%d: %d %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2144 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2145 | if (ret == -ENOSPC) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2146 | cleanup(); |
| 2147 | exit(2); |
| 2148 | } |
| 2149 | return -1; |
| 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * Check for a condition where the file was truncated just before |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2154 | * the write. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2155 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2156 | if (tmp != Woffset + grow_incr) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2157 | /* |
| 2158 | * The offset after the write was not as expected. |
| 2159 | * This could be caused by the following: |
| 2160 | * - file truncated after the lseek and before the write. |
| 2161 | * - the file was written to after fstat and before the write |
| 2162 | * and the file was opened with O_APPEND. |
| 2163 | * |
| 2164 | * The pattern written to the file will be considered corrupted. |
| 2165 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2166 | if (Debug > 0 && lockfile) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2167 | printf("%s%s: %d DEBUG1 %s/%d: offset after write(%ld) not as exp(%ld+%d=%ld)\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2168 | Progname, TagName, Pid, __FILE__, __LINE__, tmp, Woffset, grow_incr, Woffset+grow_incr); |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2169 | printf("%s%s: %d DEBUG1 %s/%d: %d Assuming file changed by another process, resetting offset:%ld (expect pattern mismatch)\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2170 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, tmp-grow_incr); |
| 2171 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2172 | if (Debug > 4) { |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2173 | printf("%s: %d DEBUG5 %s/%d: about to chop Woffset. tmp=%ld, grow_incr=%d, Woffset was %ld\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2174 | Progname, Pid, __FILE__, __LINE__, tmp, grow_incr, Woffset); |
| 2175 | } |
| 2176 | Woffset=tmp-grow_incr; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2177 | if (Woffset < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2178 | Woffset = 0; |
| 2179 | } |
| 2180 | |
| 2181 | } /* end of grow by write */ |
| 2182 | |
| 2183 | |
| 2184 | /* |
| 2185 | * Woffset - holds start of grow (start of write expect in grow by lseek) |
| 2186 | * Grow_incr - holds size of grow (write). |
| 2187 | * fsize - holds size of file before write |
| 2188 | */ |
| 2189 | size_grew=(Woffset + Grow_incr) - fsize; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2190 | if (Debug > 1) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2191 | if (Mode & MODE_FIFO) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2192 | printf("%s: %d DEBUG2 %s/%d: file is fifo, %d wrote %d bytes\n", |
| 2193 | Progname, Pid, __FILE__, __LINE__, Grow_incr, Iter_cnt); |
| 2194 | } |
| 2195 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2196 | else if (size_grew > 0) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2197 | printf("%s: %d DEBUG2 %s/%d: %d wrote %d bytes(off:%ld), grew file by %d bytes\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2198 | Progname, Pid, __FILE__, __LINE__, Iter_cnt, Grow_incr, Woffset, size_grew); |
| 2199 | else |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2200 | printf("%s: %d DEBUG2 %s/%d: %d wrote %d bytes(off:%ld), did not grow file\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2201 | Progname, Pid, __FILE__, __LINE__, Iter_cnt, Grow_incr, Woffset); |
| 2202 | } |
| 2203 | |
| 2204 | bytes_consumed += size_grew; |
| 2205 | return 0; |
| 2206 | |
| 2207 | } /* end of growfile */ |
| 2208 | |
| 2209 | /*********************************************************************** |
| 2210 | * shrinkfile file by trunc_incr. file can not be made smaller than |
| 2211 | * size zero. Therefore, if trunc_incr is larger than file size, |
| 2212 | * file will be truncated to zero. |
| 2213 | * The file descriptor current offset is assumed to be the end of the |
| 2214 | * file. |
| 2215 | * |
| 2216 | ***********************************************************************/ |
| 2217 | int |
| 2218 | shrinkfile(fd, filename, trunc_incr, trunc_inter, just_trunc) |
| 2219 | int fd; |
| 2220 | char *filename; |
| 2221 | int trunc_incr; |
| 2222 | int trunc_inter; /* interval */ |
| 2223 | int just_trunc; /* lseek has already been done for you */ |
| 2224 | { |
| 2225 | static int shrink_cnt = 0; |
| 2226 | int cur_offset; |
| 2227 | int new_offset; |
| 2228 | int ret; |
| 2229 | #ifdef CRAY |
| 2230 | int offset; |
| 2231 | #endif |
| 2232 | |
| 2233 | shrink_cnt++; |
| 2234 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2235 | if (trunc_inter == 0 || (shrink_cnt % trunc_inter != 0)) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2236 | if (Debug > 3) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2237 | printf("%s: %d DEBUG4 %s/%d: Not shrinking file - not time, iter=%d, cnt=%d\n", |
| 2238 | Progname, Pid, __FILE__, __LINE__, trunc_inter, shrink_cnt); |
| 2239 | return 0; /* not this time */ |
| 2240 | } |
| 2241 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2242 | if (Mode & MODE_FIFO) { |
| 2243 | if (Debug > 5) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2244 | printf("%s: %d DEBUG5 %s/%d: Not attempting to shrink a FIFO\n", |
| 2245 | Progname, Pid, __FILE__, __LINE__); |
| 2246 | return 0; /* can not truncate fifo */ |
| 2247 | } |
| 2248 | |
| 2249 | lkfile(fd, LOCK_EX, LKLVL0); |
| 2250 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2251 | if ((cur_offset=lseek(fd,0,SEEK_CUR)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2252 | fprintf(stderr, "%s%s: %d %s/%d: tell(%d) failed: %s\n", |
| 2253 | Progname, TagName, Pid, __FILE__, __LINE__, fd, strerror(errno)); |
| 2254 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2255 | return -1; |
| 2256 | } |
| 2257 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2258 | if (Mode & MODE_RAND_LSEEK) { |
| 2259 | if (max_lseek <= -1) { |
| 2260 | if ((new_offset=file_size(fd)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2261 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2262 | return -1; |
| 2263 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2264 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2265 | if (new_offset < min_lseek) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2266 | new_offset=min_lseek; |
| 2267 | else |
| 2268 | new_offset=random_range(min_lseek, new_offset, 1, NULL); |
| 2269 | } |
| 2270 | else { |
| 2271 | new_offset=random_range(min_lseek, max_lseek, 1, NULL); |
| 2272 | } |
| 2273 | |
| 2274 | #ifdef CRAY |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2275 | if ((offset=lseek(fd, new_offset, SEEK_SET)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2276 | fprintf(stderr, "%s%s: %d %s/%d: lseek(%d, %d, SEEK_SET) l3 failed: %s\n", |
| 2277 | Progname, TagName, Pid, __FILE__, __LINE__, fd, new_offset, strerror(errno)); |
| 2278 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2279 | return -1; |
| 2280 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2281 | else if (Debug > 3) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2282 | printf("%s: %d DEBUG4 %s/%d: lseeked to random offset %d\n", |
| 2283 | Progname, Pid, __FILE__, __LINE__, offset); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2284 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2285 | #endif |
| 2286 | } |
| 2287 | |
| 2288 | else { /* remove trunc_incr from file */ |
| 2289 | |
| 2290 | new_offset = cur_offset-trunc_incr; |
| 2291 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2292 | if (new_offset < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2293 | new_offset=0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2294 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2295 | #ifdef CRAY |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2296 | if (lseek(fd, new_offset, SEEK_SET) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2297 | fprintf(stderr, "%s%s: %d %s/%d: lseek(fd, %d, SEEK_SET) l4 failed: %s\n", |
| 2298 | Progname, TagName, Pid, __FILE__, __LINE__, new_offset, strerror(errno)); |
| 2299 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2300 | return -1; |
| 2301 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2302 | else if (Debug > 3) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2303 | printf("%s: %d DEBUG4 %s/%d: lseeked to offset %d, %d bytes from end\n", |
| 2304 | Progname, Pid, __FILE__, __LINE__, new_offset, trunc_incr); |
| 2305 | #endif |
| 2306 | } |
| 2307 | |
| 2308 | |
| 2309 | #ifdef CRAY |
| 2310 | ret=trunc(fd); |
| 2311 | #else |
| 2312 | ret=ftruncate(fd, new_offset ); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame^] | 2313 | if (ret == 0 && Debug > 3) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2314 | printf("%s: %d DEBUG4 %s/%d: ftruncated to offset %d, %d bytes from end\n", |
| 2315 | Progname, Pid, __FILE__, __LINE__, new_offset, trunc_incr); |
| 2316 | } |
| 2317 | #endif |
| 2318 | |
| 2319 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2320 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2321 | if (ret == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2322 | #ifdef CRAY |
| 2323 | fprintf(stderr, "%s%s: %d %s/%d: trunc failed: %s\n", |
| 2324 | Progname, TagName, Pid, __FILE__, __LINE__, strerror(errno)); |
| 2325 | #else |
| 2326 | fprintf(stderr, "%s%s: %d %s/%d: ftruncate failed: %s\n", |
| 2327 | Progname, TagName, Pid, __FILE__, __LINE__, strerror(errno)); |
| 2328 | #endif |
| 2329 | return -1; |
| 2330 | } |
| 2331 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2332 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2333 | printf("%s: %d DEBUG2 %s/%d: trunc file by %d bytes, to size of = %d bytes\n", |
| 2334 | Progname, Pid, __FILE__, __LINE__, cur_offset-new_offset, new_offset); |
| 2335 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2336 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2337 | |
| 2338 | bytes_consumed -= (cur_offset - new_offset); |
| 2339 | return 0; |
| 2340 | |
| 2341 | } /* end of shrinkfile */ |
| 2342 | |
| 2343 | /*********************************************************************** |
| 2344 | * |
| 2345 | ***********************************************************************/ |
| 2346 | int |
| 2347 | check_write(fd, cf_inter, filename, mode) |
| 2348 | int fd; |
| 2349 | int cf_inter; /* check file interval */ |
| 2350 | char *filename; /* needed for error messages */ |
| 2351 | int mode; /* write mode */ |
| 2352 | { |
| 2353 | int fsize; |
| 2354 | static int cf_count = 0; |
| 2355 | int ret = 0; |
| 2356 | int tmp; |
| 2357 | char *errmsg; |
| 2358 | char *ptr; |
| 2359 | |
| 2360 | cf_count++; |
| 2361 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2362 | if (cf_inter == 0 || (cf_count % cf_inter != 0)) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2363 | if (Debug > 4) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2364 | printf("%s: %d DEBUG5 %s/%d: no write check, not time iter=%d, cnt=%d\n", |
| 2365 | Progname, Pid, __FILE__, __LINE__, cf_inter, cf_count); |
| 2366 | return 0; /* no check done */ |
| 2367 | } |
| 2368 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2369 | if (Grow_incr <= 0) { |
| 2370 | if (Debug > 3) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2371 | printf("%s: %d DEBUG4 %s/%d: No write validation, Grow_incr = %d, offset = %ld\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2372 | Progname, Pid, __FILE__, __LINE__, Grow_incr, Woffset); |
| 2373 | return 0; /* no check */ |
| 2374 | } |
| 2375 | |
| 2376 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2377 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2378 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2379 | * Get the shared file lock. We need to hold the lock from before |
| 2380 | * we do the stat until after the read. |
| 2381 | */ |
| 2382 | lkfile(fd, LOCK_SH, LKLVL0); |
| 2383 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2384 | if ((fsize=file_size(fd)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2385 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2386 | return -1; |
| 2387 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2388 | } else if (fsize <= Woffset) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2389 | /* |
| 2390 | * The file was truncated between write and now. |
| 2391 | * The contents of our last write is totally gone, no check. |
| 2392 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2393 | if (Debug > 1) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2394 | printf("%s%s: %d DEBUG2 %s/%d: %d File size (%d) smaller than where last wrote (%ld)- no write validation\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2395 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, fsize, Woffset); |
| 2396 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2397 | return 0; /* no validation, but not an error */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2398 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2399 | } else if (fsize < (Woffset + Grow_incr)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2400 | /* |
| 2401 | * The file was truncated between write and now. |
| 2402 | * Part of our last write has been truncated, adjust our Grow_incr |
| 2403 | * to reflect this. |
| 2404 | */ |
| 2405 | |
| 2406 | tmp=Grow_incr; |
| 2407 | Grow_incr=fsize-Woffset; |
| 2408 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2409 | if (Debug > 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2410 | |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2411 | printf("%s%s: %d DEBUG2 %s/%d: %d fsz:%d, lost(%d)of wrt(off:%ld, sz:%d), adj=%d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2412 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, fsize, tmp-Grow_incr, Woffset, tmp, Grow_incr); |
| 2413 | } |
| 2414 | |
| 2415 | } |
| 2416 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2417 | if (Debug > 2) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2418 | printf("%s: %d DEBUG3 %s/%d: about to do write validation, offset = %ld, size = %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2419 | Progname, Pid, __FILE__, __LINE__, Woffset, Grow_incr); |
| 2420 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2421 | if (! (mode & MODE_FIFO)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2422 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2423 | if (lseek(fd, Woffset, 0) == -1) { |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2424 | fprintf(stderr, "%s%s: %d %s/%d: lseek(fd, %ld, 0) failed: %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2425 | Progname, TagName, Pid, __FILE__, __LINE__, Woffset, strerror(errno)); |
| 2426 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2427 | if (Debug > 3) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2428 | printf("%s: %d DEBUG4 %s/%d: lseeked to offset:%ld\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2429 | Progname, Pid, __FILE__, __LINE__, Woffset); |
| 2430 | } |
| 2431 | |
| 2432 | /* |
| 2433 | * Read last writes data |
| 2434 | */ |
| 2435 | #if NEWIO |
| 2436 | ret=lio_read_buffer(fd, io_type, Buffer, Grow_incr, SIGUSR1, &errmsg,0); |
| 2437 | #else |
| 2438 | ret=read_buffer(fd, io_type, Buffer, Grow_incr, 0, &errmsg); |
| 2439 | #endif |
| 2440 | |
| 2441 | /* |
| 2442 | * report the error and debug information before releasing |
| 2443 | * the file lock |
| 2444 | */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2445 | if (ret != Grow_incr) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2446 | fprintf(stderr, "%s%s: %d %s/%d: %d CW %s\n", Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg); |
| 2447 | { |
| 2448 | struct stat stbuf; |
| 2449 | fstat(fd, &stbuf); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2450 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2451 | printf("%s%s: %d DEBUG3 %s/%d: fd:%d, offset:%d, fsize:%d, openflags:%#o\n", |
| 2452 | Progname, TagName, Pid, __FILE__, __LINE__, fd, |
| 2453 | (int)lseek(fd,SEEK_CUR,0), /* FIXME: 64bit/LFS ? */ |
| 2454 | (int)stbuf.st_size, |
| 2455 | Fileinfo.openflags); |
| 2456 | } |
| 2457 | |
| 2458 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2459 | return 1; |
| 2460 | } |
| 2461 | |
| 2462 | |
| 2463 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2464 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2465 | if (Mode & MODE_GROW_BY_LSEEK) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2466 | /* check that all zeros upto last character */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2467 | for (ptr=Buffer; ptr < (Buffer+Grow_incr-1); ptr++) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2468 | if (*ptr != '\0') { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2469 | fprintf(stderr, |
| 2470 | "%s%s: %d %s/%d: data mismatch at offset %d, exp:%#o(zerofilled), act:%#o in file %s\n", |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2471 | Progname, TagName, Pid, __FILE__, __LINE__, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2472 | (int)(Woffset+(Grow_incr-(Buffer-ptr))), |
| 2473 | 0, *ptr, filename); |
| 2474 | fflush(stderr); |
| 2475 | return 1; |
| 2476 | } |
| 2477 | } |
| 2478 | /* check that the last char is a 'w' */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2479 | if (*ptr != 'w') { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2480 | fprintf(stderr, |
| 2481 | "%s%s: %d %s/%d: data mismatch at offset %d, exp:%#o(zerofilled), act:%#o in file %s\n", |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2482 | Progname, TagName, Pid, __FILE__, __LINE__, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2483 | (int)(Woffset+(Grow_incr-(Buffer-ptr))), 'w', |
| 2484 | *ptr, filename); |
| 2485 | fflush(stderr); |
| 2486 | return 1; |
| 2487 | } |
| 2488 | return 0; /* all is well */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2489 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2490 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2491 | else if (Pattern == PATTERN_OFFSET) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2492 | ret=datapidchk(STATIC_NUM, Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2493 | else if (Pattern == PATTERN_PID) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2494 | ret=datapidchk(Pid, Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2495 | else if (Pattern == PATTERN_ASCII) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2496 | ret=dataasciichk(NULL, Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2497 | else if (Pattern == PATTERN_RANDOM) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2498 | ; /* no check for random */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2499 | else if (Pattern == PATTERN_ALT) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2500 | ret=databinchk('a', Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2501 | else if (Pattern == PATTERN_CHKER) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2502 | ret=databinchk('c', Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2503 | else if (Pattern == PATTERN_CNTING) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2504 | ret=databinchk('C', Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2505 | else if (Pattern == PATTERN_ZEROS) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2506 | ret=databinchk('z', Buffer, Grow_incr, Woffset, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2507 | else if (Pattern == PATTERN_ONES) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2508 | ret=databinchk('o', Buffer, Grow_incr, Woffset, &errmsg); |
| 2509 | else |
| 2510 | ret=dataasciichk(NULL, Buffer, Grow_incr, Woffset, &errmsg); |
| 2511 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2512 | if (ret >= 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2513 | fprintf(stderr, "%s%s: %d %s/%d: %d CW %s in file %s\n", |
| 2514 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg, filename); |
| 2515 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2516 | if (Debug > 0) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2517 | printf("%s%s: %d DEBUG1 %s/%d: **fd:%d, lk:%d, offset:%ld, sz:%d open flags:%#o %s\n", |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2518 | Progname, TagName, Pid, __FILE__, __LINE__, fd, lockfile, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2519 | Woffset, Grow_incr, Fileinfo.openflags, openflags2symbols(Fileinfo.openflags, ",", NULL)); |
| 2520 | |
| 2521 | fflush(stderr); |
| 2522 | return 1; |
| 2523 | } |
| 2524 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2525 | if (Debug > 6) |
robbiew | 7976805 | 2005-12-23 17:14:18 +0000 | [diff] [blame] | 2526 | printf("%s: %d DEBUG7 %s/%d: No corruption detected on write validation , offset = %ld, size = %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2527 | Progname, Pid, __FILE__, __LINE__, Woffset, Grow_incr); |
| 2528 | |
| 2529 | return 0; /* all is well */ |
| 2530 | } |
| 2531 | |
| 2532 | |
| 2533 | |
| 2534 | /*********************************************************************** |
| 2535 | * |
| 2536 | ***********************************************************************/ |
| 2537 | int |
| 2538 | check_file(fd, cf_inter, filename, no_file_check) |
| 2539 | int fd; |
| 2540 | int cf_inter; /* check file interval */ |
| 2541 | char *filename; /* needed for error messages */ |
| 2542 | int no_file_check; /* if set, do not do file content check */ |
| 2543 | { |
| 2544 | int fsize; |
| 2545 | static int cf_count = 0; |
| 2546 | char *buf; |
| 2547 | int ret; |
| 2548 | int ret_val = 0; |
| 2549 | int rd_cnt; |
| 2550 | int rd_size; |
| 2551 | char *errmsg; |
| 2552 | |
| 2553 | cf_count++; |
| 2554 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2555 | if (cf_inter == 0 || (cf_count % cf_inter != 0)) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2556 | if (Debug > 4) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2557 | printf("%s: %d DEBUG5 %s/%d: No file check - not time, iter=%d, cnt=%d\n", |
| 2558 | Progname, Pid, __FILE__, __LINE__, cf_inter, cf_count); |
| 2559 | return 0; /* no check done */ |
| 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * if we can't determine file content, don't bother checking |
| 2564 | */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2565 | if (no_file_check) { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2566 | if (Debug > 4) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2567 | printf("%s: %d DEBUG5 %s/%d: No file check, lseek grow or random lseeks\n", |
| 2568 | Progname, Pid, __FILE__, __LINE__); |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
| 2572 | /* |
| 2573 | * Lock the file. We need to have the file lock before |
| 2574 | * the stat and until after the last read to prevent |
| 2575 | * a trunc/truncate from "corrupting" our data. |
| 2576 | */ |
| 2577 | lkfile(fd, LOCK_SH, LKLVL0); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2578 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2579 | if ((fsize=file_size(fd)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2580 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2581 | return -1; |
| 2582 | } |
| 2583 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2584 | if (fsize == 0) { |
| 2585 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2586 | printf("%s: %d DEBUG3 %s/%d: No file validation, file size == 0\n", |
| 2587 | Progname, Pid, __FILE__, __LINE__); |
| 2588 | |
| 2589 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2590 | return 0; |
| 2591 | } |
| 2592 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2593 | if (Debug > 2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2594 | printf("%s: %d DEBUG3 %s/%d: about to do file validation\n", |
| 2595 | Progname, Pid, __FILE__, __LINE__); |
| 2596 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2597 | if (fsize > MAX_FC_READ) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2598 | /* |
| 2599 | * read the file in MAX_FC_READ chuncks. |
| 2600 | */ |
| 2601 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2602 | if ((buf=(char *)malloc(MAX_FC_READ)) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2603 | fprintf(stderr, "%s%s: %s/%d: malloc(%d) failed: %s\n", Progname, TagName, |
| 2604 | __FILE__, __LINE__, MAX_FC_READ, strerror(errno)); |
| 2605 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2606 | return -1; |
| 2607 | } |
| 2608 | |
| 2609 | lseek(fd, 0, SEEK_SET); |
| 2610 | |
| 2611 | lkfile(fd, LOCK_SH, LKLVL0); /* get lock on file before getting file size */ |
| 2612 | |
| 2613 | rd_cnt=0; |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2614 | while (rd_cnt < fsize) { |
| 2615 | if (fsize - rd_cnt > MAX_FC_READ) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2616 | rd_size=MAX_FC_READ; |
| 2617 | else |
| 2618 | rd_size=fsize - rd_cnt; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2619 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2620 | #if NEWIO |
| 2621 | ret=lio_read_buffer(fd, io_type, buf, rd_size, |
| 2622 | SIGUSR1, &errmsg,0); |
| 2623 | #else |
| 2624 | ret=read_buffer(fd, io_type, buf, rd_size, 0, &errmsg); |
| 2625 | #endif |
| 2626 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2627 | if (ret != rd_size) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2628 | fprintf(stderr, "%s%s: %d %s/%d: %d CFa %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2629 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg); |
| 2630 | free(buf); |
| 2631 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2632 | return -1; |
| 2633 | } |
| 2634 | /** |
| 2635 | read(fd, buf, rd_size); |
| 2636 | ***/ |
| 2637 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2638 | if (Pattern == PATTERN_OFFSET) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2639 | ret=datapidchk(STATIC_NUM, buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2640 | else if (Pattern == PATTERN_PID) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2641 | ret=datapidchk(Pid, buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2642 | else if (Pattern == PATTERN_ASCII) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2643 | ret=dataasciichk(NULL, buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2644 | else if (Pattern == PATTERN_RANDOM) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2645 | ; /* no checks for random */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2646 | else if (Pattern == PATTERN_ALT) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2647 | ret=databinchk('a', buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2648 | else if (Pattern == PATTERN_CHKER) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2649 | ret=databinchk('c', buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2650 | else if (Pattern == PATTERN_CNTING) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2651 | ret=databinchk('C', buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2652 | else if (Pattern == PATTERN_ZEROS) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2653 | ret=databinchk('z', buf, rd_size, rd_cnt, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2654 | else if (Pattern == PATTERN_ONES) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2655 | ret=databinchk('o', buf, rd_size, rd_cnt, &errmsg); |
| 2656 | else |
| 2657 | ret=dataasciichk(NULL, buf, rd_size, rd_cnt, &errmsg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2658 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2659 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2660 | if (ret >= 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2661 | fprintf(stderr, |
| 2662 | "%s%s: %d %s/%d: %d CFp %s in file %s\n", |
| 2663 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg, filename); |
| 2664 | fflush(stderr); |
| 2665 | ret_val=1; |
| 2666 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2667 | break; |
| 2668 | } |
| 2669 | rd_cnt += rd_size; |
| 2670 | } |
| 2671 | |
| 2672 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2673 | |
| 2674 | free(buf); |
| 2675 | |
| 2676 | } |
| 2677 | else { |
| 2678 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2679 | * Read the whole file in a single read |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2680 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame^] | 2681 | if ((buf = (char *)malloc(fsize)) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2682 | fprintf(stderr, "%s%s: %s/%d: malloc(%d) failed: %s\n", Progname, TagName, |
| 2683 | __FILE__, __LINE__, fsize, strerror(errno)); |
| 2684 | fflush(stderr); |
| 2685 | return -1; |
| 2686 | } |
| 2687 | |
| 2688 | lseek(fd, 0, SEEK_SET); |
| 2689 | |
| 2690 | /**** |
| 2691 | read(fd, buf, fsize); |
| 2692 | ****/ |
| 2693 | #if NEWIO |
| 2694 | ret=lio_read_buffer(fd, io_type, buf, fsize, SIGUSR1, &errmsg,0); |
| 2695 | #else |
| 2696 | ret=read_buffer(fd, io_type, buf, fsize, 0, &errmsg); |
| 2697 | #endif |
| 2698 | |
| 2699 | /* unlock the file as soon as we can */ |
| 2700 | lkfile(fd, LOCK_UN, LKLVL0); |
| 2701 | |
| 2702 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2703 | if (ret != fsize) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2704 | fprintf(stderr, "%s%s: %d %s/%d: %d CFw %s\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2705 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg); |
| 2706 | ret_val=1; |
| 2707 | } |
| 2708 | else { |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2709 | if (Pattern == PATTERN_OFFSET) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2710 | ret=datapidchk(STATIC_NUM, buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2711 | else if (Pattern == PATTERN_PID) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2712 | ret=datapidchk(Pid, buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2713 | else if (Pattern == PATTERN_ASCII) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2714 | ret=dataasciichk(NULL, buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2715 | else if (Pattern == PATTERN_RANDOM) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2716 | ; /* no check for random */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2717 | else if (Pattern == PATTERN_ALT) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2718 | ret=databinchk('a', buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2719 | else if (Pattern == PATTERN_CHKER) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2720 | ret=databinchk('c', buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2721 | else if (Pattern == PATTERN_CNTING) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2722 | ret=databinchk('C', buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2723 | else if (Pattern == PATTERN_ZEROS) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2724 | ret=databinchk('z', buf, fsize, 0, &errmsg); |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2725 | else if (Pattern == PATTERN_ONES) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2726 | ret=databinchk('o', buf, fsize, 0, &errmsg); |
| 2727 | else |
| 2728 | ret=dataasciichk(NULL, buf, fsize, 0, &errmsg); |
| 2729 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2730 | if (ret >= 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2731 | fprintf(stderr, "%s%s: %d %s/%d: %d CFw %s in file %s\n", |
| 2732 | Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg, filename); |
| 2733 | fflush(stderr); |
| 2734 | ret_val=1; |
| 2735 | } |
| 2736 | } |
| 2737 | free(buf); |
| 2738 | } |
| 2739 | |
| 2740 | return ret_val; |
| 2741 | |
| 2742 | } /* end of check_file */ |
| 2743 | |
| 2744 | /*********************************************************************** |
| 2745 | * |
| 2746 | ***********************************************************************/ |
| 2747 | int |
| 2748 | file_size(int fd) |
| 2749 | { |
| 2750 | struct stat sb; |
| 2751 | |
| 2752 | if (fstat(fd, &sb) < 0) { |
| 2753 | fprintf(stderr, "%s%s: %d %s/%d: Unable to fstat(%d, &buf), errno:%d %s\n", |
| 2754 | Progname, TagName, Pid, __FILE__, __LINE__, fd, errno, strerror(errno)); |
| 2755 | return -1; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2756 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | return sb.st_size; |
| 2760 | } |
| 2761 | |
| 2762 | /*********************************************************************** |
| 2763 | * do file lock/unlock action. |
| 2764 | ***********************************************************************/ |
| 2765 | int |
| 2766 | lkfile(int fd, int operation, int lklevel) |
| 2767 | { |
| 2768 | char *errmsg; |
| 2769 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2770 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 2771 | if (lockfile == lklevel) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2772 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2773 | if (Debug > 5) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2774 | switch (operation) { |
| 2775 | case LOCK_UN: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2776 | printf("%s: %d DEBUG6 %s/%d: Attempting to release lock on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2777 | Progname, Pid, __FILE__, __LINE__, fd); |
| 2778 | break; |
| 2779 | |
| 2780 | case LOCK_SH: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2781 | printf("%s: %d DEBUG6 %s/%d: Attempting to get read/shared lock on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2782 | Progname, Pid, __FILE__, __LINE__, fd); |
| 2783 | break; |
| 2784 | |
| 2785 | case LOCK_EX: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2786 | printf("%s: %d DEBUG6 %s/%d: Attempting to get write/exclusive lock on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2787 | Progname, Pid, __FILE__, __LINE__, fd); |
| 2788 | break; |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | /* |
| 2793 | * Attempt to get/release desired lock. |
| 2794 | * file_lock will attempt to do action over and over again until |
| 2795 | * either an unretryable error or the action is completed. |
| 2796 | */ |
| 2797 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2798 | if (file_lock(fd, operation, &errmsg) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2799 | printf("%s%s: %d %s/%d: Unable to perform lock operation. %s\n", |
| 2800 | Progname, TagName, Pid, __FILE__, __LINE__, errmsg); |
| 2801 | |
| 2802 | /* do we count this as an error? handle_error(); */ |
| 2803 | return -1; |
| 2804 | } |
| 2805 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2806 | if (Debug > 2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2807 | switch (operation) { |
| 2808 | case LOCK_UN: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2809 | printf("%s: %d DEBUG3 %s/%d: Released lock on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2810 | Progname, Pid, __FILE__, __LINE__, fd); |
| 2811 | break; |
| 2812 | |
| 2813 | case LOCK_SH: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2814 | printf("%s: %d DEBUG3 %s/%d: Got read/shared lock on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2815 | Progname, Pid, __FILE__, __LINE__, fd); |
| 2816 | break; |
| 2817 | |
| 2818 | case LOCK_EX: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2819 | printf("%s: %d DEBUG3 %s/%d: Got write/exclusive lock on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2820 | Progname, Pid, __FILE__, __LINE__, fd); |
| 2821 | break; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 2822 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2823 | default: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 2824 | printf("%s: %d DEBUG3 %s/%d: Completed action %d on fd %d\n", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2825 | Progname, Pid, __FILE__, __LINE__, operation, fd); |
| 2826 | break; |
| 2827 | } |
| 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | #ifndef linux |
| 2835 | /*********************************************************************** |
| 2836 | * |
| 2837 | ***********************************************************************/ |
| 2838 | int |
mridge | dec5a46 | 2005-02-25 15:49:37 +0000 | [diff] [blame] | 2839 | pre_alloc(int fd, long size) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2840 | { |
| 2841 | |
| 2842 | #ifdef CRAY |
| 2843 | long avl; |
| 2844 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2845 | if (ialloc(fd, size, IA_CONT, &avl) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2846 | fprintf(stderr, "%s%s %s/%d: Unable to pre-alloc space: ialloc failed: %d %s\n", |
| 2847 | Progname, TagName, |
| 2848 | __FILE__, __LINE__, errno, strerror(errno)); |
| 2849 | return -1; |
| 2850 | } |
| 2851 | #endif |
| 2852 | |
| 2853 | #ifdef sgi |
| 2854 | struct flock f; |
| 2855 | |
| 2856 | f.l_whence = 0; |
| 2857 | f.l_start = 0; |
| 2858 | f.l_len = size; |
| 2859 | |
| 2860 | /* non-zeroing reservation */ |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 2861 | if (fcntl( fd, F_RESVSP, &f ) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2862 | fprintf(stderr, "%s%s %s/%d: Unable to pre-alloc space: fcntl(F_RESVSP) failed: %d %s\n", |
| 2863 | Progname, TagName, |
| 2864 | __FILE__, __LINE__, errno, strerror(errno)); |
| 2865 | return -1; |
| 2866 | } |
| 2867 | |
| 2868 | #endif |
| 2869 | |
| 2870 | return 0; |
| 2871 | } |
| 2872 | #endif |