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/racct.h>
67 #include <sys/syscall.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysent.h>
70 #include <sys/sysctl.h>
71 #include <sys/malloc.h>
74 #include <security/mac/mac_framework.h>
76 FEATURE(sysv_msg, "System V message queues support");
78 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
80 static int msginit(void);
81 static int msgunload(void);
82 static int sysvmsg_modload(struct module *, int, void *);
85 #define DPRINTF(a) printf a
87 #define DPRINTF(a) (void)0
90 static void msg_freehdr(struct msg *msghdr);
93 #define MSGSSZ 8 /* Each segment must be 2^N long */
96 #define MSGSEG 2048 /* must be less than 32767 */
98 #define MSGMAX (MSGSSZ*MSGSEG)
100 #define MSGMNB 2048 /* max # of bytes in a queue */
110 * Based on the configuration parameters described in an SVR2 (yes, two)
111 * config(1m) man page.
113 * Each message is broken up and stored in segments that are msgssz bytes
114 * long. For efficiency reasons, this should be a power of two. Also,
115 * it doesn't make sense if it is less than 8 or greater than about 256.
116 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
117 * two between 8 and 1024 inclusive (and panic's if it isn't).
119 struct msginfo msginfo = {
120 MSGMAX, /* max chars in a message */
121 MSGMNI, /* # of message queue identifiers */
122 MSGMNB, /* max chars in a queue */
123 MSGTQL, /* max messages in system */
124 MSGSSZ, /* size of a message segment */
125 /* (must be small power of 2 greater than 4) */
126 MSGSEG /* number of message segments */
130 * macros to convert between msqid_ds's and msqid's.
131 * (specific to this implementation)
133 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
134 #define MSQID_IX(id) ((id) & 0xffff)
135 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
138 * The rest of this file is specific to this particular implementation.
142 short next; /* next segment in buffer */
143 /* -1 -> available */
144 /* 0..(MSGSEG-1) -> index of next segment */
147 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
149 static int nfree_msgmaps; /* # of free map entries */
150 static short free_msgmaps; /* head of linked list of free map entries */
151 static struct msg *free_msghdrs;/* list of free msg headers */
152 static char *msgpool; /* MSGMAX byte long msg buffer pool */
153 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
154 static struct msg *msghdrs; /* MSGTQL msg headers */
155 static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */
156 static struct mtx msq_mtx; /* global mutex for message queues. */
158 static struct syscall_helper_data msg_syscalls[] = {
159 SYSCALL_INIT_HELPER(msgctl),
160 SYSCALL_INIT_HELPER(msgget),
161 SYSCALL_INIT_HELPER(msgsnd),
162 SYSCALL_INIT_HELPER(msgrcv),
163 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
164 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
165 SYSCALL_INIT_HELPER(msgsys),
166 SYSCALL_INIT_HELPER(freebsd7_msgctl),
171 #ifdef COMPAT_FREEBSD32
172 #include <compat/freebsd32/freebsd32.h>
173 #include <compat/freebsd32/freebsd32_ipc.h>
174 #include <compat/freebsd32/freebsd32_proto.h>
175 #include <compat/freebsd32/freebsd32_signal.h>
176 #include <compat/freebsd32/freebsd32_syscall.h>
177 #include <compat/freebsd32/freebsd32_util.h>
179 static struct syscall_helper_data msg32_syscalls[] = {
180 SYSCALL32_INIT_HELPER(freebsd32_msgctl),
181 SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
182 SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
183 SYSCALL32_INIT_HELPER(msgget),
184 SYSCALL32_INIT_HELPER(freebsd32_msgsys),
185 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
186 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
187 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
198 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
199 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
200 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
201 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
202 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
203 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
205 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
206 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
207 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
208 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
212 * msginfo.msgssz should be a power of two for efficiency reasons.
213 * It is also pretty silly if msginfo.msgssz is less than 8
214 * or greater than about 256 so ...
218 while (i < 1024 && i != msginfo.msgssz)
220 if (i != msginfo.msgssz) {
221 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
223 panic("msginfo.msgssz not a small power of 2");
226 if (msginfo.msgseg > 32767) {
227 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
228 panic("msginfo.msgseg > 32767");
231 for (i = 0; i < msginfo.msgseg; i++) {
233 msgmaps[i-1].next = i;
234 msgmaps[i].next = -1; /* implies entry is available */
237 nfree_msgmaps = msginfo.msgseg;
239 for (i = 0; i < msginfo.msgtql; i++) {
240 msghdrs[i].msg_type = 0;
242 msghdrs[i-1].msg_next = &msghdrs[i];
243 msghdrs[i].msg_next = NULL;
245 mac_sysvmsg_init(&msghdrs[i]);
248 free_msghdrs = &msghdrs[0];
250 for (i = 0; i < msginfo.msgmni; i++) {
251 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
252 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
253 msqids[i].u.msg_perm.mode = 0;
255 mac_sysvmsq_init(&msqids[i]);
258 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
260 error = syscall_helper_register(msg_syscalls);
263 #ifdef COMPAT_FREEBSD32
264 error = syscall32_helper_register(msg32_syscalls);
274 struct msqid_kernel *msqkptr;
280 syscall_helper_unregister(msg_syscalls);
281 #ifdef COMPAT_FREEBSD32
282 syscall32_helper_unregister(msg32_syscalls);
285 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
287 * Look for an unallocated and unlocked msqid_ds.
288 * msqid_ds's can be locked by msgsnd or msgrcv while
289 * they are copying the message in/out. We can't
290 * re-use the entry until they release it.
292 msqkptr = &msqids[msqid];
293 if (msqkptr->u.msg_qbytes != 0 ||
294 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
297 if (msqid != msginfo.msgmni)
301 for (i = 0; i < msginfo.msgtql; i++)
302 mac_sysvmsg_destroy(&msghdrs[i]);
303 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
304 mac_sysvmsq_destroy(&msqids[msqid]);
306 free(msgpool, M_MSG);
307 free(msgmaps, M_MSG);
308 free(msghdrs, M_MSG);
310 mtx_destroy(&msq_mtx);
316 sysvmsg_modload(struct module *module, int cmd, void *arg)
338 static moduledata_t sysvmsg_mod = {
344 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
345 MODULE_VERSION(sysvmsg, 1);
351 while (msghdr->msg_ts > 0) {
353 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
354 panic("msghdr->msg_spot out of range");
355 next = msgmaps[msghdr->msg_spot].next;
356 msgmaps[msghdr->msg_spot].next = free_msgmaps;
357 free_msgmaps = msghdr->msg_spot;
359 msghdr->msg_spot = next;
360 if (msghdr->msg_ts >= msginfo.msgssz)
361 msghdr->msg_ts -= msginfo.msgssz;
365 if (msghdr->msg_spot != -1)
366 panic("msghdr->msg_spot != -1");
367 msghdr->msg_next = free_msghdrs;
368 free_msghdrs = msghdr;
370 mac_sysvmsg_cleanup(msghdr);
374 #ifndef _SYS_SYSPROTO_H_
378 struct msqid_ds *buf;
384 register struct msgctl_args *uap;
386 int msqid = uap->msqid;
388 struct msqid_ds msqbuf;
391 DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
392 if (cmd == IPC_SET &&
393 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
395 error = kern_msgctl(td, msqid, cmd, &msqbuf);
396 if (cmd == IPC_STAT && error == 0)
397 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
402 kern_msgctl(td, msqid, cmd, msqbuf)
406 struct msqid_ds *msqbuf;
408 int rval, error, msqix;
409 register struct msqid_kernel *msqkptr;
411 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
414 msqix = IPCID_TO_IX(msqid);
416 if (msqix < 0 || msqix >= msginfo.msgmni) {
417 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
422 msqkptr = &msqids[msqix];
425 if (msqkptr->u.msg_qbytes == 0) {
426 DPRINTF(("no such msqid\n"));
430 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
431 DPRINTF(("wrong sequence number\n"));
436 error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
449 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
454 * Check that the thread has MAC access permissions to
455 * individual msghdrs. Note: We need to do this in a
456 * separate loop because the actual loop alters the
457 * msq/msghdr info as it progresses, and there is no going
458 * back if half the way through we discover that the
459 * thread cannot free a certain msghdr. The msq will get
460 * into an inconsistent state.
462 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
463 msghdr = msghdr->msg_next) {
464 error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
470 racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
471 racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
472 racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
473 crfree(msqkptr->cred);
474 msqkptr->cred = NULL;
476 /* Free the message headers */
477 msghdr = msqkptr->u.msg_first;
478 while (msghdr != NULL) {
479 struct msg *msghdr_tmp;
481 /* Free the segments of each message */
482 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
483 msqkptr->u.msg_qnum--;
485 msghdr = msghdr->msg_next;
486 msg_freehdr(msghdr_tmp);
489 if (msqkptr->u.msg_cbytes != 0)
490 panic("msg_cbytes is screwed up");
491 if (msqkptr->u.msg_qnum != 0)
492 panic("msg_qnum is screwed up");
494 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
497 mac_sysvmsq_cleanup(msqkptr);
506 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
508 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
509 error = priv_check(td, PRIV_IPC_MSGSIZE);
513 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
514 DPRINTF(("can't increase msg_qbytes beyond %d"
515 "(truncating)\n", msginfo.msgmnb));
516 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
518 if (msqbuf->msg_qbytes == 0) {
519 DPRINTF(("can't reduce msg_qbytes to 0\n"));
520 error = EINVAL; /* non-standard errno! */
523 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
524 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
525 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
526 (msqbuf->msg_perm.mode & 0777);
527 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
528 msqkptr->u.msg_ctime = time_second;
532 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
533 DPRINTF(("requester doesn't have read access\n"));
536 *msqbuf = msqkptr->u;
540 DPRINTF(("invalid command %d\n", cmd));
546 td->td_retval[0] = rval;
548 mtx_unlock(&msq_mtx);
552 #ifndef _SYS_SYSPROTO_H_
561 register struct msgget_args *uap;
563 int msqid, error = 0;
565 int msgflg = uap->msgflg;
566 struct ucred *cred = td->td_ucred;
567 register struct msqid_kernel *msqkptr = NULL;
569 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
571 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
575 if (key != IPC_PRIVATE) {
576 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
577 msqkptr = &msqids[msqid];
578 if (msqkptr->u.msg_qbytes != 0 &&
579 msqkptr->u.msg_perm.key == key)
582 if (msqid < msginfo.msgmni) {
583 DPRINTF(("found public key\n"));
584 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
585 DPRINTF(("not exclusive\n"));
589 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
591 DPRINTF(("requester doesn't have 0%o access\n",
596 error = mac_sysvmsq_check_msqget(cred, msqkptr);
604 DPRINTF(("need to allocate the msqid_ds\n"));
605 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
606 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
608 * Look for an unallocated and unlocked msqid_ds.
609 * msqid_ds's can be locked by msgsnd or msgrcv while
610 * they are copying the message in/out. We can't
611 * re-use the entry until they release it.
613 msqkptr = &msqids[msqid];
614 if (msqkptr->u.msg_qbytes == 0 &&
615 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
618 if (msqid == msginfo.msgmni) {
619 DPRINTF(("no more msqid_ds's available\n"));
623 PROC_LOCK(td->td_proc);
624 error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
625 PROC_UNLOCK(td->td_proc);
630 DPRINTF(("msqid %d is available\n", msqid));
631 msqkptr->u.msg_perm.key = key;
632 msqkptr->u.msg_perm.cuid = cred->cr_uid;
633 msqkptr->u.msg_perm.uid = cred->cr_uid;
634 msqkptr->u.msg_perm.cgid = cred->cr_gid;
635 msqkptr->u.msg_perm.gid = cred->cr_gid;
636 msqkptr->u.msg_perm.mode = (msgflg & 0777);
637 msqkptr->cred = crhold(cred);
638 /* Make sure that the returned msqid is unique */
639 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
640 msqkptr->u.msg_first = NULL;
641 msqkptr->u.msg_last = NULL;
642 msqkptr->u.msg_cbytes = 0;
643 msqkptr->u.msg_qnum = 0;
644 msqkptr->u.msg_qbytes = msginfo.msgmnb;
645 msqkptr->u.msg_lspid = 0;
646 msqkptr->u.msg_lrpid = 0;
647 msqkptr->u.msg_stime = 0;
648 msqkptr->u.msg_rtime = 0;
649 msqkptr->u.msg_ctime = time_second;
651 mac_sysvmsq_create(cred, msqkptr);
654 DPRINTF(("didn't find it and wasn't asked to create it\n"));
660 /* Construct the unique msqid */
661 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
663 mtx_unlock(&msq_mtx);
667 #ifndef _SYS_SYSPROTO_H_
676 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
679 const void *msgp; /* XXX msgp is actually mtext. */
684 int msqix, segs_needed, error = 0;
685 register struct msqid_kernel *msqkptr;
686 register struct msg *msghdr;
690 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
694 msqix = IPCID_TO_IX(msqid);
696 if (msqix < 0 || msqix >= msginfo.msgmni) {
697 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
703 msqkptr = &msqids[msqix];
704 if (msqkptr->u.msg_qbytes == 0) {
705 DPRINTF(("no such message queue id\n"));
709 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
710 DPRINTF(("wrong sequence number\n"));
715 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
716 DPRINTF(("requester doesn't have write access\n"));
721 error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
726 PROC_LOCK(td->td_proc);
727 if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
728 PROC_UNLOCK(td->td_proc);
733 if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
734 racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
735 PROC_UNLOCK(td->td_proc);
739 PROC_UNLOCK(td->td_proc);
741 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
742 DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
743 msginfo.msgssz, segs_needed));
745 int need_more_resources = 0;
749 * (inside this loop in case msg_qbytes changes while we sleep)
752 if (msgsz > msqkptr->u.msg_qbytes) {
753 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
758 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
759 DPRINTF(("msqid is locked\n"));
760 need_more_resources = 1;
762 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
763 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
764 need_more_resources = 1;
766 if (segs_needed > nfree_msgmaps) {
767 DPRINTF(("segs_needed > nfree_msgmaps\n"));
768 need_more_resources = 1;
770 if (free_msghdrs == NULL) {
771 DPRINTF(("no more msghdrs\n"));
772 need_more_resources = 1;
775 if (need_more_resources) {
778 if ((msgflg & IPC_NOWAIT) != 0) {
779 DPRINTF(("need more resources but caller "
780 "doesn't want to wait\n"));
785 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
786 DPRINTF(("we don't own the msqid_ds\n"));
789 /* Force later arrivals to wait for our
791 DPRINTF(("we own the msqid_ds\n"));
792 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
795 DPRINTF(("msgsnd: goodnight\n"));
796 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
798 DPRINTF(("msgsnd: good morning, error=%d\n", error));
800 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
801 if (error == EWOULDBLOCK) {
802 DPRINTF(("msgsnd: timed out\n"));
806 DPRINTF(("msgsnd: interrupted system call\n"));
812 * Make sure that the msq queue still exists
815 if (msqkptr->u.msg_qbytes == 0) {
816 DPRINTF(("msqid deleted\n"));
822 DPRINTF(("got all the resources that we need\n"));
828 * We have the resources that we need.
832 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
833 panic("msg_perm.mode & MSG_LOCKED");
834 if (segs_needed > nfree_msgmaps)
835 panic("segs_needed > nfree_msgmaps");
836 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
837 panic("msgsz + msg_cbytes > msg_qbytes");
838 if (free_msghdrs == NULL)
839 panic("no more msghdrs");
842 * Re-lock the msqid_ds in case we page-fault when copying in the
846 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
847 panic("msqid_ds is already locked");
848 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
851 * Allocate a message header
854 msghdr = free_msghdrs;
855 free_msghdrs = msghdr->msg_next;
856 msghdr->msg_spot = -1;
857 msghdr->msg_ts = msgsz;
858 msghdr->msg_type = mtype;
861 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
862 * immediately? Or, should it be checked just before the msg is
863 * enqueued in the msgq (as it is done now)?
865 mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
869 * Allocate space for the message
872 while (segs_needed > 0) {
873 if (nfree_msgmaps <= 0)
874 panic("not enough msgmaps");
875 if (free_msgmaps == -1)
876 panic("nil free_msgmaps");
879 panic("next too low #1");
880 if (next >= msginfo.msgseg)
881 panic("next out of range #1");
882 DPRINTF(("allocating segment %d to message\n", next));
883 free_msgmaps = msgmaps[next].next;
885 msgmaps[next].next = msghdr->msg_spot;
886 msghdr->msg_spot = next;
891 * Validate the message type
894 if (msghdr->msg_type < 1) {
896 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
898 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
904 * Copy in the message body
907 next = msghdr->msg_spot;
910 if (msgsz > msginfo.msgssz)
911 tlen = msginfo.msgssz;
915 panic("next too low #2");
916 if (next >= msginfo.msgseg)
917 panic("next out of range #2");
918 mtx_unlock(&msq_mtx);
919 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
922 DPRINTF(("error %d copying in message segment\n",
925 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
931 msgp = (const char *)msgp + tlen;
932 next = msgmaps[next].next;
935 panic("didn't use all the msg segments");
938 * We've got the message. Unlock the msqid_ds.
941 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
944 * Make sure that the msqid_ds is still allocated.
947 if (msqkptr->u.msg_qbytes == 0) {
956 * Note: Since the task/thread allocates the msghdr and usually
957 * primes it with its own MAC label, for a majority of policies, it
958 * won't be necessary to check whether the msghdr has access
959 * permissions to the msgq. The mac_sysvmsq_check_msqsnd check would
960 * suffice in that case. However, this hook may be required where
961 * individual policies derive a non-identical label for the msghdr
962 * from the current thread label and may want to check the msghdr
963 * enqueue permissions, along with read/write permissions to the
966 error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
975 * Put the message into the queue
977 if (msqkptr->u.msg_first == NULL) {
978 msqkptr->u.msg_first = msghdr;
979 msqkptr->u.msg_last = msghdr;
981 msqkptr->u.msg_last->msg_next = msghdr;
982 msqkptr->u.msg_last = msghdr;
984 msqkptr->u.msg_last->msg_next = NULL;
986 msqkptr->u.msg_cbytes += msghdr->msg_ts;
987 msqkptr->u.msg_qnum++;
988 msqkptr->u.msg_lspid = td->td_proc->p_pid;
989 msqkptr->u.msg_stime = time_second;
992 td->td_retval[0] = 0;
995 PROC_LOCK(td->td_proc);
996 racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
997 racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
998 PROC_UNLOCK(td->td_proc);
1001 mtx_unlock(&msq_mtx);
1008 register struct msgsnd_args *uap;
1013 DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1014 uap->msgsz, uap->msgflg));
1016 if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1017 DPRINTF(("error %d copying the message type\n", error));
1020 return (kern_msgsnd(td, uap->msqid,
1021 (const char *)uap->msgp + sizeof(mtype),
1022 uap->msgsz, uap->msgflg, mtype));
1025 #ifndef _SYS_SYSPROTO_H_
1026 struct msgrcv_args {
1035 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1038 void *msgp; /* XXX msgp is actually mtext. */
1045 register struct msqid_kernel *msqkptr;
1046 register struct msg *msghdr;
1047 int msqix, error = 0;
1050 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1053 msqix = IPCID_TO_IX(msqid);
1055 if (msqix < 0 || msqix >= msginfo.msgmni) {
1056 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1061 msqkptr = &msqids[msqix];
1063 if (msqkptr->u.msg_qbytes == 0) {
1064 DPRINTF(("no such message queue id\n"));
1068 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1069 DPRINTF(("wrong sequence number\n"));
1074 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1075 DPRINTF(("requester doesn't have read access\n"));
1080 error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1086 while (msghdr == NULL) {
1088 msghdr = msqkptr->u.msg_first;
1089 if (msghdr != NULL) {
1090 if (msgsz < msghdr->msg_ts &&
1091 (msgflg & MSG_NOERROR) == 0) {
1092 DPRINTF(("first message on the queue "
1093 "is too big (want %zu, got %d)\n",
1094 msgsz, msghdr->msg_ts));
1099 error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1104 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1105 msqkptr->u.msg_first = NULL;
1106 msqkptr->u.msg_last = NULL;
1108 msqkptr->u.msg_first = msghdr->msg_next;
1109 if (msqkptr->u.msg_first == NULL)
1110 panic("msg_first/last screwed up #1");
1114 struct msg *previous;
1118 prev = &(msqkptr->u.msg_first);
1119 while ((msghdr = *prev) != NULL) {
1121 * Is this message's type an exact match or is
1122 * this message's type less than or equal to
1123 * the absolute value of a negative msgtyp?
1124 * Note that the second half of this test can
1125 * NEVER be true if msgtyp is positive since
1126 * msg_type is always positive!
1129 if (msgtyp == msghdr->msg_type ||
1130 msghdr->msg_type <= -msgtyp) {
1131 DPRINTF(("found message type %ld, "
1133 msghdr->msg_type, msgtyp));
1134 if (msgsz < msghdr->msg_ts &&
1135 (msgflg & MSG_NOERROR) == 0) {
1136 DPRINTF(("requested message "
1137 "on the queue is too big "
1138 "(want %zu, got %hu)\n",
1139 msgsz, msghdr->msg_ts));
1144 error = mac_sysvmsq_check_msgrcv(
1145 td->td_ucred, msghdr);
1149 *prev = msghdr->msg_next;
1150 if (msghdr == msqkptr->u.msg_last) {
1151 if (previous == NULL) {
1153 &msqkptr->u.msg_first)
1154 panic("msg_first/last screwed up #2");
1155 msqkptr->u.msg_first =
1157 msqkptr->u.msg_last =
1161 &msqkptr->u.msg_first)
1162 panic("msg_first/last screwed up #3");
1163 msqkptr->u.msg_last =
1170 prev = &(msghdr->msg_next);
1175 * We've either extracted the msghdr for the appropriate
1176 * message or there isn't one.
1177 * If there is one then bail out of this loop.
1184 * Hmph! No message found. Does the user want to wait?
1187 if ((msgflg & IPC_NOWAIT) != 0) {
1188 DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1190 /* The SVID says to return ENOMSG. */
1196 * Wait for something to happen
1199 DPRINTF(("msgrcv: goodnight\n"));
1200 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1202 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1205 DPRINTF(("msgrcv: interrupted system call\n"));
1211 * Make sure that the msq queue still exists
1214 if (msqkptr->u.msg_qbytes == 0 ||
1215 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1216 DPRINTF(("msqid deleted\n"));
1223 * Return the message to the user.
1225 * First, do the bookkeeping (before we risk being interrupted).
1228 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1229 msqkptr->u.msg_qnum--;
1230 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1231 msqkptr->u.msg_rtime = time_second;
1233 racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1234 racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1237 * Make msgsz the actual amount that we'll be returning.
1238 * Note that this effectively truncates the message if it is too long
1239 * (since msgsz is never increased).
1242 DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1244 if (msgsz > msghdr->msg_ts)
1245 msgsz = msghdr->msg_ts;
1246 *mtype = msghdr->msg_type;
1249 * Return the segments to the user
1252 next = msghdr->msg_spot;
1253 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1256 if (msgsz - len > msginfo.msgssz)
1257 tlen = msginfo.msgssz;
1261 panic("next too low #3");
1262 if (next >= msginfo.msgseg)
1263 panic("next out of range #3");
1264 mtx_unlock(&msq_mtx);
1265 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1268 DPRINTF(("error (%d) copying out message segment\n",
1270 msg_freehdr(msghdr);
1274 msgp = (char *)msgp + tlen;
1275 next = msgmaps[next].next;
1279 * Done, return the actual number of bytes copied out.
1282 msg_freehdr(msghdr);
1284 td->td_retval[0] = msgsz;
1286 mtx_unlock(&msq_mtx);
1293 register struct msgrcv_args *uap;
1298 DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1299 uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1301 if ((error = kern_msgrcv(td, uap->msqid,
1302 (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1303 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1305 if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1306 DPRINTF(("error %d copying the message type\n", error));
1311 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1314 return (SYSCTL_OUT(req, msqids,
1315 sizeof(struct msqid_kernel) * msginfo.msgmni));
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, CTLTYPE_OPAQUE | CTLFLAG_RD,
1331 NULL, 0, sysctl_msqids, "", "Message queue IDs");
1333 #ifdef COMPAT_FREEBSD32
1335 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1338 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1339 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1340 switch (uap->which) {
1342 return (freebsd7_freebsd32_msgctl(td,
1343 (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1345 return (freebsd32_msgsnd(td,
1346 (struct freebsd32_msgsnd_args *)&uap->a2));
1348 return (freebsd32_msgrcv(td,
1349 (struct freebsd32_msgrcv_args *)&uap->a2));
1351 return (msgsys(td, (struct msgsys_args *)uap));
1354 return (nosys(td, NULL));
1358 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1359 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1361 freebsd7_freebsd32_msgctl(struct thread *td,
1362 struct freebsd7_freebsd32_msgctl_args *uap)
1364 struct msqid_ds msqbuf;
1365 struct msqid_ds32_old msqbuf32;
1368 if (uap->cmd == IPC_SET) {
1369 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1372 freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1373 PTRIN_CP(msqbuf32, msqbuf, msg_first);
1374 PTRIN_CP(msqbuf32, msqbuf, msg_last);
1375 CP(msqbuf32, msqbuf, msg_cbytes);
1376 CP(msqbuf32, msqbuf, msg_qnum);
1377 CP(msqbuf32, msqbuf, msg_qbytes);
1378 CP(msqbuf32, msqbuf, msg_lspid);
1379 CP(msqbuf32, msqbuf, msg_lrpid);
1380 CP(msqbuf32, msqbuf, msg_stime);
1381 CP(msqbuf32, msqbuf, msg_rtime);
1382 CP(msqbuf32, msqbuf, msg_ctime);
1384 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1387 if (uap->cmd == IPC_STAT) {
1388 bzero(&msqbuf32, sizeof(msqbuf32));
1389 freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1390 PTROUT_CP(msqbuf, msqbuf32, msg_first);
1391 PTROUT_CP(msqbuf, msqbuf32, msg_last);
1392 CP(msqbuf, msqbuf32, msg_cbytes);
1393 CP(msqbuf, msqbuf32, msg_qnum);
1394 CP(msqbuf, msqbuf32, msg_qbytes);
1395 CP(msqbuf, msqbuf32, msg_lspid);
1396 CP(msqbuf, msqbuf32, msg_lrpid);
1397 CP(msqbuf, msqbuf32, msg_stime);
1398 CP(msqbuf, msqbuf32, msg_rtime);
1399 CP(msqbuf, msqbuf32, msg_ctime);
1400 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1407 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1409 struct msqid_ds msqbuf;
1410 struct msqid_ds32 msqbuf32;
1413 if (uap->cmd == IPC_SET) {
1414 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1417 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1418 PTRIN_CP(msqbuf32, msqbuf, msg_first);
1419 PTRIN_CP(msqbuf32, msqbuf, msg_last);
1420 CP(msqbuf32, msqbuf, msg_cbytes);
1421 CP(msqbuf32, msqbuf, msg_qnum);
1422 CP(msqbuf32, msqbuf, msg_qbytes);
1423 CP(msqbuf32, msqbuf, msg_lspid);
1424 CP(msqbuf32, msqbuf, msg_lrpid);
1425 CP(msqbuf32, msqbuf, msg_stime);
1426 CP(msqbuf32, msqbuf, msg_rtime);
1427 CP(msqbuf32, msqbuf, msg_ctime);
1429 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1432 if (uap->cmd == IPC_STAT) {
1433 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1434 PTROUT_CP(msqbuf, msqbuf32, msg_first);
1435 PTROUT_CP(msqbuf, msqbuf32, msg_last);
1436 CP(msqbuf, msqbuf32, msg_cbytes);
1437 CP(msqbuf, msqbuf32, msg_qnum);
1438 CP(msqbuf, msqbuf32, msg_qbytes);
1439 CP(msqbuf, msqbuf32, msg_lspid);
1440 CP(msqbuf, msqbuf32, msg_lrpid);
1441 CP(msqbuf, msqbuf32, msg_stime);
1442 CP(msqbuf, msqbuf32, msg_rtime);
1443 CP(msqbuf, msqbuf32, msg_ctime);
1444 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1450 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1457 msgp = PTRIN(uap->msgp);
1458 if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1461 return (kern_msgsnd(td, uap->msqid,
1462 (const char *)msgp + sizeof(mtype32),
1463 uap->msgsz, uap->msgflg, mtype));
1467 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1474 msgp = PTRIN(uap->msgp);
1475 if ((error = kern_msgrcv(td, uap->msqid,
1476 (char *)msgp + sizeof(mtype32), uap->msgsz,
1477 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1479 mtype32 = (int32_t)mtype;
1480 return (copyout(&mtype32, msgp, sizeof(mtype32)));
1484 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1485 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1487 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1488 static sy_call_t *msgcalls[] = {
1489 (sy_call_t *)freebsd7_msgctl, (sy_call_t *)msgget,
1490 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
1494 * Entry point for all MSG calls.
1499 /* XXX actually varargs. */
1500 struct msgsys_args /* {
1511 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1513 if (uap->which < 0 ||
1514 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
1516 error = (*msgcalls[uap->which])(td, &uap->a2);
1521 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1524 #ifndef _SYS_SYSPROTO_H_
1525 struct freebsd7_msgctl_args {
1528 struct msqid_ds_old *buf;
1532 freebsd7_msgctl(td, uap)
1534 struct freebsd7_msgctl_args *uap;
1536 struct msqid_ds_old msqold;
1537 struct msqid_ds msqbuf;
1540 DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1542 if (uap->cmd == IPC_SET) {
1543 error = copyin(uap->buf, &msqold, sizeof(msqold));
1546 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1547 CP(msqold, msqbuf, msg_first);
1548 CP(msqold, msqbuf, msg_last);
1549 CP(msqold, msqbuf, msg_cbytes);
1550 CP(msqold, msqbuf, msg_qnum);
1551 CP(msqold, msqbuf, msg_qbytes);
1552 CP(msqold, msqbuf, msg_lspid);
1553 CP(msqold, msqbuf, msg_lrpid);
1554 CP(msqold, msqbuf, msg_stime);
1555 CP(msqold, msqbuf, msg_rtime);
1556 CP(msqold, msqbuf, msg_ctime);
1558 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1561 if (uap->cmd == IPC_STAT) {
1562 bzero(&msqold, sizeof(msqold));
1563 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1564 CP(msqbuf, msqold, msg_first);
1565 CP(msqbuf, msqold, msg_last);
1566 CP(msqbuf, msqold, msg_cbytes);
1567 CP(msqbuf, msqold, msg_qnum);
1568 CP(msqbuf, msqold, msg_qbytes);
1569 CP(msqbuf, msqold, msg_lspid);
1570 CP(msqbuf, msqold, msg_lrpid);
1571 CP(msqbuf, msqold, msg_stime);
1572 CP(msqbuf, msqold, msg_rtime);
1573 CP(msqbuf, msqold, msg_ctime);
1574 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1581 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||