• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 <sysutils/SocketListener.h>
20 #include <utils/RefBase.h>
21 
22 #include "LogEventFilter.h"
23 #include "logd/LogEventQueue.h"
24 
25 // DEFAULT_OVERFLOWUID is defined in linux/highuid.h, which is not part of
26 // the uapi headers for userspace to use.  This value is filled in on the
27 // out-of-band socket credentials if the OS fails to find one available.
28 // One of the causes of this is if SO_PASSCRED is set, all the packets before
29 // that point will have this value.  We also use it in a fake credential if
30 // no socket credentials are supplied.
31 #ifndef DEFAULT_OVERFLOWUID
32 #define DEFAULT_OVERFLOWUID 65534
33 #endif
34 
35 namespace android {
36 namespace os {
37 namespace statsd {
38 
39 class StatsSocketListener : public SocketListener, public virtual RefBase {
40 public:
41     explicit StatsSocketListener(std::shared_ptr<LogEventQueue> queue,
42                                  const std::shared_ptr<LogEventFilter>& logEventFilter);
43 
44     virtual ~StatsSocketListener() = default;
45 
46 protected:
47     bool onDataAvailable(SocketClient* cli) override;
48 
49 private:
50     static int getLogSocket();
51 
52     /**
53      * @brief Helper API to parse buffer, make the LogEvent & submit it into the queue
54      * Created as a separate API to be easily tested without StatsSocketListener instance
55      *
56      * @param msg 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      */
63     static void processMessage(const uint8_t* msg, uint32_t len, uint32_t uid, uint32_t pid,
64                                const std::shared_ptr<LogEventQueue>& queue,
65                                const std::shared_ptr<LogEventFilter>& filter);
66 
67     /**
68      * Who is going to get the events when they're read.
69      */
70     std::shared_ptr<LogEventQueue> mQueue;
71 
72     std::shared_ptr<LogEventFilter> mLogEventFilter;
73 
74     friend class SocketParseMessageTest;
75     friend void generateAtomLogging(const std::shared_ptr<LogEventQueue>& queue,
76                                     const std::shared_ptr<LogEventFilter>& filter, int eventCount,
77                                     int startAtomId);
78 
79     FRIEND_TEST(SocketParseMessageTestNoFiltering, TestProcessMessageNoFiltering);
80     FRIEND_TEST(SocketParseMessageTestNoFiltering,
81                 TestProcessMessageNoFilteringWithEmptySetExplicitSet);
82     FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterEmptySet);
83     FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterCompleteSet);
84     FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterPartialSet);
85     FRIEND_TEST(SocketParseMessageTest, TestProcessMessageFilterToggle);
86 };
87 
88 }  // namespace statsd
89 }  // namespace os
90 }  // namespace android
91