1 #include "tests.h"
2 #include <sys/ipc.h>
3 #include <sys/sem.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include "xlat.h"
10 #include "xlat/semop_flags.h"
11
12 union semun {
13 int val;
14 struct semid_ds *buf;
15 unsigned short *array;
16 struct seminfo *__buf;
17 };
18
19 static int id = -1;
20
21 static void
cleanup(void)22 cleanup(void)
23 {
24 semctl(id, 0, IPC_RMID, 0);
25 id = -1;
26 }
27
28 int
main(void)29 main(void)
30 {
31 static const int bogus_semid = 0xfdb97531;
32 static void * const bogus_sops = (void *) -1L;
33 static const size_t bogus_nsops = (size_t) 0xdefaceddeadbeefULL;
34
35 TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, ts);
36 int rc;
37
38 id = semget(IPC_PRIVATE, 1, 0600);
39 if (id < 0)
40 perror_msg_and_skip("semget");
41 atexit(cleanup);
42
43 union semun sem_union = { .val = 0 };
44 if (semctl(id, 0, SETVAL, sem_union) == -1)
45 perror_msg_and_skip("semctl");
46
47 TAIL_ALLOC_OBJECT_CONST_PTR(struct sembuf, sem_b);
48 TAIL_ALLOC_OBJECT_CONST_PTR(struct sembuf, sem_b2);
49
50 rc = semop(bogus_semid, NULL, bogus_nsops);
51 printf("semop(%d, NULL, %u) = %s\n",
52 bogus_semid, (unsigned) bogus_nsops, sprintrc(rc));
53
54 rc = semop(bogus_semid, bogus_sops, 1);
55 printf("semop(%d, %p, %u) = %s\n",
56 bogus_semid, bogus_sops, 1, sprintrc(rc));
57
58 sem_b->sem_num = 0;
59 sem_b->sem_op = 1;
60 sem_b->sem_flg = SEM_UNDO;
61
62 sem_b2->sem_num = 0xface;
63 sem_b2->sem_op = 0xf00d;
64 sem_b2->sem_flg = 0xbeef;
65
66 rc = semop(bogus_semid, sem_b2, 2);
67 printf("semop(%d, [{%hu, %hd, %s%s%#hx}, ... /* %p */], %u) = %s\n",
68 bogus_semid, sem_b2->sem_num, sem_b2->sem_op,
69 sem_b2->sem_flg & SEM_UNDO ? "SEM_UNDO|" : "",
70 sem_b2->sem_flg & IPC_NOWAIT ? "IPC_NOWAIT|" : "",
71 (short) (sem_b2->sem_flg & ~(SEM_UNDO | IPC_NOWAIT)),
72 sem_b2 + 1, 2, sprintrc(rc));
73
74 if (semop(id, sem_b, 1))
75 perror_msg_and_skip("semop, 1");
76 printf("semop(%d, [{0, 1, SEM_UNDO}], 1) = 0\n", id);
77
78 sem_b->sem_op = -1;
79 if (semop(id, sem_b, 1))
80 perror_msg_and_skip("semop, -1");
81 printf("semop(%d, [{0, -1, SEM_UNDO}], 1) = 0\n", id);
82
83 rc = semtimedop(bogus_semid, NULL, bogus_nsops, NULL);
84 printf("semtimedop(%d, NULL, %u, NULL) = %s\n",
85 bogus_semid, (unsigned) bogus_nsops, sprintrc(rc));
86
87 rc = semtimedop(id, sem_b + 1, 1, ts + 1);
88 printf("semtimedop(%d, %p, 1, %p) = %s\n",
89 id, sem_b + 1, ts + 1, sprintrc(rc));
90
91 ts->tv_sec = 1;
92 ts->tv_nsec = 123456789;
93 rc = semtimedop(bogus_semid, sem_b2, 2, ts);
94 printf("semtimedop(%d, [{%hu, %hd, %s%s%#hx}, ... /* %p */], %u"
95 ", {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
96 bogus_semid, sem_b2->sem_num, sem_b2->sem_op,
97 sem_b2->sem_flg & SEM_UNDO ? "SEM_UNDO|" : "",
98 sem_b2->sem_flg & IPC_NOWAIT ? "IPC_NOWAIT|" : "",
99 (short) (sem_b2->sem_flg & ~(SEM_UNDO | IPC_NOWAIT)),
100 sem_b2 + 1, 2,
101 (long long) ts->tv_sec, zero_extend_signed_to_ull(ts->tv_nsec),
102 sprintrc(rc));
103
104 sem_b->sem_op = 1;
105 if (semtimedop(id, sem_b, 1, NULL))
106 perror_msg_and_skip("semtimedop, 1");
107 printf("semtimedop(%d, [{0, 1, SEM_UNDO}], 1, NULL) = 0\n", id);
108
109 sem_b->sem_op = -1;
110 if (semtimedop(id, sem_b, 1, ts))
111 perror_msg_and_skip("semtimedop, -1");
112 printf("semtimedop(%d, [{0, -1, SEM_UNDO}], 1"
113 ", {tv_sec=%lld, tv_nsec=%llu}) = 0\n", id,
114 (long long) ts->tv_sec, zero_extend_signed_to_ull(ts->tv_nsec));
115
116 sem_b->sem_op = 1;
117 ts->tv_sec = 0xdeadbeefU;
118 ts->tv_nsec = 0xfacefeedU;
119 rc = semtimedop(id, sem_b, 1, ts);
120 printf("semtimedop(%d, [{0, 1, SEM_UNDO}], 1"
121 ", {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
122 id, (long long) ts->tv_sec,
123 zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
124
125 sem_b->sem_op = -1;
126 ts->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
127 ts->tv_nsec = (long) 0xbadc0dedfacefeedLL;
128 rc = semtimedop(id, sem_b, 1, ts);
129 printf("semtimedop(%d, [{0, -1, SEM_UNDO}], 1"
130 ", {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
131 id, (long long) ts->tv_sec,
132 zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
133
134 puts("+++ exited with 0 +++");
135 return 0;
136 }
137