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