blob: 800309515117e3f4e2ec7c5706ae4b603c662612 [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>
Mohamad Ayyash7929aa72014-03-10 15:55:33 -070025#include <fs_mgr.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080026#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070027
San Mehatd9a4e352010-03-12 13:32:47 -080028#define LOG_TAG "VoldCmdListener"
San Mehatf1b736b2009-10-10 17:22:08 -070029#include <cutils/log.h>
30
31#include <sysutils/SocketClient.h>
Ken Sumrall3ad90722011-10-04 20:38:29 -070032#include <private/android_filesystem_config.h>
San Mehatf1b736b2009-10-10 17:22:08 -070033
34#include "CommandListener.h"
35#include "VolumeManager.h"
San Mehata2677e42009-12-13 10:40:18 -080036#include "ResponseCode.h"
San Mehat586536c2010-02-16 17:12:00 -080037#include "Process.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());
Ken Sumrall8f869aa2010-12-03 03:47:09 -080052 registerCmd(new CryptfsCmd());
Ken Sumrallb87937c2013-03-19 21:46:39 -070053 registerCmd(new FstrimCmd());
San Mehatf1b736b2009-10-10 17:22:08 -070054}
55
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080056#if DUMP_ARGS
Mark Salyzyn3e971272014-01-21 13:27:04 -080057void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
San Mehatd9a4e352010-03-12 13:32:47 -080058 char buffer[4096];
59 char *p = buffer;
60
61 memset(buffer, 0, sizeof(buffer));
62 int i;
63 for (i = 0; i < argc; i++) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -080064 unsigned int len = strlen(argv[i]) + 1; // Account for space
San Mehatd9a4e352010-03-12 13:32:47 -080065 if (i == argObscure) {
66 len += 2; // Account for {}
67 }
68 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
69 if (i == argObscure) {
70 *p++ = '{';
71 *p++ = '}';
72 *p++ = ' ';
73 continue;
74 }
75 strcpy(p, argv[i]);
76 p+= strlen(argv[i]);
77 if (i != (argc -1)) {
78 *p++ = ' ';
79 }
80 }
81 }
San Mehat97ac40e2010-03-24 10:24:19 -070082 SLOGD("%s", buffer);
San Mehatd9a4e352010-03-12 13:32:47 -080083}
Mark Salyzyn3e971272014-01-21 13:27:04 -080084#else
85void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
86#endif
San Mehatd9a4e352010-03-12 13:32:47 -080087
88CommandListener::DumpCmd::DumpCmd() :
89 VoldCommand("dump") {
90}
91
92int CommandListener::DumpCmd::runCommand(SocketClient *cli,
Mark Salyzyn3e971272014-01-21 13:27:04 -080093 int /*argc*/, char ** /*argv*/) {
San Mehatd9a4e352010-03-12 13:32:47 -080094 cli->sendMsg(0, "Dumping loop status", false);
95 if (Loop::dumpState(cli)) {
96 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
97 }
98 cli->sendMsg(0, "Dumping DM status", false);
99 if (Devmapper::dumpState(cli)) {
100 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
101 }
San Mehat96597e82010-03-17 09:50:54 -0700102 cli->sendMsg(0, "Dumping mounted filesystems", false);
103 FILE *fp = fopen("/proc/mounts", "r");
104 if (fp) {
105 char line[1024];
106 while (fgets(line, sizeof(line), fp)) {
107 line[strlen(line)-1] = '\0';
108 cli->sendMsg(0, line, false);;
109 }
110 fclose(fp);
111 }
San Mehatd9a4e352010-03-12 13:32:47 -0800112
113 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
114 return 0;
115}
116
San Mehateba65e92010-01-29 05:15:16 -0800117CommandListener::VolumeCmd::VolumeCmd() :
118 VoldCommand("volume") {
San Mehatf1b736b2009-10-10 17:22:08 -0700119}
120
San Mehateba65e92010-01-29 05:15:16 -0800121int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700122 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800123 dumpArgs(argc, argv, -1);
124
San Mehateba65e92010-01-29 05:15:16 -0800125 if (argc < 2) {
126 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
127 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700128 }
129
San Mehateba65e92010-01-29 05:15:16 -0800130 VolumeManager *vm = VolumeManager::Instance();
131 int rc = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700132
San Mehateba65e92010-01-29 05:15:16 -0800133 if (!strcmp(argv[1], "list")) {
JP Abgrall40b64a62014-07-24 18:02:16 -0700134 bool broadcast = argc >= 3 && !strcmp(argv[2], "broadcast");
135 return vm->listVolumes(cli, broadcast);
San Mehatd9a4e352010-03-12 13:32:47 -0800136 } else if (!strcmp(argv[1], "debug")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700137 if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) {
138 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false);
139 return 0;
140 }
141 vm->setDebug(!strcmp(argv[2], "on") ? true : false);
San Mehateba65e92010-01-29 05:15:16 -0800142 } else if (!strcmp(argv[1], "mount")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700143 if (argc != 3) {
144 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);
145 return 0;
146 }
San Mehateba65e92010-01-29 05:15:16 -0800147 rc = vm->mountVolume(argv[2]);
148 } else if (!strcmp(argv[1], "unmount")) {
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700149 if (argc < 3 || argc > 4 ||
150 ((argc == 4 && strcmp(argv[3], "force")) &&
151 (argc == 4 && strcmp(argv[3], "force_and_revert")))) {
152 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700153 return 0;
154 }
155
San Mehat4ba89482010-02-18 09:00:18 -0800156 bool force = false;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700157 bool revert = false;
San Mehat4ba89482010-02-18 09:00:18 -0800158 if (argc >= 4 && !strcmp(argv[3], "force")) {
159 force = true;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700160 } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) {
161 force = true;
162 revert = true;
San Mehat4ba89482010-02-18 09:00:18 -0800163 }
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700164 rc = vm->unmountVolume(argv[2], force, revert);
San Mehateba65e92010-01-29 05:15:16 -0800165 } else if (!strcmp(argv[1], "format")) {
Ken Sumrall9caab762013-06-11 19:10:20 -0700166 if (argc < 3 || argc > 4 ||
167 (argc == 4 && strcmp(argv[3], "wipe"))) {
168 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path> [wipe]", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700169 return 0;
170 }
Ken Sumrall9caab762013-06-11 19:10:20 -0700171 bool wipe = false;
172 if (argc >= 4 && !strcmp(argv[3], "wipe")) {
173 wipe = true;
174 }
175 rc = vm->formatVolume(argv[2], wipe);
San Mehateba65e92010-01-29 05:15:16 -0800176 } else if (!strcmp(argv[1], "share")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700177 if (argc != 4) {
178 cli->sendMsg(ResponseCode::CommandSyntaxError,
179 "Usage: volume share <path> <method>", false);
180 return 0;
181 }
San Mehatb9aed742010-02-04 15:07:01 -0800182 rc = vm->shareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800183 } else if (!strcmp(argv[1], "unshare")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700184 if (argc != 4) {
185 cli->sendMsg(ResponseCode::CommandSyntaxError,
186 "Usage: volume unshare <path> <method>", false);
187 return 0;
188 }
San Mehatb9aed742010-02-04 15:07:01 -0800189 rc = vm->unshareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800190 } else if (!strcmp(argv[1], "shared")) {
191 bool enabled = false;
San Mehat57df7bf2010-03-14 13:41:27 -0700192 if (argc != 4) {
193 cli->sendMsg(ResponseCode::CommandSyntaxError,
194 "Usage: volume shared <path> <method>", false);
195 return 0;
196 }
San Mehatf1b736b2009-10-10 17:22:08 -0700197
San Mehat2b225522010-02-03 10:26:40 -0800198 if (vm->shareEnabled(argv[2], argv[3], &enabled)) {
San Mehateba65e92010-01-29 05:15:16 -0800199 cli->sendMsg(
200 ResponseCode::OperationFailed, "Failed to determine share enable state", true);
201 } else {
202 cli->sendMsg(ResponseCode::ShareEnabledResult,
203 (enabled ? "Share enabled" : "Share disabled"), false);
204 }
San Mehatb9aed742010-02-04 15:07:01 -0800205 return 0;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700206 } else if (!strcmp(argv[1], "mkdirs")) {
207 if (argc != 3) {
208 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mkdirs <path>", false);
209 return 0;
210 }
211 rc = vm->mkdirs(argv[2]);
San Mehat49e2bce2009-10-12 16:29:01 -0700212 } else {
San Mehateba65e92010-01-29 05:15:16 -0800213 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);
214 }
215
216 if (!rc) {
217 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
218 } else {
San Mehat8f2875b2010-02-18 11:40:49 -0800219 int erno = errno;
220 rc = ResponseCode::convertFromErrno();
San Mehateba65e92010-01-29 05:15:16 -0800221 cli->sendMsg(rc, "volume operation failed", true);
San Mehat49e2bce2009-10-12 16:29:01 -0700222 }
223
San Mehatf1b736b2009-10-10 17:22:08 -0700224 return 0;
225}
226
San Mehat586536c2010-02-16 17:12:00 -0800227CommandListener::StorageCmd::StorageCmd() :
228 VoldCommand("storage") {
229}
230
231int CommandListener::StorageCmd::runCommand(SocketClient *cli,
232 int argc, char **argv) {
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700233 /* Guarantied to be initialized by vold's main() before the CommandListener is active */
234 extern struct fstab *fstab;
235
San Mehatd9a4e352010-03-12 13:32:47 -0800236 dumpArgs(argc, argv, -1);
237
San Mehat586536c2010-02-16 17:12:00 -0800238 if (argc < 2) {
239 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
240 return 0;
241 }
242
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700243 if (!strcmp(argv[1], "mountall")) {
244 if (argc != 2) {
245 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false);
246 return 0;
247 }
248 fs_mgr_mount_all(fstab);
249 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false);
250 return 0;
251 }
San Mehat586536c2010-02-16 17:12:00 -0800252 if (!strcmp(argv[1], "users")) {
253 DIR *dir;
254 struct dirent *de;
255
JP Abgralledf7adf2014-03-12 10:41:05 -0700256 if (argc < 3) {
257 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false);
258 return 0;
259 }
San Mehat586536c2010-02-16 17:12:00 -0800260 if (!(dir = opendir("/proc"))) {
261 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
262 return 0;
263 }
264
265 while ((de = readdir(dir))) {
266 int pid = Process::getPid(de->d_name);
267
268 if (pid < 0) {
269 continue;
270 }
271
272 char processName[255];
273 Process::getProcessName(pid, processName, sizeof(processName));
274
275 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
276 Process::checkFileMaps(pid, argv[2]) ||
277 Process::checkSymLink(pid, argv[2], "cwd") ||
278 Process::checkSymLink(pid, argv[2], "root") ||
279 Process::checkSymLink(pid, argv[2], "exe")) {
280
281 char msg[1024];
282 snprintf(msg, sizeof(msg), "%d %s", pid, processName);
283 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
284 }
285 }
286 closedir(dir);
287 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
288 } else {
289 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
290 }
291 return 0;
292}
293
San Mehateba65e92010-01-29 05:15:16 -0800294CommandListener::AsecCmd::AsecCmd() :
295 VoldCommand("asec") {
San Mehata19b2502010-01-06 10:33:53 -0800296}
297
Kenny Root344ca102012-04-03 17:23:01 -0700298void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
299 DIR *d = opendir(directory);
300
301 if (!d) {
302 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
303 return;
304 }
305
306 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -0700307 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Kenny Root344ca102012-04-03 17:23:01 -0700308
309 struct dirent *dent = (struct dirent *) malloc(dirent_len);
310 if (dent == NULL) {
311 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
312 return;
313 }
314
315 struct dirent *result;
316
317 while (!readdir_r(d, dent, &result) && result != NULL) {
318 if (dent->d_name[0] == '.')
319 continue;
320 if (dent->d_type != DT_REG)
321 continue;
322 size_t name_len = strlen(dent->d_name);
323 if (name_len > 5 && name_len < 260 &&
324 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
325 char id[255];
326 memset(id, 0, sizeof(id));
Kenny Root7b0bc852012-04-27 15:33:58 -0700327 strlcpy(id, dent->d_name, name_len - 4);
Kenny Root344ca102012-04-03 17:23:01 -0700328 cli->sendMsg(ResponseCode::AsecListResult, id, false);
329 }
330 }
331 closedir(d);
332
333 free(dent);
334}
335
San Mehateba65e92010-01-29 05:15:16 -0800336int CommandListener::AsecCmd::runCommand(SocketClient *cli,
337 int argc, char **argv) {
338 if (argc < 2) {
339 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
San Mehata19b2502010-01-06 10:33:53 -0800340 return 0;
341 }
342
San Mehateba65e92010-01-29 05:15:16 -0800343 VolumeManager *vm = VolumeManager::Instance();
344 int rc = 0;
San Mehata19b2502010-01-06 10:33:53 -0800345
San Mehateba65e92010-01-29 05:15:16 -0800346 if (!strcmp(argv[1], "list")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800347 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800348
Kenny Root344ca102012-04-03 17:23:01 -0700349 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT);
350 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT);
San Mehateba65e92010-01-29 05:15:16 -0800351 } else if (!strcmp(argv[1], "create")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800352 dumpArgs(argc, argv, 5);
Kenny Root344ca102012-04-03 17:23:01 -0700353 if (argc != 8) {
San Mehateba65e92010-01-29 05:15:16 -0800354 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root344ca102012-04-03 17:23:01 -0700355 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
356 "<isExternal>", false);
San Mehateba65e92010-01-29 05:15:16 -0800357 return 0;
358 }
359
360 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
Kenny Root344ca102012-04-03 17:23:01 -0700361 const bool isExternal = (atoi(argv[7]) == 1);
362 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700363 } else if (!strcmp(argv[1], "resize")) {
364 dumpArgs(argc, argv, -1);
365 if (argc != 5) {
366 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
367 return 0;
368 }
369 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
370 rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800371 } else if (!strcmp(argv[1], "finalize")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800372 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800373 if (argc != 3) {
374 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
375 return 0;
376 }
San Mehat8f2875b2010-02-18 11:40:49 -0800377 rc = vm->finalizeAsec(argv[2]);
Kenny Root344ca102012-04-03 17:23:01 -0700378 } else if (!strcmp(argv[1], "fixperms")) {
379 dumpArgs(argc, argv, -1);
380 if (argc != 5) {
381 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
382 return 0;
383 }
384
385 char *endptr;
386 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
387 if (*endptr != '\0') {
388 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
389 return 0;
390 }
391
392 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800393 } else if (!strcmp(argv[1], "destroy")) {
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 destroy <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->destroyAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800404 } else if (!strcmp(argv[1], "mount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800405 dumpArgs(argc, argv, 3);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700406 if (argc != 6) {
San Mehateba65e92010-01-29 05:15:16 -0800407 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700408 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false);
San Mehateba65e92010-01-29 05:15:16 -0800409 return 0;
410 }
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700411 bool readOnly = true;
412 if (!strcmp(argv[5], "rw")) {
413 readOnly = false;
414 }
415 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly);
San Mehateba65e92010-01-29 05:15:16 -0800416 } else if (!strcmp(argv[1], "unmount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800417 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800418 if (argc < 3) {
419 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800420 return 0;
421 }
San Mehat4ba89482010-02-18 09:00:18 -0800422 bool force = false;
423 if (argc > 3 && !strcmp(argv[3], "force")) {
424 force = true;
425 }
San Mehat8f2875b2010-02-18 11:40:49 -0800426 rc = vm->unmountAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800427 } else if (!strcmp(argv[1], "rename")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800428 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800429 if (argc != 4) {
430 cli->sendMsg(ResponseCode::CommandSyntaxError,
431 "Usage: asec rename <old_id> <new_id>", false);
432 return 0;
433 }
San Mehat8f2875b2010-02-18 11:40:49 -0800434 rc = vm->renameAsec(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800435 } else if (!strcmp(argv[1], "path")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800436 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800437 if (argc != 3) {
438 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
439 return 0;
440 }
441 char path[255];
442
San Mehat88ac2c02010-03-23 11:15:58 -0700443 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehateba65e92010-01-29 05:15:16 -0800444 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
San Mehat88ac2c02010-03-23 11:15:58 -0700445 return 0;
San Mehateba65e92010-01-29 05:15:16 -0800446 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700447 } else if (!strcmp(argv[1], "fspath")) {
448 dumpArgs(argc, argv, -1);
449 if (argc != 3) {
450 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
451 return 0;
452 }
453 char path[255];
454
455 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
456 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
457 return 0;
458 }
San Mehata19b2502010-01-06 10:33:53 -0800459 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800460 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800461 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
San Mehata19b2502010-01-06 10:33:53 -0800462 }
463
San Mehat8f2875b2010-02-18 11:40:49 -0800464 if (!rc) {
465 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
466 } else {
467 rc = ResponseCode::convertFromErrno();
468 cli->sendMsg(rc, "asec operation failed", true);
469 }
470
San Mehata19b2502010-01-06 10:33:53 -0800471 return 0;
472}
San Mehat2350c442010-03-02 13:16:50 -0800473
Kenny Root508c0e12010-07-12 09:59:49 -0700474CommandListener::ObbCmd::ObbCmd() :
475 VoldCommand("obb") {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700476}
477
Kenny Root508c0e12010-07-12 09:59:49 -0700478int CommandListener::ObbCmd::runCommand(SocketClient *cli,
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700479 int argc, char **argv) {
480 if (argc < 2) {
481 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
482 return 0;
483 }
484
485 VolumeManager *vm = VolumeManager::Instance();
486 int rc = 0;
487
Kenny Root508c0e12010-07-12 09:59:49 -0700488 if (!strcmp(argv[1], "list")) {
489 dumpArgs(argc, argv, -1);
490
491 rc = vm->listMountedObbs(cli);
492 } else if (!strcmp(argv[1], "mount")) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700493 dumpArgs(argc, argv, 3);
494 if (argc != 5) {
495 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey69479042012-09-25 16:14:57 -0700496 "Usage: obb mount <filename> <key> <ownerGid>", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700497 return 0;
498 }
Kenny Root508c0e12010-07-12 09:59:49 -0700499 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700500 } else if (!strcmp(argv[1], "unmount")) {
501 dumpArgs(argc, argv, -1);
502 if (argc < 3) {
Kenny Root508c0e12010-07-12 09:59:49 -0700503 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700504 return 0;
505 }
506 bool force = false;
507 if (argc > 3 && !strcmp(argv[3], "force")) {
508 force = true;
509 }
Kenny Root508c0e12010-07-12 09:59:49 -0700510 rc = vm->unmountObb(argv[2], force);
511 } else if (!strcmp(argv[1], "path")) {
512 dumpArgs(argc, argv, -1);
513 if (argc != 3) {
514 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
515 return 0;
516 }
517 char path[255];
518
519 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
520 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
521 return 0;
522 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700523 } else {
524 dumpArgs(argc, argv, -1);
Kenny Root508c0e12010-07-12 09:59:49 -0700525 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700526 }
527
528 if (!rc) {
Kenny Root508c0e12010-07-12 09:59:49 -0700529 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700530 } else {
531 rc = ResponseCode::convertFromErrno();
Kenny Root508c0e12010-07-12 09:59:49 -0700532 cli->sendMsg(rc, "obb operation failed", true);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700533 }
534
535 return 0;
536}
537
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800538CommandListener::CryptfsCmd::CryptfsCmd() :
539 VoldCommand("cryptfs") {
540}
541
Paul Lawrence45f10532014-04-04 18:11:56 +0000542static int getType(const char* type)
543{
544 if (!strcmp(type, "default")) {
545 return CRYPT_TYPE_DEFAULT;
546 } else if (!strcmp(type, "password")) {
547 return CRYPT_TYPE_PASSWORD;
548 } else if (!strcmp(type, "pin")) {
549 return CRYPT_TYPE_PIN;
550 } else if (!strcmp(type, "pattern")) {
551 return CRYPT_TYPE_PATTERN;
552 } else {
553 return -1;
554 }
555}
556
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800557int CommandListener::CryptfsCmd::runCommand(SocketClient *cli,
558 int argc, char **argv) {
Ken Sumrall3ad90722011-10-04 20:38:29 -0700559 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
560 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
561 return 0;
562 }
563
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800564 if (argc < 2) {
565 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
566 return 0;
567 }
568
569 int rc = 0;
570
571 if (!strcmp(argv[1], "checkpw")) {
572 if (argc != 3) {
573 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
574 return 0;
575 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800576 dumpArgs(argc, argv, 2);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800577 rc = cryptfs_check_passwd(argv[2]);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800578 } else if (!strcmp(argv[1], "restart")) {
579 if (argc != 2) {
580 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
581 return 0;
582 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800583 dumpArgs(argc, argv, -1);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800584 rc = cryptfs_restart();
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800585 } else if (!strcmp(argv[1], "cryptocomplete")) {
586 if (argc != 2) {
587 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
588 return 0;
589 }
590 dumpArgs(argc, argv, -1);
591 rc = cryptfs_crypto_complete();
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800592 } else if (!strcmp(argv[1], "enablecrypto")) {
Paul Lawrence45f10532014-04-04 18:11:56 +0000593 const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> "
594 "default|password|pin|pattern [passwd]";
595 if ( (argc != 4 && argc != 5)
Paul Lawrence13486032014-02-03 13:28:11 -0800596 || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
Paul Lawrence45f10532014-04-04 18:11:56 +0000597 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800598 return 0;
599 }
Paul Lawrence45f10532014-04-04 18:11:56 +0000600 dumpArgs(argc, argv, 4);
JP Abgrall502dc742013-11-01 13:06:20 -0700601
Paul Lawrence13486032014-02-03 13:28:11 -0800602 int tries;
603 for (tries = 0; tries < 2; ++tries) {
Paul Lawrence45f10532014-04-04 18:11:56 +0000604 int type = getType(argv[3]);
605 if (type == -1) {
606 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax,
607 false);
608 return 0;
609 } else if (type == CRYPT_TYPE_DEFAULT) {
610 rc = cryptfs_enable_default(argv[2], /*allow_reboot*/false);
611 } else {
612 rc = cryptfs_enable(argv[2], type, argv[4],
613 /*allow_reboot*/false);
614 }
Paul Lawrence13486032014-02-03 13:28:11 -0800615
616 if (rc == 0) {
617 break;
618 } else if (tries == 0) {
619 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, 2);
620 }
621 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800622 } else if (!strcmp(argv[1], "changepw")) {
Paul Lawrencef4faa572014-01-29 13:31:03 -0800623 const char* syntax = "Usage: cryptfs changepw "
624 "default|password|pin|pattern [newpasswd]";
Paul Lawrence13486032014-02-03 13:28:11 -0800625 const char* password;
Paul Lawrencef4faa572014-01-29 13:31:03 -0800626 if (argc == 3) {
627 password = "";
628 } else if (argc == 4) {
629 password = argv[3];
630 } else {
631 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800632 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -0800633 }
Paul Lawrence45f10532014-04-04 18:11:56 +0000634 int type = getType(argv[2]);
635 if (type == -1) {
Paul Lawrencef4faa572014-01-29 13:31:03 -0800636 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
637 return 0;
638 }
639 SLOGD("cryptfs changepw %s {}", argv[2]);
640 rc = cryptfs_changepw(type, password);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700641 } else if (!strcmp(argv[1], "verifypw")) {
642 if (argc != 3) {
643 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
644 return 0;
645 }
646 SLOGD("cryptfs verifypw {}");
647 rc = cryptfs_verify_passwd(argv[2]);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700648 } else if (!strcmp(argv[1], "getfield")) {
649 char valbuf[PROPERTY_VALUE_MAX];
650
651 if (argc != 3) {
652 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
653 return 0;
654 }
655 dumpArgs(argc, argv, -1);
656 rc = cryptfs_getfield(argv[2], valbuf, sizeof(valbuf));
657 if (rc == 0) {
658 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
659 }
660 } else if (!strcmp(argv[1], "setfield")) {
661 if (argc != 4) {
662 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
663 return 0;
664 }
665 dumpArgs(argc, argv, -1);
666 rc = cryptfs_setfield(argv[2], argv[3]);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800667 } else if (!strcmp(argv[1], "mountdefaultencrypted")) {
668 SLOGD("cryptfs mountdefaultencrypted");
669 dumpArgs(argc, argv, -1);
670 rc = cryptfs_mount_default_encrypted();
671 } else if (!strcmp(argv[1], "getpwtype")) {
672 SLOGD("cryptfs getpwtype");
673 dumpArgs(argc, argv, -1);
674 switch(cryptfs_get_password_type()) {
675 case CRYPT_TYPE_PASSWORD:
676 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false);
677 return 0;
678 case CRYPT_TYPE_PATTERN:
679 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false);
680 return 0;
681 case CRYPT_TYPE_PIN:
682 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false);
683 return 0;
684 case CRYPT_TYPE_DEFAULT:
685 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false);
686 return 0;
687 default:
688 /** @TODO better error and make sure handled by callers */
689 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false);
690 return 0;
691 }
Paul Lawrence399317e2014-03-10 13:20:50 -0700692 } else if (!strcmp(argv[1], "getpw")) {
693 SLOGD("cryptfs getpw");
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800694 dumpArgs(argc, argv, -1);
Paul Lawrence399317e2014-03-10 13:20:50 -0700695 char* password = cryptfs_get_password();
696 if (password) {
697 cli->sendMsg(ResponseCode::CommandOkay, password, false);
698 return 0;
699 }
700 rc = -1;
701 } else if (!strcmp(argv[1], "clearpw")) {
702 SLOGD("cryptfs clearpw");
703 dumpArgs(argc, argv, -1);
704 cryptfs_clear_password();
705 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800706 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800707 dumpArgs(argc, argv, -1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800708 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800709 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800710 }
711
Jason parks0167cb12011-01-20 18:30:39 -0600712 // Always report that the command succeeded and return the error code.
713 // The caller will check the return value to see what the error was.
714 char msg[255];
715 snprintf(msg, sizeof(msg), "%d", rc);
716 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800717
718 return 0;
719}
Ken Sumrallb87937c2013-03-19 21:46:39 -0700720
721CommandListener::FstrimCmd::FstrimCmd() :
722 VoldCommand("fstrim") {
723}
724int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
725 int argc, char **argv) {
726 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
727 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
728 return 0;
729 }
730
731 if (argc < 2) {
732 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
733 return 0;
734 }
735
736 int rc = 0;
737
738 if (!strcmp(argv[1], "dotrim")) {
739 if (argc != 2) {
740 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false);
741 return 0;
742 }
743 dumpArgs(argc, argv, -1);
JP Abgrall422bdb72014-07-29 13:24:56 -0700744 rc = fstrim_filesystems(0);
745 } else if (!strcmp(argv[1], "dodtrim")) {
746 if (argc != 2) {
747 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false);
748 return 0;
749 }
750 dumpArgs(argc, argv, -1);
751 rc = fstrim_filesystems(1); /* Do Deep Discard trim */
Ken Sumrallb87937c2013-03-19 21:46:39 -0700752 } else {
753 dumpArgs(argc, argv, -1);
754 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false);
755 }
756
757 // Always report that the command succeeded and return the error code.
758 // The caller will check the return value to see what the error was.
759 char msg[255];
760 snprintf(msg, sizeof(msg), "%d", rc);
761 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
762
763 return 0;
764}