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 | |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 17 | #pragma once |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <pthread.h> |
Jintao Zhu | d3987a9 | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 22 | #include <time.h> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 23 | |
| 24 | #include <list> |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 25 | #include <memory> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 26 | |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 27 | #include <log/log.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | |
| 30 | class LogReader; |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 31 | class LogBufferElement; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 33 | class LogReaderThread { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | static pthread_mutex_t timesLock; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 36 | public: |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 37 | LogReaderThread(LogReader& reader, SocketClient* client, bool non_block, unsigned long tail, |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 38 | unsigned int log_mask, pid_t pid, log_time start_time, uint64_t sequence, |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 39 | uint64_t timeout, bool privileged, bool can_read_security_logs); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 40 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 41 | // Protect List manipulations |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 42 | static void wrlock() { pthread_mutex_lock(×Lock); } |
| 43 | static void rdlock() { pthread_mutex_lock(×Lock); } |
| 44 | static void unlock() { pthread_mutex_unlock(×Lock); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 46 | bool startReader_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 47 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 48 | void triggerReader_Locked() { pthread_cond_signal(&thread_triggered_condition_); } |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 49 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 50 | void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; } |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 51 | void cleanSkip_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 52 | |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 53 | void release_Locked() { |
Jintao Zhu | d3987a9 | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 54 | // gracefully shut down the socket. |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 55 | shutdown(client_->getSocket(), SHUT_RDWR); |
| 56 | release_ = true; |
| 57 | pthread_cond_signal(&thread_triggered_condition_); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 60 | bool IsWatching(log_id_t id) const { return log_mask_ & (1 << id); } |
| 61 | bool IsWatchingMultiple(unsigned int log_mask) const { return log_mask_ & log_mask; } |
| 62 | |
| 63 | const SocketClient* client() const { return client_; } |
| 64 | uint64_t start() const { return start_; } |
| 65 | const timespec& timeout() const { return timeout_; } |
Tom Cherry | 79d54f7 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 66 | |
| 67 | private: |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 68 | void ThreadFunction(); |
| 69 | // flushTo filter callbacks |
| 70 | int FilterFirstPass(const LogBufferElement* element); |
| 71 | int FilterSecondPass(const LogBufferElement* element); |
| 72 | |
| 73 | // Set to true to cause the thread to end and the LogReaderThread to delete itself. |
| 74 | bool release_ = false; |
| 75 | // Indicates whether or not 'leading' (first logs seen starting from start_) 'dropped' (chatty) |
| 76 | // messages should be ignored. |
| 77 | bool leading_dropped_; |
| 78 | |
| 79 | // Condition variable for waking the reader thread if there are messages pending for its client. |
| 80 | pthread_cond_t thread_triggered_condition_; |
| 81 | |
| 82 | // Reference to the parent thread that manages log reader sockets. |
| 83 | LogReader& reader_; |
| 84 | // A mask of the logs buffers that are read by this reader. |
| 85 | const unsigned int log_mask_; |
| 86 | // If set to non-zero, only pids equal to this are read by the reader. |
| 87 | const pid_t pid_; |
| 88 | // When a reader is referencing (via start_) old elements in the log buffer, and the log |
| 89 | // buffer's size grows past its memory limit, the log buffer may request the reader to skip |
| 90 | // ahead a specified number of logs. |
| 91 | unsigned int skip_ahead_[LOG_ID_MAX]; |
| 92 | // Used for distinguishing 'dropped' messages for duplicate logs vs chatty drops |
| 93 | pid_t last_tid_[LOG_ID_MAX]; |
| 94 | |
| 95 | // These next three variables are used for reading only the most recent lines aka `adb logcat |
| 96 | // -t` / `adb logcat -T`. |
| 97 | // tail_ is the number of most recent lines to print. |
| 98 | unsigned long tail_; |
| 99 | // count_ is the result of a first pass through the log buffer to determine how many total |
| 100 | // messages there are. |
| 101 | unsigned long count_; |
| 102 | // index_ is used along with count_ to only start sending lines once index_ > (count_ - tail_) |
| 103 | // and to disconnect the reader (if it is dumpAndClose, `adb logcat -t`), when index_ >= count_. |
| 104 | unsigned long index_; |
| 105 | |
| 106 | // A pointer to the socket for this reader. |
| 107 | SocketClient* client_; |
| 108 | // When a reader requests logs starting from a given timestamp, its stored here for the first |
| 109 | // pass, such that logs before this time stamp that are accumulated in the buffer are ignored. |
| 110 | log_time start_time_; |
| 111 | // The point from which the reader will read logs once awoken. |
| 112 | uint64_t start_; |
| 113 | // CLOCK_MONOTONIC based timeout used for log wrapping. If this timeout expires before logs |
| 114 | // wrap, then wake up and send the logs to the reader anyway. |
| 115 | timespec timeout_; |
| 116 | // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs. |
| 117 | const bool non_block_; |
| 118 | |
| 119 | // Whether or not this reader can read logs from all UIDs or only its own UID. See |
| 120 | // clientHasLogCredentials(). |
Tom Cherry | 79d54f7 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 121 | bool privileged_; |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 122 | // Whether or not this reader can read security logs. See CanReadSecurityLogs(). |
Tom Cherry | 79d54f7 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 123 | bool can_read_security_logs_; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 126 | typedef std::list<std::unique_ptr<LogReaderThread>> LastLogTimes; |