CommandListener.cpp revision 45f10533f8cb2e2ec8dc9803739870cbfafffebd
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 529static int getType(const char* type) 530{ 531 if (!strcmp(type, "default")) { 532 return CRYPT_TYPE_DEFAULT; 533 } else if (!strcmp(type, "password")) { 534 return CRYPT_TYPE_PASSWORD; 535 } else if (!strcmp(type, "pin")) { 536 return CRYPT_TYPE_PIN; 537 } else if (!strcmp(type, "pattern")) { 538 return CRYPT_TYPE_PATTERN; 539 } else { 540 return -1; 541 } 542} 543 544int CommandListener::CryptfsCmd::runCommand(SocketClient *cli, 545 int argc, char **argv) { 546 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 547 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false); 548 return 0; 549 } 550 551 if (argc < 2) { 552 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 553 return 0; 554 } 555 556 int rc = 0; 557 558 if (!strcmp(argv[1], "checkpw")) { 559 if (argc != 3) { 560 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false); 561 return 0; 562 } 563 dumpArgs(argc, argv, 2); 564 rc = cryptfs_check_passwd(argv[2]); 565 } else if (!strcmp(argv[1], "restart")) { 566 if (argc != 2) { 567 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false); 568 return 0; 569 } 570 dumpArgs(argc, argv, -1); 571 rc = cryptfs_restart(); 572 } else if (!strcmp(argv[1], "cryptocomplete")) { 573 if (argc != 2) { 574 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false); 575 return 0; 576 } 577 dumpArgs(argc, argv, -1); 578 rc = cryptfs_crypto_complete(); 579 } else if (!strcmp(argv[1], "enablecrypto")) { 580 const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> " 581 "default|password|pin|pattern [passwd]"; 582 if ( (argc != 4 && argc != 5) 583 || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) { 584 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 585 return 0; 586 } 587 dumpArgs(argc, argv, 4); 588 589 int tries; 590 for (tries = 0; tries < 2; ++tries) { 591 int type = getType(argv[3]); 592 if (type == -1) { 593 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, 594 false); 595 return 0; 596 } else if (type == CRYPT_TYPE_DEFAULT) { 597 rc = cryptfs_enable_default(argv[2], /*allow_reboot*/false); 598 } else { 599 rc = cryptfs_enable(argv[2], type, argv[4], 600 /*allow_reboot*/false); 601 } 602 603 if (rc == 0) { 604 break; 605 } else if (tries == 0) { 606 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, 2); 607 } 608 } 609 } else if (!strcmp(argv[1], "changepw")) { 610 const char* syntax = "Usage: cryptfs changepw " 611 "default|password|pin|pattern [newpasswd]"; 612 const char* password; 613 if (argc == 3) { 614 password = ""; 615 } else if (argc == 4) { 616 password = argv[3]; 617 } else { 618 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 619 return 0; 620 } 621 int type = getType(argv[2]); 622 if (type == -1) { 623 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 624 return 0; 625 } 626 SLOGD("cryptfs changepw %s {}", argv[2]); 627 rc = cryptfs_changepw(type, password); 628 } else if (!strcmp(argv[1], "verifypw")) { 629 if (argc != 3) { 630 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false); 631 return 0; 632 } 633 SLOGD("cryptfs verifypw {}"); 634 rc = cryptfs_verify_passwd(argv[2]); 635 } else if (!strcmp(argv[1], "getfield")) { 636 char valbuf[PROPERTY_VALUE_MAX]; 637 638 if (argc != 3) { 639 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false); 640 return 0; 641 } 642 dumpArgs(argc, argv, -1); 643 rc = cryptfs_getfield(argv[2], valbuf, sizeof(valbuf)); 644 if (rc == 0) { 645 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false); 646 } 647 } else if (!strcmp(argv[1], "setfield")) { 648 if (argc != 4) { 649 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false); 650 return 0; 651 } 652 dumpArgs(argc, argv, -1); 653 rc = cryptfs_setfield(argv[2], argv[3]); 654 } else if (!strcmp(argv[1], "mountdefaultencrypted")) { 655 SLOGD("cryptfs mountdefaultencrypted"); 656 dumpArgs(argc, argv, -1); 657 rc = cryptfs_mount_default_encrypted(); 658 } else if (!strcmp(argv[1], "getpwtype")) { 659 SLOGD("cryptfs getpwtype"); 660 dumpArgs(argc, argv, -1); 661 switch(cryptfs_get_password_type()) { 662 case CRYPT_TYPE_PASSWORD: 663 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false); 664 return 0; 665 case CRYPT_TYPE_PATTERN: 666 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false); 667 return 0; 668 case CRYPT_TYPE_PIN: 669 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false); 670 return 0; 671 case CRYPT_TYPE_DEFAULT: 672 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false); 673 return 0; 674 default: 675 /** @TODO better error and make sure handled by callers */ 676 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false); 677 return 0; 678 } 679 } else if (!strcmp(argv[1], "getpw")) { 680 SLOGD("cryptfs getpw"); 681 dumpArgs(argc, argv, -1); 682 char* password = cryptfs_get_password(); 683 if (password) { 684 cli->sendMsg(ResponseCode::CommandOkay, password, false); 685 return 0; 686 } 687 rc = -1; 688 } else if (!strcmp(argv[1], "clearpw")) { 689 SLOGD("cryptfs clearpw"); 690 dumpArgs(argc, argv, -1); 691 cryptfs_clear_password(); 692 rc = 0; 693 } else { 694 dumpArgs(argc, argv, -1); 695 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false); 696 return 0; 697 } 698 699 // Always report that the command succeeded and return the error code. 700 // The caller will check the return value to see what the error was. 701 char msg[255]; 702 snprintf(msg, sizeof(msg), "%d", rc); 703 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 704 705 return 0; 706} 707 708CommandListener::FstrimCmd::FstrimCmd() : 709 VoldCommand("fstrim") { 710} 711int CommandListener::FstrimCmd::runCommand(SocketClient *cli, 712 int argc, char **argv) { 713 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 714 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); 715 return 0; 716 } 717 718 if (argc < 2) { 719 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 720 return 0; 721 } 722 723 int rc = 0; 724 725 if (!strcmp(argv[1], "dotrim")) { 726 if (argc != 2) { 727 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false); 728 return 0; 729 } 730 dumpArgs(argc, argv, -1); 731 rc = fstrim_filesystems(); 732 } else { 733 dumpArgs(argc, argv, -1); 734 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false); 735 } 736 737 // Always report that the command succeeded and return the error code. 738 // The caller will check the return value to see what the error was. 739 char msg[255]; 740 snprintf(msg, sizeof(msg), "%d", rc); 741 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 742 743 return 0; 744} 745