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 int 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. */
155 static struct syscall_helper_data msg_syscalls[] = {
156 SYSCALL_INIT_HELPER(msgctl),
157 SYSCALL_INIT_HELPER(msgget),
158 SYSCALL_INIT_HELPER(msgsnd),
159 SYSCALL_INIT_HELPER(msgrcv),
160 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
161 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
162 SYSCALL_INIT_HELPER(msgsys),
163 SYSCALL_INIT_HELPER(freebsd7_msgctl),
168 #ifdef COMPAT_FREEBSD32
169 #include <compat/freebsd32/freebsd32.h>
170 #include <compat/freebsd32/freebsd32_ipc.h>
171 #include <compat/freebsd32/freebsd32_proto.h>
172 #include <compat/freebsd32/freebsd32_signal.h>
173 #include <compat/freebsd32/freebsd32_syscall.h>
174 #include <compat/freebsd32/freebsd32_util.h>
176 static struct syscall_helper_data msg32_syscalls[] = {
177 SYSCALL32_INIT_HELPER(freebsd32_msgctl),
178 SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
179 SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
180 SYSCALL32_INIT_HELPER(msgget),
181 SYSCALL32_INIT_HELPER(freebsd32_msgsys),
182 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
183 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
184 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
195 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
196 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
197 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
198 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
199 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
200 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
202 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
203 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
204 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
205 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
209 * msginfo.msgssz should be a power of two for efficiency reasons.
210 * It is also pretty silly if msginfo.msgssz is less than 8
211 * or greater than about 256 so ...
215 while (i < 1024 && i != msginfo.msgssz)
217 if (i != msginfo.msgssz) {
218 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
220 panic("msginfo.msgssz not a small power of 2");
223 if (msginfo.msgseg > 32767) {
224 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
225 panic("msginfo.msgseg > 32767");
228 for (i = 0; i < msginfo.msgseg; i++) {
230 msgmaps[i-1].next = i;
231 msgmaps[i].next = -1; /* implies entry is available */
234 nfree_msgmaps = msginfo.msgseg;
236 for (i = 0; i < msginfo.msgtql; i++) {
237 msghdrs[i].msg_type = 0;
239 msghdrs[i-1].msg_next = &msghdrs[i];
240 msghdrs[i].msg_next = NULL;
242 mac_sysvmsg_init(&msghdrs[i]);
245 free_msghdrs = &msghdrs[0];
247 for (i = 0; i < msginfo.msgmni; i++) {
248 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
249 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
250 msqids[i].u.msg_perm.mode = 0;
252 mac_sysvmsq_init(&msqids[i]);
255 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
257 error = syscall_helper_register(msg_syscalls);
260 #ifdef COMPAT_FREEBSD32
261 error = syscall32_helper_register(msg32_syscalls);
271 struct msqid_kernel *msqkptr;
277 syscall_helper_unregister(msg_syscalls);
278 #ifdef COMPAT_FREEBSD32
279 syscall32_helper_unregister(msg32_syscalls);
282 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
284 * Look for an unallocated and unlocked msqid_ds.
285 * msqid_ds's can be locked by msgsnd or msgrcv while
286 * they are copying the message in/out. We can't
287 * re-use the entry until they release it.
289 msqkptr = &msqids[msqid];
290 if (msqkptr->u.msg_qbytes != 0 ||
291 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
294 if (msqid != msginfo.msgmni)
298 for (i = 0; i < msginfo.msgtql; i++)
299 mac_sysvmsg_destroy(&msghdrs[i]);
300 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
301 mac_sysvmsq_destroy(&msqids[msqid]);
303 free(msgpool, M_MSG);
304 free(msgmaps, M_MSG);
305 free(msghdrs, M_MSG);
307 mtx_destroy(&msq_mtx);
313 sysvmsg_modload(struct module *module, int cmd, void *arg)
335 static moduledata_t sysvmsg_mod = {
341 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
342 MODULE_VERSION(sysvmsg, 1);
348 while (msghdr->msg_ts > 0) {
350 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
351 panic("msghdr->msg_spot out of range");
352 next = msgmaps[msghdr->msg_spot].next;
353 msgmaps[msghdr->msg_spot].next = free_msgmaps;
354 free_msgmaps = msghdr->msg_spot;
356 msghdr->msg_spot = next;
357 if (msghdr->msg_ts >= msginfo.msgssz)
358 msghdr->msg_ts -= msginfo.msgssz;
362 if (msghdr->msg_spot != -1)
363 panic("msghdr->msg_spot != -1");
364 msghdr->msg_next = free_msghdrs;
365 free_msghdrs = msghdr;
367 mac_sysvmsg_cleanup(msghdr);
371 #ifndef _SYS_SYSPROTO_H_
375 struct msqid_ds *buf;
381 register struct msgctl_args *uap;
383 int msqid = uap->msqid;
385 struct msqid_ds msqbuf;
388 DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
389 if (cmd == IPC_SET &&
390 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
392 error = kern_msgctl(td, msqid, cmd, &msqbuf);
393 if (cmd == IPC_STAT && error == 0)
394 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
399 kern_msgctl(td, msqid, cmd, msqbuf)
403 struct msqid_ds *msqbuf;
405 int rval, error, msqix;
406 register struct msqid_kernel *msqkptr;
408 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
411 msqix = IPCID_TO_IX(msqid);
413 if (msqix < 0 || msqix >= msginfo.msgmni) {
414 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
419 msqkptr = &msqids[msqix];
422 if (msqkptr->u.msg_qbytes == 0) {
423 DPRINTF(("no such msqid\n"));
427 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
428 DPRINTF(("wrong sequence number\n"));
433 error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
446 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
451 * Check that the thread has MAC access permissions to
452 * individual msghdrs. Note: We need to do this in a
453 * separate loop because the actual loop alters the
454 * msq/msghdr info as it progresses, and there is no going
455 * back if half the way through we discover that the
456 * thread cannot free a certain msghdr. The msq will get
457 * into an inconsistent state.
459 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
460 msghdr = msghdr->msg_next) {
461 error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
467 /* Free the message headers */
468 msghdr = msqkptr->u.msg_first;
469 while (msghdr != NULL) {
470 struct msg *msghdr_tmp;
472 /* Free the segments of each message */
473 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
474 msqkptr->u.msg_qnum--;
476 msghdr = msghdr->msg_next;
477 msg_freehdr(msghdr_tmp);
480 if (msqkptr->u.msg_cbytes != 0)
481 panic("msg_cbytes is screwed up");
482 if (msqkptr->u.msg_qnum != 0)
483 panic("msg_qnum is screwed up");
485 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
488 mac_sysvmsq_cleanup(msqkptr);
497 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
499 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
500 error = priv_check(td, PRIV_IPC_MSGSIZE);
504 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
505 DPRINTF(("can't increase msg_qbytes beyond %d"
506 "(truncating)\n", msginfo.msgmnb));
507 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
509 if (msqbuf->msg_qbytes == 0) {
510 DPRINTF(("can't reduce msg_qbytes to 0\n"));
511 error = EINVAL; /* non-standard errno! */
514 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
515 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
516 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
517 (msqbuf->msg_perm.mode & 0777);
518 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
519 msqkptr->u.msg_ctime = time_second;
523 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
524 DPRINTF(("requester doesn't have read access\n"));
527 *msqbuf = msqkptr->u;
531 DPRINTF(("invalid command %d\n", cmd));
537 td->td_retval[0] = rval;
539 mtx_unlock(&msq_mtx);
543 #ifndef _SYS_SYSPROTO_H_
552 register struct msgget_args *uap;
554 int msqid, error = 0;
556 int msgflg = uap->msgflg;
557 struct ucred *cred = td->td_ucred;
558 register struct msqid_kernel *msqkptr = NULL;
560 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
562 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
566 if (key != IPC_PRIVATE) {
567 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
568 msqkptr = &msqids[msqid];
569 if (msqkptr->u.msg_qbytes != 0 &&
570 msqkptr->u.msg_perm.key == key)
573 if (msqid < msginfo.msgmni) {
574 DPRINTF(("found public key\n"));
575 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
576 DPRINTF(("not exclusive\n"));
580 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
582 DPRINTF(("requester doesn't have 0%o access\n",
587 error = mac_sysvmsq_check_msqget(cred, msqkptr);
595 DPRINTF(("need to allocate the msqid_ds\n"));
596 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
597 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
599 * Look for an unallocated and unlocked msqid_ds.
600 * msqid_ds's can be locked by msgsnd or msgrcv while
601 * they are copying the message in/out. We can't
602 * re-use the entry until they release it.
604 msqkptr = &msqids[msqid];
605 if (msqkptr->u.msg_qbytes == 0 &&
606 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
609 if (msqid == msginfo.msgmni) {
610 DPRINTF(("no more msqid_ds's available\n"));
614 DPRINTF(("msqid %d is available\n", msqid));
615 msqkptr->u.msg_perm.key = key;
616 msqkptr->u.msg_perm.cuid = cred->cr_uid;
617 msqkptr->u.msg_perm.uid = cred->cr_uid;
618 msqkptr->u.msg_perm.cgid = cred->cr_gid;
619 msqkptr->u.msg_perm.gid = cred->cr_gid;
620 msqkptr->u.msg_perm.mode = (msgflg & 0777);
621 /* Make sure that the returned msqid is unique */
622 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
623 msqkptr->u.msg_first = NULL;
624 msqkptr->u.msg_last = NULL;
625 msqkptr->u.msg_cbytes = 0;
626 msqkptr->u.msg_qnum = 0;
627 msqkptr->u.msg_qbytes = msginfo.msgmnb;
628 msqkptr->u.msg_lspid = 0;
629 msqkptr->u.msg_lrpid = 0;
630 msqkptr->u.msg_stime = 0;
631 msqkptr->u.msg_rtime = 0;
632 msqkptr->u.msg_ctime = time_second;
634 mac_sysvmsq_create(cred, msqkptr);
637 DPRINTF(("didn't find it and wasn't asked to create it\n"));
643 /* Construct the unique msqid */
644 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
646 mtx_unlock(&msq_mtx);
650 #ifndef _SYS_SYSPROTO_H_
659 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
662 const void *msgp; /* XXX msgp is actually mtext. */
667 int msqix, segs_needed, error = 0;
668 register struct msqid_kernel *msqkptr;
669 register struct msg *msghdr;
672 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
676 msqix = IPCID_TO_IX(msqid);
678 if (msqix < 0 || msqix >= msginfo.msgmni) {
679 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
685 msqkptr = &msqids[msqix];
686 if (msqkptr->u.msg_qbytes == 0) {
687 DPRINTF(("no such message queue id\n"));
691 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
692 DPRINTF(("wrong sequence number\n"));
697 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
698 DPRINTF(("requester doesn't have write access\n"));
703 error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
708 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
709 DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
710 msginfo.msgssz, segs_needed));
712 int need_more_resources = 0;
716 * (inside this loop in case msg_qbytes changes while we sleep)
719 if (msgsz > msqkptr->u.msg_qbytes) {
720 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
725 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
726 DPRINTF(("msqid is locked\n"));
727 need_more_resources = 1;
729 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
730 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
731 need_more_resources = 1;
733 if (segs_needed > nfree_msgmaps) {
734 DPRINTF(("segs_needed > nfree_msgmaps\n"));
735 need_more_resources = 1;
737 if (free_msghdrs == NULL) {
738 DPRINTF(("no more msghdrs\n"));
739 need_more_resources = 1;
742 if (need_more_resources) {
745 if ((msgflg & IPC_NOWAIT) != 0) {
746 DPRINTF(("need more resources but caller "
747 "doesn't want to wait\n"));
752 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
753 DPRINTF(("we don't own the msqid_ds\n"));
756 /* Force later arrivals to wait for our
758 DPRINTF(("we own the msqid_ds\n"));
759 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
762 DPRINTF(("msgsnd: goodnight\n"));
763 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
765 DPRINTF(("msgsnd: good morning, error=%d\n", error));
767 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
768 if (error == EWOULDBLOCK) {
769 DPRINTF(("msgsnd: timed out\n"));
773 DPRINTF(("msgsnd: interrupted system call\n"));
779 * Make sure that the msq queue still exists
782 if (msqkptr->u.msg_qbytes == 0) {
783 DPRINTF(("msqid deleted\n"));
789 DPRINTF(("got all the resources that we need\n"));
795 * We have the resources that we need.
799 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
800 panic("msg_perm.mode & MSG_LOCKED");
801 if (segs_needed > nfree_msgmaps)
802 panic("segs_needed > nfree_msgmaps");
803 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
804 panic("msgsz + msg_cbytes > msg_qbytes");
805 if (free_msghdrs == NULL)
806 panic("no more msghdrs");
809 * Re-lock the msqid_ds in case we page-fault when copying in the
813 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
814 panic("msqid_ds is already locked");
815 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
818 * Allocate a message header
821 msghdr = free_msghdrs;
822 free_msghdrs = msghdr->msg_next;
823 msghdr->msg_spot = -1;
824 msghdr->msg_ts = msgsz;
825 msghdr->msg_type = mtype;
828 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
829 * immediately? Or, should it be checked just before the msg is
830 * enqueued in the msgq (as it is done now)?
832 mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
836 * Allocate space for the message
839 while (segs_needed > 0) {
840 if (nfree_msgmaps <= 0)
841 panic("not enough msgmaps");
842 if (free_msgmaps == -1)
843 panic("nil free_msgmaps");
846 panic("next too low #1");
847 if (next >= msginfo.msgseg)
848 panic("next out of range #1");
849 DPRINTF(("allocating segment %d to message\n", next));
850 free_msgmaps = msgmaps[next].next;
852 msgmaps[next].next = msghdr->msg_spot;
853 msghdr->msg_spot = next;
858 * Validate the message type
861 if (msghdr->msg_type < 1) {
863 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
865 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
871 * Copy in the message body
874 next = msghdr->msg_spot;
877 if (msgsz > msginfo.msgssz)
878 tlen = msginfo.msgssz;
882 panic("next too low #2");
883 if (next >= msginfo.msgseg)
884 panic("next out of range #2");
885 mtx_unlock(&msq_mtx);
886 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
889 DPRINTF(("error %d copying in message segment\n",
892 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
898 msgp = (const char *)msgp + tlen;
899 next = msgmaps[next].next;
902 panic("didn't use all the msg segments");
905 * We've got the message. Unlock the msqid_ds.
908 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
911 * Make sure that the msqid_ds is still allocated.
914 if (msqkptr->u.msg_qbytes == 0) {
923 * Note: Since the task/thread allocates the msghdr and usually
924 * primes it with its own MAC label, for a majority of policies, it
925 * won't be necessary to check whether the msghdr has access
926 * permissions to the msgq. The mac_sysvmsq_check_msqsnd check would
927 * suffice in that case. However, this hook may be required where
928 * individual policies derive a non-identical label for the msghdr
929 * from the current thread label and may want to check the msghdr
930 * enqueue permissions, along with read/write permissions to the
933 error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
942 * Put the message into the queue
944 if (msqkptr->u.msg_first == NULL) {
945 msqkptr->u.msg_first = msghdr;
946 msqkptr->u.msg_last = msghdr;
948 msqkptr->u.msg_last->msg_next = msghdr;
949 msqkptr->u.msg_last = msghdr;
951 msqkptr->u.msg_last->msg_next = NULL;
953 msqkptr->u.msg_cbytes += msghdr->msg_ts;
954 msqkptr->u.msg_qnum++;
955 msqkptr->u.msg_lspid = td->td_proc->p_pid;
956 msqkptr->u.msg_stime = time_second;
959 td->td_retval[0] = 0;
961 mtx_unlock(&msq_mtx);
968 register struct msgsnd_args *uap;
973 DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
974 uap->msgsz, uap->msgflg));
976 if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
977 DPRINTF(("error %d copying the message type\n", error));
980 return (kern_msgsnd(td, uap->msqid,
981 (const char *)uap->msgp + sizeof(mtype),
982 uap->msgsz, uap->msgflg, mtype));
985 #ifndef _SYS_SYSPROTO_H_
995 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
998 void *msgp; /* XXX msgp is actually mtext. */
1005 register struct msqid_kernel *msqkptr;
1006 register struct msg *msghdr;
1007 int msqix, error = 0;
1010 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1013 msqix = IPCID_TO_IX(msqid);
1015 if (msqix < 0 || msqix >= msginfo.msgmni) {
1016 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1021 msqkptr = &msqids[msqix];
1023 if (msqkptr->u.msg_qbytes == 0) {
1024 DPRINTF(("no such message queue id\n"));
1028 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1029 DPRINTF(("wrong sequence number\n"));
1034 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1035 DPRINTF(("requester doesn't have read access\n"));
1040 error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1046 while (msghdr == NULL) {
1048 msghdr = msqkptr->u.msg_first;
1049 if (msghdr != NULL) {
1050 if (msgsz < msghdr->msg_ts &&
1051 (msgflg & MSG_NOERROR) == 0) {
1052 DPRINTF(("first message on the queue "
1053 "is too big (want %zu, got %d)\n",
1054 msgsz, msghdr->msg_ts));
1059 error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1064 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1065 msqkptr->u.msg_first = NULL;
1066 msqkptr->u.msg_last = NULL;
1068 msqkptr->u.msg_first = msghdr->msg_next;
1069 if (msqkptr->u.msg_first == NULL)
1070 panic("msg_first/last screwed up #1");
1074 struct msg *previous;
1078 prev = &(msqkptr->u.msg_first);
1079 while ((msghdr = *prev) != NULL) {
1081 * Is this message's type an exact match or is
1082 * this message's type less than or equal to
1083 * the absolute value of a negative msgtyp?
1084 * Note that the second half of this test can
1085 * NEVER be true if msgtyp is positive since
1086 * msg_type is always positive!
1089 if (msgtyp == msghdr->msg_type ||
1090 msghdr->msg_type <= -msgtyp) {
1091 DPRINTF(("found message type %ld, "
1093 msghdr->msg_type, msgtyp));
1094 if (msgsz < msghdr->msg_ts &&
1095 (msgflg & MSG_NOERROR) == 0) {
1096 DPRINTF(("requested message "
1097 "on the queue is too big "
1098 "(want %zu, got %hu)\n",
1099 msgsz, msghdr->msg_ts));
1104 error = mac_sysvmsq_check_msgrcv(
1105 td->td_ucred, msghdr);
1109 *prev = msghdr->msg_next;
1110 if (msghdr == msqkptr->u.msg_last) {
1111 if (previous == NULL) {
1113 &msqkptr->u.msg_first)
1114 panic("msg_first/last screwed up #2");
1115 msqkptr->u.msg_first =
1117 msqkptr->u.msg_last =
1121 &msqkptr->u.msg_first)
1122 panic("msg_first/last screwed up #3");
1123 msqkptr->u.msg_last =
1130 prev = &(msghdr->msg_next);
1135 * We've either extracted the msghdr for the appropriate
1136 * message or there isn't one.
1137 * If there is one then bail out of this loop.
1144 * Hmph! No message found. Does the user want to wait?
1147 if ((msgflg & IPC_NOWAIT) != 0) {
1148 DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1150 /* The SVID says to return ENOMSG. */
1156 * Wait for something to happen
1159 DPRINTF(("msgrcv: goodnight\n"));
1160 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1162 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1165 DPRINTF(("msgrcv: interrupted system call\n"));
1171 * Make sure that the msq queue still exists
1174 if (msqkptr->u.msg_qbytes == 0 ||
1175 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1176 DPRINTF(("msqid deleted\n"));
1183 * Return the message to the user.
1185 * First, do the bookkeeping (before we risk being interrupted).
1188 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1189 msqkptr->u.msg_qnum--;
1190 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1191 msqkptr->u.msg_rtime = time_second;
1194 * Make msgsz the actual amount that we'll be returning.
1195 * Note that this effectively truncates the message if it is too long
1196 * (since msgsz is never increased).
1199 DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1201 if (msgsz > msghdr->msg_ts)
1202 msgsz = msghdr->msg_ts;
1203 *mtype = msghdr->msg_type;
1206 * Return the segments to the user
1209 next = msghdr->msg_spot;
1210 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1213 if (msgsz - len > msginfo.msgssz)
1214 tlen = msginfo.msgssz;
1218 panic("next too low #3");
1219 if (next >= msginfo.msgseg)
1220 panic("next out of range #3");
1221 mtx_unlock(&msq_mtx);
1222 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1225 DPRINTF(("error (%d) copying out message segment\n",
1227 msg_freehdr(msghdr);
1231 msgp = (char *)msgp + tlen;
1232 next = msgmaps[next].next;
1236 * Done, return the actual number of bytes copied out.
1239 msg_freehdr(msghdr);
1241 td->td_retval[0] = msgsz;
1243 mtx_unlock(&msq_mtx);
1250 register struct msgrcv_args *uap;
1255 DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1256 uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1258 if ((error = kern_msgrcv(td, uap->msqid,
1259 (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1260 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1262 if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1263 DPRINTF(("error %d copying the message type\n", error));
1268 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1271 return (SYSCTL_OUT(req, msqids,
1272 sizeof(struct msqid_kernel) * msginfo.msgmni));
1275 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1276 "Maximum message size");
1277 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1278 "Number of message queue identifiers");
1279 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1280 "Maximum number of bytes in a queue");
1281 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1282 "Maximum number of messages in the system");
1283 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1284 "Size of a message segment");
1285 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1286 "Number of message segments");
1287 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1288 NULL, 0, sysctl_msqids, "", "Message queue IDs");
1290 #ifdef COMPAT_FREEBSD32
1292 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1295 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1296 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1297 switch (uap->which) {
1299 return (freebsd7_freebsd32_msgctl(td,
1300 (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1302 return (freebsd32_msgsnd(td,
1303 (struct freebsd32_msgsnd_args *)&uap->a2));
1305 return (freebsd32_msgrcv(td,
1306 (struct freebsd32_msgrcv_args *)&uap->a2));
1308 return (msgsys(td, (struct msgsys_args *)uap));
1311 return (nosys(td, NULL));
1315 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1316 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1318 freebsd7_freebsd32_msgctl(struct thread *td,
1319 struct freebsd7_freebsd32_msgctl_args *uap)
1321 struct msqid_ds msqbuf;
1322 struct msqid_ds32_old msqbuf32;
1325 if (uap->cmd == IPC_SET) {
1326 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1329 freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1330 PTRIN_CP(msqbuf32, msqbuf, msg_first);
1331 PTRIN_CP(msqbuf32, msqbuf, msg_last);
1332 CP(msqbuf32, msqbuf, msg_cbytes);
1333 CP(msqbuf32, msqbuf, msg_qnum);
1334 CP(msqbuf32, msqbuf, msg_qbytes);
1335 CP(msqbuf32, msqbuf, msg_lspid);
1336 CP(msqbuf32, msqbuf, msg_lrpid);
1337 CP(msqbuf32, msqbuf, msg_stime);
1338 CP(msqbuf32, msqbuf, msg_rtime);
1339 CP(msqbuf32, msqbuf, msg_ctime);
1341 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1344 if (uap->cmd == IPC_STAT) {
1345 bzero(&msqbuf32, sizeof(msqbuf32));
1346 freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1347 PTROUT_CP(msqbuf, msqbuf32, msg_first);
1348 PTROUT_CP(msqbuf, msqbuf32, msg_last);
1349 CP(msqbuf, msqbuf32, msg_cbytes);
1350 CP(msqbuf, msqbuf32, msg_qnum);
1351 CP(msqbuf, msqbuf32, msg_qbytes);
1352 CP(msqbuf, msqbuf32, msg_lspid);
1353 CP(msqbuf, msqbuf32, msg_lrpid);
1354 CP(msqbuf, msqbuf32, msg_stime);
1355 CP(msqbuf, msqbuf32, msg_rtime);
1356 CP(msqbuf, msqbuf32, msg_ctime);
1357 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1364 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1366 struct msqid_ds msqbuf;
1367 struct msqid_ds32 msqbuf32;
1370 if (uap->cmd == IPC_SET) {
1371 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1374 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1375 PTRIN_CP(msqbuf32, msqbuf, msg_first);
1376 PTRIN_CP(msqbuf32, msqbuf, msg_last);
1377 CP(msqbuf32, msqbuf, msg_cbytes);
1378 CP(msqbuf32, msqbuf, msg_qnum);
1379 CP(msqbuf32, msqbuf, msg_qbytes);
1380 CP(msqbuf32, msqbuf, msg_lspid);
1381 CP(msqbuf32, msqbuf, msg_lrpid);
1382 CP(msqbuf32, msqbuf, msg_stime);
1383 CP(msqbuf32, msqbuf, msg_rtime);
1384 CP(msqbuf32, msqbuf, msg_ctime);
1386 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1389 if (uap->cmd == IPC_STAT) {
1390 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1391 PTROUT_CP(msqbuf, msqbuf32, msg_first);
1392 PTROUT_CP(msqbuf, msqbuf32, msg_last);
1393 CP(msqbuf, msqbuf32, msg_cbytes);
1394 CP(msqbuf, msqbuf32, msg_qnum);
1395 CP(msqbuf, msqbuf32, msg_qbytes);
1396 CP(msqbuf, msqbuf32, msg_lspid);
1397 CP(msqbuf, msqbuf32, msg_lrpid);
1398 CP(msqbuf, msqbuf32, msg_stime);
1399 CP(msqbuf, msqbuf32, msg_rtime);
1400 CP(msqbuf, msqbuf32, msg_ctime);
1401 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1407 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1414 msgp = PTRIN(uap->msgp);
1415 if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1418 return (kern_msgsnd(td, uap->msqid,
1419 (const char *)msgp + sizeof(mtype32),
1420 uap->msgsz, uap->msgflg, mtype));
1424 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1431 msgp = PTRIN(uap->msgp);
1432 if ((error = kern_msgrcv(td, uap->msqid,
1433 (char *)msgp + sizeof(mtype32), uap->msgsz,
1434 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1436 mtype32 = (int32_t)mtype;
1437 return (copyout(&mtype32, msgp, sizeof(mtype32)));
1441 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1442 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1444 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1445 static sy_call_t *msgcalls[] = {
1446 (sy_call_t *)freebsd7_msgctl, (sy_call_t *)msgget,
1447 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
1451 * Entry point for all MSG calls.
1456 /* XXX actually varargs. */
1457 struct msgsys_args /* {
1468 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1470 if (uap->which < 0 ||
1471 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
1473 error = (*msgcalls[uap->which])(td, &uap->a2);
1478 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1481 #ifndef _SYS_SYSPROTO_H_
1482 struct freebsd7_msgctl_args {
1485 struct msqid_ds_old *buf;
1489 freebsd7_msgctl(td, uap)
1491 struct freebsd7_msgctl_args *uap;
1493 struct msqid_ds_old msqold;
1494 struct msqid_ds msqbuf;
1497 DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1499 if (uap->cmd == IPC_SET) {
1500 error = copyin(uap->buf, &msqold, sizeof(msqold));
1503 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1504 CP(msqold, msqbuf, msg_first);
1505 CP(msqold, msqbuf, msg_last);
1506 CP(msqold, msqbuf, msg_cbytes);
1507 CP(msqold, msqbuf, msg_qnum);
1508 CP(msqold, msqbuf, msg_qbytes);
1509 CP(msqold, msqbuf, msg_lspid);
1510 CP(msqold, msqbuf, msg_lrpid);
1511 CP(msqold, msqbuf, msg_stime);
1512 CP(msqold, msqbuf, msg_rtime);
1513 CP(msqold, msqbuf, msg_ctime);
1515 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1518 if (uap->cmd == IPC_STAT) {
1519 bzero(&msqold, sizeof(msqold));
1520 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1521 CP(msqbuf, msqold, msg_first);
1522 CP(msqbuf, msqold, msg_last);
1523 CP(msqbuf, msqold, msg_cbytes);
1524 CP(msqbuf, msqold, msg_qnum);
1525 CP(msqbuf, msqold, msg_qbytes);
1526 CP(msqbuf, msqold, msg_lspid);
1527 CP(msqbuf, msqold, msg_lrpid);
1528 CP(msqbuf, msqold, msg_stime);
1529 CP(msqbuf, msqold, msg_rtime);
1530 CP(msqbuf, msqold, msg_ctime);
1531 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1538 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||