Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc32 / semctl.c
blob87cb23a517ba28c67f440f3923c37a7dbefe1f8d
1 /* Semctl for architectures where word sized unions are passed indirectly
2 Copyright (C) 1995,1997,1998,2000,2002,2003,2004,2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <sys/sem.h>
24 #include <ipc_priv.h>
26 #include <sysdep.h>
27 #include <string.h>
28 #include <sys/syscall.h>
30 #include <kernel-features.h>
31 #include <shlib-compat.h>
33 struct __old_semid_ds
35 struct __old_ipc_perm sem_perm; /* operation permission struct */
36 __time_t sem_otime; /* last semop() time */
37 __time_t sem_ctime; /* last time changed by semctl() */
38 struct sem *__sembase; /* ptr to first semaphore in array */
39 struct sem_queue *__sem_pending; /* pending operations */
40 struct sem_queue *__sem_pending_last; /* last pending operation */
41 struct sem_undo *__undo; /* ondo requests on this array */
42 unsigned short int sem_nsems; /* number of semaphores in set */
45 /* Define a `union semun' suitable for Linux here. */
46 union semun
48 int val; /* value for SETVAL */
49 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
50 unsigned short int *array; /* array for GETALL & SETALL */
51 struct seminfo *__buf; /* buffer for IPC_INFO */
52 struct __old_semid_ds *__old_buf;
55 #include <bp-checks.h>
56 #include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
58 #ifdef __NR_getuid32
59 # if __ASSUME_32BITUIDS == 0
60 /* This variable is shared with all files that need to check for 32bit
61 uids. */
62 extern int __libc_missing_32bit_uids;
63 # endif
64 #endif
66 /* Return identifier for array of NSEMS semaphores associated with
67 KEY. */
68 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
69 int __old_semctl (int semid, int semnum, int cmd, ...);
70 #endif
71 int __new_semctl (int semid, int semnum, int cmd, ...);
73 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
74 int
75 attribute_compat_text_section
76 __old_semctl (int semid, int semnum, int cmd, ...)
78 union semun arg;
79 va_list ap;
81 /* Get the argument only if required. */
82 arg.buf = NULL;
83 switch (cmd)
85 case SETVAL: /* arg.val */
86 case GETALL: /* arg.array */
87 case SETALL:
88 case IPC_STAT: /* arg.buf */
89 case IPC_SET:
90 case SEM_STAT:
91 case IPC_INFO: /* arg.__buf */
92 case SEM_INFO:
93 va_start (ap, cmd);
94 arg = va_arg (ap, union semun);
95 va_end (ap);
96 break;
99 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
100 CHECK_SEMCTL (&arg, semid, cmd));
102 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
103 #endif
106 __new_semctl (int semid, int semnum, int cmd, ...)
108 union semun arg;
109 va_list ap;
111 /* Get the argument only if required. */
112 arg.buf = NULL;
113 switch (cmd)
115 case SETVAL: /* arg.val */
116 case GETALL: /* arg.array */
117 case SETALL:
118 case IPC_STAT: /* arg.buf */
119 case IPC_SET:
120 case SEM_STAT:
121 case IPC_INFO: /* arg.__buf */
122 case SEM_INFO:
123 va_start (ap, cmd);
124 arg = va_arg (ap, union semun);
125 va_end (ap);
126 break;
129 #if __ASSUME_32BITUIDS > 0
130 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
131 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
132 #else
133 switch (cmd) {
134 case SEM_STAT:
135 case IPC_STAT:
136 case IPC_SET:
137 break;
138 default:
139 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
140 CHECK_SEMCTL (&arg, semid, cmd));
144 int result;
145 struct __old_semid_ds old;
146 struct semid_ds *buf;
148 #ifdef __NR_getuid32
149 if (__libc_missing_32bit_uids <= 0)
151 if (__libc_missing_32bit_uids < 0)
153 int save_errno = errno;
155 /* Test presence of new IPC by testing for getuid32 syscall. */
156 result = INLINE_SYSCALL (getuid32, 0);
157 if (result == -1 && errno == ENOSYS)
158 __libc_missing_32bit_uids = 1;
159 else
160 __libc_missing_32bit_uids = 0;
161 __set_errno(save_errno);
163 if (__libc_missing_32bit_uids <= 0)
165 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
166 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
167 return result;
170 #endif
172 buf = arg.buf;
173 arg.__old_buf = &old;
174 if (cmd == IPC_SET)
176 old.sem_perm.uid = buf->sem_perm.uid;
177 old.sem_perm.gid = buf->sem_perm.gid;
178 old.sem_perm.mode = buf->sem_perm.mode;
179 if (old.sem_perm.uid != buf->sem_perm.uid ||
180 old.sem_perm.gid != buf->sem_perm.gid)
182 __set_errno (EINVAL);
183 return -1;
186 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
187 CHECK_SEMCTL (&arg, semid, cmd));
188 if (result != -1 && cmd != IPC_SET)
190 memset(buf, 0, sizeof(*buf));
191 buf->sem_perm.__key = old.sem_perm.__key;
192 buf->sem_perm.uid = old.sem_perm.uid;
193 buf->sem_perm.gid = old.sem_perm.gid;
194 buf->sem_perm.cuid = old.sem_perm.cuid;
195 buf->sem_perm.cgid = old.sem_perm.cgid;
196 buf->sem_perm.mode = old.sem_perm.mode;
197 buf->sem_perm.__seq = old.sem_perm.__seq;
198 buf->sem_otime = old.sem_otime;
199 buf->sem_ctime = old.sem_ctime;
200 buf->sem_nsems = old.sem_nsems;
202 return result;
204 #endif
207 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);