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