1 #include <common.h>
2 
3 int
4 main(void) {
5 	sem_t *id;
6 
7 	/*
8 	 * Create named semaphore with value of 1 and then unlink it
9 	 * while still retaining the initial reference.
10 	 */
11 	id = sem_open(TEST_PATH, O_CREAT | O_EXCL, 0777, 1);
12 	if (id == SEM_FAILED) {
13 		perror("sem_open(O_CREAT | O_EXCL)");
14 		return 1;
15 	}
16 	if (sem_unlink(TEST_PATH) < 0) {
17 		perror("sem_unlink");
18 		sem_close(id);
19 		return 1;
20 	}
21 	if (checkvalue(id, 1) < 0) {
22 		sem_close(id);
23 		return 1;
24 	}
25 
26 	/* Post the semaphore to set its value to 2. */
27 	if (sem_post(id) < 0) {
28 		perror("sem_post");
29 		sem_close(id);
30 		return 1;
31 	}
32 	if (checkvalue(id, 2) < 0) {
33 		sem_close(id);
34 		return 1;
35 	}
36 
37 	/* Wait on the semaphore which should set its value to 1. */
38 	if (sem_wait(id) < 0) {
39 		perror("sem_wait");
40 		sem_close(id);
41 		return 1;
42 	}
43 	if (checkvalue(id, 1) < 0) {
44 		sem_close(id);
45 		return 1;
46 	}
47 
48 	if (sem_close(id) < 0) {
49 		perror("sem_close");
50 		return 1;
51 	}
52 	return 0;
53 }
54