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 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified 1993-07-28, Rik Faith <faith@cs.unc.edu>
26 .\" Modified 1993-11-28, Giorgio Ciucci <giorgio@crcc.it>
27 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
28 .\" Modified 2001-02-18, Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 2002-01-05, 2004-05-27, 2004-06-17,
30 .\" Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Modified 2004-10-11, aeb
32 .\" Modified, Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Language and formatting clean-ups
34 .\" Updated shmid_ds structure definitions
35 .\" Added information on SHM_DEST and SHM_LOCKED flags
36 .\" Noted that CAP_IPC_LOCK is not required for SHM_UNLOCK
37 .\" since kernel 2.6.9
38 .\" Modified, 2004-11-25, mtk, notes on 2.6.9 RLIMIT_MEMLOCK changes
39 .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
40 .\" attaches to a segment that has already been marked for deletion.
41 .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
43 .TH SHMCTL 2 2008-08-07 "Linux" "Linux Programmer's Manual"
45 shmctl \- shared memory control
48 .B #include <sys/ipc.h>
50 .B #include <sys/shm.h>
52 .BI "int shmctl(int " shmid ", int " cmd ", struct shmid_ds *" buf );
56 performs the control operation specified by
58 on the shared memory segment whose identifier is given in
63 argument is a pointer to a \fIshmid_ds\fP structure,
64 defined in \fI<sys/shm.h>\fP as follows:
69 struct ipc_perm shm_perm; /* Ownership and permissions */
70 size_t shm_segsz; /* Size of segment (bytes) */
71 time_t shm_atime; /* Last attach time */
72 time_t shm_dtime; /* Last detach time */
73 time_t shm_ctime; /* Last change time */
74 pid_t shm_cpid; /* PID of creator */
75 pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
76 shmatt_t shm_nattch; /* No. of current attaches */
84 structure is defined as follows
85 (the highlighted fields are settable using
91 key_t __key; /* Key supplied to shmget(2) */
92 uid_t \fBuid\fP; /* Effective UID of owner */
93 gid_t \fBgid\fP; /* Effective GID of owner */
94 uid_t cuid; /* Effective UID of creator */
95 gid_t cgid; /* Effective GID of creator */
96 unsigned short \fBmode\fP; /* \fBPermissions\fP + SHM_DEST and
98 unsigned short __seq; /* Sequence number */
109 Copy information from the kernel data structure associated with
113 structure pointed to by \fIbuf\fP.
114 The caller must have read permission on the
115 shared memory segment.
118 Write the values of some members of the
120 structure pointed to by
122 to the kernel data structure associated with this shared memory segment,
126 The following fields can be changed:
127 \fIshm_perm.uid\fP, \fIshm_perm.gid\fP,
128 and (the least significant 9 bits of) \fIshm_perm.mode\fP.
129 The effective UID of the calling process must match the owner
132 .RI ( shm_perm.cuid )
133 of the shared memory segment, or the caller must be privileged.
136 Mark the segment to be destroyed.
137 The segment will only actually be destroyed
138 after the last process detaches it (i.e., when the
140 member of the associated structure
143 The caller must be the owner or creator, or be privileged.
144 If a segment has been marked for destruction, then the (nonstandard)
148 field in the associated data structure retrieved by
152 The caller \fImust\fP ensure that a segment is eventually destroyed;
153 otherwise its pages that were faulted in will remain in memory or swap.
155 .BR IPC_INFO " (Linux-specific)"
156 Returns information about system-wide shared memory limits and
157 parameters in the structure pointed to by
159 This structure is of type
161 (thus, a cast is required),
166 feature test macro is defined:
171 unsigned long shmmax; /* Maximum segment size */
172 unsigned long shmmin; /* Minimum segment size;
174 unsigned long shmmni; /* Maximum number of segments */
175 unsigned long shmseg; /* Maximum number of segments
176 that a process can attach;
177 unused within kernel */
178 unsigned long shmall; /* Maximum number of pages of
179 shared memory, system-wide */
189 settings can be changed via
191 files of the same name; see
195 .BR SHM_INFO " (Linux-specific)"
198 structure whose fields contain information
199 about system resources consumed by shared memory.
200 This structure is defined in
204 feature test macro is defined:
209 int used_ids; /* # of currently existing
211 unsigned long shm_tot; /* Total number of shared
213 unsigned long shm_rss; /* # of resident shared
215 unsigned long shm_swp; /* # of swapped shared
217 unsigned long swap_attempts;
218 /* Unused since Linux 2.4 */
219 unsigned long swap_successes;
220 /* Unused since Linux 2.4 */
225 .BR SHM_STAT " (Linux-specific)"
232 argument is not a segment identifier, but instead an index into
233 the kernel's internal array that maintains information about
234 all shared memory segments on the system.
236 The caller can prevent or allow swapping of a shared
237 memory segment with the following \fIcmd\fP values:
240 .BR SHM_LOCK " (Linux-specific)"
241 Prevent swapping of the shared memory segment.
242 The caller must fault in
243 any pages that are required to be present after locking is enabled.
244 If a segment has been locked, then the (nonstandard)
248 field in the associated data structure retrieved by
252 .BR SHM_UNLOCK " (Linux-specific)"
253 Unlock the segment, allowing it to be swapped out.
255 In kernels before 2.6.10, only a privileged process
260 Since kernel 2.6.10, an unprivileged process can employ these operations
261 if its effective UID matches the owner or creator UID of the segment, and
264 the amount of memory to be locked falls within the
268 .\" There was some weirdness in 2.6.9: SHM_LOCK and SHM_UNLOCK could
269 .\" be applied to a segment, regardless of ownership of the segment.
270 .\" This was a botch-up in the move to RLIMIT_MEMLOCK, and was fixed
271 .\" in 2.6.10. MTK, May 2005
277 operation returns the index of the highest used entry in the
278 kernel's internal array recording information about all
279 shared memory segments.
280 (This information can be used with repeated
282 operations to obtain information about all shared memory segments
286 operation returns the identifier of the shared memory segment
287 whose index was given in
289 Other operations return 0 on success.
291 On error, \-1 is returned, and
293 is set appropriately.
297 \fBIPC_STAT\fP or \fBSHM_STAT\fP is requested and
298 \fIshm_perm.mode\fP does not allow read access for
300 and the calling process does not have the
311 but the address pointed to by
316 \fIshmid\fP points to a removed identifier.
319 \fIshmid\fP is not a valid identifier, or \fIcmd\fP
320 is not a valid command.
323 operation, the index value specified in
325 referred to an array slot that is currently unused.
328 (In kernels since 2.6.9),
330 was specified and the size of the to-be-locked segment would mean
331 that the total bytes in locked shared memory segments would exceed
332 the limit for the real user ID of the calling process.
333 This limit is defined by the
335 soft resource limit (see
339 \fBIPC_STAT\fP is attempted, and the GID or UID value
340 is too large to be stored in the structure pointed to by
344 \fBIPC_SET\fP or \fBIPC_RMID\fP is attempted, and the
345 effective user ID of the calling process is not that of the creator
351 and the process was not privileged (Linux: did not have the
355 Or (in kernels before 2.6.9),
359 was specified, but the process was not privileged
360 (Linux: did not have the
363 (Since Linux 2.6.9, this error can also occur if the
365 is 0 and the caller is not privileged.)
368 .\" SVr4 documents additional error conditions EINVAL,
369 .\" ENOENT, ENOSPC, ENOMEM, EEXIST. Neither SVr4 nor SVID documents
370 .\" an EIDRM error condition.
377 operations are used by the
379 program to provide information on allocated resources.
380 In the future these may modified or moved to a /proc file system
383 Linux permits a process to attach
385 a shared memory segment that has already been marked for deletion
387 .IR shmctl(IPC_RMID) .
388 This feature is not available on other UNIX implementations;
389 portable applications should avoid relying on it.
391 Various fields in a \fIstruct shmid_ds\fP were typed as
397 To take advantage of this,
398 a recompilation under glibc-2.1.91 or later should suffice.
399 (The kernel distinguishes old and new calls by an
408 .BR capabilities (7),