blob: 214ce3289b8ba4ff8a56e63e4b424d89811cc5a5 [file] [log] [blame]
Fei Jief22db4e2016-04-18 15:10:53 +08001#include "tests.h"
2#include <sys/types.h>
3#include <sys/ipc.h>
4#include <sys/sem.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8
9union semun
10{
11 int val;
12 struct semid_ds *buf;
13 unsigned short *array;
14 struct seminfo *__buf;
15};
16
17static int id = -1;
18
19static void
20cleanup(void)
21{
22 semctl(id, 0, IPC_RMID, 0);
23 id = -1;
24}
25
26int
27main(void)
28{
29 id = semget(IPC_PRIVATE, 1, 0600);
30 if (id < 0)
31 perror_msg_and_skip("semget");
32 atexit(cleanup);
33
34 union semun sem_union = { .val = 0 };
35 if (semctl(id, 0, SETVAL, sem_union) == -1)
36 perror_msg_and_skip("semctl");
37
38 struct sembuf *const sem_b = tail_alloc(sizeof(*sem_b));
39 sem_b->sem_num = 0;
40 sem_b->sem_op = 1;
41 sem_b->sem_flg = SEM_UNDO;
42
43 if (semop(id, sem_b, 1))
44 perror_msg_and_skip("semop, 1");
45 printf("semop(%d, [{0, 1, SEM_UNDO}], 1) = 0\n", id);
46
47 sem_b->sem_op = -1;
48 if (semop(id, sem_b, 1))
49 perror_msg_and_skip("semop, -1");
50 printf("semop(%d, [{0, -1, SEM_UNDO}], 1) = 0\n", id);
51
52 puts("+++ exited with 0 +++");
53 return 0;
54}