Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 2 | #include <errno.h> |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 3 | #include <sys/sem.h> |
| 4 | |
| 5 | int |
| 6 | main(void) |
| 7 | { |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 8 | int rc, id; |
| 9 | struct semid_ds ds; |
| 10 | struct seminfo info; |
| 11 | |
| 12 | id = semget(IPC_PRIVATE, 1, 0600); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 13 | if (id < 0) |
| 14 | return 77; |
| 15 | printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id); |
| 16 | |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 17 | if (semctl(id, 0, IPC_STAT, &ds)) |
| 18 | goto fail; |
| 19 | printf("semctl\\(%d, 0, IPC_STAT, %p\\) += 0\n", id, &ds); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 20 | |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 21 | int max = semctl(0, 0, SEM_INFO, &info); |
| 22 | if (max < 0) |
| 23 | goto fail; |
| 24 | printf("semctl\\(0, 0, SEM_INFO, %p\\) += %d\n", &info, max); |
| 25 | |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 26 | rc = semctl(id, 0, SEM_STAT, &ds); |
| 27 | if (rc != id) { |
| 28 | /* |
| 29 | * In linux < v2.6.24-rc1 the first argument must be |
| 30 | * an index in the kernel's internal array. |
| 31 | */ |
| 32 | if (-1 != rc || EINVAL != errno) |
| 33 | goto fail; |
| 34 | printf("semctl\\(%d, 0, SEM_STAT, %p\\) += -1 EINVAL \\(Invalid argument\\)\n", id, &ds); |
| 35 | } else { |
| 36 | printf("semctl\\(%d, 0, SEM_STAT, %p\\) += %d\n", id, &ds, id); |
| 37 | } |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 38 | |
| 39 | rc = 0; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 40 | done: |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 41 | if (semctl(id, 0, IPC_RMID, 0) < 0) |
| 42 | return 1; |
| 43 | printf("semctl\\(%d, 0, IPC_RMID, 0\\) += 0\n", id); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 44 | return rc; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 45 | |
| 46 | fail: |
| 47 | rc = 1; |
| 48 | goto done; |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 49 | } |