Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 18 | #include <endian.h> |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <log/logger.h> |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 26 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | |
| 28 | #include "LogBufferElement.h" |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 29 | #include "LogCommand.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include "LogReader.h" |
| 31 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 32 | const uint64_t LogBufferElement::FLUSH_ERROR(0); |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 33 | atomic_int_fast64_t LogBufferElement::sequence(1); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 35 | LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 36 | uid_t uid, pid_t pid, pid_t tid, |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 37 | const char *msg, unsigned short len) : |
| 38 | mLogId(log_id), |
| 39 | mUid(uid), |
| 40 | mPid(pid), |
| 41 | mTid(tid), |
| 42 | mMsgLen(len), |
| 43 | mSequence(sequence.fetch_add(1, memory_order_relaxed)), |
| 44 | mRealTime(realtime) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | mMsg = new char[len]; |
| 46 | memcpy(mMsg, msg, len); |
| 47 | } |
| 48 | |
| 49 | LogBufferElement::~LogBufferElement() { |
| 50 | delete [] mMsg; |
| 51 | } |
| 52 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 53 | uint32_t LogBufferElement::getTag() const { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 54 | if (((mLogId != LOG_ID_EVENTS) && (mLogId != LOG_ID_SECURITY)) || |
| 55 | !mMsg || (mMsgLen < sizeof(uint32_t))) { |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | return le32toh(reinterpret_cast<android_event_header_t *>(mMsg)->tag); |
| 59 | } |
| 60 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 61 | // caller must own and free character string |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 62 | char *android::tidToName(pid_t tid) { |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 63 | char *retval = NULL; |
| 64 | char buffer[256]; |
| 65 | snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid); |
| 66 | int fd = open(buffer, O_RDONLY); |
| 67 | if (fd >= 0) { |
| 68 | ssize_t ret = read(fd, buffer, sizeof(buffer)); |
| 69 | if (ret >= (ssize_t)sizeof(buffer)) { |
| 70 | ret = sizeof(buffer) - 1; |
| 71 | } |
| 72 | while ((ret > 0) && isspace(buffer[ret - 1])) { |
| 73 | --ret; |
| 74 | } |
| 75 | if (ret > 0) { |
| 76 | buffer[ret] = '\0'; |
| 77 | retval = strdup(buffer); |
| 78 | } |
| 79 | close(fd); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 82 | // if nothing for comm, check out cmdline |
| 83 | char *name = android::pidToName(tid); |
| 84 | if (!retval) { |
| 85 | retval = name; |
| 86 | name = NULL; |
| 87 | } |
| 88 | |
| 89 | // check if comm is truncated, see if cmdline has full representation |
| 90 | if (name) { |
| 91 | // impossible for retval to be NULL if name not NULL |
| 92 | size_t retval_len = strlen(retval); |
| 93 | size_t name_len = strlen(name); |
| 94 | // KISS: ToDo: Only checks prefix truncated, not suffix, or both |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 95 | if ((retval_len < name_len) |
| 96 | && !fast<strcmp>(retval, name + name_len - retval_len)) { |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 97 | free(retval); |
| 98 | retval = name; |
| 99 | } else { |
| 100 | free(name); |
| 101 | } |
| 102 | } |
| 103 | return retval; |
| 104 | } |
| 105 | |
| 106 | // assumption: mMsg == NULL |
| 107 | size_t LogBufferElement::populateDroppedMessage(char *&buffer, |
| 108 | LogBuffer *parent) { |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 109 | static const char tag[] = "chatty"; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame] | 111 | if (!__android_log_is_loggable(ANDROID_LOG_INFO, tag, ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static const char format_uid[] = "uid=%u%s%s expire %u line%s"; |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 116 | parent->lock(); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 117 | const char *name = parent->uidToName(mUid); |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 118 | parent->unlock(); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 119 | const char *commName = android::tidToName(mTid); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 120 | if (!commName && (mTid != mPid)) { |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 121 | commName = android::tidToName(mPid); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 122 | } |
| 123 | if (!commName) { |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 124 | parent->lock(); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 125 | commName = parent->pidToName(mPid); |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 126 | parent->unlock(); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 127 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 128 | if (name && name[0] && commName && (name[0] == commName[0])) { |
| 129 | size_t len = strlen(name + 1); |
| 130 | if (!strncmp(name + 1, commName + 1, len)) { |
| 131 | if (commName[len + 1] == '\0') { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 132 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 133 | commName = NULL; |
| 134 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 135 | free(const_cast<char *>(name)); |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 136 | name = NULL; |
| 137 | } |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 138 | } |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 139 | } |
| 140 | if (name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 141 | char *buf = NULL; |
| 142 | asprintf(&buf, "(%s)", name); |
| 143 | if (buf) { |
| 144 | free(const_cast<char *>(name)); |
| 145 | name = buf; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | if (commName) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 149 | char *buf = NULL; |
| 150 | asprintf(&buf, " %s", commName); |
| 151 | if (buf) { |
| 152 | free(const_cast<char *>(commName)); |
| 153 | commName = buf; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | // identical to below to calculate the buffer size required |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 157 | size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", |
| 158 | commName ? commName : "", |
| 159 | mDropped, (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 160 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 161 | size_t hdrLen; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 162 | // LOG_ID_SECURITY not strictly needed since spam filter not activated, |
| 163 | // but required for accuracy. |
| 164 | if ((mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY)) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 165 | hdrLen = sizeof(android_log_event_string_t); |
| 166 | } else { |
| 167 | hdrLen = 1 + sizeof(tag); |
| 168 | } |
| 169 | |
| 170 | buffer = static_cast<char *>(calloc(1, hdrLen + len + 1)); |
| 171 | if (!buffer) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 172 | free(const_cast<char *>(name)); |
| 173 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | size_t retval = hdrLen + len; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 178 | if ((mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 179 | android_log_event_string_t *event = |
| 180 | reinterpret_cast<android_log_event_string_t *>(buffer); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 181 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 182 | event->header.tag = htole32(LOGD_LOG_TAG); |
| 183 | event->type = EVENT_TYPE_STRING; |
| 184 | event->length = htole32(len); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 185 | } else { |
| 186 | ++retval; |
| 187 | buffer[0] = ANDROID_LOG_INFO; |
| 188 | strcpy(buffer + 1, tag); |
| 189 | } |
| 190 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 191 | snprintf(buffer + hdrLen, len + 1, format_uid, mUid, name ? name : "", |
| 192 | commName ? commName : "", |
| 193 | mDropped, (mDropped > 1) ? "s" : ""); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 194 | free(const_cast<char *>(name)); |
| 195 | free(const_cast<char *>(commName)); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 196 | |
| 197 | return retval; |
| 198 | } |
| 199 | |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 200 | uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent, |
| 201 | bool privileged) { |
| 202 | struct logger_entry_v4 entry; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 203 | |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 204 | memset(&entry, 0, sizeof(struct logger_entry_v4)); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 205 | |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 206 | entry.hdr_size = privileged ? |
| 207 | sizeof(struct logger_entry_v4) : |
| 208 | sizeof(struct logger_entry_v3); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 209 | entry.lid = mLogId; |
| 210 | entry.pid = mPid; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 211 | entry.tid = mTid; |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 212 | entry.uid = mUid; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 213 | entry.sec = mRealTime.tv_sec; |
| 214 | entry.nsec = mRealTime.tv_nsec; |
| 215 | |
| 216 | struct iovec iovec[2]; |
| 217 | iovec[0].iov_base = &entry; |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 218 | iovec[0].iov_len = entry.hdr_size; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 219 | |
| 220 | char *buffer = NULL; |
| 221 | |
| 222 | if (!mMsg) { |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 223 | entry.len = populateDroppedMessage(buffer, parent); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 224 | if (!entry.len) { |
| 225 | return mSequence; |
| 226 | } |
| 227 | iovec[1].iov_base = buffer; |
| 228 | } else { |
| 229 | entry.len = mMsgLen; |
| 230 | iovec[1].iov_base = mMsg; |
| 231 | } |
| 232 | iovec[1].iov_len = entry.len; |
| 233 | |
| 234 | uint64_t retval = reader->sendDatav(iovec, 2) ? FLUSH_ERROR : mSequence; |
| 235 | |
| 236 | if (buffer) { |
| 237 | free(buffer); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 240 | return retval; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 241 | } |