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