blob: 8601591002a205ebe37f67a3697047acec2991fb [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
32 start = lseek(fd, 0L, SEEK_CUR);
33 end = lseek(fd, 0L, SEEK_END);
34 (void) lseek(fd, start, SEEK_SET);
35
36 if (start == (off_t) -1 || end == (off_t) -1) {
37 LOGE("could not determine length of file\n");
38 return -1;
39 }
40
41 length = end - start;
42 if (length == 0) {
43 LOGE("file is empty\n");
44 return -1;
45 }
46
47 *start_ = start;
48 *length_ = length;
49
50 return 0;
51}
52
53/*
Doug Zongker99916f02014-01-13 14:16:58 -080054 * Map a file (from fd's current offset) into a private, read-only memory
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055 * segment. The file offset must be a multiple of the page size.
56 *
57 * On success, returns 0 and fills out "pMap". On failure, returns a nonzero
58 * value and does not disturb "pMap".
59 */
Doug Zongker99916f02014-01-13 14:16:58 -080060static int sysMapFD(int fd, MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061{
62 off_t start;
63 size_t length;
64 void* memPtr;
65
66 assert(pMap != NULL);
67
68 if (getFileStartAndLength(fd, &start, &length) < 0)
69 return -1;
70
Doug Zongker99916f02014-01-13 14:16:58 -080071 memPtr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, start);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 if (memPtr == MAP_FAILED) {
Doug Zongker99916f02014-01-13 14:16:58 -080073 LOGW("mmap(%d, R, PRIVATE, %d, %d) failed: %s\n", (int) length,
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074 fd, (int) start, strerror(errno));
75 return -1;
76 }
77
Doug Zongker99916f02014-01-13 14:16:58 -080078 pMap->addr = memPtr;
79 pMap->length = length;
80 pMap->range_count = 1;
81 pMap->ranges = malloc(sizeof(MappedRange));
Yabin Cui12739562016-02-18 11:32:10 -080082 if (pMap->ranges == NULL) {
83 LOGE("malloc failed: %s\n", strerror(errno));
84 munmap(memPtr, length);
85 return -1;
86 }
Doug Zongker99916f02014-01-13 14:16:58 -080087 pMap->ranges[0].addr = memPtr;
88 pMap->ranges[0].length = length;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089
90 return 0;
91}
92
Doug Zongker99916f02014-01-13 14:16:58 -080093static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
94{
95 char block_dev[PATH_MAX+1];
96 size_t size;
97 unsigned int blksize;
Yabin Cui12739562016-02-18 11:32:10 -080098 size_t blocks;
Doug Zongker99916f02014-01-13 14:16:58 -080099 unsigned int range_count;
100 unsigned int i;
101
102 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
103 LOGW("failed to read block device from header\n");
104 return -1;
105 }
106 for (i = 0; i < sizeof(block_dev); ++i) {
107 if (block_dev[i] == '\n') {
108 block_dev[i] = 0;
109 break;
110 }
111 }
112
Mark Salyzyn76b245c2014-03-17 15:35:52 -0700113 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Doug Zongker99916f02014-01-13 14:16:58 -0800114 LOGW("failed to parse block map header\n");
115 return -1;
116 }
Yabin Cui12739562016-02-18 11:32:10 -0800117 if (blksize != 0) {
118 blocks = ((size-1) / blksize) + 1;
119 }
120 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) {
121 LOGE("invalid data in block map file: size %zu, blksize %u, range_count %u\n",
122 size, blksize, range_count);
123 return -1;
124 }
Doug Zongker99916f02014-01-13 14:16:58 -0800125
126 pMap->range_count = range_count;
Yabin Cui12739562016-02-18 11:32:10 -0800127 pMap->ranges = calloc(range_count, sizeof(MappedRange));
128 if (pMap->ranges == NULL) {
129 LOGE("calloc(%u, %zu) failed: %s\n", range_count, sizeof(MappedRange), strerror(errno));
130 return -1;
131 }
Doug Zongker99916f02014-01-13 14:16:58 -0800132
133 // Reserve enough contiguous address space for the whole file.
134 unsigned char* reserve;
135 reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
136 if (reserve == MAP_FAILED) {
137 LOGW("failed to reserve address space: %s\n", strerror(errno));
Yabin Cui12739562016-02-18 11:32:10 -0800138 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800139 return -1;
140 }
141
Doug Zongker99916f02014-01-13 14:16:58 -0800142 int fd = open(block_dev, O_RDONLY);
143 if (fd < 0) {
144 LOGW("failed to open block device %s: %s\n", block_dev, strerror(errno));
Yabin Cui12739562016-02-18 11:32:10 -0800145 munmap(reserve, blocks * blksize);
146 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800147 return -1;
148 }
149
150 unsigned char* next = reserve;
Yabin Cui12739562016-02-18 11:32:10 -0800151 size_t remaining_size = blocks * blksize;
152 bool success = true;
Doug Zongker99916f02014-01-13 14:16:58 -0800153 for (i = 0; i < range_count; ++i) {
Yabin Cui12739562016-02-18 11:32:10 -0800154 size_t start, end;
155 if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) {
Doug Zongker99916f02014-01-13 14:16:58 -0800156 LOGW("failed to parse range %d in block map\n", i);
Yabin Cui12739562016-02-18 11:32:10 -0800157 success = false;
158 break;
159 }
160 size_t length = (end - start) * blksize;
161 if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) {
162 LOGE("unexpected range in block map: %zu %zu\n", start, end);
163 success = false;
164 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800165 }
166
Yabin Cui12739562016-02-18 11:32:10 -0800167 void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
Doug Zongker99916f02014-01-13 14:16:58 -0800168 if (addr == MAP_FAILED) {
169 LOGW("failed to map block %d: %s\n", i, strerror(errno));
Yabin Cui12739562016-02-18 11:32:10 -0800170 success = false;
171 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800172 }
173 pMap->ranges[i].addr = addr;
Yabin Cui12739562016-02-18 11:32:10 -0800174 pMap->ranges[i].length = length;
Doug Zongker99916f02014-01-13 14:16:58 -0800175
Yabin Cui12739562016-02-18 11:32:10 -0800176 next += length;
177 remaining_size -= length;
178 }
179 if (success && remaining_size != 0) {
180 LOGE("ranges in block map are invalid: remaining_size = %zu\n", remaining_size);
181 success = false;
182 }
183 if (!success) {
184 close(fd);
185 munmap(reserve, blocks * blksize);
186 free(pMap->ranges);
187 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800188 }
189
Yabin Cui12739562016-02-18 11:32:10 -0800190 close(fd);
Doug Zongker99916f02014-01-13 14:16:58 -0800191 pMap->addr = reserve;
192 pMap->length = size;
193
Doug Zongker19a8e242014-01-21 09:25:41 -0800194 LOGI("mmapped %d ranges\n", range_count);
195
Doug Zongker99916f02014-01-13 14:16:58 -0800196 return 0;
197}
198
199int sysMapFile(const char* fn, MemMapping* pMap)
200{
201 memset(pMap, 0, sizeof(*pMap));
202
203 if (fn && fn[0] == '@') {
204 // A map of blocks
205 FILE* mapf = fopen(fn+1, "r");
206 if (mapf == NULL) {
207 LOGV("Unable to open '%s': %s\n", fn+1, strerror(errno));
208 return -1;
209 }
210
211 if (sysMapBlockFile(mapf, pMap) != 0) {
212 LOGW("Map of '%s' failed\n", fn);
Yabin Cui12739562016-02-18 11:32:10 -0800213 fclose(mapf);
Doug Zongker99916f02014-01-13 14:16:58 -0800214 return -1;
215 }
216
217 fclose(mapf);
218 } else {
219 // This is a regular file.
220 int fd = open(fn, O_RDONLY, 0);
221 if (fd < 0) {
222 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
223 return -1;
224 }
225
226 if (sysMapFD(fd, pMap) != 0) {
227 LOGE("Map of '%s' failed\n", fn);
228 close(fd);
229 return -1;
230 }
231
232 close(fd);
233 }
234 return 0;
235}
236
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800238 * Release a memory mapping.
239 */
Doug Zongker99916f02014-01-13 14:16:58 -0800240void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241{
Doug Zongker99916f02014-01-13 14:16:58 -0800242 int i;
243 for (i = 0; i < pMap->range_count; ++i) {
244 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
245 LOGW("munmap(%p, %d) failed: %s\n",
246 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
247 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 }
Doug Zongker99916f02014-01-13 14:16:58 -0800249 free(pMap->ranges);
250 pMap->ranges = NULL;
251 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252}