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 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame^] | 5 | union 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. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 13 | int |
| 14 | main(void) |
| 15 | { |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 16 | int rc, id; |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame^] | 17 | union semun un; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 18 | struct semid_ds ds; |
| 19 | struct seminfo info; |
| 20 | |
| 21 | id = semget(IPC_PRIVATE, 1, 0600); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 22 | if (id < 0) |
| 23 | return 77; |
| 24 | printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id); |
| 25 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame^] | 26 | un.buf = &ds; |
| 27 | if (semctl(id, 0, IPC_STAT, un)) |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 28 | goto fail; |
| 29 | printf("semctl\\(%d, 0, IPC_STAT, %p\\) += 0\n", id, &ds); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 30 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame^] | 31 | un.__buf = &info; |
| 32 | int max = semctl(0, 0, SEM_INFO, un); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 33 | if (max < 0) |
| 34 | goto fail; |
| 35 | printf("semctl\\(0, 0, SEM_INFO, %p\\) += %d\n", &info, max); |
| 36 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame^] | 37 | un.buf = &ds; |
| 38 | rc = semctl(id, 0, SEM_STAT, un); |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 39 | 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. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 50 | |
| 51 | rc = 0; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 52 | done: |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 53 | if (semctl(id, 0, IPC_RMID, 0) < 0) |
| 54 | return 1; |
| 55 | printf("semctl\\(%d, 0, IPC_RMID, 0\\) += 0\n", id); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 56 | return rc; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 57 | |
| 58 | fail: |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame^] | 59 | rc = 1; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 60 | goto done; |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 61 | } |