1/*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*	$NetBSD: msg.h,v 1.4 1994/06/29 06:44:43 cgd Exp $	*/
29
30/*
31 * SVID compatible msg.h file
32 *
33 * Author:  Daniel Boulet
34 *
35 * Copyright 1993 Daniel Boulet and RTMX Inc.
36 *
37 * This system call was implemented by Daniel Boulet under contract from RTMX.
38 *
39 * Redistribution and use in source forms, with and without modification,
40 * are permitted provided that this entire comment appears intact.
41 *
42 * Redistribution in binary form may occur without any restrictions.
43 * Obviously, it would be nice if you gave credit where credit is due
44 * but requiring it would be too onerous.
45 *
46 * This software is provided ``AS IS'' without any warranties of any kind.
47 */
48/*
49 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
50 * support for mandatory and extensible security protections.  This notice
51 * is included in support of clause 2.2 (b) of the Apple Public License,
52 * Version 2.0.
53 */
54
55#ifndef _SYS_MSG_H_
56#define _SYS_MSG_H_
57
58#include <sys/appleapiopts.h>
59
60#include <sys/_types.h>
61#include <sys/cdefs.h>
62
63/*
64 * [XSI] All of the symbols from <sys/ipc.h> SHALL be defined when this
65 * header is included
66 */
67#include <sys/ipc.h>
68
69
70/*
71 * [XSI] The pid_t, time_t, key_t, size_t, and ssize_t types shall be
72 * defined as described in <sys/types.h>.
73 *
74 * NOTE:	The definition of the key_t type is implicit from the
75 *		inclusion of <sys/ipc.h>
76 */
77#ifndef _PID_T
78typedef __darwin_pid_t		pid_t;
79#define _PID_T
80#endif
81
82#ifndef	_TIME_T
83#define	_TIME_T
84typedef	__darwin_time_t		time_t;
85#endif
86
87#ifndef _SIZE_T
88#define _SIZE_T
89typedef __darwin_size_t		size_t;
90#endif
91
92#ifndef	_SSIZE_T
93#define	_SSIZE_T
94typedef	__darwin_ssize_t	ssize_t;
95#endif
96
97/* [XSI] Used for the number of messages in the message queue */
98typedef unsigned long		msgqnum_t;
99
100/* [XSI] Used for the number of bytes allowed in a message queue */
101typedef unsigned long		msglen_t;
102
103/*
104 * Possible values for the fifth parameter to msgrcv(), in addition to the
105 * IPC_NOWAIT flag, which is permitted.
106 */
107#define MSG_NOERROR	010000		/* [XSI] No error if big message */
108
109
110/*
111 * Technically, we should force all code references to the new structure
112 * definition, not in just the standards conformance case, and leave the
113 * legacy interface there for binary compatibility only.  Currently, we
114 * are only forcing this for programs requesting standards conformance.
115 */
116#if __DARWIN_UNIX03 || defined(KERNEL)
117#pragma pack(4)
118/*
119 * Structure used internally.
120 *
121 * Structure whose address is passed as the third parameter to msgctl()
122 * when the second parameter is IPC_SET or IPC_STAT.  In the case of the
123 * IPC_SET command, only the msg_perm.{uid|gid|perm} and msg_qbytes are
124 * honored.  In the case of IPC_STAT, only the fields indicated as [XSI]
125 * mandated fields are guaranteed to meaningful: DO NOT depend on the
126 * contents of the other fields.
127 *
128 * NOTES:	Reserved fields are not preserved across IPC_SET/IPC_STAT.
129 */
130#if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
131struct msqid_ds
132#else
133#define	msqid_ds	__msqid_ds_new
134struct __msqid_ds_new
135#endif
136{
137	struct __ipc_perm_new	msg_perm; /* [XSI] msg queue permissions */
138	__int32_t	msg_first;	/* RESERVED: kernel use only */
139	__int32_t	msg_last;	/* RESERVED: kernel use only */
140	msglen_t	msg_cbytes;	/* # of bytes on the queue */
141	msgqnum_t	msg_qnum;	/* [XSI] number of msgs on the queue */
142	msglen_t	msg_qbytes;	/* [XSI] max bytes on the queue */
143	pid_t		msg_lspid;	/* [XSI] pid of last msgsnd() */
144	pid_t		msg_lrpid;	/* [XSI] pid of last msgrcv() */
145	time_t		msg_stime;	/* [XSI] time of last msgsnd() */
146	__int32_t	msg_pad1;	/* RESERVED: DO NOT USE */
147	time_t		msg_rtime;	/* [XSI] time of last msgrcv() */
148	__int32_t	msg_pad2;	/* RESERVED: DO NOT USE */
149	time_t		msg_ctime;	/* [XSI] time of last msgctl() */
150	__int32_t	msg_pad3;	/* RESERVED: DO NOT USE */
151	__int32_t	msg_pad4[4];	/* RESERVED: DO NOT USE */
152};
153#pragma pack()
154#else	/* !__DARWIN_UNIX03 */
155#define	msqid_ds	__msqid_ds_old
156#endif	/* !__DARWIN_UNIX03 */
157
158#if !__DARWIN_UNIX03
159struct __msqid_ds_old {
160	struct __ipc_perm_old	msg_perm; /* [XSI] msg queue permissions */
161	__int32_t	msg_first;	/* RESERVED: kernel use only */
162	__int32_t	msg_last;	/* RESERVED: kernel use only */
163	msglen_t	msg_cbytes;	/* # of bytes on the queue */
164	msgqnum_t	msg_qnum;	/* [XSI] number of msgs on the queue */
165	msglen_t	msg_qbytes;	/* [XSI] max bytes on the queue */
166	pid_t		msg_lspid;	/* [XSI] pid of last msgsnd() */
167	pid_t		msg_lrpid;	/* [XSI] pid of last msgrcv() */
168	time_t		msg_stime;	/* [XSI] time of last msgsnd() */
169	__int32_t	msg_pad1;	/* RESERVED: DO NOT USE */
170	time_t		msg_rtime;	/* [XSI] time of last msgrcv() */
171	__int32_t	msg_pad2;	/* RESERVED: DO NOT USE */
172	time_t		msg_ctime;	/* [XSI] time of last msgctl() */
173	__int32_t	msg_pad3;	/* RESERVED: DO NOT USE */
174	__int32_t	msg_pad4[4];	/* RESERVED: DO NOT USE */
175};
176#endif	/* !__DARWIN_UNIX03 */
177
178#ifdef KERNEL
179#ifdef __APPLE_API_PRIVATE
180#include <machine/types.h>
181
182#if __DARWIN_ALIGN_NATURAL
183#pragma options align=natural
184#endif
185
186typedef user_ulong_t	user_msgqnum_t;
187typedef user64_ulong_t	user64_msgqnum_t;
188typedef user32_ulong_t	user32_msgqnum_t;
189
190typedef user_ulong_t	user_msglen_t;
191typedef user64_ulong_t	user64_msglen_t;
192typedef user32_ulong_t	user32_msglen_t;
193
194/* kernel version */
195struct user_msqid_ds {
196	struct ipc_perm	msg_perm;	/* [XSI] msg queue permissions */
197	struct msg	*msg_first;	/* first message in the queue */
198	struct msg	*msg_last;	/* last message in the queue */
199	user_msglen_t	msg_cbytes;	/* # of bytes on the queue */
200	user_msgqnum_t	msg_qnum;	/* [XSI] number of msgs on the queue */
201	user_msglen_t	msg_qbytes;	/* [XSI] max bytes on the queue */
202	pid_t		msg_lspid;	/* [XSI] pid of last msgsnd() */
203	pid_t		msg_lrpid;	/* [XSI] pid of last msgrcv() */
204	user_time_t	msg_stime;	/* [XSI] time of last msgsnd() */
205	__int32_t	msg_pad1;	/* RESERVED: DO NOT USE */
206	user_time_t	msg_rtime;	/* [XSI] time of last msgrcv() */
207	__int32_t	msg_pad2;	/* RESERVED: DO NOT USE */
208	user_time_t	msg_ctime;	/* [XSI] time of last msgctl() */
209	__int32_t	msg_pad3;	/* RESERVED: DO NOT USE */
210	__int32_t	msg_pad4[4];
211};
212
213/*
214 * user64 version - this structure only has to be correct if
215 * compiled LP64, because the 32 bit kernel doesn't need it
216 */
217struct user64_msqid_ds {
218	struct ipc_perm	msg_perm;	/* [XSI] msg queue permissions */
219	__int32_t	msg_first;	/* RESERVED: kernel use only */
220	__int32_t	msg_last;	/* RESERVED: kernel use only */
221	user64_msglen_t	msg_cbytes;	/* # of bytes on the queue */
222	user64_msgqnum_t	msg_qnum;	/* [XSI] number of msgs on the queue */
223	user64_msglen_t	msg_qbytes;	/* [XSI] max bytes on the queue */
224	pid_t		msg_lspid;	/* [XSI] pid of last msgsnd() */
225	pid_t		msg_lrpid;	/* [XSI] pid of last msgrcv() */
226	user64_time_t	msg_stime;	/* [XSI] time of last msgsnd() */
227	__int32_t	msg_pad1;	/* RESERVED: DO NOT USE */
228	user64_time_t	msg_rtime;	/* [XSI] time of last msgrcv() */
229	__int32_t	msg_pad2;	/* RESERVED: DO NOT USE */
230	user64_time_t	msg_ctime;	/* [XSI] time of last msgctl() */
231	__int32_t	msg_pad3;	/* RESERVED: DO NOT USE */
232	__int32_t	msg_pad4[4];
233} __attribute__((__packed__));
234
235struct user32_msqid_ds
236{
237	struct __ipc_perm_new	msg_perm; /* [XSI] msg queue permissions */
238	__int32_t	msg_first;	/* RESERVED: kernel use only */
239	__int32_t	msg_last;	/* RESERVED: kernel use only */
240	user32_msglen_t	msg_cbytes;	/* # of bytes on the queue */
241	user32_msgqnum_t	msg_qnum;	/* [XSI] number of msgs on the queue */
242	user32_msglen_t	msg_qbytes;	/* [XSI] max bytes on the queue */
243	pid_t		msg_lspid;	/* [XSI] pid of last msgsnd() */
244	pid_t		msg_lrpid;	/* [XSI] pid of last msgrcv() */
245	user32_time_t		msg_stime;	/* [XSI] time of last msgsnd() */
246	__int32_t	msg_pad1;	/* RESERVED: DO NOT USE */
247	user32_time_t		msg_rtime;	/* [XSI] time of last msgrcv() */
248	__int32_t	msg_pad2;	/* RESERVED: DO NOT USE */
249	user32_time_t		msg_ctime;	/* [XSI] time of last msgctl() */
250	__int32_t	msg_pad3;	/* RESERVED: DO NOT USE */
251	__int32_t	msg_pad4[4];	/* RESERVED: DO NOT USE */
252};
253
254#if __DARWIN_ALIGN_NATURAL
255#pragma options align=reset
256#endif
257
258struct label;
259
260/*
261 * Kernel wrapper for the user-level structure
262 */
263struct msqid_kernel {
264	struct	user_msqid_ds u;
265	struct	label *label;	/* MAC framework label */
266};
267
268#endif	/* __APPLE_API_PRIVATE */
269#endif	/* KERNEL */
270
271
272#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
273#ifdef __APPLE_API_UNSTABLE
274/* XXX kernel only; protect with macro later */
275
276struct msg {
277	struct msg	*msg_next;	/* next msg in the chain */
278	long		msg_type;	/* type of this message */
279    					/* >0 -> type of this message */
280    					/* 0 -> free header */
281	unsigned short	msg_ts;		/* size of this message */
282	short		msg_spot;	/* location of msg start in buffer */
283	struct label	*label;		/* MAC label */
284};
285
286/*
287 * Example structure describing a message whose address is to be passed as
288 * the second argument to the functions msgrcv() and msgsnd().  The only
289 * actual hard requirement is that the first field be of type long, and
290 * contain the message type.  The user is encouraged to define their own
291 * application specific structure; this definition is included solely for
292 * backward compatability with existing source code.
293 */
294struct mymsg {
295	long	mtype;		/* message type (+ve integer) */
296	char	mtext[1];	/* message body */
297};
298
299/*
300 * Based on the configuration parameters described in an SVR2 (yes, two)
301 * config(1m) man page.
302 *
303 * Each message is broken up and stored in segments that are msgssz bytes
304 * long.  For efficiency reasons, this should be a power of two.  Also,
305 * it doesn't make sense if it is less than 8 or greater than about 256.
306 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
307 * two between 8 and 1024 inclusive (and panic's if it isn't).
308 */
309struct msginfo {
310	int	msgmax,		/* max chars in a message */
311		msgmni,		/* max message queue identifiers */
312		msgmnb,		/* max chars in a queue */
313		msgtql,		/* max messages in system */
314		msgssz,		/* size of a message segment (see notes above) */
315		msgseg;		/* number of message segments */
316};
317#ifdef KERNEL
318extern struct msginfo	msginfo;
319
320#ifndef MSGSSZ
321#define MSGSSZ	8		/* Each segment must be 2^N long */
322#endif
323#ifndef MSGSEG
324#define MSGSEG	2048		/* must be less than 32767 */
325#endif
326#define MSGMAX	(MSGSSZ*MSGSEG)
327#ifndef MSGMNB
328#define MSGMNB	2048		/* max # of bytes in a queue */
329#endif
330#ifndef MSGMNI
331#define MSGMNI	40
332#endif
333#ifndef MSGTQL
334#define MSGTQL	40
335#endif
336
337/*
338 * macros to convert between msqid_ds's and msqid's.
339 * (specific to this implementation)
340 */
341#define MSQID(ix,ds)	((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
342#define MSQID_IX(id)	((id) & 0xffff)
343#define MSQID_SEQ(id)	(((id) >> 16) & 0xffff)
344
345/*
346 * The rest of this file is specific to this particular implementation.
347 */
348
349
350/*
351 * Stuff allocated in machdep.h
352 */
353struct msgmap {
354	short	next;		/* next segment in buffer */
355    				/* -1 -> available */
356    				/* 0..(MSGSEG-1) -> index of next segment */
357};
358
359/* The following four externs really, really need to die; should be static */
360extern char *msgpool;		/* MSGMAX byte long msg buffer pool */
361extern struct msgmap *msgmaps;	/* MSGSEG msgmap structures */
362extern struct msg *msghdrs;	/* MSGTQL msg headers */
363extern struct msqid_kernel *msqids;	/* MSGMNI user_msqid_ds struct's */
364
365#define MSG_LOCKED	01000	/* Is this msqid_ds locked? */
366
367#endif	/* KERNEL */
368#endif	/* __APPLE_API_UNSTABLE */
369#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
370
371#ifndef KERNEL
372
373__BEGIN_DECLS
374#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
375int msgsys(int, ...);
376#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
377int msgctl(int, int, struct msqid_ds *) __DARWIN_ALIAS(msgctl);
378int msgget(key_t, int);
379ssize_t msgrcv(int, void *, size_t, long, int) __DARWIN_ALIAS_C(msgrcv);
380int msgsnd(int, const void *, size_t, int) __DARWIN_ALIAS_C(msgsnd);
381__END_DECLS
382
383#endif /* !KERNEL */
384
385#endif /* !_SYS_MSG_H_ */
386
387
388