Update.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / semctl.c
blobb553c2aa8bc406c165ee79525a5fa604307b8549
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.
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.
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.
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. */
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <sys/sem.h>
23 #include <ipc_priv.h>
25 #include <sysdep.h>
26 #include <string.h>
27 #include <sys/syscall.h>
29 #include "kernel-features.h"
30 #include <shlib-compat.h>
32 struct __old_semid_ds
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 */
44 /* Define a `union semun' suitable for Linux here. */
45 union semun
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 */
53 #ifdef __NR_getuid32
54 # if __ASSUME_32BITUIDS == 0
55 /* This variable is shared with all files that need to check for 32bit
56 uids. */
57 extern int __libc_missing_32bit_uids;
58 # endif
59 #endif
61 /* Return identifier for array of NSEMS semaphores associated with
62 KEY. */
63 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
64 int __old_semctl (int semid, int semnum, int cmd, ...);
65 #endif
66 int __new_semctl (int semid, int semnum, int cmd, ...);
68 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
69 int
70 __old_semctl (int semid, int semnum, int cmd, ...)
72 union semun arg;
73 va_list ap;
75 va_start (ap, cmd);
77 /* Get the argument. */
78 arg = va_arg (ap, union semun);
80 va_end (ap);
82 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd, &arg);
84 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
85 #endif
87 int
88 __new_semctl (int semid, int semnum, int cmd, ...)
90 union semun arg;
91 va_list ap;
93 va_start (ap, cmd);
95 /* Get the argument. */
96 arg = va_arg (ap, union semun);
98 va_end (ap);
100 #if __ASSUME_32BITUIDS > 0
101 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64, &arg);
102 #else
103 switch (cmd) {
104 case SEM_STAT:
105 case IPC_STAT:
106 case IPC_SET:
107 break;
108 default:
109 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd, &arg);
113 int result;
114 struct __old_semid_ds old;
115 struct semid_ds *buf;
117 #ifdef __NR_getuid32
118 if (__libc_missing_32bit_uids <= 0)
120 if (__libc_missing_32bit_uids < 0)
122 int save_errno = errno;
124 /* Test presence of new IPC by testing for getuid32 syscall. */
125 result = INLINE_SYSCALL (getuid32, 0);
126 if (result == -1 && errno == ENOSYS)
127 __libc_missing_32bit_uids = 1;
128 else
129 __libc_missing_32bit_uids = 0;
130 __set_errno(save_errno);
132 if (__libc_missing_32bit_uids <= 0)
134 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64, &arg);
135 return result;
138 #endif
140 buf = arg.buf;
141 arg.buf = (struct semid_ds *)&old;
142 if (cmd == IPC_SET)
144 old.sem_perm.uid = buf->sem_perm.uid;
145 old.sem_perm.gid = buf->sem_perm.gid;
146 old.sem_perm.mode = buf->sem_perm.mode;
147 if (old.sem_perm.uid != buf->sem_perm.uid ||
148 old.sem_perm.gid != buf->sem_perm.gid)
150 __set_errno (EINVAL);
151 return -1;
154 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd, &arg);
155 if (result != -1 && cmd != IPC_SET)
157 memset(buf, 0, sizeof(*buf));
158 buf->sem_perm.__key = old.sem_perm.__key;
159 buf->sem_perm.uid = old.sem_perm.uid;
160 buf->sem_perm.gid = old.sem_perm.gid;
161 buf->sem_perm.cuid = old.sem_perm.cuid;
162 buf->sem_perm.cgid = old.sem_perm.cgid;
163 buf->sem_perm.mode = old.sem_perm.mode;
164 buf->sem_perm.__seq = old.sem_perm.__seq;
165 buf->sem_otime = old.sem_otime;
166 buf->sem_ctime = old.sem_ctime;
167 buf->sem_nsems = old.sem_nsems;
169 return result;
171 #endif
174 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);