CommandListener.cpp revision 0175b0747a1f55329109e84c9a1322dcb95e2848
1/*
2 * Copyright (C) 2012-2013 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 <sysutils/SocketClient.h>
28#include <private/android_filesystem_config.h>
29
30#include "CommandListener.h"
31
32CommandListener::CommandListener(LogBuffer *buf, LogReader * /*reader*/,
33                                 LogListener * /*swl*/)
34        : FrameworkListener("logd")
35        , mBuf(*buf) {
36    // registerCmd(new ShutdownCmd(buf, writer, swl));
37    registerCmd(new ClearCmd(buf));
38    registerCmd(new GetBufSizeCmd(buf));
39    registerCmd(new GetBufSizeUsedCmd(buf));
40}
41
42CommandListener::ShutdownCmd::ShutdownCmd(LogBuffer *buf, LogReader *reader,
43                                          LogListener *swl)
44        : LogCommand("shutdown")
45        , mBuf(*buf)
46        , mReader(*reader)
47        , mSwl(*swl)
48{ }
49
50int CommandListener::ShutdownCmd::runCommand(SocketClient * /*cli*/,
51                                             int /*argc*/,
52                                             char ** /*argv*/) {
53    mSwl.stopListener();
54    mReader.stopListener();
55    exit(0);
56}
57
58CommandListener::ClearCmd::ClearCmd(LogBuffer *buf)
59        : LogCommand("clear")
60        , mBuf(*buf)
61{ }
62
63int CommandListener::ClearCmd::runCommand(SocketClient *cli,
64                                         int argc, char **argv) {
65    if ((cli->getUid() != AID_ROOT)
66            && (cli->getGid() != AID_ROOT)
67            && (cli->getGid() != AID_LOG)) {
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) || (id >= LOG_ID_MAX)) {
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
88
89CommandListener::GetBufSizeCmd::GetBufSizeCmd(LogBuffer *buf)
90        : LogCommand("getLogSize")
91        , mBuf(*buf)
92{ }
93
94int CommandListener::GetBufSizeCmd::runCommand(SocketClient *cli,
95                                         int argc, char **argv) {
96    if (argc < 2) {
97        cli->sendMsg("Missing Argument");
98        return 0;
99    }
100
101    int id = atoi(argv[1]);
102    if ((id < LOG_ID_MIN) || (id >= LOG_ID_MAX)) {
103        cli->sendMsg("Range Error");
104        return 0;
105    }
106
107    unsigned long size = mBuf.getSize((log_id_t) id);
108    char buf[512];
109    snprintf(buf, sizeof(buf), "%lu", size);
110    cli->sendMsg(buf);
111    return 0;
112}
113
114CommandListener::GetBufSizeUsedCmd::GetBufSizeUsedCmd(LogBuffer *buf)
115        : LogCommand("getLogSizeUsed")
116        , mBuf(*buf)
117{ }
118
119int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient *cli,
120                                         int argc, char **argv) {
121    if (argc < 2) {
122        cli->sendMsg("Missing Argument");
123        return 0;
124    }
125
126    int id = atoi(argv[1]);
127    if ((id < LOG_ID_MIN) || (id >= LOG_ID_MAX)) {
128        cli->sendMsg("Range Error");
129        return 0;
130    }
131
132    unsigned long size = mBuf.getSizeUsed((log_id_t) id);
133    char buf[512];
134    snprintf(buf, sizeof(buf), "%lu", size);
135    cli->sendMsg(buf);
136    return 0;
137}
138