FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_sem.c
1 /*-
2 * Implementation of SVID semaphores
3 *
4 * Author: Daniel Boulet
5 *
6 * This software is provided ``AS IS'' without any warranties of any kind.
7 */
8
9 #include <sys/cdefs.h>
10 __FBSDID("$FreeBSD: releng/5.4/sys/kern/sysv_sem.c 145335 2005-04-20 19:11:07Z cvs2svn $");
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/eventhandler.h>
18 #include <sys/kernel.h>
19 #include <sys/proc.h>
20 #include <sys/lock.h>
21 #include <sys/module.h>
22 #include <sys/mutex.h>
23 #include <sys/sem.h>
24 #include <sys/syscall.h>
25 #include <sys/sysent.h>
26 #include <sys/sysctl.h>
27 #include <sys/malloc.h>
28 #include <sys/jail.h>
29
30 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
31
32 #ifdef SEM_DEBUG
33 #define DPRINTF(a) printf a
34 #else
35 #define DPRINTF(a)
36 #endif
37
38 static void seminit(void);
39 static int sysvsem_modload(struct module *, int, void *);
40 static int semunload(void);
41 static void semexit_myhook(void *arg, struct proc *p);
42 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
43 static int semvalid(int semid, struct semid_ds *semaptr);
44
45 #ifndef _SYS_SYSPROTO_H_
46 struct __semctl_args;
47 int __semctl(struct thread *td, struct __semctl_args *uap);
48 struct semget_args;
49 int semget(struct thread *td, struct semget_args *uap);
50 struct semop_args;
51 int semop(struct thread *td, struct semop_args *uap);
52 #endif
53
54 static struct sem_undo *semu_alloc(struct thread *td);
55 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
56 int semid, int semnum, int adjval);
57 static void semundo_clear(int semid, int semnum);
58
59 /* XXX casting to (sy_call_t *) is bogus, as usual. */
60 static sy_call_t *semcalls[] = {
61 (sy_call_t *)__semctl, (sy_call_t *)semget,
62 (sy_call_t *)semop
63 };
64
65 static struct mtx sem_mtx; /* semaphore global lock */
66 static int semtot = 0;
67 static struct semid_ds *sema; /* semaphore id pool */
68 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
69 static struct sem *sem; /* semaphore pool */
70 SLIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
71 static int *semu; /* undo structure pool */
72 static eventhandler_tag semexit_tag;
73
74 #define SEMUNDO_MTX sem_mtx
75 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
76 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
77 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
78
79 struct sem {
80 u_short semval; /* semaphore value */
81 pid_t sempid; /* pid of last operation */
82 u_short semncnt; /* # awaiting semval > cval */
83 u_short semzcnt; /* # awaiting semval = 0 */
84 };
85
86 /*
87 * Undo structure (one per process)
88 */
89 struct sem_undo {
90 SLIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
91 struct proc *un_proc; /* owner of this structure */
92 short un_cnt; /* # of active entries */
93 struct undo {
94 short un_adjval; /* adjust on exit values */
95 short un_num; /* semaphore # */
96 int un_id; /* semid */
97 } un_ent[1]; /* undo entries */
98 };
99
100 /*
101 * Configuration parameters
102 */
103 #ifndef SEMMNI
104 #define SEMMNI 10 /* # of semaphore identifiers */
105 #endif
106 #ifndef SEMMNS
107 #define SEMMNS 60 /* # of semaphores in system */
108 #endif
109 #ifndef SEMUME
110 #define SEMUME 10 /* max # of undo entries per process */
111 #endif
112 #ifndef SEMMNU
113 #define SEMMNU 30 /* # of undo structures in system */
114 #endif
115
116 /* shouldn't need tuning */
117 #ifndef SEMMAP
118 #define SEMMAP 30 /* # of entries in semaphore map */
119 #endif
120 #ifndef SEMMSL
121 #define SEMMSL SEMMNS /* max # of semaphores per id */
122 #endif
123 #ifndef SEMOPM
124 #define SEMOPM 100 /* max # of operations per semop call */
125 #endif
126
127 #define SEMVMX 32767 /* semaphore maximum value */
128 #define SEMAEM 16384 /* adjust on exit max value */
129
130 /*
131 * Due to the way semaphore memory is allocated, we have to ensure that
132 * SEMUSZ is properly aligned.
133 */
134
135 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
136
137 /* actual size of an undo structure */
138 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
139
140 /*
141 * Macro to find a particular sem_undo vector
142 */
143 #define SEMU(ix) \
144 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
145
146 /*
147 * semaphore info struct
148 */
149 struct seminfo seminfo = {
150 SEMMAP, /* # of entries in semaphore map */
151 SEMMNI, /* # of semaphore identifiers */
152 SEMMNS, /* # of semaphores in system */
153 SEMMNU, /* # of undo structures in system */
154 SEMMSL, /* max # of semaphores per id */
155 SEMOPM, /* max # of operations per semop call */
156 SEMUME, /* max # of undo entries per process */
157 SEMUSZ, /* size in bytes of undo structure */
158 SEMVMX, /* semaphore maximum value */
159 SEMAEM /* adjust on exit max value */
160 };
161
162 SYSCTL_DECL(_kern_ipc);
163 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0,
164 "Number of entries in the semaphore map");
165 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
166 "Number of semaphore identifiers");
167 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
168 "Maximum number of semaphores in the system");
169 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
170 "Maximum number of undo structures in the system");
171 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
172 "Max semaphores per id");
173 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
174 "Max operations per semop call");
175 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
176 "Max undo entries per process");
177 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
178 "Size in bytes of undo structure");
179 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
180 "Semaphore maximum value");
181 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
182 "Adjust on exit max value");
183 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
184 NULL, 0, sysctl_sema, "", "");
185
186 static void
187 seminit(void)
188 {
189 int i;
190
191 TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
192 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
193 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
194 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
195 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
196 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
197 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
198 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
199 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
200 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
201
202 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
203 sema = malloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM,
204 M_WAITOK);
205 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
206 M_WAITOK | M_ZERO);
207 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
208
209 for (i = 0; i < seminfo.semmni; i++) {
210 sema[i].sem_base = 0;
211 sema[i].sem_perm.mode = 0;
212 sema[i].sem_perm.seq = 0;
213 }
214 for (i = 0; i < seminfo.semmni; i++)
215 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
216 for (i = 0; i < seminfo.semmnu; i++) {
217 struct sem_undo *suptr = SEMU(i);
218 suptr->un_proc = NULL;
219 }
220 SLIST_INIT(&semu_list);
221 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
222 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
223 EVENTHANDLER_PRI_ANY);
224 }
225
226 static int
227 semunload(void)
228 {
229 int i;
230
231 if (semtot != 0)
232 return (EBUSY);
233
234 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
235 free(sem, M_SEM);
236 free(sema, M_SEM);
237 free(semu, M_SEM);
238 for (i = 0; i < seminfo.semmni; i++)
239 mtx_destroy(&sema_mtx[i]);
240 mtx_destroy(&sem_mtx);
241 return (0);
242 }
243
244 static int
245 sysvsem_modload(struct module *module, int cmd, void *arg)
246 {
247 int error = 0;
248
249 switch (cmd) {
250 case MOD_LOAD:
251 seminit();
252 break;
253 case MOD_UNLOAD:
254 error = semunload();
255 break;
256 case MOD_SHUTDOWN:
257 break;
258 default:
259 error = EINVAL;
260 break;
261 }
262 return (error);
263 }
264
265 static moduledata_t sysvsem_mod = {
266 "sysvsem",
267 &sysvsem_modload,
268 NULL
269 };
270
271 SYSCALL_MODULE_HELPER(semsys);
272 SYSCALL_MODULE_HELPER(__semctl);
273 SYSCALL_MODULE_HELPER(semget);
274 SYSCALL_MODULE_HELPER(semop);
275
276 DECLARE_MODULE(sysvsem, sysvsem_mod,
277 SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
278 MODULE_VERSION(sysvsem, 1);
279
280 /*
281 * Entry point for all SEM calls
282 *
283 * MPSAFE
284 */
285 int
286 semsys(td, uap)
287 struct thread *td;
288 /* XXX actually varargs. */
289 struct semsys_args /* {
290 int which;
291 int a2;
292 int a3;
293 int a4;
294 int a5;
295 } */ *uap;
296 {
297 int error;
298
299 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
300 return (ENOSYS);
301 if (uap->which < 0 ||
302 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
303 return (EINVAL);
304 error = (*semcalls[uap->which])(td, &uap->a2);
305 return (error);
306 }
307
308 /*
309 * Allocate a new sem_undo structure for a process
310 * (returns ptr to structure or NULL if no more room)
311 */
312
313 static struct sem_undo *
314 semu_alloc(td)
315 struct thread *td;
316 {
317 int i;
318 struct sem_undo *suptr;
319 struct sem_undo **supptr;
320 int attempt;
321
322 SEMUNDO_LOCKASSERT(MA_OWNED);
323 /*
324 * Try twice to allocate something.
325 * (we'll purge an empty structure after the first pass so
326 * two passes are always enough)
327 */
328
329 for (attempt = 0; attempt < 2; attempt++) {
330 /*
331 * Look for a free structure.
332 * Fill it in and return it if we find one.
333 */
334
335 for (i = 0; i < seminfo.semmnu; i++) {
336 suptr = SEMU(i);
337 if (suptr->un_proc == NULL) {
338 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
339 suptr->un_cnt = 0;
340 suptr->un_proc = td->td_proc;
341 return(suptr);
342 }
343 }
344
345 /*
346 * We didn't find a free one, if this is the first attempt
347 * then try to free a structure.
348 */
349
350 if (attempt == 0) {
351 /* All the structures are in use - try to free one */
352 int did_something = 0;
353
354 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list,
355 un_next) {
356 if (suptr->un_cnt == 0) {
357 suptr->un_proc = NULL;
358 did_something = 1;
359 *supptr = SLIST_NEXT(suptr, un_next);
360 break;
361 }
362 }
363
364 /* If we didn't free anything then just give-up */
365 if (!did_something)
366 return(NULL);
367 } else {
368 /*
369 * The second pass failed even though we freed
370 * something after the first pass!
371 * This is IMPOSSIBLE!
372 */
373 panic("semu_alloc - second attempt failed");
374 }
375 }
376 return (NULL);
377 }
378
379 /*
380 * Adjust a particular entry for a particular proc
381 */
382
383 static int
384 semundo_adjust(td, supptr, semid, semnum, adjval)
385 struct thread *td;
386 struct sem_undo **supptr;
387 int semid, semnum;
388 int adjval;
389 {
390 struct proc *p = td->td_proc;
391 struct sem_undo *suptr;
392 struct undo *sunptr;
393 int i;
394
395 SEMUNDO_LOCKASSERT(MA_OWNED);
396 /* Look for and remember the sem_undo if the caller doesn't provide
397 it */
398
399 suptr = *supptr;
400 if (suptr == NULL) {
401 SLIST_FOREACH(suptr, &semu_list, un_next) {
402 if (suptr->un_proc == p) {
403 *supptr = suptr;
404 break;
405 }
406 }
407 if (suptr == NULL) {
408 if (adjval == 0)
409 return(0);
410 suptr = semu_alloc(td);
411 if (suptr == NULL)
412 return(ENOSPC);
413 *supptr = suptr;
414 }
415 }
416
417 /*
418 * Look for the requested entry and adjust it (delete if adjval becomes
419 * 0).
420 */
421 sunptr = &suptr->un_ent[0];
422 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
423 if (sunptr->un_id != semid || sunptr->un_num != semnum)
424 continue;
425 if (adjval != 0) {
426 adjval += sunptr->un_adjval;
427 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
428 return (ERANGE);
429 }
430 sunptr->un_adjval = adjval;
431 if (sunptr->un_adjval == 0) {
432 suptr->un_cnt--;
433 if (i < suptr->un_cnt)
434 suptr->un_ent[i] =
435 suptr->un_ent[suptr->un_cnt];
436 }
437 return(0);
438 }
439
440 /* Didn't find the right entry - create it */
441 if (adjval == 0)
442 return(0);
443 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
444 return (ERANGE);
445 if (suptr->un_cnt != seminfo.semume) {
446 sunptr = &suptr->un_ent[suptr->un_cnt];
447 suptr->un_cnt++;
448 sunptr->un_adjval = adjval;
449 sunptr->un_id = semid; sunptr->un_num = semnum;
450 } else
451 return(EINVAL);
452 return(0);
453 }
454
455 static void
456 semundo_clear(semid, semnum)
457 int semid, semnum;
458 {
459 struct sem_undo *suptr;
460
461 SEMUNDO_LOCKASSERT(MA_OWNED);
462 SLIST_FOREACH(suptr, &semu_list, un_next) {
463 struct undo *sunptr = &suptr->un_ent[0];
464 int i = 0;
465
466 while (i < suptr->un_cnt) {
467 if (sunptr->un_id == semid) {
468 if (semnum == -1 || sunptr->un_num == semnum) {
469 suptr->un_cnt--;
470 if (i < suptr->un_cnt) {
471 suptr->un_ent[i] =
472 suptr->un_ent[suptr->un_cnt];
473 continue;
474 }
475 }
476 if (semnum != -1)
477 break;
478 }
479 i++, sunptr++;
480 }
481 }
482 }
483
484 static int
485 semvalid(semid, semaptr)
486 int semid;
487 struct semid_ds *semaptr;
488 {
489
490 return ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
491 semaptr->sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
492 }
493
494 /*
495 * Note that the user-mode half of this passes a union, not a pointer
496 */
497 #ifndef _SYS_SYSPROTO_H_
498 struct __semctl_args {
499 int semid;
500 int semnum;
501 int cmd;
502 union semun *arg;
503 };
504 #endif
505
506 /*
507 * MPSAFE
508 */
509 int
510 __semctl(td, uap)
511 struct thread *td;
512 struct __semctl_args *uap;
513 {
514 int semid = uap->semid;
515 int semnum = uap->semnum;
516 int cmd = uap->cmd;
517 u_short *array;
518 union semun *arg = uap->arg;
519 union semun real_arg;
520 struct ucred *cred = td->td_ucred;
521 int i, rval, error;
522 struct semid_ds sbuf;
523 struct semid_ds *semaptr;
524 struct mtx *sema_mtxp;
525 u_short usval, count;
526
527 DPRINTF(("call to semctl(%d, %d, %d, 0x%x)\n",
528 semid, semnum, cmd, arg));
529 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
530 return (ENOSYS);
531
532 array = NULL;
533
534 switch(cmd) {
535 case SEM_STAT:
536 if (semid < 0 || semid >= seminfo.semmni)
537 return (EINVAL);
538 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
539 return (error);
540 semaptr = &sema[semid];
541 sema_mtxp = &sema_mtx[semid];
542 mtx_lock(sema_mtxp);
543 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
544 error = EINVAL;
545 goto done2;
546 }
547 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
548 goto done2;
549 mtx_unlock(sema_mtxp);
550 error = copyout(semaptr, real_arg.buf, sizeof(struct semid_ds));
551 rval = IXSEQ_TO_IPCID(semid,semaptr->sem_perm);
552 if (error == 0)
553 td->td_retval[0] = rval;
554 return (error);
555 }
556
557 semid = IPCID_TO_IX(semid);
558 if (semid < 0 || semid >= seminfo.semmni)
559 return (EINVAL);
560
561 semaptr = &sema[semid];
562 sema_mtxp = &sema_mtx[semid];
563
564 error = 0;
565 rval = 0;
566
567 switch (cmd) {
568 case IPC_RMID:
569 mtx_lock(sema_mtxp);
570 if ((error = semvalid(uap->semid, semaptr)) != 0)
571 goto done2;
572 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
573 goto done2;
574 semaptr->sem_perm.cuid = cred->cr_uid;
575 semaptr->sem_perm.uid = cred->cr_uid;
576 semtot -= semaptr->sem_nsems;
577 for (i = semaptr->sem_base - sem; i < semtot; i++)
578 sem[i] = sem[i + semaptr->sem_nsems];
579 for (i = 0; i < seminfo.semmni; i++) {
580 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
581 sema[i].sem_base > semaptr->sem_base)
582 sema[i].sem_base -= semaptr->sem_nsems;
583 }
584 semaptr->sem_perm.mode = 0;
585 SEMUNDO_LOCK();
586 semundo_clear(semid, -1);
587 SEMUNDO_UNLOCK();
588 wakeup(semaptr);
589 break;
590
591 case IPC_SET:
592 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
593 goto done2;
594 if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
595 goto done2;
596 mtx_lock(sema_mtxp);
597 if ((error = semvalid(uap->semid, semaptr)) != 0)
598 goto done2;
599 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
600 goto done2;
601 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
602 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
603 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
604 (sbuf.sem_perm.mode & 0777);
605 semaptr->sem_ctime = time_second;
606 break;
607
608 case IPC_STAT:
609 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
610 goto done2;
611 mtx_lock(sema_mtxp);
612 if ((error = semvalid(uap->semid, semaptr)) != 0)
613 goto done2;
614 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
615 goto done2;
616 sbuf = *semaptr;
617 mtx_unlock(sema_mtxp);
618 error = copyout(semaptr, real_arg.buf,
619 sizeof(struct semid_ds));
620 break;
621
622 case GETNCNT:
623 mtx_lock(sema_mtxp);
624 if ((error = semvalid(uap->semid, semaptr)) != 0)
625 goto done2;
626 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
627 goto done2;
628 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
629 error = EINVAL;
630 goto done2;
631 }
632 rval = semaptr->sem_base[semnum].semncnt;
633 break;
634
635 case GETPID:
636 mtx_lock(sema_mtxp);
637 if ((error = semvalid(uap->semid, semaptr)) != 0)
638 goto done2;
639 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
640 goto done2;
641 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
642 error = EINVAL;
643 goto done2;
644 }
645 rval = semaptr->sem_base[semnum].sempid;
646 break;
647
648 case GETVAL:
649 mtx_lock(sema_mtxp);
650 if ((error = semvalid(uap->semid, semaptr)) != 0)
651 goto done2;
652 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
653 goto done2;
654 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
655 error = EINVAL;
656 goto done2;
657 }
658 rval = semaptr->sem_base[semnum].semval;
659 break;
660
661 case GETALL:
662 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
663 goto done2;
664 array = malloc(sizeof(*array) * semaptr->sem_nsems, M_TEMP,
665 M_WAITOK);
666 mtx_lock(sema_mtxp);
667 if ((error = semvalid(uap->semid, semaptr)) != 0)
668 goto done2;
669 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
670 goto done2;
671 for (i = 0; i < semaptr->sem_nsems; i++)
672 array[i] = semaptr->sem_base[i].semval;
673 mtx_unlock(sema_mtxp);
674 error = copyout(array, real_arg.array,
675 i * sizeof(real_arg.array[0]));
676 break;
677
678 case GETZCNT:
679 mtx_lock(sema_mtxp);
680 if ((error = semvalid(uap->semid, semaptr)) != 0)
681 goto done2;
682 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
683 goto done2;
684 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
685 error = EINVAL;
686 goto done2;
687 }
688 rval = semaptr->sem_base[semnum].semzcnt;
689 break;
690
691 case SETVAL:
692 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
693 goto done2;
694 mtx_lock(sema_mtxp);
695 if ((error = semvalid(uap->semid, semaptr)) != 0)
696 goto done2;
697 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
698 goto done2;
699 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
700 error = EINVAL;
701 goto done2;
702 }
703 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
704 error = ERANGE;
705 goto done2;
706 }
707 semaptr->sem_base[semnum].semval = real_arg.val;
708 SEMUNDO_LOCK();
709 semundo_clear(semid, semnum);
710 SEMUNDO_UNLOCK();
711 wakeup(semaptr);
712 break;
713
714 case SETALL:
715 mtx_lock(sema_mtxp);
716 raced:
717 if ((error = semvalid(uap->semid, semaptr)) != 0)
718 goto done2;
719 count = semaptr->sem_nsems;
720 mtx_unlock(sema_mtxp);
721 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
722 goto done2;
723 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
724 error = copyin(real_arg.array, array, count * sizeof(*array));
725 if (error)
726 break;
727 mtx_lock(sema_mtxp);
728 if ((error = semvalid(uap->semid, semaptr)) != 0)
729 goto done2;
730 /* we could have raced? */
731 if (count != semaptr->sem_nsems) {
732 free(array, M_TEMP);
733 array = NULL;
734 goto raced;
735 }
736 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
737 goto done2;
738 for (i = 0; i < semaptr->sem_nsems; i++) {
739 usval = array[i];
740 if (usval > seminfo.semvmx) {
741 error = ERANGE;
742 break;
743 }
744 semaptr->sem_base[i].semval = usval;
745 }
746 SEMUNDO_LOCK();
747 semundo_clear(semid, -1);
748 SEMUNDO_UNLOCK();
749 wakeup(semaptr);
750 break;
751
752 default:
753 error = EINVAL;
754 break;
755 }
756
757 if (error == 0)
758 td->td_retval[0] = rval;
759 done2:
760 if (mtx_owned(sema_mtxp))
761 mtx_unlock(sema_mtxp);
762 if (array != NULL)
763 free(array, M_TEMP);
764 return(error);
765 }
766
767 #ifndef _SYS_SYSPROTO_H_
768 struct semget_args {
769 key_t key;
770 int nsems;
771 int semflg;
772 };
773 #endif
774
775 /*
776 * MPSAFE
777 */
778 int
779 semget(td, uap)
780 struct thread *td;
781 struct semget_args *uap;
782 {
783 int semid, error = 0;
784 int key = uap->key;
785 int nsems = uap->nsems;
786 int semflg = uap->semflg;
787 struct ucred *cred = td->td_ucred;
788
789 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
790 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
791 return (ENOSYS);
792
793 mtx_lock(&Giant);
794 if (key != IPC_PRIVATE) {
795 for (semid = 0; semid < seminfo.semmni; semid++) {
796 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
797 sema[semid].sem_perm.key == key)
798 break;
799 }
800 if (semid < seminfo.semmni) {
801 DPRINTF(("found public key\n"));
802 if ((error = ipcperm(td, &sema[semid].sem_perm,
803 semflg & 0700))) {
804 goto done2;
805 }
806 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
807 DPRINTF(("too small\n"));
808 error = EINVAL;
809 goto done2;
810 }
811 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
812 DPRINTF(("not exclusive\n"));
813 error = EEXIST;
814 goto done2;
815 }
816 goto found;
817 }
818 }
819
820 DPRINTF(("need to allocate the semid_ds\n"));
821 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
822 if (nsems <= 0 || nsems > seminfo.semmsl) {
823 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
824 seminfo.semmsl));
825 error = EINVAL;
826 goto done2;
827 }
828 if (nsems > seminfo.semmns - semtot) {
829 DPRINTF((
830 "not enough semaphores left (need %d, got %d)\n",
831 nsems, seminfo.semmns - semtot));
832 error = ENOSPC;
833 goto done2;
834 }
835 for (semid = 0; semid < seminfo.semmni; semid++) {
836 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
837 break;
838 }
839 if (semid == seminfo.semmni) {
840 DPRINTF(("no more semid_ds's available\n"));
841 error = ENOSPC;
842 goto done2;
843 }
844 DPRINTF(("semid %d is available\n", semid));
845 sema[semid].sem_perm.key = key;
846 sema[semid].sem_perm.cuid = cred->cr_uid;
847 sema[semid].sem_perm.uid = cred->cr_uid;
848 sema[semid].sem_perm.cgid = cred->cr_gid;
849 sema[semid].sem_perm.gid = cred->cr_gid;
850 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
851 sema[semid].sem_perm.seq =
852 (sema[semid].sem_perm.seq + 1) & 0x7fff;
853 sema[semid].sem_nsems = nsems;
854 sema[semid].sem_otime = 0;
855 sema[semid].sem_ctime = time_second;
856 sema[semid].sem_base = &sem[semtot];
857 semtot += nsems;
858 bzero(sema[semid].sem_base,
859 sizeof(sema[semid].sem_base[0])*nsems);
860 DPRINTF(("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
861 &sem[semtot]));
862 } else {
863 DPRINTF(("didn't find it and wasn't asked to create it\n"));
864 error = ENOENT;
865 goto done2;
866 }
867
868 found:
869 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
870 done2:
871 mtx_unlock(&Giant);
872 return (error);
873 }
874
875 #ifndef _SYS_SYSPROTO_H_
876 struct semop_args {
877 int semid;
878 struct sembuf *sops;
879 size_t nsops;
880 };
881 #endif
882
883 /*
884 * MPSAFE
885 */
886 int
887 semop(td, uap)
888 struct thread *td;
889 struct semop_args *uap;
890 {
891 #define SMALL_SOPS 8
892 struct sembuf small_sops[SMALL_SOPS];
893 int semid = uap->semid;
894 size_t nsops = uap->nsops;
895 struct sembuf *sops;
896 struct semid_ds *semaptr;
897 struct sembuf *sopptr = 0;
898 struct sem *semptr = 0;
899 struct sem_undo *suptr;
900 struct mtx *sema_mtxp;
901 size_t i, j, k;
902 int error;
903 int do_wakeup, do_undos;
904
905 DPRINTF(("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops));
906
907 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
908 return (ENOSYS);
909
910 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
911
912 if (semid < 0 || semid >= seminfo.semmni)
913 return (EINVAL);
914
915 /* Allocate memory for sem_ops */
916 if (nsops <= SMALL_SOPS)
917 sops = small_sops;
918 else if (nsops <= seminfo.semopm)
919 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
920 else {
921 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
922 nsops));
923 return (E2BIG);
924 }
925 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
926 DPRINTF(("error = %d from copyin(%08x, %08x, %d)\n", error,
927 uap->sops, sops, nsops * sizeof(sops[0])));
928 if (sops != small_sops)
929 free(sops, M_SEM);
930 return (error);
931 }
932
933 semaptr = &sema[semid];
934 sema_mtxp = &sema_mtx[semid];
935 mtx_lock(sema_mtxp);
936 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
937 error = EINVAL;
938 goto done2;
939 }
940 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
941 error = EINVAL;
942 goto done2;
943 }
944 /*
945 * Initial pass thru sops to see what permissions are needed.
946 * Also perform any checks that don't need repeating on each
947 * attempt to satisfy the request vector.
948 */
949 j = 0; /* permission needed */
950 do_undos = 0;
951 for (i = 0; i < nsops; i++) {
952 sopptr = &sops[i];
953 if (sopptr->sem_num >= semaptr->sem_nsems) {
954 error = EFBIG;
955 goto done2;
956 }
957 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
958 do_undos = 1;
959 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
960 }
961
962 if ((error = ipcperm(td, &semaptr->sem_perm, j))) {
963 DPRINTF(("error = %d from ipaccess\n", error));
964 goto done2;
965 }
966
967 /*
968 * Loop trying to satisfy the vector of requests.
969 * If we reach a point where we must wait, any requests already
970 * performed are rolled back and we go to sleep until some other
971 * process wakes us up. At this point, we start all over again.
972 *
973 * This ensures that from the perspective of other tasks, a set
974 * of requests is atomic (never partially satisfied).
975 */
976 for (;;) {
977 do_wakeup = 0;
978 error = 0; /* error return if necessary */
979
980 for (i = 0; i < nsops; i++) {
981 sopptr = &sops[i];
982 semptr = &semaptr->sem_base[sopptr->sem_num];
983
984 DPRINTF((
985 "semop: semaptr=%x, sem_base=%x, "
986 "semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
987 semaptr, semaptr->sem_base, semptr,
988 sopptr->sem_num, semptr->semval, sopptr->sem_op,
989 (sopptr->sem_flg & IPC_NOWAIT) ?
990 "nowait" : "wait"));
991
992 if (sopptr->sem_op < 0) {
993 if (semptr->semval + sopptr->sem_op < 0) {
994 DPRINTF(("semop: can't do it now\n"));
995 break;
996 } else {
997 semptr->semval += sopptr->sem_op;
998 if (semptr->semval == 0 &&
999 semptr->semzcnt > 0)
1000 do_wakeup = 1;
1001 }
1002 } else if (sopptr->sem_op == 0) {
1003 if (semptr->semval != 0) {
1004 DPRINTF(("semop: not zero now\n"));
1005 break;
1006 }
1007 } else if (semptr->semval + sopptr->sem_op >
1008 seminfo.semvmx) {
1009 error = ERANGE;
1010 break;
1011 } else {
1012 if (semptr->semncnt > 0)
1013 do_wakeup = 1;
1014 semptr->semval += sopptr->sem_op;
1015 }
1016 }
1017
1018 /*
1019 * Did we get through the entire vector?
1020 */
1021 if (i >= nsops)
1022 goto done;
1023
1024 /*
1025 * No ... rollback anything that we've already done
1026 */
1027 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1028 for (j = 0; j < i; j++)
1029 semaptr->sem_base[sops[j].sem_num].semval -=
1030 sops[j].sem_op;
1031
1032 /* If we detected an error, return it */
1033 if (error != 0)
1034 goto done2;
1035
1036 /*
1037 * If the request that we couldn't satisfy has the
1038 * NOWAIT flag set then return with EAGAIN.
1039 */
1040 if (sopptr->sem_flg & IPC_NOWAIT) {
1041 error = EAGAIN;
1042 goto done2;
1043 }
1044
1045 if (sopptr->sem_op == 0)
1046 semptr->semzcnt++;
1047 else
1048 semptr->semncnt++;
1049
1050 DPRINTF(("semop: good night!\n"));
1051 error = msleep(semaptr, sema_mtxp, (PZERO - 4) | PCATCH,
1052 "semwait", 0);
1053 DPRINTF(("semop: good morning (error=%d)!\n", error));
1054 /* return code is checked below, after sem[nz]cnt-- */
1055
1056 /*
1057 * Make sure that the semaphore still exists
1058 */
1059 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
1060 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1061 error = EIDRM;
1062 goto done2;
1063 }
1064
1065 /*
1066 * The semaphore is still alive. Readjust the count of
1067 * waiting processes.
1068 */
1069 if (sopptr->sem_op == 0)
1070 semptr->semzcnt--;
1071 else
1072 semptr->semncnt--;
1073
1074 /*
1075 * Is it really morning, or was our sleep interrupted?
1076 * (Delayed check of msleep() return code because we
1077 * need to decrement sem[nz]cnt either way.)
1078 */
1079 if (error != 0) {
1080 error = EINTR;
1081 goto done2;
1082 }
1083 DPRINTF(("semop: good morning!\n"));
1084 }
1085
1086 done:
1087 /*
1088 * Process any SEM_UNDO requests.
1089 */
1090 if (do_undos) {
1091 SEMUNDO_LOCK();
1092 suptr = NULL;
1093 for (i = 0; i < nsops; i++) {
1094 /*
1095 * We only need to deal with SEM_UNDO's for non-zero
1096 * op's.
1097 */
1098 int adjval;
1099
1100 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1101 continue;
1102 adjval = sops[i].sem_op;
1103 if (adjval == 0)
1104 continue;
1105 error = semundo_adjust(td, &suptr, semid,
1106 sops[i].sem_num, -adjval);
1107 if (error == 0)
1108 continue;
1109
1110 /*
1111 * Oh-Oh! We ran out of either sem_undo's or undo's.
1112 * Rollback the adjustments to this point and then
1113 * rollback the semaphore ups and down so we can return
1114 * with an error with all structures restored. We
1115 * rollback the undo's in the exact reverse order that
1116 * we applied them. This guarantees that we won't run
1117 * out of space as we roll things back out.
1118 */
1119 for (j = 0; j < i; j++) {
1120 k = i - j - 1;
1121 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1122 continue;
1123 adjval = sops[k].sem_op;
1124 if (adjval == 0)
1125 continue;
1126 if (semundo_adjust(td, &suptr, semid,
1127 sops[k].sem_num, adjval) != 0)
1128 panic("semop - can't undo undos");
1129 }
1130
1131 for (j = 0; j < nsops; j++)
1132 semaptr->sem_base[sops[j].sem_num].semval -=
1133 sops[j].sem_op;
1134
1135 DPRINTF(("error = %d from semundo_adjust\n", error));
1136 SEMUNDO_UNLOCK();
1137 goto done2;
1138 } /* loop through the sops */
1139 SEMUNDO_UNLOCK();
1140 } /* if (do_undos) */
1141
1142 /* We're definitely done - set the sempid's and time */
1143 for (i = 0; i < nsops; i++) {
1144 sopptr = &sops[i];
1145 semptr = &semaptr->sem_base[sopptr->sem_num];
1146 semptr->sempid = td->td_proc->p_pid;
1147 }
1148 semaptr->sem_otime = time_second;
1149
1150 /*
1151 * Do a wakeup if any semaphore was up'd whilst something was
1152 * sleeping on it.
1153 */
1154 if (do_wakeup) {
1155 DPRINTF(("semop: doing wakeup\n"));
1156 wakeup(semaptr);
1157 DPRINTF(("semop: back from wakeup\n"));
1158 }
1159 DPRINTF(("semop: done\n"));
1160 td->td_retval[0] = 0;
1161 done2:
1162 mtx_unlock(sema_mtxp);
1163 if (sops != small_sops)
1164 free(sops, M_SEM);
1165 return (error);
1166 }
1167
1168 /*
1169 * Go through the undo structures for this process and apply the adjustments to
1170 * semaphores.
1171 */
1172 static void
1173 semexit_myhook(arg, p)
1174 void *arg;
1175 struct proc *p;
1176 {
1177 struct sem_undo *suptr;
1178 struct sem_undo **supptr;
1179
1180 /*
1181 * Go through the chain of undo vectors looking for one
1182 * associated with this process.
1183 */
1184 SEMUNDO_LOCK();
1185 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
1186 if (suptr->un_proc == p)
1187 break;
1188 }
1189 SEMUNDO_UNLOCK();
1190
1191 if (suptr == NULL)
1192 return;
1193
1194 DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
1195 suptr->un_cnt));
1196
1197 /*
1198 * If there are any active undo elements then process them.
1199 */
1200 if (suptr->un_cnt > 0) {
1201 int ix;
1202
1203 for (ix = 0; ix < suptr->un_cnt; ix++) {
1204 int semid = suptr->un_ent[ix].un_id;
1205 int semnum = suptr->un_ent[ix].un_num;
1206 int adjval = suptr->un_ent[ix].un_adjval;
1207 struct semid_ds *semaptr;
1208 struct mtx *sema_mtxp;
1209
1210 semaptr = &sema[semid];
1211 sema_mtxp = &sema_mtx[semid];
1212 mtx_lock(sema_mtxp);
1213 SEMUNDO_LOCK();
1214 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1215 panic("semexit - semid not allocated");
1216 if (semnum >= semaptr->sem_nsems)
1217 panic("semexit - semnum out of range");
1218
1219 DPRINTF((
1220 "semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1221 suptr->un_proc, suptr->un_ent[ix].un_id,
1222 suptr->un_ent[ix].un_num,
1223 suptr->un_ent[ix].un_adjval,
1224 semaptr->sem_base[semnum].semval));
1225
1226 if (adjval < 0) {
1227 if (semaptr->sem_base[semnum].semval < -adjval)
1228 semaptr->sem_base[semnum].semval = 0;
1229 else
1230 semaptr->sem_base[semnum].semval +=
1231 adjval;
1232 } else
1233 semaptr->sem_base[semnum].semval += adjval;
1234
1235 wakeup(semaptr);
1236 DPRINTF(("semexit: back from wakeup\n"));
1237 mtx_unlock(sema_mtxp);
1238 SEMUNDO_UNLOCK();
1239 }
1240 }
1241
1242 /*
1243 * Deallocate the undo vector.
1244 */
1245 DPRINTF(("removing vector\n"));
1246 suptr->un_proc = NULL;
1247 *supptr = SLIST_NEXT(suptr, un_next);
1248 }
1249
1250 static int
1251 sysctl_sema(SYSCTL_HANDLER_ARGS)
1252 {
1253
1254 return (SYSCTL_OUT(req, sema,
1255 sizeof(struct semid_ds) * seminfo.semmni));
1256 }
Cache object: efa5513f4e5ce483e0889488176b1795
|