1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "id128-print.h"
8 #include "terminal-util.h"
12 static bool arg_pretty
= false;
13 static sd_id128_t arg_app
= {};
15 static int verb_new(int argc
, char **argv
, void *userdata
) {
16 return id128_print_new(arg_pretty
);
19 static int verb_machine_id(int argc
, char **argv
, void *userdata
) {
23 if (sd_id128_is_null(arg_app
))
24 r
= sd_id128_get_machine(&id
);
26 r
= sd_id128_get_machine_app_specific(arg_app
, &id
);
28 return log_error_errno(r
, "Failed to get %smachine-ID: %m",
29 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
31 return id128_pretty_print(id
, arg_pretty
);
34 static int verb_boot_id(int argc
, char **argv
, void *userdata
) {
38 if (sd_id128_is_null(arg_app
))
39 r
= sd_id128_get_boot(&id
);
41 r
= sd_id128_get_boot_app_specific(arg_app
, &id
);
43 return log_error_errno(r
, "Failed to get %sboot-ID: %m",
44 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
46 return id128_pretty_print(id
, arg_pretty
);
49 static int verb_invocation_id(int argc
, char **argv
, void *userdata
) {
53 if (!sd_id128_is_null(arg_app
))
54 return log_error_errno(EINVAL
, "Verb \"invocation-id\" cannot be combined with --app-specific=.");
56 r
= sd_id128_get_invocation(&id
);
58 return log_error_errno(r
, "Failed to get invocation-ID: %m");
60 return id128_pretty_print(id
, arg_pretty
);
63 static int help(void) {
64 _cleanup_free_
char *link
= NULL
;
67 r
= terminal_urlify_man("systemd-id128", "1", &link
);
71 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
72 "Generate and print id128 strings.\n\n"
73 " -h --help Show this help\n"
74 " -p --pretty Generate samples of program code\n"
75 " -a --app-specific=ID Generate app-specific IDs\n"
77 " new Generate a new id128 string\n"
78 " machine-id Print the ID of current machine\n"
79 " boot-id Print the ID of current boot\n"
80 " invocation-id Print the ID of current invocation\n"
81 " help Show this help\n"
82 "\nSee the %s for details.\n"
83 , program_invocation_short_name
90 static int verb_help(int argc
, char **argv
, void *userdata
) {
94 static int parse_argv(int argc
, char *argv
[]) {
99 static const struct option options
[] = {
100 { "help", no_argument
, NULL
, 'h' },
101 { "version", no_argument
, NULL
, ARG_VERSION
},
102 { "app-specific", required_argument
, NULL
, 'a' },
111 while ((c
= getopt_long(argc
, argv
, "hpa:", options
, NULL
)) >= 0)
125 r
= sd_id128_from_string(optarg
, &arg_app
);
127 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
134 assert_not_reached("Unhandled option");
140 static int id128_main(int argc
, char *argv
[]) {
141 static const Verb verbs
[] = {
142 { "new", VERB_ANY
, 1, 0, verb_new
},
143 { "machine-id", VERB_ANY
, 1, 0, verb_machine_id
},
144 { "boot-id", VERB_ANY
, 1, 0, verb_boot_id
},
145 { "invocation-id", VERB_ANY
, 1, 0, verb_invocation_id
},
146 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
150 return dispatch_verb(argc
, argv
, verbs
, NULL
);
153 int main(int argc
, char *argv
[]) {
156 log_parse_environment();
159 r
= parse_argv(argc
, argv
);
163 r
= id128_main(argc
, argv
);
166 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;