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