blob: 7231d349e4901319949d8b1e3691c4dae18f2c0c [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
subrata_modak4bb656a2009-02-26 12:02:09 +00003 *
plars865695b2001-08-27 22:15:12 +00004 * 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_modak4bb656a2009-02-26 12:02:09 +00007 *
plars865695b2001-08-27 22:15:12 +00008 * 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_modak4bb656a2009-02-26 12:02:09 +000011 *
plars865695b2001-08-27 22:15:12 +000012 * 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_modak4bb656a2009-02-26 12:02:09 +000018 *
plars865695b2001-08-27 22:15:12 +000019 * 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_modak4bb656a2009-02-26 12:02:09 +000022 *
plars865695b2001-08-27 22:15:12 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
plars865695b2001-08-27 22:15:12 +000030 * 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_modak4bb656a2009-02-26 12:02:09 +000042 * process cmdline
plars865695b2001-08-27 22:15:12 +000043 * 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>
robbiewa70576c2003-03-04 18:33:41 +000070#include <errno.h>
plars865695b2001-08-27 22:15:12 +000071#include <stdlib.h>
72#include <ctype.h>
plars865695b2001-08-27 22:15:12 +000073#include <sys/types.h>
mridge24227452004-05-17 22:06:02 +000074#include <time.h>
mridge1f61f0d2004-05-18 17:08:11 +000075#include <sys/file.h>
mridge24227452004-05-17 22:06:02 +000076#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000077#include <sys/stat.h>
78#include <sys/time.h>
79#include <sys/param.h>
80#include <sys/signal.h>
mridge1f61f0d2004-05-18 17:08:11 +000081#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000082#include <errno.h>
83#include <string.h>
subrata_modakf17ac842009-09-07 09:06:31 +000084#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000085#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
robbiew92b688b2004-03-01 22:36:38 +000094#include "test.h"
95
96/* LTP status reporting */
97char *TCID="growfiles"; /* Default test program identifier. */
98int TST_TOTAL=1; /* Total number of test cases. */
99extern 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)
103void
104myexit (int x)
105{
subrata_modak4bb656a2009-02-26 12:02:09 +0000106 if (x)
robbiew92b688b2004-03-01 22:36:38 +0000107 tst_resm (TFAIL, "Test failed");
108 else
109 tst_resm (TPASS, "Test passed");
110 tst_exit();
111}
112
plars865695b2001-08-27 22:15:12 +0000113extern char *openflags2symbols();
114
115extern int parse_open_flags();
116extern int background();
117extern int forker();
118extern int datapidgen();
119extern void databingen();
120extern int datapidchk();
121extern int databinchk();
122extern int file_lock();
123
124int file_size();
125int check_write();
126int shrinkfile();
127int check_file();
128int growfile();
129int cleanup();
130int handle_error();
131int lkfile();
132void usage();
133void help();
134void prt_examples();
135int set_sig();
136void sig_handler();
137static void notify_others();
138#ifndef linux
139int 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
162char *Progname;
vapier93205ff2006-02-15 06:53:26 +0000163int Debug = 0;
plars865695b2001-08-27 22:15:12 +0000164
165int Pid=0;
166
167int io_type = 0; /* I/O type -sync */
robbiew85b1b032004-04-20 15:13:20 +0000168
169#ifdef O_LARGEFILE
170int open_flags = O_RDWR|O_CREAT|O_LARGEFILE; /* open flags */
171#else
172#warning O_LARGEFILE is not defined!
plars865695b2001-08-27 22:15:12 +0000173int open_flags = O_RDWR|O_CREAT; /* open flags */
robbiew85b1b032004-04-20 15:13:20 +0000174#endif
plars865695b2001-08-27 22:15:12 +0000175
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_modakbdbaec52009-02-26 12:14:51 +0000188
plars865695b2001-08-27 22:15:12 +0000189#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
207int num_files = 0; /* num_auto_files + cmd line files */
208char *filenames; /* pointer to space containing filenames */
209int remove_files = 0; /* if set, cleanup default is not to cleanup */
210int bytes_consumed = 0; /* total bytes consumed, all files */
211int bytes_to_consume = 0; /* non-zero if -B was specified, total bytes */
212int Maxerrs = 100; /* Max number errors before forced exit */
213int Errors = 0; /* number of encountered errors */
214int 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 */
217int max_size=5000;
218int min_size=1; /* also set in option parsing */
219int 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 */
221int min_lseek=0; /* also set in option parsing */
222int max_lseek=-1; /* -1 means size of file */
223#ifdef CRAY
224int Pattern=PATTERN_OFFSET; /* This pattern is 64 bit word based */
225#else
226int Pattern=PATTERN_ASCII;
227#endif
228int Seed=-1; /* random number seed, < 0 == uninitialized */
229int Nseeds=0; /* Number of seed specified by the user */
230int *Seeds; /* malloc'ed arrary of ints holding user spec seeds */
231
232int using_random=0; /* flag indicating randomization is being used */
233float delaysecs=0.0; /* delay between iterations (in seconds) */
234int delaytime; /* delay between iterations in clocks/uses */
235int 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
robbiew79768052005-12-23 17:14:18 +0000240off_t Woffset=0; /* offset before last write */
plars865695b2001-08-27 22:15:12 +0000241int Grow_incr=4096; /* sz of last write */
242int Mode=0; /* bitmask of write/trunc mode */
243 /* also knows if dealing with fifo */
244char *Buffer = NULL; /* buffer used by write and write check */
245int Alignment=0; /* if non word multiple, io will not be word aligned */
246int Opid=0; /* original pid */
247
248int Sync_with_others = 0; /* Flag indicating to stop other if we stop before DONE */
249int Iter_cnt = 0; /* contains current iteration count value */
250char TagName[40]; /* name of this growfiles (see Monster) */
251
252struct 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_modak4bb656a2009-02-26 12:02:09 +0000270 * open with O_TRUNC
plars865695b2001-08-27 22:15:12 +0000271 * 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_modak4bb656a2009-02-26 12:02:09 +0000283int Open_flags[] = {
plars865695b2001-08-27 22:15:12 +0000284#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 ***********************************************************************/
351int
352main(argc, argv)
353int argc;
354char **argv;
355{
356extern char *optarg; /* used by getopt */
357extern int optind;
plars865695b2001-08-27 22:15:12 +0000358
359int ind;
360int first_file_ind = 0;
361int num_auto_files = 0; /* files created by tool */
362int seq_auto_files = 0; /* auto files created by tool created by tool */
363char *auto_dir = DEF_DIR;
364char *auto_file = DEF_FILE;
365int grow_incr = 4096;
366int trunc_incr = 4096;
367int trunc_inter = 0; /* 0 means none, */
368int unlink_inter = 0; /* 0 means none, 1 means always unlink */
369int unlink_inter_ran = -1; /* -1 -use unlink_inter, otherwise randomly choose */
370 /* between unlink_inter and unlink_inter_ran */
371int file_check_inter = 0; /* 0 means never, 1 means always */
372int write_check_inter = 1; /* 0 means never, 1 means always */
373int iterations = 1; /* number of increments to be added */
374int no_file_check = 0; /* if set, no whole file checking will be done */
375int num;
376int fd; /* file descriptor */
377int stop = 0; /* loop stopper if set */
mridgedec5a462005-02-25 15:49:37 +0000378
379long unsigned curr_size = 0; /* BUG:14136 (keep track of file size) */
380const long unsigned ext2_limit = 2147483647; /* BUG:14136 (2GB ext2 filesize limit) */
381
robbiew79768052005-12-23 17:14:18 +0000382off_t tmp;
plars865695b2001-08-27 22:15:12 +0000383char chr;
384int ret;
385int pre_alloc_space = 0;
386#ifndef linux
mridgedec5a462005-02-25 15:49:37 +0000387long total_grow_value; /* used in pre-allocations */
plars865695b2001-08-27 22:15:12 +0000388#endif
389int backgrnd = 1; /* return control to user */
390struct stat statbuf;
391int time_iterval = -1;
392time_t start_time;
subrata_modakc6b731c2009-11-09 16:51:32 +0000393char reason[128]; /* reason for loop termination */
plars865695b2001-08-27 22:15:12 +0000394int num_procs=1;
395int forker_mode=0;
396int reexec=REXEC_INIT; /* reexec info */
397char *exec_path=NULL;
398
mridgef7298c72005-12-05 19:14:37 +0000399/*char *strrchr();*/
plars865695b2001-08-27 22:15:12 +0000400
401char *filename; /* name of file specified by user */
402char *cptr; /* temp char pointer */
403extern int Forker_npids; /* num of forked pid, defined in forker.c */
robbiewaead6ba2003-04-17 16:49:18 +0000404struct timeval tv1;
plars865695b2001-08-27 22:15:12 +0000405
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800406 if (argv[0][0] == '-')
plars865695b2001-08-27 22:15:12 +0000407 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_modak4bb656a2009-02-26 12:02:09 +0000421 while ((ind=getopt(argc, argv,
plars865695b2001-08-27 22:15:12 +0000422 "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_modak999fb4a2008-08-25 14:32:09 +0000427 tst_exit();
plars865695b2001-08-27 22:15:12 +0000428
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 Cooper8fb1cdb2010-11-28 22:56:35 -0800466 if (sscanf(optarg, "%i", &write_check_inter) != 1) {
plars865695b2001-08-27 22:15:12 +0000467 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800475 if (sscanf(optarg, "%i", &file_check_inter) != 1) {
plars865695b2001-08-27 22:15:12 +0000476 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800489 if (stat(auto_dir, &statbuf) == -1) {
490 if (mkdir(auto_dir, 0777) == -1) {
491 if (errno != EEXIST) {
plars865695b2001-08-27 22:15:12 +0000492 fprintf(stderr,
subrata_modak4bb656a2009-02-26 12:02:09 +0000493 "%s%s: Unable to make dir %s\n",
plars865695b2001-08-27 22:15:12 +0000494 Progname, TagName, auto_dir);
495 exit(1);
496 }
497 }
498 }
499 else {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800500 if (! (statbuf.st_mode & S_IFDIR)) {
plars865695b2001-08-27 22:15:12 +0000501 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800510 if (sscanf(optarg, "%i", &Debug) != 1) {
plars865695b2001-08-27 22:15:12 +0000511 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800519 if (sscanf(optarg, "%i", &Maxerrs) != 1) {
plars865695b2001-08-27 22:15:12 +0000520 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800540 if (ret == 2) {
541 if (chr == 'b' || chr == 'B')
plars865695b2001-08-27 22:15:12 +0000542 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800554 if (sscanf(optarg, "%f", &delaysecs) != 1 || delaysecs < 0) {
plars865695b2001-08-27 22:15:12 +0000555
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 Cooper8fb1cdb2010-11-28 22:56:35 -0800576 if ((io_type=lio_parse_io_arg1(optarg)) == -1) {
plars865695b2001-08-27 22:15:12 +0000577 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800582 if (io_type & LIO_RANDOM)
plars865695b2001-08-27 22:15:12 +0000583 using_random++;
584#else
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800585 if ((io_type=parse_io_arg(optarg)) == -1) {
plars865695b2001-08-27 22:15:12 +0000586 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800591 if (io_type == 99) /* hold-over until tlibio.h */
plars865695b2001-08-27 22:15:12 +0000592 using_random++;
593#endif
594 break;
595
596 case 'l':
597 lockfile++;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800598 if (lockfile > 2)
plars865695b2001-08-27 22:15:12 +0000599 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800647 if (strcmp(optarg, "random") == 0) {
plars865695b2001-08-27 22:15:12 +0000648 open_flags=RANDOM_OPEN;
649 using_random++;
650
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800651 } else if ((open_flags=parse_open_flags(optarg, NULL)) == -1) {
plars865695b2001-08-27 22:15:12 +0000652 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800671 if (strcmp(optarg, "PANIC") != 0) {
plars865695b2001-08-27 22:15:12 +0000672 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800724 if (sscanf(optarg, "%i-%i", &min_lseek, &max_lseek) != 2) {
plars865695b2001-08-27 22:15:12 +0000725 min_lseek=1; /* same as default in define */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800726 if (sscanf(optarg, "%i%c", &max_lseek, &chr) != 1) {
plars865695b2001-08-27 22:15:12 +0000727 fprintf(stderr, "%s%s: --R option arg invalid: [min-]max\n",
728 Progname, TagName);
729 exit(1);
730 }
731 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800732 if (max_lseek < LSK_EOFMINUSGROW) {
plars865695b2001-08-27 22:15:12 +0000733 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_modak4bb656a2009-02-26 12:02:09 +0000746 min_size=1;
plars865695b2001-08-27 22:15:12 +0000747 /* 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_modak4bb656a2009-02-26 12:02:09 +0000753 min_size=1;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800754 if (sscanf(optarg, "%i%c", &max_size, &chr) != 1) {
plars865695b2001-08-27 22:15:12 +0000755 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800764 if (max_size < 0) {
plars865695b2001-08-27 22:15:12 +0000765 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800772 if (min_size != max_size) {
plars865695b2001-08-27 22:15:12 +0000773 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_modakbdbaec52009-02-26 12:14:51 +0000790
plars865695b2001-08-27 22:15:12 +0000791 /* count the number of seeds */
792 cptr=optarg;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800793 for (Nseeds=1; *cptr ; Nseeds++) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800794 if ((filename=strchr(cptr, ',')) == NULL)
plars865695b2001-08-27 22:15:12 +0000795 break;
796 cptr=filename;
797 cptr++;
798 }
799 Seeds=(int *)malloc(Nseeds*sizeof(int));
800
801 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000802 * check that each seed is valid and put them in
plars865695b2001-08-27 22:15:12 +0000803 * the newly malloc'ed Seeds arrary.
804 */
805 filename=cptr=optarg;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800806 for (Nseeds=0; *cptr; Nseeds++) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800807 if ((filename=strchr(cptr, ',')) == NULL) {
808 if (sscanf(cptr, "%i", &Seeds[Nseeds]) < 1) {
plars865695b2001-08-27 22:15:12 +0000809 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800819 if (sscanf(cptr, "%i", &Seeds[Nseeds]) < 1) {
plars865695b2001-08-27 22:15:12 +0000820 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800840 if (ret == 2) {
841 if (chr == 'b' || chr == 'B')
plars865695b2001-08-27 22:15:12 +0000842 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_modak4bb656a2009-02-26 12:02:09 +0000869 /*
870 * formats:
871 * A-B - randomly pick interval between A and B
plars865695b2001-08-27 22:15:12 +0000872 * X - unlink file every X iteration
873 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000874 if (sscanf(optarg, "%i-%i", &unlink_inter,
plars865695b2001-08-27 22:15:12 +0000875 &unlink_inter_ran) == 2 ) {
876
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800877 if (unlink_inter < 0 || unlink_inter_ran < 0) {
plars865695b2001-08-27 22:15:12 +0000878 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800884 if (unlink_inter > unlink_inter_ran) {
plars865695b2001-08-27 22:15:12 +0000885 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800902 if (reexec != REXEC_DONE)
plars865695b2001-08-27 22:15:12 +0000903 reexec=REXEC_DOIT;
904 break;
905
906 case 'w':
907 Mode |= MODE_GROW_BY_LSEEK;
908 break;
909
910 case 'W':
robbiew92b688b2004-03-01 22:36:38 +0000911 TCID = optarg;
plars865695b2001-08-27 22:15:12 +0000912 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 Cooper8fb1cdb2010-11-28 22:56:35 -0800926 if (Debug == 1) {
plars865695b2001-08-27 22:15:12 +0000927 cptr = getenv("TOUTPUT");
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800928 if ((cptr != NULL) && (strcmp( cptr, "NOPASS" ) == 0)) {
plars865695b2001-08-27 22:15:12 +0000929 Debug = 0;
930 }
931 }
932
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800933 if (Pattern == PATTERN_RANDOM) {
plars865695b2001-08-27 22:15:12 +0000934 no_file_check=1;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800935 if (write_check_inter || file_check_inter)
plars865695b2001-08-27 22:15:12 +0000936 printf("%s%s: %d Using random pattern - no data checking will be performed!\n",
937 Progname, TagName, getpid());
938 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800939 else if (max_lseek == LSK_EOFPLUSGROW || Mode & MODE_GROW_BY_LSEEK) {
plars865695b2001-08-27 22:15:12 +0000940 no_file_check=1;
941
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800942 if (file_check_inter)
plars865695b2001-08-27 22:15:12 +0000943 printf("%s%s: %d Using random lseek beyond EOF or lseek grow,\n\
944no whole file checking will be performed!\n", Progname, TagName, getpid());
945
946 }
947
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800948 if (Mode & MODE_RAND_SIZE)
plars865695b2001-08-27 22:15:12 +0000949 grow_incr=max_size;
950
951 set_sig();
952
953 Opid=getpid();
954 Pid=Opid;
955
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800956 if (backgrnd) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800957 if (Debug > 1)
plars865695b2001-08-27 22:15:12 +0000958 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 Cooperdf3eb162010-11-28 22:44:32 -0800964 if (Sync_with_others)
plars865695b2001-08-27 22:15:12 +0000965 setpgrp();
966#endif
967
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800968 if (Debug > 3) {
plars865695b2001-08-27 22:15:12 +0000969#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 Cooper8fb1cdb2010-11-28 22:56:35 -0800981 if (Debug > 2) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000982
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800983 if (Mode & MODE_GROW_BY_LSEEK)
plars865695b2001-08-27 22:15:12 +0000984 printf("%s: %d DEBUG lseeking past end of file, writting a \"w\"\n",
985 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800986 else if (Pattern == PATTERN_OFFSET)
plars865695b2001-08-27 22:15:12 +0000987 printf("%s: %d DEBUG3 %d<byteoffset>%d per word pattern multi-writers.\n",
988 Progname, Pid, STATIC_NUM, STATIC_NUM);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800989 else if (Pattern == PATTERN_PID)
plars865695b2001-08-27 22:15:12 +0000990 printf("%s: %d DEBUG3 <pid><byteoffset><pid> per word pattern - 1 writer\n",
991 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800992 else if (Pattern == PATTERN_ASCII)
plars865695b2001-08-27 22:15:12 +0000993 printf("%s: %d DEBUG3 ascii pattern (vi'able)- allows multiple writers\n",
994 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800995 else if (Pattern == PATTERN_ALT)
plars865695b2001-08-27 22:15:12 +0000996 printf("%s: %d DEBUG3 alt bit pattern - allows multiple writers\n",
997 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800998 else if (Pattern == PATTERN_CHKER)
plars865695b2001-08-27 22:15:12 +0000999 printf("%s: %d DEBUG3 checkerboard pattern - allows multiple writers\n",
1000 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001001 else if (Pattern == PATTERN_CNTING)
plars865695b2001-08-27 22:15:12 +00001002 printf("%s: %d DEBUG3 counting pattern - allows multiple writers\n",
1003 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001004 else if (Pattern == PATTERN_RANDOM)
plars865695b2001-08-27 22:15:12 +00001005 printf("%s: %d DEBUG3 random integer pattern - no write/file checking\n",
1006 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001007 else if (Pattern == PATTERN_ONES)
plars865695b2001-08-27 22:15:12 +00001008 printf("%s: %d DEBUG3 all ones pattern - allows multiple writers\n",
1009 Progname, Pid);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001010 else if (Pattern == PATTERN_ZEROS)
plars865695b2001-08-27 22:15:12 +00001011 printf("%s: %d DEBUG3 all zeros pattern - allows multiple writers\n",
1012 Progname, Pid);
subrata_modakbdbaec52009-02-26 12:14:51 +00001013
plars865695b2001-08-27 22:15:12 +00001014 else
1015 printf("%s: %d DEBUG3 unknown pattern\n",
1016 Progname, Pid);
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001017 if (bytes_to_consume)
plars865695b2001-08-27 22:15:12 +00001018 printf("%s: %d DEBUG3 bytes_to_consume = %d\n",
1019 Progname, Pid, bytes_to_consume);
subrata_modak4bb656a2009-02-26 12:02:09 +00001020 printf("%s: %d DEBUG3 Maxerrs = %d, pre_alloc_space = %d, filelocking = %d\n",
plars865695b2001-08-27 22:15:12 +00001021 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001028 if (open_flags == RANDOM_OPEN)
plars865695b2001-08-27 22:15:12 +00001029 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001035 if (Mode & MODE_RAND_SIZE) {
plars865695b2001-08-27 22:15:12 +00001036 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_modak4bb656a2009-02-26 12:02:09 +00001040 printf("%s: %d DEBUG3 grow_incr = %d\n",
plars865695b2001-08-27 22:15:12 +00001041 Progname, Pid, grow_incr);
1042 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001043 if (Mode & MODE_RAND_LSEEK) {
1044 if (max_lseek == LSK_EOF)
plars865695b2001-08-27 22:15:12 +00001045 printf("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile>\n",
1046 Progname, Pid, min_lseek);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001047 else if (max_lseek == LSK_EOFPLUSGROW)
plars865695b2001-08-27 22:15:12 +00001048 printf("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile+iosize>\n",
1049 Progname, Pid, min_lseek);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001050 else if (max_lseek == LSK_EOFMINUSGROW)
plars865695b2001-08-27 22:15:12 +00001051 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 Cooperdf3eb162010-11-28 22:44:32 -08001064 if (no_file_check)
plars865695b2001-08-27 22:15:12 +00001065 printf("%s: %d DEBUG3 no whole file checking will be done\n",
1066 Progname, Pid);
subrata_modak4bb656a2009-02-26 12:02:09 +00001067
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001068 if (unlink_inter_ran == -1) {
subrata_modak4bb656a2009-02-26 12:02:09 +00001069 printf("%s: %d DEBUG3 unlink_inter = %d\n",
plars865695b2001-08-27 22:15:12 +00001070 Progname, Pid, unlink_inter);
1071 } else {
subrata_modak4bb656a2009-02-26 12:02:09 +00001072 printf("%s: %d DEBUG3 unlink_inter = %d, unlink_inter_ran = %d\n",
plars865695b2001-08-27 22:15:12 +00001073 Progname, Pid, unlink_inter, unlink_inter_ran);
subrata_modak4bb656a2009-02-26 12:02:09 +00001074 }
plars865695b2001-08-27 22:15:12 +00001075
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001076 if (Debug > 8) {
plars865695b2001-08-27 22:15:12 +00001077 num=sizeof(Open_flags)/sizeof(int);
1078 printf("%s: %d DEBUG9 random open flags values:\n", Progname, Pid);
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001079 for (ind=0; ind<num; ind++) {
plars865695b2001-08-27 22:15:12 +00001080 printf("\t%#o\n", Open_flags[ind]);
1081 }
1082 }
1083 } /* end of DEBUG > 2 */
1084
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001085 if (Debug > 1 && num_procs > 1) {
plars865695b2001-08-27 22:15:12 +00001086 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001101 if (Nseeds == 1) {
plars865695b2001-08-27 22:15:12 +00001102 /*
subrata_modak4bb656a2009-02-26 12:02:09 +00001103 * If only one seed specified, all processes will get that seed.
plars865695b2001-08-27 22:15:12 +00001104 */
1105 Seed=Seeds[0];
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001106 } else if (Nseeds > 1) {
plars865695b2001-08-27 22:15:12 +00001107 /*
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 Cooper8fb1cdb2010-11-28 22:56:35 -08001112 if (Opid == Pid) {
plars865695b2001-08-27 22:15:12 +00001113 Seed=Seeds[0];
1114 } else {
1115 /*
1116 * If user didn't specify enough seeds, use default method.
1117 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001118 if (Forker_npids >= Nseeds)
subrata_modak90d1e3c2008-08-21 13:07:02 +00001119 {
1120 struct timeval ts;
1121 gettimeofday(&ts,NULL);
1122 Seed=ts.tv_sec + Pid; /* default random seed */
1123 }
plars865695b2001-08-27 22:15:12 +00001124 else {
1125 Seed=Seeds[Forker_npids];
1126 }
1127 }
1128 } else {
subrata_modak4bb656a2009-02-26 12:02:09 +00001129 /*
plars865695b2001-08-27 22:15:12 +00001130 * Generate a random seed based on time and pid.
1131 * It has a good chance of being unique for each pid.
1132 */
subrata_modak90d1e3c2008-08-21 13:07:02 +00001133 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_modakbdbaec52009-02-26 12:14:51 +00001137
plars865695b2001-08-27 22:15:12 +00001138 }
1139
1140 random_range_seed(Seed);
1141
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001142 if (using_random && Debug > 0)
plars865695b2001-08-27 22:15:12 +00001143 printf("%s%s: %d DEBUG1 Using random seed of %d\n",
1144 Progname, TagName, Pid, Seed);
1145
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001146 if (unlink_inter_ran > 0) {
plars865695b2001-08-27 22:15:12 +00001147 /*
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 Cooper8fb1cdb2010-11-28 22:56:35 -08001155 if (Debug > 2)
robbiew79768052005-12-23 17:14:18 +00001156 printf("%s: %d DEBUG3 Unlink interval is %d (random %ld - %d)\n",
plars865695b2001-08-27 22:15:12 +00001157 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001165 if (reexec == REXEC_DOIT && Opid != Pid) {
1166 if (exec_path == NULL) {
plars865695b2001-08-27 22:15:12 +00001167 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_modak4bb656a2009-02-26 12:02:09 +00001172
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001173 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00001174 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001184 if (optind == argc) {
plars865695b2001-08-27 22:15:12 +00001185 /*
1186 * no cmd line files, therfore, set
1187 * the default number of auto created files
1188 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001189 if (! num_auto_files && ! seq_auto_files)
plars865695b2001-08-27 22:15:12 +00001190 num_auto_files=1;
1191 }
1192 else {
1193 first_file_ind=optind;
1194 num_files += argc-optind;
1195 }
1196
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001197 if (num_auto_files) {
plars865695b2001-08-27 22:15:12 +00001198 num_files += num_auto_files;
1199 }
1200
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001201 if (seq_auto_files) {
plars865695b2001-08-27 22:15:12 +00001202 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 Cooperdf3eb162010-11-28 22:44:32 -08001220 if (first_file_ind) {
1221 for (ind=first_file_ind; ind<argc; ind++, num++) {
plars865695b2001-08-27 22:15:12 +00001222 strcpy((char *)filenames+(num*PATH_MAX), argv[ind]);
1223 }
1224 }
1225
1226 /*
1227 * construct auto filename and insert them into filenames space
1228 */
subrata_modakbdbaec52009-02-26 12:14:51 +00001229
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001230 for (ind=0;ind<num_auto_files; ind++, num++) {
robbiewaead6ba2003-04-17 16:49:18 +00001231 gettimeofday(&tv1,NULL);
robbiew79ce8da2003-04-17 16:51:24 +00001232 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 );
plars865695b2001-08-27 22:15:12 +00001234 }
1235
1236 /*
1237 * construct auto seq filenames
1238 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001239 for (ind=1; ind<=seq_auto_files; ind++, num++) {
plars865695b2001-08-27 22:15:12 +00001240 sprintf((char *)filenames+(num*PATH_MAX), "%s/%s%d",
1241 auto_dir, auto_file, ind);
1242 }
1243
1244/**** end filename stuff ****/
1245
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001246 if (time_iterval > 0)
subrata_modakbdbaec52009-02-26 12:14:51 +00001247 {
subrata_modak90d1e3c2008-08-21 13:07:02 +00001248 struct timeval ts;
1249 gettimeofday(&ts,NULL);
1250 start_time=ts.tv_sec;
1251 //start_time=time(0);
1252 }
plars865695b2001-08-27 22:15:12 +00001253
1254 /*
1255 * get space for I/O buffer
1256 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001257 if (grow_incr) {
plars865695b2001-08-27 22:15:12 +00001258 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 Cooperdf3eb162010-11-28 22:44:32 -08001263 if (Alignment)
plars865695b2001-08-27 22:15:12 +00001264 Buffer = Buffer + Alignment;
1265
1266 }
1267
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001268 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001269 printf("%s: %d DEBUG3 num_files = %d\n",
1270 Progname, Pid, num_files);
1271 }
1272
1273#ifndef linux
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001274 if (pre_alloc_space) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001275 if (iterations == 0) {
plars865695b2001-08-27 22:15:12 +00001276 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001280 if (Mode & MODE_RAND_SIZE) {
plars865695b2001-08-27 22:15:12 +00001281 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_modak4bb656a2009-02-26 12:02:09 +00001290 * attempt to limit
plars865695b2001-08-27 22:15:12 +00001291 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001292 if (bytes_to_consume && bytes_to_consume < total_grow_value) {
plars865695b2001-08-27 22:15:12 +00001293 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 Cooperdf3eb162010-11-28 22:44:32 -08001305 if (delaysecs) {
plars865695b2001-08-27 22:15:12 +00001306#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_modak4bb656a2009-02-26 12:02:09 +00001318 * read to check the write, check the whole file,
1319 * truncated, and closed.
plars865695b2001-08-27 22:15:12 +00001320 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001321 for (Iter_cnt=1; ! stop ; Iter_cnt++) {
subrata_modak90d1e3c2008-08-21 13:07:02 +00001322 struct timeval ts;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001323 if (iterations && (Iter_cnt >= iterations+1)) {
plars865695b2001-08-27 22:15:12 +00001324 strcpy(reason, "Hit iteration value");
1325 stop=1;
1326 continue;
1327 }
subrata_modak90d1e3c2008-08-21 13:07:02 +00001328 gettimeofday(&ts,NULL);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001329 if ((time_iterval > 0) && (start_time + time_iterval < ts.tv_sec)) {
plars865695b2001-08-27 22:15:12 +00001330
plars865695b2001-08-27 22:15:12 +00001331 sprintf(reason, "Hit time value of %d", time_iterval);
1332 stop=1;
1333 continue;
1334 }
1335
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001336 if (bytes_to_consume && bytes_consumed >= bytes_to_consume) {
plars865695b2001-08-27 22:15:12 +00001337 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_modak4bb656a2009-02-26 12:02:09 +00001345 * read to check the write, check the whole file,
1346 * truncated, and closed.
plars865695b2001-08-27 22:15:12 +00001347 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001348 for (ind=0; ind<num_files; ind++) {
plars865695b2001-08-27 22:15:12 +00001349
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 Cooper8fb1cdb2010-11-28 22:56:35 -08001357 if (open_flags == RANDOM_OPEN) {
plars865695b2001-08-27 22:15:12 +00001358 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001366 if (Debug > 3) {
plars865695b2001-08-27 22:15:12 +00001367 printf("%s: %d DEBUG3 %s/%d: %d Open filename = %s, open flags = %#o %s\n",
subrata_modak4bb656a2009-02-26 12:02:09 +00001368 Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename, ret,
plars865695b2001-08-27 22:15:12 +00001369 openflags2symbols(ret, ",", NULL));
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001370 } else if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001371 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001378 if ((fd=open(filename, ret, 0777)) == -1) {
plars865695b2001-08-27 22:15:12 +00001379 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 Cooperdf3eb162010-11-28 22:44:32 -08001394 if (pre_alloc_space) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001395 if (pre_alloc(fd, total_grow_value) != 0) {
plars865695b2001-08-27 22:15:12 +00001396 cleanup();
1397 exit(2);
1398 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001399 if (Debug > 1) {
mridgedec5a462005-02-25 15:49:37 +00001400 printf("%s: %d DEBUG2 %s/%d: pre_allocated %ld for file %s\n",
plars865695b2001-08-27 22:15:12 +00001401 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_modak4bb656a2009-02-26 12:02:09 +00001412 * growfile() will set the Grow_incr variable and
plars865695b2001-08-27 22:15:12 +00001413 * possiblly update the Mode variable indicating
1414 * if we are dealing with a FIFO file.
1415 */
1416
mridgedec5a462005-02-25 15:49:37 +00001417 /* BUG:14136 (don't go past ext2's filesize limit) */
subrata_modak243eecf2007-11-09 12:12:44 +00001418 curr_size = file_size(fd);
mridgedec5a462005-02-25 15:49:37 +00001419 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001427 if (growfile(fd, filename, grow_incr, Buffer, &curr_size) != 0) { /* BUG:14136 */
plars865695b2001-08-27 22:15:12 +00001428 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_modak4bb656a2009-02-26 12:02:09 +00001451 * shrink file by desired amount if it is time
plars865695b2001-08-27 22:15:12 +00001452 */
1453
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001454 if (shrinkfile(fd, filename, trunc_incr, trunc_inter, Mode) != 0) {
plars865695b2001-08-27 22:15:12 +00001455 handle_error();
1456 }
1457
1458 lkfile(fd, LOCK_UN, LKLVL1); /* release lock */
1459
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001460 if (Debug > 4)
subrata_modak4bb656a2009-02-26 12:02:09 +00001461 printf("%s: %d DEBUG5 %s/%d: %d Closing file %s fd:%d \n",
plars865695b2001-08-27 22:15:12 +00001462 Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename, fd);
1463 close(fd);
1464
1465 /*
1466 * Unlink the file if that is desired
1467 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001468 if (unlink_inter && (Iter_cnt % unlink_inter == 0)) {
subrata_modakbdbaec52009-02-26 12:14:51 +00001469
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001470 if (Debug > 4)
subrata_modak4bb656a2009-02-26 12:02:09 +00001471 printf("%s: %d DEBUG5 %s/%d: %d Unlinking file %s\n",
plars865695b2001-08-27 22:15:12 +00001472 Progname, Pid, __FILE__, __LINE__, Iter_cnt, filename);
1473
1474 unlink(filename);
1475 }
1476
1477 /*
1478 * delay while staying active for "delaysecs" seconds.
1479 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001480 if (delaytime) {
subrata_modakbdbaec52009-02-26 12:14:51 +00001481
plars865695b2001-08-27 22:15:12 +00001482 int ct, end;
1483#ifdef CRAY
1484 ct=_rtc();
1485 end=ct+delaytime;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001486 while (ct < end) {
plars865695b2001-08-27 22:15:12 +00001487 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001494 while (ct < end) {
plars865695b2001-08-27 22:15:12 +00001495
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 Cooper8fb1cdb2010-11-28 22:56:35 -08001508 if (Iter_cnt == 0) {
plars865695b2001-08-27 22:15:12 +00001509 pre_alloc_space=0;
1510 }
1511#endif
1512
1513
1514 } /* end iteration for loop */
1515
1516
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001517 if (Debug) {
plars865695b2001-08-27 22:15:12 +00001518 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 Cooperdf3eb162010-11-28 22:44:32 -08001526 if (Errors) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001527 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001528 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001534 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001535 printf("%s%s: %d DEBUG3 %s/%d: no errors, exiting with value of 0\n", Progname, TagName, Pid, __FILE__, __LINE__);
mridge24227452004-05-17 22:06:02 +00001536 }
1537
plars865695b2001-08-27 22:15:12 +00001538 exit(0);
mridgedec5a462005-02-25 15:49:37 +00001539 return 0; /* to keep compiler happy */
plars865695b2001-08-27 22:15:12 +00001540}
1541
1542/***********************************************************************
1543 *
1544 ***********************************************************************/
1545int
1546set_sig()
1547{
1548 int sig;
1549
subrata_modakbdbaec52009-02-26 12:14:51 +00001550
plars865695b2001-08-27 22:15:12 +00001551 /*
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 ***********************************************************************/
1591void
1592sig_handler(sig)
1593int sig;
1594{
1595 int exit_stat = 2;
1596
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001597 if (sig == SIGUSR2) {
plars865695b2001-08-27 22:15:12 +00001598 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_modak4bb656a2009-02-26 12:02:09 +00001603
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001604 } else if (sig == SIGINT) {
plars865695b2001-08-27 22:15:12 +00001605 /* The user has told us to cleanup, don't pretend it's an error. */
1606 exit_stat=0;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001607 if (Debug != 0) {
plars865695b2001-08-27 22:15:12 +00001608 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001618 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001619 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_modak4bb656a2009-02-26 12:02:09 +00001628 *
plars865695b2001-08-27 22:15:12 +00001629 ***********************************************************************/
1630static void
1631notify_others()
1632{
1633 static int send_signals = 0;
1634 int ind;
1635 extern int Forker_pids[];
1636 extern int Forker_npids;
1637
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001638 if (Sync_with_others && send_signals == 0) {
plars865695b2001-08-27 22:15:12 +00001639
1640#if CRAY
1641 send_signals=1; /* only send signals once */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001642 if (Debug > 1)
plars865695b2001-08-27 22:15:12 +00001643 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001650 if (Forker_pids[ind] != Pid)
1651 if (Debug > 1)
plars865695b2001-08-27 22:15:12 +00001652 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 ***********************************************************************/
1666int
1667handle_error()
1668{
1669 Errors++;
1670
1671#ifdef CRAY
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001672 if (Errors & Upanic_on_error) {
plars865695b2001-08-27 22:15:12 +00001673 upanic(PA_PANIC);
1674 }
1675#endif
1676
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001677 if (Maxerrs && Errors >= Maxerrs) {
subrata_modak4bb656a2009-02-26 12:02:09 +00001678 printf("%s%s: %d %s/%d: %d Hit max errors value of %d\n",
plars865695b2001-08-27 22:15:12 +00001679 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, Maxerrs);
1680 notify_others();
1681 cleanup();
1682
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001683 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001684 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 ***********************************************************************/
1698int
1699cleanup()
1700{
1701 int ind;
1702
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001703 if (remove_files) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001704 if (Debug > 2)
subrata_modak4bb656a2009-02-26 12:02:09 +00001705 printf("%s: %d DEBUG3 Removing all %d files\n",
plars865695b2001-08-27 22:15:12 +00001706 Progname, Pid, num_files);
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001707 for (ind=0; ind<=num_files; ind++) {
plars865695b2001-08-27 22:15:12 +00001708 unlink(filenames+(ind*PATH_MAX));
1709 }
1710 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001711 if (using_random && Debug > 1)
plars865695b2001-08-27 22:15:12 +00001712 printf("%s%s: %d DEBUG2 Used random seed: %d\n",
1713 Progname, TagName, Pid, Seed);
1714 return 0;
1715}
1716
1717/***********************************************************************
1718 *
1719 ***********************************************************************/
1720void
1721usage()
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 ***********************************************************************/
1740void
1741help()
1742{
1743 usage();
1744
1745fprintf(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 ***********************************************************************/
1803void
1804prt_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_modakbdbaec52009-02-26 12:14:51 +00001827
plars865695b2001-08-27 22:15:12 +00001828 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_modak4bb656a2009-02-26 12:02:09 +00001840 fprintf(stream,
plars865695b2001-08-27 22:15:12 +00001841 "# 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_modak4bb656a2009-02-26 12:02:09 +00001843%s -i0 -r 1-50000 -R 0--2 -I r -C1 -l -n5 -u -U 100-200 gf_rana gf_ranb\n\n",
plars865695b2001-08-27 22:15:12 +00001844 Progname);
1845
subrata_modak4bb656a2009-02-26 12:02:09 +00001846 fprintf(stream,
plars865695b2001-08-27 22:15:12 +00001847 "# 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_modak4bb656a2009-02-26 12:02:09 +00001849%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",
plars865695b2001-08-27 22:15:12 +00001850 Progname);
1851
1852
1853 return;
1854}
1855
1856/***********************************************************************
1857 *
1858 * The file descriptor current offset is assumed to be the end of the
subrata_modak4bb656a2009-02-26 12:02:09 +00001859 * file.
plars865695b2001-08-27 22:15:12 +00001860 * 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 ***********************************************************************/
1863int
mridgedec5a462005-02-25 15:49:37 +00001864growfile(fd, file, grow_incr, buf, curr_size_ptr) /* BUG:14136 */
plars865695b2001-08-27 22:15:12 +00001865int fd;
1866char *file;
1867int grow_incr;
1868char *buf;
mridgedec5a462005-02-25 15:49:37 +00001869long *curr_size_ptr; /* BUG:14136 */
plars865695b2001-08-27 22:15:12 +00001870{
robbiewdd198422004-11-22 17:13:21 +00001871 off_t noffset;
plars865695b2001-08-27 22:15:12 +00001872 int ret;
1873 int cur_offset;
1874 char *errmsg;
robbiewdd198422004-11-22 17:13:21 +00001875 off_t fsize; /* current size of file */
plars865695b2001-08-27 22:15:12 +00001876 int size_grew; /* size the file grew */
1877 struct stat stbuf;
robbiewdd198422004-11-22 17:13:21 +00001878 off_t tmp = 0;
plars865695b2001-08-27 22:15:12 +00001879
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 Cooper8fb1cdb2010-11-28 22:56:35 -08001886 if (fstat(fd, &stbuf) != -1) {
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001887 if (S_ISFIFO(stbuf.st_mode)) {
plars865695b2001-08-27 22:15:12 +00001888 Fileinfo.mode |= MODE_FIFO;
1889 Mode |= MODE_FIFO;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001890 if (Debug > 3)
plars865695b2001-08-27 22:15:12 +00001891 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001904 if (grow_incr <= 0) { /* don't attempt i/o if grow_incr <= 0 */
plars865695b2001-08-27 22:15:12 +00001905
1906 Grow_incr=grow_incr;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001907 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00001908 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001913 if (Mode & MODE_RAND_SIZE) {
plars865695b2001-08-27 22:15:12 +00001914 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001924 if (! (Mode & MODE_FIFO)) {
1925 if ((cur_offset=lseek(fd,0,SEEK_CUR)) == -1) {
plars865695b2001-08-27 22:15:12 +00001926 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001932 if (Mode & MODE_GROW_BY_LSEEK) {
plars865695b2001-08-27 22:15:12 +00001933 Woffset=fsize;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001934 if (Debug > 2) {
robbiew79768052005-12-23 17:14:18 +00001935 printf("%s: %d DEBUG3 %s/%d: Current size of file is %ld\n", Progname,
plars865695b2001-08-27 22:15:12 +00001936 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001941 if ((noffset=lseek(fd, grow_incr-1, SEEK_END)) == -1) {
plars865695b2001-08-27 22:15:12 +00001942 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_modakbdbaec52009-02-26 12:14:51 +00001948
plars865695b2001-08-27 22:15:12 +00001949#if NEWIO
1950 ret=lio_write_buffer(fd, io_type, "w", 1, SIGUSR1, &errmsg,0);
1951#else
subrata_modak4bb656a2009-02-26 12:02:09 +00001952 ret=write_buffer(fd, io_type, "w", 1, 0, &errmsg);
plars865695b2001-08-27 22:15:12 +00001953#endif
1954
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001955 if (ret != 1) {
subrata_modak4bb656a2009-02-26 12:02:09 +00001956 fprintf(stderr, "%s%s: %d %s/%d: %d %s\n",
plars865695b2001-08-27 22:15:12 +00001957 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001958 if (ret == -ENOSPC) {
plars865695b2001-08-27 22:15:12 +00001959 cleanup();
1960 exit(2);
1961 }
1962 }
1963/***
1964 write(fd, "w", 1);
1965****/
1966
1967 lkfile(fd, LOCK_UN, LKLVL0);
1968
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001969 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00001970 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001975 if (Fileinfo.openflags & O_APPEND) {
plars865695b2001-08-27 22:15:12 +00001976 /*
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 Cooper8fb1cdb2010-11-28 22:56:35 -08001981 if (Debug > 4)
plars865695b2001-08-27 22:15:12 +00001982 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 Cooper8fb1cdb2010-11-28 22:56:35 -08001990 if (fstat(fd, &stbuf) != -1) {
plars865695b2001-08-27 22:15:12 +00001991 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_modakbdbaec52009-02-26 12:14:51 +00001995
plars865695b2001-08-27 22:15:12 +00001996 lkfile(fd, LOCK_UN, LKLVL0); /* release lock */
1997 return -1;
1998 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001999 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00002000 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002004 } else if (Mode & MODE_RAND_LSEEK) {
2005 if (max_lseek == LSK_EOF) { /* within file size */
plars865695b2001-08-27 22:15:12 +00002006 noffset=random_range(min_lseek, fsize, 1, NULL);
subrata_modakbdbaec52009-02-26 12:14:51 +00002007 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002008 else if (max_lseek == LSK_EOFPLUSGROW) {
plars865695b2001-08-27 22:15:12 +00002009 /* max to beyond file size */
2010 noffset=random_range(min_lseek, fsize+grow_incr, 1, NULL);
2011 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002012 else if (max_lseek == LSK_EOFMINUSGROW) {
subrata_modak4bb656a2009-02-26 12:02:09 +00002013 /*
plars865695b2001-08-27 22:15:12 +00002014 * 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002023 if (fsize <= min_lseek + grow_incr)
plars865695b2001-08-27 22:15:12 +00002024 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002032 if ((Woffset=lseek(fd, noffset, SEEK_SET)) == -1) {
robbiew79768052005-12-23 17:14:18 +00002033 fprintf(stderr, "%s%s: %d %s/%d: lseek(%d, %ld, SEEK_SET) l2 failed: %s\n",
plars865695b2001-08-27 22:15:12 +00002034 Progname, TagName, Pid, __FILE__, __LINE__, fd, noffset, strerror(errno));
2035 return -1;
2036 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002037 else if (Debug > 2)
robbiew79768052005-12-23 17:14:18 +00002038 printf("%s: %d DEBUG3 %s/%d: lseeked to random offset %ld (fsz:%d)\n",
plars865695b2001-08-27 22:15:12 +00002039 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002047 else if (! (Mode & MODE_FIFO)) {
2048 if ((Woffset=lseek(fd, 0, SEEK_END)) == -1) {
plars865695b2001-08-27 22:15:12 +00002049 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002053 else if (Debug > 2)
robbiew79768052005-12-23 17:14:18 +00002054 printf("%s: %d DEBUG3 %s/%d: lseeked to end of file, offset %ld\n",
plars865695b2001-08-27 22:15:12 +00002055 Progname, Pid, __FILE__, __LINE__, Woffset);
2056 }
2057
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002058 if (Pattern == PATTERN_OFFSET)
plars865695b2001-08-27 22:15:12 +00002059 datapidgen(STATIC_NUM, buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002060 else if (Pattern == PATTERN_PID)
plars865695b2001-08-27 22:15:12 +00002061 datapidgen(Pid, buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002062 else if (Pattern == PATTERN_ASCII)
plars865695b2001-08-27 22:15:12 +00002063 dataasciigen(NULL, buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002064 else if (Pattern == PATTERN_RANDOM)
plars865695b2001-08-27 22:15:12 +00002065 databingen('r', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002066 else if (Pattern == PATTERN_ALT)
plars865695b2001-08-27 22:15:12 +00002067 databingen('a', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002068 else if (Pattern == PATTERN_CHKER)
plars865695b2001-08-27 22:15:12 +00002069 databingen('c', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002070 else if (Pattern == PATTERN_CNTING)
plars865695b2001-08-27 22:15:12 +00002071 databingen('C', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002072 else if (Pattern == PATTERN_ZEROS)
plars865695b2001-08-27 22:15:12 +00002073 databingen('z', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002074 else if (Pattern == PATTERN_ONES)
plars865695b2001-08-27 22:15:12 +00002075 databingen('o', buf, grow_incr, Woffset);
2076 else
2077 dataasciigen(NULL, buf, grow_incr, Woffset);
2078
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002079 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00002080 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_modakbdbaec52009-02-26 12:14:51 +00002090 lkfile(fd, LOCK_UN, LKLVL0);
plars865695b2001-08-27 22:15:12 +00002091
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002092 if (ret != grow_incr) {
plars865695b2001-08-27 22:15:12 +00002093 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002106 if (Mode & MODE_FIFO) {
plars865695b2001-08-27 22:15:12 +00002107 /* 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002113 if ((tmp=lseek(fd,0,SEEK_CUR)) < 0) { /* get offset after the write */
plars865695b2001-08-27 22:15:12 +00002114 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_modak142cfbb2009-04-03 06:58:18 +00002119#if defined(sgi) || defined(__linux__)
plars865695b2001-08-27 22:15:12 +00002120 /* 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002126 if ((io_type & LIO_IO_ASYNC_TYPES) || (io_type & LIO_RANDOM)) {
2127 if (tmp != Woffset + grow_incr) {
2128 if (Debug > 5) {
subrata_modakf17ac842009-09-07 09:06:31 +00002129 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);
plars865695b2001-08-27 22:15:12 +00002131 }
2132 tmp = Woffset + grow_incr;
2133 }
2134 }
subrata_modak142cfbb2009-04-03 06:58:18 +00002135#endif /* sgi __linux__ */
plars865695b2001-08-27 22:15:12 +00002136#endif
2137 }
mridgedec5a462005-02-25 15:49:37 +00002138 *curr_size_ptr=tmp; /* BUG:14136 */
plars865695b2001-08-27 22:15:12 +00002139
subrata_modakbdbaec52009-02-26 12:14:51 +00002140 lkfile(fd, LOCK_UN, LKLVL0);
plars865695b2001-08-27 22:15:12 +00002141
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002142 if (ret != grow_incr) {
subrata_modak4bb656a2009-02-26 12:02:09 +00002143 fprintf(stderr, "%s%s: %d %s/%d: %d %s\n",
plars865695b2001-08-27 22:15:12 +00002144 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002145 if (ret == -ENOSPC) {
plars865695b2001-08-27 22:15:12 +00002146 cleanup();
2147 exit(2);
2148 }
2149 return -1;
2150 }
2151
2152 /*
2153 * Check for a condition where the file was truncated just before
subrata_modak4bb656a2009-02-26 12:02:09 +00002154 * the write.
plars865695b2001-08-27 22:15:12 +00002155 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002156 if (tmp != Woffset + grow_incr) {
plars865695b2001-08-27 22:15:12 +00002157 /*
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 Cooper8fb1cdb2010-11-28 22:56:35 -08002166 if (Debug > 0 && lockfile) {
subrata_modakbdbaec52009-02-26 12:14:51 +00002167 printf("%s%s: %d DEBUG1 %s/%d: offset after write(%ld) not as exp(%ld+%d=%ld)\n",
plars865695b2001-08-27 22:15:12 +00002168 Progname, TagName, Pid, __FILE__, __LINE__, tmp, Woffset, grow_incr, Woffset+grow_incr);
robbiew79768052005-12-23 17:14:18 +00002169 printf("%s%s: %d DEBUG1 %s/%d: %d Assuming file changed by another process, resetting offset:%ld (expect pattern mismatch)\n",
plars865695b2001-08-27 22:15:12 +00002170 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, tmp-grow_incr);
2171 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002172 if (Debug > 4) {
robbiew79768052005-12-23 17:14:18 +00002173 printf("%s: %d DEBUG5 %s/%d: about to chop Woffset. tmp=%ld, grow_incr=%d, Woffset was %ld\n",
plars865695b2001-08-27 22:15:12 +00002174 Progname, Pid, __FILE__, __LINE__, tmp, grow_incr, Woffset);
2175 }
2176 Woffset=tmp-grow_incr;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002177 if (Woffset < 0)
plars865695b2001-08-27 22:15:12 +00002178 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 Cooperdf3eb162010-11-28 22:44:32 -08002190 if (Debug > 1) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002191 if (Mode & MODE_FIFO) {
plars865695b2001-08-27 22:15:12 +00002192 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002196 else if (size_grew > 0)
robbiew79768052005-12-23 17:14:18 +00002197 printf("%s: %d DEBUG2 %s/%d: %d wrote %d bytes(off:%ld), grew file by %d bytes\n",
plars865695b2001-08-27 22:15:12 +00002198 Progname, Pid, __FILE__, __LINE__, Iter_cnt, Grow_incr, Woffset, size_grew);
2199 else
robbiew79768052005-12-23 17:14:18 +00002200 printf("%s: %d DEBUG2 %s/%d: %d wrote %d bytes(off:%ld), did not grow file\n",
plars865695b2001-08-27 22:15:12 +00002201 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 ***********************************************************************/
2217int
2218shrinkfile(fd, filename, trunc_incr, trunc_inter, just_trunc)
2219int fd;
2220char *filename;
2221int trunc_incr;
2222int trunc_inter; /* interval */
2223int 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 Cooperdf3eb162010-11-28 22:44:32 -08002235 if (trunc_inter == 0 || (shrink_cnt % trunc_inter != 0)) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002236 if (Debug > 3)
plars865695b2001-08-27 22:15:12 +00002237 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002242 if (Mode & MODE_FIFO) {
2243 if (Debug > 5)
plars865695b2001-08-27 22:15:12 +00002244 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002251 if ((cur_offset=lseek(fd,0,SEEK_CUR)) == -1) {
plars865695b2001-08-27 22:15:12 +00002252 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002258 if (Mode & MODE_RAND_LSEEK) {
2259 if (max_lseek <= -1) {
2260 if ((new_offset=file_size(fd)) == -1) {
plars865695b2001-08-27 22:15:12 +00002261 lkfile(fd, LOCK_UN, LKLVL0);
2262 return -1;
2263 }
subrata_modakbdbaec52009-02-26 12:14:51 +00002264
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002265 if (new_offset < min_lseek)
plars865695b2001-08-27 22:15:12 +00002266 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002275 if ((offset=lseek(fd, new_offset, SEEK_SET)) == -1) {
plars865695b2001-08-27 22:15:12 +00002276 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002281 else if (Debug > 3)
plars865695b2001-08-27 22:15:12 +00002282 printf("%s: %d DEBUG4 %s/%d: lseeked to random offset %d\n",
2283 Progname, Pid, __FILE__, __LINE__, offset);
subrata_modak4bb656a2009-02-26 12:02:09 +00002284
plars865695b2001-08-27 22:15:12 +00002285#endif
2286 }
2287
2288 else { /* remove trunc_incr from file */
2289
2290 new_offset = cur_offset-trunc_incr;
2291
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002292 if (new_offset < 0)
plars865695b2001-08-27 22:15:12 +00002293 new_offset=0;
subrata_modakbdbaec52009-02-26 12:14:51 +00002294
plars865695b2001-08-27 22:15:12 +00002295#ifdef CRAY
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002296 if (lseek(fd, new_offset, SEEK_SET) == -1) {
plars865695b2001-08-27 22:15:12 +00002297 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002302 else if (Debug > 3)
plars865695b2001-08-27 22:15:12 +00002303 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 Cooper53740502010-12-16 00:04:01 -08002313 if (ret == 0 && Debug > 3) {
plars865695b2001-08-27 22:15:12 +00002314 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002321 if (ret == -1) {
plars865695b2001-08-27 22:15:12 +00002322#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 Cooper8fb1cdb2010-11-28 22:56:35 -08002332 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00002333 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_modakbdbaec52009-02-26 12:14:51 +00002336
plars865695b2001-08-27 22:15:12 +00002337
2338 bytes_consumed -= (cur_offset - new_offset);
2339 return 0;
2340
2341} /* end of shrinkfile */
2342
2343/***********************************************************************
2344 *
2345 ***********************************************************************/
2346int
2347check_write(fd, cf_inter, filename, mode)
2348int fd;
2349int cf_inter; /* check file interval */
2350char *filename; /* needed for error messages */
2351int 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 Cooperdf3eb162010-11-28 22:44:32 -08002362 if (cf_inter == 0 || (cf_count % cf_inter != 0)) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002363 if (Debug > 4)
plars865695b2001-08-27 22:15:12 +00002364 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002369 if (Grow_incr <= 0) {
2370 if (Debug > 3)
robbiew79768052005-12-23 17:14:18 +00002371 printf("%s: %d DEBUG4 %s/%d: No write validation, Grow_incr = %d, offset = %ld\n",
plars865695b2001-08-27 22:15:12 +00002372 Progname, Pid, __FILE__, __LINE__, Grow_incr, Woffset);
2373 return 0; /* no check */
2374 }
2375
2376
subrata_modak4bb656a2009-02-26 12:02:09 +00002377
subrata_modakbdbaec52009-02-26 12:14:51 +00002378 /*
plars865695b2001-08-27 22:15:12 +00002379 * 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002384 if ((fsize=file_size(fd)) == -1) {
plars865695b2001-08-27 22:15:12 +00002385 lkfile(fd, LOCK_UN, LKLVL0);
2386 return -1;
2387
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002388 } else if (fsize <= Woffset) {
plars865695b2001-08-27 22:15:12 +00002389 /*
2390 * The file was truncated between write and now.
2391 * The contents of our last write is totally gone, no check.
2392 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002393 if (Debug > 1)
robbiew79768052005-12-23 17:14:18 +00002394 printf("%s%s: %d DEBUG2 %s/%d: %d File size (%d) smaller than where last wrote (%ld)- no write validation\n",
plars865695b2001-08-27 22:15:12 +00002395 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_modak4bb656a2009-02-26 12:02:09 +00002398
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002399 } else if (fsize < (Woffset + Grow_incr)) {
plars865695b2001-08-27 22:15:12 +00002400 /*
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 Cooper8fb1cdb2010-11-28 22:56:35 -08002409 if (Debug > 1) {
plars865695b2001-08-27 22:15:12 +00002410
robbiew79768052005-12-23 17:14:18 +00002411 printf("%s%s: %d DEBUG2 %s/%d: %d fsz:%d, lost(%d)of wrt(off:%ld, sz:%d), adj=%d\n",
plars865695b2001-08-27 22:15:12 +00002412 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, fsize, tmp-Grow_incr, Woffset, tmp, Grow_incr);
2413 }
2414
2415 }
2416
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002417 if (Debug > 2)
robbiew79768052005-12-23 17:14:18 +00002418 printf("%s: %d DEBUG3 %s/%d: about to do write validation, offset = %ld, size = %d\n",
plars865695b2001-08-27 22:15:12 +00002419 Progname, Pid, __FILE__, __LINE__, Woffset, Grow_incr);
2420
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002421 if (! (mode & MODE_FIFO)) {
plars865695b2001-08-27 22:15:12 +00002422
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002423 if (lseek(fd, Woffset, 0) == -1) {
robbiew79768052005-12-23 17:14:18 +00002424 fprintf(stderr, "%s%s: %d %s/%d: lseek(fd, %ld, 0) failed: %s\n",
plars865695b2001-08-27 22:15:12 +00002425 Progname, TagName, Pid, __FILE__, __LINE__, Woffset, strerror(errno));
2426 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002427 if (Debug > 3)
robbiew79768052005-12-23 17:14:18 +00002428 printf("%s: %d DEBUG4 %s/%d: lseeked to offset:%ld\n",
plars865695b2001-08-27 22:15:12 +00002429 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002445 if (ret != Grow_incr) {
plars865695b2001-08-27 22:15:12 +00002446 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002450 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00002451 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002465 if (Mode & MODE_GROW_BY_LSEEK) {
plars865695b2001-08-27 22:15:12 +00002466 /* check that all zeros upto last character */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002467 for (ptr=Buffer; ptr < (Buffer+Grow_incr-1); ptr++) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002468 if (*ptr != '\0') {
plars865695b2001-08-27 22:15:12 +00002469 fprintf(stderr,
2470 "%s%s: %d %s/%d: data mismatch at offset %d, exp:%#o(zerofilled), act:%#o in file %s\n",
subrata_modak4bb656a2009-02-26 12:02:09 +00002471 Progname, TagName, Pid, __FILE__, __LINE__,
plars865695b2001-08-27 22:15:12 +00002472 (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 Cooper8fb1cdb2010-11-28 22:56:35 -08002479 if (*ptr != 'w') {
plars865695b2001-08-27 22:15:12 +00002480 fprintf(stderr,
2481 "%s%s: %d %s/%d: data mismatch at offset %d, exp:%#o(zerofilled), act:%#o in file %s\n",
subrata_modak4bb656a2009-02-26 12:02:09 +00002482 Progname, TagName, Pid, __FILE__, __LINE__,
plars865695b2001-08-27 22:15:12 +00002483 (int)(Woffset+(Grow_incr-(Buffer-ptr))), 'w',
2484 *ptr, filename);
2485 fflush(stderr);
2486 return 1;
2487 }
2488 return 0; /* all is well */
subrata_modakbdbaec52009-02-26 12:14:51 +00002489
plars865695b2001-08-27 22:15:12 +00002490 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002491 else if (Pattern == PATTERN_OFFSET)
plars865695b2001-08-27 22:15:12 +00002492 ret=datapidchk(STATIC_NUM, Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002493 else if (Pattern == PATTERN_PID)
plars865695b2001-08-27 22:15:12 +00002494 ret=datapidchk(Pid, Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002495 else if (Pattern == PATTERN_ASCII)
plars865695b2001-08-27 22:15:12 +00002496 ret=dataasciichk(NULL, Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002497 else if (Pattern == PATTERN_RANDOM)
plars865695b2001-08-27 22:15:12 +00002498 ; /* no check for random */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002499 else if (Pattern == PATTERN_ALT)
plars865695b2001-08-27 22:15:12 +00002500 ret=databinchk('a', Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002501 else if (Pattern == PATTERN_CHKER)
plars865695b2001-08-27 22:15:12 +00002502 ret=databinchk('c', Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002503 else if (Pattern == PATTERN_CNTING)
plars865695b2001-08-27 22:15:12 +00002504 ret=databinchk('C', Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002505 else if (Pattern == PATTERN_ZEROS)
plars865695b2001-08-27 22:15:12 +00002506 ret=databinchk('z', Buffer, Grow_incr, Woffset, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002507 else if (Pattern == PATTERN_ONES)
plars865695b2001-08-27 22:15:12 +00002508 ret=databinchk('o', Buffer, Grow_incr, Woffset, &errmsg);
2509 else
2510 ret=dataasciichk(NULL, Buffer, Grow_incr, Woffset, &errmsg);
2511
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002512 if (ret >= 0) {
plars865695b2001-08-27 22:15:12 +00002513 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002516 if (Debug > 0)
robbiew79768052005-12-23 17:14:18 +00002517 printf("%s%s: %d DEBUG1 %s/%d: **fd:%d, lk:%d, offset:%ld, sz:%d open flags:%#o %s\n",
subrata_modak4bb656a2009-02-26 12:02:09 +00002518 Progname, TagName, Pid, __FILE__, __LINE__, fd, lockfile,
plars865695b2001-08-27 22:15:12 +00002519 Woffset, Grow_incr, Fileinfo.openflags, openflags2symbols(Fileinfo.openflags, ",", NULL));
2520
2521 fflush(stderr);
2522 return 1;
2523 }
2524
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002525 if (Debug > 6)
robbiew79768052005-12-23 17:14:18 +00002526 printf("%s: %d DEBUG7 %s/%d: No corruption detected on write validation , offset = %ld, size = %d\n",
plars865695b2001-08-27 22:15:12 +00002527 Progname, Pid, __FILE__, __LINE__, Woffset, Grow_incr);
2528
2529 return 0; /* all is well */
2530}
2531
2532
2533
2534/***********************************************************************
2535 *
2536 ***********************************************************************/
2537int
2538check_file(fd, cf_inter, filename, no_file_check)
2539int fd;
2540int cf_inter; /* check file interval */
2541char *filename; /* needed for error messages */
2542int 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 Cooperdf3eb162010-11-28 22:44:32 -08002555 if (cf_inter == 0 || (cf_count % cf_inter != 0)) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002556 if (Debug > 4)
plars865695b2001-08-27 22:15:12 +00002557 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 Cooperdf3eb162010-11-28 22:44:32 -08002565 if (no_file_check) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002566 if (Debug > 4)
plars865695b2001-08-27 22:15:12 +00002567 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_modakbdbaec52009-02-26 12:14:51 +00002578
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002579 if ((fsize=file_size(fd)) == -1) {
plars865695b2001-08-27 22:15:12 +00002580 lkfile(fd, LOCK_UN, LKLVL0);
2581 return -1;
2582 }
2583
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002584 if (fsize == 0) {
2585 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00002586 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002593 if (Debug > 2)
plars865695b2001-08-27 22:15:12 +00002594 printf("%s: %d DEBUG3 %s/%d: about to do file validation\n",
2595 Progname, Pid, __FILE__, __LINE__);
2596
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002597 if (fsize > MAX_FC_READ) {
plars865695b2001-08-27 22:15:12 +00002598 /*
2599 * read the file in MAX_FC_READ chuncks.
2600 */
2601
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002602 if ((buf=(char *)malloc(MAX_FC_READ)) == NULL) {
plars865695b2001-08-27 22:15:12 +00002603 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002614 while (rd_cnt < fsize) {
2615 if (fsize - rd_cnt > MAX_FC_READ)
plars865695b2001-08-27 22:15:12 +00002616 rd_size=MAX_FC_READ;
2617 else
2618 rd_size=fsize - rd_cnt;
subrata_modakbdbaec52009-02-26 12:14:51 +00002619
plars865695b2001-08-27 22:15:12 +00002620#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 Cooper8fb1cdb2010-11-28 22:56:35 -08002627 if (ret != rd_size) {
subrata_modak4bb656a2009-02-26 12:02:09 +00002628 fprintf(stderr, "%s%s: %d %s/%d: %d CFa %s\n",
plars865695b2001-08-27 22:15:12 +00002629 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002638 if (Pattern == PATTERN_OFFSET)
plars865695b2001-08-27 22:15:12 +00002639 ret=datapidchk(STATIC_NUM, buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002640 else if (Pattern == PATTERN_PID)
plars865695b2001-08-27 22:15:12 +00002641 ret=datapidchk(Pid, buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002642 else if (Pattern == PATTERN_ASCII)
plars865695b2001-08-27 22:15:12 +00002643 ret=dataasciichk(NULL, buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002644 else if (Pattern == PATTERN_RANDOM)
plars865695b2001-08-27 22:15:12 +00002645 ; /* no checks for random */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002646 else if (Pattern == PATTERN_ALT)
plars865695b2001-08-27 22:15:12 +00002647 ret=databinchk('a', buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002648 else if (Pattern == PATTERN_CHKER)
plars865695b2001-08-27 22:15:12 +00002649 ret=databinchk('c', buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002650 else if (Pattern == PATTERN_CNTING)
plars865695b2001-08-27 22:15:12 +00002651 ret=databinchk('C', buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002652 else if (Pattern == PATTERN_ZEROS)
plars865695b2001-08-27 22:15:12 +00002653 ret=databinchk('z', buf, rd_size, rd_cnt, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002654 else if (Pattern == PATTERN_ONES)
plars865695b2001-08-27 22:15:12 +00002655 ret=databinchk('o', buf, rd_size, rd_cnt, &errmsg);
2656 else
2657 ret=dataasciichk(NULL, buf, rd_size, rd_cnt, &errmsg);
subrata_modakbdbaec52009-02-26 12:14:51 +00002658
subrata_modak4bb656a2009-02-26 12:02:09 +00002659
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002660 if (ret >= 0) {
plars865695b2001-08-27 22:15:12 +00002661 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_modak4bb656a2009-02-26 12:02:09 +00002679 * Read the whole file in a single read
plars865695b2001-08-27 22:15:12 +00002680 */
Garrett Cooper53740502010-12-16 00:04:01 -08002681 if ((buf = (char *)malloc(fsize)) == NULL) {
plars865695b2001-08-27 22:15:12 +00002682 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002703 if (ret != fsize) {
subrata_modak4bb656a2009-02-26 12:02:09 +00002704 fprintf(stderr, "%s%s: %d %s/%d: %d CFw %s\n",
plars865695b2001-08-27 22:15:12 +00002705 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt, errmsg);
2706 ret_val=1;
2707 }
2708 else {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002709 if (Pattern == PATTERN_OFFSET)
plars865695b2001-08-27 22:15:12 +00002710 ret=datapidchk(STATIC_NUM, buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002711 else if (Pattern == PATTERN_PID)
plars865695b2001-08-27 22:15:12 +00002712 ret=datapidchk(Pid, buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002713 else if (Pattern == PATTERN_ASCII)
plars865695b2001-08-27 22:15:12 +00002714 ret=dataasciichk(NULL, buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002715 else if (Pattern == PATTERN_RANDOM)
plars865695b2001-08-27 22:15:12 +00002716 ; /* no check for random */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002717 else if (Pattern == PATTERN_ALT)
plars865695b2001-08-27 22:15:12 +00002718 ret=databinchk('a', buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002719 else if (Pattern == PATTERN_CHKER)
plars865695b2001-08-27 22:15:12 +00002720 ret=databinchk('c', buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002721 else if (Pattern == PATTERN_CNTING)
plars865695b2001-08-27 22:15:12 +00002722 ret=databinchk('C', buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002723 else if (Pattern == PATTERN_ZEROS)
plars865695b2001-08-27 22:15:12 +00002724 ret=databinchk('z', buf, fsize, 0, &errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002725 else if (Pattern == PATTERN_ONES)
plars865695b2001-08-27 22:15:12 +00002726 ret=databinchk('o', buf, fsize, 0, &errmsg);
2727 else
2728 ret=dataasciichk(NULL, buf, fsize, 0, &errmsg);
2729
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002730 if (ret >= 0) {
plars865695b2001-08-27 22:15:12 +00002731 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 ***********************************************************************/
2747int
2748file_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_modakbdbaec52009-02-26 12:14:51 +00002756
plars865695b2001-08-27 22:15:12 +00002757 }
2758
2759 return sb.st_size;
2760}
2761
2762/***********************************************************************
2763 * do file lock/unlock action.
2764 ***********************************************************************/
2765int
2766lkfile(int fd, int operation, int lklevel)
2767{
2768 char *errmsg;
2769
subrata_modak4bb656a2009-02-26 12:02:09 +00002770
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002771 if (lockfile == lklevel) {
subrata_modakbdbaec52009-02-26 12:14:51 +00002772
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002773 if (Debug > 5) {
plars865695b2001-08-27 22:15:12 +00002774 switch (operation) {
2775 case LOCK_UN:
subrata_modak4bb656a2009-02-26 12:02:09 +00002776 printf("%s: %d DEBUG6 %s/%d: Attempting to release lock on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002777 Progname, Pid, __FILE__, __LINE__, fd);
2778 break;
2779
2780 case LOCK_SH:
subrata_modak4bb656a2009-02-26 12:02:09 +00002781 printf("%s: %d DEBUG6 %s/%d: Attempting to get read/shared lock on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002782 Progname, Pid, __FILE__, __LINE__, fd);
2783 break;
2784
2785 case LOCK_EX:
subrata_modak4bb656a2009-02-26 12:02:09 +00002786 printf("%s: %d DEBUG6 %s/%d: Attempting to get write/exclusive lock on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002787 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002798 if (file_lock(fd, operation, &errmsg) != 0) {
plars865695b2001-08-27 22:15:12 +00002799 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002806 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00002807 switch (operation) {
2808 case LOCK_UN:
subrata_modak4bb656a2009-02-26 12:02:09 +00002809 printf("%s: %d DEBUG3 %s/%d: Released lock on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002810 Progname, Pid, __FILE__, __LINE__, fd);
2811 break;
2812
2813 case LOCK_SH:
subrata_modak4bb656a2009-02-26 12:02:09 +00002814 printf("%s: %d DEBUG3 %s/%d: Got read/shared lock on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002815 Progname, Pid, __FILE__, __LINE__, fd);
2816 break;
2817
2818 case LOCK_EX:
subrata_modak4bb656a2009-02-26 12:02:09 +00002819 printf("%s: %d DEBUG3 %s/%d: Got write/exclusive lock on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002820 Progname, Pid, __FILE__, __LINE__, fd);
2821 break;
subrata_modakbdbaec52009-02-26 12:14:51 +00002822
plars865695b2001-08-27 22:15:12 +00002823 default:
subrata_modak4bb656a2009-02-26 12:02:09 +00002824 printf("%s: %d DEBUG3 %s/%d: Completed action %d on fd %d\n",
plars865695b2001-08-27 22:15:12 +00002825 Progname, Pid, __FILE__, __LINE__, operation, fd);
2826 break;
2827 }
2828 }
2829 }
2830
2831 return 0;
2832}
2833
2834#ifndef linux
2835/***********************************************************************
2836 *
2837 ***********************************************************************/
2838int
mridgedec5a462005-02-25 15:49:37 +00002839pre_alloc(int fd, long size)
plars865695b2001-08-27 22:15:12 +00002840{
2841
2842#ifdef CRAY
2843 long avl;
2844
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002845 if (ialloc(fd, size, IA_CONT, &avl) == -1) {
plars865695b2001-08-27 22:15:12 +00002846 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 Cooper8fb1cdb2010-11-28 22:56:35 -08002861 if (fcntl( fd, F_RESVSP, &f ) == -1) {
plars865695b2001-08-27 22:15:12 +00002862 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