Blame man3/shm_open.3

Packit 7cfc04
.\" Copyright (C) 2002 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME . Add an example to this page
Packit 7cfc04
.TH SHM_OPEN 3 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
shm_open, shm_unlink \- create/open or unlink POSIX shared memory objects
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <sys/mman.h>
Packit 7cfc04
.br
Packit 7cfc04
.BR "#include <sys/stat.h>" "        /* For mode constants */"
Packit 7cfc04
.br
Packit 7cfc04
.BR "#include <fcntl.h>" "           /* For O_* constants */"
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int shm_unlink(const char *" name );
Packit 7cfc04
.PP
Packit 7cfc04
Link with \fI\-lrt\fP.
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
creates and opens a new, or opens an existing, POSIX shared memory object.
Packit 7cfc04
A POSIX shared memory object is in effect a handle which can
Packit 7cfc04
be used by unrelated processes to
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
the same region of shared memory.
Packit 7cfc04
The
Packit 7cfc04
.BR shm_unlink ()
Packit 7cfc04
function performs the converse operation,
Packit 7cfc04
removing an object previously created by
Packit 7cfc04
.BR shm_open ().
Packit 7cfc04
.PP
Packit 7cfc04
The operation of
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
is analogous to that of
Packit 7cfc04
.BR open (2).
Packit 7cfc04
.I name
Packit 7cfc04
specifies the shared memory object to be created or opened.
Packit 7cfc04
For portable use,
Packit 7cfc04
a shared memory object should be identified by a name of the form
Packit 7cfc04
.IR /somename ;
Packit 7cfc04
that is, a null-terminated string of up to
Packit 7cfc04
.BI NAME_MAX
Packit 7cfc04
(i.e., 255) characters consisting of an initial slash,
Packit 7cfc04
.\" glibc allows the initial slash to be omitted, and makes
Packit 7cfc04
.\" multiple initial slashes equivalent to a single slash.
Packit 7cfc04
.\" This differs from the implementation of POSIX message queues.
Packit 7cfc04
followed by one or more characters, none of which are slashes.
Packit 7cfc04
.\" glibc allows subdirectory components in the name, in which
Packit 7cfc04
.\" case the subdirectory must exist under /dev/shm, and allow the
Packit 7cfc04
.\" required permissions if a user wants to create a shared memory
Packit 7cfc04
.\" object in that subdirectory.
Packit 7cfc04
.PP
Packit 7cfc04
.I oflag
Packit 7cfc04
is a bit mask created by ORing together exactly one of
Packit 7cfc04
.B O_RDONLY
Packit 7cfc04
or
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
and any of the other flags listed here:
Packit 7cfc04
.TP 1.1i
Packit 7cfc04
.B O_RDONLY
Packit 7cfc04
Open the object for read access.
Packit 7cfc04
A shared memory object opened in this way can be
Packit 7cfc04
.BR mmap (2)ed
Packit 7cfc04
only for read
Packit 7cfc04
.RB ( PROT_READ )
Packit 7cfc04
access.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
Open the object for read-write access.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
Create the shared memory object if it does not exist.
Packit 7cfc04
The user and group ownership of the object are taken
Packit 7cfc04
from the corresponding effective IDs of the calling process,
Packit 7cfc04
.\" In truth it is actually the filesystem IDs on Linux, but these
Packit 7cfc04
.\" are nearly always the same as the effective IDs.  (MTK, Jul 05)
Packit 7cfc04
and the object's
Packit 7cfc04
permission bits are set according to the low-order 9 bits of
Packit 7cfc04
.IR mode ,
Packit 7cfc04
except that those bits set in the process file mode
Packit 7cfc04
creation mask (see
Packit 7cfc04
.BR umask (2))
Packit 7cfc04
are cleared for the new object.
Packit 7cfc04
A set of macro constants which can be used to define
Packit 7cfc04
.I mode
Packit 7cfc04
is listed in
Packit 7cfc04
.BR open (2).
Packit 7cfc04
(Symbolic definitions of these constants can be obtained by including
Packit 7cfc04
.IR <sys/stat.h> .)
Packit 7cfc04
.IP
Packit 7cfc04
A new shared memory object initially has zero length\(emthe size of the
Packit 7cfc04
object can be set using
Packit 7cfc04
.BR ftruncate (2).
Packit 7cfc04
The newly allocated bytes of a shared memory
Packit 7cfc04
object are automatically initialized to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
If
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
was also specified, and a shared memory object with the given
Packit 7cfc04
.I name
Packit 7cfc04
already exists, return an error.
Packit 7cfc04
The check for the existence of the object, and its creation if it
Packit 7cfc04
does not exist, are performed atomically.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
If the shared memory object already exists, truncate it to zero bytes.
Packit 7cfc04
.PP
Packit 7cfc04
Definitions of these flag values can be obtained by including
Packit 7cfc04
.IR <fcntl.h> .
Packit 7cfc04
.PP
Packit 7cfc04
On successful completion
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
returns a new file descriptor referring to the shared memory object.
Packit 7cfc04
This file descriptor is guaranteed to be the lowest-numbered file descriptor
Packit 7cfc04
not previously opened within the process.
Packit 7cfc04
The
Packit 7cfc04
.B FD_CLOEXEC
Packit 7cfc04
flag (see
Packit 7cfc04
.BR fcntl (2))
Packit 7cfc04
is set for the file descriptor.
Packit 7cfc04
.PP
Packit 7cfc04
The file descriptor is normally used in subsequent calls
Packit 7cfc04
to
Packit 7cfc04
.BR ftruncate (2)
Packit 7cfc04
(for a newly created object) and
Packit 7cfc04
.BR mmap (2).
Packit 7cfc04
After a call to
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
the file descriptor may be closed without affecting the memory mapping.
Packit 7cfc04
.PP
Packit 7cfc04
The operation
Packit 7cfc04
of
Packit 7cfc04
.BR shm_unlink ()
Packit 7cfc04
is analogous to
Packit 7cfc04
.BR unlink (2):
Packit 7cfc04
it removes a shared memory object name, and, once all processes
Packit 7cfc04
have unmapped the object, de-allocates and
Packit 7cfc04
destroys the contents of the associated memory region.
Packit 7cfc04
After a successful
Packit 7cfc04
.BR shm_unlink (),
Packit 7cfc04
attempts to
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
an object with the same
Packit 7cfc04
.I name
Packit 7cfc04
fail (unless
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
was specified, in which case a new, distinct object is created).
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
returns a nonnegative file descriptor.
Packit 7cfc04
On failure,
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
returns \-1.
Packit 7cfc04
.BR shm_unlink ()
Packit 7cfc04
returns 0 on success, or \-1 on error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
On failure,
Packit 7cfc04
.I errno
Packit 7cfc04
is set to indicate the cause of the error.
Packit 7cfc04
Values which may appear in
Packit 7cfc04
.I errno
Packit 7cfc04
include the following:
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
Permission to
Packit 7cfc04
.BR shm_unlink ()
Packit 7cfc04
the shared memory object was denied.
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
Permission was denied to
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
.I name
Packit 7cfc04
in the specified
Packit 7cfc04
.IR mode ,
Packit 7cfc04
or
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
was specified and the caller does not have write permission on the object.
Packit 7cfc04
.TP
Packit 7cfc04
.B EEXIST
Packit 7cfc04
Both
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
and
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
were specified to
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
and the shared memory object specified by
Packit 7cfc04
.I name
Packit 7cfc04
already exists.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The
Packit 7cfc04
.I name
Packit 7cfc04
argument to
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
was invalid.
Packit 7cfc04
.TP
Packit 7cfc04
.B EMFILE
Packit 7cfc04
The per-process limit on the number of open file descriptors has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENAMETOOLONG
Packit 7cfc04
The length of
Packit 7cfc04
.I name
Packit 7cfc04
exceeds
Packit 7cfc04
.BR PATH_MAX .
Packit 7cfc04
.TP
Packit 7cfc04
.B ENFILE
Packit 7cfc04
The system-wide limit on the total number of open files has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
An attempt was made to
Packit 7cfc04
.BR shm_open ()
Packit 7cfc04
a
Packit 7cfc04
.I name
Packit 7cfc04
that did not exist, and
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
was not specified.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
An attempt was to made to
Packit 7cfc04
.BR shm_unlink ()
Packit 7cfc04
a
Packit 7cfc04
.I name
Packit 7cfc04
that does not exist.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
These functions are provided in glibc 2.2 and later.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw24 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR shm_open (),
Packit 7cfc04
.BR shm_unlink ()
Packit 7cfc04
T}	Thread safety	MT-Safe locale
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008.
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1-2001 says that the group ownership of a newly created shared
Packit 7cfc04
memory object is set to either the calling process's effective group ID
Packit 7cfc04
or "a system default group ID".
Packit 7cfc04
POSIX.1-2008 says that the group ownership
Packit 7cfc04
may be set to either the calling process's effective group ID
Packit 7cfc04
or, if the object is visible in the filesystem,
Packit 7cfc04
the group ID of the parent directory.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
.PP
Packit 7cfc04
POSIX leaves the behavior of the combination of
Packit 7cfc04
.B O_RDONLY
Packit 7cfc04
and
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
unspecified.
Packit 7cfc04
On Linux, this will successfully truncate an existing
Packit 7cfc04
shared memory object\(emthis may not be so on other UNIX systems.
Packit 7cfc04
.PP
Packit 7cfc04
The POSIX shared memory object implementation on Linux makes use
Packit 7cfc04
of a dedicated
Packit 7cfc04
.BR tmpfs (5)
Packit 7cfc04
filesystem that is normally mounted under
Packit 7cfc04
.IR /dev/shm .
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR close (2),
Packit 7cfc04
.BR fchmod (2),
Packit 7cfc04
.BR fchown (2),
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR fstat (2),
Packit 7cfc04
.BR ftruncate (2),
Packit 7cfc04
.BR memfd_create (2),
Packit 7cfc04
.BR mmap (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR umask (2),
Packit 7cfc04
.BR shm_overview (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.