CommandListener.cpp revision 57df7bf33968d65c23f3d0dc9f30a8ce2625b1d0
1/* 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> 19#include <sys/types.h> 20#include <netinet/in.h> 21#include <arpa/inet.h> 22#include <dirent.h> 23#include <errno.h> 24#include <fcntl.h> 25 26#define LOG_TAG "VoldCmdListener" 27#include <cutils/log.h> 28 29#include <sysutils/SocketClient.h> 30 31#include "CommandListener.h" 32#include "VolumeManager.h" 33#include "ResponseCode.h" 34#include "Process.h" 35#include "Xwarp.h" 36#include "Loop.h" 37#include "Devmapper.h" 38 39CommandListener::CommandListener() : 40 FrameworkListener("vold") { 41 registerCmd(new DumpCmd()); 42 registerCmd(new VolumeCmd()); 43 registerCmd(new AsecCmd()); 44 registerCmd(new ShareCmd()); 45 registerCmd(new StorageCmd()); 46 registerCmd(new XwarpCmd()); 47} 48 49void CommandListener::dumpArgs(int argc, char **argv, int argObscure) { 50 char buffer[4096]; 51 char *p = buffer; 52 53 memset(buffer, 0, sizeof(buffer)); 54 int i; 55 for (i = 0; i < argc; i++) { 56 int len = strlen(argv[i]) + 1; // Account for space 57 if (i == argObscure) { 58 len += 2; // Account for {} 59 } 60 if (((p - buffer) + len) < (sizeof(buffer)-1)) { 61 if (i == argObscure) { 62 *p++ = '{'; 63 *p++ = '}'; 64 *p++ = ' '; 65 continue; 66 } 67 strcpy(p, argv[i]); 68 p+= strlen(argv[i]); 69 if (i != (argc -1)) { 70 *p++ = ' '; 71 } 72 } 73 } 74 LOGD("%s", buffer); 75} 76 77CommandListener::DumpCmd::DumpCmd() : 78 VoldCommand("dump") { 79} 80 81int CommandListener::DumpCmd::runCommand(SocketClient *cli, 82 int argc, char **argv) { 83 cli->sendMsg(0, "Dumping loop status", false); 84 if (Loop::dumpState(cli)) { 85 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true); 86 } 87 cli->sendMsg(0, "Dumping DM status", false); 88 if (Devmapper::dumpState(cli)) { 89 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true); 90 } 91 92 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false); 93 return 0; 94} 95 96 97CommandListener::VolumeCmd::VolumeCmd() : 98 VoldCommand("volume") { 99} 100 101int CommandListener::VolumeCmd::runCommand(SocketClient *cli, 102 int argc, char **argv) { 103 dumpArgs(argc, argv, -1); 104 105 if (argc < 2) { 106 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 107 return 0; 108 } 109 110 VolumeManager *vm = VolumeManager::Instance(); 111 int rc = 0; 112 113 if (!strcmp(argv[1], "list")) { 114 return vm->listVolumes(cli); 115 } else if (!strcmp(argv[1], "debug")) { 116 if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) { 117 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false); 118 return 0; 119 } 120 vm->setDebug(!strcmp(argv[2], "on") ? true : false); 121 } else if (!strcmp(argv[1], "mount")) { 122 if (argc != 3) { 123 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false); 124 return 0; 125 } 126 rc = vm->mountVolume(argv[2]); 127 } else if (!strcmp(argv[1], "unmount")) { 128 if (argc < 3 || argc > 4 || (argc == 4 && strcmp(argv[3], "force"))) { 129 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force]", false); 130 return 0; 131 } 132 133 bool force = false; 134 if (argc >= 4 && !strcmp(argv[3], "force")) { 135 force = true; 136 } 137 rc = vm->unmountVolume(argv[2], force); 138 } else if (!strcmp(argv[1], "format")) { 139 if (argc != 3) { 140 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path>", false); 141 return 0; 142 } 143 rc = vm->formatVolume(argv[2]); 144 } else if (!strcmp(argv[1], "share")) { 145 if (argc != 4) { 146 cli->sendMsg(ResponseCode::CommandSyntaxError, 147 "Usage: volume share <path> <method>", false); 148 return 0; 149 } 150 rc = vm->shareVolume(argv[2], argv[3]); 151 } else if (!strcmp(argv[1], "unshare")) { 152 if (argc != 4) { 153 cli->sendMsg(ResponseCode::CommandSyntaxError, 154 "Usage: volume unshare <path> <method>", false); 155 return 0; 156 } 157 rc = vm->unshareVolume(argv[2], argv[3]); 158 } else if (!strcmp(argv[1], "shared")) { 159 bool enabled = false; 160 if (argc != 4) { 161 cli->sendMsg(ResponseCode::CommandSyntaxError, 162 "Usage: volume shared <path> <method>", false); 163 return 0; 164 } 165 166 if (vm->shareEnabled(argv[2], argv[3], &enabled)) { 167 cli->sendMsg( 168 ResponseCode::OperationFailed, "Failed to determine share enable state", true); 169 } else { 170 cli->sendMsg(ResponseCode::ShareEnabledResult, 171 (enabled ? "Share enabled" : "Share disabled"), false); 172 } 173 return 0; 174 } else { 175 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false); 176 } 177 178 if (!rc) { 179 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false); 180 } else { 181 int erno = errno; 182 rc = ResponseCode::convertFromErrno(); 183 cli->sendMsg(rc, "volume operation failed", true); 184 } 185 186 return 0; 187} 188 189CommandListener::ShareCmd::ShareCmd() : 190 VoldCommand("share") { 191} 192 193int CommandListener::ShareCmd::runCommand(SocketClient *cli, 194 int argc, char **argv) { 195 dumpArgs(argc, argv, -1); 196 197 if (argc < 2) { 198 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 199 return 0; 200 } 201 202 VolumeManager *vm = VolumeManager::Instance(); 203 int rc = 0; 204 205 if (!strcmp(argv[1], "status")) { 206 bool avail = false; 207 208 if (vm->shareAvailable(argv[2], &avail)) { 209 cli->sendMsg( 210 ResponseCode::OperationFailed, "Failed to determine share availability", true); 211 } else { 212 cli->sendMsg(ResponseCode::ShareStatusResult, 213 (avail ? "Share available" : "Share unavailable"), false); 214 } 215 } else { 216 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown share cmd", false); 217 } 218 219 return 0; 220} 221 222CommandListener::StorageCmd::StorageCmd() : 223 VoldCommand("storage") { 224} 225 226int CommandListener::StorageCmd::runCommand(SocketClient *cli, 227 int argc, char **argv) { 228 dumpArgs(argc, argv, -1); 229 230 if (argc < 2) { 231 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 232 return 0; 233 } 234 235 if (!strcmp(argv[1], "users")) { 236 DIR *dir; 237 struct dirent *de; 238 239 if (!(dir = opendir("/proc"))) { 240 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true); 241 return 0; 242 } 243 244 while ((de = readdir(dir))) { 245 int pid = Process::getPid(de->d_name); 246 247 if (pid < 0) { 248 continue; 249 } 250 251 char processName[255]; 252 Process::getProcessName(pid, processName, sizeof(processName)); 253 254 if (Process::checkFileDescriptorSymLinks(pid, argv[2]) || 255 Process::checkFileMaps(pid, argv[2]) || 256 Process::checkSymLink(pid, argv[2], "cwd") || 257 Process::checkSymLink(pid, argv[2], "root") || 258 Process::checkSymLink(pid, argv[2], "exe")) { 259 260 char msg[1024]; 261 snprintf(msg, sizeof(msg), "%d %s", pid, processName); 262 cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false); 263 } 264 } 265 closedir(dir); 266 cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false); 267 } else { 268 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); 269 } 270 return 0; 271} 272 273CommandListener::AsecCmd::AsecCmd() : 274 VoldCommand("asec") { 275} 276 277int CommandListener::AsecCmd::runCommand(SocketClient *cli, 278 int argc, char **argv) { 279 if (argc < 2) { 280 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 281 return 0; 282 } 283 284 VolumeManager *vm = VolumeManager::Instance(); 285 int rc = 0; 286 287 if (!strcmp(argv[1], "list")) { 288 dumpArgs(argc, argv, -1); 289 DIR *d = opendir(Volume::SEC_ASECDIR); 290 291 if (!d) { 292 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true); 293 return 0; 294 } 295 296 struct dirent *dent; 297 while ((dent = readdir(d))) { 298 if (dent->d_name[0] == '.') 299 continue; 300 if (!strcmp(&dent->d_name[strlen(dent->d_name)-5], ".asec")) { 301 char id[255]; 302 memset(id, 0, sizeof(id)); 303 strncpy(id, dent->d_name, strlen(dent->d_name) -5); 304 cli->sendMsg(ResponseCode::AsecListResult, id, false); 305 } 306 } 307 closedir(d); 308 } else if (!strcmp(argv[1], "create")) { 309 dumpArgs(argc, argv, 5); 310 if (argc != 7) { 311 cli->sendMsg(ResponseCode::CommandSyntaxError, 312 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid>", false); 313 return 0; 314 } 315 316 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 317 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6])); 318 } else if (!strcmp(argv[1], "finalize")) { 319 dumpArgs(argc, argv, -1); 320 if (argc != 3) { 321 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); 322 return 0; 323 } 324 rc = vm->finalizeAsec(argv[2]); 325 } else if (!strcmp(argv[1], "destroy")) { 326 dumpArgs(argc, argv, -1); 327 if (argc < 3) { 328 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false); 329 return 0; 330 } 331 bool force = false; 332 if (argc > 3 && !strcmp(argv[3], "force")) { 333 force = true; 334 } 335 rc = vm->destroyAsec(argv[2], force); 336 } else if (!strcmp(argv[1], "mount")) { 337 dumpArgs(argc, argv, 3); 338 if (argc != 5) { 339 cli->sendMsg(ResponseCode::CommandSyntaxError, 340 "Usage: asec mount <namespace-id> <key> <ownerUid>", false); 341 return 0; 342 } 343 rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4])); 344 } else if (!strcmp(argv[1], "unmount")) { 345 dumpArgs(argc, argv, -1); 346 if (argc < 3) { 347 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); 348 return 0; 349 } 350 bool force = false; 351 if (argc > 3 && !strcmp(argv[3], "force")) { 352 force = true; 353 } 354 rc = vm->unmountAsec(argv[2], force); 355 } else if (!strcmp(argv[1], "rename")) { 356 dumpArgs(argc, argv, -1); 357 if (argc != 4) { 358 cli->sendMsg(ResponseCode::CommandSyntaxError, 359 "Usage: asec rename <old_id> <new_id>", false); 360 return 0; 361 } 362 rc = vm->renameAsec(argv[2], argv[3]); 363 } else if (!strcmp(argv[1], "path")) { 364 dumpArgs(argc, argv, -1); 365 if (argc != 3) { 366 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); 367 return 0; 368 } 369 char path[255]; 370 371 if (vm->getAsecMountPath(argv[2], path, sizeof(path))) { 372 cli->sendMsg(ResponseCode::OperationFailed, "Failed to get path", true); 373 } else { 374 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 375 } 376 return 0; 377 } else { 378 dumpArgs(argc, argv, -1); 379 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); 380 } 381 382 if (!rc) { 383 cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false); 384 } else { 385 rc = ResponseCode::convertFromErrno(); 386 cli->sendMsg(rc, "asec operation failed", true); 387 } 388 389 return 0; 390} 391 392CommandListener::XwarpCmd::XwarpCmd() : 393 VoldCommand("xwarp") { 394} 395 396int CommandListener::XwarpCmd::runCommand(SocketClient *cli, 397 int argc, char **argv) { 398 if (argc < 2) { 399 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 400 return 0; 401 } 402 403 if (!strcmp(argv[1], "enable")) { 404 if (Xwarp::enable()) { 405 cli->sendMsg(ResponseCode::OperationFailed, "Failed to enable xwarp", true); 406 return 0; 407 } 408 409 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp mirroring started", false); 410 } else if (!strcmp(argv[1], "disable")) { 411 if (Xwarp::disable()) { 412 cli->sendMsg(ResponseCode::OperationFailed, "Failed to disable xwarp", true); 413 return 0; 414 } 415 416 cli->sendMsg(ResponseCode::CommandOkay, "Xwarp disabled", false); 417 } else if (!strcmp(argv[1], "status")) { 418 char msg[255]; 419 bool r; 420 unsigned mirrorPos, maxSize; 421 422 if (Xwarp::status(&r, &mirrorPos, &maxSize)) { 423 cli->sendMsg(ResponseCode::OperationFailed, "Failed to get xwarp status", true); 424 return 0; 425 } 426 snprintf(msg, sizeof(msg), "%s %u %u", (r ? "ready" : "not-ready"), mirrorPos, maxSize); 427 cli->sendMsg(ResponseCode::XwarpStatusResult, msg, false); 428 } else { 429 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); 430 } 431 432 return 0; 433} 434 435