blob: 9373482af0f4877545d018e6175ae618f9e24dd8 [file] [log] [blame]
Dmitry V. Levin12e24422015-01-12 16:08:59 +00001#include <stdio.h>
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +00002#include <errno.h>
Dmitry V. Levin12e24422015-01-12 16:08:59 +00003#include <sys/sem.h>
4
Andreas Schwabfa5ce372015-03-11 12:33:30 +01005union semun {
6 int val; /* Value for SETVAL */
7 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
8 unsigned short *array; /* Array for GETALL, SETALL */
9 struct seminfo *__buf; /* Buffer for IPC_INFO
10 (Linux-specific) */
11};
12
Dmitry V. Levin12e24422015-01-12 16:08:59 +000013int
14main(void)
15{
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000016 int rc, id;
Andreas Schwabfa5ce372015-03-11 12:33:30 +010017 union semun un;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000018 struct semid_ds ds;
19 struct seminfo info;
20
21 id = semget(IPC_PRIVATE, 1, 0600);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000022 if (id < 0)
23 return 77;
24 printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
25
Andreas Schwabfa5ce372015-03-11 12:33:30 +010026 un.buf = &ds;
27 if (semctl(id, 0, IPC_STAT, un))
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000028 goto fail;
29 printf("semctl\\(%d, 0, IPC_STAT, %p\\) += 0\n", id, &ds);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000030
Andreas Schwabfa5ce372015-03-11 12:33:30 +010031 un.__buf = &info;
32 int max = semctl(0, 0, SEM_INFO, un);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000033 if (max < 0)
34 goto fail;
35 printf("semctl\\(0, 0, SEM_INFO, %p\\) += %d\n", &info, max);
36
Andreas Schwabfa5ce372015-03-11 12:33:30 +010037 un.buf = &ds;
38 rc = semctl(id, 0, SEM_STAT, un);
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000039 if (rc != id) {
40 /*
41 * In linux < v2.6.24-rc1 the first argument must be
42 * an index in the kernel's internal array.
43 */
44 if (-1 != rc || EINVAL != errno)
45 goto fail;
46 printf("semctl\\(%d, 0, SEM_STAT, %p\\) += -1 EINVAL \\(Invalid argument\\)\n", id, &ds);
47 } else {
48 printf("semctl\\(%d, 0, SEM_STAT, %p\\) += %d\n", id, &ds, id);
49 }
Dmitry V. Levin12e24422015-01-12 16:08:59 +000050
51 rc = 0;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000052done:
Dmitry V. Levin12e24422015-01-12 16:08:59 +000053 if (semctl(id, 0, IPC_RMID, 0) < 0)
54 return 1;
55 printf("semctl\\(%d, 0, IPC_RMID, 0\\) += 0\n", id);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000056 return rc;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000057
58fail:
Andreas Schwabfa5ce372015-03-11 12:33:30 +010059 rc = 1;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000060 goto done;
Dmitry V. Levin12e24422015-01-12 16:08:59 +000061}