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