kernel - kqueue - major refactoring
[dragonfly.git] / sys / kern / sysv_sem.c
blobee0ae08b9fa692018a454d3413960a4408ce64f2
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.19 2008/01/06 16:55:51 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 #include <sys/mplock2.h>
27 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
29 static void seminit (void *);
31 static struct sem_undo *semu_alloc (struct proc *p);
32 static int semundo_adjust (struct proc *p, struct sem_undo **supptr,
33 int semid, int semnum, int adjval);
34 static void semundo_clear (int semid, int semnum);
36 /* XXX casting to (sy_call_t *) is bogus, as usual. */
37 static sy_call_t *semcalls[] = {
38 (sy_call_t *)sys___semctl, (sy_call_t *)sys_semget,
39 (sy_call_t *)sys_semop
42 static int semtot = 0;
43 static struct semid_ds *sema; /* semaphore id pool */
44 static struct sem *sem; /* semaphore pool */
45 static struct sem_undo *semu_list; /* list of active undo structures */
46 static int *semu; /* undo structure pool */
48 struct sem {
49 u_short semval; /* semaphore value */
50 pid_t sempid; /* pid of last operation */
51 u_short semncnt; /* # awaiting semval > cval */
52 u_short semzcnt; /* # awaiting semval = 0 */
56 * Undo structure (one per process)
58 struct sem_undo {
59 struct sem_undo *un_next; /* ptr to next active undo structure */
60 struct proc *un_proc; /* owner of this structure */
61 short un_cnt; /* # of active entries */
62 struct undo {
63 short un_adjval; /* adjust on exit values */
64 short un_num; /* semaphore # */
65 int un_id; /* semid */
66 } un_ent[1]; /* undo entries */
70 * Configuration parameters
72 #ifndef SEMMNI
73 #define SEMMNI 10 /* # of semaphore identifiers */
74 #endif
75 #ifndef SEMMNS
76 #define SEMMNS 60 /* # of semaphores in system */
77 #endif
78 #ifndef SEMUME
79 #define SEMUME 10 /* max # of undo entries per process */
80 #endif
81 #ifndef SEMMNU
82 #define SEMMNU 30 /* # of undo structures in system */
83 #endif
85 /* shouldn't need tuning */
86 #ifndef SEMMAP
87 #define SEMMAP 30 /* # of entries in semaphore map */
88 #endif
89 #ifndef SEMMSL
90 #define SEMMSL SEMMNS /* max # of semaphores per id */
91 #endif
92 #ifndef SEMOPM
93 #define SEMOPM 100 /* max # of operations per semop call */
94 #endif
96 #define SEMVMX 32767 /* semaphore maximum value */
97 #define SEMAEM 16384 /* adjust on exit max value */
100 * Due to the way semaphore memory is allocated, we have to ensure that
101 * SEMUSZ is properly aligned.
104 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
106 /* actual size of an undo structure */
107 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
110 * Macro to find a particular sem_undo vector
112 #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
115 * semaphore info struct
117 struct seminfo seminfo = {
118 SEMMAP, /* # of entries in semaphore map */
119 SEMMNI, /* # of semaphore identifiers */
120 SEMMNS, /* # of semaphores in system */
121 SEMMNU, /* # of undo structures in system */
122 SEMMSL, /* max # of semaphores per id */
123 SEMOPM, /* max # of operations per semop call */
124 SEMUME, /* max # of undo entries per process */
125 SEMUSZ, /* size in bytes of undo structure */
126 SEMVMX, /* semaphore maximum value */
127 SEMAEM /* adjust on exit max value */
130 TUNABLE_INT("kern.ipc.semmap", &seminfo.semmap);
131 TUNABLE_INT("kern.ipc.semmni", &seminfo.semmni);
132 TUNABLE_INT("kern.ipc.semmns", &seminfo.semmns);
133 TUNABLE_INT("kern.ipc.semmnu", &seminfo.semmnu);
134 TUNABLE_INT("kern.ipc.semmsl", &seminfo.semmsl);
135 TUNABLE_INT("kern.ipc.semopm", &seminfo.semopm);
136 TUNABLE_INT("kern.ipc.semume", &seminfo.semume);
137 TUNABLE_INT("kern.ipc.semusz", &seminfo.semusz);
138 TUNABLE_INT("kern.ipc.semvmx", &seminfo.semvmx);
139 TUNABLE_INT("kern.ipc.semaem", &seminfo.semaem);
141 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0, "");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0, "");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0, "");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0, "");
148 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0, "");
149 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
150 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
152 #if 0
153 RO seminfo.semmap /* SEMMAP unused */
154 RO seminfo.semmni
155 RO seminfo.semmns
156 RO seminfo.semmnu /* undo entries per system */
157 RW seminfo.semmsl
158 RO seminfo.semopm /* SEMOPM unused */
159 RO seminfo.semume
160 RO seminfo.semusz /* param - derived from SEMUME for per-proc sizeof */
161 RO seminfo.semvmx /* SEMVMX unused - user param */
162 RO seminfo.semaem /* SEMAEM unused - user param */
163 #endif
165 static void
166 seminit(void *dummy)
168 int i;
170 sem = kmalloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
171 sema = kmalloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
172 semu = kmalloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
174 for (i = 0; i < seminfo.semmni; i++) {
175 sema[i].sem_base = 0;
176 sema[i].sem_perm.mode = 0;
178 for (i = 0; i < seminfo.semmnu; i++) {
179 struct sem_undo *suptr = SEMU(i);
180 suptr->un_proc = NULL;
182 semu_list = NULL;
184 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
187 * Entry point for all SEM calls
189 * semsys_args(int which, a2, a3, ...) (VARARGS)
191 * MPALMOSTSAFE
194 sys_semsys(struct semsys_args *uap)
196 struct thread *td = curthread;
197 unsigned int which = (unsigned int)uap->which;
198 int error;
200 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
201 return (ENOSYS);
203 if (which >= sizeof(semcalls)/sizeof(semcalls[0]))
204 return (EINVAL);
205 bcopy(&uap->a2, &uap->which,
206 sizeof(struct semsys_args) - offsetof(struct semsys_args, a2));
207 get_mplock();
208 error = (*semcalls[which])(uap);
209 rel_mplock();
210 return (error);
214 * Allocate a new sem_undo structure for a process
215 * (returns ptr to structure or NULL if no more room)
218 static struct sem_undo *
219 semu_alloc(struct proc *p)
221 int i;
222 struct sem_undo *suptr;
223 struct sem_undo **supptr;
224 int attempt;
227 * Try twice to allocate something.
228 * (we'll purge any empty structures after the first pass so
229 * two passes are always enough)
232 for (attempt = 0; attempt < 2; attempt++) {
234 * Look for a free structure.
235 * Fill it in and return it if we find one.
238 for (i = 0; i < seminfo.semmnu; i++) {
239 suptr = SEMU(i);
240 if (suptr->un_proc == NULL) {
241 suptr->un_next = semu_list;
242 semu_list = suptr;
243 suptr->un_cnt = 0;
244 suptr->un_proc = p;
245 return(suptr);
250 * We didn't find a free one, if this is the first attempt
251 * then try to free some structures.
254 if (attempt == 0) {
255 /* All the structures are in use - try to free some */
256 int did_something = 0;
258 supptr = &semu_list;
259 while ((suptr = *supptr) != NULL) {
260 if (suptr->un_cnt == 0) {
261 suptr->un_proc = NULL;
262 *supptr = suptr->un_next;
263 did_something = 1;
264 } else
265 supptr = &(suptr->un_next);
268 /* If we didn't free anything then just give-up */
269 if (!did_something)
270 return(NULL);
271 } else {
273 * The second pass failed even though we freed
274 * something after the first pass!
275 * This is IMPOSSIBLE!
277 panic("semu_alloc - second attempt failed");
280 return (NULL);
284 * Adjust a particular entry for a particular proc
287 static int
288 semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
289 int adjval)
291 struct sem_undo *suptr;
292 struct undo *sunptr;
293 int i;
295 /* Look for and remember the sem_undo if the caller doesn't provide
296 it */
298 suptr = *supptr;
299 if (suptr == NULL) {
300 for (suptr = semu_list; suptr != NULL;
301 suptr = suptr->un_next) {
302 if (suptr->un_proc == p) {
303 *supptr = suptr;
304 break;
307 if (suptr == NULL) {
308 if (adjval == 0)
309 return(0);
310 suptr = semu_alloc(p);
311 if (suptr == NULL)
312 return(ENOSPC);
313 *supptr = suptr;
318 * Look for the requested entry and adjust it (delete if adjval becomes
319 * 0).
321 sunptr = &suptr->un_ent[0];
322 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
323 if (sunptr->un_id != semid || sunptr->un_num != semnum)
324 continue;
325 if (adjval == 0)
326 sunptr->un_adjval = 0;
327 else
328 sunptr->un_adjval += adjval;
329 if (sunptr->un_adjval == 0) {
330 suptr->un_cnt--;
331 if (i < suptr->un_cnt)
332 suptr->un_ent[i] =
333 suptr->un_ent[suptr->un_cnt];
335 return(0);
338 /* Didn't find the right entry - create it */
339 if (adjval == 0)
340 return(0);
341 if (suptr->un_cnt != seminfo.semume) {
342 sunptr = &suptr->un_ent[suptr->un_cnt];
343 suptr->un_cnt++;
344 sunptr->un_adjval = adjval;
345 sunptr->un_id = semid; sunptr->un_num = semnum;
346 } else
347 return(EINVAL);
348 return(0);
351 static void
352 semundo_clear(int semid, int semnum)
354 struct sem_undo *suptr;
356 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
357 struct undo *sunptr = &suptr->un_ent[0];
358 int i = 0;
360 while (i < suptr->un_cnt) {
361 if (sunptr->un_id == semid) {
362 if (semnum == -1 || sunptr->un_num == semnum) {
363 suptr->un_cnt--;
364 if (i < suptr->un_cnt) {
365 suptr->un_ent[i] =
366 suptr->un_ent[suptr->un_cnt];
367 continue;
370 if (semnum != -1)
371 break;
373 i++, sunptr++;
379 * Note that the user-mode half of this passes a union, not a pointer
381 * MPALMOSTSAFE
384 sys___semctl(struct __semctl_args *uap)
386 struct thread *td = curthread;
387 int semid = uap->semid;
388 int semnum = uap->semnum;
389 int cmd = uap->cmd;
390 union semun *arg = uap->arg;
391 union semun real_arg;
392 struct ucred *cred = td->td_ucred;
393 int i, rval, eval;
394 struct semid_ds sbuf;
395 struct semid_ds *semaptr;
397 #ifdef SEM_DEBUG
398 kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
399 #endif
401 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
402 return (ENOSYS);
404 semid = IPCID_TO_IX(semid);
405 if (semid < 0 || semid >= seminfo.semmni)
406 return(EINVAL);
408 get_mplock();
409 semaptr = &sema[semid];
410 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
411 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
412 rel_mplock();
413 return(EINVAL);
416 eval = 0;
417 rval = 0;
419 switch (cmd) {
420 case IPC_RMID:
421 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M)) != 0)
422 break;
423 semaptr->sem_perm.cuid = cred->cr_uid;
424 semaptr->sem_perm.uid = cred->cr_uid;
425 semtot -= semaptr->sem_nsems;
426 for (i = semaptr->sem_base - sem; i < semtot; i++)
427 sem[i] = sem[i + semaptr->sem_nsems];
428 for (i = 0; i < seminfo.semmni; i++) {
429 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
430 sema[i].sem_base > semaptr->sem_base)
431 sema[i].sem_base -= semaptr->sem_nsems;
433 semaptr->sem_perm.mode = 0;
434 semundo_clear(semid, -1);
435 wakeup((caddr_t)semaptr);
436 break;
438 case IPC_SET:
439 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M);
440 if (eval)
441 break;
442 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
443 break;
444 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
445 sizeof(sbuf))) != 0) {
446 break;
448 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
449 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
450 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
451 (sbuf.sem_perm.mode & 0777);
452 semaptr->sem_ctime = time_second;
453 break;
455 case IPC_STAT:
456 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R)))
457 break;
458 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
459 break;
460 eval = copyout(semaptr, real_arg.buf, sizeof(struct semid_ds));
461 break;
463 case GETNCNT:
464 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
465 if (eval)
466 break;
467 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
468 eval = EINVAL;
469 break;
471 rval = semaptr->sem_base[semnum].semncnt;
472 break;
474 case GETPID:
475 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
476 if (eval)
477 break;
478 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
479 eval = EINVAL;
480 break;
482 rval = semaptr->sem_base[semnum].sempid;
483 break;
485 case GETVAL:
486 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
487 if (eval)
488 break;
489 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
490 eval = EINVAL;
491 break;
493 rval = semaptr->sem_base[semnum].semval;
494 break;
496 case GETALL:
497 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
498 if (eval)
499 break;
500 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
501 break;
502 for (i = 0; i < semaptr->sem_nsems; i++) {
503 eval = copyout(&semaptr->sem_base[i].semval,
504 &real_arg.array[i],
505 sizeof(real_arg.array[0]));
506 if (eval)
507 break;
509 break;
511 case GETZCNT:
512 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
513 if (eval)
514 break;
515 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
516 eval = EINVAL;
517 break;
519 rval = semaptr->sem_base[semnum].semzcnt;
520 break;
522 case SETVAL:
523 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W);
524 if (eval)
525 break;
526 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
527 eval = EINVAL;
528 break;
530 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
531 break;
532 semaptr->sem_base[semnum].semval = real_arg.val;
533 semundo_clear(semid, semnum);
534 wakeup((caddr_t)semaptr);
535 break;
537 case SETALL:
538 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W);
539 if (eval)
540 break;
541 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
542 break;
543 for (i = 0; i < semaptr->sem_nsems; i++) {
544 eval = copyin(&real_arg.array[i],
545 (caddr_t)&semaptr->sem_base[i].semval,
546 sizeof(real_arg.array[0]));
547 if (eval != 0)
548 break;
550 semundo_clear(semid, -1);
551 wakeup((caddr_t)semaptr);
552 break;
554 default:
555 eval = EINVAL;
556 break;
558 rel_mplock();
560 if (eval == 0)
561 uap->sysmsg_result = rval;
562 return(eval);
566 * MPALMOSTSAFE
569 sys_semget(struct semget_args *uap)
571 struct thread *td = curthread;
572 int semid, eval;
573 int key = uap->key;
574 int nsems = uap->nsems;
575 int semflg = uap->semflg;
576 struct ucred *cred = td->td_ucred;
578 #ifdef SEM_DEBUG
579 kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
580 #endif
582 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
583 return (ENOSYS);
585 get_mplock();
586 eval = 0;
588 if (key != IPC_PRIVATE) {
589 for (semid = 0; semid < seminfo.semmni; semid++) {
590 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
591 sema[semid].sem_perm.key == key)
592 break;
594 if (semid < seminfo.semmni) {
595 #ifdef SEM_DEBUG
596 kprintf("found public key\n");
597 #endif
598 if ((eval = ipcperm(td->td_proc,
599 &sema[semid].sem_perm,
600 semflg & 0700))) {
601 goto done;
603 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
604 #ifdef SEM_DEBUG
605 kprintf("too small\n");
606 #endif
607 eval = EINVAL;
608 goto done;
610 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
611 #ifdef SEM_DEBUG
612 kprintf("not exclusive\n");
613 #endif
614 eval = EEXIST;
615 goto done;
617 goto done;
621 #ifdef SEM_DEBUG
622 kprintf("need to allocate the semid_ds\n");
623 #endif
624 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
625 if (nsems <= 0 || nsems > seminfo.semmsl) {
626 #ifdef SEM_DEBUG
627 kprintf("nsems out of range (0<%d<=%d)\n", nsems,
628 seminfo.semmsl);
629 #endif
630 eval = EINVAL;
631 goto done;
633 if (nsems > seminfo.semmns - semtot) {
634 #ifdef SEM_DEBUG
635 kprintf("not enough semaphores left (need %d, got %d)\n",
636 nsems, seminfo.semmns - semtot);
637 #endif
638 eval = ENOSPC;
639 goto done;
641 for (semid = 0; semid < seminfo.semmni; semid++) {
642 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
643 break;
645 if (semid == seminfo.semmni) {
646 #ifdef SEM_DEBUG
647 kprintf("no more semid_ds's available\n");
648 #endif
649 eval = ENOSPC;
650 goto done;
652 #ifdef SEM_DEBUG
653 kprintf("semid %d is available\n", semid);
654 #endif
655 sema[semid].sem_perm.key = key;
656 sema[semid].sem_perm.cuid = cred->cr_uid;
657 sema[semid].sem_perm.uid = cred->cr_uid;
658 sema[semid].sem_perm.cgid = cred->cr_gid;
659 sema[semid].sem_perm.gid = cred->cr_gid;
660 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
661 sema[semid].sem_perm.seq =
662 (sema[semid].sem_perm.seq + 1) & 0x7fff;
663 sema[semid].sem_nsems = nsems;
664 sema[semid].sem_otime = 0;
665 sema[semid].sem_ctime = time_second;
666 sema[semid].sem_base = &sem[semtot];
667 semtot += nsems;
668 bzero(sema[semid].sem_base,
669 sizeof(sema[semid].sem_base[0])*nsems);
670 #ifdef SEM_DEBUG
671 kprintf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
672 &sem[semtot]);
673 #endif
674 } else {
675 #ifdef SEM_DEBUG
676 kprintf("didn't find it and wasn't asked to create it\n");
677 #endif
678 eval = ENOENT;
681 done:
682 if (eval == 0) {
683 uap->sysmsg_result = IXSEQ_TO_IPCID(semid,
684 sema[semid].sem_perm);
686 rel_mplock();
687 return(eval);
691 * MPALMOSTSAFE
694 sys_semop(struct semop_args *uap)
696 struct thread *td = curthread;
697 int semid = uap->semid;
698 u_int nsops = uap->nsops;
699 struct sembuf sops[MAX_SOPS];
700 struct semid_ds *semaptr;
701 struct sembuf *sopptr;
702 struct sem *semptr;
703 struct sem_undo *suptr = NULL;
704 int i, j, eval;
705 int do_wakeup, do_undos;
707 #ifdef SEM_DEBUG
708 kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
709 #endif
711 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
712 return (ENOSYS);
714 get_mplock();
715 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
717 if (semid < 0 || semid >= seminfo.semmni) {
718 eval = EINVAL;
719 goto done;
722 semaptr = &sema[semid];
723 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
724 eval = EINVAL;
725 goto done;
727 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
728 eval = EINVAL;
729 goto done;
732 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W))) {
733 #ifdef SEM_DEBUG
734 kprintf("eval = %d from ipaccess\n", eval);
735 #endif
736 goto done;
739 if (nsops > MAX_SOPS) {
740 #ifdef SEM_DEBUG
741 kprintf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
742 #endif
743 eval = E2BIG;
744 goto done;
747 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
748 #ifdef SEM_DEBUG
749 kprintf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
750 uap->sops, &sops, nsops * sizeof(sops[0]));
751 #endif
752 goto done;
756 * Loop trying to satisfy the vector of requests.
757 * If we reach a point where we must wait, any requests already
758 * performed are rolled back and we go to sleep until some other
759 * process wakes us up. At this point, we start all over again.
761 * This ensures that from the perspective of other tasks, a set
762 * of requests is atomic (never partially satisfied).
764 do_undos = 0;
766 for (;;) {
767 do_wakeup = 0;
769 for (i = 0; i < nsops; i++) {
770 sopptr = &sops[i];
772 if (sopptr->sem_num >= semaptr->sem_nsems) {
773 eval = EFBIG;
774 goto done;
777 semptr = &semaptr->sem_base[sopptr->sem_num];
779 #ifdef SEM_DEBUG
780 kprintf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
781 semaptr, semaptr->sem_base, semptr,
782 sopptr->sem_num, semptr->semval, sopptr->sem_op,
783 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
784 #endif
786 if (sopptr->sem_op < 0) {
787 if (semptr->semval + sopptr->sem_op < 0) {
788 #ifdef SEM_DEBUG
789 kprintf("semop: can't do it now\n");
790 #endif
791 break;
792 } else {
793 semptr->semval += sopptr->sem_op;
794 if (semptr->semval == 0 &&
795 semptr->semzcnt > 0)
796 do_wakeup = 1;
798 if (sopptr->sem_flg & SEM_UNDO)
799 do_undos = 1;
800 } else if (sopptr->sem_op == 0) {
801 if (semptr->semval > 0) {
802 #ifdef SEM_DEBUG
803 kprintf("semop: not zero now\n");
804 #endif
805 break;
807 } else {
808 if (semptr->semncnt > 0)
809 do_wakeup = 1;
810 semptr->semval += sopptr->sem_op;
811 if (sopptr->sem_flg & SEM_UNDO)
812 do_undos = 1;
817 * Did we get through the entire vector?
819 if (i >= nsops)
820 goto donex;
823 * No ... rollback anything that we've already done
825 #ifdef SEM_DEBUG
826 kprintf("semop: rollback 0 through %d\n", i-1);
827 #endif
828 for (j = 0; j < i; j++)
829 semaptr->sem_base[sops[j].sem_num].semval -=
830 sops[j].sem_op;
833 * If the request that we couldn't satisfy has the
834 * NOWAIT flag set then return with EAGAIN.
836 if (sopptr->sem_flg & IPC_NOWAIT) {
837 eval = EAGAIN;
838 goto done;
841 if (sopptr->sem_op == 0)
842 semptr->semzcnt++;
843 else
844 semptr->semncnt++;
846 #ifdef SEM_DEBUG
847 kprintf("semop: good night!\n");
848 #endif
849 eval = tsleep((caddr_t)semaptr, PCATCH, "semwait", 0);
850 #ifdef SEM_DEBUG
851 kprintf("semop: good morning (eval=%d)!\n", eval);
852 #endif
854 suptr = NULL; /* sem_undo may have been reallocated */
856 /* return code is checked below, after sem[nz]cnt-- */
859 * Make sure that the semaphore still exists
861 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
862 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
863 eval = EIDRM;
864 goto done;
868 * The semaphore is still alive. Readjust the count of
869 * waiting processes.
871 if (sopptr->sem_op == 0)
872 semptr->semzcnt--;
873 else
874 semptr->semncnt--;
877 * Is it really morning, or was our sleep interrupted?
878 * (Delayed check of tsleep() return code because we
879 * need to decrement sem[nz]cnt either way.)
881 if (eval) {
882 eval = EINTR;
883 goto done;
885 #ifdef SEM_DEBUG
886 kprintf("semop: good morning!\n");
887 #endif
890 donex:
892 * Process any SEM_UNDO requests.
894 if (do_undos) {
895 for (i = 0; i < nsops; i++) {
897 * We only need to deal with SEM_UNDO's for non-zero
898 * op's.
900 int adjval;
902 if ((sops[i].sem_flg & SEM_UNDO) == 0)
903 continue;
904 adjval = sops[i].sem_op;
905 if (adjval == 0)
906 continue;
907 eval = semundo_adjust(td->td_proc, &suptr, semid,
908 sops[i].sem_num, -adjval);
909 if (eval == 0)
910 continue;
913 * Oh-Oh! We ran out of either sem_undo's or undo's.
914 * Rollback the adjustments to this point and then
915 * rollback the semaphore ups and down so we can return
916 * with an error with all structures restored. We
917 * rollback the undo's in the exact reverse order that
918 * we applied them. This guarantees that we won't run
919 * out of space as we roll things back out.
921 for (j = i - 1; j >= 0; j--) {
922 if ((sops[j].sem_flg & SEM_UNDO) == 0)
923 continue;
924 adjval = sops[j].sem_op;
925 if (adjval == 0)
926 continue;
927 if (semundo_adjust(td->td_proc, &suptr, semid,
928 sops[j].sem_num, adjval) != 0)
929 panic("semop - can't undo undos");
932 for (j = 0; j < nsops; j++)
933 semaptr->sem_base[sops[j].sem_num].semval -=
934 sops[j].sem_op;
936 #ifdef SEM_DEBUG
937 kprintf("eval = %d from semundo_adjust\n", eval);
938 #endif
939 goto done;
940 } /* loop through the sops */
941 } /* if (do_undos) */
943 /* We're definitely done - set the sempid's */
944 for (i = 0; i < nsops; i++) {
945 sopptr = &sops[i];
946 semptr = &semaptr->sem_base[sopptr->sem_num];
947 semptr->sempid = td->td_proc->p_pid;
950 /* Do a wakeup if any semaphore was up'd. */
951 if (do_wakeup) {
952 #ifdef SEM_DEBUG
953 kprintf("semop: doing wakeup\n");
954 #endif
955 wakeup((caddr_t)semaptr);
956 #ifdef SEM_DEBUG
957 kprintf("semop: back from wakeup\n");
958 #endif
960 #ifdef SEM_DEBUG
961 kprintf("semop: done\n");
962 #endif
963 uap->sysmsg_result = 0;
964 eval = 0;
965 done:
966 rel_mplock();
967 return(eval);
971 * Go through the undo structures for this process and apply the adjustments to
972 * semaphores.
974 void
975 semexit(struct proc *p)
977 struct sem_undo *suptr;
978 struct sem_undo **supptr;
979 int did_something;
981 did_something = 0;
984 * Go through the chain of undo vectors looking for one
985 * associated with this process.
988 for (supptr = &semu_list; (suptr = *supptr) != NULL;
989 supptr = &suptr->un_next) {
990 if (suptr->un_proc == p)
991 break;
994 if (suptr == NULL)
995 return;
997 #ifdef SEM_DEBUG
998 kprintf("proc @%08x has undo structure with %d entries\n", p,
999 suptr->un_cnt);
1000 #endif
1003 * If there are any active undo elements then process them.
1005 if (suptr->un_cnt > 0) {
1006 int ix;
1008 for (ix = 0; ix < suptr->un_cnt; ix++) {
1009 int semid = suptr->un_ent[ix].un_id;
1010 int semnum = suptr->un_ent[ix].un_num;
1011 int adjval = suptr->un_ent[ix].un_adjval;
1012 struct semid_ds *semaptr;
1014 semaptr = &sema[semid];
1015 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1016 panic("semexit - semid not allocated");
1017 if (semnum >= semaptr->sem_nsems)
1018 panic("semexit - semnum out of range");
1020 #ifdef SEM_DEBUG
1021 kprintf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1022 suptr->un_proc, suptr->un_ent[ix].un_id,
1023 suptr->un_ent[ix].un_num,
1024 suptr->un_ent[ix].un_adjval,
1025 semaptr->sem_base[semnum].semval);
1026 #endif
1028 if (adjval < 0) {
1029 if (semaptr->sem_base[semnum].semval < -adjval)
1030 semaptr->sem_base[semnum].semval = 0;
1031 else
1032 semaptr->sem_base[semnum].semval +=
1033 adjval;
1034 } else
1035 semaptr->sem_base[semnum].semval += adjval;
1037 wakeup((caddr_t)semaptr);
1038 #ifdef SEM_DEBUG
1039 kprintf("semexit: back from wakeup\n");
1040 #endif
1045 * Deallocate the undo vector.
1047 #ifdef SEM_DEBUG
1048 kprintf("removing vector\n");
1049 #endif
1050 suptr->un_proc = NULL;
1051 *supptr = suptr->un_next;