CommandListener.cpp revision 78b524ec463da25d6512dc791112c1335efd497b
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/mount.h> 19#include <sys/socket.h> 20#include <sys/stat.h> 21#include <sys/types.h> 22#include <netinet/in.h> 23#include <arpa/inet.h> 24#include <dirent.h> 25#include <errno.h> 26#include <fcntl.h> 27#include <fs_mgr.h> 28#include <stdio.h> 29#include <string.h> 30#include <stdint.h> 31#include <inttypes.h> 32#include <ctype.h> 33 34#define LOG_TAG "VoldCmdListener" 35 36#include <android-base/stringprintf.h> 37#include <cutils/fs.h> 38#include <cutils/log.h> 39 40#include <sysutils/SocketClient.h> 41#include <private/android_filesystem_config.h> 42 43#include "CommandListener.h" 44#include "VolumeManager.h" 45#include "VolumeBase.h" 46#include "ResponseCode.h" 47#include "Process.h" 48#include "Loop.h" 49#include "Devmapper.h" 50#include "MoveTask.h" 51#include "TrimTask.h" 52 53#define DUMP_ARGS 0 54 55CommandListener::CommandListener() : 56 FrameworkListener("vold", true) { 57 registerCmd(new DumpCmd()); 58 registerCmd(new VolumeCmd()); 59 registerCmd(new AsecCmd()); 60 registerCmd(new ObbCmd()); 61 registerCmd(new StorageCmd()); 62 registerCmd(new FstrimCmd()); 63 registerCmd(new AppFuseCmd()); 64} 65 66#if DUMP_ARGS 67void CommandListener::dumpArgs(int argc, char **argv, int argObscure) { 68 char buffer[4096]; 69 char *p = buffer; 70 71 memset(buffer, 0, sizeof(buffer)); 72 int i; 73 for (i = 0; i < argc; i++) { 74 unsigned int len = strlen(argv[i]) + 1; // Account for space 75 if (i == argObscure) { 76 len += 2; // Account for {} 77 } 78 if (((p - buffer) + len) < (sizeof(buffer)-1)) { 79 if (i == argObscure) { 80 *p++ = '{'; 81 *p++ = '}'; 82 *p++ = ' '; 83 continue; 84 } 85 strcpy(p, argv[i]); 86 p+= strlen(argv[i]); 87 if (i != (argc -1)) { 88 *p++ = ' '; 89 } 90 } 91 } 92 SLOGD("%s", buffer); 93} 94#else 95void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { } 96#endif 97 98int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) { 99 if (!cond) { 100 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false); 101 } else { 102 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false); 103 } 104} 105 106CommandListener::DumpCmd::DumpCmd() : 107 VoldCommand("dump") { 108} 109 110int CommandListener::DumpCmd::runCommand(SocketClient *cli, 111 int /*argc*/, char ** /*argv*/) { 112 cli->sendMsg(0, "Dumping loop status", false); 113 if (Loop::dumpState(cli)) { 114 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true); 115 } 116 cli->sendMsg(0, "Dumping DM status", false); 117 if (Devmapper::dumpState(cli)) { 118 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true); 119 } 120 cli->sendMsg(0, "Dumping mounted filesystems", false); 121 FILE *fp = fopen("/proc/mounts", "r"); 122 if (fp) { 123 char line[1024]; 124 while (fgets(line, sizeof(line), fp)) { 125 line[strlen(line)-1] = '\0'; 126 cli->sendMsg(0, line, false);; 127 } 128 fclose(fp); 129 } 130 131 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false); 132 return 0; 133} 134 135CommandListener::VolumeCmd::VolumeCmd() : 136 VoldCommand("volume") { 137} 138 139int CommandListener::VolumeCmd::runCommand(SocketClient *cli, 140 int argc, char **argv) { 141 dumpArgs(argc, argv, -1); 142 143 if (argc < 2) { 144 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 145 return 0; 146 } 147 148 VolumeManager *vm = VolumeManager::Instance(); 149 std::lock_guard<std::mutex> lock(vm->getLock()); 150 151 // TODO: tease out methods not directly related to volumes 152 153 std::string cmd(argv[1]); 154 if (cmd == "reset") { 155 return sendGenericOkFail(cli, vm->reset()); 156 157 } else if (cmd == "shutdown") { 158 return sendGenericOkFail(cli, vm->shutdown()); 159 160 } else if (cmd == "debug") { 161 return sendGenericOkFail(cli, vm->setDebug(true)); 162 163 } else if (cmd == "partition" && argc > 3) { 164 // partition [diskId] [public|private|mixed] [ratio] 165 std::string id(argv[2]); 166 auto disk = vm->findDisk(id); 167 if (disk == nullptr) { 168 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false); 169 } 170 171 std::string type(argv[3]); 172 if (type == "public") { 173 return sendGenericOkFail(cli, disk->partitionPublic()); 174 } else if (type == "private") { 175 return sendGenericOkFail(cli, disk->partitionPrivate()); 176 } else if (type == "mixed") { 177 if (argc < 4) { 178 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 179 } 180 int frac = atoi(argv[4]); 181 return sendGenericOkFail(cli, disk->partitionMixed(frac)); 182 } else { 183 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 184 } 185 186 } else if (cmd == "mkdirs" && argc > 2) { 187 // mkdirs [path] 188 return sendGenericOkFail(cli, vm->mkdirs(argv[2])); 189 190 } else if (cmd == "user_added" && argc > 3) { 191 // user_added [user] [serial] 192 return sendGenericOkFail(cli, vm->onUserAdded(atoi(argv[2]), atoi(argv[3]))); 193 194 } else if (cmd == "user_removed" && argc > 2) { 195 // user_removed [user] 196 return sendGenericOkFail(cli, vm->onUserRemoved(atoi(argv[2]))); 197 198 } else if (cmd == "user_started" && argc > 2) { 199 // user_started [user] 200 return sendGenericOkFail(cli, vm->onUserStarted(atoi(argv[2]))); 201 202 } else if (cmd == "user_stopped" && argc > 2) { 203 // user_stopped [user] 204 return sendGenericOkFail(cli, vm->onUserStopped(atoi(argv[2]))); 205 206 } else if (cmd == "mount" && argc > 2) { 207 // mount [volId] [flags] [user] 208 std::string id(argv[2]); 209 auto vol = vm->findVolume(id); 210 if (vol == nullptr) { 211 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 212 } 213 214 int mountFlags = (argc > 3) ? atoi(argv[3]) : 0; 215 userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1; 216 217 vol->setMountFlags(mountFlags); 218 vol->setMountUserId(mountUserId); 219 220 int res = vol->mount(); 221 if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) { 222 vm->setPrimary(vol); 223 } 224 return sendGenericOkFail(cli, res); 225 226 } else if (cmd == "unmount" && argc > 2) { 227 // unmount [volId] 228 std::string id(argv[2]); 229 auto vol = vm->findVolume(id); 230 if (vol == nullptr) { 231 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 232 } 233 234 return sendGenericOkFail(cli, vol->unmount()); 235 236 } else if (cmd == "format" && argc > 3) { 237 // format [volId] [fsType|auto] 238 std::string id(argv[2]); 239 std::string fsType(argv[3]); 240 auto vol = vm->findVolume(id); 241 if (vol == nullptr) { 242 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 243 } 244 245 return sendGenericOkFail(cli, vol->format(fsType)); 246 247 } else if (cmd == "move_storage" && argc > 3) { 248 // move_storage [fromVolId] [toVolId] 249 auto fromVol = vm->findVolume(std::string(argv[2])); 250 auto toVol = vm->findVolume(std::string(argv[3])); 251 if (fromVol == nullptr || toVol == nullptr) { 252 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); 253 } 254 255 (new android::vold::MoveTask(fromVol, toVol))->start(); 256 return sendGenericOkFail(cli, 0); 257 258 } else if (cmd == "benchmark" && argc > 2) { 259 // benchmark [volId] 260 std::string id(argv[2]); 261 nsecs_t res = vm->benchmarkPrivate(id); 262 return cli->sendMsg(ResponseCode::CommandOkay, 263 android::base::StringPrintf("%" PRId64, res).c_str(), false); 264 265 } else if (cmd == "forget_partition" && argc > 2) { 266 // forget_partition [partGuid] 267 std::string partGuid(argv[2]); 268 return sendGenericOkFail(cli, vm->forgetPartition(partGuid)); 269 270 } else if (cmd == "remount_uid" && argc > 3) { 271 // remount_uid [uid] [none|default|read|write] 272 uid_t uid = atoi(argv[2]); 273 std::string mode(argv[3]); 274 return sendGenericOkFail(cli, vm->remountUid(uid, mode)); 275 } 276 277 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); 278} 279 280CommandListener::StorageCmd::StorageCmd() : 281 VoldCommand("storage") { 282} 283 284int CommandListener::StorageCmd::runCommand(SocketClient *cli, 285 int argc, char **argv) { 286 /* Guarantied to be initialized by vold's main() before the CommandListener is active */ 287 extern struct fstab *fstab; 288 289 dumpArgs(argc, argv, -1); 290 291 if (argc < 2) { 292 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 293 return 0; 294 } 295 296 if (!strcmp(argv[1], "mountall")) { 297 if (argc != 2) { 298 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false); 299 return 0; 300 } 301 fs_mgr_mount_all(fstab); 302 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false); 303 return 0; 304 } 305 if (!strcmp(argv[1], "users")) { 306 DIR *dir; 307 struct dirent *de; 308 309 if (argc < 3) { 310 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false); 311 return 0; 312 } 313 if (!(dir = opendir("/proc"))) { 314 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true); 315 return 0; 316 } 317 318 while ((de = readdir(dir))) { 319 int pid = Process::getPid(de->d_name); 320 321 if (pid < 0) { 322 continue; 323 } 324 325 char processName[255]; 326 Process::getProcessName(pid, processName, sizeof(processName)); 327 328 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) || 329 Process::checkFileMaps(pid, argv[2]) || 330 Process::checkSymLink(pid, argv[2], "cwd") || 331 Process::checkSymLink(pid, argv[2], "root") || 332 Process::checkSymLink(pid, argv[2], "exe")) { 333 334 char msg[1024]; 335 snprintf(msg, sizeof(msg), "%d %s", pid, processName); 336 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false); 337 } 338 } 339 closedir(dir); 340 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false); 341 } else { 342 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); 343 } 344 return 0; 345} 346 347CommandListener::AsecCmd::AsecCmd() : 348 VoldCommand("asec") { 349} 350 351void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) { 352 DIR *d = opendir(directory); 353 354 if (!d) { 355 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true); 356 return; 357 } 358 359 size_t dirent_len = offsetof(struct dirent, d_name) + 360 fpathconf(dirfd(d), _PC_NAME_MAX) + 1; 361 362 struct dirent *dent = (struct dirent *) malloc(dirent_len); 363 if (dent == NULL) { 364 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true); 365 return; 366 } 367 368 struct dirent *result; 369 370 while (!readdir_r(d, dent, &result) && result != NULL) { 371 if (dent->d_name[0] == '.') 372 continue; 373 if (dent->d_type != DT_REG) 374 continue; 375 size_t name_len = strlen(dent->d_name); 376 if (name_len > 5 && name_len < 260 && 377 !strcmp(&dent->d_name[name_len - 5], ".asec")) { 378 char id[255]; 379 memset(id, 0, sizeof(id)); 380 strlcpy(id, dent->d_name, name_len - 4); 381 cli->sendMsg(ResponseCode::AsecListResult, id, false); 382 } 383 } 384 closedir(d); 385 386 free(dent); 387} 388 389int CommandListener::AsecCmd::runCommand(SocketClient *cli, 390 int argc, char **argv) { 391 if (argc < 2) { 392 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 393 return 0; 394 } 395 396 VolumeManager *vm = VolumeManager::Instance(); 397 int rc = 0; 398 399 if (!strcmp(argv[1], "list")) { 400 dumpArgs(argc, argv, -1); 401 402 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT); 403 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT); 404 } else if (!strcmp(argv[1], "create")) { 405 dumpArgs(argc, argv, 5); 406 if (argc != 8) { 407 cli->sendMsg(ResponseCode::CommandSyntaxError, 408 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> " 409 "<isExternal>", false); 410 return 0; 411 } 412 413 unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 414 const bool isExternal = (atoi(argv[7]) == 1); 415 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); 416 } else if (!strcmp(argv[1], "resize")) { 417 dumpArgs(argc, argv, -1); 418 if (argc != 5) { 419 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false); 420 return 0; 421 } 422 unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 423 rc = vm->resizeAsec(argv[2], numSectors, argv[4]); 424 } else if (!strcmp(argv[1], "finalize")) { 425 dumpArgs(argc, argv, -1); 426 if (argc != 3) { 427 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); 428 return 0; 429 } 430 rc = vm->finalizeAsec(argv[2]); 431 } else if (!strcmp(argv[1], "fixperms")) { 432 dumpArgs(argc, argv, -1); 433 if (argc != 5) { 434 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 435 return 0; 436 } 437 438 char *endptr; 439 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10); 440 if (*endptr != '\0') { 441 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); 442 return 0; 443 } 444 445 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]); 446 } else if (!strcmp(argv[1], "destroy")) { 447 dumpArgs(argc, argv, -1); 448 if (argc < 3) { 449 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <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->destroyAsec(argv[2], force); 457 } else if (!strcmp(argv[1], "mount")) { 458 dumpArgs(argc, argv, 3); 459 if (argc != 6) { 460 cli->sendMsg(ResponseCode::CommandSyntaxError, 461 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false); 462 return 0; 463 } 464 bool readOnly = true; 465 if (!strcmp(argv[5], "rw")) { 466 readOnly = false; 467 } 468 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly); 469 } else if (!strcmp(argv[1], "unmount")) { 470 dumpArgs(argc, argv, -1); 471 if (argc < 3) { 472 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); 473 return 0; 474 } 475 bool force = false; 476 if (argc > 3 && !strcmp(argv[3], "force")) { 477 force = true; 478 } 479 rc = vm->unmountAsec(argv[2], force); 480 } else if (!strcmp(argv[1], "rename")) { 481 dumpArgs(argc, argv, -1); 482 if (argc != 4) { 483 cli->sendMsg(ResponseCode::CommandSyntaxError, 484 "Usage: asec rename <old_id> <new_id>", false); 485 return 0; 486 } 487 rc = vm->renameAsec(argv[2], argv[3]); 488 } else if (!strcmp(argv[1], "path")) { 489 dumpArgs(argc, argv, -1); 490 if (argc != 3) { 491 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); 492 return 0; 493 } 494 char path[255]; 495 496 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) { 497 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 498 return 0; 499 } 500 } else if (!strcmp(argv[1], "fspath")) { 501 dumpArgs(argc, argv, -1); 502 if (argc != 3) { 503 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false); 504 return 0; 505 } 506 char path[255]; 507 508 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) { 509 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 510 return 0; 511 } 512 } else { 513 dumpArgs(argc, argv, -1); 514 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); 515 } 516 517 if (!rc) { 518 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false); 519 } else { 520 rc = ResponseCode::convertFromErrno(); 521 cli->sendMsg(rc, "asec operation failed", true); 522 } 523 524 return 0; 525} 526 527CommandListener::ObbCmd::ObbCmd() : 528 VoldCommand("obb") { 529} 530 531int CommandListener::ObbCmd::runCommand(SocketClient *cli, 532 int argc, char **argv) { 533 if (argc < 2) { 534 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 535 return 0; 536 } 537 538 VolumeManager *vm = VolumeManager::Instance(); 539 int rc = 0; 540 541 if (!strcmp(argv[1], "list")) { 542 dumpArgs(argc, argv, -1); 543 544 rc = vm->listMountedObbs(cli); 545 } else if (!strcmp(argv[1], "mount")) { 546 dumpArgs(argc, argv, 3); 547 if (argc != 5) { 548 cli->sendMsg(ResponseCode::CommandSyntaxError, 549 "Usage: obb mount <filename> <key> <ownerGid>", false); 550 return 0; 551 } 552 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4])); 553 } else if (!strcmp(argv[1], "unmount")) { 554 dumpArgs(argc, argv, -1); 555 if (argc < 3) { 556 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false); 557 return 0; 558 } 559 bool force = false; 560 if (argc > 3 && !strcmp(argv[3], "force")) { 561 force = true; 562 } 563 rc = vm->unmountObb(argv[2], force); 564 } else if (!strcmp(argv[1], "path")) { 565 dumpArgs(argc, argv, -1); 566 if (argc != 3) { 567 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false); 568 return 0; 569 } 570 char path[255]; 571 572 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) { 573 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 574 return 0; 575 } 576 } else { 577 dumpArgs(argc, argv, -1); 578 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false); 579 } 580 581 if (!rc) { 582 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false); 583 } else { 584 rc = ResponseCode::convertFromErrno(); 585 cli->sendMsg(rc, "obb operation failed", true); 586 } 587 588 return 0; 589} 590 591CommandListener::FstrimCmd::FstrimCmd() : 592 VoldCommand("fstrim") { 593} 594int CommandListener::FstrimCmd::runCommand(SocketClient *cli, 595 int argc, char **argv) { 596 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 597 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); 598 return 0; 599 } 600 601 if (argc < 2) { 602 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 603 return 0; 604 } 605 606 VolumeManager *vm = VolumeManager::Instance(); 607 std::lock_guard<std::mutex> lock(vm->getLock()); 608 609 int flags = 0; 610 611 std::string cmd(argv[1]); 612 if (cmd == "dotrim") { 613 flags = 0; 614 } else if (cmd == "dotrimbench") { 615 flags = android::vold::TrimTask::Flags::kBenchmarkAfter; 616 } else if (cmd == "dodtrim") { 617 flags = android::vold::TrimTask::Flags::kDeepTrim; 618 } else if (cmd == "dodtrimbench") { 619 flags = android::vold::TrimTask::Flags::kDeepTrim 620 | android::vold::TrimTask::Flags::kBenchmarkAfter; 621 } 622 623 (new android::vold::TrimTask(flags))->start(); 624 return sendGenericOkFail(cli, 0); 625} 626 627static size_t kAppFuseMaxMountPointName = 32; 628 629static bool isValidAppFuseMountName(const std::string& name) { 630 if (name.size() > kAppFuseMaxMountPointName) { 631 return false; 632 } 633 for (size_t i = 0; i < name.size(); i++) { 634 if (!isalnum(name[i])) { 635 return false; 636 } 637 } 638 return true; 639} 640 641CommandListener::AppFuseCmd::AppFuseCmd() : VoldCommand("appfuse") {} 642 643int CommandListener::AppFuseCmd::runCommand(SocketClient *cli, 644 int argc, 645 char **argv) { 646 if (argc < 2) { 647 cli->sendMsg( 648 ResponseCode::CommandSyntaxError, "Missing argument", false); 649 return 0; 650 } 651 652 const std::string command(argv[1]); 653 654 if (command == "mount" && argc == 4) { 655 const uid_t uid = atoi(argv[2]); 656 const std::string name(argv[3]); 657 658 // Check mount point name. 659 if (!isValidAppFuseMountName(name)) { 660 return cli->sendMsg( 661 ResponseCode::CommandParameterError, 662 "Invalid mount point name.", 663 false); 664 } 665 666 // Create directories. 667 char path[PATH_MAX]; 668 { 669 snprintf(path, PATH_MAX, "/mnt/appfuse/%d_%s", uid, name.c_str()); 670 umount2(path, UMOUNT_NOFOLLOW | MNT_DETACH); 671 const int result = android::vold::PrepareDir(path, 0700, 0, 0); 672 if (result != 0) { 673 return sendGenericOkFail(cli, result); 674 } 675 } 676 677 // Open device FD. 678 const int device_fd = open("/dev/fuse", O_RDWR); 679 if (device_fd < 0) { 680 sendGenericOkFail(cli, device_fd); 681 return 0; 682 } 683 684 // Mount. 685 { 686 char opts[256]; 687 snprintf( 688 opts, 689 sizeof(opts), 690 "fd=%i," 691 "rootmode=40000," 692 "default_permissions," 693 "user_id=%d,group_id=%d", 694 device_fd, 695 uid, 696 uid); 697 // TODO: Make it bound mount in application namespace. 698 // TODO: Add context= option to opts. 699 const int result = mount( 700 "/dev/fuse", path, "fuse", 701 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts); 702 if (result != 0) { 703 sendGenericOkFail(cli, 1); 704 return 0; 705 } 706 } 707 708 const int result = sendFd(cli, device_fd); 709 close(device_fd); 710 return result; 711 } else if (command == "unmount" && argc == 4) { 712 const uid_t uid = atoi(argv[2]); 713 const std::string name(argv[3]); 714 715 // Check mount point name. 716 if (!isValidAppFuseMountName(name)) { 717 return cli->sendMsg( 718 ResponseCode::CommandParameterError, 719 "Invalid mount point name.", 720 false); 721 } 722 723 // Unmount directory. 724 char path[PATH_MAX]; 725 snprintf(path, PATH_MAX, "/mnt/appfuse/%d_%s", uid, name.c_str()); 726 umount2(path, UMOUNT_NOFOLLOW | MNT_DETACH); 727 728 return sendGenericOkFail(cli, /* success */ 0); 729 } 730 731 return cli->sendMsg( 732 ResponseCode::CommandSyntaxError, "Unknown appfuse cmd", false); 733} 734 735int CommandListener::AppFuseCmd::sendFd(SocketClient *cli, int fd) { 736 struct iovec data; 737 char dataBuffer[128]; 738 char controlBuffer[CMSG_SPACE(sizeof(int))]; 739 struct msghdr message; 740 741 // Message. 742 memset(&message, 0, sizeof(struct msghdr)); 743 message.msg_iov = &data; 744 message.msg_iovlen = 1; 745 message.msg_control = controlBuffer; 746 message.msg_controllen = CMSG_SPACE(sizeof(int)); 747 748 // Data. 749 data.iov_base = dataBuffer; 750 data.iov_len = snprintf(dataBuffer, 751 sizeof(dataBuffer), 752 "200 %d AppFuse command succeeded", 753 cli->getCmdNum()) + 1; 754 755 // Control. 756 struct cmsghdr* const controlMessage = CMSG_FIRSTHDR(&message); 757 memset(controlBuffer, 0, CMSG_SPACE(sizeof(int))); 758 controlMessage->cmsg_level = SOL_SOCKET; 759 controlMessage->cmsg_type = SCM_RIGHTS; 760 controlMessage->cmsg_len = CMSG_LEN(sizeof(int)); 761 *((int *) CMSG_DATA(controlMessage)) = fd; 762 763 return TEMP_FAILURE_RETRY(sendmsg(cli->getSocket(), &message, 0)); 764} 765