]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/mmap.2
ffix
[thirdparty/man-pages.git] / man2 / mmap.2
CommitLineData
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
41mmap, 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 54creates a new mapping in the virtual address space of
5e8cde2f
MK
55the calling process.
56The starting address for the new mapping is specified in
57.IR start .
58The
59.I length
60argument specifies the length of the mapping.
61
62If
63.I start
64is NULL,
65then the kernel chooses the address at which to create the mapping;
66this is the most portable method of creating a new mapping.
c13182ef
MK
67If
68.I start
5e8cde2f
MK
69is not NULL,
70then the kernel takes it as a hint about where to place the mapping;
71on Linux, the mapping will be created at the next higher page boundary.
72The address of the new mapping is returned as the result of the call.
73
74The contents of a file mapping (as opposed to an anonymous mapping; see
75.B MAP_ANONYMOUS
d9bfdb9c 76below), are initialized using
fea681da
MK
77.I length
78bytes starting at offset
79.I offset
5e8cde2f
MK
80in the file (or other object) referred to by the file descriptor
81.IR fd .
a62f5121
MK
82.I offset
83must be a multiple of the page size as returned by
84.IR sysconf(_SC_PAGE_SIZE) .
fea681da
MK
85.LP
86The
87.I prot
c13182ef 88argument describes the desired memory protection of the mapping
5e8cde2f
MK
89(and must not conflict with the open mode of the file).
90It is either
fea681da 91.B PROT_NONE
a62f5121 92or the bitwise OR of one or more of the following flags:
fea681da
MK
93.TP 1.1i
94.B PROT_EXEC
95Pages may be executed.
96.TP
97.B PROT_READ
98Pages may be read.
99.TP
100.B PROT_WRITE
101Pages may be written.
102.TP
103.B PROT_NONE
104Pages may not be accessed.
105.LP
106The
107.I flags
a62f5121
MK
108argument determines whether updates to the mapping
109are visible to other processes mapping the same region,
110and whether updates are caried through to the underlying file.
d9bfdb9c 111This behavior is determined by including exactly one
a62f5121 112of the following values in
5e8cde2f 113.IR flags :
fea681da 114.TP 1.1i
fea681da 115.B MAP_SHARED
c13182ef 116Share this mapping.
a62f5121
MK
117Updates to the mapping are visible to other processes that map this file,
118and are carried through to the underlying file.
fea681da
MK
119The file may not actually be updated until
120.BR msync (2)
121or
122.BR munmap (2)
a62f5121 123is called.
fea681da
MK
124.TP
125.B MAP_PRIVATE
126Create a private copy-on-write mapping.
a62f5121
MK
127Updates to the mapping are not visible to other processes
128mapping the same file, and are not carried through to
129the underlying file.
fea681da 130It is unspecified whether changes made to the file after the
1a956089 131.BR mmap ()
fea681da
MK
132call are visible in the mapped region.
133.LP
a62f5121
MK
134Both of these flags are described in POSIX.1-2001.
135
136In addition, zero or more of the following values can be ORed in
137.IR flags :
fea681da 138.TP
83314009
MK
139.B MAP_32BIT
140Put the mapping into the first 2GB of the process address space.
141Ignored when
a62f5121 142.B MAP_FIXED
83314009 143is set.
d4725a0e 144This flag is currently only supported on x86-64 for 64-bit programs.
fea681da 145.TP
a62f5121 146.B MAP_ANON
c13182ef
MK
147Synonym for
148.BR MAP_ANONYMOUS .
a62f5121
MK
149Deprecated.
150.TP
fea681da 151.B MAP_ANONYMOUS
5e8cde2f 152The mapping is not backed by any file;
d9bfdb9c 153its contents are initialized to zero.
5e8cde2f 154The
fea681da
MK
155.I fd
156and
157.I offset
a62f5121 158arguments are ignored;
c13182ef 159however, some implementations require
a62f5121 160.I fd
c13182ef
MK
161to be \-1 if
162.B MAP_ANONYMOUS
163(or
164.BR MAP_ANON )
a62f5121
MK
165is specified,
166and portable applications should ensure this.
c13182ef 167The use of
a62f5121 168.B MAP_ANONYMOUS
c13182ef 169in conjunction with
51ffcca0 170.B MAP_SHARED
74f3f90b 171is only supported on Linux since kernel 2.4.
fea681da 172.TP
83314009
MK
173.B MAP_DENYWRITE
174This 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
177should fail with
178.BR ETXTBUSY .
179But this was a source of denial-of-service attacks.)
180.TP
181.B MAP_EXECUTABLE
182This 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
190Compatibility flag.
191Ignored.
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
196Don't interpret
197.I start
198as a hint: place the mapping at exactly that address.
199.I start
200must be a multiple of the page size.
201If the memory region specified by
202.I start
203and
204.I len
205overlaps pages of any existing mapping(s), then the overlapped
206part of the existing mapping(s) will be discarded.
207If the specified address cannot be used,
208.BR mmap ()
209will fail.
210Because requiring a fixed address for a mapping is less portable,
211the use of this option is discouraged.
fea681da 212.TP
83314009
MK
213.B MAP_GROWSDOWN
214Used for stacks.
215Indicates to the kernel virtual memory system that the mapping
216should extend downwards in memory.
217.TP
218.BR MAP_LOCKED " (since Linux 2.5.37)"
219Lock the pages of the mapped region into memory in the manner of
74d32233 220.BR mlock (2).
83314009
MK
221This 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
225Only meaningful in conjunction with
226.BR MAP_POPULATE .
c13182ef
MK
227Don't perform read-ahead:
228only create page tables entries for pages
51ffcca0 229that are already present in RAM.
83314009
MK
230.TP
231.B MAP_NORESERVE
232Do not reserve swap space for this mapping.
233When swap space is reserved, one has the guarantee
234that it is possible to modify the mapping.
8bd58774
MK
235When swap space is not reserved one might get
236.B SIGSEGV
237upon a write
83314009
MK
238if no physical memory is available.
239See also the discussion of the file
240.I /proc/sys/vm/overcommit_memory
241in
242.BR proc (5).
243In kernels before 2.6, this flag only had effect for
244private writable mappings.
245.TP
246.BR MAP_POPULATE " (since Linux 2.5.46)"
247Populate (prefault) page tables for a file mapping,
248by performing read-ahead on the file.
249Later accesses to the mapping will not be blocked by page faults.
fea681da 250.LP
a62f5121
MK
251Of the above flags, only
252.B MAP_FIXED
253is specified in POSIX.1-2001.
254However, most systems also support
255.B MAP_ANONYMOUS
c13182ef 256(or its synonym
a62f5121
MK
257.BR MAP_ANON ).
258.LP
fea681da
MK
259Some systems document the additional flags MAP_AUTOGROW, MAP_AUTORESRV,
260MAP_COPY, and MAP_LOCAL.
261.LP
fea681da 262Memory mapped by
1a956089 263.BR mmap ()
fea681da
MK
264is preserved across
265.BR fork (2),
266with the same attributes.
267.LP
c13182ef
MK
268A file is mapped in multiples of the page size.
269For a file that is not
fea681da 270a multiple of the page size, the remaining memory is zeroed when mapped,
c13182ef
MK
271and writes to that region are not written out to the file.
272The effect of
fea681da
MK
273changing the size of the underlying file of a mapping on the pages that
274correspond to added or removed regions of the file is unspecified.
275
276The
1a956089 277.BR munmap ()
fea681da
MK
278system call deletes the mappings for the specified address range, and
279causes further references to addresses within the range to generate
c13182ef
MK
280invalid memory references.
281The region is also automatically unmapped
282when the process is terminated.
283On the other hand, closing the file
fea681da
MK
284descriptor does not unmap the region.
285.LP
286The address
287.I start
c13182ef
MK
288must be a multiple of the page size.
289All pages containing a part
fea681da 290of the indicated range are unmapped, and subsequent references
8bd58774
MK
291to these pages will generate
292.BR SIGSEGV .
c13182ef 293It is not an error if the
fea681da
MK
294indicated range does not contain any mapped pages.
295
296For file-backed mappings, the
8478ee02 297.I st_atime
fea681da 298field for the mapped file may be updated at any time between the
1a956089 299.BR mmap ()
fea681da
MK
300and the corresponding unmapping; the first reference to a mapped
301page will update the field if it has not been already.
302.LP
303The
8478ee02 304.I st_ctime
fea681da 305and
8478ee02 306.I st_mtime
c13182ef
MK
307field for a file mapped with
308.B PROT_WRITE
309and
310.B MAP_SHARED
51ffcca0 311will be updated after
fea681da 312a write to the mapped region, and before a subsequent
0bfa087b 313.BR msync (2)
c13182ef
MK
314with the
315.B MS_SYNC
316or
317.BR MS_ASYNC
51ffcca0 318flag, if one occurs.
fea681da
MK
319.SH "RETURN VALUE"
320On success,
1a956089 321.BR mmap ()
fea681da
MK
322returns a pointer to the mapped area.
323On error, the value
324.B MAP_FAILED
c13182ef 325(that is,
5e8cde2f
MK
326.I "(void *) \-1)"
327is returned, and
fea681da
MK
328.I errno
329is set appropriately.
330On success,
1a956089 331.BR munmap ()
fea681da
MK
332returns 0, on failure \-1, and
333.I errno
c13182ef 334is set (probably to
51ffcca0 335.BR EINVAL ).
fea681da
MK
336.SH ERRORS
337.TP
338.B EACCES
339A file descriptor refers to a non-regular file.
c13182ef
MK
340Or
341.B MAP_PRIVATE
51ffcca0 342was requested, but
fea681da
MK
343.I fd
344is not open for reading.
c13182ef
MK
345Or
346.B MAP_SHARED
347was requested and
348.B PROT_WRITE
51ffcca0 349is set, but
fea681da 350.I fd
682edefb
MK
351is not open in read/write
352.RB ( O_RDWR )
353mode.
c13182ef
MK
354Or
355.B PROT_WRITE
51ffcca0 356is set, but the file is append-only.
fea681da
MK
357.TP
358.B EAGAIN
83cd3686
MK
359The 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 364is not a valid file descriptor (and
51ffcca0
MK
365.B MAP_ANONYMOUS
366was not set).
fea681da
MK
367.TP
368.B EINVAL
369We don't like
62a04c81
MK
370.IR start ,
371.IR length ,
fea681da 372or
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
379was 0.
380.TP
381.B EINVAL
382.I flags
383contained neither
384.B MAP_PRIVATE
fea681da 385or
62a04c81
MK
386.BR MAP_SHARED ,
387or 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()
392The 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
398The underlying filesystem of the specified file does not support
399memory mapping.
400.TP
401.B ENOMEM
402No memory is available, or the process's maximum number of mappings would
403have been exceeded.
404.TP
405.B EPERM
406The
407.I prot
408argument asks for
409.B PROT_EXEC
410but the mapped area belongs to a file on a filesystem that
411was mounted no-exec.
412.\" (Since 2.4.25 / 2.6.0.)
413.TP
414.B ETXTBSY
c13182ef 415.B MAP_DENYWRITE
51ffcca0 416was set but the object specified by
fea681da
MK
417.I fd
418is open for writing.
419.LP
420Use of a mapped region can result in these signals:
421.TP
422.B SIGSEGV
1e321034 423Attempted write into a region mapped as read-only.
fea681da
MK
424.TP
425.B SIGBUS
426Attempted access to a portion of the buffer that does not correspond
427to the file (for example, beyond the end of the file, including the
428case where another process has truncated the file).
2b2581ee
MK
429.SH "CONFORMING TO"
430SVr4, 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
434On POSIX systems on which
1a956089 435.BR mmap (),
0bfa087b 436.BR msync (2)
fea681da 437and
1a956089 438.BR munmap ()
fea681da
MK
439are available,
440.B _POSIX_MAPPED_FILES
c84371c6 441is 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
447Since kernel 2.4, this system call has been superseded by
448.BR mmap2 (2).
449Nowadays,
450.\" Since around glibc 2.1/2.2, depending on the platform.
451the glibc
452.BR mmap ()
453wrapper function invokes
454.BR mmap2 (2)
455with a suitably adjusted value for
456.IR offset .
457
f3edaabb
MK
458On some hardware architectures (e.g., x86),
459.BR PROT_WRITE
460implies
461.BR PROT_READ .
a1d5f77c
MK
462It is architecture dependent whether
463.B PROT_READ
464implies
465.B PROT_EXEC
466or not.
467Portable programs should always set
468.B PROT_EXEC
469if they intend to execute code in the new mapping.
fea681da 470.SH BUGS
c13182ef
MK
471On Linux there are no guarantees like those suggested above under
472.BR MAP_NORESERVE .
dbc53ca8 473By default, any process can be killed
fea681da 474at any moment when the system runs out of memory.
dbc53ca8
MK
475
476In kernels before 2.6.7, the
477.B MAP_POPULATE
478flag only has effect if
479.I prot
480is specified as
481.BR PROT_NONE .
c8f3e580 482
c13182ef 483SUSv3 specifies that
c8f3e580
MK
484.BR mmap ()
485should fail if
486.I length
487is 0.
488However, in kernels before 2.6.12,
489.BR mmap ()
490succeeded in this case: no mapping was created and the call returned
491.IR start .
492Since kernel 2.6.12,
493.BR mmap ()
494fails with the error
495.B EINVAL
496for this case.
74fa61b7
MK
497.SH EXAMPLE
498.PP
499The following program prints part of the file specified in
500its first command-line argument to standard output.
501The range of bytes to be printed is specified via offset and length
502values in the second and third command-line arguments.
503The program creates a memory mapping of the required
504pages of the file and then uses
505.BR write (2)
506to 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
516int
517main(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
591B.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