Bug 497723 - forgot to restore callgrind output cleanup
[valgrind.git] / none / tests / sem.c
blob5a35ae16fe493c7611943f6812461b4bb929b99d
1 #define _GNU_SOURCE
3 #include <config.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/ipc.h>
9 #include <sys/sem.h>
10 #include <time.h>
11 #include <unistd.h>
13 void semctl_test (int trace, const char *fname)
15 key_t key;
16 int semid;
17 int nr_of_readers;
18 int ret;
20 union semun {
21 int val; /* Value for SETVAL */
22 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
23 unsigned short *array; /* Array for GETALL, SETALL */
24 struct seminfo *__buf; /* Buffer for IPC_INFO
25 (Linux-specific) */
26 } u;
28 struct semid_ds ds;
30 key = ftok (fname, 1);
31 if (key == -1)
32 perror ("ftok");
33 nr_of_readers = 4;
35 semid = semget (key, 2 * nr_of_readers, IPC_CREAT + 0660);
36 if (semid == -1) {
37 perror ("semget");
39 if (trace)
40 printf("semid %d\n", semid);
42 u.buf = &ds;
43 ret = semctl (semid, 0, IPC_STAT, u);
44 if (ret == -1)
45 perror("semctl IPC_STAT");
46 if (trace)
47 printf("semid %d sem_nsems %d\n", semid, (int) ds.sem_nsems);
50 unsigned short semarray[2 * nr_of_readers];
51 for (int count = 0; count < nr_of_readers; count++) {
52 semarray[2 * count] = 0;
53 semarray[2 * count + 1] = 1000;
55 ret = semctl (semid, 0, SETALL, semarray);
56 if (ret == -1)
57 perror ("semctl SETALL");
60 ret = semctl (semid, 0, IPC_RMID);
61 if (ret == -1)
62 perror ("semctl IPC_RMID");
65 int main(int argc, char **argv)
67 int semid;
68 struct sembuf sop;
69 #ifdef HAVE_SEMTIMEDOP
70 struct timespec ts;
71 #endif
73 if ((semid = semget(IPC_PRIVATE, 1, 0600)) < 0)
75 perror("semget");
76 exit(1);
79 sop.sem_num = 0;
80 sop.sem_op = 1;
81 sop.sem_flg = 0;
83 if (semop(semid, &sop, 1) < 0)
85 perror("semop");
86 semctl(semid, 0, IPC_RMID);
87 exit(1);
90 if (semctl(semid, 0, GETVAL) != 1)
92 perror("semctl GETVAL");
93 semctl(semid, 0, IPC_RMID);
94 exit(1);
97 if (semctl(semid, 0, GETPID) != getpid())
99 perror("semctl GETPID");
100 semctl(semid, 0, IPC_RMID);
101 exit(1);
104 /* The next call to semtimedop causes the program to hang on
105 ppc32-linux (Yellow Dog 4.0). I don't know why. Hence the
106 extended ifdef. */
107 #if defined(HAVE_SEMTIMEDOP) && !defined(__powerpc__)
108 sop.sem_num = 0;
109 sop.sem_op = 0;
110 sop.sem_flg = 0;
112 ts.tv_sec = 0;
113 ts.tv_nsec = 1000000;
115 if (semtimedop(semid, &sop, 1, &ts) < 0 && errno != EAGAIN)
117 perror("semtimedop");
118 semctl(semid, 0, IPC_RMID);
119 exit(1);
121 #endif
123 sop.sem_num = 0;
124 sop.sem_op = -1;
125 sop.sem_flg = 0;
127 if (semop(semid, &sop, 1) < 0)
129 perror("semop");
130 semctl(semid, 0, IPC_RMID);
131 exit(1);
134 #ifdef HAVE_SEMTIMEDOP
135 sop.sem_num = 0;
136 sop.sem_op = 0;
137 sop.sem_flg = 0;
139 ts.tv_sec = 0;
140 ts.tv_nsec = 1000;
142 if (semtimedop(semid, &sop, 1, &ts) < 0)
144 perror("semtimedop");
145 semctl(semid, 0, IPC_RMID);
146 exit(1);
148 #endif
150 if (semctl(semid, 0, IPC_RMID) < 0)
152 perror("semctl(IPC_RMID)");
153 exit(1);
156 semctl_test(argc > 1, argv[0]);
157 exit(0);