]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/i386/semctl.c
Update.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / semctl.c
CommitLineData
cd090f71
UD
1/* Copyright (C) 1995, 1997, 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20#include <errno.h>
21#include <stdarg.h>
22#include <sys/sem.h>
23#include <ipc_priv.h>
24
25#include <sysdep.h>
26#include <string.h>
27#include <sys/syscall.h>
28
29#include "kernel-features.h"
b5567b2a 30#include <shlib-compat.h>
cd090f71
UD
31
32struct __old_semid_ds
33{
34 struct __old_ipc_perm sem_perm; /* operation permission struct */
35 __time_t sem_otime; /* last semop() time */
36 __time_t sem_ctime; /* last time changed by semctl() */
37 struct sem *__sembase; /* ptr to first semaphore in array */
38 struct sem_queue *__sem_pending; /* pending operations */
39 struct sem_queue *__sem_pending_last; /* last pending operation */
40 struct sem_undo *__undo; /* ondo requests on this array */
41 unsigned short int sem_nsems; /* number of semaphores in set */
42};
43
44/* Define a `union semun' suitable for Linux here. */
45union semun
46{
47 int val; /* value for SETVAL */
48 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
49 unsigned short int *array; /* array for GETALL & SETALL */
50 struct seminfo *__buf; /* buffer for IPC_INFO */
51};
52
d25c879d
GM
53#include <bp-checks.h>
54#include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
55
cd090f71
UD
56#ifdef __NR_getuid32
57# if __ASSUME_32BITUIDS == 0
58/* This variable is shared with all files that need to check for 32bit
59 uids. */
60extern int __libc_missing_32bit_uids;
61# endif
62#endif
63
64/* Return identifier for array of NSEMS semaphores associated with
65 KEY. */
b5567b2a 66#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
cd090f71 67int __old_semctl (int semid, int semnum, int cmd, ...);
b5567b2a 68#endif
cd090f71
UD
69int __new_semctl (int semid, int semnum, int cmd, ...);
70
b5567b2a 71#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
cd090f71
UD
72int
73__old_semctl (int semid, int semnum, int cmd, ...)
74{
75 union semun arg;
76 va_list ap;
77
78 va_start (ap, cmd);
79
80 /* Get the argument. */
81 arg = va_arg (ap, union semun);
82
83 va_end (ap);
84
d25c879d
GM
85 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
86 CHECK_SEMCTL (&arg, semid, cmd));
cd090f71 87}
b5567b2a
UD
88compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
89#endif
cd090f71
UD
90
91int
92__new_semctl (int semid, int semnum, int cmd, ...)
93{
94 union semun arg;
95 va_list ap;
96
97 va_start (ap, cmd);
98
99 /* Get the argument. */
100 arg = va_arg (ap, union semun);
101
102 va_end (ap);
103
104#if __ASSUME_32BITUIDS > 0
d25c879d
GM
105 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
106 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
cd090f71
UD
107#else
108 switch (cmd) {
109 case SEM_STAT:
110 case IPC_STAT:
111 case IPC_SET:
112 break;
113 default:
d25c879d
GM
114 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
115 CHECK_SEMCTL (&arg, semid, cmd));
cd090f71
UD
116 }
117
118 {
46661856 119 int result;
cd090f71
UD
120 struct __old_semid_ds old;
121 struct semid_ds *buf;
122
123#ifdef __NR_getuid32
124 if (__libc_missing_32bit_uids <= 0)
125 {
126 if (__libc_missing_32bit_uids < 0)
127 {
46661856 128 int save_errno = errno;
cd090f71
UD
129
130 /* Test presence of new IPC by testing for getuid32 syscall. */
131 result = INLINE_SYSCALL (getuid32, 0);
132 if (result == -1 && errno == ENOSYS)
133 __libc_missing_32bit_uids = 1;
134 else
135 __libc_missing_32bit_uids = 0;
136 __set_errno(save_errno);
137 }
138 if (__libc_missing_32bit_uids <= 0)
139 {
d25c879d
GM
140 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
141 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
cd090f71
UD
142 return result;
143 }
144 }
145#endif
146
147 buf = arg.buf;
148 arg.buf = (struct semid_ds *)&old;
149 if (cmd == IPC_SET)
150 {
151 old.sem_perm.uid = buf->sem_perm.uid;
152 old.sem_perm.gid = buf->sem_perm.gid;
153 old.sem_perm.mode = buf->sem_perm.mode;
154 if (old.sem_perm.uid != buf->sem_perm.uid ||
155 old.sem_perm.gid != buf->sem_perm.gid)
156 {
157 __set_errno (EINVAL);
158 return -1;
159 }
160 }
d25c879d
GM
161 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
162 CHECK_SEMCTL (&arg, semid, cmd));
cd090f71
UD
163 if (result != -1 && cmd != IPC_SET)
164 {
165 memset(buf, 0, sizeof(*buf));
166 buf->sem_perm.__key = old.sem_perm.__key;
167 buf->sem_perm.uid = old.sem_perm.uid;
168 buf->sem_perm.gid = old.sem_perm.gid;
169 buf->sem_perm.cuid = old.sem_perm.cuid;
170 buf->sem_perm.cgid = old.sem_perm.cgid;
171 buf->sem_perm.mode = old.sem_perm.mode;
172 buf->sem_perm.__seq = old.sem_perm.__seq;
173 buf->sem_otime = old.sem_otime;
174 buf->sem_ctime = old.sem_ctime;
175 buf->sem_nsems = old.sem_nsems;
176 }
177 return result;
178 }
179#endif
180}
181
b5567b2a 182versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);