blob: eb5194c6f696b5a0b403d8a3aff6725cacad2511 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070017#include <ctype.h>
Mark Salyzynab0dcf62015-03-16 12:04:09 -070018#include <endian.h>
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070019#include <fcntl.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080020#include <stdio.h>
21#include <string.h>
22#include <time.h>
23#include <unistd.h>
24
25#include <log/logger.h>
Mark Salyzynab0dcf62015-03-16 12:04:09 -070026#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
Mark Salyzyn317bfb92016-02-23 08:55:43 -080028#include "LogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080029#include "LogBufferElement.h"
Mark Salyzynab0dcf62015-03-16 12:04:09 -070030#include "LogCommand.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080031#include "LogReader.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080032#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzynf7c0f752015-03-03 13:39:37 -080034const uint64_t LogBufferElement::FLUSH_ERROR(0);
Mark Salyzynee49c6a2015-06-12 14:59:42 -070035atomic_int_fast64_t LogBufferElement::sequence(1);
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080037LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070038 uid_t uid, pid_t pid, pid_t tid,
Mark Salyzyn77187782015-05-12 15:21:31 -070039 const char *msg, unsigned short len) :
40 mLogId(log_id),
41 mUid(uid),
42 mPid(pid),
43 mTid(tid),
44 mMsgLen(len),
45 mSequence(sequence.fetch_add(1, memory_order_relaxed)),
46 mRealTime(realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080047 mMsg = new char[len];
48 memcpy(mMsg, msg, len);
49}
50
51LogBufferElement::~LogBufferElement() {
52 delete [] mMsg;
53}
54
Mark Salyzyn344bff42015-04-13 14:24:45 -070055uint32_t LogBufferElement::getTag() const {
Mark Salyzyn083b0372015-12-04 10:59:45 -080056 if (((mLogId != LOG_ID_EVENTS) && (mLogId != LOG_ID_SECURITY)) ||
57 !mMsg || (mMsgLen < sizeof(uint32_t))) {
Mark Salyzyn344bff42015-04-13 14:24:45 -070058 return 0;
59 }
60 return le32toh(reinterpret_cast<android_event_header_t *>(mMsg)->tag);
61}
62
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070063// caller must own and free character string
Mark Salyzyn17ed6792015-04-20 13:35:15 -070064char *android::tidToName(pid_t tid) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070065 char *retval = NULL;
66 char buffer[256];
67 snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
68 int fd = open(buffer, O_RDONLY);
69 if (fd >= 0) {
70 ssize_t ret = read(fd, buffer, sizeof(buffer));
71 if (ret >= (ssize_t)sizeof(buffer)) {
72 ret = sizeof(buffer) - 1;
73 }
74 while ((ret > 0) && isspace(buffer[ret - 1])) {
75 --ret;
76 }
77 if (ret > 0) {
78 buffer[ret] = '\0';
79 retval = strdup(buffer);
80 }
81 close(fd);
Mark Salyzynab0dcf62015-03-16 12:04:09 -070082 }
83
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070084 // if nothing for comm, check out cmdline
85 char *name = android::pidToName(tid);
86 if (!retval) {
87 retval = name;
88 name = NULL;
89 }
90
91 // check if comm is truncated, see if cmdline has full representation
92 if (name) {
93 // impossible for retval to be NULL if name not NULL
94 size_t retval_len = strlen(retval);
95 size_t name_len = strlen(name);
96 // KISS: ToDo: Only checks prefix truncated, not suffix, or both
Mark Salyzynddda2122015-10-02 09:22:52 -070097 if ((retval_len < name_len)
98 && !fast<strcmp>(retval, name + name_len - retval_len)) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070099 free(retval);
100 retval = name;
101 } else {
102 free(name);
103 }
104 }
105 return retval;
106}
107
108// assumption: mMsg == NULL
109size_t LogBufferElement::populateDroppedMessage(char *&buffer,
110 LogBuffer *parent) {
Mark Salyzyn047cc072015-06-04 13:35:30 -0700111 static const char tag[] = "chatty";
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700112
Mark Salyzync2e7d492015-11-16 19:00:41 +0000113 if (!__android_log_is_loggable(ANDROID_LOG_INFO, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyne59c4692014-10-02 13:07:05 -0700114 return 0;
115 }
116
117 static const char format_uid[] = "uid=%u%s%s expire %u line%s";
Mark Salyzyned777e92015-06-24 16:22:54 -0700118 parent->lock();
Mark Salyzyn758058f2015-08-21 16:44:30 -0700119 const char *name = parent->uidToName(mUid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700120 parent->unlock();
Mark Salyzyn758058f2015-08-21 16:44:30 -0700121 const char *commName = android::tidToName(mTid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700122 if (!commName && (mTid != mPid)) {
Mark Salyzyn17ed6792015-04-20 13:35:15 -0700123 commName = android::tidToName(mPid);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700124 }
125 if (!commName) {
Mark Salyzyned777e92015-06-24 16:22:54 -0700126 parent->lock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700127 commName = parent->pidToName(mPid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700128 parent->unlock();
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700129 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700130 if (name && name[0] && commName && (name[0] == commName[0])) {
131 size_t len = strlen(name + 1);
132 if (!strncmp(name + 1, commName + 1, len)) {
133 if (commName[len + 1] == '\0') {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700134 free(const_cast<char *>(commName));
Mark Salyzynddda2122015-10-02 09:22:52 -0700135 commName = NULL;
136 } else {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700137 free(const_cast<char *>(name));
Mark Salyzynddda2122015-10-02 09:22:52 -0700138 name = NULL;
139 }
Mark Salyzyn047cc072015-06-04 13:35:30 -0700140 }
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700141 }
142 if (name) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700143 char *buf = NULL;
144 asprintf(&buf, "(%s)", name);
145 if (buf) {
146 free(const_cast<char *>(name));
147 name = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700148 }
149 }
150 if (commName) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700151 char *buf = NULL;
152 asprintf(&buf, " %s", commName);
153 if (buf) {
154 free(const_cast<char *>(commName));
155 commName = buf;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700156 }
157 }
158 // identical to below to calculate the buffer size required
Mark Salyzynddda2122015-10-02 09:22:52 -0700159 size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
160 commName ? commName : "",
161 mDropped, (mDropped > 1) ? "s" : "");
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700162
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700163 size_t hdrLen;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800164 // LOG_ID_SECURITY not strictly needed since spam filter not activated,
165 // but required for accuracy.
166 if ((mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY)) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700167 hdrLen = sizeof(android_log_event_string_t);
168 } else {
169 hdrLen = 1 + sizeof(tag);
170 }
171
172 buffer = static_cast<char *>(calloc(1, hdrLen + len + 1));
173 if (!buffer) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700174 free(const_cast<char *>(name));
175 free(const_cast<char *>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700176 return 0;
177 }
178
179 size_t retval = hdrLen + len;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800180 if ((mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY)) {
Mark Salyzyn758058f2015-08-21 16:44:30 -0700181 android_log_event_string_t *event =
182 reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700183
Mark Salyzyn758058f2015-08-21 16:44:30 -0700184 event->header.tag = htole32(LOGD_LOG_TAG);
185 event->type = EVENT_TYPE_STRING;
186 event->length = htole32(len);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700187 } else {
188 ++retval;
189 buffer[0] = ANDROID_LOG_INFO;
190 strcpy(buffer + 1, tag);
191 }
192
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700193 snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "",
194 commName ? commName : "",
195 mDropped, (mDropped > 1) ? "s" : "");
Mark Salyzyn758058f2015-08-21 16:44:30 -0700196 free(const_cast<char *>(name));
197 free(const_cast<char *>(commName));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700198
199 return retval;
200}
201
Mark Salyzyn7b873652015-12-03 15:38:35 -0800202uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent,
203 bool privileged) {
204 struct logger_entry_v4 entry;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700205
Mark Salyzyn7b873652015-12-03 15:38:35 -0800206 memset(&entry, 0, sizeof(struct logger_entry_v4));
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700207
Mark Salyzyn7b873652015-12-03 15:38:35 -0800208 entry.hdr_size = privileged ?
209 sizeof(struct logger_entry_v4) :
210 sizeof(struct logger_entry_v3);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800211 entry.lid = mLogId;
212 entry.pid = mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700213 entry.tid = mTid;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800214 entry.uid = mUid;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800215 entry.sec = mRealTime.tv_sec;
216 entry.nsec = mRealTime.tv_nsec;
217
218 struct iovec iovec[2];
219 iovec[0].iov_base = &entry;
Mark Salyzyn7b873652015-12-03 15:38:35 -0800220 iovec[0].iov_len = entry.hdr_size;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700221
222 char *buffer = NULL;
223
224 if (!mMsg) {
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700225 entry.len = populateDroppedMessage(buffer, parent);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700226 if (!entry.len) {
227 return mSequence;
228 }
229 iovec[1].iov_base = buffer;
230 } else {
231 entry.len = mMsgLen;
232 iovec[1].iov_base = mMsg;
233 }
234 iovec[1].iov_len = entry.len;
235
236 uint64_t retval = reader->sendDatav(iovec, 2) ? FLUSH_ERROR : mSequence;
237
238 if (buffer) {
239 free(buffer);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800240 }
241
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700242 return retval;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800243}