CommandListener.cpp revision 51a29c8dc445e4fb89860561933e54a231e6ffb4
1/* 2 * Copyright (C) 2012-2014 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 <arpa/inet.h> 18#include <dirent.h> 19#include <errno.h> 20#include <fcntl.h> 21#include <netinet/in.h> 22#include <string.h> 23#include <stdlib.h> 24#include <sys/socket.h> 25#include <sys/types.h> 26 27#include <private/android_filesystem_config.h> 28#include <sysutils/SocketClient.h> 29 30#include "CommandListener.h" 31#include "LogCommand.h" 32 33CommandListener::CommandListener(LogBuffer *buf, LogReader * /*reader*/, 34 LogListener * /*swl*/) 35 : FrameworkListener("logd") 36 , mBuf(*buf) { 37 // registerCmd(new ShutdownCmd(buf, writer, swl)); 38 registerCmd(new ClearCmd(buf)); 39 registerCmd(new GetBufSizeCmd(buf)); 40 registerCmd(new GetBufSizeUsedCmd(buf)); 41 registerCmd(new GetStatisticsCmd(buf)); 42} 43 44CommandListener::ShutdownCmd::ShutdownCmd(LogBuffer *buf, LogReader *reader, 45 LogListener *swl) 46 : LogCommand("shutdown") 47 , mBuf(*buf) 48 , mReader(*reader) 49 , mSwl(*swl) 50{ } 51 52int CommandListener::ShutdownCmd::runCommand(SocketClient * /*cli*/, 53 int /*argc*/, 54 char ** /*argv*/) { 55 mSwl.stopListener(); 56 mReader.stopListener(); 57 exit(0); 58} 59 60CommandListener::ClearCmd::ClearCmd(LogBuffer *buf) 61 : LogCommand("clear") 62 , mBuf(*buf) 63{ } 64 65int CommandListener::ClearCmd::runCommand(SocketClient *cli, 66 int argc, char **argv) { 67 if (!clientHasLogCredentials(cli)) { 68 cli->sendMsg("Permission Denied"); 69 return 0; 70 } 71 72 if (argc < 2) { 73 cli->sendMsg("Missing Argument"); 74 return 0; 75 } 76 77 int id = atoi(argv[1]); 78 if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) { 79 cli->sendMsg("Range Error"); 80 return 0; 81 } 82 83 mBuf.clear((log_id_t) id); 84 cli->sendMsg("success"); 85 return 0; 86} 87 88CommandListener::GetBufSizeCmd::GetBufSizeCmd(LogBuffer *buf) 89 : LogCommand("getLogSize") 90 , mBuf(*buf) 91{ } 92 93int CommandListener::GetBufSizeCmd::runCommand(SocketClient *cli, 94 int argc, char **argv) { 95 if (argc < 2) { 96 cli->sendMsg("Missing Argument"); 97 return 0; 98 } 99 100 int id = atoi(argv[1]); 101 if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) { 102 cli->sendMsg("Range Error"); 103 return 0; 104 } 105 106 unsigned long size = mBuf.getSize((log_id_t) id); 107 char buf[512]; 108 snprintf(buf, sizeof(buf), "%lu", size); 109 cli->sendMsg(buf); 110 return 0; 111} 112 113CommandListener::GetBufSizeUsedCmd::GetBufSizeUsedCmd(LogBuffer *buf) 114 : LogCommand("getLogSizeUsed") 115 , mBuf(*buf) 116{ } 117 118int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient *cli, 119 int argc, char **argv) { 120 if (argc < 2) { 121 cli->sendMsg("Missing Argument"); 122 return 0; 123 } 124 125 int id = atoi(argv[1]); 126 if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) { 127 cli->sendMsg("Range Error"); 128 return 0; 129 } 130 131 unsigned long size = mBuf.getSizeUsed((log_id_t) id); 132 char buf[512]; 133 snprintf(buf, sizeof(buf), "%lu", size); 134 cli->sendMsg(buf); 135 return 0; 136} 137 138CommandListener::GetStatisticsCmd::GetStatisticsCmd(LogBuffer *buf) 139 : LogCommand("getStatistics") 140 , mBuf(*buf) 141{ } 142 143int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli, 144 int argc, char **argv) { 145 uid_t uid = cli->getUid(); 146 gid_t gid = cli->getGid(); 147 if (clientHasLogCredentials(cli)) { 148 uid = AID_ROOT; 149 } 150 151 unsigned int logMask = -1; 152 if (argc > 1) { 153 logMask = 0; 154 for (int i = 1; i < argc; ++i) { 155 int id = atoi(argv[i]); 156 if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) { 157 cli->sendMsg("Range Error"); 158 return 0; 159 } 160 logMask |= 1 << id; 161 } 162 } 163 164 char *buf = NULL; 165 166 mBuf.formatStatistics(&buf, uid, logMask); 167 if (!buf) { 168 cli->sendMsg("Failed"); 169 } else { 170 cli->sendMsg(buf); 171 free(buf); 172 } 173 return 0; 174} 175