1/*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license.  For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7
8 * Test that clock_nanosleep() causes the current thread to be suspended
9 * until a signal whose action is to invoke a signal handling function
10 * is received for an absolute nanosleep (same as 1-3.c with
11 * TIMER_ABSTIME set).
12 *
13 * Note:  This test case has some limitations:
14 * - clock_nanosleep() could not be executed at all, and the test would
15 *   pass [Hopefully, the sleep() in the parent ensures clock_nanosleep()
16 *   will be executed.]
17 * - There is no way of knowing for sure that it was the signal that
18 *   stopped clock_nanosleep().
19 */
20#include <stdio.h>
21#include <time.h>
22#include <signal.h>
23#include <unistd.h>
24#include <sys/wait.h>
25#include "posixtest.h"
26
27#define SLEEPSEC 30
28
29void handler(int signo)
30{
31	printf("In handler\n");
32}
33
34
35int main(int argc, char *argv[])
36{
37	struct timespec tssleep, tsbefore, tsafter;
38	int pid, sleepuntilsec;
39	struct sigaction act;
40
41	if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
42		perror("clock_gettime() did not return success\n");
43		return PTS_UNRESOLVED;
44	}
45
46	sleepuntilsec = tsbefore.tv_sec + SLEEPSEC;
47
48	if ((pid = fork()) == 0) {
49		/* child here */
50		int flags = 0;
51
52		act.sa_handler=handler;
53		act.sa_flags=0;
54		if (sigemptyset(&act.sa_mask) != 0) {
55			perror("sigemptyset() did not return success\n");
56			return PTS_UNRESOLVED;
57		}
58		if (sigaction(SIGABRT, &act, 0) != 0) {
59			perror("sigaction() did not return success\n");
60			return PTS_UNRESOLVED;
61		}
62
63		tssleep.tv_sec=sleepuntilsec;
64		tssleep.tv_nsec=tsbefore.tv_nsec;
65
66		flags |= TIMER_ABSTIME;
67		clock_nanosleep(CLOCK_REALTIME, flags, &tssleep, NULL);
68	} else {
69		/* parent here */
70		int i;
71
72		sleep(1);
73
74		if (kill(pid, SIGABRT) != 0) {
75			printf("Could not raise signal being tested\n");
76			return PTS_UNRESOLVED;
77		}
78
79		if (wait(&i) == -1) {
80			perror("Error waiting for child to exit\n");
81			return PTS_UNRESOLVED;
82		}
83		if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) {
84			perror("clock_gettime() did not return success\n");
85			return PTS_UNRESOLVED;
86		}
87
88		/*
89		 * pass if we slept for less than the (large) sleep time
90		 * allotted
91		 */
92
93		if ( tsafter.tv_sec < sleepuntilsec) {
94			printf("Test PASSED\n");
95			return PTS_PASS;
96		} else {
97			printf("Slept for too long: %d >= %d\n",
98					(int) tsafter.tv_sec,
99					sleepuntilsec);
100			printf("Test FAILED\n");
101			return PTS_FAIL;
102		}
103	}
104
105	return PTS_UNRESOLVED;
106}
107