The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * System utilities. |
| 5 | */ |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 6 | #include <assert.h> |
| 7 | #include <errno.h> |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 8 | #include <fcntl.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 9 | #include <limits.h> |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 10 | #include <stdbool.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/mman.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include "SysUtil.h" |
| 22 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 23 | static bool sysMapFD(int fd, MemMapping* pMap) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | assert(pMap != NULL); |
| 25 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 26 | struct stat sb; |
| 27 | if (fstat(fd, &sb) == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 28 | PLOG(ERROR) << "fstat(" << fd << ") failed"; |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 29 | return false; |
| 30 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 32 | void* memPtr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | if (memPtr == MAP_FAILED) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 34 | PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed"; |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 35 | return false; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 38 | pMap->addr = reinterpret_cast<unsigned char*>(memPtr); |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 39 | pMap->length = sb.st_size; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 40 | pMap->range_count = 1; |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 41 | pMap->ranges = reinterpret_cast<MappedRange*>(malloc(sizeof(MappedRange))); |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 42 | if (pMap->ranges == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 43 | PLOG(ERROR) << "malloc failed"; |
Yabin Cui | 0d28ba4 | 2016-02-22 22:12:27 -0800 | [diff] [blame] | 44 | munmap(memPtr, sb.st_size); |
| 45 | return false; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 46 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 47 | pMap->ranges[0].addr = memPtr; |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 48 | pMap->ranges[0].length = sb.st_size; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 50 | return true; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 53 | static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) |
| 54 | { |
| 55 | char block_dev[PATH_MAX+1]; |
| 56 | size_t size; |
| 57 | unsigned int blksize; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 58 | size_t blocks; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 59 | unsigned int range_count; |
| 60 | unsigned int i; |
| 61 | |
| 62 | if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 63 | PLOG(ERROR) << "failed to read block device from header"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | for (i = 0; i < sizeof(block_dev); ++i) { |
| 67 | if (block_dev[i] == '\n') { |
| 68 | block_dev[i] = 0; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
Mark Salyzyn | 76b245c | 2014-03-17 15:35:52 -0700 | [diff] [blame] | 73 | if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 74 | LOG(ERROR) << "failed to parse block map header"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 75 | return -1; |
| 76 | } |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 77 | if (blksize != 0) { |
| 78 | blocks = ((size-1) / blksize) + 1; |
| 79 | } |
| 80 | if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 81 | LOG(ERROR) << "invalid data in block map file: size " << size << ", blksize " << blksize |
| 82 | << ", range_count " << range_count; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 83 | return -1; |
| 84 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 85 | |
| 86 | pMap->range_count = range_count; |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 87 | pMap->ranges = reinterpret_cast<MappedRange*>(calloc(range_count, sizeof(MappedRange))); |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 88 | if (pMap->ranges == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 89 | PLOG(ERROR) << "calloc(" << range_count << ", " << sizeof(MappedRange) << ") failed"; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 90 | return -1; |
| 91 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 92 | |
| 93 | // Reserve enough contiguous address space for the whole file. |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 94 | unsigned char* reserve = reinterpret_cast<unsigned char*>(mmap64(NULL, blocks * blksize, |
| 95 | PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0)); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 96 | if (reserve == MAP_FAILED) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 97 | PLOG(ERROR) << "failed to reserve address space"; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 98 | free(pMap->ranges); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 102 | int fd = open(block_dev, O_RDONLY); |
| 103 | if (fd < 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 104 | PLOG(ERROR) << "failed to open block device " << block_dev; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 105 | munmap(reserve, blocks * blksize); |
| 106 | free(pMap->ranges); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | unsigned char* next = reserve; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 111 | size_t remaining_size = blocks * blksize; |
| 112 | bool success = true; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 113 | for (i = 0; i < range_count; ++i) { |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 114 | size_t start, end; |
| 115 | if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 116 | LOG(ERROR) << "failed to parse range " << i << " in block map"; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 117 | success = false; |
| 118 | break; |
| 119 | } |
| 120 | size_t length = (end - start) * blksize; |
| 121 | if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 122 | LOG(ERROR) << "unexpected range in block map: " << start << " " << end; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 123 | success = false; |
| 124 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 127 | void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 128 | if (addr == MAP_FAILED) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 129 | PLOG(ERROR) << "failed to map block " << i; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 130 | success = false; |
| 131 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 132 | } |
| 133 | pMap->ranges[i].addr = addr; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 134 | pMap->ranges[i].length = length; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 135 | |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 136 | next += length; |
| 137 | remaining_size -= length; |
| 138 | } |
| 139 | if (success && remaining_size != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 140 | LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 141 | success = false; |
| 142 | } |
| 143 | if (!success) { |
| 144 | close(fd); |
| 145 | munmap(reserve, blocks * blksize); |
| 146 | free(pMap->ranges); |
| 147 | return -1; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 150 | close(fd); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 151 | pMap->addr = reserve; |
| 152 | pMap->length = size; |
| 153 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 154 | LOG(INFO) << "mmapped " << range_count << " ranges"; |
Doug Zongker | 19a8e24 | 2014-01-21 09:25:41 -0800 | [diff] [blame] | 155 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int sysMapFile(const char* fn, MemMapping* pMap) |
| 160 | { |
| 161 | memset(pMap, 0, sizeof(*pMap)); |
| 162 | |
| 163 | if (fn && fn[0] == '@') { |
| 164 | // A map of blocks |
| 165 | FILE* mapf = fopen(fn+1, "r"); |
| 166 | if (mapf == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 167 | PLOG(ERROR) << "Unable to open '" << (fn+1) << "'"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | if (sysMapBlockFile(mapf, pMap) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 172 | LOG(ERROR) << "Map of '" << fn << "' failed"; |
Yabin Cui | 4f2df16 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 173 | fclose(mapf); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | fclose(mapf); |
| 178 | } else { |
| 179 | // This is a regular file. |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 180 | int fd = open(fn, O_RDONLY); |
| 181 | if (fd == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 182 | PLOG(ERROR) << "Unable to open '" << fn << "'"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 183 | return -1; |
| 184 | } |
| 185 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 186 | if (!sysMapFD(fd, pMap)) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 187 | LOG(ERROR) << "Map of '" << fn << "' failed"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 188 | close(fd); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | close(fd); |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 197 | /* |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 198 | * Release a memory mapping. |
| 199 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 200 | void sysReleaseMap(MemMapping* pMap) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 201 | { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 202 | int i; |
| 203 | for (i = 0; i < pMap->range_count; ++i) { |
| 204 | if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 205 | PLOG(ERROR) << "munmap(" << pMap->ranges[i].addr << ", " << pMap->ranges[i].length |
| 206 | << ") failed"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 207 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 208 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 209 | free(pMap->ranges); |
| 210 | pMap->ranges = NULL; |
| 211 | pMap->range_count = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 212 | } |