blob: b160c9e3dc4ee78f346220cee9370bd567e9c0a2 [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 */
6#include <stdlib.h>
7#include <stdio.h>
8#include <unistd.h>
9#include <string.h>
10#include <sys/mman.h>
Doug Zongker99916f02014-01-13 14:16:58 -080011#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080014#include <limits.h>
15#include <errno.h>
16#include <assert.h>
17
Doug Zongker99916f02014-01-13 14:16:58 -080018#define LOG_TAG "sysutil"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include "Log.h"
20#include "SysUtil.h"
21
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022static int getFileStartAndLength(int fd, off_t *start_, size_t *length_)
23{
24 off_t start, end;
25 size_t length;
26
27 assert(start_ != NULL);
28 assert(length_ != NULL);
29
Elliott Hughes7bad7c42015-04-28 17:24:24 -070030 // 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 -080031
Elliott Hughes7bad7c42015-04-28 17:24:24 -070032 start = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_CUR));
33 end = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_END));
34
35 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1 ||
36 start == (off_t) -1 || end == (off_t) -1) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037 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));
82 pMap->ranges[0].addr = memPtr;
83 pMap->ranges[0].length = length;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084
85 return 0;
86}
87
Doug Zongker99916f02014-01-13 14:16:58 -080088static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
89{
90 char block_dev[PATH_MAX+1];
91 size_t size;
92 unsigned int blksize;
93 unsigned int blocks;
94 unsigned int range_count;
95 unsigned int i;
96
97 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
98 LOGW("failed to read block device from header\n");
99 return -1;
100 }
101 for (i = 0; i < sizeof(block_dev); ++i) {
102 if (block_dev[i] == '\n') {
103 block_dev[i] = 0;
104 break;
105 }
106 }
107
Mark Salyzyn76b245c2014-03-17 15:35:52 -0700108 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Doug Zongker99916f02014-01-13 14:16:58 -0800109 LOGW("failed to parse block map header\n");
110 return -1;
111 }
112
113 blocks = ((size-1) / blksize) + 1;
114
115 pMap->range_count = range_count;
116 pMap->ranges = malloc(range_count * sizeof(MappedRange));
117 memset(pMap->ranges, 0, range_count * sizeof(MappedRange));
118
119 // Reserve enough contiguous address space for the whole file.
120 unsigned char* reserve;
121 reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
122 if (reserve == MAP_FAILED) {
123 LOGW("failed to reserve address space: %s\n", strerror(errno));
124 return -1;
125 }
126
127 pMap->ranges[range_count-1].addr = reserve;
128 pMap->ranges[range_count-1].length = blocks * blksize;
129
130 int fd = open(block_dev, O_RDONLY);
131 if (fd < 0) {
132 LOGW("failed to open block device %s: %s\n", block_dev, strerror(errno));
133 return -1;
134 }
135
136 unsigned char* next = reserve;
137 for (i = 0; i < range_count; ++i) {
138 int start, end;
139 if (fscanf(mapf, "%d %d\n", &start, &end) != 2) {
140 LOGW("failed to parse range %d in block map\n", i);
141 return -1;
142 }
143
144 void* addr = mmap64(next, (end-start)*blksize, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
145 if (addr == MAP_FAILED) {
146 LOGW("failed to map block %d: %s\n", i, strerror(errno));
147 return -1;
148 }
149 pMap->ranges[i].addr = addr;
150 pMap->ranges[i].length = (end-start)*blksize;
151
152 next += pMap->ranges[i].length;
153 }
154
155 pMap->addr = reserve;
156 pMap->length = size;
157
Doug Zongker19a8e242014-01-21 09:25:41 -0800158 LOGI("mmapped %d ranges\n", range_count);
159
Doug Zongker99916f02014-01-13 14:16:58 -0800160 return 0;
161}
162
163int sysMapFile(const char* fn, MemMapping* pMap)
164{
165 memset(pMap, 0, sizeof(*pMap));
166
167 if (fn && fn[0] == '@') {
168 // A map of blocks
169 FILE* mapf = fopen(fn+1, "r");
170 if (mapf == NULL) {
171 LOGV("Unable to open '%s': %s\n", fn+1, strerror(errno));
172 return -1;
173 }
174
175 if (sysMapBlockFile(mapf, pMap) != 0) {
176 LOGW("Map of '%s' failed\n", fn);
177 return -1;
178 }
179
180 fclose(mapf);
181 } else {
182 // This is a regular file.
183 int fd = open(fn, O_RDONLY, 0);
184 if (fd < 0) {
185 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
186 return -1;
187 }
188
189 if (sysMapFD(fd, pMap) != 0) {
190 LOGE("Map of '%s' failed\n", fn);
191 close(fd);
192 return -1;
193 }
194
195 close(fd);
196 }
197 return 0;
198}
199
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800201 * Release a memory mapping.
202 */
Doug Zongker99916f02014-01-13 14:16:58 -0800203void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204{
Doug Zongker99916f02014-01-13 14:16:58 -0800205 int i;
206 for (i = 0; i < pMap->range_count; ++i) {
207 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
208 LOGW("munmap(%p, %d) failed: %s\n",
209 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
210 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211 }
Doug Zongker99916f02014-01-13 14:16:58 -0800212 free(pMap->ranges);
213 pMap->ranges = NULL;
214 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215}