Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2013 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 | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | #include <poll.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 19 | #include <sys/prctl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 21 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <cutils/sockets.h> |
| 23 | |
| 24 | #include "LogReader.h" |
| 25 | #include "FlushCommand.h" |
| 26 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 27 | LogReader::LogReader(LogBuffer *logbuf) : |
| 28 | SocketListener(getLogSocket(), true), |
| 29 | mLogbuf(*logbuf) { |
| 30 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 31 | |
| 32 | // When we are notified a new log entry is available, inform |
| 33 | // all of our listening sockets. |
| 34 | void LogReader::notifyNewLog() { |
| 35 | FlushCommand command(*this); |
| 36 | runOnEachSocket(&command); |
| 37 | } |
| 38 | |
| 39 | bool LogReader::onDataAvailable(SocketClient *cli) { |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 40 | static bool name_set; |
| 41 | if (!name_set) { |
| 42 | prctl(PR_SET_NAME, "logd.reader"); |
| 43 | name_set = true; |
| 44 | } |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 45 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | char buffer[255]; |
| 47 | |
| 48 | int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1); |
| 49 | if (len <= 0) { |
| 50 | doSocketDelete(cli); |
| 51 | return false; |
| 52 | } |
| 53 | buffer[len] = '\0'; |
| 54 | |
| 55 | unsigned long tail = 0; |
| 56 | static const char _tail[] = " tail="; |
| 57 | char *cp = strstr(buffer, _tail); |
| 58 | if (cp) { |
| 59 | tail = atol(cp + sizeof(_tail) - 1); |
| 60 | } |
| 61 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 62 | log_time start(log_time::EPOCH); |
| 63 | static const char _start[] = " start="; |
| 64 | cp = strstr(buffer, _start); |
| 65 | if (cp) { |
| 66 | // Parse errors will result in current time |
| 67 | start.strptime(cp + sizeof(_start) - 1, "%s.%q"); |
| 68 | } |
| 69 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 70 | uint64_t timeout = 0; |
| 71 | static const char _timeout[] = " timeout="; |
| 72 | cp = strstr(buffer, _timeout); |
| 73 | if (cp) { |
| 74 | timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC + |
| 75 | log_time(CLOCK_REALTIME).nsec(); |
| 76 | } |
| 77 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 78 | unsigned int logMask = -1; |
| 79 | static const char _logIds[] = " lids="; |
| 80 | cp = strstr(buffer, _logIds); |
| 81 | if (cp) { |
| 82 | logMask = 0; |
| 83 | cp += sizeof(_logIds) - 1; |
| 84 | while (*cp && *cp != '\0') { |
| 85 | int val = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 86 | while (isdigit(*cp)) { |
| 87 | val = val * 10 + *cp - '0'; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 88 | ++cp; |
| 89 | } |
| 90 | logMask |= 1 << val; |
| 91 | if (*cp != ',') { |
| 92 | break; |
| 93 | } |
| 94 | ++cp; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pid_t pid = 0; |
| 99 | static const char _pid[] = " pid="; |
| 100 | cp = strstr(buffer, _pid); |
| 101 | if (cp) { |
| 102 | pid = atol(cp + sizeof(_pid) - 1); |
| 103 | } |
| 104 | |
| 105 | bool nonBlock = false; |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 106 | if (!fast<strncmp>(buffer, "dumpAndClose", 12)) { |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 107 | // Allow writer to get some cycles, and wait for pending notifications |
| 108 | sched_yield(); |
| 109 | LogTimeEntry::lock(); |
| 110 | LogTimeEntry::unlock(); |
| 111 | sched_yield(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 112 | nonBlock = true; |
| 113 | } |
| 114 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 115 | uint64_t sequence = 1; |
| 116 | // Convert realtime to sequence number |
| 117 | if (start != log_time::EPOCH) { |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 118 | class LogFindStart { |
| 119 | const pid_t mPid; |
| 120 | const unsigned mLogMask; |
| 121 | bool startTimeSet; |
| 122 | log_time &start; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 123 | uint64_t &sequence; |
| 124 | uint64_t last; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 125 | bool isMonotonic; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 126 | |
| 127 | public: |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 128 | LogFindStart(unsigned logMask, pid_t pid, log_time &start, uint64_t &sequence, bool isMonotonic) : |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 129 | mPid(pid), |
| 130 | mLogMask(logMask), |
| 131 | startTimeSet(false), |
| 132 | start(start), |
| 133 | sequence(sequence), |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 134 | last(sequence), |
| 135 | isMonotonic(isMonotonic) { |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 136 | } |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 137 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 138 | static int callback(const LogBufferElement *element, void *obj) { |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 139 | LogFindStart *me = reinterpret_cast<LogFindStart *>(obj); |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 140 | if ((!me->mPid || (me->mPid == element->getPid())) |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 141 | && (me->mLogMask & (1 << element->getLogId()))) { |
| 142 | if (me->start == element->getRealTime()) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 143 | me->sequence = element->getSequence(); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 144 | me->startTimeSet = true; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 145 | return -1; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 146 | } else if (!me->isMonotonic || |
| 147 | android::isMonotonic(element->getRealTime())) { |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 148 | if (me->start < element->getRealTime()) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 149 | me->sequence = me->last; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 150 | me->startTimeSet = true; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 151 | return -1; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 152 | } |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 153 | me->last = element->getSequence(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 154 | } else { |
| 155 | me->last = element->getSequence(); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | bool found() { return startTimeSet; } |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 162 | } logFindStart(logMask, pid, start, sequence, |
| 163 | logbuf().isMonotonic() && android::isMonotonic(start)); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 164 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 165 | logbuf().flushTo(cli, sequence, FlushCommand::hasReadLogs(cli), |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 166 | FlushCommand::hasSecurityLogs(cli), |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 167 | logFindStart.callback, &logFindStart); |
| 168 | |
| 169 | if (!logFindStart.found()) { |
| 170 | if (nonBlock) { |
| 171 | doSocketDelete(cli); |
| 172 | return false; |
| 173 | } |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 174 | sequence = LogBufferElement::getCurrentSequence(); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 175 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 178 | FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence, timeout); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 179 | command.runSocketCommand(cli); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | void LogReader::doSocketDelete(SocketClient *cli) { |
| 184 | LastLogTimes × = mLogbuf.mTimes; |
| 185 | LogTimeEntry::lock(); |
| 186 | LastLogTimes::iterator it = times.begin(); |
| 187 | while(it != times.end()) { |
| 188 | LogTimeEntry *entry = (*it); |
| 189 | if (entry->mClient == cli) { |
| 190 | times.erase(it); |
| 191 | entry->release_Locked(); |
| 192 | break; |
| 193 | } |
| 194 | it++; |
| 195 | } |
| 196 | LogTimeEntry::unlock(); |
| 197 | } |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 198 | |
| 199 | int LogReader::getLogSocket() { |
| 200 | static const char socketName[] = "logdr"; |
| 201 | int sock = android_get_control_socket(socketName); |
| 202 | |
| 203 | if (sock < 0) { |
| 204 | sock = socket_local_server(socketName, |
| 205 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 206 | SOCK_SEQPACKET); |
| 207 | } |
| 208 | |
| 209 | return sock; |
| 210 | } |