blob: d75d76cf618fb6a449a0dcdfe4cc763e3f2333dc [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 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 <stdlib.h>
18#include <sys/socket.h>
San Mehata19b2502010-01-06 10:33:53 -080019#include <sys/types.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <netinet/in.h>
21#include <arpa/inet.h>
San Mehata19b2502010-01-06 10:33:53 -080022#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070023#include <errno.h>
San Mehat2350c442010-03-02 13:16:50 -080024#include <fcntl.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080025#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070026
San Mehatd9a4e352010-03-12 13:32:47 -080027#define LOG_TAG "VoldCmdListener"
San Mehatf1b736b2009-10-10 17:22:08 -070028#include <cutils/log.h>
29
30#include <sysutils/SocketClient.h>
Ken Sumrall3ad90722011-10-04 20:38:29 -070031#include <private/android_filesystem_config.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#include "CommandListener.h"
34#include "VolumeManager.h"
San Mehata2677e42009-12-13 10:40:18 -080035#include "ResponseCode.h"
San Mehat586536c2010-02-16 17:12:00 -080036#include "Process.h"
San Mehat2350c442010-03-02 13:16:50 -080037#include "Xwarp.h"
San Mehatd9a4e352010-03-12 13:32:47 -080038#include "Loop.h"
39#include "Devmapper.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080040#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070041
42CommandListener::CommandListener() :
Robert Greenwalt149aa3e2012-02-16 14:43:03 -080043 FrameworkListener("vold", true) {
San Mehatd9a4e352010-03-12 13:32:47 -080044 registerCmd(new DumpCmd());
San Mehateba65e92010-01-29 05:15:16 -080045 registerCmd(new VolumeCmd());
46 registerCmd(new AsecCmd());
Kenny Root508c0e12010-07-12 09:59:49 -070047 registerCmd(new ObbCmd());
San Mehat586536c2010-02-16 17:12:00 -080048 registerCmd(new StorageCmd());
San Mehat2350c442010-03-02 13:16:50 -080049 registerCmd(new XwarpCmd());
Ken Sumrall8f869aa2010-12-03 03:47:09 -080050 registerCmd(new CryptfsCmd());
San Mehatf1b736b2009-10-10 17:22:08 -070051}
52
San Mehatd9a4e352010-03-12 13:32:47 -080053void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
54 char buffer[4096];
55 char *p = buffer;
56
57 memset(buffer, 0, sizeof(buffer));
58 int i;
59 for (i = 0; i < argc; i++) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -080060 unsigned int len = strlen(argv[i]) + 1; // Account for space
San Mehatd9a4e352010-03-12 13:32:47 -080061 if (i == argObscure) {
62 len += 2; // Account for {}
63 }
64 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
65 if (i == argObscure) {
66 *p++ = '{';
67 *p++ = '}';
68 *p++ = ' ';
69 continue;
70 }
71 strcpy(p, argv[i]);
72 p+= strlen(argv[i]);
73 if (i != (argc -1)) {
74 *p++ = ' ';
75 }
76 }
77 }
San Mehat97ac40e2010-03-24 10:24:19 -070078 SLOGD("%s", buffer);
San Mehatd9a4e352010-03-12 13:32:47 -080079}
80
81CommandListener::DumpCmd::DumpCmd() :
82 VoldCommand("dump") {
83}
84
85int CommandListener::DumpCmd::runCommand(SocketClient *cli,
86 int argc, char **argv) {
87 cli->sendMsg(0, "Dumping loop status", false);
88 if (Loop::dumpState(cli)) {
89 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
90 }
91 cli->sendMsg(0, "Dumping DM status", false);
92 if (Devmapper::dumpState(cli)) {
93 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
94 }
San Mehat96597e82010-03-17 09:50:54 -070095 cli->sendMsg(0, "Dumping mounted filesystems", false);
96 FILE *fp = fopen("/proc/mounts", "r");
97 if (fp) {
98 char line[1024];
99 while (fgets(line, sizeof(line), fp)) {
100 line[strlen(line)-1] = '\0';
101 cli->sendMsg(0, line, false);;
102 }
103 fclose(fp);
104 }
San Mehatd9a4e352010-03-12 13:32:47 -0800105
106 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
107 return 0;
108}
109
110
San Mehateba65e92010-01-29 05:15:16 -0800111CommandListener::VolumeCmd::VolumeCmd() :
112 VoldCommand("volume") {
San Mehatf1b736b2009-10-10 17:22:08 -0700113}
114
San Mehateba65e92010-01-29 05:15:16 -0800115int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
San Mehatf1b736b2009-10-10 17:22:08 -0700116 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800117 dumpArgs(argc, argv, -1);
118
San Mehateba65e92010-01-29 05:15:16 -0800119 if (argc < 2) {
120 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
121 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700122 }
123
San Mehateba65e92010-01-29 05:15:16 -0800124 VolumeManager *vm = VolumeManager::Instance();
125 int rc = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700126
San Mehateba65e92010-01-29 05:15:16 -0800127 if (!strcmp(argv[1], "list")) {
128 return vm->listVolumes(cli);
San Mehatd9a4e352010-03-12 13:32:47 -0800129 } else if (!strcmp(argv[1], "debug")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700130 if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) {
131 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false);
132 return 0;
133 }
134 vm->setDebug(!strcmp(argv[2], "on") ? true : false);
San Mehateba65e92010-01-29 05:15:16 -0800135 } else if (!strcmp(argv[1], "mount")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700136 if (argc != 3) {
137 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);
138 return 0;
139 }
San Mehateba65e92010-01-29 05:15:16 -0800140 rc = vm->mountVolume(argv[2]);
141 } else if (!strcmp(argv[1], "unmount")) {
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700142 if (argc < 3 || argc > 4 ||
143 ((argc == 4 && strcmp(argv[3], "force")) &&
144 (argc == 4 && strcmp(argv[3], "force_and_revert")))) {
145 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700146 return 0;
147 }
148
San Mehat4ba89482010-02-18 09:00:18 -0800149 bool force = false;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700150 bool revert = false;
San Mehat4ba89482010-02-18 09:00:18 -0800151 if (argc >= 4 && !strcmp(argv[3], "force")) {
152 force = true;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700153 } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) {
154 force = true;
155 revert = true;
San Mehat4ba89482010-02-18 09:00:18 -0800156 }
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700157 rc = vm->unmountVolume(argv[2], force, revert);
San Mehateba65e92010-01-29 05:15:16 -0800158 } else if (!strcmp(argv[1], "format")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700159 if (argc != 3) {
160 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path>", false);
161 return 0;
162 }
San Mehateba65e92010-01-29 05:15:16 -0800163 rc = vm->formatVolume(argv[2]);
164 } else if (!strcmp(argv[1], "share")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700165 if (argc != 4) {
166 cli->sendMsg(ResponseCode::CommandSyntaxError,
167 "Usage: volume share <path> <method>", false);
168 return 0;
169 }
San Mehatb9aed742010-02-04 15:07:01 -0800170 rc = vm->shareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800171 } else if (!strcmp(argv[1], "unshare")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700172 if (argc != 4) {
173 cli->sendMsg(ResponseCode::CommandSyntaxError,
174 "Usage: volume unshare <path> <method>", false);
175 return 0;
176 }
San Mehatb9aed742010-02-04 15:07:01 -0800177 rc = vm->unshareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800178 } else if (!strcmp(argv[1], "shared")) {
179 bool enabled = false;
San Mehat57df7bf2010-03-14 13:41:27 -0700180 if (argc != 4) {
181 cli->sendMsg(ResponseCode::CommandSyntaxError,
182 "Usage: volume shared <path> <method>", false);
183 return 0;
184 }
San Mehatf1b736b2009-10-10 17:22:08 -0700185
San Mehat2b225522010-02-03 10:26:40 -0800186 if (vm->shareEnabled(argv[2], argv[3], &enabled)) {
San Mehateba65e92010-01-29 05:15:16 -0800187 cli->sendMsg(
188 ResponseCode::OperationFailed, "Failed to determine share enable state", true);
189 } else {
190 cli->sendMsg(ResponseCode::ShareEnabledResult,
191 (enabled ? "Share enabled" : "Share disabled"), false);
192 }
San Mehatb9aed742010-02-04 15:07:01 -0800193 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700194 } else {
San Mehateba65e92010-01-29 05:15:16 -0800195 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);
196 }
197
198 if (!rc) {
199 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
200 } else {
San Mehat8f2875b2010-02-18 11:40:49 -0800201 int erno = errno;
202 rc = ResponseCode::convertFromErrno();
San Mehateba65e92010-01-29 05:15:16 -0800203 cli->sendMsg(rc, "volume operation failed", true);
San Mehat49e2bce2009-10-12 16:29:01 -0700204 }
205
San Mehatf1b736b2009-10-10 17:22:08 -0700206 return 0;
207}
208
San Mehat586536c2010-02-16 17:12:00 -0800209CommandListener::StorageCmd::StorageCmd() :
210 VoldCommand("storage") {
211}
212
213int CommandListener::StorageCmd::runCommand(SocketClient *cli,
214 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800215 dumpArgs(argc, argv, -1);
216
San Mehat586536c2010-02-16 17:12:00 -0800217 if (argc < 2) {
218 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
219 return 0;
220 }
221
222 if (!strcmp(argv[1], "users")) {
223 DIR *dir;
224 struct dirent *de;
225
226 if (!(dir = opendir("/proc"))) {
227 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
228 return 0;
229 }
230
231 while ((de = readdir(dir))) {
232 int pid = Process::getPid(de->d_name);
233
234 if (pid < 0) {
235 continue;
236 }
237
238 char processName[255];
239 Process::getProcessName(pid, processName, sizeof(processName));
240
241 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
242 Process::checkFileMaps(pid, argv[2]) ||
243 Process::checkSymLink(pid, argv[2], "cwd") ||
244 Process::checkSymLink(pid, argv[2], "root") ||
245 Process::checkSymLink(pid, argv[2], "exe")) {
246
247 char msg[1024];
248 snprintf(msg, sizeof(msg), "%d %s", pid, processName);
249 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
250 }
251 }
252 closedir(dir);
253 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
254 } else {
255 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
256 }
257 return 0;
258}
259
San Mehateba65e92010-01-29 05:15:16 -0800260CommandListener::AsecCmd::AsecCmd() :
261 VoldCommand("asec") {
San Mehata19b2502010-01-06 10:33:53 -0800262}
263
Kenny Root344ca102012-04-03 17:23:01 -0700264void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
265 DIR *d = opendir(directory);
266
267 if (!d) {
268 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
269 return;
270 }
271
272 size_t dirent_len = offsetof(struct dirent, d_name) +
273 pathconf(directory, _PC_NAME_MAX) + 1;
274
275 struct dirent *dent = (struct dirent *) malloc(dirent_len);
276 if (dent == NULL) {
277 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
278 return;
279 }
280
281 struct dirent *result;
282
283 while (!readdir_r(d, dent, &result) && result != NULL) {
284 if (dent->d_name[0] == '.')
285 continue;
286 if (dent->d_type != DT_REG)
287 continue;
288 size_t name_len = strlen(dent->d_name);
289 if (name_len > 5 && name_len < 260 &&
290 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
291 char id[255];
292 memset(id, 0, sizeof(id));
293 strlcpy(id, dent->d_name, name_len - 5);
294 cli->sendMsg(ResponseCode::AsecListResult, id, false);
295 }
296 }
297 closedir(d);
298
299 free(dent);
300}
301
San Mehateba65e92010-01-29 05:15:16 -0800302int CommandListener::AsecCmd::runCommand(SocketClient *cli,
303 int argc, char **argv) {
304 if (argc < 2) {
305 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
San Mehata19b2502010-01-06 10:33:53 -0800306 return 0;
307 }
308
San Mehateba65e92010-01-29 05:15:16 -0800309 VolumeManager *vm = VolumeManager::Instance();
310 int rc = 0;
San Mehata19b2502010-01-06 10:33:53 -0800311
San Mehateba65e92010-01-29 05:15:16 -0800312 if (!strcmp(argv[1], "list")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800313 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800314
Kenny Root344ca102012-04-03 17:23:01 -0700315 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT);
316 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT);
San Mehateba65e92010-01-29 05:15:16 -0800317 } else if (!strcmp(argv[1], "create")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800318 dumpArgs(argc, argv, 5);
Kenny Root344ca102012-04-03 17:23:01 -0700319 if (argc != 8) {
San Mehateba65e92010-01-29 05:15:16 -0800320 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root344ca102012-04-03 17:23:01 -0700321 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
322 "<isExternal>", false);
San Mehateba65e92010-01-29 05:15:16 -0800323 return 0;
324 }
325
326 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
Kenny Root344ca102012-04-03 17:23:01 -0700327 const bool isExternal = (atoi(argv[7]) == 1);
328 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
San Mehateba65e92010-01-29 05:15:16 -0800329 } else if (!strcmp(argv[1], "finalize")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800330 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800331 if (argc != 3) {
332 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
333 return 0;
334 }
San Mehat8f2875b2010-02-18 11:40:49 -0800335 rc = vm->finalizeAsec(argv[2]);
Kenny Root344ca102012-04-03 17:23:01 -0700336 } else if (!strcmp(argv[1], "fixperms")) {
337 dumpArgs(argc, argv, -1);
338 if (argc != 5) {
339 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
340 return 0;
341 }
342
343 char *endptr;
344 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
345 if (*endptr != '\0') {
346 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
347 return 0;
348 }
349
350 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800351 } else if (!strcmp(argv[1], "destroy")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800352 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800353 if (argc < 3) {
354 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800355 return 0;
356 }
San Mehat4ba89482010-02-18 09:00:18 -0800357 bool force = false;
358 if (argc > 3 && !strcmp(argv[3], "force")) {
359 force = true;
360 }
San Mehat8f2875b2010-02-18 11:40:49 -0800361 rc = vm->destroyAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800362 } else if (!strcmp(argv[1], "mount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800363 dumpArgs(argc, argv, 3);
San Mehateba65e92010-01-29 05:15:16 -0800364 if (argc != 5) {
365 cli->sendMsg(ResponseCode::CommandSyntaxError,
366 "Usage: asec mount <namespace-id> <key> <ownerUid>", false);
367 return 0;
368 }
San Mehat8f2875b2010-02-18 11:40:49 -0800369 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
San Mehateba65e92010-01-29 05:15:16 -0800370 } else if (!strcmp(argv[1], "unmount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800371 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800372 if (argc < 3) {
373 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800374 return 0;
375 }
San Mehat4ba89482010-02-18 09:00:18 -0800376 bool force = false;
377 if (argc > 3 && !strcmp(argv[3], "force")) {
378 force = true;
379 }
San Mehat8f2875b2010-02-18 11:40:49 -0800380 rc = vm->unmountAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800381 } else if (!strcmp(argv[1], "rename")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800382 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800383 if (argc != 4) {
384 cli->sendMsg(ResponseCode::CommandSyntaxError,
385 "Usage: asec rename <old_id> <new_id>", false);
386 return 0;
387 }
San Mehat8f2875b2010-02-18 11:40:49 -0800388 rc = vm->renameAsec(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800389 } else if (!strcmp(argv[1], "path")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800390 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800391 if (argc != 3) {
392 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
393 return 0;
394 }
395 char path[255];
396
San Mehat88ac2c02010-03-23 11:15:58 -0700397 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehateba65e92010-01-29 05:15:16 -0800398 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
San Mehat88ac2c02010-03-23 11:15:58 -0700399 return 0;
San Mehateba65e92010-01-29 05:15:16 -0800400 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700401 } else if (!strcmp(argv[1], "fspath")) {
402 dumpArgs(argc, argv, -1);
403 if (argc != 3) {
404 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
405 return 0;
406 }
407 char path[255];
408
409 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
410 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
411 return 0;
412 }
San Mehata19b2502010-01-06 10:33:53 -0800413 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800414 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800415 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
San Mehata19b2502010-01-06 10:33:53 -0800416 }
417
San Mehat8f2875b2010-02-18 11:40:49 -0800418 if (!rc) {
419 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
420 } else {
421 rc = ResponseCode::convertFromErrno();
422 cli->sendMsg(rc, "asec operation failed", true);
423 }
424
San Mehata19b2502010-01-06 10:33:53 -0800425 return 0;
426}
San Mehat2350c442010-03-02 13:16:50 -0800427
Kenny Root508c0e12010-07-12 09:59:49 -0700428CommandListener::ObbCmd::ObbCmd() :
429 VoldCommand("obb") {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700430}
431
Kenny Root508c0e12010-07-12 09:59:49 -0700432int CommandListener::ObbCmd::runCommand(SocketClient *cli,
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700433 int argc, char **argv) {
434 if (argc < 2) {
435 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
436 return 0;
437 }
438
439 VolumeManager *vm = VolumeManager::Instance();
440 int rc = 0;
441
Kenny Root508c0e12010-07-12 09:59:49 -0700442 if (!strcmp(argv[1], "list")) {
443 dumpArgs(argc, argv, -1);
444
445 rc = vm->listMountedObbs(cli);
446 } else if (!strcmp(argv[1], "mount")) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700447 dumpArgs(argc, argv, 3);
448 if (argc != 5) {
449 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root508c0e12010-07-12 09:59:49 -0700450 "Usage: obb mount <filename> <key> <ownerUid>", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700451 return 0;
452 }
Kenny Root508c0e12010-07-12 09:59:49 -0700453 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700454 } else if (!strcmp(argv[1], "unmount")) {
455 dumpArgs(argc, argv, -1);
456 if (argc < 3) {
Kenny Root508c0e12010-07-12 09:59:49 -0700457 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700458 return 0;
459 }
460 bool force = false;
461 if (argc > 3 && !strcmp(argv[3], "force")) {
462 force = true;
463 }
Kenny Root508c0e12010-07-12 09:59:49 -0700464 rc = vm->unmountObb(argv[2], force);
465 } else if (!strcmp(argv[1], "path")) {
466 dumpArgs(argc, argv, -1);
467 if (argc != 3) {
468 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
469 return 0;
470 }
471 char path[255];
472
473 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
474 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
475 return 0;
476 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700477 } else {
478 dumpArgs(argc, argv, -1);
Kenny Root508c0e12010-07-12 09:59:49 -0700479 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700480 }
481
482 if (!rc) {
Kenny Root508c0e12010-07-12 09:59:49 -0700483 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700484 } else {
485 rc = ResponseCode::convertFromErrno();
Kenny Root508c0e12010-07-12 09:59:49 -0700486 cli->sendMsg(rc, "obb operation failed", true);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700487 }
488
489 return 0;
490}
491
San Mehat2350c442010-03-02 13:16:50 -0800492CommandListener::XwarpCmd::XwarpCmd() :
493 VoldCommand("xwarp") {
494}
495
496int CommandListener::XwarpCmd::runCommand(SocketClient *cli,
497 int argc, char **argv) {
498 if (argc < 2) {
499 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
500 return 0;
501 }
502
503 if (!strcmp(argv[1], "enable")) {
504 if (Xwarp::enable()) {
505 cli->sendMsg(ResponseCode::OperationFailed, "Failed to enable xwarp", true);
506 return 0;
507 }
508
509 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp mirroring started", false);
510 } else if (!strcmp(argv[1], "disable")) {
511 if (Xwarp::disable()) {
512 cli->sendMsg(ResponseCode::OperationFailed, "Failed to disable xwarp", true);
513 return 0;
514 }
515
516 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp disabled", false);
517 } else if (!strcmp(argv[1], "status")) {
518 char msg[255];
519 bool r;
520 unsigned mirrorPos, maxSize;
521
522 if (Xwarp::status(&r, &mirrorPos, &maxSize)) {
523 cli->sendMsg(ResponseCode::OperationFailed, "Failed to get xwarp status", true);
524 return 0;
525 }
526 snprintf(msg, sizeof(msg), "%s %u %u", (r ? "ready" : "not-ready"), mirrorPos, maxSize);
527 cli->sendMsg(ResponseCode::XwarpStatusResult, msg, false);
528 } else {
529 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
530 }
531
532 return 0;
533}
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800534
535CommandListener::CryptfsCmd::CryptfsCmd() :
536 VoldCommand("cryptfs") {
537}
538
539int CommandListener::CryptfsCmd::runCommand(SocketClient *cli,
540 int argc, char **argv) {
Ken Sumrall3ad90722011-10-04 20:38:29 -0700541 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
542 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
543 return 0;
544 }
545
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800546 if (argc < 2) {
547 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
548 return 0;
549 }
550
551 int rc = 0;
552
553 if (!strcmp(argv[1], "checkpw")) {
554 if (argc != 3) {
555 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
556 return 0;
557 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800558 dumpArgs(argc, argv, 2);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800559 rc = cryptfs_check_passwd(argv[2]);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800560 } else if (!strcmp(argv[1], "restart")) {
561 if (argc != 2) {
562 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
563 return 0;
564 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800565 dumpArgs(argc, argv, -1);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800566 rc = cryptfs_restart();
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800567 } else if (!strcmp(argv[1], "cryptocomplete")) {
568 if (argc != 2) {
569 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
570 return 0;
571 }
572 dumpArgs(argc, argv, -1);
573 rc = cryptfs_crypto_complete();
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800574 } else if (!strcmp(argv[1], "enablecrypto")) {
575 if ( (argc != 4) || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
576 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs enablecrypto <wipe|inplace> <passwd>", false);
577 return 0;
578 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800579 dumpArgs(argc, argv, 3);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800580 rc = cryptfs_enable(argv[2], argv[3]);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800581 } else if (!strcmp(argv[1], "changepw")) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600582 if (argc != 3) {
583 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs changepw <newpasswd>", false);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800584 return 0;
585 }
Jason parks70a4b3f2011-01-28 10:10:47 -0600586 SLOGD("cryptfs changepw {}");
587 rc = cryptfs_changepw(argv[2]);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700588 } else if (!strcmp(argv[1], "verifypw")) {
589 if (argc != 3) {
590 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
591 return 0;
592 }
593 SLOGD("cryptfs verifypw {}");
594 rc = cryptfs_verify_passwd(argv[2]);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800595 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800596 dumpArgs(argc, argv, -1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800597 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
598 }
599
Jason parks0167cb12011-01-20 18:30:39 -0600600 // Always report that the command succeeded and return the error code.
601 // The caller will check the return value to see what the error was.
602 char msg[255];
603 snprintf(msg, sizeof(msg), "%d", rc);
604 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800605
606 return 0;
607}