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