1 #include <signal.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/time.h>
5 #include "posixtest.h"
6 
7 /*
8 
9  * Copyright (c) 2002, Intel Corporation. All rights reserved.
10  * Created by:  rolla.n.selbak REMOVE-THIS AT intel DOT com
11  * This file is licensed under the GPL license.  For the full content
12  * of this license, see the COPYING file at the top level of this
13  * source tree.
14 
15  *  Test that the sigwait() function. If no signal in 'set' is pending at the
16  *  time of the call, the thread shall be suspended until one or more becomes
17  *  pending.
18  *  1)  Block a signal from delivery.
19  *  2)  Call sigwait()
20  *  3)  Raise the signal.
21  *  4)  Verify this process will return when the signal is sent.
22  */
23 
24 
main()25 int main()
26 {
27 	sigset_t newmask, pendingset;
28 	int sig;
29 
30 	struct timeval tv_ref, tv_cur;
31 
32 	/* Empty set of blocked signals */
33 
34 	if ( ( sigemptyset( &newmask ) == -1 ) ||
35 	        ( sigemptyset( &pendingset ) == -1 ) )
36 	{
37 		printf( "Error in sigemptyset()\n" );
38 		return PTS_UNRESOLVED;
39 	}
40 
41 	/* Add SIGALRM to the set of blocked signals */
42 	if ( sigaddset( &newmask, SIGALRM ) == -1 )
43 	{
44 		perror( "Error in sigaddset()\n" );
45 		return PTS_UNRESOLVED;
46 	}
47 
48 	/* Block SIGALRM */
49 	if ( sigprocmask( SIG_SETMASK, &newmask, NULL ) == -1 )
50 	{
51 		printf( "Error in sigprocmask()\n" );
52 		return PTS_UNRESOLVED;
53 	}
54 
55 	/* Read clock */
56 	if ( gettimeofday( &tv_ref, NULL ) != 0 )
57 	{
58 		printf( "Failed to get time of day" );
59 		return PTS_UNRESOLVED;
60 	}
61 
62 	/* SIGALRM will be sent in 5 seconds */
63 	alarm( 3 );
64 
65 	/* Call sigwait.  It should wait for 5 seconds and then move
66 	 * along the rest of the process when it received the SIGALRM */
67 	if ( sigwait( &newmask, &sig ) != 0 )
68 	{
69 		printf( "Error in sigwait()\n" );
70 		return PTS_UNRESOLVED;
71 	}
72 
73 	/* Re-read clock */
74 	if ( gettimeofday( &tv_cur, NULL ) != 0 )
75 	{
76 		printf( "Failed to get time of day" );
77 		return PTS_UNRESOLVED;
78 	}
79 
80 	/* Check the operation was blocking until the signal was generated */
81 	if ( tv_cur.tv_sec - tv_ref.tv_sec < 2 )
82 	{
83 		printf( "The operation lasted less than 3 seconds!\n" );
84 		return PTS_FAIL;
85 	}
86 
87 	/* If we get here, then the process was suspended until
88 	 * SIGALRM was raised.  */
89 	printf( "Test PASSED\n" );
90 
91 	return PTS_PASS;
92 
93 }
94