]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Hey Emacs! This file is -*- nroff -*- source. |
2 | .\" | |
a62f5121 | 3 | .\" Copyright (C) 1996 Andries Brouwer <aeb@cwi.nl> |
74fa61b7 | 4 | .\" and Copyright (C) 2006, 2007 Michael Kerrisk <mtk-manpages@gmx.net> |
fea681da MK |
5 | .\" |
6 | .\" Permission is granted to make and distribute verbatim copies of this | |
7 | .\" manual provided the copyright notice and this permission notice are | |
8 | .\" preserved on all copies. | |
9 | .\" | |
10 | .\" Permission is granted to copy and distribute modified versions of this | |
11 | .\" manual under the conditions for verbatim copying, provided that the | |
12 | .\" entire resulting derived work is distributed under the terms of a | |
13 | .\" permission notice identical to this one. | |
c13182ef | 14 | .\" |
fea681da MK |
15 | .\" Since the Linux kernel and libraries are constantly changing, this |
16 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
17 | .\" responsibility for errors or omissions, or for damages resulting from | |
18 | .\" the use of the information contained herein. The author(s) may not | |
19 | .\" have taken the same level of care in the production of this manual, | |
20 | .\" which is licensed free of charge, as they might when working | |
21 | .\" professionally. | |
c13182ef | 22 | .\" |
fea681da MK |
23 | .\" Formatted or processed versions of this manual, if unaccompanied by |
24 | .\" the source, must acknowledge the copyright and authors of this work. | |
25 | .\" | |
26 | .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com> | |
27 | .\" Modified 2000-03-25 by Jim Van Zandt <jrv@vanzandt.mv.com> | |
28 | .\" Modified 2001-10-04 by John Levon <moz@compsoc.man.ac.uk> | |
29 | .\" Modified 2003-02-02 by Andi Kleen <ak@muc.de> | |
305a0578 | 30 | .\" Modified 2003-05-21 by Michael Kerrisk <mtk-manpages@gmx.net> |
fea681da | 31 | .\" MAP_LOCKED works from 2.5.37 |
305a0578 | 32 | .\" Modified 2004-06-17 by Michael Kerrisk <mtk-manpages@gmx.net> |
fea681da | 33 | .\" Modified 2004-09-11 by aeb |
1a956089 MK |
34 | .\" Modified 2004-12-08, from Eric Estievenart <eric.estievenart@free.fr> |
35 | .\" Modified 2004-12-08, mtk, formatting tidy-ups | |
a62f5121 | 36 | .\" Modified 2006-12-04, mtk, various parts rewritten |
74fa61b7 | 37 | .\" 2007-07-10, mtk, Added an example program. |
fea681da | 38 | .\" |
74fa61b7 | 39 | .TH MMAP 2 2007-07-10 "Linux" "Linux Programmer's Manual" |
fea681da MK |
40 | .SH NAME |
41 | mmap, munmap \- map or unmap files or devices into memory | |
42 | .SH SYNOPSIS | |
e0037472 | 43 | .nf |
fea681da MK |
44 | .B #include <sys/mman.h> |
45 | .sp | |
a62f5121 MK |
46 | .BI "void *mmap(void *" start ", size_t " length \ |
47 | ", int " prot ", int " flags , | |
e0037472 | 48 | .BI " int " fd ", off_t " offset ); |
fea681da MK |
49 | .sp |
50 | .BI "int munmap(void *" start ", size_t " length ); | |
e0037472 | 51 | .fi |
fea681da | 52 | .SH DESCRIPTION |
1a956089 | 53 | .BR mmap () |
c13182ef | 54 | creates a new mapping in the virtual address space of |
5e8cde2f MK |
55 | the calling process. |
56 | The starting address for the new mapping is specified in | |
57 | .IR start . | |
58 | The | |
59 | .I length | |
60 | argument specifies the length of the mapping. | |
61 | ||
62 | If | |
63 | .I start | |
64 | is NULL, | |
65 | then the kernel chooses the address at which to create the mapping; | |
66 | this is the most portable method of creating a new mapping. | |
c13182ef MK |
67 | If |
68 | .I start | |
5e8cde2f MK |
69 | is not NULL, |
70 | then the kernel takes it as a hint about where to place the mapping; | |
71 | on Linux, the mapping will be created at the next higher page boundary. | |
72 | The address of the new mapping is returned as the result of the call. | |
73 | ||
74 | The contents of a file mapping (as opposed to an anonymous mapping; see | |
75 | .B MAP_ANONYMOUS | |
d9bfdb9c | 76 | below), are initialized using |
fea681da MK |
77 | .I length |
78 | bytes starting at offset | |
79 | .I offset | |
5e8cde2f MK |
80 | in the file (or other object) referred to by the file descriptor |
81 | .IR fd . | |
a62f5121 MK |
82 | .I offset |
83 | must be a multiple of the page size as returned by | |
84 | .IR sysconf(_SC_PAGE_SIZE) . | |
fea681da MK |
85 | .LP |
86 | The | |
87 | .I prot | |
c13182ef | 88 | argument describes the desired memory protection of the mapping |
5e8cde2f MK |
89 | (and must not conflict with the open mode of the file). |
90 | It is either | |
fea681da | 91 | .B PROT_NONE |
a62f5121 | 92 | or the bitwise OR of one or more of the following flags: |
fea681da MK |
93 | .TP 1.1i |
94 | .B PROT_EXEC | |
95 | Pages may be executed. | |
96 | .TP | |
97 | .B PROT_READ | |
98 | Pages may be read. | |
99 | .TP | |
100 | .B PROT_WRITE | |
101 | Pages may be written. | |
102 | .TP | |
103 | .B PROT_NONE | |
104 | Pages may not be accessed. | |
105 | .LP | |
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, | |
110 | and whether updates are caried 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 : |
fea681da | 114 | .TP 1.1i |
fea681da | 115 | .B MAP_SHARED |
c13182ef | 116 | Share this mapping. |
a62f5121 MK |
117 | Updates to the mapping are visible to other processes that map this file, |
118 | and are carried through to the underlying file. | |
fea681da MK |
119 | The file may not actually be updated until |
120 | .BR msync (2) | |
121 | or | |
122 | .BR munmap (2) | |
a62f5121 | 123 | is called. |
fea681da MK |
124 | .TP |
125 | .B MAP_PRIVATE | |
126 | Create a private copy-on-write mapping. | |
a62f5121 MK |
127 | Updates to the mapping are not visible to other processes |
128 | mapping the same file, and are not carried through to | |
129 | the underlying file. | |
fea681da | 130 | It is unspecified whether changes made to the file after the |
1a956089 | 131 | .BR mmap () |
fea681da MK |
132 | call are visible in the mapped region. |
133 | .LP | |
a62f5121 MK |
134 | Both of these flags are described in POSIX.1-2001. |
135 | ||
136 | In addition, zero or more of the following values can be ORed in | |
137 | .IR flags : | |
fea681da | 138 | .TP |
83314009 MK |
139 | .B MAP_32BIT |
140 | Put the mapping into the first 2GB of the process address space. | |
141 | Ignored when | |
a62f5121 | 142 | .B MAP_FIXED |
83314009 | 143 | is set. |
d4725a0e | 144 | This flag is currently only supported on x86-64 for 64-bit programs. |
fea681da | 145 | .TP |
a62f5121 | 146 | .B MAP_ANON |
c13182ef MK |
147 | Synonym for |
148 | .BR MAP_ANONYMOUS . | |
a62f5121 MK |
149 | Deprecated. |
150 | .TP | |
fea681da | 151 | .B MAP_ANONYMOUS |
5e8cde2f | 152 | The mapping is not backed by any file; |
d9bfdb9c | 153 | its contents are initialized to zero. |
5e8cde2f | 154 | The |
fea681da MK |
155 | .I fd |
156 | and | |
157 | .I offset | |
a62f5121 | 158 | arguments are ignored; |
c13182ef | 159 | however, some implementations require |
a62f5121 | 160 | .I fd |
c13182ef MK |
161 | to be \-1 if |
162 | .B MAP_ANONYMOUS | |
163 | (or | |
164 | .BR MAP_ANON ) | |
a62f5121 MK |
165 | is specified, |
166 | and portable applications should ensure this. | |
c13182ef | 167 | The use of |
a62f5121 | 168 | .B MAP_ANONYMOUS |
c13182ef | 169 | in conjunction with |
51ffcca0 | 170 | .B MAP_SHARED |
74f3f90b | 171 | is only supported on Linux since kernel 2.4. |
fea681da | 172 | .TP |
83314009 MK |
173 | .B MAP_DENYWRITE |
174 | This flag is ignored. | |
175 | .\" Introduced in 1.1.36, removed in 1.3.24. | |
d9bfdb9c | 176 | (Long ago, it signaled that attempts to write to the underlying file |
83314009 MK |
177 | should fail with |
178 | .BR ETXTBUSY . | |
179 | But this was a source of denial-of-service attacks.) | |
180 | .TP | |
181 | .B MAP_EXECUTABLE | |
182 | This flag is ignored. | |
183 | .\" Introduced in 1.1.38, removed in 1.3.24. Flag tested in proc_follow_link. | |
d9bfdb9c | 184 | .\" (Long ago, it signaled that the underlying file is an executable. |
83314009 MK |
185 | .\" However, that information was not really used anywhere.) |
186 | .\" Linus talked about DOS related to MAP_EXECUTABLE, but he was thinking of | |
187 | .\" MAP_DENYWRITE? | |
188 | .TP | |
fea681da | 189 | .B MAP_FILE |
c13182ef MK |
190 | Compatibility flag. |
191 | Ignored. | |
988db661 | 192 | .\" On some systems, this was required as the opposite of |
83314009 | 193 | .\" MAP_ANONYMOUS -- mtk, 1 May 2007 |
fea681da | 194 | .TP |
51ffcca0 | 195 | .B MAP_FIXED |
83314009 MK |
196 | Don't interpret |
197 | .I start | |
198 | as a hint: place the mapping at exactly that address. | |
199 | .I start | |
200 | must be a multiple of the page size. | |
201 | If the memory region specified by | |
202 | .I start | |
203 | and | |
204 | .I len | |
205 | overlaps pages of any existing mapping(s), then the overlapped | |
206 | part of the existing mapping(s) will be discarded. | |
207 | If the specified address cannot be used, | |
208 | .BR mmap () | |
209 | will fail. | |
210 | Because requiring a fixed address for a mapping is less portable, | |
211 | the use of this option is discouraged. | |
fea681da | 212 | .TP |
83314009 MK |
213 | .B MAP_GROWSDOWN |
214 | Used for stacks. | |
215 | Indicates to the kernel virtual memory system that the mapping | |
216 | should extend downwards in memory. | |
217 | .TP | |
218 | .BR MAP_LOCKED " (since Linux 2.5.37)" | |
219 | Lock the pages of the mapped region into memory in the manner of | |
74d32233 | 220 | .BR mlock (2). |
83314009 MK |
221 | This flag is ignored in older kernels. |
222 | .\" If set, the mapped pages will not be swapped out. | |
fea681da MK |
223 | .TP |
224 | .BR MAP_NONBLOCK " (since Linux 2.5.46)" | |
51ffcca0 MK |
225 | Only meaningful in conjunction with |
226 | .BR MAP_POPULATE . | |
c13182ef MK |
227 | Don't perform read-ahead: |
228 | only create page tables entries for pages | |
51ffcca0 | 229 | that are already present in RAM. |
83314009 MK |
230 | .TP |
231 | .B MAP_NORESERVE | |
232 | Do not reserve swap space for this mapping. | |
233 | When swap space is reserved, one has the guarantee | |
234 | that it is possible to modify the mapping. | |
8bd58774 MK |
235 | When swap space is not reserved one might get |
236 | .B SIGSEGV | |
237 | upon a write | |
83314009 MK |
238 | if no physical memory is available. |
239 | See also the discussion of the file | |
240 | .I /proc/sys/vm/overcommit_memory | |
241 | in | |
242 | .BR proc (5). | |
243 | In kernels before 2.6, this flag only had effect for | |
244 | private writable mappings. | |
245 | .TP | |
246 | .BR MAP_POPULATE " (since Linux 2.5.46)" | |
247 | Populate (prefault) page tables for a file mapping, | |
248 | by performing read-ahead on the file. | |
249 | Later accesses to the mapping will not be blocked by page faults. | |
fea681da | 250 | .LP |
a62f5121 MK |
251 | Of the above flags, only |
252 | .B MAP_FIXED | |
253 | is specified in POSIX.1-2001. | |
254 | However, most systems also support | |
255 | .B MAP_ANONYMOUS | |
c13182ef | 256 | (or its synonym |
a62f5121 MK |
257 | .BR MAP_ANON ). |
258 | .LP | |
fea681da MK |
259 | Some systems document the additional flags MAP_AUTOGROW, MAP_AUTORESRV, |
260 | MAP_COPY, and MAP_LOCAL. | |
261 | .LP | |
fea681da | 262 | Memory mapped by |
1a956089 | 263 | .BR mmap () |
fea681da MK |
264 | is preserved across |
265 | .BR fork (2), | |
266 | with the same attributes. | |
267 | .LP | |
c13182ef MK |
268 | A file is mapped in multiples of the page size. |
269 | For a file that is not | |
fea681da | 270 | a multiple of the page size, the remaining memory is zeroed when mapped, |
c13182ef MK |
271 | and writes to that region are not written out to the file. |
272 | The effect of | |
fea681da MK |
273 | changing the size of the underlying file of a mapping on the pages that |
274 | correspond to added or removed regions of the file is unspecified. | |
275 | ||
276 | The | |
1a956089 | 277 | .BR munmap () |
fea681da MK |
278 | system call deletes the mappings for the specified address range, and |
279 | causes further references to addresses within the range to generate | |
c13182ef MK |
280 | invalid memory references. |
281 | The region is also automatically unmapped | |
282 | when the process is terminated. | |
283 | On the other hand, closing the file | |
fea681da MK |
284 | descriptor does not unmap the region. |
285 | .LP | |
286 | The address | |
287 | .I start | |
c13182ef MK |
288 | must be a multiple of the page size. |
289 | All pages containing a part | |
fea681da | 290 | of the indicated range are unmapped, and subsequent references |
8bd58774 MK |
291 | to these pages will generate |
292 | .BR SIGSEGV . | |
c13182ef | 293 | It is not an error if the |
fea681da MK |
294 | indicated range does not contain any mapped pages. |
295 | ||
296 | For file-backed mappings, the | |
8478ee02 | 297 | .I st_atime |
fea681da | 298 | field for the mapped file may be updated at any time between the |
1a956089 | 299 | .BR mmap () |
fea681da MK |
300 | and the corresponding unmapping; the first reference to a mapped |
301 | page will update the field if it has not been already. | |
302 | .LP | |
303 | The | |
8478ee02 | 304 | .I st_ctime |
fea681da | 305 | and |
8478ee02 | 306 | .I st_mtime |
c13182ef MK |
307 | field for a file mapped with |
308 | .B PROT_WRITE | |
309 | and | |
310 | .B MAP_SHARED | |
51ffcca0 | 311 | will be updated after |
fea681da | 312 | a write to the mapped region, and before a subsequent |
0bfa087b | 313 | .BR msync (2) |
c13182ef MK |
314 | with the |
315 | .B MS_SYNC | |
316 | or | |
317 | .BR MS_ASYNC | |
51ffcca0 | 318 | flag, if one occurs. |
fea681da MK |
319 | .SH "RETURN VALUE" |
320 | On success, | |
1a956089 | 321 | .BR mmap () |
fea681da MK |
322 | returns a pointer to the mapped area. |
323 | On error, the value | |
324 | .B MAP_FAILED | |
c13182ef | 325 | (that is, |
5e8cde2f MK |
326 | .I "(void *) \-1)" |
327 | is returned, and | |
fea681da MK |
328 | .I errno |
329 | is set appropriately. | |
330 | On success, | |
1a956089 | 331 | .BR munmap () |
fea681da MK |
332 | returns 0, on failure \-1, and |
333 | .I errno | |
c13182ef | 334 | is set (probably to |
51ffcca0 | 335 | .BR EINVAL ). |
fea681da MK |
336 | .SH ERRORS |
337 | .TP | |
338 | .B EACCES | |
339 | A file descriptor refers to a non-regular file. | |
c13182ef MK |
340 | Or |
341 | .B MAP_PRIVATE | |
51ffcca0 | 342 | was requested, but |
fea681da MK |
343 | .I fd |
344 | is not open for reading. | |
c13182ef MK |
345 | Or |
346 | .B MAP_SHARED | |
347 | was requested and | |
348 | .B PROT_WRITE | |
51ffcca0 | 349 | is set, but |
fea681da | 350 | .I fd |
682edefb MK |
351 | is not open in read/write |
352 | .RB ( O_RDWR ) | |
353 | mode. | |
c13182ef MK |
354 | Or |
355 | .B PROT_WRITE | |
51ffcca0 | 356 | is set, but the file is append-only. |
fea681da MK |
357 | .TP |
358 | .B EAGAIN | |
83cd3686 MK |
359 | The file has been locked, or too much memory has been locked (see |
360 | .BR setrlimit (2)). | |
fea681da MK |
361 | .TP |
362 | .B EBADF | |
363 | .I fd | |
c13182ef | 364 | is not a valid file descriptor (and |
51ffcca0 MK |
365 | .B MAP_ANONYMOUS |
366 | was not set). | |
fea681da MK |
367 | .TP |
368 | .B EINVAL | |
369 | We don't like | |
62a04c81 MK |
370 | .IR start , |
371 | .IR length , | |
fea681da | 372 | or |
62a04c81 MK |
373 | .IR offset |
374 | (e.g., they are too large, or not aligned on a page boundary). | |
375 | .TP | |
376 | .B EINVAL | |
377 | (since Linux 2.6.12), | |
fea681da | 378 | .I length |
62a04c81 MK |
379 | was 0. |
380 | .TP | |
381 | .B EINVAL | |
382 | .I flags | |
383 | contained neither | |
384 | .B MAP_PRIVATE | |
fea681da | 385 | or |
62a04c81 MK |
386 | .BR MAP_SHARED , |
387 | or contained both of these values. | |
fea681da MK |
388 | .TP |
389 | .B ENFILE | |
390 | .\" This is for shared anonymous segments | |
391 | .\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp() | |
392 | The system limit on the total number of open files has been reached. | |
393 | .\" .TP | |
394 | .\" .B ENOEXEC | |
395 | .\" A file could not be mapped for reading. | |
396 | .TP | |
397 | .B ENODEV | |
398 | The underlying filesystem of the specified file does not support | |
399 | memory mapping. | |
400 | .TP | |
401 | .B ENOMEM | |
402 | No memory is available, or the process's maximum number of mappings would | |
403 | have been exceeded. | |
404 | .TP | |
405 | .B EPERM | |
406 | The | |
407 | .I prot | |
408 | argument asks for | |
409 | .B PROT_EXEC | |
410 | but the mapped area belongs to a file on a filesystem that | |
411 | was mounted no-exec. | |
412 | .\" (Since 2.4.25 / 2.6.0.) | |
413 | .TP | |
414 | .B ETXTBSY | |
c13182ef | 415 | .B MAP_DENYWRITE |
51ffcca0 | 416 | was set but the object specified by |
fea681da MK |
417 | .I fd |
418 | is open for writing. | |
419 | .LP | |
420 | Use of a mapped region can result in these signals: | |
421 | .TP | |
422 | .B SIGSEGV | |
1e321034 | 423 | Attempted write into a region mapped as read-only. |
fea681da MK |
424 | .TP |
425 | .B SIGBUS | |
426 | Attempted access to a portion of the buffer that does not correspond | |
427 | to the file (for example, beyond the end of the file, including the | |
428 | case where another process has truncated the file). | |
2b2581ee MK |
429 | .SH "CONFORMING TO" |
430 | SVr4, 4.4BSD, POSIX.1-2001. | |
431 | .\" SVr4 documents additional error codes ENXIO and ENODEV. | |
432 | .\" SUSv2 documents additional error codes EMFILE and EOVERFLOW. | |
fea681da MK |
433 | .SH AVAILABILITY |
434 | On POSIX systems on which | |
1a956089 | 435 | .BR mmap (), |
0bfa087b | 436 | .BR msync (2) |
fea681da | 437 | and |
1a956089 | 438 | .BR munmap () |
fea681da MK |
439 | are available, |
440 | .B _POSIX_MAPPED_FILES | |
c84371c6 | 441 | is defined in \fI<unistd.h>\fP to a value greater than 0. (See also |
fea681da | 442 | .BR sysconf (3).) |
97c1eac8 | 443 | .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L. |
fea681da MK |
444 | .\" -1: unavailable, 0: ask using sysconf(). |
445 | .\" glibc defines it to 1. | |
a1d5f77c | 446 | .SH NOTES |
5636a29a MK |
447 | Since kernel 2.4, this system call has been superseded by |
448 | .BR mmap2 (2). | |
449 | Nowadays, | |
450 | .\" Since around glibc 2.1/2.2, depending on the platform. | |
451 | the glibc | |
452 | .BR mmap () | |
453 | wrapper function invokes | |
454 | .BR mmap2 (2) | |
455 | with a suitably adjusted value for | |
456 | .IR offset . | |
457 | ||
f3edaabb MK |
458 | On some hardware architectures (e.g., x86), |
459 | .BR PROT_WRITE | |
460 | implies | |
461 | .BR PROT_READ . | |
a1d5f77c MK |
462 | It is architecture dependent whether |
463 | .B PROT_READ | |
464 | implies | |
465 | .B PROT_EXEC | |
466 | or not. | |
467 | Portable programs should always set | |
468 | .B PROT_EXEC | |
469 | if they intend to execute code in the new mapping. | |
fea681da | 470 | .SH BUGS |
c13182ef MK |
471 | On Linux there are no guarantees like those suggested above under |
472 | .BR MAP_NORESERVE . | |
dbc53ca8 | 473 | By default, any process can be killed |
fea681da | 474 | at any moment when the system runs out of memory. |
dbc53ca8 MK |
475 | |
476 | In kernels before 2.6.7, the | |
477 | .B MAP_POPULATE | |
478 | flag only has effect if | |
479 | .I prot | |
480 | is specified as | |
481 | .BR PROT_NONE . | |
c8f3e580 | 482 | |
c13182ef | 483 | SUSv3 specifies that |
c8f3e580 MK |
484 | .BR mmap () |
485 | should fail if | |
486 | .I length | |
487 | is 0. | |
488 | However, in kernels before 2.6.12, | |
489 | .BR mmap () | |
490 | succeeded in this case: no mapping was created and the call returned | |
491 | .IR start . | |
492 | Since kernel 2.6.12, | |
493 | .BR mmap () | |
494 | fails with the error | |
495 | .B EINVAL | |
496 | for this case. | |
74fa61b7 MK |
497 | .SH EXAMPLE |
498 | .PP | |
499 | The following program prints part of the file specified in | |
500 | its first command-line argument to standard output. | |
501 | The range of bytes to be printed is specified via offset and length | |
502 | values in the second and third command-line arguments. | |
503 | The program creates a memory mapping of the required | |
504 | pages of the file and then uses | |
505 | .BR write (2) | |
506 | to output the desired bytes. | |
507 | .nf | |
508 | ||
509 | #include <sys/mman.h> | |
510 | #include <sys/stat.h> | |
511 | #include <fcntl.h> | |
512 | #include <stdio.h> | |
513 | #include <stdlib.h> | |
514 | #include <unistd.h> | |
515 | ||
516 | int | |
517 | main(int argc, char *argv[]) | |
518 | { | |
519 | char *addr; | |
520 | int fd; | |
521 | struct stat sb; | |
522 | off_t offset, pa_offset; | |
523 | size_t length; | |
524 | ssize_t s; | |
525 | ||
526 | if (argc < 3 || argc > 4) == 0) { | |
527 | fprintf(stderr, "%s file offset [length]\\n", argv[0]); | |
528 | exit(EXIT_FAILURE); | |
529 | } | |
530 | ||
531 | fd = open(argv[1], O_RDONLY); | |
532 | if (fd == \-1) { | |
533 | perror("open"); | |
534 | exit(EXIT_FAILURE); | |
535 | } | |
536 | ||
537 | if (fstat(fd, &sb) == \-1) { /* To obtain file size */ | |
538 | perror("fstat"); | |
539 | exit(EXIT_FAILURE); | |
540 | } | |
541 | ||
542 | offset = atoi(argv[2]); | |
543 | pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1); | |
544 | /* offset for mmap() must be page aligned */ | |
545 | ||
546 | if (offset >= sb.st_size) { | |
547 | fprintf(stderr, "offset is past end of file\\n"); | |
548 | exit(EXIT_FAILURE); | |
549 | } | |
550 | ||
551 | if (argc == 4) { | |
552 | length = atoi(argv[3]); | |
553 | if (offset + length > sb.st_size) | |
554 | length = sb.st_size \- offset; | |
555 | /* Can't display bytes past end of file */ | |
5b6adad1 | 556 | |
74fa61b7 MK |
557 | } else { /* No length arg ==> display to end of file */ |
558 | length = sb.st_size \- offset; | |
559 | } | |
560 | ||
561 | addr = mmap(NULL, length + offset \- pa_offset, PROT_READ, | |
562 | MAP_PRIVATE, fd, pa_offset); | |
563 | if (addr == MAP_FAILED) { | |
564 | perror("mmap"); | |
565 | exit(EXIT_FAILURE); | |
566 | } | |
567 | ||
568 | s = write(STDOUT_FILENO, addr + offset \- pa_offset, length); | |
569 | if (s != length) { | |
570 | if (s == \-1) | |
571 | perror("write"); | |
572 | else | |
573 | fprintf(stderr, "partial write"); | |
574 | exit(EXIT_FAILURE); | |
575 | } | |
576 | ||
577 | exit(EXIT_SUCCESS); | |
578 | } /* main */ | |
579 | .fi | |
fea681da MK |
580 | .SH "SEE ALSO" |
581 | .BR getpagesize (2), | |
f75c3a3b | 582 | .BR mincore (2), |
fea681da MK |
583 | .BR mlock (2), |
584 | .BR mmap2 (2), | |
585 | .BR mremap (2), | |
586 | .BR msync (2), | |
931e4e25 | 587 | .BR remap_file_pages (2), |
83cd3686 | 588 | .BR setrlimit (2), |
424a8950 | 589 | .BR shm_open (3) |
fea681da MK |
590 | .br |
591 | B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391. | |
592 | .\" | |
593 | .\" Repeat after me: private read-only mappings are 100% equivalent to | |
594 | .\" shared read-only mappings. No ifs, buts, or maybes. -- Linus |