SHMCTL


Obtain information about a shared memory segment, set the owner, group, and permissions of a segment, or destroy a segment.

Arguments

ecx Shared memory segment ID.
edx Command to perform. It may be one of the following values:
IPC_STAT Copy information from the kernel data structure associated with ecx into the shmid_ds structure pointed to by esi. The caller must have read permission on the shared memory segment.

struc shmid_ds
{
.shm_perm    ipc_perm ; Ownership and permissions
.shm_segsz   rd 1 ; Size of segment (bytes)
.shm_atime   rd 1 ; Last attach time
.shm_dtime   rd 1 ; Last detach time
.shm_ctime   rd 1 ; Last change time
.shm_cpid    rw 1 ; PID of creator
.shm_lpid    rw 1 ; PID of last SHMAT/SHMDT
.shm_nattch  rw 1 ; Number of current attaches
.shm_unused  rw 1 ; Not used
.shm_unused2 rd 1 ; Not used
.shm_unused3 rd 1 ; Not used
}
IPC_SET Write the values of some members of the shmid_ds structure pointed to by esi to the kernel data structure associated with this shared memory segment, updating also its shm_ctime member. The following fields can be changed: shm_perm.uid, shm_perm.gid, and (the least significant 9 bits of) shm_perm.mode. The effective UID of the calling process must match the owner (shm_perm.uid) or creator (shm_perm.cuid) of the shared memory segment, or the caller must be privileged.
IPC_RMID Mark the segment to be destroyed. The segment will only actually be destroyed after the last process detaches it (i.e., when the shm_nattch member of the associated structure shmid_ds is zero). The caller must be the owner or creator, or be privileged. If a segment has been marked for destruction, then the (non-standard) SHM_DEST flag of the shm_perm.mode field in the associated data structure retrieved by IPC_STAT will be set.
IPC_INFO Returns information about system-wide shared memory limits and parameters in the shminfo structure pointed to by esi:

struc shminfo
{
.shmmax rd 1 ; Max. segment size
.shmmin rd 1 ; Min. segment size; always 1
.shmmni rd 1 ; Max. # of segments
.shmseg rd 1 ; Max. # of segments that a process can attach; unused
.shmall rd 1 ; Max. # of pages of shared memory, system-wide
}

The shmmni, shmmax, and shmall settings can be changed via /proc files of the same name.
SHM_INFO Returns a shm_info structure (pointed by esi) whose fields contain information about system resources consumed by shared memory:

struc shm_info
{
.used_ids       rd 1 ; # of currently existing segments
.shm_tot        rd 1 ; Total number of shared memory pages
.shm_rss        rd 1 ; # of resident shared memory pages
.shm_swp        rd 1 ; # of swapped shared memory pages
.swap_attempts  rd 1 ; Unused
.swap_successes rd 1 ; Unused
}
SHM_STAT Returns a shmid_ds structure as for IPC_STAT. However, the ecx argument is not a segment identifier, but instead an index into the kernel's internal array that maintains information about all shared memory segments on the system.
SHM_LOCK Prevent swapping of the shared memory segment. The caller must fault in any pages that are required to be present after locking is enabled. If a segment has been locked, then the (non-standard) SHM_LOCKED flag of the shm_perm.mode field in the associated data structure retrieved by IPC_STAT will be set.
SHM_UNLOCK Unlock the segment, allowing it to be swapped out.
esi This argument depends on the command used. See above.

Return values

If the function succeeds the return value depends on the command used (for other commands return value is 0):

IPC_INFO, SHM_INFO Return value is the index of the highest used entry in the kernel's internal array recording information about all shared memory segments. (This information can be used with repeated SHM_STAT operations to obtain information about all shared memory segments on the system.)
SHM_STAT Return value is the identifier of the shared memory segment whose index was given in ecx.

If the function fails the return value is one of the following errno values:

-EACCES IPC_STAT or SHM_STAT is requested and shm_perm.mode does not allow read access for ecx, and the calling process does not have the CAP_IPC_OWNER capability.
-EFAULT The argument edx has value IPC_SET or IPC_STAT but the address pointed to by esi isn't accessible.
-EIDRM ecx points to a removed identifier.
-EINVAL ecx is not a valid identifier, or edx is not a valid command. Or: for a SHM_STAT operation, the index value specified in ecx referred to an array slot that is currently unused.
-ENOMEM (In kernels since 2.6.9), SHM_LOCK was specified and the size of the to-be-locked segment would mean that the total bytes in locked shared memory segments would exceed the limit for the real user ID of the calling process. This limit is defined by the RLIMIT_MEMLOCK soft resource limit (see sys_setrlimit).
-EOVERFLOW IPC_STAT is attempted, and the GID or UID value is too large to be stored in the structure pointed to by esi.
-EPERM IPC_SET or IPC_RMID is attempted, and the effective user ID of the calling process is not that of the creator (found in shm_perm.cuid), or the owner (found in shm_perm.uid), and the process was not privileged (did not have the CAP_SYS_ADMIN capability).

Or (in kernels before 2.6.9), SHM_LOCK or SHM_UNLOCK was specified, but the process was not privileged (did not have the CAP_IPC_LOCK capability). (Since Linux 2.6.9, this error can also occur if the RLIMIT_MEMLOCK is 0 and the caller is not privileged.)

Remarks

The caller must ensure that a segment is eventually destroyed; otherwise its pages that were faulted in will remain in memory or swap.

In kernels before 2.6.10, only a privileged process could employ SHM_LOCK and SHM_UNLOCK. Since kernel 2.6.10, an unprivileged process can employ these operations if its effective UID matches the owner or creator UID of the segment, and (for SHM_LOCK) the amount of memory to be locked falls within the RLIMIT_MEMLOCK resource limit (see sys_setrlimit).

The IPC_INFO, SHM_STAT and SHM_INFO operations are used by the ipcs program to provide information on allocated resources. In the future these may modified or moved to a /proc file system interface.

Linux permits a process to attach (SHMAT) a shared memory segment that has already been marked for deletion using SHMCTL with IPC_RMID command.

See /samples/ipc/shmem.asm for an example.