blob: f98c15d5be2ace95d36ee39ae83414867776a870 [file] [log] [blame]
Jeff Sharkey068c6be2017-09-06 13:47:40 -06001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VoldNativeService.h"
18#include "VolumeManager.h"
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060019#include "MoveTask.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060020
21#include <fstream>
22
23#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
25#include <android-base/strings.h>
26#include <private/android_filesystem_config.h>
27
28#ifndef LOG_TAG
29#define LOG_TAG "vold"
30#endif
31
32using android::base::StringPrintf;
33using std::endl;
34
35namespace android {
36namespace vold {
37
38namespace {
39
40constexpr const char* kDump = "android.permission.DUMP";
41
42static binder::Status ok() {
43 return binder::Status::ok();
44}
45
46static binder::Status exception(uint32_t code, const std::string& msg) {
47 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
48}
49
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060050static binder::Status error(const std::string& msg) {
51 PLOG(ERROR) << msg;
52 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
53}
54
55static binder::Status translate(uint32_t status) {
56 if (status == 0) {
57 return binder::Status::ok();
58 } else {
59 return binder::Status::fromExceptionCode(status);
60 }
61}
62
Jeff Sharkey068c6be2017-09-06 13:47:40 -060063binder::Status checkPermission(const char* permission) {
64 pid_t pid;
65 uid_t uid;
66
67 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
68 reinterpret_cast<int32_t*>(&uid))) {
69 return ok();
70 } else {
71 return exception(binder::Status::EX_SECURITY,
72 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
73 }
74}
75
76binder::Status checkUid(uid_t expectedUid) {
77 uid_t uid = IPCThreadState::self()->getCallingUid();
78 if (uid == expectedUid || uid == AID_ROOT) {
79 return ok();
80 } else {
81 return exception(binder::Status::EX_SECURITY,
82 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
83 }
84}
85
86#define ENFORCE_UID(uid) { \
87 binder::Status status = checkUid((uid)); \
88 if (!status.isOk()) { \
89 return status; \
90 } \
91}
92
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060093#define ACQUIRE_LOCK std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
94
Jeff Sharkey068c6be2017-09-06 13:47:40 -060095} // namespace
96
97status_t VoldNativeService::start() {
98 IPCThreadState::self()->disableBackgroundScheduling(true);
99 status_t ret = BinderService<VoldNativeService>::publish();
100 if (ret != android::OK) {
101 return ret;
102 }
103 sp<ProcessState> ps(ProcessState::self());
104 ps->startThreadPool();
105 ps->giveThreadPoolName();
106 return android::OK;
107}
108
109status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) {
110 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
111 const binder::Status dump_permission = checkPermission(kDump);
112 if (!dump_permission.isOk()) {
113 out << dump_permission.toString8() << endl;
114 return PERMISSION_DENIED;
115 }
116
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600117 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600118 out << "vold is happy!" << endl;
119 out.flush();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600120 return NO_ERROR;
121}
122
123binder::Status VoldNativeService::reset() {
124 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600125 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600126
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600127 return translate(VolumeManager::Instance()->reset());
128}
129
130binder::Status VoldNativeService::shutdown() {
131 ENFORCE_UID(AID_SYSTEM);
132 ACQUIRE_LOCK;
133
134 return translate(VolumeManager::Instance()->shutdown());
135}
136
137binder::Status VoldNativeService::setDebug(bool debug) {
138 ENFORCE_UID(AID_SYSTEM);
139 ACQUIRE_LOCK;
140
141 return translate(VolumeManager::Instance()->setDebug(debug));
142}
143
144binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
145 ENFORCE_UID(AID_SYSTEM);
146 ACQUIRE_LOCK;
147
148 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
149}
150
151binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
152 ENFORCE_UID(AID_SYSTEM);
153 ACQUIRE_LOCK;
154
155 return translate(VolumeManager::Instance()->onUserRemoved(userId));
156}
157
158binder::Status VoldNativeService::onUserStarted(int32_t userId) {
159 ENFORCE_UID(AID_SYSTEM);
160 ACQUIRE_LOCK;
161
162 return translate(VolumeManager::Instance()->onUserStarted(userId));
163}
164
165binder::Status VoldNativeService::onUserStopped(int32_t userId) {
166 ENFORCE_UID(AID_SYSTEM);
167 ACQUIRE_LOCK;
168
169 return translate(VolumeManager::Instance()->onUserStopped(userId));
170}
171
172binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType, int32_t ratio) {
173 ENFORCE_UID(AID_SYSTEM);
174 ACQUIRE_LOCK;
175
176 auto disk = VolumeManager::Instance()->findDisk(diskId);
177 if (disk == nullptr) {
178 return error("Failed to find disk " + diskId);
179 }
180 switch (partitionType) {
181 case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic());
182 case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate());
183 case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio));
184 default: return error("Unknown type " + std::to_string(partitionType));
185 }
186}
187
188binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) {
189 ENFORCE_UID(AID_SYSTEM);
190 ACQUIRE_LOCK;
191
192 return translate(VolumeManager::Instance()->forgetPartition(partGuid));
193}
194
195binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags, int32_t mountUserId) {
196 ENFORCE_UID(AID_SYSTEM);
197 ACQUIRE_LOCK;
198
199 auto vol = VolumeManager::Instance()->findVolume(volId);
200 if (vol == nullptr) {
201 return error("Failed to find volume " + volId);
202 }
203
204 vol->setMountFlags(mountFlags);
205 vol->setMountUserId(mountUserId);
206
207 int res = vol->mount();
208 if (mountFlags & MOUNT_FLAG_PRIMARY) {
209 VolumeManager::Instance()->setPrimary(vol);
210 }
211 return translate(res);
212}
213
214binder::Status VoldNativeService::unmount(const std::string& volId) {
215 ENFORCE_UID(AID_SYSTEM);
216 ACQUIRE_LOCK;
217
218 auto vol = VolumeManager::Instance()->findVolume(volId);
219 if (vol == nullptr) {
220 return error("Failed to find volume " + volId);
221 }
222 return translate(vol->unmount());
223}
224
225binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
226 ENFORCE_UID(AID_SYSTEM);
227 ACQUIRE_LOCK;
228
229 auto vol = VolumeManager::Instance()->findVolume(volId);
230 if (vol == nullptr) {
231 return error("Failed to find volume " + volId);
232 }
233 return translate(vol->format(fsType));
234}
235
236binder::Status VoldNativeService::benchmark(const std::string& volId, int64_t* _aidl_return) {
237 ENFORCE_UID(AID_SYSTEM);
238 ACQUIRE_LOCK;
239
240 *_aidl_return = VolumeManager::Instance()->benchmarkPrivate(volId);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600241 return ok();
242}
243
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600244binder::Status VoldNativeService::moveStorage(const std::string& fromVolId, const std::string& toVolId) {
245 ENFORCE_UID(AID_SYSTEM);
246 ACQUIRE_LOCK;
247
248 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
249 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
250 if (fromVol == nullptr) {
251 return error("Failed to find volume " + fromVolId);
252 } else if (toVol == nullptr) {
253 return error("Failed to find volume " + toVolId);
254 }
255 (new android::vold::MoveTask(fromVol, toVol))->start();
256 return ok();
257}
258
259binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
260 ENFORCE_UID(AID_SYSTEM);
261 ACQUIRE_LOCK;
262
263 std::string tmp;
264 switch (remountMode) {
265 case REMOUNT_MODE_NONE: tmp = "none"; break;
266 case REMOUNT_MODE_DEFAULT: tmp = "default"; break;
267 case REMOUNT_MODE_READ: tmp = "read"; break;
268 case REMOUNT_MODE_WRITE: tmp = "write"; break;
269 default: return error("Unknown mode " + std::to_string(remountMode));
270 }
271 return translate(VolumeManager::Instance()->remountUid(uid, tmp));
272}
273
274binder::Status VoldNativeService::mkdirs(const std::string& path) {
275 ENFORCE_UID(AID_SYSTEM);
276 ACQUIRE_LOCK;
277
278 return translate(VolumeManager::Instance()->mkdirs(path.c_str()));
279}
280
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600281} // namespace vold
282} // namespace android