Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / semctl.c
blob2d28cf661e9b3ad067585c73f6c32de3998d1b4d
1 /* Copyright (C) 1995,1997,1998,2000,2003,2004,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, see
18 <http://www.gnu.org/licenses/>. */
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 */
51 struct __old_semid_ds *__old_buf;
54 #include <bp-checks.h>
55 #include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
57 /* Return identifier for array of NSEMS semaphores associated with
58 KEY. */
59 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
60 int __old_semctl (int semid, int semnum, int cmd, ...);
61 #endif
62 int __new_semctl (int semid, int semnum, int cmd, ...);
64 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
65 int
66 attribute_compat_text_section
67 __old_semctl (int semid, int semnum, int cmd, ...)
69 union semun arg;
70 va_list ap;
72 va_start (ap, cmd);
74 /* Get the argument only if required. */
75 arg.buf = NULL;
76 switch (cmd)
78 case SETVAL: /* arg.val */
79 case GETALL: /* arg.array */
80 case SETALL:
81 case IPC_STAT: /* arg.buf */
82 case IPC_SET:
83 case SEM_STAT:
84 case IPC_INFO: /* arg.__buf */
85 case SEM_INFO:
86 va_start (ap, cmd);
87 arg = va_arg (ap, union semun);
88 va_end (ap);
89 break;
92 va_end (ap);
94 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
95 CHECK_SEMCTL (&arg, semid, cmd));
97 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
98 #endif
101 __new_semctl (int semid, int semnum, int cmd, ...)
103 union semun arg;
104 va_list ap;
106 va_start (ap, cmd);
108 /* Get the argument only if required. */
109 arg.buf = NULL;
110 switch (cmd)
112 case SETVAL: /* arg.val */
113 case GETALL: /* arg.array */
114 case SETALL:
115 case IPC_STAT: /* arg.buf */
116 case IPC_SET:
117 case SEM_STAT:
118 case IPC_INFO: /* arg.__buf */
119 case SEM_INFO:
120 va_start (ap, cmd);
121 arg = va_arg (ap, union semun);
122 va_end (ap);
123 break;
126 va_end (ap);
128 #if __ASSUME_IPC64 > 0
129 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
130 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
131 #else
132 switch (cmd)
134 case SEM_STAT:
135 case IPC_STAT:
136 case IPC_SET:
137 break;
138 default:
139 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
140 CHECK_SEMCTL (&arg, semid, cmd));
144 int save_errno = errno, result;
145 struct __old_semid_ds old;
146 struct semid_ds *buf;
148 /* Unfortunately there is no way how to find out for sure whether
149 we should use old or new semctl. */
150 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
151 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
152 if (result != -1 || errno != EINVAL)
153 return result;
155 __set_errno(save_errno);
156 buf = arg.buf;
157 arg.__old_buf = &old;
158 if (cmd == IPC_SET)
160 old.sem_perm.uid = buf->sem_perm.uid;
161 old.sem_perm.gid = buf->sem_perm.gid;
162 old.sem_perm.mode = buf->sem_perm.mode;
163 if (old.sem_perm.uid != buf->sem_perm.uid ||
164 old.sem_perm.gid != buf->sem_perm.gid)
166 __set_errno (EINVAL);
167 return -1;
170 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
171 CHECK_SEMCTL (&arg, semid, cmd));
172 if (result != -1 && cmd != IPC_SET)
174 memset(buf, 0, sizeof(*buf));
175 buf->sem_perm.__key = old.sem_perm.__key;
176 buf->sem_perm.uid = old.sem_perm.uid;
177 buf->sem_perm.gid = old.sem_perm.gid;
178 buf->sem_perm.cuid = old.sem_perm.cuid;
179 buf->sem_perm.cgid = old.sem_perm.cgid;
180 buf->sem_perm.mode = old.sem_perm.mode;
181 buf->sem_perm.__seq = old.sem_perm.__seq;
182 buf->sem_otime = old.sem_otime;
183 buf->sem_ctime = old.sem_ctime;
184 buf->sem_nsems = old.sem_nsems;
186 return result;
188 #endif
191 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);