blob: 3a87741da74ad65da4517b74b29a170751c09ed2 [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"
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070047#include "MoveTask.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070048#include "TrimTask.h"
San Mehatf1b736b2009-10-10 17:22:08 -070049
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080050#define DUMP_ARGS 0
51
San Mehatf1b736b2009-10-10 17:22:08 -070052CommandListener::CommandListener() :
Robert Greenwalt149aa3e2012-02-16 14:43:03 -080053 FrameworkListener("vold", true) {
San Mehatd9a4e352010-03-12 13:32:47 -080054 registerCmd(new DumpCmd());
San Mehateba65e92010-01-29 05:15:16 -080055 registerCmd(new VolumeCmd());
56 registerCmd(new AsecCmd());
Kenny Root508c0e12010-07-12 09:59:49 -070057 registerCmd(new ObbCmd());
San Mehat586536c2010-02-16 17:12:00 -080058 registerCmd(new StorageCmd());
Ken Sumrallb87937c2013-03-19 21:46:39 -070059 registerCmd(new FstrimCmd());
San Mehatf1b736b2009-10-10 17:22:08 -070060}
61
Dianne Hackborn3fd60b42012-11-27 16:00:04 -080062#if DUMP_ARGS
Mark Salyzyn3e971272014-01-21 13:27:04 -080063void CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
San Mehatd9a4e352010-03-12 13:32:47 -080064 char buffer[4096];
65 char *p = buffer;
66
67 memset(buffer, 0, sizeof(buffer));
68 int i;
69 for (i = 0; i < argc; i++) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -080070 unsigned int len = strlen(argv[i]) + 1; // Account for space
San Mehatd9a4e352010-03-12 13:32:47 -080071 if (i == argObscure) {
72 len += 2; // Account for {}
73 }
74 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
75 if (i == argObscure) {
76 *p++ = '{';
77 *p++ = '}';
78 *p++ = ' ';
79 continue;
80 }
81 strcpy(p, argv[i]);
82 p+= strlen(argv[i]);
83 if (i != (argc -1)) {
84 *p++ = ' ';
85 }
86 }
87 }
San Mehat97ac40e2010-03-24 10:24:19 -070088 SLOGD("%s", buffer);
San Mehatd9a4e352010-03-12 13:32:47 -080089}
Mark Salyzyn3e971272014-01-21 13:27:04 -080090#else
91void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
92#endif
San Mehatd9a4e352010-03-12 13:32:47 -080093
Jeff Sharkey36801cc2015-03-13 16:09:20 -070094int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) {
95 if (!cond) {
96 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
97 } else {
98 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
99 }
100}
101
San Mehatd9a4e352010-03-12 13:32:47 -0800102CommandListener::DumpCmd::DumpCmd() :
103 VoldCommand("dump") {
104}
105
106int CommandListener::DumpCmd::runCommand(SocketClient *cli,
Mark Salyzyn3e971272014-01-21 13:27:04 -0800107 int /*argc*/, char ** /*argv*/) {
San Mehatd9a4e352010-03-12 13:32:47 -0800108 cli->sendMsg(0, "Dumping loop status", false);
109 if (Loop::dumpState(cli)) {
110 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
111 }
112 cli->sendMsg(0, "Dumping DM status", false);
113 if (Devmapper::dumpState(cli)) {
114 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
115 }
San Mehat96597e82010-03-17 09:50:54 -0700116 cli->sendMsg(0, "Dumping mounted filesystems", false);
117 FILE *fp = fopen("/proc/mounts", "r");
118 if (fp) {
119 char line[1024];
120 while (fgets(line, sizeof(line), fp)) {
121 line[strlen(line)-1] = '\0';
122 cli->sendMsg(0, line, false);;
123 }
124 fclose(fp);
125 }
San Mehatd9a4e352010-03-12 13:32:47 -0800126
127 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
128 return 0;
129}
130
San Mehateba65e92010-01-29 05:15:16 -0800131CommandListener::VolumeCmd::VolumeCmd() :
132 VoldCommand("volume") {
San Mehatf1b736b2009-10-10 17:22:08 -0700133}
134
San Mehateba65e92010-01-29 05:15:16 -0800135int CommandListener::VolumeCmd::runCommand(SocketClient *cli,
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700136 int argc, char **argv) {
San Mehatd9a4e352010-03-12 13:32:47 -0800137 dumpArgs(argc, argv, -1);
138
San Mehateba65e92010-01-29 05:15:16 -0800139 if (argc < 2) {
140 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
141 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700142 }
143
San Mehateba65e92010-01-29 05:15:16 -0800144 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700145 std::lock_guard<std::mutex> lock(vm->getLock());
San Mehatf1b736b2009-10-10 17:22:08 -0700146
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700147 // TODO: tease out methods not directly related to volumes
148
149 std::string cmd(argv[1]);
150 if (cmd == "reset") {
151 return sendGenericOkFail(cli, vm->reset());
152
153 } else if (cmd == "shutdown") {
154 return sendGenericOkFail(cli, vm->shutdown());
155
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700156 } else if (cmd == "debug") {
157 return sendGenericOkFail(cli, vm->setDebug(true));
158
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 } else if (cmd == "partition" && argc > 3) {
160 // partition [diskId] [public|private|mixed] [ratio]
161 std::string id(argv[2]);
162 auto disk = vm->findDisk(id);
163 if (disk == nullptr) {
164 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false);
San Mehat57df7bf2010-03-14 13:41:27 -0700165 }
166
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700167 std::string type(argv[3]);
168 if (type == "public") {
169 return sendGenericOkFail(cli, disk->partitionPublic());
170 } else if (type == "private") {
171 return sendGenericOkFail(cli, disk->partitionPrivate());
172 } else if (type == "mixed") {
173 if (argc < 4) {
174 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
175 }
176 int frac = atoi(argv[4]);
177 return sendGenericOkFail(cli, disk->partitionMixed(frac));
San Mehateba65e92010-01-29 05:15:16 -0800178 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
San Mehateba65e92010-01-29 05:15:16 -0800180 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181
182 } else if (cmd == "mkdirs" && argc > 2) {
183 // mkdirs [path]
184 return sendGenericOkFail(cli, vm->mkdirs(argv[2]));
185
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700186 } else if (cmd == "user_added" && argc > 3) {
187 // user_added [user] [serial]
188 return sendGenericOkFail(cli, vm->onUserAdded(atoi(argv[2]), atoi(argv[3])));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700189
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700190 } else if (cmd == "user_removed" && argc > 2) {
191 // user_removed [user]
192 return sendGenericOkFail(cli, vm->onUserRemoved(atoi(argv[2])));
193
194 } else if (cmd == "user_started" && argc > 2) {
195 // user_started [user]
196 return sendGenericOkFail(cli, vm->onUserStarted(atoi(argv[2])));
197
198 } else if (cmd == "user_stopped" && argc > 2) {
199 // user_stopped [user]
200 return sendGenericOkFail(cli, vm->onUserStopped(atoi(argv[2])));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700201
202 } else if (cmd == "mount" && argc > 2) {
203 // mount [volId] [flags] [user]
204 std::string id(argv[2]);
205 auto vol = vm->findVolume(id);
206 if (vol == nullptr) {
207 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700208 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700210 int mountFlags = (argc > 3) ? atoi(argv[3]) : 0;
211 userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700213 vol->setMountFlags(mountFlags);
214 vol->setMountUserId(mountUserId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700215
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700216 int res = vol->mount();
217 if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) {
218 vm->setPrimary(vol);
219 }
220 return sendGenericOkFail(cli, res);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221
222 } else if (cmd == "unmount" && argc > 2) {
223 // unmount [volId]
224 std::string id(argv[2]);
225 auto vol = vm->findVolume(id);
226 if (vol == nullptr) {
227 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
228 }
229
230 return sendGenericOkFail(cli, vol->unmount());
231
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700232 } else if (cmd == "format" && argc > 3) {
233 // format [volId] [fsType|auto]
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 std::string id(argv[2]);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700235 std::string fsType(argv[3]);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 auto vol = vm->findVolume(id);
237 if (vol == nullptr) {
238 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
239 }
240
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700241 return sendGenericOkFail(cli, vol->format(fsType));
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700242
243 } else if (cmd == "move_storage" && argc > 3) {
244 // move_storage [fromVolId] [toVolId]
245 auto fromVol = vm->findVolume(std::string(argv[2]));
246 auto toVol = vm->findVolume(std::string(argv[3]));
247 if (fromVol == nullptr || toVol == nullptr) {
248 return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false);
249 }
250
251 (new android::vold::MoveTask(fromVol, toVol))->start();
252 return sendGenericOkFail(cli, 0);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700253
254 } else if (cmd == "benchmark" && argc > 2) {
255 // benchmark [volId]
256 std::string id(argv[2]);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700257 nsecs_t res = vm->benchmarkPrivate(id);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700258 return cli->sendMsg(ResponseCode::CommandOkay,
259 android::base::StringPrintf("%" PRId64, res).c_str(), false);
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700260
261 } else if (cmd == "forget_partition" && argc > 2) {
262 // forget_partition [partGuid]
263 std::string partGuid(argv[2]);
264 return sendGenericOkFail(cli, vm->forgetPartition(partGuid));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700265
266 } else if (cmd == "remount_uid" && argc > 3) {
267 // remount_uid [uid] [none|default|read|write]
268 uid_t uid = atoi(argv[2]);
269 std::string mode(argv[3]);
270 return sendGenericOkFail(cli, vm->remountUid(uid, mode));
San Mehateba65e92010-01-29 05:15:16 -0800271 }
272
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700273 return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700274}
275
San Mehat586536c2010-02-16 17:12:00 -0800276CommandListener::StorageCmd::StorageCmd() :
277 VoldCommand("storage") {
278}
279
280int CommandListener::StorageCmd::runCommand(SocketClient *cli,
281 int argc, char **argv) {
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700282 /* Guarantied to be initialized by vold's main() before the CommandListener is active */
283 extern struct fstab *fstab;
284
San Mehatd9a4e352010-03-12 13:32:47 -0800285 dumpArgs(argc, argv, -1);
286
San Mehat586536c2010-02-16 17:12:00 -0800287 if (argc < 2) {
288 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
289 return 0;
290 }
291
Mohamad Ayyash7929aa72014-03-10 15:55:33 -0700292 if (!strcmp(argv[1], "mountall")) {
293 if (argc != 2) {
294 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false);
295 return 0;
296 }
297 fs_mgr_mount_all(fstab);
298 cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false);
299 return 0;
300 }
San Mehat586536c2010-02-16 17:12:00 -0800301 if (!strcmp(argv[1], "users")) {
302 DIR *dir;
303 struct dirent *de;
304
JP Abgralledf7adf2014-03-12 10:41:05 -0700305 if (argc < 3) {
306 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false);
307 return 0;
308 }
San Mehat586536c2010-02-16 17:12:00 -0800309 if (!(dir = opendir("/proc"))) {
310 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
311 return 0;
312 }
313
314 while ((de = readdir(dir))) {
315 int pid = Process::getPid(de->d_name);
316
317 if (pid < 0) {
318 continue;
319 }
320
321 char processName[255];
322 Process::getProcessName(pid, processName, sizeof(processName));
323
324 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
325 Process::checkFileMaps(pid, argv[2]) ||
326 Process::checkSymLink(pid, argv[2], "cwd") ||
327 Process::checkSymLink(pid, argv[2], "root") ||
328 Process::checkSymLink(pid, argv[2], "exe")) {
329
330 char msg[1024];
331 snprintf(msg, sizeof(msg), "%d %s", pid, processName);
332 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
333 }
334 }
335 closedir(dir);
336 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
337 } else {
338 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
339 }
340 return 0;
341}
342
San Mehateba65e92010-01-29 05:15:16 -0800343CommandListener::AsecCmd::AsecCmd() :
344 VoldCommand("asec") {
San Mehata19b2502010-01-06 10:33:53 -0800345}
346
Kenny Root344ca102012-04-03 17:23:01 -0700347void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
348 DIR *d = opendir(directory);
349
350 if (!d) {
351 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
352 return;
353 }
354
355 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -0700356 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Kenny Root344ca102012-04-03 17:23:01 -0700357
358 struct dirent *dent = (struct dirent *) malloc(dirent_len);
359 if (dent == NULL) {
360 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
361 return;
362 }
363
364 struct dirent *result;
365
366 while (!readdir_r(d, dent, &result) && result != NULL) {
367 if (dent->d_name[0] == '.')
368 continue;
369 if (dent->d_type != DT_REG)
370 continue;
371 size_t name_len = strlen(dent->d_name);
372 if (name_len > 5 && name_len < 260 &&
373 !strcmp(&dent->d_name[name_len - 5], ".asec")) {
374 char id[255];
375 memset(id, 0, sizeof(id));
Kenny Root7b0bc852012-04-27 15:33:58 -0700376 strlcpy(id, dent->d_name, name_len - 4);
Kenny Root344ca102012-04-03 17:23:01 -0700377 cli->sendMsg(ResponseCode::AsecListResult, id, false);
378 }
379 }
380 closedir(d);
381
382 free(dent);
383}
384
San Mehateba65e92010-01-29 05:15:16 -0800385int CommandListener::AsecCmd::runCommand(SocketClient *cli,
386 int argc, char **argv) {
387 if (argc < 2) {
388 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
San Mehata19b2502010-01-06 10:33:53 -0800389 return 0;
390 }
391
San Mehateba65e92010-01-29 05:15:16 -0800392 VolumeManager *vm = VolumeManager::Instance();
393 int rc = 0;
San Mehata19b2502010-01-06 10:33:53 -0800394
San Mehateba65e92010-01-29 05:15:16 -0800395 if (!strcmp(argv[1], "list")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800396 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800397
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700398 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT);
399 listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT);
San Mehateba65e92010-01-29 05:15:16 -0800400 } else if (!strcmp(argv[1], "create")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800401 dumpArgs(argc, argv, 5);
Kenny Root344ca102012-04-03 17:23:01 -0700402 if (argc != 8) {
San Mehateba65e92010-01-29 05:15:16 -0800403 cli->sendMsg(ResponseCode::CommandSyntaxError,
Kenny Root344ca102012-04-03 17:23:01 -0700404 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
405 "<isExternal>", false);
San Mehateba65e92010-01-29 05:15:16 -0800406 return 0;
407 }
408
409 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
Kenny Root344ca102012-04-03 17:23:01 -0700410 const bool isExternal = (atoi(argv[7]) == 1);
411 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700412 } else if (!strcmp(argv[1], "resize")) {
413 dumpArgs(argc, argv, -1);
414 if (argc != 5) {
415 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
416 return 0;
417 }
418 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
419 rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800420 } else if (!strcmp(argv[1], "finalize")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800421 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800422 if (argc != 3) {
423 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
424 return 0;
425 }
San Mehat8f2875b2010-02-18 11:40:49 -0800426 rc = vm->finalizeAsec(argv[2]);
Kenny Root344ca102012-04-03 17:23:01 -0700427 } else if (!strcmp(argv[1], "fixperms")) {
428 dumpArgs(argc, argv, -1);
429 if (argc != 5) {
430 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
431 return 0;
432 }
433
434 char *endptr;
435 gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
436 if (*endptr != '\0') {
437 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
438 return 0;
439 }
440
441 rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
San Mehateba65e92010-01-29 05:15:16 -0800442 } else if (!strcmp(argv[1], "destroy")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800443 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800444 if (argc < 3) {
445 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800446 return 0;
447 }
San Mehat4ba89482010-02-18 09:00:18 -0800448 bool force = false;
449 if (argc > 3 && !strcmp(argv[3], "force")) {
450 force = true;
451 }
San Mehat8f2875b2010-02-18 11:40:49 -0800452 rc = vm->destroyAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800453 } else if (!strcmp(argv[1], "mount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800454 dumpArgs(argc, argv, 3);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700455 if (argc != 6) {
San Mehateba65e92010-01-29 05:15:16 -0800456 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700457 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false);
San Mehateba65e92010-01-29 05:15:16 -0800458 return 0;
459 }
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700460 bool readOnly = true;
461 if (!strcmp(argv[5], "rw")) {
462 readOnly = false;
463 }
464 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly);
San Mehateba65e92010-01-29 05:15:16 -0800465 } else if (!strcmp(argv[1], "unmount")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800466 dumpArgs(argc, argv, -1);
San Mehat4ba89482010-02-18 09:00:18 -0800467 if (argc < 3) {
468 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
San Mehateba65e92010-01-29 05:15:16 -0800469 return 0;
470 }
San Mehat4ba89482010-02-18 09:00:18 -0800471 bool force = false;
472 if (argc > 3 && !strcmp(argv[3], "force")) {
473 force = true;
474 }
San Mehat8f2875b2010-02-18 11:40:49 -0800475 rc = vm->unmountAsec(argv[2], force);
San Mehateba65e92010-01-29 05:15:16 -0800476 } else if (!strcmp(argv[1], "rename")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800477 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800478 if (argc != 4) {
479 cli->sendMsg(ResponseCode::CommandSyntaxError,
480 "Usage: asec rename <old_id> <new_id>", false);
481 return 0;
482 }
San Mehat8f2875b2010-02-18 11:40:49 -0800483 rc = vm->renameAsec(argv[2], argv[3]);
San Mehateba65e92010-01-29 05:15:16 -0800484 } else if (!strcmp(argv[1], "path")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800485 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800486 if (argc != 3) {
487 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
488 return 0;
489 }
490 char path[255];
491
San Mehat88ac2c02010-03-23 11:15:58 -0700492 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
San Mehateba65e92010-01-29 05:15:16 -0800493 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
San Mehat88ac2c02010-03-23 11:15:58 -0700494 return 0;
San Mehateba65e92010-01-29 05:15:16 -0800495 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700496 } else if (!strcmp(argv[1], "fspath")) {
497 dumpArgs(argc, argv, -1);
498 if (argc != 3) {
499 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
500 return 0;
501 }
502 char path[255];
503
504 if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
505 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
506 return 0;
507 }
San Mehata19b2502010-01-06 10:33:53 -0800508 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800509 dumpArgs(argc, argv, -1);
San Mehateba65e92010-01-29 05:15:16 -0800510 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
San Mehata19b2502010-01-06 10:33:53 -0800511 }
512
San Mehat8f2875b2010-02-18 11:40:49 -0800513 if (!rc) {
514 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
515 } else {
516 rc = ResponseCode::convertFromErrno();
517 cli->sendMsg(rc, "asec operation failed", true);
518 }
519
San Mehata19b2502010-01-06 10:33:53 -0800520 return 0;
521}
San Mehat2350c442010-03-02 13:16:50 -0800522
Kenny Root508c0e12010-07-12 09:59:49 -0700523CommandListener::ObbCmd::ObbCmd() :
524 VoldCommand("obb") {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700525}
526
Kenny Root508c0e12010-07-12 09:59:49 -0700527int CommandListener::ObbCmd::runCommand(SocketClient *cli,
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700528 int argc, char **argv) {
529 if (argc < 2) {
530 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
531 return 0;
532 }
533
534 VolumeManager *vm = VolumeManager::Instance();
535 int rc = 0;
536
Kenny Root508c0e12010-07-12 09:59:49 -0700537 if (!strcmp(argv[1], "list")) {
538 dumpArgs(argc, argv, -1);
539
540 rc = vm->listMountedObbs(cli);
541 } else if (!strcmp(argv[1], "mount")) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700542 dumpArgs(argc, argv, 3);
543 if (argc != 5) {
544 cli->sendMsg(ResponseCode::CommandSyntaxError,
Jeff Sharkey69479042012-09-25 16:14:57 -0700545 "Usage: obb mount <filename> <key> <ownerGid>", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700546 return 0;
547 }
Kenny Root508c0e12010-07-12 09:59:49 -0700548 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700549 } else if (!strcmp(argv[1], "unmount")) {
550 dumpArgs(argc, argv, -1);
551 if (argc < 3) {
Kenny Root508c0e12010-07-12 09:59:49 -0700552 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700553 return 0;
554 }
555 bool force = false;
556 if (argc > 3 && !strcmp(argv[3], "force")) {
557 force = true;
558 }
Kenny Root508c0e12010-07-12 09:59:49 -0700559 rc = vm->unmountObb(argv[2], force);
560 } else if (!strcmp(argv[1], "path")) {
561 dumpArgs(argc, argv, -1);
562 if (argc != 3) {
563 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
564 return 0;
565 }
566 char path[255];
567
568 if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
569 cli->sendMsg(ResponseCode::AsecPathResult, path, false);
570 return 0;
571 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700572 } else {
573 dumpArgs(argc, argv, -1);
Kenny Root508c0e12010-07-12 09:59:49 -0700574 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700575 }
576
577 if (!rc) {
Kenny Root508c0e12010-07-12 09:59:49 -0700578 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700579 } else {
580 rc = ResponseCode::convertFromErrno();
Kenny Root508c0e12010-07-12 09:59:49 -0700581 cli->sendMsg(rc, "obb operation failed", true);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700582 }
583
584 return 0;
585}
586
Ken Sumrallb87937c2013-03-19 21:46:39 -0700587CommandListener::FstrimCmd::FstrimCmd() :
588 VoldCommand("fstrim") {
589}
590int CommandListener::FstrimCmd::runCommand(SocketClient *cli,
591 int argc, char **argv) {
592 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
593 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false);
594 return 0;
595 }
596
597 if (argc < 2) {
598 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
599 return 0;
600 }
601
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700602 VolumeManager *vm = VolumeManager::Instance();
603 std::lock_guard<std::mutex> lock(vm->getLock());
Ken Sumrallb87937c2013-03-19 21:46:39 -0700604
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700605 int flags = 0;
606
607 std::string cmd(argv[1]);
608 if (cmd == "dotrim") {
609 flags = 0;
610 } else if (cmd == "dotrimbench") {
611 flags = android::vold::TrimTask::Flags::kBenchmarkAfter;
612 } else if (cmd == "dodtrim") {
613 flags = android::vold::TrimTask::Flags::kDeepTrim;
614 } else if (cmd == "dodtrimbench") {
615 flags = android::vold::TrimTask::Flags::kDeepTrim
616 | android::vold::TrimTask::Flags::kBenchmarkAfter;
Ken Sumrallb87937c2013-03-19 21:46:39 -0700617 }
618
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700619 (new android::vold::TrimTask(flags))->start();
620 return sendGenericOkFail(cli, 0);
Ken Sumrallb87937c2013-03-19 21:46:39 -0700621}