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