]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/id128/id128.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / id128 / id128.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
0d1d512f
ZJS
2
3#include <getopt.h>
4#include <stdio.h>
5
6#include "alloc-util.h"
dc972b07 7#include "gpt.h"
0d1d512f 8#include "id128-print.h"
5e332028 9#include "main-func.h"
294bf0c3 10#include "pretty-print.h"
dc972b07
ZJS
11#include "strv.h"
12#include "format-table.h"
a19fdd66 13#include "terminal-util.h"
0d1d512f
ZJS
14#include "util.h"
15#include "verbs.h"
16
a19fdd66 17static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
206a29b2 18static sd_id128_t arg_app = {};
0d1d512f
ZJS
19
20static int verb_new(int argc, char **argv, void *userdata) {
a19fdd66 21 return id128_print_new(arg_mode);
0d1d512f
ZJS
22}
23
24static int verb_machine_id(int argc, char **argv, void *userdata) {
25 sd_id128_t id;
26 int r;
27
28 if (sd_id128_is_null(arg_app))
29 r = sd_id128_get_machine(&id);
30 else
31 r = sd_id128_get_machine_app_specific(arg_app, &id);
32 if (r < 0)
33 return log_error_errno(r, "Failed to get %smachine-ID: %m",
34 sd_id128_is_null(arg_app) ? "" : "app-specific ");
35
a19fdd66 36 return id128_pretty_print(id, arg_mode);
0d1d512f
ZJS
37}
38
39static int verb_boot_id(int argc, char **argv, void *userdata) {
40 sd_id128_t id;
41 int r;
42
43 if (sd_id128_is_null(arg_app))
44 r = sd_id128_get_boot(&id);
45 else
46 r = sd_id128_get_boot_app_specific(arg_app, &id);
47 if (r < 0)
48 return log_error_errno(r, "Failed to get %sboot-ID: %m",
49 sd_id128_is_null(arg_app) ? "" : "app-specific ");
50
a19fdd66 51 return id128_pretty_print(id, arg_mode);
0d1d512f
ZJS
52}
53
54static int verb_invocation_id(int argc, char **argv, void *userdata) {
55 sd_id128_t id;
56 int r;
57
58 if (!sd_id128_is_null(arg_app))
886cf317
ZJS
59 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
60 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
0d1d512f
ZJS
61
62 r = sd_id128_get_invocation(&id);
63 if (r < 0)
64 return log_error_errno(r, "Failed to get invocation-ID: %m");
65
a19fdd66 66 return id128_pretty_print(id, arg_mode);
0d1d512f
ZJS
67}
68
dc972b07
ZJS
69static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) {
70 int r;
71
72 if (arg_mode == ID128_PRINT_PRETTY) {
73 _cleanup_free_ char *id = NULL;
74
75 id = strreplace(name, "-", "_");
76 if (!id)
77 return log_oom();
78
79 ascii_strupper(id);
80
81 r = id128_pretty_print_sample(id, uuid);
82 if (r < 0)
83 return r;
84 if (!first)
85 puts("");
86 return 0;
87
88 } else {
89 if (!*table) {
72d43d09 90 *table = table_new("name", "id");
dc972b07
ZJS
91 if (!*table)
92 return log_oom();
93 table_set_width(*table, 0);
94 }
95
96 return table_add_many(*table,
97 TABLE_STRING, name,
98 arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID,
99 uuid);
100 }
101}
102
103static int verb_show(int argc, char **argv, void *userdata) {
104 _cleanup_(table_unrefp) Table *table = NULL;
105 char **p;
106 int r;
107
108 argv = strv_skip(argv, 1);
109 if (strv_isempty(argv))
110 for (const GptPartitionType *e = gpt_partition_type_table; e->name; e++) {
111 r = show_one(&table, e->name, e->uuid, e == gpt_partition_type_table);
112 if (r < 0)
113 return r;
114 }
115 else
116 STRV_FOREACH(p, argv) {
117 sd_id128_t uuid;
118 bool have_uuid;
119 const char *id;
120
121 /* Check if the argument is an actual UUID first */
122 have_uuid = sd_id128_from_string(*p, &uuid) >= 0;
123
124 if (have_uuid)
125 id = gpt_partition_type_uuid_to_string(uuid) ?: "XYZ";
126 else {
127 r = gpt_partition_type_uuid_from_string(*p, &uuid);
128 if (r < 0)
129 return log_error_errno(r, "Unknown identifier \"%s\".", *p);
130
131 id = *p;
132 }
133
134 r = show_one(&table, id, uuid, p == argv);
135 if (r < 0)
136 return r;
137 }
138
139 if (table) {
140 r = table_print(table, NULL);
141 if (r < 0)
4b6607d9 142 return table_log_print_error(r);
dc972b07
ZJS
143 }
144
145 return 0;
146}
147
0d1d512f
ZJS
148static int help(void) {
149 _cleanup_free_ char *link = NULL;
150 int r;
151
152 r = terminal_urlify_man("systemd-id128", "1", &link);
153 if (r < 0)
154 return log_oom();
155
a19fdd66
LP
156 printf("%s [OPTIONS...] COMMAND\n\n"
157 "%sGenerate and print 128bit identifiers.%s\n"
20a51f6a 158 "\nCommands:\n"
dc972b07 159 " new Generate a new ID\n"
0d1d512f
ZJS
160 " machine-id Print the ID of current machine\n"
161 " boot-id Print the ID of current boot\n"
162 " invocation-id Print the ID of current invocation\n"
dc972b07 163 " show [NAME] Print one or more well-known IDs\n"
0d1d512f 164 " help Show this help\n"
a19fdd66
LP
165 "\nOptions:\n"
166 " -h --help Show this help\n"
167 " -p --pretty Generate samples of program code\n"
168 " -a --app-specific=ID Generate app-specific IDs\n"
169 " -u --uuid Output in UUID format\n"
0d1d512f
ZJS
170 "\nSee the %s for details.\n"
171 , program_invocation_short_name
a19fdd66 172 , ansi_highlight(), ansi_normal()
0d1d512f
ZJS
173 , link
174 );
175
176 return 0;
177}
178
179static int verb_help(int argc, char **argv, void *userdata) {
180 return help();
181}
182
183static int parse_argv(int argc, char *argv[]) {
184 enum {
185 ARG_VERSION = 0x100,
186 };
187
188 static const struct option options[] = {
189 { "help", no_argument, NULL, 'h' },
190 { "version", no_argument, NULL, ARG_VERSION },
be440e09 191 { "pretty", no_argument, NULL, 'p' },
0d1d512f 192 { "app-specific", required_argument, NULL, 'a' },
a19fdd66 193 { "uuid", no_argument, NULL, 'u' },
0d1d512f
ZJS
194 {},
195 };
196
197 int c, r;
198
199 assert(argc >= 0);
200 assert(argv);
201
a19fdd66 202 while ((c = getopt_long(argc, argv, "hpa:u", options, NULL)) >= 0)
0d1d512f
ZJS
203 switch (c) {
204
205 case 'h':
206 return help();
207
208 case ARG_VERSION:
209 return version();
210
211 case 'p':
a19fdd66 212 arg_mode = ID128_PRINT_PRETTY;
0d1d512f
ZJS
213 break;
214
215 case 'a':
216 r = sd_id128_from_string(optarg, &arg_app);
217 if (r < 0)
218 return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
219 break;
220
a19fdd66
LP
221 case 'u':
222 arg_mode = ID128_PRINT_UUID;
223 break;
224
0d1d512f
ZJS
225 case '?':
226 return -EINVAL;
227
228 default:
229 assert_not_reached("Unhandled option");
230 }
231
232 return 1;
233}
234
235static int id128_main(int argc, char *argv[]) {
236 static const Verb verbs[] = {
237 { "new", VERB_ANY, 1, 0, verb_new },
238 { "machine-id", VERB_ANY, 1, 0, verb_machine_id },
239 { "boot-id", VERB_ANY, 1, 0, verb_boot_id },
240 { "invocation-id", VERB_ANY, 1, 0, verb_invocation_id },
dc972b07 241 { "show", VERB_ANY, VERB_ANY, 0, verb_show },
0d1d512f
ZJS
242 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
243 {}
244 };
245
246 return dispatch_verb(argc, argv, verbs, NULL);
247}
248
3c79f0b3 249static int run(int argc, char *argv[]) {
0d1d512f
ZJS
250 int r;
251
41d1f469 252 log_setup_cli();
0d1d512f
ZJS
253
254 r = parse_argv(argc, argv);
255 if (r <= 0)
3c79f0b3 256 return r;
0d1d512f 257
3c79f0b3 258 return id128_main(argc, argv);
0d1d512f 259}
3c79f0b3
ZJS
260
261DEFINE_MAIN_FUNCTION(run);