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