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