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(EINVAL
, "Verb \"invocation-id\" cannot be combined with --app-specific=.");
57 r
= sd_id128_get_invocation(&id
);
59 return log_error_errno(r
, "Failed to get invocation-ID: %m");
61 return id128_pretty_print(id
, arg_pretty
);
64 static int help(void) {
65 _cleanup_free_
char *link
= NULL
;
68 r
= terminal_urlify_man("systemd-id128", "1", &link
);
72 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
73 "Generate and print id128 strings.\n\n"
74 " -h --help Show this help\n"
75 " -p --pretty Generate samples of program code\n"
76 " -a --app-specific=ID Generate app-specific IDs\n"
78 " new Generate a new id128 string\n"
79 " machine-id Print the ID of current machine\n"
80 " boot-id Print the ID of current boot\n"
81 " invocation-id Print the ID of current invocation\n"
82 " help Show this help\n"
83 "\nSee the %s for details.\n"
84 , program_invocation_short_name
91 static int verb_help(int argc
, char **argv
, void *userdata
) {
95 static int parse_argv(int argc
, char *argv
[]) {
100 static const struct option options
[] = {
101 { "help", no_argument
, NULL
, 'h' },
102 { "version", no_argument
, NULL
, ARG_VERSION
},
103 { "app-specific", required_argument
, NULL
, 'a' },
112 while ((c
= getopt_long(argc
, argv
, "hpa:", options
, NULL
)) >= 0)
126 r
= sd_id128_from_string(optarg
, &arg_app
);
128 return log_error_errno(r
, "Failed to parse \"%s\" as application-ID: %m", optarg
);
135 assert_not_reached("Unhandled option");
141 static int id128_main(int argc
, char *argv
[]) {
142 static const Verb verbs
[] = {
143 { "new", VERB_ANY
, 1, 0, verb_new
},
144 { "machine-id", VERB_ANY
, 1, 0, verb_machine_id
},
145 { "boot-id", VERB_ANY
, 1, 0, verb_boot_id
},
146 { "invocation-id", VERB_ANY
, 1, 0, verb_invocation_id
},
147 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
},
151 return dispatch_verb(argc
, argv
, verbs
, NULL
);
154 static int run(int argc
, char *argv
[]) {
157 log_parse_environment();
160 r
= parse_argv(argc
, argv
);
164 return id128_main(argc
, argv
);
167 DEFINE_MAIN_FUNCTION(run
);