]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/mmap.2
_exit.2, dup.2, execve.2, execveat.2, fallocate.2, fcntl.2, get_robust_list.2, getrli...
[thirdparty/man-pages.git] / man2 / mmap.2
CommitLineData
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.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
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>
c11b1abf 30.\" Modified 2003-05-21 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 31.\" MAP_LOCKED works from 2.5.37
c11b1abf 32.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
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.
e6205b0c 38.\" 2008-11-18, mtk, document MAP_STACK
fea681da 39.\"
8392a3b3 40.TH MMAP 2 2015-01-22 "Linux" "Linux Programmer's Manual"
fea681da
MK
41.SH NAME
42mmap, munmap \- map or unmap files or devices into memory
43.SH SYNOPSIS
e0037472 44.nf
fea681da
MK
45.B #include <sys/mman.h>
46.sp
14f5ae6d 47.BI "void *mmap(void *" addr ", size_t " length \
a62f5121 48", int " prot ", int " flags ,
e0037472 49.BI " int " fd ", off_t " offset );
14f5ae6d 50.BI "int munmap(void *" addr ", size_t " length );
e0037472 51.fi
45e97e2a
MK
52
53See NOTES for information on feature test macro requirements.
fea681da 54.SH DESCRIPTION
1a956089 55.BR mmap ()
c13182ef 56creates a new mapping in the virtual address space of
5e8cde2f
MK
57the calling process.
58The starting address for the new mapping is specified in
14f5ae6d 59.IR addr .
5e8cde2f
MK
60The
61.I length
62argument specifies the length of the mapping.
63
64If
14f5ae6d 65.I addr
5e8cde2f
MK
66is NULL,
67then the kernel chooses the address at which to create the mapping;
68this is the most portable method of creating a new mapping.
c13182ef 69If
14f5ae6d 70.I addr
5e8cde2f
MK
71is not NULL,
72then the kernel takes it as a hint about where to place the mapping;
6aa7db0a
MK
73on Linux, the mapping will be created at a nearby page boundary.
74.\" Before Linux 2.6.24, the address was rounded up to the next page
29328361 75.\" boundary; since 2.6.24, it is rounded down!
5e8cde2f
MK
76The address of the new mapping is returned as the result of the call.
77
78The contents of a file mapping (as opposed to an anonymous mapping; see
79.B MAP_ANONYMOUS
d9bfdb9c 80below), are initialized using
fea681da
MK
81.I length
82bytes starting at offset
83.I offset
5e8cde2f
MK
84in the file (or other object) referred to by the file descriptor
85.IR fd .
a62f5121
MK
86.I offset
87must be a multiple of the page size as returned by
88.IR sysconf(_SC_PAGE_SIZE) .
fea681da
MK
89.LP
90The
91.I prot
c13182ef 92argument describes the desired memory protection of the mapping
5e8cde2f
MK
93(and must not conflict with the open mode of the file).
94It is either
fea681da 95.B PROT_NONE
a62f5121 96or the bitwise OR of one or more of the following flags:
fea681da
MK
97.TP 1.1i
98.B PROT_EXEC
99Pages may be executed.
100.TP
101.B PROT_READ
102Pages may be read.
103.TP
104.B PROT_WRITE
105Pages may be written.
106.TP
107.B PROT_NONE
108Pages may not be accessed.
109.LP
110The
111.I flags
a62f5121
MK
112argument determines whether updates to the mapping
113are visible to other processes mapping the same region,
ba7cb080 114and whether updates are carried through to the underlying file.
d9bfdb9c 115This behavior is determined by including exactly one
a62f5121 116of the following values in
5e8cde2f 117.IR flags :
fea681da 118.TP 1.1i
fea681da 119.B MAP_SHARED
c13182ef 120Share this mapping.
a62f5121
MK
121Updates to the mapping are visible to other processes that map this file,
122and are carried through to the underlying file.
fea681da
MK
123The file may not actually be updated until
124.BR msync (2)
125or
2777b1ca 126.BR munmap ()
a62f5121 127is called.
fea681da
MK
128.TP
129.B MAP_PRIVATE
130Create a private copy-on-write mapping.
a62f5121
MK
131Updates to the mapping are not visible to other processes
132mapping the same file, and are not carried through to
133the underlying file.
fea681da 134It is unspecified whether changes made to the file after the
1a956089 135.BR mmap ()
fea681da
MK
136call are visible in the mapped region.
137.LP
a62f5121
MK
138Both of these flags are described in POSIX.1-2001.
139
140In addition, zero or more of the following values can be ORed in
141.IR flags :
fea681da 142.TP
c368e7ca
MK
143.BR MAP_32BIT " (since Linux 2.4.20, 2.6)"
144Put the mapping into the first 2 Gigabytes of the process address space.
33a0ccb2 145This flag is supported only on x86-64, for 64-bit programs.
c368e7ca
MK
146It was added to allow thread stacks to be allocated somewhere
147in the first 2GB of memory,
148so as to improve context-switch performance on some early
14964-bit processors.
150.\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08
151Modern x86-64 processors no longer have this performance problem,
152so use of this flag is not required on those systems.
153The
83314009 154.B MAP_32BIT
c368e7ca 155flag is ignored when
a62f5121 156.B MAP_FIXED
83314009 157is set.
fea681da 158.TP
a62f5121 159.B MAP_ANON
c13182ef
MK
160Synonym for
161.BR MAP_ANONYMOUS .
a62f5121
MK
162Deprecated.
163.TP
fea681da 164.B MAP_ANONYMOUS
5e8cde2f 165The mapping is not backed by any file;
d9bfdb9c 166its contents are initialized to zero.
5e8cde2f 167The
fea681da
MK
168.I fd
169and
170.I offset
a62f5121 171arguments are ignored;
c13182ef 172however, some implementations require
a62f5121 173.I fd
c13182ef
MK
174to be \-1 if
175.B MAP_ANONYMOUS
176(or
177.BR MAP_ANON )
a62f5121
MK
178is specified,
179and portable applications should ensure this.
c13182ef 180The use of
a62f5121 181.B MAP_ANONYMOUS
c13182ef 182in conjunction with
51ffcca0 183.B MAP_SHARED
33a0ccb2 184is supported on Linux only since kernel 2.4.
fea681da 185.TP
83314009
MK
186.B MAP_DENYWRITE
187This flag is ignored.
188.\" Introduced in 1.1.36, removed in 1.3.24.
d9bfdb9c 189(Long ago, it signaled that attempts to write to the underlying file
83314009
MK
190should fail with
191.BR ETXTBUSY .
192But this was a source of denial-of-service attacks.)
193.TP
194.B MAP_EXECUTABLE
195This flag is ignored.
196.\" Introduced in 1.1.38, removed in 1.3.24. Flag tested in proc_follow_link.
d9bfdb9c 197.\" (Long ago, it signaled that the underlying file is an executable.
83314009
MK
198.\" However, that information was not really used anywhere.)
199.\" Linus talked about DOS related to MAP_EXECUTABLE, but he was thinking of
200.\" MAP_DENYWRITE?
201.TP
fea681da 202.B MAP_FILE
c13182ef
MK
203Compatibility flag.
204Ignored.
988db661 205.\" On some systems, this was required as the opposite of
83314009 206.\" MAP_ANONYMOUS -- mtk, 1 May 2007
fea681da 207.TP
51ffcca0 208.B MAP_FIXED
83314009 209Don't interpret
14f5ae6d 210.I addr
83314009 211as a hint: place the mapping at exactly that address.
14f5ae6d 212.I addr
83314009
MK
213must be a multiple of the page size.
214If the memory region specified by
14f5ae6d 215.I addr
83314009
MK
216and
217.I len
218overlaps pages of any existing mapping(s), then the overlapped
219part of the existing mapping(s) will be discarded.
220If the specified address cannot be used,
221.BR mmap ()
222will fail.
223Because requiring a fixed address for a mapping is less portable,
224the use of this option is discouraged.
fea681da 225.TP
83314009
MK
226.B MAP_GROWSDOWN
227Used for stacks.
228Indicates to the kernel virtual memory system that the mapping
5fab2e7c 229should extend downward in memory.
83314009 230.TP
76a34baa
MK
231.BR MAP_HUGETLB " (since Linux 2.6.32)"
232Allocate the mapping using "huge pages."
66a9882e 233See the Linux kernel source file
76a34baa
MK
234.I Documentation/vm/hugetlbpage.txt
235for further information.
236.TP
83314009
MK
237.BR MAP_LOCKED " (since Linux 2.5.37)"
238Lock the pages of the mapped region into memory in the manner of
74d32233 239.BR mlock (2).
83314009
MK
240This flag is ignored in older kernels.
241.\" If set, the mapped pages will not be swapped out.
fea681da
MK
242.TP
243.BR MAP_NONBLOCK " (since Linux 2.5.46)"
51ffcca0
MK
244Only meaningful in conjunction with
245.BR MAP_POPULATE .
c13182ef 246Don't perform read-ahead:
33a0ccb2 247create page tables entries only for pages
51ffcca0 248that are already present in RAM.
7c40de08 249Since Linux 2.6.23, this flag causes
f38fa944
MK
250.BR MAP_POPULATE
251to do nothing.
252One day the combination of
253.BR MAP_POPULATE
254and
255.BR MAP_NONBLOCK
3b777aff 256may be reimplemented.
83314009
MK
257.TP
258.B MAP_NORESERVE
259Do not reserve swap space for this mapping.
260When swap space is reserved, one has the guarantee
261that it is possible to modify the mapping.
8bd58774
MK
262When swap space is not reserved one might get
263.B SIGSEGV
264upon a write
83314009
MK
265if no physical memory is available.
266See also the discussion of the file
267.I /proc/sys/vm/overcommit_memory
268in
269.BR proc (5).
33a0ccb2 270In kernels before 2.6, this flag had effect only for
83314009
MK
271private writable mappings.
272.TP
273.BR MAP_POPULATE " (since Linux 2.5.46)"
f38fa944
MK
274Populate (prefault) page tables for a mapping.
275For a file mapping, this causes read-ahead on the file.
83314009 276Later accesses to the mapping will not be blocked by page faults.
f38fa944 277.BR MAP_POPULATE
33a0ccb2 278is supported for private mappings only since Linux 2.6.23.
e6205b0c
MK
279.TP
280.BR MAP_STACK " (since Linux 2.6.27)"
281Allocate the mapping at an address suitable for a process
282or thread stack.
283This flag is currently a no-op,
284but is used in the glibc threading implementation so that
285if some architectures require special treatment for stack allocations,
286support can later be transparently implemented for glibc.
67b59ff5 287.\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08
e6205b0c
MK
288.\" commit cd98a04a59e2f94fa64d5bf1e26498d27427d5e7
289.\" http://thread.gmane.org/gmane.linux.kernel/720412
290.\" "pthread_create() slow for many threads; also time to revisit 64b
291.\" context switch optimization?"
12062404
MK
292.TP
293.BR MAP_UNINITIALIZED " (since Linux 2.6.33)"
294Don't clear anonymous pages.
295This flag is intended to improve performance on embedded devices.
33a0ccb2 296This flag is honored only if the kernel was configured with the
12062404
MK
297.B CONFIG_MMAP_ALLOW_UNINITIALIZED
298option.
299Because of the security implications,
300that option is normally enabled only on embedded devices
301(i.e., devices where one has complete control of the contents of user memory).
a62f5121 302.LP
7c7adcbe
MK
303Of the above flags, only
304.B MAP_FIXED
305is specified in POSIX.1-2001.
306However, most systems also support
307.B MAP_ANONYMOUS
308(or its synonym
309.BR MAP_ANON ).
310.LP
2d6cfc1a
MK
311Some systems document the additional flags
312.BR MAP_AUTOGROW ,
313.BR MAP_AUTORESRV ,
314.BR MAP_COPY ,
315and
316.BR MAP_LOCAL .
fea681da 317.LP
fea681da 318Memory mapped by
1a956089 319.BR mmap ()
fea681da
MK
320is preserved across
321.BR fork (2),
322with the same attributes.
323.LP
c13182ef
MK
324A file is mapped in multiples of the page size.
325For a file that is not
fea681da 326a multiple of the page size, the remaining memory is zeroed when mapped,
c13182ef
MK
327and writes to that region are not written out to the file.
328The effect of
fea681da
MK
329changing the size of the underlying file of a mapping on the pages that
330correspond to added or removed regions of the file is unspecified.
de5f7e28 331.SS munmap()
fea681da 332The
1a956089 333.BR munmap ()
fea681da
MK
334system call deletes the mappings for the specified address range, and
335causes further references to addresses within the range to generate
c13182ef
MK
336invalid memory references.
337The region is also automatically unmapped
338when the process is terminated.
339On the other hand, closing the file
fea681da
MK
340descriptor does not unmap the region.
341.LP
342The address
14f5ae6d 343.I addr
c13182ef
MK
344must be a multiple of the page size.
345All pages containing a part
fea681da 346of the indicated range are unmapped, and subsequent references
8bd58774
MK
347to these pages will generate
348.BR SIGSEGV .
c13182ef 349It is not an error if the
fea681da 350indicated range does not contain any mapped pages.
de5f7e28 351.SS Timestamps changes for file-backed mappings
fea681da 352For file-backed mappings, the
8478ee02 353.I st_atime
fea681da 354field for the mapped file may be updated at any time between the
1a956089 355.BR mmap ()
fea681da
MK
356and the corresponding unmapping; the first reference to a mapped
357page will update the field if it has not been already.
358.LP
359The
8478ee02 360.I st_ctime
fea681da 361and
8478ee02 362.I st_mtime
c13182ef
MK
363field for a file mapped with
364.B PROT_WRITE
365and
366.B MAP_SHARED
51ffcca0 367will be updated after
fea681da 368a write to the mapped region, and before a subsequent
0bfa087b 369.BR msync (2)
c13182ef
MK
370with the
371.B MS_SYNC
372or
0daa9e92 373.B MS_ASYNC
51ffcca0 374flag, if one occurs.
47297adb 375.SH RETURN VALUE
fea681da 376On success,
1a956089 377.BR mmap ()
fea681da
MK
378returns a pointer to the mapped area.
379On error, the value
380.B MAP_FAILED
c13182ef 381(that is,
009df872 382.IR "(void\ *)\ \-1" )
5e8cde2f 383is returned, and
fea681da
MK
384.I errno
385is set appropriately.
386On success,
1a956089 387.BR munmap ()
fea681da
MK
388returns 0, on failure \-1, and
389.I errno
c13182ef 390is set (probably to
51ffcca0 391.BR EINVAL ).
fea681da
MK
392.SH ERRORS
393.TP
394.B EACCES
395A file descriptor refers to a non-regular file.
5e7c71f6 396Or a file mapping was requested, but
fea681da
MK
397.I fd
398is not open for reading.
c13182ef
MK
399Or
400.B MAP_SHARED
401was requested and
402.B PROT_WRITE
51ffcca0 403is set, but
fea681da 404.I fd
682edefb
MK
405is not open in read/write
406.RB ( O_RDWR )
407mode.
c13182ef
MK
408Or
409.B PROT_WRITE
51ffcca0 410is set, but the file is append-only.
fea681da
MK
411.TP
412.B EAGAIN
83cd3686
MK
413The file has been locked, or too much memory has been locked (see
414.BR setrlimit (2)).
fea681da
MK
415.TP
416.B EBADF
417.I fd
c13182ef 418is not a valid file descriptor (and
51ffcca0
MK
419.B MAP_ANONYMOUS
420was not set).
fea681da
MK
421.TP
422.B EINVAL
423We don't like
14f5ae6d 424.IR addr ,
62a04c81 425.IR length ,
fea681da 426or
0daa9e92 427.I offset
62a04c81
MK
428(e.g., they are too large, or not aligned on a page boundary).
429.TP
430.B EINVAL
f99fc197 431(since Linux 2.6.12)
fea681da 432.I length
62a04c81
MK
433was 0.
434.TP
435.B EINVAL
436.I flags
437contained neither
438.B MAP_PRIVATE
fea681da 439or
62a04c81
MK
440.BR MAP_SHARED ,
441or contained both of these values.
fea681da
MK
442.TP
443.B ENFILE
444.\" This is for shared anonymous segments
445.\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
446The system limit on the total number of open files has been reached.
447.\" .TP
448.\" .B ENOEXEC
449.\" A file could not be mapped for reading.
450.TP
451.B ENODEV
9ee4a2b6 452The underlying filesystem of the specified file does not support
fea681da
MK
453memory mapping.
454.TP
455.B ENOMEM
456No memory is available, or the process's maximum number of mappings would
457have been exceeded.
458.TP
459.B EPERM
460The
461.I prot
462argument asks for
463.B PROT_EXEC
9ee4a2b6 464but the mapped area belongs to a file on a filesystem that
fea681da
MK
465was mounted no-exec.
466.\" (Since 2.4.25 / 2.6.0.)
467.TP
fbab10e5
MK
468.B EPERM
469The operation was prevented by a file seal; see
470.BR fcntl (2).
471.TP
fea681da 472.B ETXTBSY
c13182ef 473.B MAP_DENYWRITE
51ffcca0 474was set but the object specified by
fea681da
MK
475.I fd
476is open for writing.
da3ce098
CH
477.TP
478.B EOVERFLOW
42b437ca
MK
479On 32-bit architecture together with the large file extension
480(i.e., using 64-bit
481.IR off_t ):
482the number of pages used for
483.I length
484plus number of pages used for
485.I offset
486would overflow
487.I "unsigned long"
488(32 bits).
fea681da
MK
489.LP
490Use of a mapped region can result in these signals:
491.TP
492.B SIGSEGV
1e321034 493Attempted write into a region mapped as read-only.
fea681da
MK
494.TP
495.B SIGBUS
496Attempted access to a portion of the buffer that does not correspond
497to the file (for example, beyond the end of the file, including the
498case where another process has truncated the file).
47297adb 499.SH CONFORMING TO
2b2581ee
MK
500SVr4, 4.4BSD, POSIX.1-2001.
501.\" SVr4 documents additional error codes ENXIO and ENODEV.
502.\" SUSv2 documents additional error codes EMFILE and EOVERFLOW.
fea681da
MK
503.SH AVAILABILITY
504On POSIX systems on which
1a956089 505.BR mmap (),
9af134cd 506.BR msync (2),
fea681da 507and
1a956089 508.BR munmap ()
fea681da
MK
509are available,
510.B _POSIX_MAPPED_FILES
6387216b
MK
511is defined in \fI<unistd.h>\fP to a value greater than 0.
512(See also
fea681da 513.BR sysconf (3).)
97c1eac8 514.\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
fea681da
MK
515.\" -1: unavailable, 0: ask using sysconf().
516.\" glibc defines it to 1.
a1d5f77c 517.SH NOTES
34ccb744 518On some hardware architectures (e.g., i386),
0daa9e92 519.B PROT_WRITE
f3edaabb
MK
520implies
521.BR PROT_READ .
a1d5f77c
MK
522It is architecture dependent whether
523.B PROT_READ
524implies
525.B PROT_EXEC
526or not.
527Portable programs should always set
528.B PROT_EXEC
529if they intend to execute code in the new mapping.
80d17cfa
MK
530
531The portable way to create a mapping is to specify
532.I addr
533as 0 (NULL), and omit
534.B MAP_FIXED
535from
536.IR flags .
537In this case, the system chooses the address for the mapping;
46cdb997 538the address is chosen so as not to conflict with any existing mapping,
80d17cfa
MK
539and will not be 0.
540If the
541.B MAP_FIXED
542flag is specified, and
543.I addr
491cd2f0 544is 0 (NULL), then the mapped address will be 0 (NULL).
45e97e2a
MK
545
546Certain
547.I flags
548constants are defined only if either
549.BR _BSD_SOURCE
550or
551.BR _SVID_SOURCE
552is defined.
553(Requiring
554.BR _GNU_SOURCE
555also suffices,
556and requiring that macro specifically would have been more logical,
76c637e1 557since these flags are all Linux-specific.)
45e97e2a
MK
558The relevant flags are:
559.BR MAP_32BIT ,
560.BR MAP_ANONYMOUS
561(and the synonym
562.BR MAP_ANON ),
563.BR MAP_DENYWRITE ,
564.BR MAP_EXECUTABLE ,
565.BR MAP_FILE ,
566.BR MAP_GROWSDOWN ,
567.BR MAP_HUGETLB ,
568.BR MAP_LOCKED ,
569.BR MAP_NONBLOCK ,
570.BR MAP_NORESERVE ,
571.BR MAP_POPULATE ,
572and
573.BR MAP_STACK .
35c189fb
MK
574.\"
575.SS C library/kernel ABI differences
576This page describes the interface provided by the glibc
577.BR mmap ()
578wrapper function.
579Originally, this function invoked a system call of the same name.
580Since kernel 2.4, that system call has been superseded by
581.BR mmap2 (2),
582and nowadays
583.\" Since around glibc 2.1/2.2, depending on the platform.
584the glibc
585.BR mmap ()
586wrapper function invokes
587.BR mmap2 (2)
588with a suitably adjusted value for
589.IR offset .
fea681da 590.SH BUGS
c13182ef
MK
591On Linux there are no guarantees like those suggested above under
592.BR MAP_NORESERVE .
dbc53ca8 593By default, any process can be killed
fea681da 594at any moment when the system runs out of memory.
dbc53ca8
MK
595
596In kernels before 2.6.7, the
597.B MAP_POPULATE
33a0ccb2 598flag has effect only if
dbc53ca8
MK
599.I prot
600is specified as
601.BR PROT_NONE .
c8f3e580 602
c13182ef 603SUSv3 specifies that
c8f3e580
MK
604.BR mmap ()
605should fail if
606.I length
607is 0.
608However, in kernels before 2.6.12,
609.BR mmap ()
610succeeded in this case: no mapping was created and the call returned
14f5ae6d 611.IR addr .
c8f3e580
MK
612Since kernel 2.6.12,
613.BR mmap ()
614fails with the error
615.B EINVAL
616for this case.
2e43522f 617
a780f17b
MK
618POSIX specifies that the system shall always
619zero fill any partial page at the end
b072a788 620of the object and that system will never write any modification of the
a780f17b
MK
621object beyond its end.
622On Linux, when you write data to such partial page after the end
b072a788 623of the object, the data stays in the page cache even after the file
a780f17b
MK
624is closed and unmapped
625and even though the data is never written to the file itself,
626subsequent mappings may see the modified content.
627In some cases, this could be fixed by calling
628.BR msync (2)
629before the unmap takes place;
630however, this doesn't work on tmpfs
631(for example, when using POSIX shared memory interface documented in
632.BR shm_overview (7)).
74fa61b7 633.SH EXAMPLE
2e001ad4
MK
634.\" FIXME . Add an example here that uses an anonymous shared region for
635.\" IPC between parent and child.
74fa61b7
MK
636.PP
637The following program prints part of the file specified in
638its first command-line argument to standard output.
639The range of bytes to be printed is specified via offset and length
640values in the second and third command-line arguments.
641The program creates a memory mapping of the required
642pages of the file and then uses
643.BR write (2)
644to output the desired bytes.
f30b7415 645.SS Program source
74fa61b7 646.nf
74fa61b7
MK
647#include <sys/mman.h>
648#include <sys/stat.h>
649#include <fcntl.h>
650#include <stdio.h>
651#include <stdlib.h>
652#include <unistd.h>
653
4407d3d8
MK
654#define handle_error(msg) \\
655 do { perror(msg); exit(EXIT_FAILURE); } while (0)
656
74fa61b7
MK
657int
658main(int argc, char *argv[])
659{
660 char *addr;
661 int fd;
662 struct stat sb;
663 off_t offset, pa_offset;
664 size_t length;
665 ssize_t s;
666
fbbfa7ce 667 if (argc < 3 || argc > 4) {
74fa61b7
MK
668 fprintf(stderr, "%s file offset [length]\\n", argv[0]);
669 exit(EXIT_FAILURE);
670 }
671
672 fd = open(argv[1], O_RDONLY);
4407d3d8 673 if (fd == \-1)
8568021d 674 handle_error("open");
74fa61b7 675
4407d3d8
MK
676 if (fstat(fd, &sb) == \-1) /* To obtain file size */
677 handle_error("fstat");
74fa61b7
MK
678
679 offset = atoi(argv[2]);
680 pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1);
681 /* offset for mmap() must be page aligned */
682
683 if (offset >= sb.st_size) {
684 fprintf(stderr, "offset is past end of file\\n");
685 exit(EXIT_FAILURE);
686 }
687
688 if (argc == 4) {
689 length = atoi(argv[3]);
690 if (offset + length > sb.st_size)
691 length = sb.st_size \- offset;
f81fb444 692 /* Can\(aqt display bytes past end of file */
5b6adad1 693
74fa61b7
MK
694 } else { /* No length arg ==> display to end of file */
695 length = sb.st_size \- offset;
696 }
697
698 addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
699 MAP_PRIVATE, fd, pa_offset);
4407d3d8
MK
700 if (addr == MAP_FAILED)
701 handle_error("mmap");
74fa61b7
MK
702
703 s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
704 if (s != length) {
705 if (s == \-1)
4407d3d8
MK
706 handle_error("write");
707
708 fprintf(stderr, "partial write");
74fa61b7
MK
709 exit(EXIT_FAILURE);
710 }
711
712 exit(EXIT_SUCCESS);
c54ed37e 713}
74fa61b7 714.fi
47297adb 715.SH SEE ALSO
fea681da 716.BR getpagesize (2),
c4d76cd9 717.BR memfd_create (2),
f75c3a3b 718.BR mincore (2),
fea681da
MK
719.BR mlock (2),
720.BR mmap2 (2),
54504ac3 721.BR mprotect (2),
fea681da
MK
722.BR mremap (2),
723.BR msync (2),
931e4e25 724.BR remap_file_pages (2),
83cd3686 725.BR setrlimit (2),
7921f13b 726.BR shmat (2),
f93af9c6
MK
727.BR shm_open (3),
728.BR shm_overview (7)
173fe7e7 729
0bf14b87
MK
730The descriptions of the following files in
731.BR proc (5):
732.IR /proc/[pid]/maps ,
733.IR /proc/[pid]/map_files ,
734and
735.IR /proc/[pid]/smaps .
736
fea681da
MK
737B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391.
738.\"
739.\" Repeat after me: private read-only mappings are 100% equivalent to
740.\" shared read-only mappings. No ifs, buts, or maybes. -- Linus