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.
21 * This file is heavily changed to become part of Cygwin's cygserver.
24 #ifdef __OUTSIDE_CYGWIN__
26 #include <sys/cdefs.h>
28 #define __FBSDID(s) const char version[] = (s)
30 __FBSDID("$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/kern/sysv_msg.c,v 1.52 2003/11/07 04:47:14 rwatson Exp $");
31 /* CV, 2006-01-09: Inspected upstream up to version 1.60. */
34 #define __BSD_VISIBLE 1
35 #include <sys/types.h>
36 #include <sys/sysproto.h>
38 #include <sys/param.h>
43 #include "cygserver.h"
45 #include "cygserver_ipc.h"
49 #endif /* __CYGWIN__ */
52 #define DPRINTF(a) debug_printf a
57 static void msg_freehdr(struct msg *msghdr);
60 int msgctl(struct thread *, struct msgctl_args *);
61 int msgget(struct thread *, struct msgget_args *);
62 int msgsnd(struct thread *, struct msgsnd_args *);
63 int msgrcv(struct thread *, struct msgrcv_args *);
65 static sy_call_t *msgcalls[] = {
66 (sy_call_t *)msgctl, (sy_call_t *)msgget,
67 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
69 #endif /* __CYGWIN__ */
73 struct msg *msg_next; /* next msg in the chain */
74 long msg_type; /* type of this message */
75 /* >0 -> type of this message */
76 /* 0 -> free header */
77 u_short msg_ts; /* size of this message */
78 short msg_spot; /* location of start of msg in buffer */
83 #define MSGSSZ 8 /* Each segment must be 2^N long */
86 #define MSGSEG 2048 /* must be less than 32767 */
88 #define MSGMAX (MSGSSZ*MSGSEG)
90 #define MSGMNB 2048 /* max # of bytes in a queue */
100 * Based on the configuration parameters described in an SVR2 (yes, two)
101 * config(1m) man page.
103 * Each message is broken up and stored in segments that are msgssz bytes
104 * long. For efficiency reasons, this should be a power of two. Also,
105 * it doesn't make sense if it is less than 8 or greater than about 256.
106 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
107 * two between 8 and 1024 inclusive (and panic's if it isn't).
109 struct msginfo msginfo = {
110 MSGMAX, /* max chars in a message */
111 MSGMNB, /* max chars in a queue */
112 MSGMNI, /* # of message queue identifiers */
113 MSGTQL, /* max messages in system */
114 MSGSSZ, /* size of a message segment */
115 /* (must be small power of 2 greater than 4) */
116 MSGSEG /* number of message segments */
120 * macros to convert between msqid_ds's and msqid's.
121 * (specific to this implementation)
123 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
124 #define MSQID_IX(id) ((id) & 0xffff)
125 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
128 * The rest of this file is specific to this particular implementation.
132 short next; /* next segment in buffer */
133 /* -1 -> available */
134 /* 0..(MSGSEG-1) -> index of next segment */
137 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
139 static int nfree_msgmaps; /* # of free map entries */
140 static short free_msgmaps; /* head of linked list of free map entries */
141 static struct msg *free_msghdrs;/* list of free msg headers */
142 static char *msgpool; /* MSGMAX byte long msg buffer pool */
143 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
144 static struct msg *msghdrs; /* MSGTQL msg headers */
145 static struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */
146 static struct mtx msq_mtx; /* global mutex for message queues. */
149 static struct msg_info msg_info;
150 #endif /* __CYGWIN__ */
157 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
158 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
159 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
160 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
161 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
162 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
164 msgpool = (char *) sys_malloc(msginfo.msgmax, M_MSG, M_WAITOK);
166 panic("msgpool is NULL");
167 msgmaps = (msgmap *) sys_malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
169 panic("msgmaps is NULL");
170 msghdrs = (msg *) sys_malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
172 panic("msghdrs is NULL");
173 msqids = (msqid_ds *) sys_malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK);
175 panic("msqids is NULL");
178 * msginfo.msgssz should be a power of two for efficiency reasons.
179 * It is also pretty silly if msginfo.msgssz is less than 8
180 * or greater than about 256 so ...
184 while (i < 1024 && i != msginfo.msgssz)
186 if (i != msginfo.msgssz) {
187 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
189 panic("msginfo.msgssz not a small power of 2");
192 if (msginfo.msgseg > 32767) {
193 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
194 panic("msginfo.msgseg > 32767");
198 panic("msgmaps is NULL");
200 for (i = 0; i < msginfo.msgseg; i++) {
202 msgmaps[i-1].next = i;
203 msgmaps[i].next = -1; /* implies entry is available */
206 nfree_msgmaps = msginfo.msgseg;
209 panic("msghdrs is NULL");
211 for (i = 0; i < msginfo.msgtql; i++) {
212 msghdrs[i].msg_type = 0;
214 msghdrs[i-1].msg_next = &msghdrs[i];
215 msghdrs[i].msg_next = NULL;
217 free_msghdrs = &msghdrs[0];
220 panic("msqids is NULL");
222 for (i = 0; i < msginfo.msgmni; i++) {
223 msqids[i].msg_qbytes = 0; /* implies entry is available */
224 msqids[i].msg_perm.seq = 0; /* reset to a known value */
225 msqids[i].msg_perm.mode = 0;
227 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
233 struct msqid_ds *msqptr;
236 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
238 * Look for an unallocated and unlocked msqid_ds.
239 * msqid_ds's can be locked by msgsnd or msgrcv while
240 * they are copying the message in/out. We can't
241 * re-use the entry until they release it.
243 msqptr = &msqids[msqid];
244 if (msqptr->msg_qbytes != 0 ||
245 (msqptr->msg_perm.mode & MSG_LOCKED) != 0)
249 if (msqid != msginfo.msgmni)
251 #endif /* __CYGWIN__ */
253 sys_free(msgpool, M_MSG);
254 sys_free(msgmaps, M_MSG);
255 sys_free(msghdrs, M_MSG);
256 sys_free(msqids, M_MSG);
257 mtx_destroy(&msq_mtx);
264 sysvmsg_modload(struct module *module, int cmd, void *arg)
284 static moduledata_t sysvmsg_mod = {
290 SYSCALL_MODULE_HELPER(msgsys);
291 SYSCALL_MODULE_HELPER(msgctl);
292 SYSCALL_MODULE_HELPER(msgget);
293 SYSCALL_MODULE_HELPER(msgsnd);
294 SYSCALL_MODULE_HELPER(msgrcv);
296 DECLARE_MODULE(sysvmsg, sysvmsg_mod,
297 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
298 MODULE_VERSION(sysvmsg, 1);
301 * Entry point for all MSG calls
306 msgsys(thread *td, struct msgsys_args *uap)
310 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
312 if (uap->which < 0 ||
313 (unsigned) uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
315 error = (*msgcalls[uap->which])(td, &uap->a2);
321 msg_freehdr(struct msg *msghdr)
323 while (msghdr->msg_ts > 0) {
325 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
326 panic("msghdr->msg_spot out of range");
327 next = msgmaps[msghdr->msg_spot].next;
328 msgmaps[msghdr->msg_spot].next = free_msgmaps;
329 free_msgmaps = msghdr->msg_spot;
331 msghdr->msg_spot = next;
332 if (msghdr->msg_ts >= msginfo.msgssz)
333 msghdr->msg_ts -= msginfo.msgssz;
337 if (msghdr->msg_spot != -1)
338 panic("msghdr->msg_spot != -1");
339 msghdr->msg_next = free_msghdrs;
340 free_msghdrs = msghdr;
343 #ifndef _SYS_SYSPROTO_H_
347 struct msqid_ds *buf;
355 msgctl(struct thread *td, struct msgctl_args *uap)
357 int msqid = uap->msqid;
359 struct msqid_ds *user_msqptr = uap->buf;
361 struct msqid_ds msqbuf;
362 register struct msqid_ds *msqptr;
364 DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr));
366 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
370 if (cmd == IPC_INFO) {
372 error = copyout(&msginfo, user_msqptr,
373 sizeof(struct msginfo));
374 td->td_retval[0] = error ? -1 : 0;
377 if (msqid > msginfo.msgmni)
378 msqid = msginfo.msgmni;
379 error = copyout(msqids, user_msqptr,
380 msqid * sizeof(struct msqid_ds));
381 td->td_retval[0] = error ? -1 : 0;
383 } else if (cmd == MSG_INFO) {
385 error = copyout(&msg_info, user_msqptr,
386 sizeof(struct msg_info));
387 td->td_retval[0] = error ? -1 : 0;
388 mtx_unlock(&msq_mtx);
391 #endif /* __CYGWIN__ */
392 msqid = IPCID_TO_IX(msqid);
394 if (msqid < 0 || msqid >= msginfo.msgmni) {
395 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
399 if (cmd == IPC_SET &&
400 (error = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
403 msqptr = &msqids[msqid];
406 if (msqptr->msg_qbytes == 0) {
407 DPRINTF(("no such msqid\n"));
411 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
412 DPRINTF(("wrong sequence number\n"));
425 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M)))
427 /* Free the message headers */
428 msghdr = msqptr->msg_first;
429 while (msghdr != NULL) {
430 struct msg *msghdr_tmp;
432 /* Free the segments of each message */
433 msqptr->msg_cbytes -= msghdr->msg_ts;
436 msghdr = msghdr->msg_next;
437 msg_freehdr(msghdr_tmp);
440 if (msqptr->msg_cbytes != 0)
441 panic("msg_cbytes is screwed up");
442 if (msqptr->msg_qnum != 0)
443 panic("msg_qnum is screwed up");
445 msqptr->msg_qbytes = 0; /* Mark it as free */
448 #endif /* __CYGWIN__ */
456 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M)))
458 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
463 if (msqbuf.msg_qbytes > (unsigned long) msginfo.msgmnb) {
464 DPRINTF(("can't increase msg_qbytes beyond %d"
465 "(truncating)\n", msginfo.msgmnb));
466 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
468 if (msqbuf.msg_qbytes == 0) {
469 DPRINTF(("can't reduce msg_qbytes to 0\n"));
470 error = EINVAL; /* non-standard errno! */
473 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
474 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
475 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
476 (msqbuf.msg_perm.mode & 0777);
477 msqptr->msg_qbytes = msqbuf.msg_qbytes;
478 msqptr->msg_ctime = time (NULL);
482 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) {
483 DPRINTF(("requester doesn't have read access\n"));
489 DPRINTF(("invalid command %d\n", cmd));
495 td->td_retval[0] = rval;
497 mtx_unlock(&msq_mtx);
498 if (cmd == IPC_STAT && error == 0)
499 error = copyout(msqptr, user_msqptr, sizeof(struct msqid_ds));
503 #ifndef _SYS_SYSPROTO_H_
514 msgget(struct thread *td, struct msgget_args *uap)
516 int msqid, error = 0;
517 key_t key = uap->key;
518 unsigned msgflg = uap->msgflg;
519 register struct msqid_ds *msqptr = NULL;
521 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
523 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
527 if (key != IPC_PRIVATE) {
528 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
529 msqptr = &msqids[msqid];
530 if (msqptr->msg_qbytes != 0 &&
531 msqptr->msg_perm.key == key)
534 if (msqid < msginfo.msgmni) {
535 DPRINTF(("found public key\n"));
536 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
537 DPRINTF(("not exclusive\n"));
541 if ((error = ipcperm(td, &msqptr->msg_perm, msgflg & 0700))) {
542 DPRINTF(("requester doesn't have 0%o access\n",
550 DPRINTF(("need to allocate the msqid_ds\n"));
551 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
552 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
554 * Look for an unallocated and unlocked msqid_ds.
555 * msqid_ds's can be locked by msgsnd or msgrcv while
556 * they are copying the message in/out. We can't
557 * re-use the entry until they release it.
559 msqptr = &msqids[msqid];
560 if (msqptr->msg_qbytes == 0 &&
561 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
564 if (msqid == msginfo.msgmni) {
565 DPRINTF(("no more msqid_ds's available\n"));
569 DPRINTF(("msqid %d is available\n", msqid));
570 msqptr->msg_perm.key = key;
572 msqptr->msg_perm.cuid = td->ipcblk->uid;
573 msqptr->msg_perm.uid = td->ipcblk->uid;
574 msqptr->msg_perm.cgid = td->ipcblk->gid;
575 msqptr->msg_perm.gid = td->ipcblk->gid;
577 msqptr->msg_perm.cuid = cred->cr_uid;
578 msqptr->msg_perm.uid = cred->cr_uid;
579 msqptr->msg_perm.cgid = cred->cr_gid;
580 msqptr->msg_perm.gid = cred->cr_gid;
581 #endif /* __CYGWIN__ */
582 msqptr->msg_perm.mode = (msgflg & 0777);
583 /* Make sure that the returned msqid is unique */
584 msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff;
585 msqptr->msg_first = NULL;
586 msqptr->msg_last = NULL;
587 msqptr->msg_cbytes = 0;
588 msqptr->msg_qnum = 0;
589 msqptr->msg_qbytes = msginfo.msgmnb;
590 msqptr->msg_lspid = 0;
591 msqptr->msg_lrpid = 0;
592 msqptr->msg_stime = 0;
593 msqptr->msg_rtime = 0;
594 msqptr->msg_ctime = time (NULL);
597 #endif /* __CYGWIN__ */
599 DPRINTF(("didn't find it and wasn't asked to create it\n"));
605 /* Construct the unique msqid */
606 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
608 mtx_unlock(&msq_mtx);
612 #ifndef _SYS_SYSPROTO_H_
625 msgsnd(struct thread *td, struct msgsnd_args *uap)
627 int msqid = uap->msqid;
628 const void *user_msgp = uap->msgp;
629 size_t msgsz = uap->msgsz;
630 int msgflg = uap->msgflg;
631 int segs_needed, error = 0;
632 register struct msqid_ds *msqptr;
633 register struct msg *msghdr;
636 DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
639 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
643 msqid = IPCID_TO_IX(msqid);
645 if (msqid < 0 || msqid >= msginfo.msgmni) {
646 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
652 msqptr = &msqids[msqid];
653 if (msqptr->msg_qbytes == 0) {
654 DPRINTF(("no such message queue id\n"));
658 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
659 DPRINTF(("wrong sequence number\n"));
664 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_W))) {
665 DPRINTF(("requester doesn't have write access\n"));
669 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
670 DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
673 int need_more_resources = 0;
677 * (inside this loop in case msg_qbytes changes while we sleep)
680 if (msgsz > msqptr->msg_qbytes) {
681 DPRINTF(("msgsz > msqptr->msg_qbytes\n"));
686 if (msqptr->msg_perm.mode & MSG_LOCKED) {
687 DPRINTF(("msqid is locked\n"));
688 need_more_resources = 1;
690 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
691 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
692 need_more_resources = 1;
694 if (segs_needed > nfree_msgmaps) {
695 DPRINTF(("segs_needed > nfree_msgmaps\n"));
696 need_more_resources = 1;
698 if (free_msghdrs == NULL) {
699 DPRINTF(("no more msghdrs\n"));
700 need_more_resources = 1;
703 if (need_more_resources) {
706 if ((msgflg & IPC_NOWAIT) != 0) {
707 DPRINTF(("need more resources but caller "
708 "doesn't want to wait\n"));
713 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
714 DPRINTF(("we don't own the msqid_ds\n"));
717 /* Force later arrivals to wait for our
719 DPRINTF(("we own the msqid_ds\n"));
720 msqptr->msg_perm.mode |= MSG_LOCKED;
723 DPRINTF(("goodnight\n"));
724 error = msleep(msqptr, &msq_mtx, (PZERO - 4) | PCATCH,
726 DPRINTF(("good morning, error=%d\n", error));
728 msqptr->msg_perm.mode &= ~MSG_LOCKED;
729 if (error == EWOULDBLOCK) {
730 DPRINTF(("timed out\n"));
734 DPRINTF(("msgsnd: interrupted system call\n"));
737 #endif /* __CYGWIN__ */
743 * Make sure that the msq queue still exists
746 if (msqptr->msg_qbytes == 0) {
747 DPRINTF(("msqid deleted\n"));
753 DPRINTF(("got all the resources that we need\n"));
759 * We have the resources that we need.
763 if (msqptr->msg_perm.mode & MSG_LOCKED)
764 panic("msg_perm.mode & MSG_LOCKED");
765 if (segs_needed > nfree_msgmaps)
766 panic("segs_needed > nfree_msgmaps");
767 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
768 panic("msgsz + msg_cbytes > msg_qbytes");
769 if (free_msghdrs == NULL)
770 panic("no more msghdrs");
773 * Re-lock the msqid_ds in case we page-fault when copying in the
777 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
778 panic("msqid_ds is already locked");
779 msqptr->msg_perm.mode |= MSG_LOCKED;
782 * Allocate a message header
785 msghdr = free_msghdrs;
786 free_msghdrs = msghdr->msg_next;
787 msghdr->msg_spot = -1;
788 msghdr->msg_ts = msgsz;
791 * Allocate space for the message
794 while (segs_needed > 0) {
795 if (nfree_msgmaps <= 0)
796 panic("not enough msgmaps");
797 if (free_msgmaps == -1)
798 panic("nil free_msgmaps");
801 panic("next too low #1");
802 if (next >= msginfo.msgseg)
803 panic("next out of range #1");
804 DPRINTF(("allocating segment %d to message\n", next));
805 free_msgmaps = msgmaps[next].next;
807 msgmaps[next].next = msghdr->msg_spot;
808 msghdr->msg_spot = next;
813 * Copy in the message type
816 mtx_unlock(&msq_mtx);
817 if ((error = copyin(user_msgp, &msghdr->msg_type,
818 sizeof(msghdr->msg_type))) != 0) {
820 DPRINTF(("error %d copying the message type\n", error));
822 msqptr->msg_perm.mode &= ~MSG_LOCKED;
827 user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type);
830 * Validate the message type
833 if (msghdr->msg_type < 1) {
835 msqptr->msg_perm.mode &= ~MSG_LOCKED;
837 DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
843 * Copy in the message body
846 next = msghdr->msg_spot;
849 if (msgsz > (unsigned long) msginfo.msgssz)
850 tlen = msginfo.msgssz;
854 panic("next too low #2");
855 if (next >= msginfo.msgseg)
856 panic("next out of range #2");
857 mtx_unlock(&msq_mtx);
858 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
861 DPRINTF(("error %d copying in message segment\n",
864 msqptr->msg_perm.mode &= ~MSG_LOCKED;
870 user_msgp = (const char *)user_msgp + tlen;
871 next = msgmaps[next].next;
874 panic("didn't use all the msg segments");
877 * We've got the message. Unlock the msqid_ds.
880 msqptr->msg_perm.mode &= ~MSG_LOCKED;
883 * Make sure that the msqid_ds is still allocated.
886 if (msqptr->msg_qbytes == 0) {
894 * Put the message into the queue
897 if (msqptr->msg_first == NULL) {
898 msqptr->msg_first = msghdr;
899 msqptr->msg_last = msghdr;
901 msqptr->msg_last->msg_next = msghdr;
902 msqptr->msg_last = msghdr;
904 msqptr->msg_last->msg_next = NULL;
906 msqptr->msg_cbytes += msghdr->msg_ts;
908 msqptr->msg_lspid = td->td_proc->p_pid;
909 msqptr->msg_stime = time (NULL);
913 msg_info.msg_tot += uap->msgsz;
914 #endif /* __CYGWIN__ */
917 td->td_retval[0] = 0;
919 mtx_unlock(&msq_mtx);
923 #ifndef _SYS_SYSPROTO_H_
937 msgrcv(struct thread *td, struct msgrcv_args *uap)
939 int msqid = uap->msqid;
940 void *user_msgp = uap->msgp;
941 size_t msgsz = uap->msgsz;
942 long msgtyp = uap->msgtyp;
943 int msgflg = uap->msgflg;
945 register struct msqid_ds *msqptr;
946 register struct msg *msghdr;
950 DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
951 msgsz, msgtyp, msgflg));
953 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
956 msqid = IPCID_TO_IX(msqid);
958 if (msqid < 0 || msqid >= msginfo.msgmni) {
959 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
964 msqptr = &msqids[msqid];
966 if (msqptr->msg_qbytes == 0) {
967 DPRINTF(("no such message queue id\n"));
971 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
972 DPRINTF(("wrong sequence number\n"));
977 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) {
978 DPRINTF(("requester doesn't have read access\n"));
983 while (msghdr == NULL) {
985 msghdr = msqptr->msg_first;
986 if (msghdr != NULL) {
987 if (msgsz < msghdr->msg_ts &&
988 (msgflg & MSG_NOERROR) == 0) {
989 DPRINTF(("first message on the queue "
990 "is too big (want %d, got %d)\n",
991 msgsz, msghdr->msg_ts));
995 if (msqptr->msg_first == msqptr->msg_last) {
996 msqptr->msg_first = NULL;
997 msqptr->msg_last = NULL;
999 msqptr->msg_first = msghdr->msg_next;
1000 if (msqptr->msg_first == NULL)
1001 panic("msg_first/last screwed up #1");
1005 struct msg *previous;
1009 prev = &(msqptr->msg_first);
1010 while ((msghdr = *prev) != NULL) {
1012 * Is this message's type an exact match or is
1013 * this message's type less than or equal to
1014 * the absolute value of a negative msgtyp?
1015 * Note that the second half of this test can
1016 * NEVER be true if msgtyp is positive since
1017 * msg_type is always positive!
1020 if (msgtyp == msghdr->msg_type ||
1021 msghdr->msg_type <= -msgtyp) {
1022 DPRINTF(("found message type %d, "
1024 msghdr->msg_type, msgtyp));
1025 if (msgsz < msghdr->msg_ts &&
1026 (msgflg & MSG_NOERROR) == 0) {
1027 DPRINTF(("requested message "
1028 "on the queue is too big "
1029 "(want %d, got %d)\n",
1030 msgsz, msghdr->msg_ts));
1034 *prev = msghdr->msg_next;
1035 if (msghdr == msqptr->msg_last) {
1036 if (previous == NULL) {
1039 panic("msg_first/last screwed up #2");
1047 panic("msg_first/last screwed up #3");
1055 prev = &(msghdr->msg_next);
1060 * We've either extracted the msghdr for the appropriate
1061 * message or there isn't one.
1062 * If there is one then bail out of this loop.
1069 * Hmph! No message found. Does the user want to wait?
1072 if ((msgflg & IPC_NOWAIT) != 0) {
1073 DPRINTF(("no appropriate message found (msgtyp=%d)\n",
1075 /* The SVID says to return ENOMSG. */
1081 * Wait for something to happen
1084 DPRINTF(("msgrcv: goodnight\n"));
1085 error = msleep(msqptr, &msq_mtx, (PZERO - 4) | PCATCH,
1087 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1090 DPRINTF(("msgrcv: interrupted system call\n"));
1093 #endif /* __CYGWIN__ */
1099 * Make sure that the msq queue still exists
1102 if (msqptr->msg_qbytes == 0 ||
1103 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1104 DPRINTF(("msqid deleted\n"));
1111 * Return the message to the user.
1113 * First, do the bookkeeping (before we risk being interrupted).
1116 msqptr->msg_cbytes -= msghdr->msg_ts;
1118 msqptr->msg_lrpid = td->td_proc->p_pid;
1119 msqptr->msg_rtime = time (NULL);
1122 * Make msgsz the actual amount that we'll be returning.
1123 * Note that this effectively truncates the message if it is too long
1124 * (since msgsz is never increased).
1127 DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1129 if (msgsz > msghdr->msg_ts)
1130 msgsz = msghdr->msg_ts;
1133 * Return the type to the user.
1136 mtx_unlock(&msq_mtx);
1137 error = copyout(&(msghdr->msg_type), user_msgp,
1138 sizeof(msghdr->msg_type));
1141 DPRINTF(("error (%d) copying out message type\n", error));
1142 msg_freehdr(msghdr);
1146 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1149 * Return the segments to the user
1152 next = msghdr->msg_spot;
1153 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1156 if (msgsz - len > (unsigned long) msginfo.msgssz)
1157 tlen = msginfo.msgssz;
1161 panic("next too low #3");
1162 if (next >= msginfo.msgseg)
1163 panic("next out of range #3");
1164 mtx_unlock(&msq_mtx);
1165 error = copyout(&msgpool[next * msginfo.msgssz],
1169 DPRINTF(("error (%d) copying out message segment\n",
1171 msg_freehdr(msghdr);
1175 user_msgp = (char *)user_msgp + tlen;
1176 next = msgmaps[next].next;
1180 * Done, return the actual number of bytes copied out.
1185 msg_info.msg_tot -= msgsz;
1186 #endif /* __CYGWIN__ */
1188 msg_freehdr(msghdr);
1190 td->td_retval[0] = msgsz;
1192 mtx_unlock(&msq_mtx);
1198 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1201 return (SYSCTL_OUT(req, msqids,
1202 sizeof(struct msqid_ds) * msginfo.msgmni));
1205 SYSCTL_DECL(_kern_ipc);
1206 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, "");
1207 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0, "");
1208 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, "");
1209 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, "");
1210 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0, "");
1211 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0, "");
1212 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1213 NULL, 0, sysctl_msqids, "", "Message queue IDs");
1214 #endif /* __CYGWIN__ */
1215 #endif /* __OUTSIDE_CYGWIN__ */