]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/id128/id128.c
Merge pull request #13953 from SpencerMichaels/systemd-boot-efistub-id-fix
[thirdparty/systemd.git] / src / id128 / id128.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4 #include <stdio.h>
5
6 #include "alloc-util.h"
7 #include "id128-print.h"
8 #include "main-func.h"
9 #include "pretty-print.h"
10 #include "terminal-util.h"
11 #include "util.h"
12 #include "verbs.h"
13
14 static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
15 static sd_id128_t arg_app = {};
16
17 static int verb_new(int argc, char **argv, void *userdata) {
18 return id128_print_new(arg_mode);
19 }
20
21 static int verb_machine_id(int argc, char **argv, void *userdata) {
22 sd_id128_t id;
23 int r;
24
25 if (sd_id128_is_null(arg_app))
26 r = sd_id128_get_machine(&id);
27 else
28 r = sd_id128_get_machine_app_specific(arg_app, &id);
29 if (r < 0)
30 return log_error_errno(r, "Failed to get %smachine-ID: %m",
31 sd_id128_is_null(arg_app) ? "" : "app-specific ");
32
33 return id128_pretty_print(id, arg_mode);
34 }
35
36 static int verb_boot_id(int argc, char **argv, void *userdata) {
37 sd_id128_t id;
38 int r;
39
40 if (sd_id128_is_null(arg_app))
41 r = sd_id128_get_boot(&id);
42 else
43 r = sd_id128_get_boot_app_specific(arg_app, &id);
44 if (r < 0)
45 return log_error_errno(r, "Failed to get %sboot-ID: %m",
46 sd_id128_is_null(arg_app) ? "" : "app-specific ");
47
48 return id128_pretty_print(id, arg_mode);
49 }
50
51 static int verb_invocation_id(int argc, char **argv, void *userdata) {
52 sd_id128_t id;
53 int r;
54
55 if (!sd_id128_is_null(arg_app))
56 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
57 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
58
59 r = sd_id128_get_invocation(&id);
60 if (r < 0)
61 return log_error_errno(r, "Failed to get invocation-ID: %m");
62
63 return id128_pretty_print(id, arg_mode);
64 }
65
66 static int help(void) {
67 _cleanup_free_ char *link = NULL;
68 int r;
69
70 r = terminal_urlify_man("systemd-id128", "1", &link);
71 if (r < 0)
72 return log_oom();
73
74 printf("%s [OPTIONS...] COMMAND\n\n"
75 "%sGenerate and print 128bit identifiers.%s\n"
76 "\nCommands:\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 "\nOptions:\n"
83 " -h --help Show this help\n"
84 " -p --pretty Generate samples of program code\n"
85 " -a --app-specific=ID Generate app-specific IDs\n"
86 " -u --uuid Output in UUID format\n"
87 "\nSee the %s for details.\n"
88 , program_invocation_short_name
89 , ansi_highlight(), ansi_normal()
90 , link
91 );
92
93 return 0;
94 }
95
96 static int verb_help(int argc, char **argv, void *userdata) {
97 return help();
98 }
99
100 static int parse_argv(int argc, char *argv[]) {
101 enum {
102 ARG_VERSION = 0x100,
103 };
104
105 static const struct option options[] = {
106 { "help", no_argument, NULL, 'h' },
107 { "version", no_argument, NULL, ARG_VERSION },
108 { "pretty", no_argument, NULL, 'p' },
109 { "app-specific", required_argument, NULL, 'a' },
110 { "uuid", no_argument, NULL, 'u' },
111 {},
112 };
113
114 int c, r;
115
116 assert(argc >= 0);
117 assert(argv);
118
119 while ((c = getopt_long(argc, argv, "hpa:u", options, NULL)) >= 0)
120 switch (c) {
121
122 case 'h':
123 return help();
124
125 case ARG_VERSION:
126 return version();
127
128 case 'p':
129 arg_mode = ID128_PRINT_PRETTY;
130 break;
131
132 case 'a':
133 r = sd_id128_from_string(optarg, &arg_app);
134 if (r < 0)
135 return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
136 break;
137
138 case 'u':
139 arg_mode = ID128_PRINT_UUID;
140 break;
141
142 case '?':
143 return -EINVAL;
144
145 default:
146 assert_not_reached("Unhandled option");
147 }
148
149 return 1;
150 }
151
152 static int id128_main(int argc, char *argv[]) {
153 static const Verb verbs[] = {
154 { "new", VERB_ANY, 1, 0, verb_new },
155 { "machine-id", VERB_ANY, 1, 0, verb_machine_id },
156 { "boot-id", VERB_ANY, 1, 0, verb_boot_id },
157 { "invocation-id", VERB_ANY, 1, 0, verb_invocation_id },
158 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
159 {}
160 };
161
162 return dispatch_verb(argc, argv, verbs, NULL);
163 }
164
165 static int run(int argc, char *argv[]) {
166 int r;
167
168 log_show_color(true);
169 log_parse_environment();
170 log_open();
171
172 r = parse_argv(argc, argv);
173 if (r <= 0)
174 return r;
175
176 return id128_main(argc, argv);
177 }
178
179 DEFINE_MAIN_FUNCTION(run);