1SD-ID128(3) sd-id128 SD-ID128(3)
2
3
4
6 sd-id128, sd_id128_t, SD_ID128_MAKE, SD_ID128_MAKE_STR, SD_ID128_NULL,
7 SD_ID128_CONST_STR, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL,
8 sd_id128_equal, sd_id128_is_null - APIs for processing 128-bit IDs
9
11 #include <systemd/sd-id128.h>
12
13 pkg-config --cflags --libs libsystemd
14
16 sd-id128.h provides APIs to process and generate 128-bit ID values. The
17 128-bit ID values processed and generated by these APIs are a
18 generalization of OSF UUIDs as defined by RFC 4122[1] but use a simpler
19 string format. These functions impose no structure on the used IDs,
20 much unlike OSF UUIDs or Microsoft GUIDs, but are fully compatible with
21 those types of IDs.
22
23 See sd_id128_to_string(3), sd_id128_randomize(3) and
24 sd_id128_get_machine(3) for more information about the implemented
25 functions.
26
27 A 128-bit ID is implemented as the following union type:
28
29 typedef union sd_id128 {
30 uint8_t bytes[16];
31 uint64_t qwords[2];
32 } sd_id128_t;
33
34 This union type allows accessing the 128-bit ID as 16 separate bytes or
35 two 64-bit words. It is generally safer to access the ID components by
36 their 8-bit array to avoid endianness issues. This union is intended to
37 be passed call-by-value (as opposed to call-by-reference) and may be
38 directly manipulated by clients.
39
40 A couple of macros are defined to denote and decode 128-bit IDs:
41
42 SD_ID128_MAKE() may be used to denote a constant 128-bit ID in source
43 code. A commonly used idiom is to assign a name to a 128-bit ID using
44 this macro:
45
46 #define SD_MESSAGE_COREDUMP SD_ID128_MAKE(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)
47
48 SD_ID128_NULL may be used to refer to the 128bit ID consisting of only
49 NUL bytes.
50
51 SD_ID128_MAKE_STR() is similar to SD_ID128_MAKE(), but creates a const
52 char* expression that can be conveniently used in message formats and
53 such:
54
55 #include <stdio.h>
56 #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)
57
58 int main(int argc, char **argv) {
59 puts("Match for coredumps: MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
60 }
61
62
63 SD_ID128_CONST_STR() may be used to convert constant 128-bit IDs into
64 constant strings for output. The following example code will output the
65 string "fc2e22bc6ee647b6b90729ab34a250b1":
66
67 int main(int argc, char *argv[]) {
68 puts("Match for coredumps: %s", SD_ID128_CONST_STR(SD_MESSAGE_COREDUMP));
69 }
70
71 SD_ID128_FORMAT_STR() and SD_ID128_FORMAT_VAL() may be used to format a
72 128-bit ID in a printf(3) format string, as shown in the following
73 example:
74
75 int main(int argc, char *argv[]) {
76 sd_id128_t id;
77 id = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
78 printf("The ID encoded in this C file is " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(id));
79 return 0;
80 }
81
82 Use sd_id128_equal() to compare two 128-bit IDs:
83
84 int main(int argc, char *argv[]) {
85 sd_id128_t a, b, c;
86 a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
87 b = SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e);
88 c = a;
89 assert(sd_id128_equal(a, c));
90 assert(!sd_id128_equal(a, b));
91 return 0;
92 }
93
94 Use sd_id128_is_null() to check if an 128bit ID consists of only NUL
95 bytes:
96
97 int main(int argc, char *argv[]) {
98 assert(sd_id128_is_null(SD_ID128_NULL));
99 }
100
101 Note that new, randomized IDs may be generated with journalctl(1)'s
102 --new-id128 option.
103
105 These APIs are implemented as a shared library, which can be compiled
106 and linked to with the libsystemd pkg-config(1) file.
107
109 systemd(1), sd_id128_to_string(3), sd_id128_randomize(3),
110 sd_id128_get_machine(3), printf(3), journalctl(1), sd-journal(7), pkg-
111 config(1), machine-id(5)
112
114 1. RFC 4122
115 https://tools.ietf.org/html/rfc4122
116
117
118
119systemd 239 SD-ID128(3)