blob: 74fe3fcdd3a6fa2e048064321dab75a48084dea2 [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>
Yabin Cuid1104f72015-01-02 13:28:28 -080026#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080027#include <string.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070028#include <stdint.h>
29#include <inttypes.h>
San Mehatf1b736b2009-10-10 17:22:08 -070030
San Mehatd9a4e352010-03-12 13:32:47 -080031#define LOG_TAG "VoldCmdListener"
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070032
33#include <base/stringprintf.h>
34#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070035#include <cutils/log.h>
36
37#include <sysutils/SocketClient.h>
Ken Sumrall3ad90722011-10-04 20:38:29 -070038#include <private/android_filesystem_config.h>
San Mehatf1b736b2009-10-10 17:22:08 -070039
40#include "CommandListener.h"
41#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070042#include "VolumeBase.h"
San Mehata2677e42009-12-13 10:40:18 -080043#include "ResponseCode.h"
San Mehat586536c2010-02-16 17:12:00 -080044#include "Process.h"
San Mehatd9a4e352010-03-12 13:32:47 -080045#include "Loop.h"
46#include "Devmapper.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080047#include "cryptfs.h"
Ken Sumrallb87937c2013-03-19 21:46:39 -070048#include "fstrim.h"
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070049#include "MoveTask.h"
San Mehatf1b736b2009-10-10 17:22:08 -070050
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080051#define DUMP_ARGS 0
52
San Mehatf1b736b2009-10-10 17:22:08 -070053CommandListener::CommandListener() :
Robert Greenwalt149aa3e2012-02-16 14:43:03 -080054 FrameworkListener("vold", true) {
San Mehatd9a4e352010-03-12 13:32:47 -080055 registerCmd(new DumpCmd());
San Mehateba65e92010-01-29 05:15:16 -080056 registerCmd(new VolumeCmd());
57 registerCmd(new AsecCmd());
Kenny Root508c0e12010-07-12 09:59:49 -070058 registerCmd(new ObbCmd());
San Mehat586536c2010-02-16 17:12:00 -080059 registerCmd(new StorageCmd());
Ken Sumrallb87937c2013-03-19 21:46:39 -070060 registerCmd(new FstrimCmd());
San Mehatf1b736b2009-10-10 17:22:08 -070061}
62
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080063#if DUMP_ARGS
Mark Salyzyn3e971272014-01-21 13:27:04 -080064void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
San Mehatd9a4e352010-03-12 13:32:47 -080065 char buffer[4096];
66 char *p = buffer;
67
68 memset(buffer, 0, sizeof(buffer));
69 int i;
70 for (i = 0; i < argc; i++) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -080071 unsigned int len = strlen(argv[i]) + 1; // Account for space
San Mehatd9a4e352010-03-12 13:32:47 -080072 if (i == argObscure) {
73 len += 2; // Account for {}
74 }
75 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
76 if (i == argObscure) {
77 *p++ = '{';
78 *p++ = '}';
79 *p++ = ' ';
80 continue;
81 }
82 strcpy(p, argv[i]);
83 p+= strlen(argv[i]);
84 if (i != (argc -1)) {
85 *p++ = ' ';
86 }
87 }
88 }
San Mehat97ac40e2010-03-24 10:24:19 -070089 SLOGD("%s", buffer);
San Mehatd9a4e352010-03-12 13:32:47 -080090}
Mark Salyzyn3e971272014-01-21 13:27:04 -080091#else
92void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
93#endif
San Mehatd9a4e352010-03-12 13:32:47 -080094
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) {
96 if (!cond) {
97 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
98 } else {
99 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
100 }
101}
102
San Mehatd9a4e352010-03-12 13:32:47 -0800103CommandListener::DumpCmd::DumpCmd() :
104 VoldCommand("dump") {
105}
106
107int CommandListener::DumpCmd::runCommand(SocketClient *cli,
Mark Salyzyn3e971272014-01-21 13:27:04 -0800108 int /*argc*/, char ** /*argv*/) {
San Mehatd9a4e352010-03-12 13:32:47 -0800109 cli->sendMsg(0, "Dumping loop status", false);
110 if (Loop::dumpState(cli)) {
111 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
112 }
113 cli->sendMsg(0, "Dumping DM status", false);
114 if (Devmapper::dumpState(cli)) {
115 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
116 }
San Mehat96597e82010-03-17 09:50:54 -0700117 cli->sendMsg(0, "Dumping mounted filesystems", false);
118 FILE *fp = fopen("/proc/mounts", "r");
119 if (fp) {
120 char line[1024];
121 while (fgets(line, sizeof(line), fp)) {
122 line[strlen(line)-1] = '\0';
123 cli->sendMsg(0, line, false);;
124 }
125 fclose(fp);
126 }
San Mehatd9a4e352010-03-12 13:32:47 -0800127
128 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
129 return 0;
130}
131
San Mehateba65e92010-01-29 05:15:16 -0800132CommandListener::VolumeCmd::VolumeCmd() :
133 VoldCommand("volume") {
San Mehatf1b736b2009-10-10 17:22:08 -0700134}
135
San Mehateba65e92010-01-29 05:15:16 -0800136int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700137 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800138 dumpArgs(argc, argv, -1);
139
San Mehateba65e92010-01-29 05:15:16 -0800140 if (argc < 2) {
141 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
142 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700143 }
144
San Mehateba65e92010-01-29 05:15:16 -0800145 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700146 std::lock_guard<std::mutex> lock(vm->getLock());
San Mehatf1b736b2009-10-10 17:22:08 -0700147
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148 // TODO: tease out methods not directly related to volumes
149
150 std::string cmd(argv[1]);
151 if (cmd == "reset") {
152 return sendGenericOkFail(cli, vm->reset());
153
154 } else if (cmd == "shutdown") {
155 return sendGenericOkFail(cli, vm->shutdown());
156
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700157 } else if (cmd == "debug") {
158 return sendGenericOkFail(cli, vm->setDebug(true));
159
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700160 } else if (cmd == "partition" && argc > 3) {
161 // partition [diskId] [public|private|mixed] [ratio]
162 std::string id(argv[2]);
163 auto disk = vm->findDisk(id);
164 if (disk == nullptr) {
165 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700166 }
167
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700168 std::string type(argv[3]);
169 if (type == "public") {
170 return sendGenericOkFail(cli, disk->partitionPublic());
171 } else if (type == "private") {
172 return sendGenericOkFail(cli, disk->partitionPrivate());
173 } else if (type == "mixed") {
174 if (argc < 4) {
175 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
176 }
177 int frac = atoi(argv[4]);
178 return sendGenericOkFail(cli, disk->partitionMixed(frac));
San Mehateba65e92010-01-29 05:15:16 -0800179 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700180 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
San Mehateba65e92010-01-29 05:15:16 -0800181 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182
183 } else if (cmd == "mkdirs" && argc > 2) {
184 // mkdirs [path]
185 return sendGenericOkFail(cli, vm->mkdirs(argv[2]));
186
187 } else if (cmd == "start_user" && argc > 2) {
188 // start_user [user]
189 return sendGenericOkFail(cli, vm->startUser(atoi(argv[2])));
190
191 } else if (cmd == "cleanup_user" && argc > 2) {
192 // cleanup_user [user]
193 return sendGenericOkFail(cli, vm->cleanupUser(atoi(argv[2])));
194
195 } else if (cmd == "mount" && argc > 2) {
196 // mount [volId] [flags] [user]
197 std::string id(argv[2]);
198 auto vol = vm->findVolume(id);
199 if (vol == nullptr) {
200 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700201 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700202
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700203 int mountFlags = (argc > 3) ? atoi(argv[3]) : 0;
204 userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700205
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700206 vol->setMountFlags(mountFlags);
207 vol->setMountUserId(mountUserId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700209 int res = vol->mount();
210 if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) {
211 vm->setPrimary(vol);
212 }
213 return sendGenericOkFail(cli, res);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214
215 } else if (cmd == "unmount" && argc > 2) {
216 // unmount [volId]
217 std::string id(argv[2]);
218 auto vol = vm->findVolume(id);
219 if (vol == nullptr) {
220 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
221 }
222
223 return sendGenericOkFail(cli, vol->unmount());
224
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700225 } else if (cmd == "format" && argc > 3) {
226 // format [volId] [fsType|auto]
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 std::string id(argv[2]);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700228 std::string fsType(argv[3]);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700229 auto vol = vm->findVolume(id);
230 if (vol == nullptr) {
231 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
232 }
233
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700234 return sendGenericOkFail(cli, vol->format(fsType));
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700235
236 } else if (cmd == "move_storage" && argc > 3) {
237 // move_storage [fromVolId] [toVolId]
238 auto fromVol = vm->findVolume(std::string(argv[2]));
239 auto toVol = vm->findVolume(std::string(argv[3]));
240 if (fromVol == nullptr || toVol == nullptr) {
241 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
242 }
243
244 (new android::vold::MoveTask(fromVol, toVol))->start();
245 return sendGenericOkFail(cli, 0);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700246
247 } else if (cmd == "benchmark" && argc > 2) {
248 // benchmark [volId]
249 std::string id(argv[2]);
250 nsecs_t res = vm->benchmarkVolume(id);
251 return cli->sendMsg(ResponseCode::CommandOkay,
252 android::base::StringPrintf("%" PRId64, res).c_str(), false);
San Mehateba65e92010-01-29 05:15:16 -0800253 }
254
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700255 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700256}
257
San Mehat586536c2010-02-16 17:12:00 -0800258CommandListener::StorageCmd::StorageCmd() :
259 VoldCommand("storage") {
260}
261
262int CommandListener::StorageCmd::runCommand(SocketClient *cli,
263 int argc, char **argv) {
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700264 /* Guarantied to be initialized by vold's main() before the CommandListener is active */
265 extern struct fstab *fstab;
266
San Mehatd9a4e352010-03-12 13:32:47 -0800267 dumpArgs(argc, argv, -1);
268
San Mehat586536c2010-02-16 17:12:00 -0800269 if (argc < 2) {
270 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
271 return 0;
272 }
273
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700274 if (!strcmp(argv[1], "mountall")) {
275 if (argc != 2) {
276 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false);
277 return 0;
278 }
279 fs_mgr_mount_all(fstab);
280 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false);
281 return 0;
282 }
San Mehat586536c2010-02-16 17:12:00 -0800283 if (!strcmp(argv[1], "users")) {
284 DIR *dir;
285 struct dirent *de;
286
JP Abgralledf7adf2014-03-12 10:41:05 -0700287 if (argc < 3) {
288 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false);
289 return 0;
290 }
San Mehat586536c2010-02-16 17:12:00 -0800291 if (!(dir = opendir("/proc"))) {
292 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
293 return 0;
294 }
295
296 while ((de = readdir(dir))) {
297 int pid = Process::getPid(de->d_name);
298
299 if (pid < 0) {
300 continue;
301 }
302
303 char processName[255];
304 Process::getProcessName(pid, processName, sizeof(processName));
305
306 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
307 Process::checkFileMaps(pid, argv[2]) ||
308 Process::checkSymLink(pid, argv[2], "cwd") ||
309 Process::checkSymLink(pid, argv[2], "root") ||
310 Process::checkSymLink(pid, argv[2], "exe")) {
311
312 char msg[1024];
313 snprintf(msg, sizeof(msg), "%d %s", pid, processName);
314 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
315 }
316 }
317 closedir(dir);
318 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
319 } else {
320 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
321 }
322 return 0;
323}
324
San Mehateba65e92010-01-29 05:15:16 -0800325CommandListener::AsecCmd::AsecCmd() :
326 VoldCommand("asec") {
San Mehata19b2502010-01-06 10:33:53 -0800327}
328
Kenny Root344ca102012-04-03 17:23:01 -0700329void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
330 DIR *d = opendir(directory);
331
332 if (!d) {
333 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
334 return;
335 }
336
337 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -0700338 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Kenny Root344ca102012-04-03 17:23:01 -0700339
340 struct dirent *dent = (struct dirent *) malloc(dirent_len);
341 if (dent == NULL) {
342 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
343 return;
344 }
345
346 struct dirent *result;
347
348 while (!readdir_r(d, dent, &result) && result != NULL) {
349 if (dent->d_name[0] == '.')
350 continue;
351 if (dent->d_type != DT_REG)
352 continue;
353 size_t name_len = strlen(dent->d_name);
354 if (name_len > 5 && name_len < 260 &&
355 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
356 char id[255];
357 memset(id, 0, sizeof(id));
Kenny Root7b0bc852012-04-27 15:33:58 -0700358 strlcpy(id, dent->d_name, name_len - 4);
Kenny Root344ca102012-04-03 17:23:01 -0700359 cli->sendMsg(ResponseCode::AsecListResult, id, false);
360 }
361 }
362 closedir(d);
363
364 free(dent);
365}
366
San Mehateba65e92010-01-29 05:15:16 -0800367int CommandListener::AsecCmd::runCommand(SocketClient *cli,
368 int argc, char **argv) {
369 if (argc < 2) {
370 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
San Mehata19b2502010-01-06 10:33:53 -0800371 return 0;
372 }
373
San Mehateba65e92010-01-29 05:15:16 -0800374 VolumeManager *vm = VolumeManager::Instance();
375 int rc = 0;
San Mehata19b2502010-01-06 10:33:53 -0800376
San Mehateba65e92010-01-29 05:15:16 -0800377 if (!strcmp(argv[1], "list")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800378 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800379
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700380 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT);
381 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT);
San Mehateba65e92010-01-29 05:15:16 -0800382 } else if (!strcmp(argv[1], "create")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800383 dumpArgs(argc, argv, 5);
Kenny Root344ca102012-04-03 17:23:01 -0700384 if (argc != 8) {
San Mehateba65e92010-01-29 05:15:16 -0800385 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root344ca102012-04-03 17:23:01 -0700386 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
387 "<isExternal>", false);
San Mehateba65e92010-01-29 05:15:16 -0800388 return 0;
389 }
390
391 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
Kenny Root344ca102012-04-03 17:23:01 -0700392 const bool isExternal = (atoi(argv[7]) == 1);
393 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700394 } else if (!strcmp(argv[1], "resize")) {
395 dumpArgs(argc, argv, -1);
396 if (argc != 5) {
397 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
398 return 0;
399 }
400 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
401 rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800402 } else if (!strcmp(argv[1], "finalize")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800403 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800404 if (argc != 3) {
405 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
406 return 0;
407 }
San Mehat8f2875b2010-02-18 11:40:49 -0800408 rc = vm->finalizeAsec(argv[2]);
Kenny Root344ca102012-04-03 17:23:01 -0700409 } else if (!strcmp(argv[1], "fixperms")) {
410 dumpArgs(argc, argv, -1);
411 if (argc != 5) {
412 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
413 return 0;
414 }
415
416 char *endptr;
417 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
418 if (*endptr != '\0') {
419 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
420 return 0;
421 }
422
423 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800424 } else if (!strcmp(argv[1], "destroy")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800425 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800426 if (argc < 3) {
427 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800428 return 0;
429 }
San Mehat4ba89482010-02-18 09:00:18 -0800430 bool force = false;
431 if (argc > 3 && !strcmp(argv[3], "force")) {
432 force = true;
433 }
San Mehat8f2875b2010-02-18 11:40:49 -0800434 rc = vm->destroyAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800435 } else if (!strcmp(argv[1], "mount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800436 dumpArgs(argc, argv, 3);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700437 if (argc != 6) {
San Mehateba65e92010-01-29 05:15:16 -0800438 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700439 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false);
San Mehateba65e92010-01-29 05:15:16 -0800440 return 0;
441 }
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700442 bool readOnly = true;
443 if (!strcmp(argv[5], "rw")) {
444 readOnly = false;
445 }
446 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly);
San Mehateba65e92010-01-29 05:15:16 -0800447 } else if (!strcmp(argv[1], "unmount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800448 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800449 if (argc < 3) {
450 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800451 return 0;
452 }
San Mehat4ba89482010-02-18 09:00:18 -0800453 bool force = false;
454 if (argc > 3 && !strcmp(argv[3], "force")) {
455 force = true;
456 }
San Mehat8f2875b2010-02-18 11:40:49 -0800457 rc = vm->unmountAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800458 } else if (!strcmp(argv[1], "rename")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800459 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800460 if (argc != 4) {
461 cli->sendMsg(ResponseCode::CommandSyntaxError,
462 "Usage: asec rename <old_id> <new_id>", false);
463 return 0;
464 }
San Mehat8f2875b2010-02-18 11:40:49 -0800465 rc = vm->renameAsec(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800466 } else if (!strcmp(argv[1], "path")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800467 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800468 if (argc != 3) {
469 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
470 return 0;
471 }
472 char path[255];
473
San Mehat88ac2c02010-03-23 11:15:58 -0700474 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehateba65e92010-01-29 05:15:16 -0800475 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
San Mehat88ac2c02010-03-23 11:15:58 -0700476 return 0;
San Mehateba65e92010-01-29 05:15:16 -0800477 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700478 } else if (!strcmp(argv[1], "fspath")) {
479 dumpArgs(argc, argv, -1);
480 if (argc != 3) {
481 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
482 return 0;
483 }
484 char path[255];
485
486 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
487 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
488 return 0;
489 }
San Mehata19b2502010-01-06 10:33:53 -0800490 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800491 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800492 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
San Mehata19b2502010-01-06 10:33:53 -0800493 }
494
San Mehat8f2875b2010-02-18 11:40:49 -0800495 if (!rc) {
496 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
497 } else {
498 rc = ResponseCode::convertFromErrno();
499 cli->sendMsg(rc, "asec operation failed", true);
500 }
501
San Mehata19b2502010-01-06 10:33:53 -0800502 return 0;
503}
San Mehat2350c442010-03-02 13:16:50 -0800504
Kenny Root508c0e12010-07-12 09:59:49 -0700505CommandListener::ObbCmd::ObbCmd() :
506 VoldCommand("obb") {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700507}
508
Kenny Root508c0e12010-07-12 09:59:49 -0700509int CommandListener::ObbCmd::runCommand(SocketClient *cli,
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700510 int argc, char **argv) {
511 if (argc < 2) {
512 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
513 return 0;
514 }
515
516 VolumeManager *vm = VolumeManager::Instance();
517 int rc = 0;
518
Kenny Root508c0e12010-07-12 09:59:49 -0700519 if (!strcmp(argv[1], "list")) {
520 dumpArgs(argc, argv, -1);
521
522 rc = vm->listMountedObbs(cli);
523 } else if (!strcmp(argv[1], "mount")) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700524 dumpArgs(argc, argv, 3);
525 if (argc != 5) {
526 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey69479042012-09-25 16:14:57 -0700527 "Usage: obb mount <filename> <key> <ownerGid>", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700528 return 0;
529 }
Kenny Root508c0e12010-07-12 09:59:49 -0700530 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700531 } else if (!strcmp(argv[1], "unmount")) {
532 dumpArgs(argc, argv, -1);
533 if (argc < 3) {
Kenny Root508c0e12010-07-12 09:59:49 -0700534 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700535 return 0;
536 }
537 bool force = false;
538 if (argc > 3 && !strcmp(argv[3], "force")) {
539 force = true;
540 }
Kenny Root508c0e12010-07-12 09:59:49 -0700541 rc = vm->unmountObb(argv[2], force);
542 } else if (!strcmp(argv[1], "path")) {
543 dumpArgs(argc, argv, -1);
544 if (argc != 3) {
545 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
546 return 0;
547 }
548 char path[255];
549
550 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
551 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
552 return 0;
553 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700554 } else {
555 dumpArgs(argc, argv, -1);
Kenny Root508c0e12010-07-12 09:59:49 -0700556 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700557 }
558
559 if (!rc) {
Kenny Root508c0e12010-07-12 09:59:49 -0700560 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700561 } else {
562 rc = ResponseCode::convertFromErrno();
Kenny Root508c0e12010-07-12 09:59:49 -0700563 cli->sendMsg(rc, "obb operation failed", true);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700564 }
565
566 return 0;
567}
568
Ken Sumrallb87937c2013-03-19 21:46:39 -0700569CommandListener::FstrimCmd::FstrimCmd() :
570 VoldCommand("fstrim") {
571}
572int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
573 int argc, char **argv) {
574 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
575 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
576 return 0;
577 }
578
579 if (argc < 2) {
580 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
581 return 0;
582 }
583
584 int rc = 0;
585
586 if (!strcmp(argv[1], "dotrim")) {
587 if (argc != 2) {
588 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false);
589 return 0;
590 }
591 dumpArgs(argc, argv, -1);
JP Abgrall422bdb72014-07-29 13:24:56 -0700592 rc = fstrim_filesystems(0);
593 } else if (!strcmp(argv[1], "dodtrim")) {
594 if (argc != 2) {
595 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false);
596 return 0;
597 }
598 dumpArgs(argc, argv, -1);
599 rc = fstrim_filesystems(1); /* Do Deep Discard trim */
Ken Sumrallb87937c2013-03-19 21:46:39 -0700600 } else {
601 dumpArgs(argc, argv, -1);
602 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false);
603 }
604
605 // Always report that the command succeeded and return the error code.
606 // The caller will check the return value to see what the error was.
607 char msg[255];
608 snprintf(msg, sizeof(msg), "%d", rc);
609 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
610
611 return 0;
612}