1 /*
2 * Copyright (C) 2007-2016 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 "logd_reader.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <poll.h>
23 #include <stdarg.h>
24 #include <stdatomic.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/un.h>
33 #include <time.h>
34 #include <unistd.h>
35
36 #include <string>
37
38 #include <android-base/parseint.h>
39 #include <private/android_logger.h>
40
41 #include "logger.h"
42
43 // Socket timeout in seconds. Accounts for tail operation which can take longer.
44 #define LIBLOG_SOCK_TIMEOUT_S 5
45
46 // Connects to /dev/socket/<name> and returns the associated fd or returns -1 on error.
47 // O_CLOEXEC is always set.
socket_local_client(const std::string & name,int type,bool timeout)48 static int socket_local_client(const std::string& name, int type, bool timeout) {
49 sockaddr_un addr = {.sun_family = AF_LOCAL};
50
51 std::string path = "/dev/socket/" + name;
52 if (path.size() + 1 > sizeof(addr.sun_path)) {
53 return -1;
54 }
55 strlcpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path));
56
57 int fd = socket(AF_LOCAL, type | SOCK_CLOEXEC, 0);
58 if (fd == -1) {
59 return -1;
60 }
61
62 if (timeout) {
63 // Sending and receiving messages should be instantaneous, but we don't want to wait forever if
64 // logd is hung, so we set a timeout.
65 struct timeval t = {LIBLOG_SOCK_TIMEOUT_S, 0};
66 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(t)) == -1) {
67 return -1;
68 }
69 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t)) == -1) {
70 return -1;
71 }
72 }
73
74 if (connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) {
75 close(fd);
76 return -1;
77 }
78
79 return fd;
80 }
81
82 /* worker for sending the command to the logger */
SendLogdControlMessage(char * buf,size_t buf_size)83 ssize_t SendLogdControlMessage(char* buf, size_t buf_size) {
84 ssize_t ret;
85 size_t len;
86 char* cp;
87 int errno_save = 0;
88 int sock = socket_local_client("logd", SOCK_STREAM, true);
89 if (sock < 0) {
90 return sock;
91 }
92
93 len = strlen(buf) + 1;
94 ret = TEMP_FAILURE_RETRY(write(sock, buf, len));
95 if (ret <= 0) {
96 goto done;
97 }
98
99 len = buf_size;
100 cp = buf;
101 while ((ret = TEMP_FAILURE_RETRY(read(sock, cp, len))) > 0) {
102 if ((size_t)ret == len) {
103 break;
104 }
105
106 len -= ret;
107 cp += ret;
108
109 // Give other side 20ms to refill pipe.
110 struct pollfd p = {.fd = sock, .events = POLLIN};
111 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 20));
112
113 if (ret <= 0) {
114 break;
115 }
116
117 if (!(p.revents & POLLIN)) {
118 ret = 0;
119 break;
120 }
121 }
122
123 if (ret >= 0) {
124 ret += buf_size - len;
125 }
126
127 done:
128 if ((ret == -1) && errno) {
129 errno_save = errno;
130 }
131 close(sock);
132 if (errno_save) {
133 errno = errno_save;
134 }
135 return ret;
136 }
137
check_log_success(char * buf,ssize_t ret)138 static int check_log_success(char* buf, ssize_t ret) {
139 if (ret < 0) {
140 return ret;
141 }
142
143 if (strncmp(buf, "success", 7)) {
144 errno = EINVAL;
145 return -1;
146 }
147
148 return 0;
149 }
150
android_logger_clear(struct logger * logger)151 int android_logger_clear(struct logger* logger) {
152 if (!android_logger_is_logd(logger)) {
153 return -EINVAL;
154 }
155 uint32_t log_id = android_logger_get_id(logger);
156 char buf[512];
157 snprintf(buf, sizeof(buf), "clear %" PRIu32, log_id);
158
159 return check_log_success(buf, SendLogdControlMessage(buf, sizeof(buf)));
160 }
161
162 enum class LogSizeType : uint32_t {
163 kAllotted = 0,
164 kReadable,
165 kConsumed,
166 };
167
GetLogSize(struct logger * logger,LogSizeType type)168 static long GetLogSize(struct logger* logger, LogSizeType type) {
169 if (!android_logger_is_logd(logger)) {
170 return -EINVAL;
171 }
172
173 uint32_t log_id = android_logger_get_id(logger);
174 char buf[512];
175 switch (type) {
176 case LogSizeType::kAllotted:
177 snprintf(buf, sizeof(buf), "getLogSize %" PRIu32, log_id);
178 break;
179 case LogSizeType::kReadable:
180 snprintf(buf, sizeof(buf), "getLogSizeReadable %" PRIu32, log_id);
181 break;
182 case LogSizeType::kConsumed:
183 snprintf(buf, sizeof(buf), "getLogSizeUsed %" PRIu32, log_id);
184 break;
185 default:
186 abort();
187 }
188
189 ssize_t ret = SendLogdControlMessage(buf, sizeof(buf));
190 if (ret < 0) {
191 return ret;
192 }
193
194 long size;
195 if (!android::base::ParseInt(buf, &size)) {
196 return -1;
197 }
198
199 return size;
200 }
201
android_logger_get_log_size(struct logger * logger)202 long android_logger_get_log_size(struct logger* logger) {
203 return GetLogSize(logger, LogSizeType::kAllotted);
204 }
205
android_logger_get_log_readable_size(struct logger * logger)206 long android_logger_get_log_readable_size(struct logger* logger) {
207 return GetLogSize(logger, LogSizeType::kReadable);
208 }
209
android_logger_get_log_consumed_size(struct logger * logger)210 long android_logger_get_log_consumed_size(struct logger* logger) {
211 return GetLogSize(logger, LogSizeType::kConsumed);
212 }
213
android_logger_set_log_size(struct logger * logger,unsigned long size)214 int android_logger_set_log_size(struct logger* logger, unsigned long size) {
215 if (!android_logger_is_logd(logger)) {
216 return -EINVAL;
217 }
218
219 uint32_t log_id = android_logger_get_id(logger);
220 char buf[512];
221 snprintf(buf, sizeof(buf), "setLogSize %" PRIu32 " %lu", log_id, size);
222
223 return check_log_success(buf, SendLogdControlMessage(buf, sizeof(buf)));
224 }
225
android_logger_get_log_version(struct logger *)226 int android_logger_get_log_version(struct logger*) {
227 return 4;
228 }
229
android_logger_get_statistics(struct logger_list * logger_list,char * buf,size_t len)230 ssize_t android_logger_get_statistics(struct logger_list* logger_list, char* buf, size_t len) {
231 if (logger_list->mode & ANDROID_LOG_PSTORE) {
232 return -EINVAL;
233 }
234
235 char* cp = buf;
236 size_t remaining = len;
237 size_t n;
238
239 n = snprintf(cp, remaining, "getStatistics");
240 n = MIN(n, remaining);
241 remaining -= n;
242 cp += n;
243
244 for (size_t log_id = 0; log_id < LOG_ID_MAX; ++log_id) {
245 if ((1 << log_id) & logger_list->log_mask) {
246 n = snprintf(cp, remaining, " %zu", log_id);
247 n = MIN(n, remaining);
248 remaining -= n;
249 cp += n;
250 }
251 }
252
253 if (logger_list->pid) {
254 snprintf(cp, remaining, " pid=%u", logger_list->pid);
255 }
256
257 return SendLogdControlMessage(buf, len);
258 }
android_logger_get_prune_list(struct logger_list * logger_list,char * buf,size_t len)259 ssize_t android_logger_get_prune_list(struct logger_list* logger_list, char* buf, size_t len) {
260 if (logger_list->mode & ANDROID_LOG_PSTORE) {
261 return -EINVAL;
262 }
263
264 snprintf(buf, len, "getPruneList");
265 return SendLogdControlMessage(buf, len);
266 }
267
android_logger_set_prune_list(struct logger_list * logger_list,const char * buf,size_t len)268 int android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len) {
269 if (logger_list->mode & ANDROID_LOG_PSTORE) {
270 return -EINVAL;
271 }
272
273 std::string cmd = "setPruneList " + std::string{buf, len};
274
275 return check_log_success(cmd.data(), SendLogdControlMessage(cmd.data(), cmd.size()));
276 }
277
logdOpen(struct logger_list * logger_list)278 static int logdOpen(struct logger_list* logger_list) {
279 char buffer[256], *cp, c;
280 int ret, remaining, sock;
281 bool set_timeout;
282
283 sock = atomic_load(&logger_list->fd);
284 if (sock > 0) {
285 return sock;
286 }
287
288 /* Set timeouts for non-blocking mode but not if wrapping mode is also set.
289 * Wrapping mode intructs logd to wake the client when the log is about to
290 * wrap(get pruned) to minimize CPU usage so the socket should stay open for
291 * up to 2hours.*/
292 set_timeout = (logger_list->mode & ANDROID_LOG_NONBLOCK) &&
293 !(logger_list->mode & ANDROID_LOG_WRAP);
294
295 sock = socket_local_client("logdr", SOCK_SEQPACKET, set_timeout);
296
297 if (sock <= 0) {
298 if ((sock == -1) && errno) {
299 return -errno;
300 }
301 return sock;
302 }
303
304 strcpy(buffer, (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose" : "stream");
305 cp = buffer + strlen(buffer);
306
307 strcpy(cp, " lids");
308 cp += 5;
309 c = '=';
310 remaining = sizeof(buffer) - (cp - buffer);
311
312 for (size_t log_id = 0; log_id < LOG_ID_MAX; ++log_id) {
313 if ((1 << log_id) & logger_list->log_mask) {
314 ret = snprintf(cp, remaining, "%c%zu", c, log_id);
315 ret = MIN(ret, remaining);
316 remaining -= ret;
317 cp += ret;
318 c = ',';
319 }
320 }
321
322 if (logger_list->tail) {
323 ret = snprintf(cp, remaining, " tail=%u", logger_list->tail);
324 ret = MIN(ret, remaining);
325 remaining -= ret;
326 cp += ret;
327 }
328
329 if (logger_list->start.tv_sec || logger_list->start.tv_nsec) {
330 if (logger_list->mode & ANDROID_LOG_WRAP) {
331 // ToDo: alternate API to allow timeout to be adjusted.
332 ret = snprintf(cp, remaining, " timeout=%u", ANDROID_LOG_WRAP_DEFAULT_TIMEOUT);
333 ret = MIN(ret, remaining);
334 remaining -= ret;
335 cp += ret;
336 }
337 ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32, logger_list->start.tv_sec,
338 logger_list->start.tv_nsec);
339 ret = MIN(ret, remaining);
340 remaining -= ret;
341 cp += ret;
342 }
343
344 if (logger_list->pid) {
345 ret = snprintf(cp, remaining, " pid=%u", logger_list->pid);
346 ret = MIN(ret, remaining);
347 cp += ret;
348 }
349
350 ret = TEMP_FAILURE_RETRY(write(sock, buffer, cp - buffer));
351 int write_errno = errno;
352
353 if (ret <= 0) {
354 close(sock);
355 if (ret == -1) {
356 return -write_errno;
357 }
358 if (ret == 0) {
359 return -EIO;
360 }
361 return ret;
362 }
363
364 ret = atomic_exchange(&logger_list->fd, sock);
365 if ((ret > 0) && (ret != sock)) {
366 close(ret);
367 }
368 return sock;
369 }
370
371 /* Read from the selected logs */
LogdRead(struct logger_list * logger_list,struct log_msg * log_msg)372 int LogdRead(struct logger_list* logger_list, struct log_msg* log_msg) {
373 int ret = logdOpen(logger_list);
374 if (ret < 0) {
375 return ret;
376 }
377
378 /* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */
379 ret = TEMP_FAILURE_RETRY(recv(ret, log_msg, LOGGER_ENTRY_MAX_LEN, 0));
380 if ((logger_list->mode & ANDROID_LOG_NONBLOCK) && ret == 0) {
381 return -EAGAIN;
382 }
383
384 if (ret == -1) {
385 return -errno;
386 }
387 return ret;
388 }
389
390 /* Close all the logs */
LogdClose(struct logger_list * logger_list)391 void LogdClose(struct logger_list* logger_list) {
392 int sock = atomic_exchange(&logger_list->fd, -1);
393 if (sock > 0) {
394 close(sock);
395 }
396 }
397