blob: e7dd17b51de9f43e8f9d210596394e15c2ec1886 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * System utilities.
5 */
Elliott Hughesf267dee2015-06-23 12:31:02 -07006#include <assert.h>
7#include <errno.h>
Doug Zongker99916f02014-01-13 14:16:58 -08008#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08009#include <limits.h>
Elliott Hughesf267dee2015-06-23 12:31:02 -070010#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 Projectc24a8e62009-03-03 19:28:42 -080018
Doug Zongker99916f02014-01-13 14:16:58 -080019#define LOG_TAG "sysutil"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020#include "Log.h"
21#include "SysUtil.h"
22
Elliott Hughesf267dee2015-06-23 12:31:02 -070023static bool sysMapFD(int fd, MemMapping* pMap) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024 assert(pMap != NULL);
25
Elliott Hughesf267dee2015-06-23 12:31:02 -070026 struct stat sb;
27 if (fstat(fd, &sb) == -1) {
Tao Baod7d0f752015-07-15 14:13:06 -070028 LOGE("fstat(%d) failed: %s\n", fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070029 return false;
30 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Elliott Hughesf267dee2015-06-23 12:31:02 -070032 void* memPtr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033 if (memPtr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -070034 LOGE("mmap(%d, R, PRIVATE, %d, 0) failed: %s\n", (int) sb.st_size, fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070035 return false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036 }
37
Doug Zongker99916f02014-01-13 14:16:58 -080038 pMap->addr = memPtr;
Elliott Hughesf267dee2015-06-23 12:31:02 -070039 pMap->length = sb.st_size;
Doug Zongker99916f02014-01-13 14:16:58 -080040 pMap->range_count = 1;
41 pMap->ranges = malloc(sizeof(MappedRange));
Yabin Cui4f2df162016-02-18 11:32:10 -080042 if (pMap->ranges == NULL) {
43 LOGE("malloc failed: %s\n", strerror(errno));
Yabin Cui0d28ba42016-02-22 22:12:27 -080044 munmap(memPtr, sb.st_size);
45 return false;
Yabin Cui4f2df162016-02-18 11:32:10 -080046 }
Doug Zongker99916f02014-01-13 14:16:58 -080047 pMap->ranges[0].addr = memPtr;
Elliott Hughesf267dee2015-06-23 12:31:02 -070048 pMap->ranges[0].length = sb.st_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Elliott Hughesf267dee2015-06-23 12:31:02 -070050 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051}
52
Doug Zongker99916f02014-01-13 14:16:58 -080053static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
54{
55 char block_dev[PATH_MAX+1];
56 size_t size;
57 unsigned int blksize;
Yabin Cui4f2df162016-02-18 11:32:10 -080058 size_t blocks;
Doug Zongker99916f02014-01-13 14:16:58 -080059 unsigned int range_count;
60 unsigned int i;
61
62 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -070063 LOGE("failed to read block device from header\n");
Doug Zongker99916f02014-01-13 14:16:58 -080064 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 Salyzyn76b245c2014-03-17 15:35:52 -070073 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Tao Baod7d0f752015-07-15 14:13:06 -070074 LOGE("failed to parse block map header\n");
Doug Zongker99916f02014-01-13 14:16:58 -080075 return -1;
76 }
Yabin Cui4f2df162016-02-18 11:32:10 -080077 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 Zongker99916f02014-01-13 14:16:58 -080085
86 pMap->range_count = range_count;
Yabin Cui4f2df162016-02-18 11:32:10 -080087 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 Zongker99916f02014-01-13 14:16:58 -080092
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 Baod7d0f752015-07-15 14:13:06 -070097 LOGE("failed to reserve address space: %s\n", strerror(errno));
Yabin Cui4f2df162016-02-18 11:32:10 -080098 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -080099 return -1;
100 }
101
Doug Zongker99916f02014-01-13 14:16:58 -0800102 int fd = open(block_dev, O_RDONLY);
103 if (fd < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700104 LOGE("failed to open block device %s: %s\n", block_dev, strerror(errno));
Yabin Cui4f2df162016-02-18 11:32:10 -0800105 munmap(reserve, blocks * blksize);
106 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800107 return -1;
108 }
109
110 unsigned char* next = reserve;
Yabin Cui4f2df162016-02-18 11:32:10 -0800111 size_t remaining_size = blocks * blksize;
112 bool success = true;
Doug Zongker99916f02014-01-13 14:16:58 -0800113 for (i = 0; i < range_count; ++i) {
Yabin Cui4f2df162016-02-18 11:32:10 -0800114 size_t start, end;
115 if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) {
Tao Baod7d0f752015-07-15 14:13:06 -0700116 LOGE("failed to parse range %d in block map\n", i);
Yabin Cui4f2df162016-02-18 11:32:10 -0800117 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 Zongker99916f02014-01-13 14:16:58 -0800125 }
126
Yabin Cui4f2df162016-02-18 11:32:10 -0800127 void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
Doug Zongker99916f02014-01-13 14:16:58 -0800128 if (addr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -0700129 LOGE("failed to map block %d: %s\n", i, strerror(errno));
Yabin Cui4f2df162016-02-18 11:32:10 -0800130 success = false;
131 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800132 }
133 pMap->ranges[i].addr = addr;
Yabin Cui4f2df162016-02-18 11:32:10 -0800134 pMap->ranges[i].length = length;
Doug Zongker99916f02014-01-13 14:16:58 -0800135
Yabin Cui4f2df162016-02-18 11:32:10 -0800136 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 Zongker99916f02014-01-13 14:16:58 -0800148 }
149
Yabin Cui4f2df162016-02-18 11:32:10 -0800150 close(fd);
Doug Zongker99916f02014-01-13 14:16:58 -0800151 pMap->addr = reserve;
152 pMap->length = size;
153
Doug Zongker19a8e242014-01-21 09:25:41 -0800154 LOGI("mmapped %d ranges\n", range_count);
155
Doug Zongker99916f02014-01-13 14:16:58 -0800156 return 0;
157}
158
159int 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 Baod7d0f752015-07-15 14:13:06 -0700167 LOGE("Unable to open '%s': %s\n", fn+1, strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -0800168 return -1;
169 }
170
171 if (sysMapBlockFile(mapf, pMap) != 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700172 LOGE("Map of '%s' failed\n", fn);
Yabin Cui4f2df162016-02-18 11:32:10 -0800173 fclose(mapf);
Doug Zongker99916f02014-01-13 14:16:58 -0800174 return -1;
175 }
176
177 fclose(mapf);
178 } else {
179 // This is a regular file.
Elliott Hughesf267dee2015-06-23 12:31:02 -0700180 int fd = open(fn, O_RDONLY);
181 if (fd == -1) {
Doug Zongker99916f02014-01-13 14:16:58 -0800182 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
183 return -1;
184 }
185
Elliott Hughesf267dee2015-06-23 12:31:02 -0700186 if (!sysMapFD(fd, pMap)) {
Doug Zongker99916f02014-01-13 14:16:58 -0800187 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 Projectc24a8e62009-03-03 19:28:42 -0800197/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198 * Release a memory mapping.
199 */
Doug Zongker99916f02014-01-13 14:16:58 -0800200void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800201{
Doug Zongker99916f02014-01-13 14:16:58 -0800202 int i;
203 for (i = 0; i < pMap->range_count; ++i) {
204 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700205 LOGE("munmap(%p, %d) failed: %s\n",
Doug Zongker99916f02014-01-13 14:16:58 -0800206 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
207 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 }
Doug Zongker99916f02014-01-13 14:16:58 -0800209 free(pMap->ranges);
210 pMap->ranges = NULL;
211 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212}