]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/semctl.c
Add script to update copyright notices and reformat some to facilitate its use.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / semctl.c
1 /* Copyright (C) 1995-2012 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.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <sys/sem.h>
22 #include <ipc_priv.h>
23
24 #include <sysdep.h>
25 #include <string.h>
26 #include <sys/syscall.h>
27 #include <shlib-compat.h>
28
29 #include <kernel-features.h>
30
31 struct __old_semid_ds
32 {
33 struct __old_ipc_perm sem_perm; /* operation permission struct */
34 __time_t sem_otime; /* last semop() time */
35 __time_t sem_ctime; /* last time changed by semctl() */
36 struct sem *__unbounded __sembase; /* ptr to first semaphore in array */
37 struct sem_queue *__unbounded __sem_pending; /* pending operations */
38 struct sem_queue *__unbounded __sem_pending_last; /* last pending operation */
39 struct sem_undo *__unbounded __undo; /* ondo requests on this array */
40 unsigned short int sem_nsems; /* number of semaphores in set */
41 };
42
43 /* Define a `union semun' suitable for Linux here. */
44 union semun
45 {
46 int val; /* value for SETVAL */
47 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
48 unsigned short int *array; /* array for GETALL & SETALL */
49 struct seminfo *__buf; /* buffer for IPC_INFO */
50 struct __old_semid_ds *__old_buf;
51 };
52
53 #include <bp-checks.h>
54 #include <bp-semctl.h> /* definition of CHECK_SEMCTL needs union semum */
55
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, ...);
62
63 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
64 int
65 attribute_compat_text_section
66 __old_semctl (int semid, int semnum, int cmd, ...)
67 {
68 union semun arg;
69 va_list ap;
70
71 va_start (ap, cmd);
72
73 /* Get the argument only if required. */
74 arg.buf = NULL;
75 switch (cmd)
76 {
77 case SETVAL: /* arg.val */
78 case GETALL: /* arg.array */
79 case SETALL:
80 case IPC_STAT: /* arg.buf */
81 case IPC_SET:
82 case SEM_STAT:
83 case IPC_INFO: /* arg.__buf */
84 case SEM_INFO:
85 va_start (ap, cmd);
86 arg = va_arg (ap, union semun);
87 va_end (ap);
88 break;
89 }
90
91 va_end (ap);
92
93 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
94 CHECK_SEMCTL (&arg, semid, cmd));
95 }
96 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
97 #endif
98
99 int
100 __new_semctl (int semid, int semnum, int cmd, ...)
101 {
102 union semun arg;
103 va_list ap;
104
105 va_start (ap, cmd);
106
107 /* Get the argument only if required. */
108 arg.buf = NULL;
109 switch (cmd)
110 {
111 case SETVAL: /* arg.val */
112 case GETALL: /* arg.array */
113 case SETALL:
114 case IPC_STAT: /* arg.buf */
115 case IPC_SET:
116 case SEM_STAT:
117 case IPC_INFO: /* arg.__buf */
118 case SEM_INFO:
119 va_start (ap, cmd);
120 arg = va_arg (ap, union semun);
121 va_end (ap);
122 break;
123 }
124
125 va_end (ap);
126
127 #if __ASSUME_IPC64 > 0
128 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
129 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
130 #else
131 switch (cmd)
132 {
133 case SEM_STAT:
134 case IPC_STAT:
135 case IPC_SET:
136 break;
137 default:
138 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
139 CHECK_SEMCTL (&arg, semid, cmd));
140 }
141
142 {
143 int save_errno = errno, result;
144 struct __old_semid_ds old;
145 struct semid_ds *buf;
146
147 /* Unfortunately there is no way how to find out for sure whether
148 we should use old or new semctl. */
149 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
150 CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
151 if (result != -1 || errno != EINVAL)
152 return result;
153
154 __set_errno(save_errno);
155 buf = arg.buf;
156 arg.__old_buf = &old;
157 if (cmd == IPC_SET)
158 {
159 old.sem_perm.uid = buf->sem_perm.uid;
160 old.sem_perm.gid = buf->sem_perm.gid;
161 old.sem_perm.mode = buf->sem_perm.mode;
162 if (old.sem_perm.uid != buf->sem_perm.uid ||
163 old.sem_perm.gid != buf->sem_perm.gid)
164 {
165 __set_errno (EINVAL);
166 return -1;
167 }
168 }
169 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
170 CHECK_SEMCTL (&arg, semid, cmd));
171 if (result != -1 && cmd != IPC_SET)
172 {
173 memset(buf, 0, sizeof(*buf));
174 buf->sem_perm.__key = old.sem_perm.__key;
175 buf->sem_perm.uid = old.sem_perm.uid;
176 buf->sem_perm.gid = old.sem_perm.gid;
177 buf->sem_perm.cuid = old.sem_perm.cuid;
178 buf->sem_perm.cgid = old.sem_perm.cgid;
179 buf->sem_perm.mode = old.sem_perm.mode;
180 buf->sem_perm.__seq = old.sem_perm.__seq;
181 buf->sem_otime = old.sem_otime;
182 buf->sem_ctime = old.sem_ctime;
183 buf->sem_nsems = old.sem_nsems;
184 }
185 return result;
186 }
187 #endif
188 }
189
190 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);