|
1 | 1 | #pragma once |
| 2 | + |
| 3 | +#include <atomic> |
| 4 | +#include <cerrno> |
| 5 | +#include <cctype> |
| 6 | +#include <cstddef> |
| 7 | +#include <cstdint> |
| 8 | +#include <fstream> |
| 9 | +#include <functional> |
| 10 | +#include <limits> |
| 11 | +#include <poll.h> |
| 12 | +#include <sstream> |
2 | 13 | #include <string> |
3 | 14 | #include <string_view> |
4 | | -#include <vector> |
| 15 | +#include <sys/inotify.h> |
5 | 16 | #include <unordered_map> |
6 | 17 | #include <unordered_set> |
7 | | -#include <fstream> |
8 | | -#include <sstream> |
9 | | -#include <cctype> |
| 18 | +#include <unistd.h> |
| 19 | +#include <utility> |
| 20 | +#include <vector> |
10 | 21 |
|
11 | 22 | namespace android_pkgmap { |
12 | 23 |
|
@@ -55,6 +66,20 @@ namespace detail { |
55 | 66 | return true; |
56 | 67 | } |
57 | 68 |
|
| 69 | + inline bool split_path(const std::string& path, |
| 70 | + std::string& out_directory, |
| 71 | + std::string& out_filename) { |
| 72 | + const std::size_t slash = path.find_last_of('/'); |
| 73 | + |
| 74 | + if (slash == std::string::npos || slash + 1 >= path.size()) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + out_directory = slash == 0 ? "/" : path.substr(0, slash); |
| 79 | + out_filename = path.substr(slash + 1); |
| 80 | + |
| 81 | + return !out_filename.empty(); |
| 82 | + } |
58 | 83 | } |
59 | 84 |
|
60 | 85 | /** |
@@ -162,4 +187,173 @@ inline uint32_t get_uid_for_package(const std::string& pkg, |
162 | 187 | return 0; |
163 | 188 | } |
164 | 189 |
|
| 190 | +/** |
| 191 | + * 最小化监听 packages.list 是否发生变化。 |
| 192 | + * |
| 193 | + * 这里只负责通知“包列表发生过变化”,不读取文件、不比较内容, |
| 194 | + * 也不区分安装、卸载、更新或 UID 变化。 |
| 195 | + * |
| 196 | + * 监听目录而不是直接监听 packages.list,是因为 Android 通常先写 |
| 197 | + * 临时文件,再通过 rename 覆盖 packages.list。直接监听文件 inode |
| 198 | + * 在文件被替换后可能失效。 |
| 199 | + * |
| 200 | + * 监听事件: |
| 201 | + * IN_MOVED_TO 临时文件 rename 为 packages.list |
| 202 | + * IN_CLOSE_WRITE 某些厂商直接写 packages.list 后关闭 |
| 203 | + * |
| 204 | + * @param callback 检测到变化时调用 |
| 205 | + * @param stop_requested 外部停止标志 |
| 206 | + * @param path packages.list 完整路径 |
| 207 | + * @param stop_poll_ms 检查停止标志的最长间隔,默认 500ms |
| 208 | + * |
| 209 | + * @return 正常停止返回 true;初始化或监听失败返回 false。 |
| 210 | + */ |
| 211 | +inline bool watch_packages_list_changed( |
| 212 | + const std::function<void()>& callback, |
| 213 | + const std::atomic_bool& stop_requested, |
| 214 | + const std::string& path = "/data/system/packages.list", |
| 215 | + int stop_poll_ms = 500) { |
| 216 | + if (!callback) { |
| 217 | + return false; |
| 218 | + } |
| 219 | + |
| 220 | + std::string directory; |
| 221 | + std::string filename; |
| 222 | + |
| 223 | + if (!detail::split_path(path, directory, filename)) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + const int inotify_fd = |
| 228 | + ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 229 | + |
| 230 | + if (inotify_fd < 0) { |
| 231 | + return false; |
| 232 | + } |
| 233 | + |
| 234 | + constexpr std::uint32_t kWatchMask = |
| 235 | + IN_MOVED_TO | |
| 236 | + IN_CLOSE_WRITE; |
| 237 | + |
| 238 | + const int watch_descriptor = |
| 239 | + ::inotify_add_watch( |
| 240 | + inotify_fd, |
| 241 | + directory.c_str(), |
| 242 | + kWatchMask); |
| 243 | + |
| 244 | + if (watch_descriptor < 0) { |
| 245 | + ::close(inotify_fd); |
| 246 | + return false; |
| 247 | + } |
| 248 | + |
| 249 | + if (stop_poll_ms < 1) { |
| 250 | + stop_poll_ms = 1; |
| 251 | + } |
| 252 | + |
| 253 | + pollfd poll_fd{ |
| 254 | + .fd = inotify_fd, |
| 255 | + .events = POLLIN, |
| 256 | + .revents = 0, |
| 257 | + }; |
| 258 | + |
| 259 | + alignas(inotify_event) char buffer[4096]; |
| 260 | + bool success = true; |
| 261 | + |
| 262 | + while (!stop_requested.load(std::memory_order_relaxed)) { |
| 263 | + poll_fd.revents = 0; |
| 264 | + |
| 265 | + const int poll_result = |
| 266 | + ::poll(&poll_fd, 1, stop_poll_ms); |
| 267 | + |
| 268 | + if (poll_result < 0) { |
| 269 | + if (errno == EINTR) { |
| 270 | + continue; |
| 271 | + } |
| 272 | + |
| 273 | + success = false; |
| 274 | + break; |
| 275 | + } |
| 276 | + |
| 277 | + if (poll_result == 0) { |
| 278 | + continue; |
| 279 | + } |
| 280 | + |
| 281 | + if ((poll_fd.revents & |
| 282 | + (POLLERR | POLLHUP | POLLNVAL)) != 0) { |
| 283 | + success = false; |
| 284 | + break; |
| 285 | + } |
| 286 | + |
| 287 | + if ((poll_fd.revents & POLLIN) == 0) { |
| 288 | + continue; |
| 289 | + } |
| 290 | + |
| 291 | + for (;;) { |
| 292 | + const ssize_t bytes_read = |
| 293 | + ::read(inotify_fd, buffer, sizeof(buffer)); |
| 294 | + |
| 295 | + if (bytes_read < 0) { |
| 296 | + if (errno == EINTR) { |
| 297 | + continue; |
| 298 | + } |
| 299 | + |
| 300 | + if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 301 | + break; |
| 302 | + } |
| 303 | + |
| 304 | + success = false; |
| 305 | + break; |
| 306 | + } |
| 307 | + |
| 308 | + if (bytes_read == 0) { |
| 309 | + success = false; |
| 310 | + break; |
| 311 | + } |
| 312 | + |
| 313 | + std::size_t offset = 0; |
| 314 | + |
| 315 | + while (offset + sizeof(inotify_event) <= |
| 316 | + static_cast<std::size_t>(bytes_read)) { |
| 317 | + const auto* event = |
| 318 | + reinterpret_cast<const inotify_event*>( |
| 319 | + buffer + offset); |
| 320 | + |
| 321 | + const std::size_t event_size = |
| 322 | + sizeof(inotify_event) + event->len; |
| 323 | + |
| 324 | + if (offset + event_size > |
| 325 | + static_cast<std::size_t>(bytes_read)) { |
| 326 | + success = false; |
| 327 | + break; |
| 328 | + } |
| 329 | + |
| 330 | + bool changed = false; |
| 331 | + |
| 332 | + if ((event->mask & IN_Q_OVERFLOW) != 0) { |
| 333 | + changed = true; |
| 334 | + } else if (event->len > 0 && |
| 335 | + std::string_view(event->name) == filename && |
| 336 | + (event->mask & |
| 337 | + (IN_MOVED_TO | IN_CLOSE_WRITE)) != 0) { |
| 338 | + changed = true; |
| 339 | + } |
| 340 | + |
| 341 | + if (changed) { |
| 342 | + callback(); |
| 343 | + } |
| 344 | + |
| 345 | + offset += event_size; |
| 346 | + } |
| 347 | + |
| 348 | + if (!success) { |
| 349 | + break; |
| 350 | + } |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + ::inotify_rm_watch(inotify_fd, watch_descriptor); |
| 355 | + ::close(inotify_fd); |
| 356 | + |
| 357 | + return success; |
| 358 | +} |
165 | 359 | } // namespace android_pkgmap |