1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "id128-print.h"
9 #include "pretty-print.h"
13 static bool arg_pretty
= false;
14 static sd_id128_t arg_app
= {};
16 static int verb_new(int argc
, char **argv
, void *userdata
) {
17 return id128_print_new(arg_pretty
);
20 static int verb_machine_id(int argc
, char **argv
, void *userdata
) {
24 if (sd_id128_is_null(arg_app
))
25 r
= sd_id128_get_machine(&id
);
27 r
= sd_id128_get_machine_app_specific(arg_app
, &id
);
29 return log_error_errno(r
, "Failed to get %smachine-ID: %m",
30 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
32 return id128_pretty_print(id
, arg_pretty
);
35 static int verb_boot_id(int argc
, char **argv
, void *userdata
) {
39 if (sd_id128_is_null(arg_app
))
40 r
= sd_id128_get_boot(&id
);
42 r
= sd_id128_get_boot_app_specific(arg_app
, &id
);
44 return log_error_errno(r
, "Failed to get %sboot-ID: %m",
45 sd_id128_is_null(arg_app
) ? "" : "app-specific ");
47 return id128_pretty_print(id
, arg_pretty
);
50 static int verb_invocation_id(int argc
, char **argv
, void *userdata
) {
54 if (!sd_id128_is_null(arg_app
))
55 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
56 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
58 r
= sd_id128_get_invocation(&id
);
60 return log_error_errno(r
, "Failed to get invocation-ID: %m");
62 return id128_pretty_print(id
, arg_pretty
);
65 static int help(void) {
66 _cleanup_free_
char *link
= NULL
;
69 r
= terminal_urlify_man("systemd-id128", "1", &link
);
73 printf("%s [OPTIONS...] {COMMAND}\n\n"
74 "Generate and print id128 strings.\n\n"
75 " -h --help Show this help\n"
76 " -p --pretty Generate samples of program code\n"
77 " -a --app-specific=ID Generate app-specific IDs\n"
79 " new Generate a new id128 string\n"
80 " machine-id Print the ID of current machine\n"
81 " boot-id Print the ID of current boot\n"
82 " invocation-id Print the ID of current invocation\n"
83 " help Show this help\n"
84 "\nSee the %s for details.\n"
85 , program_invocation_short_name
92 static int verb_help(int argc
, char **argv
, void *userdata
) {
96 static int parse_argv(int argc
, char *argv
[]) {
101 static const struct option options
[] = {
102 { "help", no_argument
, NULL
, 'h' },
103 { "version", no_argument
, NULL
, ARG_VERSION
},
104 { "pretty", no_argument
, NULL
, 'p' },
105 { "app-specific", required_argument
, NULL
, 'a' },
114 while ((c
= getopt_long(argc
, argv
, "hpa:", options
, NULL
)) >= 0)
128 r
= sd_id128_from_string(optarg
, &arg_app
);
130 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
137 assert_not_reached("Unhandled option");
143 static int id128_main(int argc
, char *argv
[]) {
144 static const Verb verbs
[] = {
145 { "new", VERB_ANY
, 1, 0, verb_new
},
146 { "machine-id", VERB_ANY
, 1, 0, verb_machine_id
},
147 { "boot-id", VERB_ANY
, 1, 0, verb_boot_id
},
148 { "invocation-id", VERB_ANY
, 1, 0, verb_invocation_id
},
149 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
153 return dispatch_verb(argc
, argv
, verbs
, NULL
);
156 static int run(int argc
, char *argv
[]) {
159 log_show_color(true);
160 log_parse_environment();
163 r
= parse_argv(argc
, argv
);
167 return id128_main(argc
, argv
);
170 DEFINE_MAIN_FUNCTION(run
);