]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/shmget.2
nscd.conf.5: spfix
[thirdparty/man-pages.git] / man2 / shmget.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1993 Luigi P. Bai (lpb@softint.com) July 28, 1993
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" Modified Wed Jul 28 10:57:35 1993, Rik Faith <faith@cs.unc.edu>
24.\" Modified Sun Nov 28 16:43:30 1993, Rik Faith <faith@cs.unc.edu>
25.\" with material from Giorgio Ciucci <giorgio@crcc.it>
26.\" Portions Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
27.\" Modified Tue Oct 22 22:03:17 1996 by Eric S. Raymond <esr@thyrsus.com>
c11b1abf 28.\" Modified, 8 Jan 2003, Michael Kerrisk, <mtk.manpages@gmail.com>
fea681da 29.\" Removed EIDRM from errors - that can't happen...
c11b1abf 30.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 31.\" Added notes on capability requirements
c11b1abf 32.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
c952e226
MK
33.\" Language and formatting clean-ups
34.\" Added notes on /proc files
fea681da 35.\"
f6652463 36.TH SHMGET 2 2012-05-31 "Linux" "Linux Programmer's Manual"
fea681da
MK
37.SH NAME
38shmget \- allocates a shared memory segment
39.SH SYNOPSIS
40.ad l
41.B #include <sys/ipc.h>
d314dbfd 42.br
fea681da
MK
43.B #include <sys/shm.h>
44.sp
f7174672 45.BI "int shmget(key_t " key ", size_t " size ", int " shmflg );
fea681da
MK
46.ad b
47.SH DESCRIPTION
c952e226 48.BR shmget ()
fea681da
MK
49returns the identifier of the shared memory segment
50associated with the value of the argument
51.IR key .
52A new shared memory segment, with size equal to the value of
53.I size
54rounded up to a multiple of
55.BR PAGE_SIZE ,
56is created if
57.I key
58has the value
59.B IPC_PRIVATE
60or
61.I key
62isn't
63.BR IPC_PRIVATE ,
64no shared memory segment corresponding to
0daa9e92 65.I key
fea681da
MK
66exists, and
67.B IPC_CREAT
c952e226
MK
68is specified in
69.IR shmflg .
70.PP
71If
fea681da 72.I shmflg
c952e226
MK
73specifies both
74.B IPC_CREAT
75and
76.B IPC_EXCL
77and a shared memory segment already exists for
78.IR key ,
79then
80.BR shmget ()
c13182ef 81fails with
c952e226
MK
82.I errno
83set to
84.BR EEXIST .
c13182ef 85(This is analogous to the effect of the combination
c952e226
MK
86.B O_CREAT | O_EXCL
87for
88.BR open (2).)
fea681da
MK
89.PP
90The value
91.I shmflg
92is composed of:
93.TP 12
94.B IPC_CREAT
c13182ef
MK
95to create a new segment.
96If this flag is not used, then
c952e226 97.BR shmget ()
c13182ef 98will find the segment associated with \fIkey\fP and check to see if
fea681da
MK
99the user has permission to access the segment.
100.TP
101.B IPC_EXCL
c13182ef 102used with \fBIPC_CREAT\fP to ensure failure if the segment already exists.
fea681da 103.TP
c13182ef 104.I mode_flags
c952e226 105(least significant 9 bits)
fea681da 106specifying the permissions granted to the owner, group, and world.
c952e226
MK
107These bits have the same format, and the same
108meaning, as the
109.I mode
110argument of
111.BR open (2).
fea681da 112Presently, the execute permissions are not used by the system.
eb57338f 113.TP
32047971 114.BR SHM_HUGETLB " (since Linux 2.6)"
c13182ef 115Allocate the segment using "huge pages."
eb57338f
MK
116See the kernel source file
117.I Documentation/vm/hugetlbpage.txt
118for further information.
32047971
MK
119.TP
120.BR SHM_NORESERVE " (since Linux 2.6.15)"
121This flag serves the same purpose as the
122.BR mmap (2)
123.B MAP_NORESERVE
124flag.
c13182ef 125Do not reserve swap space for this segment.
32047971
MK
126When swap space is reserved, one has the guarantee
127that it is possible to modify the segment.
8bd58774
MK
128When swap space is not reserved one might get
129.B SIGSEGV
130upon a write
32047971
MK
131if no physical memory is available.
132See also the discussion of the file
133.I /proc/sys/vm/overcommit_memory
c13182ef 134in
defcceb3 135.BR proc (5).
32047971
MK
136.\" As at 2.6.17-rc2, this flag has no effect if SHM_HUGETLB was also
137.\" specified.
fea681da 138.PP
6319e935 139When a new shared memory segment is created,
d9bfdb9c 140its contents are initialized to zero values, and
6319e935 141its associated data structure,
fea681da 142.I shmid_ds
c952e226 143(see
6319e935 144.BR shmctl (2)),
d9bfdb9c 145is initialized as follows:
fea681da 146.IP
c952e226 147.I shm_perm.cuid
fea681da 148and
c952e226 149.I shm_perm.uid
499d62f2 150are set to the effective user ID of the calling process.
fea681da 151.IP
c952e226 152.I shm_perm.cgid
fea681da 153and
c952e226 154.I shm_perm.gid
499d62f2 155are set to the effective group ID of the calling process.
fea681da 156.IP
c952e226
MK
157The least significant 9 bits of
158.I shm_perm.mode
159are set to the least significant 9 bit of
fea681da
MK
160.IR shmflg .
161.IP
c952e226 162.I shm_segsz
fea681da
MK
163is set to the value of
164.IR size .
165.IP
c952e226
MK
166.IR shm_lpid ,
167.IR shm_nattch ,
168.I shm_atime
fea681da 169and
c952e226
MK
170.I shm_dtime
171are set to 0.
fea681da 172.IP
c952e226 173.I shm_ctime
fea681da
MK
174is set to the current time.
175.PP
c952e226 176If the shared memory segment already exists, the permissions are
fea681da 177verified, and a check is made to see if it is marked for destruction.
fea681da
MK
178.SH "RETURN VALUE"
179A valid segment identifier,
180.IR shmid ,
181is returned on success, \-1 on error.
182.SH ERRORS
183On failure,
c952e226 184.I errno
fea681da 185is set to one of the following:
89b3c6b8 186.TP
fea681da 187.B EACCES
c13182ef 188The user does not have permission to access the
fea681da
MK
189shared memory segment, and does not have the
190.B CAP_IPC_OWNER
191capability.
192.TP
193.B EEXIST
194.B IPC_CREAT | IPC_EXCL
195was specified and the segment exists.
196.TP
fea681da
MK
197.B EINVAL
198A new segment was to be created and \fIsize\fP < \fBSHMMIN\fP
199or \fIsize\fP > \fBSHMMAX\fP, or no new segment was to be created,
200a segment with given key existed, but \fIsize\fP is greater than the size
201of that segment.
202.TP
203.B ENFILE
204.\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
205The system limit on the total number of open files has been reached.
206.TP
207.B ENOENT
208No segment exists for the given \fIkey\fP, and
209.B IPC_CREAT
210was not specified.
211.TP
212.B ENOMEM
213No memory could be allocated for segment overhead.
214.TP
215.B ENOSPC
357cf3fe 216All possible shared memory IDs have been taken
fea681da
MK
217.RB ( SHMMNI ),
218or allocating a segment of the requested
219.I size
220would cause the system to exceed the system-wide limit on shared memory
221.RB ( SHMALL ).
eb57338f
MK
222.TP
223.B EPERM
224The
225.B SHM_HUGETLB
226flag was specified, but the caller was not privileged (did not have the
227.B CAP_IPC_LOCK
228capability).
a1d5f77c
MK
229.SH "CONFORMING TO"
230SVr4, POSIX.1-2001.
231.\" SVr4 documents an additional error condition EEXIST.
232
233.B SHM_HUGETLB
d603cc27 234is a nonportable Linux extension.
fea681da 235.SH NOTES
f6652463
MK
236The inclusion of
237.I <sys/types.h>
238and
239.I <sys/ipc.h>
240isn't required on Linux or by any version of POSIX.
241However,
242some old implementations required the inclusion of these header files,
243and the SVID also documented their inclusion.
244Applications intended to be portable to such old systems may need
245to include these header files.
246.\" Like Linux, the FreeBSD man pages still document
247.\" the inclusion of these header files.
248
fea681da
MK
249.B IPC_PRIVATE
250isn't a flag field but a
c952e226 251.I key_t
fea681da
MK
252type.
253If this special value is used for
254.IR key ,
c952e226 255the system call ignores everything but the least significant 9 bits of
fea681da
MK
256.I shmflg
257and creates a new shared memory segment (on success).
258.PP
540036b2 259The following limits on shared memory segment resources affect the
c952e226 260.BR shmget ()
fea681da 261call:
89b3c6b8 262.TP
fea681da 263.B SHMALL
c952e226
MK
264System wide maximum of shared memory pages
265(on Linux, this limit can be read and modified via
266.IR /proc/sys/kernel/shmall ).
fea681da
MK
267.TP
268.B SHMMAX
c952e226
MK
269Maximum size in bytes for a shared memory segment: policy dependent
270(on Linux, this limit can be read and modified via
271.IR /proc/sys/kernel/shmmax ).
fea681da
MK
272.TP
273.B SHMMIN
274Minimum size in bytes for a shared memory segment: implementation
275dependent (currently 1 byte, though
276.B PAGE_SIZE
277is the effective minimum size).
278.TP
279.B SHMMNI
280System wide maximum number of shared memory segments: implementation
c952e226
MK
281dependent (currently 4096, was 128 before Linux 2.3.99;
282on Linux, this limit can be read and modified via
283.IR /proc/sys/kernel/shmmni ).
ed978af2 284.\" Kernels between 2.4.x and 2.6.8 had an off-by-one error that meant
cd31a0d6 285.\" that we could create one more segment than SHMMNI -- MTK
c952e226 286.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da 287.PP
5b55dde2 288The implementation has no specific limits for the per-process maximum
fea681da
MK
289number of shared memory segments
290.RB ( SHMSEG ).
4fb31341 291.SS Linux Notes
2f0af33b 292Until version 2.3.30 Linux would return
0daa9e92 293.B EIDRM
2f0af33b 294for a
4fb31341
MK
295.BR shmget ()
296on a shared memory segment scheduled for deletion.
fea681da 297.SH BUGS
2f0af33b
MK
298The name choice
299.B IPC_PRIVATE
9091595d
MK
300was perhaps unfortunate,
301.B IPC_NEW
fea681da 302would more clearly show its function.
fea681da
MK
303.SH "SEE ALSO"
304.BR shmat (2),
305.BR shmctl (2),
306.BR shmdt (2),
307.BR ftok (3),
92243659 308.BR capabilities (7),
f93af9c6 309.BR shm_overview (7),
2c5e151c 310.BR svipc (7)