sysv_sem.c revision 1.71.2.2 1 1.71.2.2 ad /* $NetBSD: sysv_sem.c,v 1.71.2.2 2007/08/04 10:53:00 ad Exp $ */
2 1.71.2.2 ad
3 1.71.2.2 ad /*-
4 1.71.2.2 ad * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
5 1.71.2.2 ad * All rights reserved.
6 1.71.2.2 ad *
7 1.71.2.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.71.2.2 ad * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.71.2.2 ad * NASA Ames Research Center, and by Andrew Doran.
10 1.71.2.2 ad *
11 1.71.2.2 ad * Redistribution and use in source and binary forms, with or without
12 1.71.2.2 ad * modification, are permitted provided that the following conditions
13 1.71.2.2 ad * are met:
14 1.71.2.2 ad * 1. Redistributions of source code must retain the above copyright
15 1.71.2.2 ad * notice, this list of conditions and the following disclaimer.
16 1.71.2.2 ad * 2. Redistributions in binary form must reproduce the above copyright
17 1.71.2.2 ad * notice, this list of conditions and the following disclaimer in the
18 1.71.2.2 ad * documentation and/or other materials provided with the distribution.
19 1.71.2.2 ad * 3. All advertising materials mentioning features or use of this software
20 1.71.2.2 ad * must display the following acknowledgement:
21 1.71.2.2 ad * This product includes software developed by the NetBSD
22 1.71.2.2 ad * Foundation, Inc. and its contributors.
23 1.71.2.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.71.2.2 ad * contributors may be used to endorse or promote products derived
25 1.71.2.2 ad * from this software without specific prior written permission.
26 1.71.2.2 ad *
27 1.71.2.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.71.2.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.71.2.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.71.2.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.71.2.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.71.2.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.71.2.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.71.2.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.71.2.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.71.2.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.71.2.2 ad * POSSIBILITY OF SUCH DAMAGE.
38 1.71.2.2 ad */
39 1.71.2.2 ad
40 1.71.2.2 ad /*
41 1.71.2.2 ad * Implementation of SVID semaphores
42 1.71.2.2 ad *
43 1.71.2.2 ad * Author: Daniel Boulet
44 1.71.2.2 ad *
45 1.71.2.2 ad * This software is provided ``AS IS'' without any warranties of any kind.
46 1.71.2.2 ad */
47 1.71.2.2 ad
48 1.71.2.2 ad #include <sys/cdefs.h>
49 1.71.2.2 ad __KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.71.2.2 2007/08/04 10:53:00 ad Exp $");
50 1.71.2.2 ad
51 1.71.2.2 ad #define SYSVSEM
52 1.71.2.2 ad
53 1.71.2.2 ad #include <sys/param.h>
54 1.71.2.2 ad #include <sys/kernel.h>
55 1.71.2.2 ad #include <sys/sem.h>
56 1.71.2.2 ad #include <sys/sysctl.h>
57 1.71.2.2 ad #include <sys/kmem.h>
58 1.71.2.2 ad #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
59 1.71.2.2 ad #include <sys/syscallargs.h>
60 1.71.2.2 ad #include <sys/kauth.h>
61 1.71.2.2 ad
62 1.71.2.2 ad static int semtot = 0;
63 1.71.2.2 ad struct semid_ds *sema; /* semaphore id pool */
64 1.71.2.2 ad static struct __sem *sem; /* semaphore pool */
65 1.71.2.2 ad static struct sem_undo *semu_list; /* list of active undo structures */
66 1.71.2.2 ad static int *semu; /* undo structure pool */
67 1.71.2.2 ad static kcondvar_t *semcv;
68 1.71.2.2 ad static kmutex_t semlock;
69 1.71.2.2 ad
70 1.71.2.2 ad #ifdef SEM_DEBUG
71 1.71.2.2 ad #define SEM_PRINTF(a) printf a
72 1.71.2.2 ad #else
73 1.71.2.2 ad #define SEM_PRINTF(a)
74 1.71.2.2 ad #endif
75 1.71.2.2 ad
76 1.71.2.2 ad struct sem_undo *semu_alloc(struct proc *);
77 1.71.2.2 ad int semundo_adjust(struct proc *, struct sem_undo **, int, int, int);
78 1.71.2.2 ad void semundo_clear(int, int);
79 1.71.2.2 ad
80 1.71.2.2 ad /*
81 1.71.2.2 ad * XXXSMP Once we go MP, there needs to be a lock for the semaphore system.
82 1.71.2.2 ad * Until then, we're saved by being a non-preemptive kernel.
83 1.71.2.2 ad */
84 1.71.2.2 ad
85 1.71.2.2 ad void
86 1.71.2.2 ad seminit(void)
87 1.71.2.2 ad {
88 1.71.2.2 ad int i, sz;
89 1.71.2.2 ad vaddr_t v;
90 1.71.2.2 ad
91 1.71.2.2 ad mutex_init(&semlock, MUTEX_DEFAULT, IPL_NONE);
92 1.71.2.2 ad
93 1.71.2.2 ad /* Allocate pageable memory for our structures */
94 1.71.2.2 ad sz = seminfo.semmni * sizeof(struct semid_ds) +
95 1.71.2.2 ad seminfo.semmns * sizeof(struct __sem) +
96 1.71.2.2 ad seminfo.semmnu * seminfo.semusz +
97 1.71.2.2 ad seminfo.semmni * sizeof(kcondvar_t);
98 1.71.2.2 ad v = uvm_km_alloc(kernel_map, round_page(sz), 0,
99 1.71.2.2 ad UVM_KMF_WIRED|UVM_KMF_ZERO);
100 1.71.2.2 ad if (v == 0)
101 1.71.2.2 ad panic("sysv_sem: cannot allocate memory");
102 1.71.2.2 ad sema = (void *)v;
103 1.71.2.2 ad sem = (void *)(sema + seminfo.semmni);
104 1.71.2.2 ad semu = (void *)(sem + seminfo.semmns);
105 1.71.2.2 ad semcv = (void *)(semu + seminfo.semmnu);
106 1.71.2.2 ad
107 1.71.2.2 ad for (i = 0; i < seminfo.semmni; i++) {
108 1.71.2.2 ad sema[i]._sem_base = 0;
109 1.71.2.2 ad sema[i].sem_perm.mode = 0;
110 1.71.2.2 ad cv_init(&semcv[i], "semwait");
111 1.71.2.2 ad }
112 1.71.2.2 ad for (i = 0; i < seminfo.semmnu; i++) {
113 1.71.2.2 ad struct sem_undo *suptr = SEMU(i);
114 1.71.2.2 ad suptr->un_proc = NULL;
115 1.71.2.2 ad }
116 1.71.2.2 ad semu_list = NULL;
117 1.71.2.2 ad exithook_establish(semexit, NULL);
118 1.71.2.2 ad }
119 1.71.2.2 ad
120 1.71.2.2 ad /*
121 1.71.2.2 ad * Placebo.
122 1.71.2.2 ad */
123 1.71.2.2 ad
124 1.71.2.2 ad int
125 1.71.2.2 ad sys_semconfig(struct lwp *l, void *v, register_t *retval)
126 1.71.2.2 ad {
127 1.71.2.2 ad
128 1.71.2.2 ad *retval = 0;
129 1.71.2.2 ad return 0;
130 1.71.2.2 ad }
131 1.71.2.2 ad
132 1.71.2.2 ad /*
133 1.71.2.2 ad * Allocate a new sem_undo structure for a process
134 1.71.2.2 ad * (returns ptr to structure or NULL if no more room)
135 1.71.2.2 ad */
136 1.71.2.2 ad
137 1.71.2.2 ad struct sem_undo *
138 1.71.2.2 ad semu_alloc(struct proc *p)
139 1.71.2.2 ad {
140 1.71.2.2 ad int i;
141 1.71.2.2 ad struct sem_undo *suptr;
142 1.71.2.2 ad struct sem_undo **supptr;
143 1.71.2.2 ad int attempt;
144 1.71.2.2 ad
145 1.71.2.2 ad KASSERT(mutex_owned(&semlock));
146 1.71.2.2 ad
147 1.71.2.2 ad /*
148 1.71.2.2 ad * Try twice to allocate something.
149 1.71.2.2 ad * (we'll purge any empty structures after the first pass so
150 1.71.2.2 ad * two passes are always enough)
151 1.71.2.2 ad */
152 1.71.2.2 ad
153 1.71.2.2 ad for (attempt = 0; attempt < 2; attempt++) {
154 1.71.2.2 ad /*
155 1.71.2.2 ad * Look for a free structure.
156 1.71.2.2 ad * Fill it in and return it if we find one.
157 1.71.2.2 ad */
158 1.71.2.2 ad
159 1.71.2.2 ad for (i = 0; i < seminfo.semmnu; i++) {
160 1.71.2.2 ad suptr = SEMU(i);
161 1.71.2.2 ad if (suptr->un_proc == NULL) {
162 1.71.2.2 ad suptr->un_next = semu_list;
163 1.71.2.2 ad semu_list = suptr;
164 1.71.2.2 ad suptr->un_cnt = 0;
165 1.71.2.2 ad suptr->un_proc = p;
166 1.71.2.2 ad return (suptr);
167 1.71.2.2 ad }
168 1.71.2.2 ad }
169 1.71.2.2 ad
170 1.71.2.2 ad /*
171 1.71.2.2 ad * We didn't find a free one, if this is the first attempt
172 1.71.2.2 ad * then try to free some structures.
173 1.71.2.2 ad */
174 1.71.2.2 ad
175 1.71.2.2 ad if (attempt == 0) {
176 1.71.2.2 ad /* All the structures are in use - try to free some */
177 1.71.2.2 ad int did_something = 0;
178 1.71.2.2 ad
179 1.71.2.2 ad supptr = &semu_list;
180 1.71.2.2 ad while ((suptr = *supptr) != NULL) {
181 1.71.2.2 ad if (suptr->un_cnt == 0) {
182 1.71.2.2 ad suptr->un_proc = NULL;
183 1.71.2.2 ad *supptr = suptr->un_next;
184 1.71.2.2 ad did_something = 1;
185 1.71.2.2 ad } else
186 1.71.2.2 ad supptr = &suptr->un_next;
187 1.71.2.2 ad }
188 1.71.2.2 ad
189 1.71.2.2 ad /* If we didn't free anything then just give-up */
190 1.71.2.2 ad if (!did_something)
191 1.71.2.2 ad return (NULL);
192 1.71.2.2 ad } else {
193 1.71.2.2 ad /*
194 1.71.2.2 ad * The second pass failed even though we freed
195 1.71.2.2 ad * something after the first pass!
196 1.71.2.2 ad * This is IMPOSSIBLE!
197 1.71.2.2 ad */
198 1.71.2.2 ad panic("semu_alloc - second attempt failed");
199 1.71.2.2 ad }
200 1.71.2.2 ad }
201 1.71.2.2 ad return NULL;
202 1.71.2.2 ad }
203 1.71.2.2 ad
204 1.71.2.2 ad /*
205 1.71.2.2 ad * Adjust a particular entry for a particular proc
206 1.71.2.2 ad */
207 1.71.2.2 ad
208 1.71.2.2 ad int
209 1.71.2.2 ad semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
210 1.71.2.2 ad int adjval)
211 1.71.2.2 ad {
212 1.71.2.2 ad struct sem_undo *suptr;
213 1.71.2.2 ad struct undo *sunptr;
214 1.71.2.2 ad int i;
215 1.71.2.2 ad
216 1.71.2.2 ad KASSERT(mutex_owned(&semlock));
217 1.71.2.2 ad
218 1.71.2.2 ad /*
219 1.71.2.2 ad * Look for and remember the sem_undo if the caller doesn't
220 1.71.2.2 ad * provide it
221 1.71.2.2 ad */
222 1.71.2.2 ad
223 1.71.2.2 ad suptr = *supptr;
224 1.71.2.2 ad if (suptr == NULL) {
225 1.71.2.2 ad for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
226 1.71.2.2 ad if (suptr->un_proc == p)
227 1.71.2.2 ad break;
228 1.71.2.2 ad
229 1.71.2.2 ad if (suptr == NULL) {
230 1.71.2.2 ad suptr = semu_alloc(p);
231 1.71.2.2 ad if (suptr == NULL)
232 1.71.2.2 ad return (ENOSPC);
233 1.71.2.2 ad }
234 1.71.2.2 ad *supptr = suptr;
235 1.71.2.2 ad }
236 1.71.2.2 ad
237 1.71.2.2 ad /*
238 1.71.2.2 ad * Look for the requested entry and adjust it (delete if
239 1.71.2.2 ad * adjval becomes 0).
240 1.71.2.2 ad */
241 1.71.2.2 ad sunptr = &suptr->un_ent[0];
242 1.71.2.2 ad for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
243 1.71.2.2 ad if (sunptr->un_id != semid || sunptr->un_num != semnum)
244 1.71.2.2 ad continue;
245 1.71.2.2 ad sunptr->un_adjval += adjval;
246 1.71.2.2 ad if (sunptr->un_adjval == 0) {
247 1.71.2.2 ad suptr->un_cnt--;
248 1.71.2.2 ad if (i < suptr->un_cnt)
249 1.71.2.2 ad suptr->un_ent[i] =
250 1.71.2.2 ad suptr->un_ent[suptr->un_cnt];
251 1.71.2.2 ad }
252 1.71.2.2 ad return (0);
253 1.71.2.2 ad }
254 1.71.2.2 ad
255 1.71.2.2 ad /* Didn't find the right entry - create it */
256 1.71.2.2 ad if (suptr->un_cnt == SEMUME)
257 1.71.2.2 ad return (EINVAL);
258 1.71.2.2 ad
259 1.71.2.2 ad sunptr = &suptr->un_ent[suptr->un_cnt];
260 1.71.2.2 ad suptr->un_cnt++;
261 1.71.2.2 ad sunptr->un_adjval = adjval;
262 1.71.2.2 ad sunptr->un_id = semid;
263 1.71.2.2 ad sunptr->un_num = semnum;
264 1.71.2.2 ad return (0);
265 1.71.2.2 ad }
266 1.71.2.2 ad
267 1.71.2.2 ad void
268 1.71.2.2 ad semundo_clear(int semid, int semnum)
269 1.71.2.2 ad {
270 1.71.2.2 ad struct sem_undo *suptr;
271 1.71.2.2 ad struct undo *sunptr, *sunend;
272 1.71.2.2 ad
273 1.71.2.2 ad KASSERT(mutex_owned(&semlock));
274 1.71.2.2 ad
275 1.71.2.2 ad for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
276 1.71.2.2 ad for (sunptr = &suptr->un_ent[0],
277 1.71.2.2 ad sunend = sunptr + suptr->un_cnt; sunptr < sunend;) {
278 1.71.2.2 ad if (sunptr->un_id == semid) {
279 1.71.2.2 ad if (semnum == -1 || sunptr->un_num == semnum) {
280 1.71.2.2 ad suptr->un_cnt--;
281 1.71.2.2 ad sunend--;
282 1.71.2.2 ad if (sunptr != sunend)
283 1.71.2.2 ad *sunptr = *sunend;
284 1.71.2.2 ad if (semnum != -1)
285 1.71.2.2 ad break;
286 1.71.2.2 ad else
287 1.71.2.2 ad continue;
288 1.71.2.2 ad }
289 1.71.2.2 ad }
290 1.71.2.2 ad sunptr++;
291 1.71.2.2 ad }
292 1.71.2.2 ad }
293 1.71.2.2 ad
294 1.71.2.2 ad int
295 1.71.2.2 ad sys_____semctl13(struct lwp *l, void *v, register_t *retval)
296 1.71.2.2 ad {
297 1.71.2.2 ad struct sys_____semctl13_args /* {
298 1.71.2.2 ad syscallarg(int) semid;
299 1.71.2.2 ad syscallarg(int) semnum;
300 1.71.2.2 ad syscallarg(int) cmd;
301 1.71.2.2 ad syscallarg(union __semun *) arg;
302 1.71.2.2 ad } */ *uap = v;
303 1.71.2.2 ad struct semid_ds sembuf;
304 1.71.2.2 ad int cmd, error;
305 1.71.2.2 ad void *pass_arg;
306 1.71.2.2 ad union __semun karg;
307 1.71.2.2 ad
308 1.71.2.2 ad cmd = SCARG(uap, cmd);
309 1.71.2.2 ad
310 1.71.2.2 ad pass_arg = get_semctl_arg(cmd, &sembuf, &karg);
311 1.71.2.2 ad
312 1.71.2.2 ad if (pass_arg) {
313 1.71.2.2 ad error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
314 1.71.2.2 ad if (error)
315 1.71.2.2 ad return error;
316 1.71.2.2 ad if (cmd == IPC_SET) {
317 1.71.2.2 ad error = copyin(karg.buf, &sembuf, sizeof(sembuf));
318 1.71.2.2 ad if (error)
319 1.71.2.2 ad return (error);
320 1.71.2.2 ad }
321 1.71.2.2 ad }
322 1.71.2.2 ad
323 1.71.2.2 ad error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
324 1.71.2.2 ad pass_arg, retval);
325 1.71.2.2 ad
326 1.71.2.2 ad if (error == 0 && cmd == IPC_STAT)
327 1.71.2.2 ad error = copyout(&sembuf, karg.buf, sizeof(sembuf));
328 1.71.2.2 ad
329 1.71.2.2 ad return (error);
330 1.71.2.2 ad }
331 1.71.2.2 ad
332 1.71.2.2 ad int
333 1.71.2.2 ad semctl1(struct lwp *l, int semid, int semnum, int cmd, void *v,
334 1.71.2.2 ad register_t *retval)
335 1.71.2.2 ad {
336 1.71.2.2 ad kauth_cred_t cred = l->l_cred;
337 1.71.2.2 ad union __semun *arg = v;
338 1.71.2.2 ad struct semid_ds *sembuf = v, *semaptr;
339 1.71.2.2 ad int i, error, ix;
340 1.71.2.2 ad
341 1.71.2.2 ad SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
342 1.71.2.2 ad semid, semnum, cmd, v));
343 1.71.2.2 ad
344 1.71.2.2 ad mutex_enter(&semlock);
345 1.71.2.2 ad
346 1.71.2.2 ad ix = IPCID_TO_IX(semid);
347 1.71.2.2 ad if (ix < 0 || ix >= seminfo.semmni) {
348 1.71.2.2 ad mutex_exit(&semlock);
349 1.71.2.2 ad return (EINVAL);
350 1.71.2.2 ad }
351 1.71.2.2 ad
352 1.71.2.2 ad semaptr = &sema[ix];
353 1.71.2.2 ad if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
354 1.71.2.2 ad semaptr->sem_perm._seq != IPCID_TO_SEQ(semid)) {
355 1.71.2.2 ad mutex_exit(&semlock);
356 1.71.2.2 ad return (EINVAL);
357 1.71.2.2 ad }
358 1.71.2.2 ad
359 1.71.2.2 ad switch (cmd) {
360 1.71.2.2 ad case IPC_RMID:
361 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
362 1.71.2.2 ad break;
363 1.71.2.2 ad semaptr->sem_perm.cuid = kauth_cred_geteuid(cred);
364 1.71.2.2 ad semaptr->sem_perm.uid = kauth_cred_geteuid(cred);
365 1.71.2.2 ad semtot -= semaptr->sem_nsems;
366 1.71.2.2 ad for (i = semaptr->_sem_base - sem; i < semtot; i++)
367 1.71.2.2 ad sem[i] = sem[i + semaptr->sem_nsems];
368 1.71.2.2 ad for (i = 0; i < seminfo.semmni; i++) {
369 1.71.2.2 ad if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
370 1.71.2.2 ad sema[i]._sem_base > semaptr->_sem_base)
371 1.71.2.2 ad sema[i]._sem_base -= semaptr->sem_nsems;
372 1.71.2.2 ad }
373 1.71.2.2 ad semaptr->sem_perm.mode = 0;
374 1.71.2.2 ad semundo_clear(ix, -1);
375 1.71.2.2 ad cv_broadcast(&semcv[ix]);
376 1.71.2.2 ad break;
377 1.71.2.2 ad
378 1.71.2.2 ad case IPC_SET:
379 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
380 1.71.2.2 ad break;
381 1.71.2.2 ad KASSERT(sembuf != NULL);
382 1.71.2.2 ad semaptr->sem_perm.uid = sembuf->sem_perm.uid;
383 1.71.2.2 ad semaptr->sem_perm.gid = sembuf->sem_perm.gid;
384 1.71.2.2 ad semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
385 1.71.2.2 ad (sembuf->sem_perm.mode & 0777);
386 1.71.2.2 ad semaptr->sem_ctime = time_second;
387 1.71.2.2 ad break;
388 1.71.2.2 ad
389 1.71.2.2 ad case IPC_STAT:
390 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
391 1.71.2.2 ad break;
392 1.71.2.2 ad KASSERT(sembuf != NULL);
393 1.71.2.2 ad memcpy(sembuf, semaptr, sizeof(struct semid_ds));
394 1.71.2.2 ad break;
395 1.71.2.2 ad
396 1.71.2.2 ad case GETNCNT:
397 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
398 1.71.2.2 ad break;
399 1.71.2.2 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
400 1.71.2.2 ad error = EINVAL;
401 1.71.2.2 ad break;
402 1.71.2.2 ad }
403 1.71.2.2 ad *retval = semaptr->_sem_base[semnum].semncnt;
404 1.71.2.2 ad break;
405 1.71.2.2 ad
406 1.71.2.2 ad case GETPID:
407 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
408 1.71.2.2 ad break;
409 1.71.2.2 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
410 1.71.2.2 ad error = EINVAL;
411 1.71.2.2 ad break;
412 1.71.2.2 ad }
413 1.71.2.2 ad *retval = semaptr->_sem_base[semnum].sempid;
414 1.71.2.2 ad break;
415 1.71.2.2 ad
416 1.71.2.2 ad case GETVAL:
417 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
418 1.71.2.2 ad break;
419 1.71.2.2 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
420 1.71.2.2 ad error = EINVAL;
421 1.71.2.2 ad break;
422 1.71.2.2 ad }
423 1.71.2.2 ad *retval = semaptr->_sem_base[semnum].semval;
424 1.71.2.2 ad break;
425 1.71.2.2 ad
426 1.71.2.2 ad case GETALL:
427 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
428 1.71.2.2 ad break;
429 1.71.2.2 ad KASSERT(arg != NULL);
430 1.71.2.2 ad for (i = 0; i < semaptr->sem_nsems; i++) {
431 1.71.2.2 ad error = copyout(&semaptr->_sem_base[i].semval,
432 1.71.2.2 ad &arg->array[i], sizeof(arg->array[i]));
433 1.71.2.2 ad if (error != 0)
434 1.71.2.2 ad break;
435 1.71.2.2 ad }
436 1.71.2.2 ad break;
437 1.71.2.2 ad
438 1.71.2.2 ad case GETZCNT:
439 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
440 1.71.2.2 ad break;
441 1.71.2.2 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
442 1.71.2.2 ad error = EINVAL;
443 1.71.2.2 ad break;
444 1.71.2.2 ad }
445 1.71.2.2 ad *retval = semaptr->_sem_base[semnum].semzcnt;
446 1.71.2.2 ad break;
447 1.71.2.2 ad
448 1.71.2.2 ad case SETVAL:
449 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
450 1.71.2.2 ad break;
451 1.71.2.2 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
452 1.71.2.2 ad error = EINVAL;
453 1.71.2.2 ad break;
454 1.71.2.2 ad }
455 1.71.2.2 ad KASSERT(arg != NULL);
456 1.71.2.2 ad semaptr->_sem_base[semnum].semval = arg->val;
457 1.71.2.2 ad semundo_clear(ix, semnum);
458 1.71.2.2 ad cv_broadcast(&semcv[ix]);
459 1.71.2.2 ad break;
460 1.71.2.2 ad
461 1.71.2.2 ad case SETALL:
462 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
463 1.71.2.2 ad break;
464 1.71.2.2 ad KASSERT(arg != NULL);
465 1.71.2.2 ad for (i = 0; i < semaptr->sem_nsems; i++) {
466 1.71.2.2 ad error = copyin(&arg->array[i],
467 1.71.2.2 ad &semaptr->_sem_base[i].semval,
468 1.71.2.2 ad sizeof(arg->array[i]));
469 1.71.2.2 ad if (error != 0)
470 1.71.2.2 ad break;
471 1.71.2.2 ad }
472 1.71.2.2 ad semundo_clear(ix, -1);
473 1.71.2.2 ad cv_broadcast(&semcv[ix]);
474 1.71.2.2 ad break;
475 1.71.2.2 ad
476 1.71.2.2 ad default:
477 1.71.2.2 ad error = EINVAL;
478 1.71.2.2 ad break;
479 1.71.2.2 ad }
480 1.71.2.2 ad
481 1.71.2.2 ad mutex_exit(&semlock);
482 1.71.2.2 ad return (error);
483 1.71.2.2 ad }
484 1.71.2.2 ad
485 1.71.2.2 ad int
486 1.71.2.2 ad sys_semget(struct lwp *l, void *v, register_t *retval)
487 1.71.2.2 ad {
488 1.71.2.2 ad struct sys_semget_args /* {
489 1.71.2.2 ad syscallarg(key_t) key;
490 1.71.2.2 ad syscallarg(int) nsems;
491 1.71.2.2 ad syscallarg(int) semflg;
492 1.71.2.2 ad } */ *uap = v;
493 1.71.2.2 ad int semid, error = 0;
494 1.71.2.2 ad int key = SCARG(uap, key);
495 1.71.2.2 ad int nsems = SCARG(uap, nsems);
496 1.71.2.2 ad int semflg = SCARG(uap, semflg);
497 1.71.2.2 ad kauth_cred_t cred = l->l_cred;
498 1.71.2.2 ad
499 1.71.2.2 ad SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
500 1.71.2.2 ad
501 1.71.2.2 ad mutex_enter(&semlock);
502 1.71.2.2 ad
503 1.71.2.2 ad if (key != IPC_PRIVATE) {
504 1.71.2.2 ad for (semid = 0; semid < seminfo.semmni; semid++) {
505 1.71.2.2 ad if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
506 1.71.2.2 ad sema[semid].sem_perm._key == key)
507 1.71.2.2 ad break;
508 1.71.2.2 ad }
509 1.71.2.2 ad if (semid < seminfo.semmni) {
510 1.71.2.2 ad SEM_PRINTF(("found public key\n"));
511 1.71.2.2 ad if ((error = ipcperm(cred, &sema[semid].sem_perm,
512 1.71.2.2 ad semflg & 0700)))
513 1.71.2.2 ad goto out;
514 1.71.2.2 ad if (nsems > 0 && sema[semid].sem_nsems < nsems) {
515 1.71.2.2 ad SEM_PRINTF(("too small\n"));
516 1.71.2.2 ad error = EINVAL;
517 1.71.2.2 ad goto out;
518 1.71.2.2 ad }
519 1.71.2.2 ad if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
520 1.71.2.2 ad SEM_PRINTF(("not exclusive\n"));
521 1.71.2.2 ad error = EEXIST;
522 1.71.2.2 ad goto out;
523 1.71.2.2 ad }
524 1.71.2.2 ad goto found;
525 1.71.2.2 ad }
526 1.71.2.2 ad }
527 1.71.2.2 ad
528 1.71.2.2 ad SEM_PRINTF(("need to allocate the semid_ds\n"));
529 1.71.2.2 ad if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
530 1.71.2.2 ad if (nsems <= 0 || nsems > seminfo.semmsl) {
531 1.71.2.2 ad SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
532 1.71.2.2 ad seminfo.semmsl));
533 1.71.2.2 ad error = EINVAL;
534 1.71.2.2 ad goto out;
535 1.71.2.2 ad }
536 1.71.2.2 ad if (nsems > seminfo.semmns - semtot) {
537 1.71.2.2 ad SEM_PRINTF(("not enough semaphores left "
538 1.71.2.2 ad "(need %d, got %d)\n",
539 1.71.2.2 ad nsems, seminfo.semmns - semtot));
540 1.71.2.2 ad error = ENOSPC;
541 1.71.2.2 ad goto out;
542 1.71.2.2 ad }
543 1.71.2.2 ad for (semid = 0; semid < seminfo.semmni; semid++) {
544 1.71.2.2 ad if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
545 1.71.2.2 ad break;
546 1.71.2.2 ad }
547 1.71.2.2 ad if (semid == seminfo.semmni) {
548 1.71.2.2 ad SEM_PRINTF(("no more semid_ds's available\n"));
549 1.71.2.2 ad error = ENOSPC;
550 1.71.2.2 ad goto out;
551 1.71.2.2 ad }
552 1.71.2.2 ad SEM_PRINTF(("semid %d is available\n", semid));
553 1.71.2.2 ad sema[semid].sem_perm._key = key;
554 1.71.2.2 ad sema[semid].sem_perm.cuid = kauth_cred_geteuid(cred);
555 1.71.2.2 ad sema[semid].sem_perm.uid = kauth_cred_geteuid(cred);
556 1.71.2.2 ad sema[semid].sem_perm.cgid = kauth_cred_getegid(cred);
557 1.71.2.2 ad sema[semid].sem_perm.gid = kauth_cred_getegid(cred);
558 1.71.2.2 ad sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
559 1.71.2.2 ad sema[semid].sem_perm._seq =
560 1.71.2.2 ad (sema[semid].sem_perm._seq + 1) & 0x7fff;
561 1.71.2.2 ad sema[semid].sem_nsems = nsems;
562 1.71.2.2 ad sema[semid].sem_otime = 0;
563 1.71.2.2 ad sema[semid].sem_ctime = time_second;
564 1.71.2.2 ad sema[semid]._sem_base = &sem[semtot];
565 1.71.2.2 ad semtot += nsems;
566 1.71.2.2 ad memset(sema[semid]._sem_base, 0,
567 1.71.2.2 ad sizeof(sema[semid]._sem_base[0]) * nsems);
568 1.71.2.2 ad SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
569 1.71.2.2 ad &sem[semtot]));
570 1.71.2.2 ad } else {
571 1.71.2.2 ad SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
572 1.71.2.2 ad error = ENOENT;
573 1.71.2.2 ad goto out;
574 1.71.2.2 ad }
575 1.71.2.2 ad
576 1.71.2.2 ad found:
577 1.71.2.2 ad *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
578 1.71.2.2 ad out:
579 1.71.2.2 ad mutex_exit(&semlock);
580 1.71.2.2 ad return (error);
581 1.71.2.2 ad }
582 1.71.2.2 ad
583 1.71.2.2 ad #define SMALL_SOPS 8
584 1.71.2.2 ad
585 1.71.2.2 ad int
586 1.71.2.2 ad sys_semop(struct lwp *l, void *v, register_t *retval)
587 1.71.2.2 ad {
588 1.71.2.2 ad struct sys_semop_args /* {
589 1.71.2.2 ad syscallarg(int) semid;
590 1.71.2.2 ad syscallarg(struct sembuf *) sops;
591 1.71.2.2 ad syscallarg(size_t) nsops;
592 1.71.2.2 ad } */ *uap = v;
593 1.71.2.2 ad struct proc *p = l->l_proc;
594 1.71.2.2 ad int semid = SCARG(uap, semid), seq;
595 1.71.2.2 ad size_t nsops = SCARG(uap, nsops);
596 1.71.2.2 ad struct sembuf small_sops[SMALL_SOPS];
597 1.71.2.2 ad struct sembuf *sops;
598 1.71.2.2 ad struct semid_ds *semaptr;
599 1.71.2.2 ad struct sembuf *sopptr = NULL;
600 1.71.2.2 ad struct __sem *semptr = NULL;
601 1.71.2.2 ad struct sem_undo *suptr = NULL;
602 1.71.2.2 ad kauth_cred_t cred = l->l_cred;
603 1.71.2.2 ad int i, error;
604 1.71.2.2 ad int do_wakeup, do_undos;
605 1.71.2.2 ad
606 1.71.2.2 ad SEM_PRINTF(("call to semop(%d, %p, %zd)\n", semid, SCARG(uap,sops), nsops));
607 1.71.2.2 ad
608 1.71.2.2 ad if (nsops <= SMALL_SOPS) {
609 1.71.2.2 ad sops = small_sops;
610 1.71.2.2 ad } else if (nsops <= seminfo.semopm) {
611 1.71.2.2 ad KERNEL_LOCK(1, l); /* XXXSMP */
612 1.71.2.2 ad sops = kmem_alloc(nsops * sizeof(*sops), KM_SLEEP);
613 1.71.2.2 ad KERNEL_UNLOCK_ONE(l); /* XXXSMP */
614 1.71.2.2 ad } else {
615 1.71.2.2 ad SEM_PRINTF(("too many sops (max=%d, nsops=%zd)\n",
616 1.71.2.2 ad seminfo.semopm, nsops));
617 1.71.2.2 ad return (E2BIG);
618 1.71.2.2 ad }
619 1.71.2.2 ad
620 1.71.2.2 ad mutex_enter(&semlock);
621 1.71.2.2 ad
622 1.71.2.2 ad semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
623 1.71.2.2 ad if (semid < 0 || semid >= seminfo.semmni) {
624 1.71.2.2 ad error = EINVAL;
625 1.71.2.2 ad goto out;
626 1.71.2.2 ad }
627 1.71.2.2 ad
628 1.71.2.2 ad semaptr = &sema[semid];
629 1.71.2.2 ad seq = IPCID_TO_SEQ(SCARG(uap, semid));
630 1.71.2.2 ad if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
631 1.71.2.2 ad semaptr->sem_perm._seq != seq) {
632 1.71.2.2 ad error = EINVAL;
633 1.71.2.2 ad goto out;
634 1.71.2.2 ad }
635 1.71.2.2 ad
636 1.71.2.2 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
637 1.71.2.2 ad SEM_PRINTF(("error = %d from ipaccess\n", error));
638 1.71.2.2 ad goto out;
639 1.71.2.2 ad }
640 1.71.2.2 ad
641 1.71.2.2 ad if ((error = copyin(SCARG(uap, sops),
642 1.71.2.2 ad sops, nsops * sizeof(sops[0]))) != 0) {
643 1.71.2.2 ad SEM_PRINTF(("error = %d from copyin(%p, %p, %zd)\n", error,
644 1.71.2.2 ad SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
645 1.71.2.2 ad goto out;
646 1.71.2.2 ad }
647 1.71.2.2 ad
648 1.71.2.2 ad for (i = 0; i < nsops; i++)
649 1.71.2.2 ad if (sops[i].sem_num >= semaptr->sem_nsems) {
650 1.71.2.2 ad error = EFBIG;
651 1.71.2.2 ad goto out;
652 1.71.2.2 ad }
653 1.71.2.2 ad
654 1.71.2.2 ad /*
655 1.71.2.2 ad * Loop trying to satisfy the vector of requests.
656 1.71.2.2 ad * If we reach a point where we must wait, any requests already
657 1.71.2.2 ad * performed are rolled back and we go to sleep until some other
658 1.71.2.2 ad * process wakes us up. At this point, we start all over again.
659 1.71.2.2 ad *
660 1.71.2.2 ad * This ensures that from the perspective of other tasks, a set
661 1.71.2.2 ad * of requests is atomic (never partially satisfied).
662 1.71.2.2 ad */
663 1.71.2.2 ad do_undos = 0;
664 1.71.2.2 ad
665 1.71.2.2 ad for (;;) {
666 1.71.2.2 ad do_wakeup = 0;
667 1.71.2.2 ad
668 1.71.2.2 ad for (i = 0; i < nsops; i++) {
669 1.71.2.2 ad sopptr = &sops[i];
670 1.71.2.2 ad semptr = &semaptr->_sem_base[sopptr->sem_num];
671 1.71.2.2 ad
672 1.71.2.2 ad SEM_PRINTF(("semop: semaptr=%p, sem_base=%p, "
673 1.71.2.2 ad "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
674 1.71.2.2 ad semaptr, semaptr->_sem_base, semptr,
675 1.71.2.2 ad sopptr->sem_num, semptr->semval, sopptr->sem_op,
676 1.71.2.2 ad (sopptr->sem_flg & IPC_NOWAIT) ?
677 1.71.2.2 ad "nowait" : "wait"));
678 1.71.2.2 ad
679 1.71.2.2 ad if (sopptr->sem_op < 0) {
680 1.71.2.2 ad if ((int)(semptr->semval +
681 1.71.2.2 ad sopptr->sem_op) < 0) {
682 1.71.2.2 ad SEM_PRINTF(("semop: "
683 1.71.2.2 ad "can't do it now\n"));
684 1.71.2.2 ad break;
685 1.71.2.2 ad } else {
686 1.71.2.2 ad semptr->semval += sopptr->sem_op;
687 1.71.2.2 ad if (semptr->semval == 0 &&
688 1.71.2.2 ad semptr->semzcnt > 0)
689 1.71.2.2 ad do_wakeup = 1;
690 1.71.2.2 ad }
691 1.71.2.2 ad if (sopptr->sem_flg & SEM_UNDO)
692 1.71.2.2 ad do_undos = 1;
693 1.71.2.2 ad } else if (sopptr->sem_op == 0) {
694 1.71.2.2 ad if (semptr->semval > 0) {
695 1.71.2.2 ad SEM_PRINTF(("semop: not zero now\n"));
696 1.71.2.2 ad break;
697 1.71.2.2 ad }
698 1.71.2.2 ad } else {
699 1.71.2.2 ad if (semptr->semncnt > 0)
700 1.71.2.2 ad do_wakeup = 1;
701 1.71.2.2 ad semptr->semval += sopptr->sem_op;
702 1.71.2.2 ad if (sopptr->sem_flg & SEM_UNDO)
703 1.71.2.2 ad do_undos = 1;
704 1.71.2.2 ad }
705 1.71.2.2 ad }
706 1.71.2.2 ad
707 1.71.2.2 ad /*
708 1.71.2.2 ad * Did we get through the entire vector?
709 1.71.2.2 ad */
710 1.71.2.2 ad if (i >= nsops)
711 1.71.2.2 ad goto done;
712 1.71.2.2 ad
713 1.71.2.2 ad /*
714 1.71.2.2 ad * No ... rollback anything that we've already done
715 1.71.2.2 ad */
716 1.71.2.2 ad SEM_PRINTF(("semop: rollback 0 through %d\n", i - 1));
717 1.71.2.2 ad while (i-- > 0)
718 1.71.2.2 ad semaptr->_sem_base[sops[i].sem_num].semval -=
719 1.71.2.2 ad sops[i].sem_op;
720 1.71.2.2 ad
721 1.71.2.2 ad /*
722 1.71.2.2 ad * If the request that we couldn't satisfy has the
723 1.71.2.2 ad * NOWAIT flag set then return with EAGAIN.
724 1.71.2.2 ad */
725 1.71.2.2 ad if (sopptr->sem_flg & IPC_NOWAIT) {
726 1.71.2.2 ad error = EAGAIN;
727 1.71.2.2 ad goto out;
728 1.71.2.2 ad }
729 1.71.2.2 ad
730 1.71.2.2 ad if (sopptr->sem_op == 0)
731 1.71.2.2 ad semptr->semzcnt++;
732 1.71.2.2 ad else
733 1.71.2.2 ad semptr->semncnt++;
734 1.71.2.2 ad
735 1.71.2.2 ad SEM_PRINTF(("semop: good night!\n"));
736 1.71.2.2 ad error = cv_wait_sig(&semcv[semid], &semlock);
737 1.71.2.2 ad SEM_PRINTF(("semop: good morning (error=%d)!\n", error));
738 1.71.2.2 ad
739 1.71.2.2 ad /*
740 1.71.2.2 ad * Make sure that the semaphore still exists
741 1.71.2.2 ad */
742 1.71.2.2 ad if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
743 1.71.2.2 ad semaptr->sem_perm._seq != seq) {
744 1.71.2.2 ad error = EIDRM;
745 1.71.2.2 ad goto out;
746 1.71.2.2 ad }
747 1.71.2.2 ad
748 1.71.2.2 ad /*
749 1.71.2.2 ad * The semaphore is still alive. Readjust the count of
750 1.71.2.2 ad * waiting processes.
751 1.71.2.2 ad */
752 1.71.2.2 ad semptr = &semaptr->_sem_base[sopptr->sem_num];
753 1.71.2.2 ad if (sopptr->sem_op == 0)
754 1.71.2.2 ad semptr->semzcnt--;
755 1.71.2.2 ad else
756 1.71.2.2 ad semptr->semncnt--;
757 1.71.2.2 ad /*
758 1.71.2.2 ad * Is it really morning, or was our sleep interrupted?
759 1.71.2.2 ad * (Delayed check of tsleep() return code because we
760 1.71.2.2 ad * need to decrement sem[nz]cnt either way.)
761 1.71.2.2 ad */
762 1.71.2.2 ad if (error != 0) {
763 1.71.2.2 ad error = EINTR;
764 1.71.2.2 ad goto out;
765 1.71.2.2 ad }
766 1.71.2.2 ad SEM_PRINTF(("semop: good morning!\n"));
767 1.71.2.2 ad }
768 1.71.2.2 ad
769 1.71.2.2 ad done:
770 1.71.2.2 ad /*
771 1.71.2.2 ad * Process any SEM_UNDO requests.
772 1.71.2.2 ad */
773 1.71.2.2 ad if (do_undos) {
774 1.71.2.2 ad for (i = 0; i < nsops; i++) {
775 1.71.2.2 ad /*
776 1.71.2.2 ad * We only need to deal with SEM_UNDO's for non-zero
777 1.71.2.2 ad * op's.
778 1.71.2.2 ad */
779 1.71.2.2 ad int adjval;
780 1.71.2.2 ad
781 1.71.2.2 ad if ((sops[i].sem_flg & SEM_UNDO) == 0)
782 1.71.2.2 ad continue;
783 1.71.2.2 ad adjval = sops[i].sem_op;
784 1.71.2.2 ad if (adjval == 0)
785 1.71.2.2 ad continue;
786 1.71.2.2 ad error = semundo_adjust(p, &suptr, semid,
787 1.71.2.2 ad sops[i].sem_num, -adjval);
788 1.71.2.2 ad if (error == 0)
789 1.71.2.2 ad continue;
790 1.71.2.2 ad
791 1.71.2.2 ad /*
792 1.71.2.2 ad * Oh-Oh! We ran out of either sem_undo's or undo's.
793 1.71.2.2 ad * Rollback the adjustments to this point and then
794 1.71.2.2 ad * rollback the semaphore ups and down so we can return
795 1.71.2.2 ad * with an error with all structures restored. We
796 1.71.2.2 ad * rollback the undo's in the exact reverse order that
797 1.71.2.2 ad * we applied them. This guarantees that we won't run
798 1.71.2.2 ad * out of space as we roll things back out.
799 1.71.2.2 ad */
800 1.71.2.2 ad while (i-- > 0) {
801 1.71.2.2 ad if ((sops[i].sem_flg & SEM_UNDO) == 0)
802 1.71.2.2 ad continue;
803 1.71.2.2 ad adjval = sops[i].sem_op;
804 1.71.2.2 ad if (adjval == 0)
805 1.71.2.2 ad continue;
806 1.71.2.2 ad if (semundo_adjust(p, &suptr, semid,
807 1.71.2.2 ad sops[i].sem_num, adjval) != 0)
808 1.71.2.2 ad panic("semop - can't undo undos");
809 1.71.2.2 ad }
810 1.71.2.2 ad
811 1.71.2.2 ad for (i = 0; i < nsops; i++)
812 1.71.2.2 ad semaptr->_sem_base[sops[i].sem_num].semval -=
813 1.71.2.2 ad sops[i].sem_op;
814 1.71.2.2 ad
815 1.71.2.2 ad SEM_PRINTF(("error = %d from semundo_adjust\n", error));
816 1.71.2.2 ad goto out;
817 1.71.2.2 ad } /* loop through the sops */
818 1.71.2.2 ad } /* if (do_undos) */
819 1.71.2.2 ad
820 1.71.2.2 ad /* We're definitely done - set the sempid's */
821 1.71.2.2 ad for (i = 0; i < nsops; i++) {
822 1.71.2.2 ad sopptr = &sops[i];
823 1.71.2.2 ad semptr = &semaptr->_sem_base[sopptr->sem_num];
824 1.71.2.2 ad semptr->sempid = p->p_pid;
825 1.71.2.2 ad }
826 1.71.2.2 ad
827 1.71.2.2 ad /* Update sem_otime */
828 1.71.2.2 ad semaptr->sem_otime = time_second;
829 1.71.2.2 ad
830 1.71.2.2 ad /* Do a wakeup if any semaphore was up'd. */
831 1.71.2.2 ad if (do_wakeup) {
832 1.71.2.2 ad SEM_PRINTF(("semop: doing wakeup\n"));
833 1.71.2.2 ad cv_broadcast(&semcv[semid]);
834 1.71.2.2 ad SEM_PRINTF(("semop: back from wakeup\n"));
835 1.71.2.2 ad }
836 1.71.2.2 ad SEM_PRINTF(("semop: done\n"));
837 1.71.2.2 ad *retval = 0;
838 1.71.2.2 ad
839 1.71.2.2 ad out:
840 1.71.2.2 ad mutex_exit(&semlock);
841 1.71.2.2 ad if (sops != small_sops) {
842 1.71.2.2 ad KERNEL_LOCK(1, l); /* XXXSMP */
843 1.71.2.2 ad kmem_free(sops, nsops * sizeof(*sops));
844 1.71.2.2 ad KERNEL_UNLOCK_ONE(l); /* XXXSMP */
845 1.71.2.2 ad }
846 1.71.2.2 ad return error;
847 1.71.2.2 ad }
848 1.71.2.2 ad
849 1.71.2.2 ad /*
850 1.71.2.2 ad * Go through the undo structures for this process and apply the
851 1.71.2.2 ad * adjustments to semaphores.
852 1.71.2.2 ad */
853 1.71.2.2 ad /*ARGSUSED*/
854 1.71.2.2 ad void
855 1.71.2.2 ad semexit(struct proc *p, void *v)
856 1.71.2.2 ad {
857 1.71.2.2 ad struct sem_undo *suptr;
858 1.71.2.2 ad struct sem_undo **supptr;
859 1.71.2.2 ad
860 1.71.2.2 ad mutex_enter(&semlock);
861 1.71.2.2 ad
862 1.71.2.2 ad /*
863 1.71.2.2 ad * Go through the chain of undo vectors looking for one
864 1.71.2.2 ad * associated with this process.
865 1.71.2.2 ad */
866 1.71.2.2 ad
867 1.71.2.2 ad for (supptr = &semu_list; (suptr = *supptr) != NULL;
868 1.71.2.2 ad supptr = &suptr->un_next) {
869 1.71.2.2 ad if (suptr->un_proc == p)
870 1.71.2.2 ad break;
871 1.71.2.2 ad }
872 1.71.2.2 ad
873 1.71.2.2 ad /*
874 1.71.2.2 ad * If there is no undo vector, skip to the end.
875 1.71.2.2 ad */
876 1.71.2.2 ad
877 1.71.2.2 ad if (suptr == NULL) {
878 1.71.2.2 ad mutex_exit(&semlock);
879 1.71.2.2 ad return;
880 1.71.2.2 ad }
881 1.71.2.2 ad
882 1.71.2.2 ad /*
883 1.71.2.2 ad * We now have an undo vector for this process.
884 1.71.2.2 ad */
885 1.71.2.2 ad
886 1.71.2.2 ad SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
887 1.71.2.2 ad suptr->un_cnt));
888 1.71.2.2 ad
889 1.71.2.2 ad /*
890 1.71.2.2 ad * If there are any active undo elements then process them.
891 1.71.2.2 ad */
892 1.71.2.2 ad if (suptr->un_cnt > 0) {
893 1.71.2.2 ad int ix;
894 1.71.2.2 ad
895 1.71.2.2 ad for (ix = 0; ix < suptr->un_cnt; ix++) {
896 1.71.2.2 ad int semid = suptr->un_ent[ix].un_id;
897 1.71.2.2 ad int semnum = suptr->un_ent[ix].un_num;
898 1.71.2.2 ad int adjval = suptr->un_ent[ix].un_adjval;
899 1.71.2.2 ad struct semid_ds *semaptr;
900 1.71.2.2 ad
901 1.71.2.2 ad semaptr = &sema[semid];
902 1.71.2.2 ad if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
903 1.71.2.2 ad panic("semexit - semid not allocated");
904 1.71.2.2 ad if (semnum >= semaptr->sem_nsems)
905 1.71.2.2 ad panic("semexit - semnum out of range");
906 1.71.2.2 ad
907 1.71.2.2 ad SEM_PRINTF(("semexit: %p id=%d num=%d(adj=%d) ; "
908 1.71.2.2 ad "sem=%d\n",
909 1.71.2.2 ad suptr->un_proc, suptr->un_ent[ix].un_id,
910 1.71.2.2 ad suptr->un_ent[ix].un_num,
911 1.71.2.2 ad suptr->un_ent[ix].un_adjval,
912 1.71.2.2 ad semaptr->_sem_base[semnum].semval));
913 1.71.2.2 ad
914 1.71.2.2 ad if (adjval < 0 &&
915 1.71.2.2 ad semaptr->_sem_base[semnum].semval < -adjval)
916 1.71.2.2 ad semaptr->_sem_base[semnum].semval = 0;
917 1.71.2.2 ad else
918 1.71.2.2 ad semaptr->_sem_base[semnum].semval += adjval;
919 1.71.2.2 ad
920 1.71.2.2 ad cv_broadcast(&semcv[semid]);
921 1.71.2.2 ad SEM_PRINTF(("semexit: back from wakeup\n"));
922 1.71.2.2 ad }
923 1.71.2.2 ad }
924 1.71.2.2 ad
925 1.71.2.2 ad /*
926 1.71.2.2 ad * Deallocate the undo vector.
927 1.71.2.2 ad */
928 1.71.2.2 ad SEM_PRINTF(("removing vector\n"));
929 1.71.2.2 ad suptr->un_proc = NULL;
930 1.71.2.2 ad *supptr = suptr->un_next;
931 1.71.2.2 ad mutex_exit(&semlock);
932 1.71.2.2 ad }
933