Update.
[glibc.git] / sysdeps / unix / sysv / linux / semctl.c
blob7ea8ee6c397c245b4842e46b1d7a3f8673c212b4
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>
28 #include <shlib-compat.h>
30 #include "kernel-features.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 */
54 /* Return identifier for array of NSEMS semaphores associated with
55 KEY. */
56 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
57 int __old_semctl (int semid, int semnum, int cmd, ...);
58 #endif
59 int __new_semctl (int semid, int semnum, int cmd, ...);
61 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
62 int
63 __old_semctl (int semid, int semnum, int cmd, ...)
65 union semun arg;
66 va_list ap;
68 va_start (ap, cmd);
70 /* Get the argument. */
71 arg = va_arg (ap, union semun);
73 va_end (ap);
75 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd, &arg);
77 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
78 #endif
80 int
81 __new_semctl (int semid, int semnum, int cmd, ...)
83 union semun arg;
84 va_list ap;
86 va_start (ap, cmd);
88 /* Get the argument. */
89 arg = va_arg (ap, union semun);
91 va_end (ap);
93 #if __ASSUME_32BITUIDS > 0
94 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64, &arg);
95 #else
96 switch (cmd) {
97 case SEM_STAT:
98 case IPC_STAT:
99 case IPC_SET:
100 break;
101 default:
102 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd, &arg);
106 int save_errno = errno, result;
107 struct __old_semid_ds old;
108 struct semid_ds *buf;
110 /* Unfortunately there is no way how to find out for sure whether
111 we should use old or new semctl. */
112 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64, &arg);
113 if (result != -1 || errno != EINVAL)
114 return result;
116 __set_errno(save_errno);
117 buf = arg.buf;
118 arg.buf = (struct semid_ds *)&old;
119 if (cmd == IPC_SET)
121 old.sem_perm.uid = buf->sem_perm.uid;
122 old.sem_perm.gid = buf->sem_perm.gid;
123 old.sem_perm.mode = buf->sem_perm.mode;
124 if (old.sem_perm.uid != buf->sem_perm.uid ||
125 old.sem_perm.gid != buf->sem_perm.gid)
127 __set_errno (EINVAL);
128 return -1;
131 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd, &arg);
132 if (result != -1 && cmd != IPC_SET)
134 memset(buf, 0, sizeof(*buf));
135 buf->sem_perm.__key = old.sem_perm.__key;
136 buf->sem_perm.uid = old.sem_perm.uid;
137 buf->sem_perm.gid = old.sem_perm.gid;
138 buf->sem_perm.cuid = old.sem_perm.cuid;
139 buf->sem_perm.cgid = old.sem_perm.cgid;
140 buf->sem_perm.mode = old.sem_perm.mode;
141 buf->sem_perm.__seq = old.sem_perm.__seq;
142 buf->sem_otime = old.sem_otime;
143 buf->sem_ctime = old.sem_ctime;
144 buf->sem_nsems = old.sem_nsems;
146 return result;
148 #endif
151 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);