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>
65 #include <sys/mount.h>
67 #include <sys/racct.h>
69 #include <sys/syscall.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysent.h>
72 #include <sys/sysctl.h>
73 #include <sys/malloc.h>
76 #include <security/mac/mac_framework.h>
78 FEATURE(sysv_msg, "System V message queues support");
80 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
82 static int msginit(void);
83 static int msgunload(void);
84 static int sysvmsg_modload(struct module *, int, void *);
85 static void msq_remove(struct msqid_kernel *);
86 static struct prison *msg_find_prison(struct ucred *);
87 static int msq_prison_cansee(struct prison *, struct msqid_kernel *);
88 static int msg_prison_check(void *, void *);
89 static int msg_prison_set(void *, void *);
90 static int msg_prison_get(void *, void *);
91 static int msg_prison_remove(void *, void *);
92 static void msg_prison_cleanup(struct prison *);
96 #define DPRINTF(a) printf a
98 #define DPRINTF(a) (void)0
101 static void msg_freehdr(struct msg *msghdr);
104 #define MSGSSZ 8 /* Each segment must be 2^N long */
107 #define MSGSEG 2048 /* must be less than 32767 */
109 #define MSGMAX (MSGSSZ*MSGSEG)
111 #define MSGMNB 2048 /* max # of bytes in a queue */
121 * Based on the configuration parameters described in an SVR2 (yes, two)
122 * config(1m) man page.
124 * Each message is broken up and stored in segments that are msgssz bytes
125 * long. For efficiency reasons, this should be a power of two. Also,
126 * it doesn't make sense if it is less than 8 or greater than about 256.
127 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
128 * two between 8 and 1024 inclusive (and panic's if it isn't).
130 struct msginfo msginfo = {
131 MSGMAX, /* max chars in a message */
132 MSGMNI, /* # of message queue identifiers */
133 MSGMNB, /* max chars in a queue */
134 MSGTQL, /* max messages in system */
135 MSGSSZ, /* size of a message segment */
136 /* (must be small power of 2 greater than 4) */
137 MSGSEG /* number of message segments */
141 * macros to convert between msqid_ds's and msqid's.
142 * (specific to this implementation)
144 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
145 #define MSQID_IX(id) ((id) & 0xffff)
146 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
149 * The rest of this file is specific to this particular implementation.
153 short next; /* next segment in buffer */
154 /* -1 -> available */
155 /* 0..(MSGSEG-1) -> index of next segment */
158 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
160 static int nfree_msgmaps; /* # of free map entries */
161 static short free_msgmaps; /* head of linked list of free map entries */
162 static struct msg *free_msghdrs;/* list of free msg headers */
163 static char *msgpool; /* MSGMAX byte long msg buffer pool */
164 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
165 static struct msg *msghdrs; /* MSGTQL msg headers */
166 static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */
167 static struct mtx msq_mtx; /* global mutex for message queues. */
168 static unsigned msg_prison_slot;/* prison OSD slot */
170 static struct syscall_helper_data msg_syscalls[] = {
171 SYSCALL_INIT_HELPER(msgctl),
172 SYSCALL_INIT_HELPER(msgget),
173 SYSCALL_INIT_HELPER(msgsnd),
174 SYSCALL_INIT_HELPER(msgrcv),
175 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
176 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
177 SYSCALL_INIT_HELPER(msgsys),
178 SYSCALL_INIT_HELPER_COMPAT(freebsd7_msgctl),
183 #ifdef COMPAT_FREEBSD32
184 #include <compat/freebsd32/freebsd32.h>
185 #include <compat/freebsd32/freebsd32_ipc.h>
186 #include <compat/freebsd32/freebsd32_proto.h>
187 #include <compat/freebsd32/freebsd32_signal.h>
188 #include <compat/freebsd32/freebsd32_syscall.h>
189 #include <compat/freebsd32/freebsd32_util.h>
191 static struct syscall_helper_data msg32_syscalls[] = {
192 SYSCALL32_INIT_HELPER(freebsd32_msgctl),
193 SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
194 SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
195 SYSCALL32_INIT_HELPER_COMPAT(msgget),
196 SYSCALL32_INIT_HELPER(freebsd32_msgsys),
197 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
198 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
199 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
211 osd_method_t methods[PR_MAXMETHOD] = {
212 [PR_METHOD_CHECK] = msg_prison_check,
213 [PR_METHOD_SET] = msg_prison_set,
214 [PR_METHOD_GET] = msg_prison_get,
215 [PR_METHOD_REMOVE] = msg_prison_remove,
218 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
219 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
220 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
221 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
222 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
223 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
225 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
226 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
227 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
228 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
232 * msginfo.msgssz should be a power of two for efficiency reasons.
233 * It is also pretty silly if msginfo.msgssz is less than 8
234 * or greater than about 256 so ...
238 while (i < 1024 && i != msginfo.msgssz)
240 if (i != msginfo.msgssz) {
241 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
243 panic("msginfo.msgssz not a small power of 2");
246 if (msginfo.msgseg > 32767) {
247 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
248 panic("msginfo.msgseg > 32767");
251 for (i = 0; i < msginfo.msgseg; i++) {
253 msgmaps[i-1].next = i;
254 msgmaps[i].next = -1; /* implies entry is available */
257 nfree_msgmaps = msginfo.msgseg;
259 for (i = 0; i < msginfo.msgtql; i++) {
260 msghdrs[i].msg_type = 0;
262 msghdrs[i-1].msg_next = &msghdrs[i];
263 msghdrs[i].msg_next = NULL;
265 mac_sysvmsg_init(&msghdrs[i]);
268 free_msghdrs = &msghdrs[0];
270 for (i = 0; i < msginfo.msgmni; i++) {
271 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
272 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
273 msqids[i].u.msg_perm.mode = 0;
275 mac_sysvmsq_init(&msqids[i]);
278 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
280 /* Set current prisons according to their allow.sysvipc. */
281 msg_prison_slot = osd_jail_register(NULL, methods);
282 rsv = osd_reserve(msg_prison_slot);
283 prison_lock(&prison0);
284 (void)osd_jail_set_reserved(&prison0, msg_prison_slot, rsv, &prison0);
285 prison_unlock(&prison0);
287 sx_slock(&allprison_lock);
288 TAILQ_FOREACH(pr, &allprison, pr_list) {
290 rsv = osd_reserve(msg_prison_slot);
292 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
293 (void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
300 osd_free_reserved(rsv);
301 sx_sunlock(&allprison_lock);
303 error = syscall_helper_register(msg_syscalls);
306 #ifdef COMPAT_FREEBSD32
307 error = syscall32_helper_register(msg32_syscalls);
317 struct msqid_kernel *msqkptr;
323 syscall_helper_unregister(msg_syscalls);
324 #ifdef COMPAT_FREEBSD32
325 syscall32_helper_unregister(msg32_syscalls);
328 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
329 msqkptr = &msqids[msqid];
330 if (msqkptr->u.msg_qbytes != 0 ||
331 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
334 if (msqid != msginfo.msgmni)
337 if (msg_prison_slot != 0)
338 osd_jail_deregister(msg_prison_slot);
340 for (i = 0; i < msginfo.msgtql; i++)
341 mac_sysvmsg_destroy(&msghdrs[i]);
342 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
343 mac_sysvmsq_destroy(&msqids[msqid]);
345 free(msgpool, M_MSG);
346 free(msgmaps, M_MSG);
347 free(msghdrs, M_MSG);
349 mtx_destroy(&msq_mtx);
355 sysvmsg_modload(struct module *module, int cmd, void *arg)
377 static moduledata_t sysvmsg_mod = {
383 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
384 MODULE_VERSION(sysvmsg, 1);
390 while (msghdr->msg_ts > 0) {
392 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
393 panic("msghdr->msg_spot out of range");
394 next = msgmaps[msghdr->msg_spot].next;
395 msgmaps[msghdr->msg_spot].next = free_msgmaps;
396 free_msgmaps = msghdr->msg_spot;
398 msghdr->msg_spot = next;
399 if (msghdr->msg_ts >= msginfo.msgssz)
400 msghdr->msg_ts -= msginfo.msgssz;
404 if (msghdr->msg_spot != -1)
405 panic("msghdr->msg_spot != -1");
406 msghdr->msg_next = free_msghdrs;
407 free_msghdrs = msghdr;
409 mac_sysvmsg_cleanup(msghdr);
414 msq_remove(struct msqid_kernel *msqkptr)
418 racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
419 racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
420 racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
421 crfree(msqkptr->cred);
422 msqkptr->cred = NULL;
424 /* Free the message headers */
425 msghdr = msqkptr->u.msg_first;
426 while (msghdr != NULL) {
427 struct msg *msghdr_tmp;
429 /* Free the segments of each message */
430 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
431 msqkptr->u.msg_qnum--;
433 msghdr = msghdr->msg_next;
434 msg_freehdr(msghdr_tmp);
437 if (msqkptr->u.msg_cbytes != 0)
438 panic("msg_cbytes is screwed up");
439 if (msqkptr->u.msg_qnum != 0)
440 panic("msg_qnum is screwed up");
442 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
445 mac_sysvmsq_cleanup(msqkptr);
451 static struct prison *
452 msg_find_prison(struct ucred *cred)
454 struct prison *pr, *rpr;
456 pr = cred->cr_prison;
458 rpr = osd_jail_get(pr, msg_prison_slot);
464 msq_prison_cansee(struct prison *rpr, struct msqid_kernel *msqkptr)
467 if (msqkptr->cred == NULL ||
468 !(rpr == msqkptr->cred->cr_prison ||
469 prison_ischild(rpr, msqkptr->cred->cr_prison)))
474 #ifndef _SYS_SYSPROTO_H_
478 struct msqid_ds *buf;
484 register struct msgctl_args *uap;
486 int msqid = uap->msqid;
488 struct msqid_ds msqbuf;
491 DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
492 if (cmd == IPC_SET &&
493 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
495 error = kern_msgctl(td, msqid, cmd, &msqbuf);
496 if (cmd == IPC_STAT && error == 0)
497 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
502 kern_msgctl(td, msqid, cmd, msqbuf)
506 struct msqid_ds *msqbuf;
508 int rval, error, msqix;
509 register struct msqid_kernel *msqkptr;
512 rpr = msg_find_prison(td->td_ucred);
516 msqix = IPCID_TO_IX(msqid);
518 if (msqix < 0 || msqix >= msginfo.msgmni) {
519 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
524 msqkptr = &msqids[msqix];
527 if (msqkptr->u.msg_qbytes == 0) {
528 DPRINTF(("no such msqid\n"));
532 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
533 DPRINTF(("wrong sequence number\n"));
538 error = msq_prison_cansee(rpr, msqkptr);
540 DPRINTF(("requester can't see prison\n"));
545 error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
560 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
565 * Check that the thread has MAC access permissions to
566 * individual msghdrs. Note: We need to do this in a
567 * separate loop because the actual loop alters the
568 * msq/msghdr info as it progresses, and there is no going
569 * back if half the way through we discover that the
570 * thread cannot free a certain msghdr. The msq will get
571 * into an inconsistent state.
573 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
574 msghdr = msghdr->msg_next) {
575 error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
587 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
589 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
590 error = priv_check(td, PRIV_IPC_MSGSIZE);
594 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
595 DPRINTF(("can't increase msg_qbytes beyond %d"
596 "(truncating)\n", msginfo.msgmnb));
597 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
599 if (msqbuf->msg_qbytes == 0) {
600 DPRINTF(("can't reduce msg_qbytes to 0\n"));
601 error = EINVAL; /* non-standard errno! */
604 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
605 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
606 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
607 (msqbuf->msg_perm.mode & 0777);
608 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
609 msqkptr->u.msg_ctime = time_second;
613 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
614 DPRINTF(("requester doesn't have read access\n"));
617 *msqbuf = msqkptr->u;
618 if (td->td_ucred->cr_prison != msqkptr->cred->cr_prison)
619 msqbuf->msg_perm.key = IPC_PRIVATE;
623 DPRINTF(("invalid command %d\n", cmd));
629 td->td_retval[0] = rval;
631 mtx_unlock(&msq_mtx);
635 #ifndef _SYS_SYSPROTO_H_
645 register struct msgget_args *uap;
647 int msqid, error = 0;
649 int msgflg = uap->msgflg;
650 struct ucred *cred = td->td_ucred;
651 register struct msqid_kernel *msqkptr = NULL;
653 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
655 if (msg_find_prison(cred) == NULL)
659 if (key != IPC_PRIVATE) {
660 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
661 msqkptr = &msqids[msqid];
662 if (msqkptr->u.msg_qbytes != 0 &&
663 msqkptr->cred != NULL &&
664 msqkptr->cred->cr_prison == cred->cr_prison &&
665 msqkptr->u.msg_perm.key == key)
668 if (msqid < msginfo.msgmni) {
669 DPRINTF(("found public key\n"));
670 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
671 DPRINTF(("not exclusive\n"));
675 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
677 DPRINTF(("requester doesn't have 0%o access\n",
682 error = mac_sysvmsq_check_msqget(cred, msqkptr);
690 DPRINTF(("need to allocate the msqid_ds\n"));
691 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
692 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
694 * Look for an unallocated and unlocked msqid_ds.
695 * msqid_ds's can be locked by msgsnd or msgrcv while
696 * they are copying the message in/out. We can't
697 * re-use the entry until they release it.
699 msqkptr = &msqids[msqid];
700 if (msqkptr->u.msg_qbytes == 0 &&
701 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
704 if (msqid == msginfo.msgmni) {
705 DPRINTF(("no more msqid_ds's available\n"));
711 PROC_LOCK(td->td_proc);
712 error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
713 PROC_UNLOCK(td->td_proc);
720 DPRINTF(("msqid %d is available\n", msqid));
721 msqkptr->u.msg_perm.key = key;
722 msqkptr->u.msg_perm.cuid = cred->cr_uid;
723 msqkptr->u.msg_perm.uid = cred->cr_uid;
724 msqkptr->u.msg_perm.cgid = cred->cr_gid;
725 msqkptr->u.msg_perm.gid = cred->cr_gid;
726 msqkptr->u.msg_perm.mode = (msgflg & 0777);
727 msqkptr->cred = crhold(cred);
728 /* Make sure that the returned msqid is unique */
729 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
730 msqkptr->u.msg_first = NULL;
731 msqkptr->u.msg_last = NULL;
732 msqkptr->u.msg_cbytes = 0;
733 msqkptr->u.msg_qnum = 0;
734 msqkptr->u.msg_qbytes = msginfo.msgmnb;
735 msqkptr->u.msg_lspid = 0;
736 msqkptr->u.msg_lrpid = 0;
737 msqkptr->u.msg_stime = 0;
738 msqkptr->u.msg_rtime = 0;
739 msqkptr->u.msg_ctime = time_second;
741 mac_sysvmsq_create(cred, msqkptr);
744 DPRINTF(("didn't find it and wasn't asked to create it\n"));
750 /* Construct the unique msqid */
751 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
753 mtx_unlock(&msq_mtx);
757 #ifndef _SYS_SYSPROTO_H_
766 kern_msgsnd(td, msqid, msgp, msgsz, msgflg, mtype)
769 const void *msgp; /* XXX msgp is actually mtext. */
774 int msqix, segs_needed, error = 0;
775 register struct msqid_kernel *msqkptr;
776 register struct msg *msghdr;
783 rpr = msg_find_prison(td->td_ucred);
788 msqix = IPCID_TO_IX(msqid);
790 if (msqix < 0 || msqix >= msginfo.msgmni) {
791 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
797 msqkptr = &msqids[msqix];
798 if (msqkptr->u.msg_qbytes == 0) {
799 DPRINTF(("no such message queue id\n"));
803 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
804 DPRINTF(("wrong sequence number\n"));
809 if ((error = msq_prison_cansee(rpr, msqkptr))) {
810 DPRINTF(("requester can't see prison\n"));
814 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
815 DPRINTF(("requester doesn't have write access\n"));
820 error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
827 PROC_LOCK(td->td_proc);
828 if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
829 PROC_UNLOCK(td->td_proc);
834 if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
835 racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
836 PROC_UNLOCK(td->td_proc);
840 PROC_UNLOCK(td->td_proc);
844 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
845 DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
846 msginfo.msgssz, segs_needed));
848 int need_more_resources = 0;
852 * (inside this loop in case msg_qbytes changes while we sleep)
855 if (msgsz > msqkptr->u.msg_qbytes) {
856 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
861 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
862 DPRINTF(("msqid is locked\n"));
863 need_more_resources = 1;
865 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
866 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
867 need_more_resources = 1;
869 if (segs_needed > nfree_msgmaps) {
870 DPRINTF(("segs_needed > nfree_msgmaps\n"));
871 need_more_resources = 1;
873 if (free_msghdrs == NULL) {
874 DPRINTF(("no more msghdrs\n"));
875 need_more_resources = 1;
878 if (need_more_resources) {
881 if ((msgflg & IPC_NOWAIT) != 0) {
882 DPRINTF(("need more resources but caller "
883 "doesn't want to wait\n"));
888 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
889 DPRINTF(("we don't own the msqid_ds\n"));
892 /* Force later arrivals to wait for our
894 DPRINTF(("we own the msqid_ds\n"));
895 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
898 DPRINTF(("msgsnd: goodnight\n"));
899 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
901 DPRINTF(("msgsnd: good morning, error=%d\n", error));
903 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
904 if (error == EWOULDBLOCK) {
905 DPRINTF(("msgsnd: timed out\n"));
909 DPRINTF(("msgsnd: interrupted system call\n"));
915 * Make sure that the msq queue still exists
918 if (msqkptr->u.msg_qbytes == 0) {
919 DPRINTF(("msqid deleted\n"));
925 DPRINTF(("got all the resources that we need\n"));
931 * We have the resources that we need.
935 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
936 panic("msg_perm.mode & MSG_LOCKED");
937 if (segs_needed > nfree_msgmaps)
938 panic("segs_needed > nfree_msgmaps");
939 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
940 panic("msgsz + msg_cbytes > msg_qbytes");
941 if (free_msghdrs == NULL)
942 panic("no more msghdrs");
945 * Re-lock the msqid_ds in case we page-fault when copying in the
949 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
950 panic("msqid_ds is already locked");
951 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
954 * Allocate a message header
957 msghdr = free_msghdrs;
958 free_msghdrs = msghdr->msg_next;
959 msghdr->msg_spot = -1;
960 msghdr->msg_ts = msgsz;
961 msghdr->msg_type = mtype;
964 * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
965 * immediately? Or, should it be checked just before the msg is
966 * enqueued in the msgq (as it is done now)?
968 mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
972 * Allocate space for the message
975 while (segs_needed > 0) {
976 if (nfree_msgmaps <= 0)
977 panic("not enough msgmaps");
978 if (free_msgmaps == -1)
979 panic("nil free_msgmaps");
982 panic("next too low #1");
983 if (next >= msginfo.msgseg)
984 panic("next out of range #1");
985 DPRINTF(("allocating segment %d to message\n", next));
986 free_msgmaps = msgmaps[next].next;
988 msgmaps[next].next = msghdr->msg_spot;
989 msghdr->msg_spot = next;
994 * Validate the message type
997 if (msghdr->msg_type < 1) {
999 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1001 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
1007 * Copy in the message body
1010 next = msghdr->msg_spot;
1013 if (msgsz > msginfo.msgssz)
1014 tlen = msginfo.msgssz;
1018 panic("next too low #2");
1019 if (next >= msginfo.msgseg)
1020 panic("next out of range #2");
1021 mtx_unlock(&msq_mtx);
1022 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
1025 DPRINTF(("error %d copying in message segment\n",
1027 msg_freehdr(msghdr);
1028 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1034 msgp = (const char *)msgp + tlen;
1035 next = msgmaps[next].next;
1038 panic("didn't use all the msg segments");
1041 * We've got the message. Unlock the msqid_ds.
1044 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1047 * Make sure that the msqid_ds is still allocated.
1050 if (msqkptr->u.msg_qbytes == 0) {
1051 msg_freehdr(msghdr);
1059 * Note: Since the task/thread allocates the msghdr and usually
1060 * primes it with its own MAC label, for a majority of policies, it
1061 * won't be necessary to check whether the msghdr has access
1062 * permissions to the msgq. The mac_sysvmsq_check_msqsnd check would
1063 * suffice in that case. However, this hook may be required where
1064 * individual policies derive a non-identical label for the msghdr
1065 * from the current thread label and may want to check the msghdr
1066 * enqueue permissions, along with read/write permissions to the
1069 error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
1071 msg_freehdr(msghdr);
1078 * Put the message into the queue
1080 if (msqkptr->u.msg_first == NULL) {
1081 msqkptr->u.msg_first = msghdr;
1082 msqkptr->u.msg_last = msghdr;
1084 msqkptr->u.msg_last->msg_next = msghdr;
1085 msqkptr->u.msg_last = msghdr;
1087 msqkptr->u.msg_last->msg_next = NULL;
1089 msqkptr->u.msg_cbytes += msghdr->msg_ts;
1090 msqkptr->u.msg_qnum++;
1091 msqkptr->u.msg_lspid = td->td_proc->p_pid;
1092 msqkptr->u.msg_stime = time_second;
1095 td->td_retval[0] = 0;
1098 if (racct_enable && error != 0) {
1099 PROC_LOCK(td->td_proc);
1100 racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1101 racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1102 PROC_UNLOCK(td->td_proc);
1106 mtx_unlock(&msq_mtx);
1113 register struct msgsnd_args *uap;
1118 DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1119 uap->msgsz, uap->msgflg));
1121 if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1122 DPRINTF(("error %d copying the message type\n", error));
1125 return (kern_msgsnd(td, uap->msqid,
1126 (const char *)uap->msgp + sizeof(mtype),
1127 uap->msgsz, uap->msgflg, mtype));
1130 #ifndef _SYS_SYSPROTO_H_
1131 struct msgrcv_args {
1140 kern_msgrcv(td, msqid, msgp, msgsz, msgtyp, msgflg, mtype)
1143 void *msgp; /* XXX msgp is actually mtext. */
1150 register struct msqid_kernel *msqkptr;
1151 register struct msg *msghdr;
1153 int msqix, error = 0;
1156 rpr = msg_find_prison(td->td_ucred);
1160 msqix = IPCID_TO_IX(msqid);
1162 if (msqix < 0 || msqix >= msginfo.msgmni) {
1163 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1168 msqkptr = &msqids[msqix];
1170 if (msqkptr->u.msg_qbytes == 0) {
1171 DPRINTF(("no such message queue id\n"));
1175 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1176 DPRINTF(("wrong sequence number\n"));
1181 if ((error = msq_prison_cansee(rpr, msqkptr))) {
1182 DPRINTF(("requester can't see prison\n"));
1186 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1187 DPRINTF(("requester doesn't have read access\n"));
1192 error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1198 while (msghdr == NULL) {
1200 msghdr = msqkptr->u.msg_first;
1201 if (msghdr != NULL) {
1202 if (msgsz < msghdr->msg_ts &&
1203 (msgflg & MSG_NOERROR) == 0) {
1204 DPRINTF(("first message on the queue "
1205 "is too big (want %zu, got %d)\n",
1206 msgsz, msghdr->msg_ts));
1211 error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1216 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1217 msqkptr->u.msg_first = NULL;
1218 msqkptr->u.msg_last = NULL;
1220 msqkptr->u.msg_first = msghdr->msg_next;
1221 if (msqkptr->u.msg_first == NULL)
1222 panic("msg_first/last screwed up #1");
1226 struct msg *previous;
1230 prev = &(msqkptr->u.msg_first);
1231 while ((msghdr = *prev) != NULL) {
1233 * Is this message's type an exact match or is
1234 * this message's type less than or equal to
1235 * the absolute value of a negative msgtyp?
1236 * Note that the second half of this test can
1237 * NEVER be true if msgtyp is positive since
1238 * msg_type is always positive!
1241 if (msgtyp == msghdr->msg_type ||
1242 msghdr->msg_type <= -msgtyp) {
1243 DPRINTF(("found message type %ld, "
1245 msghdr->msg_type, msgtyp));
1246 if (msgsz < msghdr->msg_ts &&
1247 (msgflg & MSG_NOERROR) == 0) {
1248 DPRINTF(("requested message "
1249 "on the queue is too big "
1250 "(want %zu, got %hu)\n",
1251 msgsz, msghdr->msg_ts));
1256 error = mac_sysvmsq_check_msgrcv(
1257 td->td_ucred, msghdr);
1261 *prev = msghdr->msg_next;
1262 if (msghdr == msqkptr->u.msg_last) {
1263 if (previous == NULL) {
1265 &msqkptr->u.msg_first)
1266 panic("msg_first/last screwed up #2");
1267 msqkptr->u.msg_first =
1269 msqkptr->u.msg_last =
1273 &msqkptr->u.msg_first)
1274 panic("msg_first/last screwed up #3");
1275 msqkptr->u.msg_last =
1282 prev = &(msghdr->msg_next);
1287 * We've either extracted the msghdr for the appropriate
1288 * message or there isn't one.
1289 * If there is one then bail out of this loop.
1296 * Hmph! No message found. Does the user want to wait?
1299 if ((msgflg & IPC_NOWAIT) != 0) {
1300 DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1302 /* The SVID says to return ENOMSG. */
1308 * Wait for something to happen
1311 DPRINTF(("msgrcv: goodnight\n"));
1312 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1314 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1317 DPRINTF(("msgrcv: interrupted system call\n"));
1323 * Make sure that the msq queue still exists
1326 if (msqkptr->u.msg_qbytes == 0 ||
1327 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1328 DPRINTF(("msqid deleted\n"));
1335 * Return the message to the user.
1337 * First, do the bookkeeping (before we risk being interrupted).
1340 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1341 msqkptr->u.msg_qnum--;
1342 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1343 msqkptr->u.msg_rtime = time_second;
1345 racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1346 racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1349 * Make msgsz the actual amount that we'll be returning.
1350 * Note that this effectively truncates the message if it is too long
1351 * (since msgsz is never increased).
1354 DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1356 if (msgsz > msghdr->msg_ts)
1357 msgsz = msghdr->msg_ts;
1358 *mtype = msghdr->msg_type;
1361 * Return the segments to the user
1364 next = msghdr->msg_spot;
1365 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1368 if (msgsz - len > msginfo.msgssz)
1369 tlen = msginfo.msgssz;
1373 panic("next too low #3");
1374 if (next >= msginfo.msgseg)
1375 panic("next out of range #3");
1376 mtx_unlock(&msq_mtx);
1377 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1380 DPRINTF(("error (%d) copying out message segment\n",
1382 msg_freehdr(msghdr);
1386 msgp = (char *)msgp + tlen;
1387 next = msgmaps[next].next;
1391 * Done, return the actual number of bytes copied out.
1394 msg_freehdr(msghdr);
1396 td->td_retval[0] = msgsz;
1398 mtx_unlock(&msq_mtx);
1405 register struct msgrcv_args *uap;
1410 DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1411 uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1413 if ((error = kern_msgrcv(td, uap->msqid,
1414 (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1415 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1417 if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1418 DPRINTF(("error %d copying the message type\n", error));
1423 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1425 struct msqid_kernel tmsqk;
1426 struct prison *pr, *rpr;
1429 pr = req->td->td_ucred->cr_prison;
1430 rpr = msg_find_prison(req->td->td_ucred);
1432 for (i = 0; i < msginfo.msgmni; i++) {
1434 if (msqids[i].u.msg_qbytes == 0 || rpr == NULL ||
1435 msq_prison_cansee(rpr, &msqids[i]) != 0)
1436 bzero(&tmsqk, sizeof(tmsqk));
1439 if (tmsqk.cred->cr_prison != pr)
1440 tmsqk.u.msg_perm.key = IPC_PRIVATE;
1442 mtx_unlock(&msq_mtx);
1443 error = SYSCTL_OUT(req, &tmsqk, sizeof(tmsqk));
1450 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1451 "Maximum message size");
1452 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1453 "Number of message queue identifiers");
1454 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1455 "Maximum number of bytes in a queue");
1456 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1457 "Maximum number of messages in the system");
1458 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1459 "Size of a message segment");
1460 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1461 "Number of message segments");
1462 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids,
1463 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
1464 NULL, 0, sysctl_msqids, "",
1465 "Array of struct msqid_kernel for each potential message queue");
1468 msg_prison_check(void *obj, void *data)
1470 struct prison *pr = obj;
1471 struct prison *prpr;
1472 struct vfsoptlist *opts = data;
1476 * sysvmsg is a jailsys integer.
1477 * It must be "disable" if the parent jail is disabled.
1479 error = vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1480 if (error != ENOENT) {
1484 case JAIL_SYS_DISABLE:
1487 case JAIL_SYS_INHERIT:
1488 prison_lock(pr->pr_parent);
1489 prpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1490 prison_unlock(pr->pr_parent);
1503 msg_prison_set(void *obj, void *data)
1505 struct prison *pr = obj;
1506 struct prison *tpr, *orpr, *nrpr, *trpr;
1507 struct vfsoptlist *opts = data;
1512 * sysvmsg controls which jail is the root of the associated msgs (this
1513 * jail or same as the parent), or if the feature is available at all.
1515 if (vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys)) == ENOENT)
1516 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1518 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1521 if (jsys == JAIL_SYS_DISABLE) {
1523 orpr = osd_jail_get(pr, msg_prison_slot);
1525 osd_jail_del(pr, msg_prison_slot);
1529 msg_prison_cleanup(pr);
1530 /* Disable all child jails as well. */
1531 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1533 trpr = osd_jail_get(tpr, msg_prison_slot);
1535 osd_jail_del(tpr, msg_prison_slot);
1538 msg_prison_cleanup(tpr);
1545 } else if (jsys != -1) {
1546 if (jsys == JAIL_SYS_NEW)
1549 prison_lock(pr->pr_parent);
1550 nrpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1551 prison_unlock(pr->pr_parent);
1553 rsv = osd_reserve(msg_prison_slot);
1555 orpr = osd_jail_get(pr, msg_prison_slot);
1557 (void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
1560 osd_free_reserved(rsv);
1564 msg_prison_cleanup(pr);
1566 /* Change child jails matching the old root, */
1567 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1569 trpr = osd_jail_get(tpr,
1572 (void)osd_jail_set(tpr,
1573 msg_prison_slot, nrpr);
1576 msg_prison_cleanup(tpr);
1590 msg_prison_get(void *obj, void *data)
1592 struct prison *pr = obj;
1594 struct vfsoptlist *opts = data;
1597 /* Set sysvmsg based on the jail's root prison. */
1599 rpr = osd_jail_get(pr, msg_prison_slot);
1601 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1602 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1603 error = vfs_setopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1604 if (error == ENOENT)
1610 msg_prison_remove(void *obj, void *data __unused)
1612 struct prison *pr = obj;
1616 rpr = osd_jail_get(pr, msg_prison_slot);
1619 msg_prison_cleanup(pr);
1624 msg_prison_cleanup(struct prison *pr)
1626 struct msqid_kernel *msqkptr;
1629 /* Remove any msqs that belong to this jail. */
1631 for (i = 0; i < msginfo.msgmni; i++) {
1632 msqkptr = &msqids[i];
1633 if (msqkptr->u.msg_qbytes != 0 &&
1634 msqkptr->cred != NULL && msqkptr->cred->cr_prison == pr)
1635 msq_remove(msqkptr);
1637 mtx_unlock(&msq_mtx);
1640 SYSCTL_JAIL_PARAM_SYS_NODE(sysvmsg, CTLFLAG_RW, "SYSV message queues");
1642 #ifdef COMPAT_FREEBSD32
1644 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1647 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1648 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1649 switch (uap->which) {
1651 return (freebsd7_freebsd32_msgctl(td,
1652 (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1654 return (freebsd32_msgsnd(td,
1655 (struct freebsd32_msgsnd_args *)&uap->a2));
1657 return (freebsd32_msgrcv(td,
1658 (struct freebsd32_msgrcv_args *)&uap->a2));
1660 return (sys_msgsys(td, (struct msgsys_args *)uap));
1663 return (nosys(td, NULL));
1667 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1668 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1670 freebsd7_freebsd32_msgctl(struct thread *td,
1671 struct freebsd7_freebsd32_msgctl_args *uap)
1673 struct msqid_ds msqbuf;
1674 struct msqid_ds32_old msqbuf32;
1677 if (uap->cmd == IPC_SET) {
1678 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1681 freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1682 PTRIN_CP(msqbuf32, msqbuf, msg_first);
1683 PTRIN_CP(msqbuf32, msqbuf, msg_last);
1684 CP(msqbuf32, msqbuf, msg_cbytes);
1685 CP(msqbuf32, msqbuf, msg_qnum);
1686 CP(msqbuf32, msqbuf, msg_qbytes);
1687 CP(msqbuf32, msqbuf, msg_lspid);
1688 CP(msqbuf32, msqbuf, msg_lrpid);
1689 CP(msqbuf32, msqbuf, msg_stime);
1690 CP(msqbuf32, msqbuf, msg_rtime);
1691 CP(msqbuf32, msqbuf, msg_ctime);
1693 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1696 if (uap->cmd == IPC_STAT) {
1697 bzero(&msqbuf32, sizeof(msqbuf32));
1698 freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1699 PTROUT_CP(msqbuf, msqbuf32, msg_first);
1700 PTROUT_CP(msqbuf, msqbuf32, msg_last);
1701 CP(msqbuf, msqbuf32, msg_cbytes);
1702 CP(msqbuf, msqbuf32, msg_qnum);
1703 CP(msqbuf, msqbuf32, msg_qbytes);
1704 CP(msqbuf, msqbuf32, msg_lspid);
1705 CP(msqbuf, msqbuf32, msg_lrpid);
1706 CP(msqbuf, msqbuf32, msg_stime);
1707 CP(msqbuf, msqbuf32, msg_rtime);
1708 CP(msqbuf, msqbuf32, msg_ctime);
1709 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1716 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1718 struct msqid_ds msqbuf;
1719 struct msqid_ds32 msqbuf32;
1722 if (uap->cmd == IPC_SET) {
1723 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1726 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1727 PTRIN_CP(msqbuf32, msqbuf, msg_first);
1728 PTRIN_CP(msqbuf32, msqbuf, msg_last);
1729 CP(msqbuf32, msqbuf, msg_cbytes);
1730 CP(msqbuf32, msqbuf, msg_qnum);
1731 CP(msqbuf32, msqbuf, msg_qbytes);
1732 CP(msqbuf32, msqbuf, msg_lspid);
1733 CP(msqbuf32, msqbuf, msg_lrpid);
1734 CP(msqbuf32, msqbuf, msg_stime);
1735 CP(msqbuf32, msqbuf, msg_rtime);
1736 CP(msqbuf32, msqbuf, msg_ctime);
1738 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1741 if (uap->cmd == IPC_STAT) {
1742 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1743 PTROUT_CP(msqbuf, msqbuf32, msg_first);
1744 PTROUT_CP(msqbuf, msqbuf32, msg_last);
1745 CP(msqbuf, msqbuf32, msg_cbytes);
1746 CP(msqbuf, msqbuf32, msg_qnum);
1747 CP(msqbuf, msqbuf32, msg_qbytes);
1748 CP(msqbuf, msqbuf32, msg_lspid);
1749 CP(msqbuf, msqbuf32, msg_lrpid);
1750 CP(msqbuf, msqbuf32, msg_stime);
1751 CP(msqbuf, msqbuf32, msg_rtime);
1752 CP(msqbuf, msqbuf32, msg_ctime);
1753 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1759 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1766 msgp = PTRIN(uap->msgp);
1767 if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1770 return (kern_msgsnd(td, uap->msqid,
1771 (const char *)msgp + sizeof(mtype32),
1772 uap->msgsz, uap->msgflg, mtype));
1776 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1783 msgp = PTRIN(uap->msgp);
1784 if ((error = kern_msgrcv(td, uap->msqid,
1785 (char *)msgp + sizeof(mtype32), uap->msgsz,
1786 uap->msgtyp, uap->msgflg, &mtype)) != 0)
1788 mtype32 = (int32_t)mtype;
1789 return (copyout(&mtype32, msgp, sizeof(mtype32)));
1793 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1794 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1796 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1797 static sy_call_t *msgcalls[] = {
1798 (sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
1799 (sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
1803 * Entry point for all MSG calls.
1808 /* XXX actually varargs. */
1809 struct msgsys_args /* {
1820 if (uap->which < 0 ||
1821 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
1823 error = (*msgcalls[uap->which])(td, &uap->a2);
1828 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1831 #ifndef _SYS_SYSPROTO_H_
1832 struct freebsd7_msgctl_args {
1835 struct msqid_ds_old *buf;
1839 freebsd7_msgctl(td, uap)
1841 struct freebsd7_msgctl_args *uap;
1843 struct msqid_ds_old msqold;
1844 struct msqid_ds msqbuf;
1847 DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1849 if (uap->cmd == IPC_SET) {
1850 error = copyin(uap->buf, &msqold, sizeof(msqold));
1853 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1854 CP(msqold, msqbuf, msg_first);
1855 CP(msqold, msqbuf, msg_last);
1856 CP(msqold, msqbuf, msg_cbytes);
1857 CP(msqold, msqbuf, msg_qnum);
1858 CP(msqold, msqbuf, msg_qbytes);
1859 CP(msqold, msqbuf, msg_lspid);
1860 CP(msqold, msqbuf, msg_lrpid);
1861 CP(msqold, msqbuf, msg_stime);
1862 CP(msqold, msqbuf, msg_rtime);
1863 CP(msqold, msqbuf, msg_ctime);
1865 error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1868 if (uap->cmd == IPC_STAT) {
1869 bzero(&msqold, sizeof(msqold));
1870 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1871 CP(msqbuf, msqold, msg_first);
1872 CP(msqbuf, msqold, msg_last);
1873 CP(msqbuf, msqold, msg_cbytes);
1874 CP(msqbuf, msqold, msg_qnum);
1875 CP(msqbuf, msqold, msg_qbytes);
1876 CP(msqbuf, msqold, msg_lspid);
1877 CP(msqbuf, msqold, msg_lrpid);
1878 CP(msqbuf, msqold, msg_stime);
1879 CP(msqbuf, msqold, msg_rtime);
1880 CP(msqbuf, msqold, msg_ctime);
1881 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1888 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||