Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: msg.h,v 1.30 2024/05/12 10:34:56 rillig Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * SVID compatible msg.h file
     35  *
     36  * Author:  Daniel Boulet
     37  *
     38  * Copyright 1993 Daniel Boulet and RTMX Inc.
     39  *
     40  * This system call was implemented by Daniel Boulet under contract from RTMX.
     41  *
     42  * Redistribution and use in source forms, with and without modification,
     43  * are permitted provided that this entire comment appears intact.
     44  *
     45  * Redistribution in binary form may occur without any restrictions.
     46  * Obviously, it would be nice if you gave credit where credit is due
     47  * but requiring it would be too onerous.
     48  *
     49  * This software is provided ``AS IS'' without any warranties of any kind.
     50  */
     51 
     52 #ifndef _SYS_MSG_H_
     53 #define _SYS_MSG_H_
     54 
     55 #include <sys/featuretest.h>
     56 #include <sys/ipc.h>
     57 #ifdef _KERNEL
     58 #include <sys/condvar.h>
     59 #include <sys/mutex.h>
     60 #endif
     61 
     62 #ifdef _KERNEL
     63 struct __msg {
     64 	struct	__msg *msg_next; /* next msg in the chain */
     65 	long	msg_type;	/* type of this message */
     66     				/* >0 -> type of this message */
     67     				/* 0 -> free header */
     68 	u_short	msg_ts;		/* size of this message */
     69 	short	msg_spot;	/* location of start of msg in buffer */
     70 };
     71 #endif /* _KERNEL */
     72 
     73 #define MSG_NOERROR	010000		/* don't complain about too long msgs */
     74 
     75 typedef unsigned long	msgqnum_t;
     76 typedef size_t		msglen_t;
     77 
     78 struct msqid_ds {
     79 	struct ipc_perm	msg_perm;	/* operation permission structure */
     80 	msgqnum_t	msg_qnum;	/* number of messages in the queue */
     81 	msglen_t	msg_qbytes;	/* max # of bytes in the queue */
     82 	pid_t		msg_lspid;	/* process ID of last msgsend() */
     83 	pid_t		msg_lrpid;	/* process ID of last msgrcv() */
     84 	time_t		msg_stime;	/* time of last msgsend() */
     85 	time_t		msg_rtime;	/* time of last msgrcv() */
     86 	time_t		msg_ctime;	/* time of last change */
     87 
     88 	/*
     89 	 * These members are private and used only in the internal
     90 	 * implementation of this interface.
     91 	 */
     92 	struct __msg	*_msg_first;	/* first message in the queue */
     93 	struct __msg	*_msg_last;	/* last message in the queue */
     94 	msglen_t	_msg_cbytes;	/* # of bytes currently in queue */
     95 };
     96 
     97 #if defined(_NETBSD_SOURCE)
     98 /*
     99  * Based on the configuration parameters described in an SVR2 (yes, two)
    100  * config(1m) man page.
    101  *
    102  * Each message is broken up and stored in segments that are msgssz bytes
    103  * long.  For efficiency reasons, this should be a power of two.  Also,
    104  * it doesn't make sense if it is less than 8 or greater than about 256.
    105  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
    106  * two between 8 and 1024 inclusive (and panic's if it isn't).
    107  */
    108 struct msginfo {
    109 	int32_t	msgmax;		/* max chars in a message */
    110 	int32_t	msgmni;		/* max message queue identifiers */
    111 	int32_t	msgmnb;		/* max chars in a queue */
    112 	int32_t	msgtql;		/* max messages in system */
    113 	int32_t	msgssz;		/* size of a message segment
    114 				   (see notes above) */
    115 	int32_t	msgseg;		/* number of message segments */
    116 };
    117 
    118 /* Warning: 64-bit structure padding is needed here */
    119 struct msgid_ds_sysctl {
    120 	struct		ipc_perm_sysctl msg_perm;
    121 	uint64_t	msg_qnum;
    122 	uint64_t	msg_qbytes;
    123 	uint64_t	_msg_cbytes;
    124 	pid_t		msg_lspid;
    125 	pid_t		msg_lrpid;
    126 	time_t		msg_stime;
    127 	time_t		msg_rtime;
    128 	time_t		msg_ctime;
    129 	int32_t		pad;
    130 };
    131 struct msg_sysctl_info {
    132 	struct	msginfo msginfo;
    133 	struct	msgid_ds_sysctl msgids[1];
    134 };
    135 #endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */
    136 
    137 #ifdef _KERNEL
    138 
    139 #ifndef MSGSSZ
    140 #define MSGSSZ	8		/* Each segment must be 2^N long */
    141 #endif
    142 #ifndef MSGSEG
    143 #define MSGSEG	2048		/* must be less than 32767 */
    144 #endif
    145 #undef MSGMAX			/* ALWAYS compute MGSMAX! */
    146 #define MSGMAX	(MSGSSZ*MSGSEG)
    147 #ifndef MSGMNB
    148 #define MSGMNB	2048		/* max # of bytes in a queue */
    149 #endif
    150 #ifndef MSGMNI
    151 #define MSGMNI	40
    152 #endif
    153 #ifndef MSGTQL
    154 #define MSGTQL	40
    155 #endif
    156 
    157 /*
    158  * macros to convert between msqid_ds's and msqid's.
    159  */
    160 #define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm._seq << 16) & 0xffff0000))
    161 #define MSQID_IX(id)	((id) & 0xffff)
    162 #define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
    163 
    164 /*
    165  * Stuff allocated in machdep.h
    166  */
    167 struct msgmap {
    168 	short	next;		/* next segment in buffer */
    169     				/* -1 -> available */
    170     				/* 0..(MSGSEG-1) -> index of next segment */
    171 };
    172 
    173 typedef struct kmsq {
    174 	struct msqid_ds msq_u;
    175 	kcondvar_t	msq_cv;
    176 } kmsq_t;
    177 
    178 extern struct msginfo msginfo;
    179 extern kmsq_t	*msqs;		/* MSGMNI queues */
    180 extern kmutex_t	msgmutex;
    181 
    182 #define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
    183 
    184 #define SYSCTL_FILL_MSG(src, dst) do { \
    185 	SYSCTL_FILL_PERM((src).msg_perm, (dst).msg_perm); \
    186 	(dst).msg_qnum = (src).msg_qnum; \
    187 	(dst).msg_qbytes = (src).msg_qbytes; \
    188 	(dst)._msg_cbytes = (src)._msg_cbytes; \
    189 	(dst).msg_lspid = (src).msg_lspid; \
    190 	(dst).msg_lrpid = (src).msg_lrpid; \
    191 	(dst).msg_stime = (src).msg_stime; \
    192 	(dst).msg_rtime = (src).msg_rtime; \
    193 	(dst).msg_ctime = (src).msg_ctime; \
    194 } while (0)
    195 
    196 #endif /* _KERNEL */
    197 
    198 #ifndef _KERNEL
    199 #include <sys/cdefs.h>
    200 
    201 __BEGIN_DECLS
    202 int	msgctl(int, int, struct msqid_ds *) __RENAME(__msgctl50);
    203 int	msgget(key_t, int);
    204 int	msgsnd(int, const void *, size_t, int);
    205 ssize_t	msgrcv(int, void *, size_t, long, int);
    206 __END_DECLS
    207 #else
    208 #include <sys/systm.h>
    209 
    210 struct proc;
    211 
    212 int	msginit(void);
    213 int	msgfini(void);
    214 int	msgctl1(struct lwp *, int, int, struct msqid_ds *);
    215 int	msgsnd1(struct lwp *, int, const char *, size_t, int, size_t,
    216     copyin_t);
    217 int	msgrcv1(struct lwp *, int, char *, size_t, long, int, size_t,
    218     copyout_t, register_t *);
    219 #endif /* !_KERNEL */
    220 
    221 #endif /* !_SYS_MSG_H_ */
    222