1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
8 #include "id128-print.h"
10 #include "pretty-print.h"
12 #include "format-table.h"
13 #include "terminal-util.h"
17 static Id128PrettyPrintMode arg_mode
= ID128_PRINT_ID128
;
18 static sd_id128_t arg_app
= {};
20 static int verb_new(int argc
, char **argv
, void *userdata
) {
21 return id128_print_new(arg_mode
);
24 static int verb_machine_id(int argc
, char **argv
, void *userdata
) {
28 if (sd_id128_is_null(arg_app
))
29 r
= sd_id128_get_machine(&id
);
31 r
= sd_id128_get_machine_app_specific(arg_app
, &id
);
33 return log_error_errno(r
, "Failed to get %smachine-ID: %m",
34 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
36 return id128_pretty_print(id
, arg_mode
);
39 static int verb_boot_id(int argc
, char **argv
, void *userdata
) {
43 if (sd_id128_is_null(arg_app
))
44 r
= sd_id128_get_boot(&id
);
46 r
= sd_id128_get_boot_app_specific(arg_app
, &id
);
48 return log_error_errno(r
, "Failed to get %sboot-ID: %m",
49 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
51 return id128_pretty_print(id
, arg_mode
);
54 static int verb_invocation_id(int argc
, char **argv
, void *userdata
) {
58 if (!sd_id128_is_null(arg_app
))
59 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
60 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
62 r
= sd_id128_get_invocation(&id
);
64 return log_error_errno(r
, "Failed to get invocation-ID: %m");
66 return id128_pretty_print(id
, arg_mode
);
69 static int show_one(Table
**table
, const char *name
, sd_id128_t uuid
, bool first
) {
72 if (arg_mode
== ID128_PRINT_PRETTY
) {
73 _cleanup_free_
char *id
= NULL
;
75 id
= strreplace(name
, "-", "_");
81 r
= id128_pretty_print_sample(id
, uuid
);
90 *table
= table_new("name", "id");
93 table_set_width(*table
, 0);
96 return table_add_many(*table
,
98 arg_mode
== ID128_PRINT_ID128
? TABLE_ID128
: TABLE_UUID
,
103 static int verb_show(int argc
, char **argv
, void *userdata
) {
104 _cleanup_(table_unrefp
) Table
*table
= NULL
;
107 argv
= strv_skip(argv
, 1);
108 if (strv_isempty(argv
))
109 for (const GptPartitionType
*e
= gpt_partition_type_table
; e
->name
; e
++) {
110 r
= show_one(&table
, e
->name
, e
->uuid
, e
== gpt_partition_type_table
);
115 STRV_FOREACH(p
, argv
) {
120 /* Check if the argument is an actual UUID first */
121 have_uuid
= sd_id128_from_string(*p
, &uuid
) >= 0;
124 id
= gpt_partition_type_uuid_to_string(uuid
) ?: "XYZ";
126 r
= gpt_partition_type_uuid_from_string(*p
, &uuid
);
128 return log_error_errno(r
, "Unknown identifier \"%s\".", *p
);
133 r
= show_one(&table
, id
, uuid
, p
== argv
);
139 r
= table_print(table
, NULL
);
141 return table_log_print_error(r
);
147 static int help(void) {
148 _cleanup_free_
char *link
= NULL
;
151 r
= terminal_urlify_man("systemd-id128", "1", &link
);
155 printf("%s [OPTIONS...] COMMAND\n\n"
156 "%sGenerate and print 128bit identifiers.%s\n"
158 " new Generate a new ID\n"
159 " machine-id Print the ID of current machine\n"
160 " boot-id Print the ID of current boot\n"
161 " invocation-id Print the ID of current invocation\n"
162 " show [NAME] Print one or more well-known GPT partition type IDs\n"
163 " help Show this help\n"
165 " -h --help Show this help\n"
166 " -p --pretty Generate samples of program code\n"
167 " -a --app-specific=ID Generate app-specific IDs\n"
168 " -u --uuid Output in UUID format\n"
169 "\nSee the %s for details.\n",
170 program_invocation_short_name
,
178 static int verb_help(int argc
, char **argv
, void *userdata
) {
182 static int parse_argv(int argc
, char *argv
[]) {
187 static const struct option options
[] = {
188 { "help", no_argument
, NULL
, 'h' },
189 { "version", no_argument
, NULL
, ARG_VERSION
},
190 { "pretty", no_argument
, NULL
, 'p' },
191 { "app-specific", required_argument
, NULL
, 'a' },
192 { "uuid", no_argument
, NULL
, 'u' },
201 while ((c
= getopt_long(argc
, argv
, "hpa:u", options
, NULL
)) >= 0)
211 arg_mode
= ID128_PRINT_PRETTY
;
215 r
= sd_id128_from_string(optarg
, &arg_app
);
217 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
221 arg_mode
= ID128_PRINT_UUID
;
228 assert_not_reached();
234 static int id128_main(int argc
, char *argv
[]) {
235 static const Verb verbs
[] = {
236 { "new", VERB_ANY
, 1, 0, verb_new
},
237 { "machine-id", VERB_ANY
, 1, 0, verb_machine_id
},
238 { "boot-id", VERB_ANY
, 1, 0, verb_boot_id
},
239 { "invocation-id", VERB_ANY
, 1, 0, verb_invocation_id
},
240 { "show", VERB_ANY
, VERB_ANY
, 0, verb_show
},
241 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
245 return dispatch_verb(argc
, argv
, verbs
, NULL
);
248 static int run(int argc
, char *argv
[]) {
253 r
= parse_argv(argc
, argv
);
257 return id128_main(argc
, argv
);
260 DEFINE_MAIN_FUNCTION(run
);