Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let libudev find hwdb.bin under UDEV_HWDB_BIN #264

Merged
merged 2 commits into from Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion man/udev.7
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ The content of all hwdb files is read by
and compiled to a binary database located at
/etc/udev/hwdb\&.bin, or alternatively
/usr/lib/udev/hwdb\&.bin
if you want ship the compiled database in an immutable image\&. During runtime only the binary database is used\&.
if you want ship the compiled database in an immutable image\&. If
\fBUDEV_HWDB_BIN\fR
is set at run\-time, and its value identifies a file in the file system, then the binary database located under this name will be used\&. During runtime only the binary database is used\&.
.SH "SEE ALSO"
.PP
\fBudevd\fR(8),
Expand Down
4 changes: 3 additions & 1 deletion man/udev.xml
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,9 @@
<citerefentry><refentrytitle>udevadm</refentrytitle><manvolnum>8</manvolnum></citerefentry>
and compiled to a binary database located at <filename>/etc/udev/hwdb.bin</filename>,
or alternatively <filename>/usr/lib/udev/hwdb.bin</filename> if you want ship the compiled
database in an immutable image.
database in an immutable image. If <envar>UDEV_HWDB_BIN</envar>
is set at run-time, and its value identifies a file in the file system, then the binary
database located under this name will be used.
During runtime only the binary database is used.</para>
</refsect1>

Expand Down
37 changes: 31 additions & 6 deletions src/libudev/libudev-hwdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct udev_hwdb {
struct udev *udev;
int refcount;

char *bin_paths;
FILE *f;
struct stat st;
union {
Expand Down Expand Up @@ -256,10 +257,28 @@ static int trie_search_f(struct udev_hwdb *hwdb, const char *search) {
return 0;
}

static const char hwdb_bin_paths[] =
"/etc/udev/hwdb.bin\0"
UDEV_LIBEXEC_DIR "/hwdb.bin\0";

static char *get_hwdb_bin_paths (void) {
static const char default_locations[] =
"/etc/udev/hwdb.bin\0"
UDEV_LIBEXEC_DIR "/hwdb.bin\0";
const char *by_env = getenv("UDEV_HWDB_BIN");
if (by_env != NULL) {
char *path = malloc(strlen(by_env) + 1
+ sizeof (default_locations));
if (path != NULL) {
memcpy(path, by_env, strlen(by_env) + 1);
memcpy(path + strlen(by_env) + 1,
default_locations,
sizeof (default_locations));
}
return path;
}
char *path = malloc(sizeof (default_locations));
if (path != NULL) {
memcpy(path, default_locations, sizeof (default_locations));
}
return path;
}

/**
* udev_hwdb_new:
Expand All @@ -282,7 +301,12 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
udev_list_init(udev, &hwdb->properties_list, true);

/* find hwdb.bin in hwdb_bin_paths */
NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
hwdb->bin_paths = get_hwdb_bin_paths();
if (hwdb->bin_paths == NULL) {
udev_hwdb_unref(hwdb);
return NULL;
}
NULSTR_FOREACH(hwdb_bin_path, hwdb->bin_paths) {
hwdb->f = fopen(hwdb_bin_path, "re");
if (hwdb->f)
break;
Expand Down Expand Up @@ -363,6 +387,7 @@ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
return NULL;
if (hwdb->map)
munmap((void *)hwdb->map, hwdb->st.st_size);
free(hwdb->bin_paths);
if (hwdb->f)
fclose(hwdb->f);
udev_list_cleanup(&hwdb->properties_list);
Expand All @@ -381,7 +406,7 @@ bool udev_hwdb_validate(struct udev_hwdb *hwdb) {
return false;

/* if hwdb.bin doesn't exist anywhere, we need to update */
NULSTR_FOREACH(p, hwdb_bin_paths) {
NULSTR_FOREACH(p, hwdb->bin_paths) {
if (stat(p, &st) >= 0) {
found = true;
break;
Expand Down
4 changes: 3 additions & 1 deletion src/libudev/libudev.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev
/*
* udev_hwdb
*
* access to the static hardware properties database
* access to the static hardware properties database; the database to
* use can be overriden by setting the UDEV_HWDB_BIN environment
* variable to its file name
*/
struct udev_hwdb;
struct udev_hwdb *udev_hwdb_new(struct udev *udev);
Expand Down
Loading