Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015 Andreas Schwab <schwab@suse.de> |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 3 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 29 | #include "tests.h" |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 33 | #include <sys/sem.h> |
| 34 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame] | 35 | union semun { |
| 36 | int val; /* Value for SETVAL */ |
| 37 | struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 38 | unsigned short *array; /* Array for GETALL, SETALL */ |
| 39 | struct seminfo *__buf; /* Buffer for IPC_INFO |
| 40 | (Linux-specific) */ |
| 41 | }; |
| 42 | |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 43 | static int id = -1; |
| 44 | |
| 45 | static void |
| 46 | cleanup(void) |
| 47 | { |
| 48 | semctl(id, 0, IPC_RMID, 0); |
| 49 | printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_RMID, \\[?0\\]?\\) += 0\n", id); |
| 50 | id = -1; |
| 51 | } |
| 52 | |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 53 | int |
| 54 | main(void) |
| 55 | { |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 56 | int rc; |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame] | 57 | union semun un; |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 58 | struct semid_ds ds; |
| 59 | struct seminfo info; |
| 60 | |
| 61 | id = semget(IPC_PRIVATE, 1, 0600); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 62 | if (id < 0) |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 63 | perror_msg_and_skip("semget"); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 64 | printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id); |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 65 | atexit(cleanup); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 66 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame] | 67 | un.buf = &ds; |
| 68 | if (semctl(id, 0, IPC_STAT, un)) |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 69 | perror_msg_and_skip("semctl IPC_STAT"); |
Dmitry V. Levin | 499c5aa | 2015-03-11 14:57:57 +0000 | [diff] [blame] | 70 | printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_STAT, \\[?%p\\]?\\) += 0\n", |
| 71 | id, &ds); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 72 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame] | 73 | un.__buf = &info; |
| 74 | int max = semctl(0, 0, SEM_INFO, un); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 75 | if (max < 0) |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 76 | perror_msg_and_skip("semctl SEM_INFO"); |
Dmitry V. Levin | 499c5aa | 2015-03-11 14:57:57 +0000 | [diff] [blame] | 77 | printf("semctl\\(0, 0, (IPC_64\\|)?SEM_INFO, \\[?%p\\]?\\) += %d\n", |
| 78 | &info, max); |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 79 | |
Andreas Schwab | fa5ce37 | 2015-03-11 12:33:30 +0100 | [diff] [blame] | 80 | un.buf = &ds; |
| 81 | rc = semctl(id, 0, SEM_STAT, un); |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 82 | if (rc != id) { |
| 83 | /* |
| 84 | * In linux < v2.6.24-rc1 the first argument must be |
| 85 | * an index in the kernel's internal array. |
| 86 | */ |
| 87 | if (-1 != rc || EINVAL != errno) |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 88 | perror_msg_and_skip("semctl SEM_STAT"); |
Dmitry V. Levin | 499c5aa | 2015-03-11 14:57:57 +0000 | [diff] [blame] | 89 | printf("semctl\\(%d, 0, (IPC_64\\|)?SEM_STAT, \\[?%p\\]?\\)" |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 90 | " += -1 EINVAL \\(%m\\)\n", id, &ds); |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 91 | } else { |
Dmitry V. Levin | 499c5aa | 2015-03-11 14:57:57 +0000 | [diff] [blame] | 92 | printf("semctl\\(%d, 0, (IPC_64\\|)?SEM_STAT, \\[?%p\\]?\\)" |
| 93 | " += %d\n", id, &ds, id); |
Dmitry V. Levin | 7230d0a | 2015-01-14 16:52:28 +0000 | [diff] [blame] | 94 | } |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 95 | |
Dmitry V. Levin | efb1340 | 2016-01-06 11:59:29 +0000 | [diff] [blame] | 96 | return 0; |
Dmitry V. Levin | 12e2442 | 2015-01-12 16:08:59 +0000 | [diff] [blame] | 97 | } |