2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/mount.h>
19 #include <sys/socket.h>
21 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
35 #define LOG_TAG "VoldCmdListener"
37 #include <android-base/logging.h>
38 #include <android-base/stringprintf.h>
39 #include <android-base/unique_fd.h>
40 #include <cutils/fs.h>
42 #include <sysutils/SocketClient.h>
43 #include <private/android_filesystem_config.h>
45 #include "CommandListener.h"
46 #include "VolumeManager.h"
47 #include "VolumeBase.h"
48 #include "ResponseCode.h"
51 #include "Devmapper.h"
56 #define DEBUG_APPFUSE 0
58 using android::base::unique_fd;
60 CommandListener::CommandListener() :
61 FrameworkListener("vold", true) {
62 registerCmd(new DumpCmd());
63 registerCmd(new VolumeCmd());
64 registerCmd(new AsecCmd());
65 registerCmd(new ObbCmd());
66 registerCmd(new StorageCmd());
67 registerCmd(new FstrimCmd());
68 registerCmd(new AppFuseCmd());
72 void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
76 memset(buffer, 0, sizeof(buffer));
78 for (i = 0; i < argc; i++) {
79 unsigned int len = strlen(argv[i]) + 1; // Account for space
80 if (i == argObscure) {
81 len += 2; // Account for {}
83 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
84 if (i == argObscure) {
100 void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
103 int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) {
105 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
107 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
111 CommandListener::DumpCmd::DumpCmd() :
112 VoldCommand("dump") {
115 int CommandListener::DumpCmd::runCommand(SocketClient *cli,
116 int /*argc*/, char ** /*argv*/) {
117 cli->sendMsg(0, "Dumping loop status", false);
118 if (Loop::dumpState(cli)) {
119 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
121 cli->sendMsg(0, "Dumping DM status", false);
122 if (Devmapper::dumpState(cli)) {
123 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
125 cli->sendMsg(0, "Dumping mounted filesystems", false);
126 FILE *fp = fopen("/proc/mounts", "re");
129 while (fgets(line, sizeof(line), fp)) {
130 line[strlen(line)-1] = '\0';
131 cli->sendMsg(0, line, false);;
136 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
140 CommandListener::VolumeCmd::VolumeCmd() :
141 VoldCommand("volume") {
144 int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
145 int argc, char **argv) {
146 dumpArgs(argc, argv, -1);
149 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
153 VolumeManager *vm = VolumeManager::Instance();
154 std::lock_guard<std::mutex> lock(vm->getLock());
156 // TODO: tease out methods not directly related to volumes
158 std::string cmd(argv[1]);
159 if (cmd == "reset") {
160 return sendGenericOkFail(cli, vm->reset());
162 } else if (cmd == "shutdown") {
163 return sendGenericOkFail(cli, vm->shutdown());
165 } else if (cmd == "debug") {
166 return sendGenericOkFail(cli, vm->setDebug(true));
168 } else if (cmd == "partition" && argc > 3) {
169 // partition [diskId] [public|private|mixed] [ratio]
170 std::string id(argv[2]);
171 auto disk = vm->findDisk(id);
172 if (disk == nullptr) {
173 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false);
176 std::string type(argv[3]);
177 if (type == "public") {
178 return sendGenericOkFail(cli, disk->partitionPublic());
179 } else if (type == "private") {
180 return sendGenericOkFail(cli, disk->partitionPrivate());
181 } else if (type == "mixed") {
183 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
185 int frac = atoi(argv[4]);
186 return sendGenericOkFail(cli, disk->partitionMixed(frac));
188 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
191 } else if (cmd == "mkdirs" && argc > 2) {
193 return sendGenericOkFail(cli, vm->mkdirs(argv[2]));
195 } else if (cmd == "user_added" && argc > 3) {
196 // user_added [user] [serial]
197 return sendGenericOkFail(cli, vm->onUserAdded(atoi(argv[2]), atoi(argv[3])));
199 } else if (cmd == "user_removed" && argc > 2) {
200 // user_removed [user]
201 return sendGenericOkFail(cli, vm->onUserRemoved(atoi(argv[2])));
203 } else if (cmd == "user_started" && argc > 2) {
204 // user_started [user]
205 return sendGenericOkFail(cli, vm->onUserStarted(atoi(argv[2])));
207 } else if (cmd == "user_stopped" && argc > 2) {
208 // user_stopped [user]
209 return sendGenericOkFail(cli, vm->onUserStopped(atoi(argv[2])));
211 } else if (cmd == "mount" && argc > 2) {
212 // mount [volId] [flags] [user]
213 std::string id(argv[2]);
214 auto vol = vm->findVolume(id);
215 if (vol == nullptr) {
216 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
219 int mountFlags = (argc > 3) ? atoi(argv[3]) : 0;
220 userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1;
222 vol->setMountFlags(mountFlags);
223 vol->setMountUserId(mountUserId);
225 int res = vol->mount();
226 if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) {
229 return sendGenericOkFail(cli, res);
231 } else if (cmd == "unmount" && argc > 2) {
233 std::string id(argv[2]);
234 auto vol = vm->findVolume(id);
235 if (vol == nullptr) {
236 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
239 return sendGenericOkFail(cli, vol->unmount());
241 } else if (cmd == "format" && argc > 3) {
242 // format [volId] [fsType|auto]
243 std::string id(argv[2]);
244 std::string fsType(argv[3]);
245 auto vol = vm->findVolume(id);
246 if (vol == nullptr) {
247 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
250 return sendGenericOkFail(cli, vol->format(fsType));
252 } else if (cmd == "move_storage" && argc > 3) {
253 // move_storage [fromVolId] [toVolId]
254 auto fromVol = vm->findVolume(std::string(argv[2]));
255 auto toVol = vm->findVolume(std::string(argv[3]));
256 if (fromVol == nullptr || toVol == nullptr) {
257 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
260 (new android::vold::MoveTask(fromVol, toVol))->start();
261 return sendGenericOkFail(cli, 0);
263 } else if (cmd == "benchmark" && argc > 2) {
265 std::string id(argv[2]);
266 nsecs_t res = vm->benchmarkPrivate(id);
267 return cli->sendMsg(ResponseCode::CommandOkay,
268 android::base::StringPrintf("%" PRId64, res).c_str(), false);
270 } else if (cmd == "forget_partition" && argc > 2) {
271 // forget_partition [partGuid]
272 std::string partGuid(argv[2]);
273 return sendGenericOkFail(cli, vm->forgetPartition(partGuid));
275 } else if (cmd == "remount_uid" && argc > 3) {
276 // remount_uid [uid] [none|default|read|write]
277 uid_t uid = atoi(argv[2]);
278 std::string mode(argv[3]);
279 return sendGenericOkFail(cli, vm->remountUid(uid, mode));
282 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
285 CommandListener::StorageCmd::StorageCmd() :
286 VoldCommand("storage") {
289 int CommandListener::StorageCmd::runCommand(SocketClient *cli,
290 int argc, char **argv) {
291 /* Guarantied to be initialized by vold's main() before the CommandListener is active */
292 extern struct fstab *fstab;
294 dumpArgs(argc, argv, -1);
297 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
301 if (!strcmp(argv[1], "mountall")) {
303 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false);
306 fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
307 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false);
310 if (!strcmp(argv[1], "users")) {
315 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false);
318 if (!(dir = opendir("/proc"))) {
319 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
323 while ((de = readdir(dir))) {
324 int pid = Process::getPid(de->d_name);
330 std::string processName;
331 Process::getProcessName(pid, processName);
333 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
334 Process::checkFileMaps(pid, argv[2]) ||
335 Process::checkSymLink(pid, argv[2], "cwd") ||
336 Process::checkSymLink(pid, argv[2], "root") ||
337 Process::checkSymLink(pid, argv[2], "exe")) {
340 snprintf(msg, sizeof(msg), "%d %s", pid, processName.c_str());
341 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
345 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
347 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
352 CommandListener::AsecCmd::AsecCmd() :
353 VoldCommand("asec") {
356 void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
357 DIR *d = opendir(directory);
360 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
364 size_t dirent_len = offsetof(struct dirent, d_name) +
365 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
367 struct dirent *dent = (struct dirent *) malloc(dirent_len);
369 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
373 struct dirent *result;
375 while (!readdir_r(d, dent, &result) && result != NULL) {
376 if (dent->d_name[0] == '.')
378 if (dent->d_type != DT_REG)
380 size_t name_len = strlen(dent->d_name);
381 if (name_len > 5 && name_len < 260 &&
382 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
384 memset(id, 0, sizeof(id));
385 strlcpy(id, dent->d_name, name_len - 4);
386 cli->sendMsg(ResponseCode::AsecListResult, id, false);
394 int CommandListener::AsecCmd::runCommand(SocketClient *cli,
395 int argc, char **argv) {
397 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
401 VolumeManager *vm = VolumeManager::Instance();
404 if (!strcmp(argv[1], "list")) {
405 dumpArgs(argc, argv, -1);
407 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT);
408 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT);
409 } else if (!strcmp(argv[1], "create")) {
410 dumpArgs(argc, argv, 5);
412 cli->sendMsg(ResponseCode::CommandSyntaxError,
413 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
414 "<isExternal>", false);
418 unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
419 const bool isExternal = (atoi(argv[7]) == 1);
420 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
421 } else if (!strcmp(argv[1], "resize")) {
422 dumpArgs(argc, argv, -1);
424 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
427 unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
428 rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
429 } else if (!strcmp(argv[1], "finalize")) {
430 dumpArgs(argc, argv, -1);
432 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
435 rc = vm->finalizeAsec(argv[2]);
436 } else if (!strcmp(argv[1], "fixperms")) {
437 dumpArgs(argc, argv, -1);
439 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
444 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
445 if (*endptr != '\0') {
446 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
450 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
451 } else if (!strcmp(argv[1], "destroy")) {
452 dumpArgs(argc, argv, -1);
454 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
458 if (argc > 3 && !strcmp(argv[3], "force")) {
461 rc = vm->destroyAsec(argv[2], force);
462 } else if (!strcmp(argv[1], "mount")) {
463 dumpArgs(argc, argv, 3);
465 cli->sendMsg(ResponseCode::CommandSyntaxError,
466 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false);
469 bool readOnly = true;
470 if (!strcmp(argv[5], "rw")) {
473 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly);
474 } else if (!strcmp(argv[1], "unmount")) {
475 dumpArgs(argc, argv, -1);
477 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
481 if (argc > 3 && !strcmp(argv[3], "force")) {
484 rc = vm->unmountAsec(argv[2], force);
485 } else if (!strcmp(argv[1], "rename")) {
486 dumpArgs(argc, argv, -1);
488 cli->sendMsg(ResponseCode::CommandSyntaxError,
489 "Usage: asec rename <old_id> <new_id>", false);
492 rc = vm->renameAsec(argv[2], argv[3]);
493 } else if (!strcmp(argv[1], "path")) {
494 dumpArgs(argc, argv, -1);
496 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
501 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
502 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
505 } else if (!strcmp(argv[1], "fspath")) {
506 dumpArgs(argc, argv, -1);
508 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
513 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
514 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
518 dumpArgs(argc, argv, -1);
519 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
523 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
525 rc = ResponseCode::convertFromErrno();
526 cli->sendMsg(rc, "asec operation failed", true);
532 CommandListener::ObbCmd::ObbCmd() :
536 int CommandListener::ObbCmd::runCommand(SocketClient *cli,
537 int argc, char **argv) {
539 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
543 VolumeManager *vm = VolumeManager::Instance();
546 if (!strcmp(argv[1], "list")) {
547 dumpArgs(argc, argv, -1);
549 rc = vm->listMountedObbs(cli);
550 } else if (!strcmp(argv[1], "mount")) {
551 dumpArgs(argc, argv, 3);
553 cli->sendMsg(ResponseCode::CommandSyntaxError,
554 "Usage: obb mount <filename> <key> <ownerGid>", false);
557 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
558 } else if (!strcmp(argv[1], "unmount")) {
559 dumpArgs(argc, argv, -1);
561 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
565 if (argc > 3 && !strcmp(argv[3], "force")) {
568 rc = vm->unmountObb(argv[2], force);
569 } else if (!strcmp(argv[1], "path")) {
570 dumpArgs(argc, argv, -1);
572 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
577 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
578 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
582 dumpArgs(argc, argv, -1);
583 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
587 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
589 rc = ResponseCode::convertFromErrno();
590 cli->sendMsg(rc, "obb operation failed", true);
596 CommandListener::FstrimCmd::FstrimCmd() :
597 VoldCommand("fstrim") {
599 int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
600 int argc, char **argv) {
601 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
602 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
607 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
611 VolumeManager *vm = VolumeManager::Instance();
612 std::lock_guard<std::mutex> lock(vm->getLock());
616 std::string cmd(argv[1]);
617 if (cmd == "dotrim") {
619 } else if (cmd == "dotrimbench") {
620 flags = android::vold::TrimTask::Flags::kBenchmarkAfter;
621 } else if (cmd == "dodtrim") {
622 flags = android::vold::TrimTask::Flags::kDeepTrim;
623 } else if (cmd == "dodtrimbench") {
624 flags = android::vold::TrimTask::Flags::kDeepTrim
625 | android::vold::TrimTask::Flags::kBenchmarkAfter;
628 (new android::vold::TrimTask(flags))->start();
629 return sendGenericOkFail(cli, 0);
632 static size_t kAppFuseMaxMountPointName = 32;
634 static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) {
635 if (name.size() > kAppFuseMaxMountPointName) {
636 LOG(ERROR) << "AppFuse mount name is too long.";
639 for (size_t i = 0; i < name.size(); i++) {
640 if (!isalnum(name[i])) {
641 LOG(ERROR) << "AppFuse mount name contains invalid character.";
645 *path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str());
649 static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) {
650 // Remove existing mount.
651 android::vold::ForceUnmount(path);
653 const auto opts = android::base::StringPrintf(
656 "default_permissions,"
658 "user_id=%d,group_id=%d,"
659 "context=\"u:object_r:app_fuse_file:s0\","
660 "fscontext=u:object_r:app_fusefs:s0",
665 const int result = TEMP_FAILURE_RETRY(mount(
666 "/dev/fuse", path.c_str(), "fuse",
667 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
669 PLOG(ERROR) << "Failed to mount " << path;
676 static android::status_t runCommandInNamespace(const std::string& command,
679 const std::string& path,
682 LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path
683 << " in namespace " << uid;
686 unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
687 if (dir.get() == -1) {
688 PLOG(ERROR) << "Failed to open /proc";
692 // Obtains process file descriptor.
693 const std::string pid_str = android::base::StringPrintf("%d", pid);
694 const unique_fd pid_fd(
695 openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
696 if (pid_fd.get() == -1) {
697 PLOG(ERROR) << "Failed to open /proc/" << pid;
701 // Check UID of process.
704 const int result = fstat(pid_fd.get(), &sb);
706 PLOG(ERROR) << "Failed to stat /proc/" << pid;
709 if (sb.st_uid != AID_SYSTEM) {
710 LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM
711 << ", actual=" << sb.st_uid;
716 // Matches so far, but refuse to touch if in root namespace
718 char rootName[PATH_MAX];
719 char pidName[PATH_MAX];
720 const int root_result =
721 android::vold::SaneReadLinkAt(dir.get(), "1/ns/mnt", rootName, PATH_MAX);
722 const int pid_result =
723 android::vold::SaneReadLinkAt(pid_fd.get(), "ns/mnt", pidName, PATH_MAX);
724 if (root_result == -1) {
725 LOG(ERROR) << "Failed to readlink for /proc/1/ns/mnt";
728 if (pid_result == -1) {
729 LOG(ERROR) << "Failed to readlink for /proc/" << pid << "/ns/mnt";
732 if (!strcmp(rootName, pidName)) {
733 LOG(ERROR) << "Don't mount appfuse in root namespace";
738 // We purposefully leave the namespace open across the fork
739 unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
740 if (ns_fd.get() < 0) {
741 PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt";
747 if (setns(ns_fd.get(), CLONE_NEWNS) != 0) {
748 PLOG(ERROR) << "Failed to setns";
752 if (command == "mount") {
753 _exit(mountInNamespace(uid, device_fd, path));
754 } else if (command == "unmount") {
755 // If it's just after all FD opened on mount point are closed, umount2 can fail with
756 // EBUSY. To avoid the case, specify MNT_DETACH.
757 if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 &&
758 errno != EINVAL && errno != ENOENT) {
759 PLOG(ERROR) << "Failed to unmount directory.";
762 if (rmdir(path.c_str()) != 0) {
763 PLOG(ERROR) << "Failed to remove the mount directory.";
768 LOG(ERROR) << "Unknown appfuse command " << command;
774 PLOG(ERROR) << "Failed to folk child process";
778 android::status_t status;
779 TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
784 CommandListener::AppFuseCmd::AppFuseCmd() : VoldCommand("appfuse") {}
786 int CommandListener::AppFuseCmd::runCommand(SocketClient *cli, int argc, char **argv) {
788 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
792 const std::string command(argv[1]);
794 if (command == "mount" && argc == 5) {
795 const uid_t uid = atoi(argv[2]);
796 const pid_t pid = atoi(argv[3]);
797 const std::string name(argv[4]);
799 // Check mount point name.
801 if (getMountPath(uid, name, &path) != android::OK) {
802 return cli->sendMsg(ResponseCode::CommandParameterError,
803 "Invalid mount point name.",
807 // Create directories.
809 const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0);
810 if (result != android::OK) {
811 PLOG(ERROR) << "Failed to prepare directory " << path;
812 return sendGenericOkFail(cli, result);
817 unique_fd device_fd(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC
818 if (device_fd.get() == -1) {
819 PLOG(ERROR) << "Failed to open /dev/fuse";
820 return sendGenericOkFail(cli, -errno);
825 const android::status_t result =
826 runCommandInNamespace(command, uid, pid, path, device_fd.get());
827 if (result != android::OK) {
828 return sendGenericOkFail(cli, result);
832 return sendFd(cli, device_fd.get());
833 } else if (command == "unmount" && argc == 5) {
834 const uid_t uid = atoi(argv[2]);
835 const uid_t pid = atoi(argv[3]);
836 const std::string name(argv[4]);
838 // Check mount point name.
840 if (getMountPath(uid, name, &path) != android::OK) {
841 return cli->sendMsg(ResponseCode::CommandParameterError,
842 "Invalid mount point name.",
846 const android::status_t result =
847 runCommandInNamespace(command, uid, pid, path, -1 /* device_fd */);
848 return sendGenericOkFail(cli, result);
851 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown appfuse cmd", false);
854 android::status_t CommandListener::AppFuseCmd::sendFd(SocketClient *cli, int fd) {
856 char dataBuffer[128];
857 char controlBuffer[CMSG_SPACE(sizeof(int))];
858 struct msghdr message;
861 memset(&message, 0, sizeof(struct msghdr));
862 message.msg_iov = &data;
863 message.msg_iovlen = 1;
864 message.msg_control = controlBuffer;
865 message.msg_controllen = CMSG_SPACE(sizeof(int));
868 data.iov_base = dataBuffer;
869 data.iov_len = snprintf(dataBuffer,
871 "200 %d AppFuse command succeeded",
872 cli->getCmdNum()) + 1;
875 struct cmsghdr* const controlMessage = CMSG_FIRSTHDR(&message);
876 memset(controlBuffer, 0, CMSG_SPACE(sizeof(int)));
877 controlMessage->cmsg_level = SOL_SOCKET;
878 controlMessage->cmsg_type = SCM_RIGHTS;
879 controlMessage->cmsg_len = CMSG_LEN(sizeof(int));
880 *((int *) CMSG_DATA(controlMessage)) = fd;
882 const int result = TEMP_FAILURE_RETRY(sendmsg(cli->getSocket(), &message, 0));
884 PLOG(ERROR) << "Failed to send FD from vold";