Updated to fedora-glibc-20041110T0839
[glibc.git] / sysdeps / unix / sysv / linux / semctl.c
blobecd23ce53e0e1841191e75be3a9d4a080231841d
1 /* Copyright (C) 1995, 1997, 1998, 2000, 2003, 2004
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
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>
29 #include <shlib-compat.h>
31 #include "kernel-features.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 *__unbounded __sembase; /* ptr to first semaphore in array */
39 struct sem_queue *__unbounded __sem_pending; /* pending operations */
40 struct sem_queue *__unbounded __sem_pending_last; /* last pending operation */
41 struct sem_undo *__unbounded __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 /* Return identifier for array of NSEMS semaphores associated with
59 KEY. */
60 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
61 int __old_semctl (int semid, int semnum, int cmd, ...);
62 #endif
63 int __new_semctl (int semid, int semnum, int cmd, ...);
65 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
66 int
67 attribute_compat_text_section
68 __old_semctl (int semid, int semnum, int cmd, ...)
70 union semun arg;
71 va_list ap;
73 va_start (ap, cmd);
75 /* Get the argument only if required. */
76 arg.buf = NULL;
77 switch (cmd)
79 case SETVAL: /* arg.val */
80 case GETALL: /* arg.array */
81 case SETALL:
82 case IPC_STAT: /* arg.buf */
83 case IPC_SET:
84 case SEM_STAT:
85 case IPC_INFO: /* arg.__buf */
86 case SEM_INFO:
87 va_start (ap, cmd);
88 arg = va_arg (ap, union semun);
89 va_end (ap);
90 break;
93 va_end (ap);
95 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
96 CHECK_SEMCTL (&arg, semid, cmd));
98 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
99 #endif
102 __new_semctl (int semid, int semnum, int cmd, ...)
104 union semun arg;
105 va_list ap;
107 va_start (ap, cmd);
109 /* Get the argument only if required. */
110 arg.buf = NULL;
111 switch (cmd)
113 case SETVAL: /* arg.val */
114 case GETALL: /* arg.array */
115 case SETALL:
116 case IPC_STAT: /* arg.buf */
117 case IPC_SET:
118 case SEM_STAT:
119 case IPC_INFO: /* arg.__buf */
120 case SEM_INFO:
121 va_start (ap, cmd);
122 arg = va_arg (ap, union semun);
123 va_end (ap);
124 break;
127 va_end (ap);
129 #if __ASSUME_IPC64 > 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)
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 save_errno = errno, result;
146 struct __old_semid_ds old;
147 struct semid_ds *buf;
149 /* Unfortunately there is no way how to find out for sure whether
150 we should use old or new semctl. */
151 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
152 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
153 if (result != -1 || errno != EINVAL)
154 return result;
156 __set_errno(save_errno);
157 buf = arg.buf;
158 arg.__old_buf = &old;
159 if (cmd == IPC_SET)
161 old.sem_perm.uid = buf->sem_perm.uid;
162 old.sem_perm.gid = buf->sem_perm.gid;
163 old.sem_perm.mode = buf->sem_perm.mode;
164 if (old.sem_perm.uid != buf->sem_perm.uid ||
165 old.sem_perm.gid != buf->sem_perm.gid)
167 __set_errno (EINVAL);
168 return -1;
171 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
172 CHECK_SEMCTL (&arg, semid, cmd));
173 if (result != -1 && cmd != IPC_SET)
175 memset(buf, 0, sizeof(*buf));
176 buf->sem_perm.__key = old.sem_perm.__key;
177 buf->sem_perm.uid = old.sem_perm.uid;
178 buf->sem_perm.gid = old.sem_perm.gid;
179 buf->sem_perm.cuid = old.sem_perm.cuid;
180 buf->sem_perm.cgid = old.sem_perm.cgid;
181 buf->sem_perm.mode = old.sem_perm.mode;
182 buf->sem_perm.__seq = old.sem_perm.__seq;
183 buf->sem_otime = old.sem_otime;
184 buf->sem_ctime = old.sem_ctime;
185 buf->sem_nsems = old.sem_nsems;
187 return result;
189 #endif
192 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);