How cr8t/udev works
Project Type and Purpose
• This is a Rust library (crate) that provides a safe, idiomatic Rust API for interacting with Linux’s devfs (udev), mirroring the public API of the C eudev/libudev library.
• It is an open-source tool (library) rather than a stand-alone application. Consumers link against it to enumerate devices, monitor udev events, query the hardware database (hwdb), and queue device events.
High-Level Architectural Components
a. Public API Layer (src/lib.rs)
• Re-exports and ties together the submodules under a unified, C-compatible-like API.
b. Context Management (src/context.rs)
• Udev context object: holds configuration and references needed for all subsystems.
c. Device Operations
• Enumeration (src/enumerate.rs → UdevEnumerate)
• Listing (src/list.rs → UdevList)
• Monitoring (src/monitor.rs → UdevMonitor)
• Queueing (src/queue.rs → UdevQueue)
d. Hardware Database (src/hwdb.rs + src/hwdb/)
• Loads on-disk hwdb files, parses lines (hwdb/line.rs), builds a compressed trie (hwdb/trie/) for fast lookups.
e. System Interaction Layer
• Netlink sockets (src/socket.rs) for events/enumeration
• File I/O (src/file.rs) for reading hwdb and sysfs nodes
• Utility modules (src/util/, src/murmur_hash.rs) for hashing, path handling, device node resolution
f. Cross-cutting Concerns
• Error handling (src/error.rs)
• Logging (src/log.rs)
• Modes/flags (src/mode.rs)
Relationships and Data Flow
• Consumer (application) → Public API → Context
• Context → opens netlink socket (socket module), file handles (file module)
• Enumeration/List → sends netlink requests → kernel → parses replies → returns UdevDevice objects
• Monitor → binds to socket → listens for events → wraps raw messages into UdevDevice events
• Queue → receives events from kernel → allows user to dequeue them
• Hwdb → on first query or at init, loads hwdb files into Trie structure → queries by device ID → returns metadata
• Utility modules and murmur-hash are invoked by hwdb loader and trie builder
Architectural Patterns and Principles
• Modularization: each udev subsystem lives in its own module, exposed via a flat public API
• Safe abstractions over unsafe blocks: low-level OS interactions confined to small modules with documented SAFETY sections
• Data-structure specialization: compressed trie for hwdb with child_entry, header, node, value_entry components
• Separation of concerns: socket I/O, file I/O, data parsing, and API layering are distinct