CommandListener.cpp revision a2677e4ad01f250b0765f04adf0acfa6627efc98
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 <netinet/in.h> 20#include <arpa/inet.h> 21#include <errno.h> 22 23#define LOG_TAG "CommandListener" 24#include <cutils/log.h> 25 26#include <sysutils/SocketClient.h> 27 28#include "CommandListener.h" 29#include "VolumeManager.h" 30#include "ResponseCode.h" 31 32CommandListener::CommandListener() : 33 FrameworkListener("vold") { 34 registerCmd(new ListVolumesCmd()); 35 registerCmd(new MountCmd()); 36 registerCmd(new UnmountCmd()); 37 registerCmd(new ShareCmd()); 38 registerCmd(new UnshareCmd()); 39 registerCmd(new ShareAvailableCmd()); 40 registerCmd(new SimulateCmd()); 41 registerCmd(new FormatCmd()); 42} 43 44CommandListener::ListVolumesCmd::ListVolumesCmd() : 45 VoldCommand("list_volumes") { 46} 47 48int CommandListener::ListVolumesCmd::runCommand(SocketClient *cli, 49 int argc, char **argv) { 50 return VolumeManager::Instance()->listVolumes(cli); 51} 52 53CommandListener::MountCmd::MountCmd() : 54 VoldCommand("mount") { 55} 56 57int CommandListener::MountCmd::runCommand(SocketClient *cli, 58 int argc, char **argv) { 59 /* Synchronously mount a volume */ 60 if (VolumeManager::Instance()->mountVolume(argv[1])) { 61 cli->sendMsg(ResponseCode::OperationFailed, "Failed to mount volume.", true); 62 } else { 63 cli->sendMsg(ResponseCode::CommandOkay, "Volume mounted.", false); 64 } 65 66 return 0; 67} 68 69CommandListener::UnmountCmd::UnmountCmd() : 70 VoldCommand("unmount") { 71} 72 73int CommandListener::UnmountCmd::runCommand(SocketClient *cli, 74 int argc, char **argv) { 75 /* Synchronously unmount a volume */ 76 if (VolumeManager::Instance()->unmountVolume(argv[1])) { 77 cli->sendMsg(ResponseCode::OperationFailed, "Failed to unmount volume.", true); 78 } else { 79 cli->sendMsg(ResponseCode::CommandOkay, "Volume unmounted.", false); 80 } 81 82 return 0; 83} 84 85CommandListener::ShareCmd::ShareCmd() : 86 VoldCommand("share") { 87} 88 89int CommandListener::ShareCmd::runCommand(SocketClient *cli, 90 int argc, char **argv) { 91 if (VolumeManager::Instance()->shareVolume(argv[1], argv[2])) { 92 cli->sendMsg(ResponseCode::OperationFailed, "Failed to share volume.", true); 93 } else { 94 cli->sendMsg(ResponseCode::CommandOkay, "Volume shared.", false); 95 } 96 97 return 0; 98} 99 100CommandListener::UnshareCmd::UnshareCmd() : 101 VoldCommand("unshare") { 102} 103 104int CommandListener::UnshareCmd::runCommand(SocketClient *cli, 105 int argc, char **argv) { 106 if (VolumeManager::Instance()->unshareVolume(argv[1], argv[2])) { 107 cli->sendMsg(ResponseCode::OperationFailed, "Failed to unshare volume.", true); 108 } else { 109 cli->sendMsg(ResponseCode::CommandOkay, "Volume unshared.", false); 110 } 111 112 return 0; 113} 114 115CommandListener::ShareAvailableCmd::ShareAvailableCmd() : 116 VoldCommand("share_available") { 117} 118 119int CommandListener::ShareAvailableCmd::runCommand(SocketClient *cli, 120 int argc, char **argv) { 121 bool avail = false; 122 123 if (VolumeManager::Instance()->shareAvailable(argv[1], &avail)) { 124 cli->sendMsg(ResponseCode::OperationFailed, 125 "Failed to determine share availability", true); 126 } else { 127 cli->sendMsg(ResponseCode::ShareAvailabilityResult, 128 (avail ? "Share available" : "Share unavailable"), 129 false); 130 } 131 return 0; 132} 133 134CommandListener::SimulateCmd::SimulateCmd() : 135 VoldCommand("simulate") { 136} 137 138int CommandListener::SimulateCmd::runCommand(SocketClient *cli, 139 int argc, char **argv) { 140 if (VolumeManager::Instance()->simulate(argv[1], argv[2])) { 141 cli->sendMsg(ResponseCode::OperationFailed, "Failed to execute.", true); 142 } else { 143 cli->sendMsg(ResponseCode::CommandOkay, "Simulation executed.", false); 144 } 145 146 return 0; 147} 148 149CommandListener::FormatCmd::FormatCmd() : 150 VoldCommand("format") { 151} 152 153int CommandListener::FormatCmd::runCommand(SocketClient *cli, 154 int argc, char **argv) { 155 if (VolumeManager::Instance()->formatVolume(argv[1])) { 156 cli->sendMsg(ResponseCode::OperationFailed, "Failed to format", true); 157 } else { 158 cli->sendMsg(ResponseCode::CommandOkay, "Volume formatted.", false); 159 } 160 161 return 0; 162} 163