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