1 /*
2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
3  * Created by:  majid.awad 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 
9 /*
10  * This test case will verify that sem_timedwait will wait one second to
11  * unlock the locked semaphore, and then when the semaphore fails to
12  * unlock should return -1, and leave the semaphore status unchanged
13  * by doing sem_post on the sempahore and check the current value of
14  * the semaphore.
15  */
16 
17 #define _XOPEN_SOURCE 600
18 
19 #include <stdio.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <semaphore.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <time.h>
26 #include "posixtest.h"
27 
28 
29 #define TEST "2-2"
30 #define FUNCTION "sem_timedwait"
31 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
32 
33 
34 
main()35 int main() {
36 	sem_t mysemp;
37 	struct timespec ts;
38 	int sts, val;
39 
40 
41         if ( sem_init (&mysemp, 0, 0) == -1 ) {
42                 perror(ERROR_PREFIX "sem_init");
43                 return PTS_UNRESOLVED;
44         }
45 
46 	ts.tv_sec=time(NULL)+1;
47         ts.tv_nsec=0;
48 
49 	/* Try to lock Semaphore */
50 	sts = sem_timedwait(&mysemp, &ts);
51 
52 	if ( sem_post(&mysemp) == -1 ) {
53 		perror(ERROR_PREFIX "sem_post");
54 		return PTS_UNRESOLVED;
55 	}
56 
57         if( sem_getvalue(&mysemp, &val) == -1 ) {
58                 perror(ERROR_PREFIX "sem_getvalue");
59                 return PTS_UNRESOLVED;
60         }
61 
62        if ((val == 1) && (sts == -1)) {
63                  puts("TEST PASSED");
64                  sem_destroy(&mysemp);
65                  return PTS_PASS;
66        } else {
67                  puts("TEST FAILED");
68                  return PTS_FAIL;
69        }
70 }
71