1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
8 #include "format-table.h"
10 #include "id128-print.h"
11 #include "id128-util.h"
13 #include "main-func.h"
14 #include "parse-argument.h"
15 #include "pretty-print.h"
16 #include "string-util.h"
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
;
27 static int verb_new(int argc
, char **argv
, void *userdata
) {
28 return id128_print_new(arg_mode
);
31 static int verb_machine_id(int argc
, char **argv
, void *userdata
) {
35 if (sd_id128_is_null(arg_app
))
36 r
= sd_id128_get_machine(&id
);
38 r
= sd_id128_get_machine_app_specific(arg_app
, &id
);
40 return log_error_errno(r
, "Failed to get %smachine-ID: %m",
41 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
43 return id128_pretty_print(id
, arg_mode
);
46 static int verb_boot_id(int argc
, char **argv
, void *userdata
) {
50 if (sd_id128_is_null(arg_app
))
51 r
= sd_id128_get_boot(&id
);
53 r
= sd_id128_get_boot_app_specific(arg_app
, &id
);
55 return log_error_errno(r
, "Failed to get %sboot-ID: %m",
56 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
58 return id128_pretty_print(id
, arg_mode
);
61 static int verb_invocation_id(int argc
, char **argv
, void *userdata
) {
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=.");
69 r
= sd_id128_get_invocation(&id
);
71 return log_error_errno(r
, "Failed to get invocation-ID: %m");
73 return id128_pretty_print(id
, arg_mode
);
76 static int verb_var_uuid(int argc
, char **argv
, void *userdata
) {
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=.");
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
);
87 return log_error_errno(r
, "Failed to generate machine-specific /var/ UUID: %m");
89 return id128_pretty_print(id
, arg_mode
);
92 static int show_one(Table
**table
, const char *name
, sd_id128_t uuid
, bool first
) {
100 if (arg_mode
== ID128_PRINT_PRETTY
) {
101 _cleanup_free_
char *id
= NULL
;
103 id
= strreplace(name
, "-", "_");
109 r
= id128_pretty_print_sample(id
, uuid
);
118 return id128_pretty_print(uuid
, arg_mode
);
121 *table
= table_new("name", "id");
125 table_set_width(*table
, 0);
128 return table_add_many(*table
,
130 arg_mode
== ID128_PRINT_ID128
? TABLE_ID128
: TABLE_UUID
, uuid
);
133 static int verb_show(int argc
, char **argv
, void *userdata
) {
134 _cleanup_(table_unrefp
) Table
*table
= NULL
;
137 argv
= strv_skip(argv
, 1);
139 STRV_FOREACH(p
, argv
) {
141 const char *id
= NULL
;
143 /* Check if the argument is an actual UUID first */
144 bool is_uuid
= sd_id128_from_string(*p
, &uuid
) >= 0;
147 id
= gpt_partition_type_uuid_to_string(uuid
);
149 GptPartitionType type
;
151 r
= gpt_partition_type_from_string(*p
, &type
);
153 return log_error_errno(r
, "Unknown identifier \"%s\".", *p
);
159 if (!sd_id128_is_null(arg_app
))
160 assert_se(sd_id128_get_app_specific(uuid
, arg_app
, &uuid
) >= 0);
162 r
= show_one(&table
, id
, uuid
, p
== argv
);
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.");
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
);
179 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
187 static int help(void) {
188 _cleanup_free_
char *link
= NULL
;
191 r
= terminal_urlify_man("systemd-id128", "1", &link
);
195 printf("%s [OPTIONS...] COMMAND\n\n"
196 "%sGenerate and print 128-bit identifiers.%s\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"
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
,
226 static int verb_help(int argc
, char **argv
, void *userdata
) {
230 static int parse_argv(int argc
, char *argv
[]) {
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' },
256 while ((c
= getopt_long(argc
, argv
, "hpa:uPj", options
, NULL
)) >= 0)
266 arg_pager_flags
|= PAGER_DISABLE
;
274 arg_json_format_flags
= SD_JSON_FORMAT_PRETTY_AUTO
|SD_JSON_FORMAT_COLOR_AUTO
;
278 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
284 arg_mode
= ID128_PRINT_PRETTY
;
290 if (arg_mode
== ID128_PRINT_PRETTY
)
291 arg_mode
= ID128_PRINT_ID128
;
295 r
= id128_from_string_nonzero(optarg
, &arg_app
);
297 return log_error_errno(r
, "Application ID cannot be all zeros.");
299 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
303 arg_mode
= ID128_PRINT_UUID
;
310 assert_not_reached();
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
},
328 return dispatch_verb(argc
, argv
, verbs
, NULL
);
331 static int run(int argc
, char *argv
[]) {
336 r
= parse_argv(argc
, argv
);
340 return id128_main(argc
, argv
);
343 DEFINE_MAIN_FUNCTION(run
);