]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-hwdb.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / libudev / libudev-hwdb.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2001208c 2/***
8b516fde 3 Copyright Tom Gundersen <teg@jklm.no>
2001208c
KS
4***/
5
8b516fde 6#include "sd-hwdb.h"
07630cea 7
b5efdb8a 8#include "alloc-util.h"
8b516fde 9#include "hwdb-util.h"
07630cea 10#include "libudev-private.h"
2001208c
KS
11
12/**
13 * SECTION:libudev-hwdb
14 * @short_description: retrieve properties from the hardware database
15 *
f2d433e1 16 * Libudev hardware database interface.
2001208c
KS
17 */
18
19/**
20 * udev_hwdb:
21 *
22 * Opaque object representing the hardware database.
23 */
24struct udev_hwdb {
25 struct udev *udev;
26 int refcount;
27
8b516fde 28 sd_hwdb *hwdb;
2001208c
KS
29
30 struct udev_list properties_list;
31};
32
2001208c
KS
33/**
34 * udev_hwdb_new:
35 * @udev: udev library context
36 *
37 * Create a hardware database context to query properties for devices.
38 *
39 * Returns: a hwdb context.
40 **/
41_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
4afd3348 42 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
2001208c 43 struct udev_hwdb *hwdb;
8b516fde 44 int r;
2001208c 45
309f631d 46 assert_return_errno(udev, NULL, EINVAL);
33488f19 47
8b516fde 48 r = sd_hwdb_new(&hwdb_internal);
309f631d
LP
49 if (r < 0) {
50 errno = -r;
2001208c 51 return NULL;
309f631d 52 }
2001208c 53
8b516fde 54 hwdb = new0(struct udev_hwdb, 1);
309f631d
LP
55 if (!hwdb) {
56 errno = ENOMEM;
2001208c 57 return NULL;
309f631d 58 }
2001208c 59
8b516fde 60 hwdb->refcount = 1;
1cc6c93a 61 hwdb->hwdb = TAKE_PTR(hwdb_internal);
2001208c 62
8b516fde 63 udev_list_init(udev, &hwdb->properties_list, true);
2001208c 64
2001208c
KS
65 return hwdb;
66}
67
68/**
69 * udev_hwdb_ref:
70 * @hwdb: context
71 *
72 * Take a reference of a hwdb context.
73 *
74 * Returns: the passed enumeration context
75 **/
76_public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
77 if (!hwdb)
78 return NULL;
79 hwdb->refcount++;
80 return hwdb;
81}
82
83/**
84 * udev_hwdb_unref:
85 * @hwdb: context
86 *
87 * Drop a reference of a hwdb context. If the refcount reaches zero,
88 * all resources of the hwdb context will be released.
89 *
725d7e6c 90 * Returns: #NULL
2001208c
KS
91 **/
92_public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
93 if (!hwdb)
94 return NULL;
95 hwdb->refcount--;
96 if (hwdb->refcount > 0)
725d7e6c 97 return NULL;
8b516fde 98 sd_hwdb_unref(hwdb->hwdb);
2001208c 99 udev_list_cleanup(&hwdb->properties_list);
5fecf46d 100 return mfree(hwdb);
2001208c
KS
101}
102
2001208c
KS
103/**
104 * udev_hwdb_get_properties_list_entry:
105 * @hwdb: context
106 * @modalias: modalias string
107 * @flags: (unused)
108 *
109 * Lookup a matching device in the hardware database. The lookup key is a
110 * modalias string, whose formats are defined for the Linux kernel modules.
111 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
112 * of a list of retrieved properties is returned.
113 *
114 * Returns: a udev_list_entry.
115 */
116_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
8b516fde 117 const char *key, *value;
309f631d 118 struct udev_list_entry *e;
2001208c 119
8b516fde 120 if (!hwdb || !modalias) {
2001208c
KS
121 errno = EINVAL;
122 return NULL;
123 }
124
9485d98d 125 udev_list_cleanup(&hwdb->properties_list);
8b516fde 126
0b931571 127 SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) {
8b516fde
TG
128 if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) {
129 errno = ENOMEM;
130 return NULL;
131 }
2001208c 132 }
8b516fde 133
309f631d
LP
134 e = udev_list_get_entry(&hwdb->properties_list);
135 if (!e)
136 errno = ENODATA;
137
138 return e;
2001208c 139}