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