blob: c8d684808b22810e43b2eb44a3258ce3fe0d064d [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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>
San Mehata19b2502010-01-06 10:33:53 -080019#include <sys/types.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <netinet/in.h>
21#include <arpa/inet.h>
San Mehata19b2502010-01-06 10:33:53 -080022#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070023#include <errno.h>
San Mehat2350c442010-03-02 13:16:50 -080024#include <fcntl.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080025#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070026
San Mehatd9a4e352010-03-12 13:32:47 -080027#define LOG_TAG "VoldCmdListener"
San Mehatf1b736b2009-10-10 17:22:08 -070028#include <cutils/log.h>
29
30#include <sysutils/SocketClient.h>
Ken Sumrall3ad90722011-10-04 20:38:29 -070031#include <private/android_filesystem_config.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#include "CommandListener.h"
34#include "VolumeManager.h"
San Mehata2677e42009-12-13 10:40:18 -080035#include "ResponseCode.h"
San Mehat586536c2010-02-16 17:12:00 -080036#include "Process.h"
San Mehat2350c442010-03-02 13:16:50 -080037#include "Xwarp.h"
San Mehatd9a4e352010-03-12 13:32:47 -080038#include "Loop.h"
39#include "Devmapper.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080040#include "cryptfs.h"
Ken Sumrallb87937c2013-03-19 21:46:39 -070041#include "fstrim.h"
San Mehatf1b736b2009-10-10 17:22:08 -070042
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080043#define DUMP_ARGS 0
44
San Mehatf1b736b2009-10-10 17:22:08 -070045CommandListener::CommandListener() :
Robert Greenwalt149aa3e2012-02-16 14:43:03 -080046 FrameworkListener("vold", true) {
San Mehatd9a4e352010-03-12 13:32:47 -080047 registerCmd(new DumpCmd());
San Mehateba65e92010-01-29 05:15:16 -080048 registerCmd(new VolumeCmd());
49 registerCmd(new AsecCmd());
Kenny Root508c0e12010-07-12 09:59:49 -070050 registerCmd(new ObbCmd());
San Mehat586536c2010-02-16 17:12:00 -080051 registerCmd(new StorageCmd());
San Mehat2350c442010-03-02 13:16:50 -080052 registerCmd(new XwarpCmd());
Ken Sumrall8f869aa2010-12-03 03:47:09 -080053 registerCmd(new CryptfsCmd());
Ken Sumrallb87937c2013-03-19 21:46:39 -070054 registerCmd(new FstrimCmd());
San Mehatf1b736b2009-10-10 17:22:08 -070055}
56
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080057#if DUMP_ARGS
Mark Salyzyn5eecc442014-02-12 14:16:14 -080058void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
San Mehatd9a4e352010-03-12 13:32:47 -080059 char buffer[4096];
60 char *p = buffer;
61
62 memset(buffer, 0, sizeof(buffer));
63 int i;
64 for (i = 0; i < argc; i++) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -080065 unsigned int len = strlen(argv[i]) + 1; // Account for space
San Mehatd9a4e352010-03-12 13:32:47 -080066 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 }
San Mehat97ac40e2010-03-24 10:24:19 -070083 SLOGD("%s", buffer);
San Mehatd9a4e352010-03-12 13:32:47 -080084}
Mark Salyzyn5eecc442014-02-12 14:16:14 -080085#else
86void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
87#endif
San Mehatd9a4e352010-03-12 13:32:47 -080088
89CommandListener::DumpCmd::DumpCmd() :
90 VoldCommand("dump") {
91}
92
93int CommandListener::DumpCmd::runCommand(SocketClient *cli,
Mark Salyzyn5eecc442014-02-12 14:16:14 -080094 int /*argc*/, char ** /*argv*/) {
San Mehatd9a4e352010-03-12 13:32:47 -080095 cli->sendMsg(0, "Dumping loop status", false);
96 if (Loop::dumpState(cli)) {
97 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
98 }
99 cli->sendMsg(0, "Dumping DM status", false);
100 if (Devmapper::dumpState(cli)) {
101 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
102 }
San Mehat96597e82010-03-17 09:50:54 -0700103 cli->sendMsg(0, "Dumping mounted filesystems", false);
104 FILE *fp = fopen("/proc/mounts", "r");
105 if (fp) {
106 char line[1024];
107 while (fgets(line, sizeof(line), fp)) {
108 line[strlen(line)-1] = '\0';
109 cli->sendMsg(0, line, false);;
110 }
111 fclose(fp);
112 }
San Mehatd9a4e352010-03-12 13:32:47 -0800113
114 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
115 return 0;
116}
117
118
San Mehateba65e92010-01-29 05:15:16 -0800119CommandListener::VolumeCmd::VolumeCmd() :
120 VoldCommand("volume") {
San Mehatf1b736b2009-10-10 17:22:08 -0700121}
122
San Mehateba65e92010-01-29 05:15:16 -0800123int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
San Mehatf1b736b2009-10-10 17:22:08 -0700124 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800125 dumpArgs(argc, argv, -1);
126
San Mehateba65e92010-01-29 05:15:16 -0800127 if (argc < 2) {
128 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
129 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700130 }
131
San Mehateba65e92010-01-29 05:15:16 -0800132 VolumeManager *vm = VolumeManager::Instance();
133 int rc = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700134
San Mehateba65e92010-01-29 05:15:16 -0800135 if (!strcmp(argv[1], "list")) {
136 return vm->listVolumes(cli);
San Mehatd9a4e352010-03-12 13:32:47 -0800137 } else if (!strcmp(argv[1], "debug")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700138 if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) {
139 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false);
140 return 0;
141 }
142 vm->setDebug(!strcmp(argv[2], "on") ? true : false);
San Mehateba65e92010-01-29 05:15:16 -0800143 } else if (!strcmp(argv[1], "mount")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700144 if (argc != 3) {
145 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);
146 return 0;
147 }
San Mehateba65e92010-01-29 05:15:16 -0800148 rc = vm->mountVolume(argv[2]);
149 } else if (!strcmp(argv[1], "unmount")) {
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700150 if (argc < 3 || argc > 4 ||
151 ((argc == 4 && strcmp(argv[3], "force")) &&
152 (argc == 4 && strcmp(argv[3], "force_and_revert")))) {
153 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700154 return 0;
155 }
156
San Mehat4ba89482010-02-18 09:00:18 -0800157 bool force = false;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700158 bool revert = false;
San Mehat4ba89482010-02-18 09:00:18 -0800159 if (argc >= 4 && !strcmp(argv[3], "force")) {
160 force = true;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700161 } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) {
162 force = true;
163 revert = true;
San Mehat4ba89482010-02-18 09:00:18 -0800164 }
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700165 rc = vm->unmountVolume(argv[2], force, revert);
San Mehateba65e92010-01-29 05:15:16 -0800166 } else if (!strcmp(argv[1], "format")) {
Ken Sumrall9caab762013-06-11 19:10:20 -0700167 if (argc < 3 || argc > 4 ||
168 (argc == 4 && strcmp(argv[3], "wipe"))) {
169 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path> [wipe]", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700170 return 0;
171 }
Ken Sumrall9caab762013-06-11 19:10:20 -0700172 bool wipe = false;
173 if (argc >= 4 && !strcmp(argv[3], "wipe")) {
174 wipe = true;
175 }
176 rc = vm->formatVolume(argv[2], wipe);
San Mehateba65e92010-01-29 05:15:16 -0800177 } else if (!strcmp(argv[1], "share")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700178 if (argc != 4) {
179 cli->sendMsg(ResponseCode::CommandSyntaxError,
180 "Usage: volume share <path> <method>", false);
181 return 0;
182 }
San Mehatb9aed742010-02-04 15:07:01 -0800183 rc = vm->shareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800184 } else if (!strcmp(argv[1], "unshare")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700185 if (argc != 4) {
186 cli->sendMsg(ResponseCode::CommandSyntaxError,
187 "Usage: volume unshare <path> <method>", false);
188 return 0;
189 }
San Mehatb9aed742010-02-04 15:07:01 -0800190 rc = vm->unshareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800191 } else if (!strcmp(argv[1], "shared")) {
192 bool enabled = false;
San Mehat57df7bf2010-03-14 13:41:27 -0700193 if (argc != 4) {
194 cli->sendMsg(ResponseCode::CommandSyntaxError,
195 "Usage: volume shared <path> <method>", false);
196 return 0;
197 }
San Mehatf1b736b2009-10-10 17:22:08 -0700198
San Mehat2b225522010-02-03 10:26:40 -0800199 if (vm->shareEnabled(argv[2], argv[3], &enabled)) {
San Mehateba65e92010-01-29 05:15:16 -0800200 cli->sendMsg(
201 ResponseCode::OperationFailed, "Failed to determine share enable state", true);
202 } else {
203 cli->sendMsg(ResponseCode::ShareEnabledResult,
204 (enabled ? "Share enabled" : "Share disabled"), false);
205 }
San Mehatb9aed742010-02-04 15:07:01 -0800206 return 0;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700207 } else if (!strcmp(argv[1], "mkdirs")) {
208 if (argc != 3) {
209 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mkdirs <path>", false);
210 return 0;
211 }
212 rc = vm->mkdirs(argv[2]);
San Mehat49e2bce2009-10-12 16:29:01 -0700213 } else {
San Mehateba65e92010-01-29 05:15:16 -0800214 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);
215 }
216
217 if (!rc) {
218 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
219 } else {
San Mehat8f2875b2010-02-18 11:40:49 -0800220 int erno = errno;
221 rc = ResponseCode::convertFromErrno();
San Mehateba65e92010-01-29 05:15:16 -0800222 cli->sendMsg(rc, "volume operation failed", true);
San Mehat49e2bce2009-10-12 16:29:01 -0700223 }
224
San Mehatf1b736b2009-10-10 17:22:08 -0700225 return 0;
226}
227
San Mehat586536c2010-02-16 17:12:00 -0800228CommandListener::StorageCmd::StorageCmd() :
229 VoldCommand("storage") {
230}
231
232int CommandListener::StorageCmd::runCommand(SocketClient *cli,
233 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800234 dumpArgs(argc, argv, -1);
235
San Mehat586536c2010-02-16 17:12:00 -0800236 if (argc < 2) {
237 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
238 return 0;
239 }
240
241 if (!strcmp(argv[1], "users")) {
242 DIR *dir;
243 struct dirent *de;
244
JP Abgralledf7adf2014-03-12 10:41:05 -0700245 if (argc < 3) {
246 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false);
247 return 0;
248 }
San Mehat586536c2010-02-16 17:12:00 -0800249 if (!(dir = opendir("/proc"))) {
250 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
251 return 0;
252 }
253
254 while ((de = readdir(dir))) {
255 int pid = Process::getPid(de->d_name);
256
257 if (pid < 0) {
258 continue;
259 }
260
261 char processName[255];
262 Process::getProcessName(pid, processName, sizeof(processName));
263
264 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
265 Process::checkFileMaps(pid, argv[2]) ||
266 Process::checkSymLink(pid, argv[2], "cwd") ||
267 Process::checkSymLink(pid, argv[2], "root") ||
268 Process::checkSymLink(pid, argv[2], "exe")) {
269
270 char msg[1024];
271 snprintf(msg, sizeof(msg), "%d %s", pid, processName);
272 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
273 }
274 }
275 closedir(dir);
276 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
277 } else {
278 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
279 }
280 return 0;
281}
282
San Mehateba65e92010-01-29 05:15:16 -0800283CommandListener::AsecCmd::AsecCmd() :
284 VoldCommand("asec") {
San Mehata19b2502010-01-06 10:33:53 -0800285}
286
Kenny Root344ca102012-04-03 17:23:01 -0700287void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
288 DIR *d = opendir(directory);
289
290 if (!d) {
291 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
292 return;
293 }
294
295 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -0700296 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Kenny Root344ca102012-04-03 17:23:01 -0700297
298 struct dirent *dent = (struct dirent *) malloc(dirent_len);
299 if (dent == NULL) {
300 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
301 return;
302 }
303
304 struct dirent *result;
305
306 while (!readdir_r(d, dent, &result) && result != NULL) {
307 if (dent->d_name[0] == '.')
308 continue;
309 if (dent->d_type != DT_REG)
310 continue;
311 size_t name_len = strlen(dent->d_name);
312 if (name_len > 5 && name_len < 260 &&
313 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
314 char id[255];
315 memset(id, 0, sizeof(id));
Kenny Root7b0bc852012-04-27 15:33:58 -0700316 strlcpy(id, dent->d_name, name_len - 4);
Kenny Root344ca102012-04-03 17:23:01 -0700317 cli->sendMsg(ResponseCode::AsecListResult, id, false);
318 }
319 }
320 closedir(d);
321
322 free(dent);
323}
324
San Mehateba65e92010-01-29 05:15:16 -0800325int CommandListener::AsecCmd::runCommand(SocketClient *cli,
326 int argc, char **argv) {
327 if (argc < 2) {
328 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
San Mehata19b2502010-01-06 10:33:53 -0800329 return 0;
330 }
331
San Mehateba65e92010-01-29 05:15:16 -0800332 VolumeManager *vm = VolumeManager::Instance();
333 int rc = 0;
San Mehata19b2502010-01-06 10:33:53 -0800334
San Mehateba65e92010-01-29 05:15:16 -0800335 if (!strcmp(argv[1], "list")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800336 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800337
Kenny Root344ca102012-04-03 17:23:01 -0700338 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT);
339 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT);
San Mehateba65e92010-01-29 05:15:16 -0800340 } else if (!strcmp(argv[1], "create")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800341 dumpArgs(argc, argv, 5);
Kenny Root344ca102012-04-03 17:23:01 -0700342 if (argc != 8) {
San Mehateba65e92010-01-29 05:15:16 -0800343 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root344ca102012-04-03 17:23:01 -0700344 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
345 "<isExternal>", false);
San Mehateba65e92010-01-29 05:15:16 -0800346 return 0;
347 }
348
349 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
Kenny Root344ca102012-04-03 17:23:01 -0700350 const bool isExternal = (atoi(argv[7]) == 1);
351 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
San Mehateba65e92010-01-29 05:15:16 -0800352 } else if (!strcmp(argv[1], "finalize")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800353 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800354 if (argc != 3) {
355 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
356 return 0;
357 }
San Mehat8f2875b2010-02-18 11:40:49 -0800358 rc = vm->finalizeAsec(argv[2]);
Kenny Root344ca102012-04-03 17:23:01 -0700359 } else if (!strcmp(argv[1], "fixperms")) {
360 dumpArgs(argc, argv, -1);
361 if (argc != 5) {
362 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
363 return 0;
364 }
365
366 char *endptr;
367 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
368 if (*endptr != '\0') {
369 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
370 return 0;
371 }
372
373 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800374 } else if (!strcmp(argv[1], "destroy")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800375 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800376 if (argc < 3) {
377 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800378 return 0;
379 }
San Mehat4ba89482010-02-18 09:00:18 -0800380 bool force = false;
381 if (argc > 3 && !strcmp(argv[3], "force")) {
382 force = true;
383 }
San Mehat8f2875b2010-02-18 11:40:49 -0800384 rc = vm->destroyAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800385 } else if (!strcmp(argv[1], "mount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800386 dumpArgs(argc, argv, 3);
San Mehateba65e92010-01-29 05:15:16 -0800387 if (argc != 5) {
388 cli->sendMsg(ResponseCode::CommandSyntaxError,
389 "Usage: asec mount <namespace-id> <key> <ownerUid>", false);
390 return 0;
391 }
San Mehat8f2875b2010-02-18 11:40:49 -0800392 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
San Mehateba65e92010-01-29 05:15:16 -0800393 } else if (!strcmp(argv[1], "unmount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800394 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800395 if (argc < 3) {
396 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800397 return 0;
398 }
San Mehat4ba89482010-02-18 09:00:18 -0800399 bool force = false;
400 if (argc > 3 && !strcmp(argv[3], "force")) {
401 force = true;
402 }
San Mehat8f2875b2010-02-18 11:40:49 -0800403 rc = vm->unmountAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800404 } else if (!strcmp(argv[1], "rename")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800405 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800406 if (argc != 4) {
407 cli->sendMsg(ResponseCode::CommandSyntaxError,
408 "Usage: asec rename <old_id> <new_id>", false);
409 return 0;
410 }
San Mehat8f2875b2010-02-18 11:40:49 -0800411 rc = vm->renameAsec(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800412 } else if (!strcmp(argv[1], "path")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800413 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800414 if (argc != 3) {
415 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
416 return 0;
417 }
418 char path[255];
419
San Mehat88ac2c02010-03-23 11:15:58 -0700420 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehateba65e92010-01-29 05:15:16 -0800421 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
San Mehat88ac2c02010-03-23 11:15:58 -0700422 return 0;
San Mehateba65e92010-01-29 05:15:16 -0800423 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700424 } else if (!strcmp(argv[1], "fspath")) {
425 dumpArgs(argc, argv, -1);
426 if (argc != 3) {
427 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
428 return 0;
429 }
430 char path[255];
431
432 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
433 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
434 return 0;
435 }
San Mehata19b2502010-01-06 10:33:53 -0800436 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800437 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800438 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
San Mehata19b2502010-01-06 10:33:53 -0800439 }
440
San Mehat8f2875b2010-02-18 11:40:49 -0800441 if (!rc) {
442 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
443 } else {
444 rc = ResponseCode::convertFromErrno();
445 cli->sendMsg(rc, "asec operation failed", true);
446 }
447
San Mehata19b2502010-01-06 10:33:53 -0800448 return 0;
449}
San Mehat2350c442010-03-02 13:16:50 -0800450
Kenny Root508c0e12010-07-12 09:59:49 -0700451CommandListener::ObbCmd::ObbCmd() :
452 VoldCommand("obb") {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700453}
454
Kenny Root508c0e12010-07-12 09:59:49 -0700455int CommandListener::ObbCmd::runCommand(SocketClient *cli,
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700456 int argc, char **argv) {
457 if (argc < 2) {
458 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
459 return 0;
460 }
461
462 VolumeManager *vm = VolumeManager::Instance();
463 int rc = 0;
464
Kenny Root508c0e12010-07-12 09:59:49 -0700465 if (!strcmp(argv[1], "list")) {
466 dumpArgs(argc, argv, -1);
467
468 rc = vm->listMountedObbs(cli);
469 } else if (!strcmp(argv[1], "mount")) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700470 dumpArgs(argc, argv, 3);
471 if (argc != 5) {
472 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey69479042012-09-25 16:14:57 -0700473 "Usage: obb mount <filename> <key> <ownerGid>", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700474 return 0;
475 }
Kenny Root508c0e12010-07-12 09:59:49 -0700476 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700477 } else if (!strcmp(argv[1], "unmount")) {
478 dumpArgs(argc, argv, -1);
479 if (argc < 3) {
Kenny Root508c0e12010-07-12 09:59:49 -0700480 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700481 return 0;
482 }
483 bool force = false;
484 if (argc > 3 && !strcmp(argv[3], "force")) {
485 force = true;
486 }
Kenny Root508c0e12010-07-12 09:59:49 -0700487 rc = vm->unmountObb(argv[2], force);
488 } else if (!strcmp(argv[1], "path")) {
489 dumpArgs(argc, argv, -1);
490 if (argc != 3) {
491 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
492 return 0;
493 }
494 char path[255];
495
496 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
497 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
498 return 0;
499 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700500 } else {
501 dumpArgs(argc, argv, -1);
Kenny Root508c0e12010-07-12 09:59:49 -0700502 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700503 }
504
505 if (!rc) {
Kenny Root508c0e12010-07-12 09:59:49 -0700506 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700507 } else {
508 rc = ResponseCode::convertFromErrno();
Kenny Root508c0e12010-07-12 09:59:49 -0700509 cli->sendMsg(rc, "obb operation failed", true);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700510 }
511
512 return 0;
513}
514
San Mehat2350c442010-03-02 13:16:50 -0800515CommandListener::XwarpCmd::XwarpCmd() :
516 VoldCommand("xwarp") {
517}
518
519int CommandListener::XwarpCmd::runCommand(SocketClient *cli,
520 int argc, char **argv) {
521 if (argc < 2) {
522 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
523 return 0;
524 }
525
526 if (!strcmp(argv[1], "enable")) {
527 if (Xwarp::enable()) {
528 cli->sendMsg(ResponseCode::OperationFailed, "Failed to enable xwarp", true);
529 return 0;
530 }
531
532 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp mirroring started", false);
533 } else if (!strcmp(argv[1], "disable")) {
534 if (Xwarp::disable()) {
535 cli->sendMsg(ResponseCode::OperationFailed, "Failed to disable xwarp", true);
536 return 0;
537 }
538
539 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp disabled", false);
540 } else if (!strcmp(argv[1], "status")) {
541 char msg[255];
542 bool r;
543 unsigned mirrorPos, maxSize;
544
545 if (Xwarp::status(&r, &mirrorPos, &maxSize)) {
546 cli->sendMsg(ResponseCode::OperationFailed, "Failed to get xwarp status", true);
547 return 0;
548 }
549 snprintf(msg, sizeof(msg), "%s %u %u", (r ? "ready" : "not-ready"), mirrorPos, maxSize);
550 cli->sendMsg(ResponseCode::XwarpStatusResult, msg, false);
551 } else {
552 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
553 }
554
555 return 0;
556}
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800557
558CommandListener::CryptfsCmd::CryptfsCmd() :
559 VoldCommand("cryptfs") {
560}
561
562int CommandListener::CryptfsCmd::runCommand(SocketClient *cli,
563 int argc, char **argv) {
Ken Sumrall3ad90722011-10-04 20:38:29 -0700564 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
565 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
566 return 0;
567 }
568
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800569 if (argc < 2) {
570 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
571 return 0;
572 }
573
574 int rc = 0;
575
576 if (!strcmp(argv[1], "checkpw")) {
577 if (argc != 3) {
578 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
579 return 0;
580 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800581 dumpArgs(argc, argv, 2);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800582 rc = cryptfs_check_passwd(argv[2]);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800583 } else if (!strcmp(argv[1], "restart")) {
584 if (argc != 2) {
585 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
586 return 0;
587 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800588 dumpArgs(argc, argv, -1);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800589 rc = cryptfs_restart();
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800590 } else if (!strcmp(argv[1], "cryptocomplete")) {
591 if (argc != 2) {
592 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
593 return 0;
594 }
595 dumpArgs(argc, argv, -1);
596 rc = cryptfs_crypto_complete();
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800597 } else if (!strcmp(argv[1], "enablecrypto")) {
598 if ( (argc != 4) || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
599 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs enablecrypto <wipe|inplace> <passwd>", false);
600 return 0;
601 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800602 dumpArgs(argc, argv, 3);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800603 rc = cryptfs_enable(argv[2], argv[3]);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800604 } else if (!strcmp(argv[1], "changepw")) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600605 if (argc != 3) {
606 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs changepw <newpasswd>", false);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800607 return 0;
608 }
Jason parks70a4b3f2011-01-28 10:10:47 -0600609 SLOGD("cryptfs changepw {}");
610 rc = cryptfs_changepw(argv[2]);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700611 } else if (!strcmp(argv[1], "verifypw")) {
612 if (argc != 3) {
613 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
614 return 0;
615 }
616 SLOGD("cryptfs verifypw {}");
617 rc = cryptfs_verify_passwd(argv[2]);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700618 } else if (!strcmp(argv[1], "getfield")) {
619 char valbuf[PROPERTY_VALUE_MAX];
620
621 if (argc != 3) {
622 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
623 return 0;
624 }
625 dumpArgs(argc, argv, -1);
626 rc = cryptfs_getfield(argv[2], valbuf, sizeof(valbuf));
627 if (rc == 0) {
628 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
629 }
630 } else if (!strcmp(argv[1], "setfield")) {
631 if (argc != 4) {
632 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
633 return 0;
634 }
635 dumpArgs(argc, argv, -1);
636 rc = cryptfs_setfield(argv[2], argv[3]);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800637 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800638 dumpArgs(argc, argv, -1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800639 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
640 }
641
Jason parks0167cb12011-01-20 18:30:39 -0600642 // Always report that the command succeeded and return the error code.
643 // The caller will check the return value to see what the error was.
644 char msg[255];
645 snprintf(msg, sizeof(msg), "%d", rc);
646 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800647
648 return 0;
649}
Ken Sumrallb87937c2013-03-19 21:46:39 -0700650
651CommandListener::FstrimCmd::FstrimCmd() :
652 VoldCommand("fstrim") {
653}
654int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
655 int argc, char **argv) {
656 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
657 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
658 return 0;
659 }
660
661 if (argc < 2) {
662 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
663 return 0;
664 }
665
666 int rc = 0;
667
668 if (!strcmp(argv[1], "dotrim")) {
669 if (argc != 2) {
670 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false);
671 return 0;
672 }
673 dumpArgs(argc, argv, -1);
674 rc = fstrim_filesystems();
675 } else {
676 dumpArgs(argc, argv, -1);
677 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false);
678 }
679
680 // Always report that the command succeeded and return the error code.
681 // The caller will check the return value to see what the error was.
682 char msg[255];
683 snprintf(msg, sizeof(msg), "%d", rc);
684 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
685
686 return 0;
687}