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@gmx.net>
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@gmx.net>
31 .\" Modified 2004-10-11, aeb
32 .\" Modified, Nov 2004, Michael Kerrisk <mtk-manpages@gmx.net>
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 2005-05-30 "Linux" "Linux Programmer's Manual"
45 shmctl \- shared memory control
48 .B #include <sys/ipc.h>
49 .B #include <sys/shm.h>
51 .BI "int shmctl(int " shmid ", int " cmd ", struct shmid_ds *" buf );
55 performs the control operation specified by
57 on the shared memory segment whose identifier is given in
62 argument is a pointer to a \fIshmid_ds\fP structure,
63 defined in <sys/shm.h> as follows:
68 struct ipc_perm shm_perm; /* Ownership and permissions */
69 size_t shm_segsz; /* Size of segment (bytes) */
70 time_t shm_atime; /* Last attach time */
71 time_t shm_dtime; /* Last detach time */
72 time_t shm_ctime; /* Last change time */
73 pid_t shm_cpid; /* PID of creator */
74 pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
75 shmatt_t shm_nattch; /* No. of current attaches */
83 structure is defined in <sys/ipc.h> as follows
84 (the highlighted fields are settable using
90 key_t key; /* Key supplied to shmget(2) */
91 uid_t \fBuid\fP; /* Effective UID of owner */
92 gid_t \fBgid\fP; /* Effective GID of owner */
93 uid_t cuid; /* Effective UID of creator */
94 gid_t cgid; /* Effective GID of creator */
95 unsigned short \fBmode\fP; /* \fBPermissions\fP + SHM_DEST and
97 unsigned short seq; /* Sequence number */
108 Copy information from the kernel data structure associated with
112 structure pointed to by \fIbuf\fP.
113 The caller must have read permission on the
114 shared memory segment.
117 Write the values of some members of the
119 structure pointed to by
121 to the kernel data structure associated with this shared memory segment,
125 The following fields can be changed:
126 \fIshm_perm.uid\fP, \fIshm_perm.gid\fP,
127 and (the least significant 9 bits of) \fIshm_perm.mode\fP.
128 The effective UID of the calling process must match the owner
131 .RI ( shm_perm.cuid )
132 of the shared memory segment, or the caller must be privileged.
135 Mark the segment to be destroyed.
136 The segment will only actually be destroyed
137 after the last process detaches it (i.e., when the
139 member of the associated structure
142 The caller must be the owner or creator, or be privileged.
143 If a segment has been marked for destruction, then the (non-standard)
147 field in the associated data structure retrieved by
151 The caller \fImust\fP ensure that a segment is eventually destroyed;
152 otherwise its pages that were faulted in will remain in memory or swap.
154 .BR IPC_INFO " (Linux specific)"
155 Returns information about system-wide shared memory limits and
156 parameters in the structure pointed to by
158 This structure is of type
160 (thus, a cast is required),
163 if the _GNU_SOURCE feature test macro is defined:
168 unsigned long shmmax; /* Max. segment size */
169 unsigned long shmmin; /* Min. segment size; always 1 */
170 unsigned long shmmni; /* Max. # of segments */
171 unsigned long shmseg; /* Max. # of segments that a
172 process can attach; unused */
173 unsigned long shmall; /* Max. # of pages of shared
174 memory, system-wide */
184 settings can be changed via
186 files of the same name; see
190 .BR SHM_INFO " (Linux specific)"
193 structure whose fields contain information
194 about system resources consumed by shared memory.
195 This structure is defined in
197 if the _GNU_SOURCE feature test macro is defined:
202 int used_ids; /* # of currently existing
204 unsigned long shm_tot; /* Total number of shared
206 unsigned long shm_rss; /* # of resident shared
208 unsigned long shm_swp; /* # of swapped shared
210 unsigned long swap_attempts; /* Unused since Linux 2.4 */
211 unsigned long swap_successes; /* Unused since Linux 2.4 */
216 .BR SHM_STAT " (Linux specific)"
223 argument is not a segment identifier, but instead an index into
224 the kernel's internal array that maintains information about
225 all shared memory segments on the system.
227 The caller can prevent or allow swapping of a shared
228 memory segment with the following \fIcmd\fP values:
231 .BR SHM_LOCK " (Linux specific)"
232 Prevent swapping of the shared memory segment.
233 The caller must fault in
234 any pages that are required to be present after locking is enabled.
235 If a segment has been locked, then the (non-standard)
239 field in the associated data structure retrieved by
243 .BR SHM_UNLOCK " (Linux specific)"
244 Unlock the segment, allowing it to be swapped out.
246 In kernels before 2.6.10, only a privileged process
251 Since kernel 2.6.10, an unprivileged process can employ these operations
252 if its effective UID matches the owner or creator UID of the segment, and
255 the amount of memory to be locked falls within the
259 .\" There was some weirdness in 2.6.9: SHM_LOCK and SHM_UNLOCK could
260 .\" be applied to a segment, regardless of ownership of the segment.
261 .\" This was a botch-up in the move to RLIMIT_MEMLOCK, and was fixed
262 .\" in 2.6.10. MTK, May 2005
268 operation returns the index of the highest used entry in the
269 kernel's internal array recording information about all
270 shared memory segments.
271 (This information can be used with repeated
273 operations to obtain information about all shared memory segments
277 operation returns the identifier of the shared memory segment
278 whose index was given in
280 Other operations return 0 on success.
282 On error, \-1 is returned, and
284 is set appropriately.
288 \fBIPC_STAT\fP or \fBSHM_STAT\fP is requested and
289 \fIshm_perm.mode\fP does not allow read access for
291 and the calling process does not have the
302 but the address pointed to by
307 \fIshmid\fP points to a removed identifier.
310 \fIshmid\fP is not a valid identifier, or \fIcmd\fP
311 is not a valid command.
314 operation, the index value specified in
316 referred to an array slot that is currently unused.
319 (In kernels since 2.6.9),
321 was specified and the size of the to-be-locked segment would mean
322 that the total bytes in locked shared memory segments would exceed
323 the limit for the real user ID of the calling process.
324 This limit is defined by the
326 soft resource limit (see
330 \fBIPC_STAT\fP is attempted, and the GID or UID value
331 is too large to be stored in the structure pointed to by
335 \fBIPC_SET\fP or \fBIPC_RMID\fP is attempted, and the
336 effective user ID of the calling process is not that of the creator
342 and the process was not privileged (Linux: did not have the
346 Or (in kernels before 2.6.9),
350 was specified, but the process was not privileged
351 (Linux: did not have the
354 (Since Linux 2.6.9, this error can also occur if the
356 is 0 and the caller is not privileged.)
359 .\" SVr4 documents additional error conditions EINVAL,
360 .\" ENOENT, ENOSPC, ENOMEM, EEXIST. Neither SVr4 nor SVID documents
361 .\" an EIDRM error condition.
368 operations are used by the
370 program to provide information on allocated resources.
371 In the future these may modified or moved to a /proc file system
374 Linux permits a process to attach
376 a shared memory segment that has already been marked for deletion
378 .IR shmctl(IPC_RMID) .
379 This feature is not available on other Unix implementations;
380 portable applications should avoid relying on it.
382 Various fields in a \fIstruct shmid_ds\fP were shorts under Linux 2.2
383 and have become longs under Linux 2.4.
384 To take advantage of this,
385 a recompilation under glibc-2.1.91 or later should suffice.
386 (The kernel distinguishes old and new calls by an IPC_64 flag in