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