SHMCTL(2)
β¨ SHMCTL(2) β¨
okay bestie, sit DOWN because we're talking about shmctl β that's the shared memory control girlie from the System Calls Manual π
she's part of the Standard C Library (libc, -lc) and she is literally the queen of telling your shared memory segments what to do.
πΈ SYNOPSIS πΈ
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int
shmctl(int shmid, int cmd, struct shmid_ds *buf);
π DESCRIPTION π
So basically she performs whatever action you specify in cmd, and she does it on the shared memory segment that's identified by shmid. That's it. That's the whole vibe check. π¦
Here's the tea on each cmd:
IPC_STATβ she fetches the segment'sstruct shmid_dsand stores it in the memory thatbufpoints to. Grabby hands, but make it legal β¨IPC_SETβ she changes theshm_perm.uid,shm_perm.gid, andshm_perm.modemembers of the segment'sstruct shmid_dsso they match the ones in the struct thatbufpoints to. β οΈ but like, permission check: the calling process's effective uid has to match eithershm_perm.uidorshm_perm.cuid, OR it needs superuser privileges. No shortcuts bestie.IPC_RMIDβ she removes the segment from the system. BUT β and this is the important part β the removal does not take effect until every single process that has attached the segment has exited. She's patient like that. π§ For the operation to succeed, the calling process's effective uid must matchshm_perm.uidorshm_perm.cuid, or the process must have superuser privileges.
lowkey a big deal: if thekern.ipc.shm_allow_removedsysctl(3)variable is set to0, then once theIPC_RMIDoperation has taken place, no further processes will be allowed to attach the segment. That's it. Doors closed. Not me being dramatic, that's just what it says π
The shmid_ds structure is defined as follows β copy it exactly, don't freestyle it:
struct shmid_ds {
struct ipc_perm shm_perm; /* operation permission structure */
size_t shm_segsz; /* size of segment in bytes */
pid_t shm_lpid; /* process ID of last shared memory op */
pid_t shm_cpid; /* process ID of creator */
int shm_nattch; /* number of current attaches */
time_t shm_atime; /* time of last shmat() */
time_t shm_dtime; /* time of last shmdt() */
time_t shm_ctime; /* time of last change by shmctl() */
};
π RETURN VALUES π
The shmctl() function returns the value 0 if successful. If not? She returns the value -1 and sets the global variable errno to indicate the error. The duality of woman π
π₯ ERRORS π₯
The shmctl() system call will fail if:
[EINVAL]β invalid operation, or no shared memory segment was found corresponding toshmid. She looked. She found nothing. π[EPERM]β the calling process's effective uid does not match the uid of the shared memory segment's owner or creator. Not your segment, not your rules, bestie.[EACCES]β permission denied due to a mismatch between the operation and the mode of the shared memory segment. The vibes were off, so the access was denied.
π SEE ALSO π
shmat(2), shmdt(2), shmget(2), ftok(3)
and that's the whole page, no cap π«Ά this Debian-flavoured icon dropped on October 10, 2015 β yeah she's an elder but she ate and left no crumbs β¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1995 David Hovemeyer <daveho@infocom.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms