Debian Codegraph @

upstream/debian-trixie/man2/shmop.2

raw
1 .\" Copyright 1993 Giorgio Ciucci ([email protected])
2 .\" and Copyright 2020 Michael Kerrisk <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" Modified Sun Nov 28 17:06:19 1993, Rik Faith ([email protected])
7 .\" with material from Luigi P. Bai ([email protected])
8 .\" Portions Copyright 1993 Luigi P. Bai
9 .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond <[email protected]>
10 .\" Modified, 5 Jan 2002, Michael Kerrisk <[email protected]>
11 .\" Modified, 19 Sep 2002, Michael Kerrisk <[email protected]>
12 .\" Added SHM_REMAP flag description
13 .\" Modified, 27 May 2004, Michael Kerrisk <[email protected]>
14 .\" Added notes on capability requirements
15 .\" Modified, 11 Nov 2004, Michael Kerrisk <[email protected]>
16 .\" Language and formatting clean-ups
17 .\" Changed wording and placement of sentence regarding attachment
18 .\" of segments marked for destruction
19 .\"
20 .TH SHMOP 2 2024-06-15 "Linux man-pages 6.9.1"
21 .SH NAME
22 shmat, shmdt \- System V shared memory operations
23 .SH LIBRARY
24 Standard C library
25 .RI ( libc ", " \-lc )
26 .SH SYNOPSIS
27 .nf
28 .B #include <sys/shm.h>
29 .P
30 .BI "void *shmat(int " shmid ", const void *_Nullable " shmaddr ", \
31 int " shmflg );
32 .BI "int shmdt(const void *" shmaddr );
33 .fi
34 .SH DESCRIPTION
35 .SS shmat()
36 .BR shmat ()
37 attaches the System\ V shared memory segment identified by
38 .I shmid
39 to the address space of the calling process.
40 The attaching address is specified by
41 .I shmaddr
42 with one of the following criteria:
43 .IP \[bu] 3
44 If
45 .I shmaddr
46 is NULL,
47 the system chooses a suitable (unused) page-aligned address to attach
48 the segment.
49 .IP \[bu]
50 If
51 .I shmaddr
52 isn't NULL
53 and
54 .B SHM_RND
55 is specified in
56 .IR shmflg ,
57 the attach occurs at the address equal to
58 .I shmaddr
59 rounded down to the nearest multiple of
60 .BR SHMLBA .
61 .IP \[bu]
62 Otherwise,
63 .I shmaddr
64 must be a page-aligned address at which the attach occurs.
65 .P
66 In addition to
67 .BR SHM_RND ,
68 the following flags may be specified in the
69 .I shmflg
70 bit-mask argument:
71 .TP
72 .BR SHM_EXEC " (Linux-specific; since Linux 2.6.9)"
73 Allow the contents of the segment to be executed.
74 The caller must have execute permission on the segment.
75 .TP
76 .B SHM_RDONLY
77 Attach the segment for read-only access.
78 The process must have read permission for the segment.
79 If this flag is not specified,
80 the segment is attached for read and write access,
81 and the process must have read and write permission for the segment.
82 There is no notion of a write-only shared memory segment.
83 .TP
84 .BR SHM_REMAP " (Linux-specific)"
85 This flag specifies
86 that the mapping of the segment should replace
87 any existing mapping in the range starting at
88 .I shmaddr
89 and continuing for the size of the segment.
90 (Normally, an
91 .B EINVAL
92 error would result if a mapping already exists in this address range.)
93 In this case,
94 .I shmaddr
95 must not be NULL.
96 .P
97 The
98 .BR brk (2)
99 value of the calling process is not altered by the attach.
100 The segment will automatically be detached at process exit.
101 The same segment may be attached as a read and as a read-write
102 one, and more than once, in the process's address space.
103 .P
104 A successful
105 .BR shmat ()
106 call updates the members of the
107 .I shmid_ds
108 structure (see
109 .BR shmctl (2))
110 associated with the shared memory segment as follows:
111 .IP \[bu] 3
112 .I shm_atime
113 is set to the current time.
114 .IP \[bu]
115 .I shm_lpid
116 is set to the process-ID of the calling process.
117 .IP \[bu]
118 .I shm_nattch
119 is incremented by one.
120 .\"
121 .SS shmdt()
122 .BR shmdt ()
123 detaches the shared memory segment located at the address specified by
124 .I shmaddr
125 from the address space of the calling process.
126 The to-be-detached segment must be currently
127 attached with
128 .I shmaddr
129 equal to the value returned by the attaching
130 .BR shmat ()
131 call.
132 .P
133 On a successful
134 .BR shmdt ()
135 call, the system updates the members of the
136 .I shmid_ds
137 structure associated with the shared memory segment as follows:
138 .IP \[bu] 3
139 .I shm_dtime
140 is set to the current time.
141 .IP \[bu]
142 .I shm_lpid
143 is set to the process-ID of the calling process.
144 .IP \[bu]
145 .I shm_nattch
146 is decremented by one.
147 If it becomes 0 and the segment is marked for deletion,
148 the segment is deleted.
149 .SH RETURN VALUE
150 On success,
151 .BR shmat ()
152 returns the address of the attached shared memory segment; on error,
153 .I (void\ *)\ \-1
154 is returned, and
155 .I errno
156 is set to indicate the error.
157 .P
158 On success,
159 .BR shmdt ()
160 returns 0; on error \-1 is returned, and
161 .I errno
162 is set to indicate the error.
163 .SH ERRORS
164 .BR shmat ()
165 can fail with one of the following errors:
166 .TP
167 .B EACCES
168 The calling process does not have the required permissions for
169 the requested attach type, and does not have the
170 .B CAP_IPC_OWNER
171 capability in the user namespace that governs its IPC namespace.
172 .TP
173 .B EIDRM
174 \fIshmid\fP points to a removed identifier.
175 .TP
176 .B EINVAL
177 Invalid
178 .I shmid
179 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
180 specified) or invalid
181 .I shmaddr
182 value, or can't attach segment at
183 .IR shmaddr ,
184 or
185 .B SHM_REMAP
186 was specified and
187 .I shmaddr
188 was NULL.
189 .TP
190 .B ENOMEM
191 Could not allocate memory for the descriptor or for the page tables.
192 .P
193 .BR shmdt ()
194 can fail with one of the following errors:
195 .TP
196 .B EINVAL
197 There is no shared memory segment attached at
198 .IR shmaddr ;
199 or,
200 .\" The following since Linux 2.6.17-rc1:
201 .I shmaddr
202 is not aligned on a page boundary.
203 .SH STANDARDS
204 POSIX.1-2008.
205 .SH HISTORY
206 POSIX.1-2001, SVr4.
207 .\" SVr4 documents an additional error condition EMFILE.
208 .P
209 In SVID 3 (or perhaps earlier),
210 the type of the \fIshmaddr\fP argument was changed from
211 .I "char\ *"
212 into
213 .IR "const void\ *" ,
214 and the returned type of
215 .BR shmat ()
216 from
217 .I "char\ *"
218 into
219 .IR "void\ *" .
220 .SH NOTES
221 After a
222 .BR fork (2),
223 the child inherits the attached shared memory segments.
224 .P
225 After an
226 .BR execve (2),
227 all attached shared memory segments are detached from the process.
228 .P
229 Upon
230 .BR _exit (2),
231 all attached shared memory segments are detached from the process.
232 .P
233 Using
234 .BR shmat ()
235 with
236 .I shmaddr
237 equal to NULL
238 is the preferred, portable way of attaching a shared memory segment.
239 Be aware that the shared memory segment attached in this way
240 may be attached at different addresses in different processes.
241 Therefore, any pointers maintained within the shared memory must be
242 made relative (typically to the starting address of the segment),
243 rather than absolute.
244 .P
245 On Linux, it is possible to attach a shared memory segment even if it
246 is already marked to be deleted.
247 However, POSIX.1 does not specify this behavior and
248 many other implementations do not support it.
249 .P
250 The following system parameter affects
251 .BR shmat ():
252 .TP
253 .B SHMLBA
254 Segment low boundary address multiple.
255 When explicitly specifying an attach address in a call to
256 .BR shmat (),
257 the caller should ensure that the address is a multiple of this value.
258 This is necessary on some architectures,
259 in order either to ensure good CPU cache performance or to ensure that
260 different attaches of the same segment have consistent views
261 within the CPU cache.
262 .B SHMLBA
263 is normally some multiple of the system page size.
264 (On many Linux architectures,
265 .B SHMLBA
266 is the same as the system page size.)
267 .P
268 The implementation places no intrinsic per-process limit on the
269 number of shared memory segments
270 .RB ( SHMSEG ).
271 .SH EXAMPLES
272 The two programs shown below exchange a string using a shared memory segment.
273 Further details about the programs are given below.
274 First, we show a shell session demonstrating their use.
275 .P
276 In one terminal window, we run the "reader" program,
277 which creates a System V shared memory segment and a System V semaphore set.
278 The program prints out the IDs of the created objects,
279 and then waits for the semaphore to change value.
280 .P
281 .in +4n
282 .EX
283 $ \fB./svshm_string_read\fP
284 shmid = 1114194; semid = 15
285 .EE
286 .in
287 .P
288 In another terminal window, we run the "writer" program.
289 The "writer" program takes three command-line arguments:
290 the IDs of the shared memory segment and semaphore set created
291 by the "reader", and a string.
292 It attaches the existing shared memory segment,
293 copies the string to the shared memory, and modifies the semaphore value.
294 .P
295 .in +4n
296 .EX
297 $ \fB./svshm_string_write 1114194 15 \[aq]Hello, world\[aq]\fP
298 .EE
299 .in
300 .P
301 Returning to the terminal where the "reader" is running,
302 we see that the program has ceased waiting on the semaphore
303 and has printed the string that was copied into the
304 shared memory segment by the writer:
305 .P
306 .in +4n
307 .EX
308 Hello, world
309 .EE
310 .in
311 .\"
312 .SS Program source: svshm_string.h
313 The following header file is included by the "reader" and "writer" programs:
314 .P
315 .in +4n
316 .\" SRC BEGIN (svshm_string.h)
317 .EX
318 /* svshm_string.h
319 \&
320 Licensed under GNU General Public License v2 or later.
321 */
322 #ifndef SVSHM_STRING_H
323 #define SVSHM_STRING_H
324 \&
325 #include <stdio.h>
326 #include <stdlib.h>
327 #include <sys/sem.h>
328 \&
329 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \[rs]
330 } while (0)
331 \&
332 union semun { /* Used in calls to semctl() */
333 int val;
334 struct semid_ds *buf;
335 unsigned short *array;
336 #if defined(__linux__)
337 struct seminfo *__buf;
338 #endif
339 };
340 \&
341 #define MEM_SIZE 4096
342 \&
343 #endif // include guard
344 .EE
345 .\" SRC END
346 .in
347 .\"
348 .SS Program source: svshm_string_read.c
349 The "reader" program creates a shared memory segment and a semaphore set
350 containing one semaphore.
351 It then attaches the shared memory object into its address space
352 and initializes the semaphore value to 1.
353 Finally, the program waits for the semaphore value to become 0,
354 and afterwards prints the string that has been copied into the
355 shared memory segment by the "writer".
356 .P
357 .in +4n
358 .\" SRC BEGIN (svshm_string_read.c)
359 .EX
360 /* svshm_string_read.c
361 \&
362 Licensed under GNU General Public License v2 or later.
363 */
364 #include <stdio.h>
365 #include <stdlib.h>
366 #include <sys/ipc.h>
367 #include <sys/sem.h>
368 #include <sys/shm.h>
369 \&
370 #include "svshm_string.h"
371 \&
372 int
373 main(void)
374 {
375 int semid, shmid;
376 char *addr;
377 union semun arg, dummy;
378 struct sembuf sop;
379 \&
380 /* Create shared memory and semaphore set containing one
381 semaphore. */
382 \&
383 shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
384 if (shmid == \-1)
385 errExit("shmget");
386 \&
387 semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
388 if (semid == \-1)
389 errExit("semget");
390 \&
391 /* Attach shared memory into our address space. */
392 \&
393 addr = shmat(shmid, NULL, SHM_RDONLY);
394 if (addr == (void *) \-1)
395 errExit("shmat");
396 \&
397 /* Initialize semaphore 0 in set with value 1. */
398 \&
399 arg.val = 1;
400 if (semctl(semid, 0, SETVAL, arg) == \-1)
401 errExit("semctl");
402 \&
403 printf("shmid = %d; semid = %d\[rs]n", shmid, semid);
404 \&
405 /* Wait for semaphore value to become 0. */
406 \&
407 sop.sem_num = 0;
408 sop.sem_op = 0;
409 sop.sem_flg = 0;
410 \&
411 if (semop(semid, &sop, 1) == \-1)
412 errExit("semop");
413 \&
414 /* Print the string from shared memory. */
415 \&
416 printf("%s\[rs]n", addr);
417 \&
418 /* Remove shared memory and semaphore set. */
419 \&
420 if (shmctl(shmid, IPC_RMID, NULL) == \-1)
421 errExit("shmctl");
422 if (semctl(semid, 0, IPC_RMID, dummy) == \-1)
423 errExit("semctl");
424 \&
425 exit(EXIT_SUCCESS);
426 }
427 .EE
428 .\" SRC END
429 .in
430 .\"
431 .SS Program source: svshm_string_write.c
432 The writer program takes three command-line arguments:
433 the IDs of the shared memory segment and semaphore set
434 that have already been created by the "reader", and a string.
435 It attaches the shared memory segment into its address space,
436 and then decrements the semaphore value to 0 in order to inform the
437 "reader" that it can now examine the contents of the shared memory.
438 .P
439 .in +4n
440 .\" SRC BEGIN (svshm_string_write.c)
441 .EX
442 /* svshm_string_write.c
443 \&
444 Licensed under GNU General Public License v2 or later.
445 */
446 #include <stdio.h>
447 #include <stdlib.h>
448 #include <string.h>
449 #include <sys/sem.h>
450 #include <sys/shm.h>
451 \&
452 #include "svshm_string.h"
453 \&
454 int
455 main(int argc, char *argv[])
456 {
457 int semid, shmid;
458 char *addr;
459 size_t len;
460 struct sembuf sop;
461 \&
462 if (argc != 4) {
463 fprintf(stderr, "Usage: %s shmid semid string\[rs]n", argv[0]);
464 exit(EXIT_FAILURE);
465 }
466 \&
467 len = strlen(argv[3]) + 1; /* +1 to include trailing \[aq]\[rs]0\[aq] */
468 if (len > MEM_SIZE) {
469 fprintf(stderr, "String is too big!\[rs]n");
470 exit(EXIT_FAILURE);
471 }
472 \&
473 /* Get object IDs from command\-line. */
474 \&
475 shmid = atoi(argv[1]);
476 semid = atoi(argv[2]);
477 \&
478 /* Attach shared memory into our address space and copy string
479 (including trailing null byte) into memory. */
480 \&
481 addr = shmat(shmid, NULL, 0);
482 if (addr == (void *) \-1)
483 errExit("shmat");
484 \&
485 memcpy(addr, argv[3], len);
486 \&
487 /* Decrement semaphore to 0. */
488 \&
489 sop.sem_num = 0;
490 sop.sem_op = \-1;
491 sop.sem_flg = 0;
492 \&
493 if (semop(semid, &sop, 1) == \-1)
494 errExit("semop");
495 \&
496 exit(EXIT_SUCCESS);
497 }
498 .EE
499 .\" SRC END
500 .in
501 .SH SEE ALSO
502 .BR brk (2),
503 .BR mmap (2),
504 .BR shmctl (2),
505 .BR shmget (2),
506 .BR capabilities (7),
507 .BR shm_overview (7),
508 .BR sysvipc (7)