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 | */ |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 6 | #include <stdbool.h> |
| 7 | #include <stdint.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | #include <string.h> |
| 12 | #include <sys/mman.h> |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <fcntl.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 16 | #include <limits.h> |
| 17 | #include <errno.h> |
| 18 | #include <assert.h> |
| 19 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 20 | #define LOG_TAG "sysutil" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include "Log.h" |
| 22 | #include "SysUtil.h" |
| 23 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | static int getFileStartAndLength(int fd, off_t *start_, size_t *length_) |
| 25 | { |
| 26 | off_t start, end; |
| 27 | size_t length; |
| 28 | |
| 29 | assert(start_ != NULL); |
| 30 | assert(length_ != NULL); |
| 31 | |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 32 | // TODO: isn't start always 0 for the single call site? just use fstat instead? |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 34 | start = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_CUR)); |
| 35 | end = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_END)); |
| 36 | |
| 37 | if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1 || |
| 38 | start == (off_t) -1 || end == (off_t) -1) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 39 | LOGE("could not determine length of file\n"); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | length = end - start; |
| 44 | if (length == 0) { |
| 45 | LOGE("file is empty\n"); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | *start_ = start; |
| 50 | *length_ = length; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 56 | * Map a file (from fd's current offset) into a private, read-only memory |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 57 | * segment. The file offset must be a multiple of the page size. |
| 58 | * |
| 59 | * On success, returns 0 and fills out "pMap". On failure, returns a nonzero |
| 60 | * value and does not disturb "pMap". |
| 61 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 62 | static int sysMapFD(int fd, MemMapping* pMap) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | { |
| 64 | off_t start; |
| 65 | size_t length; |
| 66 | void* memPtr; |
| 67 | |
| 68 | assert(pMap != NULL); |
| 69 | |
| 70 | if (getFileStartAndLength(fd, &start, &length) < 0) |
| 71 | return -1; |
| 72 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 73 | memPtr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, start); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 74 | if (memPtr == MAP_FAILED) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 75 | LOGW("mmap(%d, R, PRIVATE, %d, %d) failed: %s\n", (int) length, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | fd, (int) start, strerror(errno)); |
| 77 | return -1; |
| 78 | } |
| 79 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 80 | pMap->addr = memPtr; |
| 81 | pMap->length = length; |
| 82 | pMap->range_count = 1; |
| 83 | pMap->ranges = malloc(sizeof(MappedRange)); |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 84 | if (pMap->ranges == NULL) { |
| 85 | LOGE("malloc failed: %s\n", strerror(errno)); |
| 86 | munmap(memPtr, length); |
| 87 | return -1; |
| 88 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 89 | pMap->ranges[0].addr = memPtr; |
| 90 | pMap->ranges[0].length = length; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 95 | static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) |
| 96 | { |
| 97 | char block_dev[PATH_MAX+1]; |
| 98 | size_t size; |
| 99 | unsigned int blksize; |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 100 | size_t blocks; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 101 | unsigned int range_count; |
| 102 | unsigned int i; |
| 103 | |
| 104 | if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) { |
| 105 | LOGW("failed to read block device from header\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | for (i = 0; i < sizeof(block_dev); ++i) { |
| 109 | if (block_dev[i] == '\n') { |
| 110 | block_dev[i] = 0; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
Mark Salyzyn | 76b245c | 2014-03-17 15:35:52 -0700 | [diff] [blame] | 115 | if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 116 | LOGW("failed to parse block map header\n"); |
| 117 | return -1; |
| 118 | } |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 119 | if (blksize != 0) { |
| 120 | blocks = ((size-1) / blksize) + 1; |
| 121 | } |
| 122 | if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) { |
| 123 | LOGE("invalid data in block map file: size %zu, blksize %u, range_count %u\n", |
| 124 | size, blksize, range_count); |
| 125 | return -1; |
| 126 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 127 | |
| 128 | pMap->range_count = range_count; |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 129 | pMap->ranges = calloc(range_count, sizeof(MappedRange)); |
| 130 | if (pMap->ranges == NULL) { |
| 131 | LOGE("calloc(%u, %zu) failed: %s\n", range_count, sizeof(MappedRange), strerror(errno)); |
| 132 | return -1; |
| 133 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 134 | |
| 135 | // Reserve enough contiguous address space for the whole file. |
| 136 | unsigned char* reserve; |
| 137 | reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 138 | if (reserve == MAP_FAILED) { |
| 139 | LOGW("failed to reserve address space: %s\n", strerror(errno)); |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 140 | free(pMap->ranges); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 144 | int fd = open(block_dev, O_RDONLY); |
| 145 | if (fd < 0) { |
| 146 | LOGW("failed to open block device %s: %s\n", block_dev, strerror(errno)); |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 147 | munmap(reserve, blocks * blksize); |
| 148 | free(pMap->ranges); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | unsigned char* next = reserve; |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 153 | size_t remaining_size = blocks * blksize; |
| 154 | bool success = true; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 155 | for (i = 0; i < range_count; ++i) { |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 156 | size_t start, end; |
| 157 | if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 158 | LOGW("failed to parse range %d in block map\n", i); |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 159 | success = false; |
| 160 | break; |
| 161 | } |
| 162 | size_t length = (end - start) * blksize; |
| 163 | if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) { |
| 164 | LOGE("unexpected range in block map: %zu %zu\n", start, end); |
| 165 | success = false; |
| 166 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 169 | 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] | 170 | if (addr == MAP_FAILED) { |
| 171 | LOGW("failed to map block %d: %s\n", i, strerror(errno)); |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 172 | success = false; |
| 173 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 174 | } |
| 175 | pMap->ranges[i].addr = addr; |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 176 | pMap->ranges[i].length = length; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 177 | |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 178 | next += length; |
| 179 | remaining_size -= length; |
| 180 | } |
| 181 | if (success && remaining_size != 0) { |
| 182 | LOGE("ranges in block map are invalid: remaining_size = %zu\n", remaining_size); |
| 183 | success = false; |
| 184 | } |
| 185 | if (!success) { |
| 186 | close(fd); |
| 187 | munmap(reserve, blocks * blksize); |
| 188 | free(pMap->ranges); |
| 189 | return -1; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 192 | close(fd); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 193 | pMap->addr = reserve; |
| 194 | pMap->length = size; |
| 195 | |
Doug Zongker | 19a8e24 | 2014-01-21 09:25:41 -0800 | [diff] [blame] | 196 | LOGI("mmapped %d ranges\n", range_count); |
| 197 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int sysMapFile(const char* fn, MemMapping* pMap) |
| 202 | { |
| 203 | memset(pMap, 0, sizeof(*pMap)); |
| 204 | |
| 205 | if (fn && fn[0] == '@') { |
| 206 | // A map of blocks |
| 207 | FILE* mapf = fopen(fn+1, "r"); |
| 208 | if (mapf == NULL) { |
| 209 | LOGV("Unable to open '%s': %s\n", fn+1, strerror(errno)); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | if (sysMapBlockFile(mapf, pMap) != 0) { |
| 214 | LOGW("Map of '%s' failed\n", fn); |
Yabin Cui | 1273956 | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 215 | fclose(mapf); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | fclose(mapf); |
| 220 | } else { |
| 221 | // This is a regular file. |
| 222 | int fd = open(fn, O_RDONLY, 0); |
| 223 | if (fd < 0) { |
| 224 | LOGE("Unable to open '%s': %s\n", fn, strerror(errno)); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if (sysMapFD(fd, pMap) != 0) { |
| 229 | LOGE("Map of '%s' failed\n", fn); |
| 230 | close(fd); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | close(fd); |
| 235 | } |
| 236 | return 0; |
| 237 | } |
| 238 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 239 | /* |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 240 | * Release a memory mapping. |
| 241 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 242 | void sysReleaseMap(MemMapping* pMap) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 243 | { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 244 | int i; |
| 245 | for (i = 0; i < pMap->range_count; ++i) { |
| 246 | if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) { |
| 247 | LOGW("munmap(%p, %d) failed: %s\n", |
| 248 | pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno)); |
| 249 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 250 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 251 | free(pMap->ranges); |
| 252 | pMap->ranges = NULL; |
| 253 | pMap->range_count = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 254 | } |