Remove *xattr syscalls.
[glibc.git] / sysdeps / unix / sysv / linux / semctl.c
bloba3d9e588a00a06bfb0da748c5653e4097c4d0d96
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 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.
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.
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. */
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 *__unbounded __sembase; /* ptr to first semaphore in array */
38 struct sem_queue *__unbounded __sem_pending; /* pending operations */
39 struct sem_queue *__unbounded __sem_pending_last; /* last pending operation */
40 struct sem_undo *__unbounded __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 */
53 #include <bp-checks.h>
54 #include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
56 /* Return identifier for array of NSEMS semaphores associated with
57 KEY. */
58 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
59 int __old_semctl (int semid, int semnum, int cmd, ...);
60 #endif
61 int __new_semctl (int semid, int semnum, int cmd, ...);
63 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
64 int
65 __old_semctl (int semid, int semnum, int cmd, ...)
67 union semun arg;
68 va_list ap;
70 va_start (ap, cmd);
72 /* Get the argument only if required. */
73 arg.buf = NULL;
74 switch (cmd)
76 case SETVAL: /* arg.val */
77 case GETALL: /* arg.array */
78 case SETALL:
79 case IPC_STAT: /* arg.buf */
80 case IPC_SET:
81 case SEM_STAT:
82 case IPC_INFO: /* arg.__buf */
83 case SEM_INFO:
84 va_start (ap, cmd);
85 arg = va_arg (ap, union semun);
86 va_end (ap);
87 break;
90 va_end (ap);
92 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
93 CHECK_SEMCTL (&arg, semid, cmd));
95 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
96 #endif
98 int
99 __new_semctl (int semid, int semnum, int cmd, ...)
101 union semun arg;
102 va_list ap;
104 va_start (ap, cmd);
106 /* Get the argument only if required. */
107 arg.buf = NULL;
108 switch (cmd)
110 case SETVAL: /* arg.val */
111 case GETALL: /* arg.array */
112 case SETALL:
113 case IPC_STAT: /* arg.buf */
114 case IPC_SET:
115 case SEM_STAT:
116 case IPC_INFO: /* arg.__buf */
117 case SEM_INFO:
118 va_start (ap, cmd);
119 arg = va_arg (ap, union semun);
120 va_end (ap);
121 break;
124 va_end (ap);
126 #if __ASSUME_IPC64 > 0
127 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
128 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
129 #else
130 switch (cmd)
132 case SEM_STAT:
133 case IPC_STAT:
134 case IPC_SET:
135 break;
136 default:
137 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
138 CHECK_SEMCTL (&arg, semid, cmd));
142 int save_errno = errno, result;
143 struct __old_semid_ds old;
144 struct semid_ds *buf;
146 /* Unfortunately there is no way how to find out for sure whether
147 we should use old or new semctl. */
148 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
149 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
150 if (result != -1 || errno != EINVAL)
151 return result;
153 __set_errno(save_errno);
154 buf = arg.buf;
155 arg.buf = (struct semid_ds *)&old;
156 if (cmd == IPC_SET)
158 old.sem_perm.uid = buf->sem_perm.uid;
159 old.sem_perm.gid = buf->sem_perm.gid;
160 old.sem_perm.mode = buf->sem_perm.mode;
161 if (old.sem_perm.uid != buf->sem_perm.uid ||
162 old.sem_perm.gid != buf->sem_perm.gid)
164 __set_errno (EINVAL);
165 return -1;
168 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
169 CHECK_SEMCTL (&arg, semid, cmd));
170 if (result != -1 && cmd != IPC_SET)
172 memset(buf, 0, sizeof(*buf));
173 buf->sem_perm.__key = old.sem_perm.__key;
174 buf->sem_perm.uid = old.sem_perm.uid;
175 buf->sem_perm.gid = old.sem_perm.gid;
176 buf->sem_perm.cuid = old.sem_perm.cuid;
177 buf->sem_perm.cgid = old.sem_perm.cgid;
178 buf->sem_perm.mode = old.sem_perm.mode;
179 buf->sem_perm.__seq = old.sem_perm.__seq;
180 buf->sem_otime = old.sem_otime;
181 buf->sem_ctime = old.sem_ctime;
182 buf->sem_nsems = old.sem_nsems;
184 return result;
186 #endif
189 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);