#include <systemd/sd-hwdb.h>
sd_hwdb_get()
sd_hwdb_seek() selects entries matching the specified string modalias. Subsequent queries with sd_hwdb_enumerate() will access the key-value pairs for that string.
sd_hwdb_enumerate() returns (in turn) all the key-value pairs defined for the string used with sd_hwdb_seek(). Each pair is returned as NUL-terminated strings in key and value. The strings must not be modified by the caller and are valid as long as a reference to hwdb is kept. When multiple patterns in the database match modalias, the combination of all matching key-value pairs is used. See hwdb(7) for details.
The SD_HWDB_FOREACH_PROPERTY() macro combines sd_hwdb_seek() and sd_hwdb_enumerate(). No error handling is performed and iteration simply stops on error. See the example below.
On success, sd_hwdb_get() and sd_hwdb_seek() return a non-negative integer. On failure, they return a negative errno-style error code.
sd_hwdb_enumerate() returns a positive integer if another key-value pair was found or zero if all entries have already been enumerated. On failure, it returns a negative errno-style error code.
Returned errors may indicate the following problems:
-EINVAL
-ENOENT
-EAGAIN
These APIs are implemented as a shared library, which can be compiled and linked to with the libsystemd pkg-config(1) file.
Example 1. Look up hwdb entries for a USB device
#include <stdio.h> #include <stdint.h> #include <sd-hwdb.h> int print_usb_properties(uint16_t vid, uint16_t pid) { char match[15]; sd_hwdb *hwdb; const char *key, *value; int r; /* Match this USB vendor and product ID combination */ snprintf(match, sizeof match, "usb:v%04Xp%04X", vid, pid); r = sd_hwdb_new(&hwdb); if (r < 0) return r; SD_HWDB_FOREACH_PROPERTY(hwdb, match, key, value) printf("%s: \"%s\" → \"%s\"\n", match, key, value); sd_hwdb_unref(hwdb); return 0; } int main(int argc, char **argv) { print_usb_properties(0x046D, 0xC534); return 0; }
The effect is similar to calling systemd-hwdb query usb:v046DpC534.
systemd(1), systemd-udevd.service(8), sd-hwdb(3), systemd-hwdb(8)