sysv_sem.c revision 1.25 1 1.25 christos /* $NetBSD: sysv_sem.c,v 1.25 1996/02/04 02:17:06 christos Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.1 cgd * Implementation of SVID semaphores
5 1.1 cgd *
6 1.1 cgd * Author: Daniel Boulet
7 1.1 cgd *
8 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
9 1.1 cgd */
10 1.1 cgd
11 1.3 mycroft #include <sys/param.h>
12 1.3 mycroft #include <sys/systm.h>
13 1.3 mycroft #include <sys/kernel.h>
14 1.3 mycroft #include <sys/proc.h>
15 1.3 mycroft #include <sys/sem.h>
16 1.3 mycroft #include <sys/malloc.h>
17 1.1 cgd
18 1.10 cgd #include <sys/mount.h>
19 1.10 cgd #include <sys/syscallargs.h>
20 1.10 cgd
21 1.25 christos #include <kern/kern_extern.h>
22 1.25 christos
23 1.1 cgd int semtot = 0;
24 1.18 mycroft struct proc *semlock_holder = NULL;
25 1.1 cgd
26 1.25 christos void semlock __P((struct proc *));
27 1.25 christos struct sem_undo *semu_alloc __P((struct proc *));
28 1.25 christos int semundo_adjust __P((struct proc *, struct sem_undo **, int, int, int));
29 1.25 christos void semundo_clear __P((int, int));
30 1.25 christos
31 1.25 christos void
32 1.1 cgd seminit()
33 1.1 cgd {
34 1.5 mycroft register int i;
35 1.1 cgd
36 1.5 mycroft if (sema == NULL)
37 1.5 mycroft panic("sema is NULL");
38 1.5 mycroft if (semu == NULL)
39 1.5 mycroft panic("semu is NULL");
40 1.5 mycroft
41 1.5 mycroft for (i = 0; i < seminfo.semmni; i++) {
42 1.5 mycroft sema[i].sem_base = 0;
43 1.5 mycroft sema[i].sem_perm.mode = 0;
44 1.5 mycroft }
45 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
46 1.5 mycroft register struct sem_undo *suptr = SEMU(i);
47 1.5 mycroft suptr->un_proc = NULL;
48 1.5 mycroft }
49 1.5 mycroft semu_list = NULL;
50 1.1 cgd }
51 1.1 cgd
52 1.13 mycroft void
53 1.17 mycroft semlock(p)
54 1.13 mycroft struct proc *p;
55 1.13 mycroft {
56 1.13 mycroft
57 1.13 mycroft while (semlock_holder != NULL && semlock_holder != p)
58 1.13 mycroft sleep((caddr_t)&semlock_holder, (PZERO - 4));
59 1.13 mycroft }
60 1.13 mycroft
61 1.1 cgd /*
62 1.1 cgd * Lock or unlock the entire semaphore facility.
63 1.1 cgd *
64 1.1 cgd * This will probably eventually evolve into a general purpose semaphore
65 1.1 cgd * facility status enquiry mechanism (I don't like the "read /dev/kmem"
66 1.1 cgd * approach currently taken by ipcs and the amount of info that we want
67 1.12 mycroft * to be able to extract for ipcs is probably beyond the capability of
68 1.12 mycroft * the getkerninfo facility.
69 1.1 cgd *
70 1.1 cgd * At the time that the current version of semconfig was written, ipcs is
71 1.1 cgd * the only user of the semconfig facility. It uses it to ensure that the
72 1.1 cgd * semaphore facility data structures remain static while it fishes around
73 1.1 cgd * in /dev/kmem.
74 1.1 cgd */
75 1.1 cgd
76 1.1 cgd int
77 1.24 mycroft sys_semconfig(p, v, retval)
78 1.1 cgd struct proc *p;
79 1.23 thorpej void *v;
80 1.23 thorpej register_t *retval;
81 1.23 thorpej {
82 1.24 mycroft struct sys_semconfig_args /* {
83 1.10 cgd syscallarg(int) flag;
84 1.23 thorpej } */ *uap = v;
85 1.5 mycroft int eval = 0;
86 1.1 cgd
87 1.17 mycroft semlock(p);
88 1.12 mycroft
89 1.10 cgd switch (SCARG(uap, flag)) {
90 1.5 mycroft case SEM_CONFIG_FREEZE:
91 1.5 mycroft semlock_holder = p;
92 1.5 mycroft break;
93 1.6 mycroft
94 1.5 mycroft case SEM_CONFIG_THAW:
95 1.5 mycroft semlock_holder = NULL;
96 1.5 mycroft wakeup((caddr_t)&semlock_holder);
97 1.5 mycroft break;
98 1.6 mycroft
99 1.5 mycroft default:
100 1.10 cgd printf(
101 1.10 cgd "semconfig: unknown flag parameter value (%d) - ignored\n",
102 1.10 cgd SCARG(uap, flag));
103 1.5 mycroft eval = EINVAL;
104 1.5 mycroft break;
105 1.5 mycroft }
106 1.1 cgd
107 1.5 mycroft *retval = 0;
108 1.5 mycroft return(eval);
109 1.1 cgd }
110 1.1 cgd
111 1.1 cgd /*
112 1.1 cgd * Allocate a new sem_undo structure for a process
113 1.1 cgd * (returns ptr to structure or NULL if no more room)
114 1.1 cgd */
115 1.1 cgd
116 1.1 cgd struct sem_undo *
117 1.5 mycroft semu_alloc(p)
118 1.5 mycroft struct proc *p;
119 1.1 cgd {
120 1.5 mycroft register int i;
121 1.5 mycroft register struct sem_undo *suptr;
122 1.5 mycroft register struct sem_undo **supptr;
123 1.5 mycroft int attempt;
124 1.1 cgd
125 1.1 cgd /*
126 1.5 mycroft * Try twice to allocate something.
127 1.5 mycroft * (we'll purge any empty structures after the first pass so
128 1.5 mycroft * two passes are always enough)
129 1.1 cgd */
130 1.1 cgd
131 1.5 mycroft for (attempt = 0; attempt < 2; attempt++) {
132 1.5 mycroft /*
133 1.5 mycroft * Look for a free structure.
134 1.5 mycroft * Fill it in and return it if we find one.
135 1.5 mycroft */
136 1.5 mycroft
137 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
138 1.5 mycroft suptr = SEMU(i);
139 1.5 mycroft if (suptr->un_proc == NULL) {
140 1.5 mycroft suptr->un_next = semu_list;
141 1.5 mycroft semu_list = suptr;
142 1.5 mycroft suptr->un_cnt = 0;
143 1.5 mycroft suptr->un_proc = p;
144 1.5 mycroft return(suptr);
145 1.5 mycroft }
146 1.5 mycroft }
147 1.1 cgd
148 1.5 mycroft /*
149 1.5 mycroft * We didn't find a free one, if this is the first attempt
150 1.5 mycroft * then try to free some structures.
151 1.5 mycroft */
152 1.5 mycroft
153 1.5 mycroft if (attempt == 0) {
154 1.5 mycroft /* All the structures are in use - try to free some */
155 1.5 mycroft int did_something = 0;
156 1.5 mycroft
157 1.5 mycroft supptr = &semu_list;
158 1.5 mycroft while ((suptr = *supptr) != NULL) {
159 1.5 mycroft if (suptr->un_cnt == 0) {
160 1.5 mycroft suptr->un_proc = NULL;
161 1.5 mycroft *supptr = suptr->un_next;
162 1.5 mycroft did_something = 1;
163 1.5 mycroft } else
164 1.5 mycroft supptr = &(suptr->un_next);
165 1.5 mycroft }
166 1.5 mycroft
167 1.5 mycroft /* If we didn't free anything then just give-up */
168 1.5 mycroft if (!did_something)
169 1.5 mycroft return(NULL);
170 1.5 mycroft } else {
171 1.5 mycroft /*
172 1.5 mycroft * The second pass failed even though we freed
173 1.5 mycroft * something after the first pass!
174 1.5 mycroft * This is IMPOSSIBLE!
175 1.5 mycroft */
176 1.5 mycroft panic("semu_alloc - second attempt failed");
177 1.5 mycroft }
178 1.1 cgd }
179 1.25 christos return NULL;
180 1.1 cgd }
181 1.1 cgd
182 1.1 cgd /*
183 1.1 cgd * Adjust a particular entry for a particular proc
184 1.1 cgd */
185 1.1 cgd
186 1.1 cgd int
187 1.5 mycroft semundo_adjust(p, supptr, semid, semnum, adjval)
188 1.5 mycroft register struct proc *p;
189 1.5 mycroft struct sem_undo **supptr;
190 1.5 mycroft int semid, semnum;
191 1.5 mycroft int adjval;
192 1.1 cgd {
193 1.5 mycroft register struct sem_undo *suptr;
194 1.5 mycroft register struct undo *sunptr;
195 1.5 mycroft int i;
196 1.1 cgd
197 1.5 mycroft /* Look for and remember the sem_undo if the caller doesn't provide
198 1.5 mycroft it */
199 1.1 cgd
200 1.5 mycroft suptr = *supptr;
201 1.4 mycroft if (suptr == NULL) {
202 1.11 mycroft for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
203 1.5 mycroft if (suptr->un_proc == p) {
204 1.5 mycroft *supptr = suptr;
205 1.5 mycroft break;
206 1.5 mycroft }
207 1.5 mycroft }
208 1.5 mycroft if (suptr == NULL) {
209 1.5 mycroft if (adjval == 0)
210 1.5 mycroft return(0);
211 1.5 mycroft suptr = semu_alloc(p);
212 1.5 mycroft if (suptr == NULL)
213 1.5 mycroft return(ENOSPC);
214 1.5 mycroft *supptr = suptr;
215 1.5 mycroft }
216 1.1 cgd }
217 1.1 cgd
218 1.6 mycroft /*
219 1.6 mycroft * Look for the requested entry and adjust it (delete if adjval becomes
220 1.6 mycroft * 0).
221 1.6 mycroft */
222 1.6 mycroft sunptr = &suptr->un_ent[0];
223 1.5 mycroft for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
224 1.6 mycroft if (sunptr->un_id != semid || sunptr->un_num != semnum)
225 1.6 mycroft continue;
226 1.6 mycroft if (adjval == 0)
227 1.6 mycroft sunptr->un_adjval = 0;
228 1.6 mycroft else
229 1.6 mycroft sunptr->un_adjval += adjval;
230 1.6 mycroft if (sunptr->un_adjval == 0) {
231 1.6 mycroft suptr->un_cnt--;
232 1.6 mycroft if (i < suptr->un_cnt)
233 1.6 mycroft suptr->un_ent[i] =
234 1.6 mycroft suptr->un_ent[suptr->un_cnt];
235 1.5 mycroft }
236 1.6 mycroft return(0);
237 1.1 cgd }
238 1.1 cgd
239 1.5 mycroft /* Didn't find the right entry - create it */
240 1.5 mycroft if (adjval == 0)
241 1.5 mycroft return(0);
242 1.11 mycroft if (suptr->un_cnt == SEMUME)
243 1.5 mycroft return(EINVAL);
244 1.11 mycroft
245 1.11 mycroft sunptr = &suptr->un_ent[suptr->un_cnt];
246 1.11 mycroft suptr->un_cnt++;
247 1.11 mycroft sunptr->un_adjval = adjval;
248 1.11 mycroft sunptr->un_id = semid;
249 1.11 mycroft sunptr->un_num = semnum;
250 1.1 cgd return(0);
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd void
254 1.5 mycroft semundo_clear(semid, semnum)
255 1.5 mycroft int semid, semnum;
256 1.1 cgd {
257 1.6 mycroft register struct sem_undo *suptr;
258 1.1 cgd
259 1.6 mycroft for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
260 1.19 mycroft register struct undo *sunptr;
261 1.19 mycroft register int i;
262 1.6 mycroft
263 1.19 mycroft sunptr = &suptr->un_ent[0];
264 1.19 mycroft for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
265 1.6 mycroft if (sunptr->un_id == semid) {
266 1.6 mycroft if (semnum == -1 || sunptr->un_num == semnum) {
267 1.6 mycroft suptr->un_cnt--;
268 1.6 mycroft if (i < suptr->un_cnt) {
269 1.6 mycroft suptr->un_ent[i] =
270 1.6 mycroft suptr->un_ent[suptr->un_cnt];
271 1.19 mycroft i--, sunptr--;
272 1.6 mycroft }
273 1.6 mycroft }
274 1.6 mycroft if (semnum != -1)
275 1.6 mycroft break;
276 1.6 mycroft }
277 1.6 mycroft }
278 1.1 cgd }
279 1.1 cgd }
280 1.1 cgd
281 1.1 cgd int
282 1.24 mycroft sys___semctl(p, v, retval)
283 1.1 cgd struct proc *p;
284 1.25 christos register void *v;
285 1.23 thorpej register_t *retval;
286 1.23 thorpej {
287 1.24 mycroft register struct sys___semctl_args /* {
288 1.10 cgd syscallarg(int) semid;
289 1.10 cgd syscallarg(int) semnum;
290 1.10 cgd syscallarg(int) cmd;
291 1.10 cgd syscallarg(union semun *) arg;
292 1.23 thorpej } */ *uap = v;
293 1.10 cgd int semid = SCARG(uap, semid);
294 1.10 cgd int semnum = SCARG(uap, semnum);
295 1.10 cgd int cmd = SCARG(uap, cmd);
296 1.10 cgd union semun *arg = SCARG(uap, arg);
297 1.6 mycroft union semun real_arg;
298 1.6 mycroft struct ucred *cred = p->p_ucred;
299 1.6 mycroft int i, rval, eval;
300 1.6 mycroft struct semid_ds sbuf;
301 1.6 mycroft register struct semid_ds *semaptr;
302 1.1 cgd
303 1.1 cgd #ifdef SEM_DEBUG
304 1.20 mycroft printf("call to semctl(%d, %d, %d, %p)\n", semid, semnum, cmd, arg);
305 1.1 cgd #endif
306 1.1 cgd
307 1.17 mycroft semlock(p);
308 1.12 mycroft
309 1.6 mycroft semid = IPCID_TO_IX(semid);
310 1.6 mycroft if (semid < 0 || semid >= seminfo.semmsl)
311 1.6 mycroft return(EINVAL);
312 1.6 mycroft
313 1.6 mycroft semaptr = &sema[semid];
314 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
315 1.10 cgd semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
316 1.6 mycroft return(EINVAL);
317 1.6 mycroft
318 1.6 mycroft eval = 0;
319 1.6 mycroft rval = 0;
320 1.1 cgd
321 1.6 mycroft switch (cmd) {
322 1.6 mycroft case IPC_RMID:
323 1.25 christos if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
324 1.8 mycroft return(eval);
325 1.6 mycroft semaptr->sem_perm.cuid = cred->cr_uid;
326 1.6 mycroft semaptr->sem_perm.uid = cred->cr_uid;
327 1.6 mycroft semtot -= semaptr->sem_nsems;
328 1.6 mycroft for (i = semaptr->sem_base - sem; i < semtot; i++)
329 1.6 mycroft sem[i] = sem[i + semaptr->sem_nsems];
330 1.6 mycroft for (i = 0; i < seminfo.semmni; i++) {
331 1.6 mycroft if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
332 1.6 mycroft sema[i].sem_base > semaptr->sem_base)
333 1.6 mycroft sema[i].sem_base -= semaptr->sem_nsems;
334 1.6 mycroft }
335 1.6 mycroft semaptr->sem_perm.mode = 0;
336 1.6 mycroft semundo_clear(semid, -1);
337 1.6 mycroft wakeup((caddr_t)semaptr);
338 1.6 mycroft break;
339 1.1 cgd
340 1.6 mycroft case IPC_SET:
341 1.8 mycroft if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
342 1.8 mycroft return(eval);
343 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
344 1.6 mycroft return(eval);
345 1.6 mycroft if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
346 1.6 mycroft sizeof(sbuf))) != 0)
347 1.6 mycroft return(eval);
348 1.6 mycroft semaptr->sem_perm.uid = sbuf.sem_perm.uid;
349 1.6 mycroft semaptr->sem_perm.gid = sbuf.sem_perm.gid;
350 1.6 mycroft semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
351 1.6 mycroft (sbuf.sem_perm.mode & 0777);
352 1.6 mycroft semaptr->sem_ctime = time.tv_sec;
353 1.6 mycroft break;
354 1.1 cgd
355 1.6 mycroft case IPC_STAT:
356 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
357 1.6 mycroft return(eval);
358 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
359 1.6 mycroft return(eval);
360 1.6 mycroft eval = copyout((caddr_t)semaptr, real_arg.buf,
361 1.6 mycroft sizeof(struct semid_ds));
362 1.6 mycroft break;
363 1.1 cgd
364 1.6 mycroft case GETNCNT:
365 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
366 1.6 mycroft return(eval);
367 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
368 1.6 mycroft return(EINVAL);
369 1.6 mycroft rval = semaptr->sem_base[semnum].semncnt;
370 1.6 mycroft break;
371 1.1 cgd
372 1.6 mycroft case GETPID:
373 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
374 1.6 mycroft return(eval);
375 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
376 1.6 mycroft return(EINVAL);
377 1.6 mycroft rval = semaptr->sem_base[semnum].sempid;
378 1.6 mycroft break;
379 1.1 cgd
380 1.6 mycroft case GETVAL:
381 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
382 1.6 mycroft return(eval);
383 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
384 1.6 mycroft return(EINVAL);
385 1.6 mycroft rval = semaptr->sem_base[semnum].semval;
386 1.6 mycroft break;
387 1.1 cgd
388 1.6 mycroft case GETALL:
389 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
390 1.6 mycroft return(eval);
391 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
392 1.6 mycroft return(eval);
393 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
394 1.6 mycroft eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
395 1.6 mycroft &real_arg.array[i], sizeof(real_arg.array[0]));
396 1.6 mycroft if (eval != 0)
397 1.6 mycroft break;
398 1.6 mycroft }
399 1.6 mycroft break;
400 1.1 cgd
401 1.6 mycroft case GETZCNT:
402 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
403 1.6 mycroft return(eval);
404 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
405 1.6 mycroft return(EINVAL);
406 1.6 mycroft rval = semaptr->sem_base[semnum].semzcnt;
407 1.6 mycroft break;
408 1.1 cgd
409 1.6 mycroft case SETVAL:
410 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
411 1.6 mycroft return(eval);
412 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
413 1.6 mycroft return(EINVAL);
414 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
415 1.6 mycroft return(eval);
416 1.6 mycroft semaptr->sem_base[semnum].semval = real_arg.val;
417 1.6 mycroft semundo_clear(semid, semnum);
418 1.6 mycroft wakeup((caddr_t)semaptr);
419 1.6 mycroft break;
420 1.1 cgd
421 1.6 mycroft case SETALL:
422 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
423 1.6 mycroft return(eval);
424 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
425 1.6 mycroft return(eval);
426 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
427 1.6 mycroft eval = copyin(&real_arg.array[i],
428 1.6 mycroft (caddr_t)&semaptr->sem_base[i].semval,
429 1.6 mycroft sizeof(real_arg.array[0]));
430 1.6 mycroft if (eval != 0)
431 1.6 mycroft break;
432 1.6 mycroft }
433 1.6 mycroft semundo_clear(semid, -1);
434 1.6 mycroft wakeup((caddr_t)semaptr);
435 1.6 mycroft break;
436 1.1 cgd
437 1.6 mycroft default:
438 1.6 mycroft return(EINVAL);
439 1.6 mycroft }
440 1.4 mycroft
441 1.6 mycroft if (eval == 0)
442 1.6 mycroft *retval = rval;
443 1.6 mycroft return(eval);
444 1.1 cgd }
445 1.1 cgd
446 1.1 cgd int
447 1.24 mycroft sys_semget(p, v, retval)
448 1.1 cgd struct proc *p;
449 1.23 thorpej void *v;
450 1.23 thorpej register_t *retval;
451 1.23 thorpej {
452 1.24 mycroft register struct sys_semget_args /* {
453 1.10 cgd syscallarg(key_t) key;
454 1.10 cgd syscallarg(int) nsems;
455 1.10 cgd syscallarg(int) semflg;
456 1.23 thorpej } */ *uap = v;
457 1.6 mycroft int semid, eval;
458 1.10 cgd int key = SCARG(uap, key);
459 1.10 cgd int nsems = SCARG(uap, nsems);
460 1.10 cgd int semflg = SCARG(uap, semflg);
461 1.6 mycroft struct ucred *cred = p->p_ucred;
462 1.1 cgd
463 1.1 cgd #ifdef SEM_DEBUG
464 1.6 mycroft printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
465 1.1 cgd #endif
466 1.1 cgd
467 1.17 mycroft semlock(p);
468 1.12 mycroft
469 1.6 mycroft if (key != IPC_PRIVATE) {
470 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
471 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
472 1.6 mycroft sema[semid].sem_perm.key == key)
473 1.6 mycroft break;
474 1.6 mycroft }
475 1.6 mycroft if (semid < seminfo.semmni) {
476 1.1 cgd #ifdef SEM_DEBUG
477 1.6 mycroft printf("found public key\n");
478 1.1 cgd #endif
479 1.7 hpeyerl if ((eval = ipcperm(cred, &sema[semid].sem_perm,
480 1.7 hpeyerl semflg & 0700)))
481 1.6 mycroft return(eval);
482 1.6 mycroft if (nsems > 0 && sema[semid].sem_nsems < nsems) {
483 1.1 cgd #ifdef SEM_DEBUG
484 1.6 mycroft printf("too small\n");
485 1.1 cgd #endif
486 1.6 mycroft return(EINVAL);
487 1.6 mycroft }
488 1.6 mycroft if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
489 1.1 cgd #ifdef SEM_DEBUG
490 1.6 mycroft printf("not exclusive\n");
491 1.1 cgd #endif
492 1.6 mycroft return(EEXIST);
493 1.6 mycroft }
494 1.6 mycroft goto found;
495 1.6 mycroft }
496 1.6 mycroft }
497 1.6 mycroft
498 1.1 cgd #ifdef SEM_DEBUG
499 1.6 mycroft printf("need to allocate the semid_ds\n");
500 1.1 cgd #endif
501 1.6 mycroft if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
502 1.6 mycroft if (nsems <= 0 || nsems > seminfo.semmsl) {
503 1.1 cgd #ifdef SEM_DEBUG
504 1.6 mycroft printf("nsems out of range (0<%d<=%d)\n", nsems,
505 1.6 mycroft seminfo.semmsl);
506 1.1 cgd #endif
507 1.6 mycroft return(EINVAL);
508 1.6 mycroft }
509 1.6 mycroft if (nsems > seminfo.semmns - semtot) {
510 1.1 cgd #ifdef SEM_DEBUG
511 1.6 mycroft printf("not enough semaphores left (need %d, got %d)\n",
512 1.6 mycroft nsems, seminfo.semmns - semtot);
513 1.1 cgd #endif
514 1.6 mycroft return(ENOSPC);
515 1.6 mycroft }
516 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
517 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
518 1.6 mycroft break;
519 1.6 mycroft }
520 1.6 mycroft if (semid == seminfo.semmni) {
521 1.1 cgd #ifdef SEM_DEBUG
522 1.6 mycroft printf("no more semid_ds's available\n");
523 1.1 cgd #endif
524 1.6 mycroft return(ENOSPC);
525 1.6 mycroft }
526 1.1 cgd #ifdef SEM_DEBUG
527 1.6 mycroft printf("semid %d is available\n", semid);
528 1.1 cgd #endif
529 1.6 mycroft sema[semid].sem_perm.key = key;
530 1.6 mycroft sema[semid].sem_perm.cuid = cred->cr_uid;
531 1.6 mycroft sema[semid].sem_perm.uid = cred->cr_uid;
532 1.6 mycroft sema[semid].sem_perm.cgid = cred->cr_gid;
533 1.6 mycroft sema[semid].sem_perm.gid = cred->cr_gid;
534 1.6 mycroft sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
535 1.6 mycroft sema[semid].sem_perm.seq =
536 1.6 mycroft (sema[semid].sem_perm.seq + 1) & 0x7fff;
537 1.6 mycroft sema[semid].sem_nsems = nsems;
538 1.6 mycroft sema[semid].sem_otime = 0;
539 1.6 mycroft sema[semid].sem_ctime = time.tv_sec;
540 1.6 mycroft sema[semid].sem_base = &sem[semtot];
541 1.6 mycroft semtot += nsems;
542 1.6 mycroft bzero(sema[semid].sem_base,
543 1.6 mycroft sizeof(sema[semid].sem_base[0])*nsems);
544 1.1 cgd #ifdef SEM_DEBUG
545 1.20 mycroft printf("sembase = %p, next = %p\n", sema[semid].sem_base,
546 1.6 mycroft &sem[semtot]);
547 1.1 cgd #endif
548 1.1 cgd } else {
549 1.1 cgd #ifdef SEM_DEBUG
550 1.6 mycroft printf("didn't find it and wasn't asked to create it\n");
551 1.1 cgd #endif
552 1.6 mycroft return(ENOENT);
553 1.1 cgd }
554 1.1 cgd
555 1.6 mycroft found:
556 1.6 mycroft *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
557 1.6 mycroft return(0);
558 1.1 cgd }
559 1.1 cgd
560 1.1 cgd int
561 1.24 mycroft sys_semop(p, v, retval)
562 1.1 cgd struct proc *p;
563 1.23 thorpej void *v;
564 1.23 thorpej register_t *retval;
565 1.23 thorpej {
566 1.24 mycroft register struct sys_semop_args /* {
567 1.10 cgd syscallarg(int) semid;
568 1.10 cgd syscallarg(struct sembuf *) sops;
569 1.10 cgd syscallarg(u_int) nsops;
570 1.23 thorpej } */ *uap = v;
571 1.10 cgd int semid = SCARG(uap, semid);
572 1.10 cgd int nsops = SCARG(uap, nsops);
573 1.6 mycroft struct sembuf sops[MAX_SOPS];
574 1.6 mycroft register struct semid_ds *semaptr;
575 1.25 christos register struct sembuf *sopptr = NULL;
576 1.25 christos register struct sem *semptr = NULL;
577 1.6 mycroft struct sem_undo *suptr = NULL;
578 1.6 mycroft struct ucred *cred = p->p_ucred;
579 1.6 mycroft int i, j, eval;
580 1.25 christos int do_wakeup, do_undos;
581 1.1 cgd
582 1.1 cgd #ifdef SEM_DEBUG
583 1.20 mycroft printf("call to semop(%d, %p, %d)\n", semid, sops, nsops);
584 1.1 cgd #endif
585 1.1 cgd
586 1.17 mycroft semlock(p);
587 1.12 mycroft
588 1.6 mycroft semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
589 1.6 mycroft
590 1.6 mycroft if (semid < 0 || semid >= seminfo.semmsl)
591 1.6 mycroft return(EINVAL);
592 1.6 mycroft
593 1.6 mycroft semaptr = &sema[semid];
594 1.11 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
595 1.11 mycroft semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
596 1.6 mycroft return(EINVAL);
597 1.6 mycroft
598 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
599 1.1 cgd #ifdef SEM_DEBUG
600 1.6 mycroft printf("eval = %d from ipaccess\n", eval);
601 1.1 cgd #endif
602 1.6 mycroft return(eval);
603 1.6 mycroft }
604 1.1 cgd
605 1.6 mycroft if (nsops > MAX_SOPS) {
606 1.1 cgd #ifdef SEM_DEBUG
607 1.6 mycroft printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops);
608 1.1 cgd #endif
609 1.6 mycroft return(E2BIG);
610 1.6 mycroft }
611 1.1 cgd
612 1.10 cgd if ((eval = copyin(SCARG(uap, sops), sops, nsops * sizeof(sops[0])))
613 1.10 cgd != 0) {
614 1.6 mycroft #ifdef SEM_DEBUG
615 1.20 mycroft printf("eval = %d from copyin(%p, %p, %d)\n", eval,
616 1.10 cgd SCARG(uap, sops), &sops, nsops * sizeof(sops[0]));
617 1.6 mycroft #endif
618 1.6 mycroft return(eval);
619 1.6 mycroft }
620 1.1 cgd
621 1.6 mycroft /*
622 1.6 mycroft * Loop trying to satisfy the vector of requests.
623 1.6 mycroft * If we reach a point where we must wait, any requests already
624 1.6 mycroft * performed are rolled back and we go to sleep until some other
625 1.6 mycroft * process wakes us up. At this point, we start all over again.
626 1.6 mycroft *
627 1.6 mycroft * This ensures that from the perspective of other tasks, a set
628 1.6 mycroft * of requests is atomic (never partially satisfied).
629 1.6 mycroft */
630 1.6 mycroft do_undos = 0;
631 1.1 cgd
632 1.6 mycroft for (;;) {
633 1.6 mycroft do_wakeup = 0;
634 1.1 cgd
635 1.6 mycroft for (i = 0; i < nsops; i++) {
636 1.6 mycroft sopptr = &sops[i];
637 1.1 cgd
638 1.6 mycroft if (sopptr->sem_num >= semaptr->sem_nsems)
639 1.6 mycroft return(EFBIG);
640 1.1 cgd
641 1.6 mycroft semptr = &semaptr->sem_base[sopptr->sem_num];
642 1.1 cgd
643 1.1 cgd #ifdef SEM_DEBUG
644 1.6 mycroft printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
645 1.6 mycroft semaptr, semaptr->sem_base, semptr,
646 1.6 mycroft sopptr->sem_num, semptr->semval, sopptr->sem_op,
647 1.6 mycroft (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
648 1.1 cgd #endif
649 1.1 cgd
650 1.6 mycroft if (sopptr->sem_op < 0) {
651 1.25 christos if ((int)(semptr->semval +
652 1.25 christos sopptr->sem_op) < 0) {
653 1.1 cgd #ifdef SEM_DEBUG
654 1.6 mycroft printf("semop: can't do it now\n");
655 1.1 cgd #endif
656 1.6 mycroft break;
657 1.6 mycroft } else {
658 1.6 mycroft semptr->semval += sopptr->sem_op;
659 1.6 mycroft if (semptr->semval == 0 &&
660 1.6 mycroft semptr->semzcnt > 0)
661 1.6 mycroft do_wakeup = 1;
662 1.6 mycroft }
663 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
664 1.6 mycroft do_undos = 1;
665 1.6 mycroft } else if (sopptr->sem_op == 0) {
666 1.6 mycroft if (semptr->semval > 0) {
667 1.6 mycroft #ifdef SEM_DEBUG
668 1.6 mycroft printf("semop: not zero now\n");
669 1.6 mycroft #endif
670 1.6 mycroft break;
671 1.6 mycroft }
672 1.6 mycroft } else {
673 1.6 mycroft if (semptr->semncnt > 0)
674 1.6 mycroft do_wakeup = 1;
675 1.6 mycroft semptr->semval += sopptr->sem_op;
676 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
677 1.6 mycroft do_undos = 1;
678 1.6 mycroft }
679 1.6 mycroft }
680 1.1 cgd
681 1.6 mycroft /*
682 1.6 mycroft * Did we get through the entire vector?
683 1.6 mycroft */
684 1.6 mycroft if (i >= nsops)
685 1.6 mycroft goto done;
686 1.1 cgd
687 1.6 mycroft /*
688 1.6 mycroft * No ... rollback anything that we've already done
689 1.6 mycroft */
690 1.1 cgd #ifdef SEM_DEBUG
691 1.6 mycroft printf("semop: rollback 0 through %d\n", i-1);
692 1.1 cgd #endif
693 1.6 mycroft for (j = 0; j < i; j++)
694 1.6 mycroft semaptr->sem_base[sops[j].sem_num].semval -=
695 1.6 mycroft sops[j].sem_op;
696 1.1 cgd
697 1.6 mycroft /*
698 1.6 mycroft * If the request that we couldn't satisfy has the
699 1.6 mycroft * NOWAIT flag set then return with EAGAIN.
700 1.6 mycroft */
701 1.6 mycroft if (sopptr->sem_flg & IPC_NOWAIT)
702 1.6 mycroft return(EAGAIN);
703 1.1 cgd
704 1.6 mycroft if (sopptr->sem_op == 0)
705 1.6 mycroft semptr->semzcnt++;
706 1.6 mycroft else
707 1.6 mycroft semptr->semncnt++;
708 1.1 cgd
709 1.1 cgd #ifdef SEM_DEBUG
710 1.6 mycroft printf("semop: good night!\n");
711 1.1 cgd #endif
712 1.6 mycroft eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
713 1.6 mycroft "semwait", 0);
714 1.1 cgd #ifdef SEM_DEBUG
715 1.6 mycroft printf("semop: good morning (eval=%d)!\n", eval);
716 1.1 cgd #endif
717 1.1 cgd
718 1.6 mycroft suptr = NULL; /* sem_undo may have been reallocated */
719 1.1 cgd
720 1.6 mycroft if (eval != 0)
721 1.6 mycroft return(EINTR);
722 1.1 cgd #ifdef SEM_DEBUG
723 1.6 mycroft printf("semop: good morning!\n");
724 1.1 cgd #endif
725 1.1 cgd
726 1.6 mycroft /*
727 1.6 mycroft * Make sure that the semaphore still exists
728 1.6 mycroft */
729 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
730 1.10 cgd semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid))) {
731 1.6 mycroft /* The man page says to return EIDRM. */
732 1.6 mycroft /* Unfortunately, BSD doesn't define that code! */
733 1.1 cgd #ifdef EIDRM
734 1.6 mycroft return(EIDRM);
735 1.1 cgd #else
736 1.6 mycroft return(EINVAL);
737 1.1 cgd #endif
738 1.6 mycroft }
739 1.1 cgd
740 1.6 mycroft /*
741 1.6 mycroft * The semaphore is still alive. Readjust the count of
742 1.6 mycroft * waiting processes.
743 1.6 mycroft */
744 1.6 mycroft if (sopptr->sem_op == 0)
745 1.6 mycroft semptr->semzcnt--;
746 1.6 mycroft else
747 1.6 mycroft semptr->semncnt--;
748 1.6 mycroft }
749 1.1 cgd
750 1.6 mycroft done:
751 1.6 mycroft /*
752 1.6 mycroft * Process any SEM_UNDO requests.
753 1.6 mycroft */
754 1.6 mycroft if (do_undos) {
755 1.5 mycroft for (i = 0; i < nsops; i++) {
756 1.6 mycroft /*
757 1.6 mycroft * We only need to deal with SEM_UNDO's for non-zero
758 1.6 mycroft * op's.
759 1.6 mycroft */
760 1.6 mycroft int adjval;
761 1.1 cgd
762 1.6 mycroft if ((sops[i].sem_flg & SEM_UNDO) == 0)
763 1.6 mycroft continue;
764 1.6 mycroft adjval = sops[i].sem_op;
765 1.6 mycroft if (adjval == 0)
766 1.6 mycroft continue;
767 1.6 mycroft eval = semundo_adjust(p, &suptr, semid,
768 1.6 mycroft sops[i].sem_num, -adjval);
769 1.6 mycroft if (eval == 0)
770 1.6 mycroft continue;
771 1.1 cgd
772 1.6 mycroft /*
773 1.6 mycroft * Oh-Oh! We ran out of either sem_undo's or undo's.
774 1.6 mycroft * Rollback the adjustments to this point and then
775 1.6 mycroft * rollback the semaphore ups and down so we can return
776 1.6 mycroft * with an error with all structures restored. We
777 1.6 mycroft * rollback the undo's in the exact reverse order that
778 1.6 mycroft * we applied them. This guarantees that we won't run
779 1.6 mycroft * out of space as we roll things back out.
780 1.6 mycroft */
781 1.6 mycroft for (j = i - 1; j >= 0; j--) {
782 1.6 mycroft if ((sops[j].sem_flg & SEM_UNDO) == 0)
783 1.6 mycroft continue;
784 1.6 mycroft adjval = sops[j].sem_op;
785 1.6 mycroft if (adjval == 0)
786 1.6 mycroft continue;
787 1.6 mycroft if (semundo_adjust(p, &suptr, semid,
788 1.6 mycroft sops[j].sem_num, adjval) != 0)
789 1.1 cgd panic("semop - can't undo undos");
790 1.6 mycroft }
791 1.1 cgd
792 1.6 mycroft for (j = 0; j < nsops; j++)
793 1.6 mycroft semaptr->sem_base[sops[j].sem_num].semval -=
794 1.6 mycroft sops[j].sem_op;
795 1.1 cgd
796 1.1 cgd #ifdef SEM_DEBUG
797 1.6 mycroft printf("eval = %d from semundo_adjust\n", eval);
798 1.1 cgd #endif
799 1.6 mycroft return(eval);
800 1.1 cgd } /* loop through the sops */
801 1.6 mycroft } /* if (do_undos) */
802 1.1 cgd
803 1.6 mycroft /* We're definitely done - set the sempid's */
804 1.6 mycroft for (i = 0; i < nsops; i++) {
805 1.1 cgd sopptr = &sops[i];
806 1.1 cgd semptr = &semaptr->sem_base[sopptr->sem_num];
807 1.1 cgd semptr->sempid = p->p_pid;
808 1.6 mycroft }
809 1.1 cgd
810 1.6 mycroft /* Do a wakeup if any semaphore was up'd. */
811 1.6 mycroft if (do_wakeup) {
812 1.1 cgd #ifdef SEM_DEBUG
813 1.1 cgd printf("semop: doing wakeup\n");
814 1.1 cgd #ifdef SEM_WAKEUP
815 1.4 mycroft sem_wakeup((caddr_t)semaptr);
816 1.1 cgd #else
817 1.4 mycroft wakeup((caddr_t)semaptr);
818 1.1 cgd #endif
819 1.1 cgd printf("semop: back from wakeup\n");
820 1.1 cgd #else
821 1.4 mycroft wakeup((caddr_t)semaptr);
822 1.1 cgd #endif
823 1.6 mycroft }
824 1.1 cgd #ifdef SEM_DEBUG
825 1.6 mycroft printf("semop: done\n");
826 1.1 cgd #endif
827 1.6 mycroft *retval = 0;
828 1.6 mycroft return(0);
829 1.1 cgd }
830 1.1 cgd
831 1.1 cgd /*
832 1.6 mycroft * Go through the undo structures for this process and apply the adjustments to
833 1.6 mycroft * semaphores.
834 1.1 cgd */
835 1.25 christos void
836 1.1 cgd semexit(p)
837 1.6 mycroft struct proc *p;
838 1.1 cgd {
839 1.6 mycroft register struct sem_undo *suptr;
840 1.6 mycroft register struct sem_undo **supptr;
841 1.1 cgd
842 1.6 mycroft /*
843 1.17 mycroft * Go through the chain of undo vectors looking for one associated with
844 1.17 mycroft * this process.
845 1.17 mycroft */
846 1.17 mycroft
847 1.17 mycroft for (supptr = &semu_list; (suptr = *supptr) != NULL;
848 1.17 mycroft supptr = &suptr->un_next) {
849 1.17 mycroft if (suptr->un_proc == p)
850 1.17 mycroft break;
851 1.17 mycroft }
852 1.17 mycroft
853 1.17 mycroft /*
854 1.14 mycroft * There are a few possibilities to consider here ...
855 1.14 mycroft *
856 1.14 mycroft * 1) The semaphore facility isn't currently locked. In this case,
857 1.14 mycroft * this call should proceed normally.
858 1.14 mycroft * 2) The semaphore facility is locked by this process (i.e. the one
859 1.14 mycroft * that is exiting). In this case, this call should proceed as
860 1.14 mycroft * usual and the facility should be unlocked at the end of this
861 1.14 mycroft * routine (since the locker is exiting).
862 1.14 mycroft * 3) The semaphore facility is locked by some other process and this
863 1.14 mycroft * process doesn't have an undo structure allocated for it. In this
864 1.14 mycroft * case, this call should proceed normally (i.e. not accomplish
865 1.14 mycroft * anything and, most importantly, not block since that is
866 1.14 mycroft * unnecessary and could result in a LOT of processes blocking in
867 1.14 mycroft * here if the facility is locked for a long time).
868 1.14 mycroft * 4) The semaphore facility is locked by some other process and this
869 1.14 mycroft * process has an undo structure allocated for it. In this case,
870 1.14 mycroft * this call should block until the facility has been unlocked since
871 1.14 mycroft * the holder of the lock may be examining this process's proc entry
872 1.14 mycroft * (the ipcs utility does this when printing out the information
873 1.14 mycroft * from the allocated sem undo elements).
874 1.14 mycroft *
875 1.14 mycroft * This leads to the conclusion that we should not block unless we
876 1.14 mycroft * discover that the someone else has the semaphore facility locked and
877 1.14 mycroft * this process has an undo structure. Let's do that...
878 1.14 mycroft *
879 1.14 mycroft * Note that we do this in a separate pass from the one that processes
880 1.14 mycroft * any existing undo structure since we don't want to risk blocking at
881 1.14 mycroft * that time (it would make the actual unlinking of the element from
882 1.14 mycroft * the chain of allocated undo structures rather messy).
883 1.14 mycroft */
884 1.14 mycroft
885 1.14 mycroft /*
886 1.14 mycroft * Does someone else hold the semaphore facility's lock?
887 1.14 mycroft */
888 1.14 mycroft
889 1.14 mycroft if (semlock_holder != NULL && semlock_holder != p) {
890 1.14 mycroft /*
891 1.14 mycroft * Yes (i.e. we are in case 3 or 4).
892 1.14 mycroft *
893 1.15 mycroft * If we didn't find an undo vector associated with this
894 1.14 mycroft * process than we can just return (i.e. we are in case 3).
895 1.14 mycroft *
896 1.14 mycroft * Note that we know that someone else is holding the lock so
897 1.14 mycroft * we don't even have to see if we're holding it...
898 1.14 mycroft */
899 1.14 mycroft
900 1.14 mycroft if (suptr == NULL)
901 1.14 mycroft return;
902 1.14 mycroft
903 1.14 mycroft /*
904 1.14 mycroft * We are in case 4.
905 1.14 mycroft *
906 1.14 mycroft * Go to sleep as long as someone else is locking the semaphore
907 1.14 mycroft * facility (note that we won't get here if we are holding the
908 1.14 mycroft * lock so we don't need to check for that possibility).
909 1.14 mycroft */
910 1.14 mycroft
911 1.14 mycroft while (semlock_holder != NULL)
912 1.14 mycroft sleep((caddr_t)&semlock_holder, (PZERO - 4));
913 1.14 mycroft
914 1.14 mycroft /*
915 1.14 mycroft * Nobody is holding the facility (i.e. we are now in case 1).
916 1.14 mycroft * We can proceed safely according to the argument outlined
917 1.14 mycroft * above.
918 1.14 mycroft *
919 1.16 mycroft * We look up the undo vector again, in case the list changed
920 1.17 mycroft * while we were asleep, and the parent is now different.
921 1.14 mycroft */
922 1.16 mycroft
923 1.17 mycroft for (supptr = &semu_list; (suptr = *supptr) != NULL;
924 1.17 mycroft supptr = &suptr->un_next) {
925 1.17 mycroft if (suptr->un_proc == p)
926 1.17 mycroft break;
927 1.17 mycroft }
928 1.17 mycroft
929 1.17 mycroft if (suptr == NULL)
930 1.17 mycroft panic("semexit: undo vector disappeared");
931 1.17 mycroft } else {
932 1.17 mycroft /*
933 1.17 mycroft * No (i.e. we are in case 1 or 2).
934 1.17 mycroft *
935 1.17 mycroft * If there is no undo vector, skip to the end and unlock the
936 1.17 mycroft * semaphore facility if necessary.
937 1.17 mycroft */
938 1.14 mycroft
939 1.17 mycroft if (suptr == NULL)
940 1.17 mycroft goto unlock;
941 1.14 mycroft }
942 1.15 mycroft
943 1.15 mycroft /*
944 1.17 mycroft * We are now in case 1 or 2, and we have an undo vector for this
945 1.17 mycroft * process.
946 1.15 mycroft */
947 1.1 cgd
948 1.1 cgd #ifdef SEM_DEBUG
949 1.20 mycroft printf("proc @%p has undo structure with %d entries\n", p,
950 1.6 mycroft suptr->un_cnt);
951 1.1 cgd #endif
952 1.1 cgd
953 1.5 mycroft /*
954 1.5 mycroft * If there are any active undo elements then process them.
955 1.5 mycroft */
956 1.5 mycroft if (suptr->un_cnt > 0) {
957 1.6 mycroft int ix;
958 1.1 cgd
959 1.6 mycroft for (ix = 0; ix < suptr->un_cnt; ix++) {
960 1.6 mycroft int semid = suptr->un_ent[ix].un_id;
961 1.6 mycroft int semnum = suptr->un_ent[ix].un_num;
962 1.6 mycroft int adjval = suptr->un_ent[ix].un_adjval;
963 1.6 mycroft struct semid_ds *semaptr;
964 1.6 mycroft
965 1.6 mycroft semaptr = &sema[semid];
966 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
967 1.6 mycroft panic("semexit - semid not allocated");
968 1.6 mycroft if (semnum >= semaptr->sem_nsems)
969 1.6 mycroft panic("semexit - semnum out of range");
970 1.6 mycroft
971 1.6 mycroft #ifdef SEM_DEBUG
972 1.20 mycroft printf("semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
973 1.6 mycroft suptr->un_proc, suptr->un_ent[ix].un_id,
974 1.6 mycroft suptr->un_ent[ix].un_num,
975 1.6 mycroft suptr->un_ent[ix].un_adjval,
976 1.6 mycroft semaptr->sem_base[semnum].semval);
977 1.6 mycroft #endif
978 1.6 mycroft
979 1.14 mycroft if (adjval < 0 &&
980 1.14 mycroft semaptr->sem_base[semnum].semval < -adjval)
981 1.14 mycroft semaptr->sem_base[semnum].semval = 0;
982 1.14 mycroft else
983 1.6 mycroft semaptr->sem_base[semnum].semval += adjval;
984 1.1 cgd
985 1.1 cgd #ifdef SEM_WAKEUP
986 1.6 mycroft sem_wakeup((caddr_t)semaptr);
987 1.1 cgd #else
988 1.6 mycroft wakeup((caddr_t)semaptr);
989 1.1 cgd #endif
990 1.1 cgd #ifdef SEM_DEBUG
991 1.6 mycroft printf("semexit: back from wakeup\n");
992 1.1 cgd #endif
993 1.6 mycroft }
994 1.5 mycroft }
995 1.1 cgd
996 1.5 mycroft /*
997 1.5 mycroft * Deallocate the undo vector.
998 1.5 mycroft */
999 1.1 cgd #ifdef SEM_DEBUG
1000 1.5 mycroft printf("removing vector\n");
1001 1.1 cgd #endif
1002 1.5 mycroft suptr->un_proc = NULL;
1003 1.5 mycroft *supptr = suptr->un_next;
1004 1.1 cgd
1005 1.6 mycroft unlock:
1006 1.6 mycroft /*
1007 1.6 mycroft * If the exiting process is holding the global semaphore facility
1008 1.14 mycroft * lock (i.e. we are in case 2) then release it.
1009 1.6 mycroft */
1010 1.6 mycroft if (semlock_holder == p) {
1011 1.6 mycroft semlock_holder = NULL;
1012 1.6 mycroft wakeup((caddr_t)&semlock_holder);
1013 1.6 mycroft }
1014 1.1 cgd }
1015