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