blob: 27c5d23740614223461bf3d09fd6ed7afe96330f [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
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#include "uevent_listener.h"
18
19#include <fcntl.h>
20#include <poll.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <memory>
25
26#include <android-base/logging.h>
27#include <cutils/uevent.h>
28
29static void ParseEvent(const char* msg, Uevent* uevent) {
30 uevent->partition_num = -1;
31 uevent->major = -1;
32 uevent->minor = -1;
33 uevent->action.clear();
34 uevent->path.clear();
35 uevent->subsystem.clear();
36 uevent->firmware.clear();
37 uevent->partition_name.clear();
38 uevent->device_name.clear();
39 // currently ignoring SEQNUM
40 while (*msg) {
41 if (!strncmp(msg, "ACTION=", 7)) {
42 msg += 7;
43 uevent->action = msg;
44 } else if (!strncmp(msg, "DEVPATH=", 8)) {
45 msg += 8;
46 uevent->path = msg;
47 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
48 msg += 10;
49 uevent->subsystem = msg;
50 } else if (!strncmp(msg, "FIRMWARE=", 9)) {
51 msg += 9;
52 uevent->firmware = msg;
53 } else if (!strncmp(msg, "MAJOR=", 6)) {
54 msg += 6;
55 uevent->major = atoi(msg);
56 } else if (!strncmp(msg, "MINOR=", 6)) {
57 msg += 6;
58 uevent->minor = atoi(msg);
59 } else if (!strncmp(msg, "PARTN=", 6)) {
60 msg += 6;
61 uevent->partition_num = atoi(msg);
62 } else if (!strncmp(msg, "PARTNAME=", 9)) {
63 msg += 9;
64 uevent->partition_name = msg;
65 } else if (!strncmp(msg, "DEVNAME=", 8)) {
66 msg += 8;
67 uevent->device_name = msg;
68 }
69
70 // advance to after the next \0
71 while (*msg++)
72 ;
73 }
74
75 if (LOG_UEVENTS) {
76 LOG(INFO) << "event { '" << uevent->action << "', '" << uevent->path << "', '"
77 << uevent->subsystem << "', '" << uevent->firmware << "', " << uevent->major
78 << ", " << uevent->minor << " }";
79 }
80}
81
82UeventListener::UeventListener() {
83 // is 256K enough? udev uses 16MB!
84 device_fd_.reset(uevent_open_socket(256 * 1024, true));
85 if (device_fd_ == -1) {
86 LOG(FATAL) << "Could not open uevent socket";
87 }
88
89 fcntl(device_fd_, F_SETFL, O_NONBLOCK);
90}
91
92bool UeventListener::ReadUevent(Uevent* uevent) const {
93 char msg[UEVENT_MSG_LEN + 2];
94 int n = uevent_kernel_multicast_recv(device_fd_, msg, UEVENT_MSG_LEN);
95 if (n <= 0) {
96 if (errno != EAGAIN && errno != EWOULDBLOCK) {
97 LOG(ERROR) << "Error reading from Uevent Fd";
98 }
99 return false;
100 }
101 if (n >= UEVENT_MSG_LEN) {
102 LOG(ERROR) << "Uevent overflowed buffer, discarding";
103 // Return true here even if we discard as we may have more uevents pending and we
104 // want to keep processing them.
105 return true;
106 }
107
108 msg[n] = '\0';
109 msg[n + 1] = '\0';
110
111 ParseEvent(msg, uevent);
112
113 return true;
114}
115
116// RegenerateUevents*() walks parts of the /sys tree and pokes the uevent files to cause the kernel
117// to regenerate device add uevents that have already happened. This is particularly useful when
118// starting ueventd, to regenerate all of the uevents that it had previously missed.
119//
120// We drain any pending events from the netlink socket every time we poke another uevent file to
121// make sure we don't overrun the socket's buffer.
122//
123
124RegenerationAction UeventListener::RegenerateUeventsForDir(DIR* d,
125 RegenerateCallback callback) const {
126 int dfd = dirfd(d);
127
128 int fd = openat(dfd, "uevent", O_WRONLY);
129 if (fd >= 0) {
130 write(fd, "add\n", 4);
131 close(fd);
132
133 Uevent uevent;
134 while (ReadUevent(&uevent)) {
135 if (callback(uevent) == RegenerationAction::kStop) return RegenerationAction::kStop;
136 }
137 }
138
139 dirent* de;
140 while ((de = readdir(d)) != nullptr) {
141 if (de->d_type != DT_DIR || de->d_name[0] == '.') continue;
142
143 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
144 if (fd < 0) continue;
145
146 std::unique_ptr<DIR, decltype(&closedir)> d2(fdopendir(fd), closedir);
147 if (d2 == 0) {
148 close(fd);
149 } else {
150 if (RegenerateUeventsForDir(d2.get(), callback) == RegenerationAction::kStop) {
151 return RegenerationAction::kStop;
152 }
153 }
154 }
155
156 // default is always to continue looking for uevents
157 return RegenerationAction::kContinue;
158}
159
160RegenerationAction UeventListener::RegenerateUeventsForPath(const std::string& path,
161 RegenerateCallback callback) const {
162 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
163 if (!d) return RegenerationAction::kContinue;
164
165 return RegenerateUeventsForDir(d.get(), callback);
166}
167
168static const char* kRegenerationPaths[] = {"/sys/class", "/sys/block", "/sys/devices"};
169
170void UeventListener::RegenerateUevents(RegenerateCallback callback) const {
171 for (const auto path : kRegenerationPaths) {
172 if (RegenerateUeventsForPath(path, callback) == RegenerationAction::kStop) return;
173 }
174}
175
176void UeventListener::DoPolling(PollCallback callback) const {
177 pollfd ufd;
178 ufd.events = POLLIN;
179 ufd.fd = device_fd_;
180
181 while (true) {
182 ufd.revents = 0;
183 int nr = poll(&ufd, 1, -1);
184 if (nr <= 0) {
185 continue;
186 }
187 if (ufd.revents & POLLIN) {
188 // We're non-blocking, so if we receive a poll event keep processing until there
189 // we have exhausted all uevent messages.
190 Uevent uevent;
191 while (ReadUevent(&uevent)) {
192 callback(uevent);
193 }
194 }
195 }
196}