chiark / gitweb /
core: add "invocation ID" concept to service manager
[elogind.git] / src / libelogind / sd-id128 / sd-id128.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2011 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include "sd-id128.h"
25
26 #include "fd-util.h"
27 #include "hexdecoct.h"
28 #include "id128-util.h"
29 #include "io-util.h"
30 #include "macro.h"
31 #include "random-util.h"
32 #include "util.h"
33
34 _public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) {
35         unsigned n;
36
37         assert_return(s, NULL);
38
39         for (n = 0; n < 16; n++) {
40                 s[n*2] = hexchar(id.bytes[n] >> 4);
41                 s[n*2+1] = hexchar(id.bytes[n] & 0xF);
42         }
43
44         s[32] = 0;
45
46         return s;
47 }
48
49 _public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
50         unsigned n, i;
51         sd_id128_t t;
52         bool is_guid = false;
53
54         assert_return(s, -EINVAL);
55
56         for (n = 0, i = 0; n < 16;) {
57                 int a, b;
58
59                 if (s[i] == '-') {
60                         /* Is this a GUID? Then be nice, and skip over
61                          * the dashes */
62
63                         if (i == 8)
64                                 is_guid = true;
65                         else if (i == 13 || i == 18 || i == 23) {
66                                 if (!is_guid)
67                                         return -EINVAL;
68                         } else
69                                 return -EINVAL;
70
71                         i++;
72                         continue;
73                 }
74
75                 a = unhexchar(s[i++]);
76                 if (a < 0)
77                         return -EINVAL;
78
79                 b = unhexchar(s[i++]);
80                 if (b < 0)
81                         return -EINVAL;
82
83                 t.bytes[n++] = (a << 4) | b;
84         }
85
86         if (i != (is_guid ? 36 : 32))
87                 return -EINVAL;
88
89         if (s[i] != 0)
90                 return -EINVAL;
91
92         if (ret)
93                 *ret = t;
94         return 0;
95 }
96
97 _public_ int sd_id128_get_machine(sd_id128_t *ret) {
98         static thread_local sd_id128_t saved_machine_id = {};
99         int r;
100
101         assert_return(ret, -EINVAL);
102
103         if (sd_id128_is_null(saved_machine_id)) {
104                 r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id);
105                 if (r < 0)
106                         return r;
107
108                 if (sd_id128_is_null(saved_machine_id))
109                         return -EINVAL;
110         }
111
112         *ret = saved_machine_id;
113         return 0;
114 }
115
116 _public_ int sd_id128_get_boot(sd_id128_t *ret) {
117         static thread_local sd_id128_t saved_boot_id = {};
118         int r;
119
120         assert_return(ret, -EINVAL);
121
122         if (sd_id128_is_null(saved_boot_id)) {
123                 r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id);
124                 if (r < 0)
125                         return r;
126         }
127
128         *ret = saved_boot_id;
129         return 0;
130 }
131
132 _public_ int sd_id128_get_invocation(sd_id128_t *ret) {
133         static thread_local sd_id128_t saved_invocation_id = {};
134         int r;
135
136         assert_return(ret, -EINVAL);
137
138         if (sd_id128_is_null(saved_invocation_id)) {
139                 const char *e;
140
141                 e = secure_getenv("INVOCATION_ID");
142                 if (!e)
143                         return -ENXIO;
144
145                 r = sd_id128_from_string(e, &saved_invocation_id);
146                 if (r < 0)
147                         return r;
148         }
149
150         *ret = saved_invocation_id;
151         return 0;
152 }
153
154 static sd_id128_t make_v4_uuid(sd_id128_t id) {
155         /* Stolen from generate_random_uuid() of drivers/char/random.c
156          * in the kernel sources */
157
158         /* Set UUID version to 4 --- truly random generation */
159         id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
160
161         /* Set the UUID variant to DCE */
162         id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
163
164         return id;
165 }
166
167 _public_ int sd_id128_randomize(sd_id128_t *ret) {
168         sd_id128_t t;
169         int r;
170
171         assert_return(ret, -EINVAL);
172
173         r = dev_urandom(&t, sizeof(t));
174         if (r < 0)
175                 return r;
176
177         /* Turn this into a valid v4 UUID, to be nice. Note that we
178          * only guarantee this for newly generated UUIDs, not for
179          * pre-existing ones. */
180
181         *ret = make_v4_uuid(t);
182         return 0;
183 }