blob: 59c48b2944d0b9c6f84c2f148e77ae651b00fb38 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015 Andreas Schwab <schwab@suse.de>
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. Levin12e24422015-01-12 16:08:59 +000029#include <stdio.h>
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000030#include <errno.h>
Dmitry V. Levin12e24422015-01-12 16:08:59 +000031#include <sys/sem.h>
32
Andreas Schwabfa5ce372015-03-11 12:33:30 +010033union semun {
34 int val; /* Value for SETVAL */
35 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
36 unsigned short *array; /* Array for GETALL, SETALL */
37 struct seminfo *__buf; /* Buffer for IPC_INFO
38 (Linux-specific) */
39};
40
Dmitry V. Levin12e24422015-01-12 16:08:59 +000041int
42main(void)
43{
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000044 int rc, id;
Andreas Schwabfa5ce372015-03-11 12:33:30 +010045 union semun un;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000046 struct semid_ds ds;
47 struct seminfo info;
48
49 id = semget(IPC_PRIVATE, 1, 0600);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000050 if (id < 0)
51 return 77;
52 printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
53
Andreas Schwabfa5ce372015-03-11 12:33:30 +010054 un.buf = &ds;
55 if (semctl(id, 0, IPC_STAT, un))
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000056 goto fail;
Dmitry V. Levin499c5aa2015-03-11 14:57:57 +000057 printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_STAT, \\[?%p\\]?\\) += 0\n",
58 id, &ds);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000059
Andreas Schwabfa5ce372015-03-11 12:33:30 +010060 un.__buf = &info;
61 int max = semctl(0, 0, SEM_INFO, un);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000062 if (max < 0)
63 goto fail;
Dmitry V. Levin499c5aa2015-03-11 14:57:57 +000064 printf("semctl\\(0, 0, (IPC_64\\|)?SEM_INFO, \\[?%p\\]?\\) += %d\n",
65 &info, max);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000066
Andreas Schwabfa5ce372015-03-11 12:33:30 +010067 un.buf = &ds;
68 rc = semctl(id, 0, SEM_STAT, un);
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000069 if (rc != id) {
70 /*
71 * In linux < v2.6.24-rc1 the first argument must be
72 * an index in the kernel's internal array.
73 */
74 if (-1 != rc || EINVAL != errno)
75 goto fail;
Dmitry V. Levin499c5aa2015-03-11 14:57:57 +000076 printf("semctl\\(%d, 0, (IPC_64\\|)?SEM_STAT, \\[?%p\\]?\\)"
77 " += -1 EINVAL \\(Invalid argument\\)\n", id, &ds);
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000078 } else {
Dmitry V. Levin499c5aa2015-03-11 14:57:57 +000079 printf("semctl\\(%d, 0, (IPC_64\\|)?SEM_STAT, \\[?%p\\]?\\)"
80 " += %d\n", id, &ds, id);
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000081 }
Dmitry V. Levin12e24422015-01-12 16:08:59 +000082
83 rc = 0;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000084done:
Dmitry V. Levin12e24422015-01-12 16:08:59 +000085 if (semctl(id, 0, IPC_RMID, 0) < 0)
86 return 1;
Dmitry V. Levin499c5aa2015-03-11 14:57:57 +000087 printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_RMID, \\[?0\\]?\\) += 0\n", id);
Dmitry V. Levin12e24422015-01-12 16:08:59 +000088 return rc;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000089
90fail:
Andreas Schwabfa5ce372015-03-11 12:33:30 +010091 rc = 1;
Dmitry V. Levin7230d0a2015-01-14 16:52:28 +000092 goto done;
Dmitry V. Levin12e24422015-01-12 16:08:59 +000093}