1 /* $FreeBSD: src/sys/kern/sysv_msg.c,v 1.23.2.5 2002/12/31 08:54:53 maxim Exp $ */
4 * Implementation of SVID messages
6 * Author: Daniel Boulet
8 * Copyright 1993 Daniel Boulet and RTMX Inc.
10 * This system call was implemented by Daniel Boulet under contract from RTMX.
12 * Redistribution and use in source forms, with and without modification,
13 * are permitted provided that this entire comment appears intact.
15 * Redistribution in binary form may occur without any restrictions.
16 * Obviously, it would be nice if you gave credit where credit is due
17 * but requiring it would be too onerous.
19 * This software is provided ``AS IS'' without any warranties of any kind.
22 #include "opt_sysvipc.h"
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/sysproto.h>
27 #include <sys/kernel.h>
31 #include <sys/sysent.h>
32 #include <sys/sysctl.h>
33 #include <sys/malloc.h>
36 #include <sys/mplock2.h>
38 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
40 static void msginit (void *);
45 static void msg_freehdr (struct msg *msghdr);
47 /* XXX casting to (sy_call_t *) is bogus, as usual. */
48 static sy_call_t *msgcalls[] = {
49 (sy_call_t *)sys_msgctl, (sy_call_t *)sys_msgget,
50 (sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
54 struct msg *msg_next; /* next msg in the chain */
55 long msg_type; /* type of this message */
56 /* >0 -> type of this message */
57 /* 0 -> free header */
58 u_short msg_ts; /* size of this message */
59 short msg_spot; /* location of start of msg in buffer */
64 #define MSGSSZ 8 /* Each segment must be 2^N long */
67 #define MSGSEG 2048 /* must be less than 32767 */
69 #define MSGMAX (MSGSSZ*MSGSEG)
71 #define MSGMNB 2048 /* max # of bytes in a queue */
81 * Based on the configuration parameters described in an SVR2 (yes, two)
82 * config(1m) man page.
84 * Each message is broken up and stored in segments that are msgssz bytes
85 * long. For efficiency reasons, this should be a power of two. Also,
86 * it doesn't make sense if it is less than 8 or greater than about 256.
87 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
88 * two between 8 and 1024 inclusive (and panic's if it isn't).
90 struct msginfo msginfo = {
91 MSGMAX, /* max chars in a message */
92 MSGMNI, /* # of message queue identifiers */
93 MSGMNB, /* max chars in a queue */
94 MSGTQL, /* max messages in system */
95 MSGSSZ, /* size of a message segment */
96 /* (must be small power of 2 greater than 4) */
97 MSGSEG /* number of message segments */
101 * macros to convert between msqid_ds's and msqid's.
102 * (specific to this implementation)
104 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
105 #define MSQID_IX(id) ((id) & 0xffff)
106 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
109 * The rest of this file is specific to this particular implementation.
113 short next; /* next segment in buffer */
114 /* -1 -> available */
115 /* 0..(MSGSEG-1) -> index of next segment */
118 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
120 static int nfree_msgmaps; /* # of free map entries */
121 static short free_msgmaps; /* head of linked list of free map entries */
122 static struct msg *free_msghdrs;/* list of free msg headers */
123 static char *msgpool; /* MSGMAX byte long msg buffer pool */
124 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
125 static struct msg *msghdrs; /* MSGTQL msg headers */
126 static struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */
133 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
134 msgpool = kmalloc(msginfo.msgmax, M_MSG, M_WAITOK);
135 msgmaps = kmalloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
136 msghdrs = kmalloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
137 msqids = kmalloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK);
140 * msginfo.msgssz should be a power of two for efficiency reasons.
141 * It is also pretty silly if msginfo.msgssz is less than 8
142 * or greater than about 256 so ...
146 while (i < 1024 && i != msginfo.msgssz)
148 if (i != msginfo.msgssz) {
149 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
151 panic("msginfo.msgssz not a small power of 2");
154 if (msginfo.msgseg > 32767) {
155 kprintf("msginfo.msgseg=%d\n", msginfo.msgseg);
156 panic("msginfo.msgseg > 32767");
159 for (i = 0; i < msginfo.msgseg; i++) {
161 msgmaps[i-1].next = i;
162 msgmaps[i].next = -1; /* implies entry is available */
165 nfree_msgmaps = msginfo.msgseg;
167 for (i = 0; i < msginfo.msgtql; i++) {
168 msghdrs[i].msg_type = 0;
170 msghdrs[i-1].msg_next = &msghdrs[i];
171 msghdrs[i].msg_next = NULL;
173 free_msghdrs = &msghdrs[0];
175 for (i = 0; i < msginfo.msgmni; i++) {
176 msqids[i].msg_qbytes = 0; /* implies entry is available */
177 msqids[i].msg_perm.seq = 0; /* reset to a known value */
178 msqids[i].msg_perm.mode = 0;
181 SYSINIT(sysv_msg, SI_SUB_SYSV_MSG, SI_ORDER_FIRST, msginit, NULL)
184 * Entry point for all MSG calls
186 * msgsys_args(int which, int a2, ...) (VARARGS)
191 sys_msgsys(struct msgsys_args *uap)
193 struct thread *td = curthread;
194 unsigned int which = (unsigned int)uap->which;
197 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
200 if (which >= NELEM(msgcalls))
202 bcopy(&uap->a2, &uap->which,
203 sizeof(struct msgsys_args) - offsetof(struct msgsys_args, a2));
205 error = (*msgcalls[which])(uap);
211 msg_freehdr(struct msg *msghdr)
213 while (msghdr->msg_ts > 0) {
215 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
216 panic("msghdr->msg_spot out of range");
217 next = msgmaps[msghdr->msg_spot].next;
218 msgmaps[msghdr->msg_spot].next = free_msgmaps;
219 free_msgmaps = msghdr->msg_spot;
221 msghdr->msg_spot = next;
222 if (msghdr->msg_ts >= msginfo.msgssz)
223 msghdr->msg_ts -= msginfo.msgssz;
227 if (msghdr->msg_spot != -1)
228 panic("msghdr->msg_spot != -1");
229 msghdr->msg_next = free_msghdrs;
230 free_msghdrs = msghdr;
237 sys_msgctl(struct msgctl_args *uap)
239 struct thread *td = curthread;
240 struct proc *p = td->td_proc;
241 int msqid = uap->msqid;
243 struct msqid_ds *user_msqptr = uap->buf;
245 struct msqid_ds msqbuf;
246 struct msqid_ds *msqptr;
249 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
252 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
256 msqid = IPCID_TO_IX(msqid);
258 if (msqid < 0 || msqid >= msginfo.msgmni) {
260 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
267 msqptr = &msqids[msqid];
269 if (msqptr->msg_qbytes == 0) {
271 kprintf("no such msqid\n");
276 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
278 kprintf("wrong sequence number\n");
290 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)) != 0)
292 /* Free the message headers */
293 msghdr = msqptr->msg_first;
294 while (msghdr != NULL) {
295 struct msg *msghdr_tmp;
297 /* Free the segments of each message */
298 msqptr->msg_cbytes -= msghdr->msg_ts;
301 msghdr = msghdr->msg_next;
302 msg_freehdr(msghdr_tmp);
305 if (msqptr->msg_cbytes != 0)
306 panic("msg_cbytes is screwed up");
307 if (msqptr->msg_qnum != 0)
308 panic("msg_qnum is screwed up");
310 msqptr->msg_qbytes = 0; /* Mark it as free */
312 wakeup((caddr_t)msqptr);
318 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)) != 0)
320 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
322 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
323 eval = priv_check(td, PRIV_ROOT);
327 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
329 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
332 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
334 if (msqbuf.msg_qbytes == 0) {
336 kprintf("can't reduce msg_qbytes to 0\n");
338 eval = EINVAL; /* non-standard errno! */
341 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
342 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
343 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
344 (msqbuf.msg_perm.mode & 0777);
345 msqptr->msg_qbytes = msqbuf.msg_qbytes;
346 msqptr->msg_ctime = time_second;
350 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
352 kprintf("requester doesn't have read access\n");
357 eval = copyout(msqptr, user_msqptr, sizeof(struct msqid_ds));
362 kprintf("invalid command %d\n", cmd);
370 uap->sysmsg_result = rval;
378 sys_msgget(struct msgget_args *uap)
380 struct thread *td = curthread;
383 int msgflg = uap->msgflg;
384 struct ucred *cred = td->td_ucred;
385 struct msqid_ds *msqptr = NULL;
388 kprintf("msgget(0x%x, 0%o)\n", key, msgflg);
390 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
396 if (key != IPC_PRIVATE) {
397 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
398 msqptr = &msqids[msqid];
399 if (msqptr->msg_qbytes != 0 &&
400 msqptr->msg_perm.key == key)
403 if (msqid < msginfo.msgmni) {
405 kprintf("found public key\n");
407 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
409 kprintf("not exclusive\n");
414 if ((eval = ipcperm(td->td_proc, &msqptr->msg_perm, msgflg & 0700 ))) {
416 kprintf("requester doesn't have 0%o access\n",
426 kprintf("need to allocate the msqid_ds\n");
428 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
429 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
431 * Look for an unallocated and unlocked msqid_ds.
432 * msqid_ds's can be locked by msgsnd or msgrcv while
433 * they are copying the message in/out. We can't
434 * re-use the entry until they release it.
436 msqptr = &msqids[msqid];
437 if (msqptr->msg_qbytes == 0 &&
438 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
441 if (msqid == msginfo.msgmni) {
443 kprintf("no more msqid_ds's available\n");
449 kprintf("msqid %d is available\n", msqid);
451 msqptr->msg_perm.key = key;
452 msqptr->msg_perm.cuid = cred->cr_uid;
453 msqptr->msg_perm.uid = cred->cr_uid;
454 msqptr->msg_perm.cgid = cred->cr_gid;
455 msqptr->msg_perm.gid = cred->cr_gid;
456 msqptr->msg_perm.mode = (msgflg & 0777);
457 /* Make sure that the returned msqid is unique */
458 msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff;
459 msqptr->msg_first = NULL;
460 msqptr->msg_last = NULL;
461 msqptr->msg_cbytes = 0;
462 msqptr->msg_qnum = 0;
463 msqptr->msg_qbytes = msginfo.msgmnb;
464 msqptr->msg_lspid = 0;
465 msqptr->msg_lrpid = 0;
466 msqptr->msg_stime = 0;
467 msqptr->msg_rtime = 0;
468 msqptr->msg_ctime = time_second;
471 kprintf("didn't find it and wasn't asked to create it\n");
478 /* Construct the unique msqid */
480 uap->sysmsg_result = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
488 sys_msgsnd(struct msgsnd_args *uap)
490 struct thread *td = curthread;
491 int msqid = uap->msqid;
492 void *user_msgp = uap->msgp;
493 size_t msgsz = uap->msgsz;
494 int msgflg = uap->msgflg;
495 int segs_needed, eval;
496 struct msqid_ds *msqptr;
501 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
505 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
509 msqid = IPCID_TO_IX(msqid);
511 if (msqid < 0 || msqid >= msginfo.msgmni) {
513 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
520 msqptr = &msqids[msqid];
521 if (msqptr->msg_qbytes == 0) {
523 kprintf("no such message queue id\n");
528 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
530 kprintf("wrong sequence number\n");
536 if ((eval = ipcperm(td->td_proc, &msqptr->msg_perm, IPC_W))) {
538 kprintf("requester doesn't have write access\n");
544 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
546 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
550 int need_more_resources = 0;
554 * (inside this loop in case msg_qbytes changes while we sleep)
557 if (msgsz > msqptr->msg_qbytes) {
559 kprintf("msgsz > msqptr->msg_qbytes\n");
565 if (msqptr->msg_perm.mode & MSG_LOCKED) {
567 kprintf("msqid is locked\n");
569 need_more_resources = 1;
571 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
573 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
575 need_more_resources = 1;
577 if (segs_needed > nfree_msgmaps) {
579 kprintf("segs_needed > nfree_msgmaps\n");
581 need_more_resources = 1;
583 if (free_msghdrs == NULL) {
585 kprintf("no more msghdrs\n");
587 need_more_resources = 1;
590 if (need_more_resources) {
593 if ((msgflg & IPC_NOWAIT) != 0) {
595 kprintf("need more resources but caller doesn't want to wait\n");
601 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
603 kprintf("we don't own the msqid_ds\n");
607 /* Force later arrivals to wait for our
610 kprintf("we own the msqid_ds\n");
612 msqptr->msg_perm.mode |= MSG_LOCKED;
616 kprintf("goodnight\n");
618 eval = tsleep((caddr_t)msqptr, PCATCH, "msgwait", 0);
620 kprintf("good morning, eval=%d\n", eval);
623 msqptr->msg_perm.mode &= ~MSG_LOCKED;
626 kprintf("msgsnd: interrupted system call\n");
633 * Make sure that the msq queue still exists
636 if (msqptr->msg_qbytes == 0) {
638 kprintf("msqid deleted\n");
646 kprintf("got all the resources that we need\n");
653 * We have the resources that we need.
657 if (msqptr->msg_perm.mode & MSG_LOCKED)
658 panic("msg_perm.mode & MSG_LOCKED");
659 if (segs_needed > nfree_msgmaps)
660 panic("segs_needed > nfree_msgmaps");
661 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
662 panic("msgsz + msg_cbytes > msg_qbytes");
663 if (free_msghdrs == NULL)
664 panic("no more msghdrs");
667 * Re-lock the msqid_ds in case we page-fault when copying in the
671 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
672 panic("msqid_ds is already locked");
673 msqptr->msg_perm.mode |= MSG_LOCKED;
676 * Allocate a message header
679 msghdr = free_msghdrs;
680 free_msghdrs = msghdr->msg_next;
681 msghdr->msg_spot = -1;
682 msghdr->msg_ts = msgsz;
685 * Allocate space for the message
688 while (segs_needed > 0) {
689 if (nfree_msgmaps <= 0)
690 panic("not enough msgmaps");
691 if (free_msgmaps == -1)
692 panic("nil free_msgmaps");
695 panic("next too low #1");
696 if (next >= msginfo.msgseg)
697 panic("next out of range #1");
699 kprintf("allocating segment %d to message\n", next);
701 free_msgmaps = msgmaps[next].next;
703 msgmaps[next].next = msghdr->msg_spot;
704 msghdr->msg_spot = next;
709 * Copy in the message type
712 if ((eval = copyin(user_msgp, &msghdr->msg_type,
713 sizeof(msghdr->msg_type))) != 0) {
715 kprintf("error %d copying the message type\n", eval);
718 msqptr->msg_perm.mode &= ~MSG_LOCKED;
719 wakeup((caddr_t)msqptr);
722 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
725 * Validate the message type
728 if (msghdr->msg_type < 1) {
730 msqptr->msg_perm.mode &= ~MSG_LOCKED;
731 wakeup((caddr_t)msqptr);
733 kprintf("mtype (%d) < 1\n", msghdr->msg_type);
740 * Copy in the message body
743 next = msghdr->msg_spot;
746 if (msgsz > msginfo.msgssz)
747 tlen = msginfo.msgssz;
751 panic("next too low #2");
752 if (next >= msginfo.msgseg)
753 panic("next out of range #2");
754 if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
757 kprintf("error %d copying in message segment\n", eval);
760 msqptr->msg_perm.mode &= ~MSG_LOCKED;
761 wakeup((caddr_t)msqptr);
765 user_msgp = (char *)user_msgp + tlen;
766 next = msgmaps[next].next;
769 panic("didn't use all the msg segments");
772 * We've got the message. Unlock the msqid_ds.
775 msqptr->msg_perm.mode &= ~MSG_LOCKED;
778 * Make sure that the msqid_ds is still allocated.
781 if (msqptr->msg_qbytes == 0) {
783 wakeup((caddr_t)msqptr);
789 * Put the message into the queue
792 if (msqptr->msg_first == NULL) {
793 msqptr->msg_first = msghdr;
794 msqptr->msg_last = msghdr;
796 msqptr->msg_last->msg_next = msghdr;
797 msqptr->msg_last = msghdr;
799 msqptr->msg_last->msg_next = NULL;
801 msqptr->msg_cbytes += msghdr->msg_ts;
803 msqptr->msg_lspid = td->td_proc->p_pid;
804 msqptr->msg_stime = time_second;
806 wakeup((caddr_t)msqptr);
811 uap->sysmsg_result = 0;
819 sys_msgrcv(struct msgrcv_args *uap)
821 struct thread *td = curthread;
822 int msqid = uap->msqid;
823 void *user_msgp = uap->msgp;
824 size_t msgsz = uap->msgsz;
825 long msgtyp = uap->msgtyp;
826 int msgflg = uap->msgflg;
828 struct msqid_ds *msqptr;
834 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
835 msgsz, msgtyp, msgflg);
838 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
842 msqid = IPCID_TO_IX(msqid);
844 if (msqid < 0 || msqid >= msginfo.msgmni) {
846 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
853 msqptr = &msqids[msqid];
854 if (msqptr->msg_qbytes == 0) {
856 kprintf("no such message queue id\n");
861 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
863 kprintf("wrong sequence number\n");
869 if ((eval = ipcperm(td->td_proc, &msqptr->msg_perm, IPC_R))) {
871 kprintf("requester doesn't have read access\n");
877 while (msghdr == NULL) {
879 msghdr = msqptr->msg_first;
880 if (msghdr != NULL) {
881 if (msgsz < msghdr->msg_ts &&
882 (msgflg & MSG_NOERROR) == 0) {
884 kprintf("first message on the queue is too big (want %d, got %d)\n",
885 msgsz, msghdr->msg_ts);
890 if (msqptr->msg_first == msqptr->msg_last) {
891 msqptr->msg_first = NULL;
892 msqptr->msg_last = NULL;
894 msqptr->msg_first = msghdr->msg_next;
895 if (msqptr->msg_first == NULL)
896 panic("msg_first/last screwed up #1");
900 struct msg *previous;
904 prev = &(msqptr->msg_first);
905 while ((msghdr = *prev) != NULL) {
907 * Is this message's type an exact match or is
908 * this message's type less than or equal to
909 * the absolute value of a negative msgtyp?
910 * Note that the second half of this test can
911 * NEVER be true if msgtyp is positive since
912 * msg_type is always positive!
915 if (msgtyp == msghdr->msg_type ||
916 msghdr->msg_type <= -msgtyp) {
918 kprintf("found message type %d, requested %d\n",
919 msghdr->msg_type, msgtyp);
921 if (msgsz < msghdr->msg_ts &&
922 (msgflg & MSG_NOERROR) == 0) {
924 kprintf("requested message on the queue is too big (want %d, got %d)\n",
925 msgsz, msghdr->msg_ts);
930 *prev = msghdr->msg_next;
931 if (msghdr == msqptr->msg_last) {
932 if (previous == NULL) {
935 panic("msg_first/last screwed up #2");
943 panic("msg_first/last screwed up #3");
951 prev = &(msghdr->msg_next);
956 * We've either extracted the msghdr for the appropriate
957 * message or there isn't one.
958 * If there is one then bail out of this loop.
965 * Hmph! No message found. Does the user want to wait?
968 if ((msgflg & IPC_NOWAIT) != 0) {
970 kprintf("no appropriate message found (msgtyp=%d)\n",
973 /* The SVID says to return ENOMSG. */
977 /* Unfortunately, BSD doesn't define that code yet! */
984 * Wait for something to happen
988 kprintf("msgrcv: goodnight\n");
990 eval = tsleep((caddr_t)msqptr, PCATCH, "msgwait", 0);
992 kprintf("msgrcv: good morning (eval=%d)\n", eval);
997 kprintf("msgsnd: interrupted system call\n");
1004 * Make sure that the msq queue still exists
1007 if (msqptr->msg_qbytes == 0 ||
1008 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1010 kprintf("msqid deleted\n");
1018 * Return the message to the user.
1020 * First, do the bookkeeping (before we risk being interrupted).
1023 msqptr->msg_cbytes -= msghdr->msg_ts;
1025 msqptr->msg_lrpid = td->td_proc->p_pid;
1026 msqptr->msg_rtime = time_second;
1029 * Make msgsz the actual amount that we'll be returning.
1030 * Note that this effectively truncates the message if it is too long
1031 * (since msgsz is never increased).
1035 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1038 if (msgsz > msghdr->msg_ts)
1039 msgsz = msghdr->msg_ts;
1042 * Return the type to the user.
1045 eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1046 sizeof(msghdr->msg_type));
1049 kprintf("error (%d) copying out message type\n", eval);
1051 msg_freehdr(msghdr);
1052 wakeup((caddr_t)msqptr);
1055 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1058 * Return the segments to the user
1061 next = msghdr->msg_spot;
1062 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1065 if (msgsz - len > msginfo.msgssz)
1066 tlen = msginfo.msgssz;
1070 panic("next too low #3");
1071 if (next >= msginfo.msgseg)
1072 panic("next out of range #3");
1073 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1077 kprintf("error (%d) copying out message segment\n",
1080 msg_freehdr(msghdr);
1081 wakeup((caddr_t)msqptr);
1084 user_msgp = (char *)user_msgp + tlen;
1085 next = msgmaps[next].next;
1089 * Done, return the actual number of bytes copied out.
1092 msg_freehdr(msghdr);
1093 wakeup((caddr_t)msqptr);
1098 uap->sysmsg_result = msgsz;
1103 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1106 return (SYSCTL_OUT(req, msqids,
1107 sizeof(struct msqid_ds) * msginfo.msgmni));
1110 TUNABLE_INT("kern.ipc.msgseg", &msginfo.msgseg);
1111 TUNABLE_INT("kern.ipc.msgssz", &msginfo.msgssz);
1112 TUNABLE_INT("kern.ipc.msgmni", &msginfo.msgmni);
1114 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1115 "Max characters in message");
1116 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RD, &msginfo.msgmni, 0,
1117 "Max message queue identifiers");
1118 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0,
1119 "Max characters in message queue");
1120 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0,
1121 "Max SVID messages in system");
1122 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RD, &msginfo.msgssz, 0,
1123 "Power-of-two size of a message segment");
1124 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RD, &msginfo.msgseg, 0,
1125 "Number of message segments");
1126 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1127 NULL, 0, sysctl_msqids, "", "Message queue IDs");