CommandListener.cpp revision 9caab76c6b5aefdeeb1715a3695491ca793b8c18
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 <string.h>
26
27#define LOG_TAG "VoldCmdListener"
28#include <cutils/log.h>
29
30#include <sysutils/SocketClient.h>
31#include <private/android_filesystem_config.h>
32
33#include "CommandListener.h"
34#include "VolumeManager.h"
35#include "ResponseCode.h"
36#include "Process.h"
37#include "Xwarp.h"
38#include "Loop.h"
39#include "Devmapper.h"
40#include "cryptfs.h"
41#include "fstrim.h"
42
43#define DUMP_ARGS 0
44
45CommandListener::CommandListener() :
46                 FrameworkListener("vold", true) {
47    registerCmd(new DumpCmd());
48    registerCmd(new VolumeCmd());
49    registerCmd(new AsecCmd());
50    registerCmd(new ObbCmd());
51    registerCmd(new StorageCmd());
52    registerCmd(new XwarpCmd());
53    registerCmd(new CryptfsCmd());
54    registerCmd(new FstrimCmd());
55}
56
57void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
58#if DUMP_ARGS
59    char buffer[4096];
60    char *p = buffer;
61
62    memset(buffer, 0, sizeof(buffer));
63    int i;
64    for (i = 0; i < argc; i++) {
65        unsigned int len = strlen(argv[i]) + 1; // Account for space
66        if (i == argObscure) {
67            len += 2; // Account for {}
68        }
69        if (((p - buffer) + len) < (sizeof(buffer)-1)) {
70            if (i == argObscure) {
71                *p++ = '{';
72                *p++ = '}';
73                *p++ = ' ';
74                continue;
75            }
76            strcpy(p, argv[i]);
77            p+= strlen(argv[i]);
78            if (i != (argc -1)) {
79                *p++ = ' ';
80            }
81        }
82    }
83    SLOGD("%s", buffer);
84#endif
85}
86
87CommandListener::DumpCmd::DumpCmd() :
88                 VoldCommand("dump") {
89}
90
91int CommandListener::DumpCmd::runCommand(SocketClient *cli,
92                                         int argc, char **argv) {
93    cli->sendMsg(0, "Dumping loop status", false);
94    if (Loop::dumpState(cli)) {
95        cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
96    }
97    cli->sendMsg(0, "Dumping DM status", false);
98    if (Devmapper::dumpState(cli)) {
99        cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
100    }
101    cli->sendMsg(0, "Dumping mounted filesystems", false);
102    FILE *fp = fopen("/proc/mounts", "r");
103    if (fp) {
104        char line[1024];
105        while (fgets(line, sizeof(line), fp)) {
106            line[strlen(line)-1] = '\0';
107            cli->sendMsg(0, line, false);;
108        }
109        fclose(fp);
110    }
111
112    cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
113    return 0;
114}
115
116
117CommandListener::VolumeCmd::VolumeCmd() :
118                 VoldCommand("volume") {
119}
120
121int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
122                                                      int argc, char **argv) {
123    dumpArgs(argc, argv, -1);
124
125    if (argc < 2) {
126        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
127        return 0;
128    }
129
130    VolumeManager *vm = VolumeManager::Instance();
131    int rc = 0;
132
133    if (!strcmp(argv[1], "list")) {
134        return vm->listVolumes(cli);
135    } else if (!strcmp(argv[1], "debug")) {
136        if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) {
137            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false);
138            return 0;
139        }
140        vm->setDebug(!strcmp(argv[2], "on") ? true : false);
141    } else if (!strcmp(argv[1], "mount")) {
142        if (argc != 3) {
143            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);
144            return 0;
145        }
146        rc = vm->mountVolume(argv[2]);
147    } else if (!strcmp(argv[1], "unmount")) {
148        if (argc < 3 || argc > 4 ||
149           ((argc == 4 && strcmp(argv[3], "force")) &&
150            (argc == 4 && strcmp(argv[3], "force_and_revert")))) {
151            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false);
152            return 0;
153        }
154
155        bool force = false;
156        bool revert = false;
157        if (argc >= 4 && !strcmp(argv[3], "force")) {
158            force = true;
159        } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) {
160            force = true;
161            revert = true;
162        }
163        rc = vm->unmountVolume(argv[2], force, revert);
164    } else if (!strcmp(argv[1], "format")) {
165        if (argc < 3 || argc > 4 ||
166            (argc == 4 && strcmp(argv[3], "wipe"))) {
167            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path> [wipe]", false);
168            return 0;
169        }
170        bool wipe = false;
171        if (argc >= 4 && !strcmp(argv[3], "wipe")) {
172            wipe = true;
173        }
174        rc = vm->formatVolume(argv[2], wipe);
175    } else if (!strcmp(argv[1], "share")) {
176        if (argc != 4) {
177            cli->sendMsg(ResponseCode::CommandSyntaxError,
178                    "Usage: volume share <path> <method>", false);
179            return 0;
180        }
181        rc = vm->shareVolume(argv[2], argv[3]);
182    } else if (!strcmp(argv[1], "unshare")) {
183        if (argc != 4) {
184            cli->sendMsg(ResponseCode::CommandSyntaxError,
185                    "Usage: volume unshare <path> <method>", false);
186            return 0;
187        }
188        rc = vm->unshareVolume(argv[2], argv[3]);
189    } else if (!strcmp(argv[1], "shared")) {
190        bool enabled = false;
191        if (argc != 4) {
192            cli->sendMsg(ResponseCode::CommandSyntaxError,
193                    "Usage: volume shared <path> <method>", false);
194            return 0;
195        }
196
197        if (vm->shareEnabled(argv[2], argv[3], &enabled)) {
198            cli->sendMsg(
199                    ResponseCode::OperationFailed, "Failed to determine share enable state", true);
200        } else {
201            cli->sendMsg(ResponseCode::ShareEnabledResult,
202                    (enabled ? "Share enabled" : "Share disabled"), false);
203        }
204        return 0;
205    } else {
206        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);
207    }
208
209    if (!rc) {
210        cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
211    } else {
212        int erno = errno;
213        rc = ResponseCode::convertFromErrno();
214        cli->sendMsg(rc, "volume operation failed", true);
215    }
216
217    return 0;
218}
219
220CommandListener::StorageCmd::StorageCmd() :
221                 VoldCommand("storage") {
222}
223
224int CommandListener::StorageCmd::runCommand(SocketClient *cli,
225                                                      int argc, char **argv) {
226    dumpArgs(argc, argv, -1);
227
228    if (argc < 2) {
229        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
230        return 0;
231    }
232
233    if (!strcmp(argv[1], "users")) {
234        DIR *dir;
235        struct dirent *de;
236
237        if (!(dir = opendir("/proc"))) {
238            cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
239            return 0;
240        }
241
242        while ((de = readdir(dir))) {
243            int pid = Process::getPid(de->d_name);
244
245            if (pid < 0) {
246                continue;
247            }
248
249            char processName[255];
250            Process::getProcessName(pid, processName, sizeof(processName));
251
252            if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
253                Process::checkFileMaps(pid, argv[2]) ||
254                Process::checkSymLink(pid, argv[2], "cwd") ||
255                Process::checkSymLink(pid, argv[2], "root") ||
256                Process::checkSymLink(pid, argv[2], "exe")) {
257
258                char msg[1024];
259                snprintf(msg, sizeof(msg), "%d %s", pid, processName);
260                cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
261            }
262        }
263        closedir(dir);
264        cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
265    } else {
266        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
267    }
268    return 0;
269}
270
271CommandListener::AsecCmd::AsecCmd() :
272                 VoldCommand("asec") {
273}
274
275void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
276    DIR *d = opendir(directory);
277
278    if (!d) {
279        cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
280        return;
281    }
282
283    size_t dirent_len = offsetof(struct dirent, d_name) +
284            fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
285
286    struct dirent *dent = (struct dirent *) malloc(dirent_len);
287    if (dent == NULL) {
288        cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
289        return;
290    }
291
292    struct dirent *result;
293
294    while (!readdir_r(d, dent, &result) && result != NULL) {
295        if (dent->d_name[0] == '.')
296            continue;
297        if (dent->d_type != DT_REG)
298            continue;
299        size_t name_len = strlen(dent->d_name);
300        if (name_len > 5 && name_len < 260 &&
301                !strcmp(&dent->d_name[name_len - 5], ".asec")) {
302            char id[255];
303            memset(id, 0, sizeof(id));
304            strlcpy(id, dent->d_name, name_len - 4);
305            cli->sendMsg(ResponseCode::AsecListResult, id, false);
306        }
307    }
308    closedir(d);
309
310    free(dent);
311}
312
313int CommandListener::AsecCmd::runCommand(SocketClient *cli,
314                                                      int argc, char **argv) {
315    if (argc < 2) {
316        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
317        return 0;
318    }
319
320    VolumeManager *vm = VolumeManager::Instance();
321    int rc = 0;
322
323    if (!strcmp(argv[1], "list")) {
324        dumpArgs(argc, argv, -1);
325
326        listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT);
327        listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT);
328    } else if (!strcmp(argv[1], "create")) {
329        dumpArgs(argc, argv, 5);
330        if (argc != 8) {
331            cli->sendMsg(ResponseCode::CommandSyntaxError,
332                    "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
333                    "<isExternal>", false);
334            return 0;
335        }
336
337        unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
338        const bool isExternal = (atoi(argv[7]) == 1);
339        rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
340    } else if (!strcmp(argv[1], "finalize")) {
341        dumpArgs(argc, argv, -1);
342        if (argc != 3) {
343            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
344            return 0;
345        }
346        rc = vm->finalizeAsec(argv[2]);
347    } else if (!strcmp(argv[1], "fixperms")) {
348        dumpArgs(argc, argv, -1);
349        if  (argc != 5) {
350            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
351            return 0;
352        }
353
354        char *endptr;
355        gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
356        if (*endptr != '\0') {
357            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
358            return 0;
359        }
360
361        rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
362    } else if (!strcmp(argv[1], "destroy")) {
363        dumpArgs(argc, argv, -1);
364        if (argc < 3) {
365            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
366            return 0;
367        }
368        bool force = false;
369        if (argc > 3 && !strcmp(argv[3], "force")) {
370            force = true;
371        }
372        rc = vm->destroyAsec(argv[2], force);
373    } else if (!strcmp(argv[1], "mount")) {
374        dumpArgs(argc, argv, 3);
375        if (argc != 5) {
376            cli->sendMsg(ResponseCode::CommandSyntaxError,
377                    "Usage: asec mount <namespace-id> <key> <ownerUid>", false);
378            return 0;
379        }
380        rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
381    } else if (!strcmp(argv[1], "unmount")) {
382        dumpArgs(argc, argv, -1);
383        if (argc < 3) {
384            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
385            return 0;
386        }
387        bool force = false;
388        if (argc > 3 && !strcmp(argv[3], "force")) {
389            force = true;
390        }
391        rc = vm->unmountAsec(argv[2], force);
392    } else if (!strcmp(argv[1], "rename")) {
393        dumpArgs(argc, argv, -1);
394        if (argc != 4) {
395            cli->sendMsg(ResponseCode::CommandSyntaxError,
396                    "Usage: asec rename <old_id> <new_id>", false);
397            return 0;
398        }
399        rc = vm->renameAsec(argv[2], argv[3]);
400    } else if (!strcmp(argv[1], "path")) {
401        dumpArgs(argc, argv, -1);
402        if (argc != 3) {
403            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
404            return 0;
405        }
406        char path[255];
407
408        if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
409            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
410            return 0;
411        }
412    } else if (!strcmp(argv[1], "fspath")) {
413        dumpArgs(argc, argv, -1);
414        if (argc != 3) {
415            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
416            return 0;
417        }
418        char path[255];
419
420        if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
421            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
422            return 0;
423        }
424    } else {
425        dumpArgs(argc, argv, -1);
426        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
427    }
428
429    if (!rc) {
430        cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
431    } else {
432        rc = ResponseCode::convertFromErrno();
433        cli->sendMsg(rc, "asec operation failed", true);
434    }
435
436    return 0;
437}
438
439CommandListener::ObbCmd::ObbCmd() :
440                 VoldCommand("obb") {
441}
442
443int CommandListener::ObbCmd::runCommand(SocketClient *cli,
444                                                      int argc, char **argv) {
445    if (argc < 2) {
446        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
447        return 0;
448    }
449
450    VolumeManager *vm = VolumeManager::Instance();
451    int rc = 0;
452
453    if (!strcmp(argv[1], "list")) {
454        dumpArgs(argc, argv, -1);
455
456        rc = vm->listMountedObbs(cli);
457    } else if (!strcmp(argv[1], "mount")) {
458            dumpArgs(argc, argv, 3);
459            if (argc != 5) {
460                cli->sendMsg(ResponseCode::CommandSyntaxError,
461                        "Usage: obb mount <filename> <key> <ownerGid>", false);
462                return 0;
463            }
464            rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
465    } else if (!strcmp(argv[1], "unmount")) {
466        dumpArgs(argc, argv, -1);
467        if (argc < 3) {
468            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [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->unmountObb(argv[2], force);
476    } else if (!strcmp(argv[1], "path")) {
477        dumpArgs(argc, argv, -1);
478        if (argc != 3) {
479            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
480            return 0;
481        }
482        char path[255];
483
484        if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
485            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
486            return 0;
487        }
488    } else {
489        dumpArgs(argc, argv, -1);
490        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
491    }
492
493    if (!rc) {
494        cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
495    } else {
496        rc = ResponseCode::convertFromErrno();
497        cli->sendMsg(rc, "obb operation failed", true);
498    }
499
500    return 0;
501}
502
503CommandListener::XwarpCmd::XwarpCmd() :
504                 VoldCommand("xwarp") {
505}
506
507int CommandListener::XwarpCmd::runCommand(SocketClient *cli,
508                                                      int argc, char **argv) {
509    if (argc < 2) {
510        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
511        return 0;
512    }
513
514    if (!strcmp(argv[1], "enable")) {
515        if (Xwarp::enable()) {
516            cli->sendMsg(ResponseCode::OperationFailed, "Failed to enable xwarp", true);
517            return 0;
518        }
519
520        cli->sendMsg(ResponseCode::CommandOkay, "Xwarp mirroring started", false);
521    } else if (!strcmp(argv[1], "disable")) {
522        if (Xwarp::disable()) {
523            cli->sendMsg(ResponseCode::OperationFailed, "Failed to disable xwarp", true);
524            return 0;
525        }
526
527        cli->sendMsg(ResponseCode::CommandOkay, "Xwarp disabled", false);
528    } else if (!strcmp(argv[1], "status")) {
529        char msg[255];
530        bool r;
531        unsigned mirrorPos, maxSize;
532
533        if (Xwarp::status(&r, &mirrorPos, &maxSize)) {
534            cli->sendMsg(ResponseCode::OperationFailed, "Failed to get xwarp status", true);
535            return 0;
536        }
537        snprintf(msg, sizeof(msg), "%s %u %u", (r ? "ready" : "not-ready"), mirrorPos, maxSize);
538        cli->sendMsg(ResponseCode::XwarpStatusResult, msg, false);
539    } else {
540        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
541    }
542
543    return 0;
544}
545
546CommandListener::CryptfsCmd::CryptfsCmd() :
547                 VoldCommand("cryptfs") {
548}
549
550int CommandListener::CryptfsCmd::runCommand(SocketClient *cli,
551                                                      int argc, char **argv) {
552    if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
553        cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
554        return 0;
555    }
556
557    if (argc < 2) {
558        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
559        return 0;
560    }
561
562    int rc = 0;
563
564    if (!strcmp(argv[1], "checkpw")) {
565        if (argc != 3) {
566            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
567            return 0;
568        }
569        dumpArgs(argc, argv, 2);
570        rc = cryptfs_check_passwd(argv[2]);
571    } else if (!strcmp(argv[1], "restart")) {
572        if (argc != 2) {
573            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
574            return 0;
575        }
576        dumpArgs(argc, argv, -1);
577        rc = cryptfs_restart();
578    } else if (!strcmp(argv[1], "cryptocomplete")) {
579        if (argc != 2) {
580            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
581            return 0;
582        }
583        dumpArgs(argc, argv, -1);
584        rc = cryptfs_crypto_complete();
585    } else if (!strcmp(argv[1], "enablecrypto")) {
586        if ( (argc != 4) || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
587            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs enablecrypto <wipe|inplace> <passwd>", false);
588            return 0;
589        }
590        dumpArgs(argc, argv, 3);
591        rc = cryptfs_enable(argv[2], argv[3]);
592    } else if (!strcmp(argv[1], "changepw")) {
593        if (argc != 3) {
594            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs changepw <newpasswd>", false);
595            return 0;
596        }
597        SLOGD("cryptfs changepw {}");
598        rc = cryptfs_changepw(argv[2]);
599    } else if (!strcmp(argv[1], "verifypw")) {
600        if (argc != 3) {
601            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
602            return 0;
603        }
604        SLOGD("cryptfs verifypw {}");
605        rc = cryptfs_verify_passwd(argv[2]);
606    } else if (!strcmp(argv[1], "getfield")) {
607        char valbuf[PROPERTY_VALUE_MAX];
608
609        if (argc != 3) {
610            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
611            return 0;
612        }
613        dumpArgs(argc, argv, -1);
614        rc = cryptfs_getfield(argv[2], valbuf, sizeof(valbuf));
615        if (rc == 0) {
616            cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
617        }
618    } else if (!strcmp(argv[1], "setfield")) {
619        if (argc != 4) {
620            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
621            return 0;
622        }
623        dumpArgs(argc, argv, -1);
624        rc = cryptfs_setfield(argv[2], argv[3]);
625    } else {
626        dumpArgs(argc, argv, -1);
627        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
628    }
629
630    // Always report that the command succeeded and return the error code.
631    // The caller will check the return value to see what the error was.
632    char msg[255];
633    snprintf(msg, sizeof(msg), "%d", rc);
634    cli->sendMsg(ResponseCode::CommandOkay, msg, false);
635
636    return 0;
637}
638
639CommandListener::FstrimCmd::FstrimCmd() :
640                 VoldCommand("fstrim") {
641}
642int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
643                                                      int argc, char **argv) {
644    if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
645        cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
646        return 0;
647    }
648
649    if (argc < 2) {
650        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
651        return 0;
652    }
653
654    int rc = 0;
655
656    if (!strcmp(argv[1], "dotrim")) {
657        if (argc != 2) {
658            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false);
659            return 0;
660        }
661        dumpArgs(argc, argv, -1);
662        rc = fstrim_filesystems();
663    } else {
664        dumpArgs(argc, argv, -1);
665        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false);
666    }
667
668    // Always report that the command succeeded and return the error code.
669    // The caller will check the return value to see what the error was.
670    char msg[255];
671    snprintf(msg, sizeof(msg), "%d", rc);
672    cli->sendMsg(ResponseCode::CommandOkay, msg, false);
673
674    return 0;
675}
676