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