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