shmctl() — Shared memory control operations

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <sys/shm.h>

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

General description

The shmctl() function provides a variety of shared memory control operations on the shared memory segment identified by the argument, shmid.

The argument cmd specifies the shared memory control operation and may be any of the following values:
IPC_STAT
This command obtains status information for the shared memory segment specified by the shared memory identifier, shmid. It places the current value of each member of the shmid_ds data structure associated with shmid into the structure pointed to by buf. The contents of this structure is defined in <sys/shm.h>. This command requires read permission.
IPC_SET
Set the value of the following members of the shmid_ds data structure associated with shmid to the corresponding value in the structure pointed to by buf:

       shm_perm.uid
       shm_perm.gid
       shm_perm.mode  (only the low-order 9 bits)

This command can only be executed by a process that has an effective user ID equal to either that of a process with appropriate privileges or to the value of shm_perm.cuid or shm_perm.uid in the shmid_ds data structure associated with shmid.

Using the IPC_SET function to change the IPC_MODE for an __IPC_MEGA shared memory segment will have an immediate effect on all attaches to the target segment. That is, the read and write access of all current attachers is immediately affected by the permissions specified in the new IPC_MODE. To determine how the new mode affects access, you must consider the effect of all three parts of the mode field (the owner permissions, group permissions and other permissions). If all three read and all three write permissions in the new mode are set off, then the access for all attachors is changed to read. If any of the three read permission bits is set on but the corresponding write permission bit is off, then the access for all attachors is changed to read. Otherwise, the access of all attachors is changed to write.

IPC_RMID
Remove the shared memory identified specified by shmid from the system and destroy the shared memory segment and shmid_ds data structure associated with shmid. This command can only be executed by a process that has an effective user ID equal to either that of a process with appropriate privileges or to the value of shm_perm.cuid or shm_perm.uid in the shmid_ds data structure associated with shmid. The remove will be completed asynchronous to the return from the shmctl() function, when the last attachment is detached. When IPC_RMID is processed, no further attaches will be allowed.

The IPC_STAT option of shmctl() returns a structure named shmid_ds (mapped in shm.h). shmid_ds contains status information for the requested shared memory segment.

As part of its own dump priority support, the USS Kernel will be adding a new field to shmid_ds. This field will contain the dump priority of the requested shared memory segment. The new field will be added to the shmid_ds structure in shm.h.

Returned value

If successful, shmctl() returns 0.

If unsuccessful, shmctl() returns -1 and sets errno to one of the following values:
Error Code
Description
EACCES
The argument cmd is equal to IPC_STAT but the calling process does not have read permission.
EINVAL
The value of shmid is not a valid shared memory identifier or the value of cmd is not a valid command.
EPERM
The argument cmd is equal to either IPC_RMID or IPC_SET and the effective user ID of the calling process is not equal to that of a process with appropriate privileges and it is not equal to the value of shm_perm.cuid or shm_perm.uid in the data structure associated with shmid.

Related information