shmctl#
Control or query a System V shared memory segment.
shmctl is the management primitive for shared memory segments created
with shmget. It performs one of a small set of operations
named by CMD — reading the segment’s metadata, changing its
permissions, or marking it for removal. ID is the segment identifier
returned by shmget. ARG is a buffer scalar that either
supplies an input shmid_ds structure (for IPC_SET) or receives the
returned one (for IPC_STAT); for IPC_RMID it is ignored.
Synopsis#
use IPC::SysV qw(IPC_STAT IPC_SET IPC_RMID);
shmctl $id, IPC_STAT, $buf or die "shmctl STAT: $!";
shmctl $id, IPC_SET, $buf or die "shmctl SET: $!";
shmctl $id, IPC_RMID, 0 or die "shmctl RMID: $!";
What you get back#
Return value follows the ioctl convention, not the usual
0-on-success Unix pattern:
The string
"0 but true"when the underlying call returned zero. This compares numerically equal to0but is boolean-true, soshmctl(...) or diestill works.Any other non-zero value otherwise.
The practical rule: test for falsehood with
shmctl(...) or die "...: $!" and never compare the return value to
literal 0 with ==.
The IPC_STAT form writes a packed shmid_ds structure into ARG.
Unpack it with IPC::SysV’s helper or with a manual
unpack template; the structure layout is platform-dependent
so relying on a fixed template is not portable.
Global state it touches#
$!is set on failure to the errno from the underlyingshmctl(2)call. Typical values:EINVAL(badIDorCMD),EACCES(insufficient permissions for the operation),EPERM(non-owner attemptingIPC_SETorIPC_RMID),EFAULT(badARGbuffer forIPC_STAT/IPC_SET).
Examples#
Read a segment’s metadata. IPC::SysV provides the shmid_ds
unpacker; without it, the structure is an opaque byte string:
use IPC::SysV qw(IPC_STAT);
my $buf = "";
shmctl($id, IPC_STAT, $buf) or die "stat failed: $!";
# $buf now holds the packed shmid_ds structure
Change the permissions on an existing segment. The pattern is read-modify-write on the packed structure:
use IPC::SysV qw(IPC_STAT IPC_SET);
my $buf = "";
shmctl($id, IPC_STAT, $buf) or die $!;
# ... modify the mode bits inside $buf ...
shmctl($id, IPC_SET, $buf) or die $!;
Mark a segment for removal. The segment is destroyed once the last
process detaches from it; IPC_RMID only flags it:
use IPC::SysV qw(IPC_RMID);
shmctl($id, IPC_RMID, 0) or die "rmid failed: $!";
Guarding on the "0 but true" return. Because the zero case is a
string that is boolean-true, the idiomatic check is the or die
form, not a numeric comparison:
my $rv = shmctl($id, IPC_STAT, $buf);
defined $rv or die "error: $!";
# do NOT write: $rv == 0 and ... # misses the "0 but true" case
Edge cases#
Missing constants:
IPC_STAT,IPC_SET,IPC_RMIDare not built-in. Withoutuse IPC::SysVthey are bareword function calls and the code either fails to compile underuse strictor silently passes the wrong integer. Alwaysuse IPC::SysV qw(...).Owner-only operations:
IPC_SETandIPC_RMIDrequire the effective uid to match the segment’s creator or owner, or the process to haveCAP_SYS_ADMIN. Otherwise$!becomesEPERM.IPC_RMIDsemantics: marking for removal does not detach the caller and does not invalidate existing attachments. The segment stays usable until every process has calledshmdton it; only then is it actually destroyed. A process that attaches afterIPC_RMIDgetsEIDRMorEINVALdepending on timing.Structure layout is not portable:
shmid_dsfields and offsets vary between kernels and libc versions. Do not hand-codeunpacktemplates for scripts that need to run on multiple platforms; useIPC::SysV::ShmIdor a similar wrapper.Buffer sizing for
IPC_STAT:ARGmust be a scalar of at leastsizeof(shmid_ds)bytes on output. Perl grows it as needed; pre-sizing with$buf = "\0" x 256is common but not required.Unconfigured kernels: on Linux built without
CONFIG_SYSVIPC, the call fails withENOSYS. Container images that drop SysV IPC exhibit the same behaviour.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
shmget— create or look up the segment whoseIDshmctloperates onshmread— copy bytes out of an attached segment; uses theIDreturned byshmget, not anshmctlresultshmwrite— copy bytes into an attached segmentmsgctl— same control pattern for System V message queues; same"0 but true"return conventionsemctl— same control pattern for System V semaphore sets; richerCMDset but identical return-value rulesIPC::SysV— provides theIPC_STAT/IPC_SET/IPC_RMIDconstants and theshmid_dspackers