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