• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
main(void)24 int main(void)
25 {
26 	sigset_t newmask, pendingset;
27 	int sig;
28 
29 	struct timeval tv_ref, tv_cur;
30 
31 	/* Empty set of blocked signals */
32 
33 	if (sigemptyset(&newmask) == -1 || sigemptyset(&pendingset) == -1) {
34 		printf("Error in sigemptyset()\n");
35 		return PTS_UNRESOLVED;
36 	}
37 
38 	/* Add SIGALRM to the set of blocked signals */
39 	if (sigaddset(&newmask, SIGALRM) == -1) {
40 		perror("Error in sigaddset()\n");
41 		return PTS_UNRESOLVED;
42 	}
43 
44 	/* Block SIGALRM */
45 	if (sigprocmask(SIG_SETMASK, &newmask, NULL) == -1) {
46 		printf("Error in sigprocmask()\n");
47 		return PTS_UNRESOLVED;
48 	}
49 
50 	/* Read clock */
51 	if (gettimeofday(&tv_ref, NULL) != 0) {
52 		printf("Failed to get time of day");
53 		return PTS_UNRESOLVED;
54 	}
55 
56 	/* SIGALRM will be sent in 5 seconds */
57 	alarm(3);
58 
59 	/* Call sigwait.  It should wait for 5 seconds and then move
60 	 * along the rest of the process when it received the SIGALRM */
61 	if (sigwait(&newmask, &sig) != 0) {
62 		printf("Error in sigwait()\n");
63 		return PTS_UNRESOLVED;
64 	}
65 
66 	/* Re-read clock */
67 	if (gettimeofday(&tv_cur, NULL) != 0) {
68 		printf("Failed to get time of day");
69 		return PTS_UNRESOLVED;
70 	}
71 
72 	/* Check the operation was blocking until the signal was generated */
73 	if (tv_cur.tv_sec - tv_ref.tv_sec < 2) {
74 		printf("The operation lasted less than 3 seconds!\n");
75 		return PTS_FAIL;
76 	}
77 
78 	/* If we get here, then the process was suspended until
79 	 * SIGALRM was raised.  */
80 	printf("Test PASSED\n");
81 
82 	return PTS_PASS;
83 
84 }
85