]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/shm_open.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / sys / shm_open.2
1 .\"
2 .\" Copyright 2000 Massachusetts Institute of Technology
3 .\"
4 .\" Permission to use, copy, modify, and distribute this software and
5 .\" its documentation for any purpose and without fee is hereby
6 .\" granted, provided that both the above copyright notice and this
7 .\" permission notice appear in all copies, that both the above
8 .\" copyright notice and this permission notice appear in all
9 .\" supporting documentation, and that the name of M.I.T. not be used
10 .\" in advertising or publicity pertaining to distribution of the
11 .\" software without specific, written prior permission.  M.I.T. makes
12 .\" no representations about the suitability of this software for any
13 .\" purpose.  It is provided "as is" without express or implied
14 .\" warranty.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 .\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 .\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 .\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 .\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 .\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 .\" SUCH DAMAGE.
28 .\"
29 .\" $FreeBSD$
30 .\"
31 .Dd March 20, 2007
32 .Dt SHM_OPEN 2
33 .Os
34 .Sh NAME
35 .Nm shm_open , shm_unlink
36 .Nd "shared memory object operations"
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In sys/types.h
41 .In sys/mman.h
42 .In fcntl.h
43 .Ft int
44 .Fn shm_open "const char *path" "int flags" "mode_t mode"
45 .Ft int
46 .Fn shm_unlink "const char *path"
47 .Sh DESCRIPTION
48 The
49 .Fn shm_open
50 system call opens (or optionally creates) a
51 .Tn POSIX
52 shared memory object named
53 .Fa path .
54 The
55 .Fa flags
56 argument contains a subset of the flags used by
57 .Xr open 2 .
58 An access mode of either
59 .Dv O_RDONLY
60 or
61 .Dv O_RDWR
62 must be included in
63 .Fa flags .
64 The optional flags
65 .Dv O_CREAT ,
66 .Dv O_EXCL ,
67 and
68 .Dv O_TRUNC
69 may also be specified.
70 .Pp
71 If
72 .Dv O_CREAT
73 is specified,
74 then a new shared memory object named
75 .Fa path
76 will be created if it does not exist.
77 In this case,
78 the shared memory object is created with mode
79 .Fa mode
80 subject to the process' umask value.
81 If both the
82 .Dv O_CREAT
83 and
84 .Dv O_EXCL
85 flags are specified and a shared memory object named
86 .Fa path
87 already exists,
88 then
89 .Fn shm_open
90 will fail with
91 .Er EEXIST .
92 .Pp
93 Newly created objects start off with a size of zero.
94 If an existing shared memory object is opened with
95 .Dv O_RDWR
96 and the
97 .Dv O_TRUNC
98 flag is specified,
99 then the shared memory object will be truncated to a size of zero.
100 The size of the object can be adjusted via
101 .Xr ftruncate 2
102 and queried via
103 .Xr fstat 2 .
104 .Pp
105 The new descriptor is set to close during
106 .Xr execve 2
107 system calls;
108 see
109 .Xr close 2
110 and
111 .Xr fcntl 2 .
112 .Pp
113 As a FreeBSD extension,
114 the constant
115 .Dv SHM_ANON
116 may be used for the
117 .Fa path
118 argument to
119 .Fn shm_open .
120 In this case, an anonymous, unnamed shared memory object is created.
121 Since the object has no name,
122 it cannot be removed via a subsequent call to
123 .Fn shm_unlink .
124 Instead,
125 the shared memory object will be garbage collected when the last reference to
126 the shared memory object is removed.
127 The shared memory object may be shared with other processes by sharing the
128 file descriptor via
129 .Xr fork 2
130 or
131 .Xr sendmsg 2 .
132 Attempting to open an anonymous shared memory object with
133 .Dv O_RDONLY
134 will fail with
135 .Er EINVAL .
136 All other flags are ignored.
137 .Pp
138 The
139 .Fn shm_unlink
140 system call removes a shared memory object named
141 .Fa path .
142 .Pp
143 .Sh RETURN VALUES
144 If successful,
145 .Fn shm_open
146 returns a non-negative integer,
147 and
148 .Fn shm_unlink
149 returns zero.
150 Both functions return -1 on failure, and set
151 .Va errno
152 to indicate the error.
153 .Sh COMPATIBILITY
154 The
155 .Fa path
156 argument does not necessarily represent a pathname (although it does in
157 most other implementations).
158 Two processes opening the same
159 .Fa path
160 are guaranteed to access the same shared memory object if and only if
161 .Fa path
162 begins with a slash
163 .Pq Ql \&/
164 character.
165 .Pp
166 Only the
167 .Dv O_RDONLY ,
168 .Dv O_RDWR ,
169 .Dv O_CREAT ,
170 .Dv O_EXCL ,
171 and
172 .Dv O_TRUNC
173 flags may be used in portable programs.
174 .Pp
175 The result of using
176 .Xr open 2 ,
177 .Xr read 2 ,
178 or
179 .Xr write 2
180 on a shared memory object, or on the descriptor returned by
181 .Fn shm_open ,
182 is undefined.
183 It is also undefined whether the shared memory object itself, or its
184 contents, persist across reboots.
185 .Pp
186 In FreeBSD,
187 .Xr read 2
188 and
189 .Xr write 2
190 on a shared memory object will fail with
191 .Er EOPNOTSUPP
192 and neither shared memory objects nor their contents persist across reboots.
193 .Sh ERRORS
194 The following errors are defined for
195 .Fn shm_open :
196 .Bl -tag -width Er
197 .It Bq Er EINVAL
198 A flag other than
199 .Dv O_RDONLY ,
200 .Dv O_RDWR ,
201 .Dv O_CREAT ,
202 .Dv O_EXCL ,
203 or
204 .Dv O_TRUNC
205 was included in
206 .Fa flags .
207 .It Bq Er EMFILE
208 The process has already reached its limit for open file descriptors.
209 .It Bq Er ENFILE
210 The system file table is full.
211 .It Bq Er EINVAL
212 .Dv O_RDONLY
213 was specified while creating an anonymous shared memory object via
214 .Dv SHM_ANON .
215 .It Bq Er EFAULT
216 The
217 .Fa path
218 argument points outside the process' allocated address space.
219 .It Bq Er ENAMETOOLONG
220 The entire pathname exceeded 1023 characters.
221 .It Bq Er EINVAL
222 The
223 .Fa path
224 does not begin with a slash
225 .Pq Ql \&/
226 character.
227 .It Bq Er ENOENT
228 .Dv O_CREAT
229 is specified and the named shared memory object does not exist.
230 .It Bq Er EEXIST
231 .Dv O_CREAT
232 and
233 .Dv O_EXCL
234 are specified and the named shared memory object does exist.
235 .It Bq Er EACCES
236 The required permissions (for reading or reading and writing) are denied.
237 .El
238 .Pp
239 The following errors are defined for
240 .Fn shm_unlink :
241 .Bl -tag -width Er
242 .It Bq Er EFAULT
243 The
244 .Fa path
245 argument points outside the process' allocated address space.
246 .It Bq Er ENAMETOOLONG
247 The entire pathname exceeded 1023 characters.
248 .It Bq Er ENOENT
249 The named shared memory object does not exist.
250 .It Bq Er EACCES
251 The required permissions are denied.
252 .Fn shm_unlink
253 requires write permission to the shared memory object.
254 .El
255 .Sh SEE ALSO
256 .Xr close 2 ,
257 .Xr ftruncate 2 ,
258 .Xr fstat 2 ,
259 .Xr mmap 2 ,
260 .Xr munmap 2
261 .Sh STANDARDS
262 The
263 .Fn shm_open
264 and
265 .Fn shm_unlink
266 functions are believed to conform to
267 .St -p1003.1b-93 .
268 .Sh HISTORY
269 The
270 .Fn shm_open
271 and
272 .Fn shm_unlink
273 functions first appeared in
274 .Fx 4.3 .
275 The functions were reimplemented as system calls using shared memory objects
276 directly rather than files in
277 .Fx 7.0 .
278 .Sh AUTHORS
279 .An Garrett A. Wollman Aq wollman@FreeBSD.org
280 (C library support and this manual page)
281 .Pp
282 .An Matthew Dillon Aq dillon@FreeBSD.org
283 .Pq Dv MAP_NOSYNC