xref: /freebsd/sys/kern/sysv_msg.c (revision cb1f0db9dbf89ed25f16308c33a721e7ed6ba531)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
23d903220SDoug Rabson 
33d903220SDoug Rabson /*
43d903220SDoug Rabson  * Implementation of SVID messages
53d903220SDoug Rabson  *
63d903220SDoug Rabson  * Author:  Daniel Boulet
73d903220SDoug Rabson  *
83d903220SDoug Rabson  * Copyright 1993 Daniel Boulet and RTMX Inc.
93d903220SDoug Rabson  *
103d903220SDoug Rabson  * This system call was implemented by Daniel Boulet under contract from RTMX.
113d903220SDoug Rabson  *
123d903220SDoug Rabson  * Redistribution and use in source forms, with and without modification,
133d903220SDoug Rabson  * are permitted provided that this entire comment appears intact.
143d903220SDoug Rabson  *
153d903220SDoug Rabson  * Redistribution in binary form may occur without any restrictions.
163d903220SDoug Rabson  * Obviously, it would be nice if you gave credit where credit is due
173d903220SDoug Rabson  * but requiring it would be too onerous.
183d903220SDoug Rabson  *
193d903220SDoug Rabson  * This software is provided ``AS IS'' without any warranties of any kind.
203d903220SDoug Rabson  */
213d903220SDoug Rabson 
22ab063af9SPeter Wemm #include "opt_sysvipc.h"
23ab063af9SPeter Wemm 
243d903220SDoug Rabson #include <sys/param.h>
253d903220SDoug Rabson #include <sys/systm.h>
26725db531SBruce Evans #include <sys/sysproto.h>
273d903220SDoug Rabson #include <sys/kernel.h>
283d903220SDoug Rabson #include <sys/proc.h>
293d903220SDoug Rabson #include <sys/msg.h>
30725db531SBruce Evans #include <sys/sysent.h>
31ab063af9SPeter Wemm #include <sys/sysctl.h>
32ab063af9SPeter Wemm #include <sys/malloc.h>
33cb1f0db9SRobert Watson #include <sys/jail.h>
34ab063af9SPeter Wemm 
35ab063af9SPeter Wemm static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
363d903220SDoug Rabson 
374590fd3aSDavid Greenman static void msginit __P((void *));
382b14f991SJulian Elischer 
393d903220SDoug Rabson #define MSG_DEBUG
403d903220SDoug Rabson #undef MSG_DEBUG_OK
413d903220SDoug Rabson 
42725db531SBruce Evans static void msg_freehdr __P((struct msg *msghdr));
433d903220SDoug Rabson 
44725db531SBruce Evans /* XXX casting to (sy_call_t *) is bogus, as usual. */
4587b6de2bSPoul-Henning Kamp static sy_call_t *msgcalls[] = {
46725db531SBruce Evans 	(sy_call_t *)msgctl, (sy_call_t *)msgget,
47725db531SBruce Evans 	(sy_call_t *)msgsnd, (sy_call_t *)msgrcv
48725db531SBruce Evans };
493d903220SDoug Rabson 
50ab063af9SPeter Wemm struct msg {
51ab063af9SPeter Wemm 	struct	msg *msg_next;	/* next msg in the chain */
52ab063af9SPeter Wemm 	long	msg_type;	/* type of this message */
53ab063af9SPeter Wemm     				/* >0 -> type of this message */
54ab063af9SPeter Wemm     				/* 0 -> free header */
55ab063af9SPeter Wemm 	u_short	msg_ts;		/* size of this message */
56ab063af9SPeter Wemm 	short	msg_spot;	/* location of start of msg in buffer */
57ab063af9SPeter Wemm };
58ab063af9SPeter Wemm 
59ab063af9SPeter Wemm 
60ab063af9SPeter Wemm #ifndef MSGSSZ
61ab063af9SPeter Wemm #define MSGSSZ	8		/* Each segment must be 2^N long */
62ab063af9SPeter Wemm #endif
63ab063af9SPeter Wemm #ifndef MSGSEG
64ab063af9SPeter Wemm #define MSGSEG	2048		/* must be less than 32767 */
65ab063af9SPeter Wemm #endif
66ab063af9SPeter Wemm #define MSGMAX	(MSGSSZ*MSGSEG)
67ab063af9SPeter Wemm #ifndef MSGMNB
68ab063af9SPeter Wemm #define MSGMNB	2048		/* max # of bytes in a queue */
69ab063af9SPeter Wemm #endif
70ab063af9SPeter Wemm #ifndef MSGMNI
71ab063af9SPeter Wemm #define MSGMNI	40
72ab063af9SPeter Wemm #endif
73ab063af9SPeter Wemm #ifndef MSGTQL
74ab063af9SPeter Wemm #define MSGTQL	40
75ab063af9SPeter Wemm #endif
76ab063af9SPeter Wemm 
77ab063af9SPeter Wemm /*
78ab063af9SPeter Wemm  * Based on the configuration parameters described in an SVR2 (yes, two)
79ab063af9SPeter Wemm  * config(1m) man page.
80ab063af9SPeter Wemm  *
81ab063af9SPeter Wemm  * Each message is broken up and stored in segments that are msgssz bytes
82ab063af9SPeter Wemm  * long.  For efficiency reasons, this should be a power of two.  Also,
83ab063af9SPeter Wemm  * it doesn't make sense if it is less than 8 or greater than about 256.
84ab063af9SPeter Wemm  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
85ab063af9SPeter Wemm  * two between 8 and 1024 inclusive (and panic's if it isn't).
86ab063af9SPeter Wemm  */
87ab063af9SPeter Wemm struct msginfo msginfo = {
88ab063af9SPeter Wemm                 MSGMAX,         /* max chars in a message */
89ab063af9SPeter Wemm                 MSGMNI,         /* # of message queue identifiers */
90ab063af9SPeter Wemm                 MSGMNB,         /* max chars in a queue */
91ab063af9SPeter Wemm                 MSGTQL,         /* max messages in system */
92ab063af9SPeter Wemm                 MSGSSZ,         /* size of a message segment */
93ab063af9SPeter Wemm                 		/* (must be small power of 2 greater than 4) */
94ab063af9SPeter Wemm                 MSGSEG          /* number of message segments */
95ab063af9SPeter Wemm };
96ab063af9SPeter Wemm 
97ab063af9SPeter Wemm /*
98ab063af9SPeter Wemm  * macros to convert between msqid_ds's and msqid's.
99ab063af9SPeter Wemm  * (specific to this implementation)
100ab063af9SPeter Wemm  */
101ab063af9SPeter Wemm #define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
102ab063af9SPeter Wemm #define MSQID_IX(id)	((id) & 0xffff)
103ab063af9SPeter Wemm #define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
104ab063af9SPeter Wemm 
105ab063af9SPeter Wemm /*
106ab063af9SPeter Wemm  * The rest of this file is specific to this particular implementation.
107ab063af9SPeter Wemm  */
108ab063af9SPeter Wemm 
109ab063af9SPeter Wemm struct msgmap {
110ab063af9SPeter Wemm 	short	next;		/* next segment in buffer */
111ab063af9SPeter Wemm     				/* -1 -> available */
112ab063af9SPeter Wemm     				/* 0..(MSGSEG-1) -> index of next segment */
113ab063af9SPeter Wemm };
114ab063af9SPeter Wemm 
115ab063af9SPeter Wemm #define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
116ab063af9SPeter Wemm 
11787b6de2bSPoul-Henning Kamp static int nfree_msgmaps;	/* # of free map entries */
11887b6de2bSPoul-Henning Kamp static short free_msgmaps;	/* head of linked list of free map entries */
11987b6de2bSPoul-Henning Kamp static struct msg *free_msghdrs;/* list of free msg headers */
120ab063af9SPeter Wemm static char *msgpool;		/* MSGMAX byte long msg buffer pool */
121ab063af9SPeter Wemm static struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
122ab063af9SPeter Wemm static struct msg *msghdrs;	/* MSGTQL msg headers */
123ab063af9SPeter Wemm static struct msqid_ds *msqids;	/* MSGMNI msqid_ds struct's */
1243d903220SDoug Rabson 
125ab063af9SPeter Wemm static void
126725db531SBruce Evans msginit(dummy)
127725db531SBruce Evans 	void *dummy;
1283d903220SDoug Rabson {
1293d903220SDoug Rabson 	register int i;
1303d903220SDoug Rabson 
131ab063af9SPeter Wemm 	msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
132ab063af9SPeter Wemm 	if (msgpool == NULL)
133ab063af9SPeter Wemm 		panic("msgpool is NULL");
134ab063af9SPeter Wemm 	msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
135ab063af9SPeter Wemm 	if (msgmaps == NULL)
136ab063af9SPeter Wemm 		panic("msgmaps is NULL");
137ab063af9SPeter Wemm 	msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
138ab063af9SPeter Wemm 	if (msghdrs == NULL)
139ab063af9SPeter Wemm 		panic("msghdrs is NULL");
140ab063af9SPeter Wemm 	msqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK);
141ab063af9SPeter Wemm 	if (msqids == NULL)
142ab063af9SPeter Wemm 		panic("msqids is NULL");
143ab063af9SPeter Wemm 
1443d903220SDoug Rabson 	/*
1453d903220SDoug Rabson 	 * msginfo.msgssz should be a power of two for efficiency reasons.
1463d903220SDoug Rabson 	 * It is also pretty silly if msginfo.msgssz is less than 8
1473d903220SDoug Rabson 	 * or greater than about 256 so ...
1483d903220SDoug Rabson 	 */
1493d903220SDoug Rabson 
1503d903220SDoug Rabson 	i = 8;
1513d903220SDoug Rabson 	while (i < 1024 && i != msginfo.msgssz)
1523d903220SDoug Rabson 		i <<= 1;
1533d903220SDoug Rabson     	if (i != msginfo.msgssz) {
1543d903220SDoug Rabson 		printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
1553d903220SDoug Rabson 		    msginfo.msgssz);
1563d903220SDoug Rabson 		panic("msginfo.msgssz not a small power of 2");
1573d903220SDoug Rabson 	}
1583d903220SDoug Rabson 
1593d903220SDoug Rabson 	if (msginfo.msgseg > 32767) {
1603d903220SDoug Rabson 		printf("msginfo.msgseg=%d\n", msginfo.msgseg);
1613d903220SDoug Rabson 		panic("msginfo.msgseg > 32767");
1623d903220SDoug Rabson 	}
1633d903220SDoug Rabson 
1643d903220SDoug Rabson 	if (msgmaps == NULL)
1653d903220SDoug Rabson 		panic("msgmaps is NULL");
1663d903220SDoug Rabson 
1673d903220SDoug Rabson 	for (i = 0; i < msginfo.msgseg; i++) {
1683d903220SDoug Rabson 		if (i > 0)
1693d903220SDoug Rabson 			msgmaps[i-1].next = i;
1703d903220SDoug Rabson 		msgmaps[i].next = -1;	/* implies entry is available */
1713d903220SDoug Rabson 	}
1723d903220SDoug Rabson 	free_msgmaps = 0;
1733d903220SDoug Rabson 	nfree_msgmaps = msginfo.msgseg;
1743d903220SDoug Rabson 
1753d903220SDoug Rabson 	if (msghdrs == NULL)
1763d903220SDoug Rabson 		panic("msghdrs is NULL");
1773d903220SDoug Rabson 
1783d903220SDoug Rabson 	for (i = 0; i < msginfo.msgtql; i++) {
1793d903220SDoug Rabson 		msghdrs[i].msg_type = 0;
1803d903220SDoug Rabson 		if (i > 0)
1813d903220SDoug Rabson 			msghdrs[i-1].msg_next = &msghdrs[i];
1823d903220SDoug Rabson 		msghdrs[i].msg_next = NULL;
1833d903220SDoug Rabson     	}
1843d903220SDoug Rabson 	free_msghdrs = &msghdrs[0];
1853d903220SDoug Rabson 
1863d903220SDoug Rabson 	if (msqids == NULL)
1873d903220SDoug Rabson 		panic("msqids is NULL");
1883d903220SDoug Rabson 
1893d903220SDoug Rabson 	for (i = 0; i < msginfo.msgmni; i++) {
1903d903220SDoug Rabson 		msqids[i].msg_qbytes = 0;	/* implies entry is available */
1913d903220SDoug Rabson 		msqids[i].msg_perm.seq = 0;	/* reset to a known value */
1926413a4bcSPeter Wemm 		msqids[i].msg_perm.mode = 0;
1933d903220SDoug Rabson 	}
1943d903220SDoug Rabson }
195ab063af9SPeter Wemm SYSINIT(sysv_msg, SI_SUB_SYSV_MSG, SI_ORDER_FIRST, msginit, NULL)
1963d903220SDoug Rabson 
1973d903220SDoug Rabson /*
1983d903220SDoug Rabson  * Entry point for all MSG calls
1993d903220SDoug Rabson  */
2003d903220SDoug Rabson int
201cb226aaaSPoul-Henning Kamp msgsys(p, uap)
202725db531SBruce Evans 	struct proc *p;
203725db531SBruce Evans 	/* XXX actually varargs. */
204725db531SBruce Evans 	struct msgsys_args /* {
205725db531SBruce Evans 		u_int	which;
206725db531SBruce Evans 		int	a2;
207725db531SBruce Evans 		int	a3;
208725db531SBruce Evans 		int	a4;
209725db531SBruce Evans 		int	a5;
210725db531SBruce Evans 		int	a6;
211725db531SBruce Evans 	} */ *uap;
2123d903220SDoug Rabson {
2133d903220SDoug Rabson 
214cb1f0db9SRobert Watson 	if (!jail_sysvipc_allowed && p->p_prison != NULL)
215cb1f0db9SRobert Watson 		return (ENOSYS);
216cb1f0db9SRobert Watson 
2173d903220SDoug Rabson 	if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
2183d903220SDoug Rabson 		return (EINVAL);
219cb226aaaSPoul-Henning Kamp 	return ((*msgcalls[uap->which])(p, &uap->a2));
2203d903220SDoug Rabson }
2213d903220SDoug Rabson 
2223d903220SDoug Rabson static void
2233d903220SDoug Rabson msg_freehdr(msghdr)
2243d903220SDoug Rabson 	struct msg *msghdr;
2253d903220SDoug Rabson {
2263d903220SDoug Rabson 	while (msghdr->msg_ts > 0) {
2273d903220SDoug Rabson 		short next;
2283d903220SDoug Rabson 		if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
2293d903220SDoug Rabson 			panic("msghdr->msg_spot out of range");
2303d903220SDoug Rabson 		next = msgmaps[msghdr->msg_spot].next;
2313d903220SDoug Rabson 		msgmaps[msghdr->msg_spot].next = free_msgmaps;
2323d903220SDoug Rabson 		free_msgmaps = msghdr->msg_spot;
2333d903220SDoug Rabson 		nfree_msgmaps++;
2343d903220SDoug Rabson 		msghdr->msg_spot = next;
2353d903220SDoug Rabson 		if (msghdr->msg_ts >= msginfo.msgssz)
2363d903220SDoug Rabson 			msghdr->msg_ts -= msginfo.msgssz;
2373d903220SDoug Rabson 		else
2383d903220SDoug Rabson 			msghdr->msg_ts = 0;
2393d903220SDoug Rabson 	}
2403d903220SDoug Rabson 	if (msghdr->msg_spot != -1)
2413d903220SDoug Rabson 		panic("msghdr->msg_spot != -1");
2423d903220SDoug Rabson 	msghdr->msg_next = free_msghdrs;
2433d903220SDoug Rabson 	free_msghdrs = msghdr;
2443d903220SDoug Rabson }
2453d903220SDoug Rabson 
246b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_
2473d903220SDoug Rabson struct msgctl_args {
2483d903220SDoug Rabson 	int	msqid;
2493d903220SDoug Rabson 	int	cmd;
250b5d5c0c9SPeter Wemm 	struct	msqid_ds *buf;
2513d903220SDoug Rabson };
252b5d5c0c9SPeter Wemm #endif
2533d903220SDoug Rabson 
254b5d5c0c9SPeter Wemm int
255cb226aaaSPoul-Henning Kamp msgctl(p, uap)
2563d903220SDoug Rabson 	struct proc *p;
2573d903220SDoug Rabson 	register struct msgctl_args *uap;
2583d903220SDoug Rabson {
2593d903220SDoug Rabson 	int msqid = uap->msqid;
2603d903220SDoug Rabson 	int cmd = uap->cmd;
261b5d5c0c9SPeter Wemm 	struct msqid_ds *user_msqptr = uap->buf;
262797f2d22SPoul-Henning Kamp 	int rval, eval;
2633d903220SDoug Rabson 	struct msqid_ds msqbuf;
2643d903220SDoug Rabson 	register struct msqid_ds *msqptr;
2653d903220SDoug Rabson 
2663d903220SDoug Rabson #ifdef MSG_DEBUG_OK
2673d903220SDoug Rabson 	printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
2683d903220SDoug Rabson #endif
2693d903220SDoug Rabson 
270cb1f0db9SRobert Watson 	if (!jail_sysvipc_allowed && p->p_prison != NULL)
271cb1f0db9SRobert Watson 		return (ENOSYS);
272cb1f0db9SRobert Watson 
2733d903220SDoug Rabson 	msqid = IPCID_TO_IX(msqid);
2743d903220SDoug Rabson 
2753d903220SDoug Rabson 	if (msqid < 0 || msqid >= msginfo.msgmni) {
2763d903220SDoug Rabson #ifdef MSG_DEBUG_OK
2773d903220SDoug Rabson 		printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
2783d903220SDoug Rabson 		    msginfo.msgmni);
2793d903220SDoug Rabson #endif
2803d903220SDoug Rabson 		return(EINVAL);
2813d903220SDoug Rabson 	}
2823d903220SDoug Rabson 
2833d903220SDoug Rabson 	msqptr = &msqids[msqid];
2843d903220SDoug Rabson 
2853d903220SDoug Rabson 	if (msqptr->msg_qbytes == 0) {
2863d903220SDoug Rabson #ifdef MSG_DEBUG_OK
2873d903220SDoug Rabson 		printf("no such msqid\n");
2883d903220SDoug Rabson #endif
2893d903220SDoug Rabson 		return(EINVAL);
2903d903220SDoug Rabson 	}
2913d903220SDoug Rabson 	if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
2923d903220SDoug Rabson #ifdef MSG_DEBUG_OK
2933d903220SDoug Rabson 		printf("wrong sequence number\n");
2943d903220SDoug Rabson #endif
2953d903220SDoug Rabson 		return(EINVAL);
2963d903220SDoug Rabson 	}
2973d903220SDoug Rabson 
2983d903220SDoug Rabson 	eval = 0;
2993d903220SDoug Rabson 	rval = 0;
3003d903220SDoug Rabson 
3013d903220SDoug Rabson 	switch (cmd) {
3023d903220SDoug Rabson 
3033d903220SDoug Rabson 	case IPC_RMID:
3043d903220SDoug Rabson 	{
3053d903220SDoug Rabson 		struct msg *msghdr;
3061c308b81SPoul-Henning Kamp 		if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
3073d903220SDoug Rabson 			return(eval);
3083d903220SDoug Rabson 		/* Free the message headers */
3093d903220SDoug Rabson 		msghdr = msqptr->msg_first;
3103d903220SDoug Rabson 		while (msghdr != NULL) {
3113d903220SDoug Rabson 			struct msg *msghdr_tmp;
3123d903220SDoug Rabson 
3133d903220SDoug Rabson 			/* Free the segments of each message */
3143d903220SDoug Rabson 			msqptr->msg_cbytes -= msghdr->msg_ts;
3153d903220SDoug Rabson 			msqptr->msg_qnum--;
3163d903220SDoug Rabson 			msghdr_tmp = msghdr;
3173d903220SDoug Rabson 			msghdr = msghdr->msg_next;
3183d903220SDoug Rabson 			msg_freehdr(msghdr_tmp);
3193d903220SDoug Rabson 		}
3203d903220SDoug Rabson 
3213d903220SDoug Rabson 		if (msqptr->msg_cbytes != 0)
3223d903220SDoug Rabson 			panic("msg_cbytes is screwed up");
3233d903220SDoug Rabson 		if (msqptr->msg_qnum != 0)
3243d903220SDoug Rabson 			panic("msg_qnum is screwed up");
3253d903220SDoug Rabson 
3263d903220SDoug Rabson 		msqptr->msg_qbytes = 0;	/* Mark it as free */
3273d903220SDoug Rabson 
3283d903220SDoug Rabson 		wakeup((caddr_t)msqptr);
3293d903220SDoug Rabson 	}
3303d903220SDoug Rabson 
3313d903220SDoug Rabson 		break;
3323d903220SDoug Rabson 
3333d903220SDoug Rabson 	case IPC_SET:
3341c308b81SPoul-Henning Kamp 		if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
3353d903220SDoug Rabson 			return(eval);
3363d903220SDoug Rabson 		if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
3373d903220SDoug Rabson 			return(eval);
33857c90d6fSPoul-Henning Kamp 		if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
3391c308b81SPoul-Henning Kamp 			eval = suser(p);
34057c90d6fSPoul-Henning Kamp 			if (eval)
34157c90d6fSPoul-Henning Kamp 				return(eval);
34257c90d6fSPoul-Henning Kamp 		}
3433d903220SDoug Rabson 		if (msqbuf.msg_qbytes > msginfo.msgmnb) {
3443d903220SDoug Rabson #ifdef MSG_DEBUG_OK
3453d903220SDoug Rabson 			printf("can't increase msg_qbytes beyond %d (truncating)\n",
3463d903220SDoug Rabson 			    msginfo.msgmnb);
3473d903220SDoug Rabson #endif
3483d903220SDoug Rabson 			msqbuf.msg_qbytes = msginfo.msgmnb;	/* silently restrict qbytes to system limit */
3493d903220SDoug Rabson 		}
3503d903220SDoug Rabson 		if (msqbuf.msg_qbytes == 0) {
3513d903220SDoug Rabson #ifdef MSG_DEBUG_OK
3523d903220SDoug Rabson 			printf("can't reduce msg_qbytes to 0\n");
3533d903220SDoug Rabson #endif
3543d903220SDoug Rabson 			return(EINVAL);		/* non-standard errno! */
3553d903220SDoug Rabson 		}
3563d903220SDoug Rabson 		msqptr->msg_perm.uid = msqbuf.msg_perm.uid;	/* change the owner */
3573d903220SDoug Rabson 		msqptr->msg_perm.gid = msqbuf.msg_perm.gid;	/* change the owner */
3583d903220SDoug Rabson 		msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
3593d903220SDoug Rabson 		    (msqbuf.msg_perm.mode & 0777);
3603d903220SDoug Rabson 		msqptr->msg_qbytes = msqbuf.msg_qbytes;
361227ee8a1SPoul-Henning Kamp 		msqptr->msg_ctime = time_second;
3623d903220SDoug Rabson 		break;
3633d903220SDoug Rabson 
3643d903220SDoug Rabson 	case IPC_STAT:
3651c308b81SPoul-Henning Kamp 		if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
3663d903220SDoug Rabson #ifdef MSG_DEBUG_OK
3673d903220SDoug Rabson 			printf("requester doesn't have read access\n");
3683d903220SDoug Rabson #endif
3693d903220SDoug Rabson 			return(eval);
3703d903220SDoug Rabson 		}
3713d903220SDoug Rabson 		eval = copyout((caddr_t)msqptr, user_msqptr,
3723d903220SDoug Rabson 		    sizeof(struct msqid_ds));
3733d903220SDoug Rabson 		break;
3743d903220SDoug Rabson 
3753d903220SDoug Rabson 	default:
3763d903220SDoug Rabson #ifdef MSG_DEBUG_OK
3773d903220SDoug Rabson 		printf("invalid command %d\n", cmd);
3783d903220SDoug Rabson #endif
3793d903220SDoug Rabson 		return(EINVAL);
3803d903220SDoug Rabson 	}
3813d903220SDoug Rabson 
3823d903220SDoug Rabson 	if (eval == 0)
383cb226aaaSPoul-Henning Kamp 		p->p_retval[0] = rval;
3843d903220SDoug Rabson 	return(eval);
3853d903220SDoug Rabson }
3863d903220SDoug Rabson 
387b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_
3883d903220SDoug Rabson struct msgget_args {
3893d903220SDoug Rabson 	key_t	key;
3903d903220SDoug Rabson 	int	msgflg;
3913d903220SDoug Rabson };
392b5d5c0c9SPeter Wemm #endif
3933d903220SDoug Rabson 
394b5d5c0c9SPeter Wemm int
395cb226aaaSPoul-Henning Kamp msgget(p, uap)
3963d903220SDoug Rabson 	struct proc *p;
3973d903220SDoug Rabson 	register struct msgget_args *uap;
3983d903220SDoug Rabson {
3993d903220SDoug Rabson 	int msqid, eval;
4003d903220SDoug Rabson 	int key = uap->key;
4013d903220SDoug Rabson 	int msgflg = uap->msgflg;
4023d903220SDoug Rabson 	struct ucred *cred = p->p_ucred;
403789668e2SDavid Greenman 	register struct msqid_ds *msqptr = NULL;
4043d903220SDoug Rabson 
4053d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4063d903220SDoug Rabson 	printf("msgget(0x%x, 0%o)\n", key, msgflg);
4073d903220SDoug Rabson #endif
4083d903220SDoug Rabson 
409cb1f0db9SRobert Watson 	if (!jail_sysvipc_allowed && p->p_prison != NULL)
410cb1f0db9SRobert Watson 		return (ENOSYS);
411cb1f0db9SRobert Watson 
4123d903220SDoug Rabson 	if (key != IPC_PRIVATE) {
4133d903220SDoug Rabson 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
4143d903220SDoug Rabson 			msqptr = &msqids[msqid];
4153d903220SDoug Rabson 			if (msqptr->msg_qbytes != 0 &&
4163d903220SDoug Rabson 			    msqptr->msg_perm.key == key)
4173d903220SDoug Rabson 				break;
4183d903220SDoug Rabson 		}
4193d903220SDoug Rabson 		if (msqid < msginfo.msgmni) {
4203d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4213d903220SDoug Rabson 			printf("found public key\n");
4223d903220SDoug Rabson #endif
4233d903220SDoug Rabson 			if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
4243d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4253d903220SDoug Rabson 				printf("not exclusive\n");
4263d903220SDoug Rabson #endif
4273d903220SDoug Rabson 				return(EEXIST);
4283d903220SDoug Rabson 			}
4291c308b81SPoul-Henning Kamp 			if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) {
4303d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4313d903220SDoug Rabson 				printf("requester doesn't have 0%o access\n",
4323d903220SDoug Rabson 				    msgflg & 0700);
4333d903220SDoug Rabson #endif
4343d903220SDoug Rabson 				return(eval);
4353d903220SDoug Rabson 			}
4363d903220SDoug Rabson 			goto found;
4373d903220SDoug Rabson 		}
4383d903220SDoug Rabson 	}
4393d903220SDoug Rabson 
4403d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4413d903220SDoug Rabson 	printf("need to allocate the msqid_ds\n");
4423d903220SDoug Rabson #endif
4433d903220SDoug Rabson 	if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
4443d903220SDoug Rabson 		for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
4453d903220SDoug Rabson 			/*
4463d903220SDoug Rabson 			 * Look for an unallocated and unlocked msqid_ds.
4473d903220SDoug Rabson 			 * msqid_ds's can be locked by msgsnd or msgrcv while
4483d903220SDoug Rabson 			 * they are copying the message in/out.  We can't
4493d903220SDoug Rabson 			 * re-use the entry until they release it.
4503d903220SDoug Rabson 			 */
4513d903220SDoug Rabson 			msqptr = &msqids[msqid];
4523d903220SDoug Rabson 			if (msqptr->msg_qbytes == 0 &&
4533d903220SDoug Rabson 			    (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
4543d903220SDoug Rabson 				break;
4553d903220SDoug Rabson 		}
4563d903220SDoug Rabson 		if (msqid == msginfo.msgmni) {
4573d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4583d903220SDoug Rabson 			printf("no more msqid_ds's available\n");
4593d903220SDoug Rabson #endif
4603d903220SDoug Rabson 			return(ENOSPC);
4613d903220SDoug Rabson 		}
4623d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4633d903220SDoug Rabson 		printf("msqid %d is available\n", msqid);
4643d903220SDoug Rabson #endif
4653d903220SDoug Rabson 		msqptr->msg_perm.key = key;
4663d903220SDoug Rabson 		msqptr->msg_perm.cuid = cred->cr_uid;
4673d903220SDoug Rabson 		msqptr->msg_perm.uid = cred->cr_uid;
4683d903220SDoug Rabson 		msqptr->msg_perm.cgid = cred->cr_gid;
4693d903220SDoug Rabson 		msqptr->msg_perm.gid = cred->cr_gid;
4703d903220SDoug Rabson 		msqptr->msg_perm.mode = (msgflg & 0777);
4713d903220SDoug Rabson 		/* Make sure that the returned msqid is unique */
4723d903220SDoug Rabson 		msqptr->msg_perm.seq++;
4733d903220SDoug Rabson 		msqptr->msg_first = NULL;
4743d903220SDoug Rabson 		msqptr->msg_last = NULL;
4753d903220SDoug Rabson 		msqptr->msg_cbytes = 0;
4763d903220SDoug Rabson 		msqptr->msg_qnum = 0;
4773d903220SDoug Rabson 		msqptr->msg_qbytes = msginfo.msgmnb;
4783d903220SDoug Rabson 		msqptr->msg_lspid = 0;
4793d903220SDoug Rabson 		msqptr->msg_lrpid = 0;
4803d903220SDoug Rabson 		msqptr->msg_stime = 0;
4813d903220SDoug Rabson 		msqptr->msg_rtime = 0;
482227ee8a1SPoul-Henning Kamp 		msqptr->msg_ctime = time_second;
4833d903220SDoug Rabson 	} else {
4843d903220SDoug Rabson #ifdef MSG_DEBUG_OK
4853d903220SDoug Rabson 		printf("didn't find it and wasn't asked to create it\n");
4863d903220SDoug Rabson #endif
4873d903220SDoug Rabson 		return(ENOENT);
4883d903220SDoug Rabson 	}
4893d903220SDoug Rabson 
4903d903220SDoug Rabson found:
4913d903220SDoug Rabson 	/* Construct the unique msqid */
492cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
4933d903220SDoug Rabson 	return(0);
4943d903220SDoug Rabson }
4953d903220SDoug Rabson 
496b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_
4973d903220SDoug Rabson struct msgsnd_args {
4983d903220SDoug Rabson 	int	msqid;
499b5d5c0c9SPeter Wemm 	void	*msgp;
5003d903220SDoug Rabson 	size_t	msgsz;
5013d903220SDoug Rabson 	int	msgflg;
5023d903220SDoug Rabson };
503b5d5c0c9SPeter Wemm #endif
5043d903220SDoug Rabson 
505b5d5c0c9SPeter Wemm int
506cb226aaaSPoul-Henning Kamp msgsnd(p, uap)
5073d903220SDoug Rabson 	struct proc *p;
5083d903220SDoug Rabson 	register struct msgsnd_args *uap;
5093d903220SDoug Rabson {
5103d903220SDoug Rabson 	int msqid = uap->msqid;
511b5d5c0c9SPeter Wemm 	void *user_msgp = uap->msgp;
5123d903220SDoug Rabson 	size_t msgsz = uap->msgsz;
5133d903220SDoug Rabson 	int msgflg = uap->msgflg;
5143d903220SDoug Rabson 	int segs_needed, eval;
5153d903220SDoug Rabson 	register struct msqid_ds *msqptr;
5163d903220SDoug Rabson 	register struct msg *msghdr;
5173d903220SDoug Rabson 	short next;
5183d903220SDoug Rabson 
5193d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5203d903220SDoug Rabson 	printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
5213d903220SDoug Rabson 	    msgflg);
5223d903220SDoug Rabson #endif
5233d903220SDoug Rabson 
524cb1f0db9SRobert Watson 	if (!jail_sysvipc_allowed && p->p_prison != NULL)
525cb1f0db9SRobert Watson 		return (ENOSYS);
526cb1f0db9SRobert Watson 
5273d903220SDoug Rabson 	msqid = IPCID_TO_IX(msqid);
5283d903220SDoug Rabson 
5293d903220SDoug Rabson 	if (msqid < 0 || msqid >= msginfo.msgmni) {
5303d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5313d903220SDoug Rabson 		printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
5323d903220SDoug Rabson 		    msginfo.msgmni);
5333d903220SDoug Rabson #endif
5343d903220SDoug Rabson 		return(EINVAL);
5353d903220SDoug Rabson 	}
5363d903220SDoug Rabson 
5373d903220SDoug Rabson 	msqptr = &msqids[msqid];
5383d903220SDoug Rabson 	if (msqptr->msg_qbytes == 0) {
5393d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5403d903220SDoug Rabson 		printf("no such message queue id\n");
5413d903220SDoug Rabson #endif
5423d903220SDoug Rabson 		return(EINVAL);
5433d903220SDoug Rabson 	}
5443d903220SDoug Rabson 	if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
5453d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5463d903220SDoug Rabson 		printf("wrong sequence number\n");
5473d903220SDoug Rabson #endif
5483d903220SDoug Rabson 		return(EINVAL);
5493d903220SDoug Rabson 	}
5503d903220SDoug Rabson 
5511c308b81SPoul-Henning Kamp 	if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) {
5523d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5533d903220SDoug Rabson 		printf("requester doesn't have write access\n");
5543d903220SDoug Rabson #endif
5553d903220SDoug Rabson 		return(eval);
5563d903220SDoug Rabson 	}
5573d903220SDoug Rabson 
5583d903220SDoug Rabson 	segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
5593d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5603d903220SDoug Rabson 	printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
5613d903220SDoug Rabson 	    segs_needed);
5623d903220SDoug Rabson #endif
5633d903220SDoug Rabson 	for (;;) {
5643d903220SDoug Rabson 		int need_more_resources = 0;
5653d903220SDoug Rabson 
5663d903220SDoug Rabson 		/*
5673d903220SDoug Rabson 		 * check msgsz
5683d903220SDoug Rabson 		 * (inside this loop in case msg_qbytes changes while we sleep)
5693d903220SDoug Rabson 		 */
5703d903220SDoug Rabson 
571789668e2SDavid Greenman 		if (msgsz > msqptr->msg_qbytes) {
5723d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5733d903220SDoug Rabson 			printf("msgsz > msqptr->msg_qbytes\n");
5743d903220SDoug Rabson #endif
5753d903220SDoug Rabson 			return(EINVAL);
5763d903220SDoug Rabson 		}
5773d903220SDoug Rabson 
5783d903220SDoug Rabson 		if (msqptr->msg_perm.mode & MSG_LOCKED) {
5793d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5803d903220SDoug Rabson 			printf("msqid is locked\n");
5813d903220SDoug Rabson #endif
5823d903220SDoug Rabson 			need_more_resources = 1;
5833d903220SDoug Rabson 		}
5843d903220SDoug Rabson 		if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
5853d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5863d903220SDoug Rabson 			printf("msgsz + msg_cbytes > msg_qbytes\n");
5873d903220SDoug Rabson #endif
5883d903220SDoug Rabson 			need_more_resources = 1;
5893d903220SDoug Rabson 		}
5903d903220SDoug Rabson 		if (segs_needed > nfree_msgmaps) {
5913d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5923d903220SDoug Rabson 			printf("segs_needed > nfree_msgmaps\n");
5933d903220SDoug Rabson #endif
5943d903220SDoug Rabson 			need_more_resources = 1;
5953d903220SDoug Rabson 		}
5963d903220SDoug Rabson 		if (free_msghdrs == NULL) {
5973d903220SDoug Rabson #ifdef MSG_DEBUG_OK
5983d903220SDoug Rabson 			printf("no more msghdrs\n");
5993d903220SDoug Rabson #endif
6003d903220SDoug Rabson 			need_more_resources = 1;
6013d903220SDoug Rabson 		}
6023d903220SDoug Rabson 
6033d903220SDoug Rabson 		if (need_more_resources) {
6043d903220SDoug Rabson 			int we_own_it;
6053d903220SDoug Rabson 
6063d903220SDoug Rabson 			if ((msgflg & IPC_NOWAIT) != 0) {
6073d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6083d903220SDoug Rabson 				printf("need more resources but caller doesn't want to wait\n");
6093d903220SDoug Rabson #endif
6103d903220SDoug Rabson 				return(EAGAIN);
6113d903220SDoug Rabson 			}
6123d903220SDoug Rabson 
6133d903220SDoug Rabson 			if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
6143d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6153d903220SDoug Rabson 				printf("we don't own the msqid_ds\n");
6163d903220SDoug Rabson #endif
6173d903220SDoug Rabson 				we_own_it = 0;
6183d903220SDoug Rabson 			} else {
6193d903220SDoug Rabson 				/* Force later arrivals to wait for our
6203d903220SDoug Rabson 				   request */
6213d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6223d903220SDoug Rabson 				printf("we own the msqid_ds\n");
6233d903220SDoug Rabson #endif
6243d903220SDoug Rabson 				msqptr->msg_perm.mode |= MSG_LOCKED;
6253d903220SDoug Rabson 				we_own_it = 1;
6263d903220SDoug Rabson 			}
6273d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6283d903220SDoug Rabson 			printf("goodnight\n");
6293d903220SDoug Rabson #endif
6303d903220SDoug Rabson 			eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
6313d903220SDoug Rabson 			    "msgwait", 0);
6323d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6333d903220SDoug Rabson 			printf("good morning, eval=%d\n", eval);
6343d903220SDoug Rabson #endif
6353d903220SDoug Rabson 			if (we_own_it)
6363d903220SDoug Rabson 				msqptr->msg_perm.mode &= ~MSG_LOCKED;
6373d903220SDoug Rabson 			if (eval != 0) {
6383d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6393d903220SDoug Rabson 				printf("msgsnd:  interrupted system call\n");
6403d903220SDoug Rabson #endif
6413d903220SDoug Rabson 				return(EINTR);
6423d903220SDoug Rabson 			}
6433d903220SDoug Rabson 
6443d903220SDoug Rabson 			/*
6453d903220SDoug Rabson 			 * Make sure that the msq queue still exists
6463d903220SDoug Rabson 			 */
6473d903220SDoug Rabson 
6483d903220SDoug Rabson 			if (msqptr->msg_qbytes == 0) {
6493d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6503d903220SDoug Rabson 				printf("msqid deleted\n");
6513d903220SDoug Rabson #endif
6523d903220SDoug Rabson 				return(EIDRM);
6533d903220SDoug Rabson 			}
6543d903220SDoug Rabson 
6553d903220SDoug Rabson 		} else {
6563d903220SDoug Rabson #ifdef MSG_DEBUG_OK
6573d903220SDoug Rabson 			printf("got all the resources that we need\n");
6583d903220SDoug Rabson #endif
6593d903220SDoug Rabson 			break;
6603d903220SDoug Rabson 		}
6613d903220SDoug Rabson 	}
6623d903220SDoug Rabson 
6633d903220SDoug Rabson 	/*
6643d903220SDoug Rabson 	 * We have the resources that we need.
6653d903220SDoug Rabson 	 * Make sure!
6663d903220SDoug Rabson 	 */
6673d903220SDoug Rabson 
6683d903220SDoug Rabson 	if (msqptr->msg_perm.mode & MSG_LOCKED)
6693d903220SDoug Rabson 		panic("msg_perm.mode & MSG_LOCKED");
6703d903220SDoug Rabson 	if (segs_needed > nfree_msgmaps)
6713d903220SDoug Rabson 		panic("segs_needed > nfree_msgmaps");
6723d903220SDoug Rabson 	if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
6733d903220SDoug Rabson 		panic("msgsz + msg_cbytes > msg_qbytes");
6743d903220SDoug Rabson 	if (free_msghdrs == NULL)
6753d903220SDoug Rabson 		panic("no more msghdrs");
6763d903220SDoug Rabson 
6773d903220SDoug Rabson 	/*
6783d903220SDoug Rabson 	 * Re-lock the msqid_ds in case we page-fault when copying in the
6793d903220SDoug Rabson 	 * message
6803d903220SDoug Rabson 	 */
6813d903220SDoug Rabson 
6823d903220SDoug Rabson 	if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
6833d903220SDoug Rabson 		panic("msqid_ds is already locked");
6843d903220SDoug Rabson 	msqptr->msg_perm.mode |= MSG_LOCKED;
6853d903220SDoug Rabson 
6863d903220SDoug Rabson 	/*
6873d903220SDoug Rabson 	 * Allocate a message header
6883d903220SDoug Rabson 	 */
6893d903220SDoug Rabson 
6903d903220SDoug Rabson 	msghdr = free_msghdrs;
6913d903220SDoug Rabson 	free_msghdrs = msghdr->msg_next;
6923d903220SDoug Rabson 	msghdr->msg_spot = -1;
6933d903220SDoug Rabson 	msghdr->msg_ts = msgsz;
6943d903220SDoug Rabson 
6953d903220SDoug Rabson 	/*
6963d903220SDoug Rabson 	 * Allocate space for the message
6973d903220SDoug Rabson 	 */
6983d903220SDoug Rabson 
6993d903220SDoug Rabson 	while (segs_needed > 0) {
7003d903220SDoug Rabson 		if (nfree_msgmaps <= 0)
7013d903220SDoug Rabson 			panic("not enough msgmaps");
7023d903220SDoug Rabson 		if (free_msgmaps == -1)
7033d903220SDoug Rabson 			panic("nil free_msgmaps");
7043d903220SDoug Rabson 		next = free_msgmaps;
7053d903220SDoug Rabson 		if (next <= -1)
7063d903220SDoug Rabson 			panic("next too low #1");
7073d903220SDoug Rabson 		if (next >= msginfo.msgseg)
7083d903220SDoug Rabson 			panic("next out of range #1");
7093d903220SDoug Rabson #ifdef MSG_DEBUG_OK
7103d903220SDoug Rabson 		printf("allocating segment %d to message\n", next);
7113d903220SDoug Rabson #endif
7123d903220SDoug Rabson 		free_msgmaps = msgmaps[next].next;
7133d903220SDoug Rabson 		nfree_msgmaps--;
7143d903220SDoug Rabson 		msgmaps[next].next = msghdr->msg_spot;
7153d903220SDoug Rabson 		msghdr->msg_spot = next;
7163d903220SDoug Rabson 		segs_needed--;
7173d903220SDoug Rabson 	}
7183d903220SDoug Rabson 
7193d903220SDoug Rabson 	/*
7203d903220SDoug Rabson 	 * Copy in the message type
7213d903220SDoug Rabson 	 */
7223d903220SDoug Rabson 
7233d903220SDoug Rabson 	if ((eval = copyin(user_msgp, &msghdr->msg_type,
7243d903220SDoug Rabson 	    sizeof(msghdr->msg_type))) != 0) {
7253d903220SDoug Rabson #ifdef MSG_DEBUG_OK
7263d903220SDoug Rabson 		printf("error %d copying the message type\n", eval);
7273d903220SDoug Rabson #endif
7283d903220SDoug Rabson 		msg_freehdr(msghdr);
7293d903220SDoug Rabson 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
7303d903220SDoug Rabson 		wakeup((caddr_t)msqptr);
7313d903220SDoug Rabson 		return(eval);
7323d903220SDoug Rabson 	}
73309a8dfa2SBruce Evans 	user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
7343d903220SDoug Rabson 
7353d903220SDoug Rabson 	/*
7363d903220SDoug Rabson 	 * Validate the message type
7373d903220SDoug Rabson 	 */
7383d903220SDoug Rabson 
7393d903220SDoug Rabson 	if (msghdr->msg_type < 1) {
7403d903220SDoug Rabson 		msg_freehdr(msghdr);
7413d903220SDoug Rabson 		msqptr->msg_perm.mode &= ~MSG_LOCKED;
7423d903220SDoug Rabson 		wakeup((caddr_t)msqptr);
7433d903220SDoug Rabson #ifdef MSG_DEBUG_OK
7443d903220SDoug Rabson 		printf("mtype (%d) < 1\n", msghdr->msg_type);
7453d903220SDoug Rabson #endif
7463d903220SDoug Rabson 		return(EINVAL);
7473d903220SDoug Rabson 	}
7483d903220SDoug Rabson 
7493d903220SDoug Rabson 	/*
7503d903220SDoug Rabson 	 * Copy in the message body
7513d903220SDoug Rabson 	 */
7523d903220SDoug Rabson 
7533d903220SDoug Rabson 	next = msghdr->msg_spot;
7543d903220SDoug Rabson 	while (msgsz > 0) {
7553d903220SDoug Rabson 		size_t tlen;
7563d903220SDoug Rabson 		if (msgsz > msginfo.msgssz)
7573d903220SDoug Rabson 			tlen = msginfo.msgssz;
7583d903220SDoug Rabson 		else
7593d903220SDoug Rabson 			tlen = msgsz;
7603d903220SDoug Rabson 		if (next <= -1)
7613d903220SDoug Rabson 			panic("next too low #2");
7623d903220SDoug Rabson 		if (next >= msginfo.msgseg)
7633d903220SDoug Rabson 			panic("next out of range #2");
7643d903220SDoug Rabson 		if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
7653d903220SDoug Rabson 		    tlen)) != 0) {
7663d903220SDoug Rabson #ifdef MSG_DEBUG_OK
7673d903220SDoug Rabson 			printf("error %d copying in message segment\n", eval);
7683d903220SDoug Rabson #endif
7693d903220SDoug Rabson 			msg_freehdr(msghdr);
7703d903220SDoug Rabson 			msqptr->msg_perm.mode &= ~MSG_LOCKED;
7713d903220SDoug Rabson 			wakeup((caddr_t)msqptr);
7723d903220SDoug Rabson 			return(eval);
7733d903220SDoug Rabson 		}
7743d903220SDoug Rabson 		msgsz -= tlen;
77509a8dfa2SBruce Evans 		user_msgp = (char *)user_msgp + tlen;
7763d903220SDoug Rabson 		next = msgmaps[next].next;
7773d903220SDoug Rabson 	}
7783d903220SDoug Rabson 	if (next != -1)
7793d903220SDoug Rabson 		panic("didn't use all the msg segments");
7803d903220SDoug Rabson 
7813d903220SDoug Rabson 	/*
7823d903220SDoug Rabson 	 * We've got the message.  Unlock the msqid_ds.
7833d903220SDoug Rabson 	 */
7843d903220SDoug Rabson 
7853d903220SDoug Rabson 	msqptr->msg_perm.mode &= ~MSG_LOCKED;
7863d903220SDoug Rabson 
7873d903220SDoug Rabson 	/*
7883d903220SDoug Rabson 	 * Make sure that the msqid_ds is still allocated.
7893d903220SDoug Rabson 	 */
7903d903220SDoug Rabson 
7913d903220SDoug Rabson 	if (msqptr->msg_qbytes == 0) {
7923d903220SDoug Rabson 		msg_freehdr(msghdr);
7933d903220SDoug Rabson 		wakeup((caddr_t)msqptr);
7943d903220SDoug Rabson 		return(EIDRM);
7953d903220SDoug Rabson 	}
7963d903220SDoug Rabson 
7973d903220SDoug Rabson 	/*
7983d903220SDoug Rabson 	 * Put the message into the queue
7993d903220SDoug Rabson 	 */
8003d903220SDoug Rabson 
8013d903220SDoug Rabson 	if (msqptr->msg_first == NULL) {
8023d903220SDoug Rabson 		msqptr->msg_first = msghdr;
8033d903220SDoug Rabson 		msqptr->msg_last = msghdr;
8043d903220SDoug Rabson 	} else {
8053d903220SDoug Rabson 		msqptr->msg_last->msg_next = msghdr;
8063d903220SDoug Rabson 		msqptr->msg_last = msghdr;
8073d903220SDoug Rabson 	}
8083d903220SDoug Rabson 	msqptr->msg_last->msg_next = NULL;
8093d903220SDoug Rabson 
8103d903220SDoug Rabson 	msqptr->msg_cbytes += msghdr->msg_ts;
8113d903220SDoug Rabson 	msqptr->msg_qnum++;
8123d903220SDoug Rabson 	msqptr->msg_lspid = p->p_pid;
813227ee8a1SPoul-Henning Kamp 	msqptr->msg_stime = time_second;
8143d903220SDoug Rabson 
8153d903220SDoug Rabson 	wakeup((caddr_t)msqptr);
816cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = 0;
8173d903220SDoug Rabson 	return(0);
8183d903220SDoug Rabson }
8193d903220SDoug Rabson 
820b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_
8213d903220SDoug Rabson struct msgrcv_args {
8223d903220SDoug Rabson 	int	msqid;
8233d903220SDoug Rabson 	void	*msgp;
8243d903220SDoug Rabson 	size_t	msgsz;
8253d903220SDoug Rabson 	long	msgtyp;
8263d903220SDoug Rabson 	int	msgflg;
8273d903220SDoug Rabson };
828b5d5c0c9SPeter Wemm #endif
8293d903220SDoug Rabson 
830b5d5c0c9SPeter Wemm int
831cb226aaaSPoul-Henning Kamp msgrcv(p, uap)
8323d903220SDoug Rabson 	struct proc *p;
8333d903220SDoug Rabson 	register struct msgrcv_args *uap;
8343d903220SDoug Rabson {
8353d903220SDoug Rabson 	int msqid = uap->msqid;
8363d903220SDoug Rabson 	void *user_msgp = uap->msgp;
8373d903220SDoug Rabson 	size_t msgsz = uap->msgsz;
8383d903220SDoug Rabson 	long msgtyp = uap->msgtyp;
8393d903220SDoug Rabson 	int msgflg = uap->msgflg;
8403d903220SDoug Rabson 	size_t len;
8413d903220SDoug Rabson 	register struct msqid_ds *msqptr;
8423d903220SDoug Rabson 	register struct msg *msghdr;
8433d903220SDoug Rabson 	int eval;
8443d903220SDoug Rabson 	short next;
8453d903220SDoug Rabson 
8463d903220SDoug Rabson #ifdef MSG_DEBUG_OK
8473d903220SDoug Rabson 	printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
8483d903220SDoug Rabson 	    msgsz, msgtyp, msgflg);
8493d903220SDoug Rabson #endif
8503d903220SDoug Rabson 
851cb1f0db9SRobert Watson 	if (!jail_sysvipc_allowed && p->p_prison != NULL)
852cb1f0db9SRobert Watson 		return (ENOSYS);
853cb1f0db9SRobert Watson 
8543d903220SDoug Rabson 	msqid = IPCID_TO_IX(msqid);
8553d903220SDoug Rabson 
8563d903220SDoug Rabson 	if (msqid < 0 || msqid >= msginfo.msgmni) {
8573d903220SDoug Rabson #ifdef MSG_DEBUG_OK
8583d903220SDoug Rabson 		printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
8593d903220SDoug Rabson 		    msginfo.msgmni);
8603d903220SDoug Rabson #endif
8613d903220SDoug Rabson 		return(EINVAL);
8623d903220SDoug Rabson 	}
8633d903220SDoug Rabson 
8643d903220SDoug Rabson 	msqptr = &msqids[msqid];
8653d903220SDoug Rabson 	if (msqptr->msg_qbytes == 0) {
8663d903220SDoug Rabson #ifdef MSG_DEBUG_OK
8673d903220SDoug Rabson 		printf("no such message queue id\n");
8683d903220SDoug Rabson #endif
8693d903220SDoug Rabson 		return(EINVAL);
8703d903220SDoug Rabson 	}
8713d903220SDoug Rabson 	if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
8723d903220SDoug Rabson #ifdef MSG_DEBUG_OK
8733d903220SDoug Rabson 		printf("wrong sequence number\n");
8743d903220SDoug Rabson #endif
8753d903220SDoug Rabson 		return(EINVAL);
8763d903220SDoug Rabson 	}
8773d903220SDoug Rabson 
8781c308b81SPoul-Henning Kamp 	if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
8793d903220SDoug Rabson #ifdef MSG_DEBUG_OK
8803d903220SDoug Rabson 		printf("requester doesn't have read access\n");
8813d903220SDoug Rabson #endif
8823d903220SDoug Rabson 		return(eval);
8833d903220SDoug Rabson 	}
8843d903220SDoug Rabson 
8853d903220SDoug Rabson 	msghdr = NULL;
8863d903220SDoug Rabson 	while (msghdr == NULL) {
8873d903220SDoug Rabson 		if (msgtyp == 0) {
8883d903220SDoug Rabson 			msghdr = msqptr->msg_first;
8893d903220SDoug Rabson 			if (msghdr != NULL) {
8903d903220SDoug Rabson 				if (msgsz < msghdr->msg_ts &&
8913d903220SDoug Rabson 				    (msgflg & MSG_NOERROR) == 0) {
8923d903220SDoug Rabson #ifdef MSG_DEBUG_OK
8933d903220SDoug Rabson 					printf("first message on the queue is too big (want %d, got %d)\n",
8943d903220SDoug Rabson 					    msgsz, msghdr->msg_ts);
8953d903220SDoug Rabson #endif
8963d903220SDoug Rabson 					return(E2BIG);
8973d903220SDoug Rabson 				}
8983d903220SDoug Rabson 				if (msqptr->msg_first == msqptr->msg_last) {
8993d903220SDoug Rabson 					msqptr->msg_first = NULL;
9003d903220SDoug Rabson 					msqptr->msg_last = NULL;
9013d903220SDoug Rabson 				} else {
9023d903220SDoug Rabson 					msqptr->msg_first = msghdr->msg_next;
9033d903220SDoug Rabson 					if (msqptr->msg_first == NULL)
9043d903220SDoug Rabson 						panic("msg_first/last screwed up #1");
9053d903220SDoug Rabson 				}
9063d903220SDoug Rabson 			}
9073d903220SDoug Rabson 		} else {
9083d903220SDoug Rabson 			struct msg *previous;
9093d903220SDoug Rabson 			struct msg **prev;
9103d903220SDoug Rabson 
9113d903220SDoug Rabson 			previous = NULL;
9123d903220SDoug Rabson 			prev = &(msqptr->msg_first);
9133d903220SDoug Rabson 			while ((msghdr = *prev) != NULL) {
9143d903220SDoug Rabson 				/*
9153d903220SDoug Rabson 				 * Is this message's type an exact match or is
9163d903220SDoug Rabson 				 * this message's type less than or equal to
9173d903220SDoug Rabson 				 * the absolute value of a negative msgtyp?
9183d903220SDoug Rabson 				 * Note that the second half of this test can
9193d903220SDoug Rabson 				 * NEVER be true if msgtyp is positive since
9203d903220SDoug Rabson 				 * msg_type is always positive!
9213d903220SDoug Rabson 				 */
9223d903220SDoug Rabson 
9233d903220SDoug Rabson 				if (msgtyp == msghdr->msg_type ||
9243d903220SDoug Rabson 				    msghdr->msg_type <= -msgtyp) {
9253d903220SDoug Rabson #ifdef MSG_DEBUG_OK
9263d903220SDoug Rabson 					printf("found message type %d, requested %d\n",
9273d903220SDoug Rabson 					    msghdr->msg_type, msgtyp);
9283d903220SDoug Rabson #endif
9293d903220SDoug Rabson 					if (msgsz < msghdr->msg_ts &&
9303d903220SDoug Rabson 					    (msgflg & MSG_NOERROR) == 0) {
9313d903220SDoug Rabson #ifdef MSG_DEBUG_OK
9323d903220SDoug Rabson 						printf("requested message on the queue is too big (want %d, got %d)\n",
9333d903220SDoug Rabson 						    msgsz, msghdr->msg_ts);
9343d903220SDoug Rabson #endif
9353d903220SDoug Rabson 						return(E2BIG);
9363d903220SDoug Rabson 					}
9373d903220SDoug Rabson 					*prev = msghdr->msg_next;
9383d903220SDoug Rabson 					if (msghdr == msqptr->msg_last) {
9393d903220SDoug Rabson 						if (previous == NULL) {
9403d903220SDoug Rabson 							if (prev !=
9413d903220SDoug Rabson 							    &msqptr->msg_first)
9423d903220SDoug Rabson 								panic("msg_first/last screwed up #2");
9433d903220SDoug Rabson 							msqptr->msg_first =
9443d903220SDoug Rabson 							    NULL;
9453d903220SDoug Rabson 							msqptr->msg_last =
9463d903220SDoug Rabson 							    NULL;
9473d903220SDoug Rabson 						} else {
9483d903220SDoug Rabson 							if (prev ==
9493d903220SDoug Rabson 							    &msqptr->msg_first)
9503d903220SDoug Rabson 								panic("msg_first/last screwed up #3");
9513d903220SDoug Rabson 							msqptr->msg_last =
9523d903220SDoug Rabson 							    previous;
9533d903220SDoug Rabson 						}
9543d903220SDoug Rabson 					}
9553d903220SDoug Rabson 					break;
9563d903220SDoug Rabson 				}
9573d903220SDoug Rabson 				previous = msghdr;
9583d903220SDoug Rabson 				prev = &(msghdr->msg_next);
9593d903220SDoug Rabson 			}
9603d903220SDoug Rabson 		}
9613d903220SDoug Rabson 
9623d903220SDoug Rabson 		/*
9633d903220SDoug Rabson 		 * We've either extracted the msghdr for the appropriate
9643d903220SDoug Rabson 		 * message or there isn't one.
9653d903220SDoug Rabson 		 * If there is one then bail out of this loop.
9663d903220SDoug Rabson 		 */
9673d903220SDoug Rabson 
9683d903220SDoug Rabson 		if (msghdr != NULL)
9693d903220SDoug Rabson 			break;
9703d903220SDoug Rabson 
9713d903220SDoug Rabson 		/*
9723d903220SDoug Rabson 		 * Hmph!  No message found.  Does the user want to wait?
9733d903220SDoug Rabson 		 */
9743d903220SDoug Rabson 
9753d903220SDoug Rabson 		if ((msgflg & IPC_NOWAIT) != 0) {
9763d903220SDoug Rabson #ifdef MSG_DEBUG_OK
9773d903220SDoug Rabson 			printf("no appropriate message found (msgtyp=%d)\n",
9783d903220SDoug Rabson 			    msgtyp);
9793d903220SDoug Rabson #endif
9803d903220SDoug Rabson 			/* The SVID says to return ENOMSG. */
9813d903220SDoug Rabson #ifdef ENOMSG
9823d903220SDoug Rabson 			return(ENOMSG);
9833d903220SDoug Rabson #else
9843d903220SDoug Rabson 			/* Unfortunately, BSD doesn't define that code yet! */
9853d903220SDoug Rabson 			return(EAGAIN);
9863d903220SDoug Rabson #endif
9873d903220SDoug Rabson 		}
9883d903220SDoug Rabson 
9893d903220SDoug Rabson 		/*
9903d903220SDoug Rabson 		 * Wait for something to happen
9913d903220SDoug Rabson 		 */
9923d903220SDoug Rabson 
9933d903220SDoug Rabson #ifdef MSG_DEBUG_OK
9943d903220SDoug Rabson 		printf("msgrcv:  goodnight\n");
9953d903220SDoug Rabson #endif
9963d903220SDoug Rabson 		eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
9973d903220SDoug Rabson 		    0);
9983d903220SDoug Rabson #ifdef MSG_DEBUG_OK
9993d903220SDoug Rabson 		printf("msgrcv:  good morning (eval=%d)\n", eval);
10003d903220SDoug Rabson #endif
10013d903220SDoug Rabson 
10023d903220SDoug Rabson 		if (eval != 0) {
10033d903220SDoug Rabson #ifdef MSG_DEBUG_OK
10043d903220SDoug Rabson 			printf("msgsnd:  interrupted system call\n");
10053d903220SDoug Rabson #endif
10063d903220SDoug Rabson 			return(EINTR);
10073d903220SDoug Rabson 		}
10083d903220SDoug Rabson 
10093d903220SDoug Rabson 		/*
10103d903220SDoug Rabson 		 * Make sure that the msq queue still exists
10113d903220SDoug Rabson 		 */
10123d903220SDoug Rabson 
10133d903220SDoug Rabson 		if (msqptr->msg_qbytes == 0 ||
10143d903220SDoug Rabson 		    msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
10153d903220SDoug Rabson #ifdef MSG_DEBUG_OK
10163d903220SDoug Rabson 			printf("msqid deleted\n");
10173d903220SDoug Rabson #endif
10183d903220SDoug Rabson 			return(EIDRM);
10193d903220SDoug Rabson 		}
10203d903220SDoug Rabson 	}
10213d903220SDoug Rabson 
10223d903220SDoug Rabson 	/*
10233d903220SDoug Rabson 	 * Return the message to the user.
10243d903220SDoug Rabson 	 *
10253d903220SDoug Rabson 	 * First, do the bookkeeping (before we risk being interrupted).
10263d903220SDoug Rabson 	 */
10273d903220SDoug Rabson 
10283d903220SDoug Rabson 	msqptr->msg_cbytes -= msghdr->msg_ts;
10293d903220SDoug Rabson 	msqptr->msg_qnum--;
10303d903220SDoug Rabson 	msqptr->msg_lrpid = p->p_pid;
1031227ee8a1SPoul-Henning Kamp 	msqptr->msg_rtime = time_second;
10323d903220SDoug Rabson 
10333d903220SDoug Rabson 	/*
10343d903220SDoug Rabson 	 * Make msgsz the actual amount that we'll be returning.
10353d903220SDoug Rabson 	 * Note that this effectively truncates the message if it is too long
10363d903220SDoug Rabson 	 * (since msgsz is never increased).
10373d903220SDoug Rabson 	 */
10383d903220SDoug Rabson 
10393d903220SDoug Rabson #ifdef MSG_DEBUG_OK
10403d903220SDoug Rabson 	printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
10413d903220SDoug Rabson 	    msghdr->msg_ts);
10423d903220SDoug Rabson #endif
10433d903220SDoug Rabson 	if (msgsz > msghdr->msg_ts)
10443d903220SDoug Rabson 		msgsz = msghdr->msg_ts;
10453d903220SDoug Rabson 
10463d903220SDoug Rabson 	/*
10473d903220SDoug Rabson 	 * Return the type to the user.
10483d903220SDoug Rabson 	 */
10493d903220SDoug Rabson 
10503d903220SDoug Rabson 	eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
10513d903220SDoug Rabson 	    sizeof(msghdr->msg_type));
10523d903220SDoug Rabson 	if (eval != 0) {
10533d903220SDoug Rabson #ifdef MSG_DEBUG_OK
10543d903220SDoug Rabson 		printf("error (%d) copying out message type\n", eval);
10553d903220SDoug Rabson #endif
10563d903220SDoug Rabson 		msg_freehdr(msghdr);
10573d903220SDoug Rabson 		wakeup((caddr_t)msqptr);
10583d903220SDoug Rabson 		return(eval);
10593d903220SDoug Rabson 	}
106009a8dfa2SBruce Evans 	user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
10613d903220SDoug Rabson 
10623d903220SDoug Rabson 	/*
10633d903220SDoug Rabson 	 * Return the segments to the user
10643d903220SDoug Rabson 	 */
10653d903220SDoug Rabson 
10663d903220SDoug Rabson 	next = msghdr->msg_spot;
10673d903220SDoug Rabson 	for (len = 0; len < msgsz; len += msginfo.msgssz) {
10683d903220SDoug Rabson 		size_t tlen;
10693d903220SDoug Rabson 
1070565592bdSSADA Kenji 		if (msgsz - len > msginfo.msgssz)
10713d903220SDoug Rabson 			tlen = msginfo.msgssz;
10723d903220SDoug Rabson 		else
1073565592bdSSADA Kenji 			tlen = msgsz - len;
10743d903220SDoug Rabson 		if (next <= -1)
10753d903220SDoug Rabson 			panic("next too low #3");
10763d903220SDoug Rabson 		if (next >= msginfo.msgseg)
10773d903220SDoug Rabson 			panic("next out of range #3");
10783d903220SDoug Rabson 		eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
10793d903220SDoug Rabson 		    user_msgp, tlen);
10803d903220SDoug Rabson 		if (eval != 0) {
10813d903220SDoug Rabson #ifdef MSG_DEBUG_OK
10823d903220SDoug Rabson 			printf("error (%d) copying out message segment\n",
10833d903220SDoug Rabson 			    eval);
10843d903220SDoug Rabson #endif
10853d903220SDoug Rabson 			msg_freehdr(msghdr);
10863d903220SDoug Rabson 			wakeup((caddr_t)msqptr);
10873d903220SDoug Rabson 			return(eval);
10883d903220SDoug Rabson 		}
108909a8dfa2SBruce Evans 		user_msgp = (char *)user_msgp + tlen;
10903d903220SDoug Rabson 		next = msgmaps[next].next;
10913d903220SDoug Rabson 	}
10923d903220SDoug Rabson 
10933d903220SDoug Rabson 	/*
10943d903220SDoug Rabson 	 * Done, return the actual number of bytes copied out.
10953d903220SDoug Rabson 	 */
10963d903220SDoug Rabson 
10973d903220SDoug Rabson 	msg_freehdr(msghdr);
10983d903220SDoug Rabson 	wakeup((caddr_t)msqptr);
1099cb226aaaSPoul-Henning Kamp 	p->p_retval[0] = msgsz;
11003d903220SDoug Rabson 	return(0);
11013d903220SDoug Rabson }
1102