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