]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/id128/id128.c
journalctl: make libqrencode a weak dependency
[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 "gpt.h"
8 #include "id128-print.h"
9 #include "main-func.h"
10 #include "pretty-print.h"
11 #include "strv.h"
12 #include "format-table.h"
13 #include "terminal-util.h"
14 #include "util.h"
15 #include "verbs.h"
16
17 static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
18 static sd_id128_t arg_app = {};
19
20 static int verb_new(int argc, char **argv, void *userdata) {
21 return id128_print_new(arg_mode);
22 }
23
24 static int verb_machine_id(int argc, char **argv, void *userdata) {
25 sd_id128_t id;
26 int r;
27
28 if (sd_id128_is_null(arg_app))
29 r = sd_id128_get_machine(&id);
30 else
31 r = sd_id128_get_machine_app_specific(arg_app, &id);
32 if (r < 0)
33 return log_error_errno(r, "Failed to get %smachine-ID: %m",
34 sd_id128_is_null(arg_app) ? "" : "app-specific ");
35
36 return id128_pretty_print(id, arg_mode);
37 }
38
39 static int verb_boot_id(int argc, char **argv, void *userdata) {
40 sd_id128_t id;
41 int r;
42
43 if (sd_id128_is_null(arg_app))
44 r = sd_id128_get_boot(&id);
45 else
46 r = sd_id128_get_boot_app_specific(arg_app, &id);
47 if (r < 0)
48 return log_error_errno(r, "Failed to get %sboot-ID: %m",
49 sd_id128_is_null(arg_app) ? "" : "app-specific ");
50
51 return id128_pretty_print(id, arg_mode);
52 }
53
54 static int verb_invocation_id(int argc, char **argv, void *userdata) {
55 sd_id128_t id;
56 int r;
57
58 if (!sd_id128_is_null(arg_app))
59 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
60 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
61
62 r = sd_id128_get_invocation(&id);
63 if (r < 0)
64 return log_error_errno(r, "Failed to get invocation-ID: %m");
65
66 return id128_pretty_print(id, arg_mode);
67 }
68
69 static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) {
70 int r;
71
72 if (arg_mode == ID128_PRINT_PRETTY) {
73 _cleanup_free_ char *id = NULL;
74
75 id = strreplace(name, "-", "_");
76 if (!id)
77 return log_oom();
78
79 ascii_strupper(id);
80
81 r = id128_pretty_print_sample(id, uuid);
82 if (r < 0)
83 return r;
84 if (!first)
85 puts("");
86 return 0;
87
88 } else {
89 if (!*table) {
90 *table = table_new("name", "id");
91 if (!*table)
92 return log_oom();
93 table_set_width(*table, 0);
94 }
95
96 return table_add_many(*table,
97 TABLE_STRING, name,
98 arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID,
99 uuid);
100 }
101 }
102
103 static int verb_show(int argc, char **argv, void *userdata) {
104 _cleanup_(table_unrefp) Table *table = NULL;
105 char **p;
106 int r;
107
108 argv = strv_skip(argv, 1);
109 if (strv_isempty(argv))
110 for (const GptPartitionType *e = gpt_partition_type_table; e->name; e++) {
111 r = show_one(&table, e->name, e->uuid, e == gpt_partition_type_table);
112 if (r < 0)
113 return r;
114 }
115 else
116 STRV_FOREACH(p, argv) {
117 sd_id128_t uuid;
118 bool have_uuid;
119 const char *id;
120
121 /* Check if the argument is an actual UUID first */
122 have_uuid = sd_id128_from_string(*p, &uuid) >= 0;
123
124 if (have_uuid)
125 id = gpt_partition_type_uuid_to_string(uuid) ?: "XYZ";
126 else {
127 r = gpt_partition_type_uuid_from_string(*p, &uuid);
128 if (r < 0)
129 return log_error_errno(r, "Unknown identifier \"%s\".", *p);
130
131 id = *p;
132 }
133
134 r = show_one(&table, id, uuid, p == argv);
135 if (r < 0)
136 return r;
137 }
138
139 if (table) {
140 r = table_print(table, NULL);
141 if (r < 0)
142 return log_error_errno(r, "Failed to print table: %m");
143 }
144
145 return 0;
146 }
147
148 static int help(void) {
149 _cleanup_free_ char *link = NULL;
150 int r;
151
152 r = terminal_urlify_man("systemd-id128", "1", &link);
153 if (r < 0)
154 return log_oom();
155
156 printf("%s [OPTIONS...] COMMAND\n\n"
157 "%sGenerate and print 128bit identifiers.%s\n"
158 "\nCommands:\n"
159 " new Generate a new ID\n"
160 " machine-id Print the ID of current machine\n"
161 " boot-id Print the ID of current boot\n"
162 " invocation-id Print the ID of current invocation\n"
163 " show [NAME] Print one or more well-known IDs\n"
164 " help Show this help\n"
165 "\nOptions:\n"
166 " -h --help Show this help\n"
167 " -p --pretty Generate samples of program code\n"
168 " -a --app-specific=ID Generate app-specific IDs\n"
169 " -u --uuid Output in UUID format\n"
170 "\nSee the %s for details.\n"
171 , program_invocation_short_name
172 , ansi_highlight(), ansi_normal()
173 , link
174 );
175
176 return 0;
177 }
178
179 static int verb_help(int argc, char **argv, void *userdata) {
180 return help();
181 }
182
183 static int parse_argv(int argc, char *argv[]) {
184 enum {
185 ARG_VERSION = 0x100,
186 };
187
188 static const struct option options[] = {
189 { "help", no_argument, NULL, 'h' },
190 { "version", no_argument, NULL, ARG_VERSION },
191 { "pretty", no_argument, NULL, 'p' },
192 { "app-specific", required_argument, NULL, 'a' },
193 { "uuid", no_argument, NULL, 'u' },
194 {},
195 };
196
197 int c, r;
198
199 assert(argc >= 0);
200 assert(argv);
201
202 while ((c = getopt_long(argc, argv, "hpa:u", options, NULL)) >= 0)
203 switch (c) {
204
205 case 'h':
206 return help();
207
208 case ARG_VERSION:
209 return version();
210
211 case 'p':
212 arg_mode = ID128_PRINT_PRETTY;
213 break;
214
215 case 'a':
216 r = sd_id128_from_string(optarg, &arg_app);
217 if (r < 0)
218 return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
219 break;
220
221 case 'u':
222 arg_mode = ID128_PRINT_UUID;
223 break;
224
225 case '?':
226 return -EINVAL;
227
228 default:
229 assert_not_reached("Unhandled option");
230 }
231
232 return 1;
233 }
234
235 static int id128_main(int argc, char *argv[]) {
236 static const Verb verbs[] = {
237 { "new", VERB_ANY, 1, 0, verb_new },
238 { "machine-id", VERB_ANY, 1, 0, verb_machine_id },
239 { "boot-id", VERB_ANY, 1, 0, verb_boot_id },
240 { "invocation-id", VERB_ANY, 1, 0, verb_invocation_id },
241 { "show", VERB_ANY, VERB_ANY, 0, verb_show },
242 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
243 {}
244 };
245
246 return dispatch_verb(argc, argv, verbs, NULL);
247 }
248
249 static int run(int argc, char *argv[]) {
250 int r;
251
252 log_setup_cli();
253
254 r = parse_argv(argc, argv);
255 if (r <= 0)
256 return r;
257
258 return id128_main(argc, argv);
259 }
260
261 DEFINE_MAIN_FUNCTION(run);