Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/mmap-util.c @ 43:526284657da7 HEAD
Fail if we try to mmap() files larger than 2G fully to memory
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 27 Aug 2002 05:27:05 +0300 |
parents | 622e22c0486f |
children | 4a7ab9e94f25 |
rev | line source |
---|---|
0 | 1 /* |
2 mmap-util.c - Memory mapping utilities | |
3 | |
4 Copyright (c) 2002 Timo Sirainen | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining | |
7 a copy of this software and associated documentation files (the | |
8 "Software"), to deal in the Software without restriction, including | |
9 without limitation the rights to use, copy, modify, merge, publish, | |
10 distribute, sublicense, and/or sell copies of the Software, and to | |
11 permit persons to whom the Software is furnished to do so, subject to | |
12 the following conditions: | |
13 | |
14 The above copyright notice and this permission notice shall be | |
15 included in all copies or substantial portions of the Software. | |
16 | |
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 */ | |
25 | |
26 #include "lib.h" | |
27 #include "mmap-util.h" | |
28 | |
29 static void *mmap_file(int fd, size_t *length, int access) | |
30 { | |
43
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
31 off_t size; |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
32 |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
33 size = lseek(fd, 0, SEEK_END); |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
34 if (size == -1) |
0 | 35 return MAP_FAILED; |
36 | |
43
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
37 if (size > INT_MAX) { |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
38 /* too large file to map into memory */ |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
39 errno = EFBIG; |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
40 return MAP_FAILED; |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
41 } |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
42 |
526284657da7
Fail if we try to mmap() files larger than 2G fully to memory
Timo Sirainen <tss@iki.fi>
parents:
6
diff
changeset
|
43 *length = (size_t)size; |
0 | 44 if (*length == 0) |
45 return NULL; | |
46 | |
47 i_assert(*length > 0 && *length < INT_MAX); | |
48 | |
49 return mmap(NULL, *length, access, MAP_SHARED, fd, 0); | |
50 } | |
51 | |
52 void *mmap_ro_file(int fd, size_t *length) | |
53 { | |
54 return mmap_file(fd, length, PROT_READ); | |
55 } | |
56 | |
57 void *mmap_rw_file(int fd, size_t *length) | |
58 { | |
59 return mmap_file(fd, length, PROT_READ | PROT_WRITE); | |
60 } | |
61 | |
62 void *mmap_aligned(int fd, int access, off_t offset, size_t length, | |
63 void **data_start, size_t *mmap_length) | |
64 { | |
65 void *mmap_base; | |
66 static int pagemask = 0; | |
67 | |
68 if (pagemask == 0) { | |
6 | 69 pagemask = getpagesize(); |
0 | 70 i_assert(pagemask > 0); |
71 pagemask--; | |
72 } | |
73 | |
74 *mmap_length = length + (offset & pagemask); | |
75 | |
76 mmap_base = mmap(NULL, *mmap_length, access, MAP_SHARED, | |
77 fd, offset & ~pagemask); | |
78 *data_start = mmap_base == MAP_FAILED || mmap_base == NULL ? NULL : | |
79 (char *) mmap_base + (offset & pagemask); | |
80 | |
81 return mmap_base; | |
82 } | |
83 | |
84 #ifndef HAVE_MADVISE | |
85 int madvise(void *start, size_t length, int advice) | |
86 { | |
87 } | |
88 #endif |