/*
 * $Header: /smsa/bsd:include/sys/msg.h:bsd  1.6  $
 * $Source: /smsa/bsd:include/sys/msg.h: $
 *
 * Copyright (c) 1988 Acorn Computers Ltd., Cambridge, England
 *
 */

#ifndef	__sys_msg_h
#define	__sys_msg_h

/*
**	IPC Message Facility.
*/

#ifndef __sys_types_h
#include "types.h"
#endif
#ifndef	__sys_ipc_h
#include "ipc.h"
#endif

/*
**	Implementation Constants and system limits.
*/

#define	NMSQID		(5)		/* Max no. of msg queues in system */
#define NMESSAGES	(5)		/* Max no. of messages in system */
#define NMSQBYTES	(1024)		/* Max bytes on one message queue */
#define MSGMAXBYTES	(512)		/* Max bytes in one message */
#ifdef	_KERNEL
#define	PMSG		(PZERO + 2)	/* message facility sleep priority */
#endif

/*
**	Permission Definitions.
*/

#define	MSG_R	0400	/* read permission */
#define	MSG_W	0200	/* write permission */

/*
**	ipc_perm Mode Definitions.
*/

#define	MSG_RWAIT	01000	/* a reader is waiting for a message */
#define	MSG_WWAIT	02000	/* a writer is waiting to send */

/*
**	Message Operation Flags.
*/

#define	MSG_NOERROR	010000	/* no error if big message */

/*
**	Structure Definitions.
*/

/*
**	There is one msg queue id data structure for each q in the system.
*/

struct msqid_ds {
	struct ipc_perm	msg_perm;	/* operation permission struct */
	ushort		msg_qnum;	/* # of messages on q */
	ushort		msg_qbytes;	/* max # of bytes on q */
	pid_t		msg_lspid;	/* pid of last msgsnd */
	pid_t		msg_lrpid;	/* pid of last msgrcv */
	time_t		msg_stime;	/* last msgsnd time */
	time_t		msg_rtime;	/* last msgrcv time */
	time_t		msg_ctime;	/* last change time */
	/* End of part defined in the SVID */
	struct msg	*msg_first;	/* ptr to first message on q */
	struct msg	*msg_last;	/* ptr to last message on q */
	ushort		msg_cbytes;	/* current # bytes on q */
};

/*
 *	There is one msg structure for each message that may be in the system.
 *	Note that the SVID does not list 'running out of message text 
 *	space' as a possible error. Therefore, we must have space for
 *	NMESSAGES messages of maximum size. A simple way to achieve this is
 *	allocate the buffer within each msg structure - OK as long as all 
 *	the limits are kept small, otherwise we should allocate dynamically.
 */

struct msg {
	struct msg	*msg_next;	/* ptr to next message on q */
	struct msg	*msg_prev;	/* ptr to previous message on q */
	long		msg_type;	/* message type */
	int		msg_ts;		/* message text size */
	char		msg_text[MSGMAXBYTES];	/* message text */
};

/*
**	User message buffer template for msgsnd and msgrecv system calls.
*/

struct msgbuf {
	long	mtype;		/* message type */
	char	mtext[1];	/* message text */
};

/*
**	Message information structure.
*/

struct msginfo {
	int	msgmap,	/* # of entries in msg map */
		msgmax,	/* max message size */
		msgmnb,	/* max # bytes on queue */
		msgmni,	/* # of message queue identifiers */
		msgssz,	/* msg segment size (should be word size multiple) */
		msgtql;	/* # of system message headers */
	ushort	msgseg;	/* # of msg segments (MUST BE < 32768) */
};

#ifdef _KERNEL
/*	We have to be able to lock a message queue since we can
**	sleep during message processing due to a page fault in
**	copyin/copyout or iomove.  We cannot add anything to the
**	msqid_ds structure since this is used in user programs
**	and any change would break object file compatibility.
**	Therefore, we allocate a parallel array, msglock, which
**	is used to lock a message queue.  The array is defined
**	in the msg master file.  The following macro takes a
**	pointer to a message queue and returns a pointer to the
**	lock entry.  The argument must be a pointer to a msgqid
**	structure.
**/

#define	MSGLOCK(X)	&msglock[X - msgque]
#endif

#ifndef	_KERNEL
extern int msgctl (int __msqid, int __cmd, struct msqid_ds *__buf);
extern int msgget (key_t __key, int __msgflag);
extern int msgrcv (int __msqid, void *__msgp, size_t __msgsz,
		   long __msgtyp, int __msgflg);
extern int msgsnd (int __msqid, void *__msgp, size_t msgsz, int msgflg);
#endif

#endif/*__sys_msg_h*/

/* EOF msg.h */
