1 .\" Copyright (c) 1993 Luigi P. Bai (lpb@softint.com) July 28, 1993
2 .\" and Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
3 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Modified 1993-07-28, Rik Faith <faith@cs.unc.edu>
8 .\" Modified 1993-11-28, Giorgio Ciucci <giorgio@crcc.it>
9 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
10 .\" Modified 2001-02-18, Andries Brouwer <aeb@cwi.nl>
11 .\" Modified 2002-01-05, 2004-05-27, 2004-06-17,
12 .\" Michael Kerrisk <mtk.manpages@gmail.com>
13 .\" Modified 2004-10-11, aeb
14 .\" Modified, Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
15 .\" Language and formatting clean-ups
16 .\" Updated shmid_ds structure definitions
17 .\" Added information on SHM_DEST and SHM_LOCKED flags
18 .\" Noted that CAP_IPC_LOCK is not required for SHM_UNLOCK
19 .\" since kernel 2.6.9
20 .\" Modified, 2004-11-25, mtk, notes on 2.6.9 RLIMIT_MEMLOCK changes
21 .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
22 .\" attaches to a segment that has already been marked for deletion.
23 .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
24 .\" 2018-03-20, dbueso: Added SHM_STAT_ANY description.
26 .TH SHMCTL 2 2022-09-09 "Linux man-pages (unreleased)"
28 shmctl \- System V shared memory control
31 .RI ( libc ", " \-lc )
34 .B #include <sys/shm.h>
36 .BI "int shmctl(int " shmid ", int " cmd ", struct shmid_ds *" buf );
40 performs the control operation specified by
42 on the System\ V shared memory segment whose identifier is given in
47 argument is a pointer to a \fIshmid_ds\fP structure,
48 defined in \fI<sys/shm.h>\fP as follows:
53 struct ipc_perm shm_perm; /* Ownership and permissions */
54 size_t shm_segsz; /* Size of segment (bytes) */
55 time_t shm_atime; /* Last attach time */
56 time_t shm_dtime; /* Last detach time */
57 time_t shm_ctime; /* Creation time/time of last
58 modification via shmctl() */
59 pid_t shm_cpid; /* PID of creator */
60 pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
61 shmatt_t shm_nattch; /* No. of current attaches */
69 structure are as follows:
74 structure (see below) that specifies the access permissions
75 on the shared memory segment.
78 Size in bytes of the shared memory segment.
83 system call that attached this segment.
88 system call that detached tgis segment.
91 Time of creation of segment or time of the last
97 ID of the process that created the shared memory segment.
100 ID of the last process that executed a
104 system call on this segment.
107 Number of processes that have this segment attached.
111 structure is defined as follows
112 (the highlighted fields are settable using
118 key_t __key; /* Key supplied to shmget(2) */
119 uid_t \fBuid\fP; /* Effective UID of owner */
120 gid_t \fBgid\fP; /* Effective GID of owner */
121 uid_t cuid; /* Effective UID of creator */
122 gid_t cgid; /* Effective GID of creator */
123 unsigned short \fBmode\fP; /* \fBPermissions\fP + SHM_DEST and
125 unsigned short __seq; /* Sequence number */
130 The least significant 9 bits of the
134 structure define the access permissions for the shared memory segment.
135 The permission bits are as follows:
146 Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
147 (It is not necessary to have execute permission on a segment
148 in order to perform a
159 Copy information from the kernel data structure associated with
163 structure pointed to by \fIbuf\fP.
164 The caller must have read permission on the
165 shared memory segment.
168 Write the values of some members of the
170 structure pointed to by
172 to the kernel data structure associated with this shared memory segment,
177 The following fields are updated:
178 \fIshm_perm.uid\fP, \fIshm_perm.gid\fP,
179 and (the least significant 9 bits of) \fIshm_perm.mode\fP.
181 The effective UID of the calling process must match the owner
184 .RI ( shm_perm.cuid )
185 of the shared memory segment, or the caller must be privileged.
188 Mark the segment to be destroyed.
189 The segment will actually be destroyed
190 only after the last process detaches it (i.e., when the
192 member of the associated structure
195 The caller must be the owner or creator of the segment, or be privileged.
200 If a segment has been marked for destruction, then the (nonstandard)
204 field in the associated data structure retrieved by
208 The caller \fImust\fP ensure that a segment is eventually destroyed;
209 otherwise its pages that were faulted in will remain in memory or swap.
211 See also the description of
212 .I /proc/sys/kernel/shm_rmid_forced
216 .BR IPC_INFO " (Linux-specific)"
217 Return information about system-wide shared memory limits and
218 parameters in the structure pointed to by
220 This structure is of type
222 (thus, a cast is required),
227 feature test macro is defined:
232 unsigned long shmmax; /* Maximum segment size */
233 unsigned long shmmin; /* Minimum segment size;
235 unsigned long shmmni; /* Maximum number of segments */
236 unsigned long shmseg; /* Maximum number of segments
237 that a process can attach;
238 unused within kernel */
239 unsigned long shmall; /* Maximum number of pages of
240 shared memory, system\-wide */
250 settings can be changed via
252 files of the same name; see
256 .BR SHM_INFO " (Linux-specific)"
259 structure whose fields contain information
260 about system resources consumed by shared memory.
261 This structure is defined in
265 feature test macro is defined:
270 int used_ids; /* # of currently existing
272 unsigned long shm_tot; /* Total number of shared
274 unsigned long shm_rss; /* # of resident shared
276 unsigned long shm_swp; /* # of swapped shared
278 unsigned long swap_attempts;
279 /* Unused since Linux 2.4 */
280 unsigned long swap_successes;
281 /* Unused since Linux 2.4 */
286 .BR SHM_STAT " (Linux-specific)"
293 argument is not a segment identifier, but instead an index into
294 the kernel's internal array that maintains information about
295 all shared memory segments on the system.
297 .BR SHM_STAT_ANY " (Linux-specific, since Linux 4.17)"
304 is not checked for read access for
306 meaning that any user can employ this operation (just as any user may read
308 to obtain the same information).
310 The caller can prevent or allow swapping of a shared
311 memory segment with the following \fIcmd\fP values:
313 .BR SHM_LOCK " (Linux-specific)"
314 Prevent swapping of the shared memory segment.
315 The caller must fault in
316 any pages that are required to be present after locking is enabled.
317 If a segment has been locked, then the (nonstandard)
321 field in the associated data structure retrieved by
325 .BR SHM_UNLOCK " (Linux-specific)"
326 Unlock the segment, allowing it to be swapped out.
328 In kernels before 2.6.10, only a privileged process
333 Since kernel 2.6.10, an unprivileged process can employ these operations
334 if its effective UID matches the owner or creator UID of the segment, and
337 the amount of memory to be locked falls within the
341 .\" There was some weirdness in 2.6.9: SHM_LOCK and SHM_UNLOCK could
342 .\" be applied to a segment, regardless of ownership of the segment.
343 .\" This was a botch-up in the move to RLIMIT_MEMLOCK, and was fixed
344 .\" in 2.6.10. MTK, May 2005
350 operation returns the index of the highest used entry in the
351 kernel's internal array recording information about all
352 shared memory segments.
353 (This information can be used with repeated
357 operations to obtain information about all shared memory segments
361 operation returns the identifier of the shared memory segment
362 whose index was given in
364 Other operations return 0 on success.
366 On error, \-1 is returned, and
368 is set to indicate the error.
372 \fBIPC_STAT\fP or \fBSHM_STAT\fP is requested and
373 \fIshm_perm.mode\fP does not allow read access for
375 and the calling process does not have the
377 capability in the user namespace that governs its IPC namespace.
386 but the address pointed to by
391 \fIshmid\fP points to a removed identifier.
394 \fIshmid\fP is not a valid identifier, or \fIcmd\fP
395 is not a valid command.
400 operation, the index value specified in
402 referred to an array slot that is currently unused.
405 (In kernels since 2.6.9),
407 was specified and the size of the to-be-locked segment would mean
408 that the total bytes in locked shared memory segments would exceed
409 the limit for the real user ID of the calling process.
410 This limit is defined by the
412 soft resource limit (see
416 \fBIPC_STAT\fP is attempted, and the GID or UID value
417 is too large to be stored in the structure pointed to by
421 \fBIPC_SET\fP or \fBIPC_RMID\fP is attempted, and the
422 effective user ID of the calling process is not that of the creator
428 and the process was not privileged (Linux: did not have the
432 Or (in kernels before 2.6.9),
436 was specified, but the process was not privileged
437 (Linux: did not have the
440 (Since Linux 2.6.9, this error can also occur if the
442 is 0 and the caller is not privileged.)
444 POSIX.1-2001, POSIX.1-2008, SVr4.
445 .\" SVr4 documents additional error conditions EINVAL,
446 .\" ENOENT, ENOSPC, ENOMEM, EEXIST. Neither SVr4 nor SVID documents
447 .\" an EIDRM error condition.
454 operations are used by the
456 program to provide information on allocated resources.
457 In the future, these may modified or moved to a
459 filesystem interface.
461 Linux permits a process to attach
463 a shared memory segment that has already been marked for deletion
465 .IR shmctl(IPC_RMID) .
466 This feature is not available on other UNIX implementations;
467 portable applications should avoid relying on it.
469 Various fields in a \fIstruct shmid_ds\fP were typed as
475 To take advantage of this,
476 a recompilation under glibc-2.1.91 or later should suffice.
477 (The kernel distinguishes old and new calls by an
486 .BR capabilities (7),