1SHM_OPEN(3)                Linux Programmer's Manual               SHM_OPEN(3)
2
3
4

NAME

6       shm_open,  shm_unlink  -  create/open  or  unlink  POSIX  shared memory
7       objects
8

SYNOPSIS

10       #include <sys/mman.h>
11       #include <sys/stat.h>        /* For mode constants */
12       #include <fcntl.h>           /* For O_* constants */
13
14       int shm_open(const char *name, int oflag, mode_t mode);
15
16       int shm_unlink(const char *name);
17
18       Link with -lrt.
19

DESCRIPTION

21       shm_open() creates and opens a new, or opens an existing, POSIX  shared
22       memory  object.   A  POSIX  shared  memory object is in effect a handle
23       which can be used by unrelated processes to mmap(2) the same region  of
24       shared  memory.  The shm_unlink() function performs the converse opera‐
25       tion, removing an object previously created by shm_open().
26
27       The operation of shm_open() is analogous  to  that  of  open(2).   name
28       specifies the shared memory object to be created or opened.  For porta‐
29       ble use, a shared memory object should be identified by a name  of  the
30       form  /somename;  that  is,  a null-terminated string of up to NAME_MAX
31       (i.e., 255) characters consisting of an initial slash, followed by  one
32       or more characters, none of which are slashes.
33
34       oflag  is  a bit mask created by ORing together exactly one of O_RDONLY
35       or O_RDWR and any of the other flags listed here:
36
37       O_RDONLY   Open the object for read access.   A  shared  memory  object
38                  opened   in   this  way  can  be  mmap(2)ed  only  for  read
39                  (PROT_READ) access.
40
41       O_RDWR     Open the object for read-write access.
42
43       O_CREAT    Create the shared memory object if it does not  exist.   The
44                  user  and  group  ownership of the object are taken from the
45                  corresponding effective IDs of the calling process, and  the
46                  object's  permission bits are set according to the low-order
47                  9 bits of mode, except that those bits set  in  the  process
48                  file  mode  creation mask (see umask(2)) are cleared for the
49                  new object.  A set of macro constants which can be  used  to
50                  define  mode is listed in open(2).  (Symbolic definitions of
51                  these constants can be obtained by including <sys/stat.h>.)
52
53                  A new shared memory object  initially  has  zero  length—the
54                  size of the object can be set using ftruncate(2).  The newly
55                  allocated bytes of a shared memory object are  automatically
56                  initialized to 0.
57
58       O_EXCL     If  O_CREAT  was  also specified, and a shared memory object
59                  with the given name already exists, return  an  error.   The
60                  check  for  the existence of the object, and its creation if
61                  it does not exist, are performed atomically.
62
63       O_TRUNC    If the shared memory object already exists, truncate  it  to
64                  zero bytes.
65
66       Definitions   of  these  flag  values  can  be  obtained  by  including
67       <fcntl.h>.
68
69       On successful completion  shm_open()  returns  a  new  file  descriptor
70       referring to the shared memory object.  This file descriptor is guaran‐
71       teed to be the lowest-numbered file descriptor  not  previously  opened
72       within  the process.  The FD_CLOEXEC flag (see fcntl(2)) is set for the
73       file descriptor.
74
75       The file descriptor is normally used  in  subsequent  calls  to  ftrun‐
76       cate(2)  (for  a  newly  created  object) and mmap(2).  After a call to
77       mmap(2) the file descriptor may be closed without affecting the  memory
78       mapping.
79
80       The  operation  of shm_unlink() is analogous to unlink(2): it removes a
81       shared memory object name, and, once all processes  have  unmapped  the
82       object, de-allocates and destroys the contents of the associated memory
83       region.  After a successful shm_unlink(),  attempts  to  shm_open()  an
84       object  with the same name fail (unless O_CREAT was specified, in which
85       case a new, distinct object is created).
86

RETURN VALUE

88       On success, shm_open() returns a nonnegative file descriptor.  On fail‐
89       ure,  shm_open()  returns -1.  shm_unlink() returns 0 on success, or -1
90       on error.
91

ERRORS

93       On failure, errno is set to indicate the cause of  the  error.   Values
94       which may appear in errno include the following:
95
96       EACCES Permission to shm_unlink() the shared memory object was denied.
97
98       EACCES Permission  was denied to shm_open() name in the specified mode,
99              or O_TRUNC was specified and the caller does not have write per‐
100              mission on the object.
101
102       EEXIST Both  O_CREAT  and  O_EXCL  were specified to shm_open() and the
103              shared memory object specified by name already exists.
104
105       EINVAL The name argument to shm_open() was invalid.
106
107       EMFILE The per-process limit on the number of open file descriptors has
108              been reached.
109
110       ENAMETOOLONG
111              The length of name exceeds PATH_MAX.
112
113       ENFILE The system-wide limit on the total number of open files has been
114              reached.
115
116       ENOENT An attempt was made to shm_open() a name that did not exist, and
117              O_CREAT was not specified.
118
119       ENOENT An  attempt  was  to  made  to shm_unlink() a name that does not
120              exist.
121

VERSIONS

123       These functions are provided in glibc 2.2 and later.
124

ATTRIBUTES

126       For  an  explanation  of  the  terms  used   in   this   section,   see
127       attributes(7).
128
129       ┌─────────────────────────┬───────────────┬────────────────┐
130Interface                Attribute     Value          
131       ├─────────────────────────┼───────────────┼────────────────┤
132shm_open(), shm_unlink() │ Thread safety │ MT-Safe locale │
133       └─────────────────────────┴───────────────┴────────────────┘
134

CONFORMING TO

136       POSIX.1-2001, POSIX.1-2008.
137
138       POSIX.1-2001  says  that  the group ownership of a newly created shared
139       memory object is set to either the calling process's effective group ID
140       or  "a system default group ID".  POSIX.1-2008 says that the group own‐
141       ership may be set to either the calling process's  effective  group  ID
142       or,  if  the  object  is visible in the filesystem, the group ID of the
143       parent directory.
144

NOTES

146       POSIX leaves the behavior of the combination of  O_RDONLY  and  O_TRUNC
147       unspecified.   On  Linux,  this  will successfully truncate an existing
148       shared memory object—this may not be so on other UNIX systems.
149
150       The POSIX shared memory object implementation on Linux makes use  of  a
151       dedicated tmpfs(5) filesystem that is normally mounted under /dev/shm.
152

SEE ALSO

154       close(2),   fchmod(2),  fchown(2),  fcntl(2),  fstat(2),  ftruncate(2),
155       memfd_create(2), mmap(2), open(2), umask(2), shm_overview(7)
156

COLOPHON

158       This page is part of release 5.04 of the Linux  man-pages  project.   A
159       description  of  the project, information about reporting bugs, and the
160       latest    version    of    this    page,    can     be     found     at
161       https://www.kernel.org/doc/man-pages/.
162
163
164
165Linux                             2017-09-15                       SHM_OPEN(3)
Impressum