]>
Commit | Line | Data |
---|---|---|
12355095 | 1 | <?xml version='1.0'?> <!--*-nxml-*--> |
3a54a157 | 2 | <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
eea10b26 | 3 | "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
db9ecf05 | 4 | <!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
12355095 | 5 | |
6a70f3aa | 6 | <refentry id="sd-id128" |
798d3a52 ZJS |
7 | xmlns:xi="http://www.w3.org/2001/XInclude"> |
8 | ||
9 | <refentryinfo> | |
10 | <title>sd-id128</title> | |
11 | <productname>systemd</productname> | |
798d3a52 ZJS |
12 | </refentryinfo> |
13 | ||
14 | <refmeta> | |
15 | <refentrytitle>sd-id128</refentrytitle> | |
16 | <manvolnum>3</manvolnum> | |
17 | </refmeta> | |
18 | ||
19 | <refnamediv> | |
20 | <refname>sd-id128</refname> | |
78aa5b6f ZJS |
21 | <refname>SD_ID128_ALLF</refname> |
22 | <refname>SD_ID128_CONST_STR</refname> | |
23 | <refname>SD_ID128_FORMAT_STR</refname> | |
24 | <refname>SD_ID128_FORMAT_VAL</refname> | |
798d3a52 | 25 | <refname>SD_ID128_MAKE</refname> |
2b044526 | 26 | <refname>SD_ID128_MAKE_STR</refname> |
7c7683f3 | 27 | <refname>SD_ID128_MAKE_UUID_STR</refname> |
3dbea941 | 28 | <refname>SD_ID128_NULL</refname> |
38df8d3f | 29 | <refname>SD_ID128_UUID_FORMAT_STR</refname> |
798d3a52 | 30 | <refname>sd_id128_equal</refname> |
944c1243 | 31 | <refname>sd_id128_string_equal</refname> |
64b21afc ZJS |
32 | <refname>sd_id128_in_set</refname> |
33 | <refname>sd_id128_in_set_sentinel</refname> | |
34 | <refname>sd_id128_in_setv</refname> | |
78aa5b6f | 35 | <refname>sd_id128_is_allf</refname> |
3dbea941 | 36 | <refname>sd_id128_is_null</refname> |
78aa5b6f | 37 | <refname>sd_id128_t</refname> |
798d3a52 ZJS |
38 | <refpurpose>APIs for processing 128-bit IDs</refpurpose> |
39 | </refnamediv> | |
40 | ||
41 | <refsynopsisdiv> | |
42 | <funcsynopsis> | |
43 | <funcsynopsisinfo>#include <systemd/sd-id128.h></funcsynopsisinfo> | |
996de339 | 44 | </funcsynopsis> |
d13f1051 | 45 | |
996de339 DT |
46 | <para> |
47 | <constant>SD_ID128_ALLF</constant> | |
48 | </para> | |
49 | <para> | |
50 | <constant>SD_ID128_NULL</constant> | |
51 | </para> | |
52 | <para> | |
53 | <constant>SD_ID128_CONST_STR(<replaceable>id</replaceable>)</constant> | |
54 | </para> | |
55 | <para> | |
56 | <constant>SD_ID128_FORMAT_STR</constant> | |
57 | </para> | |
58 | <para> | |
59 | <constant>SD_ID128_FORMAT_VAL(<replaceable>id</replaceable>)</constant> | |
60 | </para> | |
61 | <para> | |
62 | <constant>SD_ID128_MAKE(<replaceable>v0</replaceable>, <replaceable>v1</replaceable>, <replaceable>v2</replaceable>, <replaceable>v3</replaceable>, <replaceable>v4</replaceable>, <replaceable>v5</replaceable>, <replaceable>v6</replaceable>, <replaceable>v7</replaceable>, <replaceable>v8</replaceable>, <replaceable>v9</replaceable>, <replaceable>vA</replaceable>, <replaceable>vB</replaceable>, <replaceable>vC</replaceable>, <replaceable>vD</replaceable>, <replaceable>vE</replaceable>, <replaceable>vF</replaceable>)</constant> | |
63 | </para> | |
64 | <para> | |
65 | <constant>SD_ID128_MAKE_STR(<replaceable>v0</replaceable>, <replaceable>v1</replaceable>, <replaceable>v2</replaceable>, <replaceable>v3</replaceable>, <replaceable>v4</replaceable>, <replaceable>v5</replaceable>, <replaceable>v6</replaceable>, <replaceable>v7</replaceable>, <replaceable>v8</replaceable>, <replaceable>v9</replaceable>, <replaceable>vA</replaceable>, <replaceable>vB</replaceable>, <replaceable>vC</replaceable>, <replaceable>vD</replaceable>, <replaceable>vE</replaceable>, <replaceable>vF</replaceable>)</constant> | |
66 | </para> | |
67 | <para> | |
68 | <constant>SD_ID128_MAKE_UUID_STR(<replaceable>v0</replaceable>, <replaceable>v1</replaceable>, <replaceable>v2</replaceable>, <replaceable>v3</replaceable>, <replaceable>v4</replaceable>, <replaceable>v5</replaceable>, <replaceable>v6</replaceable>, <replaceable>v7</replaceable>, <replaceable>v8</replaceable>, <replaceable>v9</replaceable>, <replaceable>vA</replaceable>, <replaceable>vB</replaceable>, <replaceable>vC</replaceable>, <replaceable>vD</replaceable>, <replaceable>vE</replaceable>, <replaceable>vF</replaceable>)</constant> | |
69 | </para> | |
70 | <para> | |
71 | <constant>SD_ID128_UUID_FORMAT_STR</constant> | |
72 | </para> | |
d13f1051 | 73 | |
996de339 | 74 | <funcsynopsis> |
d13f1051 ZJS |
75 | <funcprototype> |
76 | <funcdef>int <function>sd_id128_equal</function></funcdef> | |
77 | <paramdef>sd_id128_t <parameter>a</parameter></paramdef> | |
78 | <paramdef>sd_id128_t <parameter>b</parameter></paramdef> | |
79 | </funcprototype> | |
80 | ||
944c1243 ZJS |
81 | <funcprototype> |
82 | <funcdef>int <function>sd_id128_string_equal</function></funcdef> | |
83 | <paramdef>const char *<parameter>a</parameter></paramdef> | |
84 | <paramdef>sd_id128_t <parameter>b</parameter></paramdef> | |
85 | </funcprototype> | |
86 | ||
d13f1051 ZJS |
87 | <funcprototype> |
88 | <funcdef>int <function>sd_id128_is_null</function></funcdef> | |
89 | <paramdef>sd_id128_t <parameter>id</parameter></paramdef> | |
90 | </funcprototype> | |
91 | ||
92 | <funcprototype> | |
93 | <funcdef>int <function>sd_id128_is_allf</function></funcdef> | |
94 | <paramdef>sd_id128_t <parameter>id</parameter></paramdef> | |
95 | </funcprototype> | |
96 | ||
97 | <funcprototype> | |
98 | <funcdef>int <function>sd_id128_in_setv</function></funcdef> | |
99 | <paramdef>sd_id128_t <parameter>id</parameter></paramdef> | |
100 | <paramdef>va_list <parameter>ap</parameter></paramdef> | |
101 | </funcprototype> | |
102 | ||
103 | <funcprototype> | |
104 | <funcdef>int <function>sd_id128_in_set_sentinel</function></funcdef> | |
105 | <paramdef>sd_id128_t <parameter>id</parameter></paramdef> | |
106 | <paramdef>…</paramdef> | |
3c2b711f | 107 | <paramdef><parameter><constant>SD_ID128_NULL</constant></parameter></paramdef> |
d13f1051 ZJS |
108 | </funcprototype> |
109 | ||
110 | <funcprototype> | |
111 | <funcdef>int <function>sd_id128_in_set</function></funcdef> | |
112 | <paramdef>sd_id128_t <parameter>id</parameter></paramdef> | |
113 | <paramdef>…</paramdef> | |
114 | </funcprototype> | |
798d3a52 ZJS |
115 | </funcsynopsis> |
116 | ||
117 | <cmdsynopsis> | |
118 | <command>pkg-config --cflags --libs libsystemd</command> | |
119 | </cmdsynopsis> | |
120 | ||
121 | </refsynopsisdiv> | |
122 | ||
123 | <refsect1> | |
124 | <title>Description</title> | |
125 | ||
4bc96dc1 ZJS |
126 | <para><filename>sd-id128.h</filename> is part of |
127 | <citerefentry><refentrytitle>libsystemd</refentrytitle><manvolnum>3</manvolnum></citerefentry> and | |
128 | provides APIs to generate, convert, and compare 128-bit ID values. The 128-bit ID values processed and | |
129 | generated by these APIs are a generalization of OSF UUIDs as defined by <ulink | |
130 | url="https://tools.ietf.org/html/rfc4122">RFC 4122</ulink> but use a simpler string format. These | |
131 | functions impose no structure on the used IDs, much unlike OSF UUIDs or Microsoft GUIDs, but are mostly | |
132 | compatible with those types of IDs. | |
798d3a52 ZJS |
133 | </para> |
134 | ||
798d3a52 ZJS |
135 | <para>A 128-bit ID is implemented as the following |
136 | union type:</para> | |
137 | ||
138 | <programlisting>typedef union sd_id128 { | |
e0a41aa4 ZJS |
139 | uint8_t bytes[16]; |
140 | uint64_t qwords[2]; | |
12355095 LP |
141 | } sd_id128_t;</programlisting> |
142 | ||
d13f1051 ZJS |
143 | <para>This union type allows accessing the 128-bit ID as 16 separate bytes or two 64-bit words. It is |
144 | generally safer to access the ID components by their 8-bit array to avoid endianness issues. This union | |
145 | is intended to be passed by value (as opposed to pass-by-reference) and may be directly manipulated by | |
798d3a52 ZJS |
146 | clients.</para> |
147 | ||
148 | <para>A couple of macros are defined to denote and decode 128-bit | |
149 | IDs:</para> | |
150 | ||
d13f1051 ZJS |
151 | <para><function>SD_ID128_MAKE()</function> is used to write a constant ID in source code. A commonly used |
152 | idiom is to assign a name to an ID using this macro:</para> | |
798d3a52 ZJS |
153 | |
154 | <programlisting>#define SD_MESSAGE_COREDUMP SD_ID128_MAKE(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)</programlisting> | |
155 | ||
d13f1051 ZJS |
156 | <para><constant>SD_ID128_NULL</constant> defines an ID consisting of only <constant>NUL</constant> bytes |
157 | (i.e. all bits off).</para> | |
7f3c90ed | 158 | |
d13f1051 ZJS |
159 | <para><constant>SD_ID128_ALLF</constant> defines an ID consisting of only <constant>0xFF</constant> bytes |
160 | (i.e. all bits on).</para> | |
3dbea941 | 161 | |
d13f1051 ZJS |
162 | <para><function>SD_ID128_MAKE_STR()</function> is similar to <function>SD_ID128_MAKE()</function>, but |
163 | creates a <type>const char*</type> expression that can be conveniently used in message formats and | |
164 | such:</para> | |
2b044526 ZJS |
165 | |
166 | <programlisting>#include <stdio.h> | |
167 | #define SD_MESSAGE_COREDUMP_STR SD_ID128_MAKE_STR(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1) | |
168 | ||
169 | int main(int argc, char **argv) { | |
e0a41aa4 | 170 | puts("Match for coredumps: MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); |
39d02a17 | 171 | }</programlisting> |
2b044526 | 172 | |
d13f1051 ZJS |
173 | <para><function>SD_ID128_CONST_STR()</function> converts constant IDs into constant strings for |
174 | output. The following example code will output the string "fc2e22bc6ee647b6b90729ab34a250b1":</para> | |
798d3a52 | 175 | <programlisting>int main(int argc, char *argv[]) { |
e0a41aa4 | 176 | puts("Match for coredumps: %s", SD_ID128_CONST_STR(SD_MESSAGE_COREDUMP)); |
183de6d7 LP |
177 | }</programlisting> |
178 | ||
d13f1051 ZJS |
179 | <para><constant>SD_ID128_FORMAT_STR</constant> and <function>SD_ID128_FORMAT_VAL()</function> is used to |
180 | format an ID in a <citerefentry | |
181 | project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry> format | |
182 | string, as shown in the following example:</para> | |
798d3a52 ZJS |
183 | |
184 | <programlisting>int main(int argc, char *argv[]) { | |
e0a41aa4 ZJS |
185 | sd_id128_t id; |
186 | id = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07); | |
187 | printf("The ID encoded in this C file is " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(id)); | |
188 | return 0; | |
12355095 LP |
189 | }</programlisting> |
190 | ||
7c7683f3 ZJS |
191 | <para><constant>SD_ID128_UUID_FORMAT_STR</constant> and <function>SD_ID128_MAKE_UUID_STR()</function> |
192 | are similar to | |
193 | <constant>SD_ID128_FORMAT_STR</constant> and <function>SD_ID128_MAKE_STR()</function>, | |
194 | but include separating hyphens to conform to the | |
c8cd6d7b | 195 | "<ulink url="https://en.wikipedia.org/wiki/Universally_unique_identifier#Format">UUID canonical representation</ulink>". |
7c7683f3 | 196 | They format the string based on <ulink |
39d02a17 LP |
197 | url="https://tools.ietf.org/html/rfc4122">RFC4122</ulink> Variant 1 rules, i.e. converting from Big |
198 | Endian byte order. This matches behaviour of most other Linux userspace infrastructure. It's probably | |
199 | best to avoid UUIDs of other variants, in order to avoid unnecessary ambiguities. All 128-bit IDs | |
200 | generated by the sd-id128 APIs strictly conform to Variant 1 Version 4 UUIDs, as per RFC 4122.</para> | |
38df8d3f | 201 | |
d13f1051 | 202 | <para><function>sd_id128_equal()</function> compares two 128-bit IDs:</para> |
12355095 | 203 | |
798d3a52 | 204 | <programlisting>int main(int argc, char *argv[]) { |
e0a41aa4 ZJS |
205 | sd_id128_t a, b, c; |
206 | a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07); | |
207 | b = SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e); | |
208 | c = a; | |
209 | assert(sd_id128_equal(a, c)); | |
210 | assert(!sd_id128_equal(a, b)); | |
211 | return 0; | |
3dbea941 LP |
212 | }</programlisting> |
213 | ||
944c1243 ZJS |
214 | <para><function>sd_id128_string_equal()</function> is similar to <function>sd_id128_equal()</function>, |
215 | but the first ID is formatted as <type>const char*</type>. The same restrictions apply as to the first | |
216 | argument of <function>sd_id128_from_string()</function>.</para> | |
217 | ||
d13f1051 ZJS |
218 | <para><function>sd_id128_is_null()</function> checks if an ID consists of only <constant>NUL</constant> |
219 | bytes:</para> | |
3dbea941 | 220 | |
78aa5b6f ZJS |
221 | <programlisting>assert(sd_id128_is_null(SD_ID128_NULL));</programlisting> |
222 | ||
d13f1051 | 223 | <para>Similarly, <function>sd_id128_is_allf()</function> checks if an ID consists of only |
78aa5b6f ZJS |
224 | <constant>0xFF</constant> bytes (all bits on):</para> |
225 | ||
226 | <programlisting>assert(sd_id128_is_allf(SD_ID128_ALLF));</programlisting> | |
12355095 | 227 | |
d13f1051 ZJS |
228 | <para><function>sd_id128_in_set_sentinel()</function> takes a list of IDs and returns true if the first |
229 | argument is equal to any of the subsequent arguments. The argument list is terminated by an | |
230 | <constant>SD_ID128_NULL</constant> sentinel, which must be present.</para> | |
231 | ||
232 | <para><function>sd_id128_in_set()</function> is a convenience function that takes a list of IDs and | |
233 | returns true if the first argument is equal to any of the subsequent arguments:</para> | |
64b21afc ZJS |
234 | |
235 | <programlisting>int main(int argc, char *argv[]) { | |
236 | sd_id12_t a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07); | |
237 | assert(sd_id128_in_set(a, a)); | |
238 | assert(sd_id128_in_set(a, a, a)); | |
239 | assert(!sd_id128_in_set(a)); | |
240 | assert(!sd_id128_in_set(a, | |
241 | SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e) | |
242 | SD_ID128_MAKE(2f,88,28,5f,9c,44,09,9d,d7,15,77,04,bc,85,7e,e3) | |
243 | SD_ID128_ALLF)); | |
244 | return 0; | |
245 | } | |
246 | </programlisting> | |
247 | ||
248 | <para><function>sd_id128_in_set()</function> is defined as a macro over | |
d13f1051 ZJS |
249 | <function>sd_id128_in_set_sentinel()</function>, adding the <constant>SD_ID128_NULL</constant> sentinel |
250 | automatically. Since <function>sd_id128_in_set_sentinel()</function> uses | |
251 | <constant>SD_ID128_NULL</constant> as the sentinel, <constant>SD_ID128_NULL</constant> cannot be | |
252 | otherwise placed in the argument list.</para> | |
64b21afc ZJS |
253 | |
254 | <para><function>sd_id128_in_setv()</function> is similar to | |
255 | <function>sd_id128_in_set_sentinel()</function>, but takes a <structname>struct varargs</structname> | |
256 | argument.</para> | |
257 | ||
d13f1051 | 258 | <para>New randomized IDs may be generated with |
b9d016d6 LP |
259 | <citerefentry><refentrytitle>systemd-id128</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s |
260 | <command>new</command> command.</para> | |
d13f1051 ZJS |
261 | |
262 | <para>See | |
263 | <citerefentry><refentrytitle>sd_id128_to_string</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
264 | <citerefentry><refentrytitle>sd_id128_randomize</refentrytitle><manvolnum>3</manvolnum></citerefentry> | |
265 | and | |
266 | <citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry> | |
267 | for information about other implemented functions.</para> | |
798d3a52 ZJS |
268 | </refsect1> |
269 | ||
270 | <xi:include href="libsystemd-pkgconfig.xml" /> | |
271 | ||
69106f47 AK |
272 | <refsect1> |
273 | <title>History</title> | |
00f95506 AK |
274 | <para><function>sd_id128_equal()</function>, |
275 | <function>sd_id128_string_equal()</function>, | |
276 | <function>sd_id128_is_null()</function>, | |
277 | <function>sd_id128_is_allf()</function>, | |
278 | <function>sd_id128_in_setv()</function>, | |
279 | <function>sd_id128_in_set_sentinel()</function>, and | |
280 | <function>sd_id128_in_set()</function> were added in version 252.</para> | |
69106f47 AK |
281 | </refsect1> |
282 | ||
798d3a52 ZJS |
283 | <refsect1> |
284 | <title>See Also</title> | |
13a69c12 DT |
285 | <para><simplelist type="inline"> |
286 | <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> | |
287 | <member><citerefentry><refentrytitle>sd_id128_to_string</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
288 | <member><citerefentry><refentrytitle>sd_id128_randomize</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
289 | <member><citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
290 | <member><citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
291 | <member><citerefentry><refentrytitle>journalctl</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> | |
292 | <member><citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>7</manvolnum></citerefentry></member> | |
293 | <member><citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> | |
294 | <member><citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> | |
295 | </simplelist></para> | |
798d3a52 | 296 | </refsect1> |
12355095 LP |
297 | |
298 | </refentry> |