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