CommandListener.cpp 20.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <sys/socket.h>
19
#include <sys/types.h>
20 21
#include <netinet/in.h>
#include <arpa/inet.h>
22
#include <dirent.h>
23
#include <errno.h>
San Mehat's avatar
San Mehat committed
24
#include <fcntl.h>
25
#include <fs_mgr.h>
26
#include <stdio.h>
27
#include <string.h>
28 29
#include <stdint.h>
#include <inttypes.h>
30

San Mehat's avatar
San Mehat committed
31
#define LOG_TAG "VoldCmdListener"
32 33 34

#include <base/stringprintf.h>
#include <cutils/fs.h>
35 36 37
#include <cutils/log.h>

#include <sysutils/SocketClient.h>
38
#include <private/android_filesystem_config.h>
39 40 41

#include "CommandListener.h"
#include "VolumeManager.h"
42
#include "VolumeBase.h"
43
#include "ResponseCode.h"
44
#include "Process.h"
San Mehat's avatar
San Mehat committed
45 46
#include "Loop.h"
#include "Devmapper.h"
47
#include "cryptfs.h"
48
#include "MoveTask.h"
49
#include "TrimTask.h"
50

51 52
#define DUMP_ARGS 0

53
CommandListener::CommandListener() :
54
                 FrameworkListener("vold", true) {
San Mehat's avatar
San Mehat committed
55
    registerCmd(new DumpCmd());
San Mehat's avatar
San Mehat committed
56 57
    registerCmd(new VolumeCmd());
    registerCmd(new AsecCmd());
Kenny Root's avatar
Kenny Root committed
58
    registerCmd(new ObbCmd());
59
    registerCmd(new StorageCmd());
60
    registerCmd(new FstrimCmd());
61 62
}

63
#if DUMP_ARGS
64
void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
San Mehat's avatar
San Mehat committed
65 66 67 68 69 70
    char buffer[4096];
    char *p = buffer;

    memset(buffer, 0, sizeof(buffer));
    int i;
    for (i = 0; i < argc; i++) {
71
        unsigned int len = strlen(argv[i]) + 1; // Account for space
San Mehat's avatar
San Mehat committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        if (i == argObscure) {
            len += 2; // Account for {}
        }
        if (((p - buffer) + len) < (sizeof(buffer)-1)) {
            if (i == argObscure) {
                *p++ = '{';
                *p++ = '}';
                *p++ = ' ';
                continue;
            }
            strcpy(p, argv[i]);
            p+= strlen(argv[i]);
            if (i != (argc -1)) {
                *p++ = ' ';
            }
        }
    }
San Mehat's avatar
San Mehat committed
89
    SLOGD("%s", buffer);
San Mehat's avatar
San Mehat committed
90
}
91 92 93
#else
void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
#endif
San Mehat's avatar
San Mehat committed
94

95 96 97 98 99 100 101 102
int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) {
    if (!cond) {
        return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
    } else {
        return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
    }
}

San Mehat's avatar
San Mehat committed
103 104 105 106 107
CommandListener::DumpCmd::DumpCmd() :
                 VoldCommand("dump") {
}

int CommandListener::DumpCmd::runCommand(SocketClient *cli,
108
                                         int /*argc*/, char ** /*argv*/) {
San Mehat's avatar
San Mehat committed
109 110 111 112 113 114 115 116
    cli->sendMsg(0, "Dumping loop status", false);
    if (Loop::dumpState(cli)) {
        cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
    }
    cli->sendMsg(0, "Dumping DM status", false);
    if (Devmapper::dumpState(cli)) {
        cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
    }
117 118 119 120 121 122 123 124 125 126
    cli->sendMsg(0, "Dumping mounted filesystems", false);
    FILE *fp = fopen("/proc/mounts", "r");
    if (fp) {
        char line[1024];
        while (fgets(line, sizeof(line), fp)) {
            line[strlen(line)-1] = '\0';
            cli->sendMsg(0, line, false);;
        }
        fclose(fp);
    }
San Mehat's avatar
San Mehat committed
127 128 129 130 131

    cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
    return 0;
}

San Mehat's avatar
San Mehat committed
132 133
CommandListener::VolumeCmd::VolumeCmd() :
                 VoldCommand("volume") {
134 135
}

San Mehat's avatar
San Mehat committed
136
int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
137
                                           int argc, char **argv) {
San Mehat's avatar
San Mehat committed
138 139
    dumpArgs(argc, argv, -1);

San Mehat's avatar
San Mehat committed
140 141 142 143
    if (argc < 2) {
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
        return 0;
    }
144

San Mehat's avatar
San Mehat committed
145
    VolumeManager *vm = VolumeManager::Instance();
146
    std::lock_guard<std::mutex> lock(vm->getLock());
San Mehat's avatar
San Mehat committed
147

148
    // TODO: tease out methods not directly related to volumes
149

150 151 152 153 154 155 156
    std::string cmd(argv[1]);
    if (cmd == "reset") {
        return sendGenericOkFail(cli, vm->reset());

    } else if (cmd == "shutdown") {
        return sendGenericOkFail(cli, vm->shutdown());

157 158 159
    } else if (cmd == "debug") {
        return sendGenericOkFail(cli, vm->setDebug(true));

160 161 162 163 164 165
    } else if (cmd == "partition" && argc > 3) {
        // partition [diskId] [public|private|mixed] [ratio]
        std::string id(argv[2]);
        auto disk = vm->findDisk(id);
        if (disk == nullptr) {
            return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false);
166
        }
167 168 169 170 171 172 173 174 175 176 177 178 179 180

        std::string type(argv[3]);
        if (type == "public") {
            return sendGenericOkFail(cli, disk->partitionPublic());
        } else if (type == "private") {
            return sendGenericOkFail(cli, disk->partitionPrivate());
        } else if (type == "mixed") {
            if (argc < 4) {
                return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
            }
            int frac = atoi(argv[4]);
            return sendGenericOkFail(cli, disk->partitionMixed(frac));
        } else {
            return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
181
        }
182 183 184 185 186

    } else if (cmd == "mkdirs" && argc > 2) {
        // mkdirs [path]
        return sendGenericOkFail(cli, vm->mkdirs(argv[2]));

187 188 189
    } else if (cmd == "user_added" && argc > 3) {
        // user_added [user] [serial]
        return sendGenericOkFail(cli, vm->onUserAdded(atoi(argv[2]), atoi(argv[3])));
190

191 192 193 194 195 196 197 198 199 200 201
    } else if (cmd == "user_removed" && argc > 2) {
        // user_removed [user]
        return sendGenericOkFail(cli, vm->onUserRemoved(atoi(argv[2])));

    } else if (cmd == "user_started" && argc > 2) {
        // user_started [user]
        return sendGenericOkFail(cli, vm->onUserStarted(atoi(argv[2])));

    } else if (cmd == "user_stopped" && argc > 2) {
        // user_stopped [user]
        return sendGenericOkFail(cli, vm->onUserStopped(atoi(argv[2])));
202 203 204 205 206 207 208

    } else if (cmd == "mount" && argc > 2) {
        // mount [volId] [flags] [user]
        std::string id(argv[2]);
        auto vol = vm->findVolume(id);
        if (vol == nullptr) {
            return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
209
        }
210

211 212
        int mountFlags = (argc > 3) ? atoi(argv[3]) : 0;
        userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1;
213

214 215
        vol->setMountFlags(mountFlags);
        vol->setMountUserId(mountUserId);
216

217 218 219 220 221
        int res = vol->mount();
        if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) {
            vm->setPrimary(vol);
        }
        return sendGenericOkFail(cli, res);
222 223 224 225 226 227 228

    } else if (cmd == "unmount" && argc > 2) {
        // unmount [volId]
        std::string id(argv[2]);
        auto vol = vm->findVolume(id);
        if (vol == nullptr) {
            return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
San Mehat's avatar
San Mehat committed
229
        }
230 231 232

        return sendGenericOkFail(cli, vol->unmount());

233 234
    } else if (cmd == "format" && argc > 3) {
        // format [volId] [fsType|auto]
235
        std::string id(argv[2]);
236
        std::string fsType(argv[3]);
237 238 239
        auto vol = vm->findVolume(id);
        if (vol == nullptr) {
            return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
Jeff Sharkey's avatar
Jeff Sharkey committed
240
        }
241

242
        return sendGenericOkFail(cli, vol->format(fsType));
243 244 245 246 247 248 249 250 251 252 253

    } else if (cmd == "move_storage" && argc > 3) {
        // move_storage [fromVolId] [toVolId]
        auto fromVol = vm->findVolume(std::string(argv[2]));
        auto toVol = vm->findVolume(std::string(argv[3]));
        if (fromVol == nullptr || toVol == nullptr) {
            return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
        }

        (new android::vold::MoveTask(fromVol, toVol))->start();
        return sendGenericOkFail(cli, 0);
254 255 256 257

    } else if (cmd == "benchmark" && argc > 2) {
        // benchmark [volId]
        std::string id(argv[2]);
258
        nsecs_t res = vm->benchmarkPrivate(id);
259 260
        return cli->sendMsg(ResponseCode::CommandOkay,
                android::base::StringPrintf("%" PRId64, res).c_str(), false);
261 262 263 264 265

    } else if (cmd == "forget_partition" && argc > 2) {
        // forget_partition [partGuid]
        std::string partGuid(argv[2]);
        return sendGenericOkFail(cli, vm->forgetPartition(partGuid));
266 267 268 269 270 271

    } else if (cmd == "remount_uid" && argc > 3) {
        // remount_uid [uid] [none|default|read|write]
        uid_t uid = atoi(argv[2]);
        std::string mode(argv[3]);
        return sendGenericOkFail(cli, vm->remountUid(uid, mode));
272 273
    }

274
    return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
275 276
}

277 278 279 280 281 282
CommandListener::StorageCmd::StorageCmd() :
                 VoldCommand("storage") {
}

int CommandListener::StorageCmd::runCommand(SocketClient *cli,
                                                      int argc, char **argv) {
283 284 285
    /* Guarantied to be initialized by vold's main() before the CommandListener is active */
    extern struct fstab *fstab;

San Mehat's avatar
San Mehat committed
286 287
    dumpArgs(argc, argv, -1);

288 289 290 291 292
    if (argc < 2) {
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
        return 0;
    }

293 294 295 296 297 298 299 300 301
    if (!strcmp(argv[1], "mountall")) {
        if (argc != 2) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false);
            return 0;
        }
        fs_mgr_mount_all(fstab);
        cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false);
        return 0;
    }
302 303 304 305
    if (!strcmp(argv[1], "users")) {
        DIR *dir;
        struct dirent *de;

306 307 308 309
        if (argc < 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false);
            return 0;
        }
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        if (!(dir = opendir("/proc"))) {
            cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
            return 0;
        }

        while ((de = readdir(dir))) {
            int pid = Process::getPid(de->d_name);

            if (pid < 0) {
                continue;
            }

            char processName[255];
            Process::getProcessName(pid, processName, sizeof(processName));

            if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
                Process::checkFileMaps(pid, argv[2]) ||
                Process::checkSymLink(pid, argv[2], "cwd") ||
                Process::checkSymLink(pid, argv[2], "root") ||
                Process::checkSymLink(pid, argv[2], "exe")) {

                char msg[1024];
                snprintf(msg, sizeof(msg), "%d %s", pid, processName);
                cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
            }
        }
        closedir(dir);
        cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
    } else {
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
    }
    return 0;
}

San Mehat's avatar
San Mehat committed
344 345
CommandListener::AsecCmd::AsecCmd() :
                 VoldCommand("asec") {
346 347
}

348 349 350 351 352 353 354 355 356
void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
    DIR *d = opendir(directory);

    if (!d) {
        cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
        return;
    }

    size_t dirent_len = offsetof(struct dirent, d_name) +
357
            fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

    struct dirent *dent = (struct dirent *) malloc(dirent_len);
    if (dent == NULL) {
        cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
        return;
    }

    struct dirent *result;

    while (!readdir_r(d, dent, &result) && result != NULL) {
        if (dent->d_name[0] == '.')
            continue;
        if (dent->d_type != DT_REG)
            continue;
        size_t name_len = strlen(dent->d_name);
        if (name_len > 5 && name_len < 260 &&
                !strcmp(&dent->d_name[name_len - 5], ".asec")) {
            char id[255];
            memset(id, 0, sizeof(id));
Kenny Root's avatar
Kenny Root committed
377
            strlcpy(id, dent->d_name, name_len - 4);
378 379 380 381 382 383 384 385
            cli->sendMsg(ResponseCode::AsecListResult, id, false);
        }
    }
    closedir(d);

    free(dent);
}

San Mehat's avatar
San Mehat committed
386 387 388 389
int CommandListener::AsecCmd::runCommand(SocketClient *cli,
                                                      int argc, char **argv) {
    if (argc < 2) {
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
390 391 392
        return 0;
    }

San Mehat's avatar
San Mehat committed
393 394
    VolumeManager *vm = VolumeManager::Instance();
    int rc = 0;
395

San Mehat's avatar
San Mehat committed
396
    if (!strcmp(argv[1], "list")) {
San Mehat's avatar
San Mehat committed
397
        dumpArgs(argc, argv, -1);
398

Jeff Sharkey's avatar
Jeff Sharkey committed
399 400
        listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT);
        listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT);
San Mehat's avatar
San Mehat committed
401
    } else if (!strcmp(argv[1], "create")) {
San Mehat's avatar
San Mehat committed
402
        dumpArgs(argc, argv, 5);
403
        if (argc != 8) {
San Mehat's avatar
San Mehat committed
404
            cli->sendMsg(ResponseCode::CommandSyntaxError,
405 406
                    "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
                    "<isExternal>", false);
San Mehat's avatar
San Mehat committed
407
            return 0;
408 409
        }

San Mehat's avatar
San Mehat committed
410
        unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
411 412
        const bool isExternal = (atoi(argv[7]) == 1);
        rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
413 414 415 416 417 418 419 420
    } else if (!strcmp(argv[1], "resize")) {
        dumpArgs(argc, argv, -1);
        if (argc != 5) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
            return 0;
        }
        unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
        rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
San Mehat's avatar
San Mehat committed
421
    } else if (!strcmp(argv[1], "finalize")) {
San Mehat's avatar
San Mehat committed
422
        dumpArgs(argc, argv, -1);
San Mehat's avatar
San Mehat committed
423 424 425 426
        if (argc != 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
            return 0;
        }
427
        rc = vm->finalizeAsec(argv[2]);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    } else if (!strcmp(argv[1], "fixperms")) {
        dumpArgs(argc, argv, -1);
        if  (argc != 5) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
            return 0;
        }

        char *endptr;
        gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
        if (*endptr != '\0') {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
            return 0;
        }

        rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehat's avatar
San Mehat committed
443
    } else if (!strcmp(argv[1], "destroy")) {
San Mehat's avatar
San Mehat committed
444
        dumpArgs(argc, argv, -1);
445 446
        if (argc < 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
San Mehat's avatar
San Mehat committed
447 448
            return 0;
        }
449 450 451 452
        bool force = false;
        if (argc > 3 && !strcmp(argv[3], "force")) {
            force = true;
        }
453
        rc = vm->destroyAsec(argv[2], force);
San Mehat's avatar
San Mehat committed
454
    } else if (!strcmp(argv[1], "mount")) {
San Mehat's avatar
San Mehat committed
455
        dumpArgs(argc, argv, 3);
456
        if (argc != 6) {
San Mehat's avatar
San Mehat committed
457
            cli->sendMsg(ResponseCode::CommandSyntaxError,
458
                    "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false);
San Mehat's avatar
San Mehat committed
459 460
            return 0;
        }
461 462 463 464 465
        bool readOnly = true;
        if (!strcmp(argv[5], "rw")) {
            readOnly = false;
        }
        rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly);
San Mehat's avatar
San Mehat committed
466
    } else if (!strcmp(argv[1], "unmount")) {
San Mehat's avatar
San Mehat committed
467
        dumpArgs(argc, argv, -1);
468 469
        if (argc < 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehat's avatar
San Mehat committed
470 471
            return 0;
        }
472 473 474 475
        bool force = false;
        if (argc > 3 && !strcmp(argv[3], "force")) {
            force = true;
        }
476
        rc = vm->unmountAsec(argv[2], force);
San Mehat's avatar
San Mehat committed
477
    } else if (!strcmp(argv[1], "rename")) {
San Mehat's avatar
San Mehat committed
478
        dumpArgs(argc, argv, -1);
San Mehat's avatar
San Mehat committed
479 480 481 482 483
        if (argc != 4) {
            cli->sendMsg(ResponseCode::CommandSyntaxError,
                    "Usage: asec rename <old_id> <new_id>", false);
            return 0;
        }
484
        rc = vm->renameAsec(argv[2], argv[3]);
San Mehat's avatar
San Mehat committed
485
    } else if (!strcmp(argv[1], "path")) {
San Mehat's avatar
San Mehat committed
486
        dumpArgs(argc, argv, -1);
San Mehat's avatar
San Mehat committed
487 488 489 490 491
        if (argc != 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
            return 0;
        }
        char path[255];
492

493
        if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehat's avatar
San Mehat committed
494
            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
495
            return 0;
San Mehat's avatar
San Mehat committed
496
        }
497 498 499 500 501 502 503 504 505 506 507 508
    } else if (!strcmp(argv[1], "fspath")) {
        dumpArgs(argc, argv, -1);
        if (argc != 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
            return 0;
        }
        char path[255];

        if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
            return 0;
        }
509
    } else {
San Mehat's avatar
San Mehat committed
510
        dumpArgs(argc, argv, -1);
San Mehat's avatar
San Mehat committed
511
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
512 513
    }

514 515 516 517 518 519 520
    if (!rc) {
        cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
    } else {
        rc = ResponseCode::convertFromErrno();
        cli->sendMsg(rc, "asec operation failed", true);
    }

521 522
    return 0;
}
San Mehat's avatar
San Mehat committed
523

Kenny Root's avatar
Kenny Root committed
524 525
CommandListener::ObbCmd::ObbCmd() :
                 VoldCommand("obb") {
526 527
}

Kenny Root's avatar
Kenny Root committed
528
int CommandListener::ObbCmd::runCommand(SocketClient *cli,
529 530 531 532 533 534 535 536 537
                                                      int argc, char **argv) {
    if (argc < 2) {
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
        return 0;
    }

    VolumeManager *vm = VolumeManager::Instance();
    int rc = 0;

Kenny Root's avatar
Kenny Root committed
538 539 540 541 542
    if (!strcmp(argv[1], "list")) {
        dumpArgs(argc, argv, -1);

        rc = vm->listMountedObbs(cli);
    } else if (!strcmp(argv[1], "mount")) {
543 544 545
            dumpArgs(argc, argv, 3);
            if (argc != 5) {
                cli->sendMsg(ResponseCode::CommandSyntaxError,
546
                        "Usage: obb mount <filename> <key> <ownerGid>", false);
547 548
                return 0;
            }
Kenny Root's avatar
Kenny Root committed
549
            rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
550 551 552
    } else if (!strcmp(argv[1], "unmount")) {
        dumpArgs(argc, argv, -1);
        if (argc < 3) {
Kenny Root's avatar
Kenny Root committed
553
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
554 555 556 557 558 559
            return 0;
        }
        bool force = false;
        if (argc > 3 && !strcmp(argv[3], "force")) {
            force = true;
        }
Kenny Root's avatar
Kenny Root committed
560 561 562 563 564 565 566 567 568 569 570 571 572
        rc = vm->unmountObb(argv[2], force);
    } else if (!strcmp(argv[1], "path")) {
        dumpArgs(argc, argv, -1);
        if (argc != 3) {
            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
            return 0;
        }
        char path[255];

        if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
            return 0;
        }
573 574
    } else {
        dumpArgs(argc, argv, -1);
Kenny Root's avatar
Kenny Root committed
575
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
576 577 578
    }

    if (!rc) {
Kenny Root's avatar
Kenny Root committed
579
        cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
580 581
    } else {
        rc = ResponseCode::convertFromErrno();
Kenny Root's avatar
Kenny Root committed
582
        cli->sendMsg(rc, "obb operation failed", true);
583 584 585 586 587
    }

    return 0;
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
CommandListener::FstrimCmd::FstrimCmd() :
                 VoldCommand("fstrim") {
}
int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
                                                      int argc, char **argv) {
    if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
        cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
        return 0;
    }

    if (argc < 2) {
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
        return 0;
    }

603 604
    VolumeManager *vm = VolumeManager::Instance();
    std::lock_guard<std::mutex> lock(vm->getLock());
605

606
    int flags = 0;
607

608 609 610 611 612 613 614 615 616 617 618
    std::string cmd(argv[1]);
    if (cmd == "dotrim") {
        flags = 0;
    } else if (cmd == "dotrimbench") {
        flags = android::vold::TrimTask::Flags::kBenchmarkAfter;
    } else if (cmd == "dodtrim") {
        flags = android::vold::TrimTask::Flags::kDeepTrim;
    } else if (cmd == "dodtrimbench") {
        flags = android::vold::TrimTask::Flags::kDeepTrim
                | android::vold::TrimTask::Flags::kBenchmarkAfter;
    }
619

620 621
    (new android::vold::TrimTask(flags))->start();
    return sendGenericOkFail(cli, 0);
622
}