]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/semctl.c
Update.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / semctl.c
1 /* Copyright (C) 1995, 1997, 1998, 2000, 2003 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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"
30 #include <shlib-compat.h>
31
32 struct __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. */
45 union 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 struct __old_semid_ds *__old_buf;
52 };
53
54 #include <bp-checks.h>
55 #include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
56
57 #ifdef __NR_getuid32
58 # if __ASSUME_32BITUIDS == 0
59 /* This variable is shared with all files that need to check for 32bit
60 uids. */
61 extern int __libc_missing_32bit_uids;
62 # endif
63 #endif
64
65 /* Return identifier for array of NSEMS semaphores associated with
66 KEY. */
67 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
68 int __old_semctl (int semid, int semnum, int cmd, ...);
69 #endif
70 int __new_semctl (int semid, int semnum, int cmd, ...);
71
72 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
73 int
74 __old_semctl (int semid, int semnum, int cmd, ...)
75 {
76 union semun arg;
77 va_list ap;
78
79 va_start (ap, cmd);
80
81 /* Get the argument. */
82 arg = va_arg (ap, union semun);
83
84 va_end (ap);
85
86 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
87 CHECK_SEMCTL (&arg, semid, cmd));
88 }
89 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
90 #endif
91
92 int
93 __new_semctl (int semid, int semnum, int cmd, ...)
94 {
95 union semun arg;
96 va_list ap;
97
98 va_start (ap, cmd);
99
100 /* Get the argument. */
101 arg = va_arg (ap, union semun);
102
103 va_end (ap);
104
105 #if __ASSUME_32BITUIDS > 0
106 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
107 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
108 #else
109 switch (cmd) {
110 case SEM_STAT:
111 case IPC_STAT:
112 case IPC_SET:
113 break;
114 default:
115 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
116 CHECK_SEMCTL (&arg, semid, cmd));
117 }
118
119 {
120 int result;
121 struct __old_semid_ds old;
122 struct semid_ds *buf;
123
124 #ifdef __NR_getuid32
125 if (__libc_missing_32bit_uids <= 0)
126 {
127 if (__libc_missing_32bit_uids < 0)
128 {
129 int save_errno = errno;
130
131 /* Test presence of new IPC by testing for getuid32 syscall. */
132 result = INLINE_SYSCALL (getuid32, 0);
133 if (result == -1 && errno == ENOSYS)
134 __libc_missing_32bit_uids = 1;
135 else
136 __libc_missing_32bit_uids = 0;
137 __set_errno(save_errno);
138 }
139 if (__libc_missing_32bit_uids <= 0)
140 {
141 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
142 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
143 return result;
144 }
145 }
146 #endif
147
148 buf = arg.buf;
149 arg.__old_buf = &old;
150 if (cmd == IPC_SET)
151 {
152 old.sem_perm.uid = buf->sem_perm.uid;
153 old.sem_perm.gid = buf->sem_perm.gid;
154 old.sem_perm.mode = buf->sem_perm.mode;
155 if (old.sem_perm.uid != buf->sem_perm.uid ||
156 old.sem_perm.gid != buf->sem_perm.gid)
157 {
158 __set_errno (EINVAL);
159 return -1;
160 }
161 }
162 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
163 CHECK_SEMCTL (&arg, semid, cmd));
164 if (result != -1 && cmd != IPC_SET)
165 {
166 memset(buf, 0, sizeof(*buf));
167 buf->sem_perm.__key = old.sem_perm.__key;
168 buf->sem_perm.uid = old.sem_perm.uid;
169 buf->sem_perm.gid = old.sem_perm.gid;
170 buf->sem_perm.cuid = old.sem_perm.cuid;
171 buf->sem_perm.cgid = old.sem_perm.cgid;
172 buf->sem_perm.mode = old.sem_perm.mode;
173 buf->sem_perm.__seq = old.sem_perm.__seq;
174 buf->sem_otime = old.sem_otime;
175 buf->sem_ctime = old.sem_ctime;
176 buf->sem_nsems = old.sem_nsems;
177 }
178 return result;
179 }
180 #endif
181 }
182
183 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);