Debian Codegraph @

upstream/debian-trixie/man2/shmget.2

raw
1 .\" Copyright (c) 1993 Luigi P. Bai ([email protected]) July 28, 1993
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .\" Modified Wed Jul 28 10:57:35 1993, Rik Faith <[email protected]>
6 .\" Modified Sun Nov 28 16:43:30 1993, Rik Faith <[email protected]>
7 .\" with material from Giorgio Ciucci <[email protected]>
8 .\" Portions Copyright 1993 Giorgio Ciucci <[email protected]>
9 .\" Modified Tue Oct 22 22:03:17 1996 by Eric S. Raymond <[email protected]>
10 .\" Modified, 8 Jan 2003, Michael Kerrisk, <[email protected]>
11 .\" Removed EIDRM from errors - that can't happen...
12 .\" Modified, 27 May 2004, Michael Kerrisk <[email protected]>
13 .\" Added notes on capability requirements
14 .\" Modified, 11 Nov 2004, Michael Kerrisk <[email protected]>
15 .\" Language and formatting clean-ups
16 .\" Added notes on /proc files
17 .\"
18 .TH shmget 2 2024-05-02 "Linux man-pages 6.9.1"
19 .SH NAME
20 shmget \- allocates a System V shared memory segment
21 .SH LIBRARY
22 Standard C library
23 .RI ( libc ", " \-lc )
24 .SH SYNOPSIS
25 .nf
26 .B #include <sys/shm.h>
27 .P
28 .BI "int shmget(key_t " key ", size_t " size ", int " shmflg );
29 .fi
30 .SH DESCRIPTION
31 .BR shmget ()
32 returns the identifier of the System\ V shared memory segment
33 associated with the value of the argument
34 .IR key .
35 It may be used either to obtain the identifier of a previously created
36 shared memory segment (when
37 .I shmflg
38 is zero and
39 .I key
40 does not have the value
41 .BR IPC_PRIVATE ),
42 or to create a new set.
43 .P
44 A new shared memory segment, with size equal to the value of
45 .I size
46 rounded up to a multiple of
47 .BR PAGE_SIZE ,
48 is created if
49 .I key
50 has the value
51 .B IPC_PRIVATE
52 or
53 .I key
54 isn't
55 .BR IPC_PRIVATE ,
56 no shared memory segment corresponding to
57 .I key
58 exists, and
59 .B IPC_CREAT
60 is specified in
61 .IR shmflg .
62 .P
63 If
64 .I shmflg
65 specifies both
66 .B IPC_CREAT
67 and
68 .B IPC_EXCL
69 and a shared memory segment already exists for
70 .IR key ,
71 then
72 .BR shmget ()
73 fails with
74 .I errno
75 set to
76 .BR EEXIST .
77 (This is analogous to the effect of the combination
78 .B O_CREAT | O_EXCL
79 for
80 .BR open (2).)
81 .P
82 The value
83 .I shmflg
84 is composed of:
85 .TP
86 .B IPC_CREAT
87 Create a new segment.
88 If this flag is not used, then
89 .BR shmget ()
90 will find the segment associated with \fIkey\fP and check to see if
91 the user has permission to access the segment.
92 .TP
93 .B IPC_EXCL
94 This flag is used with
95 .B IPC_CREAT
96 to ensure that this call creates the segment.
97 If the segment already exists, the call fails.
98 .TP
99 .BR SHM_HUGETLB " (since Linux 2.6)"
100 Allocate the segment using "huge" pages.
101 See the Linux kernel source file
102 .I Documentation/admin\-guide/mm/hugetlbpage.rst
103 for further information.
104 .TP
105 .B SHM_HUGE_2MB
106 .TQ
107 .BR SHM_HUGE_1GB " (since Linux 3.8)"
108 .\" See https://lwn.net/Articles/533499/
109 Used in conjunction with
110 .B SHM_HUGETLB
111 to select alternative hugetlb page sizes (respectively, 2\ MB and 1\ GB)
112 on systems that support multiple hugetlb page sizes.
113 .IP
114 More generally, the desired huge page size can be configured by encoding
115 the base-2 logarithm of the desired page size in the six bits at the offset
116 .BR SHM_HUGE_SHIFT .
117 Thus, the above two constants are defined as:
118 .IP
119 .in +4n
120 .EX
121 #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
122 #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
123 .EE
124 .in
125 .IP
126 For some additional details,
127 see the discussion of the similarly named constants in
128 .BR mmap (2).
129 .TP
130 .BR SHM_NORESERVE " (since Linux 2.6.15)"
131 This flag serves the same purpose as the
132 .BR mmap (2)
133 .B MAP_NORESERVE
134 flag.
135 Do not reserve swap space for this segment.
136 When swap space is reserved, one has the guarantee
137 that it is possible to modify the segment.
138 When swap space is not reserved one might get
139 .B SIGSEGV
140 upon a write
141 if no physical memory is available.
142 See also the discussion of the file
143 .I /proc/sys/vm/overcommit_memory
144 in
145 .BR proc (5).
146 .\" As at 2.6.17-rc2, this flag has no effect if SHM_HUGETLB was also
147 .\" specified.
148 .P
149 In addition to the above flags, the least significant 9 bits of
150 .I shmflg
151 specify the permissions granted to the owner, group, and others.
152 These bits have the same format, and the same
153 meaning, as the
154 .I mode
155 argument of
156 .BR open (2).
157 Presently, execute permissions are not used by the system.
158 .P
159 When a new shared memory segment is created,
160 its contents are initialized to zero values, and
161 its associated data structure,
162 .I shmid_ds
163 (see
164 .BR shmctl (2)),
165 is initialized as follows:
166 .IP \[bu] 3
167 .I shm_perm.cuid
168 and
169 .I shm_perm.uid
170 are set to the effective user ID of the calling process.
171 .IP \[bu]
172 .I shm_perm.cgid
173 and
174 .I shm_perm.gid
175 are set to the effective group ID of the calling process.
176 .IP \[bu]
177 The least significant 9 bits of
178 .I shm_perm.mode
179 are set to the least significant 9 bit of
180 .IR shmflg .
181 .IP \[bu]
182 .I shm_segsz
183 is set to the value of
184 .IR size .
185 .IP \[bu]
186 .IR shm_lpid ,
187 .IR shm_nattch ,
188 .IR shm_atime ,
189 and
190 .I shm_dtime
191 are set to 0.
192 .IP \[bu]
193 .I shm_ctime
194 is set to the current time.
195 .P
196 If the shared memory segment already exists, the permissions are
197 verified, and a check is made to see if it is marked for destruction.
198 .SH RETURN VALUE
199 On success, a valid shared memory identifier is returned.
200 On error, \-1 is returned, and
201 .I errno
202 is set to indicate the error.
203 .SH ERRORS
204 .TP
205 .B EACCES
206 The user does not have permission to access the
207 shared memory segment, and does not have the
208 .B CAP_IPC_OWNER
209 capability in the user namespace that governs its IPC namespace.
210 .TP
211 .B EEXIST
212 .B IPC_CREAT
213 and
214 .B IPC_EXCL
215 were specified in
216 .IR shmflg ,
217 but a shared memory segment already exists for
218 .IR key .
219 .TP
220 .B EINVAL
221 A new segment was to be created and
222 .I size
223 is less than
224 .B SHMMIN
225 or greater than
226 .BR SHMMAX .
227 .TP
228 .B EINVAL
229 A segment for the given
230 .I key
231 exists, but \fIsize\fP is greater than the size
232 of that segment.
233 .TP
234 .B ENFILE
235 .\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
236 The system-wide limit on the total number of open files has been reached.
237 .TP
238 .B ENOENT
239 No segment exists for the given \fIkey\fP, and
240 .B IPC_CREAT
241 was not specified.
242 .TP
243 .B ENOMEM
244 No memory could be allocated for segment overhead.
245 .TP
246 .B ENOSPC
247 All possible shared memory IDs have been taken
248 .RB ( SHMMNI ),
249 or allocating a segment of the requested
250 .I size
251 would cause the system to exceed the system-wide limit on shared memory
252 .RB ( SHMALL ).
253 .TP
254 .B EPERM
255 The
256 .B SHM_HUGETLB
257 flag was specified, but the caller was not privileged (did not have the
258 .B CAP_IPC_LOCK
259 capability)
260 and is not a member of the
261 .I sysctl_hugetlb_shm_group
262 group; see the description of
263 .I /proc/sys/vm/sysctl_hugetlb_shm_group
264 in
265 .BR proc (5).
266 .SH STANDARDS
267 POSIX.1-2008.
268 .P
269 .B SHM_HUGETLB
270 and
271 .B SHM_NORESERVE
272 are Linux extensions.
273 .SH HISTORY
274 POSIX.1-2001, SVr4.
275 .\" SVr4 documents an additional error condition EEXIST.
276 .SH NOTES
277 .B IPC_PRIVATE
278 isn't a flag field but a
279 .I key_t
280 type.
281 If this special value is used for
282 .IR key ,
283 the system call ignores all but the least significant 9 bits of
284 .I shmflg
285 and creates a new shared memory segment.
286 .\"
287 .SS Shared memory limits
288 The following limits on shared memory segment resources affect the
289 .BR shmget ()
290 call:
291 .TP
292 .B SHMALL
293 System-wide limit on the total amount of shared memory,
294 measured in units of the system page size.
295 .IP
296 On Linux, this limit can be read and modified via
297 .IR /proc/sys/kernel/shmall .
298 Since Linux 3.16,
299 .\" commit 060028bac94bf60a65415d1d55a359c3a17d5c31
300 the default value for this limit is:
301 .IP
302 .in +4n
303 .EX
304 ULONG_MAX - 2\[ha]24
305 .EE
306 .in
307 .IP
308 The effect of this value
309 (which is suitable for both 32-bit and 64-bit systems)
310 is to impose no limitation on allocations.
311 This value, rather than
312 .BR ULONG_MAX ,
313 was chosen as the default to prevent some cases where historical
314 applications simply raised the existing limit without first checking
315 its current value.
316 Such applications would cause the value to overflow if the limit was set at
317 .BR ULONG_MAX .
318 .IP
319 From Linux 2.4 up to Linux 3.15,
320 the default value for this limit was:
321 .IP
322 .in +4n
323 .EX
324 SHMMAX / PAGE_SIZE * (SHMMNI / 16)
325 .EE
326 .in
327 .IP
328 If
329 .B SHMMAX
330 and
331 .B SHMMNI
332 were not modified, then multiplying the result of this formula
333 by the page size (to get a value in bytes) yielded a value of 8\ GB
334 as the limit on the total memory used by all shared memory segments.
335 .TP
336 .B SHMMAX
337 Maximum size in bytes for a shared memory segment.
338 .IP
339 On Linux, this limit can be read and modified via
340 .IR /proc/sys/kernel/shmmax .
341 Since Linux 3.16,
342 .\" commit 060028bac94bf60a65415d1d55a359c3a17d5c31
343 the default value for this limit is:
344 .IP
345 .in +4n
346 .EX
347 ULONG_MAX - 2\[ha]24
348 .EE
349 .in
350 .IP
351 The effect of this value
352 (which is suitable for both 32-bit and 64-bit systems)
353 is to impose no limitation on allocations.
354 See the description of
355 .B SHMALL
356 for a discussion of why this default value (rather than
357 .BR ULONG_MAX )
358 is used.
359 .IP
360 From Linux 2.2 up to Linux 3.15, the default value of
361 this limit was 0x2000000 (32\ MiB).
362 .IP
363 Because it is not possible to map just part of a shared memory segment,
364 the amount of virtual memory places another limit on the maximum size of a
365 usable segment:
366 for example, on i386 the largest segments that can be mapped have a
367 size of around 2.8\ GB, and on x86-64 the limit is around 127 TB.
368 .TP
369 .B SHMMIN
370 Minimum size in bytes for a shared memory segment: implementation
371 dependent (currently 1 byte, though
372 .B PAGE_SIZE
373 is the effective minimum size).
374 .TP
375 .B SHMMNI
376 System-wide limit on the number of shared memory segments.
377 In Linux 2.2, the default value for this limit was 128;
378 since Linux 2.4, the default value is 4096.
379 .IP
380 On Linux, this limit can be read and modified via
381 .IR /proc/sys/kernel/shmmni .
382 .\" Kernels between Linux 2.4.x and Linux 2.6.8 had an off-by-one error
383 .\" that meant that we could create one more segment than SHMMNI -- MTK
384 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
385 .P
386 The implementation has no specific limits for the per-process maximum
387 number of shared memory segments
388 .RB ( SHMSEG ).
389 .SS Linux notes
390 Until Linux 2.3.30, Linux would return
391 .B EIDRM
392 for a
393 .BR shmget ()
394 on a shared memory segment scheduled for deletion.
395 .SH BUGS
396 The name choice
397 .B IPC_PRIVATE
398 was perhaps unfortunate,
399 .B IPC_NEW
400 would more clearly show its function.
401 .SH EXAMPLES
402 See
403 .BR shmop (2).
404 .SH SEE ALSO
405 .BR memfd_create (2),
406 .BR shmat (2),
407 .BR shmctl (2),
408 .BR shmdt (2),
409 .BR ftok (3),
410 .BR capabilities (7),
411 .BR shm_overview (7),
412 .BR sysvipc (7)