2006-02-27 Roland McGrath <roland@redhat.com>
[glibc-ports.git] / sysdeps / unix / sysv / linux / alpha / semctl.c
blob9957f983bff72bd25b5911b4a9f2a0c3e5372738
1 /* Copyright (C) 1995,1997,1998,2000,2003,2006
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>
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 */
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 int __new_semctl (int semid, int semnum, int cmd, ...);
60 int
61 __new_semctl (int semid, int semnum, int cmd, ...)
63 union semun arg;
64 va_list ap;
66 va_start (ap, cmd);
68 /* Get the argument. */
69 arg = va_arg (ap, union semun);
71 va_end (ap);
73 #if __ASSUME_32BITUIDS > 0
74 return INLINE_SYSCALL (semctl, 4, semid, semnum, cmd | __IPC_64,
75 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64)->array);
76 #else
77 switch (cmd) {
78 case SEM_STAT:
79 case IPC_STAT:
80 case IPC_SET:
81 break;
82 default:
83 return INLINE_SYSCALL (semctl, 4, semid, semnum, cmd,
84 CHECK_SEMCTL (&arg, semid, cmd)->array);
88 int save_errno = errno, result;
89 struct __old_semid_ds old;
90 struct semid_ds *buf;
92 /* Unfortunately there is no way how to find out for sure whether
93 we should use old or new semctl. */
94 result = INLINE_SYSCALL (semctl, 4, semid, semnum, cmd | __IPC_64,
95 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64)->array);
96 if (result != -1 || errno != EINVAL)
97 return result;
99 __set_errno(save_errno);
100 buf = arg.buf;
101 arg.buf = (void *)&old;
102 if (cmd == IPC_SET)
104 old.sem_perm.uid = buf->sem_perm.uid;
105 old.sem_perm.gid = buf->sem_perm.gid;
106 old.sem_perm.mode = buf->sem_perm.mode;
107 if (old.sem_perm.uid != buf->sem_perm.uid ||
108 old.sem_perm.gid != buf->sem_perm.gid)
110 __set_errno (EINVAL);
111 return -1;
114 result = INLINE_SYSCALL (semctl, 4, semid, semnum, cmd,
115 CHECK_SEMCTL (&arg, semid, cmd)->array);
116 if (result != -1 && cmd != IPC_SET)
118 memset(buf, 0, sizeof(*buf));
119 buf->sem_perm.__key = old.sem_perm.__key;
120 buf->sem_perm.uid = old.sem_perm.uid;
121 buf->sem_perm.gid = old.sem_perm.gid;
122 buf->sem_perm.cuid = old.sem_perm.cuid;
123 buf->sem_perm.cgid = old.sem_perm.cgid;
124 buf->sem_perm.mode = old.sem_perm.mode;
125 buf->sem_perm.__seq = old.sem_perm.__seq;
126 buf->sem_otime = old.sem_otime;
127 buf->sem_ctime = old.sem_ctime;
128 buf->sem_nsems = old.sem_nsems;
130 return result;
132 #endif
135 #include <shlib-compat.h>
136 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);