• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 #ifndef _INIT_UEVENT_LISTENER_H
18 #define _INIT_UEVENT_LISTENER_H
19 
20 #include <dirent.h>
21 
22 #include <chrono>
23 #include <functional>
24 #include <optional>
25 
26 #include <android-base/unique_fd.h>
27 
28 #include "uevent.h"
29 
30 #define UEVENT_MSG_LEN 8192
31 
32 namespace android {
33 namespace init {
34 
35 enum class ListenerAction {
36     kStop = 0,  // Stop regenerating uevents as we've handled the one(s) we're interested in.
37     kContinue,  // Continue regenerating uevents as we haven't seen the one(s) we're interested in.
38 };
39 
40 enum class ReadUeventResult {
41     kSuccess = 0,  // Uevent was successfully read.
42     kFailed,       // Uevent reading has failed.
43     kInvalid,      // An Invalid Uevent was read (like say, the msg received is >= UEVENT_MSG_LEN).
44 };
45 
46 using ListenerCallback = std::function<ListenerAction(const Uevent&)>;
47 
48 class UeventListener {
49   public:
50     UeventListener(size_t uevent_socket_rcvbuf_size);
51 
52     void RegenerateUevents(const ListenerCallback& callback) const;
53     ListenerAction RegenerateUeventsForPath(const std::string& path,
54                                             const ListenerCallback& callback) const;
55     void Poll(const ListenerCallback& callback,
56               const std::optional<std::chrono::milliseconds> relative_timeout = {}) const;
57 
58   private:
59     ReadUeventResult ReadUevent(Uevent* uevent) const;
60     ListenerAction RegenerateUeventsForDir(DIR* d, const ListenerCallback& callback) const;
61 
62     android::base::unique_fd device_fd_;
63 };
64 
65 }  // namespace init
66 }  // namespace android
67 
68 #endif
69