CommandListener.cpp revision d0b4295ccc07d0cd715ade415c8c0d7d6945880e
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 <stdio.h> 27#include <string.h> 28#include <stdint.h> 29#include <inttypes.h> 30 31#define LOG_TAG "VoldCmdListener" 32 33#include <base/stringprintf.h> 34#include <cutils/fs.h> 35#include <cutils/log.h> 36 37#include <sysutils/SocketClient.h> 38#include <private/android_filesystem_config.h> 39 40#include "CommandListener.h" 41#include "VolumeManager.h" 42#include "VolumeBase.h" 43#include "ResponseCode.h" 44#include "Process.h" 45#include "Loop.h" 46#include "Devmapper.h" 47#include "cryptfs.h" 48#include "fstrim.h" 49#include "MoveTask.h" 50 51#define DUMP_ARGS 0 52 53CommandListener::CommandListener() : 54 FrameworkListener("vold", true) { 55 registerCmd(new DumpCmd()); 56 registerCmd(new VolumeCmd()); 57 registerCmd(new AsecCmd()); 58 registerCmd(new ObbCmd()); 59 registerCmd(new StorageCmd()); 60 registerCmd(new FstrimCmd()); 61} 62 63#if DUMP_ARGS 64void CommandListener::dumpArgs(int argc, char **argv, int argObscure) { 65 char buffer[4096]; 66 char *p = buffer; 67 68 memset(buffer, 0, sizeof(buffer)); 69 int i; 70 for (i = 0; i < argc; i++) { 71 unsigned int len = strlen(argv[i]) + 1; // Account for space 72 if (i == argObscure) { 73 len += 2; // Account for {} 74 } 75 if (((p - buffer) + len) < (sizeof(buffer)-1)) { 76 if (i == argObscure) { 77 *p++ = '{'; 78 *p++ = '}'; 79 *p++ = ' '; 80 continue; 81 } 82 strcpy(p, argv[i]); 83 p+= strlen(argv[i]); 84 if (i != (argc -1)) { 85 *p++ = ' '; 86 } 87 } 88 } 89 SLOGD("%s", buffer); 90} 91#else 92void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { } 93#endif 94 95int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) { 96 if (!cond) { 97 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false); 98 } else { 99 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false); 100 } 101} 102 103CommandListener::DumpCmd::DumpCmd() : 104 VoldCommand("dump") { 105} 106 107int CommandListener::DumpCmd::runCommand(SocketClient *cli, 108 int /*argc*/, char ** /*argv*/) { 109 cli->sendMsg(0, "Dumping loop status", false); 110 if (Loop::dumpState(cli)) { 111 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true); 112 } 113 cli->sendMsg(0, "Dumping DM status", false); 114 if (Devmapper::dumpState(cli)) { 115 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true); 116 } 117 cli->sendMsg(0, "Dumping mounted filesystems", false); 118 FILE *fp = fopen("/proc/mounts", "r"); 119 if (fp) { 120 char line[1024]; 121 while (fgets(line, sizeof(line), fp)) { 122 line[strlen(line)-1] = '\0'; 123 cli->sendMsg(0, line, false);; 124 } 125 fclose(fp); 126 } 127 128 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false); 129 return 0; 130} 131 132CommandListener::VolumeCmd::VolumeCmd() : 133 VoldCommand("volume") { 134} 135 136int CommandListener::VolumeCmd::runCommand(SocketClient *cli, 137 int argc, char **argv) { 138 dumpArgs(argc, argv, -1); 139 140 if (argc < 2) { 141 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 142 return 0; 143 } 144 145 VolumeManager *vm = VolumeManager::Instance(); 146 std::lock_guard<std::mutex> lock(vm->getLock()); 147 148 // TODO: tease out methods not directly related to volumes 149 150 std::string cmd(argv[1]); 151 if (cmd == "reset") { 152 return sendGenericOkFail(cli, vm->reset()); 153 154 } else if (cmd == "shutdown") { 155 return sendGenericOkFail(cli, vm->shutdown()); 156 157 } else if (cmd == "debug") { 158 return sendGenericOkFail(cli, vm->setDebug(true)); 159 160 } else if (cmd == "partition" && argc > 3) { 161 // partition [diskId] [public|private|mixed] [ratio] 162 std::string id(argv[2]); 163 auto disk = vm->findDisk(id); 164 if (disk == nullptr) { 165 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false); 166 } 167 168 std::string type(argv[3]); 169 if (type == "public") { 170 return sendGenericOkFail(cli, disk->partitionPublic()); 171 } else if (type == "private") { 172 return sendGenericOkFail(cli, disk->partitionPrivate()); 173 } else if (type == "mixed") { 174 if (argc < 4) { 175 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 176 } 177 int frac = atoi(argv[4]); 178 return sendGenericOkFail(cli, disk->partitionMixed(frac)); 179 } else { 180 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 181 } 182 183 } else if (cmd == "mkdirs" && argc > 2) { 184 // mkdirs [path] 185 return sendGenericOkFail(cli, vm->mkdirs(argv[2])); 186 187 } else if (cmd == "start_user" && argc > 2) { 188 // start_user [user] 189 return sendGenericOkFail(cli, vm->startUser(atoi(argv[2]))); 190 191 } else if (cmd == "cleanup_user" && argc > 2) { 192 // cleanup_user [user] 193 return sendGenericOkFail(cli, vm->cleanupUser(atoi(argv[2]))); 194 195 } else if (cmd == "mount" && argc > 2) { 196 // mount [volId] [flags] [user] 197 std::string id(argv[2]); 198 auto vol = vm->findVolume(id); 199 if (vol == nullptr) { 200 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 201 } 202 203 int mountFlags = (argc > 3) ? atoi(argv[3]) : 0; 204 userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1; 205 206 vol->setMountFlags(mountFlags); 207 vol->setMountUserId(mountUserId); 208 209 int res = vol->mount(); 210 if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) { 211 vm->setPrimary(vol); 212 } 213 return sendGenericOkFail(cli, res); 214 215 } else if (cmd == "unmount" && argc > 2) { 216 // unmount [volId] 217 std::string id(argv[2]); 218 auto vol = vm->findVolume(id); 219 if (vol == nullptr) { 220 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 221 } 222 223 return sendGenericOkFail(cli, vol->unmount()); 224 225 } else if (cmd == "format" && argc > 2) { 226 // format [volId] 227 std::string id(argv[2]); 228 auto vol = vm->findVolume(id); 229 if (vol == nullptr) { 230 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 231 } 232 233 return sendGenericOkFail(cli, vol->format()); 234 235 } else if (cmd == "move_storage" && argc > 3) { 236 // move_storage [fromVolId] [toVolId] 237 auto fromVol = vm->findVolume(std::string(argv[2])); 238 auto toVol = vm->findVolume(std::string(argv[3])); 239 if (fromVol == nullptr || toVol == nullptr) { 240 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 241 } 242 243 (new android::vold::MoveTask(fromVol, toVol))->start(); 244 return sendGenericOkFail(cli, 0); 245 246 } else if (cmd == "benchmark" && argc > 2) { 247 // benchmark [volId] 248 std::string id(argv[2]); 249 nsecs_t res = vm->benchmarkVolume(id); 250 return cli->sendMsg(ResponseCode::CommandOkay, 251 android::base::StringPrintf("%" PRId64, res).c_str(), false); 252 } 253 254 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 255} 256 257CommandListener::StorageCmd::StorageCmd() : 258 VoldCommand("storage") { 259} 260 261int CommandListener::StorageCmd::runCommand(SocketClient *cli, 262 int argc, char **argv) { 263 /* Guarantied to be initialized by vold's main() before the CommandListener is active */ 264 extern struct fstab *fstab; 265 266 dumpArgs(argc, argv, -1); 267 268 if (argc < 2) { 269 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 270 return 0; 271 } 272 273 if (!strcmp(argv[1], "mountall")) { 274 if (argc != 2) { 275 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false); 276 return 0; 277 } 278 fs_mgr_mount_all(fstab); 279 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false); 280 return 0; 281 } 282 if (!strcmp(argv[1], "users")) { 283 DIR *dir; 284 struct dirent *de; 285 286 if (argc < 3) { 287 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false); 288 return 0; 289 } 290 if (!(dir = opendir("/proc"))) { 291 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true); 292 return 0; 293 } 294 295 while ((de = readdir(dir))) { 296 int pid = Process::getPid(de->d_name); 297 298 if (pid < 0) { 299 continue; 300 } 301 302 char processName[255]; 303 Process::getProcessName(pid, processName, sizeof(processName)); 304 305 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) || 306 Process::checkFileMaps(pid, argv[2]) || 307 Process::checkSymLink(pid, argv[2], "cwd") || 308 Process::checkSymLink(pid, argv[2], "root") || 309 Process::checkSymLink(pid, argv[2], "exe")) { 310 311 char msg[1024]; 312 snprintf(msg, sizeof(msg), "%d %s", pid, processName); 313 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false); 314 } 315 } 316 closedir(dir); 317 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false); 318 } else { 319 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); 320 } 321 return 0; 322} 323 324CommandListener::AsecCmd::AsecCmd() : 325 VoldCommand("asec") { 326} 327 328void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) { 329 DIR *d = opendir(directory); 330 331 if (!d) { 332 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true); 333 return; 334 } 335 336 size_t dirent_len = offsetof(struct dirent, d_name) + 337 fpathconf(dirfd(d), _PC_NAME_MAX) + 1; 338 339 struct dirent *dent = (struct dirent *) malloc(dirent_len); 340 if (dent == NULL) { 341 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true); 342 return; 343 } 344 345 struct dirent *result; 346 347 while (!readdir_r(d, dent, &result) && result != NULL) { 348 if (dent->d_name[0] == '.') 349 continue; 350 if (dent->d_type != DT_REG) 351 continue; 352 size_t name_len = strlen(dent->d_name); 353 if (name_len > 5 && name_len < 260 && 354 !strcmp(&dent->d_name[name_len - 5], ".asec")) { 355 char id[255]; 356 memset(id, 0, sizeof(id)); 357 strlcpy(id, dent->d_name, name_len - 4); 358 cli->sendMsg(ResponseCode::AsecListResult, id, false); 359 } 360 } 361 closedir(d); 362 363 free(dent); 364} 365 366int CommandListener::AsecCmd::runCommand(SocketClient *cli, 367 int argc, char **argv) { 368 if (argc < 2) { 369 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 370 return 0; 371 } 372 373 VolumeManager *vm = VolumeManager::Instance(); 374 int rc = 0; 375 376 if (!strcmp(argv[1], "list")) { 377 dumpArgs(argc, argv, -1); 378 379 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT); 380 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT); 381 } else if (!strcmp(argv[1], "create")) { 382 dumpArgs(argc, argv, 5); 383 if (argc != 8) { 384 cli->sendMsg(ResponseCode::CommandSyntaxError, 385 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> " 386 "<isExternal>", false); 387 return 0; 388 } 389 390 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 391 const bool isExternal = (atoi(argv[7]) == 1); 392 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); 393 } else if (!strcmp(argv[1], "resize")) { 394 dumpArgs(argc, argv, -1); 395 if (argc != 5) { 396 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false); 397 return 0; 398 } 399 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 400 rc = vm->resizeAsec(argv[2], numSectors, argv[4]); 401 } else if (!strcmp(argv[1], "finalize")) { 402 dumpArgs(argc, argv, -1); 403 if (argc != 3) { 404 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); 405 return 0; 406 } 407 rc = vm->finalizeAsec(argv[2]); 408 } else if (!strcmp(argv[1], "fixperms")) { 409 dumpArgs(argc, argv, -1); 410 if (argc != 5) { 411 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 412 return 0; 413 } 414 415 char *endptr; 416 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10); 417 if (*endptr != '\0') { 418 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 419 return 0; 420 } 421 422 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]); 423 } else if (!strcmp(argv[1], "destroy")) { 424 dumpArgs(argc, argv, -1); 425 if (argc < 3) { 426 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false); 427 return 0; 428 } 429 bool force = false; 430 if (argc > 3 && !strcmp(argv[3], "force")) { 431 force = true; 432 } 433 rc = vm->destroyAsec(argv[2], force); 434 } else if (!strcmp(argv[1], "mount")) { 435 dumpArgs(argc, argv, 3); 436 if (argc != 6) { 437 cli->sendMsg(ResponseCode::CommandSyntaxError, 438 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false); 439 return 0; 440 } 441 bool readOnly = true; 442 if (!strcmp(argv[5], "rw")) { 443 readOnly = false; 444 } 445 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly); 446 } else if (!strcmp(argv[1], "unmount")) { 447 dumpArgs(argc, argv, -1); 448 if (argc < 3) { 449 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); 450 return 0; 451 } 452 bool force = false; 453 if (argc > 3 && !strcmp(argv[3], "force")) { 454 force = true; 455 } 456 rc = vm->unmountAsec(argv[2], force); 457 } else if (!strcmp(argv[1], "rename")) { 458 dumpArgs(argc, argv, -1); 459 if (argc != 4) { 460 cli->sendMsg(ResponseCode::CommandSyntaxError, 461 "Usage: asec rename <old_id> <new_id>", false); 462 return 0; 463 } 464 rc = vm->renameAsec(argv[2], argv[3]); 465 } else if (!strcmp(argv[1], "path")) { 466 dumpArgs(argc, argv, -1); 467 if (argc != 3) { 468 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); 469 return 0; 470 } 471 char path[255]; 472 473 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) { 474 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 475 return 0; 476 } 477 } else if (!strcmp(argv[1], "fspath")) { 478 dumpArgs(argc, argv, -1); 479 if (argc != 3) { 480 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false); 481 return 0; 482 } 483 char path[255]; 484 485 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) { 486 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 487 return 0; 488 } 489 } else { 490 dumpArgs(argc, argv, -1); 491 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); 492 } 493 494 if (!rc) { 495 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false); 496 } else { 497 rc = ResponseCode::convertFromErrno(); 498 cli->sendMsg(rc, "asec operation failed", true); 499 } 500 501 return 0; 502} 503 504CommandListener::ObbCmd::ObbCmd() : 505 VoldCommand("obb") { 506} 507 508int CommandListener::ObbCmd::runCommand(SocketClient *cli, 509 int argc, char **argv) { 510 if (argc < 2) { 511 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 512 return 0; 513 } 514 515 VolumeManager *vm = VolumeManager::Instance(); 516 int rc = 0; 517 518 if (!strcmp(argv[1], "list")) { 519 dumpArgs(argc, argv, -1); 520 521 rc = vm->listMountedObbs(cli); 522 } else if (!strcmp(argv[1], "mount")) { 523 dumpArgs(argc, argv, 3); 524 if (argc != 5) { 525 cli->sendMsg(ResponseCode::CommandSyntaxError, 526 "Usage: obb mount <filename> <key> <ownerGid>", false); 527 return 0; 528 } 529 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4])); 530 } else if (!strcmp(argv[1], "unmount")) { 531 dumpArgs(argc, argv, -1); 532 if (argc < 3) { 533 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false); 534 return 0; 535 } 536 bool force = false; 537 if (argc > 3 && !strcmp(argv[3], "force")) { 538 force = true; 539 } 540 rc = vm->unmountObb(argv[2], force); 541 } else if (!strcmp(argv[1], "path")) { 542 dumpArgs(argc, argv, -1); 543 if (argc != 3) { 544 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false); 545 return 0; 546 } 547 char path[255]; 548 549 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) { 550 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 551 return 0; 552 } 553 } else { 554 dumpArgs(argc, argv, -1); 555 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false); 556 } 557 558 if (!rc) { 559 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false); 560 } else { 561 rc = ResponseCode::convertFromErrno(); 562 cli->sendMsg(rc, "obb operation failed", true); 563 } 564 565 return 0; 566} 567 568CommandListener::FstrimCmd::FstrimCmd() : 569 VoldCommand("fstrim") { 570} 571int CommandListener::FstrimCmd::runCommand(SocketClient *cli, 572 int argc, char **argv) { 573 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 574 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); 575 return 0; 576 } 577 578 if (argc < 2) { 579 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 580 return 0; 581 } 582 583 int rc = 0; 584 585 if (!strcmp(argv[1], "dotrim")) { 586 if (argc != 2) { 587 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false); 588 return 0; 589 } 590 dumpArgs(argc, argv, -1); 591 rc = fstrim_filesystems(0); 592 } else if (!strcmp(argv[1], "dodtrim")) { 593 if (argc != 2) { 594 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false); 595 return 0; 596 } 597 dumpArgs(argc, argv, -1); 598 rc = fstrim_filesystems(1); /* Do Deep Discard trim */ 599 } else { 600 dumpArgs(argc, argv, -1); 601 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false); 602 } 603 604 // Always report that the command succeeded and return the error code. 605 // The caller will check the return value to see what the error was. 606 char msg[255]; 607 snprintf(msg, sizeof(msg), "%d", rc); 608 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 609 610 return 0; 611} 612