1 #define _GNU_SOURCE
2
3 #include <config.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/ipc.h>
8 #include <sys/sem.h>
9 #include <time.h>
main(int argc,char ** argv)10 int main(int argc, char **argv)
11 {
12 int semid;
13 struct sembuf sop;
14 #ifdef HAVE_SEMTIMEDOP
15 struct timespec ts;
16 #endif
17
18 if ((semid = semget(IPC_PRIVATE, 1, 0600)) < 0)
19 {
20 perror("semget");
21 exit(1);
22 }
23
24 sop.sem_num = 0;
25 sop.sem_op = 1;
26 sop.sem_flg = 0;
27
28 if (semop(semid, &sop, 1) < 0)
29 {
30 perror("semop");
31 semctl(semid, 0, IPC_RMID);
32 exit(1);
33 }
34
35 /* The next call to semtimedop causes the program to hang on
36 ppc32-linux (Yellow Dog 4.0). I don't know why. Hence the
37 extended ifdef. */
38 #if defined(HAVE_SEMTIMEDOP) && !defined(__powerpc__)
39 sop.sem_num = 0;
40 sop.sem_op = 0;
41 sop.sem_flg = 0;
42
43 ts.tv_sec = 0;
44 ts.tv_nsec = 1000000;
45
46 if (semtimedop(semid, &sop, 1, &ts) < 0 && errno != EAGAIN)
47 {
48 perror("semtimedop");
49 semctl(semid, 0, IPC_RMID);
50 exit(1);
51 }
52 #endif
53
54 sop.sem_num = 0;
55 sop.sem_op = -1;
56 sop.sem_flg = 0;
57
58 if (semop(semid, &sop, 1) < 0)
59 {
60 perror("semop");
61 semctl(semid, 0, IPC_RMID);
62 exit(1);
63 }
64
65 #ifdef HAVE_SEMTIMEDOP
66 sop.sem_num = 0;
67 sop.sem_op = 0;
68 sop.sem_flg = 0;
69
70 ts.tv_sec = 0;
71 ts.tv_nsec = 1000;
72
73 if (semtimedop(semid, &sop, 1, &ts) < 0)
74 {
75 perror("semtimedop");
76 semctl(semid, 0, IPC_RMID);
77 exit(1);
78 }
79 #endif
80
81 if (semctl(semid, 0, IPC_RMID) < 0)
82 {
83 perror("semctl(IPC_RMID)");
84 exit(1);
85 }
86
87 exit(0);
88 }
89