1SD_ID128_GET_MACHINE(3) sd_id128_get_machine SD_ID128_GET_MACHINE(3)
2
3
4
6 sd_id128_get_machine, sd_id128_get_machine_app_specific,
7 sd_id128_get_boot, sd_id128_get_boot_app_specific,
8 sd_id128_get_invocation - Retrieve 128-bit IDs
9
11 #include <systemd/sd-id128.h>
12
13 int sd_id128_get_machine(sd_id128_t *ret);
14
15 int sd_id128_get_machine_app_specific(sd_id128_t app_id,
16 sd_id128_t *ret);
17
18 int sd_id128_get_boot(sd_id128_t *ret);
19
20 int sd_id128_get_boot_app_specific(sd_id128_t app_id, sd_id128_t *ret);
21
22 int sd_id128_get_invocation(sd_id128_t *ret);
23
25 sd_id128_get_machine() returns the machine ID of the executing host.
26 This reads and parses the machine-id(5) file. This function caches the
27 machine ID internally to make retrieving the machine ID a cheap
28 operation. This ID may be used wherever a unique identifier for the
29 local system is needed. However, it is recommended to use this ID as-is
30 only in trusted environments. In untrusted environments it is
31 recommended to derive an application specific ID from this machine ID,
32 in an irreversible (cryptographically secure) way. To make this easy
33 sd_id128_get_machine_app_specific() is provided, see below.
34
35 sd_id128_get_machine_app_specific() is similar to
36 sd_id128_get_machine(), but retrieves a machine ID that is specific to
37 the application that is identified by the indicated application ID. It
38 is recommended to use this function instead of sd_id128_get_machine()
39 when passing an ID to untrusted environments, in order to make sure
40 that the original machine ID may not be determined externally. This
41 way, the ID used by the application remains stable on a given machine,
42 but cannot be easily correlated with IDs used in other applications on
43 the same machine. The application-specific ID should be generated via a
44 tool like systemd-id128 new, and may be compiled into the application.
45 This function will return the same application-specific ID for each
46 combination of machine ID and application ID. Internally, this function
47 calculates HMAC-SHA256 of the application ID, keyed by the machine ID.
48
49 sd_id128_get_boot() returns the boot ID of the executing kernel. This
50 reads and parses the /proc/sys/kernel/random/boot_id file exposed by
51 the kernel. It is randomly generated early at boot and is unique for
52 every running kernel instance. See random(4) for more information. This
53 function also internally caches the returned ID to make this call a
54 cheap operation. It is recommended to use this ID as-is only in trusted
55 environments. In untrusted environments it is recommended to derive an
56 application specific ID using sd_id128_get_boot_app_specific(), see
57 below.
58
59 sd_id128_get_boot_app_specific() is analogous to
60 sd_id128_get_machine_app_specific() but returns an ID that changes
61 between boots. Some machines may be used for a long time without
62 rebooting, hence the boot ID may remain constant for a long time, and
63 has properties similar to the machine ID during that time.
64
65 sd_id128_get_invocation() returns the invocation ID of the currently
66 executed service. In its current implementation, this tries to read and
67 parse the following:
68
69 • The $INVOCATION_ID environment variable that the service manager
70 sets when activating a service.
71
72 • An entry in the kernel keyring that the system service manager sets
73 when activating a service.
74
75 See systemd.exec(5) for details. The ID is cached internally. In future
76 a different mechanism to determine the invocation ID may be added.
77
78 Note that sd_id128_get_machine_app_specific(), sd_id128_get_boot(),
79 sd_id128_get_boot_app_specific(), and sd_id128_get_invocation() always
80 return UUID Variant 1 Version 4 compatible IDs. sd_id128_get_machine()
81 will also return a UUID Variant 1 Version 4 compatible ID on new
82 installations but might not on older. It is possible to convert the
83 machine ID non-reversibly into a UUID Variant 1 Version 4 compatible
84 one. For more information, see machine-id(5). It is hence guaranteed
85 that these functions will never return the ID consisting of all zero or
86 all one bits (SD_ID128_NULL, SD_ID128_ALLF) — with the possible
87 exception of sd_id128_get_machine(), as mentioned.
88
89 For more information about the "sd_id128_t" type see sd-id128(3).
90
92 Those calls return 0 on success (in which case ret is filled in), or a
93 negative errno-style error code.
94
95 Errors
96 Returned errors may indicate the following problems:
97
98 -ENOENT
99 Returned by sd_id128_get_machine() and
100 sd_id128_get_machine_app_specific() when /etc/machine-id is
101 missing.
102
103 -ENOMEDIUM
104 Returned by sd_id128_get_machine() and
105 sd_id128_get_machine_app_specific() when /etc/machine-id is empty
106 or all zeros. Also returned by sd_id128_get_invocation() when the
107 invocation ID is all zeros.
108
109 -ENOPKG
110 Returned by sd_id128_get_machine() and
111 sd_id128_get_machine_app_specific() when the content of
112 /etc/machine-id is "uninitialized".
113
114 -ENOSYS
115 Returned by sd_id128_get_boot() and
116 sd_id128_get_boot_app_specific() when /proc/ is not mounted.
117
118 -ENXIO
119 Returned by sd_id128_get_invocation() if no invocation ID is set.
120
121 -EUCLEAN
122 Returned by any of the functions described here when the configured
123 value has invalid format.
124
125 -EPERM
126 Requested information could not be retrieved because of
127 insufficient permissions.
128
130 These APIs are implemented as a shared library, which can be compiled
131 and linked to with the libsystemd pkg-config(1) file.
132
134 Example 1. Application-specific machine ID
135
136 First, generate the application ID:
137
138 $ systemd-id128 -p new
139 As string:
140 c273277323db454ea63bb96e79b53e97
141
142 As UUID:
143 c2732773-23db-454e-a63b-b96e79b53e97
144
145 As man:sd-id128(3) macro:
146 #define MESSAGE_XYZ SD_ID128_MAKE(c2,73,27,73,23,db,45,4e,a6,3b,b9,6e,79,b5,3e,97)
147 ...
148
149 Then use the new identifier in an example application:
150
151 /* SPDX-License-Identifier: MIT-0 */
152
153 #include <stdio.h>
154 #include <systemd/sd-id128.h>
155
156 #define OUR_APPLICATION_ID SD_ID128_MAKE(c2,73,27,73,23,db,45,4e,a6,3b,b9,6e,79,b5,3e,97)
157
158 int main(int argc, char *argv[]) {
159 sd_id128_t id;
160 sd_id128_get_machine_app_specific(OUR_APPLICATION_ID, &id);
161 printf("Our application ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id));
162 return 0;
163 }
164
166 systemd(1), systemd-id128(1), sd-id128(3), machine-id(5),
167 systemd.exec(5), sd_id128_randomize(3), random(4)
168
169
170
171systemd 253 SD_ID128_GET_MACHINE(3)