blob: f30652764be73782e103a859ee2adf4a90726fee [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
San Mehatd9a4e352010-03-12 13:32:47 -080057void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080058#if DUMP_ARGS
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);
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080084#endif
San Mehatd9a4e352010-03-12 13:32:47 -080085}
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 }
San Mehat96597e82010-03-17 09:50:54 -0700101 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 }
San Mehatd9a4e352010-03-12 13:32:47 -0800111
112 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
113 return 0;
114}
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,
San Mehatf1b736b2009-10-10 17:22:08 -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")) {
134 return vm->listVolumes(cli);
San Mehatd9a4e352010-03-12 13:32:47 -0800135 } else if (!strcmp(argv[1], "debug")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700136 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);
San Mehateba65e92010-01-29 05:15:16 -0800141 } else if (!strcmp(argv[1], "mount")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700142 if (argc != 3) {
143 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);
144 return 0;
145 }
San Mehateba65e92010-01-29 05:15:16 -0800146 rc = vm->mountVolume(argv[2]);
147 } else if (!strcmp(argv[1], "unmount")) {
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700148 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);
San Mehat57df7bf2010-03-14 13:41:27 -0700152 return 0;
153 }
154
San Mehat4ba89482010-02-18 09:00:18 -0800155 bool force = false;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700156 bool revert = false;
San Mehat4ba89482010-02-18 09:00:18 -0800157 if (argc >= 4 && !strcmp(argv[3], "force")) {
158 force = true;
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700159 } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) {
160 force = true;
161 revert = true;
San Mehat4ba89482010-02-18 09:00:18 -0800162 }
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700163 rc = vm->unmountVolume(argv[2], force, revert);
San Mehateba65e92010-01-29 05:15:16 -0800164 } else if (!strcmp(argv[1], "format")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700165 if (argc != 3) {
166 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path>", false);
167 return 0;
168 }
San Mehateba65e92010-01-29 05:15:16 -0800169 rc = vm->formatVolume(argv[2]);
170 } else if (!strcmp(argv[1], "share")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700171 if (argc != 4) {
172 cli->sendMsg(ResponseCode::CommandSyntaxError,
173 "Usage: volume share <path> <method>", false);
174 return 0;
175 }
San Mehatb9aed742010-02-04 15:07:01 -0800176 rc = vm->shareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800177 } else if (!strcmp(argv[1], "unshare")) {
San Mehat57df7bf2010-03-14 13:41:27 -0700178 if (argc != 4) {
179 cli->sendMsg(ResponseCode::CommandSyntaxError,
180 "Usage: volume unshare <path> <method>", false);
181 return 0;
182 }
San Mehatb9aed742010-02-04 15:07:01 -0800183 rc = vm->unshareVolume(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800184 } else if (!strcmp(argv[1], "shared")) {
185 bool enabled = false;
San Mehat57df7bf2010-03-14 13:41:27 -0700186 if (argc != 4) {
187 cli->sendMsg(ResponseCode::CommandSyntaxError,
188 "Usage: volume shared <path> <method>", false);
189 return 0;
190 }
San Mehatf1b736b2009-10-10 17:22:08 -0700191
San Mehat2b225522010-02-03 10:26:40 -0800192 if (vm->shareEnabled(argv[2], argv[3], &enabled)) {
San Mehateba65e92010-01-29 05:15:16 -0800193 cli->sendMsg(
194 ResponseCode::OperationFailed, "Failed to determine share enable state", true);
195 } else {
196 cli->sendMsg(ResponseCode::ShareEnabledResult,
197 (enabled ? "Share enabled" : "Share disabled"), false);
198 }
San Mehatb9aed742010-02-04 15:07:01 -0800199 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700200 } else {
San Mehateba65e92010-01-29 05:15:16 -0800201 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);
202 }
203
204 if (!rc) {
205 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
206 } else {
San Mehat8f2875b2010-02-18 11:40:49 -0800207 int erno = errno;
208 rc = ResponseCode::convertFromErrno();
San Mehateba65e92010-01-29 05:15:16 -0800209 cli->sendMsg(rc, "volume operation failed", true);
San Mehat49e2bce2009-10-12 16:29:01 -0700210 }
211
San Mehatf1b736b2009-10-10 17:22:08 -0700212 return 0;
213}
214
San Mehat586536c2010-02-16 17:12:00 -0800215CommandListener::StorageCmd::StorageCmd() :
216 VoldCommand("storage") {
217}
218
219int CommandListener::StorageCmd::runCommand(SocketClient *cli,
220 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800221 dumpArgs(argc, argv, -1);
222
San Mehat586536c2010-02-16 17:12:00 -0800223 if (argc < 2) {
224 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
225 return 0;
226 }
227
228 if (!strcmp(argv[1], "users")) {
229 DIR *dir;
230 struct dirent *de;
231
232 if (!(dir = opendir("/proc"))) {
233 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
234 return 0;
235 }
236
237 while ((de = readdir(dir))) {
238 int pid = Process::getPid(de->d_name);
239
240 if (pid < 0) {
241 continue;
242 }
243
244 char processName[255];
245 Process::getProcessName(pid, processName, sizeof(processName));
246
247 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
248 Process::checkFileMaps(pid, argv[2]) ||
249 Process::checkSymLink(pid, argv[2], "cwd") ||
250 Process::checkSymLink(pid, argv[2], "root") ||
251 Process::checkSymLink(pid, argv[2], "exe")) {
252
253 char msg[1024];
254 snprintf(msg, sizeof(msg), "%d %s", pid, processName);
255 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
256 }
257 }
258 closedir(dir);
259 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
260 } else {
261 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
262 }
263 return 0;
264}
265
San Mehateba65e92010-01-29 05:15:16 -0800266CommandListener::AsecCmd::AsecCmd() :
267 VoldCommand("asec") {
San Mehata19b2502010-01-06 10:33:53 -0800268}
269
Kenny Root344ca102012-04-03 17:23:01 -0700270void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
271 DIR *d = opendir(directory);
272
273 if (!d) {
274 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
275 return;
276 }
277
278 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -0700279 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Kenny Root344ca102012-04-03 17:23:01 -0700280
281 struct dirent *dent = (struct dirent *) malloc(dirent_len);
282 if (dent == NULL) {
283 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
284 return;
285 }
286
287 struct dirent *result;
288
289 while (!readdir_r(d, dent, &result) && result != NULL) {
290 if (dent->d_name[0] == '.')
291 continue;
292 if (dent->d_type != DT_REG)
293 continue;
294 size_t name_len = strlen(dent->d_name);
295 if (name_len > 5 && name_len < 260 &&
296 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
297 char id[255];
298 memset(id, 0, sizeof(id));
Kenny Root7b0bc852012-04-27 15:33:58 -0700299 strlcpy(id, dent->d_name, name_len - 4);
Kenny Root344ca102012-04-03 17:23:01 -0700300 cli->sendMsg(ResponseCode::AsecListResult, id, false);
301 }
302 }
303 closedir(d);
304
305 free(dent);
306}
307
San Mehateba65e92010-01-29 05:15:16 -0800308int CommandListener::AsecCmd::runCommand(SocketClient *cli,
309 int argc, char **argv) {
310 if (argc < 2) {
311 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
San Mehata19b2502010-01-06 10:33:53 -0800312 return 0;
313 }
314
San Mehateba65e92010-01-29 05:15:16 -0800315 VolumeManager *vm = VolumeManager::Instance();
316 int rc = 0;
San Mehata19b2502010-01-06 10:33:53 -0800317
San Mehateba65e92010-01-29 05:15:16 -0800318 if (!strcmp(argv[1], "list")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800319 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800320
Kenny Root344ca102012-04-03 17:23:01 -0700321 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT);
322 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT);
San Mehateba65e92010-01-29 05:15:16 -0800323 } else if (!strcmp(argv[1], "create")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800324 dumpArgs(argc, argv, 5);
Kenny Root344ca102012-04-03 17:23:01 -0700325 if (argc != 8) {
San Mehateba65e92010-01-29 05:15:16 -0800326 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root344ca102012-04-03 17:23:01 -0700327 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
328 "<isExternal>", false);
San Mehateba65e92010-01-29 05:15:16 -0800329 return 0;
330 }
331
332 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
Kenny Root344ca102012-04-03 17:23:01 -0700333 const bool isExternal = (atoi(argv[7]) == 1);
334 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
San Mehateba65e92010-01-29 05:15:16 -0800335 } else if (!strcmp(argv[1], "finalize")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800336 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800337 if (argc != 3) {
338 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
339 return 0;
340 }
San Mehat8f2875b2010-02-18 11:40:49 -0800341 rc = vm->finalizeAsec(argv[2]);
Kenny Root344ca102012-04-03 17:23:01 -0700342 } else if (!strcmp(argv[1], "fixperms")) {
343 dumpArgs(argc, argv, -1);
344 if (argc != 5) {
345 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
346 return 0;
347 }
348
349 char *endptr;
350 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
351 if (*endptr != '\0') {
352 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
353 return 0;
354 }
355
356 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800357 } else if (!strcmp(argv[1], "destroy")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800358 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800359 if (argc < 3) {
360 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800361 return 0;
362 }
San Mehat4ba89482010-02-18 09:00:18 -0800363 bool force = false;
364 if (argc > 3 && !strcmp(argv[3], "force")) {
365 force = true;
366 }
San Mehat8f2875b2010-02-18 11:40:49 -0800367 rc = vm->destroyAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800368 } else if (!strcmp(argv[1], "mount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800369 dumpArgs(argc, argv, 3);
San Mehateba65e92010-01-29 05:15:16 -0800370 if (argc != 5) {
371 cli->sendMsg(ResponseCode::CommandSyntaxError,
372 "Usage: asec mount <namespace-id> <key> <ownerUid>", false);
373 return 0;
374 }
San Mehat8f2875b2010-02-18 11:40:49 -0800375 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
San Mehateba65e92010-01-29 05:15:16 -0800376 } else if (!strcmp(argv[1], "unmount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800377 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800378 if (argc < 3) {
379 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800380 return 0;
381 }
San Mehat4ba89482010-02-18 09:00:18 -0800382 bool force = false;
383 if (argc > 3 && !strcmp(argv[3], "force")) {
384 force = true;
385 }
San Mehat8f2875b2010-02-18 11:40:49 -0800386 rc = vm->unmountAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800387 } else if (!strcmp(argv[1], "rename")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800388 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800389 if (argc != 4) {
390 cli->sendMsg(ResponseCode::CommandSyntaxError,
391 "Usage: asec rename <old_id> <new_id>", false);
392 return 0;
393 }
San Mehat8f2875b2010-02-18 11:40:49 -0800394 rc = vm->renameAsec(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800395 } else if (!strcmp(argv[1], "path")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800396 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800397 if (argc != 3) {
398 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
399 return 0;
400 }
401 char path[255];
402
San Mehat88ac2c02010-03-23 11:15:58 -0700403 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehateba65e92010-01-29 05:15:16 -0800404 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
San Mehat88ac2c02010-03-23 11:15:58 -0700405 return 0;
San Mehateba65e92010-01-29 05:15:16 -0800406 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700407 } else if (!strcmp(argv[1], "fspath")) {
408 dumpArgs(argc, argv, -1);
409 if (argc != 3) {
410 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
411 return 0;
412 }
413 char path[255];
414
415 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
416 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
417 return 0;
418 }
San Mehata19b2502010-01-06 10:33:53 -0800419 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800420 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800421 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
San Mehata19b2502010-01-06 10:33:53 -0800422 }
423
San Mehat8f2875b2010-02-18 11:40:49 -0800424 if (!rc) {
425 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
426 } else {
427 rc = ResponseCode::convertFromErrno();
428 cli->sendMsg(rc, "asec operation failed", true);
429 }
430
San Mehata19b2502010-01-06 10:33:53 -0800431 return 0;
432}
San Mehat2350c442010-03-02 13:16:50 -0800433
Kenny Root508c0e12010-07-12 09:59:49 -0700434CommandListener::ObbCmd::ObbCmd() :
435 VoldCommand("obb") {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700436}
437
Kenny Root508c0e12010-07-12 09:59:49 -0700438int CommandListener::ObbCmd::runCommand(SocketClient *cli,
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700439 int argc, char **argv) {
440 if (argc < 2) {
441 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
442 return 0;
443 }
444
445 VolumeManager *vm = VolumeManager::Instance();
446 int rc = 0;
447
Kenny Root508c0e12010-07-12 09:59:49 -0700448 if (!strcmp(argv[1], "list")) {
449 dumpArgs(argc, argv, -1);
450
451 rc = vm->listMountedObbs(cli);
452 } else if (!strcmp(argv[1], "mount")) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700453 dumpArgs(argc, argv, 3);
454 if (argc != 5) {
455 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey69479042012-09-25 16:14:57 -0700456 "Usage: obb mount <filename> <key> <ownerGid>", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700457 return 0;
458 }
Kenny Root508c0e12010-07-12 09:59:49 -0700459 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700460 } else if (!strcmp(argv[1], "unmount")) {
461 dumpArgs(argc, argv, -1);
462 if (argc < 3) {
Kenny Root508c0e12010-07-12 09:59:49 -0700463 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700464 return 0;
465 }
466 bool force = false;
467 if (argc > 3 && !strcmp(argv[3], "force")) {
468 force = true;
469 }
Kenny Root508c0e12010-07-12 09:59:49 -0700470 rc = vm->unmountObb(argv[2], force);
471 } else if (!strcmp(argv[1], "path")) {
472 dumpArgs(argc, argv, -1);
473 if (argc != 3) {
474 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
475 return 0;
476 }
477 char path[255];
478
479 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
480 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
481 return 0;
482 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700483 } else {
484 dumpArgs(argc, argv, -1);
Kenny Root508c0e12010-07-12 09:59:49 -0700485 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700486 }
487
488 if (!rc) {
Kenny Root508c0e12010-07-12 09:59:49 -0700489 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700490 } else {
491 rc = ResponseCode::convertFromErrno();
Kenny Root508c0e12010-07-12 09:59:49 -0700492 cli->sendMsg(rc, "obb operation failed", true);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700493 }
494
495 return 0;
496}
497
San Mehat2350c442010-03-02 13:16:50 -0800498CommandListener::XwarpCmd::XwarpCmd() :
499 VoldCommand("xwarp") {
500}
501
502int CommandListener::XwarpCmd::runCommand(SocketClient *cli,
503 int argc, char **argv) {
504 if (argc < 2) {
505 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
506 return 0;
507 }
508
509 if (!strcmp(argv[1], "enable")) {
510 if (Xwarp::enable()) {
511 cli->sendMsg(ResponseCode::OperationFailed, "Failed to enable xwarp", true);
512 return 0;
513 }
514
515 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp mirroring started", false);
516 } else if (!strcmp(argv[1], "disable")) {
517 if (Xwarp::disable()) {
518 cli->sendMsg(ResponseCode::OperationFailed, "Failed to disable xwarp", true);
519 return 0;
520 }
521
522 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp disabled", false);
523 } else if (!strcmp(argv[1], "status")) {
524 char msg[255];
525 bool r;
526 unsigned mirrorPos, maxSize;
527
528 if (Xwarp::status(&r, &mirrorPos, &maxSize)) {
529 cli->sendMsg(ResponseCode::OperationFailed, "Failed to get xwarp status", true);
530 return 0;
531 }
532 snprintf(msg, sizeof(msg), "%s %u %u", (r ? "ready" : "not-ready"), mirrorPos, maxSize);
533 cli->sendMsg(ResponseCode::XwarpStatusResult, msg, false);
534 } else {
535 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
536 }
537
538 return 0;
539}
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800540
541CommandListener::CryptfsCmd::CryptfsCmd() :
542 VoldCommand("cryptfs") {
543}
544
545int CommandListener::CryptfsCmd::runCommand(SocketClient *cli,
546 int argc, char **argv) {
Ken Sumrall3ad90722011-10-04 20:38:29 -0700547 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
548 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
549 return 0;
550 }
551
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800552 if (argc < 2) {
553 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
554 return 0;
555 }
556
557 int rc = 0;
558
559 if (!strcmp(argv[1], "checkpw")) {
560 if (argc != 3) {
561 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
562 return 0;
563 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800564 dumpArgs(argc, argv, 2);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800565 rc = cryptfs_check_passwd(argv[2]);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800566 } else if (!strcmp(argv[1], "restart")) {
567 if (argc != 2) {
568 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
569 return 0;
570 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800571 dumpArgs(argc, argv, -1);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800572 rc = cryptfs_restart();
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800573 } else if (!strcmp(argv[1], "cryptocomplete")) {
574 if (argc != 2) {
575 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
576 return 0;
577 }
578 dumpArgs(argc, argv, -1);
579 rc = cryptfs_crypto_complete();
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800580 } else if (!strcmp(argv[1], "enablecrypto")) {
581 if ( (argc != 4) || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
582 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs enablecrypto <wipe|inplace> <passwd>", false);
583 return 0;
584 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800585 dumpArgs(argc, argv, 3);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800586 rc = cryptfs_enable(argv[2], argv[3]);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800587 } else if (!strcmp(argv[1], "changepw")) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600588 if (argc != 3) {
589 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs changepw <newpasswd>", false);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800590 return 0;
591 }
Jason parks70a4b3f2011-01-28 10:10:47 -0600592 SLOGD("cryptfs changepw {}");
593 rc = cryptfs_changepw(argv[2]);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700594 } else if (!strcmp(argv[1], "verifypw")) {
595 if (argc != 3) {
596 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
597 return 0;
598 }
599 SLOGD("cryptfs verifypw {}");
600 rc = cryptfs_verify_passwd(argv[2]);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700601 } else if (!strcmp(argv[1], "getfield")) {
602 char valbuf[PROPERTY_VALUE_MAX];
603
604 if (argc != 3) {
605 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
606 return 0;
607 }
608 dumpArgs(argc, argv, -1);
609 rc = cryptfs_getfield(argv[2], valbuf, sizeof(valbuf));
610 if (rc == 0) {
611 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
612 }
613 } else if (!strcmp(argv[1], "setfield")) {
614 if (argc != 4) {
615 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
616 return 0;
617 }
618 dumpArgs(argc, argv, -1);
619 rc = cryptfs_setfield(argv[2], argv[3]);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800620 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800621 dumpArgs(argc, argv, -1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800622 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
623 }
624
Jason parks0167cb12011-01-20 18:30:39 -0600625 // Always report that the command succeeded and return the error code.
626 // The caller will check the return value to see what the error was.
627 char msg[255];
628 snprintf(msg, sizeof(msg), "%d", rc);
629 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800630
631 return 0;
632}
Ken Sumrallb87937c2013-03-19 21:46:39 -0700633
634CommandListener::FstrimCmd::FstrimCmd() :
635 VoldCommand("fstrim") {
636}
637int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
638 int argc, char **argv) {
639 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
640 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
641 return 0;
642 }
643
644 if (argc < 2) {
645 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
646 return 0;
647 }
648
649 int rc = 0;
650
651 if (!strcmp(argv[1], "dotrim")) {
652 if (argc != 2) {
653 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false);
654 return 0;
655 }
656 dumpArgs(argc, argv, -1);
657 rc = fstrim_filesystems();
658 } else {
659 dumpArgs(argc, argv, -1);
660 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false);
661 }
662
663 // Always report that the command succeeded and return the error code.
664 // The caller will check the return value to see what the error was.
665 char msg[255];
666 snprintf(msg, sizeof(msg), "%d", rc);
667 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
668
669 return 0;
670}