OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / shm_open.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) 2002 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"
25 .\" FIXME . Add an example to this page
26 .TH SHM_OPEN 3 2009-02-25 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 shm_open, shm_unlink \- Create/open or unlink POSIX shared memory objects
29 .SH SYNOPSIS
30 .B #include <sys/mman.h>
31 .br
32 .BR "#include <sys/stat.h>" "        /* For mode constants */"
33 .br
34 .BR "#include <fcntl.h>" "           /* For O_* constants */"
35 .sp
36 .BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
37 .sp
38 .BI "int shm_unlink(const char *" name );
39 .sp
40 Link with \fI\-lrt\fP.
41 .SH DESCRIPTION
42 .BR shm_open ()
43 creates and opens a new, or opens an existing, POSIX shared memory object.
44 A POSIX shared memory object is in effect a handle which can
45 be used by unrelated processes to
46 .BR mmap (2)
47 the same region of shared memory.
48 The
49 .BR shm_unlink ()
50 function performs the converse operation,
51 removing an object previously created by
52 .BR shm_open ().
53 .LP
54 The operation of
55 .BR shm_open ()
56 is analogous to that of
57 .BR open (2).
58 .I name
59 specifies the shared memory object to be created or opened.
60 For portable use,
61 a shared memory object should be identified by a name of the form
62 .IR /somename ;
63 that is, a null-terminated string of up to
64 .BI NAME_MAX
65 (i.e., 255) characters consisting of an initial slash,
66 .\" glibc allows the initial slash to be omitted, and makes
67 .\" multiple initial slashes equivalent to a single slash.
68 .\" This differs from the implementation of POSIX message queues.
69 followed by one or more characters, none of which are slashes.
70 .\" glibc allows subdirectory components in the name, in which
71 .\" case the subdirectory must exist under /dev/shm, and allow the
72 .\" required permissions if a user wants to create a shared memory
73 .\" object in that subdirectory.
74 .LP
75 .I oflag
76 is a bit mask created by ORing together exactly one of
77 .B O_RDONLY
78 or
79 .B O_RDWR
80 and any of the other flags listed here:
81 .TP 1.1i
82 .B O_RDONLY
83 Open the object for read access.
84 A shared memory object opened in this way can only be
85 .BR mmap (2)ed
86 for read (\fBPROT_READ\fP) access.
87 .TP
88 .B O_RDWR
89 Open the object for read-write access.
90 .TP
91 .B O_CREAT
92 Create the shared memory object if it does not exist.
93 The user and group ownership of the object are taken
94 from the corresponding effective IDs of the calling process,
95 .\" In truth it is actually the file system IDs on Linux, but these
96 .\" are nearly always the same as the effective IDs.  (MTK, Jul 05)
97 and the object's
98 permission bits are set according to the low-order 9 bits of
99 .IR mode ,
100 except that those bits set in the process file mode
101 creation mask (see
102 .BR umask (2))
103 are cleared for the new object.
104 A set of macro constants which can be used to define
105 .I mode
106 is listed in
107 .BR open (2).
108 (Symbolic definitions of these constants can be obtained by including
109 .IR <sys/stat.h> .)
110 .sp
111 A new shared memory object initially has zero length \(em the size of the
112 object can be set using
113 .BR ftruncate (2).
114 The newly allocated bytes of a shared memory
115 object are automatically initialized to 0.
116 .TP
117 .B O_EXCL
118 If
119 .B O_CREAT
120 was also specified, and a shared memory object with the given
121 .I name
122 already exists, return an error.
123 The check for the existence of the object, and its creation if it
124 does not exist, are performed atomically.
125 .TP
126 .B O_TRUNC
127 If the shared memory object already exists, truncate it to zero bytes.
128 .LP
129 Definitions of these flag values can be obtained by including
130 .IR <fcntl.h> .
131 .LP
132 On successful completion
133 .BR shm_open ()
134 returns a new file descriptor referring to the shared memory object.
135 This file descriptor is guaranteed to be the lowest-numbered file descriptor
136 not previously opened within the process.
137 The
138 .B FD_CLOEXEC
139 flag (see
140 .BR fcntl (2))
141 is set for the file descriptor.
142
143 The file descriptor is normally used in subsequent calls
144 to
145 .BR ftruncate (2)
146 (for a newly created object) and
147 .BR mmap (2).
148 After a call to
149 .BR mmap (2)
150 the file descriptor may be closed without affecting the memory mapping.
151
152 The operation
153 of
154 .BR shm_unlink ()
155 is analogous to
156 .BR unlink (2):
157 it removes a shared memory object name, and, once all processes
158 have unmapped the object, de-allocates and
159 destroys the contents of the associated memory region.
160 After a successful
161 .BR shm_unlink (),
162 attempts to
163 .BR shm_open ()
164 an object with the same
165 .I name
166 will fail (unless
167 .B O_CREAT
168 was specified, in which case a new, distinct object is created).
169 .SH "RETURN VALUE"
170 On success,
171 .BR shm_open ()
172 returns a nonnegative file descriptor.
173 On failure,
174 .BR shm_open ()
175 returns \-1.
176 .BR shm_unlink ()
177 returns 0 on success, or \-1 on error.
178 .SH ERRORS
179 On failure,
180 .I errno
181 is set to indicate the cause of the error.
182 Values which may appear in
183 .I errno
184 include the following:
185 .TP
186 .B EACCES
187 Permission to
188 .BR shm_unlink ()
189 the shared memory object was denied.
190 .TP
191 .B EACCES
192 Permission was denied to
193 .BR shm_open ()
194 .I name
195 in the specified
196 .IR mode ,
197 or
198 .B O_TRUNC
199 was specified and the caller does not have write permission on the object.
200 .TP
201 .B EEXIST
202 Both
203 .B O_CREAT
204 and
205 .B O_EXCL
206 were specified to
207 .BR shm_open ()
208 and the shared memory object specified by
209 .I name
210 already exists.
211 .TP
212 .B EINVAL
213 The
214 .I name
215 argument to
216 .BR shm_open ()
217 was invalid.
218 .TP
219 .B EMFILE
220 The process already has the maximum number of files open.
221 .TP
222 .B ENAMETOOLONG
223 The length of
224 .I name
225 exceeds
226 .BR PATH_MAX .
227 .TP
228 .B ENFILE
229 The limit on the total number of files open on the system has been
230 reached.
231 .TP
232 .B ENOENT
233 An attempt was made to
234 .BR shm_open ()
235 a
236 .I name
237 that did not exist, and
238 .B O_CREAT
239 was not specified.
240 .TP
241 .B ENOENT
242 An attempt was to made to
243 .BR shm_unlink ()
244 a
245 .I name
246 that does not exist.
247 .SH VERSIONS
248 These functions are provided in glibc 2.2 and later.
249 .SH "CONFORMING TO"
250 POSIX.1-2001.
251 .LP
252 POSIX.1-2001 says that the group ownership of a newly created shared
253 memory object is set to either the calling process's effective group ID
254 or "a system default group ID".
255 .SH "NOTES"
256 .LP
257 POSIX leaves the behavior of the combination of
258 .B O_RDONLY
259 and
260 .B O_TRUNC
261 unspecified.
262 On Linux, this will successfully truncate an existing
263 shared memory object \(em this may not be so on other UNIX systems.
264 .LP
265 The POSIX shared memory object implementation on Linux 2.4 makes use
266 of a dedicated file system, which is normally
267 mounted under
268 .IR /dev/shm .
269 .SH "SEE ALSO"
270 .BR close (2),
271 .BR fchmod (2),
272 .BR fchown (2),
273 .BR fcntl (2),
274 .BR fstat (2),
275 .BR ftruncate (2),
276 .BR mmap (2),
277 .BR open (2),
278 .BR umask (2),
279 .BR shm_overview (7)