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_compat.h"
54 #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 #include <security/mac/mac_framework.h>
75 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
77 static void msginit(void);
78 static int msgunload(void);
79 static int sysvmsg_modload(struct module *, int, void *);
82 #define DPRINTF(a) printf a
84 #define DPRINTF(a) (void)0
87 static void msg_freehdr(struct msg *msghdr);
90 #define MSGSSZ 8 /* Each segment must be 2^N long */
93 #define MSGSEG 2048 /* must be less than 32767 */
95 #define MSGMAX (MSGSSZ*MSGSEG)
97 #define MSGMNB 2048 /* max # of bytes in a queue */
107 * Based on the configuration parameters described in an SVR2 (yes, two)
108 * config(1m) man page.
110 * Each message is broken up and stored in segments that are msgssz bytes
111 * long. For efficiency reasons, this should be a power of two. Also,
112 * it doesn't make sense if it is less than 8 or greater than about 256.
113 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
114 * two between 8 and 1024 inclusive (and panic's if it isn't).
116 struct msginfo msginfo = {
117 MSGMAX, /* max chars in a message */
118 MSGMNI, /* # of message queue identifiers */
119 MSGMNB, /* max chars in a queue */
120 MSGTQL, /* max messages in system */
121 MSGSSZ, /* size of a message segment */
122 /* (must be small power of 2 greater than 4) */
123 MSGSEG /* number of message segments */
127 * macros to convert between msqid_ds's and msqid's.
128 * (specific to this implementation)
130 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
131 #define MSQID_IX(id) ((id) & 0xffff)
132 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
135 * The rest of this file is specific to this particular implementation.
139 short next; /* next segment in buffer */
140 /* -1 -> available */
141 /* 0..(MSGSEG-1) -> index of next segment */
144 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
146 static int nfree_msgmaps; /* # of free map entries */
147 static short free_msgmaps; /* head of linked list of free map entries */
148 static struct msg *free_msghdrs;/* list of free msg headers */
149 static char *msgpool; /* MSGMAX byte long msg buffer pool */
150 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
151 static struct msg *msghdrs; /* MSGTQL msg headers */
152 static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */
153 static struct mtx msq_mtx; /* global mutex for message queues. */
160 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
161 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
162 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
163 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
164 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
165 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
167 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
169 panic("msgpool is NULL");
170 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
172 panic("msgmaps is NULL");
173 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
175 panic("msghdrs is NULL");
176 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
179 panic("msqids is NULL");
182 * msginfo.msgssz should be a power of two for efficiency reasons.
183 * It is also pretty silly if msginfo.msgssz is less than 8
184 * or greater than about 256 so ...
188 while (i < 1024 && i != msginfo.msgssz)
190 if (i != msginfo.msgssz) {
191 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
193 panic("msginfo.msgssz not a small power of 2");
196 if (msginfo.msgseg > 32767) {
197 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
198 panic("msginfo.msgseg > 32767");
202 panic("msgmaps is NULL");
204 for (i = 0; i < msginfo.msgseg; i++) {
206 msgmaps[i-1].next = i;
207 msgmaps[i].next = -1; /* implies entry is available */
210 nfree_msgmaps = msginfo.msgseg;
213 panic("msghdrs is NULL");
215 for (i = 0; i < msginfo.msgtql; i++) {
216 msghdrs[i].msg_type = 0;
218 msghdrs[i-1].msg_next = &msghdrs[i];
219 msghdrs[i].msg_next = NULL;
221 mac_sysvmsg_init(&msghdrs[i]);
224 free_msghdrs = &msghdrs[0];
227 panic("msqids is NULL");
229 for (i = 0; i < msginfo.msgmni; i++) {
230 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
231 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
232 msqids[i].u.msg_perm.mode = 0;
234 mac_sysvmsq_init(&msqids[i]);
237 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
243 struct msqid_kernel *msqkptr;
249 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
251 * Look for an unallocated and unlocked msqid_ds.
252 * msqid_ds's can be locked by msgsnd or msgrcv while
253 * they are copying the message in/out. We can't
254 * re-use the entry until they release it.
256 msqkptr = &msqids[msqid];
257 if (msqkptr->u.msg_qbytes != 0 ||
258 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
261 if (msqid != msginfo.msgmni)
265 for (i = 0; i < msginfo.msgtql; i++)
266 mac_sysvmsg_destroy(&msghdrs[i]);
267 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
268 mac_sysvmsq_destroy(&msqids[msqid]);
270 free(msgpool, M_MSG);
271 free(msgmaps, M_MSG);
272 free(msghdrs, M_MSG);
274 mtx_destroy(&msq_mtx);
280 sysvmsg_modload(struct module *module, int cmd, void *arg)
300 static moduledata_t sysvmsg_mod = {
306 SYSCALL_MODULE_HELPER(msgctl);
307 SYSCALL_MODULE_HELPER(msgget);
308 SYSCALL_MODULE_HELPER(msgsnd);
309 SYSCALL_MODULE_HELPER(msgrcv);
311 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
312 MODULE_VERSION(sysvmsg, 1);
318 while (msghdr->msg_ts > 0) {
320 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
321 panic("msghdr->msg_spot out of range");
322 next = msgmaps[msghdr->msg_spot].next;
323 msgmaps[msghdr->msg_spot].next = free_msgmaps;
324 free_msgmaps = msghdr->msg_spot;
326 msghdr->msg_spot = next;
327 if (msghdr->msg_ts >= msginfo.msgssz)
328 msghdr->msg_ts -= msginfo.msgssz;
332 if (msghdr->msg_spot != -1)
333 panic("msghdr->msg_spot != -1");
334 msghdr->msg_next = free_msghdrs;
335 free_msghdrs = msghdr;
337 mac_sysvmsg_cleanup(msghdr);
341 #ifndef _SYS_SYSPROTO_H_
345 struct msqid_ds *buf;
351 register struct msgctl_args *uap;
353 int msqid = uap->msqid;
355 struct msqid_ds msqbuf;
358 DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
359 if (cmd == IPC_SET &&
360 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
362 error = kern_msgctl(td, msqid, cmd, &msqbuf);
363 if (cmd == IPC_STAT && error == 0)
364 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
369 kern_msgctl(td, msqid, cmd, msqbuf)
373 struct msqid_ds *msqbuf;
375 int rval, error, msqix;
376 register struct msqid_kernel *msqkptr;
378 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
381 msqix = IPCID_TO_IX(msqid);
383 if (msqix < 0 || msqix >= msginfo.msgmni) {
384 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
389 msqkptr = &msqids[msqix];
392 if (msqkptr->u.msg_qbytes == 0) {
393 DPRINTF(("no such msqid\n"));
397 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
398 DPRINTF(("wrong sequence number\n"));
403 error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
416 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
421 * Check that the thread has MAC access permissions to
422 * individual msghdrs. Note: We need to do this in a
423 * separate loop because the actual loop alters the
424 * msq/msghdr info as it progresses, and there is no going
425 * back if half the way through we discover that the
426 * thread cannot free a certain msghdr. The msq will get
427 * into an inconsistent state.
429 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
430 msghdr = msghdr->msg_next) {
431 error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
437 /* Free the message headers */
438 msghdr = msqkptr->u.msg_first;
439 while (msghdr != NULL) {
440 struct msg *msghdr_tmp;
442 /* Free the segments of each message */
443 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
444 msqkptr->u.msg_qnum--;
446 msghdr = msghdr->msg_next;
447 msg_freehdr(msghdr_tmp);
450 if (msqkptr->u.msg_cbytes != 0)
451 panic("msg_cbytes is screwed up");
452 if (msqkptr->u.msg_qnum != 0)
453 panic("msg_qnum is screwed up");
455 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
458 mac_sysvmsq_cleanup(msqkptr);
467 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
469 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
470 error = priv_check(td, PRIV_IPC_MSGSIZE);
474 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
475 DPRINTF(("can't increase msg_qbytes beyond %d"
476 "(truncating)\n", msginfo.msgmnb));
477 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
479 if (msqbuf->msg_qbytes == 0) {
480 DPRINTF(("can't reduce msg_qbytes to 0\n"));
481 error = EINVAL; /* non-standard errno! */
484 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
485 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
486 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
487 (msqbuf->msg_perm.mode & 0777);
488 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
489 msqkptr->u.msg_ctime = time_second;
493 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
494 DPRINTF(("requester doesn't have read access\n"));
497 *msqbuf = msqkptr->u;
501 DPRINTF(("invalid command %d\n", cmd));
507 td->td_retval[0] = rval;
509 mtx_unlock(&msq_mtx);
513 #ifndef _SYS_SYSPROTO_H_
522 register struct msgget_args *uap;
524 int msqid, error = 0;
526 int msgflg = uap->msgflg;
527 struct ucred *cred = td->td_ucred;
528 register struct msqid_kernel *msqkptr = NULL;
530 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
532 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
536 if (key != IPC_PRIVATE) {
537 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
538 msqkptr = &msqids[msqid];
539 if (msqkptr->u.msg_qbytes != 0 &&
540 msqkptr->u.msg_perm.key == key)
543 if (msqid < msginfo.msgmni) {
544 DPRINTF(("found public key\n"));
545 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
546 DPRINTF(("not exclusive\n"));
550 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
552 DPRINTF(("requester doesn't have 0%o access\n",
557 error = mac_sysvmsq_check_msqget(cred, msqkptr);
565 DPRINTF(("need to allocate the msqid_ds\n"));
566 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
567 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
569 * Look for an unallocated and unlocked msqid_ds.
570 * msqid_ds's can be locked by msgsnd or msgrcv while
571 * they are copying the message in/out. We can't
572 * re-use the entry until they release it.
574 msqkptr = &msqids[msqid];
575 if (msqkptr->u.msg_qbytes == 0 &&
576 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
579 if (msqid == msginfo.msgmni) {
580 DPRINTF(("no more msqid_ds's available\n"));
584 DPRINTF(("msqid %d is available\n", msqid));
585 msqkptr->u.msg_perm.key = key;
586 msqkptr->u.msg_perm.cuid = cred->cr_uid;
587 msqkptr->u.msg_perm.uid = cred->cr_uid;
588 msqkptr->u.msg_perm.cgid = cred->cr_gid;
589 msqkptr->u.msg_perm.gid = cred->cr_gid;
590 msqkptr->u.msg_perm.mode = (msgflg & 0777);
591 /* Make sure that the returned msqid is unique */
592 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
593 msqkptr->u.msg_first = NULL;
594 msqkptr->u.msg_last = NULL;
595 msqkptr->u.msg_cbytes = 0;
596 msqkptr->u.msg_qnum = 0;
597 msqkptr->u.msg_qbytes = msginfo.msgmnb;
598 msqkptr->u.msg_lspid = 0;
599 msqkptr->u.msg_lrpid = 0;
600 msqkptr->u.msg_stime = 0;
601 msqkptr->u.msg_rtime = 0;
602 msqkptr->u.msg_ctime = time_second;
604 mac_sysvmsq_create(cred, msqkptr);
607 DPRINTF(("didn't find it and wasn't asked to create it\n"));
613 /* Construct the unique msqid */
614 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
616 mtx_unlock(&msq_mtx);
620 #ifndef _SYS_SYSPROTO_H_
629 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
632 const void *msgp; /* XXX msgp is actually mtext. */
637 int msqix, segs_needed, error = 0;
638 register struct msqid_kernel *msqkptr;
639 register struct msg *msghdr;
642 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
646 msqix = IPCID_TO_IX(msqid);
648 if (msqix < 0 || msqix >= msginfo.msgmni) {
649 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
655 msqkptr = &msqids[msqix];
656 if (msqkptr->u.msg_qbytes == 0) {
657 DPRINTF(("no such message queue id\n"));
661 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
662 DPRINTF(("wrong sequence number\n"));
667 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
668 DPRINTF(("requester doesn't have write access\n"));
673 error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
678 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
679 DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
680 msginfo.msgssz, segs_needed));
682 int need_more_resources = 0;
686 * (inside this loop in case msg_qbytes changes while we sleep)
689 if (msgsz > msqkptr->u.msg_qbytes) {
690 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
695 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
696 DPRINTF(("msqid is locked\n"));
697 need_more_resources = 1;
699 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
700 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
701 need_more_resources = 1;
703 if (segs_needed > nfree_msgmaps) {
704 DPRINTF(("segs_needed > nfree_msgmaps\n"));
705 need_more_resources = 1;
707 if (free_msghdrs == NULL) {
708 DPRINTF(("no more msghdrs\n"));
709 need_more_resources = 1;
712 if (need_more_resources) {
715 if ((msgflg & IPC_NOWAIT) != 0) {
716 DPRINTF(("need more resources but caller "
717 "doesn't want to wait\n"));
722 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
723 DPRINTF(("we don't own the msqid_ds\n"));
726 /* Force later arrivals to wait for our
728 DPRINTF(("we own the msqid_ds\n"));
729 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
732 DPRINTF(("msgsnd: goodnight\n"));
733 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
735 DPRINTF(("msgsnd: good morning, error=%d\n", error));
737 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
738 if (error == EWOULDBLOCK) {
739 DPRINTF(("msgsnd: timed out\n"));
743 DPRINTF(("msgsnd: interrupted system call\n"));
749 * Make sure that the msq queue still exists
752 if (msqkptr->u.msg_qbytes == 0) {
753 DPRINTF(("msqid deleted\n"));
759 DPRINTF(("got all the resources that we need\n"));
765 * We have the resources that we need.
769 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
770 panic("msg_perm.mode & MSG_LOCKED");
771 if (segs_needed > nfree_msgmaps)
772 panic("segs_needed > nfree_msgmaps");
773 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
774 panic("msgsz + msg_cbytes > msg_qbytes");
775 if (free_msghdrs == NULL)
776 panic("no more msghdrs");
779 * Re-lock the msqid_ds in case we page-fault when copying in the
783 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
784 panic("msqid_ds is already locked");
785 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
788 * Allocate a message header
791 msghdr = free_msghdrs;
792 free_msghdrs = msghdr->msg_next;
793 msghdr->msg_spot = -1;
794 msghdr->msg_ts = msgsz;
795 msghdr->msg_type = mtype;
798 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
799 * immediately? Or, should it be checked just before the msg is
800 * enqueued in the msgq (as it is done now)?
802 mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
806 * Allocate space for the message
809 while (segs_needed > 0) {
810 if (nfree_msgmaps <= 0)
811 panic("not enough msgmaps");
812 if (free_msgmaps == -1)
813 panic("nil free_msgmaps");
816 panic("next too low #1");
817 if (next >= msginfo.msgseg)
818 panic("next out of range #1");
819 DPRINTF(("allocating segment %d to message\n", next));
820 free_msgmaps = msgmaps[next].next;
822 msgmaps[next].next = msghdr->msg_spot;
823 msghdr->msg_spot = next;
828 * Validate the message type
831 if (msghdr->msg_type < 1) {
833 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
835 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
841 * Copy in the message body
844 next = msghdr->msg_spot;
847 if (msgsz > msginfo.msgssz)
848 tlen = msginfo.msgssz;
852 panic("next too low #2");
853 if (next >= msginfo.msgseg)
854 panic("next out of range #2");
855 mtx_unlock(&msq_mtx);
856 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
859 DPRINTF(("error %d copying in message segment\n",
862 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
868 msgp = (const char *)msgp + tlen;
869 next = msgmaps[next].next;
872 panic("didn't use all the msg segments");
875 * We've got the message. Unlock the msqid_ds.
878 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
881 * Make sure that the msqid_ds is still allocated.
884 if (msqkptr->u.msg_qbytes == 0) {
893 * Note: Since the task/thread allocates the msghdr and usually
894 * primes it with its own MAC label, for a majority of policies, it
895 * won't be necessary to check whether the msghdr has access
896 * permissions to the msgq. The mac_sysvmsq_check_msqsnd check would
897 * suffice in that case. However, this hook may be required where
898 * individual policies derive a non-identical label for the msghdr
899 * from the current thread label and may want to check the msghdr
900 * enqueue permissions, along with read/write permissions to the
903 error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
912 * Put the message into the queue
914 if (msqkptr->u.msg_first == NULL) {
915 msqkptr->u.msg_first = msghdr;
916 msqkptr->u.msg_last = msghdr;
918 msqkptr->u.msg_last->msg_next = msghdr;
919 msqkptr->u.msg_last = msghdr;
921 msqkptr->u.msg_last->msg_next = NULL;
923 msqkptr->u.msg_cbytes += msghdr->msg_ts;
924 msqkptr->u.msg_qnum++;
925 msqkptr->u.msg_lspid = td->td_proc->p_pid;
926 msqkptr->u.msg_stime = time_second;
929 td->td_retval[0] = 0;
931 mtx_unlock(&msq_mtx);
938 register struct msgsnd_args *uap;
943 DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
944 uap->msgsz, uap->msgflg));
946 if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
947 DPRINTF(("error %d copying the message type\n", error));
950 return (kern_msgsnd(td, uap->msqid,
951 (const char *)uap->msgp + sizeof(mtype),
952 uap->msgsz, uap->msgflg, mtype));
955 #ifndef _SYS_SYSPROTO_H_
965 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
968 void *msgp; /* XXX msgp is actually mtext. */
975 register struct msqid_kernel *msqkptr;
976 register struct msg *msghdr;
977 int msqix, error = 0;
980 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
983 msqix = IPCID_TO_IX(msqid);
985 if (msqix < 0 || msqix >= msginfo.msgmni) {
986 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
991 msqkptr = &msqids[msqix];
993 if (msqkptr->u.msg_qbytes == 0) {
994 DPRINTF(("no such message queue id\n"));
998 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
999 DPRINTF(("wrong sequence number\n"));
1004 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1005 DPRINTF(("requester doesn't have read access\n"));
1010 error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1016 while (msghdr == NULL) {
1018 msghdr = msqkptr->u.msg_first;
1019 if (msghdr != NULL) {
1020 if (msgsz < msghdr->msg_ts &&
1021 (msgflg & MSG_NOERROR) == 0) {
1022 DPRINTF(("first message on the queue "
1023 "is too big (want %zu, got %d)\n",
1024 msgsz, msghdr->msg_ts));
1029 error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1034 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1035 msqkptr->u.msg_first = NULL;
1036 msqkptr->u.msg_last = NULL;
1038 msqkptr->u.msg_first = msghdr->msg_next;
1039 if (msqkptr->u.msg_first == NULL)
1040 panic("msg_first/last screwed up #1");
1044 struct msg *previous;
1048 prev = &(msqkptr->u.msg_first);
1049 while ((msghdr = *prev) != NULL) {
1051 * Is this message's type an exact match or is
1052 * this message's type less than or equal to
1053 * the absolute value of a negative msgtyp?
1054 * Note that the second half of this test can
1055 * NEVER be true if msgtyp is positive since
1056 * msg_type is always positive!
1059 if (msgtyp == msghdr->msg_type ||
1060 msghdr->msg_type <= -msgtyp) {
1061 DPRINTF(("found message type %ld, "
1063 msghdr->msg_type, msgtyp));
1064 if (msgsz < msghdr->msg_ts &&
1065 (msgflg & MSG_NOERROR) == 0) {
1066 DPRINTF(("requested message "
1067 "on the queue is too big "
1068 "(want %zu, got %hu)\n",
1069 msgsz, msghdr->msg_ts));
1074 error = mac_sysvmsq_check_msgrcv(
1075 td->td_ucred, msghdr);
1079 *prev = msghdr->msg_next;
1080 if (msghdr == msqkptr->u.msg_last) {
1081 if (previous == NULL) {
1083 &msqkptr->u.msg_first)
1084 panic("msg_first/last screwed up #2");
1085 msqkptr->u.msg_first =
1087 msqkptr->u.msg_last =
1091 &msqkptr->u.msg_first)
1092 panic("msg_first/last screwed up #3");
1093 msqkptr->u.msg_last =
1100 prev = &(msghdr->msg_next);
1105 * We've either extracted the msghdr for the appropriate
1106 * message or there isn't one.
1107 * If there is one then bail out of this loop.
1114 * Hmph! No message found. Does the user want to wait?
1117 if ((msgflg & IPC_NOWAIT) != 0) {
1118 DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1120 /* The SVID says to return ENOMSG. */
1126 * Wait for something to happen
1129 DPRINTF(("msgrcv: goodnight\n"));
1130 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1132 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1135 DPRINTF(("msgrcv: interrupted system call\n"));
1141 * Make sure that the msq queue still exists
1144 if (msqkptr->u.msg_qbytes == 0 ||
1145 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1146 DPRINTF(("msqid deleted\n"));
1153 * Return the message to the user.
1155 * First, do the bookkeeping (before we risk being interrupted).
1158 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1159 msqkptr->u.msg_qnum--;
1160 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1161 msqkptr->u.msg_rtime = time_second;
1164 * Make msgsz the actual amount that we'll be returning.
1165 * Note that this effectively truncates the message if it is too long
1166 * (since msgsz is never increased).
1169 DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1171 if (msgsz > msghdr->msg_ts)
1172 msgsz = msghdr->msg_ts;
1173 *mtype = msghdr->msg_type;
1176 * Return the segments to the user
1179 next = msghdr->msg_spot;
1180 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1183 if (msgsz - len > msginfo.msgssz)
1184 tlen = msginfo.msgssz;
1188 panic("next too low #3");
1189 if (next >= msginfo.msgseg)
1190 panic("next out of range #3");
1191 mtx_unlock(&msq_mtx);
1192 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1195 DPRINTF(("error (%d) copying out message segment\n",
1197 msg_freehdr(msghdr);
1201 msgp = (char *)msgp + tlen;
1202 next = msgmaps[next].next;
1206 * Done, return the actual number of bytes copied out.
1209 msg_freehdr(msghdr);
1211 td->td_retval[0] = msgsz;
1213 mtx_unlock(&msq_mtx);
1220 register struct msgrcv_args *uap;
1225 DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1226 uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1228 if ((error = kern_msgrcv(td, uap->msqid,
1229 (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1230 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1232 if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1233 DPRINTF(("error %d copying the message type\n", error));
1238 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1241 return (SYSCTL_OUT(req, msqids,
1242 sizeof(struct msqid_kernel) * msginfo.msgmni));
1245 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1246 "Maximum message size");
1247 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1248 "Number of message queue identifiers");
1249 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1250 "Maximum number of bytes in a queue");
1251 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1252 "Maximum number of messages in the system");
1253 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1254 "Size of a message segment");
1255 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1256 "Number of message segments");
1257 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1258 NULL, 0, sysctl_msqids, "", "Message queue IDs");
1260 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1261 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1262 SYSCALL_MODULE_HELPER(msgsys);
1263 SYSCALL_MODULE_HELPER(freebsd7_msgctl);
1265 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1266 static sy_call_t *msgcalls[] = {
1267 (sy_call_t *)freebsd7_msgctl, (sy_call_t *)msgget,
1268 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
1272 * Entry point for all MSG calls.
1277 /* XXX actually varargs. */
1278 struct msgsys_args /* {
1289 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1291 if (uap->which < 0 ||
1292 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
1294 error = (*msgcalls[uap->which])(td, &uap->a2);
1298 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1300 #ifndef _SYS_SYSPROTO_H_
1301 struct freebsd7_msgctl_args {
1304 struct msqid_ds_old *buf;
1308 freebsd7_msgctl(td, uap)
1310 struct freebsd7_msgctl_args *uap;
1312 struct msqid_ds_old msqold;
1313 struct msqid_ds msqbuf;
1316 DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1318 if (uap->cmd == IPC_SET) {
1319 error = copyin(uap->buf, &msqold, sizeof(msqold));
1322 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1323 CP(msqold, msqbuf, msg_first);
1324 CP(msqold, msqbuf, msg_last);
1325 CP(msqold, msqbuf, msg_cbytes);
1326 CP(msqold, msqbuf, msg_qnum);
1327 CP(msqold, msqbuf, msg_qbytes);
1328 CP(msqold, msqbuf, msg_lspid);
1329 CP(msqold, msqbuf, msg_lrpid);
1330 CP(msqold, msqbuf, msg_stime);
1331 CP(msqold, msqbuf, msg_rtime);
1332 CP(msqold, msqbuf, msg_ctime);
1334 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1337 if (uap->cmd == IPC_STAT) {
1338 bzero(&msqold, sizeof(msqold));
1339 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1340 CP(msqbuf, msqold, msg_first);
1341 CP(msqbuf, msqold, msg_last);
1342 CP(msqbuf, msqold, msg_cbytes);
1343 CP(msqbuf, msqold, msg_qnum);
1344 CP(msqbuf, msqold, msg_qbytes);
1345 CP(msqbuf, msqold, msg_lspid);
1346 CP(msqbuf, msqold, msg_lrpid);
1347 CP(msqbuf, msqold, msg_stime);
1348 CP(msqbuf, msqold, msg_rtime);
1349 CP(msqbuf, msqold, msg_ctime);
1350 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1357 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||