]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/shmop.2
getent.1, intro.1, time.1, _exit.2, _syscall.2, accept.2, access.2, acct.2, adjtimex...
[thirdparty/man-pages.git] / man2 / shmop.2
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
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.
11 .\"
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.
19 .\"
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 Sun Nov 28 17:06:19 1993, Rik Faith (faith@cs.unc.edu)
24 .\" with material from Luigi P. Bai (lpb@softint.com)
25 .\" Portions Copyright 1993 Luigi P. Bai
26 .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified, 5 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
28 .\" Modified, 19 Sep 2002, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Added SHM_REMAP flag description
30 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Added notes on capability requirements
32 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Language and formatting clean-ups
34 .\" Changed wording and placement of sentence regarding attachment
35 .\" of segments marked for destruction
36 .\"
37 .\" FIXME . Add an example program to this page.
38 .\" FIXME Linux 2.6.9 added SHM_EXEC, which should be documented
39 .TH SHMOP 2 2013-02-12 "Linux" "Linux Programmer's Manual"
40 .SH NAME
41 shmat, shmdt \- shared memory operations
42 .SH SYNOPSIS
43 .nf
44 .B #include <sys/types.h>
45 .B #include <sys/shm.h>
46
47 .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg );
48
49 .BI "int shmdt(const void *" shmaddr );
50 .fi
51 .SH DESCRIPTION
52 .BR shmat ()
53 attaches the 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 .LP
60 If
61 .I shmaddr
62 is NULL,
63 the system chooses a suitable (unused) address at which to attach
64 the segment.
65 .LP
66 If
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 Otherwise
78 .I shmaddr
79 must be a page-aligned address at which the attach occurs.
80 .PP
81 If
82 .B SHM_RDONLY
83 is specified in
84 .IR shmflg ,
85 the segment is attached for reading and the process must have
86 read permission for the segment.
87 Otherwise the segment is attached for read and write
88 and the process must have read and write permission for the segment.
89 There is no notion of a write-only shared memory segment.
90 .PP
91 The (Linux-specific)
92 .B SHM_REMAP
93 flag may be specified in
94 .I shmflg
95 to indicate that the mapping of the segment should replace
96 any existing mapping in the range starting at
97 .I shmaddr
98 and continuing for the size of the segment.
99 (Normally an
100 .B EINVAL
101 error would result if a mapping already exists in this address range.)
102 In this case,
103 .I shmaddr
104 must not be NULL.
105 .PP
106 The
107 .BR brk (2)
108 value of the calling process is not altered by the attach.
109 The segment will automatically be detached at process exit.
110 The same segment may be attached as a read and as a read-write
111 one, and more than once, in the process's address space.
112 .PP
113 A successful
114 .BR shmat ()
115 call updates the members of the
116 .I shmid_ds
117 structure (see
118 .BR shmctl (2))
119 associated with the shared memory segment as follows:
120 .IP
121 .I shm_atime
122 is set to the current time.
123 .IP
124 .I shm_lpid
125 is set to the process-ID of the calling process.
126 .IP
127 .I shm_nattch
128 is incremented by one.
129 .PP
130 .BR shmdt ()
131 detaches the shared memory segment located at the address specified by
132 .I shmaddr
133 from the address space of the calling process.
134 The to-be-detached segment must be currently
135 attached with
136 .I shmaddr
137 equal to the value returned by the attaching
138 .BR shmat ()
139 call.
140 .PP
141 On a successful
142 .BR shmdt ()
143 call the system updates the members of the
144 .I shmid_ds
145 structure associated with the shared memory segment as follows:
146 .IP
147 .I shm_dtime
148 is set to the current time.
149 .IP
150 .I shm_lpid
151 is set to the process-ID of the calling process.
152 .IP
153 .I shm_nattch
154 is decremented by one.
155 If it becomes 0 and the segment is marked for deletion,
156 the segment is deleted.
157 .PP
158 After a
159 .BR fork (2)
160 the child inherits the attached shared memory segments.
161
162 After an
163 .BR execve (2)
164 all attached shared memory segments are detached from the process.
165
166 Upon
167 .BR _exit (2)
168 all attached shared memory segments are detached from the process.
169 .SH RETURN VALUE
170 On success
171 .BR shmat ()
172 returns the address of the attached shared memory segment; on error
173 .I (void\ *)\ \-1
174 is returned, and
175 .I errno
176 is set to indicate the cause of the error.
177
178 On success
179 .BR shmdt ()
180 returns 0; on error \-1 is returned, and
181 .I errno
182 is set to indicate the cause of the error.
183 .SH ERRORS
184 When
185 .BR shmat ()
186 fails,
187 .I errno
188 is set to one of the following:
189 .TP
190 .B EACCES
191 The calling process does not have the required permissions for
192 the requested attach type, and does not have the
193 .B CAP_IPC_OWNER
194 capability.
195 .TP
196 .B EIDRM
197 \fIshmid\fP points to a removed identifier.
198 .TP
199 .B EINVAL
200 Invalid
201 .I shmid
202 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
203 specified) or invalid
204 .I shmaddr
205 value, or can't attach segment at
206 .IR shmaddr ,
207 or
208 .B SHM_REMAP
209 was specified and
210 .I shmaddr
211 was NULL.
212 .TP
213 .B ENOMEM
214 Could not allocate memory for the descriptor or for the page tables.
215 .PP
216 When
217 .BR shmdt ()
218 fails,
219 .I errno
220 is set as follows:
221 .TP
222 .B EINVAL
223 There is no shared memory segment attached at
224 .IR shmaddr ;
225 or,
226 .\" The following since 2.6.17-rc1:
227 .I shmaddr
228 is not aligned on a page boundary.
229 .SH CONFORMING TO
230 SVr4, POSIX.1-2001.
231 .\" SVr4 documents an additional error condition EMFILE.
232
233 In SVID 3 (or perhaps earlier)
234 the type of the \fIshmaddr\fP argument was changed from
235 .I "char *"
236 into
237 .IR "const void *" ,
238 and the returned type of
239 .BR shmat ()
240 from
241 .I "char *"
242 into
243 .IR "void *" .
244 (Linux libc4 and libc5 have the
245 .I "char *"
246 prototypes; glibc2 has
247 .IR "void *" .)
248 .SH NOTES
249 Using
250 .BR shmat ()
251 with
252 .I shmaddr
253 equal to NULL
254 is the preferred, portable way of attaching a shared memory segment.
255 Be aware that the shared memory segment attached in this way
256 may be attached at different addresses in different processes.
257 Therefore, any pointers maintained within the shared memory must be
258 made relative (typically to the starting address of the segment),
259 rather than absolute.
260 .PP
261 On Linux, it is possible to attach a shared memory segment even if it
262 is already marked to be deleted.
263 However, POSIX.1-2001 does not specify this behavior and
264 many other implementations do not support it.
265 .LP
266 The following system parameter affects
267 .BR shmat ():
268 .TP
269 .\" FIXME A good explanation of the rationale for the existence
270 .\" of SHMLBA would be useful here
271 .B SHMLBA
272 Segment low boundary address multiple.
273 Must be page aligned.
274 For the current implementation the
275 .B SHMLBA
276 value is
277 .BR PAGE_SIZE .
278 .\" FIXME That last sentence isn't true for all Linux
279 .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures)
280 .\" -- MTK, Nov 04
281 .PP
282 The implementation places no intrinsic limit on the per-process maximum
283 number of shared memory segments
284 .RB ( SHMSEG ).
285 .SH SEE ALSO
286 .BR brk (2),
287 .BR mmap (2),
288 .BR shmctl (2),
289 .BR shmget (2),
290 .BR capabilities (7),
291 .BR shm_overview (7),
292 .BR svipc (7)