Deleted Added
full compact
sysv_sem.c (256281) sysv_sem.c (284665)
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 * Copyright (c) 2003-2005 McAfee, Inc.
10 * All rights reserved.
11 *
12 * This software was developed for the FreeBSD Project in part by McAfee
13 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
14 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
15 * program.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
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 * Copyright (c) 2003-2005 McAfee, Inc.
10 * All rights reserved.
11 *
12 * This software was developed for the FreeBSD Project in part by McAfee
13 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
14 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
15 * program.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/10/sys/kern/sysv_sem.c 225617 2011-09-16 13:58:51Z kmacy $");
40__FBSDID("$FreeBSD: stable/10/sys/kern/sysv_sem.c 284665 2015-06-21 06:28:26Z trasz $");
41
42#include "opt_compat.h"
43#include "opt_sysvipc.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/eventhandler.h>
49#include <sys/kernel.h>
50#include <sys/proc.h>
51#include <sys/lock.h>
52#include <sys/module.h>
53#include <sys/mutex.h>
54#include <sys/racct.h>
55#include <sys/sem.h>
56#include <sys/syscall.h>
57#include <sys/syscallsubr.h>
58#include <sys/sysent.h>
59#include <sys/sysctl.h>
60#include <sys/uio.h>
61#include <sys/malloc.h>
62#include <sys/jail.h>
63
64#include <security/mac/mac_framework.h>
65
66FEATURE(sysv_sem, "System V semaphores support");
67
68static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
69
70#ifdef SEM_DEBUG
71#define DPRINTF(a) printf a
72#else
73#define DPRINTF(a)
74#endif
75
76static int seminit(void);
77static int sysvsem_modload(struct module *, int, void *);
78static int semunload(void);
79static void semexit_myhook(void *arg, struct proc *p);
80static int sysctl_sema(SYSCTL_HANDLER_ARGS);
81static int semvalid(int semid, struct semid_kernel *semakptr);
82
83#ifndef _SYS_SYSPROTO_H_
84struct __semctl_args;
85int __semctl(struct thread *td, struct __semctl_args *uap);
86struct semget_args;
87int semget(struct thread *td, struct semget_args *uap);
88struct semop_args;
89int semop(struct thread *td, struct semop_args *uap);
90#endif
91
92static struct sem_undo *semu_alloc(struct thread *td);
93static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
94 int semid, int semseq, int semnum, int adjval);
95static void semundo_clear(int semid, int semnum);
96
97static struct mtx sem_mtx; /* semaphore global lock */
98static struct mtx sem_undo_mtx;
99static int semtot = 0;
100static struct semid_kernel *sema; /* semaphore id pool */
101static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
102static struct sem *sem; /* semaphore pool */
103LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
104LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */
105static int *semu; /* undo structure pool */
106static eventhandler_tag semexit_tag;
107
108#define SEMUNDO_MTX sem_undo_mtx
109#define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
110#define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
111#define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
112
113struct sem {
114 u_short semval; /* semaphore value */
115 pid_t sempid; /* pid of last operation */
116 u_short semncnt; /* # awaiting semval > cval */
117 u_short semzcnt; /* # awaiting semval = 0 */
118};
119
120/*
121 * Undo structure (one per process)
122 */
123struct sem_undo {
124 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
125 struct proc *un_proc; /* owner of this structure */
126 short un_cnt; /* # of active entries */
127 struct undo {
128 short un_adjval; /* adjust on exit values */
129 short un_num; /* semaphore # */
130 int un_id; /* semid */
131 unsigned short un_seq;
132 } un_ent[1]; /* undo entries */
133};
134
135/*
136 * Configuration parameters
137 */
138#ifndef SEMMNI
139#define SEMMNI 50 /* # of semaphore identifiers */
140#endif
141#ifndef SEMMNS
142#define SEMMNS 340 /* # of semaphores in system */
143#endif
144#ifndef SEMUME
145#define SEMUME 50 /* max # of undo entries per process */
146#endif
147#ifndef SEMMNU
148#define SEMMNU 150 /* # of undo structures in system */
149#endif
150
151/* shouldn't need tuning */
152#ifndef SEMMSL
153#define SEMMSL SEMMNS /* max # of semaphores per id */
154#endif
155#ifndef SEMOPM
156#define SEMOPM 100 /* max # of operations per semop call */
157#endif
158
159#define SEMVMX 32767 /* semaphore maximum value */
160#define SEMAEM 16384 /* adjust on exit max value */
161
162/*
163 * Due to the way semaphore memory is allocated, we have to ensure that
164 * SEMUSZ is properly aligned.
165 */
166
167#define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
168
169/* actual size of an undo structure */
170#define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
171
172/*
173 * Macro to find a particular sem_undo vector
174 */
175#define SEMU(ix) \
176 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
177
178/*
179 * semaphore info struct
180 */
181struct seminfo seminfo = {
182 SEMMNI, /* # of semaphore identifiers */
183 SEMMNS, /* # of semaphores in system */
184 SEMMNU, /* # of undo structures in system */
185 SEMMSL, /* max # of semaphores per id */
186 SEMOPM, /* max # of operations per semop call */
187 SEMUME, /* max # of undo entries per process */
188 SEMUSZ, /* size in bytes of undo structure */
189 SEMVMX, /* semaphore maximum value */
190 SEMAEM /* adjust on exit max value */
191};
192
193SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
194 "Number of semaphore identifiers");
195SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
196 "Maximum number of semaphores in the system");
197SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
198 "Maximum number of undo structures in the system");
199SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
200 "Max semaphores per id");
201SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
202 "Max operations per semop call");
203SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
204 "Max undo entries per process");
205SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
206 "Size in bytes of undo structure");
207SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
208 "Semaphore maximum value");
209SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
210 "Adjust on exit max value");
211SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLTYPE_OPAQUE | CTLFLAG_RD,
212 NULL, 0, sysctl_sema, "", "Semaphore id pool");
213
214static struct syscall_helper_data sem_syscalls[] = {
215 SYSCALL_INIT_HELPER(__semctl),
216 SYSCALL_INIT_HELPER(semget),
217 SYSCALL_INIT_HELPER(semop),
218#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
219 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
220 SYSCALL_INIT_HELPER(semsys),
221 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
222#endif
223 SYSCALL_INIT_LAST
224};
225
226#ifdef COMPAT_FREEBSD32
227#include <compat/freebsd32/freebsd32.h>
228#include <compat/freebsd32/freebsd32_ipc.h>
229#include <compat/freebsd32/freebsd32_proto.h>
230#include <compat/freebsd32/freebsd32_signal.h>
231#include <compat/freebsd32/freebsd32_syscall.h>
232#include <compat/freebsd32/freebsd32_util.h>
233
234static struct syscall_helper_data sem32_syscalls[] = {
235 SYSCALL32_INIT_HELPER(freebsd32_semctl),
236 SYSCALL32_INIT_HELPER_COMPAT(semget),
237 SYSCALL32_INIT_HELPER_COMPAT(semop),
238 SYSCALL32_INIT_HELPER(freebsd32_semsys),
239#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
240 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
241 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
242#endif
243 SYSCALL_INIT_LAST
244};
245#endif
246
247static int
248seminit(void)
249{
250 int i, error;
251
252 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
253 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
254 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
255 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
256 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
257 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
258 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
259 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
260 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
261
262 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
263 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
264 M_WAITOK);
265 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
266 M_WAITOK | M_ZERO);
267 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
268
269 for (i = 0; i < seminfo.semmni; i++) {
270 sema[i].u.sem_base = 0;
271 sema[i].u.sem_perm.mode = 0;
272 sema[i].u.sem_perm.seq = 0;
273#ifdef MAC
274 mac_sysvsem_init(&sema[i]);
275#endif
276 }
277 for (i = 0; i < seminfo.semmni; i++)
278 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
279 LIST_INIT(&semu_free_list);
280 for (i = 0; i < seminfo.semmnu; i++) {
281 struct sem_undo *suptr = SEMU(i);
282 suptr->un_proc = NULL;
283 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
284 }
285 LIST_INIT(&semu_list);
286 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
287 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
288 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
289 EVENTHANDLER_PRI_ANY);
290
291 error = syscall_helper_register(sem_syscalls);
292 if (error != 0)
293 return (error);
294#ifdef COMPAT_FREEBSD32
295 error = syscall32_helper_register(sem32_syscalls);
296 if (error != 0)
297 return (error);
298#endif
299 return (0);
300}
301
302static int
303semunload(void)
304{
305 int i;
306
307 /* XXXKIB */
308 if (semtot != 0)
309 return (EBUSY);
310
311#ifdef COMPAT_FREEBSD32
312 syscall32_helper_unregister(sem32_syscalls);
313#endif
314 syscall_helper_unregister(sem_syscalls);
315 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
316#ifdef MAC
317 for (i = 0; i < seminfo.semmni; i++)
318 mac_sysvsem_destroy(&sema[i]);
319#endif
320 free(sem, M_SEM);
321 free(sema, M_SEM);
322 free(semu, M_SEM);
323 for (i = 0; i < seminfo.semmni; i++)
324 mtx_destroy(&sema_mtx[i]);
325 free(sema_mtx, M_SEM);
326 mtx_destroy(&sem_mtx);
327 mtx_destroy(&sem_undo_mtx);
328 return (0);
329}
330
331static int
332sysvsem_modload(struct module *module, int cmd, void *arg)
333{
334 int error = 0;
335
336 switch (cmd) {
337 case MOD_LOAD:
338 error = seminit();
339 if (error != 0)
340 semunload();
341 break;
342 case MOD_UNLOAD:
343 error = semunload();
344 break;
345 case MOD_SHUTDOWN:
346 break;
347 default:
348 error = EINVAL;
349 break;
350 }
351 return (error);
352}
353
354static moduledata_t sysvsem_mod = {
355 "sysvsem",
356 &sysvsem_modload,
357 NULL
358};
359
360DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
361MODULE_VERSION(sysvsem, 1);
362
363/*
364 * Allocate a new sem_undo structure for a process
365 * (returns ptr to structure or NULL if no more room)
366 */
367
368static struct sem_undo *
369semu_alloc(struct thread *td)
370{
371 struct sem_undo *suptr;
372
373 SEMUNDO_LOCKASSERT(MA_OWNED);
374 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
375 return (NULL);
376 LIST_REMOVE(suptr, un_next);
377 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
378 suptr->un_cnt = 0;
379 suptr->un_proc = td->td_proc;
380 return (suptr);
381}
382
383static int
384semu_try_free(struct sem_undo *suptr)
385{
386
387 SEMUNDO_LOCKASSERT(MA_OWNED);
388
389 if (suptr->un_cnt != 0)
390 return (0);
391 LIST_REMOVE(suptr, un_next);
392 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
393 return (1);
394}
395
396/*
397 * Adjust a particular entry for a particular proc
398 */
399
400static int
401semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
402 int semseq, int semnum, int adjval)
403{
404 struct proc *p = td->td_proc;
405 struct sem_undo *suptr;
406 struct undo *sunptr;
407 int i;
408
409 SEMUNDO_LOCKASSERT(MA_OWNED);
410 /* Look for and remember the sem_undo if the caller doesn't provide
411 it */
412
413 suptr = *supptr;
414 if (suptr == NULL) {
415 LIST_FOREACH(suptr, &semu_list, un_next) {
416 if (suptr->un_proc == p) {
417 *supptr = suptr;
418 break;
419 }
420 }
421 if (suptr == NULL) {
422 if (adjval == 0)
423 return(0);
424 suptr = semu_alloc(td);
425 if (suptr == NULL)
426 return (ENOSPC);
427 *supptr = suptr;
428 }
429 }
430
431 /*
432 * Look for the requested entry and adjust it (delete if adjval becomes
433 * 0).
434 */
435 sunptr = &suptr->un_ent[0];
436 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
437 if (sunptr->un_id != semid || sunptr->un_num != semnum)
438 continue;
439 if (adjval != 0) {
440 adjval += sunptr->un_adjval;
441 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
442 return (ERANGE);
443 }
444 sunptr->un_adjval = adjval;
445 if (sunptr->un_adjval == 0) {
446 suptr->un_cnt--;
447 if (i < suptr->un_cnt)
448 suptr->un_ent[i] =
449 suptr->un_ent[suptr->un_cnt];
450 if (suptr->un_cnt == 0)
451 semu_try_free(suptr);
452 }
453 return (0);
454 }
455
456 /* Didn't find the right entry - create it */
457 if (adjval == 0)
458 return (0);
459 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
460 return (ERANGE);
461 if (suptr->un_cnt != seminfo.semume) {
462 sunptr = &suptr->un_ent[suptr->un_cnt];
463 suptr->un_cnt++;
464 sunptr->un_adjval = adjval;
465 sunptr->un_id = semid;
466 sunptr->un_num = semnum;
467 sunptr->un_seq = semseq;
468 } else
469 return (EINVAL);
470 return (0);
471}
472
473static void
474semundo_clear(int semid, int semnum)
475{
476 struct sem_undo *suptr, *suptr1;
477 struct undo *sunptr;
478 int i;
479
480 SEMUNDO_LOCKASSERT(MA_OWNED);
481 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
482 sunptr = &suptr->un_ent[0];
483 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
484 if (sunptr->un_id != semid)
485 continue;
486 if (semnum == -1 || sunptr->un_num == semnum) {
487 suptr->un_cnt--;
488 if (i < suptr->un_cnt) {
489 suptr->un_ent[i] =
490 suptr->un_ent[suptr->un_cnt];
491 continue;
492 }
493 semu_try_free(suptr);
494 }
495 if (semnum != -1)
496 break;
497 }
498 }
499}
500
501static int
502semvalid(int semid, struct semid_kernel *semakptr)
503{
504
505 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
506 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
507}
508
509/*
510 * Note that the user-mode half of this passes a union, not a pointer.
511 */
512#ifndef _SYS_SYSPROTO_H_
513struct __semctl_args {
514 int semid;
515 int semnum;
516 int cmd;
517 union semun *arg;
518};
519#endif
520int
521sys___semctl(struct thread *td, struct __semctl_args *uap)
522{
523 struct semid_ds dsbuf;
524 union semun arg, semun;
525 register_t rval;
526 int error;
527
528 switch (uap->cmd) {
529 case SEM_STAT:
530 case IPC_SET:
531 case IPC_STAT:
532 case GETALL:
533 case SETVAL:
534 case SETALL:
535 error = copyin(uap->arg, &arg, sizeof(arg));
536 if (error)
537 return (error);
538 break;
539 }
540
541 switch (uap->cmd) {
542 case SEM_STAT:
543 case IPC_STAT:
544 semun.buf = &dsbuf;
545 break;
546 case IPC_SET:
547 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
548 if (error)
549 return (error);
550 semun.buf = &dsbuf;
551 break;
552 case GETALL:
553 case SETALL:
554 semun.array = arg.array;
555 break;
556 case SETVAL:
557 semun.val = arg.val;
558 break;
559 }
560
561 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
562 &rval);
563 if (error)
564 return (error);
565
566 switch (uap->cmd) {
567 case SEM_STAT:
568 case IPC_STAT:
569 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
570 break;
571 }
572
573 if (error == 0)
574 td->td_retval[0] = rval;
575 return (error);
576}
577
578int
579kern_semctl(struct thread *td, int semid, int semnum, int cmd,
580 union semun *arg, register_t *rval)
581{
582 u_short *array;
583 struct ucred *cred = td->td_ucred;
584 int i, error;
585 struct semid_ds *sbuf;
586 struct semid_kernel *semakptr;
587 struct mtx *sema_mtxp;
588 u_short usval, count;
589 int semidx;
590
591 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
592 semid, semnum, cmd, arg));
593 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
594 return (ENOSYS);
595
596 array = NULL;
597
598 switch(cmd) {
599 case SEM_STAT:
600 /*
601 * For this command we assume semid is an array index
602 * rather than an IPC id.
603 */
604 if (semid < 0 || semid >= seminfo.semmni)
605 return (EINVAL);
606 semakptr = &sema[semid];
607 sema_mtxp = &sema_mtx[semid];
608 mtx_lock(sema_mtxp);
609 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
610 error = EINVAL;
611 goto done2;
612 }
613 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
614 goto done2;
615#ifdef MAC
616 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
617 if (error != 0)
618 goto done2;
619#endif
620 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
621 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
622 mtx_unlock(sema_mtxp);
623 return (0);
624 }
625
626 semidx = IPCID_TO_IX(semid);
627 if (semidx < 0 || semidx >= seminfo.semmni)
628 return (EINVAL);
629
630 semakptr = &sema[semidx];
631 sema_mtxp = &sema_mtx[semidx];
632 if (cmd == IPC_RMID)
633 mtx_lock(&sem_mtx);
634 mtx_lock(sema_mtxp);
635#ifdef MAC
636 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
637 if (error != 0)
638 goto done2;
639#endif
640
641 error = 0;
642 *rval = 0;
643
644 switch (cmd) {
645 case IPC_RMID:
646 if ((error = semvalid(semid, semakptr)) != 0)
647 goto done2;
648 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
649 goto done2;
650 semakptr->u.sem_perm.cuid = cred->cr_uid;
651 semakptr->u.sem_perm.uid = cred->cr_uid;
652 semakptr->u.sem_perm.mode = 0;
653 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
654 crfree(semakptr->cred);
655 semakptr->cred = NULL;
656 SEMUNDO_LOCK();
657 semundo_clear(semidx, -1);
658 SEMUNDO_UNLOCK();
659#ifdef MAC
660 mac_sysvsem_cleanup(semakptr);
661#endif
662 wakeup(semakptr);
663 for (i = 0; i < seminfo.semmni; i++) {
664 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
665 sema[i].u.sem_base > semakptr->u.sem_base)
666 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
667 }
668 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
669 sem[i] = sem[i + semakptr->u.sem_nsems];
670 for (i = 0; i < seminfo.semmni; i++) {
671 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
672 sema[i].u.sem_base > semakptr->u.sem_base) {
673 sema[i].u.sem_base -= semakptr->u.sem_nsems;
674 mtx_unlock(&sema_mtx[i]);
675 }
676 }
677 semtot -= semakptr->u.sem_nsems;
678 break;
679
680 case IPC_SET:
681 if ((error = semvalid(semid, semakptr)) != 0)
682 goto done2;
683 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
684 goto done2;
685 sbuf = arg->buf;
686 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
687 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
688 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
689 ~0777) | (sbuf->sem_perm.mode & 0777);
690 semakptr->u.sem_ctime = time_second;
691 break;
692
693 case IPC_STAT:
694 if ((error = semvalid(semid, semakptr)) != 0)
695 goto done2;
696 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
697 goto done2;
698 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
699 break;
700
701 case GETNCNT:
702 if ((error = semvalid(semid, semakptr)) != 0)
703 goto done2;
704 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
705 goto done2;
706 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
707 error = EINVAL;
708 goto done2;
709 }
710 *rval = semakptr->u.sem_base[semnum].semncnt;
711 break;
712
713 case GETPID:
714 if ((error = semvalid(semid, semakptr)) != 0)
715 goto done2;
716 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
717 goto done2;
718 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
719 error = EINVAL;
720 goto done2;
721 }
722 *rval = semakptr->u.sem_base[semnum].sempid;
723 break;
724
725 case GETVAL:
726 if ((error = semvalid(semid, semakptr)) != 0)
727 goto done2;
728 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
729 goto done2;
730 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
731 error = EINVAL;
732 goto done2;
733 }
734 *rval = semakptr->u.sem_base[semnum].semval;
735 break;
736
737 case GETALL:
738 /*
739 * Unfortunately, callers of this function don't know
740 * in advance how many semaphores are in this set.
741 * While we could just allocate the maximum size array
742 * and pass the actual size back to the caller, that
743 * won't work for SETALL since we can't copyin() more
744 * data than the user specified as we may return a
745 * spurious EFAULT.
746 *
747 * Note that the number of semaphores in a set is
748 * fixed for the life of that set. The only way that
749 * the 'count' could change while are blocked in
750 * malloc() is if this semaphore set were destroyed
751 * and a new one created with the same index.
752 * However, semvalid() will catch that due to the
753 * sequence number unless exactly 0x8000 (or a
754 * multiple thereof) semaphore sets for the same index
755 * are created and destroyed while we are in malloc!
756 *
757 */
758 count = semakptr->u.sem_nsems;
759 mtx_unlock(sema_mtxp);
760 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
761 mtx_lock(sema_mtxp);
762 if ((error = semvalid(semid, semakptr)) != 0)
763 goto done2;
764 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
765 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
766 goto done2;
767 for (i = 0; i < semakptr->u.sem_nsems; i++)
768 array[i] = semakptr->u.sem_base[i].semval;
769 mtx_unlock(sema_mtxp);
770 error = copyout(array, arg->array, count * sizeof(*array));
771 mtx_lock(sema_mtxp);
772 break;
773
774 case GETZCNT:
775 if ((error = semvalid(semid, semakptr)) != 0)
776 goto done2;
777 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
778 goto done2;
779 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
780 error = EINVAL;
781 goto done2;
782 }
783 *rval = semakptr->u.sem_base[semnum].semzcnt;
784 break;
785
786 case SETVAL:
787 if ((error = semvalid(semid, semakptr)) != 0)
788 goto done2;
789 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
790 goto done2;
791 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
792 error = EINVAL;
793 goto done2;
794 }
795 if (arg->val < 0 || arg->val > seminfo.semvmx) {
796 error = ERANGE;
797 goto done2;
798 }
799 semakptr->u.sem_base[semnum].semval = arg->val;
800 SEMUNDO_LOCK();
801 semundo_clear(semidx, semnum);
802 SEMUNDO_UNLOCK();
803 wakeup(semakptr);
804 break;
805
806 case SETALL:
807 /*
808 * See comment on GETALL for why 'count' shouldn't change
809 * and why we require a userland buffer.
810 */
811 count = semakptr->u.sem_nsems;
812 mtx_unlock(sema_mtxp);
813 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
814 error = copyin(arg->array, array, count * sizeof(*array));
815 mtx_lock(sema_mtxp);
816 if (error)
817 break;
818 if ((error = semvalid(semid, semakptr)) != 0)
819 goto done2;
820 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
821 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
822 goto done2;
823 for (i = 0; i < semakptr->u.sem_nsems; i++) {
824 usval = array[i];
825 if (usval > seminfo.semvmx) {
826 error = ERANGE;
827 break;
828 }
829 semakptr->u.sem_base[i].semval = usval;
830 }
831 SEMUNDO_LOCK();
832 semundo_clear(semidx, -1);
833 SEMUNDO_UNLOCK();
834 wakeup(semakptr);
835 break;
836
837 default:
838 error = EINVAL;
839 break;
840 }
841
842done2:
843 mtx_unlock(sema_mtxp);
844 if (cmd == IPC_RMID)
845 mtx_unlock(&sem_mtx);
846 if (array != NULL)
847 free(array, M_TEMP);
848 return(error);
849}
850
851#ifndef _SYS_SYSPROTO_H_
852struct semget_args {
853 key_t key;
854 int nsems;
855 int semflg;
856};
857#endif
858int
859sys_semget(struct thread *td, struct semget_args *uap)
860{
861 int semid, error = 0;
862 int key = uap->key;
863 int nsems = uap->nsems;
864 int semflg = uap->semflg;
865 struct ucred *cred = td->td_ucred;
866
867 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
868 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
869 return (ENOSYS);
870
871 mtx_lock(&sem_mtx);
872 if (key != IPC_PRIVATE) {
873 for (semid = 0; semid < seminfo.semmni; semid++) {
874 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
875 sema[semid].u.sem_perm.key == key)
876 break;
877 }
878 if (semid < seminfo.semmni) {
879 DPRINTF(("found public key\n"));
880 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
881 semflg & 0700))) {
882 goto done2;
883 }
884 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
885 DPRINTF(("too small\n"));
886 error = EINVAL;
887 goto done2;
888 }
889 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
890 DPRINTF(("not exclusive\n"));
891 error = EEXIST;
892 goto done2;
893 }
894#ifdef MAC
895 error = mac_sysvsem_check_semget(cred, &sema[semid]);
896 if (error != 0)
897 goto done2;
898#endif
899 goto found;
900 }
901 }
902
903 DPRINTF(("need to allocate the semid_kernel\n"));
904 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
905 if (nsems <= 0 || nsems > seminfo.semmsl) {
906 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
907 seminfo.semmsl));
908 error = EINVAL;
909 goto done2;
910 }
911 if (nsems > seminfo.semmns - semtot) {
912 DPRINTF((
913 "not enough semaphores left (need %d, got %d)\n",
914 nsems, seminfo.semmns - semtot));
915 error = ENOSPC;
916 goto done2;
917 }
918 for (semid = 0; semid < seminfo.semmni; semid++) {
919 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
920 break;
921 }
922 if (semid == seminfo.semmni) {
923 DPRINTF(("no more semid_kernel's available\n"));
924 error = ENOSPC;
925 goto done2;
926 }
927#ifdef RACCT
41
42#include "opt_compat.h"
43#include "opt_sysvipc.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/eventhandler.h>
49#include <sys/kernel.h>
50#include <sys/proc.h>
51#include <sys/lock.h>
52#include <sys/module.h>
53#include <sys/mutex.h>
54#include <sys/racct.h>
55#include <sys/sem.h>
56#include <sys/syscall.h>
57#include <sys/syscallsubr.h>
58#include <sys/sysent.h>
59#include <sys/sysctl.h>
60#include <sys/uio.h>
61#include <sys/malloc.h>
62#include <sys/jail.h>
63
64#include <security/mac/mac_framework.h>
65
66FEATURE(sysv_sem, "System V semaphores support");
67
68static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
69
70#ifdef SEM_DEBUG
71#define DPRINTF(a) printf a
72#else
73#define DPRINTF(a)
74#endif
75
76static int seminit(void);
77static int sysvsem_modload(struct module *, int, void *);
78static int semunload(void);
79static void semexit_myhook(void *arg, struct proc *p);
80static int sysctl_sema(SYSCTL_HANDLER_ARGS);
81static int semvalid(int semid, struct semid_kernel *semakptr);
82
83#ifndef _SYS_SYSPROTO_H_
84struct __semctl_args;
85int __semctl(struct thread *td, struct __semctl_args *uap);
86struct semget_args;
87int semget(struct thread *td, struct semget_args *uap);
88struct semop_args;
89int semop(struct thread *td, struct semop_args *uap);
90#endif
91
92static struct sem_undo *semu_alloc(struct thread *td);
93static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
94 int semid, int semseq, int semnum, int adjval);
95static void semundo_clear(int semid, int semnum);
96
97static struct mtx sem_mtx; /* semaphore global lock */
98static struct mtx sem_undo_mtx;
99static int semtot = 0;
100static struct semid_kernel *sema; /* semaphore id pool */
101static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
102static struct sem *sem; /* semaphore pool */
103LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
104LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */
105static int *semu; /* undo structure pool */
106static eventhandler_tag semexit_tag;
107
108#define SEMUNDO_MTX sem_undo_mtx
109#define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
110#define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
111#define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
112
113struct sem {
114 u_short semval; /* semaphore value */
115 pid_t sempid; /* pid of last operation */
116 u_short semncnt; /* # awaiting semval > cval */
117 u_short semzcnt; /* # awaiting semval = 0 */
118};
119
120/*
121 * Undo structure (one per process)
122 */
123struct sem_undo {
124 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
125 struct proc *un_proc; /* owner of this structure */
126 short un_cnt; /* # of active entries */
127 struct undo {
128 short un_adjval; /* adjust on exit values */
129 short un_num; /* semaphore # */
130 int un_id; /* semid */
131 unsigned short un_seq;
132 } un_ent[1]; /* undo entries */
133};
134
135/*
136 * Configuration parameters
137 */
138#ifndef SEMMNI
139#define SEMMNI 50 /* # of semaphore identifiers */
140#endif
141#ifndef SEMMNS
142#define SEMMNS 340 /* # of semaphores in system */
143#endif
144#ifndef SEMUME
145#define SEMUME 50 /* max # of undo entries per process */
146#endif
147#ifndef SEMMNU
148#define SEMMNU 150 /* # of undo structures in system */
149#endif
150
151/* shouldn't need tuning */
152#ifndef SEMMSL
153#define SEMMSL SEMMNS /* max # of semaphores per id */
154#endif
155#ifndef SEMOPM
156#define SEMOPM 100 /* max # of operations per semop call */
157#endif
158
159#define SEMVMX 32767 /* semaphore maximum value */
160#define SEMAEM 16384 /* adjust on exit max value */
161
162/*
163 * Due to the way semaphore memory is allocated, we have to ensure that
164 * SEMUSZ is properly aligned.
165 */
166
167#define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
168
169/* actual size of an undo structure */
170#define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
171
172/*
173 * Macro to find a particular sem_undo vector
174 */
175#define SEMU(ix) \
176 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
177
178/*
179 * semaphore info struct
180 */
181struct seminfo seminfo = {
182 SEMMNI, /* # of semaphore identifiers */
183 SEMMNS, /* # of semaphores in system */
184 SEMMNU, /* # of undo structures in system */
185 SEMMSL, /* max # of semaphores per id */
186 SEMOPM, /* max # of operations per semop call */
187 SEMUME, /* max # of undo entries per process */
188 SEMUSZ, /* size in bytes of undo structure */
189 SEMVMX, /* semaphore maximum value */
190 SEMAEM /* adjust on exit max value */
191};
192
193SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
194 "Number of semaphore identifiers");
195SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
196 "Maximum number of semaphores in the system");
197SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
198 "Maximum number of undo structures in the system");
199SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
200 "Max semaphores per id");
201SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
202 "Max operations per semop call");
203SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
204 "Max undo entries per process");
205SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
206 "Size in bytes of undo structure");
207SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
208 "Semaphore maximum value");
209SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
210 "Adjust on exit max value");
211SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLTYPE_OPAQUE | CTLFLAG_RD,
212 NULL, 0, sysctl_sema, "", "Semaphore id pool");
213
214static struct syscall_helper_data sem_syscalls[] = {
215 SYSCALL_INIT_HELPER(__semctl),
216 SYSCALL_INIT_HELPER(semget),
217 SYSCALL_INIT_HELPER(semop),
218#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
219 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
220 SYSCALL_INIT_HELPER(semsys),
221 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
222#endif
223 SYSCALL_INIT_LAST
224};
225
226#ifdef COMPAT_FREEBSD32
227#include <compat/freebsd32/freebsd32.h>
228#include <compat/freebsd32/freebsd32_ipc.h>
229#include <compat/freebsd32/freebsd32_proto.h>
230#include <compat/freebsd32/freebsd32_signal.h>
231#include <compat/freebsd32/freebsd32_syscall.h>
232#include <compat/freebsd32/freebsd32_util.h>
233
234static struct syscall_helper_data sem32_syscalls[] = {
235 SYSCALL32_INIT_HELPER(freebsd32_semctl),
236 SYSCALL32_INIT_HELPER_COMPAT(semget),
237 SYSCALL32_INIT_HELPER_COMPAT(semop),
238 SYSCALL32_INIT_HELPER(freebsd32_semsys),
239#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
240 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
241 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
242#endif
243 SYSCALL_INIT_LAST
244};
245#endif
246
247static int
248seminit(void)
249{
250 int i, error;
251
252 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
253 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
254 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
255 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
256 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
257 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
258 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
259 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
260 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
261
262 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
263 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
264 M_WAITOK);
265 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
266 M_WAITOK | M_ZERO);
267 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
268
269 for (i = 0; i < seminfo.semmni; i++) {
270 sema[i].u.sem_base = 0;
271 sema[i].u.sem_perm.mode = 0;
272 sema[i].u.sem_perm.seq = 0;
273#ifdef MAC
274 mac_sysvsem_init(&sema[i]);
275#endif
276 }
277 for (i = 0; i < seminfo.semmni; i++)
278 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
279 LIST_INIT(&semu_free_list);
280 for (i = 0; i < seminfo.semmnu; i++) {
281 struct sem_undo *suptr = SEMU(i);
282 suptr->un_proc = NULL;
283 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
284 }
285 LIST_INIT(&semu_list);
286 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
287 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
288 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
289 EVENTHANDLER_PRI_ANY);
290
291 error = syscall_helper_register(sem_syscalls);
292 if (error != 0)
293 return (error);
294#ifdef COMPAT_FREEBSD32
295 error = syscall32_helper_register(sem32_syscalls);
296 if (error != 0)
297 return (error);
298#endif
299 return (0);
300}
301
302static int
303semunload(void)
304{
305 int i;
306
307 /* XXXKIB */
308 if (semtot != 0)
309 return (EBUSY);
310
311#ifdef COMPAT_FREEBSD32
312 syscall32_helper_unregister(sem32_syscalls);
313#endif
314 syscall_helper_unregister(sem_syscalls);
315 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
316#ifdef MAC
317 for (i = 0; i < seminfo.semmni; i++)
318 mac_sysvsem_destroy(&sema[i]);
319#endif
320 free(sem, M_SEM);
321 free(sema, M_SEM);
322 free(semu, M_SEM);
323 for (i = 0; i < seminfo.semmni; i++)
324 mtx_destroy(&sema_mtx[i]);
325 free(sema_mtx, M_SEM);
326 mtx_destroy(&sem_mtx);
327 mtx_destroy(&sem_undo_mtx);
328 return (0);
329}
330
331static int
332sysvsem_modload(struct module *module, int cmd, void *arg)
333{
334 int error = 0;
335
336 switch (cmd) {
337 case MOD_LOAD:
338 error = seminit();
339 if (error != 0)
340 semunload();
341 break;
342 case MOD_UNLOAD:
343 error = semunload();
344 break;
345 case MOD_SHUTDOWN:
346 break;
347 default:
348 error = EINVAL;
349 break;
350 }
351 return (error);
352}
353
354static moduledata_t sysvsem_mod = {
355 "sysvsem",
356 &sysvsem_modload,
357 NULL
358};
359
360DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
361MODULE_VERSION(sysvsem, 1);
362
363/*
364 * Allocate a new sem_undo structure for a process
365 * (returns ptr to structure or NULL if no more room)
366 */
367
368static struct sem_undo *
369semu_alloc(struct thread *td)
370{
371 struct sem_undo *suptr;
372
373 SEMUNDO_LOCKASSERT(MA_OWNED);
374 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
375 return (NULL);
376 LIST_REMOVE(suptr, un_next);
377 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
378 suptr->un_cnt = 0;
379 suptr->un_proc = td->td_proc;
380 return (suptr);
381}
382
383static int
384semu_try_free(struct sem_undo *suptr)
385{
386
387 SEMUNDO_LOCKASSERT(MA_OWNED);
388
389 if (suptr->un_cnt != 0)
390 return (0);
391 LIST_REMOVE(suptr, un_next);
392 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
393 return (1);
394}
395
396/*
397 * Adjust a particular entry for a particular proc
398 */
399
400static int
401semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
402 int semseq, int semnum, int adjval)
403{
404 struct proc *p = td->td_proc;
405 struct sem_undo *suptr;
406 struct undo *sunptr;
407 int i;
408
409 SEMUNDO_LOCKASSERT(MA_OWNED);
410 /* Look for and remember the sem_undo if the caller doesn't provide
411 it */
412
413 suptr = *supptr;
414 if (suptr == NULL) {
415 LIST_FOREACH(suptr, &semu_list, un_next) {
416 if (suptr->un_proc == p) {
417 *supptr = suptr;
418 break;
419 }
420 }
421 if (suptr == NULL) {
422 if (adjval == 0)
423 return(0);
424 suptr = semu_alloc(td);
425 if (suptr == NULL)
426 return (ENOSPC);
427 *supptr = suptr;
428 }
429 }
430
431 /*
432 * Look for the requested entry and adjust it (delete if adjval becomes
433 * 0).
434 */
435 sunptr = &suptr->un_ent[0];
436 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
437 if (sunptr->un_id != semid || sunptr->un_num != semnum)
438 continue;
439 if (adjval != 0) {
440 adjval += sunptr->un_adjval;
441 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
442 return (ERANGE);
443 }
444 sunptr->un_adjval = adjval;
445 if (sunptr->un_adjval == 0) {
446 suptr->un_cnt--;
447 if (i < suptr->un_cnt)
448 suptr->un_ent[i] =
449 suptr->un_ent[suptr->un_cnt];
450 if (suptr->un_cnt == 0)
451 semu_try_free(suptr);
452 }
453 return (0);
454 }
455
456 /* Didn't find the right entry - create it */
457 if (adjval == 0)
458 return (0);
459 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
460 return (ERANGE);
461 if (suptr->un_cnt != seminfo.semume) {
462 sunptr = &suptr->un_ent[suptr->un_cnt];
463 suptr->un_cnt++;
464 sunptr->un_adjval = adjval;
465 sunptr->un_id = semid;
466 sunptr->un_num = semnum;
467 sunptr->un_seq = semseq;
468 } else
469 return (EINVAL);
470 return (0);
471}
472
473static void
474semundo_clear(int semid, int semnum)
475{
476 struct sem_undo *suptr, *suptr1;
477 struct undo *sunptr;
478 int i;
479
480 SEMUNDO_LOCKASSERT(MA_OWNED);
481 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
482 sunptr = &suptr->un_ent[0];
483 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
484 if (sunptr->un_id != semid)
485 continue;
486 if (semnum == -1 || sunptr->un_num == semnum) {
487 suptr->un_cnt--;
488 if (i < suptr->un_cnt) {
489 suptr->un_ent[i] =
490 suptr->un_ent[suptr->un_cnt];
491 continue;
492 }
493 semu_try_free(suptr);
494 }
495 if (semnum != -1)
496 break;
497 }
498 }
499}
500
501static int
502semvalid(int semid, struct semid_kernel *semakptr)
503{
504
505 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
506 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
507}
508
509/*
510 * Note that the user-mode half of this passes a union, not a pointer.
511 */
512#ifndef _SYS_SYSPROTO_H_
513struct __semctl_args {
514 int semid;
515 int semnum;
516 int cmd;
517 union semun *arg;
518};
519#endif
520int
521sys___semctl(struct thread *td, struct __semctl_args *uap)
522{
523 struct semid_ds dsbuf;
524 union semun arg, semun;
525 register_t rval;
526 int error;
527
528 switch (uap->cmd) {
529 case SEM_STAT:
530 case IPC_SET:
531 case IPC_STAT:
532 case GETALL:
533 case SETVAL:
534 case SETALL:
535 error = copyin(uap->arg, &arg, sizeof(arg));
536 if (error)
537 return (error);
538 break;
539 }
540
541 switch (uap->cmd) {
542 case SEM_STAT:
543 case IPC_STAT:
544 semun.buf = &dsbuf;
545 break;
546 case IPC_SET:
547 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
548 if (error)
549 return (error);
550 semun.buf = &dsbuf;
551 break;
552 case GETALL:
553 case SETALL:
554 semun.array = arg.array;
555 break;
556 case SETVAL:
557 semun.val = arg.val;
558 break;
559 }
560
561 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
562 &rval);
563 if (error)
564 return (error);
565
566 switch (uap->cmd) {
567 case SEM_STAT:
568 case IPC_STAT:
569 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
570 break;
571 }
572
573 if (error == 0)
574 td->td_retval[0] = rval;
575 return (error);
576}
577
578int
579kern_semctl(struct thread *td, int semid, int semnum, int cmd,
580 union semun *arg, register_t *rval)
581{
582 u_short *array;
583 struct ucred *cred = td->td_ucred;
584 int i, error;
585 struct semid_ds *sbuf;
586 struct semid_kernel *semakptr;
587 struct mtx *sema_mtxp;
588 u_short usval, count;
589 int semidx;
590
591 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
592 semid, semnum, cmd, arg));
593 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
594 return (ENOSYS);
595
596 array = NULL;
597
598 switch(cmd) {
599 case SEM_STAT:
600 /*
601 * For this command we assume semid is an array index
602 * rather than an IPC id.
603 */
604 if (semid < 0 || semid >= seminfo.semmni)
605 return (EINVAL);
606 semakptr = &sema[semid];
607 sema_mtxp = &sema_mtx[semid];
608 mtx_lock(sema_mtxp);
609 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
610 error = EINVAL;
611 goto done2;
612 }
613 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
614 goto done2;
615#ifdef MAC
616 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
617 if (error != 0)
618 goto done2;
619#endif
620 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
621 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
622 mtx_unlock(sema_mtxp);
623 return (0);
624 }
625
626 semidx = IPCID_TO_IX(semid);
627 if (semidx < 0 || semidx >= seminfo.semmni)
628 return (EINVAL);
629
630 semakptr = &sema[semidx];
631 sema_mtxp = &sema_mtx[semidx];
632 if (cmd == IPC_RMID)
633 mtx_lock(&sem_mtx);
634 mtx_lock(sema_mtxp);
635#ifdef MAC
636 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
637 if (error != 0)
638 goto done2;
639#endif
640
641 error = 0;
642 *rval = 0;
643
644 switch (cmd) {
645 case IPC_RMID:
646 if ((error = semvalid(semid, semakptr)) != 0)
647 goto done2;
648 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
649 goto done2;
650 semakptr->u.sem_perm.cuid = cred->cr_uid;
651 semakptr->u.sem_perm.uid = cred->cr_uid;
652 semakptr->u.sem_perm.mode = 0;
653 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
654 crfree(semakptr->cred);
655 semakptr->cred = NULL;
656 SEMUNDO_LOCK();
657 semundo_clear(semidx, -1);
658 SEMUNDO_UNLOCK();
659#ifdef MAC
660 mac_sysvsem_cleanup(semakptr);
661#endif
662 wakeup(semakptr);
663 for (i = 0; i < seminfo.semmni; i++) {
664 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
665 sema[i].u.sem_base > semakptr->u.sem_base)
666 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
667 }
668 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
669 sem[i] = sem[i + semakptr->u.sem_nsems];
670 for (i = 0; i < seminfo.semmni; i++) {
671 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
672 sema[i].u.sem_base > semakptr->u.sem_base) {
673 sema[i].u.sem_base -= semakptr->u.sem_nsems;
674 mtx_unlock(&sema_mtx[i]);
675 }
676 }
677 semtot -= semakptr->u.sem_nsems;
678 break;
679
680 case IPC_SET:
681 if ((error = semvalid(semid, semakptr)) != 0)
682 goto done2;
683 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
684 goto done2;
685 sbuf = arg->buf;
686 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
687 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
688 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
689 ~0777) | (sbuf->sem_perm.mode & 0777);
690 semakptr->u.sem_ctime = time_second;
691 break;
692
693 case IPC_STAT:
694 if ((error = semvalid(semid, semakptr)) != 0)
695 goto done2;
696 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
697 goto done2;
698 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
699 break;
700
701 case GETNCNT:
702 if ((error = semvalid(semid, semakptr)) != 0)
703 goto done2;
704 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
705 goto done2;
706 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
707 error = EINVAL;
708 goto done2;
709 }
710 *rval = semakptr->u.sem_base[semnum].semncnt;
711 break;
712
713 case GETPID:
714 if ((error = semvalid(semid, semakptr)) != 0)
715 goto done2;
716 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
717 goto done2;
718 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
719 error = EINVAL;
720 goto done2;
721 }
722 *rval = semakptr->u.sem_base[semnum].sempid;
723 break;
724
725 case GETVAL:
726 if ((error = semvalid(semid, semakptr)) != 0)
727 goto done2;
728 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
729 goto done2;
730 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
731 error = EINVAL;
732 goto done2;
733 }
734 *rval = semakptr->u.sem_base[semnum].semval;
735 break;
736
737 case GETALL:
738 /*
739 * Unfortunately, callers of this function don't know
740 * in advance how many semaphores are in this set.
741 * While we could just allocate the maximum size array
742 * and pass the actual size back to the caller, that
743 * won't work for SETALL since we can't copyin() more
744 * data than the user specified as we may return a
745 * spurious EFAULT.
746 *
747 * Note that the number of semaphores in a set is
748 * fixed for the life of that set. The only way that
749 * the 'count' could change while are blocked in
750 * malloc() is if this semaphore set were destroyed
751 * and a new one created with the same index.
752 * However, semvalid() will catch that due to the
753 * sequence number unless exactly 0x8000 (or a
754 * multiple thereof) semaphore sets for the same index
755 * are created and destroyed while we are in malloc!
756 *
757 */
758 count = semakptr->u.sem_nsems;
759 mtx_unlock(sema_mtxp);
760 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
761 mtx_lock(sema_mtxp);
762 if ((error = semvalid(semid, semakptr)) != 0)
763 goto done2;
764 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
765 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
766 goto done2;
767 for (i = 0; i < semakptr->u.sem_nsems; i++)
768 array[i] = semakptr->u.sem_base[i].semval;
769 mtx_unlock(sema_mtxp);
770 error = copyout(array, arg->array, count * sizeof(*array));
771 mtx_lock(sema_mtxp);
772 break;
773
774 case GETZCNT:
775 if ((error = semvalid(semid, semakptr)) != 0)
776 goto done2;
777 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
778 goto done2;
779 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
780 error = EINVAL;
781 goto done2;
782 }
783 *rval = semakptr->u.sem_base[semnum].semzcnt;
784 break;
785
786 case SETVAL:
787 if ((error = semvalid(semid, semakptr)) != 0)
788 goto done2;
789 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
790 goto done2;
791 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
792 error = EINVAL;
793 goto done2;
794 }
795 if (arg->val < 0 || arg->val > seminfo.semvmx) {
796 error = ERANGE;
797 goto done2;
798 }
799 semakptr->u.sem_base[semnum].semval = arg->val;
800 SEMUNDO_LOCK();
801 semundo_clear(semidx, semnum);
802 SEMUNDO_UNLOCK();
803 wakeup(semakptr);
804 break;
805
806 case SETALL:
807 /*
808 * See comment on GETALL for why 'count' shouldn't change
809 * and why we require a userland buffer.
810 */
811 count = semakptr->u.sem_nsems;
812 mtx_unlock(sema_mtxp);
813 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
814 error = copyin(arg->array, array, count * sizeof(*array));
815 mtx_lock(sema_mtxp);
816 if (error)
817 break;
818 if ((error = semvalid(semid, semakptr)) != 0)
819 goto done2;
820 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
821 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
822 goto done2;
823 for (i = 0; i < semakptr->u.sem_nsems; i++) {
824 usval = array[i];
825 if (usval > seminfo.semvmx) {
826 error = ERANGE;
827 break;
828 }
829 semakptr->u.sem_base[i].semval = usval;
830 }
831 SEMUNDO_LOCK();
832 semundo_clear(semidx, -1);
833 SEMUNDO_UNLOCK();
834 wakeup(semakptr);
835 break;
836
837 default:
838 error = EINVAL;
839 break;
840 }
841
842done2:
843 mtx_unlock(sema_mtxp);
844 if (cmd == IPC_RMID)
845 mtx_unlock(&sem_mtx);
846 if (array != NULL)
847 free(array, M_TEMP);
848 return(error);
849}
850
851#ifndef _SYS_SYSPROTO_H_
852struct semget_args {
853 key_t key;
854 int nsems;
855 int semflg;
856};
857#endif
858int
859sys_semget(struct thread *td, struct semget_args *uap)
860{
861 int semid, error = 0;
862 int key = uap->key;
863 int nsems = uap->nsems;
864 int semflg = uap->semflg;
865 struct ucred *cred = td->td_ucred;
866
867 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
868 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
869 return (ENOSYS);
870
871 mtx_lock(&sem_mtx);
872 if (key != IPC_PRIVATE) {
873 for (semid = 0; semid < seminfo.semmni; semid++) {
874 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
875 sema[semid].u.sem_perm.key == key)
876 break;
877 }
878 if (semid < seminfo.semmni) {
879 DPRINTF(("found public key\n"));
880 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
881 semflg & 0700))) {
882 goto done2;
883 }
884 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
885 DPRINTF(("too small\n"));
886 error = EINVAL;
887 goto done2;
888 }
889 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
890 DPRINTF(("not exclusive\n"));
891 error = EEXIST;
892 goto done2;
893 }
894#ifdef MAC
895 error = mac_sysvsem_check_semget(cred, &sema[semid]);
896 if (error != 0)
897 goto done2;
898#endif
899 goto found;
900 }
901 }
902
903 DPRINTF(("need to allocate the semid_kernel\n"));
904 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
905 if (nsems <= 0 || nsems > seminfo.semmsl) {
906 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
907 seminfo.semmsl));
908 error = EINVAL;
909 goto done2;
910 }
911 if (nsems > seminfo.semmns - semtot) {
912 DPRINTF((
913 "not enough semaphores left (need %d, got %d)\n",
914 nsems, seminfo.semmns - semtot));
915 error = ENOSPC;
916 goto done2;
917 }
918 for (semid = 0; semid < seminfo.semmni; semid++) {
919 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
920 break;
921 }
922 if (semid == seminfo.semmni) {
923 DPRINTF(("no more semid_kernel's available\n"));
924 error = ENOSPC;
925 goto done2;
926 }
927#ifdef RACCT
928 PROC_LOCK(td->td_proc);
929 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
930 PROC_UNLOCK(td->td_proc);
931 if (error != 0) {
932 error = ENOSPC;
933 goto done2;
928 if (racct_enable) {
929 PROC_LOCK(td->td_proc);
930 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
931 PROC_UNLOCK(td->td_proc);
932 if (error != 0) {
933 error = ENOSPC;
934 goto done2;
935 }
934 }
935#endif
936 DPRINTF(("semid %d is available\n", semid));
937 mtx_lock(&sema_mtx[semid]);
938 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
939 ("Lost semaphore %d", semid));
940 sema[semid].u.sem_perm.key = key;
941 sema[semid].u.sem_perm.cuid = cred->cr_uid;
942 sema[semid].u.sem_perm.uid = cred->cr_uid;
943 sema[semid].u.sem_perm.cgid = cred->cr_gid;
944 sema[semid].u.sem_perm.gid = cred->cr_gid;
945 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
946 sema[semid].cred = crhold(cred);
947 sema[semid].u.sem_perm.seq =
948 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
949 sema[semid].u.sem_nsems = nsems;
950 sema[semid].u.sem_otime = 0;
951 sema[semid].u.sem_ctime = time_second;
952 sema[semid].u.sem_base = &sem[semtot];
953 semtot += nsems;
954 bzero(sema[semid].u.sem_base,
955 sizeof(sema[semid].u.sem_base[0])*nsems);
956#ifdef MAC
957 mac_sysvsem_create(cred, &sema[semid]);
958#endif
959 mtx_unlock(&sema_mtx[semid]);
960 DPRINTF(("sembase = %p, next = %p\n",
961 sema[semid].u.sem_base, &sem[semtot]));
962 } else {
963 DPRINTF(("didn't find it and wasn't asked to create it\n"));
964 error = ENOENT;
965 goto done2;
966 }
967
968found:
969 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
970done2:
971 mtx_unlock(&sem_mtx);
972 return (error);
973}
974
975#ifndef _SYS_SYSPROTO_H_
976struct semop_args {
977 int semid;
978 struct sembuf *sops;
979 size_t nsops;
980};
981#endif
982int
983sys_semop(struct thread *td, struct semop_args *uap)
984{
985#define SMALL_SOPS 8
986 struct sembuf small_sops[SMALL_SOPS];
987 int semid = uap->semid;
988 size_t nsops = uap->nsops;
989 struct sembuf *sops;
990 struct semid_kernel *semakptr;
991 struct sembuf *sopptr = 0;
992 struct sem *semptr = 0;
993 struct sem_undo *suptr;
994 struct mtx *sema_mtxp;
995 size_t i, j, k;
996 int error;
997 int do_wakeup, do_undos;
998 unsigned short seq;
999
1000#ifdef SEM_DEBUG
1001 sops = NULL;
1002#endif
1003 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1004
1005 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1006 return (ENOSYS);
1007
1008 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1009
1010 if (semid < 0 || semid >= seminfo.semmni)
1011 return (EINVAL);
1012
1013 /* Allocate memory for sem_ops */
1014 if (nsops <= SMALL_SOPS)
1015 sops = small_sops;
1016 else if (nsops > seminfo.semopm) {
1017 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1018 nsops));
1019 return (E2BIG);
1020 } else {
1021#ifdef RACCT
936 }
937#endif
938 DPRINTF(("semid %d is available\n", semid));
939 mtx_lock(&sema_mtx[semid]);
940 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
941 ("Lost semaphore %d", semid));
942 sema[semid].u.sem_perm.key = key;
943 sema[semid].u.sem_perm.cuid = cred->cr_uid;
944 sema[semid].u.sem_perm.uid = cred->cr_uid;
945 sema[semid].u.sem_perm.cgid = cred->cr_gid;
946 sema[semid].u.sem_perm.gid = cred->cr_gid;
947 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
948 sema[semid].cred = crhold(cred);
949 sema[semid].u.sem_perm.seq =
950 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
951 sema[semid].u.sem_nsems = nsems;
952 sema[semid].u.sem_otime = 0;
953 sema[semid].u.sem_ctime = time_second;
954 sema[semid].u.sem_base = &sem[semtot];
955 semtot += nsems;
956 bzero(sema[semid].u.sem_base,
957 sizeof(sema[semid].u.sem_base[0])*nsems);
958#ifdef MAC
959 mac_sysvsem_create(cred, &sema[semid]);
960#endif
961 mtx_unlock(&sema_mtx[semid]);
962 DPRINTF(("sembase = %p, next = %p\n",
963 sema[semid].u.sem_base, &sem[semtot]));
964 } else {
965 DPRINTF(("didn't find it and wasn't asked to create it\n"));
966 error = ENOENT;
967 goto done2;
968 }
969
970found:
971 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
972done2:
973 mtx_unlock(&sem_mtx);
974 return (error);
975}
976
977#ifndef _SYS_SYSPROTO_H_
978struct semop_args {
979 int semid;
980 struct sembuf *sops;
981 size_t nsops;
982};
983#endif
984int
985sys_semop(struct thread *td, struct semop_args *uap)
986{
987#define SMALL_SOPS 8
988 struct sembuf small_sops[SMALL_SOPS];
989 int semid = uap->semid;
990 size_t nsops = uap->nsops;
991 struct sembuf *sops;
992 struct semid_kernel *semakptr;
993 struct sembuf *sopptr = 0;
994 struct sem *semptr = 0;
995 struct sem_undo *suptr;
996 struct mtx *sema_mtxp;
997 size_t i, j, k;
998 int error;
999 int do_wakeup, do_undos;
1000 unsigned short seq;
1001
1002#ifdef SEM_DEBUG
1003 sops = NULL;
1004#endif
1005 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1006
1007 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1008 return (ENOSYS);
1009
1010 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1011
1012 if (semid < 0 || semid >= seminfo.semmni)
1013 return (EINVAL);
1014
1015 /* Allocate memory for sem_ops */
1016 if (nsops <= SMALL_SOPS)
1017 sops = small_sops;
1018 else if (nsops > seminfo.semopm) {
1019 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1020 nsops));
1021 return (E2BIG);
1022 } else {
1023#ifdef RACCT
1022 PROC_LOCK(td->td_proc);
1023 if (nsops > racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1024 if (racct_enable) {
1025 PROC_LOCK(td->td_proc);
1026 if (nsops >
1027 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1028 PROC_UNLOCK(td->td_proc);
1029 return (E2BIG);
1030 }
1024 PROC_UNLOCK(td->td_proc);
1031 PROC_UNLOCK(td->td_proc);
1025 return (E2BIG);
1026 }
1032 }
1027 PROC_UNLOCK(td->td_proc);
1028#endif
1029
1030 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1031 }
1032 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1033 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1034 uap->sops, sops, nsops * sizeof(sops[0])));
1035 if (sops != small_sops)
1036 free(sops, M_SEM);
1037 return (error);
1038 }
1039
1040 semakptr = &sema[semid];
1041 sema_mtxp = &sema_mtx[semid];
1042 mtx_lock(sema_mtxp);
1043 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1044 error = EINVAL;
1045 goto done2;
1046 }
1047 seq = semakptr->u.sem_perm.seq;
1048 if (seq != IPCID_TO_SEQ(uap->semid)) {
1049 error = EINVAL;
1050 goto done2;
1051 }
1052 /*
1053 * Initial pass thru sops to see what permissions are needed.
1054 * Also perform any checks that don't need repeating on each
1055 * attempt to satisfy the request vector.
1056 */
1057 j = 0; /* permission needed */
1058 do_undos = 0;
1059 for (i = 0; i < nsops; i++) {
1060 sopptr = &sops[i];
1061 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1062 error = EFBIG;
1063 goto done2;
1064 }
1065 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1066 do_undos = 1;
1067 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1068 }
1069
1070 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1071 DPRINTF(("error = %d from ipaccess\n", error));
1072 goto done2;
1073 }
1074#ifdef MAC
1075 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1076 if (error != 0)
1077 goto done2;
1078#endif
1079
1080 /*
1081 * Loop trying to satisfy the vector of requests.
1082 * If we reach a point where we must wait, any requests already
1083 * performed are rolled back and we go to sleep until some other
1084 * process wakes us up. At this point, we start all over again.
1085 *
1086 * This ensures that from the perspective of other tasks, a set
1087 * of requests is atomic (never partially satisfied).
1088 */
1089 for (;;) {
1090 do_wakeup = 0;
1091 error = 0; /* error return if necessary */
1092
1093 for (i = 0; i < nsops; i++) {
1094 sopptr = &sops[i];
1095 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1096
1097 DPRINTF((
1098 "semop: semakptr=%p, sem_base=%p, "
1099 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1100 semakptr, semakptr->u.sem_base, semptr,
1101 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1102 (sopptr->sem_flg & IPC_NOWAIT) ?
1103 "nowait" : "wait"));
1104
1105 if (sopptr->sem_op < 0) {
1106 if (semptr->semval + sopptr->sem_op < 0) {
1107 DPRINTF(("semop: can't do it now\n"));
1108 break;
1109 } else {
1110 semptr->semval += sopptr->sem_op;
1111 if (semptr->semval == 0 &&
1112 semptr->semzcnt > 0)
1113 do_wakeup = 1;
1114 }
1115 } else if (sopptr->sem_op == 0) {
1116 if (semptr->semval != 0) {
1117 DPRINTF(("semop: not zero now\n"));
1118 break;
1119 }
1120 } else if (semptr->semval + sopptr->sem_op >
1121 seminfo.semvmx) {
1122 error = ERANGE;
1123 break;
1124 } else {
1125 if (semptr->semncnt > 0)
1126 do_wakeup = 1;
1127 semptr->semval += sopptr->sem_op;
1128 }
1129 }
1130
1131 /*
1132 * Did we get through the entire vector?
1133 */
1134 if (i >= nsops)
1135 goto done;
1136
1137 /*
1138 * No ... rollback anything that we've already done
1139 */
1140 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1141 for (j = 0; j < i; j++)
1142 semakptr->u.sem_base[sops[j].sem_num].semval -=
1143 sops[j].sem_op;
1144
1145 /* If we detected an error, return it */
1146 if (error != 0)
1147 goto done2;
1148
1149 /*
1150 * If the request that we couldn't satisfy has the
1151 * NOWAIT flag set then return with EAGAIN.
1152 */
1153 if (sopptr->sem_flg & IPC_NOWAIT) {
1154 error = EAGAIN;
1155 goto done2;
1156 }
1157
1158 if (sopptr->sem_op == 0)
1159 semptr->semzcnt++;
1160 else
1161 semptr->semncnt++;
1162
1163 DPRINTF(("semop: good night!\n"));
1164 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1165 "semwait", 0);
1166 DPRINTF(("semop: good morning (error=%d)!\n", error));
1167 /* return code is checked below, after sem[nz]cnt-- */
1168
1169 /*
1170 * Make sure that the semaphore still exists
1171 */
1172 seq = semakptr->u.sem_perm.seq;
1173 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1174 seq != IPCID_TO_SEQ(uap->semid)) {
1175 error = EIDRM;
1176 goto done2;
1177 }
1178
1179 /*
1180 * Renew the semaphore's pointer after wakeup since
1181 * during msleep sem_base may have been modified and semptr
1182 * is not valid any more
1183 */
1184 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1185
1186 /*
1187 * The semaphore is still alive. Readjust the count of
1188 * waiting processes.
1189 */
1190 if (sopptr->sem_op == 0)
1191 semptr->semzcnt--;
1192 else
1193 semptr->semncnt--;
1194
1195 /*
1196 * Is it really morning, or was our sleep interrupted?
1197 * (Delayed check of msleep() return code because we
1198 * need to decrement sem[nz]cnt either way.)
1199 */
1200 if (error != 0) {
1201 error = EINTR;
1202 goto done2;
1203 }
1204 DPRINTF(("semop: good morning!\n"));
1205 }
1206
1207done:
1208 /*
1209 * Process any SEM_UNDO requests.
1210 */
1211 if (do_undos) {
1212 SEMUNDO_LOCK();
1213 suptr = NULL;
1214 for (i = 0; i < nsops; i++) {
1215 /*
1216 * We only need to deal with SEM_UNDO's for non-zero
1217 * op's.
1218 */
1219 int adjval;
1220
1221 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1222 continue;
1223 adjval = sops[i].sem_op;
1224 if (adjval == 0)
1225 continue;
1226 error = semundo_adjust(td, &suptr, semid, seq,
1227 sops[i].sem_num, -adjval);
1228 if (error == 0)
1229 continue;
1230
1231 /*
1232 * Oh-Oh! We ran out of either sem_undo's or undo's.
1233 * Rollback the adjustments to this point and then
1234 * rollback the semaphore ups and down so we can return
1235 * with an error with all structures restored. We
1236 * rollback the undo's in the exact reverse order that
1237 * we applied them. This guarantees that we won't run
1238 * out of space as we roll things back out.
1239 */
1240 for (j = 0; j < i; j++) {
1241 k = i - j - 1;
1242 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1243 continue;
1244 adjval = sops[k].sem_op;
1245 if (adjval == 0)
1246 continue;
1247 if (semundo_adjust(td, &suptr, semid, seq,
1248 sops[k].sem_num, adjval) != 0)
1249 panic("semop - can't undo undos");
1250 }
1251
1252 for (j = 0; j < nsops; j++)
1253 semakptr->u.sem_base[sops[j].sem_num].semval -=
1254 sops[j].sem_op;
1255
1256 DPRINTF(("error = %d from semundo_adjust\n", error));
1257 SEMUNDO_UNLOCK();
1258 goto done2;
1259 } /* loop through the sops */
1260 SEMUNDO_UNLOCK();
1261 } /* if (do_undos) */
1262
1263 /* We're definitely done - set the sempid's and time */
1264 for (i = 0; i < nsops; i++) {
1265 sopptr = &sops[i];
1266 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1267 semptr->sempid = td->td_proc->p_pid;
1268 }
1269 semakptr->u.sem_otime = time_second;
1270
1271 /*
1272 * Do a wakeup if any semaphore was up'd whilst something was
1273 * sleeping on it.
1274 */
1275 if (do_wakeup) {
1276 DPRINTF(("semop: doing wakeup\n"));
1277 wakeup(semakptr);
1278 DPRINTF(("semop: back from wakeup\n"));
1279 }
1280 DPRINTF(("semop: done\n"));
1281 td->td_retval[0] = 0;
1282done2:
1283 mtx_unlock(sema_mtxp);
1284 if (sops != small_sops)
1285 free(sops, M_SEM);
1286 return (error);
1287}
1288
1289/*
1290 * Go through the undo structures for this process and apply the adjustments to
1291 * semaphores.
1292 */
1293static void
1294semexit_myhook(void *arg, struct proc *p)
1295{
1296 struct sem_undo *suptr;
1297 struct semid_kernel *semakptr;
1298 struct mtx *sema_mtxp;
1299 int semid, semnum, adjval, ix;
1300 unsigned short seq;
1301
1302 /*
1303 * Go through the chain of undo vectors looking for one
1304 * associated with this process.
1305 */
1306 SEMUNDO_LOCK();
1307 LIST_FOREACH(suptr, &semu_list, un_next) {
1308 if (suptr->un_proc == p)
1309 break;
1310 }
1311 if (suptr == NULL) {
1312 SEMUNDO_UNLOCK();
1313 return;
1314 }
1315 LIST_REMOVE(suptr, un_next);
1316
1317 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1318 suptr->un_cnt));
1319
1320 /*
1321 * If there are any active undo elements then process them.
1322 */
1323 if (suptr->un_cnt > 0) {
1324 SEMUNDO_UNLOCK();
1325 for (ix = 0; ix < suptr->un_cnt; ix++) {
1326 semid = suptr->un_ent[ix].un_id;
1327 semnum = suptr->un_ent[ix].un_num;
1328 adjval = suptr->un_ent[ix].un_adjval;
1329 seq = suptr->un_ent[ix].un_seq;
1330 semakptr = &sema[semid];
1331 sema_mtxp = &sema_mtx[semid];
1332
1333 mtx_lock(sema_mtxp);
1334 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1335 (semakptr->u.sem_perm.seq != seq)) {
1336 mtx_unlock(sema_mtxp);
1337 continue;
1338 }
1339 if (semnum >= semakptr->u.sem_nsems)
1340 panic("semexit - semnum out of range");
1341
1342 DPRINTF((
1343 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1344 suptr->un_proc, suptr->un_ent[ix].un_id,
1345 suptr->un_ent[ix].un_num,
1346 suptr->un_ent[ix].un_adjval,
1347 semakptr->u.sem_base[semnum].semval));
1348
1349 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1350 -adjval)
1351 semakptr->u.sem_base[semnum].semval = 0;
1352 else
1353 semakptr->u.sem_base[semnum].semval += adjval;
1354
1355 wakeup(semakptr);
1356 DPRINTF(("semexit: back from wakeup\n"));
1357 mtx_unlock(sema_mtxp);
1358 }
1359 SEMUNDO_LOCK();
1360 }
1361
1362 /*
1363 * Deallocate the undo vector.
1364 */
1365 DPRINTF(("removing vector\n"));
1366 suptr->un_proc = NULL;
1367 suptr->un_cnt = 0;
1368 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1369 SEMUNDO_UNLOCK();
1370}
1371
1372static int
1373sysctl_sema(SYSCTL_HANDLER_ARGS)
1374{
1375
1376 return (SYSCTL_OUT(req, sema,
1377 sizeof(struct semid_kernel) * seminfo.semmni));
1378}
1379
1380#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1381 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1382
1383/* XXX casting to (sy_call_t *) is bogus, as usual. */
1384static sy_call_t *semcalls[] = {
1385 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1386 (sy_call_t *)sys_semop
1387};
1388
1389/*
1390 * Entry point for all SEM calls.
1391 */
1392int
1393sys_semsys(td, uap)
1394 struct thread *td;
1395 /* XXX actually varargs. */
1396 struct semsys_args /* {
1397 int which;
1398 int a2;
1399 int a3;
1400 int a4;
1401 int a5;
1402 } */ *uap;
1403{
1404 int error;
1405
1406 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1407 return (ENOSYS);
1408 if (uap->which < 0 ||
1409 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1410 return (EINVAL);
1411 error = (*semcalls[uap->which])(td, &uap->a2);
1412 return (error);
1413}
1414
1415#ifndef CP
1416#define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1417#endif
1418
1419#ifndef _SYS_SYSPROTO_H_
1420struct freebsd7___semctl_args {
1421 int semid;
1422 int semnum;
1423 int cmd;
1424 union semun_old *arg;
1425};
1426#endif
1427int
1428freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1429{
1430 struct semid_ds_old dsold;
1431 struct semid_ds dsbuf;
1432 union semun_old arg;
1433 union semun semun;
1434 register_t rval;
1435 int error;
1436
1437 switch (uap->cmd) {
1438 case SEM_STAT:
1439 case IPC_SET:
1440 case IPC_STAT:
1441 case GETALL:
1442 case SETVAL:
1443 case SETALL:
1444 error = copyin(uap->arg, &arg, sizeof(arg));
1445 if (error)
1446 return (error);
1447 break;
1448 }
1449
1450 switch (uap->cmd) {
1451 case SEM_STAT:
1452 case IPC_STAT:
1453 semun.buf = &dsbuf;
1454 break;
1455 case IPC_SET:
1456 error = copyin(arg.buf, &dsold, sizeof(dsold));
1457 if (error)
1458 return (error);
1459 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1460 CP(dsold, dsbuf, sem_base);
1461 CP(dsold, dsbuf, sem_nsems);
1462 CP(dsold, dsbuf, sem_otime);
1463 CP(dsold, dsbuf, sem_ctime);
1464 semun.buf = &dsbuf;
1465 break;
1466 case GETALL:
1467 case SETALL:
1468 semun.array = arg.array;
1469 break;
1470 case SETVAL:
1471 semun.val = arg.val;
1472 break;
1473 }
1474
1475 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1476 &rval);
1477 if (error)
1478 return (error);
1479
1480 switch (uap->cmd) {
1481 case SEM_STAT:
1482 case IPC_STAT:
1483 bzero(&dsold, sizeof(dsold));
1484 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1485 CP(dsbuf, dsold, sem_base);
1486 CP(dsbuf, dsold, sem_nsems);
1487 CP(dsbuf, dsold, sem_otime);
1488 CP(dsbuf, dsold, sem_ctime);
1489 error = copyout(&dsold, arg.buf, sizeof(dsold));
1490 break;
1491 }
1492
1493 if (error == 0)
1494 td->td_retval[0] = rval;
1495 return (error);
1496}
1497
1498#endif /* COMPAT_FREEBSD{4,5,6,7} */
1499
1500#ifdef COMPAT_FREEBSD32
1501
1502int
1503freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1504{
1505
1506#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1507 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1508 switch (uap->which) {
1509 case 0:
1510 return (freebsd7_freebsd32_semctl(td,
1511 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1512 default:
1513 return (sys_semsys(td, (struct semsys_args *)uap));
1514 }
1515#else
1516 return (nosys(td, NULL));
1517#endif
1518}
1519
1520#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1521 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1522int
1523freebsd7_freebsd32_semctl(struct thread *td,
1524 struct freebsd7_freebsd32_semctl_args *uap)
1525{
1526 struct semid_ds32_old dsbuf32;
1527 struct semid_ds dsbuf;
1528 union semun semun;
1529 union semun32 arg;
1530 register_t rval;
1531 int error;
1532
1533 switch (uap->cmd) {
1534 case SEM_STAT:
1535 case IPC_SET:
1536 case IPC_STAT:
1537 case GETALL:
1538 case SETVAL:
1539 case SETALL:
1540 error = copyin(uap->arg, &arg, sizeof(arg));
1541 if (error)
1542 return (error);
1543 break;
1544 }
1545
1546 switch (uap->cmd) {
1547 case SEM_STAT:
1548 case IPC_STAT:
1549 semun.buf = &dsbuf;
1550 break;
1551 case IPC_SET:
1552 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1553 if (error)
1554 return (error);
1555 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1556 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1557 CP(dsbuf32, dsbuf, sem_nsems);
1558 CP(dsbuf32, dsbuf, sem_otime);
1559 CP(dsbuf32, dsbuf, sem_ctime);
1560 semun.buf = &dsbuf;
1561 break;
1562 case GETALL:
1563 case SETALL:
1564 semun.array = PTRIN(arg.array);
1565 break;
1566 case SETVAL:
1567 semun.val = arg.val;
1568 break;
1569 }
1570
1571 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1572 &rval);
1573 if (error)
1574 return (error);
1575
1576 switch (uap->cmd) {
1577 case SEM_STAT:
1578 case IPC_STAT:
1579 bzero(&dsbuf32, sizeof(dsbuf32));
1580 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1581 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1582 CP(dsbuf, dsbuf32, sem_nsems);
1583 CP(dsbuf, dsbuf32, sem_otime);
1584 CP(dsbuf, dsbuf32, sem_ctime);
1585 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1586 break;
1587 }
1588
1589 if (error == 0)
1590 td->td_retval[0] = rval;
1591 return (error);
1592}
1593#endif
1594
1595int
1596freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1597{
1598 struct semid_ds32 dsbuf32;
1599 struct semid_ds dsbuf;
1600 union semun semun;
1601 union semun32 arg;
1602 register_t rval;
1603 int error;
1604
1605 switch (uap->cmd) {
1606 case SEM_STAT:
1607 case IPC_SET:
1608 case IPC_STAT:
1609 case GETALL:
1610 case SETVAL:
1611 case SETALL:
1612 error = copyin(uap->arg, &arg, sizeof(arg));
1613 if (error)
1614 return (error);
1615 break;
1616 }
1617
1618 switch (uap->cmd) {
1619 case SEM_STAT:
1620 case IPC_STAT:
1621 semun.buf = &dsbuf;
1622 break;
1623 case IPC_SET:
1624 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1625 if (error)
1626 return (error);
1627 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1628 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1629 CP(dsbuf32, dsbuf, sem_nsems);
1630 CP(dsbuf32, dsbuf, sem_otime);
1631 CP(dsbuf32, dsbuf, sem_ctime);
1632 semun.buf = &dsbuf;
1633 break;
1634 case GETALL:
1635 case SETALL:
1636 semun.array = PTRIN(arg.array);
1637 break;
1638 case SETVAL:
1639 semun.val = arg.val;
1640 break;
1641 }
1642
1643 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1644 &rval);
1645 if (error)
1646 return (error);
1647
1648 switch (uap->cmd) {
1649 case SEM_STAT:
1650 case IPC_STAT:
1651 bzero(&dsbuf32, sizeof(dsbuf32));
1652 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1653 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1654 CP(dsbuf, dsbuf32, sem_nsems);
1655 CP(dsbuf, dsbuf32, sem_otime);
1656 CP(dsbuf, dsbuf32, sem_ctime);
1657 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1658 break;
1659 }
1660
1661 if (error == 0)
1662 td->td_retval[0] = rval;
1663 return (error);
1664}
1665
1666#endif /* COMPAT_FREEBSD32 */
1033#endif
1034
1035 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1036 }
1037 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1038 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1039 uap->sops, sops, nsops * sizeof(sops[0])));
1040 if (sops != small_sops)
1041 free(sops, M_SEM);
1042 return (error);
1043 }
1044
1045 semakptr = &sema[semid];
1046 sema_mtxp = &sema_mtx[semid];
1047 mtx_lock(sema_mtxp);
1048 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1049 error = EINVAL;
1050 goto done2;
1051 }
1052 seq = semakptr->u.sem_perm.seq;
1053 if (seq != IPCID_TO_SEQ(uap->semid)) {
1054 error = EINVAL;
1055 goto done2;
1056 }
1057 /*
1058 * Initial pass thru sops to see what permissions are needed.
1059 * Also perform any checks that don't need repeating on each
1060 * attempt to satisfy the request vector.
1061 */
1062 j = 0; /* permission needed */
1063 do_undos = 0;
1064 for (i = 0; i < nsops; i++) {
1065 sopptr = &sops[i];
1066 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1067 error = EFBIG;
1068 goto done2;
1069 }
1070 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1071 do_undos = 1;
1072 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1073 }
1074
1075 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1076 DPRINTF(("error = %d from ipaccess\n", error));
1077 goto done2;
1078 }
1079#ifdef MAC
1080 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1081 if (error != 0)
1082 goto done2;
1083#endif
1084
1085 /*
1086 * Loop trying to satisfy the vector of requests.
1087 * If we reach a point where we must wait, any requests already
1088 * performed are rolled back and we go to sleep until some other
1089 * process wakes us up. At this point, we start all over again.
1090 *
1091 * This ensures that from the perspective of other tasks, a set
1092 * of requests is atomic (never partially satisfied).
1093 */
1094 for (;;) {
1095 do_wakeup = 0;
1096 error = 0; /* error return if necessary */
1097
1098 for (i = 0; i < nsops; i++) {
1099 sopptr = &sops[i];
1100 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1101
1102 DPRINTF((
1103 "semop: semakptr=%p, sem_base=%p, "
1104 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1105 semakptr, semakptr->u.sem_base, semptr,
1106 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1107 (sopptr->sem_flg & IPC_NOWAIT) ?
1108 "nowait" : "wait"));
1109
1110 if (sopptr->sem_op < 0) {
1111 if (semptr->semval + sopptr->sem_op < 0) {
1112 DPRINTF(("semop: can't do it now\n"));
1113 break;
1114 } else {
1115 semptr->semval += sopptr->sem_op;
1116 if (semptr->semval == 0 &&
1117 semptr->semzcnt > 0)
1118 do_wakeup = 1;
1119 }
1120 } else if (sopptr->sem_op == 0) {
1121 if (semptr->semval != 0) {
1122 DPRINTF(("semop: not zero now\n"));
1123 break;
1124 }
1125 } else if (semptr->semval + sopptr->sem_op >
1126 seminfo.semvmx) {
1127 error = ERANGE;
1128 break;
1129 } else {
1130 if (semptr->semncnt > 0)
1131 do_wakeup = 1;
1132 semptr->semval += sopptr->sem_op;
1133 }
1134 }
1135
1136 /*
1137 * Did we get through the entire vector?
1138 */
1139 if (i >= nsops)
1140 goto done;
1141
1142 /*
1143 * No ... rollback anything that we've already done
1144 */
1145 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1146 for (j = 0; j < i; j++)
1147 semakptr->u.sem_base[sops[j].sem_num].semval -=
1148 sops[j].sem_op;
1149
1150 /* If we detected an error, return it */
1151 if (error != 0)
1152 goto done2;
1153
1154 /*
1155 * If the request that we couldn't satisfy has the
1156 * NOWAIT flag set then return with EAGAIN.
1157 */
1158 if (sopptr->sem_flg & IPC_NOWAIT) {
1159 error = EAGAIN;
1160 goto done2;
1161 }
1162
1163 if (sopptr->sem_op == 0)
1164 semptr->semzcnt++;
1165 else
1166 semptr->semncnt++;
1167
1168 DPRINTF(("semop: good night!\n"));
1169 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1170 "semwait", 0);
1171 DPRINTF(("semop: good morning (error=%d)!\n", error));
1172 /* return code is checked below, after sem[nz]cnt-- */
1173
1174 /*
1175 * Make sure that the semaphore still exists
1176 */
1177 seq = semakptr->u.sem_perm.seq;
1178 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1179 seq != IPCID_TO_SEQ(uap->semid)) {
1180 error = EIDRM;
1181 goto done2;
1182 }
1183
1184 /*
1185 * Renew the semaphore's pointer after wakeup since
1186 * during msleep sem_base may have been modified and semptr
1187 * is not valid any more
1188 */
1189 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1190
1191 /*
1192 * The semaphore is still alive. Readjust the count of
1193 * waiting processes.
1194 */
1195 if (sopptr->sem_op == 0)
1196 semptr->semzcnt--;
1197 else
1198 semptr->semncnt--;
1199
1200 /*
1201 * Is it really morning, or was our sleep interrupted?
1202 * (Delayed check of msleep() return code because we
1203 * need to decrement sem[nz]cnt either way.)
1204 */
1205 if (error != 0) {
1206 error = EINTR;
1207 goto done2;
1208 }
1209 DPRINTF(("semop: good morning!\n"));
1210 }
1211
1212done:
1213 /*
1214 * Process any SEM_UNDO requests.
1215 */
1216 if (do_undos) {
1217 SEMUNDO_LOCK();
1218 suptr = NULL;
1219 for (i = 0; i < nsops; i++) {
1220 /*
1221 * We only need to deal with SEM_UNDO's for non-zero
1222 * op's.
1223 */
1224 int adjval;
1225
1226 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1227 continue;
1228 adjval = sops[i].sem_op;
1229 if (adjval == 0)
1230 continue;
1231 error = semundo_adjust(td, &suptr, semid, seq,
1232 sops[i].sem_num, -adjval);
1233 if (error == 0)
1234 continue;
1235
1236 /*
1237 * Oh-Oh! We ran out of either sem_undo's or undo's.
1238 * Rollback the adjustments to this point and then
1239 * rollback the semaphore ups and down so we can return
1240 * with an error with all structures restored. We
1241 * rollback the undo's in the exact reverse order that
1242 * we applied them. This guarantees that we won't run
1243 * out of space as we roll things back out.
1244 */
1245 for (j = 0; j < i; j++) {
1246 k = i - j - 1;
1247 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1248 continue;
1249 adjval = sops[k].sem_op;
1250 if (adjval == 0)
1251 continue;
1252 if (semundo_adjust(td, &suptr, semid, seq,
1253 sops[k].sem_num, adjval) != 0)
1254 panic("semop - can't undo undos");
1255 }
1256
1257 for (j = 0; j < nsops; j++)
1258 semakptr->u.sem_base[sops[j].sem_num].semval -=
1259 sops[j].sem_op;
1260
1261 DPRINTF(("error = %d from semundo_adjust\n", error));
1262 SEMUNDO_UNLOCK();
1263 goto done2;
1264 } /* loop through the sops */
1265 SEMUNDO_UNLOCK();
1266 } /* if (do_undos) */
1267
1268 /* We're definitely done - set the sempid's and time */
1269 for (i = 0; i < nsops; i++) {
1270 sopptr = &sops[i];
1271 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1272 semptr->sempid = td->td_proc->p_pid;
1273 }
1274 semakptr->u.sem_otime = time_second;
1275
1276 /*
1277 * Do a wakeup if any semaphore was up'd whilst something was
1278 * sleeping on it.
1279 */
1280 if (do_wakeup) {
1281 DPRINTF(("semop: doing wakeup\n"));
1282 wakeup(semakptr);
1283 DPRINTF(("semop: back from wakeup\n"));
1284 }
1285 DPRINTF(("semop: done\n"));
1286 td->td_retval[0] = 0;
1287done2:
1288 mtx_unlock(sema_mtxp);
1289 if (sops != small_sops)
1290 free(sops, M_SEM);
1291 return (error);
1292}
1293
1294/*
1295 * Go through the undo structures for this process and apply the adjustments to
1296 * semaphores.
1297 */
1298static void
1299semexit_myhook(void *arg, struct proc *p)
1300{
1301 struct sem_undo *suptr;
1302 struct semid_kernel *semakptr;
1303 struct mtx *sema_mtxp;
1304 int semid, semnum, adjval, ix;
1305 unsigned short seq;
1306
1307 /*
1308 * Go through the chain of undo vectors looking for one
1309 * associated with this process.
1310 */
1311 SEMUNDO_LOCK();
1312 LIST_FOREACH(suptr, &semu_list, un_next) {
1313 if (suptr->un_proc == p)
1314 break;
1315 }
1316 if (suptr == NULL) {
1317 SEMUNDO_UNLOCK();
1318 return;
1319 }
1320 LIST_REMOVE(suptr, un_next);
1321
1322 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1323 suptr->un_cnt));
1324
1325 /*
1326 * If there are any active undo elements then process them.
1327 */
1328 if (suptr->un_cnt > 0) {
1329 SEMUNDO_UNLOCK();
1330 for (ix = 0; ix < suptr->un_cnt; ix++) {
1331 semid = suptr->un_ent[ix].un_id;
1332 semnum = suptr->un_ent[ix].un_num;
1333 adjval = suptr->un_ent[ix].un_adjval;
1334 seq = suptr->un_ent[ix].un_seq;
1335 semakptr = &sema[semid];
1336 sema_mtxp = &sema_mtx[semid];
1337
1338 mtx_lock(sema_mtxp);
1339 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1340 (semakptr->u.sem_perm.seq != seq)) {
1341 mtx_unlock(sema_mtxp);
1342 continue;
1343 }
1344 if (semnum >= semakptr->u.sem_nsems)
1345 panic("semexit - semnum out of range");
1346
1347 DPRINTF((
1348 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1349 suptr->un_proc, suptr->un_ent[ix].un_id,
1350 suptr->un_ent[ix].un_num,
1351 suptr->un_ent[ix].un_adjval,
1352 semakptr->u.sem_base[semnum].semval));
1353
1354 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1355 -adjval)
1356 semakptr->u.sem_base[semnum].semval = 0;
1357 else
1358 semakptr->u.sem_base[semnum].semval += adjval;
1359
1360 wakeup(semakptr);
1361 DPRINTF(("semexit: back from wakeup\n"));
1362 mtx_unlock(sema_mtxp);
1363 }
1364 SEMUNDO_LOCK();
1365 }
1366
1367 /*
1368 * Deallocate the undo vector.
1369 */
1370 DPRINTF(("removing vector\n"));
1371 suptr->un_proc = NULL;
1372 suptr->un_cnt = 0;
1373 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1374 SEMUNDO_UNLOCK();
1375}
1376
1377static int
1378sysctl_sema(SYSCTL_HANDLER_ARGS)
1379{
1380
1381 return (SYSCTL_OUT(req, sema,
1382 sizeof(struct semid_kernel) * seminfo.semmni));
1383}
1384
1385#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1386 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1387
1388/* XXX casting to (sy_call_t *) is bogus, as usual. */
1389static sy_call_t *semcalls[] = {
1390 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1391 (sy_call_t *)sys_semop
1392};
1393
1394/*
1395 * Entry point for all SEM calls.
1396 */
1397int
1398sys_semsys(td, uap)
1399 struct thread *td;
1400 /* XXX actually varargs. */
1401 struct semsys_args /* {
1402 int which;
1403 int a2;
1404 int a3;
1405 int a4;
1406 int a5;
1407 } */ *uap;
1408{
1409 int error;
1410
1411 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1412 return (ENOSYS);
1413 if (uap->which < 0 ||
1414 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1415 return (EINVAL);
1416 error = (*semcalls[uap->which])(td, &uap->a2);
1417 return (error);
1418}
1419
1420#ifndef CP
1421#define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1422#endif
1423
1424#ifndef _SYS_SYSPROTO_H_
1425struct freebsd7___semctl_args {
1426 int semid;
1427 int semnum;
1428 int cmd;
1429 union semun_old *arg;
1430};
1431#endif
1432int
1433freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1434{
1435 struct semid_ds_old dsold;
1436 struct semid_ds dsbuf;
1437 union semun_old arg;
1438 union semun semun;
1439 register_t rval;
1440 int error;
1441
1442 switch (uap->cmd) {
1443 case SEM_STAT:
1444 case IPC_SET:
1445 case IPC_STAT:
1446 case GETALL:
1447 case SETVAL:
1448 case SETALL:
1449 error = copyin(uap->arg, &arg, sizeof(arg));
1450 if (error)
1451 return (error);
1452 break;
1453 }
1454
1455 switch (uap->cmd) {
1456 case SEM_STAT:
1457 case IPC_STAT:
1458 semun.buf = &dsbuf;
1459 break;
1460 case IPC_SET:
1461 error = copyin(arg.buf, &dsold, sizeof(dsold));
1462 if (error)
1463 return (error);
1464 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1465 CP(dsold, dsbuf, sem_base);
1466 CP(dsold, dsbuf, sem_nsems);
1467 CP(dsold, dsbuf, sem_otime);
1468 CP(dsold, dsbuf, sem_ctime);
1469 semun.buf = &dsbuf;
1470 break;
1471 case GETALL:
1472 case SETALL:
1473 semun.array = arg.array;
1474 break;
1475 case SETVAL:
1476 semun.val = arg.val;
1477 break;
1478 }
1479
1480 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1481 &rval);
1482 if (error)
1483 return (error);
1484
1485 switch (uap->cmd) {
1486 case SEM_STAT:
1487 case IPC_STAT:
1488 bzero(&dsold, sizeof(dsold));
1489 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1490 CP(dsbuf, dsold, sem_base);
1491 CP(dsbuf, dsold, sem_nsems);
1492 CP(dsbuf, dsold, sem_otime);
1493 CP(dsbuf, dsold, sem_ctime);
1494 error = copyout(&dsold, arg.buf, sizeof(dsold));
1495 break;
1496 }
1497
1498 if (error == 0)
1499 td->td_retval[0] = rval;
1500 return (error);
1501}
1502
1503#endif /* COMPAT_FREEBSD{4,5,6,7} */
1504
1505#ifdef COMPAT_FREEBSD32
1506
1507int
1508freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1509{
1510
1511#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1512 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1513 switch (uap->which) {
1514 case 0:
1515 return (freebsd7_freebsd32_semctl(td,
1516 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1517 default:
1518 return (sys_semsys(td, (struct semsys_args *)uap));
1519 }
1520#else
1521 return (nosys(td, NULL));
1522#endif
1523}
1524
1525#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1526 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1527int
1528freebsd7_freebsd32_semctl(struct thread *td,
1529 struct freebsd7_freebsd32_semctl_args *uap)
1530{
1531 struct semid_ds32_old dsbuf32;
1532 struct semid_ds dsbuf;
1533 union semun semun;
1534 union semun32 arg;
1535 register_t rval;
1536 int error;
1537
1538 switch (uap->cmd) {
1539 case SEM_STAT:
1540 case IPC_SET:
1541 case IPC_STAT:
1542 case GETALL:
1543 case SETVAL:
1544 case SETALL:
1545 error = copyin(uap->arg, &arg, sizeof(arg));
1546 if (error)
1547 return (error);
1548 break;
1549 }
1550
1551 switch (uap->cmd) {
1552 case SEM_STAT:
1553 case IPC_STAT:
1554 semun.buf = &dsbuf;
1555 break;
1556 case IPC_SET:
1557 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1558 if (error)
1559 return (error);
1560 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1561 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1562 CP(dsbuf32, dsbuf, sem_nsems);
1563 CP(dsbuf32, dsbuf, sem_otime);
1564 CP(dsbuf32, dsbuf, sem_ctime);
1565 semun.buf = &dsbuf;
1566 break;
1567 case GETALL:
1568 case SETALL:
1569 semun.array = PTRIN(arg.array);
1570 break;
1571 case SETVAL:
1572 semun.val = arg.val;
1573 break;
1574 }
1575
1576 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1577 &rval);
1578 if (error)
1579 return (error);
1580
1581 switch (uap->cmd) {
1582 case SEM_STAT:
1583 case IPC_STAT:
1584 bzero(&dsbuf32, sizeof(dsbuf32));
1585 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1586 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1587 CP(dsbuf, dsbuf32, sem_nsems);
1588 CP(dsbuf, dsbuf32, sem_otime);
1589 CP(dsbuf, dsbuf32, sem_ctime);
1590 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1591 break;
1592 }
1593
1594 if (error == 0)
1595 td->td_retval[0] = rval;
1596 return (error);
1597}
1598#endif
1599
1600int
1601freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1602{
1603 struct semid_ds32 dsbuf32;
1604 struct semid_ds dsbuf;
1605 union semun semun;
1606 union semun32 arg;
1607 register_t rval;
1608 int error;
1609
1610 switch (uap->cmd) {
1611 case SEM_STAT:
1612 case IPC_SET:
1613 case IPC_STAT:
1614 case GETALL:
1615 case SETVAL:
1616 case SETALL:
1617 error = copyin(uap->arg, &arg, sizeof(arg));
1618 if (error)
1619 return (error);
1620 break;
1621 }
1622
1623 switch (uap->cmd) {
1624 case SEM_STAT:
1625 case IPC_STAT:
1626 semun.buf = &dsbuf;
1627 break;
1628 case IPC_SET:
1629 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1630 if (error)
1631 return (error);
1632 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1633 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1634 CP(dsbuf32, dsbuf, sem_nsems);
1635 CP(dsbuf32, dsbuf, sem_otime);
1636 CP(dsbuf32, dsbuf, sem_ctime);
1637 semun.buf = &dsbuf;
1638 break;
1639 case GETALL:
1640 case SETALL:
1641 semun.array = PTRIN(arg.array);
1642 break;
1643 case SETVAL:
1644 semun.val = arg.val;
1645 break;
1646 }
1647
1648 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1649 &rval);
1650 if (error)
1651 return (error);
1652
1653 switch (uap->cmd) {
1654 case SEM_STAT:
1655 case IPC_STAT:
1656 bzero(&dsbuf32, sizeof(dsbuf32));
1657 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1658 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1659 CP(dsbuf, dsbuf32, sem_nsems);
1660 CP(dsbuf, dsbuf32, sem_otime);
1661 CP(dsbuf, dsbuf32, sem_ctime);
1662 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1663 break;
1664 }
1665
1666 if (error == 0)
1667 td->td_retval[0] = rval;
1668 return (error);
1669}
1670
1671#endif /* COMPAT_FREEBSD32 */