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

signal03.c « ltp « winsup.api « testsuite « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2a0923ca2664747633d7d26b300910e9e1a7dfdd (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
/*
 * 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.  Eagan, Minnesota
 * 
 * TEST IDENTIFIER :  signal03  Boundary value and other invalid value checking
 * 				 of signal setup and signal sending.
 * 
 * PARENT DOCUMENT :  sgntds01  Signal System Call
 * 		
 * AUTHOR           :  Dave Baumgartner
 * 		    :  Rewrote 12/92 by Richard Logan
 * 
 * CO-PILOT         :  Barrie Kletscher
 * 
 * DATE STARTED     :  10/17/85
 * 
 * TEST ITEMS
 * 
 * 	1. SIGKILL can not be set to be caught, errno:EINVAL (POSIX).
 * 	2. SIGKILL can not be caught.
 * 	3. SIGKILL can not be set to be ignored, errno:EINVAL (POSIX).
 * 	4. SIGKILL can not be ignored.
 * 	5. SIGKILL can not be reset to default, errno:EINVAL (POSIX.
 * 
 * ENVIRONMENTAL NEEDS
 * 
 * 	NONE
 * 
 * SPECIAL PROCEDURAL REQUIREMENTS
 * 
 * 	None
 * 
 * INTERCASE DEPENDENCIES
 * 
 * 	2 depends on 1 and 4 on 3.
 * 
 * DETAILED DESCRIPTION
 * 
 * 	main()
 * 	Call catch_test to test setup and catching of SIGKILL.
 * 
 * 
 * 	Call ignore_test to test setup and ignoring of SIGKILL.
 * 
 * 
 * 	Call sigdfl_test to test setting SIGKILL to default.
 * 
 * 	* END OF MAIN *
 * 
 * 
 * 	catch_test()
 * 
 * 	fork a child
 * 	if this is the parent
 * 		sleep to let child start.
 * 		send sig to child.
 * 		wait for the child to terminate.
 * 
 * 		if the termination status of the child equals the signal sent to it
 * 			Test item 1 PASSED the child was killed.
 * 		else if status equals the exit value of SIG_CAUGHT
 * 			Test item 2 FAILED sig was caught.
 * 		else 
 * 			Test item 2 FAILED because the child was not killed
 * 			but sig was not caught either.
 * 
 * 	else this the child
 * 		set exit_val to SIG_NOT_CAUGHT.
 * 		set to catch sig, where the interrupt routine just sets 
 * 		exit_val to SIG_CAUGHT.
 * 
 * 		If the return value and errno, after trying to set to catch sig,
 * 		do not indicate that an error has occurred.
 * 			Test item 1 FAILED bad return, return value:X, errno:X.
 * 		else
 * 			Test item 1 PASSED sig was not set to be caught.
 * 
 * 		pause until the parent sends a signal.
 * 		The child should be killed by the signal but if not exit
 * 		with exit_val.
 * 
 * 	* End of catch_test. *
 * 
 * 
 * 	ignore_test()
 * 
 * 	fork a child
 * 	if this is the parent
 * 		sleep to let child start.
 * 		send SIGKILL to child.
 * 		wait for the child to terminate.
 * 
 * 		if the termination status of the child equals SIGKILL
 * 			Test item 4 PASSED the child was killed.
 * 		else if the status equals the exit value of SIG_IGNORED
 * 			Test item 4 FAILED SIGKILL was ignored.
 * 
 * 	else this the child
 * 
 * 		If the return value and errno, after trying to set to ignore SIGKILL,
 * 		do not indicate that an error has occurred.
 * 			Test item 3 FAILED bad return, return value:X, errno:X.
 * 		else
 * 			Test item 3 PASSED SIGKILL was not set to be ignored.
 * 
 * 		pause until the parent sends SIGKILL.
 * 		The child should be killed by the signal but if not exit
 * 		with SIG_IGNORED.
 * 
 * 	* End of ignore_test. *
 * 
 * 
 * 	sigdfl_test()
 * 
 * 	If the return value and errno, after trying to set to SIGKILL to default,
 * 	do not indicate that an error has occurred.
 * 		Test item 5 FAILED bad return, return value:X, errno:X.
 * 	else
 * 		Test item 5 PASSED SIGKILL was not set to default.
 * 
 * 	* End of sigdfl_test. *
 * 
 * BUGS/NOTES
 * 	Since the system call under test is executed in the child, no
 *	timings on this system call will be reported.
 * 
***********************************************************************************/

#include <signal.h>
#include <errno.h>  
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>

#include "test.h"
#include "usctest.h"

void setup();
void cleanup(void) __attribute__((noreturn));
void do_test();
void sigdfl_test();
void update_timings();

#if defined(linux)
# define SIG_PF sig_t  /* This might need to be sighandler_t on some systems */
#elif defined(__CYGWIN__)
typedef void (*sighandler_t) (int);
#define SIG_PF sighandler_t
#endif

#define SIG_CAUGHT	1
#define SIG_NOT_CAUGHT	0
#define SIG_IGNORED	5
#define TIMED_OUT	99

#define TIMEOUT		20

#define GO_FLAG		1
#define ERROR_FLAG	2
#define PASS_FLAG	3
#define FAIL_FLAG	4

#define IGNORE_TEST	1
#define CATCH_TEST	2

#define MAXMESG 150	/* The Maximum message that can be created.	*/

int exit_val;		/* Global variable, used to tell whether the	*/
			/* child exited instead of being killed.	*/
extern int errno;	/* errno holds the error value returned by	*/
			/* a system call.                        	*/
char mesg[MAXMESG];	/* Holds messages to pass to tst_res.		*/ 

struct ipc_t {
    int status;
    char mesg[MAXMESG];
    struct tblock timings;
} Ipc_info;

const char *TCID = "signal03";
int TST_TOTAL = 5;
extern int Tst_count;           /* count of test items completed */

int Pid;		/* Return value from fork.			 */

long Tret;

/***********************************************************************
 *   M A I N
 ***********************************************************************/
int
main(argc, argv)
int argc;
char **argv;
{
    int lc;
    const char *msg;

    /***************************************************************
    * parse standard options
    ***************************************************************/
    if ( (msg=parse_opts(argc, argv, (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;

	errno=-4;

	/*
	 * Call catch_test to test setup and catching of SIGKILL.
	 */
	(void) do_test(CATCH_TEST, Tst_count);

	/*
	 * Call ignore_test to test setup and ignoring of SIGKILL.
	 */
	(void) do_test(IGNORE_TEST, Tst_count);

	/*
	 * Call sigdfl_test to test setting SIGKILL to default.
	 */
	(void) sigdfl_test();

    }

    cleanup();

    return 0;
} /*End of main*/

/***********************************************************************
 *
 ***********************************************************************/
void
do_test(test_case, tst_count)
int test_case;
int tst_count;
{
  int term_stat;	/* Termination status of the child returned to	 */
			/* the parent.					 */
  char string[30];
  int fd1[2];		/* ipc */
  int rd_sz;		/* size of read */
  void p_timeout_handler();
  void c_timeout_handler();
  void catchsig();

  Tst_count = tst_count;

  /*
   * Create a pipe of ipc
   */
  if ( pipe(fd1) == -1 ) {
	 sprintf(mesg,
	 "pipe system call failed, Errno: %d, Error message: %s",
			errno,strerror(errno)); 
	 tst_resm(TBROK,mesg);
	 tst_resm(TBROK,mesg);
	 return;
  }

  /*
   * Cause the read to return 0 once EOF is encountered and the
   * read to return -1 if pipe is empty.
   */

  if ( fcntl(fd1[0], F_SETFL, O_NONBLOCK) == -1 ) {
	sprintf(mesg, "fcntl(fd1[0], F_SETFL, O_NONBLOCK) failed: errno=%d",
	    errno);
	tst_resm(TBROK,mesg);
        tst_resm(TBROK,mesg);
	close(fd1[0]);
	close(fd1[1]);
	return;
  }

  if ((Pid = fork()) > 0) {	/* parent */

	signal(SIGALRM, p_timeout_handler);

	alarm(TIMEOUT);

	close(fd1[1]);      /* close write side */

	/*
	 * Deal with child's messages.
	 * Only the GO_FLAG status will allow parent to
	 * go on.  All pipe io will be in the ipc_t structure sizes 
	 * to avoid reading part of next message.
	 */
        while ( 1 ) {

	    while ( (rd_sz=read(fd1[0], (char *)&Ipc_info, sizeof(Ipc_info))) != 0 ) {
	        if ( rd_sz > 0 )
	            break;	/* read something */
	    }

	    if ( rd_sz == 0 ) {		/* if EOF encountered */
		sprintf(mesg, "child's pipe is closed before 'go' message received");
		tst_resm(TBROK, Ipc_info.mesg);
		tst_resm(TBROK, Ipc_info.mesg);
		close(fd1[0]);
	 	return;
	    }

	    else if ( Ipc_info.status == GO_FLAG ) {
	 	break;	/* go on */
	    }
	    else if ( Ipc_info.status == ERROR_FLAG ) {
		tst_resm(TBROK, "From child: %s", Ipc_info.mesg);
		tst_resm(TBROK, "From child: %s", Ipc_info.mesg);
		close(fd1[0]);
	 	return;
	    }
	    else if ( Ipc_info.status == PASS_FLAG ) {
		
		if ( STD_FUNCTIONAL_TEST )
		    tst_resm(TPASS, "From child: %s", Ipc_info.mesg);
		else
		    Tst_count++;
		update_timings(Ipc_info.timings);
	    }
	    else if ( Ipc_info.status == FAIL_FLAG ) {
		tst_resm(TFAIL, "From child: %s", Ipc_info.mesg);
		update_timings(Ipc_info.timings);
            }   
	    else {
		tst_resm(TINFO, "Unknown message from child: %s", mesg);
	    }
	}

	/*
	 * Send the signal SIGKILL to the child.
	 */
	 if (kill(Pid,SIGKILL) == -1)
	 {
		/*
		 * The kill system call failed.
		 */
	       	 sprintf(mesg,
		 "kill(Pid,SIGKILL) failed, Errno: %d, Error message: %s",
				errno,strerror(errno)); 
		 tst_resm(TBROK,mesg);
		 tst_resm(TBROK,mesg);
		 close(fd1[0]);
	 	 return;
	}

	/*
	 * Wait for the child to terminate and check the termination status.
	 */
	if (wait(&term_stat) == -1) {
		/*
		 * The wait system call failed.
		 */
	       	 sprintf(mesg,
		 "Wait system call failed. Errno: %d, Error message: %s",
				errno,strerror(errno)); 
		 tst_resm(TBROK,mesg);
		 tst_resm(TBROK,mesg);
		 close(fd1[0]);
	 	 return;
	}
	else if ( STD_FUNCTIONAL_TEST ) {
	    if ((term_stat & 0377) == SIGKILL) {
		/*
		 * The child was killed by the signal sent,
		 * which is correct.
		 */
		 tst_resm(TPASS,"The child was killed by SIGKILL.");
	    }
	    else if ( (term_stat >> 8) == TIMED_OUT ) {
	        sprintf(mesg, "child exited with a timed out exit status");
	        tst_resm(TBROK,mesg);
	    }
	    else {
	        if ((term_stat >> 8) == SIG_IGNORED && test_case == IGNORE_TEST ) {
		    sprintf(mesg, 
		        "SIGKILL was ignored by child after sent by parent.");
	        }
	        else if ((term_stat >> 8) == SIG_CAUGHT && test_case == CATCH_TEST ) {
		    sprintf(mesg,
		        "SIGKILL was caught by child after sent by parent.");
	        }
	        else {
	            sprintf(mesg,
		        "Child's termination status is unexpected. Status: %d (%#o).",
		        term_stat, term_stat);
	        }
	        tst_resm(TFAIL, mesg);
	    }
	}
	else {
	    Tst_count++;	/* increment test counter */
	}
	close(fd1[0]);

    }	/* End of parent. */
    else if (Pid == 0) {	
	/*
	 * This is the child.
	 * Set up to ignore/catch SIGKILL and check the return values.
	 */
	errno=0;
	if ( test_case == IGNORE_TEST ) {
	    exit_val = SIG_IGNORED;
	    strcpy(string, "signal(SIGKILL, SIG_IGN)");

	    Tret=(long)signal(SIGKILL, SIG_IGN);
	    TEST_ERRNO=errno;
	}
	else {
	    exit_val = SIG_NOT_CAUGHT;
	    strcpy(string, "signal(SIGKILL, catchsig)");
	    Tret=(long)signal(SIGKILL, catchsig);
	    TEST_ERRNO=errno;
	}
	Ipc_info.timings=tblock;	

	if ( (SIG_PF)Tret == SIG_ERR  ) {
	    if ( TEST_ERRNO == EINVAL ) {
	        sprintf(Ipc_info.mesg, "%s ret:%ld SIG_ERR (%ld) as expected",
		    string, Tret, (long)SIG_ERR);
		Ipc_info.status = PASS_FLAG;
	    }
	    else {
	        sprintf(Ipc_info.mesg,
		    "%s ret:%ld, errno:%d expected ret:%ld, errno:%d",
		    string, Tret, TEST_ERRNO, (long)SIG_ERR, EINVAL);
		Ipc_info.status = FAIL_FLAG;
	    }
		
	    write(fd1[1], (char *)&Ipc_info, sizeof(Ipc_info));
	}
	else {
	    /*
	     * The child was not allowed to set the signal to 
	     * be ignored and errno was correct.
	     */
	    sprintf(Ipc_info.mesg,
		"%s ret:%ld, errno:%d expected ret:%ld, errno:%d",
		string, Tret, TEST_ERRNO, (long)SIG_ERR, EINVAL);
	    Ipc_info.status = FAIL_FLAG;
	    write(fd1[1], (char *)&Ipc_info, sizeof(Ipc_info));
	}

	/*
	 * tell parent we are ready - setup by child is done
	 */
	Ipc_info.status = GO_FLAG;
	write(fd1[1], (char *)&Ipc_info, sizeof(Ipc_info));

	/*
	 * Set the alarm to wake up from the pause below if
	 * the parents signal is ignored.
	 */
	signal(SIGALRM, p_timeout_handler);
	alarm(TIMEOUT);

	/*
	 * Pause until the parent sends a signal or until alarm is received.
	 */
	pause();

	exit(exit_val);


    }	/* End of child. */
    else {
	/*
	 * The fork system call failed.
	 */
	 sprintf(mesg,
		"Fork system call failed. Errno: %d, Error message: %s",
			errno,strerror(errno)); 
	 tst_resm(TBROK,mesg);
	 tst_resm(TBROK,mesg);
 	 close(fd1[0]);
         close(fd1[1]);
	 return;
    }

} /* End of do_test. */

/***********************************************************************
 * sigdfl_test - test for attempt to set SIGKILL to default 
 ***********************************************************************/
void
sigdfl_test()
{
  /*
   * Try to set SIGKILL to default and check the return values.
   */
   errno=-4;

   Tret=(long)signal(SIGKILL,SIG_DFL);
   TEST_RETURN=Tret;
   TEST_ERRNO=errno;


    if ( (SIG_PF)TEST_RETURN == SIG_ERR  ) {
	if ( STD_FUNCTIONAL_TEST ) {
	    if ( TEST_ERRNO != EINVAL ) {
	        sprintf(mesg,
	            "signal(SIGKILL,SIG_DFL) ret:%d, errno:%d expected ret:-1, errno:%d",
	            TEST_RETURN, TEST_ERRNO, EINVAL);
	        tst_resm(TFAIL, mesg);
	    }
	    else {
	        sprintf(mesg,
	            "signal(SIGKILL,SIG_DFL) ret:%d, errno:%d as expected.",
	            TEST_RETURN, TEST_ERRNO);
	        tst_resm(TPASS, mesg);
	    }
	}
	else
	    Tst_count++;
    }
    else {
	sprintf(mesg,
	    "signal(SIGKILL,SIG_DFL) ret:%d, errno:%d expected ret:-1, errno:%d",
	    TEST_RETURN, TEST_ERRNO, EINVAL);
	tst_resm(TFAIL, mesg);
    }

}	/* End of sigdfl_test. */

/***************************************************************
 * setup() - performs all ONE TIME setup for this test.
 ***************************************************************/
void
setup()
{
    /* capture signals */
    tst_sig(FORK, DEF_HANDLER, cleanup);

    /* make and change to a temporary directory */
    tst_tmpdir();

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

}       /* End setup() */

/***************************************************************
 * 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;

    /*
     * remove the temporary directory and exit with
     * return code appropriate for results 
     */

    tst_rmdir();

    tst_exit();

}       /* End cleanup() */

/***********************************************************************
 *  Signal handler routine that used by the parent to handler
 *  a time out situation.  It will attempt to kill the child and
 *  call cleanup.
 ***********************************************************************/
void
p_timeout_handler()
{
    kill(Pid, SIGKILL);
    cleanup();
}

/***********************************************************************
 * Signal handler routine that used by the child to handle
 * a time out situation.  It will set a global varaible and return
 * if called.
 ***********************************************************************/
void
c_timeout_handler()
{
    exit_val = TIMED_OUT;
    return;
}

/***********************************************************************
 * This signal handling routine will set a global variable and return
 * if called.
 ***********************************************************************/
void
catchsig()
{
   exit_val = SIG_CAUGHT;
   return;
}

/***********************************************************************
 * Update timing information
 ***********************************************************************/
void
update_timings(atblock)
struct tblock atblock;
{
    tblock.tb_max += atblock.tb_max;
    tblock.tb_min += atblock.tb_min;
    tblock.tb_total += atblock.tb_total;
    tblock.tb_count += atblock.tb_count;
}