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