blob: 9c3575b9c8246fa854c9ab8ca2c366dd73cf6924 [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
nkk716069a792016-01-19 16:36:47 +020023static int getFileStartAndLength(int fd, loff_t *start_, size_t *length_)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024{
nkk716069a792016-01-19 16:36:47 +020025 loff_t start, end;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026 size_t length;
27
28 assert(start_ != NULL);
29 assert(length_ != NULL);
30
Elliott Hughes2f5feed2015-04-28 17:24:24 -070031 // TODO: isn't start always 0 for the single call site? just use fstat instead?
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
nkk716069a792016-01-19 16:36:47 +020033 start = TEMP_FAILURE_RETRY(lseek64(fd, 0L, SEEK_CUR));
34 end = TEMP_FAILURE_RETRY(lseek64(fd, 0L, SEEK_END));
Elliott Hughes2f5feed2015-04-28 17:24:24 -070035
nkk716069a792016-01-19 16:36:47 +020036 if (TEMP_FAILURE_RETRY(lseek64(fd, start, SEEK_SET)) == -1 ||
37 start == (loff_t) -1 || end == (loff_t) -1) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038 LOGE("could not determine length of file\n");
39 return -1;
40 }
41
42 length = end - start;
43 if (length == 0) {
44 LOGE("file is empty\n");
45 return -1;
46 }
47
48 *start_ = start;
49 *length_ = length;
50
51 return 0;
52}
53
54/*
Doug Zongker99916f02014-01-13 14:16:58 -080055 * Map a file (from fd's current offset) into a private, read-only memory
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056 * segment. The file offset must be a multiple of the page size.
57 *
58 * On success, returns 0 and fills out "pMap". On failure, returns a nonzero
59 * value and does not disturb "pMap".
60 */
Ethan Yonker34ae4832016-08-24 15:32:18 -050061static bool sysMapFD(int fd, MemMapping* pMap) {
nkk716069a792016-01-19 16:36:47 +020062 loff_t start;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063 size_t length;
64 void* memPtr;
65
66 assert(pMap != NULL);
67
68 if (getFileStartAndLength(fd, &start, &length) < 0)
69 return -1;
70
nkk716069a792016-01-19 16:36:47 +020071#if (PLATFORM_SDK_VERSION >= 21)
72 memPtr = mmap64(NULL, length, PROT_READ, MAP_PRIVATE, fd, start);
73#else
74 // Older versions of Android do not have mmap64 so we will just use mmap instead
Doug Zongker99916f02014-01-13 14:16:58 -080075 memPtr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, start);
nkk716069a792016-01-19 16:36:47 +020076#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 if (memPtr == MAP_FAILED) {
Ethan Yonker34ae4832016-08-24 15:32:18 -050078 LOGE("mmap(%d, R, PRIVATE, %d, 0) failed: %s\n", (int) length, fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070079 return false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080 }
81
Doug Zongker99916f02014-01-13 14:16:58 -080082 pMap->addr = memPtr;
83 pMap->length = length;
84 pMap->range_count = 1;
85 pMap->ranges = malloc(sizeof(MappedRange));
Yabin Cui4f2df162016-02-18 11:32:10 -080086 if (pMap->ranges == NULL) {
87 LOGE("malloc failed: %s\n", strerror(errno));
Ethan Yonker34ae4832016-08-24 15:32:18 -050088 munmap(memPtr, length);
Yabin Cui0d28ba42016-02-22 22:12:27 -080089 return false;
Yabin Cui4f2df162016-02-18 11:32:10 -080090 }
Doug Zongker99916f02014-01-13 14:16:58 -080091 pMap->ranges[0].addr = memPtr;
92 pMap->ranges[0].length = length;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093
Elliott Hughesf267dee2015-06-23 12:31:02 -070094 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095}
96
Doug Zongker99916f02014-01-13 14:16:58 -080097static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
98{
99 char block_dev[PATH_MAX+1];
100 size_t size;
101 unsigned int blksize;
Yabin Cui4f2df162016-02-18 11:32:10 -0800102 size_t blocks;
Doug Zongker99916f02014-01-13 14:16:58 -0800103 unsigned int range_count;
104 unsigned int i;
105
106 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -0700107 LOGE("failed to read block device from header\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800108 return -1;
109 }
110 for (i = 0; i < sizeof(block_dev); ++i) {
111 if (block_dev[i] == '\n') {
112 block_dev[i] = 0;
113 break;
114 }
115 }
116
Mark Salyzyn76b245c2014-03-17 15:35:52 -0700117 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Tao Baod7d0f752015-07-15 14:13:06 -0700118 LOGE("failed to parse block map header\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800119 return -1;
120 }
Yabin Cui4f2df162016-02-18 11:32:10 -0800121 if (blksize != 0) {
122 blocks = ((size-1) / blksize) + 1;
123 }
124 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) {
125 LOGE("invalid data in block map file: size %zu, blksize %u, range_count %u\n",
126 size, blksize, range_count);
Doug Zongker99916f02014-01-13 14:16:58 -0800127 return -1;
128 }
129
Doug Zongker99916f02014-01-13 14:16:58 -0800130 pMap->range_count = range_count;
Yabin Cui4f2df162016-02-18 11:32:10 -0800131 pMap->ranges = calloc(range_count, sizeof(MappedRange));
132 if (pMap->ranges == NULL) {
133 LOGE("calloc(%u, %zu) failed: %s\n", range_count, sizeof(MappedRange), strerror(errno));
134 return -1;
135 }
Doug Zongker99916f02014-01-13 14:16:58 -0800136
137 // Reserve enough contiguous address space for the whole file.
138 unsigned char* reserve;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600139#if (PLATFORM_SDK_VERSION >= 21)
Doug Zongker99916f02014-01-13 14:16:58 -0800140 reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
Ethan Yonker738be7a2014-12-10 11:40:43 -0600141#else
142 // Older versions of Android do not have mmap64 so we will just use mmap instead
143 reserve = mmap(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
144#endif
Doug Zongker99916f02014-01-13 14:16:58 -0800145 if (reserve == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -0700146 LOGE("failed to reserve address space: %s\n", strerror(errno));
Yabin Cui4f2df162016-02-18 11:32:10 -0800147 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800148 return -1;
149 }
150
Doug Zongker99916f02014-01-13 14:16:58 -0800151 int fd = open(block_dev, O_RDONLY);
152 if (fd < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700153 LOGE("failed to open block device %s: %s\n", block_dev, strerror(errno));
Yabin Cui4f2df162016-02-18 11:32:10 -0800154 munmap(reserve, blocks * blksize);
155 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800156 return -1;
157 }
158
159 unsigned char* next = reserve;
Yabin Cui4f2df162016-02-18 11:32:10 -0800160 size_t remaining_size = blocks * blksize;
161 bool success = true;
Doug Zongker99916f02014-01-13 14:16:58 -0800162 for (i = 0; i < range_count; ++i) {
Yabin Cui4f2df162016-02-18 11:32:10 -0800163 size_t start, end;
164 if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) {
Tao Baod7d0f752015-07-15 14:13:06 -0700165 LOGE("failed to parse range %d in block map\n", i);
Yabin Cui4f2df162016-02-18 11:32:10 -0800166 success = false;
167 break;
168 }
169 size_t length = (end - start) * blksize;
170 if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) {
171 LOGE("unexpected range in block map: %zu %zu\n", start, end);
172 success = false;
173 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800174 }
Ethan Yonker738be7a2014-12-10 11:40:43 -0600175#if (PLATFORM_SDK_VERSION >= 21)
Doug Zongker99916f02014-01-13 14:16:58 -0800176 void* addr = mmap64(next, (end-start)*blksize, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
Ethan Yonker738be7a2014-12-10 11:40:43 -0600177#else
178 // Older versions of Android do not have mmap64 so we will just use mmap instead
179 void* addr = mmap(next, (end-start)*blksize, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
180#endif
Doug Zongker99916f02014-01-13 14:16:58 -0800181 if (addr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -0700182 LOGE("failed to map block %d: %s\n", i, strerror(errno));
Yabin Cui4f2df162016-02-18 11:32:10 -0800183 success = false;
184 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800185 }
186 pMap->ranges[i].addr = addr;
Yabin Cui4f2df162016-02-18 11:32:10 -0800187 pMap->ranges[i].length = length;
Doug Zongker99916f02014-01-13 14:16:58 -0800188
Yabin Cui4f2df162016-02-18 11:32:10 -0800189 next += length;
190 remaining_size -= length;
191 }
192 if (success && remaining_size != 0) {
193 LOGE("ranges in block map are invalid: remaining_size = %zu\n", remaining_size);
194 success = false;
195 }
196 if (!success) {
197 close(fd);
198 munmap(reserve, blocks * blksize);
199 free(pMap->ranges);
200 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800201 }
202
Yabin Cui4f2df162016-02-18 11:32:10 -0800203 close(fd);
Doug Zongker99916f02014-01-13 14:16:58 -0800204 pMap->addr = reserve;
205 pMap->length = size;
206
Doug Zongker19a8e242014-01-21 09:25:41 -0800207 LOGI("mmapped %d ranges\n", range_count);
208
Doug Zongker99916f02014-01-13 14:16:58 -0800209 return 0;
210}
211
212int sysMapFile(const char* fn, MemMapping* pMap)
213{
214 memset(pMap, 0, sizeof(*pMap));
215
216 if (fn && fn[0] == '@') {
217 // A map of blocks
218 FILE* mapf = fopen(fn+1, "r");
219 if (mapf == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -0700220 LOGE("Unable to open '%s': %s\n", fn+1, strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -0800221 return -1;
222 }
223
224 if (sysMapBlockFile(mapf, pMap) != 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700225 LOGE("Map of '%s' failed\n", fn);
Yabin Cui4f2df162016-02-18 11:32:10 -0800226 fclose(mapf);
Doug Zongker99916f02014-01-13 14:16:58 -0800227 return -1;
228 }
229
230 fclose(mapf);
231 } else {
232 // This is a regular file.
Elliott Hughesf267dee2015-06-23 12:31:02 -0700233 int fd = open(fn, O_RDONLY);
234 if (fd == -1) {
Doug Zongker99916f02014-01-13 14:16:58 -0800235 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
236 return -1;
237 }
238
Elliott Hughesf267dee2015-06-23 12:31:02 -0700239 if (!sysMapFD(fd, pMap)) {
Doug Zongker99916f02014-01-13 14:16:58 -0800240 LOGE("Map of '%s' failed\n", fn);
241 close(fd);
242 return -1;
243 }
244
245 close(fd);
246 }
247 return 0;
248}
249
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 * Release a memory mapping.
252 */
Doug Zongker99916f02014-01-13 14:16:58 -0800253void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254{
Doug Zongker99916f02014-01-13 14:16:58 -0800255 int i;
256 for (i = 0; i < pMap->range_count; ++i) {
257 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700258 LOGE("munmap(%p, %d) failed: %s\n",
Doug Zongker99916f02014-01-13 14:16:58 -0800259 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
260 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 }
Doug Zongker99916f02014-01-13 14:16:58 -0800262 free(pMap->ranges);
263 pMap->ranges = NULL;
264 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265}