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/mutex.h>
32 #include <sys/syscall.h>
33 #include <sys/sysent.h>
34 #include <sys/sysctl.h>
35 #include <sys/malloc.h>
38 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
40 static void msginit(void);
41 static int msgunload(void);
42 static int sysvmsg_modload(struct module *, int, void *);
47 static void msg_freehdr(struct msg *msghdr);
49 /* XXX casting to (sy_call_t *) is bogus, as usual. */
50 static sy_call_t *msgcalls[] = {
51 (sy_call_t *)msgctl, (sy_call_t *)msgget,
52 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
56 struct msg *msg_next; /* next msg in the chain */
57 long msg_type; /* type of this message */
58 /* >0 -> type of this message */
59 /* 0 -> free header */
60 u_short msg_ts; /* size of this message */
61 short msg_spot; /* location of start of msg in buffer */
66 #define MSGSSZ 8 /* Each segment must be 2^N long */
69 #define MSGSEG 2048 /* must be less than 32767 */
71 #define MSGMAX (MSGSSZ*MSGSEG)
73 #define MSGMNB 2048 /* max # of bytes in a queue */
83 * Based on the configuration parameters described in an SVR2 (yes, two)
84 * config(1m) man page.
86 * Each message is broken up and stored in segments that are msgssz bytes
87 * long. For efficiency reasons, this should be a power of two. Also,
88 * it doesn't make sense if it is less than 8 or greater than about 256.
89 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
90 * two between 8 and 1024 inclusive (and panic's if it isn't).
92 struct msginfo msginfo = {
93 MSGMAX, /* max chars in a message */
94 MSGMNI, /* # of message queue identifiers */
95 MSGMNB, /* max chars in a queue */
96 MSGTQL, /* max messages in system */
97 MSGSSZ, /* size of a message segment */
98 /* (must be small power of 2 greater than 4) */
99 MSGSEG /* number of message segments */
103 * macros to convert between msqid_ds's and msqid's.
104 * (specific to this implementation)
106 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
107 #define MSQID_IX(id) ((id) & 0xffff)
108 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
111 * The rest of this file is specific to this particular implementation.
115 short next; /* next segment in buffer */
116 /* -1 -> available */
117 /* 0..(MSGSEG-1) -> index of next segment */
120 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
122 static int nfree_msgmaps; /* # of free map entries */
123 static short free_msgmaps; /* head of linked list of free map entries */
124 static struct msg *free_msghdrs;/* list of free msg headers */
125 static char *msgpool; /* MSGMAX byte long msg buffer pool */
126 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
127 static struct msg *msghdrs; /* MSGTQL msg headers */
128 static struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */
135 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
136 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
137 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
138 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
140 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
142 panic("msgpool is NULL");
143 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
145 panic("msgmaps is NULL");
146 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
148 panic("msghdrs is NULL");
149 msqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK);
151 panic("msqids is NULL");
154 * msginfo.msgssz should be a power of two for efficiency reasons.
155 * It is also pretty silly if msginfo.msgssz is less than 8
156 * or greater than about 256 so ...
160 while (i < 1024 && i != msginfo.msgssz)
162 if (i != msginfo.msgssz) {
163 printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
165 panic("msginfo.msgssz not a small power of 2");
168 if (msginfo.msgseg > 32767) {
169 printf("msginfo.msgseg=%d\n", msginfo.msgseg);
170 panic("msginfo.msgseg > 32767");
174 panic("msgmaps is NULL");
176 for (i = 0; i < msginfo.msgseg; i++) {
178 msgmaps[i-1].next = i;
179 msgmaps[i].next = -1; /* implies entry is available */
182 nfree_msgmaps = msginfo.msgseg;
185 panic("msghdrs is NULL");
187 for (i = 0; i < msginfo.msgtql; i++) {
188 msghdrs[i].msg_type = 0;
190 msghdrs[i-1].msg_next = &msghdrs[i];
191 msghdrs[i].msg_next = NULL;
193 free_msghdrs = &msghdrs[0];
196 panic("msqids is NULL");
198 for (i = 0; i < msginfo.msgmni; i++) {
199 msqids[i].msg_qbytes = 0; /* implies entry is available */
200 msqids[i].msg_perm.seq = 0; /* reset to a known value */
201 msqids[i].msg_perm.mode = 0;
208 struct msqid_ds *msqptr;
211 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
213 * Look for an unallocated and unlocked msqid_ds.
214 * msqid_ds's can be locked by msgsnd or msgrcv while
215 * they are copying the message in/out. We can't
216 * re-use the entry until they release it.
218 msqptr = &msqids[msqid];
219 if (msqptr->msg_qbytes != 0 ||
220 (msqptr->msg_perm.mode & MSG_LOCKED) != 0)
223 if (msqid != msginfo.msgmni)
226 free(msgpool, M_MSG);
227 free(msgmaps, M_MSG);
228 free(msghdrs, M_MSG);
235 sysvmsg_modload(struct module *module, int cmd, void *arg)
255 static moduledata_t sysvmsg_mod = {
261 SYSCALL_MODULE_HELPER(msgsys);
262 SYSCALL_MODULE_HELPER(msgctl);
263 SYSCALL_MODULE_HELPER(msgget);
264 SYSCALL_MODULE_HELPER(msgsnd);
265 SYSCALL_MODULE_HELPER(msgrcv);
267 DECLARE_MODULE(sysvmsg, sysvmsg_mod,
268 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
269 MODULE_VERSION(sysvmsg, 1);
272 * Entry point for all MSG calls
279 /* XXX actually varargs. */
280 struct msgsys_args /* {
291 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
293 if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
296 error = (*msgcalls[uap->which])(td, &uap->a2);
305 while (msghdr->msg_ts > 0) {
307 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
308 panic("msghdr->msg_spot out of range");
309 next = msgmaps[msghdr->msg_spot].next;
310 msgmaps[msghdr->msg_spot].next = free_msgmaps;
311 free_msgmaps = msghdr->msg_spot;
313 msghdr->msg_spot = next;
314 if (msghdr->msg_ts >= msginfo.msgssz)
315 msghdr->msg_ts -= msginfo.msgssz;
319 if (msghdr->msg_spot != -1)
320 panic("msghdr->msg_spot != -1");
321 msghdr->msg_next = free_msghdrs;
322 free_msghdrs = msghdr;
325 #ifndef _SYS_SYSPROTO_H_
329 struct msqid_ds *buf;
339 register struct msgctl_args *uap;
341 int msqid = uap->msqid;
343 struct msqid_ds *user_msqptr = uap->buf;
345 struct msqid_ds msqbuf;
346 register struct msqid_ds *msqptr;
349 printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
351 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
355 msqid = IPCID_TO_IX(msqid);
357 if (msqid < 0 || msqid >= msginfo.msgmni) {
359 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
366 msqptr = &msqids[msqid];
368 if (msqptr->msg_qbytes == 0) {
370 printf("no such msqid\n");
375 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
377 printf("wrong sequence number\n");
391 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M)))
393 /* Free the message headers */
394 msghdr = msqptr->msg_first;
395 while (msghdr != NULL) {
396 struct msg *msghdr_tmp;
398 /* Free the segments of each message */
399 msqptr->msg_cbytes -= msghdr->msg_ts;
402 msghdr = msghdr->msg_next;
403 msg_freehdr(msghdr_tmp);
406 if (msqptr->msg_cbytes != 0)
407 panic("msg_cbytes is screwed up");
408 if (msqptr->msg_qnum != 0)
409 panic("msg_qnum is screwed up");
411 msqptr->msg_qbytes = 0; /* Mark it as free */
413 wakeup((caddr_t)msqptr);
419 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M)))
421 if ((error = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
423 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
424 error = suser_td(td);
428 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
430 printf("can't increase msg_qbytes beyond %d (truncating)\n",
433 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
435 if (msqbuf.msg_qbytes == 0) {
437 printf("can't reduce msg_qbytes to 0\n");
439 error = EINVAL; /* non-standard errno! */
442 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
443 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
444 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
445 (msqbuf.msg_perm.mode & 0777);
446 msqptr->msg_qbytes = msqbuf.msg_qbytes;
447 msqptr->msg_ctime = time_second;
451 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) {
453 printf("requester doesn't have read access\n");
457 error = copyout((caddr_t)msqptr, user_msqptr,
458 sizeof(struct msqid_ds));
463 printf("invalid command %d\n", cmd);
470 td->td_retval[0] = rval;
476 #ifndef _SYS_SYSPROTO_H_
489 register struct msgget_args *uap;
491 int msqid, error = 0;
493 int msgflg = uap->msgflg;
494 struct ucred *cred = td->td_ucred;
495 register struct msqid_ds *msqptr = NULL;
498 printf("msgget(0x%x, 0%o)\n", key, msgflg);
501 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
505 if (key != IPC_PRIVATE) {
506 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
507 msqptr = &msqids[msqid];
508 if (msqptr->msg_qbytes != 0 &&
509 msqptr->msg_perm.key == key)
512 if (msqid < msginfo.msgmni) {
514 printf("found public key\n");
516 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
518 printf("not exclusive\n");
523 if ((error = ipcperm(td, &msqptr->msg_perm, msgflg & 0700 ))) {
525 printf("requester doesn't have 0%o access\n",
535 printf("need to allocate the msqid_ds\n");
537 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
538 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
540 * Look for an unallocated and unlocked msqid_ds.
541 * msqid_ds's can be locked by msgsnd or msgrcv while
542 * they are copying the message in/out. We can't
543 * re-use the entry until they release it.
545 msqptr = &msqids[msqid];
546 if (msqptr->msg_qbytes == 0 &&
547 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
550 if (msqid == msginfo.msgmni) {
552 printf("no more msqid_ds's available\n");
558 printf("msqid %d is available\n", msqid);
560 msqptr->msg_perm.key = key;
561 msqptr->msg_perm.cuid = cred->cr_uid;
562 msqptr->msg_perm.uid = cred->cr_uid;
563 msqptr->msg_perm.cgid = cred->cr_gid;
564 msqptr->msg_perm.gid = cred->cr_gid;
565 msqptr->msg_perm.mode = (msgflg & 0777);
566 /* Make sure that the returned msqid is unique */
567 msqptr->msg_perm.seq++;
568 msqptr->msg_first = NULL;
569 msqptr->msg_last = NULL;
570 msqptr->msg_cbytes = 0;
571 msqptr->msg_qnum = 0;
572 msqptr->msg_qbytes = msginfo.msgmnb;
573 msqptr->msg_lspid = 0;
574 msqptr->msg_lrpid = 0;
575 msqptr->msg_stime = 0;
576 msqptr->msg_rtime = 0;
577 msqptr->msg_ctime = time_second;
580 printf("didn't find it and wasn't asked to create it\n");
587 /* Construct the unique msqid */
588 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
594 #ifndef _SYS_SYSPROTO_H_
609 register struct msgsnd_args *uap;
611 int msqid = uap->msqid;
612 void *user_msgp = uap->msgp;
613 size_t msgsz = uap->msgsz;
614 int msgflg = uap->msgflg;
615 int segs_needed, error = 0;
616 register struct msqid_ds *msqptr;
617 register struct msg *msghdr;
621 printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
624 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
628 msqid = IPCID_TO_IX(msqid);
630 if (msqid < 0 || msqid >= msginfo.msgmni) {
632 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
639 msqptr = &msqids[msqid];
640 if (msqptr->msg_qbytes == 0) {
642 printf("no such message queue id\n");
647 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
649 printf("wrong sequence number\n");
655 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_W))) {
657 printf("requester doesn't have write access\n");
662 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
664 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
668 int need_more_resources = 0;
672 * (inside this loop in case msg_qbytes changes while we sleep)
675 if (msgsz > msqptr->msg_qbytes) {
677 printf("msgsz > msqptr->msg_qbytes\n");
683 if (msqptr->msg_perm.mode & MSG_LOCKED) {
685 printf("msqid is locked\n");
687 need_more_resources = 1;
689 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
691 printf("msgsz + msg_cbytes > msg_qbytes\n");
693 need_more_resources = 1;
695 if (segs_needed > nfree_msgmaps) {
697 printf("segs_needed > nfree_msgmaps\n");
699 need_more_resources = 1;
701 if (free_msghdrs == NULL) {
703 printf("no more msghdrs\n");
705 need_more_resources = 1;
708 if (need_more_resources) {
711 if ((msgflg & IPC_NOWAIT) != 0) {
713 printf("need more resources but caller doesn't want to wait\n");
719 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
721 printf("we don't own the msqid_ds\n");
725 /* Force later arrivals to wait for our
728 printf("we own the msqid_ds\n");
730 msqptr->msg_perm.mode |= MSG_LOCKED;
734 printf("goodnight\n");
736 error = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
739 printf("good morning, error=%d\n", error);
742 msqptr->msg_perm.mode &= ~MSG_LOCKED;
745 printf("msgsnd: interrupted system call\n");
752 * Make sure that the msq queue still exists
755 if (msqptr->msg_qbytes == 0) {
757 printf("msqid deleted\n");
765 printf("got all the resources that we need\n");
772 * We have the resources that we need.
776 if (msqptr->msg_perm.mode & MSG_LOCKED)
777 panic("msg_perm.mode & MSG_LOCKED");
778 if (segs_needed > nfree_msgmaps)
779 panic("segs_needed > nfree_msgmaps");
780 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
781 panic("msgsz + msg_cbytes > msg_qbytes");
782 if (free_msghdrs == NULL)
783 panic("no more msghdrs");
786 * Re-lock the msqid_ds in case we page-fault when copying in the
790 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
791 panic("msqid_ds is already locked");
792 msqptr->msg_perm.mode |= MSG_LOCKED;
795 * Allocate a message header
798 msghdr = free_msghdrs;
799 free_msghdrs = msghdr->msg_next;
800 msghdr->msg_spot = -1;
801 msghdr->msg_ts = msgsz;
804 * Allocate space for the message
807 while (segs_needed > 0) {
808 if (nfree_msgmaps <= 0)
809 panic("not enough msgmaps");
810 if (free_msgmaps == -1)
811 panic("nil free_msgmaps");
814 panic("next too low #1");
815 if (next >= msginfo.msgseg)
816 panic("next out of range #1");
818 printf("allocating segment %d to message\n", next);
820 free_msgmaps = msgmaps[next].next;
822 msgmaps[next].next = msghdr->msg_spot;
823 msghdr->msg_spot = next;
828 * Copy in the message type
831 if ((error = copyin(user_msgp, &msghdr->msg_type,
832 sizeof(msghdr->msg_type))) != 0) {
834 printf("error %d copying the message type\n", error);
837 msqptr->msg_perm.mode &= ~MSG_LOCKED;
838 wakeup((caddr_t)msqptr);
841 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
844 * Validate the message type
847 if (msghdr->msg_type < 1) {
849 msqptr->msg_perm.mode &= ~MSG_LOCKED;
850 wakeup((caddr_t)msqptr);
852 printf("mtype (%d) < 1\n", msghdr->msg_type);
859 * Copy in the message body
862 next = msghdr->msg_spot;
865 if (msgsz > msginfo.msgssz)
866 tlen = msginfo.msgssz;
870 panic("next too low #2");
871 if (next >= msginfo.msgseg)
872 panic("next out of range #2");
873 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
876 printf("error %d copying in message segment\n", error);
879 msqptr->msg_perm.mode &= ~MSG_LOCKED;
880 wakeup((caddr_t)msqptr);
884 user_msgp = (char *)user_msgp + tlen;
885 next = msgmaps[next].next;
888 panic("didn't use all the msg segments");
891 * We've got the message. Unlock the msqid_ds.
894 msqptr->msg_perm.mode &= ~MSG_LOCKED;
897 * Make sure that the msqid_ds is still allocated.
900 if (msqptr->msg_qbytes == 0) {
902 wakeup((caddr_t)msqptr);
908 * Put the message into the queue
911 if (msqptr->msg_first == NULL) {
912 msqptr->msg_first = msghdr;
913 msqptr->msg_last = msghdr;
915 msqptr->msg_last->msg_next = msghdr;
916 msqptr->msg_last = msghdr;
918 msqptr->msg_last->msg_next = NULL;
920 msqptr->msg_cbytes += msghdr->msg_ts;
922 msqptr->msg_lspid = td->td_proc->p_pid;
923 msqptr->msg_stime = time_second;
925 wakeup((caddr_t)msqptr);
926 td->td_retval[0] = 0;
932 #ifndef _SYS_SYSPROTO_H_
948 register struct msgrcv_args *uap;
950 int msqid = uap->msqid;
951 void *user_msgp = uap->msgp;
952 size_t msgsz = uap->msgsz;
953 long msgtyp = uap->msgtyp;
954 int msgflg = uap->msgflg;
956 register struct msqid_ds *msqptr;
957 register struct msg *msghdr;
962 printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
963 msgsz, msgtyp, msgflg);
966 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
970 msqid = IPCID_TO_IX(msqid);
972 if (msqid < 0 || msqid >= msginfo.msgmni) {
974 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
981 msqptr = &msqids[msqid];
982 if (msqptr->msg_qbytes == 0) {
984 printf("no such message queue id\n");
989 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
991 printf("wrong sequence number\n");
997 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) {
999 printf("requester doesn't have read access\n");
1005 while (msghdr == NULL) {
1007 msghdr = msqptr->msg_first;
1008 if (msghdr != NULL) {
1009 if (msgsz < msghdr->msg_ts &&
1010 (msgflg & MSG_NOERROR) == 0) {
1012 printf("first message on the queue is too big (want %d, got %d)\n",
1013 msgsz, msghdr->msg_ts);
1018 if (msqptr->msg_first == msqptr->msg_last) {
1019 msqptr->msg_first = NULL;
1020 msqptr->msg_last = NULL;
1022 msqptr->msg_first = msghdr->msg_next;
1023 if (msqptr->msg_first == NULL)
1024 panic("msg_first/last screwed up #1");
1028 struct msg *previous;
1032 prev = &(msqptr->msg_first);
1033 while ((msghdr = *prev) != NULL) {
1035 * Is this message's type an exact match or is
1036 * this message's type less than or equal to
1037 * the absolute value of a negative msgtyp?
1038 * Note that the second half of this test can
1039 * NEVER be true if msgtyp is positive since
1040 * msg_type is always positive!
1043 if (msgtyp == msghdr->msg_type ||
1044 msghdr->msg_type <= -msgtyp) {
1046 printf("found message type %d, requested %d\n",
1047 msghdr->msg_type, msgtyp);
1049 if (msgsz < msghdr->msg_ts &&
1050 (msgflg & MSG_NOERROR) == 0) {
1052 printf("requested message on the queue is too big (want %d, got %d)\n",
1053 msgsz, msghdr->msg_ts);
1058 *prev = msghdr->msg_next;
1059 if (msghdr == msqptr->msg_last) {
1060 if (previous == NULL) {
1063 panic("msg_first/last screwed up #2");
1071 panic("msg_first/last screwed up #3");
1079 prev = &(msghdr->msg_next);
1084 * We've either extracted the msghdr for the appropriate
1085 * message or there isn't one.
1086 * If there is one then bail out of this loop.
1093 * Hmph! No message found. Does the user want to wait?
1096 if ((msgflg & IPC_NOWAIT) != 0) {
1098 printf("no appropriate message found (msgtyp=%d)\n",
1101 /* The SVID says to return ENOMSG. */
1107 * Wait for something to happen
1111 printf("msgrcv: goodnight\n");
1113 error = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
1116 printf("msgrcv: good morning (error=%d)\n", error);
1121 printf("msgsnd: interrupted system call\n");
1128 * Make sure that the msq queue still exists
1131 if (msqptr->msg_qbytes == 0 ||
1132 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1134 printf("msqid deleted\n");
1142 * Return the message to the user.
1144 * First, do the bookkeeping (before we risk being interrupted).
1147 msqptr->msg_cbytes -= msghdr->msg_ts;
1149 msqptr->msg_lrpid = td->td_proc->p_pid;
1150 msqptr->msg_rtime = time_second;
1153 * Make msgsz the actual amount that we'll be returning.
1154 * Note that this effectively truncates the message if it is too long
1155 * (since msgsz is never increased).
1159 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1162 if (msgsz > msghdr->msg_ts)
1163 msgsz = msghdr->msg_ts;
1166 * Return the type to the user.
1169 error = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1170 sizeof(msghdr->msg_type));
1173 printf("error (%d) copying out message type\n", error);
1175 msg_freehdr(msghdr);
1176 wakeup((caddr_t)msqptr);
1179 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1182 * Return the segments to the user
1185 next = msghdr->msg_spot;
1186 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1189 if (msgsz - len > msginfo.msgssz)
1190 tlen = msginfo.msgssz;
1194 panic("next too low #3");
1195 if (next >= msginfo.msgseg)
1196 panic("next out of range #3");
1197 error = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1201 printf("error (%d) copying out message segment\n",
1204 msg_freehdr(msghdr);
1205 wakeup((caddr_t)msqptr);
1208 user_msgp = (char *)user_msgp + tlen;
1209 next = msgmaps[next].next;
1213 * Done, return the actual number of bytes copied out.
1216 msg_freehdr(msghdr);
1217 wakeup((caddr_t)msqptr);
1218 td->td_retval[0] = msgsz;
1225 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1228 return (SYSCTL_OUT(req, msqids,
1229 sizeof(struct msqid_ds) * msginfo.msgmni));
1232 SYSCTL_DECL(_kern_ipc);
1233 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, "");
1234 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RD, &msginfo.msgmni, 0, "");
1235 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, "");
1236 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, "");
1237 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RD, &msginfo.msgssz, 0, "");
1238 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RD, &msginfo.msgseg, 0, "")
1239 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1240 NULL, 0, sysctl_msqids, "", "Message queue IDs");