sysv_msg.c revision 223825
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: head/sys/kern/sysv_msg.c 223825 2011-07-06 20:06:44Z trasz $");
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/msg.h>
66#include <sys/racct.h>
67#include <sys/syscall.h>
68#include <sys/syscallsubr.h>
69#include <sys/sysent.h>
70#include <sys/sysctl.h>
71#include <sys/malloc.h>
72#include <sys/jail.h>
73
74#include <security/mac/mac_framework.h>
75
76FEATURE(sysv_msg, "System V message queues support");
77
78static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
79
80static int msginit(void);
81static int msgunload(void);
82static int sysvmsg_modload(struct module *, int, void *);
83
84#ifdef MSG_DEBUG
85#define DPRINTF(a)	printf a
86#else
87#define DPRINTF(a)	(void)0
88#endif
89
90static void msg_freehdr(struct msg *msghdr);
91
92#ifndef MSGSSZ
93#define MSGSSZ	8		/* Each segment must be 2^N long */
94#endif
95#ifndef MSGSEG
96#define MSGSEG	2048		/* must be less than 32767 */
97#endif
98#define MSGMAX	(MSGSSZ*MSGSEG)
99#ifndef MSGMNB
100#define MSGMNB	2048		/* max # of bytes in a queue */
101#endif
102#ifndef MSGMNI
103#define MSGMNI	40
104#endif
105#ifndef MSGTQL
106#define MSGTQL	40
107#endif
108
109/*
110 * Based on the configuration parameters described in an SVR2 (yes, two)
111 * config(1m) man page.
112 *
113 * Each message is broken up and stored in segments that are msgssz bytes
114 * long.  For efficiency reasons, this should be a power of two.  Also,
115 * it doesn't make sense if it is less than 8 or greater than about 256.
116 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
117 * two between 8 and 1024 inclusive (and panic's if it isn't).
118 */
119struct msginfo msginfo = {
120                MSGMAX,         /* max chars in a message */
121                MSGMNI,         /* # of message queue identifiers */
122                MSGMNB,         /* max chars in a queue */
123                MSGTQL,         /* max messages in system */
124                MSGSSZ,         /* size of a message segment */
125                		/* (must be small power of 2 greater than 4) */
126                MSGSEG          /* number of message segments */
127};
128
129/*
130 * macros to convert between msqid_ds's and msqid's.
131 * (specific to this implementation)
132 */
133#define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
134#define MSQID_IX(id)	((id) & 0xffff)
135#define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
136
137/*
138 * The rest of this file is specific to this particular implementation.
139 */
140
141struct msgmap {
142	short	next;		/* next segment in buffer */
143    				/* -1 -> available */
144    				/* 0..(MSGSEG-1) -> index of next segment */
145};
146
147#define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
148
149static int nfree_msgmaps;	/* # of free map entries */
150static short free_msgmaps;	/* head of linked list of free map entries */
151static struct msg *free_msghdrs;/* list of free msg headers */
152static char *msgpool;		/* MSGMAX byte long msg buffer pool */
153static struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
154static struct msg *msghdrs;	/* MSGTQL msg headers */
155static struct msqid_kernel *msqids;	/* MSGMNI msqid_kernel struct's */
156static struct mtx msq_mtx;	/* global mutex for message queues. */
157
158static struct syscall_helper_data msg_syscalls[] = {
159	SYSCALL_INIT_HELPER(msgctl),
160	SYSCALL_INIT_HELPER(msgget),
161	SYSCALL_INIT_HELPER(msgsnd),
162	SYSCALL_INIT_HELPER(msgrcv),
163#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
164    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
165	SYSCALL_INIT_HELPER(msgsys),
166	SYSCALL_INIT_HELPER(freebsd7_msgctl),
167#endif
168	SYSCALL_INIT_LAST
169};
170
171#ifdef COMPAT_FREEBSD32
172#include <compat/freebsd32/freebsd32.h>
173#include <compat/freebsd32/freebsd32_ipc.h>
174#include <compat/freebsd32/freebsd32_proto.h>
175#include <compat/freebsd32/freebsd32_signal.h>
176#include <compat/freebsd32/freebsd32_syscall.h>
177#include <compat/freebsd32/freebsd32_util.h>
178
179static struct syscall_helper_data msg32_syscalls[] = {
180	SYSCALL32_INIT_HELPER(freebsd32_msgctl),
181	SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
182	SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
183	SYSCALL32_INIT_HELPER(msgget),
184	SYSCALL32_INIT_HELPER(freebsd32_msgsys),
185#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
186    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
187	SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
188#endif
189	SYSCALL_INIT_LAST
190};
191#endif
192
193static int
194msginit()
195{
196	int i, error;
197
198	TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
199	TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
200	msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
201	TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
202	TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
203	TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
204
205	msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
206	msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
207	msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
208	msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
209	    M_WAITOK);
210
211	/*
212	 * msginfo.msgssz should be a power of two for efficiency reasons.
213	 * It is also pretty silly if msginfo.msgssz is less than 8
214	 * or greater than about 256 so ...
215	 */
216
217	i = 8;
218	while (i < 1024 && i != msginfo.msgssz)
219		i <<= 1;
220    	if (i != msginfo.msgssz) {
221		DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
222		    msginfo.msgssz));
223		panic("msginfo.msgssz not a small power of 2");
224	}
225
226	if (msginfo.msgseg > 32767) {
227		DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
228		panic("msginfo.msgseg > 32767");
229	}
230
231	for (i = 0; i < msginfo.msgseg; i++) {
232		if (i > 0)
233			msgmaps[i-1].next = i;
234		msgmaps[i].next = -1;	/* implies entry is available */
235	}
236	free_msgmaps = 0;
237	nfree_msgmaps = msginfo.msgseg;
238
239	for (i = 0; i < msginfo.msgtql; i++) {
240		msghdrs[i].msg_type = 0;
241		if (i > 0)
242			msghdrs[i-1].msg_next = &msghdrs[i];
243		msghdrs[i].msg_next = NULL;
244#ifdef MAC
245		mac_sysvmsg_init(&msghdrs[i]);
246#endif
247    	}
248	free_msghdrs = &msghdrs[0];
249
250	for (i = 0; i < msginfo.msgmni; i++) {
251		msqids[i].u.msg_qbytes = 0;	/* implies entry is available */
252		msqids[i].u.msg_perm.seq = 0;	/* reset to a known value */
253		msqids[i].u.msg_perm.mode = 0;
254#ifdef MAC
255		mac_sysvmsq_init(&msqids[i]);
256#endif
257	}
258	mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
259
260	error = syscall_helper_register(msg_syscalls);
261	if (error != 0)
262		return (error);
263#ifdef COMPAT_FREEBSD32
264	error = syscall32_helper_register(msg32_syscalls);
265	if (error != 0)
266		return (error);
267#endif
268	return (0);
269}
270
271static int
272msgunload()
273{
274	struct msqid_kernel *msqkptr;
275	int msqid;
276#ifdef MAC
277	int i;
278#endif
279
280	syscall_helper_unregister(msg_syscalls);
281#ifdef COMPAT_FREEBSD32
282	syscall32_helper_unregister(msg32_syscalls);
283#endif
284
285	for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
286		/*
287		 * Look for an unallocated and unlocked msqid_ds.
288		 * msqid_ds's can be locked by msgsnd or msgrcv while
289		 * they are copying the message in/out.  We can't
290		 * re-use the entry until they release it.
291		 */
292		msqkptr = &msqids[msqid];
293		if (msqkptr->u.msg_qbytes != 0 ||
294		    (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
295			break;
296	}
297	if (msqid != msginfo.msgmni)
298		return (EBUSY);
299
300#ifdef MAC
301	for (i = 0; i < msginfo.msgtql; i++)
302		mac_sysvmsg_destroy(&msghdrs[i]);
303	for (msqid = 0; msqid < msginfo.msgmni; msqid++)
304		mac_sysvmsq_destroy(&msqids[msqid]);
305#endif
306	free(msgpool, M_MSG);
307	free(msgmaps, M_MSG);
308	free(msghdrs, M_MSG);
309	free(msqids, M_MSG);
310	mtx_destroy(&msq_mtx);
311	return (0);
312}
313
314
315static int
316sysvmsg_modload(struct module *module, int cmd, void *arg)
317{
318	int error = 0;
319
320	switch (cmd) {
321	case MOD_LOAD:
322		error = msginit();
323		if (error != 0)
324			msgunload();
325		break;
326	case MOD_UNLOAD:
327		error = msgunload();
328		break;
329	case MOD_SHUTDOWN:
330		break;
331	default:
332		error = EINVAL;
333		break;
334	}
335	return (error);
336}
337
338static moduledata_t sysvmsg_mod = {
339	"sysvmsg",
340	&sysvmsg_modload,
341	NULL
342};
343
344DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
345MODULE_VERSION(sysvmsg, 1);
346
347static void
348msg_freehdr(msghdr)
349	struct msg *msghdr;
350{
351	while (msghdr->msg_ts > 0) {
352		short next;
353		if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
354			panic("msghdr->msg_spot out of range");
355		next = msgmaps[msghdr->msg_spot].next;
356		msgmaps[msghdr->msg_spot].next = free_msgmaps;
357		free_msgmaps = msghdr->msg_spot;
358		nfree_msgmaps++;
359		msghdr->msg_spot = next;
360		if (msghdr->msg_ts >= msginfo.msgssz)
361			msghdr->msg_ts -= msginfo.msgssz;
362		else
363			msghdr->msg_ts = 0;
364	}
365	if (msghdr->msg_spot != -1)
366		panic("msghdr->msg_spot != -1");
367	msghdr->msg_next = free_msghdrs;
368	free_msghdrs = msghdr;
369#ifdef MAC
370	mac_sysvmsg_cleanup(msghdr);
371#endif
372}
373
374#ifndef _SYS_SYSPROTO_H_
375struct msgctl_args {
376	int	msqid;
377	int	cmd;
378	struct	msqid_ds *buf;
379};
380#endif
381int
382msgctl(td, uap)
383	struct thread *td;
384	register struct msgctl_args *uap;
385{
386	int msqid = uap->msqid;
387	int cmd = uap->cmd;
388	struct msqid_ds msqbuf;
389	int error;
390
391	DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
392	if (cmd == IPC_SET &&
393	    (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
394		return (error);
395	error = kern_msgctl(td, msqid, cmd, &msqbuf);
396	if (cmd == IPC_STAT && error == 0)
397		error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
398	return (error);
399}
400
401int
402kern_msgctl(td, msqid, cmd, msqbuf)
403	struct thread *td;
404	int msqid;
405	int cmd;
406	struct msqid_ds *msqbuf;
407{
408	int rval, error, msqix;
409	register struct msqid_kernel *msqkptr;
410
411	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
412		return (ENOSYS);
413
414	msqix = IPCID_TO_IX(msqid);
415
416	if (msqix < 0 || msqix >= msginfo.msgmni) {
417		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
418		    msginfo.msgmni));
419		return (EINVAL);
420	}
421
422	msqkptr = &msqids[msqix];
423
424	mtx_lock(&msq_mtx);
425	if (msqkptr->u.msg_qbytes == 0) {
426		DPRINTF(("no such msqid\n"));
427		error = EINVAL;
428		goto done2;
429	}
430	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
431		DPRINTF(("wrong sequence number\n"));
432		error = EINVAL;
433		goto done2;
434	}
435#ifdef MAC
436	error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
437	if (error != 0)
438		goto done2;
439#endif
440
441	error = 0;
442	rval = 0;
443
444	switch (cmd) {
445
446	case IPC_RMID:
447	{
448		struct msg *msghdr;
449		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
450			goto done2;
451
452#ifdef MAC
453		/*
454		 * Check that the thread has MAC access permissions to
455		 * individual msghdrs.  Note: We need to do this in a
456		 * separate loop because the actual loop alters the
457		 * msq/msghdr info as it progresses, and there is no going
458		 * back if half the way through we discover that the
459		 * thread cannot free a certain msghdr.  The msq will get
460		 * into an inconsistent state.
461		 */
462		for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
463		    msghdr = msghdr->msg_next) {
464			error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
465			if (error != 0)
466				goto done2;
467		}
468#endif
469
470		racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
471		racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
472		racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
473		crfree(msqkptr->cred);
474		msqkptr->cred = NULL;
475
476		/* Free the message headers */
477		msghdr = msqkptr->u.msg_first;
478		while (msghdr != NULL) {
479			struct msg *msghdr_tmp;
480
481			/* Free the segments of each message */
482			msqkptr->u.msg_cbytes -= msghdr->msg_ts;
483			msqkptr->u.msg_qnum--;
484			msghdr_tmp = msghdr;
485			msghdr = msghdr->msg_next;
486			msg_freehdr(msghdr_tmp);
487		}
488
489		if (msqkptr->u.msg_cbytes != 0)
490			panic("msg_cbytes is screwed up");
491		if (msqkptr->u.msg_qnum != 0)
492			panic("msg_qnum is screwed up");
493
494		msqkptr->u.msg_qbytes = 0;	/* Mark it as free */
495
496#ifdef MAC
497		mac_sysvmsq_cleanup(msqkptr);
498#endif
499
500		wakeup(msqkptr);
501	}
502
503		break;
504
505	case IPC_SET:
506		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
507			goto done2;
508		if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
509			error = priv_check(td, PRIV_IPC_MSGSIZE);
510			if (error)
511				goto done2;
512		}
513		if (msqbuf->msg_qbytes > msginfo.msgmnb) {
514			DPRINTF(("can't increase msg_qbytes beyond %d"
515			    "(truncating)\n", msginfo.msgmnb));
516			msqbuf->msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
517		}
518		if (msqbuf->msg_qbytes == 0) {
519			DPRINTF(("can't reduce msg_qbytes to 0\n"));
520			error = EINVAL;		/* non-standard errno! */
521			goto done2;
522		}
523		msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid;	/* change the owner */
524		msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid;	/* change the owner */
525		msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
526		    (msqbuf->msg_perm.mode & 0777);
527		msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
528		msqkptr->u.msg_ctime = time_second;
529		break;
530
531	case IPC_STAT:
532		if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
533			DPRINTF(("requester doesn't have read access\n"));
534			goto done2;
535		}
536		*msqbuf = msqkptr->u;
537		break;
538
539	default:
540		DPRINTF(("invalid command %d\n", cmd));
541		error = EINVAL;
542		goto done2;
543	}
544
545	if (error == 0)
546		td->td_retval[0] = rval;
547done2:
548	mtx_unlock(&msq_mtx);
549	return (error);
550}
551
552#ifndef _SYS_SYSPROTO_H_
553struct msgget_args {
554	key_t	key;
555	int	msgflg;
556};
557#endif
558int
559msgget(td, uap)
560	struct thread *td;
561	register struct msgget_args *uap;
562{
563	int msqid, error = 0;
564	int key = uap->key;
565	int msgflg = uap->msgflg;
566	struct ucred *cred = td->td_ucred;
567	register struct msqid_kernel *msqkptr = NULL;
568
569	DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
570
571	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
572		return (ENOSYS);
573
574	mtx_lock(&msq_mtx);
575	if (key != IPC_PRIVATE) {
576		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
577			msqkptr = &msqids[msqid];
578			if (msqkptr->u.msg_qbytes != 0 &&
579			    msqkptr->u.msg_perm.key == key)
580				break;
581		}
582		if (msqid < msginfo.msgmni) {
583			DPRINTF(("found public key\n"));
584			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
585				DPRINTF(("not exclusive\n"));
586				error = EEXIST;
587				goto done2;
588			}
589			if ((error = ipcperm(td, &msqkptr->u.msg_perm,
590			    msgflg & 0700))) {
591				DPRINTF(("requester doesn't have 0%o access\n",
592				    msgflg & 0700));
593				goto done2;
594			}
595#ifdef MAC
596			error = mac_sysvmsq_check_msqget(cred, msqkptr);
597			if (error != 0)
598				goto done2;
599#endif
600			goto found;
601		}
602	}
603
604	DPRINTF(("need to allocate the msqid_ds\n"));
605	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
606		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
607			/*
608			 * Look for an unallocated and unlocked msqid_ds.
609			 * msqid_ds's can be locked by msgsnd or msgrcv while
610			 * they are copying the message in/out.  We can't
611			 * re-use the entry until they release it.
612			 */
613			msqkptr = &msqids[msqid];
614			if (msqkptr->u.msg_qbytes == 0 &&
615			    (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
616				break;
617		}
618		if (msqid == msginfo.msgmni) {
619			DPRINTF(("no more msqid_ds's available\n"));
620			error = ENOSPC;
621			goto done2;
622		}
623#ifdef RACCT
624		PROC_LOCK(td->td_proc);
625		error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
626		PROC_UNLOCK(td->td_proc);
627		if (error != 0) {
628			error = ENOSPC;
629			goto done2;
630		}
631#endif
632		DPRINTF(("msqid %d is available\n", msqid));
633		msqkptr->u.msg_perm.key = key;
634		msqkptr->u.msg_perm.cuid = cred->cr_uid;
635		msqkptr->u.msg_perm.uid = cred->cr_uid;
636		msqkptr->u.msg_perm.cgid = cred->cr_gid;
637		msqkptr->u.msg_perm.gid = cred->cr_gid;
638		msqkptr->u.msg_perm.mode = (msgflg & 0777);
639		msqkptr->cred = crhold(cred);
640		/* Make sure that the returned msqid is unique */
641		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
642		msqkptr->u.msg_first = NULL;
643		msqkptr->u.msg_last = NULL;
644		msqkptr->u.msg_cbytes = 0;
645		msqkptr->u.msg_qnum = 0;
646		msqkptr->u.msg_qbytes = msginfo.msgmnb;
647		msqkptr->u.msg_lspid = 0;
648		msqkptr->u.msg_lrpid = 0;
649		msqkptr->u.msg_stime = 0;
650		msqkptr->u.msg_rtime = 0;
651		msqkptr->u.msg_ctime = time_second;
652#ifdef MAC
653		mac_sysvmsq_create(cred, msqkptr);
654#endif
655	} else {
656		DPRINTF(("didn't find it and wasn't asked to create it\n"));
657		error = ENOENT;
658		goto done2;
659	}
660
661found:
662	/* Construct the unique msqid */
663	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
664done2:
665	mtx_unlock(&msq_mtx);
666	return (error);
667}
668
669#ifndef _SYS_SYSPROTO_H_
670struct msgsnd_args {
671	int	msqid;
672	const void	*msgp;
673	size_t	msgsz;
674	int	msgflg;
675};
676#endif
677int
678kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
679	struct thread *td;
680	int msqid;
681	const void *msgp;	/* XXX msgp is actually mtext. */
682	size_t msgsz;
683	int msgflg;
684	long mtype;
685{
686	int msqix, segs_needed, error = 0;
687	register struct msqid_kernel *msqkptr;
688	register struct msg *msghdr;
689	short next;
690#ifdef RACCT
691	size_t saved_msgsz;
692#endif
693
694	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
695		return (ENOSYS);
696
697	mtx_lock(&msq_mtx);
698	msqix = IPCID_TO_IX(msqid);
699
700	if (msqix < 0 || msqix >= msginfo.msgmni) {
701		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
702		    msginfo.msgmni));
703		error = EINVAL;
704		goto done2;
705	}
706
707	msqkptr = &msqids[msqix];
708	if (msqkptr->u.msg_qbytes == 0) {
709		DPRINTF(("no such message queue id\n"));
710		error = EINVAL;
711		goto done2;
712	}
713	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
714		DPRINTF(("wrong sequence number\n"));
715		error = EINVAL;
716		goto done2;
717	}
718
719	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
720		DPRINTF(("requester doesn't have write access\n"));
721		goto done2;
722	}
723
724#ifdef MAC
725	error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
726	if (error != 0)
727		goto done2;
728#endif
729
730#ifdef RACCT
731	PROC_LOCK(td->td_proc);
732	if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
733		PROC_UNLOCK(td->td_proc);
734		error = EAGAIN;
735		goto done2;
736	}
737	saved_msgsz = msgsz;
738	if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
739		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
740		PROC_UNLOCK(td->td_proc);
741		error = EAGAIN;
742		goto done2;
743	}
744	PROC_UNLOCK(td->td_proc);
745#endif
746
747	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
748	DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
749	    msginfo.msgssz, segs_needed));
750	for (;;) {
751		int need_more_resources = 0;
752
753		/*
754		 * check msgsz
755		 * (inside this loop in case msg_qbytes changes while we sleep)
756		 */
757
758		if (msgsz > msqkptr->u.msg_qbytes) {
759			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
760			error = EINVAL;
761			goto done3;
762		}
763
764		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
765			DPRINTF(("msqid is locked\n"));
766			need_more_resources = 1;
767		}
768		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
769			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
770			need_more_resources = 1;
771		}
772		if (segs_needed > nfree_msgmaps) {
773			DPRINTF(("segs_needed > nfree_msgmaps\n"));
774			need_more_resources = 1;
775		}
776		if (free_msghdrs == NULL) {
777			DPRINTF(("no more msghdrs\n"));
778			need_more_resources = 1;
779		}
780
781		if (need_more_resources) {
782			int we_own_it;
783
784			if ((msgflg & IPC_NOWAIT) != 0) {
785				DPRINTF(("need more resources but caller "
786				    "doesn't want to wait\n"));
787				error = EAGAIN;
788				goto done3;
789			}
790
791			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
792				DPRINTF(("we don't own the msqid_ds\n"));
793				we_own_it = 0;
794			} else {
795				/* Force later arrivals to wait for our
796				   request */
797				DPRINTF(("we own the msqid_ds\n"));
798				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
799				we_own_it = 1;
800			}
801			DPRINTF(("msgsnd:  goodnight\n"));
802			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
803			    "msgsnd", hz);
804			DPRINTF(("msgsnd:  good morning, error=%d\n", error));
805			if (we_own_it)
806				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
807			if (error == EWOULDBLOCK) {
808				DPRINTF(("msgsnd:  timed out\n"));
809				continue;
810			}
811			if (error != 0) {
812				DPRINTF(("msgsnd:  interrupted system call\n"));
813				error = EINTR;
814				goto done3;
815			}
816
817			/*
818			 * Make sure that the msq queue still exists
819			 */
820
821			if (msqkptr->u.msg_qbytes == 0) {
822				DPRINTF(("msqid deleted\n"));
823				error = EIDRM;
824				goto done3;
825			}
826
827		} else {
828			DPRINTF(("got all the resources that we need\n"));
829			break;
830		}
831	}
832
833	/*
834	 * We have the resources that we need.
835	 * Make sure!
836	 */
837
838	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
839		panic("msg_perm.mode & MSG_LOCKED");
840	if (segs_needed > nfree_msgmaps)
841		panic("segs_needed > nfree_msgmaps");
842	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
843		panic("msgsz + msg_cbytes > msg_qbytes");
844	if (free_msghdrs == NULL)
845		panic("no more msghdrs");
846
847	/*
848	 * Re-lock the msqid_ds in case we page-fault when copying in the
849	 * message
850	 */
851
852	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
853		panic("msqid_ds is already locked");
854	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
855
856	/*
857	 * Allocate a message header
858	 */
859
860	msghdr = free_msghdrs;
861	free_msghdrs = msghdr->msg_next;
862	msghdr->msg_spot = -1;
863	msghdr->msg_ts = msgsz;
864	msghdr->msg_type = mtype;
865#ifdef MAC
866	/*
867	 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
868	 * immediately?  Or, should it be checked just before the msg is
869	 * enqueued in the msgq (as it is done now)?
870	 */
871	mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
872#endif
873
874	/*
875	 * Allocate space for the message
876	 */
877
878	while (segs_needed > 0) {
879		if (nfree_msgmaps <= 0)
880			panic("not enough msgmaps");
881		if (free_msgmaps == -1)
882			panic("nil free_msgmaps");
883		next = free_msgmaps;
884		if (next <= -1)
885			panic("next too low #1");
886		if (next >= msginfo.msgseg)
887			panic("next out of range #1");
888		DPRINTF(("allocating segment %d to message\n", next));
889		free_msgmaps = msgmaps[next].next;
890		nfree_msgmaps--;
891		msgmaps[next].next = msghdr->msg_spot;
892		msghdr->msg_spot = next;
893		segs_needed--;
894	}
895
896	/*
897	 * Validate the message type
898	 */
899
900	if (msghdr->msg_type < 1) {
901		msg_freehdr(msghdr);
902		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
903		wakeup(msqkptr);
904		DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
905		error = EINVAL;
906		goto done3;
907	}
908
909	/*
910	 * Copy in the message body
911	 */
912
913	next = msghdr->msg_spot;
914	while (msgsz > 0) {
915		size_t tlen;
916		if (msgsz > msginfo.msgssz)
917			tlen = msginfo.msgssz;
918		else
919			tlen = msgsz;
920		if (next <= -1)
921			panic("next too low #2");
922		if (next >= msginfo.msgseg)
923			panic("next out of range #2");
924		mtx_unlock(&msq_mtx);
925		if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
926		    tlen)) != 0) {
927			mtx_lock(&msq_mtx);
928			DPRINTF(("error %d copying in message segment\n",
929			    error));
930			msg_freehdr(msghdr);
931			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
932			wakeup(msqkptr);
933			goto done3;
934		}
935		mtx_lock(&msq_mtx);
936		msgsz -= tlen;
937		msgp = (const char *)msgp + tlen;
938		next = msgmaps[next].next;
939	}
940	if (next != -1)
941		panic("didn't use all the msg segments");
942
943	/*
944	 * We've got the message.  Unlock the msqid_ds.
945	 */
946
947	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
948
949	/*
950	 * Make sure that the msqid_ds is still allocated.
951	 */
952
953	if (msqkptr->u.msg_qbytes == 0) {
954		msg_freehdr(msghdr);
955		wakeup(msqkptr);
956		error = EIDRM;
957		goto done3;
958	}
959
960#ifdef MAC
961	/*
962	 * Note: Since the task/thread allocates the msghdr and usually
963	 * primes it with its own MAC label, for a majority of policies, it
964	 * won't be necessary to check whether the msghdr has access
965	 * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
966	 * suffice in that case.  However, this hook may be required where
967	 * individual policies derive a non-identical label for the msghdr
968	 * from the current thread label and may want to check the msghdr
969	 * enqueue permissions, along with read/write permissions to the
970	 * msgq.
971	 */
972	error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
973	if (error != 0) {
974		msg_freehdr(msghdr);
975		wakeup(msqkptr);
976		goto done3;
977	}
978#endif
979
980	/*
981	 * Put the message into the queue
982	 */
983	if (msqkptr->u.msg_first == NULL) {
984		msqkptr->u.msg_first = msghdr;
985		msqkptr->u.msg_last = msghdr;
986	} else {
987		msqkptr->u.msg_last->msg_next = msghdr;
988		msqkptr->u.msg_last = msghdr;
989	}
990	msqkptr->u.msg_last->msg_next = NULL;
991
992	msqkptr->u.msg_cbytes += msghdr->msg_ts;
993	msqkptr->u.msg_qnum++;
994	msqkptr->u.msg_lspid = td->td_proc->p_pid;
995	msqkptr->u.msg_stime = time_second;
996
997	wakeup(msqkptr);
998	td->td_retval[0] = 0;
999done3:
1000#ifdef RACCT
1001	if (error != 0) {
1002		PROC_LOCK(td->td_proc);
1003		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1004		racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1005		PROC_UNLOCK(td->td_proc);
1006	}
1007#endif
1008done2:
1009	mtx_unlock(&msq_mtx);
1010	return (error);
1011}
1012
1013int
1014msgsnd(td, uap)
1015	struct thread *td;
1016	register struct msgsnd_args *uap;
1017{
1018	int error;
1019	long mtype;
1020
1021	DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1022	    uap->msgsz, uap->msgflg));
1023
1024	if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1025		DPRINTF(("error %d copying the message type\n", error));
1026		return (error);
1027	}
1028	return (kern_msgsnd(td, uap->msqid,
1029	    (const char *)uap->msgp + sizeof(mtype),
1030	    uap->msgsz, uap->msgflg, mtype));
1031}
1032
1033#ifndef _SYS_SYSPROTO_H_
1034struct msgrcv_args {
1035	int	msqid;
1036	void	*msgp;
1037	size_t	msgsz;
1038	long	msgtyp;
1039	int	msgflg;
1040};
1041#endif
1042int
1043kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1044	struct thread *td;
1045	int msqid;
1046	void *msgp;	/* XXX msgp is actually mtext. */
1047	size_t msgsz;
1048	long msgtyp;
1049	int msgflg;
1050	long *mtype;
1051{
1052	size_t len;
1053	register struct msqid_kernel *msqkptr;
1054	register struct msg *msghdr;
1055	int msqix, error = 0;
1056	short next;
1057
1058	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1059		return (ENOSYS);
1060
1061	msqix = IPCID_TO_IX(msqid);
1062
1063	if (msqix < 0 || msqix >= msginfo.msgmni) {
1064		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1065		    msginfo.msgmni));
1066		return (EINVAL);
1067	}
1068
1069	msqkptr = &msqids[msqix];
1070	mtx_lock(&msq_mtx);
1071	if (msqkptr->u.msg_qbytes == 0) {
1072		DPRINTF(("no such message queue id\n"));
1073		error = EINVAL;
1074		goto done2;
1075	}
1076	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1077		DPRINTF(("wrong sequence number\n"));
1078		error = EINVAL;
1079		goto done2;
1080	}
1081
1082	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1083		DPRINTF(("requester doesn't have read access\n"));
1084		goto done2;
1085	}
1086
1087#ifdef MAC
1088	error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1089	if (error != 0)
1090		goto done2;
1091#endif
1092
1093	msghdr = NULL;
1094	while (msghdr == NULL) {
1095		if (msgtyp == 0) {
1096			msghdr = msqkptr->u.msg_first;
1097			if (msghdr != NULL) {
1098				if (msgsz < msghdr->msg_ts &&
1099				    (msgflg & MSG_NOERROR) == 0) {
1100					DPRINTF(("first message on the queue "
1101					    "is too big (want %zu, got %d)\n",
1102					    msgsz, msghdr->msg_ts));
1103					error = E2BIG;
1104					goto done2;
1105				}
1106#ifdef MAC
1107				error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1108				    msghdr);
1109				if (error != 0)
1110					goto done2;
1111#endif
1112				if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1113					msqkptr->u.msg_first = NULL;
1114					msqkptr->u.msg_last = NULL;
1115				} else {
1116					msqkptr->u.msg_first = msghdr->msg_next;
1117					if (msqkptr->u.msg_first == NULL)
1118						panic("msg_first/last screwed up #1");
1119				}
1120			}
1121		} else {
1122			struct msg *previous;
1123			struct msg **prev;
1124
1125			previous = NULL;
1126			prev = &(msqkptr->u.msg_first);
1127			while ((msghdr = *prev) != NULL) {
1128				/*
1129				 * Is this message's type an exact match or is
1130				 * this message's type less than or equal to
1131				 * the absolute value of a negative msgtyp?
1132				 * Note that the second half of this test can
1133				 * NEVER be true if msgtyp is positive since
1134				 * msg_type is always positive!
1135				 */
1136
1137				if (msgtyp == msghdr->msg_type ||
1138				    msghdr->msg_type <= -msgtyp) {
1139					DPRINTF(("found message type %ld, "
1140					    "requested %ld\n",
1141					    msghdr->msg_type, msgtyp));
1142					if (msgsz < msghdr->msg_ts &&
1143					    (msgflg & MSG_NOERROR) == 0) {
1144						DPRINTF(("requested message "
1145						    "on the queue is too big "
1146						    "(want %zu, got %hu)\n",
1147						    msgsz, msghdr->msg_ts));
1148						error = E2BIG;
1149						goto done2;
1150					}
1151#ifdef MAC
1152					error = mac_sysvmsq_check_msgrcv(
1153					    td->td_ucred, msghdr);
1154					if (error != 0)
1155						goto done2;
1156#endif
1157					*prev = msghdr->msg_next;
1158					if (msghdr == msqkptr->u.msg_last) {
1159						if (previous == NULL) {
1160							if (prev !=
1161							    &msqkptr->u.msg_first)
1162								panic("msg_first/last screwed up #2");
1163							msqkptr->u.msg_first =
1164							    NULL;
1165							msqkptr->u.msg_last =
1166							    NULL;
1167						} else {
1168							if (prev ==
1169							    &msqkptr->u.msg_first)
1170								panic("msg_first/last screwed up #3");
1171							msqkptr->u.msg_last =
1172							    previous;
1173						}
1174					}
1175					break;
1176				}
1177				previous = msghdr;
1178				prev = &(msghdr->msg_next);
1179			}
1180		}
1181
1182		/*
1183		 * We've either extracted the msghdr for the appropriate
1184		 * message or there isn't one.
1185		 * If there is one then bail out of this loop.
1186		 */
1187
1188		if (msghdr != NULL)
1189			break;
1190
1191		/*
1192		 * Hmph!  No message found.  Does the user want to wait?
1193		 */
1194
1195		if ((msgflg & IPC_NOWAIT) != 0) {
1196			DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1197			    msgtyp));
1198			/* The SVID says to return ENOMSG. */
1199			error = ENOMSG;
1200			goto done2;
1201		}
1202
1203		/*
1204		 * Wait for something to happen
1205		 */
1206
1207		DPRINTF(("msgrcv:  goodnight\n"));
1208		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1209		    "msgrcv", 0);
1210		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1211
1212		if (error != 0) {
1213			DPRINTF(("msgrcv:  interrupted system call\n"));
1214			error = EINTR;
1215			goto done2;
1216		}
1217
1218		/*
1219		 * Make sure that the msq queue still exists
1220		 */
1221
1222		if (msqkptr->u.msg_qbytes == 0 ||
1223		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1224			DPRINTF(("msqid deleted\n"));
1225			error = EIDRM;
1226			goto done2;
1227		}
1228	}
1229
1230	/*
1231	 * Return the message to the user.
1232	 *
1233	 * First, do the bookkeeping (before we risk being interrupted).
1234	 */
1235
1236	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1237	msqkptr->u.msg_qnum--;
1238	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1239	msqkptr->u.msg_rtime = time_second;
1240
1241	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1242	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1243
1244	/*
1245	 * Make msgsz the actual amount that we'll be returning.
1246	 * Note that this effectively truncates the message if it is too long
1247	 * (since msgsz is never increased).
1248	 */
1249
1250	DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1251	    msghdr->msg_ts));
1252	if (msgsz > msghdr->msg_ts)
1253		msgsz = msghdr->msg_ts;
1254	*mtype = msghdr->msg_type;
1255
1256	/*
1257	 * Return the segments to the user
1258	 */
1259
1260	next = msghdr->msg_spot;
1261	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1262		size_t tlen;
1263
1264		if (msgsz - len > msginfo.msgssz)
1265			tlen = msginfo.msgssz;
1266		else
1267			tlen = msgsz - len;
1268		if (next <= -1)
1269			panic("next too low #3");
1270		if (next >= msginfo.msgseg)
1271			panic("next out of range #3");
1272		mtx_unlock(&msq_mtx);
1273		error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1274		mtx_lock(&msq_mtx);
1275		if (error != 0) {
1276			DPRINTF(("error (%d) copying out message segment\n",
1277			    error));
1278			msg_freehdr(msghdr);
1279			wakeup(msqkptr);
1280			goto done2;
1281		}
1282		msgp = (char *)msgp + tlen;
1283		next = msgmaps[next].next;
1284	}
1285
1286	/*
1287	 * Done, return the actual number of bytes copied out.
1288	 */
1289
1290	msg_freehdr(msghdr);
1291	wakeup(msqkptr);
1292	td->td_retval[0] = msgsz;
1293done2:
1294	mtx_unlock(&msq_mtx);
1295	return (error);
1296}
1297
1298int
1299msgrcv(td, uap)
1300	struct thread *td;
1301	register struct msgrcv_args *uap;
1302{
1303	int error;
1304	long mtype;
1305
1306	DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1307	    uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1308
1309	if ((error = kern_msgrcv(td, uap->msqid,
1310	    (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1311	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1312		return (error);
1313	if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1314		DPRINTF(("error %d copying the message type\n", error));
1315	return (error);
1316}
1317
1318static int
1319sysctl_msqids(SYSCTL_HANDLER_ARGS)
1320{
1321
1322	return (SYSCTL_OUT(req, msqids,
1323	    sizeof(struct msqid_kernel) * msginfo.msgmni));
1324}
1325
1326SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1327    "Maximum message size");
1328SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1329    "Number of message queue identifiers");
1330SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1331    "Maximum number of bytes in a queue");
1332SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1333    "Maximum number of messages in the system");
1334SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1335    "Size of a message segment");
1336SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1337    "Number of message segments");
1338SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLTYPE_OPAQUE | CTLFLAG_RD,
1339    NULL, 0, sysctl_msqids, "", "Message queue IDs");
1340
1341#ifdef COMPAT_FREEBSD32
1342int
1343freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1344{
1345
1346#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1347    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1348	switch (uap->which) {
1349	case 0:
1350		return (freebsd7_freebsd32_msgctl(td,
1351		    (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1352	case 2:
1353		return (freebsd32_msgsnd(td,
1354		    (struct freebsd32_msgsnd_args *)&uap->a2));
1355	case 3:
1356		return (freebsd32_msgrcv(td,
1357		    (struct freebsd32_msgrcv_args *)&uap->a2));
1358	default:
1359		return (msgsys(td, (struct msgsys_args *)uap));
1360	}
1361#else
1362	return (nosys(td, NULL));
1363#endif
1364}
1365
1366#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1367    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1368int
1369freebsd7_freebsd32_msgctl(struct thread *td,
1370    struct freebsd7_freebsd32_msgctl_args *uap)
1371{
1372	struct msqid_ds msqbuf;
1373	struct msqid_ds32_old msqbuf32;
1374	int error;
1375
1376	if (uap->cmd == IPC_SET) {
1377		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1378		if (error)
1379			return (error);
1380		freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1381		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1382		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1383		CP(msqbuf32, msqbuf, msg_cbytes);
1384		CP(msqbuf32, msqbuf, msg_qnum);
1385		CP(msqbuf32, msqbuf, msg_qbytes);
1386		CP(msqbuf32, msqbuf, msg_lspid);
1387		CP(msqbuf32, msqbuf, msg_lrpid);
1388		CP(msqbuf32, msqbuf, msg_stime);
1389		CP(msqbuf32, msqbuf, msg_rtime);
1390		CP(msqbuf32, msqbuf, msg_ctime);
1391	}
1392	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1393	if (error)
1394		return (error);
1395	if (uap->cmd == IPC_STAT) {
1396		bzero(&msqbuf32, sizeof(msqbuf32));
1397		freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1398		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1399		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1400		CP(msqbuf, msqbuf32, msg_cbytes);
1401		CP(msqbuf, msqbuf32, msg_qnum);
1402		CP(msqbuf, msqbuf32, msg_qbytes);
1403		CP(msqbuf, msqbuf32, msg_lspid);
1404		CP(msqbuf, msqbuf32, msg_lrpid);
1405		CP(msqbuf, msqbuf32, msg_stime);
1406		CP(msqbuf, msqbuf32, msg_rtime);
1407		CP(msqbuf, msqbuf32, msg_ctime);
1408		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1409	}
1410	return (error);
1411}
1412#endif
1413
1414int
1415freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1416{
1417	struct msqid_ds msqbuf;
1418	struct msqid_ds32 msqbuf32;
1419	int error;
1420
1421	if (uap->cmd == IPC_SET) {
1422		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1423		if (error)
1424			return (error);
1425		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1426		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1427		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1428		CP(msqbuf32, msqbuf, msg_cbytes);
1429		CP(msqbuf32, msqbuf, msg_qnum);
1430		CP(msqbuf32, msqbuf, msg_qbytes);
1431		CP(msqbuf32, msqbuf, msg_lspid);
1432		CP(msqbuf32, msqbuf, msg_lrpid);
1433		CP(msqbuf32, msqbuf, msg_stime);
1434		CP(msqbuf32, msqbuf, msg_rtime);
1435		CP(msqbuf32, msqbuf, msg_ctime);
1436	}
1437	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1438	if (error)
1439		return (error);
1440	if (uap->cmd == IPC_STAT) {
1441		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1442		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1443		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1444		CP(msqbuf, msqbuf32, msg_cbytes);
1445		CP(msqbuf, msqbuf32, msg_qnum);
1446		CP(msqbuf, msqbuf32, msg_qbytes);
1447		CP(msqbuf, msqbuf32, msg_lspid);
1448		CP(msqbuf, msqbuf32, msg_lrpid);
1449		CP(msqbuf, msqbuf32, msg_stime);
1450		CP(msqbuf, msqbuf32, msg_rtime);
1451		CP(msqbuf, msqbuf32, msg_ctime);
1452		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1453	}
1454	return (error);
1455}
1456
1457int
1458freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1459{
1460	const void *msgp;
1461	long mtype;
1462	int32_t mtype32;
1463	int error;
1464
1465	msgp = PTRIN(uap->msgp);
1466	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1467		return (error);
1468	mtype = mtype32;
1469	return (kern_msgsnd(td, uap->msqid,
1470	    (const char *)msgp + sizeof(mtype32),
1471	    uap->msgsz, uap->msgflg, mtype));
1472}
1473
1474int
1475freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1476{
1477	void *msgp;
1478	long mtype;
1479	int32_t mtype32;
1480	int error;
1481
1482	msgp = PTRIN(uap->msgp);
1483	if ((error = kern_msgrcv(td, uap->msqid,
1484	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1485	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1486		return (error);
1487	mtype32 = (int32_t)mtype;
1488	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1489}
1490#endif
1491
1492#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1493    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1494
1495/* XXX casting to (sy_call_t *) is bogus, as usual. */
1496static sy_call_t *msgcalls[] = {
1497	(sy_call_t *)freebsd7_msgctl, (sy_call_t *)msgget,
1498	(sy_call_t *)msgsnd, (sy_call_t *)msgrcv
1499};
1500
1501/*
1502 * Entry point for all MSG calls.
1503 */
1504int
1505msgsys(td, uap)
1506	struct thread *td;
1507	/* XXX actually varargs. */
1508	struct msgsys_args /* {
1509		int	which;
1510		int	a2;
1511		int	a3;
1512		int	a4;
1513		int	a5;
1514		int	a6;
1515	} */ *uap;
1516{
1517	int error;
1518
1519	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1520		return (ENOSYS);
1521	if (uap->which < 0 ||
1522	    uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
1523		return (EINVAL);
1524	error = (*msgcalls[uap->which])(td, &uap->a2);
1525	return (error);
1526}
1527
1528#ifndef CP
1529#define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1530#endif
1531
1532#ifndef _SYS_SYSPROTO_H_
1533struct freebsd7_msgctl_args {
1534	int	msqid;
1535	int	cmd;
1536	struct	msqid_ds_old *buf;
1537};
1538#endif
1539int
1540freebsd7_msgctl(td, uap)
1541	struct thread *td;
1542	struct freebsd7_msgctl_args *uap;
1543{
1544	struct msqid_ds_old msqold;
1545	struct msqid_ds msqbuf;
1546	int error;
1547
1548	DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1549	    uap->buf));
1550	if (uap->cmd == IPC_SET) {
1551		error = copyin(uap->buf, &msqold, sizeof(msqold));
1552		if (error)
1553			return (error);
1554		ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1555		CP(msqold, msqbuf, msg_first);
1556		CP(msqold, msqbuf, msg_last);
1557		CP(msqold, msqbuf, msg_cbytes);
1558		CP(msqold, msqbuf, msg_qnum);
1559		CP(msqold, msqbuf, msg_qbytes);
1560		CP(msqold, msqbuf, msg_lspid);
1561		CP(msqold, msqbuf, msg_lrpid);
1562		CP(msqold, msqbuf, msg_stime);
1563		CP(msqold, msqbuf, msg_rtime);
1564		CP(msqold, msqbuf, msg_ctime);
1565	}
1566	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1567	if (error)
1568		return (error);
1569	if (uap->cmd == IPC_STAT) {
1570		bzero(&msqold, sizeof(msqold));
1571		ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1572		CP(msqbuf, msqold, msg_first);
1573		CP(msqbuf, msqold, msg_last);
1574		CP(msqbuf, msqold, msg_cbytes);
1575		CP(msqbuf, msqold, msg_qnum);
1576		CP(msqbuf, msqold, msg_qbytes);
1577		CP(msqbuf, msqold, msg_lspid);
1578		CP(msqbuf, msqold, msg_lrpid);
1579		CP(msqbuf, msqold, msg_stime);
1580		CP(msqbuf, msqold, msg_rtime);
1581		CP(msqbuf, msqold, msg_ctime);
1582		error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1583	}
1584	return (error);
1585}
1586
1587#undef CP
1588
1589#endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1590	   COMPAT_FREEBSD7 */
1591