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 "CommandListener.h"
18
19 #include <arpa/inet.h>
20 #include <ctype.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <math.h>
25 #include <netinet/in.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/prctl.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31
32 #include <string>
33
34 #include <android-base/logging.h>
35 #include <android-base/parseint.h>
36 #include <android-base/stringprintf.h>
37 #include <cutils/sockets.h>
38 #include <log/log_properties.h>
39 #include <private/android_filesystem_config.h>
40 #include <sysutils/SocketClient.h>
41
42 #include "LogPermissions.h"
43
CommandListener(LogBuffer * buf,LogTags * tags,PruneList * prune,LogStatistics * stats)44 CommandListener::CommandListener(LogBuffer* buf, LogTags* tags, PruneList* prune,
45 LogStatistics* stats)
46 : FrameworkListener(getLogSocket()), buf_(buf), tags_(tags), prune_(prune), stats_(stats) {
47 registerCmd(new ClearCmd(this));
48 registerCmd(new GetBufSizeCmd(this));
49 registerCmd(new SetBufSizeCmd(this));
50 registerCmd(new GetBufSizeReadableCmd(this));
51 registerCmd(new GetBufSizeUsedCmd(this));
52 registerCmd(new GetStatisticsCmd(this));
53 registerCmd(new SetPruneListCmd(this));
54 registerCmd(new GetPruneListCmd(this));
55 registerCmd(new GetEventTagCmd(this));
56 registerCmd(new ReinitCmd(this));
57 registerCmd(new ExitCmd(this));
58 }
59
setname()60 static void setname() {
61 static bool name_set;
62 if (!name_set) {
63 prctl(PR_SET_NAME, "logd.control");
64 name_set = true;
65 }
66 }
67
68 template <typename F>
LogIdCommand(SocketClient * cli,int argc,char ** argv,F && function)69 static int LogIdCommand(SocketClient* cli, int argc, char** argv, F&& function) {
70 setname();
71 if (argc < 2) {
72 cli->sendMsg("Missing Argument");
73 return 0;
74 }
75
76 int log_id;
77 if (!android::base::ParseInt(argv[1], &log_id, static_cast<int>(LOG_ID_MAIN),
78 static_cast<int>(LOG_ID_KERNEL))) {
79 cli->sendMsg("Range Error");
80 return 0;
81 }
82
83 function(static_cast<log_id_t>(log_id));
84 return 0;
85 }
86
runCommand(SocketClient * cli,int argc,char ** argv)87 int CommandListener::ClearCmd::runCommand(SocketClient* cli, int argc, char** argv) {
88 uid_t uid = cli->getUid();
89 if (clientHasLogCredentials(cli)) {
90 uid = AID_ROOT;
91 }
92
93 return LogIdCommand(cli, argc, argv, [&](log_id_t id) {
94 cli->sendMsg(buf()->Clear(id, uid) ? "success" : "busy");
95 });
96 }
97
98 template <typename F>
LogSizeCommand(SocketClient * cli,int argc,char ** argv,F && size_function)99 static int LogSizeCommand(SocketClient* cli, int argc, char** argv, F&& size_function) {
100 return LogIdCommand(cli, argc, argv, [&](log_id_t log_id) {
101 cli->sendMsg(std::to_string(size_function(log_id)).c_str());
102 });
103 }
104
runCommand(SocketClient * cli,int argc,char ** argv)105 int CommandListener::GetBufSizeCmd::runCommand(SocketClient* cli, int argc, char** argv) {
106 return LogSizeCommand(cli, argc, argv, [this](log_id_t id) { return buf()->GetSize(id); });
107 }
108
runCommand(SocketClient * cli,int argc,char ** argv)109 int CommandListener::GetBufSizeReadableCmd::runCommand(SocketClient* cli, int argc, char** argv) {
110 return LogSizeCommand(cli, argc, argv,
111 [this](log_id_t id) { return stats()->SizeReadable(id); });
112 }
113
runCommand(SocketClient * cli,int argc,char ** argv)114 int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient* cli, int argc, char** argv) {
115 return LogSizeCommand(cli, argc, argv, [this](log_id_t id) { return stats()->Sizes(id); });
116 }
117
runCommand(SocketClient * cli,int argc,char ** argv)118 int CommandListener::SetBufSizeCmd::runCommand(SocketClient* cli, int argc,
119 char** argv) {
120 if (!clientHasLogCredentials(cli)) {
121 cli->sendMsg("Permission Denied");
122 return 0;
123 }
124
125 if (argc < 3) {
126 cli->sendMsg("Missing Argument");
127 return 0;
128 }
129 size_t size = atol(argv[2]);
130
131 return LogIdCommand(cli, argc, argv, [&](log_id_t log_id) {
132 cli->sendMsg(buf()->SetSize(log_id, size) ? "success" : "busy");
133 });
134 }
135
136 // This returns a string with a length prefix with the format <length>\n<data>\n\f. The length
137 // prefix includes the length of the prefix itself.
PackageString(const std::string & str)138 static std::string PackageString(const std::string& str) {
139 size_t overhead_length = 3; // \n \n \f.
140
141 // Number of digits needed to represent length(str + overhead_length).
142 size_t str_size_digits = 1 + static_cast<size_t>(log10(str.size() + overhead_length));
143 // Number of digits needed to represent the total size.
144 size_t total_size_digits =
145 1 + static_cast<size_t>(log10(str.size() + overhead_length + str_size_digits));
146
147 // If adding the size prefix causes a new digit to be required to represent the new total
148 // size, add it to the 'overhead_length'. This can only happen once, since each new digit
149 // allows for 10x the previous size to be recorded.
150 if (total_size_digits != str_size_digits) {
151 overhead_length++;
152 }
153
154 size_t total_size = str.size() + overhead_length + str_size_digits;
155 return android::base::StringPrintf("%zu\n%s\n\f", total_size, str.c_str());
156 }
157
runCommand(SocketClient * cli,int argc,char ** argv)158 int CommandListener::GetStatisticsCmd::runCommand(SocketClient* cli, int argc, char** argv) {
159 setname();
160 uid_t uid = cli->getUid();
161 if (clientHasLogCredentials(cli)) {
162 uid = AID_ROOT;
163 }
164
165 unsigned int logMask = -1;
166 pid_t pid = 0;
167 if (argc > 1) {
168 logMask = 0;
169 for (int i = 1; i < argc; ++i) {
170 static const char _pid[] = "pid=";
171 if (!strncmp(argv[i], _pid, sizeof(_pid) - 1)) {
172 pid = atol(argv[i] + sizeof(_pid) - 1);
173 if (pid == 0) {
174 cli->sendMsg("PID Error");
175 return 0;
176 }
177 continue;
178 }
179
180 int id = atoi(argv[i]);
181 if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) {
182 cli->sendMsg("Range Error");
183 return 0;
184 }
185 logMask |= 1 << id;
186 }
187 }
188
189 cli->sendMsg(PackageString(stats()->Format(uid, pid, logMask)).c_str());
190 return 0;
191 }
192
runCommand(SocketClient * cli,int,char **)193 int CommandListener::GetPruneListCmd::runCommand(SocketClient* cli, int, char**) {
194 setname();
195 cli->sendMsg(PackageString(prune()->Format()).c_str());
196 return 0;
197 }
198
runCommand(SocketClient * cli,int argc,char ** argv)199 int CommandListener::SetPruneListCmd::runCommand(SocketClient* cli, int argc, char** argv) {
200 setname();
201 if (!clientHasLogCredentials(cli)) {
202 cli->sendMsg("Permission Denied");
203 return 0;
204 }
205
206 std::string str;
207 for (int i = 1; i < argc; ++i) {
208 if (str.length()) {
209 str += " ";
210 }
211 str += argv[i];
212 }
213
214 if (!prune()->Init(str.c_str())) {
215 cli->sendMsg("Invalid");
216 return 0;
217 }
218
219 cli->sendMsg("success");
220 return 0;
221 }
222
runCommand(SocketClient * cli,int argc,char ** argv)223 int CommandListener::GetEventTagCmd::runCommand(SocketClient* cli, int argc, char** argv) {
224 setname();
225 uid_t uid = cli->getUid();
226 if (clientHasLogCredentials(cli)) {
227 uid = AID_ROOT;
228 }
229
230 const char* name = nullptr;
231 const char* format = nullptr;
232 const char* id = nullptr;
233 for (int i = 1; i < argc; ++i) {
234 static const char _name[] = "name=";
235 if (!strncmp(argv[i], _name, strlen(_name))) {
236 name = argv[i] + strlen(_name);
237 continue;
238 }
239
240 static const char _format[] = "format=";
241 if (!strncmp(argv[i], _format, strlen(_format))) {
242 format = argv[i] + strlen(_format);
243 continue;
244 }
245
246 static const char _id[] = "id=";
247 if (!strncmp(argv[i], _id, strlen(_id))) {
248 id = argv[i] + strlen(_id);
249 continue;
250 }
251 }
252
253 if (id) {
254 if (format || name) {
255 cli->sendMsg("can not mix id= with either format= or name=");
256 return 0;
257 }
258 cli->sendMsg(PackageString(tags()->formatEntry(atoi(id), uid)).c_str());
259 return 0;
260 }
261
262 cli->sendMsg(PackageString(tags()->formatGetEventTag(uid, name, format)).c_str());
263
264 return 0;
265 }
266
runCommand(SocketClient * cli,int,char **)267 int CommandListener::ReinitCmd::runCommand(SocketClient* cli, int, char**) {
268 setname();
269
270 LOG(INFO) << "logd reinit";
271 buf()->Init();
272 prune()->Init(nullptr);
273
274 // This only works on userdebug and eng devices to re-read the
275 // /data/misc/logd/event-log-tags file right after /data is mounted.
276 // The operation is near to boot and should only happen once. There
277 // are races associated with its use since it can trigger a Rebuild
278 // of the file, but that is a can-not-happen since the file was not
279 // read yet. More dangerous if called later, but if all is well it
280 // should just skip over everything and not write any new entries.
281 if (__android_log_is_debuggable()) {
282 tags()->ReadFileEventLogTags(tags()->debug_event_log_tags);
283 }
284
285 cli->sendMsg("success");
286
287 return 0;
288 }
289
runCommand(SocketClient * cli,int,char **)290 int CommandListener::ExitCmd::runCommand(SocketClient* cli, int, char**) {
291 setname();
292
293 cli->sendMsg("success");
294 parent_->release(cli);
295
296 return 0;
297 }
298
getLogSocket()299 int CommandListener::getLogSocket() {
300 static const char socketName[] = "logd";
301 int sock = android_get_control_socket(socketName);
302
303 if (sock < 0) {
304 sock = socket_local_server(
305 socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
306 }
307
308 return sock;
309 }
310