blob: b1fb4556da49a30703f2c654a08eb8578d805e11 [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 */
Yabin Cui12739562016-02-18 11:32:10 -08006#include <stdbool.h>
7#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08008#include <stdlib.h>
9#include <stdio.h>
10#include <unistd.h>
11#include <string.h>
12#include <sys/mman.h>
Doug Zongker99916f02014-01-13 14:16:58 -080013#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080016#include <limits.h>
17#include <errno.h>
18#include <assert.h>
19
Doug Zongker99916f02014-01-13 14:16:58 -080020#define LOG_TAG "sysutil"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include "Log.h"
22#include "SysUtil.h"
23
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024static int getFileStartAndLength(int fd, off_t *start_, size_t *length_)
25{
26 off_t start, end;
27 size_t length;
28
29 assert(start_ != NULL);
30 assert(length_ != NULL);
31
Elliott Hughes2f5feed2015-04-28 17:24:24 -070032 // 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 -080033
Elliott Hughes2f5feed2015-04-28 17:24:24 -070034 start = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_CUR));
35 end = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_END));
36
37 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1 ||
38 start == (off_t) -1 || end == (off_t) -1) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039 LOGE("could not determine length of file\n");
40 return -1;
41 }
42
43 length = end - start;
44 if (length == 0) {
45 LOGE("file is empty\n");
46 return -1;
47 }
48
49 *start_ = start;
50 *length_ = length;
51
52 return 0;
53}
54
55/*
Doug Zongker99916f02014-01-13 14:16:58 -080056 * Map a file (from fd's current offset) into a private, read-only memory
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 * segment. The file offset must be a multiple of the page size.
58 *
59 * On success, returns 0 and fills out "pMap". On failure, returns a nonzero
60 * value and does not disturb "pMap".
61 */
Doug Zongker99916f02014-01-13 14:16:58 -080062static int sysMapFD(int fd, MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063{
64 off_t start;
65 size_t length;
66 void* memPtr;
67
68 assert(pMap != NULL);
69
70 if (getFileStartAndLength(fd, &start, &length) < 0)
71 return -1;
72
Doug Zongker99916f02014-01-13 14:16:58 -080073 memPtr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074 if (memPtr == MAP_FAILED) {
Doug Zongker99916f02014-01-13 14:16:58 -080075 LOGW("mmap(%d, R, PRIVATE, %d, %d) failed: %s\n", (int) length,
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 fd, (int) start, strerror(errno));
77 return -1;
78 }
79
Doug Zongker99916f02014-01-13 14:16:58 -080080 pMap->addr = memPtr;
81 pMap->length = length;
82 pMap->range_count = 1;
83 pMap->ranges = malloc(sizeof(MappedRange));
Yabin Cui12739562016-02-18 11:32:10 -080084 if (pMap->ranges == NULL) {
85 LOGE("malloc failed: %s\n", strerror(errno));
86 munmap(memPtr, length);
87 return -1;
88 }
Doug Zongker99916f02014-01-13 14:16:58 -080089 pMap->ranges[0].addr = memPtr;
90 pMap->ranges[0].length = length;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091
92 return 0;
93}
94
Doug Zongker99916f02014-01-13 14:16:58 -080095static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
96{
97 char block_dev[PATH_MAX+1];
98 size_t size;
99 unsigned int blksize;
Yabin Cui12739562016-02-18 11:32:10 -0800100 size_t blocks;
Doug Zongker99916f02014-01-13 14:16:58 -0800101 unsigned int range_count;
102 unsigned int i;
103
104 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
105 LOGW("failed to read block device from header\n");
106 return -1;
107 }
108 for (i = 0; i < sizeof(block_dev); ++i) {
109 if (block_dev[i] == '\n') {
110 block_dev[i] = 0;
111 break;
112 }
113 }
114
Mark Salyzyn76b245c2014-03-17 15:35:52 -0700115 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Doug Zongker99916f02014-01-13 14:16:58 -0800116 LOGW("failed to parse block map header\n");
117 return -1;
118 }
Yabin Cui12739562016-02-18 11:32:10 -0800119 if (blksize != 0) {
120 blocks = ((size-1) / blksize) + 1;
121 }
122 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) {
123 LOGE("invalid data in block map file: size %zu, blksize %u, range_count %u\n",
124 size, blksize, range_count);
125 return -1;
126 }
Doug Zongker99916f02014-01-13 14:16:58 -0800127
128 pMap->range_count = range_count;
Yabin Cui12739562016-02-18 11:32:10 -0800129 pMap->ranges = calloc(range_count, sizeof(MappedRange));
130 if (pMap->ranges == NULL) {
131 LOGE("calloc(%u, %zu) failed: %s\n", range_count, sizeof(MappedRange), strerror(errno));
132 return -1;
133 }
Doug Zongker99916f02014-01-13 14:16:58 -0800134
135 // Reserve enough contiguous address space for the whole file.
136 unsigned char* reserve;
137 reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
138 if (reserve == MAP_FAILED) {
139 LOGW("failed to reserve address space: %s\n", strerror(errno));
Yabin Cui12739562016-02-18 11:32:10 -0800140 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800141 return -1;
142 }
143
Doug Zongker99916f02014-01-13 14:16:58 -0800144 int fd = open(block_dev, O_RDONLY);
145 if (fd < 0) {
146 LOGW("failed to open block device %s: %s\n", block_dev, strerror(errno));
Yabin Cui12739562016-02-18 11:32:10 -0800147 munmap(reserve, blocks * blksize);
148 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800149 return -1;
150 }
151
152 unsigned char* next = reserve;
Yabin Cui12739562016-02-18 11:32:10 -0800153 size_t remaining_size = blocks * blksize;
154 bool success = true;
Doug Zongker99916f02014-01-13 14:16:58 -0800155 for (i = 0; i < range_count; ++i) {
Yabin Cui12739562016-02-18 11:32:10 -0800156 size_t start, end;
157 if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) {
Doug Zongker99916f02014-01-13 14:16:58 -0800158 LOGW("failed to parse range %d in block map\n", i);
Yabin Cui12739562016-02-18 11:32:10 -0800159 success = false;
160 break;
161 }
162 size_t length = (end - start) * blksize;
163 if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) {
164 LOGE("unexpected range in block map: %zu %zu\n", start, end);
165 success = false;
166 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800167 }
168
Yabin Cui12739562016-02-18 11:32:10 -0800169 void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
Doug Zongker99916f02014-01-13 14:16:58 -0800170 if (addr == MAP_FAILED) {
171 LOGW("failed to map block %d: %s\n", i, strerror(errno));
Yabin Cui12739562016-02-18 11:32:10 -0800172 success = false;
173 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800174 }
175 pMap->ranges[i].addr = addr;
Yabin Cui12739562016-02-18 11:32:10 -0800176 pMap->ranges[i].length = length;
Doug Zongker99916f02014-01-13 14:16:58 -0800177
Yabin Cui12739562016-02-18 11:32:10 -0800178 next += length;
179 remaining_size -= length;
180 }
181 if (success && remaining_size != 0) {
182 LOGE("ranges in block map are invalid: remaining_size = %zu\n", remaining_size);
183 success = false;
184 }
185 if (!success) {
186 close(fd);
187 munmap(reserve, blocks * blksize);
188 free(pMap->ranges);
189 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800190 }
191
Yabin Cui12739562016-02-18 11:32:10 -0800192 close(fd);
Doug Zongker99916f02014-01-13 14:16:58 -0800193 pMap->addr = reserve;
194 pMap->length = size;
195
Doug Zongker19a8e242014-01-21 09:25:41 -0800196 LOGI("mmapped %d ranges\n", range_count);
197
Doug Zongker99916f02014-01-13 14:16:58 -0800198 return 0;
199}
200
201int sysMapFile(const char* fn, MemMapping* pMap)
202{
203 memset(pMap, 0, sizeof(*pMap));
204
205 if (fn && fn[0] == '@') {
206 // A map of blocks
207 FILE* mapf = fopen(fn+1, "r");
208 if (mapf == NULL) {
209 LOGV("Unable to open '%s': %s\n", fn+1, strerror(errno));
210 return -1;
211 }
212
213 if (sysMapBlockFile(mapf, pMap) != 0) {
214 LOGW("Map of '%s' failed\n", fn);
Yabin Cui12739562016-02-18 11:32:10 -0800215 fclose(mapf);
Doug Zongker99916f02014-01-13 14:16:58 -0800216 return -1;
217 }
218
219 fclose(mapf);
220 } else {
221 // This is a regular file.
222 int fd = open(fn, O_RDONLY, 0);
223 if (fd < 0) {
224 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
225 return -1;
226 }
227
228 if (sysMapFD(fd, pMap) != 0) {
229 LOGE("Map of '%s' failed\n", fn);
230 close(fd);
231 return -1;
232 }
233
234 close(fd);
235 }
236 return 0;
237}
238
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240 * Release a memory mapping.
241 */
Doug Zongker99916f02014-01-13 14:16:58 -0800242void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243{
Doug Zongker99916f02014-01-13 14:16:58 -0800244 int i;
245 for (i = 0; i < pMap->range_count; ++i) {
246 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
247 LOGW("munmap(%p, %d) failed: %s\n",
248 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
249 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250 }
Doug Zongker99916f02014-01-13 14:16:58 -0800251 free(pMap->ranges);
252 pMap->ranges = NULL;
253 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254}