]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/shmctl.2
shmctl.2: Refer to proc(5) for description of /proc/sys/kernel/shm_rmid_forced
[thirdparty/man-pages.git] / man2 / shmctl.2
CommitLineData
fea681da 1.\" Copyright (c) 1993 Luigi P. Bai (lpb@softint.com) July 28, 1993
6883b3e7 2.\" and Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
c11b1abf 3.\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
9.\"
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.
9907019a 14.\"
fea681da
MK
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
21.\" professionally.
9907019a 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 25.\" %%%LICENSE_END
fea681da
MK
26.\"
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,
c11b1abf 32.\" Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 33.\" Modified 2004-10-11, aeb
c11b1abf 34.\" Modified, Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
9907019a
MK
35.\" Language and formatting clean-ups
36.\" Updated shmid_ds structure definitions
37.\" Added information on SHM_DEST and SHM_LOCKED flags
c13182ef 38.\" Noted that CAP_IPC_LOCK is not required for SHM_UNLOCK
3d388649 39.\" since kernel 2.6.9
583d5fd3 40.\" Modified, 2004-11-25, mtk, notes on 2.6.9 RLIMIT_MEMLOCK changes
d9bfdb9c 41.\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
75ced2e8 42.\" attaches to a segment that has already been marked for deletion.
79dded64 43.\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
fea681da 44.\"
91bb32bf 45.TH SHMCTL 2 2013-03-18 "Linux" "Linux Programmer's Manual"
fea681da
MK
46.SH NAME
47shmctl \- shared memory control
48.SH SYNOPSIS
49.ad l
50.B #include <sys/ipc.h>
d314dbfd 51.br
fea681da
MK
52.B #include <sys/shm.h>
53.sp
54.BI "int shmctl(int " shmid ", int " cmd ", struct shmid_ds *" buf );
55.ad b
56.SH DESCRIPTION
540036b2 57.BR shmctl ()
de84c942
MK
58performs the control operation specified by
59.I cmd
60on the shared memory segment whose identifier is given in
61.IR shmid .
9907019a
MK
62.PP
63The
64.I buf
65argument is a pointer to a \fIshmid_ds\fP structure,
c84371c6 66defined in \fI<sys/shm.h>\fP as follows:
fea681da
MK
67.PP
68.in +4n
69.nf
70struct shmid_ds {
9907019a
MK
71 struct ipc_perm shm_perm; /* Ownership and permissions */
72 size_t shm_segsz; /* Size of segment (bytes) */
73 time_t shm_atime; /* Last attach time */
74 time_t shm_dtime; /* Last detach time */
75 time_t shm_ctime; /* Last change time */
76 pid_t shm_cpid; /* PID of creator */
0bfa087b 77 pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
9907019a 78 shmatt_t shm_nattch; /* No. of current attaches */
fea681da
MK
79 ...
80};
81.fi
088a639b 82.in
fea681da 83.PP
9907019a
MK
84The
85.I ipc_perm
548be2a6 86structure is defined as follows
9907019a
MK
87(the highlighted fields are settable using
88.BR IPC_SET ):
fea681da
MK
89.PP
90.in +4n
91.nf
92struct ipc_perm {
2d2892fc 93 key_t __key; /* Key supplied to shmget(2) */
6edbac01
MK
94 uid_t \fBuid\fP; /* Effective UID of owner */
95 gid_t \fBgid\fP; /* Effective GID of owner */
96 uid_t cuid; /* Effective UID of creator */
97 gid_t cgid; /* Effective GID of creator */
98 unsigned short \fBmode\fP; /* \fBPermissions\fP + SHM_DEST and
99 SHM_LOCKED flags */
2d2892fc 100 unsigned short __seq; /* Sequence number */
fea681da
MK
101};
102.fi
088a639b 103.in
fea681da 104.PP
9907019a
MK
105Valid values for
106.I cmd
107are:
fea681da 108.br
9fcbe835 109.TP 10
fea681da 110.B IPC_STAT
9907019a
MK
111Copy information from the kernel data structure associated with
112.I shmid
113into the
114.I shmid_ds
115structure pointed to by \fIbuf\fP.
116The caller must have read permission on the
fea681da
MK
117shared memory segment.
118.TP
119.B IPC_SET
9907019a
MK
120Write the values of some members of the
121.I shmid_ds
122structure pointed to by
945dc55a 123.I buf
9907019a
MK
124to the kernel data structure associated with this shared memory segment,
125updating also its
fea681da 126.I shm_ctime
9907019a
MK
127member.
128The following fields can be changed:
75ced2e8
MK
129\fIshm_perm.uid\fP, \fIshm_perm.gid\fP,
130and (the least significant 9 bits of) \fIshm_perm.mode\fP.
9907019a
MK
131The effective UID of the calling process must match the owner
132.RI ( shm_perm.uid )
133or creator
134.RI ( shm_perm.cuid )
135of the shared memory segment, or the caller must be privileged.
fea681da
MK
136.TP
137.B IPC_RMID
9907019a
MK
138Mark the segment to be destroyed.
139The segment will only actually be destroyed
140after the last process detaches it (i.e., when the
fea681da
MK
141.I shm_nattch
142member of the associated structure
143.I shmid_ds
9907019a
MK
144is zero).
145The caller must be the owner or creator, or be privileged.
c8f2dd47 146If a segment has been marked for destruction, then the (nonstandard)
9907019a
MK
147.B SHM_DEST
148flag of the
149.I shm_perm.mode
150field in the associated data structure retrieved by
151.B IPC_STAT
152will be set.
df102cb5 153.IP
9907019a
MK
154The caller \fImust\fP ensure that a segment is eventually destroyed;
155otherwise its pages that were faulted in will remain in memory or swap.
91bb32bf
MK
156.IP
157See also the description of
158.I /proc/sys/kernel/shm_rmid_forced
159in
160.BR proc (5).
9fcbe835 161.TP 10
8382f16d 162.BR IPC_INFO " (Linux-specific)"
c13182ef 163Returns information about system-wide shared memory limits and
79dded64
MK
164parameters in the structure pointed to by
165.IR buf .
166This structure is of type
0daa9e92 167.I shminfo
79dded64
MK
168(thus, a cast is required),
169defined in
170.I <sys/shm.h>
c3dfd2c8
MK
171if the
172.B _GNU_SOURCE
173feature test macro is defined:
79dded64 174.nf
088a639b 175.in +4n
79dded64
MK
176
177struct shminfo {
ba1eb78c 178 unsigned long shmmax; /* Maximum segment size */
9fcbe835
MK
179 unsigned long shmmin; /* Minimum segment size;
180 always 1 */
ba1eb78c 181 unsigned long shmmni; /* Maximum number of segments */
9fcbe835
MK
182 unsigned long shmseg; /* Maximum number of segments
183 that a process can attach;
184 unused within kernel */
ba1eb78c
MK
185 unsigned long shmall; /* Maximum number of pages of
186 shared memory, system-wide */
79dded64
MK
187};
188
088a639b 189.in
79dded64 190.fi
c13182ef 191The
79dded64
MK
192.IR shmmni ,
193.IR shmmax ,
2202881e 194and
79dded64
MK
195.I shmall
196settings can be changed via
c13182ef 197.I /proc
79dded64 198files of the same name; see
c13182ef 199.BR proc (5)
79dded64
MK
200for details.
201.TP
8382f16d 202.BR SHM_INFO " (Linux-specific)"
79dded64
MK
203Returns a
204.I shm_info
c13182ef 205structure whose fields contain information
79dded64
MK
206about system resources consumed by shared memory.
207This structure is defined in
208.I <sys/shm.h>
c3dfd2c8
MK
209if the
210.B _GNU_SOURCE
211feature test macro is defined:
79dded64 212.nf
088a639b 213.in +4n
79dded64
MK
214
215struct shm_info {
b0575744
MK
216 int used_ids; /* # of currently existing
217 segments */
218 unsigned long shm_tot; /* Total number of shared
219 memory pages */
220 unsigned long shm_rss; /* # of resident shared
221 memory pages */
222 unsigned long shm_swp; /* # of swapped shared
223 memory pages */
f3690310 224 unsigned long swap_attempts;
b0575744 225 /* Unused since Linux 2.4 */
f3690310 226 unsigned long swap_successes;
b0575744 227 /* Unused since Linux 2.4 */
79dded64 228};
088a639b 229.in
79dded64
MK
230.fi
231.TP
8382f16d 232.BR SHM_STAT " (Linux-specific)"
c13182ef
MK
233Returns a
234.I shmid_ds
79dded64
MK
235structure as for
236.BR IPC_STAT .
237However, the
238.I shmid
239argument is not a segment identifier, but instead an index into
240the kernel's internal array that maintains information about
241all shared memory segments on the system.
2202881e
MK
242.PP
243The caller can prevent or allow swapping of a shared
9907019a 244memory segment with the following \fIcmd\fP values:
fea681da 245.br
9fcbe835 246.TP 10
8382f16d 247.BR SHM_LOCK " (Linux-specific)"
c13182ef
MK
248Prevent swapping of the shared memory segment.
249The caller must fault in
fea681da 250any pages that are required to be present after locking is enabled.
c8f2dd47 251If a segment has been locked, then the (nonstandard)
0daa9e92 252.B SHM_LOCKED
9907019a
MK
253flag of the
254.I shm_perm.mode
255field in the associated data structure retrieved by
256.B IPC_STAT
257will be set.
fea681da 258.TP
8382f16d 259.BR SHM_UNLOCK " (Linux-specific)"
9907019a 260Unlock the segment, allowing it to be swapped out.
fea681da 261.PP
2202881e
MK
262In kernels before 2.6.10, only a privileged process
263could employ
264.B SHM_LOCK
fea681da 265and
2202881e 266.BR SHM_UNLOCK .
c13182ef 267Since kernel 2.6.10, an unprivileged process can employ these operations
2202881e
MK
268if its effective UID matches the owner or creator UID of the segment, and
269(for
270.BR SHM_LOCK )
271the amount of memory to be locked falls within the
0daa9e92 272.B RLIMIT_MEMLOCK
2202881e
MK
273resource limit (see
274.BR setrlimit (2)).
c13182ef 275.\" There was some weirdness in 2.6.9: SHM_LOCK and SHM_UNLOCK could
2202881e 276.\" be applied to a segment, regardless of ownership of the segment.
c13182ef 277.\" This was a botch-up in the move to RLIMIT_MEMLOCK, and was fixed
2202881e 278.\" in 2.6.10. MTK, May 2005
47297adb 279.SH RETURN VALUE
79dded64
MK
280A successful
281.B IPC_INFO
c13182ef 282or
79dded64
MK
283.B SHM_INFO
284operation returns the index of the highest used entry in the
285kernel's internal array recording information about all
286shared memory segments.
287(This information can be used with repeated
c13182ef
MK
288.B SHM_STAT
289operations to obtain information about all shared memory segments
79dded64
MK
290on the system.)
291A successful
292.B SHM_STAT
c13182ef 293operation returns the identifier of the shared memory segment
79dded64
MK
294whose index was given in
295.IR shmid .
296Other operations return 0 on success.
297
fea681da
MK
298On error, \-1 is returned, and
299.I errno
300is set appropriately.
301.SH ERRORS
89b3c6b8 302.TP
fea681da
MK
303.B EACCES
304\fBIPC_STAT\fP or \fBSHM_STAT\fP is requested and
75ced2e8 305\fIshm_perm.mode\fP does not allow read access for
fea681da
MK
306.IR shmid ,
307and the calling process does not have the
0daa9e92 308.B CAP_IPC_OWNER
fea681da
MK
309capability.
310.TP
311.B EFAULT
312The argument
313.I cmd
314has value
315.B IPC_SET
316or
317.B IPC_STAT
318but the address pointed to by
319.I buf
320isn't accessible.
321.TP
322.B EIDRM
323\fIshmid\fP points to a removed identifier.
324.TP
325.B EINVAL
326\fIshmid\fP is not a valid identifier, or \fIcmd\fP
327is not a valid command.
79dded64
MK
328Or: for a
329.B SHM_STAT
c13182ef 330operation, the index value specified in
79dded64
MK
331.I shmid
332referred to an array slot that is currently unused.
fea681da 333.TP
583d5fd3
MK
334.B ENOMEM
335(In kernels since 2.6.9),
336.B SHM_LOCK
c13182ef 337was specified and the size of the to-be-locked segment would mean
583d5fd3
MK
338that the total bytes in locked shared memory segments would exceed
339the limit for the real user ID of the calling process.
340This limit is defined by the
0daa9e92 341.B RLIMIT_MEMLOCK
583d5fd3
MK
342soft resource limit (see
343.BR setrlimit (2)).
344.TP
fea681da 345.B EOVERFLOW
e75a4542 346\fBIPC_STAT\fP is attempted, and the GID or UID value
fea681da 347is too large to be stored in the structure pointed to by
9907019a 348.IR buf .
fea681da
MK
349.TP
350.B EPERM
351\fBIPC_SET\fP or \fBIPC_RMID\fP is attempted, and the
352effective user ID of the calling process is not that of the creator
353(found in
354.IR shm_perm.cuid ),
355or the owner
356(found in
357.IR shm_perm.uid ),
358and the process was not privileged (Linux: did not have the
359.B CAP_SYS_ADMIN
360capability).
361
583d5fd3 362Or (in kernels before 2.6.9),
fea681da
MK
363.B SHM_LOCK
364or
365.B SHM_UNLOCK
366was specified, but the process was not privileged
367(Linux: did not have the
368.B CAP_IPC_LOCK
369capability).
583d5fd3 370(Since Linux 2.6.9, this error can also occur if the
0daa9e92 371.B RLIMIT_MEMLOCK
583d5fd3 372is 0 and the caller is not privileged.)
47297adb 373.SH CONFORMING TO
a1d5f77c
MK
374SVr4, POSIX.1-2001.
375.\" SVr4 documents additional error conditions EINVAL,
376.\" ENOENT, ENOSPC, ENOMEM, EEXIST. Neither SVr4 nor SVID documents
377.\" an EIDRM error condition.
75ced2e8 378.SH NOTES
f6652463
MK
379The inclusion of
380.I <sys/types.h>
381and
382.I <sys/ipc.h>
383isn't required on Linux or by any version of POSIX.
384However,
385some old implementations required the inclusion of these header files,
386and the SVID also documented their inclusion.
387Applications intended to be portable to such old systems may need
388to include these header files.
389.\" Like Linux, the FreeBSD man pages still document
390.\" the inclusion of these header files.
391
79dded64
MK
392The
393.BR IPC_INFO ,
0daa9e92 394.B SHM_STAT
79dded64
MK
395and
396.B SHM_INFO
397operations are used by the
81a8f8e7 398.BR ipcs (1)
79dded64
MK
399program to provide information on allocated resources.
400In the future these may modified or moved to a /proc file system
401interface.
402
c13182ef 403Linux permits a process to attach
0bfa087b 404.RB ( shmat (2))
75ced2e8
MK
405a shared memory segment that has already been marked for deletion
406using
407.IR shmctl(IPC_RMID) .
008f1ecc 408This feature is not available on other UNIX implementations;
75ced2e8
MK
409portable applications should avoid relying on it.
410
e3e25559
MK
411Various fields in a \fIstruct shmid_ds\fP were typed as
412.I short
413under Linux 2.2
414and have become
415.I long
416under Linux 2.4.
c13182ef 417To take advantage of this,
fea681da 418a recompilation under glibc-2.1.91 or later should suffice.
097585ed
MK
419(The kernel distinguishes old and new calls by an
420.B IPC_64
421flag in
fea681da 422.IR cmd .)
47297adb 423.SH SEE ALSO
9907019a 424.BR mlock (2),
583d5fd3 425.BR setrlimit (2),
fea681da
MK
426.BR shmget (2),
427.BR shmop (2),
1df962c8
MK
428.BR capabilities (7),
429.BR svipc (7)