Welcome to mirror list, hosted at ThFree Co, Russian Federation.

kill02.c « ltp « winsup.api « testsuite « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eba47cc6ac2ee2982f614d1fe8d3aa74385cf9e9 (plain)
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 * Mountain View, CA  94043, or:
 *
 * http://www.sgi.com
 *
 * For further information regarding this notice, see:
 *
 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
 *
 */
/* $Id$ */
/***********************************************************************************

    OS Test -  Silicon Graphics, Inc.

    TEST IDENTIFIER :  kill02  Sending a signal to processes with the same process group ID.

    PARENT DOCUMENT :  kiltds01  Kill System Call.
		
    AUTHOR          :  Dave Baumgartner

    CO-PILOT        :  Barrie Kletscher

    DATE STARTED    :  12/30/85

    TEST ITEMS

	1. Sending a signal to pid of zero sends to all processes whose process
	   group ID is equal to the process group ID as the sender.

	2. Sending a signal to pid of zero does not send to processes in another process group.


    OUTPUT SPECIFICATIONS

	PASS :
		kiltcs02 1 PASS The signal was sent to all processes in the process group.
		kiltcs02 2 PASS The signal was not sent to selective processes that were not in the process group.

	FAIL :
		kiltcs02 1 FAIL The signal was not sent to all processes in the process group.
		kiltcs02 2 FAIL The signal was sent to a process that was not in the process group.

	BROK :
		kiltcs02 # BROK System call XXX failed. Errno:X, Error message:XXX.
		kiltcs02 # BROK Setting to catch unexpected signal %d failed. Errno: %d, Error message %s.
		kiltcs02 # BROK Setting to ignore signal %d failed. Errno: %d, Error message %s.

	WARN :
		kiltcs02 0 WARN Unexpected signal X was caught.

    SPECIAL PROCEDURAL REQUIREMENTS

	The program must be linked with tst_res.o.

    DETAILED DESCRIPTION

	**Setup**
	Set up unexpected signal handling.
	Set up one pipe for each process to be created with no blocking for read.
	
	**MAIN**
	If setup fails exit.
	Fork 2 children(1 & 2).
	Wait for set up complete messages from the 1st and 2nd child.
	Send the signal SIGUSR1 with pid equal to zero.
	Sleep a reasonable amount of time so that each child has been swapped in
	to process the signal.
	Now decide the outcome of the test items by reading from each pipe to find
	out if the child was interrupted by the signal and wrote to it.
	Remove the second child.
	Tell the first child it is time to remove it's child B because the decisions have been made.
	Exit.

	**First Child**
	Set to catch SIGUSR1 with an int_rout1.
	Set up to handle the message from the parent to remove child B.
	Fork two children(A & B).
	Wait for set up complete messages from child A & child B.
	Send a set up complete message to the parent.
	Pause until the signal SIGUSR1 comes in from the parent.
	Pause until the parent says it is time to remove the child.
	Exit.

	**Second Child**
	Set to catch SIGUSR1 with an int_rout2.
	Set the process group to be something different than the parents.
	Send a set up complete message to the parent.
	Pause until killed by parent because this child shouldn't receive signal SIGUSR1.

	**Child A**
	Set to catch SIGUSR1 with an int_routA.
	Send a set up complete message to the parent(First Child).
	Pause until the signal SIGUSR1 comes in from the parent.
	Exit.

	**Child B**
	Set to catch SIGUSR1 with an int_routB.
	Set the process group to be something different than the parents(First Child's).
	Send a set up complete message to the parent.
	Pause until killed by parent because this child shouldn't receive signal SIGUSR1.

	**usr1_rout-Used by all children**
	Write to the appropriate pipe that the signal SIGUSR1 was caught.

	**usr2_rout**
	Remove child B.

******************************************************************************/
#include <sys/param.h>
#include <signal.h>
#include <errno.h>   
#include <fcntl.h>
#include <string.h>
#include "test.h"    
#include "usctest.h"

#define MAXMESG 150		/*The maximum message that can be created.		*/
#define CHAR_SET_FAILED	"0"	/*Set up failing status transferred through the pipe.	*/
#define CHAR_SET_PASSED	"1"	/*Set up passing status transferred through the pipe.	*/
#define SIG_CAUGHT	"2"	/*Indicates that the signal SIGUSR1 was caught.		*/
#define SIG_RECEIVED	1	/*Integer value that indicates that the signal SIGUSR1	*/
				/*was caught.						*/
#define SIG_NOT_RECD	0	/*Integer value that indicates that the signal SIGUSR1	*/
				/*was caught.						*/
#define INT_SET_FAILED	0	/*Set up failing status transferred through the pipe.	*/
#define INT_SET_PASSED	1	/*Set up passing status transferred through the pipe.	*/
#define SLEEP_TIME	10	/*Amount of time the children get to catch the signal 	*/
#define TRUE	 	40	/*Child exits with this if execution was as	*/
				/*expected.						*/
#define FALSE	 	50	/*Child exits with this if it timed out waiting for the 	*/
				/*parents signal.					*/
#define TIMEOUT		60	/*Amount of time given in alarm calls.			*/
#define CHILD_EXIT(VAR) ((VAR >> 8) & 0377)  /*Exit value from the child.		*/
#define CHILD_SIG(VAR) (VAR & 0377) /*Signal value from the termination of child.	*/
				/*from the parent.					*/
#define SYS_FAIL	"The system call %s failed. Errno: %d, Error message: %s."

int pid1;			/*Return value from 1st fork. Global so that it can be	*/
				/*used in interrupt handling routines.			*/
int pid2;			/*Return value from 2nd fork. Global so that it can be	*/
				/*used in interrupt handling routines.			*/
int pidA;			/*Return value from 1st fork in child 1. Global so that it 	*/
				/*can be used in interrupt handling routines.		*/
int pidB;			/*Return value from 2nd fork in child 1. Global so that it 	*/
				/*can be used in interrupt handling routines.		*/
extern int errno;		/*Error number returned by a system call.		*/
int pipe1_fd[2];		/*Pipe file descriptors used for communication  	*/
				/*between child 1 and the 1st parent.			*/
int pipe2_fd[2];		/*Pipe file descriptors used for communication  	*/
				/*between child 2 and the 1st parent.			*/
int pipeA_fd[2];		/*Pipe file descriptors used for communication  	*/
				/*between child A and the 1st parent.			*/
int pipeB_fd[2];		/*Pipe file descriptors used for communication  	*/
				/*between child B and the 1st parent.			*/
char pipe_buf[10];		/*Pipe buffer.					 	*/
char buf_tmp1[2],buf_tmp2[2];	/*Temp hold for info read into pipe_buf.		*/
int read1_stat = 0;		/*Number of characters read from pipe 1.		*/
int read2_stat = 0;		/*Number of characters read from pipe 2.		*/
int readA_stat = 0;		/*Number of characters read from pipe A.		*/
int readB_stat = 0;		/*Number of characters read from pipe B.		*/
int alarm_flag = FALSE;		/*This flag indicates an alarm time out.			*/
char who_am_i = '0';		/*This indicates which process is which when using	*/
				/*interrupt routine usr1_rout.				*/

void notify_timeout();		/*Signal handler that the parent enters if it times out	*/
				/*waiting for the child to indicate its set up status.	*/
void parent_rout();		/*This is the parents routine.				*/
void child1_rout();		/*This is child 1's routine.				*/
void child2_rout();		/*This is child 2's routine.				*/
void childA_rout();		/*This is child A's routine.				*/
void childB_rout();		/*This is child B's routine.				*/
void usr1_rout();		/*This routine is used by all children to indicate that	*/
				/*they have caught signal SIGUSR1.			*/
void par_kill();		/*This routine is called by the original parent to	*/
				/*remove child 2 and to indicate to child 1 to 		*/
				/*remove its children.					*/
void chld1_kill();		/*This routine is used by child 1 to remove itself and	*/
				/*its children A and B.					*/

void setup();
void cleanup(void) __attribute__((noreturn));

const char *TCID="kill02";          /* Test program identifier.    */
int TST_TOTAL=2;                /* Total number of test cases. */
extern int Tst_count;           /* Test Case counter for tst_* routines */
extern int Tst_nobuf;           /* var. used to turn off tst_res buffering */

int exp_enos[]={0};             /* Array of expected errnos */

/***********************************************************************
 * MAIN
 ***********************************************************************/
int
main(int ac, char **av)
{
  char mesg[MAXMESG];	/* Holds messages to pass to tst_res.		 */

    int lc;             /* loop counter */
    const char *msg;	/* message returned from parse_opts */

    Tst_nobuf=1;

    /***************************************************************
     * parse standard options, and exit if there is an error
     * the -t and -f options not support yet.
     ***************************************************************/
    if ( (msg=parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *) NULL ) {
        tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
        tst_exit();
    }

    /***************************************************************
     * perform global setup for test
     ***************************************************************/
    setup();

    /***************************************************************
     * check looping state if -c option given
     ***************************************************************/
    for (lc=0; TEST_LOOPING(lc); lc++) {

        /* reset Tst_count in case we are looping. */
        Tst_count=0;

	if ((pid1 = fork()) > 0)
	{
	    /*
	     *  This is the parent, fork again to create child 2.
	     */
	     if ((pid2 = fork()) > 0) {
		/*
		 *  This is the parent.
		 */
		 (void) parent_rout();
	     }
	     else if (pid2 == 0) {
		/*
		 *  This is child 2.
		 */
		 (void) child2_rout();
	     }
	     else {
		/*
		 *  The second fork failed kill the first child.
		 */
		 if (kill(pid1,SIGKILL) == -1 && errno != ESRCH) {
			tst_resm(TWARN,"Child process may not have been killed.");
		 }
		 (void) sprintf(mesg,SYS_FAIL,"fork",errno,strerror(errno));
		 tst_brkm(TBROK,cleanup,mesg);
	    }

	}
	else if (pid1 == 0) {
	    /*
	     *  This is child 1.
	     */
	     (void) child1_rout();
	}
	else {
	    /*
	     * Fork failed.
	     */
	     (void) sprintf(mesg,SYS_FAIL,"fork",errno,strerror(errno)); 
	     tst_brkm(TBROK,cleanup,mesg);
	 }
   }

   cleanup();

   return 0;
} /* END OF MAIN.*/

/******************************************************************************
 *  This is the parents routine.  The parent waits for the children 1 and 2 to
 *  get set up. Then sends the signal and checks the outcome.
 *********************************************************************************/
void parent_rout()
{
	char mesg[MAXMESG];	/*Used to buffer messages for tst_resm.	*/

  /*
   *  Set to catch the alarm signal SIGALRM.
   */
   if (signal(SIGALRM,notify_timeout) == SIG_ERR) {
	(void) par_kill();
	tst_brkm(TBROK, NULL,
	    "Could not set to catch the parents time out alarm.");
	tst_exit();
   }

  /*
   *  Setting to catch the timeout alarm worked now let the children start up.
   *  Set an alarm which causes a time out on the read pipe loop.
   *  The children will notify the parent that set up is complete
   *  and the pass/fail status of set up.
   */
   (void) alarm(TIMEOUT);

   while ((read(pipe1_fd[0],pipe_buf,1) != 1) && (alarm_flag == FALSE))
	/*EMPTY*/;
   strncpy(buf_tmp1,pipe_buf,1);
   (void) alarm(TIMEOUT);

   while ((read(pipe2_fd[0],pipe_buf,1) != 1) && (alarm_flag == FALSE))
	/*EMPTY*/;
   (void) alarm(0);	/*Reset the alarm clock.*/
   strncpy(buf_tmp2,pipe_buf,1);

  /*
   *  Check the alarm flag.
   */
   if (alarm_flag == TRUE) {
	 tst_brkm(TBROK,NULL,"The set up of the children failed by timing out.");
	 (void) par_kill();
	 cleanup();
   }

  /*
   *  Check to see if either child failed in the set up.
   */
   if ( (strncmp(buf_tmp1,CHAR_SET_FAILED,1) == 0) ||
	(strncmp(buf_tmp2,CHAR_SET_FAILED,1) == 0)) {
	/*
	 * Problems were encountered in the set up of one of the children.
	 * The error message has been displayed by the child.
	 */
	 (void) par_kill();
	 cleanup();
   }

  /*
   *  Setup passed, now send SIGUSR1 to process id of zero.
   */
   TEST( kill(0,SIGUSR1) );
  
   if ( TEST_RETURN == -1)
   {
	(void) sprintf(mesg,SYS_FAIL,"kill",errno,strerror(errno));
	tst_brkm(TBROK,NULL,mesg);
	(void) par_kill();
	cleanup();
   }

  /*
   *  Sleep for a while to allow the children to get a chance to 
   *  catch the signal.
   */
   (void) sleep(SLEEP_TIME);
	
  /*
   *  The signal was sent above and time has run out for child response,
   *  check the outcomes.
   */
   read1_stat = read(pipe1_fd[0],pipe_buf,1);
   if (read1_stat == -1 && errno == EAGAIN) read1_stat = 0;
   read2_stat = read(pipe2_fd[0],pipe_buf,1);
   if (read2_stat == -1 && errno == EAGAIN) read2_stat = 0;
   readA_stat = read(pipeA_fd[0],pipe_buf,1);
   if (readA_stat == -1 && errno == EAGAIN) readA_stat = 0;
   readB_stat = read(pipeB_fd[0],pipe_buf,1);
   if (readB_stat == -1 && errno == EAGAIN) readB_stat = 0;

   if (read1_stat == -1 || read2_stat == -1 || 
		   readA_stat == -1 || readB_stat == -1) {
	/*
	 * The read system call failed.
	 */
	 (void) sprintf(mesg,SYS_FAIL,"read",errno,strerror(errno));
	 tst_brkm(TBROK,NULL,mesg);
	 (void) par_kill();
	 cleanup();
   }

   /*
    * Check the processes that were supposed to get the signal.
    */
    if (read1_stat == SIG_RECEIVED) {
	if (readA_stat == SIG_RECEIVED) {
	    /*
	     *  Both processes, 1 and A, that were supposed to receive
	     *  the signal did receive the signal.
	     */
	    if ( STD_FUNCTIONAL_TEST )
	        tst_resm(TPASS,
		    "The signal was sent to all processes in the process group.");
	    else
		Tst_count++;
	}
	else { /*Process A didn't receive the signal.*/
	    tst_resm(TFAIL,"Process A did not receive the signal.");
	}

    }
    else { /*Process 1 didn't receive the signal.*/
	tst_resm(TFAIL,"Process 1 did not receive the signal.");
    }

   /*
    * Check the processes that were not supposed to get the signal.
    */
    if (read2_stat == SIG_NOT_RECD) {
	if (readB_stat == SIG_NOT_RECD) {
	    /*
	     *  Both processes, 2 and B did not receive the signal.
	     */
	    if ( STD_FUNCTIONAL_TEST )
	        tst_resm(TPASS,
		    "The signal was not sent to selective processes that were not in the process group.");
	    else
		Tst_count++;
	}
	else { /*Process B received the signal.*/
	    tst_resm(TFAIL,"Process B received the signal.");
	}

   }

   else /*Process 2 received the signal.*/
   {
	 tst_resm(TFAIL,"Process 2 received the signal.");
   }

   (void) par_kill();
  return;	
}  /*End of parent_rout*/

/*******************************************************************************
 * This is child 1's routine.  It creates children A & B, checks their set up,
 * reports to the parent set up pass/fail info., then waits for
 * the parents signal.
 ******************************************************************************/
void child1_rout()
{
	char mesg[MAXMESG];	/*Used to buffer messages for tst_resm.	*/
   who_am_i = '1';

  /*
   *  Set to catch the SIGUSR1 with int1_rout.
   */
   if (signal(SIGUSR1,usr1_rout) == SIG_ERR)
   {
	tst_brkm(TBROK,NULL,"Could not set to catch the childrens signal.");
	(void) write(pipe1_fd[1],CHAR_SET_FAILED,1);
	exit(0);
   }

  /*
   *  Create children A & B.
   */
   if ((pidA = fork()) > 0) {
	/*
	 *  This is the parent(child1), fork again to create child B.
	 */
	 if ((pidB = fork()) == 0) {
	     /* This is child B. */
	     (void) childB_rout();
	 }

	 else if (pidB == -1) {
		/*
		 *  The fork of child B failed kill child A.
		 */
		 if (kill(pidA,SIGKILL) == -1)
			tst_resm(TWARN,"Child process may not have been killed.");
		 (void) sprintf(mesg,SYS_FAIL,"fork",errno,strerror(errno)); 
		 tst_brkm(TBROK,NULL,mesg);
		 (void) write(pipe2_fd[1],CHAR_SET_FAILED,1);
		 exit(0);
	 }
   }

   else if (pidA == 0) {
	/* This is child A. */
	(void) childA_rout();

   }

   else if (pidA == -1) {
	/*
	 *  The fork of child A failed.
	 */
	 (void) sprintf(mesg,SYS_FAIL,"fork",errno,strerror(errno)); 
	 tst_brkm(TBROK,NULL,mesg);
	 (void) write(pipe1_fd[1],CHAR_SET_FAILED,1);
	 exit(0);
   }

  /*
   *  Set to catch the SIGUSR2 with chld1_kill.
   */
   if (signal(SIGUSR2,chld1_kill) == SIG_ERR) {
	tst_brkm(TBROK,NULL,"Could not set to catch the parents signal.");
	(void) write(pipe1_fd[1],CHAR_SET_FAILED,1);
	(void) chld1_kill();
	exit(0);
   }

  /*
   *  Set to catch the alarm signal SIGALRM.
   */
   if (signal(SIGALRM,notify_timeout) == SIG_ERR) {
	tst_brkm(TBROK,NULL,"Could not set to catch the childs time out alarm.");
	(void) write(pipe1_fd[1],CHAR_SET_FAILED,1);
	(void) chld1_kill();
	exit(0);
   }

  /*
   *  Setting to catch the signals worked now let the children start up.
   *  Set an alarm which causes a time out on the pipe read loop.
   *  The children A & B will notify the parent(child1) that set up is complete
   *  and the pass/fail status of set up.
   */
   (void) alarm(TIMEOUT-40);

   while ((read(pipeA_fd[0],pipe_buf,1) != 1) && (alarm_flag == FALSE))
	/*EMPTY*/;
   (void) alarm(TIMEOUT-40);

   while ((read(pipeB_fd[0],pipe_buf,1) != 1) && (alarm_flag == FALSE))
	/*EMPTY*/;
   (void) alarm(0);	/*Reset the alarm clock.*/

  /*
   *  Check the alarm flag.
   */
   if (alarm_flag == TRUE) {
	tst_brkm(TBROK,NULL,"The set up of the children failed by timing out.");
	(void) chld1_kill();
	(void) write(pipe1_fd[1],CHAR_SET_FAILED,1);
	exit(0);
   }

  /*
   *  Send a set up complete message to the parent.
   */
   (void) write(pipe1_fd[1],CHAR_SET_PASSED,1);

  /*
   *  Pause until the signal SIGUSR1 or SIGUSR2 is sent from the parent.
   */
   (void) pause();

  /*
   *  Pause until signal SIGUSR2 is sent from the parent.
   *  This pause will only be executed if SIGUSR2 has not been received yet.
   */
   while (1) {
	sleep(1);
   }

} /*End of child1_rout*/

/*******************************************************************************
 *  This is the routine for child 2, which should not receive the parents signal.
 ******************************************************************************/
void child2_rout()
{
   who_am_i = '2';

  /*
   * Set the process group of this process to be different
   * than the other processes.
   */
   (void) setpgrp();

  /*
   *  Set to catch the SIGUSR1 with usr1_rout.
   */
   if (signal(SIGUSR1,usr1_rout) == SIG_ERR) {
	tst_brkm(TBROK,cleanup,"Could not set to catch the parents signal.");
	(void) write(pipe2_fd[1],CHAR_SET_FAILED,1);
	exit(0);
   }

  /* Send a set up complete message to parent.*/
   (void) write(pipe2_fd[1],CHAR_SET_PASSED,1);

  /*
   *  Pause until killed by the parent or SIGUSR1 is received.
   */
   (void) pause();
}


/*******************************************************************************
 *  This is the routine for child A, which should receive the parents signal.
 ******************************************************************************/
void childA_rout()
{
   who_am_i = 'A';

  /* Send a set up complete message to parent.*/
   write(pipeA_fd[1],CHAR_SET_PASSED,1);

  /*
   *  Pause until killed by the parent or SIGUSR1 is received.
   */
   (void) pause();

   exit(0);
} /*End of childA_rout*/

/*******************************************************************************
 *  This is the routine for child B, which should not receive the parents signal.
 ******************************************************************************/
void childB_rout()
{
   who_am_i = 'B';

  /*
   * Set the process group of this process to be different
   * than the other processes.
   */
   (void) setpgrp();

  /* Send a set up complete message to parent(child 1).*/
   write(pipeB_fd[1],CHAR_SET_PASSED,1);

  /*
   *  Pause until killed by the parent(child 1) or SIGUSR1 is received.
   */
   (void) pause();

  exit(0);
}

/*******************************************************************************
 *  This routine sets up the interprocess communication pipes, signal handling,
 *  and process group information.
 ******************************************************************************/
void
setup()
{
	int errno_buf = 0;	/*indicates the errno if pipe set up fails.		*/
	int err_flag = FALSE;	/*Indicates if an error has occurred in pipe set up.	*/
	char mesg[MAXMESG];	/*Used to buffer messages for tst_res.			*/

  /*
   *  Set the process group ID to be equal between the parent and children.
   */
   (void) setpgrp();

  /*
   *  Set to catch unexpected signals.
   *  SIGCLD is set to be ignored because we do not wait for termination status.
   *  SIGUSR1 is set to be ignored because this is the signal we are using for
   *  the test and we are not concerned with the parent getting it.
   */
    /* capture signals */
    tst_sig(FORK, DEF_HANDLER, cleanup);

	if (signal(SIGUSR1,SIG_IGN) == SIG_ERR) {
	    (void) sprintf(mesg,
		"Setting to ignore signal SIGCLD failed. Errno: %d, Error message %s.",
		errno,strerror(errno));
	    tst_brkm(TBROK,NULL,mesg);
	    tst_exit();
	}

	if (signal(SIGCLD,SIG_IGN) == SIG_ERR) {
	    (void) sprintf(mesg,
		"Setting to ignore signal SIGCLD failed. Errno: %d, Error message %s.",
		errno,strerror(errno));
	    tst_brkm(TBROK,NULL,mesg);
	    tst_exit();
	}

    /* Indicate which errnos are expected */
    TEST_EXP_ENOS(exp_enos);

    /* Pause if that option was specified */
    TEST_PAUSE;


  /*
   *  Set up pipe1, pipe2, pipeA, and pipeB.
   */
   if ((pipe(pipe1_fd) == -1) || (fcntl(pipe1_fd[0],F_SETFL,O_NDELAY) == -1))
   {
	errno_buf = errno;
	err_flag = TRUE;
   }

   if ((pipe(pipe2_fd) == -1) || (fcntl(pipe2_fd[0],F_SETFL,O_NDELAY) == -1))
   {
	errno_buf = errno;
	err_flag = TRUE;
   }

   if ((pipe(pipeA_fd) == -1) || (fcntl(pipeA_fd[0],F_SETFL,O_NDELAY) == -1))
   {
	errno_buf = errno;
	err_flag = TRUE;
   }

   if ((pipe(pipeB_fd) == -1) || (fcntl(pipeB_fd[0],F_SETFL,O_NDELAY) == -1))
   {
	errno_buf = errno;
	err_flag = TRUE;
   }

  /*
   *  Check for errors.
   */
   if (err_flag == TRUE)
   {
	(void) sprintf(mesg,SYS_FAIL,"pipe",errno_buf,strerror(errno_buf));
	tst_brkm(TBROK, NULL, mesg);
	tst_exit();
   }
   return;

}

/***********************************************************
 *  This routine indicates that the process caught SIGUSR1.
 **********************************************************/
void usr1_rout()
{
	char mesg[MAXMESG];	/*Used to buffer messages for tst_res.	*/

   switch (who_am_i)
   {
	case '1': if (write(pipe1_fd[1],SIG_CAUGHT,1) == -1)
			tst_resm(TWARN,"Writing signal catching status failed in child 1.");
		  break;
	case '2': if (write(pipe2_fd[1],SIG_CAUGHT,1) == -1)
			tst_resm(TWARN,"Writing signal catching status failed in child 2.");
		  break;
	case 'A': if (write(pipeA_fd[1],SIG_CAUGHT,1) == -1)
			tst_resm(TWARN,"Writing signal catching status failed in child A.");
		  break;
	case 'B': if (write(pipeB_fd[1],SIG_CAUGHT,1) == -1)
			tst_resm(TWARN,"Writing signal catching status failed in child B.");
		  break;
	default:
		  sprintf(mesg,"Unexpected value %d for who_am_i in usr1_rout().",who_am_i);
		  tst_resm(TWARN,mesg);
		  break;
   }

} /*End of usr1_rout*/


/***********************************************************
 *  This routine handles the timeout alarm in the parent,
 *  which occurs when the child fails to notify the parent
 *  the status of set up.
 **********************************************************/
void notify_timeout()
{
  alarm_flag = TRUE;

} /*End of notify_timeout*/

/***********************************************************
 *  This routine handles the procedure for removing the 
 *  children forked off during this test.
 **********************************************************/
void par_kill()
{
	char mesg[MAXMESG];	/*Used to buffer messages for tst_res.	*/
  /*
   *  Indicate to child1 that it can remove it's children and itself now.
   */
   if (kill(pid1,SIGUSR2) == -1 && errno != ESRCH)
   {
	(void) sprintf(mesg,SYS_FAIL,"kill",errno,strerror(errno));
	tst_resm(TWARN,mesg);
	tst_resm(TWARN,"Child 1 and it's children may still be alive.");
   }

  /*
   *  Remove child 2.
   */
   if (kill(pid2,SIGKILL) == -1 && errno != ESRCH)
	tst_resm(TWARN,"Child2 may still be alive.");

  return;
	
} /*End of par_kill*/


/*********************************************************************
 *  This routine is executed by child 1 when the parent tells it to
 *  remove it's children and itself.
 ********************************************************************/
void chld1_kill()
{
	char mesg[MAXMESG];	/*Used to buffer messages for tst_resm.	*/

  /*
   *  Remove children A & B.
   */
   if (kill(pidA,SIGKILL) == -1 && errno != ESRCH)
   {
	(void) sprintf(mesg,
		"The system call kill failed. Child 1's(A) child may still be alive. Errno: %d, Error message %s.",
		errno,strerror(errno));
	tst_resm(TWARN,mesg);
   }
   if (kill(pidB,SIGKILL) == -1 && errno != ESRCH)
   {
	(void) sprintf(mesg,
		"The system call kill failed. Child 1's(B) child may still be alive. Errno: %d, Error message %s.",
		errno,strerror(errno));
	tst_resm(TWARN,mesg);
   }

  exit(0);

} /*End of chld1_kill*/

/***************************************************************
 * cleanup() - performs all ONE TIME cleanup for this test at
 *              completion or premature exit.
 ***************************************************************/
void
cleanup()
{
    /*
     * print timing stats if that option was specified.
     * print errno log if that option was specified.
     */
    TEST_CLEANUP;

    /* exit with return code appropriate for results */
    tst_exit();

}       /* End cleanup() */