2 * Implementation of SVID messages
4 * Author: Daniel Boulet
6 * Copyright 1993 Daniel Boulet and RTMX Inc.
8 * This system call was implemented by Daniel Boulet under contract from RTMX.
10 * Redistribution and use in source forms, with and without modification,
11 * are permitted provided that this entire comment appears intact.
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.
17 * This software is provided ``AS IS'' without any warranties of any kind.
20 * Copyright (c) 2003-2005 McAfee, Inc.
21 * All rights reserved.
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
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include "opt_sysvipc.h"
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/sysproto.h>
59 #include <sys/kernel.h>
63 #include <sys/mutex.h>
64 #include <sys/module.h>
66 #include <sys/syscall.h>
67 #include <sys/syscallsubr.h>
68 #include <sys/sysent.h>
69 #include <sys/sysctl.h>
70 #include <sys/malloc.h>
73 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
75 static void msginit(void);
76 static int msgunload(void);
77 static int sysvmsg_modload(struct module *, int, void *);
80 #define DPRINTF(a) printf a
85 #define MPRINTF(a) printf a
90 static void msg_freehdr(struct msg *msghdr);
92 /* XXX casting to (sy_call_t *) is bogus, as usual. */
93 static sy_call_t *msgcalls[] = {
94 (sy_call_t *)msgctl, (sy_call_t *)msgget,
95 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
99 #define MSGSSZ 8 /* Each segment must be 2^N long */
102 #define MSGSEG 2048 /* must be less than 32767 */
104 #define MSGMAX (MSGSSZ*MSGSEG)
106 #define MSGMNB 2048 /* max # of bytes in a queue */
116 * Based on the configuration parameters described in an SVR2 (yes, two)
117 * config(1m) man page.
119 * Each message is broken up and stored in segments that are msgssz bytes
120 * long. For efficiency reasons, this should be a power of two. Also,
121 * it doesn't make sense if it is less than 8 or greater than about 256.
122 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
123 * two between 8 and 1024 inclusive (and panic's if it isn't).
125 struct msginfo msginfo = {
126 MSGMAX, /* max chars in a message */
127 MSGMNI, /* # of message queue identifiers */
128 MSGMNB, /* max chars in a queue */
129 MSGTQL, /* max messages in system */
130 MSGSSZ, /* size of a message segment */
131 /* (must be small power of 2 greater than 4) */
132 MSGSEG /* number of message segments */
136 * macros to convert between msqid_ds's and msqid's.
137 * (specific to this implementation)
139 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
140 #define MSQID_IX(id) ((id) & 0xffff)
141 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
144 * The rest of this file is specific to this particular implementation.
148 short next; /* next segment in buffer */
149 /* -1 -> available */
150 /* 0..(MSGSEG-1) -> index of next segment */
153 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
155 static int nfree_msgmaps; /* # of free map entries */
156 static short free_msgmaps; /* head of linked list of free map entries */
157 static struct msg *free_msghdrs;/* list of free msg headers */
158 static char *msgpool; /* MSGMAX byte long msg buffer pool */
159 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
160 static struct msg *msghdrs; /* MSGTQL msg headers */
161 static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */
162 static struct mtx msq_mtx; /* global mutex for message queues. */
169 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
170 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
171 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
172 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
173 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
174 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
176 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
178 panic("msgpool is NULL");
179 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
181 panic("msgmaps is NULL");
182 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
184 panic("msghdrs is NULL");
185 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
188 panic("msqids is NULL");
191 * msginfo.msgssz should be a power of two for efficiency reasons.
192 * It is also pretty silly if msginfo.msgssz is less than 8
193 * or greater than about 256 so ...
197 while (i < 1024 && i != msginfo.msgssz)
199 if (i != msginfo.msgssz) {
200 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
202 panic("msginfo.msgssz not a small power of 2");
205 if (msginfo.msgseg > 32767) {
206 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
207 panic("msginfo.msgseg > 32767");
211 panic("msgmaps is NULL");
213 for (i = 0; i < msginfo.msgseg; i++) {
215 msgmaps[i-1].next = i;
216 msgmaps[i].next = -1; /* implies entry is available */
219 nfree_msgmaps = msginfo.msgseg;
222 panic("msghdrs is NULL");
224 for (i = 0; i < msginfo.msgtql; i++) {
225 msghdrs[i].msg_type = 0;
227 msghdrs[i-1].msg_next = &msghdrs[i];
228 msghdrs[i].msg_next = NULL;
230 mac_init_sysv_msgmsg(&msghdrs[i]);
233 free_msghdrs = &msghdrs[0];
236 panic("msqids is NULL");
238 for (i = 0; i < msginfo.msgmni; i++) {
239 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
240 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
241 msqids[i].u.msg_perm.mode = 0;
243 mac_init_sysv_msgqueue(&msqids[i]);
246 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
252 struct msqid_kernel *msqkptr;
258 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
260 * Look for an unallocated and unlocked msqid_ds.
261 * msqid_ds's can be locked by msgsnd or msgrcv while
262 * they are copying the message in/out. We can't
263 * re-use the entry until they release it.
265 msqkptr = &msqids[msqid];
266 if (msqkptr->u.msg_qbytes != 0 ||
267 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
270 if (msqid != msginfo.msgmni)
274 for (i = 0; i < msginfo.msgtql; i++)
275 mac_destroy_sysv_msgmsg(&msghdrs[i]);
276 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
277 mac_destroy_sysv_msgqueue(&msqids[msqid]);
279 free(msgpool, M_MSG);
280 free(msgmaps, M_MSG);
281 free(msghdrs, M_MSG);
283 mtx_destroy(&msq_mtx);
289 sysvmsg_modload(struct module *module, int cmd, void *arg)
309 static moduledata_t sysvmsg_mod = {
315 SYSCALL_MODULE_HELPER(msgsys);
316 SYSCALL_MODULE_HELPER(msgctl);
317 SYSCALL_MODULE_HELPER(msgget);
318 SYSCALL_MODULE_HELPER(msgsnd);
319 SYSCALL_MODULE_HELPER(msgrcv);
321 DECLARE_MODULE(sysvmsg, sysvmsg_mod,
322 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
323 MODULE_VERSION(sysvmsg, 1);
326 * Entry point for all MSG calls
333 /* XXX actually varargs. */
334 struct msgsys_args /* {
345 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
347 if (uap->which < 0 ||
348 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
350 error = (*msgcalls[uap->which])(td, &uap->a2);
358 while (msghdr->msg_ts > 0) {
360 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
361 panic("msghdr->msg_spot out of range");
362 next = msgmaps[msghdr->msg_spot].next;
363 msgmaps[msghdr->msg_spot].next = free_msgmaps;
364 free_msgmaps = msghdr->msg_spot;
366 msghdr->msg_spot = next;
367 if (msghdr->msg_ts >= msginfo.msgssz)
368 msghdr->msg_ts -= msginfo.msgssz;
372 if (msghdr->msg_spot != -1)
373 panic("msghdr->msg_spot != -1");
374 msghdr->msg_next = free_msghdrs;
375 free_msghdrs = msghdr;
377 mac_cleanup_sysv_msgmsg(msghdr);
381 #ifndef _SYS_SYSPROTO_H_
385 struct msqid_ds *buf;
395 register struct msgctl_args *uap;
397 int msqid = uap->msqid;
399 struct msqid_ds msqbuf;
402 DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, uap->buf));
403 if (cmd == IPC_SET &&
404 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
406 error = kern_msgctl(td, msqid, cmd, &msqbuf);
407 if (cmd == IPC_STAT && error == 0)
408 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
413 kern_msgctl(td, msqid, cmd, msqbuf)
417 struct msqid_ds *msqbuf;
419 int rval, error, msqix;
420 register struct msqid_kernel *msqkptr;
422 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
425 msqix = IPCID_TO_IX(msqid);
427 if (msqix < 0 || msqix >= msginfo.msgmni) {
428 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
433 msqkptr = &msqids[msqix];
436 if (msqkptr->u.msg_qbytes == 0) {
437 DPRINTF(("no such msqid\n"));
441 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
442 DPRINTF(("wrong sequence number\n"));
447 error = mac_check_sysv_msqctl(td->td_ucred, msqkptr, cmd);
449 MPRINTF(("mac_check_sysv_msqctl returned %d\n", error));
462 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
467 * Check that the thread has MAC access permissions to
468 * individual msghdrs. Note: We need to do this in a
469 * separate loop because the actual loop alters the
470 * msq/msghdr info as it progresses, and there is no going
471 * back if half the way through we discover that the
472 * thread cannot free a certain msghdr. The msq will get
473 * into an inconsistent state.
475 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
476 msghdr = msghdr->msg_next) {
477 error = mac_check_sysv_msgrmid(td->td_ucred, msghdr);
479 MPRINTF(("mac_check_sysv_msgrmid returned %d\n",
486 /* Free the message headers */
487 msghdr = msqkptr->u.msg_first;
488 while (msghdr != NULL) {
489 struct msg *msghdr_tmp;
491 /* Free the segments of each message */
492 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
493 msqkptr->u.msg_qnum--;
495 msghdr = msghdr->msg_next;
496 msg_freehdr(msghdr_tmp);
499 if (msqkptr->u.msg_cbytes != 0)
500 panic("msg_cbytes is screwed up");
501 if (msqkptr->u.msg_qnum != 0)
502 panic("msg_qnum is screwed up");
504 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
507 mac_cleanup_sysv_msgqueue(msqkptr);
516 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
518 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
523 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
524 DPRINTF(("can't increase msg_qbytes beyond %d"
525 "(truncating)\n", msginfo.msgmnb));
526 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
528 if (msqbuf->msg_qbytes == 0) {
529 DPRINTF(("can't reduce msg_qbytes to 0\n"));
530 error = EINVAL; /* non-standard errno! */
533 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
534 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
535 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
536 (msqbuf->msg_perm.mode & 0777);
537 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
538 msqkptr->u.msg_ctime = time_second;
542 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
543 DPRINTF(("requester doesn't have read access\n"));
546 *msqbuf = msqkptr->u;
550 DPRINTF(("invalid command %d\n", cmd));
556 td->td_retval[0] = rval;
558 mtx_unlock(&msq_mtx);
562 #ifndef _SYS_SYSPROTO_H_
575 register struct msgget_args *uap;
577 int msqid, error = 0;
579 int msgflg = uap->msgflg;
580 struct ucred *cred = td->td_ucred;
581 register struct msqid_kernel *msqkptr = NULL;
583 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
585 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
589 if (key != IPC_PRIVATE) {
590 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
591 msqkptr = &msqids[msqid];
592 if (msqkptr->u.msg_qbytes != 0 &&
593 msqkptr->u.msg_perm.key == key)
596 if (msqid < msginfo.msgmni) {
597 DPRINTF(("found public key\n"));
598 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
599 DPRINTF(("not exclusive\n"));
603 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
605 DPRINTF(("requester doesn't have 0%o access\n",
610 error = mac_check_sysv_msqget(cred, msqkptr);
612 MPRINTF(("mac_check_sysv_msqget returned %d\n",
621 DPRINTF(("need to allocate the msqid_ds\n"));
622 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
623 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
625 * Look for an unallocated and unlocked msqid_ds.
626 * msqid_ds's can be locked by msgsnd or msgrcv while
627 * they are copying the message in/out. We can't
628 * re-use the entry until they release it.
630 msqkptr = &msqids[msqid];
631 if (msqkptr->u.msg_qbytes == 0 &&
632 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
635 if (msqid == msginfo.msgmni) {
636 DPRINTF(("no more msqid_ds's available\n"));
640 DPRINTF(("msqid %d is available\n", msqid));
641 msqkptr->u.msg_perm.key = key;
642 msqkptr->u.msg_perm.cuid = cred->cr_uid;
643 msqkptr->u.msg_perm.uid = cred->cr_uid;
644 msqkptr->u.msg_perm.cgid = cred->cr_gid;
645 msqkptr->u.msg_perm.gid = cred->cr_gid;
646 msqkptr->u.msg_perm.mode = (msgflg & 0777);
647 /* Make sure that the returned msqid is unique */
648 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
649 msqkptr->u.msg_first = NULL;
650 msqkptr->u.msg_last = NULL;
651 msqkptr->u.msg_cbytes = 0;
652 msqkptr->u.msg_qnum = 0;
653 msqkptr->u.msg_qbytes = msginfo.msgmnb;
654 msqkptr->u.msg_lspid = 0;
655 msqkptr->u.msg_lrpid = 0;
656 msqkptr->u.msg_stime = 0;
657 msqkptr->u.msg_rtime = 0;
658 msqkptr->u.msg_ctime = time_second;
660 mac_create_sysv_msgqueue(cred, msqkptr);
663 DPRINTF(("didn't find it and wasn't asked to create it\n"));
669 /* Construct the unique msqid */
670 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
672 mtx_unlock(&msq_mtx);
676 #ifndef _SYS_SYSPROTO_H_
691 register struct msgsnd_args *uap;
693 int msqid = uap->msqid;
694 const void *user_msgp = uap->msgp;
695 size_t msgsz = uap->msgsz;
696 int msgflg = uap->msgflg;
697 int segs_needed, error = 0;
698 register struct msqid_kernel *msqkptr;
699 register struct msg *msghdr;
702 DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
704 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
708 msqid = IPCID_TO_IX(msqid);
710 if (msqid < 0 || msqid >= msginfo.msgmni) {
711 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
717 msqkptr = &msqids[msqid];
718 if (msqkptr->u.msg_qbytes == 0) {
719 DPRINTF(("no such message queue id\n"));
723 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
724 DPRINTF(("wrong sequence number\n"));
729 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
730 DPRINTF(("requester doesn't have write access\n"));
735 error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr);
737 MPRINTF(("mac_check_sysv_msqsnd returned %d\n", error));
742 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
743 DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
746 int need_more_resources = 0;
750 * (inside this loop in case msg_qbytes changes while we sleep)
753 if (msgsz > msqkptr->u.msg_qbytes) {
754 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
759 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
760 DPRINTF(("msqid is locked\n"));
761 need_more_resources = 1;
763 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
764 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
765 need_more_resources = 1;
767 if (segs_needed > nfree_msgmaps) {
768 DPRINTF(("segs_needed > nfree_msgmaps\n"));
769 need_more_resources = 1;
771 if (free_msghdrs == NULL) {
772 DPRINTF(("no more msghdrs\n"));
773 need_more_resources = 1;
776 if (need_more_resources) {
779 if ((msgflg & IPC_NOWAIT) != 0) {
780 DPRINTF(("need more resources but caller "
781 "doesn't want to wait\n"));
786 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
787 DPRINTF(("we don't own the msqid_ds\n"));
790 /* Force later arrivals to wait for our
792 DPRINTF(("we own the msqid_ds\n"));
793 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
796 DPRINTF(("goodnight\n"));
797 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
799 DPRINTF(("good morning, error=%d\n", error));
801 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
803 DPRINTF(("msgsnd: interrupted system call\n"));
809 * Make sure that the msq queue still exists
812 if (msqkptr->u.msg_qbytes == 0) {
813 DPRINTF(("msqid deleted\n"));
819 DPRINTF(("got all the resources that we need\n"));
825 * We have the resources that we need.
829 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
830 panic("msg_perm.mode & MSG_LOCKED");
831 if (segs_needed > nfree_msgmaps)
832 panic("segs_needed > nfree_msgmaps");
833 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
834 panic("msgsz + msg_cbytes > msg_qbytes");
835 if (free_msghdrs == NULL)
836 panic("no more msghdrs");
839 * Re-lock the msqid_ds in case we page-fault when copying in the
843 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
844 panic("msqid_ds is already locked");
845 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
848 * Allocate a message header
851 msghdr = free_msghdrs;
852 free_msghdrs = msghdr->msg_next;
853 msghdr->msg_spot = -1;
854 msghdr->msg_ts = msgsz;
857 * XXXMAC: Should the mac_check_sysv_msgmsq check follow here
858 * immediately? Or, should it be checked just before the msg is
859 * enqueued in the msgq (as it is done now)?
861 mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr);
865 * Allocate space for the message
868 while (segs_needed > 0) {
869 if (nfree_msgmaps <= 0)
870 panic("not enough msgmaps");
871 if (free_msgmaps == -1)
872 panic("nil free_msgmaps");
875 panic("next too low #1");
876 if (next >= msginfo.msgseg)
877 panic("next out of range #1");
878 DPRINTF(("allocating segment %d to message\n", next));
879 free_msgmaps = msgmaps[next].next;
881 msgmaps[next].next = msghdr->msg_spot;
882 msghdr->msg_spot = next;
887 * Copy in the message type
890 mtx_unlock(&msq_mtx);
891 if ((error = copyin(user_msgp, &msghdr->msg_type,
892 sizeof(msghdr->msg_type))) != 0) {
894 DPRINTF(("error %d copying the message type\n", error));
896 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
901 user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type);
904 * Validate the message type
907 if (msghdr->msg_type < 1) {
909 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
911 DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
917 * Copy in the message body
920 next = msghdr->msg_spot;
923 if (msgsz > msginfo.msgssz)
924 tlen = msginfo.msgssz;
928 panic("next too low #2");
929 if (next >= msginfo.msgseg)
930 panic("next out of range #2");
931 mtx_unlock(&msq_mtx);
932 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
935 DPRINTF(("error %d copying in message segment\n",
938 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
944 user_msgp = (const char *)user_msgp + tlen;
945 next = msgmaps[next].next;
948 panic("didn't use all the msg segments");
951 * We've got the message. Unlock the msqid_ds.
954 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
957 * Make sure that the msqid_ds is still allocated.
960 if (msqkptr->u.msg_qbytes == 0) {
969 * Note: Since the task/thread allocates the msghdr and usually
970 * primes it with its own MAC label, for a majority of policies, it
971 * won't be necessary to check whether the msghdr has access
972 * permissions to the msgq. The mac_check_sysv_msqsnd check would
973 * suffice in that case. However, this hook may be required where
974 * individual policies derive a non-identical label for the msghdr
975 * from the current thread label and may want to check the msghdr
976 * enqueue permissions, along with read/write permissions to the
979 error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr);
981 MPRINTF(("mac_check_sysv_msqmsq returned %d\n", error));
989 * Put the message into the queue
991 if (msqkptr->u.msg_first == NULL) {
992 msqkptr->u.msg_first = msghdr;
993 msqkptr->u.msg_last = msghdr;
995 msqkptr->u.msg_last->msg_next = msghdr;
996 msqkptr->u.msg_last = msghdr;
998 msqkptr->u.msg_last->msg_next = NULL;
1000 msqkptr->u.msg_cbytes += msghdr->msg_ts;
1001 msqkptr->u.msg_qnum++;
1002 msqkptr->u.msg_lspid = td->td_proc->p_pid;
1003 msqkptr->u.msg_stime = time_second;
1006 td->td_retval[0] = 0;
1008 mtx_unlock(&msq_mtx);
1012 #ifndef _SYS_SYSPROTO_H_
1013 struct msgrcv_args {
1028 register struct msgrcv_args *uap;
1030 int msqid = uap->msqid;
1031 void *user_msgp = uap->msgp;
1032 size_t msgsz = uap->msgsz;
1033 long msgtyp = uap->msgtyp;
1034 int msgflg = uap->msgflg;
1036 register struct msqid_kernel *msqkptr;
1037 register struct msg *msghdr;
1041 DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
1042 msgsz, msgtyp, msgflg));
1044 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1047 msqid = IPCID_TO_IX(msqid);
1049 if (msqid < 0 || msqid >= msginfo.msgmni) {
1050 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
1055 msqkptr = &msqids[msqid];
1057 if (msqkptr->u.msg_qbytes == 0) {
1058 DPRINTF(("no such message queue id\n"));
1062 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1063 DPRINTF(("wrong sequence number\n"));
1068 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1069 DPRINTF(("requester doesn't have read access\n"));
1074 error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr);
1076 MPRINTF(("mac_check_sysv_msqrcv returned %d\n", error));
1082 while (msghdr == NULL) {
1084 msghdr = msqkptr->u.msg_first;
1085 if (msghdr != NULL) {
1086 if (msgsz < msghdr->msg_ts &&
1087 (msgflg & MSG_NOERROR) == 0) {
1088 DPRINTF(("first message on the queue "
1089 "is too big (want %d, got %d)\n",
1090 msgsz, msghdr->msg_ts));
1095 error = mac_check_sysv_msgrcv(td->td_ucred,
1098 MPRINTF(("mac_check_sysv_msgrcv "
1099 "returned %d\n", error));
1103 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1104 msqkptr->u.msg_first = NULL;
1105 msqkptr->u.msg_last = NULL;
1107 msqkptr->u.msg_first = msghdr->msg_next;
1108 if (msqkptr->u.msg_first == NULL)
1109 panic("msg_first/last screwed up #1");
1113 struct msg *previous;
1117 prev = &(msqkptr->u.msg_first);
1118 while ((msghdr = *prev) != NULL) {
1120 * Is this message's type an exact match or is
1121 * this message's type less than or equal to
1122 * the absolute value of a negative msgtyp?
1123 * Note that the second half of this test can
1124 * NEVER be true if msgtyp is positive since
1125 * msg_type is always positive!
1128 if (msgtyp == msghdr->msg_type ||
1129 msghdr->msg_type <= -msgtyp) {
1130 DPRINTF(("found message type %d, "
1132 msghdr->msg_type, msgtyp));
1133 if (msgsz < msghdr->msg_ts &&
1134 (msgflg & MSG_NOERROR) == 0) {
1135 DPRINTF(("requested message "
1136 "on the queue is too big "
1137 "(want %d, got %d)\n",
1138 msgsz, msghdr->msg_ts));
1143 error = mac_check_sysv_msgrcv(
1144 td->td_ucred, msghdr);
1146 MPRINTF(("mac_check_sysv_"
1147 "msgrcv returned %d\n",
1152 *prev = msghdr->msg_next;
1153 if (msghdr == msqkptr->u.msg_last) {
1154 if (previous == NULL) {
1156 &msqkptr->u.msg_first)
1157 panic("msg_first/last screwed up #2");
1158 msqkptr->u.msg_first =
1160 msqkptr->u.msg_last =
1164 &msqkptr->u.msg_first)
1165 panic("msg_first/last screwed up #3");
1166 msqkptr->u.msg_last =
1173 prev = &(msghdr->msg_next);
1178 * We've either extracted the msghdr for the appropriate
1179 * message or there isn't one.
1180 * If there is one then bail out of this loop.
1187 * Hmph! No message found. Does the user want to wait?
1190 if ((msgflg & IPC_NOWAIT) != 0) {
1191 DPRINTF(("no appropriate message found (msgtyp=%d)\n",
1193 /* The SVID says to return ENOMSG. */
1199 * Wait for something to happen
1202 DPRINTF(("msgrcv: goodnight\n"));
1203 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1205 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1208 DPRINTF(("msgsnd: interrupted system call\n"));
1214 * Make sure that the msq queue still exists
1217 if (msqkptr->u.msg_qbytes == 0 ||
1218 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1219 DPRINTF(("msqid deleted\n"));
1226 * Return the message to the user.
1228 * First, do the bookkeeping (before we risk being interrupted).
1231 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1232 msqkptr->u.msg_qnum--;
1233 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1234 msqkptr->u.msg_rtime = time_second;
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).
1242 DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1244 if (msgsz > msghdr->msg_ts)
1245 msgsz = msghdr->msg_ts;
1248 * Return the type to the user.
1251 mtx_unlock(&msq_mtx);
1252 error = copyout(&(msghdr->msg_type), user_msgp,
1253 sizeof(msghdr->msg_type));
1256 DPRINTF(("error (%d) copying out message type\n", error));
1257 msg_freehdr(msghdr);
1261 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1264 * Return the segments to the user
1267 next = msghdr->msg_spot;
1268 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1271 if (msgsz - len > msginfo.msgssz)
1272 tlen = msginfo.msgssz;
1276 panic("next too low #3");
1277 if (next >= msginfo.msgseg)
1278 panic("next out of range #3");
1279 mtx_unlock(&msq_mtx);
1280 error = copyout(&msgpool[next * msginfo.msgssz],
1284 DPRINTF(("error (%d) copying out message segment\n",
1286 msg_freehdr(msghdr);
1290 user_msgp = (char *)user_msgp + tlen;
1291 next = msgmaps[next].next;
1295 * Done, return the actual number of bytes copied out.
1298 msg_freehdr(msghdr);
1300 td->td_retval[0] = msgsz;
1302 mtx_unlock(&msq_mtx);
1307 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1310 return (SYSCTL_OUT(req, msqids,
1311 sizeof(struct msqid_kernel) * msginfo.msgmni));
1314 SYSCTL_DECL(_kern_ipc);
1315 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1316 "Maximum message size");
1317 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1318 "Number of message queue identifiers");
1319 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1320 "Maximum number of bytes in a queue");
1321 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1322 "Maximum number of messages in the system");
1323 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1324 "Size of a message segment");
1325 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1326 "Number of message segments");
1327 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1328 NULL, 0, sysctl_msqids, "", "Message queue IDs");