summaryrefslogtreecommitdiffstats
path: root/man3/shm_open.3
blob: 63601fc30568435cf1955fa3b3730dffb279a8fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2002 Michael Kerrisk (mtk16@ext.canterbury.ac.nz)
.\"
.\" 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.
.\" 
.\" 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
.\" the use of the information contained herein.  
.\" 
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH SHM_OPEN 3 2002-02-22 "Linux 2.4" "Linux Programmer's Manual"
.SH NAME
shm_open, shm_unlink \- Create/open or unlink POSIX shared memory objects
.SH SYNOPSIS
.B #include <sys/types.h>
.br
.B #include <sys/mman.h>
.sp
.BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
.sp
.BI "int shm_unlink(const char *" name );
.SH DESCRIPTION
.B shm_open
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 
.BR mmap (2)
the same region of shared memory.  
The 
.B shm_unlink
function performs the converse operation, 
removing an object previously created by
.BR shm_open .
.LP
The operation of
.B shm_open
is analogous to that of
.BR open (2).
.I name
specifies the shared memory object to be created or opened.  
For portable use, 
.I name
should have an initial slash (/) and contain no embedded slashes.
.\" The names used may or may not live in a file system, and may or may not
.\" survive a reboot. Names starting with a slash are also visible to other
.\" processes. Other names have implementation-defined effect.
.LP
.I oflag
is a bit mask created by ORing together exactly one of
.B O_RDONLY 
or
.B O_RWDR
and any of the other flags listed here:
.TP 1.1i
.B O_RDONLY
Open the object for read access.
A shared memory object opened in this way can only be 
.BR mmap (2)ed 
for read (\fBPROT_READ\fP) access.
.TP
.B O_RDWR
Open the object for read-write access.
.TP
.B O_CREAT
Create the shared memory object if it does not exist.  
The user and group ownership of the object are set as for
.BR open (2),
and the object's
permission bits are set according to the low-order 9 bits of 
.IR mode ,
except that those bits set in the process file mode
creation mask (see
.BR umask (2))
are cleared for the new object.
(A set of macro constants which can be used to define
.I mode
is listed in 
.BR open (2).)
.sp
A new shared memory object initially has zero length \- the size of the
object can be set using
.BR ftruncate (2).
(The newly-allocated bytes of a shared memory
object are automatically initialised to 0.)
.TP 
.B O_EXCL
If 
.B O_CREAT
was also specified, and a share memory object with the given
.I name 
already exists, return an error.
The check for the existence of the object, and its creation if it 
does not exist, are performed atomically.
.TP
.B O_TRUNC
If the shared memory object already exists, truncate it to zero bytes.
.LP
On successful completion
.B shm_open
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
flag (see 
.BR fcntl (2))
is set for the file descriptor.

The file descriptor is normally used in subsequent calls 
to 
.BR ftruncate (2)
(for a newly-created object) and
.BR mmap (2).
After a call to
.BR mmap (2)
the file descriptor may be closed without affecting the memory mapping.

The operation
of 
.B shm_unlink
is analogous to
.BR unlink (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 
.BR shm_unlink ,
attempts to 
.B shm_open 
an object with the same 
.I name
will fail (unless
.B O_CREAT
was specified, in which case a new, distinct object is created).
.SH "RETURN VALUE"
On success,
.B shm_open
returns a non-negative file descriptor.  On failure,
.B shm_open
returns \-1.
.B shm_unlink
returns 0 on success, or \-1 on error.
.SH ERRORS
On failure,
.I errno
is set to indicate the cause of the error.  Values which may appear in
.I errno
include the following:
.TP 
.B EACCES
Permission to
.B shm_unlink
the shared memory object was denied.
.TP
.B EACCES
Permission was denied to 
.B shm_open
.I name
in the specified
.IR mode,
or 
.B O_TRUNC
was specified and the caller does not have write permission on the object.
.TP 
.B EEXIST
Both
.B O_CREAT
and
.B O_EXCL 
were specified to
.B shm_open
and the shared memory object specified by
.I name
already exists.
.TP
.B EINVAL
The
.I name
argument to 
.B shm_open
was invalid.
.TP
.B EMFILE
The process already has the maximum number of files open.
.TP
.B ENAMETOOLONG
The length of 
.I name
exceeds 
.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
.B shm_open
a 
.I name 
that did not exist, and
.B O_CREAT
was not specified.
.TP
.B ENOENT
An attempt was to made to
.B shm_unlink
a 
.I name 
that does not exist.
.SH "NOTES"
These functions are provided in glibc 2.2 and later.  Programs using these 
functions must specify the 
.B \-lrt
flag to
.B cc
in order to link against the required ("realtime") library.
.LP
POSIX leaves the behavior of the combination of 
.B O_RDONLY
and
.B O_TRUNC
unspecified.  On Linux, this will successfully truncate an existing 
shared memory object \- this may not be so on other Unices.
.LP
The POSIX shared memory object implementation on Linux 2.4 makes use
of a dedicated file system, which is normally
mounted under 
.BR /dev/shm .
.SH "CONFORMING TO"
POSIX 1003.1 (2001).
.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),
.BR umask (2)