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