]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/shm_open.3
libc.7: Add a note on why glibc 2.x uses the soname libc.so.6
[thirdparty/man-pages.git] / man3 / shm_open.3
CommitLineData
c11b1abf 1.\" Copyright (C) 2002 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
10d76543
MK
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da 24.\"
df18528c 25.\" FIXME . Add an example to this page
29b41e74 26.TH SHM_OPEN 3 2015-12-28 "Linux" "Linux Programmer's Manual"
fea681da 27.SH NAME
f68512e9 28shm_open, shm_unlink \- create/open or unlink POSIX shared memory objects
fea681da 29.SH SYNOPSIS
fea681da 30.B #include <sys/mman.h>
e03e2055 31.br
84c517a4 32.BR "#include <sys/stat.h>" " /* For mode constants */"
87d17de4 33.br
e03e2055 34.BR "#include <fcntl.h>" " /* For O_* constants */"
fea681da
MK
35.sp
36.BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
37.sp
38.BI "int shm_unlink(const char *" name );
67ff1512 39.sp
20c58d70 40Link with \fI\-lrt\fP.
fea681da 41.SH DESCRIPTION
bf0cac28 42.BR shm_open ()
fea681da
MK
43creates and opens a new, or opens an existing, POSIX shared memory object.
44A POSIX shared memory object is in effect a handle which can
c13182ef 45be used by unrelated processes to
fea681da 46.BR mmap (2)
c13182ef
MK
47the same region of shared memory.
48The
bf0cac28 49.BR shm_unlink ()
c13182ef 50function performs the converse operation,
fea681da 51removing an object previously created by
bf0cac28 52.BR shm_open ().
fea681da
MK
53.LP
54The operation of
bf0cac28 55.BR shm_open ()
fea681da
MK
56is analogous to that of
57.BR open (2).
58.I name
c13182ef
MK
59specifies the shared memory object to be created or opened.
60For portable use,
8e984751
MK
61a shared memory object should be identified by a name of the form
62.IR /somename ;
63that is, a null-terminated string of up to
bd838488
MK
64.BI NAME_MAX
65(i.e., 255) characters consisting of an initial slash,
8e984751
MK
66.\" glibc allows the initial slash to be omitted, and makes
67.\" multiple initial slashes equivalent to a single slash.
68.\" This differs from the implementation of POSIX message queues.
69followed by one or more characters, none of which are slashes.
70.\" glibc allows subdirectory components in the name, in which
71.\" case the subdirectory must exist under /dev/shm, and allow the
72.\" required permissions if a user wants to create a shared memory
73.\" object in that subdirectory.
fea681da
MK
74.LP
75.I oflag
76is a bit mask created by ORing together exactly one of
c13182ef 77.B O_RDONLY
fea681da 78or
2d5e8aeb 79.B O_RDWR
fea681da
MK
80and any of the other flags listed here:
81.TP 1.1i
82.B O_RDONLY
83Open the object for read access.
33a0ccb2 84A shared memory object opened in this way can be
c13182ef 85.BR mmap (2)ed
c9942389
MK
86only for read
87.RB ( PROT_READ )
88access.
fea681da
MK
89.TP
90.B O_RDWR
91Open the object for read-write access.
92.TP
93.B O_CREAT
c13182ef 94Create the shared memory object if it does not exist.
bf0cac28
MK
95The user and group ownership of the object are taken
96from the corresponding effective IDs of the calling process,
9ee4a2b6 97.\" In truth it is actually the filesystem IDs on Linux, but these
bf0cac28 98.\" are nearly always the same as the effective IDs. (MTK, Jul 05)
fea681da 99and the object's
c13182ef 100permission bits are set according to the low-order 9 bits of
fea681da
MK
101.IR mode ,
102except that those bits set in the process file mode
103creation mask (see
104.BR umask (2))
105are cleared for the new object.
bf0cac28 106A set of macro constants which can be used to define
fea681da 107.I mode
c13182ef 108is listed in
bf0cac28 109.BR open (2).
87d17de4
MK
110(Symbolic definitions of these constants can be obtained by including
111.IR <sys/stat.h> .)
fea681da 112.sp
5503c85e 113A new shared memory object initially has zero length\(emthe size of the
fea681da
MK
114object can be set using
115.BR ftruncate (2).
bf0cac28 116The newly allocated bytes of a shared memory
d9bfdb9c 117object are automatically initialized to 0.
c13182ef 118.TP
fea681da 119.B O_EXCL
c13182ef 120If
fea681da 121.B O_CREAT
bf0cac28 122was also specified, and a shared memory object with the given
c13182ef 123.I name
fea681da 124already exists, return an error.
c13182ef 125The check for the existence of the object, and its creation if it
fea681da
MK
126does not exist, are performed atomically.
127.TP
128.B O_TRUNC
129If the shared memory object already exists, truncate it to zero bytes.
130.LP
87d17de4
MK
131Definitions of these flag values can be obtained by including
132.IR <fcntl.h> .
133.LP
fea681da 134On successful completion
bf0cac28 135.BR shm_open ()
fea681da
MK
136returns a new file descriptor referring to the shared memory object.
137This file descriptor is guaranteed to be the lowest-numbered file descriptor
c13182ef 138not previously opened within the process.
fea681da
MK
139The
140.B FD_CLOEXEC
c13182ef 141flag (see
fea681da
MK
142.BR fcntl (2))
143is set for the file descriptor.
144
c13182ef
MK
145The file descriptor is normally used in subsequent calls
146to
fea681da 147.BR ftruncate (2)
bf0cac28 148(for a newly created object) and
fea681da
MK
149.BR mmap (2).
150After a call to
151.BR mmap (2)
152the file descriptor may be closed without affecting the memory mapping.
153
154The operation
c13182ef 155of
bf0cac28 156.BR shm_unlink ()
fea681da
MK
157is analogous to
158.BR unlink (2):
159it removes a shared memory object name, and, once all processes
c13182ef 160have unmapped the object, de-allocates and
fea681da 161destroys the contents of the associated memory region.
c13182ef 162After a successful
bf0cac28 163.BR shm_unlink (),
c13182ef 164attempts to
bf0cac28 165.BR shm_open ()
c13182ef 166an object with the same
fea681da
MK
167.I name
168will fail (unless
169.B O_CREAT
170was specified, in which case a new, distinct object is created).
47297adb 171.SH RETURN VALUE
fea681da 172On success,
bf0cac28 173.BR shm_open ()
2fda57bd 174returns a nonnegative file descriptor.
c13182ef 175On failure,
bf0cac28 176.BR shm_open ()
fea681da 177returns \-1.
bf0cac28 178.BR shm_unlink ()
fea681da
MK
179returns 0 on success, or \-1 on error.
180.SH ERRORS
181On failure,
182.I errno
c13182ef
MK
183is set to indicate the cause of the error.
184Values which may appear in
fea681da
MK
185.I errno
186include the following:
c13182ef 187.TP
fea681da
MK
188.B EACCES
189Permission to
bf0cac28 190.BR shm_unlink ()
fea681da
MK
191the shared memory object was denied.
192.TP
193.B EACCES
c13182ef 194Permission was denied to
bf0cac28 195.BR shm_open ()
fea681da
MK
196.I name
197in the specified
bf0cac28 198.IR mode ,
c13182ef 199or
fea681da
MK
200.B O_TRUNC
201was specified and the caller does not have write permission on the object.
c13182ef 202.TP
fea681da
MK
203.B EEXIST
204Both
205.B O_CREAT
206and
c13182ef 207.B O_EXCL
fea681da 208were specified to
bf0cac28 209.BR shm_open ()
fea681da
MK
210and the shared memory object specified by
211.I name
212already exists.
213.TP
214.B EINVAL
215The
216.I name
c13182ef 217argument to
bf0cac28 218.BR shm_open ()
fea681da
MK
219was invalid.
220.TP
221.B EMFILE
26c32fab 222The per-process limit on the number of open file descriptors has been reached.
fea681da
MK
223.TP
224.B ENAMETOOLONG
c13182ef 225The length of
fea681da 226.I name
c13182ef 227exceeds
fea681da
MK
228.BR PATH_MAX .
229.TP
230.B ENFILE
e258766b 231The system-wide limit on the total number of open files has been reached.
fea681da
MK
232.TP
233.B ENOENT
234An attempt was made to
bf0cac28 235.BR shm_open ()
c13182ef
MK
236a
237.I name
fea681da
MK
238that did not exist, and
239.B O_CREAT
240was not specified.
241.TP
242.B ENOENT
243An attempt was to made to
bf0cac28 244.BR shm_unlink ()
c13182ef
MK
245a
246.I name
fea681da 247that does not exist.
67ff1512
MK
248.SH VERSIONS
249These functions are provided in glibc 2.2 and later.
be409be4
ZL
250.SH ATTRIBUTES
251For an explanation of the terms used in this section, see
252.BR attributes (7).
253.TS
254allbox;
255lbw24 lb lb
256l l l.
257Interface Attribute Value
258T{
259.BR shm_open (),
260.BR shm_unlink ()
261T} Thread safety MT-Safe locale
262.TE
263
47297adb 264.SH CONFORMING TO
abb4b5b5 265POSIX.1-2001, POSIX.1-2008.
2b2581ee
MK
266.LP
267POSIX.1-2001 says that the group ownership of a newly created shared
268memory object is set to either the calling process's effective group ID
44a2c328 269or "a system default group ID".
f71eeb14
MK
270POSIX.1-2008 says that the group ownership
271may be set to either the calling process's effective group ID
272or, if the object is visible in the filesystem,
273the group ID of the parent directory.
47297adb 274.SH NOTES
fea681da 275.LP
c13182ef 276POSIX leaves the behavior of the combination of
fea681da
MK
277.B O_RDONLY
278and
279.B O_TRUNC
c13182ef
MK
280unspecified.
281On Linux, this will successfully truncate an existing
5503c85e 282shared memory object\(emthis may not be so on other UNIX systems.
fea681da
MK
283.LP
284The POSIX shared memory object implementation on Linux 2.4 makes use
9ee4a2b6 285of a dedicated filesystem, which is normally
c13182ef 286mounted under
bf0cac28 287.IR /dev/shm .
47297adb 288.SH SEE ALSO
fea681da
MK
289.BR close (2),
290.BR fchmod (2),
291.BR fchown (2),
292.BR fcntl (2),
293.BR fstat (2),
294.BR ftruncate (2),
c4d76cd9 295.BR memfd_create (2),
fea681da
MK
296.BR mmap (2),
297.BR open (2),
f93af9c6
MK
298.BR umask (2),
299.BR shm_overview (7)