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(msgctl, 3);
255 SYSCALL_MODULE_HELPER(msgget, 2);
256 SYSCALL_MODULE_HELPER(msgsnd, 4);
257 SYSCALL_MODULE_HELPER(msgrcv, 5);
259 DECLARE_MODULE(sysvmsg_mod, sysvmsg_moduledata,
260 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
263 * Entry point for all MSG calls
268 /* XXX actually varargs. */
269 struct msgsys_args /* {
279 if (!jail_sysvipc_allowed && p->p_prison != NULL)
282 if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
284 return ((*msgcalls[uap->which])(p, &uap->a2));
291 while (msghdr->msg_ts > 0) {
293 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
294 panic("msghdr->msg_spot out of range");
295 next = msgmaps[msghdr->msg_spot].next;
296 msgmaps[msghdr->msg_spot].next = free_msgmaps;
297 free_msgmaps = msghdr->msg_spot;
299 msghdr->msg_spot = next;
300 if (msghdr->msg_ts >= msginfo.msgssz)
301 msghdr->msg_ts -= msginfo.msgssz;
305 if (msghdr->msg_spot != -1)
306 panic("msghdr->msg_spot != -1");
307 msghdr->msg_next = free_msghdrs;
308 free_msghdrs = msghdr;
311 #ifndef _SYS_SYSPROTO_H_
315 struct msqid_ds *buf;
322 register struct msgctl_args *uap;
324 int msqid = uap->msqid;
326 struct msqid_ds *user_msqptr = uap->buf;
328 struct msqid_ds msqbuf;
329 register struct msqid_ds *msqptr;
332 printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
335 if (!jail_sysvipc_allowed && p->p_prison != NULL)
338 msqid = IPCID_TO_IX(msqid);
340 if (msqid < 0 || msqid >= msginfo.msgmni) {
342 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
348 msqptr = &msqids[msqid];
350 if (msqptr->msg_qbytes == 0) {
352 printf("no such msqid\n");
356 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
358 printf("wrong sequence number\n");
371 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
373 /* Free the message headers */
374 msghdr = msqptr->msg_first;
375 while (msghdr != NULL) {
376 struct msg *msghdr_tmp;
378 /* Free the segments of each message */
379 msqptr->msg_cbytes -= msghdr->msg_ts;
382 msghdr = msghdr->msg_next;
383 msg_freehdr(msghdr_tmp);
386 if (msqptr->msg_cbytes != 0)
387 panic("msg_cbytes is screwed up");
388 if (msqptr->msg_qnum != 0)
389 panic("msg_qnum is screwed up");
391 msqptr->msg_qbytes = 0; /* Mark it as free */
393 wakeup((caddr_t)msqptr);
399 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
401 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
403 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
408 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
410 printf("can't increase msg_qbytes beyond %d (truncating)\n",
413 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
415 if (msqbuf.msg_qbytes == 0) {
417 printf("can't reduce msg_qbytes to 0\n");
419 return(EINVAL); /* non-standard errno! */
421 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
422 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
423 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
424 (msqbuf.msg_perm.mode & 0777);
425 msqptr->msg_qbytes = msqbuf.msg_qbytes;
426 msqptr->msg_ctime = time_second;
430 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
432 printf("requester doesn't have read access\n");
436 eval = copyout((caddr_t)msqptr, user_msqptr,
437 sizeof(struct msqid_ds));
442 printf("invalid command %d\n", cmd);
448 p->p_retval[0] = rval;
452 #ifndef _SYS_SYSPROTO_H_
462 register struct msgget_args *uap;
466 int msgflg = uap->msgflg;
467 struct ucred *cred = p->p_ucred;
468 register struct msqid_ds *msqptr = NULL;
471 printf("msgget(0x%x, 0%o)\n", key, msgflg);
474 if (!jail_sysvipc_allowed && p->p_prison != NULL)
477 if (key != IPC_PRIVATE) {
478 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
479 msqptr = &msqids[msqid];
480 if (msqptr->msg_qbytes != 0 &&
481 msqptr->msg_perm.key == key)
484 if (msqid < msginfo.msgmni) {
486 printf("found public key\n");
488 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
490 printf("not exclusive\n");
494 if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) {
496 printf("requester doesn't have 0%o access\n",
506 printf("need to allocate the msqid_ds\n");
508 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
509 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
511 * Look for an unallocated and unlocked msqid_ds.
512 * msqid_ds's can be locked by msgsnd or msgrcv while
513 * they are copying the message in/out. We can't
514 * re-use the entry until they release it.
516 msqptr = &msqids[msqid];
517 if (msqptr->msg_qbytes == 0 &&
518 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
521 if (msqid == msginfo.msgmni) {
523 printf("no more msqid_ds's available\n");
528 printf("msqid %d is available\n", msqid);
530 msqptr->msg_perm.key = key;
531 msqptr->msg_perm.cuid = cred->cr_uid;
532 msqptr->msg_perm.uid = cred->cr_uid;
533 msqptr->msg_perm.cgid = cred->cr_gid;
534 msqptr->msg_perm.gid = cred->cr_gid;
535 msqptr->msg_perm.mode = (msgflg & 0777);
536 /* Make sure that the returned msqid is unique */
537 msqptr->msg_perm.seq++;
538 msqptr->msg_first = NULL;
539 msqptr->msg_last = NULL;
540 msqptr->msg_cbytes = 0;
541 msqptr->msg_qnum = 0;
542 msqptr->msg_qbytes = msginfo.msgmnb;
543 msqptr->msg_lspid = 0;
544 msqptr->msg_lrpid = 0;
545 msqptr->msg_stime = 0;
546 msqptr->msg_rtime = 0;
547 msqptr->msg_ctime = time_second;
550 printf("didn't find it and wasn't asked to create it\n");
556 /* Construct the unique msqid */
557 p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
561 #ifndef _SYS_SYSPROTO_H_
573 register struct msgsnd_args *uap;
575 int msqid = uap->msqid;
576 void *user_msgp = uap->msgp;
577 size_t msgsz = uap->msgsz;
578 int msgflg = uap->msgflg;
579 int segs_needed, eval;
580 register struct msqid_ds *msqptr;
581 register struct msg *msghdr;
585 printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
589 if (!jail_sysvipc_allowed && p->p_prison != NULL)
592 msqid = IPCID_TO_IX(msqid);
594 if (msqid < 0 || msqid >= msginfo.msgmni) {
596 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
602 msqptr = &msqids[msqid];
603 if (msqptr->msg_qbytes == 0) {
605 printf("no such message queue id\n");
609 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
611 printf("wrong sequence number\n");
616 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) {
618 printf("requester doesn't have write access\n");
623 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
625 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
629 int need_more_resources = 0;
633 * (inside this loop in case msg_qbytes changes while we sleep)
636 if (msgsz > msqptr->msg_qbytes) {
638 printf("msgsz > msqptr->msg_qbytes\n");
643 if (msqptr->msg_perm.mode & MSG_LOCKED) {
645 printf("msqid is locked\n");
647 need_more_resources = 1;
649 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
651 printf("msgsz + msg_cbytes > msg_qbytes\n");
653 need_more_resources = 1;
655 if (segs_needed > nfree_msgmaps) {
657 printf("segs_needed > nfree_msgmaps\n");
659 need_more_resources = 1;
661 if (free_msghdrs == NULL) {
663 printf("no more msghdrs\n");
665 need_more_resources = 1;
668 if (need_more_resources) {
671 if ((msgflg & IPC_NOWAIT) != 0) {
673 printf("need more resources but caller doesn't want to wait\n");
678 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
680 printf("we don't own the msqid_ds\n");
684 /* Force later arrivals to wait for our
687 printf("we own the msqid_ds\n");
689 msqptr->msg_perm.mode |= MSG_LOCKED;
693 printf("goodnight\n");
695 eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
698 printf("good morning, eval=%d\n", eval);
701 msqptr->msg_perm.mode &= ~MSG_LOCKED;
704 printf("msgsnd: interrupted system call\n");
710 * Make sure that the msq queue still exists
713 if (msqptr->msg_qbytes == 0) {
715 printf("msqid deleted\n");
722 printf("got all the resources that we need\n");
729 * We have the resources that we need.
733 if (msqptr->msg_perm.mode & MSG_LOCKED)
734 panic("msg_perm.mode & MSG_LOCKED");
735 if (segs_needed > nfree_msgmaps)
736 panic("segs_needed > nfree_msgmaps");
737 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
738 panic("msgsz + msg_cbytes > msg_qbytes");
739 if (free_msghdrs == NULL)
740 panic("no more msghdrs");
743 * Re-lock the msqid_ds in case we page-fault when copying in the
747 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
748 panic("msqid_ds is already locked");
749 msqptr->msg_perm.mode |= MSG_LOCKED;
752 * Allocate a message header
755 msghdr = free_msghdrs;
756 free_msghdrs = msghdr->msg_next;
757 msghdr->msg_spot = -1;
758 msghdr->msg_ts = msgsz;
761 * Allocate space for the message
764 while (segs_needed > 0) {
765 if (nfree_msgmaps <= 0)
766 panic("not enough msgmaps");
767 if (free_msgmaps == -1)
768 panic("nil free_msgmaps");
771 panic("next too low #1");
772 if (next >= msginfo.msgseg)
773 panic("next out of range #1");
775 printf("allocating segment %d to message\n", next);
777 free_msgmaps = msgmaps[next].next;
779 msgmaps[next].next = msghdr->msg_spot;
780 msghdr->msg_spot = next;
785 * Copy in the message type
788 if ((eval = copyin(user_msgp, &msghdr->msg_type,
789 sizeof(msghdr->msg_type))) != 0) {
791 printf("error %d copying the message type\n", eval);
794 msqptr->msg_perm.mode &= ~MSG_LOCKED;
795 wakeup((caddr_t)msqptr);
798 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
801 * Validate the message type
804 if (msghdr->msg_type < 1) {
806 msqptr->msg_perm.mode &= ~MSG_LOCKED;
807 wakeup((caddr_t)msqptr);
809 printf("mtype (%d) < 1\n", msghdr->msg_type);
815 * Copy in the message body
818 next = msghdr->msg_spot;
821 if (msgsz > msginfo.msgssz)
822 tlen = msginfo.msgssz;
826 panic("next too low #2");
827 if (next >= msginfo.msgseg)
828 panic("next out of range #2");
829 if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
832 printf("error %d copying in message segment\n", eval);
835 msqptr->msg_perm.mode &= ~MSG_LOCKED;
836 wakeup((caddr_t)msqptr);
840 user_msgp = (char *)user_msgp + tlen;
841 next = msgmaps[next].next;
844 panic("didn't use all the msg segments");
847 * We've got the message. Unlock the msqid_ds.
850 msqptr->msg_perm.mode &= ~MSG_LOCKED;
853 * Make sure that the msqid_ds is still allocated.
856 if (msqptr->msg_qbytes == 0) {
858 wakeup((caddr_t)msqptr);
863 * Put the message into the queue
866 if (msqptr->msg_first == NULL) {
867 msqptr->msg_first = msghdr;
868 msqptr->msg_last = msghdr;
870 msqptr->msg_last->msg_next = msghdr;
871 msqptr->msg_last = msghdr;
873 msqptr->msg_last->msg_next = NULL;
875 msqptr->msg_cbytes += msghdr->msg_ts;
877 msqptr->msg_lspid = p->p_pid;
878 msqptr->msg_stime = time_second;
880 wakeup((caddr_t)msqptr);
885 #ifndef _SYS_SYSPROTO_H_
898 register struct msgrcv_args *uap;
900 int msqid = uap->msqid;
901 void *user_msgp = uap->msgp;
902 size_t msgsz = uap->msgsz;
903 long msgtyp = uap->msgtyp;
904 int msgflg = uap->msgflg;
906 register struct msqid_ds *msqptr;
907 register struct msg *msghdr;
912 printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
913 msgsz, msgtyp, msgflg);
916 if (!jail_sysvipc_allowed && p->p_prison != NULL)
919 msqid = IPCID_TO_IX(msqid);
921 if (msqid < 0 || msqid >= msginfo.msgmni) {
923 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
929 msqptr = &msqids[msqid];
930 if (msqptr->msg_qbytes == 0) {
932 printf("no such message queue id\n");
936 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
938 printf("wrong sequence number\n");
943 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
945 printf("requester doesn't have read access\n");
951 while (msghdr == NULL) {
953 msghdr = msqptr->msg_first;
954 if (msghdr != NULL) {
955 if (msgsz < msghdr->msg_ts &&
956 (msgflg & MSG_NOERROR) == 0) {
958 printf("first message on the queue is too big (want %d, got %d)\n",
959 msgsz, msghdr->msg_ts);
963 if (msqptr->msg_first == msqptr->msg_last) {
964 msqptr->msg_first = NULL;
965 msqptr->msg_last = NULL;
967 msqptr->msg_first = msghdr->msg_next;
968 if (msqptr->msg_first == NULL)
969 panic("msg_first/last screwed up #1");
973 struct msg *previous;
977 prev = &(msqptr->msg_first);
978 while ((msghdr = *prev) != NULL) {
980 * Is this message's type an exact match or is
981 * this message's type less than or equal to
982 * the absolute value of a negative msgtyp?
983 * Note that the second half of this test can
984 * NEVER be true if msgtyp is positive since
985 * msg_type is always positive!
988 if (msgtyp == msghdr->msg_type ||
989 msghdr->msg_type <= -msgtyp) {
991 printf("found message type %d, requested %d\n",
992 msghdr->msg_type, msgtyp);
994 if (msgsz < msghdr->msg_ts &&
995 (msgflg & MSG_NOERROR) == 0) {
997 printf("requested message on the queue is too big (want %d, got %d)\n",
998 msgsz, msghdr->msg_ts);
1002 *prev = msghdr->msg_next;
1003 if (msghdr == msqptr->msg_last) {
1004 if (previous == NULL) {
1007 panic("msg_first/last screwed up #2");
1015 panic("msg_first/last screwed up #3");
1023 prev = &(msghdr->msg_next);
1028 * We've either extracted the msghdr for the appropriate
1029 * message or there isn't one.
1030 * If there is one then bail out of this loop.
1037 * Hmph! No message found. Does the user want to wait?
1040 if ((msgflg & IPC_NOWAIT) != 0) {
1042 printf("no appropriate message found (msgtyp=%d)\n",
1045 /* The SVID says to return ENOMSG. */
1049 /* Unfortunately, BSD doesn't define that code yet! */
1055 * Wait for something to happen
1059 printf("msgrcv: goodnight\n");
1061 eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
1064 printf("msgrcv: good morning (eval=%d)\n", eval);
1069 printf("msgsnd: interrupted system call\n");
1075 * Make sure that the msq queue still exists
1078 if (msqptr->msg_qbytes == 0 ||
1079 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1081 printf("msqid deleted\n");
1088 * Return the message to the user.
1090 * First, do the bookkeeping (before we risk being interrupted).
1093 msqptr->msg_cbytes -= msghdr->msg_ts;
1095 msqptr->msg_lrpid = p->p_pid;
1096 msqptr->msg_rtime = time_second;
1099 * Make msgsz the actual amount that we'll be returning.
1100 * Note that this effectively truncates the message if it is too long
1101 * (since msgsz is never increased).
1105 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1108 if (msgsz > msghdr->msg_ts)
1109 msgsz = msghdr->msg_ts;
1112 * Return the type to the user.
1115 eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1116 sizeof(msghdr->msg_type));
1119 printf("error (%d) copying out message type\n", eval);
1121 msg_freehdr(msghdr);
1122 wakeup((caddr_t)msqptr);
1125 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1128 * Return the segments to the user
1131 next = msghdr->msg_spot;
1132 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1135 if (msgsz - len > msginfo.msgssz)
1136 tlen = msginfo.msgssz;
1140 panic("next too low #3");
1141 if (next >= msginfo.msgseg)
1142 panic("next out of range #3");
1143 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1147 printf("error (%d) copying out message segment\n",
1150 msg_freehdr(msghdr);
1151 wakeup((caddr_t)msqptr);
1154 user_msgp = (char *)user_msgp + tlen;
1155 next = msgmaps[next].next;
1159 * Done, return the actual number of bytes copied out.
1162 msg_freehdr(msghdr);
1163 wakeup((caddr_t)msqptr);
1164 p->p_retval[0] = msgsz;