CommandListener.cpp revision 49e2bce5b74129c26a35e25d4693cbfe98c4688e
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 "ErrorCode.h"
31
32CommandListener::CommandListener() :
33                 FrameworkListener("vold") {
34    registerCmd(new ListVolumesCmd());
35    registerCmd(new MountVolumeCmd());
36    registerCmd(new UnmountVolumeCmd());
37    registerCmd(new ShareVolumeCmd());
38    registerCmd(new UnshareVolumeCmd());
39}
40
41CommandListener::ListVolumesCmd::ListVolumesCmd() :
42                 VoldCommand("list_volumes") {
43}
44
45int CommandListener::ListVolumesCmd::runCommand(SocketClient *cli,
46                                                      int argc, char **argv) {
47    return VolumeManager::Instance()->listVolumes(cli);
48}
49
50CommandListener::MountVolumeCmd::MountVolumeCmd() :
51                 VoldCommand("mount_volume") {
52}
53
54int CommandListener::MountVolumeCmd::runCommand(SocketClient *cli,
55                                                      int argc, char **argv) {
56    /* Synchronously mount a volume */
57    if (VolumeManager::Instance()->mountVolume(argv[1])) {
58        cli->sendMsg(ErrorCode::OperationFailed, "Failed to mount volume.", true);
59    } else {
60        cli->sendMsg(ErrorCode::CommandOkay, "Volume mounted.", false);
61    }
62
63    return 0;
64}
65
66CommandListener::UnmountVolumeCmd::UnmountVolumeCmd() :
67                 VoldCommand("unmount_volume") {
68}
69
70int CommandListener::UnmountVolumeCmd::runCommand(SocketClient *cli,
71                                                      int argc, char **argv) {
72    /* Synchronously unmount a volume */
73    if (VolumeManager::Instance()->mountVolume(argv[1])) {
74        cli->sendMsg(ErrorCode::OperationFailed, "Failed to unmount volume.", true);
75    } else {
76        cli->sendMsg(ErrorCode::CommandOkay, "Volume unmounted.", false);
77    }
78
79    return 0;
80}
81
82CommandListener::ShareVolumeCmd::ShareVolumeCmd() :
83                 VoldCommand("share_volume") {
84}
85
86int CommandListener::ShareVolumeCmd::runCommand(SocketClient *cli,
87                                                      int argc, char **argv) {
88    VolumeManager *nm = VolumeManager::Instance();
89    errno = ENOSYS;
90    cli->sendMsg(ErrorCode::OperationFailed, "Failed to share volume", true);
91    return 0;
92}
93
94CommandListener::UnshareVolumeCmd::UnshareVolumeCmd() :
95                 VoldCommand("unshare_volume") {
96}
97
98int CommandListener::UnshareVolumeCmd::runCommand(SocketClient *cli,
99                                                      int argc, char **argv) {
100    VolumeManager *nm = VolumeManager::Instance();
101    errno = ENOSYS;
102    cli->sendMsg(ErrorCode::OperationFailed, "Failed to unshare volume", true);
103    return 0;
104}
105