summaryrefslogtreecommitdiffstats
path: root/upstream/fedora-rawhide/man3/sd_hwdb_get.3
blob: 2a6d51ee0dc19ec345db5d0bcdffd04cab65c564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'\" t
.TH "SD_HWDB_GET" "3" "" "systemd 255" "sd_hwdb_get"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
sd_hwdb_get, sd_hwdb_seek, sd_hwdb_enumerate, SD_HWDB_FOREACH_PROPERTY \- Seek to a location in hwdb or access entries
.SH "SYNOPSIS"
.sp
.ft B
.nf
#include <systemd/sd\-hwdb\&.h>
.fi
.ft
.HP \w'int\ sd_hwdb_get('u
.BI "int sd_hwdb_get(sd_hwdb\ *" "hwdb" ", const\ char\ *" "modalias" ", const\ char\ *" "key" ", const\ char\ **" "value" ");"
.HP \w'int\ sd_hwdb_seek('u
.BI "int sd_hwdb_seek(sd_hwdb\ *" "hwdb" ", const\ char\ *" "modalias" ");"
.HP \w'int\ sd_hwdb_enumerate('u
.BI "int sd_hwdb_enumerate(sd_hwdb\ *" "hwdb" ", const\ char\ **" "key" ", const\ char\ **" "value" ");"
.HP \w'SD_HWDB_FOREACH_PROPERTY('u
.BI "SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value);"
.SH "DESCRIPTION"
.PP
\fBsd_hwdb_get()\fR
queries the
\fIhwdb\fR
object created earlier with
\fBsd_hwdb_new\fR(3)
for entries matching the specified string
\fImodalias\fR, and returns the value corresponding to the key
\fIkey\fR\&. The value is returned as a
\fBNUL\fR\-terminated string in
\fIvalue\fR\&. It must not be modified by the caller and is valid as long as a reference to
\fIhwdb\fR
is kept\&. When multiple patterns in the database match
\fImodalias\fR, the one with the highest priority is used\&. See
\fBhwdb\fR(7)
for details\&.
.PP
\fBsd_hwdb_seek()\fR
selects entries matching the specified string
\fImodalias\fR\&. Subsequent queries with
\fBsd_hwdb_enumerate()\fR
will access the key\-value pairs for that string\&.
.PP
\fBsd_hwdb_enumerate()\fR
returns (in turn) all the key\-value pairs defined for the string used with
\fBsd_hwdb_seek()\fR\&. Each pair is returned as
\fBNUL\fR\-terminated strings in
\fIkey\fR
and
\fIvalue\fR\&. The strings must not be modified by the caller and are valid as long as a reference to
\fIhwdb\fR
is kept\&. When multiple patterns in the database match
\fImodalias\fR, the combination of all matching key\-value pairs is used\&. See
\fBhwdb\fR(7)
for details\&.
.PP
The
\fBSD_HWDB_FOREACH_PROPERTY()\fR
macro combines
\fBsd_hwdb_seek()\fR
and
\fBsd_hwdb_enumerate()\fR\&. No error handling is performed and iteration simply stops on error\&. See the example below\&.
.SH "RETURN VALUE"
.PP
On success,
\fBsd_hwdb_get()\fR
and
\fBsd_hwdb_seek()\fR
return a non\-negative integer\&. On failure, they return a negative errno\-style error code\&.
.PP
\fBsd_hwdb_enumerate()\fR
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\&.
.SS "Errors"
.PP
Returned errors may indicate the following problems:
.PP
\fB\-EINVAL\fR
.RS 4
A parameter is
\fBNULL\fR\&.
.sp
Added in version 246\&.
.RE
.PP
\fB\-ENOENT\fR
.RS 4
An entry for the specified
\fImodalias\fR
was not found\&.
.sp
Added in version 246\&.
.RE
.PP
\fB\-EAGAIN\fR
.RS 4
\fBsd_hwdb_seek()\fR
was not called before
\fBsd_hwdb_enumerate()\fR\&.
.sp
Added in version 246\&.
.RE
.SH "NOTES"
.PP
Functions described here are available as a shared library, which can be compiled against and linked to with the
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
file\&.
.PP
The code described here uses
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call
\fBsetenv\fR(3)
from a parallel thread\&. It is recommended to only do calls to
\fBsetenv()\fR
from an early phase of the program when no other threads have been started\&.
.SH "EXAMPLES"
.PP
\fBExample\ \&1.\ \&Look up hwdb entries for a USB device\fR
.sp
.if n \{\
.RS 4
.\}
.nf
/* SPDX\-License\-Identifier: MIT\-0 */

#include <stdio\&.h>
#include <stdint\&.h>
#include <sd\-hwdb\&.h>

int print_usb_properties(uint16_t vid, uint16_t pid) {
  char match[STRLEN("usb:vp") + DECIMAL_STR_MAX(uint16_t) * 2];
  sd_hwdb *hwdb;
  const char *key, *value;
  int r;

  /* Match this USB vendor and product ID combination */
  xsprintf(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: \e"%s\e" → \e"%s\e"\en", match, key, value);

  sd_hwdb_unref(hwdb);
  return 0;
}

int main(int argc, char **argv) {
  print_usb_properties(0x046D, 0xC534);
  return 0;
}
.fi
.if n \{\
.RE
.\}
.PP
The effect is similar to calling
\fBsystemd\-hwdb query usb:v046DpC534\fR\&.
.SH "HISTORY"
.PP
\fBsd_hwdb_get()\fR,
\fBsd_hwdb_seek()\fR,
\fBsd_hwdb_enumerate()\fR, and
\fBSD_HWDB_FOREACH_PROPERTY()\fR
were added in version 246\&.
.SH "SEE ALSO"
.PP
\fBsystemd\fR(1),
\fBsystemd-udevd.service\fR(8),
\fBsd-hwdb\fR(3),
\fBsystemd-hwdb\fR(8)