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>
30 #include <sys/syscall.h>
31 #include <sys/sysent.h>
32 #include <sys/sysctl.h>
33 #include <sys/malloc.h>
36 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
38 static void msginit __P((void));
39 static int msgunload __P((void));
40 static int sysvmsg_modload __P((struct module *, int, void *));
45 static void msg_freehdr __P((struct msg *msghdr));
47 /* XXX casting to (sy_call_t *) is bogus, as usual. */
48 static sy_call_t *msgcalls[] = {
49 (sy_call_t *)msgctl, (sy_call_t *)msgget,
50 (sy_call_t *)msgsnd, (sy_call_t *)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 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
135 panic("msgpool is NULL");
136 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
138 panic("msgmaps is NULL");
139 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
141 panic("msghdrs is NULL");
142 msqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK);
144 panic("msqids is NULL");
147 * msginfo.msgssz should be a power of two for efficiency reasons.
148 * It is also pretty silly if msginfo.msgssz is less than 8
149 * or greater than about 256 so ...
153 while (i < 1024 && i != msginfo.msgssz)
155 if (i != msginfo.msgssz) {
156 printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
158 panic("msginfo.msgssz not a small power of 2");
161 if (msginfo.msgseg > 32767) {
162 printf("msginfo.msgseg=%d\n", msginfo.msgseg);
163 panic("msginfo.msgseg > 32767");
167 panic("msgmaps is NULL");
169 for (i = 0; i < msginfo.msgseg; i++) {
171 msgmaps[i-1].next = i;
172 msgmaps[i].next = -1; /* implies entry is available */
175 nfree_msgmaps = msginfo.msgseg;
178 panic("msghdrs is NULL");
180 for (i = 0; i < msginfo.msgtql; i++) {
181 msghdrs[i].msg_type = 0;
183 msghdrs[i-1].msg_next = &msghdrs[i];
184 msghdrs[i].msg_next = NULL;
186 free_msghdrs = &msghdrs[0];
189 panic("msqids is NULL");
191 for (i = 0; i < msginfo.msgmni; i++) {
192 msqids[i].msg_qbytes = 0; /* implies entry is available */
193 msqids[i].msg_perm.seq = 0; /* reset to a known value */
194 msqids[i].msg_perm.mode = 0;
201 struct msqid_ds *msqptr;
204 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
206 * Look for an unallocated and unlocked msqid_ds.
207 * msqid_ds's can be locked by msgsnd or msgrcv while
208 * they are copying the message in/out. We can't
209 * re-use the entry until they release it.
211 msqptr = &msqids[msqid];
212 if (msqptr->msg_qbytes != 0 ||
213 (msqptr->msg_perm.mode & MSG_LOCKED) != 0)
216 if (msqid != msginfo.msgmni)
219 free(msgpool, M_MSG);
220 free(msgmaps, M_MSG);
221 free(msghdrs, M_MSG);
228 sysvmsg_modload(struct module *module, int cmd, void *arg)
248 static moduledata_t sysvmsg_moduledata = {
254 SYSCALL_MODULE_HELPER(msgsys, 6);
255 SYSCALL_MODULE_HELPER(msgctl, 3);
256 SYSCALL_MODULE_HELPER(msgget, 2);
257 SYSCALL_MODULE_HELPER(msgsnd, 4);
258 SYSCALL_MODULE_HELPER(msgrcv, 5);
260 DECLARE_MODULE(sysvmsg_mod, sysvmsg_moduledata,
261 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
264 * Entry point for all MSG calls
269 /* XXX actually varargs. */
270 struct msgsys_args /* {
280 if (!jail_sysvipc_allowed && p->p_prison != NULL)
283 if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
285 return ((*msgcalls[uap->which])(p, &uap->a2));
292 while (msghdr->msg_ts > 0) {
294 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
295 panic("msghdr->msg_spot out of range");
296 next = msgmaps[msghdr->msg_spot].next;
297 msgmaps[msghdr->msg_spot].next = free_msgmaps;
298 free_msgmaps = msghdr->msg_spot;
300 msghdr->msg_spot = next;
301 if (msghdr->msg_ts >= msginfo.msgssz)
302 msghdr->msg_ts -= msginfo.msgssz;
306 if (msghdr->msg_spot != -1)
307 panic("msghdr->msg_spot != -1");
308 msghdr->msg_next = free_msghdrs;
309 free_msghdrs = msghdr;
312 #ifndef _SYS_SYSPROTO_H_
316 struct msqid_ds *buf;
323 register struct msgctl_args *uap;
325 int msqid = uap->msqid;
327 struct msqid_ds *user_msqptr = uap->buf;
329 struct msqid_ds msqbuf;
330 register struct msqid_ds *msqptr;
333 printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
336 if (!jail_sysvipc_allowed && p->p_prison != NULL)
339 msqid = IPCID_TO_IX(msqid);
341 if (msqid < 0 || msqid >= msginfo.msgmni) {
343 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
349 msqptr = &msqids[msqid];
351 if (msqptr->msg_qbytes == 0) {
353 printf("no such msqid\n");
357 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
359 printf("wrong sequence number\n");
372 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
374 /* Free the message headers */
375 msghdr = msqptr->msg_first;
376 while (msghdr != NULL) {
377 struct msg *msghdr_tmp;
379 /* Free the segments of each message */
380 msqptr->msg_cbytes -= msghdr->msg_ts;
383 msghdr = msghdr->msg_next;
384 msg_freehdr(msghdr_tmp);
387 if (msqptr->msg_cbytes != 0)
388 panic("msg_cbytes is screwed up");
389 if (msqptr->msg_qnum != 0)
390 panic("msg_qnum is screwed up");
392 msqptr->msg_qbytes = 0; /* Mark it as free */
394 wakeup((caddr_t)msqptr);
400 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
402 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
404 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
409 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
411 printf("can't increase msg_qbytes beyond %d (truncating)\n",
414 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
416 if (msqbuf.msg_qbytes == 0) {
418 printf("can't reduce msg_qbytes to 0\n");
420 return(EINVAL); /* non-standard errno! */
422 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
423 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
424 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
425 (msqbuf.msg_perm.mode & 0777);
426 msqptr->msg_qbytes = msqbuf.msg_qbytes;
427 msqptr->msg_ctime = time_second;
431 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
433 printf("requester doesn't have read access\n");
437 eval = copyout((caddr_t)msqptr, user_msqptr,
438 sizeof(struct msqid_ds));
443 printf("invalid command %d\n", cmd);
449 p->p_retval[0] = rval;
453 #ifndef _SYS_SYSPROTO_H_
463 register struct msgget_args *uap;
467 int msgflg = uap->msgflg;
468 struct ucred *cred = p->p_ucred;
469 register struct msqid_ds *msqptr = NULL;
472 printf("msgget(0x%x, 0%o)\n", key, msgflg);
475 if (!jail_sysvipc_allowed && p->p_prison != NULL)
478 if (key != IPC_PRIVATE) {
479 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
480 msqptr = &msqids[msqid];
481 if (msqptr->msg_qbytes != 0 &&
482 msqptr->msg_perm.key == key)
485 if (msqid < msginfo.msgmni) {
487 printf("found public key\n");
489 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
491 printf("not exclusive\n");
495 if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) {
497 printf("requester doesn't have 0%o access\n",
507 printf("need to allocate the msqid_ds\n");
509 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
510 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
512 * Look for an unallocated and unlocked msqid_ds.
513 * msqid_ds's can be locked by msgsnd or msgrcv while
514 * they are copying the message in/out. We can't
515 * re-use the entry until they release it.
517 msqptr = &msqids[msqid];
518 if (msqptr->msg_qbytes == 0 &&
519 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
522 if (msqid == msginfo.msgmni) {
524 printf("no more msqid_ds's available\n");
529 printf("msqid %d is available\n", msqid);
531 msqptr->msg_perm.key = key;
532 msqptr->msg_perm.cuid = cred->cr_uid;
533 msqptr->msg_perm.uid = cred->cr_uid;
534 msqptr->msg_perm.cgid = cred->cr_gid;
535 msqptr->msg_perm.gid = cred->cr_gid;
536 msqptr->msg_perm.mode = (msgflg & 0777);
537 /* Make sure that the returned msqid is unique */
538 msqptr->msg_perm.seq++;
539 msqptr->msg_first = NULL;
540 msqptr->msg_last = NULL;
541 msqptr->msg_cbytes = 0;
542 msqptr->msg_qnum = 0;
543 msqptr->msg_qbytes = msginfo.msgmnb;
544 msqptr->msg_lspid = 0;
545 msqptr->msg_lrpid = 0;
546 msqptr->msg_stime = 0;
547 msqptr->msg_rtime = 0;
548 msqptr->msg_ctime = time_second;
551 printf("didn't find it and wasn't asked to create it\n");
557 /* Construct the unique msqid */
558 p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
562 #ifndef _SYS_SYSPROTO_H_
574 register struct msgsnd_args *uap;
576 int msqid = uap->msqid;
577 void *user_msgp = uap->msgp;
578 size_t msgsz = uap->msgsz;
579 int msgflg = uap->msgflg;
580 int segs_needed, eval;
581 register struct msqid_ds *msqptr;
582 register struct msg *msghdr;
586 printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
590 if (!jail_sysvipc_allowed && p->p_prison != NULL)
593 msqid = IPCID_TO_IX(msqid);
595 if (msqid < 0 || msqid >= msginfo.msgmni) {
597 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
603 msqptr = &msqids[msqid];
604 if (msqptr->msg_qbytes == 0) {
606 printf("no such message queue id\n");
610 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
612 printf("wrong sequence number\n");
617 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) {
619 printf("requester doesn't have write access\n");
624 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
626 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
630 int need_more_resources = 0;
634 * (inside this loop in case msg_qbytes changes while we sleep)
637 if (msgsz > msqptr->msg_qbytes) {
639 printf("msgsz > msqptr->msg_qbytes\n");
644 if (msqptr->msg_perm.mode & MSG_LOCKED) {
646 printf("msqid is locked\n");
648 need_more_resources = 1;
650 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
652 printf("msgsz + msg_cbytes > msg_qbytes\n");
654 need_more_resources = 1;
656 if (segs_needed > nfree_msgmaps) {
658 printf("segs_needed > nfree_msgmaps\n");
660 need_more_resources = 1;
662 if (free_msghdrs == NULL) {
664 printf("no more msghdrs\n");
666 need_more_resources = 1;
669 if (need_more_resources) {
672 if ((msgflg & IPC_NOWAIT) != 0) {
674 printf("need more resources but caller doesn't want to wait\n");
679 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
681 printf("we don't own the msqid_ds\n");
685 /* Force later arrivals to wait for our
688 printf("we own the msqid_ds\n");
690 msqptr->msg_perm.mode |= MSG_LOCKED;
694 printf("goodnight\n");
696 eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
699 printf("good morning, eval=%d\n", eval);
702 msqptr->msg_perm.mode &= ~MSG_LOCKED;
705 printf("msgsnd: interrupted system call\n");
711 * Make sure that the msq queue still exists
714 if (msqptr->msg_qbytes == 0) {
716 printf("msqid deleted\n");
723 printf("got all the resources that we need\n");
730 * We have the resources that we need.
734 if (msqptr->msg_perm.mode & MSG_LOCKED)
735 panic("msg_perm.mode & MSG_LOCKED");
736 if (segs_needed > nfree_msgmaps)
737 panic("segs_needed > nfree_msgmaps");
738 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
739 panic("msgsz + msg_cbytes > msg_qbytes");
740 if (free_msghdrs == NULL)
741 panic("no more msghdrs");
744 * Re-lock the msqid_ds in case we page-fault when copying in the
748 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
749 panic("msqid_ds is already locked");
750 msqptr->msg_perm.mode |= MSG_LOCKED;
753 * Allocate a message header
756 msghdr = free_msghdrs;
757 free_msghdrs = msghdr->msg_next;
758 msghdr->msg_spot = -1;
759 msghdr->msg_ts = msgsz;
762 * Allocate space for the message
765 while (segs_needed > 0) {
766 if (nfree_msgmaps <= 0)
767 panic("not enough msgmaps");
768 if (free_msgmaps == -1)
769 panic("nil free_msgmaps");
772 panic("next too low #1");
773 if (next >= msginfo.msgseg)
774 panic("next out of range #1");
776 printf("allocating segment %d to message\n", next);
778 free_msgmaps = msgmaps[next].next;
780 msgmaps[next].next = msghdr->msg_spot;
781 msghdr->msg_spot = next;
786 * Copy in the message type
789 if ((eval = copyin(user_msgp, &msghdr->msg_type,
790 sizeof(msghdr->msg_type))) != 0) {
792 printf("error %d copying the message type\n", eval);
795 msqptr->msg_perm.mode &= ~MSG_LOCKED;
796 wakeup((caddr_t)msqptr);
799 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
802 * Validate the message type
805 if (msghdr->msg_type < 1) {
807 msqptr->msg_perm.mode &= ~MSG_LOCKED;
808 wakeup((caddr_t)msqptr);
810 printf("mtype (%d) < 1\n", msghdr->msg_type);
816 * Copy in the message body
819 next = msghdr->msg_spot;
822 if (msgsz > msginfo.msgssz)
823 tlen = msginfo.msgssz;
827 panic("next too low #2");
828 if (next >= msginfo.msgseg)
829 panic("next out of range #2");
830 if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
833 printf("error %d copying in message segment\n", eval);
836 msqptr->msg_perm.mode &= ~MSG_LOCKED;
837 wakeup((caddr_t)msqptr);
841 user_msgp = (char *)user_msgp + tlen;
842 next = msgmaps[next].next;
845 panic("didn't use all the msg segments");
848 * We've got the message. Unlock the msqid_ds.
851 msqptr->msg_perm.mode &= ~MSG_LOCKED;
854 * Make sure that the msqid_ds is still allocated.
857 if (msqptr->msg_qbytes == 0) {
859 wakeup((caddr_t)msqptr);
864 * Put the message into the queue
867 if (msqptr->msg_first == NULL) {
868 msqptr->msg_first = msghdr;
869 msqptr->msg_last = msghdr;
871 msqptr->msg_last->msg_next = msghdr;
872 msqptr->msg_last = msghdr;
874 msqptr->msg_last->msg_next = NULL;
876 msqptr->msg_cbytes += msghdr->msg_ts;
878 msqptr->msg_lspid = p->p_pid;
879 msqptr->msg_stime = time_second;
881 wakeup((caddr_t)msqptr);
886 #ifndef _SYS_SYSPROTO_H_
899 register struct msgrcv_args *uap;
901 int msqid = uap->msqid;
902 void *user_msgp = uap->msgp;
903 size_t msgsz = uap->msgsz;
904 long msgtyp = uap->msgtyp;
905 int msgflg = uap->msgflg;
907 register struct msqid_ds *msqptr;
908 register struct msg *msghdr;
913 printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
914 msgsz, msgtyp, msgflg);
917 if (!jail_sysvipc_allowed && p->p_prison != NULL)
920 msqid = IPCID_TO_IX(msqid);
922 if (msqid < 0 || msqid >= msginfo.msgmni) {
924 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
930 msqptr = &msqids[msqid];
931 if (msqptr->msg_qbytes == 0) {
933 printf("no such message queue id\n");
937 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
939 printf("wrong sequence number\n");
944 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
946 printf("requester doesn't have read access\n");
952 while (msghdr == NULL) {
954 msghdr = msqptr->msg_first;
955 if (msghdr != NULL) {
956 if (msgsz < msghdr->msg_ts &&
957 (msgflg & MSG_NOERROR) == 0) {
959 printf("first message on the queue is too big (want %d, got %d)\n",
960 msgsz, msghdr->msg_ts);
964 if (msqptr->msg_first == msqptr->msg_last) {
965 msqptr->msg_first = NULL;
966 msqptr->msg_last = NULL;
968 msqptr->msg_first = msghdr->msg_next;
969 if (msqptr->msg_first == NULL)
970 panic("msg_first/last screwed up #1");
974 struct msg *previous;
978 prev = &(msqptr->msg_first);
979 while ((msghdr = *prev) != NULL) {
981 * Is this message's type an exact match or is
982 * this message's type less than or equal to
983 * the absolute value of a negative msgtyp?
984 * Note that the second half of this test can
985 * NEVER be true if msgtyp is positive since
986 * msg_type is always positive!
989 if (msgtyp == msghdr->msg_type ||
990 msghdr->msg_type <= -msgtyp) {
992 printf("found message type %d, requested %d\n",
993 msghdr->msg_type, msgtyp);
995 if (msgsz < msghdr->msg_ts &&
996 (msgflg & MSG_NOERROR) == 0) {
998 printf("requested message on the queue is too big (want %d, got %d)\n",
999 msgsz, msghdr->msg_ts);
1003 *prev = msghdr->msg_next;
1004 if (msghdr == msqptr->msg_last) {
1005 if (previous == NULL) {
1008 panic("msg_first/last screwed up #2");
1016 panic("msg_first/last screwed up #3");
1024 prev = &(msghdr->msg_next);
1029 * We've either extracted the msghdr for the appropriate
1030 * message or there isn't one.
1031 * If there is one then bail out of this loop.
1038 * Hmph! No message found. Does the user want to wait?
1041 if ((msgflg & IPC_NOWAIT) != 0) {
1043 printf("no appropriate message found (msgtyp=%d)\n",
1046 /* The SVID says to return ENOMSG. */
1050 /* Unfortunately, BSD doesn't define that code yet! */
1056 * Wait for something to happen
1060 printf("msgrcv: goodnight\n");
1062 eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
1065 printf("msgrcv: good morning (eval=%d)\n", eval);
1070 printf("msgsnd: interrupted system call\n");
1076 * Make sure that the msq queue still exists
1079 if (msqptr->msg_qbytes == 0 ||
1080 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1082 printf("msqid deleted\n");
1089 * Return the message to the user.
1091 * First, do the bookkeeping (before we risk being interrupted).
1094 msqptr->msg_cbytes -= msghdr->msg_ts;
1096 msqptr->msg_lrpid = p->p_pid;
1097 msqptr->msg_rtime = time_second;
1100 * Make msgsz the actual amount that we'll be returning.
1101 * Note that this effectively truncates the message if it is too long
1102 * (since msgsz is never increased).
1106 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1109 if (msgsz > msghdr->msg_ts)
1110 msgsz = msghdr->msg_ts;
1113 * Return the type to the user.
1116 eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1117 sizeof(msghdr->msg_type));
1120 printf("error (%d) copying out message type\n", eval);
1122 msg_freehdr(msghdr);
1123 wakeup((caddr_t)msqptr);
1126 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1129 * Return the segments to the user
1132 next = msghdr->msg_spot;
1133 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1136 if (msgsz - len > msginfo.msgssz)
1137 tlen = msginfo.msgssz;
1141 panic("next too low #3");
1142 if (next >= msginfo.msgseg)
1143 panic("next out of range #3");
1144 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1148 printf("error (%d) copying out message segment\n",
1151 msg_freehdr(msghdr);
1152 wakeup((caddr_t)msqptr);
1155 user_msgp = (char *)user_msgp + tlen;
1156 next = msgmaps[next].next;
1160 * Done, return the actual number of bytes copied out.
1163 msg_freehdr(msghdr);
1164 wakeup((caddr_t)msqptr);
1165 p->p_retval[0] = msgsz;