Switch ipfw from ipfw1 to ipfw2.
[dragonfly/port-amd64.git] / sys / kern / sysv_sem.c
blob204a66403d59ff0e8c25d0a8ac1aa313fd433627
1 /* $FreeBSD: src/sys/kern/sysv_sem.c,v 1.69 2004/03/17 09:37:13 cperciva Exp $ */
2 /* $DragonFly: src/sys/kern/sysv_sem.c,v 1.18 2006/12/23 23:47:54 swildner Exp $ */
4 /*
5 * Implementation of SVID semaphores
7 * Author: Daniel Boulet
9 * This software is provided ``AS IS'' without any warranties of any kind.
12 #include "opt_sysvipc.h"
14 #include <sys/param.h>
15 #include <sys/systm.h>
16 #include <sys/sysproto.h>
17 #include <sys/kernel.h>
18 #include <sys/proc.h>
19 #include <sys/sem.h>
20 #include <sys/sysent.h>
21 #include <sys/sysctl.h>
22 #include <sys/malloc.h>
23 #include <sys/jail.h>
25 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
27 static void seminit (void *);
29 static struct sem_undo *semu_alloc (struct proc *p);
30 static int semundo_adjust (struct proc *p, struct sem_undo **supptr,
31 int semid, int semnum, int adjval);
32 static void semundo_clear (int semid, int semnum);
34 /* XXX casting to (sy_call_t *) is bogus, as usual. */
35 static sy_call_t *semcalls[] = {
36 (sy_call_t *)sys___semctl, (sy_call_t *)sys_semget,
37 (sy_call_t *)sys_semop
40 static int semtot = 0;
41 static struct semid_ds *sema; /* semaphore id pool */
42 static struct sem *sem; /* semaphore pool */
43 static struct sem_undo *semu_list; /* list of active undo structures */
44 static int *semu; /* undo structure pool */
46 struct sem {
47 u_short semval; /* semaphore value */
48 pid_t sempid; /* pid of last operation */
49 u_short semncnt; /* # awaiting semval > cval */
50 u_short semzcnt; /* # awaiting semval = 0 */
54 * Undo structure (one per process)
56 struct sem_undo {
57 struct sem_undo *un_next; /* ptr to next active undo structure */
58 struct proc *un_proc; /* owner of this structure */
59 short un_cnt; /* # of active entries */
60 struct undo {
61 short un_adjval; /* adjust on exit values */
62 short un_num; /* semaphore # */
63 int un_id; /* semid */
64 } un_ent[1]; /* undo entries */
68 * Configuration parameters
70 #ifndef SEMMNI
71 #define SEMMNI 10 /* # of semaphore identifiers */
72 #endif
73 #ifndef SEMMNS
74 #define SEMMNS 60 /* # of semaphores in system */
75 #endif
76 #ifndef SEMUME
77 #define SEMUME 10 /* max # of undo entries per process */
78 #endif
79 #ifndef SEMMNU
80 #define SEMMNU 30 /* # of undo structures in system */
81 #endif
83 /* shouldn't need tuning */
84 #ifndef SEMMAP
85 #define SEMMAP 30 /* # of entries in semaphore map */
86 #endif
87 #ifndef SEMMSL
88 #define SEMMSL SEMMNS /* max # of semaphores per id */
89 #endif
90 #ifndef SEMOPM
91 #define SEMOPM 100 /* max # of operations per semop call */
92 #endif
94 #define SEMVMX 32767 /* semaphore maximum value */
95 #define SEMAEM 16384 /* adjust on exit max value */
98 * Due to the way semaphore memory is allocated, we have to ensure that
99 * SEMUSZ is properly aligned.
102 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
104 /* actual size of an undo structure */
105 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
108 * Macro to find a particular sem_undo vector
110 #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
113 * semaphore info struct
115 struct seminfo seminfo = {
116 SEMMAP, /* # of entries in semaphore map */
117 SEMMNI, /* # of semaphore identifiers */
118 SEMMNS, /* # of semaphores in system */
119 SEMMNU, /* # of undo structures in system */
120 SEMMSL, /* max # of semaphores per id */
121 SEMOPM, /* max # of operations per semop call */
122 SEMUME, /* max # of undo entries per process */
123 SEMUSZ, /* size in bytes of undo structure */
124 SEMVMX, /* semaphore maximum value */
125 SEMAEM /* adjust on exit max value */
128 TUNABLE_INT("kern.ipc.semmap", &seminfo.semmap);
129 TUNABLE_INT("kern.ipc.semmni", &seminfo.semmni);
130 TUNABLE_INT("kern.ipc.semmns", &seminfo.semmns);
131 TUNABLE_INT("kern.ipc.semmnu", &seminfo.semmnu);
132 TUNABLE_INT("kern.ipc.semmsl", &seminfo.semmsl);
133 TUNABLE_INT("kern.ipc.semopm", &seminfo.semopm);
134 TUNABLE_INT("kern.ipc.semume", &seminfo.semume);
135 TUNABLE_INT("kern.ipc.semusz", &seminfo.semusz);
136 TUNABLE_INT("kern.ipc.semvmx", &seminfo.semvmx);
137 TUNABLE_INT("kern.ipc.semaem", &seminfo.semaem);
139 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
140 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0, "");
141 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0, "");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0, "");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0, "");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0, "");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
148 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
150 #if 0
151 RO seminfo.semmap /* SEMMAP unused */
152 RO seminfo.semmni
153 RO seminfo.semmns
154 RO seminfo.semmnu /* undo entries per system */
155 RW seminfo.semmsl
156 RO seminfo.semopm /* SEMOPM unused */
157 RO seminfo.semume
158 RO seminfo.semusz /* param - derived from SEMUME for per-proc sizeof */
159 RO seminfo.semvmx /* SEMVMX unused - user param */
160 RO seminfo.semaem /* SEMAEM unused - user param */
161 #endif
163 static void
164 seminit(void *dummy)
166 int i;
168 sem = kmalloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
169 if (sem == NULL)
170 panic("sem is NULL");
171 sema = kmalloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
172 if (sema == NULL)
173 panic("sema is NULL");
174 semu = kmalloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
175 if (semu == NULL)
176 panic("semu is NULL");
178 for (i = 0; i < seminfo.semmni; i++) {
179 sema[i].sem_base = 0;
180 sema[i].sem_perm.mode = 0;
182 for (i = 0; i < seminfo.semmnu; i++) {
183 struct sem_undo *suptr = SEMU(i);
184 suptr->un_proc = NULL;
186 semu_list = NULL;
188 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
191 * Entry point for all SEM calls
193 * semsys_args(int which, a2, a3, ...) (VARARGS)
196 sys_semsys(struct semsys_args *uap)
198 struct proc *p = curproc;
199 unsigned int which = (unsigned int)uap->which;
201 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
202 return (ENOSYS);
204 if (which >= sizeof(semcalls)/sizeof(semcalls[0]))
205 return (EINVAL);
206 bcopy(&uap->a2, &uap->which,
207 sizeof(struct semsys_args) - offsetof(struct semsys_args, a2));
208 return ((*semcalls[which])(uap));
212 * Allocate a new sem_undo structure for a process
213 * (returns ptr to structure or NULL if no more room)
216 static struct sem_undo *
217 semu_alloc(struct proc *p)
219 int i;
220 struct sem_undo *suptr;
221 struct sem_undo **supptr;
222 int attempt;
225 * Try twice to allocate something.
226 * (we'll purge any empty structures after the first pass so
227 * two passes are always enough)
230 for (attempt = 0; attempt < 2; attempt++) {
232 * Look for a free structure.
233 * Fill it in and return it if we find one.
236 for (i = 0; i < seminfo.semmnu; i++) {
237 suptr = SEMU(i);
238 if (suptr->un_proc == NULL) {
239 suptr->un_next = semu_list;
240 semu_list = suptr;
241 suptr->un_cnt = 0;
242 suptr->un_proc = p;
243 return(suptr);
248 * We didn't find a free one, if this is the first attempt
249 * then try to free some structures.
252 if (attempt == 0) {
253 /* All the structures are in use - try to free some */
254 int did_something = 0;
256 supptr = &semu_list;
257 while ((suptr = *supptr) != NULL) {
258 if (suptr->un_cnt == 0) {
259 suptr->un_proc = NULL;
260 *supptr = suptr->un_next;
261 did_something = 1;
262 } else
263 supptr = &(suptr->un_next);
266 /* If we didn't free anything then just give-up */
267 if (!did_something)
268 return(NULL);
269 } else {
271 * The second pass failed even though we freed
272 * something after the first pass!
273 * This is IMPOSSIBLE!
275 panic("semu_alloc - second attempt failed");
278 return (NULL);
282 * Adjust a particular entry for a particular proc
285 static int
286 semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
287 int adjval)
289 struct sem_undo *suptr;
290 struct undo *sunptr;
291 int i;
293 /* Look for and remember the sem_undo if the caller doesn't provide
294 it */
296 suptr = *supptr;
297 if (suptr == NULL) {
298 for (suptr = semu_list; suptr != NULL;
299 suptr = suptr->un_next) {
300 if (suptr->un_proc == p) {
301 *supptr = suptr;
302 break;
305 if (suptr == NULL) {
306 if (adjval == 0)
307 return(0);
308 suptr = semu_alloc(p);
309 if (suptr == NULL)
310 return(ENOSPC);
311 *supptr = suptr;
316 * Look for the requested entry and adjust it (delete if adjval becomes
317 * 0).
319 sunptr = &suptr->un_ent[0];
320 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
321 if (sunptr->un_id != semid || sunptr->un_num != semnum)
322 continue;
323 if (adjval == 0)
324 sunptr->un_adjval = 0;
325 else
326 sunptr->un_adjval += adjval;
327 if (sunptr->un_adjval == 0) {
328 suptr->un_cnt--;
329 if (i < suptr->un_cnt)
330 suptr->un_ent[i] =
331 suptr->un_ent[suptr->un_cnt];
333 return(0);
336 /* Didn't find the right entry - create it */
337 if (adjval == 0)
338 return(0);
339 if (suptr->un_cnt != seminfo.semume) {
340 sunptr = &suptr->un_ent[suptr->un_cnt];
341 suptr->un_cnt++;
342 sunptr->un_adjval = adjval;
343 sunptr->un_id = semid; sunptr->un_num = semnum;
344 } else
345 return(EINVAL);
346 return(0);
349 static void
350 semundo_clear(int semid, int semnum)
352 struct sem_undo *suptr;
354 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
355 struct undo *sunptr = &suptr->un_ent[0];
356 int i = 0;
358 while (i < suptr->un_cnt) {
359 if (sunptr->un_id == semid) {
360 if (semnum == -1 || sunptr->un_num == semnum) {
361 suptr->un_cnt--;
362 if (i < suptr->un_cnt) {
363 suptr->un_ent[i] =
364 suptr->un_ent[suptr->un_cnt];
365 continue;
368 if (semnum != -1)
369 break;
371 i++, sunptr++;
377 * Note that the user-mode half of this passes a union, not a pointer
381 sys___semctl(struct __semctl_args *uap)
383 struct proc *p = curproc;
384 int semid = uap->semid;
385 int semnum = uap->semnum;
386 int cmd = uap->cmd;
387 union semun *arg = uap->arg;
388 union semun real_arg;
389 struct ucred *cred = p->p_ucred;
390 int i, rval, eval;
391 struct semid_ds sbuf;
392 struct semid_ds *semaptr;
394 #ifdef SEM_DEBUG
395 kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
396 #endif
398 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
399 return (ENOSYS);
401 semid = IPCID_TO_IX(semid);
402 if (semid < 0 || semid >= seminfo.semmni)
403 return(EINVAL);
405 semaptr = &sema[semid];
406 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
407 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
408 return(EINVAL);
410 eval = 0;
411 rval = 0;
413 switch (cmd) {
414 case IPC_RMID:
415 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)))
416 return(eval);
417 semaptr->sem_perm.cuid = cred->cr_uid;
418 semaptr->sem_perm.uid = cred->cr_uid;
419 semtot -= semaptr->sem_nsems;
420 for (i = semaptr->sem_base - sem; i < semtot; i++)
421 sem[i] = sem[i + semaptr->sem_nsems];
422 for (i = 0; i < seminfo.semmni; i++) {
423 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
424 sema[i].sem_base > semaptr->sem_base)
425 sema[i].sem_base -= semaptr->sem_nsems;
427 semaptr->sem_perm.mode = 0;
428 semundo_clear(semid, -1);
429 wakeup((caddr_t)semaptr);
430 break;
432 case IPC_SET:
433 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)))
434 return(eval);
435 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
436 return(eval);
437 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
438 sizeof(sbuf))) != 0)
439 return(eval);
440 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
441 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
442 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
443 (sbuf.sem_perm.mode & 0777);
444 semaptr->sem_ctime = time_second;
445 break;
447 case IPC_STAT:
448 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
449 return(eval);
450 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
451 return(eval);
452 eval = copyout((caddr_t)semaptr, real_arg.buf,
453 sizeof(struct semid_ds));
454 break;
456 case GETNCNT:
457 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
458 return(eval);
459 if (semnum < 0 || semnum >= semaptr->sem_nsems)
460 return(EINVAL);
461 rval = semaptr->sem_base[semnum].semncnt;
462 break;
464 case GETPID:
465 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
466 return(eval);
467 if (semnum < 0 || semnum >= semaptr->sem_nsems)
468 return(EINVAL);
469 rval = semaptr->sem_base[semnum].sempid;
470 break;
472 case GETVAL:
473 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
474 return(eval);
475 if (semnum < 0 || semnum >= semaptr->sem_nsems)
476 return(EINVAL);
477 rval = semaptr->sem_base[semnum].semval;
478 break;
480 case GETALL:
481 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
482 return(eval);
483 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
484 return(eval);
485 for (i = 0; i < semaptr->sem_nsems; i++) {
486 eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
487 &real_arg.array[i], sizeof(real_arg.array[0]));
488 if (eval != 0)
489 break;
491 break;
493 case GETZCNT:
494 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
495 return(eval);
496 if (semnum < 0 || semnum >= semaptr->sem_nsems)
497 return(EINVAL);
498 rval = semaptr->sem_base[semnum].semzcnt;
499 break;
501 case SETVAL:
502 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)))
503 return(eval);
504 if (semnum < 0 || semnum >= semaptr->sem_nsems)
505 return(EINVAL);
506 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
507 return(eval);
508 semaptr->sem_base[semnum].semval = real_arg.val;
509 semundo_clear(semid, semnum);
510 wakeup((caddr_t)semaptr);
511 break;
513 case SETALL:
514 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)))
515 return(eval);
516 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
517 return(eval);
518 for (i = 0; i < semaptr->sem_nsems; i++) {
519 eval = copyin(&real_arg.array[i],
520 (caddr_t)&semaptr->sem_base[i].semval,
521 sizeof(real_arg.array[0]));
522 if (eval != 0)
523 break;
525 semundo_clear(semid, -1);
526 wakeup((caddr_t)semaptr);
527 break;
529 default:
530 return(EINVAL);
533 if (eval == 0)
534 uap->sysmsg_result = rval;
535 return(eval);
539 sys_semget(struct semget_args *uap)
541 struct proc *p = curproc;
542 int semid, eval;
543 int key = uap->key;
544 int nsems = uap->nsems;
545 int semflg = uap->semflg;
546 struct ucred *cred = p->p_ucred;
548 #ifdef SEM_DEBUG
549 kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
550 #endif
552 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
553 return (ENOSYS);
555 if (key != IPC_PRIVATE) {
556 for (semid = 0; semid < seminfo.semmni; semid++) {
557 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
558 sema[semid].sem_perm.key == key)
559 break;
561 if (semid < seminfo.semmni) {
562 #ifdef SEM_DEBUG
563 kprintf("found public key\n");
564 #endif
565 if ((eval = ipcperm(p, &sema[semid].sem_perm,
566 semflg & 0700)))
567 return(eval);
568 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
569 #ifdef SEM_DEBUG
570 kprintf("too small\n");
571 #endif
572 return(EINVAL);
574 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
575 #ifdef SEM_DEBUG
576 kprintf("not exclusive\n");
577 #endif
578 return(EEXIST);
580 goto found;
584 #ifdef SEM_DEBUG
585 kprintf("need to allocate the semid_ds\n");
586 #endif
587 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
588 if (nsems <= 0 || nsems > seminfo.semmsl) {
589 #ifdef SEM_DEBUG
590 kprintf("nsems out of range (0<%d<=%d)\n", nsems,
591 seminfo.semmsl);
592 #endif
593 return(EINVAL);
595 if (nsems > seminfo.semmns - semtot) {
596 #ifdef SEM_DEBUG
597 kprintf("not enough semaphores left (need %d, got %d)\n",
598 nsems, seminfo.semmns - semtot);
599 #endif
600 return(ENOSPC);
602 for (semid = 0; semid < seminfo.semmni; semid++) {
603 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
604 break;
606 if (semid == seminfo.semmni) {
607 #ifdef SEM_DEBUG
608 kprintf("no more semid_ds's available\n");
609 #endif
610 return(ENOSPC);
612 #ifdef SEM_DEBUG
613 kprintf("semid %d is available\n", semid);
614 #endif
615 sema[semid].sem_perm.key = key;
616 sema[semid].sem_perm.cuid = cred->cr_uid;
617 sema[semid].sem_perm.uid = cred->cr_uid;
618 sema[semid].sem_perm.cgid = cred->cr_gid;
619 sema[semid].sem_perm.gid = cred->cr_gid;
620 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
621 sema[semid].sem_perm.seq =
622 (sema[semid].sem_perm.seq + 1) & 0x7fff;
623 sema[semid].sem_nsems = nsems;
624 sema[semid].sem_otime = 0;
625 sema[semid].sem_ctime = time_second;
626 sema[semid].sem_base = &sem[semtot];
627 semtot += nsems;
628 bzero(sema[semid].sem_base,
629 sizeof(sema[semid].sem_base[0])*nsems);
630 #ifdef SEM_DEBUG
631 kprintf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
632 &sem[semtot]);
633 #endif
634 } else {
635 #ifdef SEM_DEBUG
636 kprintf("didn't find it and wasn't asked to create it\n");
637 #endif
638 return(ENOENT);
641 found:
642 uap->sysmsg_result = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
643 return(0);
647 sys_semop(struct semop_args *uap)
649 struct proc *p = curproc;
650 int semid = uap->semid;
651 u_int nsops = uap->nsops;
652 struct sembuf sops[MAX_SOPS];
653 struct semid_ds *semaptr;
654 struct sembuf *sopptr;
655 struct sem *semptr;
656 struct sem_undo *suptr = NULL;
657 int i, j, eval;
658 int do_wakeup, do_undos;
660 #ifdef SEM_DEBUG
661 kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
662 #endif
664 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
665 return (ENOSYS);
667 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
669 if (semid < 0 || semid >= seminfo.semmni)
670 return(EINVAL);
672 semaptr = &sema[semid];
673 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
674 return(EINVAL);
675 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
676 return(EINVAL);
678 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W))) {
679 #ifdef SEM_DEBUG
680 kprintf("eval = %d from ipaccess\n", eval);
681 #endif
682 return(eval);
685 if (nsops > MAX_SOPS) {
686 #ifdef SEM_DEBUG
687 kprintf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
688 #endif
689 return(E2BIG);
692 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
693 #ifdef SEM_DEBUG
694 kprintf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
695 uap->sops, &sops, nsops * sizeof(sops[0]));
696 #endif
697 return(eval);
701 * Loop trying to satisfy the vector of requests.
702 * If we reach a point where we must wait, any requests already
703 * performed are rolled back and we go to sleep until some other
704 * process wakes us up. At this point, we start all over again.
706 * This ensures that from the perspective of other tasks, a set
707 * of requests is atomic (never partially satisfied).
709 do_undos = 0;
711 for (;;) {
712 do_wakeup = 0;
714 for (i = 0; i < nsops; i++) {
715 sopptr = &sops[i];
717 if (sopptr->sem_num >= semaptr->sem_nsems)
718 return(EFBIG);
720 semptr = &semaptr->sem_base[sopptr->sem_num];
722 #ifdef SEM_DEBUG
723 kprintf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
724 semaptr, semaptr->sem_base, semptr,
725 sopptr->sem_num, semptr->semval, sopptr->sem_op,
726 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
727 #endif
729 if (sopptr->sem_op < 0) {
730 if (semptr->semval + sopptr->sem_op < 0) {
731 #ifdef SEM_DEBUG
732 kprintf("semop: can't do it now\n");
733 #endif
734 break;
735 } else {
736 semptr->semval += sopptr->sem_op;
737 if (semptr->semval == 0 &&
738 semptr->semzcnt > 0)
739 do_wakeup = 1;
741 if (sopptr->sem_flg & SEM_UNDO)
742 do_undos = 1;
743 } else if (sopptr->sem_op == 0) {
744 if (semptr->semval > 0) {
745 #ifdef SEM_DEBUG
746 kprintf("semop: not zero now\n");
747 #endif
748 break;
750 } else {
751 if (semptr->semncnt > 0)
752 do_wakeup = 1;
753 semptr->semval += sopptr->sem_op;
754 if (sopptr->sem_flg & SEM_UNDO)
755 do_undos = 1;
760 * Did we get through the entire vector?
762 if (i >= nsops)
763 goto done;
766 * No ... rollback anything that we've already done
768 #ifdef SEM_DEBUG
769 kprintf("semop: rollback 0 through %d\n", i-1);
770 #endif
771 for (j = 0; j < i; j++)
772 semaptr->sem_base[sops[j].sem_num].semval -=
773 sops[j].sem_op;
776 * If the request that we couldn't satisfy has the
777 * NOWAIT flag set then return with EAGAIN.
779 if (sopptr->sem_flg & IPC_NOWAIT)
780 return(EAGAIN);
782 if (sopptr->sem_op == 0)
783 semptr->semzcnt++;
784 else
785 semptr->semncnt++;
787 #ifdef SEM_DEBUG
788 kprintf("semop: good night!\n");
789 #endif
790 eval = tsleep((caddr_t)semaptr, PCATCH, "semwait", 0);
791 #ifdef SEM_DEBUG
792 kprintf("semop: good morning (eval=%d)!\n", eval);
793 #endif
795 suptr = NULL; /* sem_undo may have been reallocated */
797 /* return code is checked below, after sem[nz]cnt-- */
800 * Make sure that the semaphore still exists
802 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
803 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
804 return(EIDRM);
807 * The semaphore is still alive. Readjust the count of
808 * waiting processes.
810 if (sopptr->sem_op == 0)
811 semptr->semzcnt--;
812 else
813 semptr->semncnt--;
816 * Is it really morning, or was our sleep interrupted?
817 * (Delayed check of msleep() return code because we
818 * need to decrement sem[nz]cnt either way.)
820 if (eval != 0)
821 return(EINTR);
822 #ifdef SEM_DEBUG
823 kprintf("semop: good morning!\n");
824 #endif
827 done:
829 * Process any SEM_UNDO requests.
831 if (do_undos) {
832 for (i = 0; i < nsops; i++) {
834 * We only need to deal with SEM_UNDO's for non-zero
835 * op's.
837 int adjval;
839 if ((sops[i].sem_flg & SEM_UNDO) == 0)
840 continue;
841 adjval = sops[i].sem_op;
842 if (adjval == 0)
843 continue;
844 eval = semundo_adjust(p, &suptr, semid,
845 sops[i].sem_num, -adjval);
846 if (eval == 0)
847 continue;
850 * Oh-Oh! We ran out of either sem_undo's or undo's.
851 * Rollback the adjustments to this point and then
852 * rollback the semaphore ups and down so we can return
853 * with an error with all structures restored. We
854 * rollback the undo's in the exact reverse order that
855 * we applied them. This guarantees that we won't run
856 * out of space as we roll things back out.
858 for (j = i - 1; j >= 0; j--) {
859 if ((sops[j].sem_flg & SEM_UNDO) == 0)
860 continue;
861 adjval = sops[j].sem_op;
862 if (adjval == 0)
863 continue;
864 if (semundo_adjust(p, &suptr, semid,
865 sops[j].sem_num, adjval) != 0)
866 panic("semop - can't undo undos");
869 for (j = 0; j < nsops; j++)
870 semaptr->sem_base[sops[j].sem_num].semval -=
871 sops[j].sem_op;
873 #ifdef SEM_DEBUG
874 kprintf("eval = %d from semundo_adjust\n", eval);
875 #endif
876 return(eval);
877 } /* loop through the sops */
878 } /* if (do_undos) */
880 /* We're definitely done - set the sempid's */
881 for (i = 0; i < nsops; i++) {
882 sopptr = &sops[i];
883 semptr = &semaptr->sem_base[sopptr->sem_num];
884 semptr->sempid = p->p_pid;
887 /* Do a wakeup if any semaphore was up'd. */
888 if (do_wakeup) {
889 #ifdef SEM_DEBUG
890 kprintf("semop: doing wakeup\n");
891 #endif
892 wakeup((caddr_t)semaptr);
893 #ifdef SEM_DEBUG
894 kprintf("semop: back from wakeup\n");
895 #endif
897 #ifdef SEM_DEBUG
898 kprintf("semop: done\n");
899 #endif
900 uap->sysmsg_result = 0;
901 return(0);
905 * Go through the undo structures for this process and apply the adjustments to
906 * semaphores.
908 void
909 semexit(struct proc *p)
911 struct sem_undo *suptr;
912 struct sem_undo **supptr;
913 int did_something;
915 did_something = 0;
918 * Go through the chain of undo vectors looking for one
919 * associated with this process.
922 for (supptr = &semu_list; (suptr = *supptr) != NULL;
923 supptr = &suptr->un_next) {
924 if (suptr->un_proc == p)
925 break;
928 if (suptr == NULL)
929 return;
931 #ifdef SEM_DEBUG
932 kprintf("proc @%08x has undo structure with %d entries\n", p,
933 suptr->un_cnt);
934 #endif
937 * If there are any active undo elements then process them.
939 if (suptr->un_cnt > 0) {
940 int ix;
942 for (ix = 0; ix < suptr->un_cnt; ix++) {
943 int semid = suptr->un_ent[ix].un_id;
944 int semnum = suptr->un_ent[ix].un_num;
945 int adjval = suptr->un_ent[ix].un_adjval;
946 struct semid_ds *semaptr;
948 semaptr = &sema[semid];
949 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
950 panic("semexit - semid not allocated");
951 if (semnum >= semaptr->sem_nsems)
952 panic("semexit - semnum out of range");
954 #ifdef SEM_DEBUG
955 kprintf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
956 suptr->un_proc, suptr->un_ent[ix].un_id,
957 suptr->un_ent[ix].un_num,
958 suptr->un_ent[ix].un_adjval,
959 semaptr->sem_base[semnum].semval);
960 #endif
962 if (adjval < 0) {
963 if (semaptr->sem_base[semnum].semval < -adjval)
964 semaptr->sem_base[semnum].semval = 0;
965 else
966 semaptr->sem_base[semnum].semval +=
967 adjval;
968 } else
969 semaptr->sem_base[semnum].semval += adjval;
971 wakeup((caddr_t)semaptr);
972 #ifdef SEM_DEBUG
973 kprintf("semexit: back from wakeup\n");
974 #endif
979 * Deallocate the undo vector.
981 #ifdef SEM_DEBUG
982 kprintf("removing vector\n");
983 #endif
984 suptr->un_proc = NULL;
985 *supptr = suptr->un_next;