xref: /freebsd/sys/kern/sysv_msg.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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/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 
76 FEATURE(sysv_msg, "System V message queues support");
77 
78 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
79 
80 static int msginit(void);
81 static int msgunload(void);
82 static 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 
90 static 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  */
119 struct 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 
141 struct 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 
149 static int nfree_msgmaps;	/* # of free map entries */
150 static short free_msgmaps;	/* head of linked list of free map entries */
151 static struct msg *free_msghdrs;/* list of free msg headers */
152 static char *msgpool;		/* MSGMAX byte long msg buffer pool */
153 static struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
154 static struct msg *msghdrs;	/* MSGTQL msg headers */
155 static struct msqid_kernel *msqids;	/* MSGMNI msqid_kernel struct's */
156 static struct mtx msq_mtx;	/* global mutex for message queues. */
157 
158 static 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 
179 static 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 
193 static int
194 msginit()
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 
271 static int
272 msgunload()
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 
315 static int
316 sysvmsg_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 
338 static moduledata_t sysvmsg_mod = {
339 	"sysvmsg",
340 	&sysvmsg_modload,
341 	NULL
342 };
343 
344 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
345 MODULE_VERSION(sysvmsg, 1);
346 
347 static void
348 msg_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_
375 struct msgctl_args {
376 	int	msqid;
377 	int	cmd;
378 	struct	msqid_ds *buf;
379 };
380 #endif
381 int
382 msgctl(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 
401 int
402 kern_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;
547 done2:
548 	mtx_unlock(&msq_mtx);
549 	return (error);
550 }
551 
552 #ifndef _SYS_SYSPROTO_H_
553 struct msgget_args {
554 	key_t	key;
555 	int	msgflg;
556 };
557 #endif
558 int
559 msgget(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 		PROC_LOCK(td->td_proc);
624 		error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
625 		PROC_UNLOCK(td->td_proc);
626 		if (error != 0) {
627 			error = ENOSPC;
628 			goto done2;
629 		}
630 		DPRINTF(("msqid %d is available\n", msqid));
631 		msqkptr->u.msg_perm.key = key;
632 		msqkptr->u.msg_perm.cuid = cred->cr_uid;
633 		msqkptr->u.msg_perm.uid = cred->cr_uid;
634 		msqkptr->u.msg_perm.cgid = cred->cr_gid;
635 		msqkptr->u.msg_perm.gid = cred->cr_gid;
636 		msqkptr->u.msg_perm.mode = (msgflg & 0777);
637 		msqkptr->cred = crhold(cred);
638 		/* Make sure that the returned msqid is unique */
639 		msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
640 		msqkptr->u.msg_first = NULL;
641 		msqkptr->u.msg_last = NULL;
642 		msqkptr->u.msg_cbytes = 0;
643 		msqkptr->u.msg_qnum = 0;
644 		msqkptr->u.msg_qbytes = msginfo.msgmnb;
645 		msqkptr->u.msg_lspid = 0;
646 		msqkptr->u.msg_lrpid = 0;
647 		msqkptr->u.msg_stime = 0;
648 		msqkptr->u.msg_rtime = 0;
649 		msqkptr->u.msg_ctime = time_second;
650 #ifdef MAC
651 		mac_sysvmsq_create(cred, msqkptr);
652 #endif
653 	} else {
654 		DPRINTF(("didn't find it and wasn't asked to create it\n"));
655 		error = ENOENT;
656 		goto done2;
657 	}
658 
659 found:
660 	/* Construct the unique msqid */
661 	td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
662 done2:
663 	mtx_unlock(&msq_mtx);
664 	return (error);
665 }
666 
667 #ifndef _SYS_SYSPROTO_H_
668 struct msgsnd_args {
669 	int	msqid;
670 	const void	*msgp;
671 	size_t	msgsz;
672 	int	msgflg;
673 };
674 #endif
675 int
676 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
677 	struct thread *td;
678 	int msqid;
679 	const void *msgp;	/* XXX msgp is actually mtext. */
680 	size_t msgsz;
681 	int msgflg;
682 	long mtype;
683 {
684 	int msqix, segs_needed, error = 0;
685 	register struct msqid_kernel *msqkptr;
686 	register struct msg *msghdr;
687 	short next;
688 	size_t saved_msgsz;
689 
690 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
691 		return (ENOSYS);
692 
693 	mtx_lock(&msq_mtx);
694 	msqix = IPCID_TO_IX(msqid);
695 
696 	if (msqix < 0 || msqix >= msginfo.msgmni) {
697 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
698 		    msginfo.msgmni));
699 		error = EINVAL;
700 		goto done2;
701 	}
702 
703 	msqkptr = &msqids[msqix];
704 	if (msqkptr->u.msg_qbytes == 0) {
705 		DPRINTF(("no such message queue id\n"));
706 		error = EINVAL;
707 		goto done2;
708 	}
709 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
710 		DPRINTF(("wrong sequence number\n"));
711 		error = EINVAL;
712 		goto done2;
713 	}
714 
715 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
716 		DPRINTF(("requester doesn't have write access\n"));
717 		goto done2;
718 	}
719 
720 #ifdef MAC
721 	error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
722 	if (error != 0)
723 		goto done2;
724 #endif
725 
726 	PROC_LOCK(td->td_proc);
727 	if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
728 		PROC_UNLOCK(td->td_proc);
729 		error = EAGAIN;
730 		goto done2;
731 	}
732 	saved_msgsz = msgsz;
733 	if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
734 		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
735 		PROC_UNLOCK(td->td_proc);
736 		error = EAGAIN;
737 		goto done2;
738 	}
739 	PROC_UNLOCK(td->td_proc);
740 
741 	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
742 	DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
743 	    msginfo.msgssz, segs_needed));
744 	for (;;) {
745 		int need_more_resources = 0;
746 
747 		/*
748 		 * check msgsz
749 		 * (inside this loop in case msg_qbytes changes while we sleep)
750 		 */
751 
752 		if (msgsz > msqkptr->u.msg_qbytes) {
753 			DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
754 			error = EINVAL;
755 			goto done3;
756 		}
757 
758 		if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
759 			DPRINTF(("msqid is locked\n"));
760 			need_more_resources = 1;
761 		}
762 		if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
763 			DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
764 			need_more_resources = 1;
765 		}
766 		if (segs_needed > nfree_msgmaps) {
767 			DPRINTF(("segs_needed > nfree_msgmaps\n"));
768 			need_more_resources = 1;
769 		}
770 		if (free_msghdrs == NULL) {
771 			DPRINTF(("no more msghdrs\n"));
772 			need_more_resources = 1;
773 		}
774 
775 		if (need_more_resources) {
776 			int we_own_it;
777 
778 			if ((msgflg & IPC_NOWAIT) != 0) {
779 				DPRINTF(("need more resources but caller "
780 				    "doesn't want to wait\n"));
781 				error = EAGAIN;
782 				goto done3;
783 			}
784 
785 			if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
786 				DPRINTF(("we don't own the msqid_ds\n"));
787 				we_own_it = 0;
788 			} else {
789 				/* Force later arrivals to wait for our
790 				   request */
791 				DPRINTF(("we own the msqid_ds\n"));
792 				msqkptr->u.msg_perm.mode |= MSG_LOCKED;
793 				we_own_it = 1;
794 			}
795 			DPRINTF(("msgsnd:  goodnight\n"));
796 			error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
797 			    "msgsnd", hz);
798 			DPRINTF(("msgsnd:  good morning, error=%d\n", error));
799 			if (we_own_it)
800 				msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
801 			if (error == EWOULDBLOCK) {
802 				DPRINTF(("msgsnd:  timed out\n"));
803 				continue;
804 			}
805 			if (error != 0) {
806 				DPRINTF(("msgsnd:  interrupted system call\n"));
807 				error = EINTR;
808 				goto done3;
809 			}
810 
811 			/*
812 			 * Make sure that the msq queue still exists
813 			 */
814 
815 			if (msqkptr->u.msg_qbytes == 0) {
816 				DPRINTF(("msqid deleted\n"));
817 				error = EIDRM;
818 				goto done3;
819 			}
820 
821 		} else {
822 			DPRINTF(("got all the resources that we need\n"));
823 			break;
824 		}
825 	}
826 
827 	/*
828 	 * We have the resources that we need.
829 	 * Make sure!
830 	 */
831 
832 	if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
833 		panic("msg_perm.mode & MSG_LOCKED");
834 	if (segs_needed > nfree_msgmaps)
835 		panic("segs_needed > nfree_msgmaps");
836 	if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
837 		panic("msgsz + msg_cbytes > msg_qbytes");
838 	if (free_msghdrs == NULL)
839 		panic("no more msghdrs");
840 
841 	/*
842 	 * Re-lock the msqid_ds in case we page-fault when copying in the
843 	 * message
844 	 */
845 
846 	if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
847 		panic("msqid_ds is already locked");
848 	msqkptr->u.msg_perm.mode |= MSG_LOCKED;
849 
850 	/*
851 	 * Allocate a message header
852 	 */
853 
854 	msghdr = free_msghdrs;
855 	free_msghdrs = msghdr->msg_next;
856 	msghdr->msg_spot = -1;
857 	msghdr->msg_ts = msgsz;
858 	msghdr->msg_type = mtype;
859 #ifdef MAC
860 	/*
861 	 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
862 	 * immediately?  Or, should it be checked just before the msg is
863 	 * enqueued in the msgq (as it is done now)?
864 	 */
865 	mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
866 #endif
867 
868 	/*
869 	 * Allocate space for the message
870 	 */
871 
872 	while (segs_needed > 0) {
873 		if (nfree_msgmaps <= 0)
874 			panic("not enough msgmaps");
875 		if (free_msgmaps == -1)
876 			panic("nil free_msgmaps");
877 		next = free_msgmaps;
878 		if (next <= -1)
879 			panic("next too low #1");
880 		if (next >= msginfo.msgseg)
881 			panic("next out of range #1");
882 		DPRINTF(("allocating segment %d to message\n", next));
883 		free_msgmaps = msgmaps[next].next;
884 		nfree_msgmaps--;
885 		msgmaps[next].next = msghdr->msg_spot;
886 		msghdr->msg_spot = next;
887 		segs_needed--;
888 	}
889 
890 	/*
891 	 * Validate the message type
892 	 */
893 
894 	if (msghdr->msg_type < 1) {
895 		msg_freehdr(msghdr);
896 		msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
897 		wakeup(msqkptr);
898 		DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
899 		error = EINVAL;
900 		goto done3;
901 	}
902 
903 	/*
904 	 * Copy in the message body
905 	 */
906 
907 	next = msghdr->msg_spot;
908 	while (msgsz > 0) {
909 		size_t tlen;
910 		if (msgsz > msginfo.msgssz)
911 			tlen = msginfo.msgssz;
912 		else
913 			tlen = msgsz;
914 		if (next <= -1)
915 			panic("next too low #2");
916 		if (next >= msginfo.msgseg)
917 			panic("next out of range #2");
918 		mtx_unlock(&msq_mtx);
919 		if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
920 		    tlen)) != 0) {
921 			mtx_lock(&msq_mtx);
922 			DPRINTF(("error %d copying in message segment\n",
923 			    error));
924 			msg_freehdr(msghdr);
925 			msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
926 			wakeup(msqkptr);
927 			goto done3;
928 		}
929 		mtx_lock(&msq_mtx);
930 		msgsz -= tlen;
931 		msgp = (const char *)msgp + tlen;
932 		next = msgmaps[next].next;
933 	}
934 	if (next != -1)
935 		panic("didn't use all the msg segments");
936 
937 	/*
938 	 * We've got the message.  Unlock the msqid_ds.
939 	 */
940 
941 	msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
942 
943 	/*
944 	 * Make sure that the msqid_ds is still allocated.
945 	 */
946 
947 	if (msqkptr->u.msg_qbytes == 0) {
948 		msg_freehdr(msghdr);
949 		wakeup(msqkptr);
950 		error = EIDRM;
951 		goto done3;
952 	}
953 
954 #ifdef MAC
955 	/*
956 	 * Note: Since the task/thread allocates the msghdr and usually
957 	 * primes it with its own MAC label, for a majority of policies, it
958 	 * won't be necessary to check whether the msghdr has access
959 	 * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
960 	 * suffice in that case.  However, this hook may be required where
961 	 * individual policies derive a non-identical label for the msghdr
962 	 * from the current thread label and may want to check the msghdr
963 	 * enqueue permissions, along with read/write permissions to the
964 	 * msgq.
965 	 */
966 	error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
967 	if (error != 0) {
968 		msg_freehdr(msghdr);
969 		wakeup(msqkptr);
970 		goto done3;
971 	}
972 #endif
973 
974 	/*
975 	 * Put the message into the queue
976 	 */
977 	if (msqkptr->u.msg_first == NULL) {
978 		msqkptr->u.msg_first = msghdr;
979 		msqkptr->u.msg_last = msghdr;
980 	} else {
981 		msqkptr->u.msg_last->msg_next = msghdr;
982 		msqkptr->u.msg_last = msghdr;
983 	}
984 	msqkptr->u.msg_last->msg_next = NULL;
985 
986 	msqkptr->u.msg_cbytes += msghdr->msg_ts;
987 	msqkptr->u.msg_qnum++;
988 	msqkptr->u.msg_lspid = td->td_proc->p_pid;
989 	msqkptr->u.msg_stime = time_second;
990 
991 	wakeup(msqkptr);
992 	td->td_retval[0] = 0;
993 done3:
994 	if (error != 0) {
995 		PROC_LOCK(td->td_proc);
996 		racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
997 		racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
998 		PROC_UNLOCK(td->td_proc);
999 	}
1000 done2:
1001 	mtx_unlock(&msq_mtx);
1002 	return (error);
1003 }
1004 
1005 int
1006 msgsnd(td, uap)
1007 	struct thread *td;
1008 	register struct msgsnd_args *uap;
1009 {
1010 	int error;
1011 	long mtype;
1012 
1013 	DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1014 	    uap->msgsz, uap->msgflg));
1015 
1016 	if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1017 		DPRINTF(("error %d copying the message type\n", error));
1018 		return (error);
1019 	}
1020 	return (kern_msgsnd(td, uap->msqid,
1021 	    (const char *)uap->msgp + sizeof(mtype),
1022 	    uap->msgsz, uap->msgflg, mtype));
1023 }
1024 
1025 #ifndef _SYS_SYSPROTO_H_
1026 struct msgrcv_args {
1027 	int	msqid;
1028 	void	*msgp;
1029 	size_t	msgsz;
1030 	long	msgtyp;
1031 	int	msgflg;
1032 };
1033 #endif
1034 int
1035 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1036 	struct thread *td;
1037 	int msqid;
1038 	void *msgp;	/* XXX msgp is actually mtext. */
1039 	size_t msgsz;
1040 	long msgtyp;
1041 	int msgflg;
1042 	long *mtype;
1043 {
1044 	size_t len;
1045 	register struct msqid_kernel *msqkptr;
1046 	register struct msg *msghdr;
1047 	int msqix, error = 0;
1048 	short next;
1049 
1050 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1051 		return (ENOSYS);
1052 
1053 	msqix = IPCID_TO_IX(msqid);
1054 
1055 	if (msqix < 0 || msqix >= msginfo.msgmni) {
1056 		DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1057 		    msginfo.msgmni));
1058 		return (EINVAL);
1059 	}
1060 
1061 	msqkptr = &msqids[msqix];
1062 	mtx_lock(&msq_mtx);
1063 	if (msqkptr->u.msg_qbytes == 0) {
1064 		DPRINTF(("no such message queue id\n"));
1065 		error = EINVAL;
1066 		goto done2;
1067 	}
1068 	if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1069 		DPRINTF(("wrong sequence number\n"));
1070 		error = EINVAL;
1071 		goto done2;
1072 	}
1073 
1074 	if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1075 		DPRINTF(("requester doesn't have read access\n"));
1076 		goto done2;
1077 	}
1078 
1079 #ifdef MAC
1080 	error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1081 	if (error != 0)
1082 		goto done2;
1083 #endif
1084 
1085 	msghdr = NULL;
1086 	while (msghdr == NULL) {
1087 		if (msgtyp == 0) {
1088 			msghdr = msqkptr->u.msg_first;
1089 			if (msghdr != NULL) {
1090 				if (msgsz < msghdr->msg_ts &&
1091 				    (msgflg & MSG_NOERROR) == 0) {
1092 					DPRINTF(("first message on the queue "
1093 					    "is too big (want %zu, got %d)\n",
1094 					    msgsz, msghdr->msg_ts));
1095 					error = E2BIG;
1096 					goto done2;
1097 				}
1098 #ifdef MAC
1099 				error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1100 				    msghdr);
1101 				if (error != 0)
1102 					goto done2;
1103 #endif
1104 				if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1105 					msqkptr->u.msg_first = NULL;
1106 					msqkptr->u.msg_last = NULL;
1107 				} else {
1108 					msqkptr->u.msg_first = msghdr->msg_next;
1109 					if (msqkptr->u.msg_first == NULL)
1110 						panic("msg_first/last screwed up #1");
1111 				}
1112 			}
1113 		} else {
1114 			struct msg *previous;
1115 			struct msg **prev;
1116 
1117 			previous = NULL;
1118 			prev = &(msqkptr->u.msg_first);
1119 			while ((msghdr = *prev) != NULL) {
1120 				/*
1121 				 * Is this message's type an exact match or is
1122 				 * this message's type less than or equal to
1123 				 * the absolute value of a negative msgtyp?
1124 				 * Note that the second half of this test can
1125 				 * NEVER be true if msgtyp is positive since
1126 				 * msg_type is always positive!
1127 				 */
1128 
1129 				if (msgtyp == msghdr->msg_type ||
1130 				    msghdr->msg_type <= -msgtyp) {
1131 					DPRINTF(("found message type %ld, "
1132 					    "requested %ld\n",
1133 					    msghdr->msg_type, msgtyp));
1134 					if (msgsz < msghdr->msg_ts &&
1135 					    (msgflg & MSG_NOERROR) == 0) {
1136 						DPRINTF(("requested message "
1137 						    "on the queue is too big "
1138 						    "(want %zu, got %hu)\n",
1139 						    msgsz, msghdr->msg_ts));
1140 						error = E2BIG;
1141 						goto done2;
1142 					}
1143 #ifdef MAC
1144 					error = mac_sysvmsq_check_msgrcv(
1145 					    td->td_ucred, msghdr);
1146 					if (error != 0)
1147 						goto done2;
1148 #endif
1149 					*prev = msghdr->msg_next;
1150 					if (msghdr == msqkptr->u.msg_last) {
1151 						if (previous == NULL) {
1152 							if (prev !=
1153 							    &msqkptr->u.msg_first)
1154 								panic("msg_first/last screwed up #2");
1155 							msqkptr->u.msg_first =
1156 							    NULL;
1157 							msqkptr->u.msg_last =
1158 							    NULL;
1159 						} else {
1160 							if (prev ==
1161 							    &msqkptr->u.msg_first)
1162 								panic("msg_first/last screwed up #3");
1163 							msqkptr->u.msg_last =
1164 							    previous;
1165 						}
1166 					}
1167 					break;
1168 				}
1169 				previous = msghdr;
1170 				prev = &(msghdr->msg_next);
1171 			}
1172 		}
1173 
1174 		/*
1175 		 * We've either extracted the msghdr for the appropriate
1176 		 * message or there isn't one.
1177 		 * If there is one then bail out of this loop.
1178 		 */
1179 
1180 		if (msghdr != NULL)
1181 			break;
1182 
1183 		/*
1184 		 * Hmph!  No message found.  Does the user want to wait?
1185 		 */
1186 
1187 		if ((msgflg & IPC_NOWAIT) != 0) {
1188 			DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1189 			    msgtyp));
1190 			/* The SVID says to return ENOMSG. */
1191 			error = ENOMSG;
1192 			goto done2;
1193 		}
1194 
1195 		/*
1196 		 * Wait for something to happen
1197 		 */
1198 
1199 		DPRINTF(("msgrcv:  goodnight\n"));
1200 		error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1201 		    "msgrcv", 0);
1202 		DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1203 
1204 		if (error != 0) {
1205 			DPRINTF(("msgrcv:  interrupted system call\n"));
1206 			error = EINTR;
1207 			goto done2;
1208 		}
1209 
1210 		/*
1211 		 * Make sure that the msq queue still exists
1212 		 */
1213 
1214 		if (msqkptr->u.msg_qbytes == 0 ||
1215 		    msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1216 			DPRINTF(("msqid deleted\n"));
1217 			error = EIDRM;
1218 			goto done2;
1219 		}
1220 	}
1221 
1222 	/*
1223 	 * Return the message to the user.
1224 	 *
1225 	 * First, do the bookkeeping (before we risk being interrupted).
1226 	 */
1227 
1228 	msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1229 	msqkptr->u.msg_qnum--;
1230 	msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1231 	msqkptr->u.msg_rtime = time_second;
1232 
1233 	racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1234 	racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1235 
1236 	/*
1237 	 * Make msgsz the actual amount that we'll be returning.
1238 	 * Note that this effectively truncates the message if it is too long
1239 	 * (since msgsz is never increased).
1240 	 */
1241 
1242 	DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1243 	    msghdr->msg_ts));
1244 	if (msgsz > msghdr->msg_ts)
1245 		msgsz = msghdr->msg_ts;
1246 	*mtype = msghdr->msg_type;
1247 
1248 	/*
1249 	 * Return the segments to the user
1250 	 */
1251 
1252 	next = msghdr->msg_spot;
1253 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
1254 		size_t tlen;
1255 
1256 		if (msgsz - len > msginfo.msgssz)
1257 			tlen = msginfo.msgssz;
1258 		else
1259 			tlen = msgsz - len;
1260 		if (next <= -1)
1261 			panic("next too low #3");
1262 		if (next >= msginfo.msgseg)
1263 			panic("next out of range #3");
1264 		mtx_unlock(&msq_mtx);
1265 		error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1266 		mtx_lock(&msq_mtx);
1267 		if (error != 0) {
1268 			DPRINTF(("error (%d) copying out message segment\n",
1269 			    error));
1270 			msg_freehdr(msghdr);
1271 			wakeup(msqkptr);
1272 			goto done2;
1273 		}
1274 		msgp = (char *)msgp + tlen;
1275 		next = msgmaps[next].next;
1276 	}
1277 
1278 	/*
1279 	 * Done, return the actual number of bytes copied out.
1280 	 */
1281 
1282 	msg_freehdr(msghdr);
1283 	wakeup(msqkptr);
1284 	td->td_retval[0] = msgsz;
1285 done2:
1286 	mtx_unlock(&msq_mtx);
1287 	return (error);
1288 }
1289 
1290 int
1291 msgrcv(td, uap)
1292 	struct thread *td;
1293 	register struct msgrcv_args *uap;
1294 {
1295 	int error;
1296 	long mtype;
1297 
1298 	DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1299 	    uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1300 
1301 	if ((error = kern_msgrcv(td, uap->msqid,
1302 	    (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1303 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1304 		return (error);
1305 	if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1306 		DPRINTF(("error %d copying the message type\n", error));
1307 	return (error);
1308 }
1309 
1310 static int
1311 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1312 {
1313 
1314 	return (SYSCTL_OUT(req, msqids,
1315 	    sizeof(struct msqid_kernel) * msginfo.msgmni));
1316 }
1317 
1318 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1319     "Maximum message size");
1320 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1321     "Number of message queue identifiers");
1322 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1323     "Maximum number of bytes in a queue");
1324 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1325     "Maximum number of messages in the system");
1326 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1327     "Size of a message segment");
1328 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1329     "Number of message segments");
1330 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLTYPE_OPAQUE | CTLFLAG_RD,
1331     NULL, 0, sysctl_msqids, "", "Message queue IDs");
1332 
1333 #ifdef COMPAT_FREEBSD32
1334 int
1335 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1336 {
1337 
1338 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1339     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1340 	switch (uap->which) {
1341 	case 0:
1342 		return (freebsd7_freebsd32_msgctl(td,
1343 		    (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1344 	case 2:
1345 		return (freebsd32_msgsnd(td,
1346 		    (struct freebsd32_msgsnd_args *)&uap->a2));
1347 	case 3:
1348 		return (freebsd32_msgrcv(td,
1349 		    (struct freebsd32_msgrcv_args *)&uap->a2));
1350 	default:
1351 		return (msgsys(td, (struct msgsys_args *)uap));
1352 	}
1353 #else
1354 	return (nosys(td, NULL));
1355 #endif
1356 }
1357 
1358 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1359     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1360 int
1361 freebsd7_freebsd32_msgctl(struct thread *td,
1362     struct freebsd7_freebsd32_msgctl_args *uap)
1363 {
1364 	struct msqid_ds msqbuf;
1365 	struct msqid_ds32_old msqbuf32;
1366 	int error;
1367 
1368 	if (uap->cmd == IPC_SET) {
1369 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1370 		if (error)
1371 			return (error);
1372 		freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1373 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1374 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1375 		CP(msqbuf32, msqbuf, msg_cbytes);
1376 		CP(msqbuf32, msqbuf, msg_qnum);
1377 		CP(msqbuf32, msqbuf, msg_qbytes);
1378 		CP(msqbuf32, msqbuf, msg_lspid);
1379 		CP(msqbuf32, msqbuf, msg_lrpid);
1380 		CP(msqbuf32, msqbuf, msg_stime);
1381 		CP(msqbuf32, msqbuf, msg_rtime);
1382 		CP(msqbuf32, msqbuf, msg_ctime);
1383 	}
1384 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1385 	if (error)
1386 		return (error);
1387 	if (uap->cmd == IPC_STAT) {
1388 		bzero(&msqbuf32, sizeof(msqbuf32));
1389 		freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1390 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1391 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1392 		CP(msqbuf, msqbuf32, msg_cbytes);
1393 		CP(msqbuf, msqbuf32, msg_qnum);
1394 		CP(msqbuf, msqbuf32, msg_qbytes);
1395 		CP(msqbuf, msqbuf32, msg_lspid);
1396 		CP(msqbuf, msqbuf32, msg_lrpid);
1397 		CP(msqbuf, msqbuf32, msg_stime);
1398 		CP(msqbuf, msqbuf32, msg_rtime);
1399 		CP(msqbuf, msqbuf32, msg_ctime);
1400 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1401 	}
1402 	return (error);
1403 }
1404 #endif
1405 
1406 int
1407 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1408 {
1409 	struct msqid_ds msqbuf;
1410 	struct msqid_ds32 msqbuf32;
1411 	int error;
1412 
1413 	if (uap->cmd == IPC_SET) {
1414 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1415 		if (error)
1416 			return (error);
1417 		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1418 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1419 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1420 		CP(msqbuf32, msqbuf, msg_cbytes);
1421 		CP(msqbuf32, msqbuf, msg_qnum);
1422 		CP(msqbuf32, msqbuf, msg_qbytes);
1423 		CP(msqbuf32, msqbuf, msg_lspid);
1424 		CP(msqbuf32, msqbuf, msg_lrpid);
1425 		CP(msqbuf32, msqbuf, msg_stime);
1426 		CP(msqbuf32, msqbuf, msg_rtime);
1427 		CP(msqbuf32, msqbuf, msg_ctime);
1428 	}
1429 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1430 	if (error)
1431 		return (error);
1432 	if (uap->cmd == IPC_STAT) {
1433 		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1434 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1435 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1436 		CP(msqbuf, msqbuf32, msg_cbytes);
1437 		CP(msqbuf, msqbuf32, msg_qnum);
1438 		CP(msqbuf, msqbuf32, msg_qbytes);
1439 		CP(msqbuf, msqbuf32, msg_lspid);
1440 		CP(msqbuf, msqbuf32, msg_lrpid);
1441 		CP(msqbuf, msqbuf32, msg_stime);
1442 		CP(msqbuf, msqbuf32, msg_rtime);
1443 		CP(msqbuf, msqbuf32, msg_ctime);
1444 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1445 	}
1446 	return (error);
1447 }
1448 
1449 int
1450 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1451 {
1452 	const void *msgp;
1453 	long mtype;
1454 	int32_t mtype32;
1455 	int error;
1456 
1457 	msgp = PTRIN(uap->msgp);
1458 	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1459 		return (error);
1460 	mtype = mtype32;
1461 	return (kern_msgsnd(td, uap->msqid,
1462 	    (const char *)msgp + sizeof(mtype32),
1463 	    uap->msgsz, uap->msgflg, mtype));
1464 }
1465 
1466 int
1467 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1468 {
1469 	void *msgp;
1470 	long mtype;
1471 	int32_t mtype32;
1472 	int error;
1473 
1474 	msgp = PTRIN(uap->msgp);
1475 	if ((error = kern_msgrcv(td, uap->msqid,
1476 	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1477 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1478 		return (error);
1479 	mtype32 = (int32_t)mtype;
1480 	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1481 }
1482 #endif
1483 
1484 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1485     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1486 
1487 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1488 static sy_call_t *msgcalls[] = {
1489 	(sy_call_t *)freebsd7_msgctl, (sy_call_t *)msgget,
1490 	(sy_call_t *)msgsnd, (sy_call_t *)msgrcv
1491 };
1492 
1493 /*
1494  * Entry point for all MSG calls.
1495  */
1496 int
1497 msgsys(td, uap)
1498 	struct thread *td;
1499 	/* XXX actually varargs. */
1500 	struct msgsys_args /* {
1501 		int	which;
1502 		int	a2;
1503 		int	a3;
1504 		int	a4;
1505 		int	a5;
1506 		int	a6;
1507 	} */ *uap;
1508 {
1509 	int error;
1510 
1511 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1512 		return (ENOSYS);
1513 	if (uap->which < 0 ||
1514 	    uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
1515 		return (EINVAL);
1516 	error = (*msgcalls[uap->which])(td, &uap->a2);
1517 	return (error);
1518 }
1519 
1520 #ifndef CP
1521 #define CP(src, dst, fld)	do { (dst).fld = (src).fld; } while (0)
1522 #endif
1523 
1524 #ifndef _SYS_SYSPROTO_H_
1525 struct freebsd7_msgctl_args {
1526 	int	msqid;
1527 	int	cmd;
1528 	struct	msqid_ds_old *buf;
1529 };
1530 #endif
1531 int
1532 freebsd7_msgctl(td, uap)
1533 	struct thread *td;
1534 	struct freebsd7_msgctl_args *uap;
1535 {
1536 	struct msqid_ds_old msqold;
1537 	struct msqid_ds msqbuf;
1538 	int error;
1539 
1540 	DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1541 	    uap->buf));
1542 	if (uap->cmd == IPC_SET) {
1543 		error = copyin(uap->buf, &msqold, sizeof(msqold));
1544 		if (error)
1545 			return (error);
1546 		ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1547 		CP(msqold, msqbuf, msg_first);
1548 		CP(msqold, msqbuf, msg_last);
1549 		CP(msqold, msqbuf, msg_cbytes);
1550 		CP(msqold, msqbuf, msg_qnum);
1551 		CP(msqold, msqbuf, msg_qbytes);
1552 		CP(msqold, msqbuf, msg_lspid);
1553 		CP(msqold, msqbuf, msg_lrpid);
1554 		CP(msqold, msqbuf, msg_stime);
1555 		CP(msqold, msqbuf, msg_rtime);
1556 		CP(msqold, msqbuf, msg_ctime);
1557 	}
1558 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1559 	if (error)
1560 		return (error);
1561 	if (uap->cmd == IPC_STAT) {
1562 		bzero(&msqold, sizeof(msqold));
1563 		ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1564 		CP(msqbuf, msqold, msg_first);
1565 		CP(msqbuf, msqold, msg_last);
1566 		CP(msqbuf, msqold, msg_cbytes);
1567 		CP(msqbuf, msqold, msg_qnum);
1568 		CP(msqbuf, msqold, msg_qbytes);
1569 		CP(msqbuf, msqold, msg_lspid);
1570 		CP(msqbuf, msqold, msg_lrpid);
1571 		CP(msqbuf, msqold, msg_stime);
1572 		CP(msqbuf, msqold, msg_rtime);
1573 		CP(msqbuf, msqold, msg_ctime);
1574 		error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1575 	}
1576 	return (error);
1577 }
1578 
1579 #undef CP
1580 
1581 #endif	/* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1582 	   COMPAT_FREEBSD7 */
1583