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_machine_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 reads and parses
67 the $INVOCATION_ID environment variable that the service manager sets
68 when activating a service, see systemd.exec(5) for details. The ID is
69 cached internally. In future a different mechanism to determine the
70 invocation ID may be added.
71
72 Note that sd_id128_get_machine_app_specific(), sd_id128_get_boot(),
73 sd_id128_get_boot_app_specific(), and sd_id128_get_invocation() always
74 return UUID Variant 1 Version 4 compatible IDs. sd_id128_get_machine()
75 will also return a UUID Variant 1 Version 4 compatible ID on new
76 installations but might not on older. It is possible to convert the
77 machine ID non-reversibly into a UUID Variant 1 Version 4 compatible
78 one. For more information, see machine-id(5). It is hence guaranteed
79 that these functions will never return the ID consisting of all zero or
80 all one bits (SD_ID128_NULL, SD_ID128_ALLF) — with the possible
81 exception of sd_id128_get_machine(), as mentioned.
82
83 For more information about the "sd_id128_t" type see sd-id128(3).
84
86 Those calls return 0 on success (in which case ret is filled in), or a
87 negative errno-style error code.
88
89 Errors
90 Returned errors may indicate the following problems:
91
92 -ENOENT
93 Returned by sd_id128_get_machine(),
94 sd_id128_get_machine_app_specific(), and
95 sd_id128_get_boot_app_specific() when /etc/machine-id is missing.
96
97 -ENOMEDIUM
98 Returned by sd_id128_get_machine(),
99 sd_id128_get_machine_app_specific(), and
100 sd_id128_get_boot_app_specific() when /etc/machine-id is empty or
101 all zeros.
102
103 -ENXIO
104 Returned by sd_id128_get_invocation() if no invocation ID is set.
105
106 -EIO
107 Returned by any of the functions described here when the configured
108 value has invalid format.
109
110 -EPERM
111 Requested information could not be retrieved because of
112 insufficient permissions.
113
115 These APIs are implemented as a shared library, which can be compiled
116 and linked to with the libsystemd pkg-config(1) file.
117
119 Example 1. Application-specific machine ID
120
121 First, generate the application ID:
122
123 $ systemd-id128 -p new
124 As string:
125 c273277323db454ea63bb96e79b53e97
126
127 As UUID:
128 c2732773-23db-454e-a63b-b96e79b53e97
129
130 As man:sd-id128(3) macro:
131 #define MESSAGE_XYZ SD_ID128_MAKE(c2,73,27,73,23,db,45,4e,a6,3b,b9,6e,79,b5,3e,97)
132 ...
133
134 Then use the new identifier in an example application:
135
136 /* SPDX-License-Identifier: CC0-1.0 */
137
138 #include <stdio.h>
139 #include <systemd/sd-id128.h>
140
141 #define OUR_APPLICATION_ID SD_ID128_MAKE(c2,73,27,73,23,db,45,4e,a6,3b,b9,6e,79,b5,3e,97)
142
143 int main(int argc, char *argv[]) {
144 sd_id128_t id;
145 sd_id128_get_machine_app_specific(OUR_APPLICATION_ID, &id);
146 printf("Our application ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id));
147 return 0;
148 }
149
151 systemd(1), systemd-id128(1), sd-id128(3), machine-id(5),
152 systemd.exec(5), sd_id128_randomize(3), random(4)
153
154
155
156systemd 250 SD_ID128_GET_MACHINE(3)