CommandListener.cpp revision 422bdb7e49b39475328f05d765b00f0ef96820b8
1/* 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> 19#include <sys/types.h> 20#include <netinet/in.h> 21#include <arpa/inet.h> 22#include <dirent.h> 23#include <errno.h> 24#include <fcntl.h> 25#include <fs_mgr.h> 26#include <string.h> 27 28#define LOG_TAG "VoldCmdListener" 29#include <cutils/log.h> 30 31#include <sysutils/SocketClient.h> 32#include <private/android_filesystem_config.h> 33 34#include "CommandListener.h" 35#include "VolumeManager.h" 36#include "ResponseCode.h" 37#include "Process.h" 38#include "Loop.h" 39#include "Devmapper.h" 40#include "cryptfs.h" 41#include "fstrim.h" 42 43#define DUMP_ARGS 0 44 45CommandListener::CommandListener() : 46 FrameworkListener("vold", true) { 47 registerCmd(new DumpCmd()); 48 registerCmd(new VolumeCmd()); 49 registerCmd(new AsecCmd()); 50 registerCmd(new ObbCmd()); 51 registerCmd(new StorageCmd()); 52 registerCmd(new CryptfsCmd()); 53 registerCmd(new FstrimCmd()); 54} 55 56#if DUMP_ARGS 57void CommandListener::dumpArgs(int argc, char **argv, int argObscure) { 58 char buffer[4096]; 59 char *p = buffer; 60 61 memset(buffer, 0, sizeof(buffer)); 62 int i; 63 for (i = 0; i < argc; i++) { 64 unsigned int len = strlen(argv[i]) + 1; // Account for space 65 if (i == argObscure) { 66 len += 2; // Account for {} 67 } 68 if (((p - buffer) + len) < (sizeof(buffer)-1)) { 69 if (i == argObscure) { 70 *p++ = '{'; 71 *p++ = '}'; 72 *p++ = ' '; 73 continue; 74 } 75 strcpy(p, argv[i]); 76 p+= strlen(argv[i]); 77 if (i != (argc -1)) { 78 *p++ = ' '; 79 } 80 } 81 } 82 SLOGD("%s", buffer); 83} 84#else 85void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { } 86#endif 87 88CommandListener::DumpCmd::DumpCmd() : 89 VoldCommand("dump") { 90} 91 92int CommandListener::DumpCmd::runCommand(SocketClient *cli, 93 int /*argc*/, char ** /*argv*/) { 94 cli->sendMsg(0, "Dumping loop status", false); 95 if (Loop::dumpState(cli)) { 96 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true); 97 } 98 cli->sendMsg(0, "Dumping DM status", false); 99 if (Devmapper::dumpState(cli)) { 100 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true); 101 } 102 cli->sendMsg(0, "Dumping mounted filesystems", false); 103 FILE *fp = fopen("/proc/mounts", "r"); 104 if (fp) { 105 char line[1024]; 106 while (fgets(line, sizeof(line), fp)) { 107 line[strlen(line)-1] = '\0'; 108 cli->sendMsg(0, line, false);; 109 } 110 fclose(fp); 111 } 112 113 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false); 114 return 0; 115} 116 117CommandListener::VolumeCmd::VolumeCmd() : 118 VoldCommand("volume") { 119} 120 121int CommandListener::VolumeCmd::runCommand(SocketClient *cli, 122 int argc, char **argv) { 123 dumpArgs(argc, argv, -1); 124 125 if (argc < 2) { 126 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 127 return 0; 128 } 129 130 VolumeManager *vm = VolumeManager::Instance(); 131 int rc = 0; 132 133 if (!strcmp(argv[1], "list")) { 134 bool broadcast = argc >= 3 && !strcmp(argv[2], "broadcast"); 135 return vm->listVolumes(cli, broadcast); 136 } else if (!strcmp(argv[1], "debug")) { 137 if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) { 138 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false); 139 return 0; 140 } 141 vm->setDebug(!strcmp(argv[2], "on") ? true : false); 142 } else if (!strcmp(argv[1], "mount")) { 143 if (argc != 3) { 144 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false); 145 return 0; 146 } 147 rc = vm->mountVolume(argv[2]); 148 } else if (!strcmp(argv[1], "unmount")) { 149 if (argc < 3 || argc > 4 || 150 ((argc == 4 && strcmp(argv[3], "force")) && 151 (argc == 4 && strcmp(argv[3], "force_and_revert")))) { 152 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false); 153 return 0; 154 } 155 156 bool force = false; 157 bool revert = false; 158 if (argc >= 4 && !strcmp(argv[3], "force")) { 159 force = true; 160 } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) { 161 force = true; 162 revert = true; 163 } 164 rc = vm->unmountVolume(argv[2], force, revert); 165 } else if (!strcmp(argv[1], "format")) { 166 if (argc < 3 || argc > 4 || 167 (argc == 4 && strcmp(argv[3], "wipe"))) { 168 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path> [wipe]", false); 169 return 0; 170 } 171 bool wipe = false; 172 if (argc >= 4 && !strcmp(argv[3], "wipe")) { 173 wipe = true; 174 } 175 rc = vm->formatVolume(argv[2], wipe); 176 } else if (!strcmp(argv[1], "share")) { 177 if (argc != 4) { 178 cli->sendMsg(ResponseCode::CommandSyntaxError, 179 "Usage: volume share <path> <method>", false); 180 return 0; 181 } 182 rc = vm->shareVolume(argv[2], argv[3]); 183 } else if (!strcmp(argv[1], "unshare")) { 184 if (argc != 4) { 185 cli->sendMsg(ResponseCode::CommandSyntaxError, 186 "Usage: volume unshare <path> <method>", false); 187 return 0; 188 } 189 rc = vm->unshareVolume(argv[2], argv[3]); 190 } else if (!strcmp(argv[1], "shared")) { 191 bool enabled = false; 192 if (argc != 4) { 193 cli->sendMsg(ResponseCode::CommandSyntaxError, 194 "Usage: volume shared <path> <method>", false); 195 return 0; 196 } 197 198 if (vm->shareEnabled(argv[2], argv[3], &enabled)) { 199 cli->sendMsg( 200 ResponseCode::OperationFailed, "Failed to determine share enable state", true); 201 } else { 202 cli->sendMsg(ResponseCode::ShareEnabledResult, 203 (enabled ? "Share enabled" : "Share disabled"), false); 204 } 205 return 0; 206 } else if (!strcmp(argv[1], "mkdirs")) { 207 if (argc != 3) { 208 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mkdirs <path>", false); 209 return 0; 210 } 211 rc = vm->mkdirs(argv[2]); 212 } else { 213 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false); 214 } 215 216 if (!rc) { 217 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false); 218 } else { 219 int erno = errno; 220 rc = ResponseCode::convertFromErrno(); 221 cli->sendMsg(rc, "volume operation failed", true); 222 } 223 224 return 0; 225} 226 227CommandListener::StorageCmd::StorageCmd() : 228 VoldCommand("storage") { 229} 230 231int CommandListener::StorageCmd::runCommand(SocketClient *cli, 232 int argc, char **argv) { 233 /* Guarantied to be initialized by vold's main() before the CommandListener is active */ 234 extern struct fstab *fstab; 235 236 dumpArgs(argc, argv, -1); 237 238 if (argc < 2) { 239 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 240 return 0; 241 } 242 243 if (!strcmp(argv[1], "mountall")) { 244 if (argc != 2) { 245 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false); 246 return 0; 247 } 248 fs_mgr_mount_all(fstab); 249 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false); 250 return 0; 251 } 252 if (!strcmp(argv[1], "users")) { 253 DIR *dir; 254 struct dirent *de; 255 256 if (argc < 3) { 257 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false); 258 return 0; 259 } 260 if (!(dir = opendir("/proc"))) { 261 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true); 262 return 0; 263 } 264 265 while ((de = readdir(dir))) { 266 int pid = Process::getPid(de->d_name); 267 268 if (pid < 0) { 269 continue; 270 } 271 272 char processName[255]; 273 Process::getProcessName(pid, processName, sizeof(processName)); 274 275 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) || 276 Process::checkFileMaps(pid, argv[2]) || 277 Process::checkSymLink(pid, argv[2], "cwd") || 278 Process::checkSymLink(pid, argv[2], "root") || 279 Process::checkSymLink(pid, argv[2], "exe")) { 280 281 char msg[1024]; 282 snprintf(msg, sizeof(msg), "%d %s", pid, processName); 283 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false); 284 } 285 } 286 closedir(dir); 287 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false); 288 } else { 289 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); 290 } 291 return 0; 292} 293 294CommandListener::AsecCmd::AsecCmd() : 295 VoldCommand("asec") { 296} 297 298void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) { 299 DIR *d = opendir(directory); 300 301 if (!d) { 302 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true); 303 return; 304 } 305 306 size_t dirent_len = offsetof(struct dirent, d_name) + 307 fpathconf(dirfd(d), _PC_NAME_MAX) + 1; 308 309 struct dirent *dent = (struct dirent *) malloc(dirent_len); 310 if (dent == NULL) { 311 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true); 312 return; 313 } 314 315 struct dirent *result; 316 317 while (!readdir_r(d, dent, &result) && result != NULL) { 318 if (dent->d_name[0] == '.') 319 continue; 320 if (dent->d_type != DT_REG) 321 continue; 322 size_t name_len = strlen(dent->d_name); 323 if (name_len > 5 && name_len < 260 && 324 !strcmp(&dent->d_name[name_len - 5], ".asec")) { 325 char id[255]; 326 memset(id, 0, sizeof(id)); 327 strlcpy(id, dent->d_name, name_len - 4); 328 cli->sendMsg(ResponseCode::AsecListResult, id, false); 329 } 330 } 331 closedir(d); 332 333 free(dent); 334} 335 336int CommandListener::AsecCmd::runCommand(SocketClient *cli, 337 int argc, char **argv) { 338 if (argc < 2) { 339 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 340 return 0; 341 } 342 343 VolumeManager *vm = VolumeManager::Instance(); 344 int rc = 0; 345 346 if (!strcmp(argv[1], "list")) { 347 dumpArgs(argc, argv, -1); 348 349 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT); 350 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT); 351 } else if (!strcmp(argv[1], "create")) { 352 dumpArgs(argc, argv, 5); 353 if (argc != 8) { 354 cli->sendMsg(ResponseCode::CommandSyntaxError, 355 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> " 356 "<isExternal>", false); 357 return 0; 358 } 359 360 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 361 const bool isExternal = (atoi(argv[7]) == 1); 362 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); 363 } else if (!strcmp(argv[1], "resize")) { 364 dumpArgs(argc, argv, -1); 365 if (argc != 5) { 366 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false); 367 return 0; 368 } 369 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 370 rc = vm->resizeAsec(argv[2], numSectors, argv[4]); 371 } else if (!strcmp(argv[1], "finalize")) { 372 dumpArgs(argc, argv, -1); 373 if (argc != 3) { 374 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); 375 return 0; 376 } 377 rc = vm->finalizeAsec(argv[2]); 378 } else if (!strcmp(argv[1], "fixperms")) { 379 dumpArgs(argc, argv, -1); 380 if (argc != 5) { 381 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 382 return 0; 383 } 384 385 char *endptr; 386 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10); 387 if (*endptr != '\0') { 388 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 389 return 0; 390 } 391 392 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]); 393 } else if (!strcmp(argv[1], "destroy")) { 394 dumpArgs(argc, argv, -1); 395 if (argc < 3) { 396 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false); 397 return 0; 398 } 399 bool force = false; 400 if (argc > 3 && !strcmp(argv[3], "force")) { 401 force = true; 402 } 403 rc = vm->destroyAsec(argv[2], force); 404 } else if (!strcmp(argv[1], "mount")) { 405 dumpArgs(argc, argv, 3); 406 if (argc != 5) { 407 cli->sendMsg(ResponseCode::CommandSyntaxError, 408 "Usage: asec mount <namespace-id> <key> <ownerUid>", false); 409 return 0; 410 } 411 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4])); 412 } else if (!strcmp(argv[1], "unmount")) { 413 dumpArgs(argc, argv, -1); 414 if (argc < 3) { 415 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); 416 return 0; 417 } 418 bool force = false; 419 if (argc > 3 && !strcmp(argv[3], "force")) { 420 force = true; 421 } 422 rc = vm->unmountAsec(argv[2], force); 423 } else if (!strcmp(argv[1], "rename")) { 424 dumpArgs(argc, argv, -1); 425 if (argc != 4) { 426 cli->sendMsg(ResponseCode::CommandSyntaxError, 427 "Usage: asec rename <old_id> <new_id>", false); 428 return 0; 429 } 430 rc = vm->renameAsec(argv[2], argv[3]); 431 } else if (!strcmp(argv[1], "path")) { 432 dumpArgs(argc, argv, -1); 433 if (argc != 3) { 434 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); 435 return 0; 436 } 437 char path[255]; 438 439 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) { 440 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 441 return 0; 442 } 443 } else if (!strcmp(argv[1], "fspath")) { 444 dumpArgs(argc, argv, -1); 445 if (argc != 3) { 446 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false); 447 return 0; 448 } 449 char path[255]; 450 451 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) { 452 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 453 return 0; 454 } 455 } else { 456 dumpArgs(argc, argv, -1); 457 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); 458 } 459 460 if (!rc) { 461 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false); 462 } else { 463 rc = ResponseCode::convertFromErrno(); 464 cli->sendMsg(rc, "asec operation failed", true); 465 } 466 467 return 0; 468} 469 470CommandListener::ObbCmd::ObbCmd() : 471 VoldCommand("obb") { 472} 473 474int CommandListener::ObbCmd::runCommand(SocketClient *cli, 475 int argc, char **argv) { 476 if (argc < 2) { 477 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 478 return 0; 479 } 480 481 VolumeManager *vm = VolumeManager::Instance(); 482 int rc = 0; 483 484 if (!strcmp(argv[1], "list")) { 485 dumpArgs(argc, argv, -1); 486 487 rc = vm->listMountedObbs(cli); 488 } else if (!strcmp(argv[1], "mount")) { 489 dumpArgs(argc, argv, 3); 490 if (argc != 5) { 491 cli->sendMsg(ResponseCode::CommandSyntaxError, 492 "Usage: obb mount <filename> <key> <ownerGid>", false); 493 return 0; 494 } 495 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4])); 496 } else if (!strcmp(argv[1], "unmount")) { 497 dumpArgs(argc, argv, -1); 498 if (argc < 3) { 499 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false); 500 return 0; 501 } 502 bool force = false; 503 if (argc > 3 && !strcmp(argv[3], "force")) { 504 force = true; 505 } 506 rc = vm->unmountObb(argv[2], force); 507 } else if (!strcmp(argv[1], "path")) { 508 dumpArgs(argc, argv, -1); 509 if (argc != 3) { 510 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false); 511 return 0; 512 } 513 char path[255]; 514 515 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) { 516 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 517 return 0; 518 } 519 } else { 520 dumpArgs(argc, argv, -1); 521 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false); 522 } 523 524 if (!rc) { 525 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false); 526 } else { 527 rc = ResponseCode::convertFromErrno(); 528 cli->sendMsg(rc, "obb operation failed", true); 529 } 530 531 return 0; 532} 533 534CommandListener::CryptfsCmd::CryptfsCmd() : 535 VoldCommand("cryptfs") { 536} 537 538static int getType(const char* type) 539{ 540 if (!strcmp(type, "default")) { 541 return CRYPT_TYPE_DEFAULT; 542 } else if (!strcmp(type, "password")) { 543 return CRYPT_TYPE_PASSWORD; 544 } else if (!strcmp(type, "pin")) { 545 return CRYPT_TYPE_PIN; 546 } else if (!strcmp(type, "pattern")) { 547 return CRYPT_TYPE_PATTERN; 548 } else { 549 return -1; 550 } 551} 552 553int CommandListener::CryptfsCmd::runCommand(SocketClient *cli, 554 int argc, char **argv) { 555 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 556 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false); 557 return 0; 558 } 559 560 if (argc < 2) { 561 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 562 return 0; 563 } 564 565 int rc = 0; 566 567 if (!strcmp(argv[1], "checkpw")) { 568 if (argc != 3) { 569 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false); 570 return 0; 571 } 572 dumpArgs(argc, argv, 2); 573 rc = cryptfs_check_passwd(argv[2]); 574 } else if (!strcmp(argv[1], "restart")) { 575 if (argc != 2) { 576 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false); 577 return 0; 578 } 579 dumpArgs(argc, argv, -1); 580 rc = cryptfs_restart(); 581 } else if (!strcmp(argv[1], "cryptocomplete")) { 582 if (argc != 2) { 583 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false); 584 return 0; 585 } 586 dumpArgs(argc, argv, -1); 587 rc = cryptfs_crypto_complete(); 588 } else if (!strcmp(argv[1], "enablecrypto")) { 589 const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> " 590 "default|password|pin|pattern [passwd]"; 591 if ( (argc != 4 && argc != 5) 592 || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) { 593 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 594 return 0; 595 } 596 dumpArgs(argc, argv, 4); 597 598 int tries; 599 for (tries = 0; tries < 2; ++tries) { 600 int type = getType(argv[3]); 601 if (type == -1) { 602 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, 603 false); 604 return 0; 605 } else if (type == CRYPT_TYPE_DEFAULT) { 606 rc = cryptfs_enable_default(argv[2], /*allow_reboot*/false); 607 } else { 608 rc = cryptfs_enable(argv[2], type, argv[4], 609 /*allow_reboot*/false); 610 } 611 612 if (rc == 0) { 613 break; 614 } else if (tries == 0) { 615 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, 2); 616 } 617 } 618 } else if (!strcmp(argv[1], "changepw")) { 619 const char* syntax = "Usage: cryptfs changepw " 620 "default|password|pin|pattern [newpasswd]"; 621 const char* password; 622 if (argc == 3) { 623 password = ""; 624 } else if (argc == 4) { 625 password = argv[3]; 626 } else { 627 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 628 return 0; 629 } 630 int type = getType(argv[2]); 631 if (type == -1) { 632 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 633 return 0; 634 } 635 SLOGD("cryptfs changepw %s {}", argv[2]); 636 rc = cryptfs_changepw(type, password); 637 } else if (!strcmp(argv[1], "verifypw")) { 638 if (argc != 3) { 639 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false); 640 return 0; 641 } 642 SLOGD("cryptfs verifypw {}"); 643 rc = cryptfs_verify_passwd(argv[2]); 644 } else if (!strcmp(argv[1], "getfield")) { 645 char valbuf[PROPERTY_VALUE_MAX]; 646 647 if (argc != 3) { 648 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false); 649 return 0; 650 } 651 dumpArgs(argc, argv, -1); 652 rc = cryptfs_getfield(argv[2], valbuf, sizeof(valbuf)); 653 if (rc == 0) { 654 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false); 655 } 656 } else if (!strcmp(argv[1], "setfield")) { 657 if (argc != 4) { 658 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false); 659 return 0; 660 } 661 dumpArgs(argc, argv, -1); 662 rc = cryptfs_setfield(argv[2], argv[3]); 663 } else if (!strcmp(argv[1], "mountdefaultencrypted")) { 664 SLOGD("cryptfs mountdefaultencrypted"); 665 dumpArgs(argc, argv, -1); 666 rc = cryptfs_mount_default_encrypted(); 667 } else if (!strcmp(argv[1], "getpwtype")) { 668 SLOGD("cryptfs getpwtype"); 669 dumpArgs(argc, argv, -1); 670 switch(cryptfs_get_password_type()) { 671 case CRYPT_TYPE_PASSWORD: 672 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false); 673 return 0; 674 case CRYPT_TYPE_PATTERN: 675 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false); 676 return 0; 677 case CRYPT_TYPE_PIN: 678 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false); 679 return 0; 680 case CRYPT_TYPE_DEFAULT: 681 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false); 682 return 0; 683 default: 684 /** @TODO better error and make sure handled by callers */ 685 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false); 686 return 0; 687 } 688 } else if (!strcmp(argv[1], "getpw")) { 689 SLOGD("cryptfs getpw"); 690 dumpArgs(argc, argv, -1); 691 char* password = cryptfs_get_password(); 692 if (password) { 693 cli->sendMsg(ResponseCode::CommandOkay, password, false); 694 return 0; 695 } 696 rc = -1; 697 } else if (!strcmp(argv[1], "clearpw")) { 698 SLOGD("cryptfs clearpw"); 699 dumpArgs(argc, argv, -1); 700 cryptfs_clear_password(); 701 rc = 0; 702 } else { 703 dumpArgs(argc, argv, -1); 704 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false); 705 return 0; 706 } 707 708 // Always report that the command succeeded and return the error code. 709 // The caller will check the return value to see what the error was. 710 char msg[255]; 711 snprintf(msg, sizeof(msg), "%d", rc); 712 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 713 714 return 0; 715} 716 717CommandListener::FstrimCmd::FstrimCmd() : 718 VoldCommand("fstrim") { 719} 720int CommandListener::FstrimCmd::runCommand(SocketClient *cli, 721 int argc, char **argv) { 722 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 723 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); 724 return 0; 725 } 726 727 if (argc < 2) { 728 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 729 return 0; 730 } 731 732 int rc = 0; 733 734 if (!strcmp(argv[1], "dotrim")) { 735 if (argc != 2) { 736 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false); 737 return 0; 738 } 739 dumpArgs(argc, argv, -1); 740 rc = fstrim_filesystems(0); 741 } else if (!strcmp(argv[1], "dodtrim")) { 742 if (argc != 2) { 743 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false); 744 return 0; 745 } 746 dumpArgs(argc, argv, -1); 747 rc = fstrim_filesystems(1); /* Do Deep Discard trim */ 748 } else { 749 dumpArgs(argc, argv, -1); 750 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false); 751 } 752 753 // Always report that the command succeeded and return the error code. 754 // The caller will check the return value to see what the error was. 755 char msg[255]; 756 snprintf(msg, sizeof(msg), "%d", rc); 757 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 758 759 return 0; 760} 761