shm_open(2) shm_unlink

LIBRARY

Lb libc

SYNOPSIS

In sys/types.h In sys/mman.h In fcntl.h Ft int Fn shm_open const char *path int flags mode_t mode Ft int Fn shm_unlink const char *path

DESCRIPTION

The Fn shm_open system call opens (or optionally creates) a POSIX shared memory object named Fa path . The Fa flags argument contains a subset of the flags used by open(2). An access mode of either O_RDONLY or O_RDWR must be included in Fa flags . The optional flags O_CREAT O_EXCL and O_TRUNC may also be specified.

If O_CREAT is specified, then a new shared memory object named Fa path will be created if it does not exist. In this case, the shared memory object is created with mode Fa mode subject to the process' umask value. If both the O_CREAT and O_EXCL flags are specified and a shared memory object named Fa path already exists, then Fn shm_open will fail with Er EEXIST .

Newly created objects start off with a size of zero. If an existing shared memory object is opened with O_RDWR and the O_TRUNC flag is specified, then the shared memory object will be truncated to a size of zero. The size of the object can be adjusted via ftruncate(2) and queried via fstat(2).

The new descriptor is set to close during execve(2) system calls; see close(2) and fcntl(2).

As a FreeBSD extension, the constant SHM_ANON may be used for the Fa path argument to Fn shm_open . In this case, an anonymous, unnamed shared memory object is created. Since the object has no name, it cannot be removed via a subsequent call to Fn shm_unlink . Instead, the shared memory object will be garbage collected when the last reference to the shared memory object is removed. The shared memory object may be shared with other processes by sharing the file descriptor via fork(2) or sendmsg(2). Attempting to open an anonymous shared memory object with O_RDONLY will fail with Er EINVAL . All other flags are ignored.

The Fn shm_unlink system call removes a shared memory object named Fa path .

RETURN VALUES

If successful, Fn shm_open returns a non-negative integer, and Fn shm_unlink returns zero. Both functions return -1 on failure, and set errno to indicate the error.

COMPATIBILITY

The Fa path argument does not necessarily represent a pathname (although it does in most other implementations). 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 (`/' ) character.

Only the O_RDONLY O_RDWR O_CREAT O_EXCL and O_TRUNC flags may be used in portable programs.

The result of using open(2), read(2), or write(2) on a shared memory object, or on the descriptor returned by Fn shm_open , is undefined. It is also undefined whether the shared memory object itself, or its contents, persist across reboots.

In FreeBSD, read(2) and write(2) on a shared memory object will fail with Er EOPNOTSUPP and neither shared memory objects nor their contents persist across reboots.

ERRORS

The following errors are defined for Fn shm_open :

Bq Er EINVAL
A flag other than O_RDONLY O_RDWR O_CREAT O_EXCL or O_TRUNC was included in Fa flags .
Bq Er EMFILE
The process has already reached its limit for open file descriptors.
Bq Er ENFILE
The system file table is full.
Bq Er EINVAL
O_RDONLY was specified while creating an anonymous shared memory object via SHM_ANON
Bq Er EFAULT
The Fa path argument points outside the process' allocated address space.
Bq Er ENAMETOOLONG
The entire pathname exceeded 1023 characters.
Bq Er EINVAL
The Fa path does not begin with a slash (`/' ) character.
Bq Er ENOENT
O_CREAT is specified and the named shared memory object does not exist.
Bq Er EEXIST
O_CREAT and O_EXCL are specified and the named shared memory object does exist.
Bq Er EACCES
The required permissions (for reading or reading and writing) are denied.

The following errors are defined for Fn shm_unlink :

Bq Er EFAULT
The Fa path argument points outside the process' allocated address space.
Bq Er ENAMETOOLONG
The entire pathname exceeded 1023 characters.
Bq Er ENOENT
The named shared memory object does not exist.
Bq Er EACCES
The required permissions are denied. Fn shm_unlink requires write permission to the shared memory object.

STANDARDS

The Fn shm_open and Fn shm_unlink functions are believed to conform to St -p1003.1b-93 .

HISTORY

The Fn shm_open and Fn shm_unlink functions first appeared in Fx 4.3 . The functions were reimplemented as system calls using shared memory objects directly rather than files in Fx 7.0 .

AUTHORS

An Garrett A. Wollman Aq [email protected] (C library support and this manual page)

An Matthew Dillon Aq [email protected] (MAP_NOSYNC )