Debian Codegraph @

upstream/debian-trixie/man3/shm_open.3

raw
1 '\" t
2 .\" Copyright (C) 2002, 2020 Michael Kerrisk <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH shm_open 3 2024-06-15 "Linux man-pages 6.9.1"
7 .SH NAME
8 shm_open, shm_unlink \- create/open or unlink POSIX shared memory objects
9 .SH LIBRARY
10 Real-time library
11 .RI ( librt ", " \-lrt )
12 .SH SYNOPSIS
13 .nf
14 .B #include <sys/mman.h>
15 .BR "#include <sys/stat.h>" " /* For mode constants */"
16 .BR "#include <fcntl.h>" " /* For O_* constants */"
17 .P
18 .BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
19 .BI "int shm_unlink(const char *" name );
20 .fi
21 .SH DESCRIPTION
22 .BR shm_open ()
23 creates and opens a new, or opens an existing, POSIX shared memory object.
24 A POSIX shared memory object is in effect a handle which can
25 be used by unrelated processes to
26 .BR mmap (2)
27 the same region of shared memory.
28 The
29 .BR shm_unlink ()
30 function performs the converse operation,
31 removing an object previously created by
32 .BR shm_open ().
33 .P
34 The operation of
35 .BR shm_open ()
36 is analogous to that of
37 .BR open (2).
38 .I name
39 specifies the shared memory object to be created or opened.
40 For portable use,
41 a shared memory object should be identified by a name of the form
42 .IR /somename ;
43 that is, a null-terminated string of up to
44 .B NAME_MAX
45 (i.e., 255) characters consisting of an initial slash,
46 .\" glibc allows the initial slash to be omitted, and makes
47 .\" multiple initial slashes equivalent to a single slash.
48 .\" This differs from the implementation of POSIX message queues.
49 followed by one or more characters, none of which are slashes.
50 .\" glibc allows subdirectory components in the name, in which
51 .\" case the subdirectory must exist under /dev/shm, and allow the
52 .\" required permissions if a user wants to create a shared memory
53 .\" object in that subdirectory.
54 .P
55 .I oflag
56 is a bit mask created by ORing together exactly one of
57 .B O_RDONLY
58 or
59 .B O_RDWR
60 and any of the other flags listed here:
61 .TP
62 .B O_RDONLY
63 Open the object for read access.
64 A shared memory object opened in this way can be
65 .BR mmap (2)ed
66 only for read
67 .RB ( PROT_READ )
68 access.
69 .TP
70 .B O_RDWR
71 Open the object for read-write access.
72 .TP
73 .B O_CREAT
74 Create the shared memory object if it does not exist.
75 The user and group ownership of the object are taken
76 from the corresponding effective IDs of the calling process,
77 .\" In truth it is actually the filesystem IDs on Linux, but these
78 .\" are nearly always the same as the effective IDs. (MTK, Jul 05)
79 and the object's
80 permission bits are set according to the low-order 9 bits of
81 .IR mode ,
82 except that those bits set in the process file mode
83 creation mask (see
84 .BR umask (2))
85 are cleared for the new object.
86 A set of macro constants which can be used to define
87 .I mode
88 is listed in
89 .BR open (2).
90 (Symbolic definitions of these constants can be obtained by including
91 .IR <sys/stat.h> .)
92 .IP
93 A new shared memory object initially has zero length\[em]the size of the
94 object can be set using
95 .BR ftruncate (2).
96 The newly allocated bytes of a shared memory
97 object are automatically initialized to 0.
98 .TP
99 .B O_EXCL
100 If
101 .B O_CREAT
102 was also specified, and a shared memory object with the given
103 .I name
104 already exists, return an error.
105 The check for the existence of the object, and its creation if it
106 does not exist, are performed atomically.
107 .TP
108 .B O_TRUNC
109 If the shared memory object already exists, truncate it to zero bytes.
110 .P
111 Definitions of these flag values can be obtained by including
112 .IR <fcntl.h> .
113 .P
114 On successful completion
115 .BR shm_open ()
116 returns a new file descriptor referring to the shared memory object.
117 This file descriptor is guaranteed to be the lowest-numbered file descriptor
118 not previously opened within the process.
119 The
120 .B FD_CLOEXEC
121 flag (see
122 .BR fcntl (2))
123 is set for the file descriptor.
124 .P
125 The file descriptor is normally used in subsequent calls
126 to
127 .BR ftruncate (2)
128 (for a newly created object) and
129 .BR mmap (2).
130 After a call to
131 .BR mmap (2)
132 the file descriptor may be closed without affecting the memory mapping.
133 .P
134 The operation
135 of
136 .BR shm_unlink ()
137 is analogous to
138 .BR unlink (2):
139 it removes a shared memory object name, and, once all processes
140 have unmapped the object, deallocates and
141 destroys the contents of the associated memory region.
142 After a successful
143 .BR shm_unlink (),
144 attempts to
145 .BR shm_open ()
146 an object with the same
147 .I name
148 fail (unless
149 .B O_CREAT
150 was specified, in which case a new, distinct object is created).
151 .SH RETURN VALUE
152 On success,
153 .BR shm_open ()
154 returns a file descriptor (a nonnegative integer).
155 On success,
156 .BR shm_unlink ()
157 returns 0.
158 On failure, both functions return \-1 and set
159 .I errno
160 to indicate the error.
161 .SH ERRORS
162 .TP
163 .B EACCES
164 Permission to
165 .BR shm_unlink ()
166 the shared memory object was denied.
167 .TP
168 .B EACCES
169 Permission was denied to
170 .BR shm_open ()
171 .I name
172 in the specified
173 .IR mode ,
174 or
175 .B O_TRUNC
176 was specified and the caller does not have write permission on the object.
177 .TP
178 .B EEXIST
179 Both
180 .B O_CREAT
181 and
182 .B O_EXCL
183 were specified to
184 .BR shm_open ()
185 and the shared memory object specified by
186 .I name
187 already exists.
188 .TP
189 .B EINVAL
190 The
191 .I name
192 argument to
193 .BR shm_open ()
194 was invalid.
195 .TP
196 .B EMFILE
197 The per-process limit on the number of open file descriptors has been reached.
198 .TP
199 .B ENAMETOOLONG
200 The length of
201 .I name
202 exceeds
203 .BR PATH_MAX .
204 .TP
205 .B ENFILE
206 The system-wide limit on the total number of open files has been reached.
207 .TP
208 .B ENOENT
209 An attempt was made to
210 .BR shm_open ()
211 a
212 .I name
213 that did not exist, and
214 .B O_CREAT
215 was not specified.
216 .TP
217 .B ENOENT
218 An attempt was to made to
219 .BR shm_unlink ()
220 a
221 .I name
222 that does not exist.
223 .SH ATTRIBUTES
224 For an explanation of the terms used in this section, see
225 .BR attributes (7).
226 .TS
227 allbox;
228 lbx lb lb
229 l l l.
230 Interface Attribute Value
231 T{
232 .na
233 .nh
234 .BR shm_open (),
235 .BR shm_unlink ()
236 T} Thread safety MT-Safe locale
237 .TE
238 .SH VERSIONS
239 POSIX leaves the behavior of the combination of
240 .B O_RDONLY
241 and
242 .B O_TRUNC
243 unspecified.
244 On Linux, this will successfully truncate an existing
245 shared memory object\[em]this may not be so on other UNIX systems.
246 .P
247 The POSIX shared memory object implementation on Linux makes use
248 of a dedicated
249 .BR tmpfs (5)
250 filesystem that is normally mounted under
251 .IR /dev/shm .
252 .SH STANDARDS
253 POSIX.1-2008.
254 .SH HISTORY
255 glibc 2.2.
256 POSIX.1-2001.
257 .P
258 POSIX.1-2001 says that the group ownership of a newly created shared
259 memory object is set to either the calling process's effective group ID
260 or "a system default group ID".
261 POSIX.1-2008 says that the group ownership
262 may be set to either the calling process's effective group ID
263 or, if the object is visible in the filesystem,
264 the group ID of the parent directory.
265 .SH EXAMPLES
266 The programs below employ POSIX shared memory and POSIX unnamed semaphores
267 to exchange a piece of data.
268 The "bounce" program (which must be run first) raises the case
269 of a string that is placed into the shared memory by the "send" program.
270 Once the data has been modified, the "send" program then prints
271 the contents of the modified shared memory.
272 An example execution of the two programs is the following:
273 .P
274 .in +4n
275 .EX
276 $ \fB./pshm_ucase_bounce /myshm &\fP
277 [1] 270171
278 $ \fB./pshm_ucase_send /myshm hello\fP
279 HELLO
280 .EE
281 .in
282 .P
283 Further detail about these programs is provided below.
284 .\"
285 .SS Program source: pshm_ucase.h
286 The following header file is included by both programs below.
287 Its primary purpose is to define a structure that will be imposed
288 on the memory object that is shared between the two programs.
289 .P
290 .in +4n
291 .\" SRC BEGIN (pshm_ucase.h)
292 .EX
293 #ifndef PSHM_UCASE_H
294 #define PSHM_UCASE_H
295 \&
296 #include <semaphore.h>
297 #include <stddef.h>
298 #include <stdio.h>
299 #include <stdlib.h>
300 \&
301 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \[rs]
302 } while (0)
303 \&
304 #define BUF_SIZE 1024 /* Maximum size for exchanged string */
305 \&
306 /* Define a structure that will be imposed on the shared
307 memory object */
308 \&
309 struct shmbuf {
310 sem_t sem1; /* POSIX unnamed semaphore */
311 sem_t sem2; /* POSIX unnamed semaphore */
312 size_t cnt; /* Number of bytes used in \[aq]buf\[aq] */
313 char buf[BUF_SIZE]; /* Data being transferred */
314 };
315 \&
316 #endif // include guard
317 .EE
318 .\" SRC END
319 .in
320 .\"
321 .SS Program source: pshm_ucase_bounce.c
322 The "bounce" program creates a new shared memory object with the name
323 given in its command-line argument and sizes the object to
324 match the size of the
325 .I shmbuf
326 structure defined in the header file.
327 It then maps the object into the process's address space,
328 and initializes two POSIX semaphores inside the object to 0.
329 .P
330 After the "send" program has posted the first of the semaphores,
331 the "bounce" program upper cases the data that has been placed
332 in the memory by the "send" program and then posts the second semaphore
333 to tell the "send" program that it may now access the shared memory.
334 .P
335 .in +4n
336 .\" SRC BEGIN (pshm_ucase_bounce.c)
337 .EX
338 /* pshm_ucase_bounce.c
339 \&
340 Licensed under GNU General Public License v2 or later.
341 */
342 #include <ctype.h>
343 #include <fcntl.h>
344 #include <stdio.h>
345 #include <stdlib.h>
346 #include <sys/mman.h>
347 #include <unistd.h>
348 \&
349 #include "pshm_ucase.h"
350 \&
351 int
352 main(int argc, char *argv[])
353 {
354 int fd;
355 char *shmpath;
356 struct shmbuf *shmp;
357 \&
358 if (argc != 2) {
359 fprintf(stderr, "Usage: %s /shm\-path\[rs]n", argv[0]);
360 exit(EXIT_FAILURE);
361 }
362 \&
363 shmpath = argv[1];
364 \&
365 /* Create shared memory object and set its size to the size
366 of our structure. */
367 \&
368 fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, 0600);
369 if (fd == \-1)
370 errExit("shm_open");
371 \&
372 if (ftruncate(fd, sizeof(struct shmbuf)) == \-1)
373 errExit("ftruncate");
374 \&
375 /* Map the object into the caller\[aq]s address space. */
376 \&
377 shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
378 MAP_SHARED, fd, 0);
379 if (shmp == MAP_FAILED)
380 errExit("mmap");
381 \&
382 /* Initialize semaphores as process\-shared, with value 0. */
383 \&
384 if (sem_init(&shmp\->sem1, 1, 0) == \-1)
385 errExit("sem_init\-sem1");
386 if (sem_init(&shmp\->sem2, 1, 0) == \-1)
387 errExit("sem_init\-sem2");
388 \&
389 /* Wait for \[aq]sem1\[aq] to be posted by peer before touching
390 shared memory. */
391 \&
392 if (sem_wait(&shmp\->sem1) == \-1)
393 errExit("sem_wait");
394 \&
395 /* Convert data in shared memory into upper case. */
396 \&
397 for (size_t j = 0; j < shmp\->cnt; j++)
398 shmp\->buf[j] = toupper((unsigned char) shmp\->buf[j]);
399 \&
400 /* Post \[aq]sem2\[aq] to tell the peer that it can now
401 access the modified data in shared memory. */
402 \&
403 if (sem_post(&shmp\->sem2) == \-1)
404 errExit("sem_post");
405 \&
406 /* Unlink the shared memory object. Even if the peer process
407 is still using the object, this is okay. The object will
408 be removed only after all open references are closed. */
409 \&
410 shm_unlink(shmpath);
411 \&
412 exit(EXIT_SUCCESS);
413 }
414 .EE
415 .\" SRC END
416 .in
417 .\"
418 .SS Program source: pshm_ucase_send.c
419 The "send" program takes two command-line arguments:
420 the pathname of a shared memory object previously created by the "bounce"
421 program and a string that is to be copied into that object.
422 .P
423 The program opens the shared memory object
424 and maps the object into its address space.
425 It then copies the data specified in its second argument
426 into the shared memory,
427 and posts the first semaphore,
428 which tells the "bounce" program that it can now access that data.
429 After the "bounce" program posts the second semaphore,
430 the "send" program prints the contents of the shared memory
431 on standard output.
432 .P
433 .in +4n
434 .\" SRC BEGIN (pshm_ucase_send.c)
435 .EX
436 /* pshm_ucase_send.c
437 \&
438 Licensed under GNU General Public License v2 or later.
439 */
440 #include <fcntl.h>
441 #include <stddef.h>
442 #include <stdio.h>
443 #include <stdlib.h>
444 #include <string.h>
445 #include <sys/mman.h>
446 #include <unistd.h>
447 \&
448 #include "pshm_ucase.h"
449 \&
450 int
451 main(int argc, char *argv[])
452 {
453 int fd;
454 char *shmpath, *string;
455 size_t len;
456 struct shmbuf *shmp;
457 \&
458 if (argc != 3) {
459 fprintf(stderr, "Usage: %s /shm\-path string\[rs]n", argv[0]);
460 exit(EXIT_FAILURE);
461 }
462 \&
463 shmpath = argv[1];
464 string = argv[2];
465 len = strlen(string);
466 \&
467 if (len > BUF_SIZE) {
468 fprintf(stderr, "String is too long\[rs]n");
469 exit(EXIT_FAILURE);
470 }
471 \&
472 /* Open the existing shared memory object and map it
473 into the caller\[aq]s address space. */
474 \&
475 fd = shm_open(shmpath, O_RDWR, 0);
476 if (fd == \-1)
477 errExit("shm_open");
478 \&
479 shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
480 MAP_SHARED, fd, 0);
481 if (shmp == MAP_FAILED)
482 errExit("mmap");
483 \&
484 /* Copy data into the shared memory object. */
485 \&
486 shmp\->cnt = len;
487 memcpy(&shmp\->buf, string, len);
488 \&
489 /* Tell peer that it can now access shared memory. */
490 \&
491 if (sem_post(&shmp\->sem1) == \-1)
492 errExit("sem_post");
493 \&
494 /* Wait until peer says that it has finished accessing
495 the shared memory. */
496 \&
497 if (sem_wait(&shmp\->sem2) == \-1)
498 errExit("sem_wait");
499 \&
500 /* Write modified data in shared memory to standard output. */
501 \&
502 write(STDOUT_FILENO, &shmp\->buf, len);
503 write(STDOUT_FILENO, "\[rs]n", 1);
504 \&
505 exit(EXIT_SUCCESS);
506 }
507 .EE
508 .\" SRC END
509 .in
510 .SH SEE ALSO
511 .BR close (2),
512 .BR fchmod (2),
513 .BR fchown (2),
514 .BR fcntl (2),
515 .BR fstat (2),
516 .BR ftruncate (2),
517 .BR memfd_create (2),
518 .BR mmap (2),
519 .BR open (2),
520 .BR umask (2),
521 .BR shm_overview (7)