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