Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #pragma once |
| 18 | |
| 19 | #include <linux/netlink.h> |
Drew Davenport | b852605 | 2024-08-28 10:24:33 -0600 | [diff] [blame] | 20 | #include <poll.h> |
| 21 | #include <sys/eventfd.h> |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | |
| 24 | #include <cerrno> |
| 25 | #include <memory> |
| 26 | #include <optional> |
| 27 | #include <string> |
| 28 | |
Roman Stratiienko | 7689278 | 2023-01-16 17:15:53 +0200 | [diff] [blame] | 29 | #include "fd.h" |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 30 | #include "log.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | class UEvent { |
| 35 | public: |
| 36 | static auto CreateInstance() -> std::unique_ptr<UEvent> { |
Roman Stratiienko | 7689278 | 2023-01-16 17:15:53 +0200 | [diff] [blame] | 37 | auto fd = MakeUniqueFd( |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 38 | socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)); |
| 39 | |
| 40 | if (!fd) { |
| 41 | ALOGE("Failed to open uevent socket: errno=%i", errno); |
| 42 | return {}; |
| 43 | } |
| 44 | |
| 45 | struct sockaddr_nl addr {}; |
| 46 | addr.nl_family = AF_NETLINK; |
| 47 | addr.nl_pid = 0; |
| 48 | addr.nl_groups = UINT32_MAX; |
| 49 | |
| 50 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) |
Roman Stratiienko | 7689278 | 2023-01-16 17:15:53 +0200 | [diff] [blame] | 51 | const int ret = bind(*fd, (struct sockaddr *)&addr, sizeof(addr)); |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 52 | if (ret != 0) { |
| 53 | ALOGE("Failed to bind uevent socket: errno=%i", errno); |
| 54 | return {}; |
| 55 | } |
| 56 | |
Drew Davenport | b852605 | 2024-08-28 10:24:33 -0600 | [diff] [blame] | 57 | auto stop_event_fd = MakeUniqueFd(eventfd(0, EFD_CLOEXEC)); |
| 58 | if (!stop_event_fd) { |
| 59 | ALOGE("Failed to create eventfd: errno=%i", errno); |
| 60 | return {}; |
| 61 | } |
| 62 | |
| 63 | return std::unique_ptr<UEvent>(new UEvent(fd, stop_event_fd)); |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | auto ReadNext() -> std::optional<std::string> { |
| 67 | constexpr int kUEventBufferSize = 1024; |
| 68 | char buffer[kUEventBufferSize]; |
Drew Davenport | b852605 | 2024-08-28 10:24:33 -0600 | [diff] [blame] | 69 | |
| 70 | if (!WaitForData()) { |
| 71 | return {}; |
| 72 | } |
| 73 | |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 74 | ssize_t ret = 0; |
Roman Stratiienko | 7689278 | 2023-01-16 17:15:53 +0200 | [diff] [blame] | 75 | ret = read(*fd_, &buffer, sizeof(buffer)); |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 76 | if (ret == 0) |
| 77 | return {}; |
| 78 | |
| 79 | if (ret < 0) { |
| 80 | ALOGE("Got error reading uevent %zd", ret); |
| 81 | return {}; |
| 82 | } |
| 83 | |
| 84 | for (int i = 0; i < ret - 1; i++) { |
| 85 | if (buffer[i] == '\0') { |
| 86 | buffer[i] = '\n'; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return std::string(buffer); |
| 91 | } |
| 92 | |
Drew Davenport | b852605 | 2024-08-28 10:24:33 -0600 | [diff] [blame] | 93 | void Stop() { |
| 94 | // Increment the eventfd by writing 1. All subsequent calls to ReadNext will |
| 95 | // return false. |
| 96 | const uint64_t value = 1; |
| 97 | const ssize_t ret = write(*stop_event_fd_, &value, sizeof(value)); |
| 98 | if (ret == -1) { |
| 99 | ALOGE("Error writing to eventfd. errno: %d", errno); |
| 100 | } else if (ret != sizeof(value)) { |
| 101 | ALOGE("Wrote fewer bytes to eventfd than expected: %zd vs %zd", ret, |
| 102 | sizeof(value)); |
| 103 | } |
| 104 | } |
| 105 | |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 106 | private: |
Drew Davenport | b852605 | 2024-08-28 10:24:33 -0600 | [diff] [blame] | 107 | enum { kFdIdx = 0, kStopEventFdIdx, kNumFds }; |
| 108 | |
| 109 | UEvent(UniqueFd &fd, UniqueFd &stop_event_fd) |
| 110 | : fd_(std::move(fd)), stop_event_fd_(std::move(stop_event_fd)) {}; |
| 111 | |
| 112 | // Returns true if there is data to be read off of fd_. |
| 113 | bool WaitForData() { |
| 114 | struct pollfd poll_fds[kNumFds]; |
| 115 | poll_fds[kFdIdx].fd = *fd_; |
| 116 | poll_fds[kFdIdx].events = POLLIN; |
| 117 | poll_fds[kStopEventFdIdx].fd = *stop_event_fd_; |
| 118 | poll_fds[kStopEventFdIdx].events = POLLIN; |
| 119 | |
| 120 | const int ret = poll(poll_fds, kNumFds, -1); |
| 121 | if (ret == 0) { |
| 122 | // Timeout shouldn't happen, but return here anyways. |
| 123 | ALOGE("Timed out polling uevent."); |
| 124 | return false; |
| 125 | } |
| 126 | if (ret < 1) { |
| 127 | ALOGE("Error polling uevent. errno: %d", errno); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if ((poll_fds[kStopEventFdIdx].revents & POLLIN) != 0) { |
| 132 | // Stop event has been signalled. Return without reading from the fd to |
| 133 | // ensure that this fd stays in a readable state. |
| 134 | ALOGI("Stop event signalled."); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // Return true if there is data to read. |
| 139 | return (poll_fds[kFdIdx].revents & POLLIN) != 0; |
| 140 | } |
| 141 | |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 142 | UniqueFd fd_; |
Drew Davenport | b852605 | 2024-08-28 10:24:33 -0600 | [diff] [blame] | 143 | UniqueFd stop_event_fd_; |
Roman Stratiienko | bd97317 | 2022-02-18 16:51:53 +0200 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | } // namespace android |