2004-11-03 13:51:07 +00:00
|
|
|
.\" Hey Emacs! This file is -*- nroff -*- source.
|
|
|
|
.\"
|
2007-09-20 06:52:22 +00:00
|
|
|
.\" Copyright (C) 2002 Michael Kerrisk <mtk.manpages@gmail.com>
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
2008-03-18 14:47:54 +00:00
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
2008-04-24 09:36:55 +00:00
|
|
|
.\" FIXME . Add an example to this page
|
2009-02-24 23:47:23 +00:00
|
|
|
.TH SHM_OPEN 3 2009-02-25 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
shm_open, shm_unlink \- Create/open or unlink POSIX shared memory objects
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <sys/mman.h>
|
2004-12-17 14:05:55 +00:00
|
|
|
.br
|
2008-06-05 20:14:50 +00:00
|
|
|
.BR "#include <sys/stat.h>" " /* For mode constants */"
|
2008-03-10 09:49:26 +00:00
|
|
|
.br
|
2004-12-17 14:05:55 +00:00
|
|
|
.BR "#include <fcntl.h>" " /* For O_* constants */"
|
2004-11-03 13:51:07 +00:00
|
|
|
.sp
|
|
|
|
.BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
|
|
|
|
.sp
|
|
|
|
.BI "int shm_unlink(const char *" name );
|
2007-06-05 06:44:53 +00:00
|
|
|
.sp
|
2007-07-21 05:25:03 +00:00
|
|
|
Link with \fI\-lrt\fP.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
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
|
2007-04-12 22:42:49 +00:00
|
|
|
be used by unrelated processes to
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR mmap (2)
|
2007-04-12 22:42:49 +00:00
|
|
|
the same region of shared memory.
|
|
|
|
The
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_unlink ()
|
2007-04-12 22:42:49 +00:00
|
|
|
function performs the converse operation,
|
2004-11-03 13:51:07 +00:00
|
|
|
removing an object previously created by
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ().
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
|
|
|
The operation of
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is analogous to that of
|
|
|
|
.BR open (2).
|
|
|
|
.I name
|
2007-04-12 22:42:49 +00:00
|
|
|
specifies the shared memory object to be created or opened.
|
|
|
|
For portable use,
|
2009-02-20 01:46:46 +00:00
|
|
|
a shared memory object should be identified by a name of the form
|
|
|
|
.IR /somename ;
|
|
|
|
that is, a null-terminated string of up to
|
2009-02-24 23:47:23 +00:00
|
|
|
.BI NAME_MAX
|
|
|
|
(i.e., 255) characters consisting of an initial slash,
|
2009-02-20 01:46:46 +00:00
|
|
|
.\" glibc allows the initial slash to be omitted, and makes
|
|
|
|
.\" multiple initial slashes equivalent to a single slash.
|
|
|
|
.\" This differs from the implementation of POSIX message queues.
|
|
|
|
followed by one or more characters, none of which are slashes.
|
|
|
|
.\" glibc allows subdirectory components in the name, in which
|
|
|
|
.\" case the subdirectory must exist under /dev/shm, and allow the
|
|
|
|
.\" required permissions if a user wants to create a shared memory
|
|
|
|
.\" object in that subdirectory.
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
|
|
|
.I oflag
|
|
|
|
is a bit mask created by ORing together exactly one of
|
2007-04-12 22:42:49 +00:00
|
|
|
.B O_RDONLY
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2004-12-14 18:09:58 +00:00
|
|
|
.B O_RDWR
|
2004-11-03 13:51:07 +00:00
|
|
|
and any of the other flags listed here:
|
|
|
|
.TP 1.1i
|
|
|
|
.B O_RDONLY
|
|
|
|
Open the object for read access.
|
2007-04-12 22:42:49 +00:00
|
|
|
A shared memory object opened in this way can only be
|
|
|
|
.BR mmap (2)ed
|
2004-11-03 13:51:07 +00:00
|
|
|
for read (\fBPROT_READ\fP) access.
|
|
|
|
.TP
|
|
|
|
.B O_RDWR
|
|
|
|
Open the object for read-write access.
|
|
|
|
.TP
|
|
|
|
.B O_CREAT
|
2007-04-12 22:42:49 +00:00
|
|
|
Create the shared memory object if it does not exist.
|
2005-07-05 14:22:14 +00:00
|
|
|
The user and group ownership of the object are taken
|
|
|
|
from the corresponding effective IDs of the calling process,
|
|
|
|
.\" In truth it is actually the file system IDs on Linux, but these
|
|
|
|
.\" are nearly always the same as the effective IDs. (MTK, Jul 05)
|
2004-11-03 13:51:07 +00:00
|
|
|
and the object's
|
2007-04-12 22:42:49 +00:00
|
|
|
permission bits are set according to the low-order 9 bits of
|
2004-11-03 13:51:07 +00:00
|
|
|
.IR mode ,
|
|
|
|
except that those bits set in the process file mode
|
|
|
|
creation mask (see
|
|
|
|
.BR umask (2))
|
|
|
|
are cleared for the new object.
|
2005-07-05 14:22:14 +00:00
|
|
|
A set of macro constants which can be used to define
|
2004-11-03 13:51:07 +00:00
|
|
|
.I mode
|
2007-04-12 22:42:49 +00:00
|
|
|
is listed in
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR open (2).
|
2008-03-10 09:49:26 +00:00
|
|
|
(Symbolic definitions of these constants can be obtained by including
|
|
|
|
.IR <sys/stat.h> .)
|
2004-11-03 13:51:07 +00:00
|
|
|
.sp
|
2005-07-18 12:43:00 +00:00
|
|
|
A new shared memory object initially has zero length \(em the size of the
|
2004-11-03 13:51:07 +00:00
|
|
|
object can be set using
|
|
|
|
.BR ftruncate (2).
|
2005-07-05 14:22:14 +00:00
|
|
|
The newly allocated bytes of a shared memory
|
2007-06-08 09:56:56 +00:00
|
|
|
object are automatically initialized to 0.
|
2007-04-12 22:42:49 +00:00
|
|
|
.TP
|
2004-11-03 13:51:07 +00:00
|
|
|
.B O_EXCL
|
2007-04-12 22:42:49 +00:00
|
|
|
If
|
2004-11-03 13:51:07 +00:00
|
|
|
.B O_CREAT
|
2005-07-05 14:22:14 +00:00
|
|
|
was also specified, and a shared memory object with the given
|
2007-04-12 22:42:49 +00:00
|
|
|
.I name
|
2004-11-03 13:51:07 +00:00
|
|
|
already exists, return an error.
|
2007-04-12 22:42:49 +00:00
|
|
|
The check for the existence of the object, and its creation if it
|
2004-11-03 13:51:07 +00:00
|
|
|
does not exist, are performed atomically.
|
|
|
|
.TP
|
|
|
|
.B O_TRUNC
|
|
|
|
If the shared memory object already exists, truncate it to zero bytes.
|
|
|
|
.LP
|
2008-03-10 09:49:26 +00:00
|
|
|
Definitions of these flag values can be obtained by including
|
|
|
|
.IR <fcntl.h> .
|
|
|
|
.LP
|
2004-11-03 13:51:07 +00:00
|
|
|
On successful completion
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns a new file descriptor referring to the shared memory object.
|
|
|
|
This file descriptor is guaranteed to be the lowest-numbered file descriptor
|
2007-04-12 22:42:49 +00:00
|
|
|
not previously opened within the process.
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.B FD_CLOEXEC
|
2007-04-12 22:42:49 +00:00
|
|
|
flag (see
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR fcntl (2))
|
|
|
|
is set for the file descriptor.
|
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
The file descriptor is normally used in subsequent calls
|
|
|
|
to
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR ftruncate (2)
|
2005-07-05 14:22:14 +00:00
|
|
|
(for a newly created object) and
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR mmap (2).
|
|
|
|
After a call to
|
|
|
|
.BR mmap (2)
|
|
|
|
the file descriptor may be closed without affecting the memory mapping.
|
|
|
|
|
|
|
|
The operation
|
2007-04-12 22:42:49 +00:00
|
|
|
of
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_unlink ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is analogous to
|
|
|
|
.BR unlink (2):
|
|
|
|
it removes a shared memory object name, and, once all processes
|
2007-04-12 22:42:49 +00:00
|
|
|
have unmapped the object, de-allocates and
|
2004-11-03 13:51:07 +00:00
|
|
|
destroys the contents of the associated memory region.
|
2007-04-12 22:42:49 +00:00
|
|
|
After a successful
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_unlink (),
|
2007-04-12 22:42:49 +00:00
|
|
|
attempts to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2007-04-12 22:42:49 +00:00
|
|
|
an object with the same
|
2004-11-03 13:51:07 +00:00
|
|
|
.I name
|
|
|
|
will fail (unless
|
|
|
|
.B O_CREAT
|
|
|
|
was specified, in which case a new, distinct object is created).
|
|
|
|
.SH "RETURN VALUE"
|
|
|
|
On success,
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
_syscall.2, accept.2, epoll_create.2, inotify_add_watch.2, ioctl.2, msgget.2, msgop.2, open.2, prctl.2, sched_setscheduler.2, semctl.2, semget.2, swapon.2, addseverity.3, cacosh.3, csqrt.3, dirfd.3, drand48.3, fputws.3, mq_send.3, posix_openpt.3, puts.3, sched_getcpu.3, shm_open.3, sqrt.3, sysconf.3, tgamma.3, ulimit.3, credentials.7, svipc.7: Global fix: s/non-negative/nonnegative/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:44:48 +00:00
|
|
|
returns a nonnegative file descriptor.
|
2007-04-12 22:42:49 +00:00
|
|
|
On failure,
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns \-1.
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_unlink ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns 0 on success, or \-1 on error.
|
|
|
|
.SH ERRORS
|
|
|
|
On failure,
|
|
|
|
.I errno
|
2007-04-12 22:42:49 +00:00
|
|
|
is set to indicate the cause of the error.
|
|
|
|
Values which may appear in
|
2004-11-03 13:51:07 +00:00
|
|
|
.I errno
|
|
|
|
include the following:
|
2007-04-12 22:42:49 +00:00
|
|
|
.TP
|
2004-11-03 13:51:07 +00:00
|
|
|
.B EACCES
|
|
|
|
Permission to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_unlink ()
|
2004-11-03 13:51:07 +00:00
|
|
|
the shared memory object was denied.
|
|
|
|
.TP
|
|
|
|
.B EACCES
|
2007-04-12 22:42:49 +00:00
|
|
|
Permission was denied to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
.I name
|
|
|
|
in the specified
|
2005-07-05 14:22:14 +00:00
|
|
|
.IR mode ,
|
2007-04-12 22:42:49 +00:00
|
|
|
or
|
2004-11-03 13:51:07 +00:00
|
|
|
.B O_TRUNC
|
|
|
|
was specified and the caller does not have write permission on the object.
|
2007-04-12 22:42:49 +00:00
|
|
|
.TP
|
2004-11-03 13:51:07 +00:00
|
|
|
.B EEXIST
|
|
|
|
Both
|
|
|
|
.B O_CREAT
|
|
|
|
and
|
2007-04-12 22:42:49 +00:00
|
|
|
.B O_EXCL
|
2004-11-03 13:51:07 +00:00
|
|
|
were specified to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and the shared memory object specified by
|
|
|
|
.I name
|
|
|
|
already exists.
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
The
|
|
|
|
.I name
|
2007-04-12 22:42:49 +00:00
|
|
|
argument to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2004-11-03 13:51:07 +00:00
|
|
|
was invalid.
|
|
|
|
.TP
|
|
|
|
.B EMFILE
|
|
|
|
The process already has the maximum number of files open.
|
|
|
|
.TP
|
|
|
|
.B ENAMETOOLONG
|
2007-04-12 22:42:49 +00:00
|
|
|
The length of
|
2004-11-03 13:51:07 +00:00
|
|
|
.I name
|
2007-04-12 22:42:49 +00:00
|
|
|
exceeds
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR PATH_MAX .
|
|
|
|
.TP
|
|
|
|
.B ENFILE
|
|
|
|
The limit on the total number of files open on the system has been
|
|
|
|
reached.
|
|
|
|
.TP
|
|
|
|
.B ENOENT
|
|
|
|
An attempt was made to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_open ()
|
2007-04-12 22:42:49 +00:00
|
|
|
a
|
|
|
|
.I name
|
2004-11-03 13:51:07 +00:00
|
|
|
that did not exist, and
|
|
|
|
.B O_CREAT
|
|
|
|
was not specified.
|
|
|
|
.TP
|
|
|
|
.B ENOENT
|
|
|
|
An attempt was to made to
|
2005-07-05 14:22:14 +00:00
|
|
|
.BR shm_unlink ()
|
2007-04-12 22:42:49 +00:00
|
|
|
a
|
|
|
|
.I name
|
2004-11-03 13:51:07 +00:00
|
|
|
that does not exist.
|
2007-06-05 06:44:53 +00:00
|
|
|
.SH VERSIONS
|
|
|
|
These functions are provided in glibc 2.2 and later.
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
POSIX.1-2001.
|
|
|
|
.LP
|
|
|
|
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
|
2008-07-15 13:39:17 +00:00
|
|
|
or "a system default group ID".
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "NOTES"
|
|
|
|
.LP
|
2007-04-12 22:42:49 +00:00
|
|
|
POSIX leaves the behavior of the combination of
|
2004-11-03 13:51:07 +00:00
|
|
|
.B O_RDONLY
|
|
|
|
and
|
|
|
|
.B O_TRUNC
|
2007-04-12 22:42:49 +00:00
|
|
|
unspecified.
|
|
|
|
On Linux, this will successfully truncate an existing
|
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/
The man pages were rather inconsistent in the use of "Unix"
versus "UNIX". Let's go with the trademark usage.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
|
|
|
shared memory object \(em this may not be so on other UNIX systems.
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
|
|
|
The POSIX shared memory object implementation on Linux 2.4 makes use
|
|
|
|
of a dedicated file system, which is normally
|
2007-04-12 22:42:49 +00:00
|
|
|
mounted under
|
2005-07-05 14:22:14 +00:00
|
|
|
.IR /dev/shm .
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR close (2),
|
|
|
|
.BR fchmod (2),
|
|
|
|
.BR fchown (2),
|
|
|
|
.BR fcntl (2),
|
|
|
|
.BR fstat (2),
|
|
|
|
.BR ftruncate (2),
|
|
|
|
.BR mmap (2),
|
|
|
|
.BR open (2),
|
2008-07-02 05:01:23 +00:00
|
|
|
.BR umask (2),
|
|
|
|
.BR shm_overview (7)
|