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 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified 1993-07-28, Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1993-11-28, Giorgio Ciucci <giorgio@crcc.it>
29 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2001-02-18, Andries Brouwer <aeb@cwi.nl>
31 .\" Modified 2002-01-05, 2004-05-27, 2004-06-17,
32 .\" Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Modified 2004-10-11, aeb
34 .\" Modified, Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\" Language and formatting clean-ups
36 .\" Updated shmid_ds structure definitions
37 .\" Added information on SHM_DEST and SHM_LOCKED flags
38 .\" Noted that CAP_IPC_LOCK is not required for SHM_UNLOCK
39 .\" since kernel 2.6.9
40 .\" Modified, 2004-11-25, mtk, notes on 2.6.9 RLIMIT_MEMLOCK changes
41 .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
42 .\" attaches to a segment that has already been marked for deletion.
43 .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
44 .\" 2018-03-20, dbueso: Added SHM_STAT_ANY description.
46 .TH SHMCTL 2 2020-04-11 "Linux" "Linux Programmer's Manual"
48 shmctl \- System V shared memory control
51 .B #include <sys/ipc.h>
53 .B #include <sys/shm.h>
55 .BI "int shmctl(int " shmid ", int " cmd ", struct shmid_ds *" buf );
59 performs the control operation specified by
61 on the System\ V shared memory segment whose identifier is given in
66 argument is a pointer to a \fIshmid_ds\fP structure,
67 defined in \fI<sys/shm.h>\fP as follows:
72 struct ipc_perm shm_perm; /* Ownership and permissions */
73 size_t shm_segsz; /* Size of segment (bytes) */
74 time_t shm_atime; /* Last attach time */
75 time_t shm_dtime; /* Last detach time */
76 time_t shm_ctime; /* Creation time/time of last
77 modification via shmctl() */
78 pid_t shm_cpid; /* PID of creator */
79 pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
80 shmatt_t shm_nattch; /* No. of current attaches */
88 structure are as follows:
93 structure (see below) that specifies the access permissions
94 on the shared memory segment.
97 Size in bytes of the shared memory segment.
100 ID of the process that created the shared memory segment.
103 ID of the last process that executed a
107 system call on this segment.
110 Number of processes that have this segment attached.
115 system call that attached this segment.
120 system call that detached tgis segment.
123 Time of creation of segment or time of the last
130 structure is defined as follows
131 (the highlighted fields are settable using
137 key_t __key; /* Key supplied to shmget(2) */
138 uid_t \fBuid\fP; /* Effective UID of owner */
139 gid_t \fBgid\fP; /* Effective GID of owner */
140 uid_t cuid; /* Effective UID of creator */
141 gid_t cgid; /* Effective GID of creator */
142 unsigned short \fBmode\fP; /* \fBPermissions\fP + SHM_DEST and
144 unsigned short __seq; /* Sequence number */
149 The least significant 9 bits of the
153 structure define the access permissions for the shared memory segment.
154 The permission bits are as follows:
165 Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
166 (It is not necessary to have execute permission on a segment
167 in order to perform a
178 Copy information from the kernel data structure associated with
182 structure pointed to by \fIbuf\fP.
183 The caller must have read permission on the
184 shared memory segment.
187 Write the values of some members of the
189 structure pointed to by
191 to the kernel data structure associated with this shared memory segment,
195 The following fields can be changed:
196 \fIshm_perm.uid\fP, \fIshm_perm.gid\fP,
197 and (the least significant 9 bits of) \fIshm_perm.mode\fP.
198 The effective UID of the calling process must match the owner
201 .RI ( shm_perm.cuid )
202 of the shared memory segment, or the caller must be privileged.
205 Mark the segment to be destroyed.
206 The segment will actually be destroyed
207 only after the last process detaches it (i.e., when the
209 member of the associated structure
212 The caller must be the owner or creator of the segment, or be privileged.
217 If a segment has been marked for destruction, then the (nonstandard)
221 field in the associated data structure retrieved by
225 The caller \fImust\fP ensure that a segment is eventually destroyed;
226 otherwise its pages that were faulted in will remain in memory or swap.
228 See also the description of
229 .I /proc/sys/kernel/shm_rmid_forced
233 .BR IPC_INFO " (Linux-specific)"
234 Return information about system-wide shared memory limits and
235 parameters in the structure pointed to by
237 This structure is of type
239 (thus, a cast is required),
244 feature test macro is defined:
249 unsigned long shmmax; /* Maximum segment size */
250 unsigned long shmmin; /* Minimum segment size;
252 unsigned long shmmni; /* Maximum number of segments */
253 unsigned long shmseg; /* Maximum number of segments
254 that a process can attach;
255 unused within kernel */
256 unsigned long shmall; /* Maximum number of pages of
257 shared memory, system-wide */
267 settings can be changed via
269 files of the same name; see
273 .BR SHM_INFO " (Linux-specific)"
276 structure whose fields contain information
277 about system resources consumed by shared memory.
278 This structure is defined in
282 feature test macro is defined:
287 int used_ids; /* # of currently existing
289 unsigned long shm_tot; /* Total number of shared
291 unsigned long shm_rss; /* # of resident shared
293 unsigned long shm_swp; /* # of swapped shared
295 unsigned long swap_attempts;
296 /* Unused since Linux 2.4 */
297 unsigned long swap_successes;
298 /* Unused since Linux 2.4 */
303 .BR SHM_STAT " (Linux-specific)"
310 argument is not a segment identifier, but instead an index into
311 the kernel's internal array that maintains information about
312 all shared memory segments on the system.
314 .BR SHM_STAT_ANY " (Linux-specific, since Linux 4.17)"
321 is not checked for read access for
323 meaning that any user can employ this operation (just as any user may read
324 .IR /proc/sysvipc/shm
325 to obtain the same information).
327 The caller can prevent or allow swapping of a shared
328 memory segment with the following \fIcmd\fP values:
330 .BR SHM_LOCK " (Linux-specific)"
331 Prevent swapping of the shared memory segment.
332 The caller must fault in
333 any pages that are required to be present after locking is enabled.
334 If a segment has been locked, then the (nonstandard)
338 field in the associated data structure retrieved by
342 .BR SHM_UNLOCK " (Linux-specific)"
343 Unlock the segment, allowing it to be swapped out.
345 In kernels before 2.6.10, only a privileged process
350 Since kernel 2.6.10, an unprivileged process can employ these operations
351 if its effective UID matches the owner or creator UID of the segment, and
354 the amount of memory to be locked falls within the
358 .\" There was some weirdness in 2.6.9: SHM_LOCK and SHM_UNLOCK could
359 .\" be applied to a segment, regardless of ownership of the segment.
360 .\" This was a botch-up in the move to RLIMIT_MEMLOCK, and was fixed
361 .\" in 2.6.10. MTK, May 2005
367 operation returns the index of the highest used entry in the
368 kernel's internal array recording information about all
369 shared memory segments.
370 (This information can be used with repeated
374 operations to obtain information about all shared memory segments
378 operation returns the identifier of the shared memory segment
379 whose index was given in
381 Other operations return 0 on success.
383 On error, \-1 is returned, and
385 is set appropriately.
389 \fBIPC_STAT\fP or \fBSHM_STAT\fP is requested and
390 \fIshm_perm.mode\fP does not allow read access for
392 and the calling process does not have the
394 capability in the user namespace that governs its IPC namespace.
403 but the address pointed to by
408 \fIshmid\fP points to a removed identifier.
411 \fIshmid\fP is not a valid identifier, or \fIcmd\fP
412 is not a valid command.
417 operation, the index value specified in
419 referred to an array slot that is currently unused.
422 (In kernels since 2.6.9),
424 was specified and the size of the to-be-locked segment would mean
425 that the total bytes in locked shared memory segments would exceed
426 the limit for the real user ID of the calling process.
427 This limit is defined by the
429 soft resource limit (see
433 \fBIPC_STAT\fP is attempted, and the GID or UID value
434 is too large to be stored in the structure pointed to by
438 \fBIPC_SET\fP or \fBIPC_RMID\fP is attempted, and the
439 effective user ID of the calling process is not that of the creator
445 and the process was not privileged (Linux: did not have the
449 Or (in kernels before 2.6.9),
453 was specified, but the process was not privileged
454 (Linux: did not have the
457 (Since Linux 2.6.9, this error can also occur if the
459 is 0 and the caller is not privileged.)
461 POSIX.1-2001, POSIX.1-2008, SVr4.
462 .\" SVr4 documents additional error conditions EINVAL,
463 .\" ENOENT, ENOSPC, ENOMEM, EEXIST. Neither SVr4 nor SVID documents
464 .\" an EIDRM error condition.
470 isn't required on Linux or by any version of POSIX.
472 some old implementations required the inclusion of these header files,
473 and the SVID also documented their inclusion.
474 Applications intended to be portable to such old systems may need
475 to include these header files.
476 .\" Like Linux, the FreeBSD man pages still document
477 .\" the inclusion of these header files.
484 operations are used by the
486 program to provide information on allocated resources.
487 In the future, these may modified or moved to a
489 filesystem interface.
491 Linux permits a process to attach
493 a shared memory segment that has already been marked for deletion
495 .IR shmctl(IPC_RMID) .
496 This feature is not available on other UNIX implementations;
497 portable applications should avoid relying on it.
499 Various fields in a \fIstruct shmid_ds\fP were typed as
505 To take advantage of this,
506 a recompilation under glibc-2.1.91 or later should suffice.
507 (The kernel distinguishes old and new calls by an
516 .BR capabilities (7),