.\" .\" Copyright 2000 Massachusetts Institute of Technology .\" .\" Permission to use, copy, modify, and distribute this software and .\" its documentation for any purpose and without fee is hereby .\" granted, provided that both the above copyright notice and this .\" permission notice appear in all copies, that both the above .\" copyright notice and this permission notice appear in all .\" supporting documentation, and that the name of M.I.T. not be used .\" in advertising or publicity pertaining to distribution of the .\" software without specific, written prior permission. M.I.T. makes .\" no representations about the suitability of this software for any .\" purpose. It is provided "as is" without express or implied .\" warranty. .\" .\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS .\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT .\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT .\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF .\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND .\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD: src/lib/libc/gen/shm_open.3,v 1.3.2.5 2001/12/14 18:33:51 ru Exp $ .\" .Dd June 28, 2019 .Dt SHM_OPEN 3 .Os .Sh NAME .Nm shm_open , .Nm shm_unlink .Nd POSIX shared memory object operations .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In sys/types.h .In sys/mman.h .Ft int .Fn shm_open "const char *path" "int flags" "mode_t mode" .Ft int .Fn shm_unlink "const char *path" .Sh DESCRIPTION The .Fn shm_open function opens (or optionally creates) a .Tn POSIX shared memory object named .Fa path . The .Fn shm_unlink function removes a shared memory object named .Fa path . Using the same .Fa path allows unrelated processes to access the same object. .Sh IMPLEMENTATION NOTES .Dx mounts a .Xr tmpfs 5 file system at .Pa /var/run/shm . .Tn POSIX shared memory objects are implemented as ordinary files under that directory. The .Fn shm_open and .Fn shm_unlink functions act as wrappers around .Xr open 2 and .Xr unlink 2 . Any leading slash .Pq Ql \&/ characters are removed from .Fa path to make it relative to .Pa /var/run/shm . The .Fa flags and .Fa mode arguments are passed through unaltered. .Fa flags is checked to ensure that the access mode specified is not .Dv O_WRONLY (which is not defined for shared memory objects). .Pp In addition, the .Dx implementation causes .Fn mmap of a descriptor returned by .Fn shm_open to behave as if the .Dv MAP_NOSYNC flag had been specified to .Xr mmap 2 . (It does so by setting a special file flag using .Xr fcntl 2 . ) .Sh RETURN VALUES If successful, .Fn shm_open returns a non-negative integer; .Fn shm_unlink returns zero. Both functions return -1 on failure, and set .Va errno to indicate the error. .Sh COMPATIBILITY On .Dx and many other operating systems the .Fa path argument is interpreted as a file system pathname under a special directory where a memory-backed file system is mounted. Most operating systems do some name mangling to .Fa path . Leading slashes are commonly removed to turn an absolute pathname into a relative one. Problematic characters may be escaped and there may be a length limit on .Fa path . On some systems the mangled pathname is completely different from the given .Fa path . On a few systems, shared memory objects live outside the ordinary file system in their own dedicated namespace. .Pp According to .Tn POSIX two processes opening the same .Fa path are guaranteed to access the same shared memory object if and only if .Fa path begins with a slash. The most portable form of pathname is probably .Ql /foobar , i.e.\& one leading slash, no other slashes and no dots. .Pp The result of using .Xr open 2 on the pathname of a shared memory object, or using .Xr read 2 or .Xr write 2 on a file descriptor returned by .Fn shm_open , is undefined by .Tn POSIX . It is also undefined whether the shared memory object itself, or its contents, persist across reboots. On .Dx and most other systems they do not. Only the .Dv O_RDONLY , .Dv O_RDWR , .Dv O_CREAT , .Dv O_EXCL , and .Dv O_TRUNC flags may be used in portable programs. .Sh ERRORS The .Fn shm_open and .Fn shm_unlink functions can fail with any error defined for .Fn open and .Fn unlink , respectively. In addition, the following errors are defined for .Fn shm_open : .Bl -tag -width Er .It Bq Er EINVAL The object named by .Fa path is not a shared memory object (i.e., it is not a regular file). .It Bq Er EINVAL The .Fa flags argument to .Fn shm_open specifies an access mode of .Dv O_WRONLY . .El .Sh SEE ALSO .Xr mmap 2 , .Xr munmap 2 , .Xr open 2 , .Xr unlink 2 , .Xr tmpfs 5 .Sh STANDARDS The .Fn shm_open and .Fn shm_unlink functions are believed to conform to .St -p1003.1b-93 . .Sh HISTORY The .Fn shm_open and .Fn shm_unlink functions first appeared in .Fx 4.3 . .Sh AUTHORS .An Garrett A. Wollman Aq Mt wollman@FreeBSD.org (C library support and this manual page) .An Matthew Dillon Aq Mt dillon@FreeBSD.org .Pq Dv MAP_NOSYNC