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, %p)\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_
686 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
689 const void *msgp; /* XXX msgp is actually mtext. */
694 int msqix, segs_needed, error = 0;
695 register struct msqid_kernel *msqkptr;
696 register struct msg *msghdr;
699 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
703 msqix = IPCID_TO_IX(msqid);
705 if (msqix < 0 || msqix >= msginfo.msgmni) {
706 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
712 msqkptr = &msqids[msqix];
713 if (msqkptr->u.msg_qbytes == 0) {
714 DPRINTF(("no such message queue id\n"));
718 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
719 DPRINTF(("wrong sequence number\n"));
724 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
725 DPRINTF(("requester doesn't have write access\n"));
730 error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr);
732 MPRINTF(("mac_check_sysv_msqsnd returned %d\n", error));
737 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
738 DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
739 msginfo.msgssz, segs_needed));
741 int need_more_resources = 0;
745 * (inside this loop in case msg_qbytes changes while we sleep)
748 if (msgsz > msqkptr->u.msg_qbytes) {
749 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
754 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
755 DPRINTF(("msqid is locked\n"));
756 need_more_resources = 1;
758 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
759 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
760 need_more_resources = 1;
762 if (segs_needed > nfree_msgmaps) {
763 DPRINTF(("segs_needed > nfree_msgmaps\n"));
764 need_more_resources = 1;
766 if (free_msghdrs == NULL) {
767 DPRINTF(("no more msghdrs\n"));
768 need_more_resources = 1;
771 if (need_more_resources) {
774 if ((msgflg & IPC_NOWAIT) != 0) {
775 DPRINTF(("need more resources but caller "
776 "doesn't want to wait\n"));
781 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
782 DPRINTF(("we don't own the msqid_ds\n"));
785 /* Force later arrivals to wait for our
787 DPRINTF(("we own the msqid_ds\n"));
788 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
791 DPRINTF(("goodnight\n"));
792 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
794 DPRINTF(("good morning, error=%d\n", error));
796 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
798 DPRINTF(("msgsnd: interrupted system call\n"));
804 * Make sure that the msq queue still exists
807 if (msqkptr->u.msg_qbytes == 0) {
808 DPRINTF(("msqid deleted\n"));
814 DPRINTF(("got all the resources that we need\n"));
820 * We have the resources that we need.
824 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
825 panic("msg_perm.mode & MSG_LOCKED");
826 if (segs_needed > nfree_msgmaps)
827 panic("segs_needed > nfree_msgmaps");
828 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
829 panic("msgsz + msg_cbytes > msg_qbytes");
830 if (free_msghdrs == NULL)
831 panic("no more msghdrs");
834 * Re-lock the msqid_ds in case we page-fault when copying in the
838 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
839 panic("msqid_ds is already locked");
840 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
843 * Allocate a message header
846 msghdr = free_msghdrs;
847 free_msghdrs = msghdr->msg_next;
848 msghdr->msg_spot = -1;
849 msghdr->msg_ts = msgsz;
850 msghdr->msg_type = mtype;
853 * XXXMAC: Should the mac_check_sysv_msgmsq check follow here
854 * immediately? Or, should it be checked just before the msg is
855 * enqueued in the msgq (as it is done now)?
857 mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr);
861 * Allocate space for the message
864 while (segs_needed > 0) {
865 if (nfree_msgmaps <= 0)
866 panic("not enough msgmaps");
867 if (free_msgmaps == -1)
868 panic("nil free_msgmaps");
871 panic("next too low #1");
872 if (next >= msginfo.msgseg)
873 panic("next out of range #1");
874 DPRINTF(("allocating segment %d to message\n", next));
875 free_msgmaps = msgmaps[next].next;
877 msgmaps[next].next = msghdr->msg_spot;
878 msghdr->msg_spot = next;
883 * Validate the message type
886 if (msghdr->msg_type < 1) {
888 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
890 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
896 * Copy in the message body
899 next = msghdr->msg_spot;
902 if (msgsz > msginfo.msgssz)
903 tlen = msginfo.msgssz;
907 panic("next too low #2");
908 if (next >= msginfo.msgseg)
909 panic("next out of range #2");
910 mtx_unlock(&msq_mtx);
911 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
914 DPRINTF(("error %d copying in message segment\n",
917 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
923 msgp = (const char *)msgp + tlen;
924 next = msgmaps[next].next;
927 panic("didn't use all the msg segments");
930 * We've got the message. Unlock the msqid_ds.
933 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
936 * Make sure that the msqid_ds is still allocated.
939 if (msqkptr->u.msg_qbytes == 0) {
948 * Note: Since the task/thread allocates the msghdr and usually
949 * primes it with its own MAC label, for a majority of policies, it
950 * won't be necessary to check whether the msghdr has access
951 * permissions to the msgq. The mac_check_sysv_msqsnd check would
952 * suffice in that case. However, this hook may be required where
953 * individual policies derive a non-identical label for the msghdr
954 * from the current thread label and may want to check the msghdr
955 * enqueue permissions, along with read/write permissions to the
958 error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr);
960 MPRINTF(("mac_check_sysv_msqmsq returned %d\n", error));
968 * Put the message into the queue
970 if (msqkptr->u.msg_first == NULL) {
971 msqkptr->u.msg_first = msghdr;
972 msqkptr->u.msg_last = msghdr;
974 msqkptr->u.msg_last->msg_next = msghdr;
975 msqkptr->u.msg_last = msghdr;
977 msqkptr->u.msg_last->msg_next = NULL;
979 msqkptr->u.msg_cbytes += msghdr->msg_ts;
980 msqkptr->u.msg_qnum++;
981 msqkptr->u.msg_lspid = td->td_proc->p_pid;
982 msqkptr->u.msg_stime = time_second;
985 td->td_retval[0] = 0;
987 mtx_unlock(&msq_mtx);
997 register struct msgsnd_args *uap;
1002 DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1003 uap->msgsz, uap->msgflg));
1005 if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1006 DPRINTF(("error %d copying the message type\n", error));
1009 return (kern_msgsnd(td, uap->msqid,
1010 (const char *)uap->msgp + sizeof(mtype),
1011 uap->msgsz, uap->msgflg, mtype));
1014 #ifndef _SYS_SYSPROTO_H_
1015 struct msgrcv_args {
1025 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1028 void *msgp; /* XXX msgp is actually mtext. */
1035 register struct msqid_kernel *msqkptr;
1036 register struct msg *msghdr;
1037 int msqix, error = 0;
1040 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1043 msqix = IPCID_TO_IX(msqid);
1045 if (msqix < 0 || msqix >= msginfo.msgmni) {
1046 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1051 msqkptr = &msqids[msqix];
1053 if (msqkptr->u.msg_qbytes == 0) {
1054 DPRINTF(("no such message queue id\n"));
1058 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1059 DPRINTF(("wrong sequence number\n"));
1064 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1065 DPRINTF(("requester doesn't have read access\n"));
1070 error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr);
1072 MPRINTF(("mac_check_sysv_msqrcv returned %d\n", error));
1078 while (msghdr == NULL) {
1080 msghdr = msqkptr->u.msg_first;
1081 if (msghdr != NULL) {
1082 if (msgsz < msghdr->msg_ts &&
1083 (msgflg & MSG_NOERROR) == 0) {
1084 DPRINTF(("first message on the queue "
1085 "is too big (want %zu, got %d)\n",
1086 msgsz, msghdr->msg_ts));
1091 error = mac_check_sysv_msgrcv(td->td_ucred,
1094 MPRINTF(("mac_check_sysv_msgrcv "
1095 "returned %d\n", error));
1099 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1100 msqkptr->u.msg_first = NULL;
1101 msqkptr->u.msg_last = NULL;
1103 msqkptr->u.msg_first = msghdr->msg_next;
1104 if (msqkptr->u.msg_first == NULL)
1105 panic("msg_first/last screwed up #1");
1109 struct msg *previous;
1113 prev = &(msqkptr->u.msg_first);
1114 while ((msghdr = *prev) != NULL) {
1116 * Is this message's type an exact match or is
1117 * this message's type less than or equal to
1118 * the absolute value of a negative msgtyp?
1119 * Note that the second half of this test can
1120 * NEVER be true if msgtyp is positive since
1121 * msg_type is always positive!
1124 if (msgtyp == msghdr->msg_type ||
1125 msghdr->msg_type <= -msgtyp) {
1126 DPRINTF(("found message type %ld, "
1128 msghdr->msg_type, msgtyp));
1129 if (msgsz < msghdr->msg_ts &&
1130 (msgflg & MSG_NOERROR) == 0) {
1131 DPRINTF(("requested message "
1132 "on the queue is too big "
1133 "(want %zu, got %hu)\n",
1134 msgsz, msghdr->msg_ts));
1139 error = mac_check_sysv_msgrcv(
1140 td->td_ucred, msghdr);
1142 MPRINTF(("mac_check_sysv_"
1143 "msgrcv returned %d\n",
1148 *prev = msghdr->msg_next;
1149 if (msghdr == msqkptr->u.msg_last) {
1150 if (previous == NULL) {
1152 &msqkptr->u.msg_first)
1153 panic("msg_first/last screwed up #2");
1154 msqkptr->u.msg_first =
1156 msqkptr->u.msg_last =
1160 &msqkptr->u.msg_first)
1161 panic("msg_first/last screwed up #3");
1162 msqkptr->u.msg_last =
1169 prev = &(msghdr->msg_next);
1174 * We've either extracted the msghdr for the appropriate
1175 * message or there isn't one.
1176 * If there is one then bail out of this loop.
1183 * Hmph! No message found. Does the user want to wait?
1186 if ((msgflg & IPC_NOWAIT) != 0) {
1187 DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1189 /* The SVID says to return ENOMSG. */
1195 * Wait for something to happen
1198 DPRINTF(("msgrcv: goodnight\n"));
1199 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1201 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1204 DPRINTF(("msgsnd: interrupted system call\n"));
1210 * Make sure that the msq queue still exists
1213 if (msqkptr->u.msg_qbytes == 0 ||
1214 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1215 DPRINTF(("msqid deleted\n"));
1222 * Return the message to the user.
1224 * First, do the bookkeeping (before we risk being interrupted).
1227 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1228 msqkptr->u.msg_qnum--;
1229 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1230 msqkptr->u.msg_rtime = time_second;
1233 * Make msgsz the actual amount that we'll be returning.
1234 * Note that this effectively truncates the message if it is too long
1235 * (since msgsz is never increased).
1238 DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1240 if (msgsz > msghdr->msg_ts)
1241 msgsz = msghdr->msg_ts;
1242 *mtype = msghdr->msg_type;
1245 * Return the segments to the user
1248 next = msghdr->msg_spot;
1249 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1252 if (msgsz - len > msginfo.msgssz)
1253 tlen = msginfo.msgssz;
1257 panic("next too low #3");
1258 if (next >= msginfo.msgseg)
1259 panic("next out of range #3");
1260 mtx_unlock(&msq_mtx);
1261 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1264 DPRINTF(("error (%d) copying out message segment\n",
1266 msg_freehdr(msghdr);
1270 msgp = (char *)msgp + tlen;
1271 next = msgmaps[next].next;
1275 * Done, return the actual number of bytes copied out.
1278 msg_freehdr(msghdr);
1280 td->td_retval[0] = msgsz;
1282 mtx_unlock(&msq_mtx);
1292 register struct msgrcv_args *uap;
1297 DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1298 uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1300 if ((error = kern_msgrcv(td, uap->msqid,
1301 (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1302 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1304 if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1305 DPRINTF(("error %d copying the message type\n", error));
1310 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1313 return (SYSCTL_OUT(req, msqids,
1314 sizeof(struct msqid_kernel) * msginfo.msgmni));
1317 SYSCTL_DECL(_kern_ipc);
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, CTLFLAG_RD,
1331 NULL, 0, sysctl_msqids, "", "Message queue IDs");