File: crashme.c

package info (click to toggle)
crashme 2.4-5
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 116 kB
  • ctags: 78
  • sloc: ansic: 868; makefile: 60; sh: 5
file content (756 lines) | stat: -rw-r--r-- 21,672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* crashme: Create a string of random bytes and then jump to it.
            crashme [+]<nbytes>[.inc] <srand> <ntrys> [nsub] [verboseness] */

char *crashme_version = "2.4 20-MAY-1994";

/*
 *             COPYRIGHT (c) 1990-1994 BY        *
 *  GEORGE J. CARRETTE, CONCORD, MASSACHUSETTS.  *
 *             ALL RIGHTS RESERVED               *

Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all copies
and that both that copyright notice and this permission notice appear
in supporting documentation, and that the name of the author
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

A signal handler is set up so that in most cases the machine exception
generated by the illegal instructions, bad operands, etc in the procedure
made up of random data are caught; and another round of randomness may
be tried. Eventually a random instruction may corrupt the program or
the machine state in such a way that the program must halt. This is
a test of the robustness of the hardware/software for instruction
fault handling.

Note: Running this program just a few times, using total CPU time of
less than a few seconds SHOULD NOT GIVE YOU ANY CONFIDENCE in system
robustness. Having it run for hours, with tens of thousands of cases
would be a different thing. It would also make sense to run this
stress test at the same time you run other tests, like a multi-user
benchmark.

Comments may be addressed to the author at GJC@WORLD.STD.COM

See the documentation in crashme.1 and READ.ME, or read this code for 
a description of command line arguments to this program. 

Version Date         Description
----------------------------------------------------------------------
 1.0    early 1990   initial hack.
 1.1    19-SEP-1990  added more signals and an alarm to abort looping.
 1.2    25-JUN-1991  added [nsub] to vfork multiple subprocesses of self.
 1.3    14-AUG-1991  +nbytes malloc option, and -nsub option.
 1.4    29-AUG-1991  fix +nbytes (subproc). Add time-driven nprocs. SIGINT.
 1.5     3-SEP-1991  added alarm subprocess monitor to vfork_main.
 1.6     5-SEP-1991  some systems don't have vfork, so use fork by default.
 1.7    25-SEP-1991  verboseness level, exit summary report.
 1.8      -SEP-1991  address page protection issues on badboy.
 1.9     6-AUG-1993  DECC(VMS)/WIN32/NT/Posix, #ifdef some SIGxxx.
 2.0     7-SEP-1993  More extensive WIN32 conditionalization. record_note.
 2.1     6-MAY-1994  Added "dot" syntax to NBYTES. Modularized castaway.
 2.2     9-MAY-1994  __ALPHA && VMS version is now more interesting.
 2.3    11-MAY-1994  Added _IBMRT2 and _POWER code.
 2.4    20-MAY-1994  Added __hpux. Linux from jik@cam.ov.com.

Suggested test: At least let the thing run the length of your lunch break,
in this case 1 hour, 10 minutes, and 30 seconds.

  crashme +2000 666 100 1:10:30 2

Also, it may spend more time trapping and less time computing random bytes
by using these arguments:

  crashme +2000.80 666 100 1:10:30 2

                        CRASH REPORTS

Date,               Machine              Crashme        Reported 
Crashme Ver   Make    Model OS Version   Arguments      by:        
------------------------------------------------------------------------------
10-JUL-90 1.0 SUN     4/110 4.1          1000 20 200    GJC@paradigm.com
10-JUL-90 1.0 SUN     4/280 4.0.3        1000 20 200    GJC@paradigm.com
31-JUL-90 1.0 DIGITAL DECstation 3100    100 10 10000   GAVRON@ARIZONA.EDU
31-JUL-90 1.0 IBM     RT                 100 10 10000   GAVRON@ARIZONA.EDU
 1-AUG-90 1.0 DIGITAL DECstation 5000    10000 230 1000 hudgens@scri.fsu.edu
 3-AUG-90 1.0 Alliant FX/2800                           SJA@SIRIUS.HUT.FI
27-JUN-91 1.2 SUN     4/110 4.1.1        10 1000 10     LPH@PARADIGM.COM   
27-JUN-91 1.2 SUN     4/110 4.1.1        1000 20 200 10 LPH@PARADIGM.COM   
29-JUN-91 1.2 SUN     4/40C 4.1.1        9 29748 5877 4 jon@uk.ac.oxford.robots
29-JUN-91 1.2 SUN     4/60 4.1.1         9 29748 5877 4 jon@uk.ac.oxford.robots
29-JUN-91 1.2 SUN     4/100 4.1.1        9 29748 5877 4 jon@uk.ac.oxford.robots
29-JUN-91 1.2 SUN     4/65 4.1.1         9 29748 5877 4 jon@uk.ac.oxford.robots
18-JUL-91 1.2 SGI     Iris4d Unix 3.3.2  1000 $$ 1000 4 tsacas@ilog.ilog.fr
29-JUL-91 1.1 IBM     RS/6000 AIX 1.3    script         brandis@inf.ethz.ch
 5-SEP-91 1.6 IBM     RS/6000-320 AIX 3.1.5 +2000 666 50 40:00:00 LPH
26-SEP-91 1.8 Nixdorf Targon/35 TOS3.3   script         petri@ibr.cs.tu-bs.de
 9-SEP-93 2.0 Microsoft WNT Build 511 i486 +1000 24131 50 gjc@mitech.com
 3-FEB-94 1.8 HP710/HP-UX 9.00 +2000 666 100 2:00:00 5 UFOP@fpsp.fapesp.br
 5-MAY-94 2.0 HP807/HPUX 9.00  4000 666 100 00:30:00 2 UFOP@fpsp.fapesp.br

Notes: Crashme V1.0 {1000 20 200} used to down the SUN 4/110. V1.2 does *not*
crash SUNOS 4.1.1 on the same arguments. Although using the extra argument
for subprocesses it will crash, with the console reporting:
"Bad Trap, Bad Kernel Read Fault, Bus error. Reboot" 

Script means invoking file with many calls to crashme such as this:
#/bin/csh
crashme 1020 234 500 &
crashme 394 38484 5723 &
crashme 3784 474 474 &
crashme 437 4747 38 &
crashme 47848 4745 123 &
crashme 4747 4747 3463 &
crashme 474 46464 262 &
crashme 37 3644 3723 &
crashme 374 46464 22 &
crashme 3747 464 363 &
crashme 347 4747 44 &
crashme 37374 374 66 &
crashme 3737 474 4444 &

The 4-argument case of crashme could likely do as well as executing
a script.

*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <excpt.h>
#include <windows.h>
#else
#include <signal.h>
#include <setjmp.h>
#endif
#include <time.h>
#ifdef VMS
#include <processes.h>
#endif

#ifdef pyr
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/vmmac.h>
#define strchr index
#endif

#ifdef linux
#include <unistd.h>
#endif

typedef void (*BADBOY)();

BADBOY badboy;

long nbytes,nseed,ntrys;
long incptr = 0;
long offset = 0;
long next_offset = 0;
long malloc_flag = 0;
unsigned char *the_data;
char *note_buffer;
char *notes;

long verbose_level = 5;

void old_main(),copyright_note(),vfork_main(),badboy_loop();
void record_note();


FILE *logfile = NULL;

void record_note()
{char *logfilename;
 if (!(logfilename = getenv("CRASHLOG"))) return;
 if (!(logfile = fopen(logfilename,
		       (strncmp(note_buffer,"Subprocess",10) == 0)
		       ? "a" : "w")))
   {perror(logfilename);
    return;}
 if (note_buffer[strlen(note_buffer)-1] != '\n')
   strcat(note_buffer,"\n");
 fputs(note_buffer,logfile);
 fclose(logfile);
 logfile = NULL;}

void open_record()
{char *logfilename;
 if (!(logfilename = getenv("CRASHLOG"))) return;
 if (!(logfile = fopen(logfilename,"a")))
   {perror(logfilename);
    return;}}

void close_record()
{if (logfile)
   {fclose(logfile);
    logfile = NULL;}}
 
void note(level)
     long level;
{if (level > verbose_level) return;
 strcat(note_buffer,"\n");
 fputs(note_buffer,stdout);
 if (logfile)
   fputs(note_buffer,logfile);}

#ifndef WIN32
jmp_buf again_buff;
#endif

unsigned char *bad_malloc(n)
     long n;
{unsigned char *data;
 data = (unsigned char *) malloc(n);
#ifdef pyr
 if (mprotect(((int)data/PAGSIZ)*PAGSIZ, (n/PAGSIZ+1)*PAGSIZ,
	      PROT_READ|PROT_WRITE|PROT_EXEC))
   perror("mprotect");
#endif
 return(data);}

#ifndef WIN32

void again_handler(sig)
     int sig;
{char *ss;
 switch(sig)
   {case SIGILL: ss =   " illegal instruction"; break;
#ifdef SIGTRAP
    case SIGTRAP: ss =   " trace trap"; break;
#endif
    case SIGFPE: ss =   " arithmetic exception"; break;
#ifdef SIGBUS
    case SIGBUS: ss =  " bus error"; break;
#endif
    case SIGSEGV: ss =  " segmentation violation"; break;
#ifdef SIGIOT
    case SIGIOT: ss = " IOT instruction"; break;
#endif
#ifdef SIGEMT
    case SIGEMT: ss = " EMT instruction"; break;
#endif
#ifdef SIGALRM
    case SIGALRM: ss = " alarm clock"; break;
#endif
    case SIGINT:  ss = " interrupt"; break;
    default: ss = "";}
 sprintf(notes,"Got signal %d%s",sig,ss);
 note(5);
 longjmp(again_buff,3);}

void my_signal(sig, func)
     int sig;
     void (*func)();
{
#ifndef SA_ONESHOT
 signal(sig, func);
#else
 struct sigaction act;
 act.sa_handler = func;
 bzero(&act.sa_mask,sizeof(sigset_t));
 act.sa_flags = SA_NOMASK;
#ifdef SA_RESTART
 act.sa_flags |= SA_RESTART;
#endif
 sigaction(sig, &act, 0);
#endif /* SA_ONESHOT */
}
 
set_up_signals()
{my_signal(SIGILL,again_handler);
#ifdef SIGTRAP
 my_signal(SIGTRAP,again_handler);
#endif
 my_signal(SIGFPE,again_handler);
#ifdef SIGBUS
 my_signal(SIGBUS,again_handler);
#endif
 my_signal(SIGSEGV,again_handler);
#ifdef SIGIOT
 my_signal(SIGIOT,again_handler);
#endif
#ifdef SIGEMT
 my_signal(SIGEMT,again_handler);
#endif
#ifdef SIGALRM
 my_signal(SIGALRM,again_handler);
#endif
 my_signal(SIGINT,again_handler);}

#endif

compute_badboy_1(n)
     long n;
{long j;
 if (malloc_flag == 1)
   the_data = bad_malloc(n);
 for(j=0;j<n;++j) the_data[j] = (rand() >> 7) & 0xFF;
 if (nbytes < 0)
   {sprintf(notes,"Dump of %ld bytes of data",n);
    note(1);
    for(j=0;j<n;++j)
      {fprintf(stdout,"%3d",the_data[j]);
       if ((j % 20) == 19) putc('\n',stdout); else putc(' ',stdout);}
    putc('\n',stdout);}}

void proto_badboy()
{printf("Hello world.\n");}

#if defined(__ALPHA) && defined(VMS) && !defined(NOCASTAWAY)
#include <pdscdef.h>
#endif

BADBOY castaway(dat)
     char *dat;
{
#if defined(VAX) && !defined(NOCASTAWAY)
  /* register save mask avoids bashing our callers locals */
  ((unsigned short *)dat)[0] = 0x0FFC;
#endif
#if defined(__ALPHA) && defined(VMS) && !defined(NOCASTAWAY)
  struct pdscdef *p,*b;
  p = (struct pdscdef *) proto_badboy;
  b = (struct pdscdef *) dat;
  memcpy(b,p,sizeof(struct pdscdef));
  b->pdsc$q_entry[1] = 0;
  b->pdsc$q_entry[0] = (int)&dat[sizeof(struct pdscdef)];
#endif
#if (defined(_IBMR2) || defined(_POWER)) && !defined(NOCASTAWAY)
  struct fdesc {void *fcn_addr; void *toc; void *linkage;} *p,*b;
  p = (struct fdesc *) proto_badboy;
  b = (struct fdesc *) dat;
  memcpy(b,p,sizeof(struct fdesc));
  b->fcn_addr = (void *) &dat[sizeof(struct fdesc)];
#endif
#if defined(__hpux) && defined(__hppa) && !defined(NOCASTAWAY)
  struct fdesc {void *fcn_addr; void *toc;} *p,*b;
  p = (struct fdesc *) proto_badboy;
  b = (struct fdesc *) dat;
  memcpy(b,p,sizeof(struct fdesc));
  b->fcn_addr = (void *) &dat[sizeof(struct fdesc)];
#endif
  return((BADBOY)dat);}

compute_badboy()
{long n;
 n = (nbytes < 0) ? - nbytes : nbytes;
 if (incptr == 0)
   {compute_badboy_1(n);
    badboy = castaway(the_data);}
 else if ((next_offset == 0) || (next_offset > ((n * 90) / 100)))
   {compute_badboy_1(n);
    offset = 0;
    next_offset = offset + incptr;
    badboy = castaway(the_data);}
 else
   {offset = next_offset;
    next_offset = offset + incptr;
    badboy = castaway(&the_data[offset]);}}

/* maybe add this code before invoking badboy. But it didn't seem
   to be needed when using +1000.
   FlushInstructionCache(GetCurrentProcess(),
                         the_data,(nbytes < 0) ? - nbytes : nbytes);
*/

try_one_crash()
{if (nbytes > 0)
   (*badboy)();
 else if (nbytes == 0)
   while(1);}

char *subprocess_ind = "subprocess";
 
main(argc,argv)
     int argc; char **argv;
{long nsubs,hrs,mns,scs,tflag,j,m;
 note_buffer = (char *) malloc(512);
 notes = note_buffer;
 if ((argc == 7) &&
     (strcmp(argv[6],subprocess_ind) == 0))
   {sprintf(note_buffer,"Subprocess %s: ",argv[4]);
    notes = note_buffer + strlen(note_buffer);
    verbose_level = atol(argv[5]);
    sprintf(notes,"starting");
    note(3);
    old_main(4,argv);}
 else if (argc == 4)
   old_main(4,argv);
 else if ((argc == 6) && ((strlen(argv[4]) == 0) ||
			  (strcmp(argv[4],".") == 0)))
   {verbose_level = atol(argv[5]);
    old_main(4,argv);}
 else if ((argc == 5) || (argc == 6))
   {if (argc == 6)
      verbose_level = atol(argv[5]);
    copyright_note(1);
    if (argc < 7)
      m = argc;
    else
      m = 6;
    strcpy(notes,"crashme");
    for(j=1;j<m;++j)
      {strcat(notes," ");
       strcat(notes,argv[j]);}
    note(1);
    record_note();
    if (strchr(argv[4],':'))
      {sscanf(argv[4],"%d:%d:%d",&hrs,&mns,&scs);
       tflag = 1;
       nsubs = (((hrs * 60) + mns) * 60) + scs;
       sprintf(notes,"Subprocess run for %d seconds (%d %02d:%02d:%02d)",
	       nsubs, hrs / 24, hrs % 24,mns,scs);}
    else
      {tflag = 0;
       nsubs = atol(argv[4]);
       sprintf(notes,"Creating %d crashme subprocesses",nsubs);}
    note(1);
    vfork_main(tflag,nsubs,argv[0],argv[1],atol(argv[2]),argv[3]);}
 else
   {sprintf(notes,
	    "crashme [+]<nbytes>[.inc] <srand> <ntrys> [nsub] [verbose]");
    note(0);}}

void copyright_note(n)
     long n;
{sprintf(notes,"Crashme: (c) Copyright 1990-1994 George J. Carrette");
 note(n);
 sprintf(notes,"Version: %s",crashme_version);
 note(n);}

void old_main(argc,argv)
     int argc;
     char **argv;
{char *ptr;
 copyright_note(3);
 nbytes = atol(argv[1]);
 if (ptr = strchr(argv[1],'.'))
   incptr = atol(&ptr[1]);
 if (argv[1][0] == '+') malloc_flag = 1;
 nseed = atol(argv[2]);
 ntrys = atol(argv[3]);
 sprintf(notes,"crashme %s%ld.%d %ld %ld",
	 (malloc_flag == 0) ? "" : "+",nbytes,incptr,nseed,ntrys);
 note(3);
 record_note();
 if (malloc_flag == 0)
   {the_data = bad_malloc((nbytes < 0) ? -nbytes : nbytes);
    badboy = castaway(the_data);
    sprintf(notes,"Badboy at %d. 0x%X",badboy,badboy);
    note(3);}
 srand(nseed);
#ifdef WIN32
 SetErrorMode(SEM_FAILCRITICALERRORS |
	      SEM_NOGPFAULTERRORBOX |
	      SEM_NOOPENFILEERRORBOX);
#endif
 badboy_loop();}

#ifdef WIN32
DWORD exception_filter(DWORD value)
{int sev,cus,res,fac,cod;
 sev = 3 & (value >> 30);
 cus = 1 & (value >> 29);
 res = 1 & (value >> 28);
 fac = 07777 & (value >> 16);
 cod = 0xFFFF & value;
 sprintf(notes,"sev(%d)cus(%d)res(%d)fac(%d)code(%d)",
	 sev,cus,res,fac,cod);
 note(5);
 return(EXCEPTION_EXECUTE_HANDLER);}
#endif

void badboy_loop()
{int i;
 for(i=0;i<ntrys;++i)
   {compute_badboy();
    if (offset)
      sprintf(notes,"try %d, offset %d",i,offset);
    else if (malloc_flag == 1)
      sprintf(notes,"try %d, Badboy at %d. 0x%X",i,badboy,badboy);
    else
      sprintf(notes,"try %d",i);
    note(5);

#ifdef WIN32
    try {try_one_crash();
	 sprintf(notes,"didn't barf!");
	 note(5);}
    except(exception_filter(GetExceptionCode()))
      {}
#else
    if (setjmp(again_buff) == 3)
      {sprintf(notes,"Barfed");
       note(5);}
    else
      {set_up_signals();
       alarm(10);
       try_one_crash();
       sprintf(notes,"didn't barf!");
       note(5);}
#endif
  }}

struct status_list
{long status;
 long count;
 struct status_list *next;};

struct status_list *slist = NULL;

record_status(n)
     long n;
{struct status_list *l;
 for(l=slist;l != NULL; l = l->next)
   if (n == l->status)
     return(++l->count);
 l = (struct status_list *) malloc(sizeof(struct status_list));
 l->count = 1;
 l->status = n;
 l->next = slist;
 slist = l;
 return(1);}

summarize_status()
{struct status_list *l;
 sprintf(notes,"exit status ... number of cases");
 note(2);
 for(l=slist;l != NULL; l = l->next)
   {sprintf(notes,"exit status ... number of cases");
    sprintf(notes,"%11d ... %5d",l->status,l->count);
    note(2);}}

#ifndef WIN32

long monitor_pid = 0;
long monitor_period = 5;
long monitor_limit =  6; /* 30 second limit on a subprocess */
long monitor_count = 0;
long monitor_active = 0;

void monitor_fcn(sig)
     int sig;
{long status;
 my_signal(SIGALRM,monitor_fcn);
 alarm(monitor_period);
 if (monitor_active)
   {++monitor_count;
    if (monitor_count >= monitor_limit)
      {sprintf(notes,"time limit reached on pid %d 0x%X. using kill.",
	       monitor_pid,monitor_pid);
       note(3);
       status = kill(monitor_pid,SIGKILL);
       if (status < 0)
	 {sprintf(notes,"failed to kill process");
	  note(3);}
       monitor_active = 0;}}}

void vfork_main(tflag,nsubs,cmd,nb,sr,nt)
     long tflag,nsubs,sr;
     char *cmd,*nb,*nt;
{long j,pid,n,seq,total_time,dys,hrs,mns,scs;
 int status;
 char arg2[20],arg4[20],arg5[20];
 time_t before_time,after_time;
 if (tflag == 1)
   {seq = 1;
    n = 100000000;}
 else if (nsubs < 0)
   {n = -nsubs;
    seq = 1;}
 else
   {n = nsubs;
    seq = 0;}
 if (seq == 1)
   {my_signal(SIGALRM,monitor_fcn);
    alarm(monitor_period);}
 time(&before_time);
 sprintf(arg5,"%d",verbose_level);
 for(j=0;j<n;++j)
   {sprintf(arg2,"%d",sr+j);
    sprintf(arg4,"%d",j+1);
#ifdef VMS
    status = vfork();
#else
    status = fork();
#endif
    if (status == 0)
      {status = execlp(cmd,cmd,nb,arg2,nt,arg4,arg5,subprocess_ind,0);
       if (status == -1)
	 {perror(cmd);
	  exit(1);}}
    else if (status < 0)
      perror(cmd);
    else
      {sprintf(notes,"pid = %d 0x%X (subprocess %d)",status,status,j+1);
       note(3);
       if (seq == 1)
	 {monitor_pid = status;
	  monitor_count = 0;
	  monitor_active = 1;
	  while((pid = wait(&status)) > 0)
	    {monitor_active = 0;
	     sprintf(notes,"pid %d 0x%X exited with status %d",pid,pid,status);
	     note(3);
	     record_status(status);}}
       if (tflag == 1)
	 {time(&after_time);
	  total_time = after_time - before_time;
	  if (total_time >= nsubs)
	    {sprintf(notes,"Time limit reached after run %d",j+1);
	     note(2);
	     break;}}}}
 if (seq == 0)
   while((pid = wait(&status)) > 0)
     {sprintf(notes,"pid %d 0x%X exited with status %d",pid,pid,status);
      note(3);
      record_status(status);}
 time(&after_time);
 total_time = after_time - before_time;
 scs = total_time;
 mns = scs / 60;
 hrs = mns / 60;
 dys = hrs / 24;
 scs = scs % 60;
 mns = mns % 60;
 hrs = hrs % 24;
 open_record();
 sprintf(notes,
	 "Test complete, total real time: %d seconds (%d %02d:%02d:%02d)",
	 total_time,dys,hrs,mns,scs);
 note(1);
 summarize_status();
 close_record();}

#else

void chk_CloseHandle(HANDLE h)
{DWORD err;
 if (CloseHandle(h) == FALSE)
   {err = GetLastError();
    sprintf(notes,"err %d trying to close handle.",err);
    note(3);}}

int maxticks = 100; /* tenths of a second before forced termination
		       of the subprocess */

void vfork_main(tflag,nsubs,cmd,nb,sr,nt)
     long tflag,nsubs,sr;
     char *cmd,*nb,*nt;
{long j,pid,n,seq,total_time,dys,hrs,mns,scs;
 char arg2[20],arg4[20],arg5[20];
 time_t before_time,after_time;
 char cmdbuf[250];
 int nticks;
 PROCESS_INFORMATION pinfo;
 STARTUPINFO sinfo;
 DWORD exit_code,err;
 if (tflag == 1)
   {seq = 1;
    n = 100000000;}
 else if (nsubs < 0)
   {n = -nsubs;
    seq = 1;}
 else
   {n = nsubs;
    seq = 0;}
 /* tflag says this is a timed run. So nsub is the time in seconds.
    seq says to run in sequence, not created nsub processes all
    at once. */
 if (seq == 0)
   {printf("Not implemented. Use [-nsub] or [HH:MM:SS] instead.\n");
    return;}
 time(&before_time);
 sprintf(arg5,"%d",verbose_level);
 for(j=0;j<n;++j)
   {sprintf(arg2,"%d",sr+j);
    sprintf(arg4,"%d",j+1);
    sprintf(cmdbuf,"%s %s %s %s %s %s %s",
	    cmd,nb,arg2,nt,arg4,arg5,subprocess_ind);
    /* kind of dumb that this sinfo structure is a required argument */
    memset(&sinfo,0,sizeof(STARTUPINFO));
    sinfo.cb = sizeof (STARTUPINFO);
    if (CreateProcess(NULL,cmdbuf,NULL,NULL,TRUE,0,NULL,NULL,&sinfo,&pinfo)
	== FALSE)
      {err = GetLastError();
       sprintf(notes,"err %d trying to create process",err);
       continue;}
    sprintf(notes,"pid = %d 0x%X (subprocess %d)",
	    pinfo.dwProcessId,pinfo.dwProcessId,j+1);
    note(3);
    nticks = 0;
    while(1)
      {if (GetExitCodeProcess(pinfo.hProcess,&exit_code) == TRUE)
	 {if (exit_code == STILL_ACTIVE)
	    {if (nticks == maxticks)
	       {sprintf(notes,"time limit reached on pid %d 0x%X. using kill.",
			pinfo.dwProcessId,pinfo.dwProcessId);
		note(3);
		if (TerminateProcess(pinfo.hProcess,
				     APPLICATION_ERROR_MASK |
				     ERROR_SEVERITY_ERROR |
				     1) == FALSE)
		  {err = GetLastError();
		   sprintf(notes,"err %d trying to terminate process.",err);
		   note(3);
		   chk_CloseHandle(pinfo.hProcess);
		   chk_CloseHandle(pinfo.hThread);
		   break;}
		++nticks;}
	    else
	      {++nticks;
	       Sleep(100);}}
	 else
	   {sprintf(notes,"pid %d 0x%X exited with status %d",
		    pinfo.dwProcessId,pinfo.dwProcessId,exit_code);
	    note(3);
	    record_status(exit_code);
	    chk_CloseHandle(pinfo.hProcess);
	    chk_CloseHandle(pinfo.hThread);
	    break;}}
      else
	{err = GetLastError();
	 sprintf(notes,"err %d on GetExitCodeProcess.");
	 note(3);
	 chk_CloseHandle(pinfo.hProcess);
	 chk_CloseHandle(pinfo.hThread);
	 break;}}
    if (tflag == 1)
      {time(&after_time);
       total_time = after_time - before_time;
       if (total_time >= nsubs)
	 {sprintf(notes,"Time limit reached after run %d",j+1);
	  note(2);
	  break;}}}
 time(&after_time);
 total_time = after_time - before_time;
 scs = total_time;
 mns = scs / 60;
 hrs = mns / 60;
 dys = hrs / 24;
 scs = scs % 60;
 mns = mns % 60;
 hrs = hrs % 24;
 open_record();
 sprintf(notes,
	 "Test complete, total real time: %d seconds (%d %02d:%02d:%02d)",
	 total_time,dys,hrs,mns,scs);
 note(1);
 summarize_status();
 open_record();}

#endif