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>
62 #include <sys/mutex.h>
63 #include <sys/module.h>
65 #include <sys/syscall.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysent.h>
68 #include <sys/sysctl.h>
69 #include <sys/malloc.h>
72 #include <security/mac/mac_framework.h>
74 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
76 static void msginit(void);
77 static int msgunload(void);
78 static int sysvmsg_modload(struct module *, int, void *);
81 #define DPRINTF(a) printf a
86 static void msg_freehdr(struct msg *msghdr);
88 /* XXX casting to (sy_call_t *) is bogus, as usual. */
89 static sy_call_t *msgcalls[] = {
90 (sy_call_t *)msgctl, (sy_call_t *)msgget,
91 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
95 #define MSGSSZ 8 /* Each segment must be 2^N long */
98 #define MSGSEG 2048 /* must be less than 32767 */
100 #define MSGMAX (MSGSSZ*MSGSEG)
102 #define MSGMNB 2048 /* max # of bytes in a queue */
112 * Based on the configuration parameters described in an SVR2 (yes, two)
113 * config(1m) man page.
115 * Each message is broken up and stored in segments that are msgssz bytes
116 * long. For efficiency reasons, this should be a power of two. Also,
117 * it doesn't make sense if it is less than 8 or greater than about 256.
118 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
119 * two between 8 and 1024 inclusive (and panic's if it isn't).
121 struct msginfo msginfo = {
122 MSGMAX, /* max chars in a message */
123 MSGMNI, /* # of message queue identifiers */
124 MSGMNB, /* max chars in a queue */
125 MSGTQL, /* max messages in system */
126 MSGSSZ, /* size of a message segment */
127 /* (must be small power of 2 greater than 4) */
128 MSGSEG /* number of message segments */
132 * macros to convert between msqid_ds's and msqid's.
133 * (specific to this implementation)
135 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
136 #define MSQID_IX(id) ((id) & 0xffff)
137 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
140 * The rest of this file is specific to this particular implementation.
144 short next; /* next segment in buffer */
145 /* -1 -> available */
146 /* 0..(MSGSEG-1) -> index of next segment */
149 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
151 static int nfree_msgmaps; /* # of free map entries */
152 static short free_msgmaps; /* head of linked list of free map entries */
153 static struct msg *free_msghdrs;/* list of free msg headers */
154 static char *msgpool; /* MSGMAX byte long msg buffer pool */
155 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
156 static struct msg *msghdrs; /* MSGTQL msg headers */
157 static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */
158 static struct mtx msq_mtx; /* global mutex for message queues. */
165 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
166 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
167 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
168 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
169 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
170 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
172 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
174 panic("msgpool is NULL");
175 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
177 panic("msgmaps is NULL");
178 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
180 panic("msghdrs is NULL");
181 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
184 panic("msqids is NULL");
187 * msginfo.msgssz should be a power of two for efficiency reasons.
188 * It is also pretty silly if msginfo.msgssz is less than 8
189 * or greater than about 256 so ...
193 while (i < 1024 && i != msginfo.msgssz)
195 if (i != msginfo.msgssz) {
196 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
198 panic("msginfo.msgssz not a small power of 2");
201 if (msginfo.msgseg > 32767) {
202 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
203 panic("msginfo.msgseg > 32767");
207 panic("msgmaps is NULL");
209 for (i = 0; i < msginfo.msgseg; i++) {
211 msgmaps[i-1].next = i;
212 msgmaps[i].next = -1; /* implies entry is available */
215 nfree_msgmaps = msginfo.msgseg;
218 panic("msghdrs is NULL");
220 for (i = 0; i < msginfo.msgtql; i++) {
221 msghdrs[i].msg_type = 0;
223 msghdrs[i-1].msg_next = &msghdrs[i];
224 msghdrs[i].msg_next = NULL;
226 mac_init_sysv_msgmsg(&msghdrs[i]);
229 free_msghdrs = &msghdrs[0];
232 panic("msqids is NULL");
234 for (i = 0; i < msginfo.msgmni; i++) {
235 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
236 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
237 msqids[i].u.msg_perm.mode = 0;
239 mac_init_sysv_msgqueue(&msqids[i]);
242 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
248 struct msqid_kernel *msqkptr;
254 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
256 * Look for an unallocated and unlocked msqid_ds.
257 * msqid_ds's can be locked by msgsnd or msgrcv while
258 * they are copying the message in/out. We can't
259 * re-use the entry until they release it.
261 msqkptr = &msqids[msqid];
262 if (msqkptr->u.msg_qbytes != 0 ||
263 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
266 if (msqid != msginfo.msgmni)
270 for (i = 0; i < msginfo.msgtql; i++)
271 mac_destroy_sysv_msgmsg(&msghdrs[i]);
272 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
273 mac_destroy_sysv_msgqueue(&msqids[msqid]);
275 free(msgpool, M_MSG);
276 free(msgmaps, M_MSG);
277 free(msghdrs, M_MSG);
279 mtx_destroy(&msq_mtx);
285 sysvmsg_modload(struct module *module, int cmd, void *arg)
305 static moduledata_t sysvmsg_mod = {
311 SYSCALL_MODULE_HELPER(msgsys);
312 SYSCALL_MODULE_HELPER(msgctl);
313 SYSCALL_MODULE_HELPER(msgget);
314 SYSCALL_MODULE_HELPER(msgsnd);
315 SYSCALL_MODULE_HELPER(msgrcv);
317 DECLARE_MODULE(sysvmsg, sysvmsg_mod,
318 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
319 MODULE_VERSION(sysvmsg, 1);
322 * Entry point for all MSG calls
329 /* XXX actually varargs. */
330 struct msgsys_args /* {
341 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
343 if (uap->which < 0 ||
344 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
346 error = (*msgcalls[uap->which])(td, &uap->a2);
354 while (msghdr->msg_ts > 0) {
356 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
357 panic("msghdr->msg_spot out of range");
358 next = msgmaps[msghdr->msg_spot].next;
359 msgmaps[msghdr->msg_spot].next = free_msgmaps;
360 free_msgmaps = msghdr->msg_spot;
362 msghdr->msg_spot = next;
363 if (msghdr->msg_ts >= msginfo.msgssz)
364 msghdr->msg_ts -= msginfo.msgssz;
368 if (msghdr->msg_spot != -1)
369 panic("msghdr->msg_spot != -1");
370 msghdr->msg_next = free_msghdrs;
371 free_msghdrs = msghdr;
373 mac_cleanup_sysv_msgmsg(msghdr);
377 #ifndef _SYS_SYSPROTO_H_
381 struct msqid_ds *buf;
391 register struct msgctl_args *uap;
393 int msqid = uap->msqid;
395 struct msqid_ds msqbuf;
398 DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, uap->buf));
399 if (cmd == IPC_SET &&
400 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
402 error = kern_msgctl(td, msqid, cmd, &msqbuf);
403 if (cmd == IPC_STAT && error == 0)
404 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
409 kern_msgctl(td, msqid, cmd, msqbuf)
413 struct msqid_ds *msqbuf;
415 int rval, error, msqix;
416 register struct msqid_kernel *msqkptr;
418 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
421 msqix = IPCID_TO_IX(msqid);
423 if (msqix < 0 || msqix >= msginfo.msgmni) {
424 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
429 msqkptr = &msqids[msqix];
432 if (msqkptr->u.msg_qbytes == 0) {
433 DPRINTF(("no such msqid\n"));
437 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
438 DPRINTF(("wrong sequence number\n"));
443 error = mac_check_sysv_msqctl(td->td_ucred, msqkptr, cmd);
456 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
461 * Check that the thread has MAC access permissions to
462 * individual msghdrs. Note: We need to do this in a
463 * separate loop because the actual loop alters the
464 * msq/msghdr info as it progresses, and there is no going
465 * back if half the way through we discover that the
466 * thread cannot free a certain msghdr. The msq will get
467 * into an inconsistent state.
469 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
470 msghdr = msghdr->msg_next) {
471 error = mac_check_sysv_msgrmid(td->td_ucred, msghdr);
477 /* Free the message headers */
478 msghdr = msqkptr->u.msg_first;
479 while (msghdr != NULL) {
480 struct msg *msghdr_tmp;
482 /* Free the segments of each message */
483 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
484 msqkptr->u.msg_qnum--;
486 msghdr = msghdr->msg_next;
487 msg_freehdr(msghdr_tmp);
490 if (msqkptr->u.msg_cbytes != 0)
491 panic("msg_cbytes is screwed up");
492 if (msqkptr->u.msg_qnum != 0)
493 panic("msg_qnum is screwed up");
495 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
498 mac_cleanup_sysv_msgqueue(msqkptr);
507 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
509 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
514 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
515 DPRINTF(("can't increase msg_qbytes beyond %d"
516 "(truncating)\n", msginfo.msgmnb));
517 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
519 if (msqbuf->msg_qbytes == 0) {
520 DPRINTF(("can't reduce msg_qbytes to 0\n"));
521 error = EINVAL; /* non-standard errno! */
524 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
525 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
526 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
527 (msqbuf->msg_perm.mode & 0777);
528 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
529 msqkptr->u.msg_ctime = time_second;
533 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
534 DPRINTF(("requester doesn't have read access\n"));
537 *msqbuf = msqkptr->u;
541 DPRINTF(("invalid command %d\n", cmd));
547 td->td_retval[0] = rval;
549 mtx_unlock(&msq_mtx);
553 #ifndef _SYS_SYSPROTO_H_
566 register struct msgget_args *uap;
568 int msqid, error = 0;
570 int msgflg = uap->msgflg;
571 struct ucred *cred = td->td_ucred;
572 register struct msqid_kernel *msqkptr = NULL;
574 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
576 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
580 if (key != IPC_PRIVATE) {
581 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
582 msqkptr = &msqids[msqid];
583 if (msqkptr->u.msg_qbytes != 0 &&
584 msqkptr->u.msg_perm.key == key)
587 if (msqid < msginfo.msgmni) {
588 DPRINTF(("found public key\n"));
589 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
590 DPRINTF(("not exclusive\n"));
594 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
596 DPRINTF(("requester doesn't have 0%o access\n",
601 error = mac_check_sysv_msqget(cred, msqkptr);
609 DPRINTF(("need to allocate the msqid_ds\n"));
610 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
611 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
613 * Look for an unallocated and unlocked msqid_ds.
614 * msqid_ds's can be locked by msgsnd or msgrcv while
615 * they are copying the message in/out. We can't
616 * re-use the entry until they release it.
618 msqkptr = &msqids[msqid];
619 if (msqkptr->u.msg_qbytes == 0 &&
620 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
623 if (msqid == msginfo.msgmni) {
624 DPRINTF(("no more msqid_ds's available\n"));
628 DPRINTF(("msqid %d is available\n", msqid));
629 msqkptr->u.msg_perm.key = key;
630 msqkptr->u.msg_perm.cuid = cred->cr_uid;
631 msqkptr->u.msg_perm.uid = cred->cr_uid;
632 msqkptr->u.msg_perm.cgid = cred->cr_gid;
633 msqkptr->u.msg_perm.gid = cred->cr_gid;
634 msqkptr->u.msg_perm.mode = (msgflg & 0777);
635 /* Make sure that the returned msqid is unique */
636 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
637 msqkptr->u.msg_first = NULL;
638 msqkptr->u.msg_last = NULL;
639 msqkptr->u.msg_cbytes = 0;
640 msqkptr->u.msg_qnum = 0;
641 msqkptr->u.msg_qbytes = msginfo.msgmnb;
642 msqkptr->u.msg_lspid = 0;
643 msqkptr->u.msg_lrpid = 0;
644 msqkptr->u.msg_stime = 0;
645 msqkptr->u.msg_rtime = 0;
646 msqkptr->u.msg_ctime = time_second;
648 mac_create_sysv_msgqueue(cred, msqkptr);
651 DPRINTF(("didn't find it and wasn't asked to create it\n"));
657 /* Construct the unique msqid */
658 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
660 mtx_unlock(&msq_mtx);
664 #ifndef _SYS_SYSPROTO_H_
679 register struct msgsnd_args *uap;
681 int msqid = uap->msqid;
682 const void *user_msgp = uap->msgp;
683 size_t msgsz = uap->msgsz;
684 int msgflg = uap->msgflg;
685 int segs_needed, error = 0;
686 register struct msqid_kernel *msqkptr;
687 register struct msg *msghdr;
690 DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
692 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
696 msqid = IPCID_TO_IX(msqid);
698 if (msqid < 0 || msqid >= msginfo.msgmni) {
699 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
705 msqkptr = &msqids[msqid];
706 if (msqkptr->u.msg_qbytes == 0) {
707 DPRINTF(("no such message queue id\n"));
711 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
712 DPRINTF(("wrong sequence number\n"));
717 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
718 DPRINTF(("requester doesn't have write access\n"));
723 error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr);
728 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
729 DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
732 int need_more_resources = 0;
736 * (inside this loop in case msg_qbytes changes while we sleep)
739 if (msgsz > msqkptr->u.msg_qbytes) {
740 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
745 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
746 DPRINTF(("msqid is locked\n"));
747 need_more_resources = 1;
749 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
750 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
751 need_more_resources = 1;
753 if (segs_needed > nfree_msgmaps) {
754 DPRINTF(("segs_needed > nfree_msgmaps\n"));
755 need_more_resources = 1;
757 if (free_msghdrs == NULL) {
758 DPRINTF(("no more msghdrs\n"));
759 need_more_resources = 1;
762 if (need_more_resources) {
765 if ((msgflg & IPC_NOWAIT) != 0) {
766 DPRINTF(("need more resources but caller "
767 "doesn't want to wait\n"));
772 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
773 DPRINTF(("we don't own the msqid_ds\n"));
776 /* Force later arrivals to wait for our
778 DPRINTF(("we own the msqid_ds\n"));
779 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
782 DPRINTF(("goodnight\n"));
783 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
785 DPRINTF(("good morning, error=%d\n", error));
787 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
789 DPRINTF(("msgsnd: interrupted system call\n"));
795 * Make sure that the msq queue still exists
798 if (msqkptr->u.msg_qbytes == 0) {
799 DPRINTF(("msqid deleted\n"));
805 DPRINTF(("got all the resources that we need\n"));
811 * We have the resources that we need.
815 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
816 panic("msg_perm.mode & MSG_LOCKED");
817 if (segs_needed > nfree_msgmaps)
818 panic("segs_needed > nfree_msgmaps");
819 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
820 panic("msgsz + msg_cbytes > msg_qbytes");
821 if (free_msghdrs == NULL)
822 panic("no more msghdrs");
825 * Re-lock the msqid_ds in case we page-fault when copying in the
829 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
830 panic("msqid_ds is already locked");
831 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
834 * Allocate a message header
837 msghdr = free_msghdrs;
838 free_msghdrs = msghdr->msg_next;
839 msghdr->msg_spot = -1;
840 msghdr->msg_ts = msgsz;
843 * XXXMAC: Should the mac_check_sysv_msgmsq check follow here
844 * immediately? Or, should it be checked just before the msg is
845 * enqueued in the msgq (as it is done now)?
847 mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr);
851 * Allocate space for the message
854 while (segs_needed > 0) {
855 if (nfree_msgmaps <= 0)
856 panic("not enough msgmaps");
857 if (free_msgmaps == -1)
858 panic("nil free_msgmaps");
861 panic("next too low #1");
862 if (next >= msginfo.msgseg)
863 panic("next out of range #1");
864 DPRINTF(("allocating segment %d to message\n", next));
865 free_msgmaps = msgmaps[next].next;
867 msgmaps[next].next = msghdr->msg_spot;
868 msghdr->msg_spot = next;
873 * Copy in the message type
876 mtx_unlock(&msq_mtx);
877 if ((error = copyin(user_msgp, &msghdr->msg_type,
878 sizeof(msghdr->msg_type))) != 0) {
880 DPRINTF(("error %d copying the message type\n", error));
882 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
887 user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type);
890 * Validate the message type
893 if (msghdr->msg_type < 1) {
895 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
897 DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
903 * Copy in the message body
906 next = msghdr->msg_spot;
909 if (msgsz > msginfo.msgssz)
910 tlen = msginfo.msgssz;
914 panic("next too low #2");
915 if (next >= msginfo.msgseg)
916 panic("next out of range #2");
917 mtx_unlock(&msq_mtx);
918 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
921 DPRINTF(("error %d copying in message segment\n",
924 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
930 user_msgp = (const char *)user_msgp + tlen;
931 next = msgmaps[next].next;
934 panic("didn't use all the msg segments");
937 * We've got the message. Unlock the msqid_ds.
940 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
943 * Make sure that the msqid_ds is still allocated.
946 if (msqkptr->u.msg_qbytes == 0) {
955 * Note: Since the task/thread allocates the msghdr and usually
956 * primes it with its own MAC label, for a majority of policies, it
957 * won't be necessary to check whether the msghdr has access
958 * permissions to the msgq. The mac_check_sysv_msqsnd check would
959 * suffice in that case. However, this hook may be required where
960 * individual policies derive a non-identical label for the msghdr
961 * from the current thread label and may want to check the msghdr
962 * enqueue permissions, along with read/write permissions to the
965 error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr);
974 * Put the message into the queue
976 if (msqkptr->u.msg_first == NULL) {
977 msqkptr->u.msg_first = msghdr;
978 msqkptr->u.msg_last = msghdr;
980 msqkptr->u.msg_last->msg_next = msghdr;
981 msqkptr->u.msg_last = msghdr;
983 msqkptr->u.msg_last->msg_next = NULL;
985 msqkptr->u.msg_cbytes += msghdr->msg_ts;
986 msqkptr->u.msg_qnum++;
987 msqkptr->u.msg_lspid = td->td_proc->p_pid;
988 msqkptr->u.msg_stime = time_second;
991 td->td_retval[0] = 0;
993 mtx_unlock(&msq_mtx);
997 #ifndef _SYS_SYSPROTO_H_
1013 register struct msgrcv_args *uap;
1015 int msqid = uap->msqid;
1016 void *user_msgp = uap->msgp;
1017 size_t msgsz = uap->msgsz;
1018 long msgtyp = uap->msgtyp;
1019 int msgflg = uap->msgflg;
1021 register struct msqid_kernel *msqkptr;
1022 register struct msg *msghdr;
1026 DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
1027 msgsz, msgtyp, msgflg));
1029 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1032 msqid = IPCID_TO_IX(msqid);
1034 if (msqid < 0 || msqid >= msginfo.msgmni) {
1035 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
1040 msqkptr = &msqids[msqid];
1042 if (msqkptr->u.msg_qbytes == 0) {
1043 DPRINTF(("no such message queue id\n"));
1047 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1048 DPRINTF(("wrong sequence number\n"));
1053 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1054 DPRINTF(("requester doesn't have read access\n"));
1059 error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr);
1065 while (msghdr == NULL) {
1067 msghdr = msqkptr->u.msg_first;
1068 if (msghdr != NULL) {
1069 if (msgsz < msghdr->msg_ts &&
1070 (msgflg & MSG_NOERROR) == 0) {
1071 DPRINTF(("first message on the queue "
1072 "is too big (want %d, got %d)\n",
1073 msgsz, msghdr->msg_ts));
1078 error = mac_check_sysv_msgrcv(td->td_ucred,
1083 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1084 msqkptr->u.msg_first = NULL;
1085 msqkptr->u.msg_last = NULL;
1087 msqkptr->u.msg_first = msghdr->msg_next;
1088 if (msqkptr->u.msg_first == NULL)
1089 panic("msg_first/last screwed up #1");
1093 struct msg *previous;
1097 prev = &(msqkptr->u.msg_first);
1098 while ((msghdr = *prev) != NULL) {
1100 * Is this message's type an exact match or is
1101 * this message's type less than or equal to
1102 * the absolute value of a negative msgtyp?
1103 * Note that the second half of this test can
1104 * NEVER be true if msgtyp is positive since
1105 * msg_type is always positive!
1108 if (msgtyp == msghdr->msg_type ||
1109 msghdr->msg_type <= -msgtyp) {
1110 DPRINTF(("found message type %d, "
1112 msghdr->msg_type, msgtyp));
1113 if (msgsz < msghdr->msg_ts &&
1114 (msgflg & MSG_NOERROR) == 0) {
1115 DPRINTF(("requested message "
1116 "on the queue is too big "
1117 "(want %d, got %d)\n",
1118 msgsz, msghdr->msg_ts));
1123 error = mac_check_sysv_msgrcv(
1124 td->td_ucred, msghdr);
1128 *prev = msghdr->msg_next;
1129 if (msghdr == msqkptr->u.msg_last) {
1130 if (previous == NULL) {
1132 &msqkptr->u.msg_first)
1133 panic("msg_first/last screwed up #2");
1134 msqkptr->u.msg_first =
1136 msqkptr->u.msg_last =
1140 &msqkptr->u.msg_first)
1141 panic("msg_first/last screwed up #3");
1142 msqkptr->u.msg_last =
1149 prev = &(msghdr->msg_next);
1154 * We've either extracted the msghdr for the appropriate
1155 * message or there isn't one.
1156 * If there is one then bail out of this loop.
1163 * Hmph! No message found. Does the user want to wait?
1166 if ((msgflg & IPC_NOWAIT) != 0) {
1167 DPRINTF(("no appropriate message found (msgtyp=%d)\n",
1169 /* The SVID says to return ENOMSG. */
1175 * Wait for something to happen
1178 DPRINTF(("msgrcv: goodnight\n"));
1179 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1181 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1184 DPRINTF(("msgsnd: interrupted system call\n"));
1190 * Make sure that the msq queue still exists
1193 if (msqkptr->u.msg_qbytes == 0 ||
1194 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1195 DPRINTF(("msqid deleted\n"));
1202 * Return the message to the user.
1204 * First, do the bookkeeping (before we risk being interrupted).
1207 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1208 msqkptr->u.msg_qnum--;
1209 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1210 msqkptr->u.msg_rtime = time_second;
1213 * Make msgsz the actual amount that we'll be returning.
1214 * Note that this effectively truncates the message if it is too long
1215 * (since msgsz is never increased).
1218 DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1220 if (msgsz > msghdr->msg_ts)
1221 msgsz = msghdr->msg_ts;
1224 * Return the type to the user.
1227 mtx_unlock(&msq_mtx);
1228 error = copyout(&(msghdr->msg_type), user_msgp,
1229 sizeof(msghdr->msg_type));
1232 DPRINTF(("error (%d) copying out message type\n", error));
1233 msg_freehdr(msghdr);
1237 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1240 * Return the segments to the user
1243 next = msghdr->msg_spot;
1244 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1247 if (msgsz - len > msginfo.msgssz)
1248 tlen = msginfo.msgssz;
1252 panic("next too low #3");
1253 if (next >= msginfo.msgseg)
1254 panic("next out of range #3");
1255 mtx_unlock(&msq_mtx);
1256 error = copyout(&msgpool[next * msginfo.msgssz],
1260 DPRINTF(("error (%d) copying out message segment\n",
1262 msg_freehdr(msghdr);
1266 user_msgp = (char *)user_msgp + tlen;
1267 next = msgmaps[next].next;
1271 * Done, return the actual number of bytes copied out.
1274 msg_freehdr(msghdr);
1276 td->td_retval[0] = msgsz;
1278 mtx_unlock(&msq_mtx);
1283 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1286 return (SYSCTL_OUT(req, msqids,
1287 sizeof(struct msqid_kernel) * msginfo.msgmni));
1290 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1291 "Maximum message size");
1292 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1293 "Number of message queue identifiers");
1294 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1295 "Maximum number of bytes in a queue");
1296 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1297 "Maximum number of messages in the system");
1298 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1299 "Size of a message segment");
1300 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1301 "Number of message segments");
1302 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1303 NULL, 0, sysctl_msqids, "", "Message queue IDs");