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