CommandListener.cpp revision c36ffa0010c0634d0ba0a72e89ca0cfbd7b00cd9
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 } else if (cmd == "remount_uid" && argc > 3) { 267 // remount_uid [uid] [none|default|read|write] 268 uid_t uid = atoi(argv[2]); 269 std::string mode(argv[3]); 270 return sendGenericOkFail(cli, vm->remountUid(uid, mode)); 271 } 272 273 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 274} 275 276CommandListener::StorageCmd::StorageCmd() : 277 VoldCommand("storage") { 278} 279 280int CommandListener::StorageCmd::runCommand(SocketClient *cli, 281 int argc, char **argv) { 282 /* Guarantied to be initialized by vold's main() before the CommandListener is active */ 283 extern struct fstab *fstab; 284 285 dumpArgs(argc, argv, -1); 286 287 if (argc < 2) { 288 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 289 return 0; 290 } 291 292 if (!strcmp(argv[1], "mountall")) { 293 if (argc != 2) { 294 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false); 295 return 0; 296 } 297 fs_mgr_mount_all(fstab); 298 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false); 299 return 0; 300 } 301 if (!strcmp(argv[1], "users")) { 302 DIR *dir; 303 struct dirent *de; 304 305 if (argc < 3) { 306 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false); 307 return 0; 308 } 309 if (!(dir = opendir("/proc"))) { 310 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true); 311 return 0; 312 } 313 314 while ((de = readdir(dir))) { 315 int pid = Process::getPid(de->d_name); 316 317 if (pid < 0) { 318 continue; 319 } 320 321 char processName[255]; 322 Process::getProcessName(pid, processName, sizeof(processName)); 323 324 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) || 325 Process::checkFileMaps(pid, argv[2]) || 326 Process::checkSymLink(pid, argv[2], "cwd") || 327 Process::checkSymLink(pid, argv[2], "root") || 328 Process::checkSymLink(pid, argv[2], "exe")) { 329 330 char msg[1024]; 331 snprintf(msg, sizeof(msg), "%d %s", pid, processName); 332 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false); 333 } 334 } 335 closedir(dir); 336 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false); 337 } else { 338 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); 339 } 340 return 0; 341} 342 343CommandListener::AsecCmd::AsecCmd() : 344 VoldCommand("asec") { 345} 346 347void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) { 348 DIR *d = opendir(directory); 349 350 if (!d) { 351 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true); 352 return; 353 } 354 355 size_t dirent_len = offsetof(struct dirent, d_name) + 356 fpathconf(dirfd(d), _PC_NAME_MAX) + 1; 357 358 struct dirent *dent = (struct dirent *) malloc(dirent_len); 359 if (dent == NULL) { 360 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true); 361 return; 362 } 363 364 struct dirent *result; 365 366 while (!readdir_r(d, dent, &result) && result != NULL) { 367 if (dent->d_name[0] == '.') 368 continue; 369 if (dent->d_type != DT_REG) 370 continue; 371 size_t name_len = strlen(dent->d_name); 372 if (name_len > 5 && name_len < 260 && 373 !strcmp(&dent->d_name[name_len - 5], ".asec")) { 374 char id[255]; 375 memset(id, 0, sizeof(id)); 376 strlcpy(id, dent->d_name, name_len - 4); 377 cli->sendMsg(ResponseCode::AsecListResult, id, false); 378 } 379 } 380 closedir(d); 381 382 free(dent); 383} 384 385int CommandListener::AsecCmd::runCommand(SocketClient *cli, 386 int argc, char **argv) { 387 if (argc < 2) { 388 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 389 return 0; 390 } 391 392 VolumeManager *vm = VolumeManager::Instance(); 393 int rc = 0; 394 395 if (!strcmp(argv[1], "list")) { 396 dumpArgs(argc, argv, -1); 397 398 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT); 399 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT); 400 } else if (!strcmp(argv[1], "create")) { 401 dumpArgs(argc, argv, 5); 402 if (argc != 8) { 403 cli->sendMsg(ResponseCode::CommandSyntaxError, 404 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> " 405 "<isExternal>", false); 406 return 0; 407 } 408 409 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 410 const bool isExternal = (atoi(argv[7]) == 1); 411 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); 412 } else if (!strcmp(argv[1], "resize")) { 413 dumpArgs(argc, argv, -1); 414 if (argc != 5) { 415 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false); 416 return 0; 417 } 418 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 419 rc = vm->resizeAsec(argv[2], numSectors, argv[4]); 420 } else if (!strcmp(argv[1], "finalize")) { 421 dumpArgs(argc, argv, -1); 422 if (argc != 3) { 423 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); 424 return 0; 425 } 426 rc = vm->finalizeAsec(argv[2]); 427 } else if (!strcmp(argv[1], "fixperms")) { 428 dumpArgs(argc, argv, -1); 429 if (argc != 5) { 430 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 431 return 0; 432 } 433 434 char *endptr; 435 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10); 436 if (*endptr != '\0') { 437 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 438 return 0; 439 } 440 441 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]); 442 } else if (!strcmp(argv[1], "destroy")) { 443 dumpArgs(argc, argv, -1); 444 if (argc < 3) { 445 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false); 446 return 0; 447 } 448 bool force = false; 449 if (argc > 3 && !strcmp(argv[3], "force")) { 450 force = true; 451 } 452 rc = vm->destroyAsec(argv[2], force); 453 } else if (!strcmp(argv[1], "mount")) { 454 dumpArgs(argc, argv, 3); 455 if (argc != 6) { 456 cli->sendMsg(ResponseCode::CommandSyntaxError, 457 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false); 458 return 0; 459 } 460 bool readOnly = true; 461 if (!strcmp(argv[5], "rw")) { 462 readOnly = false; 463 } 464 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly); 465 } else if (!strcmp(argv[1], "unmount")) { 466 dumpArgs(argc, argv, -1); 467 if (argc < 3) { 468 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); 469 return 0; 470 } 471 bool force = false; 472 if (argc > 3 && !strcmp(argv[3], "force")) { 473 force = true; 474 } 475 rc = vm->unmountAsec(argv[2], force); 476 } else if (!strcmp(argv[1], "rename")) { 477 dumpArgs(argc, argv, -1); 478 if (argc != 4) { 479 cli->sendMsg(ResponseCode::CommandSyntaxError, 480 "Usage: asec rename <old_id> <new_id>", false); 481 return 0; 482 } 483 rc = vm->renameAsec(argv[2], argv[3]); 484 } else if (!strcmp(argv[1], "path")) { 485 dumpArgs(argc, argv, -1); 486 if (argc != 3) { 487 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); 488 return 0; 489 } 490 char path[255]; 491 492 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) { 493 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 494 return 0; 495 } 496 } else if (!strcmp(argv[1], "fspath")) { 497 dumpArgs(argc, argv, -1); 498 if (argc != 3) { 499 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false); 500 return 0; 501 } 502 char path[255]; 503 504 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) { 505 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 506 return 0; 507 } 508 } else { 509 dumpArgs(argc, argv, -1); 510 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); 511 } 512 513 if (!rc) { 514 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false); 515 } else { 516 rc = ResponseCode::convertFromErrno(); 517 cli->sendMsg(rc, "asec operation failed", true); 518 } 519 520 return 0; 521} 522 523CommandListener::ObbCmd::ObbCmd() : 524 VoldCommand("obb") { 525} 526 527int CommandListener::ObbCmd::runCommand(SocketClient *cli, 528 int argc, char **argv) { 529 if (argc < 2) { 530 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 531 return 0; 532 } 533 534 VolumeManager *vm = VolumeManager::Instance(); 535 int rc = 0; 536 537 if (!strcmp(argv[1], "list")) { 538 dumpArgs(argc, argv, -1); 539 540 rc = vm->listMountedObbs(cli); 541 } else if (!strcmp(argv[1], "mount")) { 542 dumpArgs(argc, argv, 3); 543 if (argc != 5) { 544 cli->sendMsg(ResponseCode::CommandSyntaxError, 545 "Usage: obb mount <filename> <key> <ownerGid>", false); 546 return 0; 547 } 548 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4])); 549 } else if (!strcmp(argv[1], "unmount")) { 550 dumpArgs(argc, argv, -1); 551 if (argc < 3) { 552 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false); 553 return 0; 554 } 555 bool force = false; 556 if (argc > 3 && !strcmp(argv[3], "force")) { 557 force = true; 558 } 559 rc = vm->unmountObb(argv[2], force); 560 } else if (!strcmp(argv[1], "path")) { 561 dumpArgs(argc, argv, -1); 562 if (argc != 3) { 563 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false); 564 return 0; 565 } 566 char path[255]; 567 568 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) { 569 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 570 return 0; 571 } 572 } else { 573 dumpArgs(argc, argv, -1); 574 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false); 575 } 576 577 if (!rc) { 578 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false); 579 } else { 580 rc = ResponseCode::convertFromErrno(); 581 cli->sendMsg(rc, "obb operation failed", true); 582 } 583 584 return 0; 585} 586 587CommandListener::FstrimCmd::FstrimCmd() : 588 VoldCommand("fstrim") { 589} 590int CommandListener::FstrimCmd::runCommand(SocketClient *cli, 591 int argc, char **argv) { 592 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 593 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); 594 return 0; 595 } 596 597 if (argc < 2) { 598 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 599 return 0; 600 } 601 602 int rc = 0; 603 604 if (!strcmp(argv[1], "dotrim")) { 605 if (argc != 2) { 606 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false); 607 return 0; 608 } 609 dumpArgs(argc, argv, -1); 610 rc = fstrim_filesystems(0); 611 } else if (!strcmp(argv[1], "dodtrim")) { 612 if (argc != 2) { 613 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false); 614 return 0; 615 } 616 dumpArgs(argc, argv, -1); 617 rc = fstrim_filesystems(1); /* Do Deep Discard trim */ 618 } else { 619 dumpArgs(argc, argv, -1); 620 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false); 621 } 622 623 // Always report that the command succeeded and return the error code. 624 // The caller will check the return value to see what the error was. 625 char msg[255]; 626 snprintf(msg, sizeof(msg), "%d", rc); 627 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 628 629 return 0; 630} 631