1 /*	$OpenBSD: shm.h,v 1.19 2004/07/15 11:24:46 millert Exp $	*/
2 /*	$NetBSD: shm.h,v 1.20 1996/04/09 20:55:35 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1994 Adam Glass
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Adam Glass.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * As defined+described in "X/Open System Interfaces and Headers"
36  *                         Issue 4, p. XXX
37  */
38 
39 #ifndef _SYS_SHM_H_
40 #define _SYS_SHM_H_
41 
42 #ifndef _SYS_IPC_H_
43 #include <sys/ipc.h>
44 #endif
45 
46 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
47 
48 /* shm-specific sysctl variables corresponding to members of struct shminfo */
49 #define KERN_SHMINFO_SHMMAX	1	/* int: max shm segment size (bytes) */
50 #define KERN_SHMINFO_SHMMIN	2	/* int: min shm segment size (bytes) */
51 #define KERN_SHMINFO_SHMMNI	3	/* int: max number of shm identifiers */
52 #define KERN_SHMINFO_SHMSEG	4	/* int: max shm segments per process */
53 #define KERN_SHMINFO_SHMALL	5	/* int: max amount of shm (pages) */
54 #define KERN_SHMINFO_MAXID	6	/* number of valid shared memory ids */
55 
56 #define CTL_KERN_SHMINFO_NAMES { \
57 	{ 0, 0 }, \
58 	{ "shmmax", CTLTYPE_INT }, \
59 	{ "shmmin", CTLTYPE_INT }, \
60 	{ "shmmni", CTLTYPE_INT }, \
61 	{ "shmseg", CTLTYPE_INT }, \
62 	{ "shmall", CTLTYPE_INT }, \
63 }
64 
65 /*
66  * Old (deprecated) access mode definitions--do not use.
67  * Provided for compatibility with old code only.
68  */
69 #define SHM_R		IPC_R
70 #define SHM_W		IPC_W
71 
72 #endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */
73 
74 /*
75  * Shared memory operation flags for shmat(2).
76  */
77 #define	SHM_RDONLY	010000	/* Attach read-only (else read-write) */
78 #define	SHM_RND		020000	/* Round attach address to SHMLBA */
79 
80 /*
81  * Shared memory specific control commands for shmctl().
82  * We accept but ignore these (XXX).
83  */
84 #define	SHM_LOCK	3	/* Lock segment in memory. */
85 #define	SHM_UNLOCK	4	/* Unlock a segment locked by SHM_LOCK. */
86 
87 /*
88  * Segment low boundry address multiple
89  * Use PAGE_SIZE for kernel but for userland query the kernel for the value.
90  */
91 #if defined(_KERNEL) || defined(_STANDALONE) || defined(_LKM)
92 #define	SHMLBA		PAGE_SIZE
93 #else
94 #define	SHMLBA		(getpagesize())
95 #endif /* _KERNEL || _STANDALONE || _LKM */
96 
97 typedef short		shmatt_t;
98 
99 struct shmid_ds {
100 	struct ipc_perm	shm_perm;	/* operation permission structure */
101 	int		shm_segsz;	/* size of segment in bytes */
102 	pid_t		shm_lpid;	/* process ID of last shm op */
103 	pid_t		shm_cpid;	/* process ID of creator */
104 	shmatt_t	shm_nattch;	/* number of current attaches */
105 	time_t		shm_atime;	/* time of last shmat() */
106 	time_t		shm_dtime;	/* time of last shmdt() */
107 	time_t		shm_ctime;	/* time of last change by shmctl() */
108 	void		*shm_internal;	/* implementation specific data */
109 };
110 
111 #ifdef _KERNEL
112 struct shmid_ds23 {
113 	struct ipc_perm23 shm_perm;	/* operation permission structure */
114 	int		shm_segsz;	/* size of segment in bytes */
115 	pid_t		shm_lpid;	/* process ID of last shm op */
116 	pid_t		shm_cpid;	/* process ID of creator */
117 	shmatt_t	shm_nattch;	/* number of current attaches */
118 	time_t		shm_atime;	/* time of last shmat() */
119 	time_t		shm_dtime;	/* time of last shmdt() */
120 	time_t		shm_ctime;	/* time of last change by shmctl() */
121 	void		*shm_internal;	/* implementation specific data */
122 };
123 
124 struct shmid_ds35 {
125 	struct ipc_perm35 shm_perm;	/* operation permission structure */
126 	int		  shm_segsz;	/* size of segment in bytes */
127 	pid_t		  shm_lpid;	/* process ID of last shm op */
128 	pid_t		  shm_cpid;	/* process ID of creator */
129 	shmatt_t	  shm_nattch;	/* number of current attaches */
130 	time_t		  shm_atime;	/* time of last shmat() */
131 	time_t		  shm_dtime;	/* time of last shmdt() */
132 	time_t		  shm_ctime;	/* time of last change by shmctl() */
133 	void		  *shm_internal;/* implementation specific data */
134 };
135 #endif
136 
137 #if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)
138 /*
139  * System V style catch-all structure for shared memory constants that
140  * might be of interest to user programs.  Do we really want/need this?
141  */
142 struct shminfo {
143 	int	shmmax;		/* max shared memory segment size (bytes) */
144 	int	shmmin;		/* min shared memory segment size (bytes) */
145 	int	shmmni;		/* max number of shared memory identifiers */
146 	int	shmseg;		/* max shared memory segments per process */
147 	int	shmall;		/* max amount of shared memory (pages) */
148 };
149 
150 struct shm_sysctl_info {
151 	struct	shminfo shminfo;
152 	struct	shmid_ds shmids[1];
153 };
154 #endif /* !_POSIX_C_SOURCE && !_XOPEN_SOURCE */
155 
156 #ifdef _KERNEL
157 extern struct shminfo shminfo;
158 extern struct shmid_ds **shmsegs;
159 
160 /* initial values for machdep.c */
161 extern int shmseg;
162 extern int shmmaxpgs;
163 
164 struct proc;
165 struct vmspace;
166 
167 void	shminit(void);
168 void	shmfork(struct vmspace *, struct vmspace *);
169 void	shmexit(struct vmspace *);
170 int	sysctl_sysvshm(int *, u_int, void *, size_t *, void *, size_t);
171 int	sys_shmat1(struct proc *, void *, register_t *, int);
172 int	shmctl1(struct proc *, int, int, caddr_t,
173 	    int (*)(const void *, void *, size_t),
174 	    int (*)(const void *, void *, size_t));
175 
176 #else /* !_KERNEL */
177 
178 #include <sys/cdefs.h>
179 
180 __BEGIN_DECLS
181 void *shmat(int, const void *, int);
182 int shmctl(int, int, struct shmid_ds *);
183 int shmdt(const void *);
184 int shmget(key_t, size_t, int);
185 __END_DECLS
186 
187 #endif /* !_KERNEL */
188 
189 #endif /* !_SYS_SHM_H_ */
190