]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/id128/id128.c
Two follow-ups for recent PRs (#38062)
[thirdparty/systemd.git] / src / id128 / id128.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4 #include <stdio.h>
5
6 #include "alloc-util.h"
7 #include "build.h"
8 #include "format-table.h"
9 #include "gpt.h"
10 #include "id128-print.h"
11 #include "id128-util.h"
12 #include "log.h"
13 #include "main-func.h"
14 #include "parse-argument.h"
15 #include "pretty-print.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "verbs.h"
19
20 static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
21 static sd_id128_t arg_app = SD_ID128_NULL;
22 static bool arg_value = false;
23 static PagerFlags arg_pager_flags = 0;
24 static bool arg_legend = true;
25 static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
26
27 static int verb_new(int argc, char **argv, void *userdata) {
28 return id128_print_new(arg_mode);
29 }
30
31 static int verb_machine_id(int argc, char **argv, void *userdata) {
32 sd_id128_t id;
33 int r;
34
35 if (sd_id128_is_null(arg_app))
36 r = sd_id128_get_machine(&id);
37 else
38 r = sd_id128_get_machine_app_specific(arg_app, &id);
39 if (r < 0)
40 return log_error_errno(r, "Failed to get %smachine-ID: %m",
41 sd_id128_is_null(arg_app) ? "" : "app-specific ");
42
43 return id128_pretty_print(id, arg_mode);
44 }
45
46 static int verb_boot_id(int argc, char **argv, void *userdata) {
47 sd_id128_t id;
48 int r;
49
50 if (sd_id128_is_null(arg_app))
51 r = sd_id128_get_boot(&id);
52 else
53 r = sd_id128_get_boot_app_specific(arg_app, &id);
54 if (r < 0)
55 return log_error_errno(r, "Failed to get %sboot-ID: %m",
56 sd_id128_is_null(arg_app) ? "" : "app-specific ");
57
58 return id128_pretty_print(id, arg_mode);
59 }
60
61 static int verb_invocation_id(int argc, char **argv, void *userdata) {
62 sd_id128_t id;
63 int r;
64
65 if (!sd_id128_is_null(arg_app))
66 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
67 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
68
69 r = sd_id128_get_invocation(&id);
70 if (r < 0)
71 return log_error_errno(r, "Failed to get invocation-ID: %m");
72
73 return id128_pretty_print(id, arg_mode);
74 }
75
76 static int verb_var_uuid(int argc, char **argv, void *userdata) {
77 sd_id128_t id;
78 int r;
79
80 if (!sd_id128_is_null(arg_app))
81 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
82 "Verb \"var-partition-uuid\" cannot be combined with --app-specific=.");
83
84 /* The DPS says that the UUID for /var/ should be keyed with machine-id. */
85 r = sd_id128_get_machine_app_specific(SD_GPT_VAR, &id);
86 if (r < 0)
87 return log_error_errno(r, "Failed to generate machine-specific /var/ UUID: %m");
88
89 return id128_pretty_print(id, arg_mode);
90 }
91
92 static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) {
93 int r;
94
95 assert(table);
96
97 if (!name)
98 name = "XYZ";
99
100 if (arg_mode == ID128_PRINT_PRETTY) {
101 _cleanup_free_ char *id = NULL;
102
103 id = strreplace(name, "-", "_");
104 if (!id)
105 return log_oom();
106
107 ascii_strupper(id);
108
109 r = id128_pretty_print_sample(id, uuid);
110 if (r < 0)
111 return r;
112 if (!first)
113 puts("");
114 return 0;
115 }
116
117 if (arg_value)
118 return id128_pretty_print(uuid, arg_mode);
119
120 if (!*table) {
121 *table = table_new("name", "id");
122 if (!*table)
123 return log_oom();
124
125 table_set_width(*table, 0);
126 }
127
128 return table_add_many(*table,
129 TABLE_STRING, name,
130 arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID, uuid);
131 }
132
133 static int verb_show(int argc, char **argv, void *userdata) {
134 _cleanup_(table_unrefp) Table *table = NULL;
135 int r;
136
137 argv = strv_skip(argv, 1);
138 if (argv)
139 STRV_FOREACH(p, argv) {
140 sd_id128_t uuid;
141 const char *id = NULL;
142
143 /* Check if the argument is an actual UUID first */
144 bool is_uuid = sd_id128_from_string(*p, &uuid) >= 0;
145
146 if (is_uuid)
147 id = gpt_partition_type_uuid_to_string(uuid);
148 else {
149 GptPartitionType type;
150
151 r = gpt_partition_type_from_string(*p, &type);
152 if (r < 0)
153 return log_error_errno(r, "Unknown identifier \"%s\".", *p);
154
155 uuid = type.uuid;
156 id = *p;
157 }
158
159 if (!sd_id128_is_null(arg_app))
160 assert_se(sd_id128_get_app_specific(uuid, arg_app, &uuid) >= 0);
161
162 r = show_one(&table, id, uuid, p == argv);
163 if (r < 0)
164 return r;
165 }
166 else {
167 if (!sd_id128_is_null(arg_app))
168 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
169 "'show --app-specific=' can only be used with explicit UUID input.");
170
171 for (const GptPartitionType *e = gpt_partition_type_table; e->name; e++) {
172 r = show_one(&table, e->name, e->uuid, e == gpt_partition_type_table);
173 if (r < 0)
174 return r;
175 }
176 }
177
178 if (table) {
179 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
180 if (r < 0)
181 return r;
182 }
183
184 return 0;
185 }
186
187 static int help(void) {
188 _cleanup_free_ char *link = NULL;
189 int r;
190
191 r = terminal_urlify_man("systemd-id128", "1", &link);
192 if (r < 0)
193 return log_oom();
194
195 printf("%s [OPTIONS...] COMMAND\n\n"
196 "%sGenerate and print 128-bit identifiers.%s\n"
197 "\nCommands:\n"
198 " new Generate a new ID\n"
199 " machine-id Print the ID of current machine\n"
200 " boot-id Print the ID of current boot\n"
201 " invocation-id Print the ID of current invocation\n"
202 " var-partition-uuid Print the UUID for the /var/ partition\n"
203 " show [NAME|UUID] Print one or more UUIDs\n"
204 " help Show this help\n"
205 "\nOptions:\n"
206 " -h --help Show this help\n"
207 " --no-pager Do not pipe output into a pager\n"
208 " --no-legend Do not show the headers and footers\n"
209 " --json=FORMAT Output inspection data in JSON (takes one of\n"
210 " pretty, short, off)\n"
211 " -j Equivalent to --json=pretty (on TTY) or\n"
212 " --json=short (otherwise)\n"
213 " -p --pretty Generate samples of program code\n"
214 " -P --value Only print the value\n"
215 " -a --app-specific=ID Generate app-specific IDs\n"
216 " -u --uuid Output in UUID format\n"
217 "\nSee the %s for details.\n",
218 program_invocation_short_name,
219 ansi_highlight(),
220 ansi_normal(),
221 link);
222
223 return 0;
224 }
225
226 static int verb_help(int argc, char **argv, void *userdata) {
227 return help();
228 }
229
230 static int parse_argv(int argc, char *argv[]) {
231 enum {
232 ARG_VERSION = 0x100,
233 ARG_NO_PAGER,
234 ARG_NO_LEGEND,
235 ARG_JSON,
236 };
237
238 static const struct option options[] = {
239 { "help", no_argument, NULL, 'h' },
240 { "version", no_argument, NULL, ARG_VERSION },
241 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
242 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
243 { "json", required_argument, NULL, ARG_JSON },
244 { "pretty", no_argument, NULL, 'p' },
245 { "value", no_argument, NULL, 'P' },
246 { "app-specific", required_argument, NULL, 'a' },
247 { "uuid", no_argument, NULL, 'u' },
248 {},
249 };
250
251 int c, r;
252
253 assert(argc >= 0);
254 assert(argv);
255
256 while ((c = getopt_long(argc, argv, "hpa:uPj", options, NULL)) >= 0)
257 switch (c) {
258
259 case 'h':
260 return help();
261
262 case ARG_VERSION:
263 return version();
264
265 case ARG_NO_PAGER:
266 arg_pager_flags |= PAGER_DISABLE;
267 break;
268
269 case ARG_NO_LEGEND:
270 arg_legend = false;
271 break;
272
273 case 'j':
274 arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;
275 break;
276
277 case ARG_JSON:
278 r = parse_json_argument(optarg, &arg_json_format_flags);
279 if (r <= 0)
280 return r;
281
282 break;
283 case 'p':
284 arg_mode = ID128_PRINT_PRETTY;
285 arg_value = false;
286 break;
287
288 case 'P':
289 arg_value = true;
290 if (arg_mode == ID128_PRINT_PRETTY)
291 arg_mode = ID128_PRINT_ID128;
292 break;
293
294 case 'a':
295 r = id128_from_string_nonzero(optarg, &arg_app);
296 if (r == -ENXIO)
297 return log_error_errno(r, "Application ID cannot be all zeros.");
298 if (r < 0)
299 return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
300 break;
301
302 case 'u':
303 arg_mode = ID128_PRINT_UUID;
304 break;
305
306 case '?':
307 return -EINVAL;
308
309 default:
310 assert_not_reached();
311 }
312
313 return 1;
314 }
315
316 static int id128_main(int argc, char *argv[]) {
317 static const Verb verbs[] = {
318 { "new", VERB_ANY, 1, 0, verb_new },
319 { "machine-id", VERB_ANY, 1, 0, verb_machine_id },
320 { "boot-id", VERB_ANY, 1, 0, verb_boot_id },
321 { "invocation-id", VERB_ANY, 1, 0, verb_invocation_id },
322 { "var-partition-uuid", VERB_ANY, 1, 0, verb_var_uuid },
323 { "show", VERB_ANY, VERB_ANY, 0, verb_show },
324 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
325 {}
326 };
327
328 return dispatch_verb(argc, argv, verbs, NULL);
329 }
330
331 static int run(int argc, char *argv[]) {
332 int r;
333
334 log_setup();
335
336 r = parse_argv(argc, argv);
337 if (r <= 0)
338 return r;
339
340 return id128_main(argc, argv);
341 }
342
343 DEFINE_MAIN_FUNCTION(run);