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