2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc32 / semctl.c
blob64d47b34c5209fc1c00cbabce45955dec608fb2d
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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <sys/sem.h>
25 #include <ipc_priv.h>
27 #include <sysdep.h>
28 #include <string.h>
29 #include <sys/syscall.h>
31 #include <kernel-features.h>
32 #include <shlib-compat.h>
34 struct __old_semid_ds
36 struct __old_ipc_perm sem_perm; /* operation permission struct */
37 __time_t sem_otime; /* last semop() time */
38 __time_t sem_ctime; /* last time changed by semctl() */
39 struct sem *__sembase; /* ptr to first semaphore in array */
40 struct sem_queue *__sem_pending; /* pending operations */
41 struct sem_queue *__sem_pending_last; /* last pending operation */
42 struct sem_undo *__undo; /* ondo requests on this array */
43 unsigned short int sem_nsems; /* number of semaphores in set */
46 /* Define a `union semun' suitable for Linux here. */
47 union semun
49 int val; /* value for SETVAL */
50 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
51 unsigned short int *array; /* array for GETALL & SETALL */
52 struct seminfo *__buf; /* buffer for IPC_INFO */
53 struct __old_semid_ds *__old_buf;
56 #include <bp-checks.h>
57 #include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
59 #ifdef __NR_getuid32
60 # if __ASSUME_32BITUIDS == 0
61 /* This variable is shared with all files that need to check for 32bit
62 uids. */
63 extern int __libc_missing_32bit_uids;
64 # endif
65 #endif
67 /* Return identifier for array of NSEMS semaphores associated with
68 KEY. */
69 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
70 int __old_semctl (int semid, int semnum, int cmd, ...);
71 #endif
72 int __new_semctl (int semid, int semnum, int cmd, ...);
74 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
75 int
76 attribute_compat_text_section
77 __old_semctl (int semid, int semnum, int cmd, ...)
79 union semun arg;
80 va_list ap;
82 /* Get the argument only if required. */
83 arg.buf = NULL;
84 switch (cmd)
86 case SETVAL: /* arg.val */
87 case GETALL: /* arg.array */
88 case SETALL:
89 case IPC_STAT: /* arg.buf */
90 case IPC_SET:
91 case SEM_STAT:
92 case IPC_INFO: /* arg.__buf */
93 case SEM_INFO:
94 va_start (ap, cmd);
95 arg = va_arg (ap, union semun);
96 va_end (ap);
97 break;
100 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
101 CHECK_SEMCTL (&arg, semid, cmd));
103 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
104 #endif
107 __new_semctl (int semid, int semnum, int cmd, ...)
109 union semun arg;
110 va_list ap;
112 /* Get the argument only if required. */
113 arg.buf = NULL;
114 switch (cmd)
116 case SETVAL: /* arg.val */
117 case GETALL: /* arg.array */
118 case SETALL:
119 case IPC_STAT: /* arg.buf */
120 case IPC_SET:
121 case SEM_STAT:
122 case IPC_INFO: /* arg.__buf */
123 case SEM_INFO:
124 va_start (ap, cmd);
125 arg = va_arg (ap, union semun);
126 va_end (ap);
127 break;
130 #if __ASSUME_32BITUIDS > 0
131 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
132 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
133 #else
134 switch (cmd) {
135 case SEM_STAT:
136 case IPC_STAT:
137 case IPC_SET:
138 break;
139 default:
140 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
141 CHECK_SEMCTL (&arg, semid, cmd));
145 int result;
146 struct __old_semid_ds old;
147 struct semid_ds *buf;
149 #ifdef __NR_getuid32
150 if (__libc_missing_32bit_uids <= 0)
152 if (__libc_missing_32bit_uids < 0)
154 int save_errno = errno;
156 /* Test presence of new IPC by testing for getuid32 syscall. */
157 result = INLINE_SYSCALL (getuid32, 0);
158 if (result == -1 && errno == ENOSYS)
159 __libc_missing_32bit_uids = 1;
160 else
161 __libc_missing_32bit_uids = 0;
162 __set_errno(save_errno);
164 if (__libc_missing_32bit_uids <= 0)
166 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
167 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
168 return result;
171 #endif
173 buf = arg.buf;
174 arg.__old_buf = &old;
175 if (cmd == IPC_SET)
177 old.sem_perm.uid = buf->sem_perm.uid;
178 old.sem_perm.gid = buf->sem_perm.gid;
179 old.sem_perm.mode = buf->sem_perm.mode;
180 if (old.sem_perm.uid != buf->sem_perm.uid ||
181 old.sem_perm.gid != buf->sem_perm.gid)
183 __set_errno (EINVAL);
184 return -1;
187 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
188 CHECK_SEMCTL (&arg, semid, cmd));
189 if (result != -1 && cmd != IPC_SET)
191 memset(buf, 0, sizeof(*buf));
192 buf->sem_perm.__key = old.sem_perm.__key;
193 buf->sem_perm.uid = old.sem_perm.uid;
194 buf->sem_perm.gid = old.sem_perm.gid;
195 buf->sem_perm.cuid = old.sem_perm.cuid;
196 buf->sem_perm.cgid = old.sem_perm.cgid;
197 buf->sem_perm.mode = old.sem_perm.mode;
198 buf->sem_perm.__seq = old.sem_perm.__seq;
199 buf->sem_otime = old.sem_otime;
200 buf->sem_ctime = old.sem_ctime;
201 buf->sem_nsems = old.sem_nsems;
203 return result;
205 #endif
208 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);