1 /* 2 * Copyright (C) 2025 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 #pragma once 17 18 #include <gtest/gtest_prod.h> 19 #include <utils/RefBase.h> 20 21 #include "LogEventFilter.h" 22 #include "logd/LogEventQueue.h" 23 24 // DEFAULT_OVERFLOWUID is defined in linux/highuid.h, which is not part of 25 // the uapi headers for userspace to use. This value is filled in on the 26 // out-of-band socket credentials if the OS fails to find one available. 27 // One of the causes of this is if SO_PASSCRED is set, all the packets before 28 // that point will have this value. We also use it in a fake credential if 29 // no socket credentials are supplied. 30 #ifndef DEFAULT_OVERFLOWUID 31 #define DEFAULT_OVERFLOWUID 65534 32 #endif 33 34 namespace android { 35 namespace os { 36 namespace statsd { 37 38 class BaseStatsSocketListener : public virtual RefBase { 39 public: 40 /** 41 * Constructor of the BaseStatsSocketListener class 42 * 43 * @param queue queue to submit the event 44 * @param logEventFilter to be used for event evaluation 45 */ 46 explicit BaseStatsSocketListener(const std::shared_ptr<LogEventQueue>& queue, 47 const std::shared_ptr<LogEventFilter>& logEventFilter); 48 protected: 49 static int getLogSocket(); 50 51 /** 52 * @brief Helper API to parse raw socket data buffer, make the LogEvent & submit it into the 53 * queue. Performs preliminary data validation. 54 * Created as a separate API to be easily tested without StatsSocketListener instance 55 * 56 * @param buffer buffer to parse 57 * @param len size of buffer in bytes 58 * @param uid arguments for LogEvent constructor 59 * @param pid arguments for LogEvent constructor 60 * @param queue queue to submit the event 61 * @param filter to be used for event evaluation 62 * @return tuple of <atom id, elapsed time> 63 */ 64 std::tuple<int32_t, int64_t> processSocketMessage(void* buffer, uint32_t len, 65 uint32_t uid, uint32_t pid); 66 67 68 69 void noteBatchSocketRead(int32_t size, int64_t lastReadTimeNs, int64_t currReadTimeNs, 70 int64_t minAtomReadTimeNs, int64_t maxAtomReadTimeNs, 71 const std::unordered_map<int32_t, int32_t>& atomCounts); 72 73 74 75 int64_t mLastSocketReadTimeNs = 0; 76 77 // Tracks the atom counts per read. Member variable to avoid churn. 78 std::unordered_map<int32_t, int32_t> mAtomCounts; 79 80 /** 81 * Who is going to get the events when they're read. 82 */ 83 std::shared_ptr<LogEventQueue> mQueue; 84 85 std::shared_ptr<LogEventFilter> mLogEventFilter; 86 87 private: 88 /** 89 * @brief Helper API to parse buffer, make the LogEvent & submit it into the queue 90 * Created as a separate API to be easily tested without StatsSocketListener instance 91 * 92 * @param msg buffer to parse 93 * @param len size of buffer in bytes 94 * @param uid arguments for LogEvent constructor 95 * @param pid arguments for LogEvent constructor 96 * @param queue queue to submit the event 97 * @param filter to be used for event evaluation 98 * @return tuple of <atom id, elapsed time> 99 */ 100 static std::tuple<int32_t, int64_t> processStatsEventBuffer(const uint8_t* msg, uint32_t len, 101 uint32_t uid, uint32_t pid, 102 LogEventQueue& queue, 103 const LogEventFilter& filter); 104 friend void fuzzSocket(const uint8_t* data, size_t size); 105 106 friend class SocketParseMessageTest; 107 friend void generateAtomLogging(LogEventQueue& queue, const LogEventFilter& filter, 108 int eventCount, int startAtomId); 109 110 FRIEND_TEST(SocketParseMessageTest, TestProcessMessage); 111 FRIEND_TEST(SocketParseMessageTest, TestProcessMessageEmptySetExplicitSet); 112 FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterCompleteSet); 113 FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterPartialSet); 114 FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterToggle); 115 FRIEND_TEST(LogEventQueue_test, TestQueueMaxSize); 116 }; 117 118 } // namespace statsd 119 } // namespace os 120 } // namespace android 121