xref: /freebsd/sys/kern/sysv_msg.c (revision 30b72b6871140f0b29c64d41fc85c4c1d4d4b3f4)
1 /*-
2  * Implementation of SVID messages
3  *
4  * Author:  Daniel Boulet
5  *
6  * Copyright 1993 Daniel Boulet and RTMX Inc.
7  *
8  * This system call was implemented by Daniel Boulet under contract from RTMX.
9  *
10  * Redistribution and use in source forms, with and without modification,
11  * are permitted provided that this entire comment appears intact.
12  *
13  * Redistribution in binary form may occur without any restrictions.
14  * Obviously, it would be nice if you gave credit where credit is due
15  * but requiring it would be too onerous.
16  *
17  * This software is provided ``AS IS'' without any warranties of any kind.
18  */
19 /*-
20  * Copyright (c) 2003-2005 McAfee, Inc.
21  * All rights reserved.
22  *
23  * This software was developed for the FreeBSD Project in part by McAfee
24  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
25  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
26  * program.
27  *
28  * Redistribution and use in source and binary forms, with or without
29  * modification, are permitted provided that the following conditions
30  * are met:
31  * 1. Redistributions of source code must retain the above copyright
32  *    notice, this list of conditions and the following disclaimer.
33  * 2. Redistributions in binary form must reproduce the above copyright
34  *    notice, this list of conditions and the following disclaimer in the
35  *    documentation and/or other materials provided with the distribution.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  */
49 
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52 
53 #include "opt_compat.h"
54 #include "opt_sysvipc.h"
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/sysproto.h>
59 #include <sys/kernel.h>
60 #include <sys/priv.h>
61 #include <sys/proc.h>
62 #include <sys/lock.h>
63 #include <sys/mutex.h>
64 #include <sys/module.h>
65 #include <sys/mount.h>
66 #include <sys/msg.h>
67 #include <sys/racct.h>
68 #include <sys/sbuf.h>
69 #include <sys/sx.h>
70 #include <sys/syscall.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysent.h>
73 #include <sys/sysctl.h>
74 #include <sys/malloc.h>
75 #include <sys/jail.h>
76 
77 #include <security/mac/mac_framework.h>
78 
79 FEATURE(sysv_msg, "System V message queues support");
80 
81 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
82 
83 static int msginit(void);
84 static int msgunload(void);
85 static int sysvmsg_modload(struct module *, int, void *);
86 static void msq_remove(struct msqid_kernel *);
87 static struct prison *msg_find_prison(struct ucred *);
88 static int msq_prison_cansee(struct prison *, struct msqid_kernel *);
89 static int msg_prison_check(void *, void *);
90 static int msg_prison_set(void *, void *);
91 static int msg_prison_get(void *, void *);
92 static int msg_prison_remove(void *, void *);
93 static void msg_prison_cleanup(struct prison *);
94 
95 
96 #ifdef MSG_DEBUG
97 #define DPRINTF(a)	printf a
98 #else
99 #define DPRINTF(a)	(void)0
100 #endif
101 
102 static void msg_freehdr(struct msg *msghdr);
103 
104 #ifndef MSGSSZ
105 #define MSGSSZ	8		/* Each segment must be 2^N long */
106 #endif
107 #ifndef MSGSEG
108 #define MSGSEG	2048		/* must be less than 32767 */
109 #endif
110 #define MSGMAX	(MSGSSZ*MSGSEG)
111 #ifndef MSGMNB
112 #define MSGMNB	2048		/* max # of bytes in a queue */
113 #endif
114 #ifndef MSGMNI
115 #define MSGMNI	40
116 #endif
117 #ifndef MSGTQL
118 #define MSGTQL	40
119 #endif
120 
121 /*
122  * Based on the configuration parameters described in an SVR2 (yes, two)
123  * config(1m) man page.
124  *
125  * Each message is broken up and stored in segments that are msgssz bytes
126  * long.  For efficiency reasons, this should be a power of two.  Also,
127  * it doesn't make sense if it is less than 8 or greater than about 256.
128  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
129  * two between 8 and 1024 inclusive (and panic's if it isn't).
130  */
131 struct msginfo msginfo = {
132                 MSGMAX,         /* max chars in a message */
133                 MSGMNI,         /* # of message queue identifiers */
134                 MSGMNB,         /* max chars in a queue */
135                 MSGTQL,         /* max messages in system */
136                 MSGSSZ,         /* size of a message segment */
137                 		/* (must be small power of 2 greater than 4) */
138                 MSGSEG          /* number of message segments */
139 };
140 
141 /*
142  * macros to convert between msqid_ds's and msqid's.
143  * (specific to this implementation)
144  */
145 #define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
146 #define MSQID_IX(id)	((id) & 0xffff)
147 #define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
148 
149 /*
150  * The rest of this file is specific to this particular implementation.
151  */
152 
153 struct msgmap {
154 	short	next;		/* next segment in buffer */
155     				/* -1 -> available */
156     				/* 0..(MSGSEG-1) -> index of next segment */
157 };
158 
159 #define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
160 
161 static int nfree_msgmaps;	/* # of free map entries */
162 static short free_msgmaps;	/* head of linked list of free map entries */
163 static struct msg *free_msghdrs;/* list of free msg headers */
164 static char *msgpool;		/* MSGMAX byte long msg buffer pool */
165 static struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
166 static struct msg *msghdrs;	/* MSGTQL msg headers */
167 static struct msqid_kernel *msqids;	/* MSGMNI msqid_kernel struct's */
168 static struct mtx msq_mtx;	/* global mutex for message queues. */
169 static unsigned msg_prison_slot;/* prison OSD slot */
170 
171 static struct syscall_helper_data msg_syscalls[] = {
172 	SYSCALL_INIT_HELPER(msgctl),
173 	SYSCALL_INIT_HELPER(msgget),
174 	SYSCALL_INIT_HELPER(msgsnd),
175 	SYSCALL_INIT_HELPER(msgrcv),
176 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
177     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
178 	SYSCALL_INIT_HELPER(msgsys),
179 	SYSCALL_INIT_HELPER_COMPAT(freebsd7_msgctl),
180 #endif
181 	SYSCALL_INIT_LAST
182 };
183 
184 #ifdef COMPAT_FREEBSD32
185 #include <compat/freebsd32/freebsd32.h>
186 #include <compat/freebsd32/freebsd32_ipc.h>
187 #include <compat/freebsd32/freebsd32_proto.h>
188 #include <compat/freebsd32/freebsd32_signal.h>
189 #include <compat/freebsd32/freebsd32_syscall.h>
190 #include <compat/freebsd32/freebsd32_util.h>
191 
192 static struct syscall_helper_data msg32_syscalls[] = {
193 	SYSCALL32_INIT_HELPER(freebsd32_msgctl),
194 	SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
195 	SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
196 	SYSCALL32_INIT_HELPER_COMPAT(msgget),
197 	SYSCALL32_INIT_HELPER(freebsd32_msgsys),
198 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
199     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
200 	SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
201 #endif
202 	SYSCALL_INIT_LAST
203 };
204 #endif
205 
206 static int
207 msginit()
208 {
209 	struct prison *pr;
210 	void *rsv;
211 	int i, error;
212 	osd_method_t methods[PR_MAXMETHOD] = {
213 	    [PR_METHOD_CHECK] =		msg_prison_check,
214 	    [PR_METHOD_SET] =		msg_prison_set,
215 	    [PR_METHOD_GET] =		msg_prison_get,
216 	    [PR_METHOD_REMOVE] =	msg_prison_remove,
217 	};
218 
219 	msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
220 	msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
221 	msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
222 	msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
223 	msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
224 	    M_WAITOK);
225 
226 	/*
227 	 * msginfo.msgssz should be a power of two for efficiency reasons.
228 	 * It is also pretty silly if msginfo.msgssz is less than 8
229 	 * or greater than about 256 so ...
230 	 */
231 
232 	i = 8;
233 	while (i < 1024 && i != msginfo.msgssz)
234 		i <<= 1;
235     	if (i != msginfo.msgssz) {
236 		DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
237 		    msginfo.msgssz));
238 		panic("msginfo.msgssz not a small power of 2");
239 	}
240 
241 	if (msginfo.msgseg > 32767) {
242 		DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
243 		panic("msginfo.msgseg > 32767");
244 	}
245 
246 	for (i = 0; i < msginfo.msgseg; i++) {
247 		if (i > 0)
248 			msgmaps[i-1].next = i;
249 		msgmaps[i].next = -1;	/* implies entry is available */
250 	}
251 	free_msgmaps = 0;
252 	nfree_msgmaps = msginfo.msgseg;
253 
254 	for (i = 0; i < msginfo.msgtql; i++) {
255 		msghdrs[i].msg_type = 0;
256 		if (i > 0)
257 			msghdrs[i-1].msg_next = &msghdrs[i];
258 		msghdrs[i].msg_next = NULL;
259 #ifdef MAC
260 		mac_sysvmsg_init(&msghdrs[i]);
261 #endif
262     	}
263 	free_msghdrs = &msghdrs[0];
264 
265 	for (i = 0; i < msginfo.msgmni; i++) {
266 		msqids[i].u.msg_qbytes = 0;	/* implies entry is available */
267 		msqids[i].u.msg_perm.seq = 0;	/* reset to a known value */
268 		msqids[i].u.msg_perm.mode = 0;
269 #ifdef MAC
270 		mac_sysvmsq_init(&msqids[i]);
271 #endif
272 	}
273 	mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
274 
275 	/* Set current prisons according to their allow.sysvipc. */
276 	msg_prison_slot = osd_jail_register(NULL, methods);
277 	rsv = osd_reserve(msg_prison_slot);
278 	prison_lock(&prison0);
279 	(void)osd_jail_set_reserved(&prison0, msg_prison_slot, rsv, &prison0);
280 	prison_unlock(&prison0);
281 	rsv = NULL;
282 	sx_slock(&allprison_lock);
283 	TAILQ_FOREACH(pr, &allprison, pr_list) {
284 		if (rsv == NULL)
285 			rsv = osd_reserve(msg_prison_slot);
286 		prison_lock(pr);
287 		if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
288 			(void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
289 			    &prison0);
290 			rsv = NULL;
291 		}
292 		prison_unlock(pr);
293 	}
294 	if (rsv != NULL)
295 		osd_free_reserved(rsv);
296 	sx_sunlock(&allprison_lock);
297 
298 	error = syscall_helper_register(msg_syscalls, SY_THR_STATIC_KLD);
299 	if (error != 0)
300 		return (error);
301 #ifdef COMPAT_FREEBSD32
302 	error = syscall32_helper_register(msg32_syscalls, SY_THR_STATIC_KLD);
303 	if (error != 0)
304 		return (error);
305 #endif
306 	return (0);
307 }
308 
309 static int
310 msgunload()
311 {
312 	struct msqid_kernel *msqkptr;
313 	int msqid;
314 #ifdef MAC
315 	int i;
316 #endif
317 
318 	syscall_helper_unregister(msg_syscalls);
319 #ifdef COMPAT_FREEBSD32
320 	syscall32_helper_unregister(msg32_syscalls);
321 #endif
322 
323 	for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
324 		/*
325 		 * Look for an unallocated and unlocked msqid_ds.
326 		 * msqid_ds's can be locked by msgsnd or msgrcv while
327 		 * they are copying the message in/out.  We can't
328 		 * re-use the entry until they release it.
329 		 */
330 		msqkptr = &msqids[msqid];
331 		if (msqkptr->u.msg_qbytes != 0 ||
332 		    (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
333 			break;
334 	}
335 	if (msqid != msginfo.msgmni)
336 		return (EBUSY);
337 
338 	if (msg_prison_slot != 0)
339 		osd_jail_deregister(msg_prison_slot);
340 #ifdef MAC
341 	for (i = 0; i < msginfo.msgtql; i++)
342 		mac_sysvmsg_destroy(&msghdrs[i]);
343 	for (msqid = 0; msqid < msginfo.msgmni; msqid++)
344 		mac_sysvmsq_destroy(&msqids[msqid]);
345 #endif
346 	free(msgpool, M_MSG);
347 	free(msgmaps, M_MSG);
348 	free(msghdrs, M_MSG);
349 	free(msqids, M_MSG);
350 	mtx_destroy(&msq_mtx);
351 	return (0);
352 }
353 
354 
355 static int
356 sysvmsg_modload(struct module *module, int cmd, void *arg)
357 {
358 	int error = 0;
359 
360 	switch (cmd) {
361 	case MOD_LOAD:
362 		error = msginit();
363 		if (error != 0)
364 			msgunload();
365 		break;
366 	case MOD_UNLOAD:
367 		error = msgunload();
368 		break;
369 	case MOD_SHUTDOWN:
370 		break;
371 	default:
372 		error = EINVAL;
373 		break;
374 	}
375 	return (error);
376 }
377 
378 static moduledata_t sysvmsg_mod = {
379 	"sysvmsg",
380 	&sysvmsg_modload,
381 	NULL
382 };
383 
384 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
385 MODULE_VERSION(sysvmsg, 1);
386 
387 static void
388 msg_freehdr(msghdr)
389 	struct msg *msghdr;
390 {
391 	while (msghdr->msg_ts > 0) {
392 		short next;
393 		if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
394 			panic("msghdr->msg_spot out of range");
395 		next = msgmaps[msghdr->msg_spot].next;
396 		msgmaps[msghdr->msg_spot].next = free_msgmaps;
397 		free_msgmaps = msghdr->msg_spot;
398 		nfree_msgmaps++;
399 		msghdr->msg_spot = next;
400 		if (msghdr->msg_ts >= msginfo.msgssz)
401 			msghdr->msg_ts -= msginfo.msgssz;
402 		else
403 			msghdr->msg_ts = 0;
404 	}
405 	if (msghdr->msg_spot != -1)
406 		panic("msghdr->msg_spot != -1");
407 	msghdr->msg_next = free_msghdrs;
408 	free_msghdrs = msghdr;
409 #ifdef MAC
410 	mac_sysvmsg_cleanup(msghdr);
411 #endif
412 }
413 
414 static void
415 msq_remove(struct msqid_kernel *msqkptr)
416 {
417 	struct msg *msghdr;
418 
419 	racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
420 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
421 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
422 	crfree(msqkptr->cred);
423 	msqkptr->cred = NULL;
424 
425 	/* Free the message headers */
426 	msghdr = msqkptr->u.msg_first;
427 	while (msghdr != NULL) {
428 		struct msg *msghdr_tmp;
429 
430 		/* Free the segments of each message */
431 		msqkptr->u.msg_cbytes -= msghdr->msg_ts;
432 		msqkptr->u.msg_qnum--;
433 		msghdr_tmp = msghdr;
434 		msghdr = msghdr->msg_next;
435 		msg_freehdr(msghdr_tmp);
436 	}
437 
438 	if (msqkptr->u.msg_cbytes != 0)
439 		panic("msg_cbytes is screwed up");
440 	if (msqkptr->u.msg_qnum != 0)
441 		panic("msg_qnum is screwed up");
442 
443 	msqkptr->u.msg_qbytes = 0;	/* Mark it as free */
444 
445 #ifdef MAC
446 	mac_sysvmsq_cleanup(msqkptr);
447 #endif
448 
449 	wakeup(msqkptr);
450 }
451 
452 static struct prison *
453 msg_find_prison(struct ucred *cred)
454 {
455 	struct prison *pr, *rpr;
456 
457 	pr = cred->cr_prison;
458 	prison_lock(pr);
459 	rpr = osd_jail_get(pr, msg_prison_slot);
460 	prison_unlock(pr);
461 	return rpr;
462 }
463 
464 static int
465 msq_prison_cansee(struct prison *rpr, struct msqid_kernel *msqkptr)
466 {
467 
468 	if (msqkptr->cred == NULL ||
469 	    !(rpr == msqkptr->cred->cr_prison ||
470 	      prison_ischild(rpr, msqkptr->cred->cr_prison)))
471 		return (EINVAL);
472 	return (0);
473 }
474 
475 #ifndef _SYS_SYSPROTO_H_
476 struct msgctl_args {
477 	int	msqid;
478 	int	cmd;
479 	struct	msqid_ds *buf;
480 };
481 #endif
482 int
483 sys_msgctl(td, uap)
484 	struct thread *td;
485 	register struct msgctl_args *uap;
486 {
487 	int msqid = uap->msqid;
488 	int cmd = uap->cmd;
489 	struct msqid_ds msqbuf;
490 	int error;
491 
492 	DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
493 	if (cmd == IPC_SET &&
494 	    (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
495 		return (error);
496 	error = kern_msgctl(td, msqid, cmd, &msqbuf);
497 	if (cmd == IPC_STAT && error == 0)
498 		error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
499 	return (error);
500 }
501 
502 int
503 kern_msgctl(td, msqid, cmd, msqbuf)
504 	struct thread *td;
505 	int msqid;
506 	int cmd;
507 	struct msqid_ds *msqbuf;
508 {
509 	int rval, error, msqix;
510 	register struct msqid_kernel *msqkptr;
511 	struct prison *rpr;
512 
513 	rpr = msg_find_prison(td->td_ucred);
514 	if (rpr == NULL)
515 		return (ENOSYS);
516 
517 	msqix = IPCID_TO_IX(msqid);
518 
519 	if (msqix < 0 || msqix >= msginfo.msgmni) {
520 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
521 		    msginfo.msgmni));
522 		return (EINVAL);
523 	}
524 
525 	msqkptr = &msqids[msqix];
526 
527 	mtx_lock(&msq_mtx);
528 	if (msqkptr->u.msg_qbytes == 0) {
529 		DPRINTF(("no such msqid\n"));
530 		error = EINVAL;
531 		goto done2;
532 	}
533 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
534 		DPRINTF(("wrong sequence number\n"));
535 		error = EINVAL;
536 		goto done2;
537 	}
538 
539 	error = msq_prison_cansee(rpr, msqkptr);
540 	if (error != 0) {
541 		DPRINTF(("requester can't see prison\n"));
542 		goto done2;
543 	}
544 
545 #ifdef MAC
546 	error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
547 	if (error != 0)
548 		goto done2;
549 #endif
550 
551 	error = 0;
552 	rval = 0;
553 
554 	switch (cmd) {
555 
556 	case IPC_RMID:
557 	{
558 #ifdef MAC
559 		struct msg *msghdr;
560 #endif
561 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
562 			goto done2;
563 
564 #ifdef MAC
565 		/*
566 		 * Check that the thread has MAC access permissions to
567 		 * individual msghdrs.  Note: We need to do this in a
568 		 * separate loop because the actual loop alters the
569 		 * msq/msghdr info as it progresses, and there is no going
570 		 * back if half the way through we discover that the
571 		 * thread cannot free a certain msghdr.  The msq will get
572 		 * into an inconsistent state.
573 		 */
574 		for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
575 		    msghdr = msghdr->msg_next) {
576 			error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
577 			if (error != 0)
578 				goto done2;
579 		}
580 #endif
581 
582 		msq_remove(msqkptr);
583 	}
584 
585 		break;
586 
587 	case IPC_SET:
588 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
589 			goto done2;
590 		if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
591 			error = priv_check(td, PRIV_IPC_MSGSIZE);
592 			if (error)
593 				goto done2;
594 		}
595 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
596 			DPRINTF(("can't increase msg_qbytes beyond %d"
597 			    "(truncating)\n", msginfo.msgmnb));
598 			msqbuf->msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
599 		}
600 		if (msqbuf->msg_qbytes == 0) {
601 			DPRINTF(("can't reduce msg_qbytes to 0\n"));
602 			error = EINVAL;		/* non-standard errno! */
603 			goto done2;
604 		}
605 		msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid;	/* change the owner */
606 		msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid;	/* change the owner */
607 		msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
608 		    (msqbuf->msg_perm.mode & 0777);
609 		msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
610 		msqkptr->u.msg_ctime = time_second;
611 		break;
612 
613 	case IPC_STAT:
614 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
615 			DPRINTF(("requester doesn't have read access\n"));
616 			goto done2;
617 		}
618 		*msqbuf = msqkptr->u;
619 		if (td->td_ucred->cr_prison != msqkptr->cred->cr_prison)
620 			msqbuf->msg_perm.key = IPC_PRIVATE;
621 		break;
622 
623 	default:
624 		DPRINTF(("invalid command %d\n", cmd));
625 		error = EINVAL;
626 		goto done2;
627 	}
628 
629 	if (error == 0)
630 		td->td_retval[0] = rval;
631 done2:
632 	mtx_unlock(&msq_mtx);
633 	return (error);
634 }
635 
636 #ifndef _SYS_SYSPROTO_H_
637 struct msgget_args {
638 	key_t	key;
639 	int	msgflg;
640 };
641 #endif
642 
643 int
644 sys_msgget(td, uap)
645 	struct thread *td;
646 	register struct msgget_args *uap;
647 {
648 	int msqid, error = 0;
649 	int key = uap->key;
650 	int msgflg = uap->msgflg;
651 	struct ucred *cred = td->td_ucred;
652 	register struct msqid_kernel *msqkptr = NULL;
653 
654 	DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
655 
656 	if (msg_find_prison(cred) == NULL)
657 		return (ENOSYS);
658 
659 	mtx_lock(&msq_mtx);
660 	if (key != IPC_PRIVATE) {
661 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
662 			msqkptr = &msqids[msqid];
663 			if (msqkptr->u.msg_qbytes != 0 &&
664 			    msqkptr->cred != NULL &&
665 			    msqkptr->cred->cr_prison == cred->cr_prison &&
666 			    msqkptr->u.msg_perm.key == key)
667 				break;
668 		}
669 		if (msqid < msginfo.msgmni) {
670 			DPRINTF(("found public key\n"));
671 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
672 				DPRINTF(("not exclusive\n"));
673 				error = EEXIST;
674 				goto done2;
675 			}
676 			if ((error = ipcperm(td, &msqkptr->u.msg_perm,
677 			    msgflg & 0700))) {
678 				DPRINTF(("requester doesn't have 0%o access\n",
679 				    msgflg & 0700));
680 				goto done2;
681 			}
682 #ifdef MAC
683 			error = mac_sysvmsq_check_msqget(cred, msqkptr);
684 			if (error != 0)
685 				goto done2;
686 #endif
687 			goto found;
688 		}
689 	}
690 
691 	DPRINTF(("need to allocate the msqid_ds\n"));
692 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
693 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
694 			/*
695 			 * Look for an unallocated and unlocked msqid_ds.
696 			 * msqid_ds's can be locked by msgsnd or msgrcv while
697 			 * they are copying the message in/out.  We can't
698 			 * re-use the entry until they release it.
699 			 */
700 			msqkptr = &msqids[msqid];
701 			if (msqkptr->u.msg_qbytes == 0 &&
702 			    (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
703 				break;
704 		}
705 		if (msqid == msginfo.msgmni) {
706 			DPRINTF(("no more msqid_ds's available\n"));
707 			error = ENOSPC;
708 			goto done2;
709 		}
710 #ifdef RACCT
711 		if (racct_enable) {
712 			PROC_LOCK(td->td_proc);
713 			error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
714 			PROC_UNLOCK(td->td_proc);
715 			if (error != 0) {
716 				error = ENOSPC;
717 				goto done2;
718 			}
719 		}
720 #endif
721 		DPRINTF(("msqid %d is available\n", msqid));
722 		msqkptr->u.msg_perm.key = key;
723 		msqkptr->u.msg_perm.cuid = cred->cr_uid;
724 		msqkptr->u.msg_perm.uid = cred->cr_uid;
725 		msqkptr->u.msg_perm.cgid = cred->cr_gid;
726 		msqkptr->u.msg_perm.gid = cred->cr_gid;
727 		msqkptr->u.msg_perm.mode = (msgflg & 0777);
728 		msqkptr->cred = crhold(cred);
729 		/* Make sure that the returned msqid is unique */
730 		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
731 		msqkptr->u.msg_first = NULL;
732 		msqkptr->u.msg_last = NULL;
733 		msqkptr->u.msg_cbytes = 0;
734 		msqkptr->u.msg_qnum = 0;
735 		msqkptr->u.msg_qbytes = msginfo.msgmnb;
736 		msqkptr->u.msg_lspid = 0;
737 		msqkptr->u.msg_lrpid = 0;
738 		msqkptr->u.msg_stime = 0;
739 		msqkptr->u.msg_rtime = 0;
740 		msqkptr->u.msg_ctime = time_second;
741 #ifdef MAC
742 		mac_sysvmsq_create(cred, msqkptr);
743 #endif
744 	} else {
745 		DPRINTF(("didn't find it and wasn't asked to create it\n"));
746 		error = ENOENT;
747 		goto done2;
748 	}
749 
750 found:
751 	/* Construct the unique msqid */
752 	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
753 done2:
754 	mtx_unlock(&msq_mtx);
755 	return (error);
756 }
757 
758 #ifndef _SYS_SYSPROTO_H_
759 struct msgsnd_args {
760 	int	msqid;
761 	const void	*msgp;
762 	size_t	msgsz;
763 	int	msgflg;
764 };
765 #endif
766 int
767 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
768 	struct thread *td;
769 	int msqid;
770 	const void *msgp;	/* XXX msgp is actually mtext. */
771 	size_t msgsz;
772 	int msgflg;
773 	long mtype;
774 {
775 	int msqix, segs_needed, error = 0;
776 	register struct msqid_kernel *msqkptr;
777 	register struct msg *msghdr;
778 	struct prison *rpr;
779 	short next;
780 #ifdef RACCT
781 	size_t saved_msgsz;
782 #endif
783 
784 	rpr = msg_find_prison(td->td_ucred);
785 	if (rpr == NULL)
786 		return (ENOSYS);
787 
788 	mtx_lock(&msq_mtx);
789 	msqix = IPCID_TO_IX(msqid);
790 
791 	if (msqix < 0 || msqix >= msginfo.msgmni) {
792 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
793 		    msginfo.msgmni));
794 		error = EINVAL;
795 		goto done2;
796 	}
797 
798 	msqkptr = &msqids[msqix];
799 	if (msqkptr->u.msg_qbytes == 0) {
800 		DPRINTF(("no such message queue id\n"));
801 		error = EINVAL;
802 		goto done2;
803 	}
804 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
805 		DPRINTF(("wrong sequence number\n"));
806 		error = EINVAL;
807 		goto done2;
808 	}
809 
810 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
811 		DPRINTF(("requester can't see prison\n"));
812 		goto done2;
813 	}
814 
815 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
816 		DPRINTF(("requester doesn't have write access\n"));
817 		goto done2;
818 	}
819 
820 #ifdef MAC
821 	error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
822 	if (error != 0)
823 		goto done2;
824 #endif
825 
826 #ifdef RACCT
827 	if (racct_enable) {
828 		PROC_LOCK(td->td_proc);
829 		if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
830 			PROC_UNLOCK(td->td_proc);
831 			error = EAGAIN;
832 			goto done2;
833 		}
834 		saved_msgsz = msgsz;
835 		if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
836 			racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
837 			PROC_UNLOCK(td->td_proc);
838 			error = EAGAIN;
839 			goto done2;
840 		}
841 		PROC_UNLOCK(td->td_proc);
842 	}
843 #endif
844 
845 	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
846 	DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
847 	    msginfo.msgssz, segs_needed));
848 	for (;;) {
849 		int need_more_resources = 0;
850 
851 		/*
852 		 * check msgsz
853 		 * (inside this loop in case msg_qbytes changes while we sleep)
854 		 */
855 
856 		if (msgsz > msqkptr->u.msg_qbytes) {
857 			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
858 			error = EINVAL;
859 			goto done3;
860 		}
861 
862 		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
863 			DPRINTF(("msqid is locked\n"));
864 			need_more_resources = 1;
865 		}
866 		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
867 			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
868 			need_more_resources = 1;
869 		}
870 		if (segs_needed > nfree_msgmaps) {
871 			DPRINTF(("segs_needed > nfree_msgmaps\n"));
872 			need_more_resources = 1;
873 		}
874 		if (free_msghdrs == NULL) {
875 			DPRINTF(("no more msghdrs\n"));
876 			need_more_resources = 1;
877 		}
878 
879 		if (need_more_resources) {
880 			int we_own_it;
881 
882 			if ((msgflg & IPC_NOWAIT) != 0) {
883 				DPRINTF(("need more resources but caller "
884 				    "doesn't want to wait\n"));
885 				error = EAGAIN;
886 				goto done3;
887 			}
888 
889 			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
890 				DPRINTF(("we don't own the msqid_ds\n"));
891 				we_own_it = 0;
892 			} else {
893 				/* Force later arrivals to wait for our
894 				   request */
895 				DPRINTF(("we own the msqid_ds\n"));
896 				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
897 				we_own_it = 1;
898 			}
899 			DPRINTF(("msgsnd:  goodnight\n"));
900 			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
901 			    "msgsnd", hz);
902 			DPRINTF(("msgsnd:  good morning, error=%d\n", error));
903 			if (we_own_it)
904 				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
905 			if (error == EWOULDBLOCK) {
906 				DPRINTF(("msgsnd:  timed out\n"));
907 				continue;
908 			}
909 			if (error != 0) {
910 				DPRINTF(("msgsnd:  interrupted system call\n"));
911 				error = EINTR;
912 				goto done3;
913 			}
914 
915 			/*
916 			 * Make sure that the msq queue still exists
917 			 */
918 
919 			if (msqkptr->u.msg_qbytes == 0) {
920 				DPRINTF(("msqid deleted\n"));
921 				error = EIDRM;
922 				goto done3;
923 			}
924 
925 		} else {
926 			DPRINTF(("got all the resources that we need\n"));
927 			break;
928 		}
929 	}
930 
931 	/*
932 	 * We have the resources that we need.
933 	 * Make sure!
934 	 */
935 
936 	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
937 		panic("msg_perm.mode & MSG_LOCKED");
938 	if (segs_needed > nfree_msgmaps)
939 		panic("segs_needed > nfree_msgmaps");
940 	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
941 		panic("msgsz + msg_cbytes > msg_qbytes");
942 	if (free_msghdrs == NULL)
943 		panic("no more msghdrs");
944 
945 	/*
946 	 * Re-lock the msqid_ds in case we page-fault when copying in the
947 	 * message
948 	 */
949 
950 	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
951 		panic("msqid_ds is already locked");
952 	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
953 
954 	/*
955 	 * Allocate a message header
956 	 */
957 
958 	msghdr = free_msghdrs;
959 	free_msghdrs = msghdr->msg_next;
960 	msghdr->msg_spot = -1;
961 	msghdr->msg_ts = msgsz;
962 	msghdr->msg_type = mtype;
963 #ifdef MAC
964 	/*
965 	 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
966 	 * immediately?  Or, should it be checked just before the msg is
967 	 * enqueued in the msgq (as it is done now)?
968 	 */
969 	mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
970 #endif
971 
972 	/*
973 	 * Allocate space for the message
974 	 */
975 
976 	while (segs_needed > 0) {
977 		if (nfree_msgmaps <= 0)
978 			panic("not enough msgmaps");
979 		if (free_msgmaps == -1)
980 			panic("nil free_msgmaps");
981 		next = free_msgmaps;
982 		if (next <= -1)
983 			panic("next too low #1");
984 		if (next >= msginfo.msgseg)
985 			panic("next out of range #1");
986 		DPRINTF(("allocating segment %d to message\n", next));
987 		free_msgmaps = msgmaps[next].next;
988 		nfree_msgmaps--;
989 		msgmaps[next].next = msghdr->msg_spot;
990 		msghdr->msg_spot = next;
991 		segs_needed--;
992 	}
993 
994 	/*
995 	 * Validate the message type
996 	 */
997 
998 	if (msghdr->msg_type < 1) {
999 		msg_freehdr(msghdr);
1000 		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1001 		wakeup(msqkptr);
1002 		DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
1003 		error = EINVAL;
1004 		goto done3;
1005 	}
1006 
1007 	/*
1008 	 * Copy in the message body
1009 	 */
1010 
1011 	next = msghdr->msg_spot;
1012 	while (msgsz > 0) {
1013 		size_t tlen;
1014 		if (msgsz > msginfo.msgssz)
1015 			tlen = msginfo.msgssz;
1016 		else
1017 			tlen = msgsz;
1018 		if (next <= -1)
1019 			panic("next too low #2");
1020 		if (next >= msginfo.msgseg)
1021 			panic("next out of range #2");
1022 		mtx_unlock(&msq_mtx);
1023 		if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
1024 		    tlen)) != 0) {
1025 			mtx_lock(&msq_mtx);
1026 			DPRINTF(("error %d copying in message segment\n",
1027 			    error));
1028 			msg_freehdr(msghdr);
1029 			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1030 			wakeup(msqkptr);
1031 			goto done3;
1032 		}
1033 		mtx_lock(&msq_mtx);
1034 		msgsz -= tlen;
1035 		msgp = (const char *)msgp + tlen;
1036 		next = msgmaps[next].next;
1037 	}
1038 	if (next != -1)
1039 		panic("didn't use all the msg segments");
1040 
1041 	/*
1042 	 * We've got the message.  Unlock the msqid_ds.
1043 	 */
1044 
1045 	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1046 
1047 	/*
1048 	 * Make sure that the msqid_ds is still allocated.
1049 	 */
1050 
1051 	if (msqkptr->u.msg_qbytes == 0) {
1052 		msg_freehdr(msghdr);
1053 		wakeup(msqkptr);
1054 		error = EIDRM;
1055 		goto done3;
1056 	}
1057 
1058 #ifdef MAC
1059 	/*
1060 	 * Note: Since the task/thread allocates the msghdr and usually
1061 	 * primes it with its own MAC label, for a majority of policies, it
1062 	 * won't be necessary to check whether the msghdr has access
1063 	 * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
1064 	 * suffice in that case.  However, this hook may be required where
1065 	 * individual policies derive a non-identical label for the msghdr
1066 	 * from the current thread label and may want to check the msghdr
1067 	 * enqueue permissions, along with read/write permissions to the
1068 	 * msgq.
1069 	 */
1070 	error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
1071 	if (error != 0) {
1072 		msg_freehdr(msghdr);
1073 		wakeup(msqkptr);
1074 		goto done3;
1075 	}
1076 #endif
1077 
1078 	/*
1079 	 * Put the message into the queue
1080 	 */
1081 	if (msqkptr->u.msg_first == NULL) {
1082 		msqkptr->u.msg_first = msghdr;
1083 		msqkptr->u.msg_last = msghdr;
1084 	} else {
1085 		msqkptr->u.msg_last->msg_next = msghdr;
1086 		msqkptr->u.msg_last = msghdr;
1087 	}
1088 	msqkptr->u.msg_last->msg_next = NULL;
1089 
1090 	msqkptr->u.msg_cbytes += msghdr->msg_ts;
1091 	msqkptr->u.msg_qnum++;
1092 	msqkptr->u.msg_lspid = td->td_proc->p_pid;
1093 	msqkptr->u.msg_stime = time_second;
1094 
1095 	wakeup(msqkptr);
1096 	td->td_retval[0] = 0;
1097 done3:
1098 #ifdef RACCT
1099 	if (racct_enable && error != 0) {
1100 		PROC_LOCK(td->td_proc);
1101 		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1102 		racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1103 		PROC_UNLOCK(td->td_proc);
1104 	}
1105 #endif
1106 done2:
1107 	mtx_unlock(&msq_mtx);
1108 	return (error);
1109 }
1110 
1111 int
1112 sys_msgsnd(td, uap)
1113 	struct thread *td;
1114 	register struct msgsnd_args *uap;
1115 {
1116 	int error;
1117 	long mtype;
1118 
1119 	DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1120 	    uap->msgsz, uap->msgflg));
1121 
1122 	if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1123 		DPRINTF(("error %d copying the message type\n", error));
1124 		return (error);
1125 	}
1126 	return (kern_msgsnd(td, uap->msqid,
1127 	    (const char *)uap->msgp + sizeof(mtype),
1128 	    uap->msgsz, uap->msgflg, mtype));
1129 }
1130 
1131 #ifndef _SYS_SYSPROTO_H_
1132 struct msgrcv_args {
1133 	int	msqid;
1134 	void	*msgp;
1135 	size_t	msgsz;
1136 	long	msgtyp;
1137 	int	msgflg;
1138 };
1139 #endif
1140 int
1141 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1142 	struct thread *td;
1143 	int msqid;
1144 	void *msgp;	/* XXX msgp is actually mtext. */
1145 	size_t msgsz;
1146 	long msgtyp;
1147 	int msgflg;
1148 	long *mtype;
1149 {
1150 	size_t len;
1151 	register struct msqid_kernel *msqkptr;
1152 	register struct msg *msghdr;
1153 	struct prison *rpr;
1154 	int msqix, error = 0;
1155 	short next;
1156 
1157 	rpr = msg_find_prison(td->td_ucred);
1158 	if (rpr == NULL)
1159 		return (ENOSYS);
1160 
1161 	msqix = IPCID_TO_IX(msqid);
1162 
1163 	if (msqix < 0 || msqix >= msginfo.msgmni) {
1164 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1165 		    msginfo.msgmni));
1166 		return (EINVAL);
1167 	}
1168 
1169 	msqkptr = &msqids[msqix];
1170 	mtx_lock(&msq_mtx);
1171 	if (msqkptr->u.msg_qbytes == 0) {
1172 		DPRINTF(("no such message queue id\n"));
1173 		error = EINVAL;
1174 		goto done2;
1175 	}
1176 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1177 		DPRINTF(("wrong sequence number\n"));
1178 		error = EINVAL;
1179 		goto done2;
1180 	}
1181 
1182 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
1183 		DPRINTF(("requester can't see prison\n"));
1184 		goto done2;
1185 	}
1186 
1187 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1188 		DPRINTF(("requester doesn't have read access\n"));
1189 		goto done2;
1190 	}
1191 
1192 #ifdef MAC
1193 	error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1194 	if (error != 0)
1195 		goto done2;
1196 #endif
1197 
1198 	msghdr = NULL;
1199 	while (msghdr == NULL) {
1200 		if (msgtyp == 0) {
1201 			msghdr = msqkptr->u.msg_first;
1202 			if (msghdr != NULL) {
1203 				if (msgsz < msghdr->msg_ts &&
1204 				    (msgflg & MSG_NOERROR) == 0) {
1205 					DPRINTF(("first message on the queue "
1206 					    "is too big (want %zu, got %d)\n",
1207 					    msgsz, msghdr->msg_ts));
1208 					error = E2BIG;
1209 					goto done2;
1210 				}
1211 #ifdef MAC
1212 				error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1213 				    msghdr);
1214 				if (error != 0)
1215 					goto done2;
1216 #endif
1217 				if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1218 					msqkptr->u.msg_first = NULL;
1219 					msqkptr->u.msg_last = NULL;
1220 				} else {
1221 					msqkptr->u.msg_first = msghdr->msg_next;
1222 					if (msqkptr->u.msg_first == NULL)
1223 						panic("msg_first/last screwed up #1");
1224 				}
1225 			}
1226 		} else {
1227 			struct msg *previous;
1228 			struct msg **prev;
1229 
1230 			previous = NULL;
1231 			prev = &(msqkptr->u.msg_first);
1232 			while ((msghdr = *prev) != NULL) {
1233 				/*
1234 				 * Is this message's type an exact match or is
1235 				 * this message's type less than or equal to
1236 				 * the absolute value of a negative msgtyp?
1237 				 * Note that the second half of this test can
1238 				 * NEVER be true if msgtyp is positive since
1239 				 * msg_type is always positive!
1240 				 */
1241 
1242 				if (msgtyp == msghdr->msg_type ||
1243 				    msghdr->msg_type <= -msgtyp) {
1244 					DPRINTF(("found message type %ld, "
1245 					    "requested %ld\n",
1246 					    msghdr->msg_type, msgtyp));
1247 					if (msgsz < msghdr->msg_ts &&
1248 					    (msgflg & MSG_NOERROR) == 0) {
1249 						DPRINTF(("requested message "
1250 						    "on the queue is too big "
1251 						    "(want %zu, got %hu)\n",
1252 						    msgsz, msghdr->msg_ts));
1253 						error = E2BIG;
1254 						goto done2;
1255 					}
1256 #ifdef MAC
1257 					error = mac_sysvmsq_check_msgrcv(
1258 					    td->td_ucred, msghdr);
1259 					if (error != 0)
1260 						goto done2;
1261 #endif
1262 					*prev = msghdr->msg_next;
1263 					if (msghdr == msqkptr->u.msg_last) {
1264 						if (previous == NULL) {
1265 							if (prev !=
1266 							    &msqkptr->u.msg_first)
1267 								panic("msg_first/last screwed up #2");
1268 							msqkptr->u.msg_first =
1269 							    NULL;
1270 							msqkptr->u.msg_last =
1271 							    NULL;
1272 						} else {
1273 							if (prev ==
1274 							    &msqkptr->u.msg_first)
1275 								panic("msg_first/last screwed up #3");
1276 							msqkptr->u.msg_last =
1277 							    previous;
1278 						}
1279 					}
1280 					break;
1281 				}
1282 				previous = msghdr;
1283 				prev = &(msghdr->msg_next);
1284 			}
1285 		}
1286 
1287 		/*
1288 		 * We've either extracted the msghdr for the appropriate
1289 		 * message or there isn't one.
1290 		 * If there is one then bail out of this loop.
1291 		 */
1292 
1293 		if (msghdr != NULL)
1294 			break;
1295 
1296 		/*
1297 		 * Hmph!  No message found.  Does the user want to wait?
1298 		 */
1299 
1300 		if ((msgflg & IPC_NOWAIT) != 0) {
1301 			DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1302 			    msgtyp));
1303 			/* The SVID says to return ENOMSG. */
1304 			error = ENOMSG;
1305 			goto done2;
1306 		}
1307 
1308 		/*
1309 		 * Wait for something to happen
1310 		 */
1311 
1312 		DPRINTF(("msgrcv:  goodnight\n"));
1313 		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1314 		    "msgrcv", 0);
1315 		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1316 
1317 		if (error != 0) {
1318 			DPRINTF(("msgrcv:  interrupted system call\n"));
1319 			error = EINTR;
1320 			goto done2;
1321 		}
1322 
1323 		/*
1324 		 * Make sure that the msq queue still exists
1325 		 */
1326 
1327 		if (msqkptr->u.msg_qbytes == 0 ||
1328 		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1329 			DPRINTF(("msqid deleted\n"));
1330 			error = EIDRM;
1331 			goto done2;
1332 		}
1333 	}
1334 
1335 	/*
1336 	 * Return the message to the user.
1337 	 *
1338 	 * First, do the bookkeeping (before we risk being interrupted).
1339 	 */
1340 
1341 	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1342 	msqkptr->u.msg_qnum--;
1343 	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1344 	msqkptr->u.msg_rtime = time_second;
1345 
1346 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1347 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1348 
1349 	/*
1350 	 * Make msgsz the actual amount that we'll be returning.
1351 	 * Note that this effectively truncates the message if it is too long
1352 	 * (since msgsz is never increased).
1353 	 */
1354 
1355 	DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1356 	    msghdr->msg_ts));
1357 	if (msgsz > msghdr->msg_ts)
1358 		msgsz = msghdr->msg_ts;
1359 	*mtype = msghdr->msg_type;
1360 
1361 	/*
1362 	 * Return the segments to the user
1363 	 */
1364 
1365 	next = msghdr->msg_spot;
1366 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1367 		size_t tlen;
1368 
1369 		if (msgsz - len > msginfo.msgssz)
1370 			tlen = msginfo.msgssz;
1371 		else
1372 			tlen = msgsz - len;
1373 		if (next <= -1)
1374 			panic("next too low #3");
1375 		if (next >= msginfo.msgseg)
1376 			panic("next out of range #3");
1377 		mtx_unlock(&msq_mtx);
1378 		error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1379 		mtx_lock(&msq_mtx);
1380 		if (error != 0) {
1381 			DPRINTF(("error (%d) copying out message segment\n",
1382 			    error));
1383 			msg_freehdr(msghdr);
1384 			wakeup(msqkptr);
1385 			goto done2;
1386 		}
1387 		msgp = (char *)msgp + tlen;
1388 		next = msgmaps[next].next;
1389 	}
1390 
1391 	/*
1392 	 * Done, return the actual number of bytes copied out.
1393 	 */
1394 
1395 	msg_freehdr(msghdr);
1396 	wakeup(msqkptr);
1397 	td->td_retval[0] = msgsz;
1398 done2:
1399 	mtx_unlock(&msq_mtx);
1400 	return (error);
1401 }
1402 
1403 int
1404 sys_msgrcv(td, uap)
1405 	struct thread *td;
1406 	register struct msgrcv_args *uap;
1407 {
1408 	int error;
1409 	long mtype;
1410 
1411 	DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1412 	    uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1413 
1414 	if ((error = kern_msgrcv(td, uap->msqid,
1415 	    (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1416 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1417 		return (error);
1418 	if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1419 		DPRINTF(("error %d copying the message type\n", error));
1420 	return (error);
1421 }
1422 
1423 static int
1424 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1425 {
1426 	struct sbuf sb;
1427 	struct msqid_kernel tmp, empty;
1428 	struct msqid_kernel *msqkptr;
1429 	struct prison *rpr;
1430 	int error, i;
1431 
1432 	error = sysctl_wire_old_buffer(req, 0);
1433 	if (error != 0)
1434 		goto done;
1435 	rpr = msg_find_prison(req->td->td_ucred);
1436 	sbuf_new_for_sysctl(&sb, NULL, sizeof(struct msqid_kernel) *
1437 	    msginfo.msgmni, req);
1438 
1439 	bzero(&empty, sizeof(empty));
1440 	for (i = 0; i < msginfo.msgmni; i++) {
1441 		msqkptr = &msqids[i];
1442 		if (msqkptr->u.msg_qbytes == 0 || rpr == NULL ||
1443 		    msq_prison_cansee(rpr, msqkptr) != 0) {
1444 			msqkptr = &empty;
1445 		} else if (req->td->td_ucred->cr_prison !=
1446 		    msqkptr->cred->cr_prison) {
1447 			bcopy(msqkptr, &tmp, sizeof(tmp));
1448 			msqkptr = &tmp;
1449 			msqkptr->u.msg_perm.key = IPC_PRIVATE;
1450 		}
1451 
1452 		sbuf_bcat(&sb, msqkptr, sizeof(*msqkptr));
1453 	}
1454 	error = sbuf_finish(&sb);
1455 	sbuf_delete(&sb);
1456 
1457 done:
1458 	return (error);
1459 }
1460 
1461 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1462     "Maximum message size");
1463 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1464     "Number of message queue identifiers");
1465 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1466     "Maximum number of bytes in a queue");
1467 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1468     "Maximum number of messages in the system");
1469 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1470     "Size of a message segment");
1471 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1472     "Number of message segments");
1473 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLTYPE_OPAQUE | CTLFLAG_RD,
1474     NULL, 0, sysctl_msqids, "", "Message queue IDs");
1475 
1476 static int
1477 msg_prison_check(void *obj, void *data)
1478 {
1479 	struct prison *pr = obj;
1480 	struct prison *prpr;
1481 	struct vfsoptlist *opts = data;
1482 	int error, jsys;
1483 
1484 	/*
1485 	 * sysvmsg is a jailsys integer.
1486 	 * It must be "disable" if the parent jail is disabled.
1487 	 */
1488 	error = vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1489 	if (error != ENOENT) {
1490 		if (error != 0)
1491 			return (error);
1492 		switch (jsys) {
1493 		case JAIL_SYS_DISABLE:
1494 			break;
1495 		case JAIL_SYS_NEW:
1496 		case JAIL_SYS_INHERIT:
1497 			prison_lock(pr->pr_parent);
1498 			prpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1499 			prison_unlock(pr->pr_parent);
1500 			if (prpr == NULL)
1501 				return (EPERM);
1502 			break;
1503 		default:
1504 			return (EINVAL);
1505 		}
1506 	}
1507 
1508 	return (0);
1509 }
1510 
1511 static int
1512 msg_prison_set(void *obj, void *data)
1513 {
1514 	struct prison *pr = obj;
1515 	struct prison *tpr, *orpr, *nrpr, *trpr;
1516 	struct vfsoptlist *opts = data;
1517 	void *rsv;
1518 	int jsys, descend;
1519 
1520 	/*
1521 	 * sysvmsg controls which jail is the root of the associated msgs (this
1522 	 * jail or same as the parent), or if the feature is available at all.
1523 	 */
1524 	if (vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys)) == ENOENT)
1525 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1526 		    ? JAIL_SYS_INHERIT
1527 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1528 		    ? JAIL_SYS_DISABLE
1529 		    : -1;
1530 	if (jsys == JAIL_SYS_DISABLE) {
1531 		prison_lock(pr);
1532 		orpr = osd_jail_get(pr, msg_prison_slot);
1533 		if (orpr != NULL)
1534 			osd_jail_del(pr, msg_prison_slot);
1535 		prison_unlock(pr);
1536 		if (orpr != NULL) {
1537 			if (orpr == pr)
1538 				msg_prison_cleanup(pr);
1539 			/* Disable all child jails as well. */
1540 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1541 				prison_lock(tpr);
1542 				trpr = osd_jail_get(tpr, msg_prison_slot);
1543 				if (trpr != NULL) {
1544 					osd_jail_del(tpr, msg_prison_slot);
1545 					prison_unlock(tpr);
1546 					if (trpr == tpr)
1547 						msg_prison_cleanup(tpr);
1548 				} else {
1549 					prison_unlock(tpr);
1550 					descend = 0;
1551 				}
1552 			}
1553 		}
1554 	} else if (jsys != -1) {
1555 		if (jsys == JAIL_SYS_NEW)
1556 			nrpr = pr;
1557 		else {
1558 			prison_lock(pr->pr_parent);
1559 			nrpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1560 			prison_unlock(pr->pr_parent);
1561 		}
1562 		rsv = osd_reserve(msg_prison_slot);
1563 		prison_lock(pr);
1564 		orpr = osd_jail_get(pr, msg_prison_slot);
1565 		if (orpr != nrpr)
1566 			(void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
1567 			    nrpr);
1568 		else
1569 			osd_free_reserved(rsv);
1570 		prison_unlock(pr);
1571 		if (orpr != nrpr) {
1572 			if (orpr == pr)
1573 				msg_prison_cleanup(pr);
1574 			if (orpr != NULL) {
1575 				/* Change child jails matching the old root, */
1576 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1577 					prison_lock(tpr);
1578 					trpr = osd_jail_get(tpr,
1579 					    msg_prison_slot);
1580 					if (trpr == orpr) {
1581 						(void)osd_jail_set(tpr,
1582 						    msg_prison_slot, nrpr);
1583 						prison_unlock(tpr);
1584 						if (trpr == tpr)
1585 							msg_prison_cleanup(tpr);
1586 					} else {
1587 						prison_unlock(tpr);
1588 						descend = 0;
1589 					}
1590 				}
1591 			}
1592 		}
1593 	}
1594 
1595 	return (0);
1596 }
1597 
1598 static int
1599 msg_prison_get(void *obj, void *data)
1600 {
1601 	struct prison *pr = obj;
1602 	struct prison *rpr;
1603 	struct vfsoptlist *opts = data;
1604 	int error, jsys;
1605 
1606 	/* Set sysvmsg based on the jail's root prison. */
1607 	prison_lock(pr);
1608 	rpr = osd_jail_get(pr, msg_prison_slot);
1609 	prison_unlock(pr);
1610 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1611 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1612 	error = vfs_setopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1613 	if (error == ENOENT)
1614 		error = 0;
1615 	return (error);
1616 }
1617 
1618 static int
1619 msg_prison_remove(void *obj, void *data __unused)
1620 {
1621 	struct prison *pr = obj;
1622 	struct prison *rpr;
1623 
1624 	prison_lock(pr);
1625 	rpr = osd_jail_get(pr, msg_prison_slot);
1626 	prison_unlock(pr);
1627 	if (rpr == pr)
1628 		msg_prison_cleanup(pr);
1629 	return (0);
1630 }
1631 
1632 static void
1633 msg_prison_cleanup(struct prison *pr)
1634 {
1635 	struct msqid_kernel *msqkptr;
1636 	int i;
1637 
1638 	/* Remove any msqs that belong to this jail. */
1639 	mtx_lock(&msq_mtx);
1640 	for (i = 0; i < msginfo.msgmni; i++) {
1641 		msqkptr = &msqids[i];
1642 		if (msqkptr->u.msg_qbytes != 0 &&
1643 		    msqkptr->cred != NULL && msqkptr->cred->cr_prison == pr)
1644 			msq_remove(msqkptr);
1645 	}
1646 	mtx_unlock(&msq_mtx);
1647 }
1648 
1649 SYSCTL_JAIL_PARAM_SYS_NODE(sysvmsg, CTLFLAG_RW, "SYSV message queues");
1650 
1651 #ifdef COMPAT_FREEBSD32
1652 int
1653 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1654 {
1655 
1656 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1657     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1658 	switch (uap->which) {
1659 	case 0:
1660 		return (freebsd7_freebsd32_msgctl(td,
1661 		    (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1662 	case 2:
1663 		return (freebsd32_msgsnd(td,
1664 		    (struct freebsd32_msgsnd_args *)&uap->a2));
1665 	case 3:
1666 		return (freebsd32_msgrcv(td,
1667 		    (struct freebsd32_msgrcv_args *)&uap->a2));
1668 	default:
1669 		return (sys_msgsys(td, (struct msgsys_args *)uap));
1670 	}
1671 #else
1672 	return (nosys(td, NULL));
1673 #endif
1674 }
1675 
1676 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1677     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1678 int
1679 freebsd7_freebsd32_msgctl(struct thread *td,
1680     struct freebsd7_freebsd32_msgctl_args *uap)
1681 {
1682 	struct msqid_ds msqbuf;
1683 	struct msqid_ds32_old msqbuf32;
1684 	int error;
1685 
1686 	if (uap->cmd == IPC_SET) {
1687 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1688 		if (error)
1689 			return (error);
1690 		freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1691 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1692 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1693 		CP(msqbuf32, msqbuf, msg_cbytes);
1694 		CP(msqbuf32, msqbuf, msg_qnum);
1695 		CP(msqbuf32, msqbuf, msg_qbytes);
1696 		CP(msqbuf32, msqbuf, msg_lspid);
1697 		CP(msqbuf32, msqbuf, msg_lrpid);
1698 		CP(msqbuf32, msqbuf, msg_stime);
1699 		CP(msqbuf32, msqbuf, msg_rtime);
1700 		CP(msqbuf32, msqbuf, msg_ctime);
1701 	}
1702 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1703 	if (error)
1704 		return (error);
1705 	if (uap->cmd == IPC_STAT) {
1706 		bzero(&msqbuf32, sizeof(msqbuf32));
1707 		freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1708 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1709 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1710 		CP(msqbuf, msqbuf32, msg_cbytes);
1711 		CP(msqbuf, msqbuf32, msg_qnum);
1712 		CP(msqbuf, msqbuf32, msg_qbytes);
1713 		CP(msqbuf, msqbuf32, msg_lspid);
1714 		CP(msqbuf, msqbuf32, msg_lrpid);
1715 		CP(msqbuf, msqbuf32, msg_stime);
1716 		CP(msqbuf, msqbuf32, msg_rtime);
1717 		CP(msqbuf, msqbuf32, msg_ctime);
1718 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1719 	}
1720 	return (error);
1721 }
1722 #endif
1723 
1724 int
1725 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1726 {
1727 	struct msqid_ds msqbuf;
1728 	struct msqid_ds32 msqbuf32;
1729 	int error;
1730 
1731 	if (uap->cmd == IPC_SET) {
1732 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1733 		if (error)
1734 			return (error);
1735 		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1736 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1737 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1738 		CP(msqbuf32, msqbuf, msg_cbytes);
1739 		CP(msqbuf32, msqbuf, msg_qnum);
1740 		CP(msqbuf32, msqbuf, msg_qbytes);
1741 		CP(msqbuf32, msqbuf, msg_lspid);
1742 		CP(msqbuf32, msqbuf, msg_lrpid);
1743 		CP(msqbuf32, msqbuf, msg_stime);
1744 		CP(msqbuf32, msqbuf, msg_rtime);
1745 		CP(msqbuf32, msqbuf, msg_ctime);
1746 	}
1747 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1748 	if (error)
1749 		return (error);
1750 	if (uap->cmd == IPC_STAT) {
1751 		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1752 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1753 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1754 		CP(msqbuf, msqbuf32, msg_cbytes);
1755 		CP(msqbuf, msqbuf32, msg_qnum);
1756 		CP(msqbuf, msqbuf32, msg_qbytes);
1757 		CP(msqbuf, msqbuf32, msg_lspid);
1758 		CP(msqbuf, msqbuf32, msg_lrpid);
1759 		CP(msqbuf, msqbuf32, msg_stime);
1760 		CP(msqbuf, msqbuf32, msg_rtime);
1761 		CP(msqbuf, msqbuf32, msg_ctime);
1762 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1763 	}
1764 	return (error);
1765 }
1766 
1767 int
1768 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1769 {
1770 	const void *msgp;
1771 	long mtype;
1772 	int32_t mtype32;
1773 	int error;
1774 
1775 	msgp = PTRIN(uap->msgp);
1776 	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1777 		return (error);
1778 	mtype = mtype32;
1779 	return (kern_msgsnd(td, uap->msqid,
1780 	    (const char *)msgp + sizeof(mtype32),
1781 	    uap->msgsz, uap->msgflg, mtype));
1782 }
1783 
1784 int
1785 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1786 {
1787 	void *msgp;
1788 	long mtype;
1789 	int32_t mtype32;
1790 	int error;
1791 
1792 	msgp = PTRIN(uap->msgp);
1793 	if ((error = kern_msgrcv(td, uap->msqid,
1794 	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1795 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1796 		return (error);
1797 	mtype32 = (int32_t)mtype;
1798 	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1799 }
1800 #endif
1801 
1802 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1803     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1804 
1805 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1806 static sy_call_t *msgcalls[] = {
1807 	(sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
1808 	(sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
1809 };
1810 
1811 /*
1812  * Entry point for all MSG calls.
1813  */
1814 int
1815 sys_msgsys(td, uap)
1816 	struct thread *td;
1817 	/* XXX actually varargs. */
1818 	struct msgsys_args /* {
1819 		int	which;
1820 		int	a2;
1821 		int	a3;
1822 		int	a4;
1823 		int	a5;
1824 		int	a6;
1825 	} */ *uap;
1826 {
1827 	int error;
1828 
1829 	if (uap->which < 0 || uap->which >= nitems(msgcalls))
1830 		return (EINVAL);
1831 	error = (*msgcalls[uap->which])(td, &uap->a2);
1832 	return (error);
1833 }
1834 
1835 #ifndef CP
1836 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1837 #endif
1838 
1839 #ifndef _SYS_SYSPROTO_H_
1840 struct freebsd7_msgctl_args {
1841 	int	msqid;
1842 	int	cmd;
1843 	struct	msqid_ds_old *buf;
1844 };
1845 #endif
1846 int
1847 freebsd7_msgctl(td, uap)
1848 	struct thread *td;
1849 	struct freebsd7_msgctl_args *uap;
1850 {
1851 	struct msqid_ds_old msqold;
1852 	struct msqid_ds msqbuf;
1853 	int error;
1854 
1855 	DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1856 	    uap->buf));
1857 	if (uap->cmd == IPC_SET) {
1858 		error = copyin(uap->buf, &msqold, sizeof(msqold));
1859 		if (error)
1860 			return (error);
1861 		ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1862 		CP(msqold, msqbuf, msg_first);
1863 		CP(msqold, msqbuf, msg_last);
1864 		CP(msqold, msqbuf, msg_cbytes);
1865 		CP(msqold, msqbuf, msg_qnum);
1866 		CP(msqold, msqbuf, msg_qbytes);
1867 		CP(msqold, msqbuf, msg_lspid);
1868 		CP(msqold, msqbuf, msg_lrpid);
1869 		CP(msqold, msqbuf, msg_stime);
1870 		CP(msqold, msqbuf, msg_rtime);
1871 		CP(msqold, msqbuf, msg_ctime);
1872 	}
1873 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1874 	if (error)
1875 		return (error);
1876 	if (uap->cmd == IPC_STAT) {
1877 		bzero(&msqold, sizeof(msqold));
1878 		ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1879 		CP(msqbuf, msqold, msg_first);
1880 		CP(msqbuf, msqold, msg_last);
1881 		CP(msqbuf, msqold, msg_cbytes);
1882 		CP(msqbuf, msqold, msg_qnum);
1883 		CP(msqbuf, msqold, msg_qbytes);
1884 		CP(msqbuf, msqold, msg_lspid);
1885 		CP(msqbuf, msqold, msg_lrpid);
1886 		CP(msqbuf, msqold, msg_stime);
1887 		CP(msqbuf, msqold, msg_rtime);
1888 		CP(msqbuf, msqold, msg_ctime);
1889 		error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1890 	}
1891 	return (error);
1892 }
1893 
1894 #undef CP
1895 
1896 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1897 	   COMPAT_FREEBSD7 */
1898