xref: /freebsd/sys/kern/sysv_msg.c (revision cac6fd1154811b24f3899a8f9e07c0140542a90d)
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  * Copyright (c) 2016-2017 Robert N. M. Watson
22  * All rights reserved.
23  *
24  * This software was developed for the FreeBSD Project in part by McAfee
25  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
26  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
27  * program.
28  *
29  * Portions of this software were developed by BAE Systems, the University of
30  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
31  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
32  * Computing (TC) research program.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58 
59 #include "opt_compat.h"
60 #include "opt_sysvipc.h"
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/sysproto.h>
65 #include <sys/kernel.h>
66 #include <sys/priv.h>
67 #include <sys/proc.h>
68 #include <sys/lock.h>
69 #include <sys/mutex.h>
70 #include <sys/module.h>
71 #include <sys/mount.h>
72 #include <sys/msg.h>
73 #include <sys/racct.h>
74 #include <sys/sx.h>
75 #include <sys/syscall.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysent.h>
78 #include <sys/sysctl.h>
79 #include <sys/malloc.h>
80 #include <sys/jail.h>
81 
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
84 
85 FEATURE(sysv_msg, "System V message queues support");
86 
87 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
88 
89 static int msginit(void);
90 static int msgunload(void);
91 static int sysvmsg_modload(struct module *, int, void *);
92 static void msq_remove(struct msqid_kernel *);
93 static struct prison *msg_find_prison(struct ucred *);
94 static int msq_prison_cansee(struct prison *, struct msqid_kernel *);
95 static int msg_prison_check(void *, void *);
96 static int msg_prison_set(void *, void *);
97 static int msg_prison_get(void *, void *);
98 static int msg_prison_remove(void *, void *);
99 static void msg_prison_cleanup(struct prison *);
100 
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);
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 
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 	AUDIT_ARG_SVIPC_CMD(cmd);
518 	AUDIT_ARG_SVIPC_ID(msqid);
519 	msqix = IPCID_TO_IX(msqid);
520 
521 	if (msqix < 0 || msqix >= msginfo.msgmni) {
522 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
523 		    msginfo.msgmni));
524 		return (EINVAL);
525 	}
526 
527 	msqkptr = &msqids[msqix];
528 
529 	mtx_lock(&msq_mtx);
530 	if (msqkptr->u.msg_qbytes == 0) {
531 		DPRINTF(("no such msqid\n"));
532 		error = EINVAL;
533 		goto done2;
534 	}
535 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
536 		DPRINTF(("wrong sequence number\n"));
537 		error = EINVAL;
538 		goto done2;
539 	}
540 
541 	error = msq_prison_cansee(rpr, msqkptr);
542 	if (error != 0) {
543 		DPRINTF(("requester can't see prison\n"));
544 		goto done2;
545 	}
546 
547 #ifdef MAC
548 	error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
549 	if (error != 0)
550 		goto done2;
551 #endif
552 
553 	error = 0;
554 	rval = 0;
555 
556 	switch (cmd) {
557 
558 	case IPC_RMID:
559 	{
560 #ifdef MAC
561 		struct msg *msghdr;
562 #endif
563 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
564 			goto done2;
565 
566 #ifdef MAC
567 		/*
568 		 * Check that the thread has MAC access permissions to
569 		 * individual msghdrs.  Note: We need to do this in a
570 		 * separate loop because the actual loop alters the
571 		 * msq/msghdr info as it progresses, and there is no going
572 		 * back if half the way through we discover that the
573 		 * thread cannot free a certain msghdr.  The msq will get
574 		 * into an inconsistent state.
575 		 */
576 		for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
577 		    msghdr = msghdr->msg_next) {
578 			error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
579 			if (error != 0)
580 				goto done2;
581 		}
582 #endif
583 
584 		msq_remove(msqkptr);
585 	}
586 
587 		break;
588 
589 	case IPC_SET:
590 		AUDIT_ARG_SVIPC_PERM(&msqbuf->msg_perm);
591 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
592 			goto done2;
593 		if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
594 			error = priv_check(td, PRIV_IPC_MSGSIZE);
595 			if (error)
596 				goto done2;
597 		}
598 		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
599 			DPRINTF(("can't increase msg_qbytes beyond %d"
600 			    "(truncating)\n", msginfo.msgmnb));
601 			msqbuf->msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
602 		}
603 		if (msqbuf->msg_qbytes == 0) {
604 			DPRINTF(("can't reduce msg_qbytes to 0\n"));
605 			error = EINVAL;		/* non-standard errno! */
606 			goto done2;
607 		}
608 		msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid;	/* change the owner */
609 		msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid;	/* change the owner */
610 		msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
611 		    (msqbuf->msg_perm.mode & 0777);
612 		msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
613 		msqkptr->u.msg_ctime = time_second;
614 		break;
615 
616 	case IPC_STAT:
617 		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
618 			DPRINTF(("requester doesn't have read access\n"));
619 			goto done2;
620 		}
621 		*msqbuf = msqkptr->u;
622 		if (td->td_ucred->cr_prison != msqkptr->cred->cr_prison)
623 			msqbuf->msg_perm.key = IPC_PRIVATE;
624 		break;
625 
626 	default:
627 		DPRINTF(("invalid command %d\n", cmd));
628 		error = EINVAL;
629 		goto done2;
630 	}
631 
632 	if (error == 0)
633 		td->td_retval[0] = rval;
634 done2:
635 	mtx_unlock(&msq_mtx);
636 	return (error);
637 }
638 
639 #ifndef _SYS_SYSPROTO_H_
640 struct msgget_args {
641 	key_t	key;
642 	int	msgflg;
643 };
644 #endif
645 
646 int
647 sys_msgget(td, uap)
648 	struct thread *td;
649 	register struct msgget_args *uap;
650 {
651 	int msqid, error = 0;
652 	int key = uap->key;
653 	int msgflg = uap->msgflg;
654 	struct ucred *cred = td->td_ucred;
655 	register struct msqid_kernel *msqkptr = NULL;
656 
657 	DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
658 
659 	if (msg_find_prison(cred) == NULL)
660 		return (ENOSYS);
661 
662 	mtx_lock(&msq_mtx);
663 	if (key != IPC_PRIVATE) {
664 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
665 			msqkptr = &msqids[msqid];
666 			if (msqkptr->u.msg_qbytes != 0 &&
667 			    msqkptr->cred != NULL &&
668 			    msqkptr->cred->cr_prison == cred->cr_prison &&
669 			    msqkptr->u.msg_perm.key == key)
670 				break;
671 		}
672 		if (msqid < msginfo.msgmni) {
673 			DPRINTF(("found public key\n"));
674 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
675 				DPRINTF(("not exclusive\n"));
676 				error = EEXIST;
677 				goto done2;
678 			}
679 			AUDIT_ARG_SVIPC_ID(IXSEQ_TO_IPCID(msqid,
680 			    msqkptr->u.msg_perm));
681 			if ((error = ipcperm(td, &msqkptr->u.msg_perm,
682 			    msgflg & 0700))) {
683 				DPRINTF(("requester doesn't have 0%o access\n",
684 				    msgflg & 0700));
685 				goto done2;
686 			}
687 #ifdef MAC
688 			error = mac_sysvmsq_check_msqget(cred, msqkptr);
689 			if (error != 0)
690 				goto done2;
691 #endif
692 			goto found;
693 		}
694 	}
695 
696 	DPRINTF(("need to allocate the msqid_ds\n"));
697 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
698 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
699 			/*
700 			 * Look for an unallocated and unlocked msqid_ds.
701 			 * msqid_ds's can be locked by msgsnd or msgrcv while
702 			 * they are copying the message in/out.  We can't
703 			 * re-use the entry until they release it.
704 			 */
705 			msqkptr = &msqids[msqid];
706 			if (msqkptr->u.msg_qbytes == 0 &&
707 			    (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
708 				break;
709 		}
710 		if (msqid == msginfo.msgmni) {
711 			DPRINTF(("no more msqid_ds's available\n"));
712 			error = ENOSPC;
713 			goto done2;
714 		}
715 #ifdef RACCT
716 		if (racct_enable) {
717 			PROC_LOCK(td->td_proc);
718 			error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
719 			PROC_UNLOCK(td->td_proc);
720 			if (error != 0) {
721 				error = ENOSPC;
722 				goto done2;
723 			}
724 		}
725 #endif
726 		DPRINTF(("msqid %d is available\n", msqid));
727 		msqkptr->u.msg_perm.key = key;
728 		msqkptr->u.msg_perm.cuid = cred->cr_uid;
729 		msqkptr->u.msg_perm.uid = cred->cr_uid;
730 		msqkptr->u.msg_perm.cgid = cred->cr_gid;
731 		msqkptr->u.msg_perm.gid = cred->cr_gid;
732 		msqkptr->u.msg_perm.mode = (msgflg & 0777);
733 		msqkptr->cred = crhold(cred);
734 		/* Make sure that the returned msqid is unique */
735 		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
736 		msqkptr->u.msg_first = NULL;
737 		msqkptr->u.msg_last = NULL;
738 		msqkptr->u.msg_cbytes = 0;
739 		msqkptr->u.msg_qnum = 0;
740 		msqkptr->u.msg_qbytes = msginfo.msgmnb;
741 		msqkptr->u.msg_lspid = 0;
742 		msqkptr->u.msg_lrpid = 0;
743 		msqkptr->u.msg_stime = 0;
744 		msqkptr->u.msg_rtime = 0;
745 		msqkptr->u.msg_ctime = time_second;
746 #ifdef MAC
747 		mac_sysvmsq_create(cred, msqkptr);
748 #endif
749 		AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
750 	} else {
751 		DPRINTF(("didn't find it and wasn't asked to create it\n"));
752 		error = ENOENT;
753 		goto done2;
754 	}
755 
756 found:
757 	/* Construct the unique msqid */
758 	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
759 done2:
760 	mtx_unlock(&msq_mtx);
761 	return (error);
762 }
763 
764 #ifndef _SYS_SYSPROTO_H_
765 struct msgsnd_args {
766 	int	msqid;
767 	const void	*msgp;
768 	size_t	msgsz;
769 	int	msgflg;
770 };
771 #endif
772 int
773 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
774 	struct thread *td;
775 	int msqid;
776 	const void *msgp;	/* XXX msgp is actually mtext. */
777 	size_t msgsz;
778 	int msgflg;
779 	long mtype;
780 {
781 	int msqix, segs_needed, error = 0;
782 	register struct msqid_kernel *msqkptr;
783 	register struct msg *msghdr;
784 	struct prison *rpr;
785 	short next;
786 #ifdef RACCT
787 	size_t saved_msgsz;
788 #endif
789 
790 	rpr = msg_find_prison(td->td_ucred);
791 	if (rpr == NULL)
792 		return (ENOSYS);
793 
794 	mtx_lock(&msq_mtx);
795 	AUDIT_ARG_SVIPC_ID(msqid);
796 	msqix = IPCID_TO_IX(msqid);
797 
798 	if (msqix < 0 || msqix >= msginfo.msgmni) {
799 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
800 		    msginfo.msgmni));
801 		error = EINVAL;
802 		goto done2;
803 	}
804 
805 	msqkptr = &msqids[msqix];
806 	AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
807 	if (msqkptr->u.msg_qbytes == 0) {
808 		DPRINTF(("no such message queue id\n"));
809 		error = EINVAL;
810 		goto done2;
811 	}
812 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
813 		DPRINTF(("wrong sequence number\n"));
814 		error = EINVAL;
815 		goto done2;
816 	}
817 
818 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
819 		DPRINTF(("requester can't see prison\n"));
820 		goto done2;
821 	}
822 
823 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
824 		DPRINTF(("requester doesn't have write access\n"));
825 		goto done2;
826 	}
827 
828 #ifdef MAC
829 	error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
830 	if (error != 0)
831 		goto done2;
832 #endif
833 
834 #ifdef RACCT
835 	if (racct_enable) {
836 		PROC_LOCK(td->td_proc);
837 		if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
838 			PROC_UNLOCK(td->td_proc);
839 			error = EAGAIN;
840 			goto done2;
841 		}
842 		saved_msgsz = msgsz;
843 		if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
844 			racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
845 			PROC_UNLOCK(td->td_proc);
846 			error = EAGAIN;
847 			goto done2;
848 		}
849 		PROC_UNLOCK(td->td_proc);
850 	}
851 #endif
852 
853 	segs_needed = howmany(msgsz, msginfo.msgssz);
854 	DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
855 	    msginfo.msgssz, segs_needed));
856 	for (;;) {
857 		int need_more_resources = 0;
858 
859 		/*
860 		 * check msgsz
861 		 * (inside this loop in case msg_qbytes changes while we sleep)
862 		 */
863 
864 		if (msgsz > msqkptr->u.msg_qbytes) {
865 			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
866 			error = EINVAL;
867 			goto done3;
868 		}
869 
870 		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
871 			DPRINTF(("msqid is locked\n"));
872 			need_more_resources = 1;
873 		}
874 		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
875 			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
876 			need_more_resources = 1;
877 		}
878 		if (segs_needed > nfree_msgmaps) {
879 			DPRINTF(("segs_needed > nfree_msgmaps\n"));
880 			need_more_resources = 1;
881 		}
882 		if (free_msghdrs == NULL) {
883 			DPRINTF(("no more msghdrs\n"));
884 			need_more_resources = 1;
885 		}
886 
887 		if (need_more_resources) {
888 			int we_own_it;
889 
890 			if ((msgflg & IPC_NOWAIT) != 0) {
891 				DPRINTF(("need more resources but caller "
892 				    "doesn't want to wait\n"));
893 				error = EAGAIN;
894 				goto done3;
895 			}
896 
897 			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
898 				DPRINTF(("we don't own the msqid_ds\n"));
899 				we_own_it = 0;
900 			} else {
901 				/* Force later arrivals to wait for our
902 				   request */
903 				DPRINTF(("we own the msqid_ds\n"));
904 				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
905 				we_own_it = 1;
906 			}
907 			DPRINTF(("msgsnd:  goodnight\n"));
908 			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
909 			    "msgsnd", hz);
910 			DPRINTF(("msgsnd:  good morning, error=%d\n", error));
911 			if (we_own_it)
912 				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
913 			if (error == EWOULDBLOCK) {
914 				DPRINTF(("msgsnd:  timed out\n"));
915 				continue;
916 			}
917 			if (error != 0) {
918 				DPRINTF(("msgsnd:  interrupted system call\n"));
919 				error = EINTR;
920 				goto done3;
921 			}
922 
923 			/*
924 			 * Make sure that the msq queue still exists
925 			 */
926 
927 			if (msqkptr->u.msg_qbytes == 0) {
928 				DPRINTF(("msqid deleted\n"));
929 				error = EIDRM;
930 				goto done3;
931 			}
932 
933 		} else {
934 			DPRINTF(("got all the resources that we need\n"));
935 			break;
936 		}
937 	}
938 
939 	/*
940 	 * We have the resources that we need.
941 	 * Make sure!
942 	 */
943 
944 	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
945 		panic("msg_perm.mode & MSG_LOCKED");
946 	if (segs_needed > nfree_msgmaps)
947 		panic("segs_needed > nfree_msgmaps");
948 	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
949 		panic("msgsz + msg_cbytes > msg_qbytes");
950 	if (free_msghdrs == NULL)
951 		panic("no more msghdrs");
952 
953 	/*
954 	 * Re-lock the msqid_ds in case we page-fault when copying in the
955 	 * message
956 	 */
957 
958 	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
959 		panic("msqid_ds is already locked");
960 	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
961 
962 	/*
963 	 * Allocate a message header
964 	 */
965 
966 	msghdr = free_msghdrs;
967 	free_msghdrs = msghdr->msg_next;
968 	msghdr->msg_spot = -1;
969 	msghdr->msg_ts = msgsz;
970 	msghdr->msg_type = mtype;
971 #ifdef MAC
972 	/*
973 	 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
974 	 * immediately?  Or, should it be checked just before the msg is
975 	 * enqueued in the msgq (as it is done now)?
976 	 */
977 	mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
978 #endif
979 
980 	/*
981 	 * Allocate space for the message
982 	 */
983 
984 	while (segs_needed > 0) {
985 		if (nfree_msgmaps <= 0)
986 			panic("not enough msgmaps");
987 		if (free_msgmaps == -1)
988 			panic("nil free_msgmaps");
989 		next = free_msgmaps;
990 		if (next <= -1)
991 			panic("next too low #1");
992 		if (next >= msginfo.msgseg)
993 			panic("next out of range #1");
994 		DPRINTF(("allocating segment %d to message\n", next));
995 		free_msgmaps = msgmaps[next].next;
996 		nfree_msgmaps--;
997 		msgmaps[next].next = msghdr->msg_spot;
998 		msghdr->msg_spot = next;
999 		segs_needed--;
1000 	}
1001 
1002 	/*
1003 	 * Validate the message type
1004 	 */
1005 
1006 	if (msghdr->msg_type < 1) {
1007 		msg_freehdr(msghdr);
1008 		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1009 		wakeup(msqkptr);
1010 		DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
1011 		error = EINVAL;
1012 		goto done3;
1013 	}
1014 
1015 	/*
1016 	 * Copy in the message body
1017 	 */
1018 
1019 	next = msghdr->msg_spot;
1020 	while (msgsz > 0) {
1021 		size_t tlen;
1022 		if (msgsz > msginfo.msgssz)
1023 			tlen = msginfo.msgssz;
1024 		else
1025 			tlen = msgsz;
1026 		if (next <= -1)
1027 			panic("next too low #2");
1028 		if (next >= msginfo.msgseg)
1029 			panic("next out of range #2");
1030 		mtx_unlock(&msq_mtx);
1031 		if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
1032 		    tlen)) != 0) {
1033 			mtx_lock(&msq_mtx);
1034 			DPRINTF(("error %d copying in message segment\n",
1035 			    error));
1036 			msg_freehdr(msghdr);
1037 			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1038 			wakeup(msqkptr);
1039 			goto done3;
1040 		}
1041 		mtx_lock(&msq_mtx);
1042 		msgsz -= tlen;
1043 		msgp = (const char *)msgp + tlen;
1044 		next = msgmaps[next].next;
1045 	}
1046 	if (next != -1)
1047 		panic("didn't use all the msg segments");
1048 
1049 	/*
1050 	 * We've got the message.  Unlock the msqid_ds.
1051 	 */
1052 
1053 	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1054 
1055 	/*
1056 	 * Make sure that the msqid_ds is still allocated.
1057 	 */
1058 
1059 	if (msqkptr->u.msg_qbytes == 0) {
1060 		msg_freehdr(msghdr);
1061 		wakeup(msqkptr);
1062 		error = EIDRM;
1063 		goto done3;
1064 	}
1065 
1066 #ifdef MAC
1067 	/*
1068 	 * Note: Since the task/thread allocates the msghdr and usually
1069 	 * primes it with its own MAC label, for a majority of policies, it
1070 	 * won't be necessary to check whether the msghdr has access
1071 	 * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
1072 	 * suffice in that case.  However, this hook may be required where
1073 	 * individual policies derive a non-identical label for the msghdr
1074 	 * from the current thread label and may want to check the msghdr
1075 	 * enqueue permissions, along with read/write permissions to the
1076 	 * msgq.
1077 	 */
1078 	error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
1079 	if (error != 0) {
1080 		msg_freehdr(msghdr);
1081 		wakeup(msqkptr);
1082 		goto done3;
1083 	}
1084 #endif
1085 
1086 	/*
1087 	 * Put the message into the queue
1088 	 */
1089 	if (msqkptr->u.msg_first == NULL) {
1090 		msqkptr->u.msg_first = msghdr;
1091 		msqkptr->u.msg_last = msghdr;
1092 	} else {
1093 		msqkptr->u.msg_last->msg_next = msghdr;
1094 		msqkptr->u.msg_last = msghdr;
1095 	}
1096 	msqkptr->u.msg_last->msg_next = NULL;
1097 
1098 	msqkptr->u.msg_cbytes += msghdr->msg_ts;
1099 	msqkptr->u.msg_qnum++;
1100 	msqkptr->u.msg_lspid = td->td_proc->p_pid;
1101 	msqkptr->u.msg_stime = time_second;
1102 
1103 	wakeup(msqkptr);
1104 	td->td_retval[0] = 0;
1105 done3:
1106 #ifdef RACCT
1107 	if (racct_enable && error != 0) {
1108 		PROC_LOCK(td->td_proc);
1109 		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1110 		racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1111 		PROC_UNLOCK(td->td_proc);
1112 	}
1113 #endif
1114 done2:
1115 	mtx_unlock(&msq_mtx);
1116 	return (error);
1117 }
1118 
1119 int
1120 sys_msgsnd(td, uap)
1121 	struct thread *td;
1122 	register struct msgsnd_args *uap;
1123 {
1124 	int error;
1125 	long mtype;
1126 
1127 	DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1128 	    uap->msgsz, uap->msgflg));
1129 
1130 	if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1131 		DPRINTF(("error %d copying the message type\n", error));
1132 		return (error);
1133 	}
1134 	return (kern_msgsnd(td, uap->msqid,
1135 	    (const char *)uap->msgp + sizeof(mtype),
1136 	    uap->msgsz, uap->msgflg, mtype));
1137 }
1138 
1139 #ifndef _SYS_SYSPROTO_H_
1140 struct msgrcv_args {
1141 	int	msqid;
1142 	void	*msgp;
1143 	size_t	msgsz;
1144 	long	msgtyp;
1145 	int	msgflg;
1146 };
1147 #endif
1148 int
1149 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1150 	struct thread *td;
1151 	int msqid;
1152 	void *msgp;	/* XXX msgp is actually mtext. */
1153 	size_t msgsz;
1154 	long msgtyp;
1155 	int msgflg;
1156 	long *mtype;
1157 {
1158 	size_t len;
1159 	register struct msqid_kernel *msqkptr;
1160 	register struct msg *msghdr;
1161 	struct prison *rpr;
1162 	int msqix, error = 0;
1163 	short next;
1164 
1165 	rpr = msg_find_prison(td->td_ucred);
1166 	if (rpr == NULL)
1167 		return (ENOSYS);
1168 
1169 	AUDIT_ARG_SVIPC_ID(msqid);
1170 	msqix = IPCID_TO_IX(msqid);
1171 
1172 	if (msqix < 0 || msqix >= msginfo.msgmni) {
1173 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1174 		    msginfo.msgmni));
1175 		return (EINVAL);
1176 	}
1177 
1178 	msqkptr = &msqids[msqix];
1179 	mtx_lock(&msq_mtx);
1180 	AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
1181 	if (msqkptr->u.msg_qbytes == 0) {
1182 		DPRINTF(("no such message queue id\n"));
1183 		error = EINVAL;
1184 		goto done2;
1185 	}
1186 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1187 		DPRINTF(("wrong sequence number\n"));
1188 		error = EINVAL;
1189 		goto done2;
1190 	}
1191 
1192 	if ((error = msq_prison_cansee(rpr, msqkptr))) {
1193 		DPRINTF(("requester can't see prison\n"));
1194 		goto done2;
1195 	}
1196 
1197 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1198 		DPRINTF(("requester doesn't have read access\n"));
1199 		goto done2;
1200 	}
1201 
1202 #ifdef MAC
1203 	error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1204 	if (error != 0)
1205 		goto done2;
1206 #endif
1207 
1208 	msghdr = NULL;
1209 	while (msghdr == NULL) {
1210 		if (msgtyp == 0) {
1211 			msghdr = msqkptr->u.msg_first;
1212 			if (msghdr != NULL) {
1213 				if (msgsz < msghdr->msg_ts &&
1214 				    (msgflg & MSG_NOERROR) == 0) {
1215 					DPRINTF(("first message on the queue "
1216 					    "is too big (want %zu, got %d)\n",
1217 					    msgsz, msghdr->msg_ts));
1218 					error = E2BIG;
1219 					goto done2;
1220 				}
1221 #ifdef MAC
1222 				error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1223 				    msghdr);
1224 				if (error != 0)
1225 					goto done2;
1226 #endif
1227 				if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1228 					msqkptr->u.msg_first = NULL;
1229 					msqkptr->u.msg_last = NULL;
1230 				} else {
1231 					msqkptr->u.msg_first = msghdr->msg_next;
1232 					if (msqkptr->u.msg_first == NULL)
1233 						panic("msg_first/last screwed up #1");
1234 				}
1235 			}
1236 		} else {
1237 			struct msg *previous;
1238 			struct msg **prev;
1239 
1240 			previous = NULL;
1241 			prev = &(msqkptr->u.msg_first);
1242 			while ((msghdr = *prev) != NULL) {
1243 				/*
1244 				 * Is this message's type an exact match or is
1245 				 * this message's type less than or equal to
1246 				 * the absolute value of a negative msgtyp?
1247 				 * Note that the second half of this test can
1248 				 * NEVER be true if msgtyp is positive since
1249 				 * msg_type is always positive!
1250 				 */
1251 
1252 				if (msgtyp == msghdr->msg_type ||
1253 				    msghdr->msg_type <= -msgtyp) {
1254 					DPRINTF(("found message type %ld, "
1255 					    "requested %ld\n",
1256 					    msghdr->msg_type, msgtyp));
1257 					if (msgsz < msghdr->msg_ts &&
1258 					    (msgflg & MSG_NOERROR) == 0) {
1259 						DPRINTF(("requested message "
1260 						    "on the queue is too big "
1261 						    "(want %zu, got %hu)\n",
1262 						    msgsz, msghdr->msg_ts));
1263 						error = E2BIG;
1264 						goto done2;
1265 					}
1266 #ifdef MAC
1267 					error = mac_sysvmsq_check_msgrcv(
1268 					    td->td_ucred, msghdr);
1269 					if (error != 0)
1270 						goto done2;
1271 #endif
1272 					*prev = msghdr->msg_next;
1273 					if (msghdr == msqkptr->u.msg_last) {
1274 						if (previous == NULL) {
1275 							if (prev !=
1276 							    &msqkptr->u.msg_first)
1277 								panic("msg_first/last screwed up #2");
1278 							msqkptr->u.msg_first =
1279 							    NULL;
1280 							msqkptr->u.msg_last =
1281 							    NULL;
1282 						} else {
1283 							if (prev ==
1284 							    &msqkptr->u.msg_first)
1285 								panic("msg_first/last screwed up #3");
1286 							msqkptr->u.msg_last =
1287 							    previous;
1288 						}
1289 					}
1290 					break;
1291 				}
1292 				previous = msghdr;
1293 				prev = &(msghdr->msg_next);
1294 			}
1295 		}
1296 
1297 		/*
1298 		 * We've either extracted the msghdr for the appropriate
1299 		 * message or there isn't one.
1300 		 * If there is one then bail out of this loop.
1301 		 */
1302 
1303 		if (msghdr != NULL)
1304 			break;
1305 
1306 		/*
1307 		 * Hmph!  No message found.  Does the user want to wait?
1308 		 */
1309 
1310 		if ((msgflg & IPC_NOWAIT) != 0) {
1311 			DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1312 			    msgtyp));
1313 			/* The SVID says to return ENOMSG. */
1314 			error = ENOMSG;
1315 			goto done2;
1316 		}
1317 
1318 		/*
1319 		 * Wait for something to happen
1320 		 */
1321 
1322 		DPRINTF(("msgrcv:  goodnight\n"));
1323 		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1324 		    "msgrcv", 0);
1325 		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1326 
1327 		if (error != 0) {
1328 			DPRINTF(("msgrcv:  interrupted system call\n"));
1329 			error = EINTR;
1330 			goto done2;
1331 		}
1332 
1333 		/*
1334 		 * Make sure that the msq queue still exists
1335 		 */
1336 
1337 		if (msqkptr->u.msg_qbytes == 0 ||
1338 		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1339 			DPRINTF(("msqid deleted\n"));
1340 			error = EIDRM;
1341 			goto done2;
1342 		}
1343 	}
1344 
1345 	/*
1346 	 * Return the message to the user.
1347 	 *
1348 	 * First, do the bookkeeping (before we risk being interrupted).
1349 	 */
1350 
1351 	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1352 	msqkptr->u.msg_qnum--;
1353 	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1354 	msqkptr->u.msg_rtime = time_second;
1355 
1356 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1357 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1358 
1359 	/*
1360 	 * Make msgsz the actual amount that we'll be returning.
1361 	 * Note that this effectively truncates the message if it is too long
1362 	 * (since msgsz is never increased).
1363 	 */
1364 
1365 	DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1366 	    msghdr->msg_ts));
1367 	if (msgsz > msghdr->msg_ts)
1368 		msgsz = msghdr->msg_ts;
1369 	*mtype = msghdr->msg_type;
1370 
1371 	/*
1372 	 * Return the segments to the user
1373 	 */
1374 
1375 	next = msghdr->msg_spot;
1376 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1377 		size_t tlen;
1378 
1379 		if (msgsz - len > msginfo.msgssz)
1380 			tlen = msginfo.msgssz;
1381 		else
1382 			tlen = msgsz - len;
1383 		if (next <= -1)
1384 			panic("next too low #3");
1385 		if (next >= msginfo.msgseg)
1386 			panic("next out of range #3");
1387 		mtx_unlock(&msq_mtx);
1388 		error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1389 		mtx_lock(&msq_mtx);
1390 		if (error != 0) {
1391 			DPRINTF(("error (%d) copying out message segment\n",
1392 			    error));
1393 			msg_freehdr(msghdr);
1394 			wakeup(msqkptr);
1395 			goto done2;
1396 		}
1397 		msgp = (char *)msgp + tlen;
1398 		next = msgmaps[next].next;
1399 	}
1400 
1401 	/*
1402 	 * Done, return the actual number of bytes copied out.
1403 	 */
1404 
1405 	msg_freehdr(msghdr);
1406 	wakeup(msqkptr);
1407 	td->td_retval[0] = msgsz;
1408 done2:
1409 	mtx_unlock(&msq_mtx);
1410 	return (error);
1411 }
1412 
1413 int
1414 sys_msgrcv(td, uap)
1415 	struct thread *td;
1416 	register struct msgrcv_args *uap;
1417 {
1418 	int error;
1419 	long mtype;
1420 
1421 	DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1422 	    uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1423 
1424 	if ((error = kern_msgrcv(td, uap->msqid,
1425 	    (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1426 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1427 		return (error);
1428 	if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1429 		DPRINTF(("error %d copying the message type\n", error));
1430 	return (error);
1431 }
1432 
1433 static int
1434 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1435 {
1436 	struct msqid_kernel tmsqk;
1437 	struct prison *pr, *rpr;
1438 	int error, i;
1439 
1440 	pr = req->td->td_ucred->cr_prison;
1441 	rpr = msg_find_prison(req->td->td_ucred);
1442 	error = 0;
1443 	for (i = 0; i < msginfo.msgmni; i++) {
1444 		mtx_lock(&msq_mtx);
1445 		if (msqids[i].u.msg_qbytes == 0 || rpr == NULL ||
1446 		    msq_prison_cansee(rpr, &msqids[i]) != 0)
1447 			bzero(&tmsqk, sizeof(tmsqk));
1448 		else {
1449 			tmsqk = msqids[i];
1450 			if (tmsqk.cred->cr_prison != pr)
1451 				tmsqk.u.msg_perm.key = IPC_PRIVATE;
1452 		}
1453 		mtx_unlock(&msq_mtx);
1454 		error = SYSCTL_OUT(req, &tmsqk, sizeof(tmsqk));
1455 		if (error != 0)
1456 			break;
1457 	}
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,
1474     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
1475     NULL, 0, sysctl_msqids, "", "Message queue IDs");
1476 
1477 static int
1478 msg_prison_check(void *obj, void *data)
1479 {
1480 	struct prison *pr = obj;
1481 	struct prison *prpr;
1482 	struct vfsoptlist *opts = data;
1483 	int error, jsys;
1484 
1485 	/*
1486 	 * sysvmsg is a jailsys integer.
1487 	 * It must be "disable" if the parent jail is disabled.
1488 	 */
1489 	error = vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1490 	if (error != ENOENT) {
1491 		if (error != 0)
1492 			return (error);
1493 		switch (jsys) {
1494 		case JAIL_SYS_DISABLE:
1495 			break;
1496 		case JAIL_SYS_NEW:
1497 		case JAIL_SYS_INHERIT:
1498 			prison_lock(pr->pr_parent);
1499 			prpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1500 			prison_unlock(pr->pr_parent);
1501 			if (prpr == NULL)
1502 				return (EPERM);
1503 			break;
1504 		default:
1505 			return (EINVAL);
1506 		}
1507 	}
1508 
1509 	return (0);
1510 }
1511 
1512 static int
1513 msg_prison_set(void *obj, void *data)
1514 {
1515 	struct prison *pr = obj;
1516 	struct prison *tpr, *orpr, *nrpr, *trpr;
1517 	struct vfsoptlist *opts = data;
1518 	void *rsv;
1519 	int jsys, descend;
1520 
1521 	/*
1522 	 * sysvmsg controls which jail is the root of the associated msgs (this
1523 	 * jail or same as the parent), or if the feature is available at all.
1524 	 */
1525 	if (vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys)) == ENOENT)
1526 		jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1527 		    ? JAIL_SYS_INHERIT
1528 		    : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1529 		    ? JAIL_SYS_DISABLE
1530 		    : -1;
1531 	if (jsys == JAIL_SYS_DISABLE) {
1532 		prison_lock(pr);
1533 		orpr = osd_jail_get(pr, msg_prison_slot);
1534 		if (orpr != NULL)
1535 			osd_jail_del(pr, msg_prison_slot);
1536 		prison_unlock(pr);
1537 		if (orpr != NULL) {
1538 			if (orpr == pr)
1539 				msg_prison_cleanup(pr);
1540 			/* Disable all child jails as well. */
1541 			FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1542 				prison_lock(tpr);
1543 				trpr = osd_jail_get(tpr, msg_prison_slot);
1544 				if (trpr != NULL) {
1545 					osd_jail_del(tpr, msg_prison_slot);
1546 					prison_unlock(tpr);
1547 					if (trpr == tpr)
1548 						msg_prison_cleanup(tpr);
1549 				} else {
1550 					prison_unlock(tpr);
1551 					descend = 0;
1552 				}
1553 			}
1554 		}
1555 	} else if (jsys != -1) {
1556 		if (jsys == JAIL_SYS_NEW)
1557 			nrpr = pr;
1558 		else {
1559 			prison_lock(pr->pr_parent);
1560 			nrpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1561 			prison_unlock(pr->pr_parent);
1562 		}
1563 		rsv = osd_reserve(msg_prison_slot);
1564 		prison_lock(pr);
1565 		orpr = osd_jail_get(pr, msg_prison_slot);
1566 		if (orpr != nrpr)
1567 			(void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
1568 			    nrpr);
1569 		else
1570 			osd_free_reserved(rsv);
1571 		prison_unlock(pr);
1572 		if (orpr != nrpr) {
1573 			if (orpr == pr)
1574 				msg_prison_cleanup(pr);
1575 			if (orpr != NULL) {
1576 				/* Change child jails matching the old root, */
1577 				FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1578 					prison_lock(tpr);
1579 					trpr = osd_jail_get(tpr,
1580 					    msg_prison_slot);
1581 					if (trpr == orpr) {
1582 						(void)osd_jail_set(tpr,
1583 						    msg_prison_slot, nrpr);
1584 						prison_unlock(tpr);
1585 						if (trpr == tpr)
1586 							msg_prison_cleanup(tpr);
1587 					} else {
1588 						prison_unlock(tpr);
1589 						descend = 0;
1590 					}
1591 				}
1592 			}
1593 		}
1594 	}
1595 
1596 	return (0);
1597 }
1598 
1599 static int
1600 msg_prison_get(void *obj, void *data)
1601 {
1602 	struct prison *pr = obj;
1603 	struct prison *rpr;
1604 	struct vfsoptlist *opts = data;
1605 	int error, jsys;
1606 
1607 	/* Set sysvmsg based on the jail's root prison. */
1608 	prison_lock(pr);
1609 	rpr = osd_jail_get(pr, msg_prison_slot);
1610 	prison_unlock(pr);
1611 	jsys = rpr == NULL ? JAIL_SYS_DISABLE
1612 	    : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1613 	error = vfs_setopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1614 	if (error == ENOENT)
1615 		error = 0;
1616 	return (error);
1617 }
1618 
1619 static int
1620 msg_prison_remove(void *obj, void *data __unused)
1621 {
1622 	struct prison *pr = obj;
1623 	struct prison *rpr;
1624 
1625 	prison_lock(pr);
1626 	rpr = osd_jail_get(pr, msg_prison_slot);
1627 	prison_unlock(pr);
1628 	if (rpr == pr)
1629 		msg_prison_cleanup(pr);
1630 	return (0);
1631 }
1632 
1633 static void
1634 msg_prison_cleanup(struct prison *pr)
1635 {
1636 	struct msqid_kernel *msqkptr;
1637 	int i;
1638 
1639 	/* Remove any msqs that belong to this jail. */
1640 	mtx_lock(&msq_mtx);
1641 	for (i = 0; i < msginfo.msgmni; i++) {
1642 		msqkptr = &msqids[i];
1643 		if (msqkptr->u.msg_qbytes != 0 &&
1644 		    msqkptr->cred != NULL && msqkptr->cred->cr_prison == pr)
1645 			msq_remove(msqkptr);
1646 	}
1647 	mtx_unlock(&msq_mtx);
1648 }
1649 
1650 SYSCTL_JAIL_PARAM_SYS_NODE(sysvmsg, CTLFLAG_RW, "SYSV message queues");
1651 
1652 #ifdef COMPAT_FREEBSD32
1653 int
1654 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1655 {
1656 
1657 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1658     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1659 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1660 	switch (uap->which) {
1661 	case 0:
1662 		return (freebsd7_freebsd32_msgctl(td,
1663 		    (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1664 	case 2:
1665 		return (freebsd32_msgsnd(td,
1666 		    (struct freebsd32_msgsnd_args *)&uap->a2));
1667 	case 3:
1668 		return (freebsd32_msgrcv(td,
1669 		    (struct freebsd32_msgrcv_args *)&uap->a2));
1670 	default:
1671 		return (sys_msgsys(td, (struct msgsys_args *)uap));
1672 	}
1673 #else
1674 	return (nosys(td, NULL));
1675 #endif
1676 }
1677 
1678 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1679     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1680 int
1681 freebsd7_freebsd32_msgctl(struct thread *td,
1682     struct freebsd7_freebsd32_msgctl_args *uap)
1683 {
1684 	struct msqid_ds msqbuf;
1685 	struct msqid_ds32_old msqbuf32;
1686 	int error;
1687 
1688 	if (uap->cmd == IPC_SET) {
1689 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1690 		if (error)
1691 			return (error);
1692 		freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1693 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1694 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1695 		CP(msqbuf32, msqbuf, msg_cbytes);
1696 		CP(msqbuf32, msqbuf, msg_qnum);
1697 		CP(msqbuf32, msqbuf, msg_qbytes);
1698 		CP(msqbuf32, msqbuf, msg_lspid);
1699 		CP(msqbuf32, msqbuf, msg_lrpid);
1700 		CP(msqbuf32, msqbuf, msg_stime);
1701 		CP(msqbuf32, msqbuf, msg_rtime);
1702 		CP(msqbuf32, msqbuf, msg_ctime);
1703 	}
1704 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1705 	if (error)
1706 		return (error);
1707 	if (uap->cmd == IPC_STAT) {
1708 		bzero(&msqbuf32, sizeof(msqbuf32));
1709 		freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1710 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1711 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1712 		CP(msqbuf, msqbuf32, msg_cbytes);
1713 		CP(msqbuf, msqbuf32, msg_qnum);
1714 		CP(msqbuf, msqbuf32, msg_qbytes);
1715 		CP(msqbuf, msqbuf32, msg_lspid);
1716 		CP(msqbuf, msqbuf32, msg_lrpid);
1717 		CP(msqbuf, msqbuf32, msg_stime);
1718 		CP(msqbuf, msqbuf32, msg_rtime);
1719 		CP(msqbuf, msqbuf32, msg_ctime);
1720 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1721 	}
1722 	return (error);
1723 }
1724 #endif
1725 
1726 int
1727 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1728 {
1729 	struct msqid_ds msqbuf;
1730 	struct msqid_ds32 msqbuf32;
1731 	int error;
1732 
1733 	if (uap->cmd == IPC_SET) {
1734 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1735 		if (error)
1736 			return (error);
1737 		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1738 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1739 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1740 		CP(msqbuf32, msqbuf, msg_cbytes);
1741 		CP(msqbuf32, msqbuf, msg_qnum);
1742 		CP(msqbuf32, msqbuf, msg_qbytes);
1743 		CP(msqbuf32, msqbuf, msg_lspid);
1744 		CP(msqbuf32, msqbuf, msg_lrpid);
1745 		CP(msqbuf32, msqbuf, msg_stime);
1746 		CP(msqbuf32, msqbuf, msg_rtime);
1747 		CP(msqbuf32, msqbuf, msg_ctime);
1748 	}
1749 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1750 	if (error)
1751 		return (error);
1752 	if (uap->cmd == IPC_STAT) {
1753 		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1754 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1755 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1756 		CP(msqbuf, msqbuf32, msg_cbytes);
1757 		CP(msqbuf, msqbuf32, msg_qnum);
1758 		CP(msqbuf, msqbuf32, msg_qbytes);
1759 		CP(msqbuf, msqbuf32, msg_lspid);
1760 		CP(msqbuf, msqbuf32, msg_lrpid);
1761 		CP(msqbuf, msqbuf32, msg_stime);
1762 		CP(msqbuf, msqbuf32, msg_rtime);
1763 		CP(msqbuf, msqbuf32, msg_ctime);
1764 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1765 	}
1766 	return (error);
1767 }
1768 
1769 int
1770 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1771 {
1772 	const void *msgp;
1773 	long mtype;
1774 	int32_t mtype32;
1775 	int error;
1776 
1777 	msgp = PTRIN(uap->msgp);
1778 	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1779 		return (error);
1780 	mtype = mtype32;
1781 	return (kern_msgsnd(td, uap->msqid,
1782 	    (const char *)msgp + sizeof(mtype32),
1783 	    uap->msgsz, uap->msgflg, mtype));
1784 }
1785 
1786 int
1787 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1788 {
1789 	void *msgp;
1790 	long mtype;
1791 	int32_t mtype32;
1792 	int error;
1793 
1794 	msgp = PTRIN(uap->msgp);
1795 	if ((error = kern_msgrcv(td, uap->msqid,
1796 	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1797 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1798 		return (error);
1799 	mtype32 = (int32_t)mtype;
1800 	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1801 }
1802 #endif
1803 
1804 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1805     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1806 
1807 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1808 static sy_call_t *msgcalls[] = {
1809 	(sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
1810 	(sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
1811 };
1812 
1813 /*
1814  * Entry point for all MSG calls.
1815  */
1816 int
1817 sys_msgsys(td, uap)
1818 	struct thread *td;
1819 	/* XXX actually varargs. */
1820 	struct msgsys_args /* {
1821 		int	which;
1822 		int	a2;
1823 		int	a3;
1824 		int	a4;
1825 		int	a5;
1826 		int	a6;
1827 	} */ *uap;
1828 {
1829 	int error;
1830 
1831 	AUDIT_ARG_SVIPC_WHICH(uap->which);
1832 	if (uap->which < 0 || uap->which >= nitems(msgcalls))
1833 		return (EINVAL);
1834 	error = (*msgcalls[uap->which])(td, &uap->a2);
1835 	return (error);
1836 }
1837 
1838 #ifndef CP
1839 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1840 #endif
1841 
1842 #ifndef _SYS_SYSPROTO_H_
1843 struct freebsd7_msgctl_args {
1844 	int	msqid;
1845 	int	cmd;
1846 	struct	msqid_ds_old *buf;
1847 };
1848 #endif
1849 int
1850 freebsd7_msgctl(td, uap)
1851 	struct thread *td;
1852 	struct freebsd7_msgctl_args *uap;
1853 {
1854 	struct msqid_ds_old msqold;
1855 	struct msqid_ds msqbuf;
1856 	int error;
1857 
1858 	DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1859 	    uap->buf));
1860 	if (uap->cmd == IPC_SET) {
1861 		error = copyin(uap->buf, &msqold, sizeof(msqold));
1862 		if (error)
1863 			return (error);
1864 		ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1865 		CP(msqold, msqbuf, msg_first);
1866 		CP(msqold, msqbuf, msg_last);
1867 		CP(msqold, msqbuf, msg_cbytes);
1868 		CP(msqold, msqbuf, msg_qnum);
1869 		CP(msqold, msqbuf, msg_qbytes);
1870 		CP(msqold, msqbuf, msg_lspid);
1871 		CP(msqold, msqbuf, msg_lrpid);
1872 		CP(msqold, msqbuf, msg_stime);
1873 		CP(msqold, msqbuf, msg_rtime);
1874 		CP(msqold, msqbuf, msg_ctime);
1875 	}
1876 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1877 	if (error)
1878 		return (error);
1879 	if (uap->cmd == IPC_STAT) {
1880 		bzero(&msqold, sizeof(msqold));
1881 		ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1882 		CP(msqbuf, msqold, msg_first);
1883 		CP(msqbuf, msqold, msg_last);
1884 		CP(msqbuf, msqold, msg_cbytes);
1885 		CP(msqbuf, msqold, msg_qnum);
1886 		CP(msqbuf, msqold, msg_qbytes);
1887 		CP(msqbuf, msqold, msg_lspid);
1888 		CP(msqbuf, msqold, msg_lrpid);
1889 		CP(msqbuf, msqold, msg_stime);
1890 		CP(msqbuf, msqold, msg_rtime);
1891 		CP(msqbuf, msqold, msg_ctime);
1892 		error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1893 	}
1894 	return (error);
1895 }
1896 
1897 #undef CP
1898 
1899 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1900 	   COMPAT_FREEBSD7 */
1901