man-pages/man3/shm_open.3.html
2021-03-31 01:06:50 +01:00

450 lines
9.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SHM_OPEN</TITLE>
</HEAD><BODY>
<H1>SHM_OPEN</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
shm_open, shm_unlink - create/open or unlink POSIX shared memory objects
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/mman.h">sys/mman.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;</B> /* For mode constants */
<BR>
<B>#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;</B> /* For O_* constants */
<P>
<B>int shm_open(const char *</B><I>name</I><B>, int </B><I>oflag</I><B>, mode_t </B><I>mode</I><B>);</B>
<P>
<B>int shm_unlink(const char *</B><I>name</I><B>);</B>
<P>
Link with <I>-lrt</I>.
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>shm_open</B>()
creates and opens a new, or opens an existing, POSIX shared memory object.
A POSIX shared memory object is in effect a handle which can
be used by unrelated processes to
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
the same region of shared memory.
The
<B>shm_unlink</B>()
function performs the converse operation,
removing an object previously created by
<B>shm_open</B>().
<P>
The operation of
<B>shm_open</B>()
is analogous to that of
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
<I>name</I>
specifies the shared memory object to be created or opened.
For portable use,
a shared memory object should be identified by a name of the form
<I>/somename</I>;
that is, a null-terminated string of up to
<B>NAME_MAX</B>
(i.e., 255) characters consisting of an initial slash,
followed by one or more characters, none of which are slashes.
<P>
<I>oflag</I>
is a bit mask created by ORing together exactly one of
<B>O_RDONLY</B>
or
<B>O_RDWR</B>
and any of the other flags listed here:
<DL COMPACT>
<DT id="1"><B>O_RDONLY</B>
<DD>
Open the object for read access.
A shared memory object opened in this way can be
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)ed
only for read
(<B>PROT_READ</B>)
access.
<DT id="2"><B>O_RDWR</B>
<DD>
Open the object for read-write access.
<DT id="3"><B>O_CREAT</B>
<DD>
Create the shared memory object if it does not exist.
The user and group ownership of the object are taken
from the corresponding effective IDs of the calling process,
and the object's
permission bits are set according to the low-order 9 bits of
<I>mode</I>,
except that those bits set in the process file mode
creation mask (see
<B><A HREF="/cgi-bin/man/man2html?2+umask">umask</A></B>(2))
are cleared for the new object.
A set of macro constants which can be used to define
<I>mode</I>
is listed in
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
(Symbolic definitions of these constants can be obtained by including
<I>&lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;</I>.)
<DT id="4"><DD>
A new shared memory object initially has zero length---the size of the
object can be set using
<B><A HREF="/cgi-bin/man/man2html?2+ftruncate">ftruncate</A></B>(2).
The newly allocated bytes of a shared memory
object are automatically initialized to 0.
<DT id="5"><B>O_EXCL</B>
<DD>
If
<B>O_CREAT</B>
was also specified, and a shared memory object with the given
<I>name</I>
already exists, return an error.
The check for the existence of the object, and its creation if it
does not exist, are performed atomically.
<DT id="6"><B>O_TRUNC</B>
<DD>
If the shared memory object already exists, truncate it to zero bytes.
</DL>
<P>
Definitions of these flag values can be obtained by including
<I>&lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;</I>.
<P>
On successful completion
<B>shm_open</B>()
returns a new file descriptor referring to the shared memory object.
This file descriptor is guaranteed to be the lowest-numbered file descriptor
not previously opened within the process.
The
<B>FD_CLOEXEC</B>
flag (see
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2))
is set for the file descriptor.
<P>
The file descriptor is normally used in subsequent calls
to
<B><A HREF="/cgi-bin/man/man2html?2+ftruncate">ftruncate</A></B>(2)
(for a newly created object) and
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2).
After a call to
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
the file descriptor may be closed without affecting the memory mapping.
<P>
The operation
of
<B>shm_unlink</B>()
is analogous to
<B><A HREF="/cgi-bin/man/man2html?2+unlink">unlink</A></B>(2):
it removes a shared memory object name, and, once all processes
have unmapped the object, de-allocates and
destroys the contents of the associated memory region.
After a successful
<B>shm_unlink</B>(),
attempts to
<B>shm_open</B>()
an object with the same
<I>name</I>
fail (unless
<B>O_CREAT</B>
was specified, in which case a new, distinct object is created).
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>shm_open</B>()
returns a nonnegative file descriptor.
On failure,
<B>shm_open</B>()
returns -1.
<B>shm_unlink</B>()
returns 0 on success, or -1 on error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
On failure,
<I>errno</I>
is set to indicate the cause of the error.
Values which may appear in
<I>errno</I>
include the following:
<DL COMPACT>
<DT id="7"><B>EACCES</B>
<DD>
Permission to
<B>shm_unlink</B>()
the shared memory object was denied.
<DT id="8"><B>EACCES</B>
<DD>
Permission was denied to
<B>shm_open</B>()
<I>name</I>
in the specified
<I>mode</I>,
or
<B>O_TRUNC</B>
was specified and the caller does not have write permission on the object.
<DT id="9"><B>EEXIST</B>
<DD>
Both
<B>O_CREAT</B>
and
<B>O_EXCL</B>
were specified to
<B>shm_open</B>()
and the shared memory object specified by
<I>name</I>
already exists.
<DT id="10"><B>EINVAL</B>
<DD>
The
<I>name</I>
argument to
<B>shm_open</B>()
was invalid.
<DT id="11"><B>EMFILE</B>
<DD>
The per-process limit on the number of open file descriptors has been reached.
<DT id="12"><B>ENAMETOOLONG</B>
<DD>
The length of
<I>name</I>
exceeds
<B>PATH_MAX</B>.
<DT id="13"><B>ENFILE</B>
<DD>
The system-wide limit on the total number of open files has been reached.
<DT id="14"><B>ENOENT</B>
<DD>
An attempt was made to
<B>shm_open</B>()
a
<I>name</I>
that did not exist, and
<B>O_CREAT</B>
was not specified.
<DT id="15"><B>ENOENT</B>
<DD>
An attempt was to made to
<B>shm_unlink</B>()
a
<I>name</I>
that does not exist.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
These functions are provided in glibc 2.2 and later.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>shm_open</B>(),
<B>shm_unlink</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<P>
POSIX.1-2001 says that the group ownership of a newly created shared
memory object is set to either the calling process's effective group ID
or &quot;a system default group ID&quot;.
POSIX.1-2008 says that the group ownership
may be set to either the calling process's effective group ID
or, if the object is visible in the filesystem,
the group ID of the parent directory.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
<P>
POSIX leaves the behavior of the combination of
<B>O_RDONLY</B>
and
<B>O_TRUNC</B>
unspecified.
On Linux, this will successfully truncate an existing
shared memory object---this may not be so on other UNIX systems.
<P>
The POSIX shared memory object implementation on Linux makes use
of a dedicated
<B><A HREF="/cgi-bin/man/man2html?5+tmpfs">tmpfs</A></B>(5)
filesystem that is normally mounted under
<I>/dev/shm</I>.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fchmod">fchmod</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fchown">fchown</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fstat">fstat</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+ftruncate">ftruncate</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+memfd_create">memfd_create</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+umask">umask</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+shm_overview">shm_overview</A></B>(7)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="16"><A HREF="#lbAB">NAME</A><DD>
<DT id="17"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="18"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="19"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="20"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="21"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="22"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="23"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="24"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="25"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="26"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:56 GMT, March 31, 2021
</BODY>
</HTML>