]>
Commit | Line | Data |
---|---|---|
a62f5121 | 1 | .\" Copyright (C) 1996 Andries Brouwer <aeb@cwi.nl> |
c11b1abf | 2 | .\" and Copyright (C) 2006, 2007 Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 3 | .\" |
5fbde956 | 4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da MK |
5 | .\" |
6 | .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com> | |
7 | .\" Modified 2000-03-25 by Jim Van Zandt <jrv@vanzandt.mv.com> | |
8 | .\" Modified 2001-10-04 by John Levon <moz@compsoc.man.ac.uk> | |
9 | .\" Modified 2003-02-02 by Andi Kleen <ak@muc.de> | |
c11b1abf | 10 | .\" Modified 2003-05-21 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 11 | .\" MAP_LOCKED works from 2.5.37 |
c11b1abf | 12 | .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 13 | .\" Modified 2004-09-11 by aeb |
1a956089 MK |
14 | .\" Modified 2004-12-08, from Eric Estievenart <eric.estievenart@free.fr> |
15 | .\" Modified 2004-12-08, mtk, formatting tidy-ups | |
a62f5121 | 16 | .\" Modified 2006-12-04, mtk, various parts rewritten |
74fa61b7 | 17 | .\" 2007-07-10, mtk, Added an example program. |
e6205b0c | 18 | .\" 2008-11-18, mtk, document MAP_STACK |
fea681da | 19 | .\" |
45186a5d | 20 | .TH MMAP 2 2021-03-22 "Linux man-pages (unreleased)" |
fea681da MK |
21 | .SH NAME |
22 | mmap, munmap \- map or unmap files or devices into memory | |
82623c80 AC |
23 | .SH LIBRARY |
24 | Standard C library | |
8fc3b2cf | 25 | .RI ( libc ", " \-lc ) |
fea681da | 26 | .SH SYNOPSIS |
e0037472 | 27 | .nf |
fea681da | 28 | .B #include <sys/mman.h> |
68e4db0a | 29 | .PP |
14f5ae6d | 30 | .BI "void *mmap(void *" addr ", size_t " length \ |
a62f5121 | 31 | ", int " prot ", int " flags , |
e0037472 | 32 | .BI " int " fd ", off_t " offset ); |
14f5ae6d | 33 | .BI "int munmap(void *" addr ", size_t " length ); |
e0037472 | 34 | .fi |
dbfe9c70 | 35 | .PP |
45e97e2a | 36 | See NOTES for information on feature test macro requirements. |
fea681da | 37 | .SH DESCRIPTION |
1a956089 | 38 | .BR mmap () |
c13182ef | 39 | creates a new mapping in the virtual address space of |
5e8cde2f MK |
40 | the calling process. |
41 | The starting address for the new mapping is specified in | |
14f5ae6d | 42 | .IR addr . |
5e8cde2f MK |
43 | The |
44 | .I length | |
c19250ad | 45 | argument specifies the length of the mapping (which must be greater than 0). |
efeece04 | 46 | .PP |
5e8cde2f | 47 | If |
14f5ae6d | 48 | .I addr |
5e8cde2f | 49 | is NULL, |
365a54c7 MK |
50 | then the kernel chooses the (page-aligned) address |
51 | at which to create the mapping; | |
5e8cde2f | 52 | this is the most portable method of creating a new mapping. |
c13182ef | 53 | If |
14f5ae6d | 54 | .I addr |
5e8cde2f MK |
55 | is not NULL, |
56 | then the kernel takes it as a hint about where to place the mapping; | |
f58e9ed0 JH |
57 | on Linux, the kernel will pick a nearby page boundary (but always above |
58 | or equal to the value specified by | |
59 | .IR /proc/sys/vm/mmap_min_addr ) | |
60 | and attempt to create the mapping there. | |
61 | If another mapping already exists there, the kernel picks a new address that | |
62 | may or may not depend on the hint. | |
6aa7db0a | 63 | .\" Before Linux 2.6.24, the address was rounded up to the next page |
29328361 | 64 | .\" boundary; since 2.6.24, it is rounded down! |
5e8cde2f | 65 | The address of the new mapping is returned as the result of the call. |
efeece04 | 66 | .PP |
5e8cde2f MK |
67 | The contents of a file mapping (as opposed to an anonymous mapping; see |
68 | .B MAP_ANONYMOUS | |
d9bfdb9c | 69 | below), are initialized using |
fea681da MK |
70 | .I length |
71 | bytes starting at offset | |
72 | .I offset | |
5e8cde2f MK |
73 | in the file (or other object) referred to by the file descriptor |
74 | .IR fd . | |
a62f5121 MK |
75 | .I offset |
76 | must be a multiple of the page size as returned by | |
77 | .IR sysconf(_SC_PAGE_SIZE) . | |
fa38cc32 MK |
78 | .PP |
79 | After the | |
80 | .BR mmap () | |
81 | call has returned, the file descriptor, | |
82 | .IR fd , | |
83 | can be closed immediately without invalidating the mapping. | |
dd3568a1 | 84 | .PP |
fea681da MK |
85 | The |
86 | .I prot | |
c13182ef | 87 | argument describes the desired memory protection of the mapping |
5e8cde2f MK |
88 | (and must not conflict with the open mode of the file). |
89 | It is either | |
fea681da | 90 | .B PROT_NONE |
a62f5121 | 91 | or the bitwise OR of one or more of the following flags: |
fea681da MK |
92 | .TP 1.1i |
93 | .B PROT_EXEC | |
94 | Pages may be executed. | |
95 | .TP | |
96 | .B PROT_READ | |
97 | Pages may be read. | |
98 | .TP | |
99 | .B PROT_WRITE | |
100 | Pages may be written. | |
101 | .TP | |
102 | .B PROT_NONE | |
103 | Pages may not be accessed. | |
a2d56dbb MK |
104 | .\" |
105 | .SS The flags argument | |
fea681da MK |
106 | The |
107 | .I flags | |
a62f5121 MK |
108 | argument determines whether updates to the mapping |
109 | are visible to other processes mapping the same region, | |
ba7cb080 | 110 | and whether updates are carried through to the underlying file. |
d9bfdb9c | 111 | This behavior is determined by including exactly one |
a62f5121 | 112 | of the following values in |
5e8cde2f | 113 | .IR flags : |
ca90e95a | 114 | .TP |
fea681da | 115 | .B MAP_SHARED |
c13182ef | 116 | Share this mapping. |
bf525e90 MK |
117 | Updates to the mapping are visible to other processes mapping the same region, |
118 | and (in the case of file-backed mappings) | |
119 | are carried through to the underlying file. | |
72e8bdae MK |
120 | (To precisely control when updates are carried through |
121 | to the underlying file requires the use of | |
122 | .BR msync (2).) | |
fea681da | 123 | .TP |
d8aeb42d | 124 | .BR MAP_SHARED_VALIDATE " (since Linux 4.15)" |
79da08c8 | 125 | This flag provides the same behavior as |
d8aeb42d JK |
126 | .B MAP_SHARED |
127 | except that | |
128 | .B MAP_SHARED | |
129 | mappings ignore unknown flags in | |
130 | .IR flags . | |
79da08c8 | 131 | By contrast, when creating a mapping using |
08a239f3 | 132 | .BR MAP_SHARED_VALIDATE , |
79da08c8 MK |
133 | the kernel verifies all passed flags are known and fails the |
134 | mapping with the error | |
1ae6b2c7 | 135 | .B EOPNOTSUPP |
79da08c8 MK |
136 | for unknown flags. |
137 | This mapping type is also required to be able to use some mapping flags | |
138 | (e.g., | |
139 | .BR MAP_SYNC ). | |
d8aeb42d | 140 | .TP |
fea681da MK |
141 | .B MAP_PRIVATE |
142 | Create a private copy-on-write mapping. | |
a62f5121 MK |
143 | Updates to the mapping are not visible to other processes |
144 | mapping the same file, and are not carried through to | |
145 | the underlying file. | |
fea681da | 146 | It is unspecified whether changes made to the file after the |
1a956089 | 147 | .BR mmap () |
fea681da | 148 | call are visible in the mapped region. |
dd3568a1 | 149 | .PP |
421508eb MK |
150 | Both |
151 | .B MAP_SHARED | |
152 | and | |
153 | .B MAP_PRIVATE | |
154 | are described in POSIX.1-2001 and POSIX.1-2008. | |
79da08c8 MK |
155 | .B MAP_SHARED_VALIDATE |
156 | is a Linux extension. | |
efeece04 | 157 | .PP |
a62f5121 MK |
158 | In addition, zero or more of the following values can be ORed in |
159 | .IR flags : | |
fea681da | 160 | .TP |
c368e7ca MK |
161 | .BR MAP_32BIT " (since Linux 2.4.20, 2.6)" |
162 | Put the mapping into the first 2 Gigabytes of the process address space. | |
33a0ccb2 | 163 | This flag is supported only on x86-64, for 64-bit programs. |
c368e7ca | 164 | It was added to allow thread stacks to be allocated somewhere |
ee8655b5 | 165 | in the first 2\ GB of memory, |
c368e7ca MK |
166 | so as to improve context-switch performance on some early |
167 | 64-bit processors. | |
168 | .\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08 | |
169 | Modern x86-64 processors no longer have this performance problem, | |
170 | so use of this flag is not required on those systems. | |
171 | The | |
83314009 | 172 | .B MAP_32BIT |
c368e7ca | 173 | flag is ignored when |
a62f5121 | 174 | .B MAP_FIXED |
83314009 | 175 | is set. |
fea681da | 176 | .TP |
a62f5121 | 177 | .B MAP_ANON |
c13182ef | 178 | Synonym for |
9154da7f MK |
179 | .BR MAP_ANONYMOUS ; |
180 | provided for compatibility with other implementations. | |
a62f5121 | 181 | .TP |
fea681da | 182 | .B MAP_ANONYMOUS |
5e8cde2f | 183 | The mapping is not backed by any file; |
d9bfdb9c | 184 | its contents are initialized to zero. |
5e8cde2f | 185 | The |
fea681da | 186 | .I fd |
423cb9f7 | 187 | argument is ignored; |
c13182ef | 188 | however, some implementations require |
a62f5121 | 189 | .I fd |
c13182ef MK |
190 | to be \-1 if |
191 | .B MAP_ANONYMOUS | |
192 | (or | |
193 | .BR MAP_ANON ) | |
a62f5121 MK |
194 | is specified, |
195 | and portable applications should ensure this. | |
423cb9f7 JH |
196 | The |
197 | .I offset | |
198 | argument should be zero. | |
199 | .\" See the pgoff overflow check in do_mmap(). | |
200 | .\" See the offset check in sys_mmap in arch/x86/kernel/sys_x86_64.c. | |
8a7c2ab9 | 201 | Support for |
a62f5121 | 202 | .B MAP_ANONYMOUS |
c13182ef | 203 | in conjunction with |
51ffcca0 | 204 | .B MAP_SHARED |
8a7c2ab9 | 205 | was added in Linux 2.4. |
fea681da | 206 | .TP |
83314009 MK |
207 | .B MAP_DENYWRITE |
208 | This flag is ignored. | |
209 | .\" Introduced in 1.1.36, removed in 1.3.24. | |
a7a21482 MK |
210 | (Long ago\(emLinux 2.0 and earlier\(emit signaled |
211 | that attempts to write to the underlying file should fail with | |
ba6431be | 212 | .BR ETXTBSY . |
83314009 MK |
213 | But this was a source of denial-of-service attacks.) |
214 | .TP | |
215 | .B MAP_EXECUTABLE | |
216 | This flag is ignored. | |
217 | .\" Introduced in 1.1.38, removed in 1.3.24. Flag tested in proc_follow_link. | |
d9bfdb9c | 218 | .\" (Long ago, it signaled that the underlying file is an executable. |
83314009 MK |
219 | .\" However, that information was not really used anywhere.) |
220 | .\" Linus talked about DOS related to MAP_EXECUTABLE, but he was thinking of | |
221 | .\" MAP_DENYWRITE? | |
222 | .TP | |
fea681da | 223 | .B MAP_FILE |
c13182ef MK |
224 | Compatibility flag. |
225 | Ignored. | |
988db661 | 226 | .\" On some systems, this was required as the opposite of |
83314009 | 227 | .\" MAP_ANONYMOUS -- mtk, 1 May 2007 |
fea681da | 228 | .TP |
51ffcca0 | 229 | .B MAP_FIXED |
83314009 | 230 | Don't interpret |
14f5ae6d | 231 | .I addr |
83314009 | 232 | as a hint: place the mapping at exactly that address. |
14f5ae6d | 233 | .I addr |
87c8ded5 | 234 | must be suitably aligned: for most architectures a multiple of the page |
04bb0b99 | 235 | size is sufficient; however, some architectures may impose additional |
e2da344f MK |
236 | restrictions. |
237 | If the memory region specified by | |
14f5ae6d | 238 | .I addr |
83314009 | 239 | and |
5f6ad971 | 240 | .I length |
83314009 MK |
241 | overlaps pages of any existing mapping(s), then the overlapped |
242 | part of the existing mapping(s) will be discarded. | |
243 | If the specified address cannot be used, | |
244 | .BR mmap () | |
245 | will fail. | |
0113b287 | 246 | .IP |
56a033af | 247 | Software that aspires to be portable should use the |
1ae6b2c7 | 248 | .B MAP_FIXED |
56a033af | 249 | flag with care, |
9b92bc14 | 250 | keeping in mind that the exact layout of a process's memory mappings |
e2da344f MK |
251 | is allowed to change significantly between kernel versions, |
252 | C library versions, and operating system releases. | |
1ae6b2c7 | 253 | .I Carefully read the discussion of this flag in NOTES! |
c6a51c06 MH |
254 | .TP |
255 | .BR MAP_FIXED_NOREPLACE " (since Linux 4.17)" | |
3a548c59 MK |
256 | .\" commit a4ff8e8620d3f4f50ac4b41e8067b7d395056843 |
257 | This flag provides behavior that is similar to | |
228d889f MK |
258 | .B MAP_FIXED |
259 | with respect to the | |
260 | .I addr | |
3a548c59 | 261 | enforcement, but differs in that |
228d889f | 262 | .B MAP_FIXED_NOREPLACE |
509935b7 | 263 | never clobbers a preexisting mapped range. |
007d7833 | 264 | If the requested range would collide with an existing mapping, |
3a548c59 | 265 | then this call fails with the error |
c6a51c06 | 266 | .B EEXIST. |
007d7833 MK |
267 | This flag can therefore be used as a way to atomically |
268 | (with respect to other threads) attempt to map an address range: | |
269 | one thread will succeed; all others will report failure. | |
08a239f3 | 270 | .IP |
a8fd3403 | 271 | Note that older kernels which do not recognize the |
1ae6b2c7 | 272 | .B MAP_FIXED_NOREPLACE |
509935b7 | 273 | flag will typically (upon detecting a collision with a preexisting mapping) |
3b79b7f9 | 274 | fall back to a |
ad1f0dee | 275 | .RB \(lqnon- MAP_FIXED \(rq |
3b79b7f9 | 276 | type of behavior: |
3a548c59 | 277 | they will return an address that is different from the requested address. |
007d7833 | 278 | Therefore, backward-compatible software |
c6a51c06 | 279 | should check the returned address against the requested address. |
fea681da | 280 | .TP |
83314009 | 281 | .B MAP_GROWSDOWN |
86f12eb0 MK |
282 | This flag is used for stacks. |
283 | It indicates to the kernel virtual memory system that the mapping | |
5fab2e7c | 284 | should extend downward in memory. |
176b1a76 MK |
285 | The return address is one page lower than the memory area that is |
286 | actually created in the process's virtual address space. | |
287 | Touching an address in the "guard" page below the mapping will cause | |
288 | the mapping to grow by a page. | |
289 | This growth can be repeated until the mapping grows to within a | |
290 | page of the high end of the next lower mapping, | |
291 | at which point touching the "guard" page will result in a | |
292 | .B SIGSEGV | |
293 | signal. | |
83314009 | 294 | .TP |
76a34baa | 295 | .BR MAP_HUGETLB " (since Linux 2.6.32)" |
02576fcb | 296 | Allocate the mapping using "huge" pages. |
66a9882e | 297 | See the Linux kernel source file |
b49c2acb | 298 | .I Documentation/admin\-guide/mm/hugetlbpage.rst |
f1461fe1 | 299 | for further information, as well as NOTES, below. |
76a34baa | 300 | .TP |
5d2038b6 MK |
301 | .BR MAP_HUGE_2MB ", " MAP_HUGE_1GB " (since Linux 3.8)" |
302 | .\" See https://lwn.net/Articles/533499/ | |
303 | Used in conjunction with | |
304 | .B MAP_HUGETLB | |
c4b7e5ac | 305 | to select alternative hugetlb page sizes (respectively, 2\ MB and 1\ GB) |
5d2038b6 | 306 | on systems that support multiple hugetlb page sizes. |
efeece04 | 307 | .IP |
5d2038b6 MK |
308 | More generally, the desired huge page size can be configured by encoding |
309 | the base-2 logarithm of the desired page size in the six bits at the offset | |
310 | .BR MAP_HUGE_SHIFT . | |
311 | (A value of zero in this bit field provides the default huge page size; | |
a6bf8e7e | 312 | the default huge page size can be discovered via the |
5d2038b6 MK |
313 | .I Hugepagesize |
314 | field exposed by | |
315 | .IR /proc/meminfo .) | |
316 | Thus, the above two constants are defined as: | |
efeece04 | 317 | .IP |
5d2038b6 | 318 | .in +4n |
b8302363 | 319 | .EX |
5d2038b6 MK |
320 | #define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT) |
321 | #define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT) | |
b8302363 | 322 | .EE |
e646a1ba | 323 | .in |
efeece04 | 324 | .IP |
5d2038b6 MK |
325 | The range of huge page sizes that are supported by the system |
326 | can be discovered by listing the subdirectories in | |
327 | .IR /sys/kernel/mm/hugepages . | |
328 | .TP | |
83314009 | 329 | .BR MAP_LOCKED " (since Linux 2.5.37)" |
db4e8e25 | 330 | Mark the mapped region to be locked in the same way as |
74d32233 | 331 | .BR mlock (2). |
db4e8e25 MK |
332 | This implementation will try to populate (prefault) the whole range but the |
333 | .BR mmap () | |
334 | call doesn't fail with | |
7e3786bc | 335 | .B ENOMEM |
911f1c7a MK |
336 | if this fails. |
337 | Therefore major faults might happen later on. | |
338 | So the semantic is not as strong as | |
7e3786bc | 339 | .BR mlock (2). |
911f1c7a | 340 | One should use |
bf7bc8b8 | 341 | .BR mmap () |
911f1c7a | 342 | plus |
7e3786bc | 343 | .BR mlock (2) |
911f1c7a MK |
344 | when major faults are not acceptable after the initialization of the mapping. |
345 | The | |
1ae6b2c7 | 346 | .B MAP_LOCKED |
911f1c7a | 347 | flag is ignored in older kernels. |
83314009 | 348 | .\" If set, the mapped pages will not be swapped out. |
fea681da MK |
349 | .TP |
350 | .BR MAP_NONBLOCK " (since Linux 2.5.46)" | |
3f06ade3 | 351 | This flag is meaningful only in conjunction with |
51ffcca0 | 352 | .BR MAP_POPULATE . |
c13182ef | 353 | Don't perform read-ahead: |
33a0ccb2 | 354 | create page tables entries only for pages |
51ffcca0 | 355 | that are already present in RAM. |
90fb5f52 KS |
356 | Since Linux 2.6.23, |
357 | .\" commit 54cb8821de07f2ffcd28c380ce9b93d5784b40d7 | |
358 | this flag causes | |
1ae6b2c7 | 359 | .B MAP_POPULATE |
f38fa944 | 360 | to do nothing. |
487c2f05 | 361 | One day, the combination of |
1ae6b2c7 | 362 | .B MAP_POPULATE |
f38fa944 | 363 | and |
1ae6b2c7 | 364 | .B MAP_NONBLOCK |
3b777aff | 365 | may be reimplemented. |
83314009 MK |
366 | .TP |
367 | .B MAP_NORESERVE | |
368 | Do not reserve swap space for this mapping. | |
369 | When swap space is reserved, one has the guarantee | |
370 | that it is possible to modify the mapping. | |
8bd58774 MK |
371 | When swap space is not reserved one might get |
372 | .B SIGSEGV | |
373 | upon a write | |
83314009 MK |
374 | if no physical memory is available. |
375 | See also the discussion of the file | |
376 | .I /proc/sys/vm/overcommit_memory | |
377 | in | |
378 | .BR proc (5). | |
33a0ccb2 | 379 | In kernels before 2.6, this flag had effect only for |
83314009 MK |
380 | private writable mappings. |
381 | .TP | |
382 | .BR MAP_POPULATE " (since Linux 2.5.46)" | |
f38fa944 MK |
383 | Populate (prefault) page tables for a mapping. |
384 | For a file mapping, this causes read-ahead on the file. | |
bbebbb6d | 385 | This will help to reduce blocking on page faults later. |
285a7373 BM |
386 | The |
387 | .BR mmap () | |
388 | call doesn't fail if the mapping cannot be populated (for example, due | |
389 | to limitations on the number of mapped huge pages when using | |
390 | .BR MAP_HUGETLB ). | |
8a7c2ab9 | 391 | Support for |
1ae6b2c7 | 392 | .B MAP_POPULATE |
8a7c2ab9 | 393 | in conjunction with private mappings was added in Linux 2.6.23. |
e6205b0c MK |
394 | .TP |
395 | .BR MAP_STACK " (since Linux 2.6.27)" | |
396 | Allocate the mapping at an address suitable for a process | |
397 | or thread stack. | |
d3d88123 | 398 | .IP |
91243dad MK |
399 | This flag is currently a no-op on Linux. |
400 | However, by employing this flag, applications can ensure that | |
401 | they transparently obtain support if the flag | |
402 | is implemented in the future. | |
403 | Thus, it is used in the glibc threading implementation to allow for | |
404 | the fact that some architectures may (later) require special treatment | |
405 | for stack allocations. | |
67b59ff5 | 406 | .\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08 |
e6205b0c MK |
407 | .\" commit cd98a04a59e2f94fa64d5bf1e26498d27427d5e7 |
408 | .\" http://thread.gmane.org/gmane.linux.kernel/720412 | |
409 | .\" "pthread_create() slow for many threads; also time to revisit 64b | |
410 | .\" context switch optimization?" | |
d3d88123 | 411 | A further reason to employ this flag is portability: |
1ae6b2c7 | 412 | .B MAP_STACK |
d3d88123 | 413 | exists (and has an effect) on some other systems (e.g., some of the BSDs). |
12062404 | 414 | .TP |
d8aeb42d | 415 | .BR MAP_SYNC " (since Linux 4.15)" |
79da08c8 | 416 | This flag is available only with the |
d8aeb42d | 417 | .B MAP_SHARED_VALIDATE |
79da08c8 MK |
418 | mapping type; |
419 | mappings of type | |
d8aeb42d | 420 | .B MAP_SHARED |
79da08c8 | 421 | will silently ignore this flag. |
b138773b MK |
422 | This flag is supported only for files supporting DAX |
423 | (direct mapping of persistent memory). | |
79da08c8 | 424 | For other files, creating a mapping with this flag results in an |
d8aeb42d | 425 | .B EOPNOTSUPP |
b138773b | 426 | error. |
79da08c8 | 427 | .IP |
b138773b | 428 | Shared file mappings with this flag provide the guarantee that while |
9d4976ce | 429 | some memory is mapped writable in the address space of the process, |
b138773b MK |
430 | it will be visible in the same file at the same offset even after |
431 | the system crashes or is rebooted. | |
5bc2d858 MK |
432 | In conjunction with the use of appropriate CPU instructions, |
433 | this provides users of such mappings with a more efficient way | |
434 | of making data modifications persistent. | |
d8aeb42d | 435 | .TP |
12062404 MK |
436 | .BR MAP_UNINITIALIZED " (since Linux 2.6.33)" |
437 | Don't clear anonymous pages. | |
438 | This flag is intended to improve performance on embedded devices. | |
33a0ccb2 | 439 | This flag is honored only if the kernel was configured with the |
12062404 MK |
440 | .B CONFIG_MMAP_ALLOW_UNINITIALIZED |
441 | option. | |
442 | Because of the security implications, | |
443 | that option is normally enabled only on embedded devices | |
444 | (i.e., devices where one has complete control of the contents of user memory). | |
dd3568a1 | 445 | .PP |
7c7adcbe MK |
446 | Of the above flags, only |
447 | .B MAP_FIXED | |
78cdbef7 | 448 | is specified in POSIX.1-2001 and POSIX.1-2008. |
7c7adcbe MK |
449 | However, most systems also support |
450 | .B MAP_ANONYMOUS | |
451 | (or its synonym | |
452 | .BR MAP_ANON ). | |
f5f41651 MK |
453 | .\" FIXME . for later review when Issue 8 is one day released... |
454 | .\" POSIX may add MAP_ANON in the future | |
455 | .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8 | |
456 | .\" http://austingroupbugs.net/view.php?id=850 | |
de5f7e28 | 457 | .SS munmap() |
fea681da | 458 | The |
1a956089 | 459 | .BR munmap () |
fea681da MK |
460 | system call deletes the mappings for the specified address range, and |
461 | causes further references to addresses within the range to generate | |
c13182ef MK |
462 | invalid memory references. |
463 | The region is also automatically unmapped | |
464 | when the process is terminated. | |
465 | On the other hand, closing the file | |
fea681da | 466 | descriptor does not unmap the region. |
dd3568a1 | 467 | .PP |
fea681da | 468 | The address |
14f5ae6d | 469 | .I addr |
0e824bcb MK |
470 | must be a multiple of the page size (but |
471 | .I length | |
472 | need not be). | |
c13182ef | 473 | All pages containing a part |
fea681da | 474 | of the indicated range are unmapped, and subsequent references |
8bd58774 MK |
475 | to these pages will generate |
476 | .BR SIGSEGV . | |
c13182ef | 477 | It is not an error if the |
fea681da | 478 | indicated range does not contain any mapped pages. |
47297adb | 479 | .SH RETURN VALUE |
fea681da | 480 | On success, |
1a956089 | 481 | .BR mmap () |
fea681da MK |
482 | returns a pointer to the mapped area. |
483 | On error, the value | |
484 | .B MAP_FAILED | |
c13182ef | 485 | (that is, |
009df872 | 486 | .IR "(void\ *)\ \-1" ) |
5e8cde2f | 487 | is returned, and |
fea681da | 488 | .I errno |
855d489a | 489 | is set to indicate the error. |
efeece04 | 490 | .PP |
fea681da | 491 | On success, |
1a956089 | 492 | .BR munmap () |
80691a91 MK |
493 | returns 0. |
494 | On failure, it returns \-1, and | |
fea681da | 495 | .I errno |
855d489a | 496 | is set to indicate the error (probably to |
51ffcca0 | 497 | .BR EINVAL ). |
fea681da MK |
498 | .SH ERRORS |
499 | .TP | |
500 | .B EACCES | |
501 | A file descriptor refers to a non-regular file. | |
5e7c71f6 | 502 | Or a file mapping was requested, but |
fea681da MK |
503 | .I fd |
504 | is not open for reading. | |
c13182ef MK |
505 | Or |
506 | .B MAP_SHARED | |
507 | was requested and | |
508 | .B PROT_WRITE | |
51ffcca0 | 509 | is set, but |
fea681da | 510 | .I fd |
682edefb MK |
511 | is not open in read/write |
512 | .RB ( O_RDWR ) | |
513 | mode. | |
c13182ef MK |
514 | Or |
515 | .B PROT_WRITE | |
51ffcca0 | 516 | is set, but the file is append-only. |
fea681da MK |
517 | .TP |
518 | .B EAGAIN | |
83cd3686 MK |
519 | The file has been locked, or too much memory has been locked (see |
520 | .BR setrlimit (2)). | |
fea681da MK |
521 | .TP |
522 | .B EBADF | |
523 | .I fd | |
c13182ef | 524 | is not a valid file descriptor (and |
51ffcca0 MK |
525 | .B MAP_ANONYMOUS |
526 | was not set). | |
fea681da | 527 | .TP |
c6a51c06 | 528 | .B EEXIST |
1ae6b2c7 | 529 | .B MAP_FIXED_NOREPLACE |
95b9ecbf MK |
530 | was specified in |
531 | .IR flags , | |
532 | and the range covered by | |
1ae6b2c7 | 533 | .I addr |
95b9ecbf | 534 | and |
1ae6b2c7 | 535 | .I length |
c3617f39 | 536 | clashes with an existing mapping. |
c6a51c06 | 537 | .TP |
fea681da MK |
538 | .B EINVAL |
539 | We don't like | |
14f5ae6d | 540 | .IR addr , |
62a04c81 | 541 | .IR length , |
fea681da | 542 | or |
0daa9e92 | 543 | .I offset |
62a04c81 MK |
544 | (e.g., they are too large, or not aligned on a page boundary). |
545 | .TP | |
546 | .B EINVAL | |
f99fc197 | 547 | (since Linux 2.6.12) |
fea681da | 548 | .I length |
62a04c81 MK |
549 | was 0. |
550 | .TP | |
551 | .B EINVAL | |
552 | .I flags | |
c4f0c33f NF |
553 | contained none of |
554 | .BR MAP_PRIVATE , | |
aa0da2b9 | 555 | .BR MAP_SHARED , |
fea681da | 556 | or |
c4f0c33f | 557 | .BR MAP_SHARED_VALIDATE . |
fea681da MK |
558 | .TP |
559 | .B ENFILE | |
560 | .\" This is for shared anonymous segments | |
561 | .\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp() | |
e258766b | 562 | The system-wide limit on the total number of open files has been reached. |
fea681da MK |
563 | .\" .TP |
564 | .\" .B ENOEXEC | |
565 | .\" A file could not be mapped for reading. | |
566 | .TP | |
567 | .B ENODEV | |
9ee4a2b6 | 568 | The underlying filesystem of the specified file does not support |
fea681da MK |
569 | memory mapping. |
570 | .TP | |
571 | .B ENOMEM | |
74309bed MK |
572 | No memory is available. |
573 | .TP | |
574 | .B ENOMEM | |
575 | The process's maximum number of mappings would have been exceeded. | |
c0b89788 | 576 | This error can also occur for |
3804d39d | 577 | .BR munmap (), |
c0b89788 MK |
578 | when unmapping a region in the middle of an existing mapping, |
579 | since this results in two smaller mappings on either side of | |
580 | the region being unmapped. | |
fea681da | 581 | .TP |
c87d084b JG |
582 | .B ENOMEM |
583 | (since Linux 4.7) | |
584 | The process's | |
585 | .B RLIMIT_DATA | |
586 | limit, described in | |
587 | .BR getrlimit (2), | |
588 | would have been exceeded. | |
589 | .TP | |
9c07c1e1 TM |
590 | .B ENOMEM |
591 | We don't like | |
592 | .IR addr , | |
593 | because it exceeds the virtual address space of the CPU. | |
594 | .TP | |
038f5175 MK |
595 | .B EOVERFLOW |
596 | On 32-bit architecture together with the large file extension | |
597 | (i.e., using 64-bit | |
598 | .IR off_t ): | |
599 | the number of pages used for | |
600 | .I length | |
601 | plus number of pages used for | |
602 | .I offset | |
603 | would overflow | |
604 | .I "unsigned long" | |
605 | (32 bits). | |
606 | .TP | |
fea681da MK |
607 | .B EPERM |
608 | The | |
609 | .I prot | |
610 | argument asks for | |
611 | .B PROT_EXEC | |
9ee4a2b6 | 612 | but the mapped area belongs to a file on a filesystem that |
fea681da MK |
613 | was mounted no-exec. |
614 | .\" (Since 2.4.25 / 2.6.0.) | |
615 | .TP | |
fbab10e5 MK |
616 | .B EPERM |
617 | The operation was prevented by a file seal; see | |
618 | .BR fcntl (2). | |
619 | .TP | |
090fdddb MK |
620 | .B EPERM |
621 | The | |
622 | .B MAP_HUGETLB | |
623 | flag was specified, but the caller was not privileged (did not have the | |
624 | .B CAP_IPC_LOCK | |
625 | capability) | |
626 | and is not a member of the | |
627 | .I sysctl_hugetlb_shm_group | |
628 | group; see the description of | |
629 | .I /proc/sys/vm/sysctl_hugetlb_shm_group | |
630 | in | |
631 | .TP | |
fea681da | 632 | .B ETXTBSY |
c13182ef | 633 | .B MAP_DENYWRITE |
51ffcca0 | 634 | was set but the object specified by |
fea681da MK |
635 | .I fd |
636 | is open for writing. | |
dd3568a1 | 637 | .PP |
fea681da MK |
638 | Use of a mapped region can result in these signals: |
639 | .TP | |
640 | .B SIGSEGV | |
1e321034 | 641 | Attempted write into a region mapped as read-only. |
fea681da MK |
642 | .TP |
643 | .B SIGBUS | |
9807d9af MK |
644 | Attempted access to a page of the buffer that lies beyond the |
645 | end of the mapped file. | |
646 | For an explanation of the treatment of the bytes in the page that | |
647 | corresponds to the end of a mapped file that is not a multiple | |
648 | of the page size, see NOTES. | |
8fddf95a MS |
649 | .SH ATTRIBUTES |
650 | For an explanation of the terms used in this section, see | |
651 | .BR attributes (7). | |
c466875e MK |
652 | .ad l |
653 | .nh | |
8fddf95a MS |
654 | .TS |
655 | allbox; | |
c466875e | 656 | lbx lb lb |
8fddf95a MS |
657 | l l l. |
658 | Interface Attribute Value | |
659 | T{ | |
660 | .BR mmap (), | |
661 | .BR munmap () | |
662 | T} Thread safety MT-Safe | |
663 | .TE | |
c466875e MK |
664 | .hy |
665 | .ad | |
666 | .sp 1 | |
3113c7f3 | 667 | .SH STANDARDS |
78cdbef7 | 668 | POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD. |
2b2581ee MK |
669 | .\" SVr4 documents additional error codes ENXIO and ENODEV. |
670 | .\" SUSv2 documents additional error codes EMFILE and EOVERFLOW. | |
bd168648 | 671 | .PP |
fea681da | 672 | On POSIX systems on which |
1a956089 | 673 | .BR mmap (), |
9af134cd | 674 | .BR msync (2), |
fea681da | 675 | and |
1a956089 | 676 | .BR munmap () |
fea681da MK |
677 | are available, |
678 | .B _POSIX_MAPPED_FILES | |
6387216b MK |
679 | is defined in \fI<unistd.h>\fP to a value greater than 0. |
680 | (See also | |
fea681da | 681 | .BR sysconf (3).) |
97c1eac8 | 682 | .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L. |
fea681da MK |
683 | .\" -1: unavailable, 0: ask using sysconf(). |
684 | .\" glibc defines it to 1. | |
a1d5f77c | 685 | .SH NOTES |
dd6ceee6 MK |
686 | Memory mapped by |
687 | .BR mmap () | |
688 | is preserved across | |
689 | .BR fork (2), | |
690 | with the same attributes. | |
691 | .PP | |
692 | A file is mapped in multiples of the page size. | |
693 | For a file that is not | |
9807d9af MK |
694 | a multiple of the page size, |
695 | the remaining bytes in the partial page at the end of the mapping | |
696 | are zeroed when mapped, | |
697 | and modifications to that region are not written out to the file. | |
dd6ceee6 MK |
698 | The effect of |
699 | changing the size of the underlying file of a mapping on the pages that | |
700 | correspond to added or removed regions of the file is unspecified. | |
701 | .PP | |
34ccb744 | 702 | On some hardware architectures (e.g., i386), |
0daa9e92 | 703 | .B PROT_WRITE |
f3edaabb MK |
704 | implies |
705 | .BR PROT_READ . | |
a1d5f77c MK |
706 | It is architecture dependent whether |
707 | .B PROT_READ | |
708 | implies | |
709 | .B PROT_EXEC | |
710 | or not. | |
711 | Portable programs should always set | |
712 | .B PROT_EXEC | |
713 | if they intend to execute code in the new mapping. | |
efeece04 | 714 | .PP |
80d17cfa MK |
715 | The portable way to create a mapping is to specify |
716 | .I addr | |
717 | as 0 (NULL), and omit | |
718 | .B MAP_FIXED | |
719 | from | |
720 | .IR flags . | |
721 | In this case, the system chooses the address for the mapping; | |
46cdb997 | 722 | the address is chosen so as not to conflict with any existing mapping, |
80d17cfa MK |
723 | and will not be 0. |
724 | If the | |
725 | .B MAP_FIXED | |
726 | flag is specified, and | |
727 | .I addr | |
491cd2f0 | 728 | is 0 (NULL), then the mapped address will be 0 (NULL). |
efeece04 | 729 | .PP |
45e97e2a MK |
730 | Certain |
731 | .I flags | |
f8619b6a MK |
732 | constants are defined only if suitable feature test macros are defined |
733 | (possibly by default): | |
1ae6b2c7 | 734 | .B _DEFAULT_SOURCE |
f8619b6a MK |
735 | with glibc 2.19 or later; |
736 | or | |
1ae6b2c7 | 737 | .B _BSD_SOURCE |
45e97e2a | 738 | or |
1ae6b2c7 | 739 | .B _SVID_SOURCE |
f8619b6a | 740 | in glibc 2.19 and earlier. |
50efeef8 | 741 | (Employing |
1ae6b2c7 | 742 | .B _GNU_SOURCE |
45e97e2a MK |
743 | also suffices, |
744 | and requiring that macro specifically would have been more logical, | |
76c637e1 | 745 | since these flags are all Linux-specific.) |
45e97e2a MK |
746 | The relevant flags are: |
747 | .BR MAP_32BIT , | |
1ae6b2c7 | 748 | .B MAP_ANONYMOUS |
45e97e2a MK |
749 | (and the synonym |
750 | .BR MAP_ANON ), | |
751 | .BR MAP_DENYWRITE , | |
752 | .BR MAP_EXECUTABLE , | |
753 | .BR MAP_FILE , | |
754 | .BR MAP_GROWSDOWN , | |
755 | .BR MAP_HUGETLB , | |
756 | .BR MAP_LOCKED , | |
757 | .BR MAP_NONBLOCK , | |
758 | .BR MAP_NORESERVE , | |
759 | .BR MAP_POPULATE , | |
760 | and | |
761 | .BR MAP_STACK . | |
efeece04 | 762 | .PP |
3bd859bf MK |
763 | An application can determine which pages of a mapping are |
764 | currently resident in the buffer/page cache using | |
765 | .BR mincore (2). | |
35c189fb | 766 | .\" |
49a8d3a1 MK |
767 | .SS Using MAP_FIXED safely |
768 | The only safe use for | |
1ae6b2c7 | 769 | .B MAP_FIXED |
49a8d3a1 | 770 | is where the address range specified by |
1ae6b2c7 | 771 | .I addr |
49a8d3a1 MK |
772 | and |
773 | .I length | |
774 | was previously reserved using another mapping; | |
775 | otherwise, the use of | |
1ae6b2c7 | 776 | .B MAP_FIXED |
49a8d3a1 MK |
777 | is hazardous because it forcibly removes preexisting mappings, |
778 | making it easy for a multithreaded process to corrupt its own address space. | |
779 | .PP | |
780 | For example, suppose that thread A looks through | |
781 | .I /proc/<pid>/maps | |
0664253b | 782 | in order to locate an unused address range that it can map using |
56f85b84 | 783 | .BR MAP_FIXED , |
49a8d3a1 MK |
784 | while thread B simultaneously acquires part or all of that same |
785 | address range. | |
56f85b84 | 786 | When thread A subsequently employs |
49a8d3a1 | 787 | .BR mmap(MAP_FIXED) , |
56f85b84 | 788 | it will effectively clobber the mapping that thread B created. |
49a8d3a1 MK |
789 | In this scenario, |
790 | thread B need not create a mapping directly; simply making a library call | |
791 | that, internally, uses | |
792 | .BR dlopen (3) | |
793 | to load some other shared library, will suffice. | |
794 | The | |
795 | .BR dlopen (3) | |
796 | call will map the library into the process's address space. | |
797 | Furthermore, almost any library call may be implemented in a way that | |
798 | adds memory mappings to the address space, either with this technique, | |
799 | or by simply allocating memory. | |
800 | Examples include | |
801 | .BR brk (2), | |
802 | .BR malloc (3), | |
803 | .BR pthread_create (3), | |
804 | and the PAM libraries | |
805 | .UR http://www.linux-pam.org | |
806 | .UE . | |
807 | .PP | |
0949fa5e | 808 | Since Linux 4.17, a multithreaded program can use the |
1ae6b2c7 | 809 | .B MAP_FIXED_NOREPLACE |
0949fa5e MK |
810 | flag to avoid the hazard described above |
811 | when attempting to create a mapping at a fixed address | |
812 | that has not been reserved by a preexisting mapping. | |
49a8d3a1 | 813 | .\" |
0f319769 MK |
814 | .SS Timestamps changes for file-backed mappings |
815 | For file-backed mappings, the | |
816 | .I st_atime | |
817 | field for the mapped file may be updated at any time between the | |
818 | .BR mmap () | |
819 | and the corresponding unmapping; the first reference to a mapped | |
820 | page will update the field if it has not been already. | |
dd3568a1 | 821 | .PP |
0f319769 MK |
822 | The |
823 | .I st_ctime | |
824 | and | |
825 | .I st_mtime | |
826 | field for a file mapped with | |
827 | .B PROT_WRITE | |
828 | and | |
829 | .B MAP_SHARED | |
830 | will be updated after | |
831 | a write to the mapped region, and before a subsequent | |
832 | .BR msync (2) | |
833 | with the | |
834 | .B MS_SYNC | |
835 | or | |
836 | .B MS_ASYNC | |
837 | flag, if one occurs. | |
838 | .\" | |
f1461fe1 MK |
839 | .SS Huge page (Huge TLB) mappings |
840 | For mappings that employ huge pages, the requirements for the arguments of | |
841 | .BR mmap () | |
842 | and | |
843 | .BR munmap () | |
844 | differ somewhat from the requirements for mappings | |
845 | that use the native system page size. | |
efeece04 | 846 | .PP |
f1461fe1 MK |
847 | For |
848 | .BR mmap (), | |
849 | .I offset | |
850 | must be a multiple of the underlying huge page size. | |
851 | The system automatically aligns | |
852 | .I length | |
853 | to be a multiple of the underlying huge page size. | |
efeece04 | 854 | .PP |
f1461fe1 MK |
855 | For |
856 | .BR munmap (), | |
d556548b | 857 | .IR addr , |
f1461fe1 MK |
858 | and |
859 | .I length | |
860 | must both be a multiple of the underlying huge page size. | |
861 | .\" | |
0722a578 | 862 | .SS C library/kernel differences |
35c189fb MK |
863 | This page describes the interface provided by the glibc |
864 | .BR mmap () | |
865 | wrapper function. | |
866 | Originally, this function invoked a system call of the same name. | |
867 | Since kernel 2.4, that system call has been superseded by | |
868 | .BR mmap2 (2), | |
869 | and nowadays | |
870 | .\" Since around glibc 2.1/2.2, depending on the platform. | |
871 | the glibc | |
872 | .BR mmap () | |
873 | wrapper function invokes | |
874 | .BR mmap2 (2) | |
875 | with a suitably adjusted value for | |
876 | .IR offset . | |
fea681da | 877 | .SH BUGS |
329ad271 | 878 | On Linux, there are no guarantees like those suggested above under |
c13182ef | 879 | .BR MAP_NORESERVE . |
dbc53ca8 | 880 | By default, any process can be killed |
fea681da | 881 | at any moment when the system runs out of memory. |
efeece04 | 882 | .PP |
dbc53ca8 MK |
883 | In kernels before 2.6.7, the |
884 | .B MAP_POPULATE | |
33a0ccb2 | 885 | flag has effect only if |
dbc53ca8 MK |
886 | .I prot |
887 | is specified as | |
888 | .BR PROT_NONE . | |
efeece04 | 889 | .PP |
c13182ef | 890 | SUSv3 specifies that |
c8f3e580 MK |
891 | .BR mmap () |
892 | should fail if | |
893 | .I length | |
894 | is 0. | |
895 | However, in kernels before 2.6.12, | |
896 | .BR mmap () | |
897 | succeeded in this case: no mapping was created and the call returned | |
14f5ae6d | 898 | .IR addr . |
c8f3e580 MK |
899 | Since kernel 2.6.12, |
900 | .BR mmap () | |
901 | fails with the error | |
902 | .B EINVAL | |
903 | for this case. | |
efeece04 | 904 | .PP |
a780f17b MK |
905 | POSIX specifies that the system shall always |
906 | zero fill any partial page at the end | |
b072a788 | 907 | of the object and that system will never write any modification of the |
a780f17b MK |
908 | object beyond its end. |
909 | On Linux, when you write data to such partial page after the end | |
b072a788 | 910 | of the object, the data stays in the page cache even after the file |
a780f17b MK |
911 | is closed and unmapped |
912 | and even though the data is never written to the file itself, | |
913 | subsequent mappings may see the modified content. | |
914 | In some cases, this could be fixed by calling | |
915 | .BR msync (2) | |
916 | before the unmap takes place; | |
4e07c70f MK |
917 | however, this doesn't work on |
918 | .BR tmpfs (5) | |
b758a50a | 919 | (for example, when using the POSIX shared memory interface documented in |
a780f17b | 920 | .BR shm_overview (7)). |
a14af333 | 921 | .SH EXAMPLES |
2e001ad4 MK |
922 | .\" FIXME . Add an example here that uses an anonymous shared region for |
923 | .\" IPC between parent and child. | |
74fa61b7 MK |
924 | The following program prints part of the file specified in |
925 | its first command-line argument to standard output. | |
926 | The range of bytes to be printed is specified via offset and length | |
927 | values in the second and third command-line arguments. | |
928 | The program creates a memory mapping of the required | |
929 | pages of the file and then uses | |
930 | .BR write (2) | |
931 | to output the desired bytes. | |
f30b7415 | 932 | .SS Program source |
33857069 | 933 | .\" SRC BEGIN (mmap.c) |
e7d0bb47 | 934 | .EX |
74fa61b7 MK |
935 | #include <fcntl.h> |
936 | #include <stdio.h> | |
937 | #include <stdlib.h> | |
4ae706b0 AC |
938 | #include <sys/mman.h> |
939 | #include <sys/stat.h> | |
74fa61b7 MK |
940 | #include <unistd.h> |
941 | ||
d1a71985 | 942 | #define handle_error(msg) \e |
4407d3d8 MK |
943 | do { perror(msg); exit(EXIT_FAILURE); } while (0) |
944 | ||
74fa61b7 MK |
945 | int |
946 | main(int argc, char *argv[]) | |
947 | { | |
948 | char *addr; | |
949 | int fd; | |
950 | struct stat sb; | |
951 | off_t offset, pa_offset; | |
952 | size_t length; | |
953 | ssize_t s; | |
954 | ||
fbbfa7ce | 955 | if (argc < 3 || argc > 4) { |
d1a71985 | 956 | fprintf(stderr, "%s file offset [length]\en", argv[0]); |
74fa61b7 MK |
957 | exit(EXIT_FAILURE); |
958 | } | |
959 | ||
960 | fd = open(argv[1], O_RDONLY); | |
4407d3d8 | 961 | if (fd == \-1) |
8568021d | 962 | handle_error("open"); |
74fa61b7 | 963 | |
4407d3d8 MK |
964 | if (fstat(fd, &sb) == \-1) /* To obtain file size */ |
965 | handle_error("fstat"); | |
74fa61b7 MK |
966 | |
967 | offset = atoi(argv[2]); | |
af2d18b2 | 968 | pa_offset = offset & \(ti(sysconf(_SC_PAGE_SIZE) \- 1); |
74fa61b7 MK |
969 | /* offset for mmap() must be page aligned */ |
970 | ||
971 | if (offset >= sb.st_size) { | |
d1a71985 | 972 | fprintf(stderr, "offset is past end of file\en"); |
74fa61b7 MK |
973 | exit(EXIT_FAILURE); |
974 | } | |
975 | ||
976 | if (argc == 4) { | |
977 | length = atoi(argv[3]); | |
978 | if (offset + length > sb.st_size) | |
979 | length = sb.st_size \- offset; | |
f81fb444 | 980 | /* Can\(aqt display bytes past end of file */ |
5b6adad1 | 981 | |
74fa61b7 MK |
982 | } else { /* No length arg ==> display to end of file */ |
983 | length = sb.st_size \- offset; | |
984 | } | |
985 | ||
986 | addr = mmap(NULL, length + offset \- pa_offset, PROT_READ, | |
987 | MAP_PRIVATE, fd, pa_offset); | |
4407d3d8 MK |
988 | if (addr == MAP_FAILED) |
989 | handle_error("mmap"); | |
74fa61b7 MK |
990 | |
991 | s = write(STDOUT_FILENO, addr + offset \- pa_offset, length); | |
992 | if (s != length) { | |
993 | if (s == \-1) | |
4407d3d8 MK |
994 | handle_error("write"); |
995 | ||
996 | fprintf(stderr, "partial write"); | |
74fa61b7 MK |
997 | exit(EXIT_FAILURE); |
998 | } | |
999 | ||
40142309 MK |
1000 | munmap(addr, length + offset \- pa_offset); |
1001 | close(fd); | |
1002 | ||
74fa61b7 | 1003 | exit(EXIT_SUCCESS); |
c54ed37e | 1004 | } |
e7d0bb47 | 1005 | .EE |
33857069 | 1006 | .\" SRC END |
47297adb | 1007 | .SH SEE ALSO |
74f25490 | 1008 | .BR ftruncate (2), |
fea681da | 1009 | .BR getpagesize (2), |
c4d76cd9 | 1010 | .BR memfd_create (2), |
f75c3a3b | 1011 | .BR mincore (2), |
fea681da MK |
1012 | .BR mlock (2), |
1013 | .BR mmap2 (2), | |
54504ac3 | 1014 | .BR mprotect (2), |
fea681da MK |
1015 | .BR mremap (2), |
1016 | .BR msync (2), | |
931e4e25 | 1017 | .BR remap_file_pages (2), |
83cd3686 | 1018 | .BR setrlimit (2), |
7921f13b | 1019 | .BR shmat (2), |
13acca70 | 1020 | .BR userfaultfd (2), |
f93af9c6 MK |
1021 | .BR shm_open (3), |
1022 | .BR shm_overview (7) | |
efeece04 | 1023 | .PP |
0bf14b87 MK |
1024 | The descriptions of the following files in |
1025 | .BR proc (5): | |
1026 | .IR /proc/[pid]/maps , | |
1027 | .IR /proc/[pid]/map_files , | |
1028 | and | |
1029 | .IR /proc/[pid]/smaps . | |
efeece04 | 1030 | .PP |
d2fdb1e3 | 1031 | B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128\(en129 and 389\(en391. |
fea681da MK |
1032 | .\" |
1033 | .\" Repeat after me: private read-only mappings are 100% equivalent to | |
1034 | .\" shared read-only mappings. No ifs, buts, or maybes. -- Linus |