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