| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "build.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "gpt.h" |
| 9 |
#include "id128-print.h" |
| 10 |
#include "id128-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "main-func.h" |
| 13 |
#include "options.h" |
| 14 |
#include "parse-argument.h" |
| 15 |
#include "pretty-print.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "strv.h" |
| 18 |
#include "verbs.h" |
| 19 |
|
| 20 |
static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128; |
| 21 |
static sd_id128_t arg_app = SD_ID128_NULL; |
| 22 |
static bool arg_value = false; |
| 23 |
static PagerFlags arg_pager_flags = 0; |
| 24 |
static bool arg_legend = true; |
| 25 |
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; |
| 26 |
|
| 27 |
VERB_NOARG(verb_new, "new", "Generate a new ID"); |
| 28 |
static int verb_new(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 29 |
return id128_print_new(arg_mode); |
| 30 |
} |
| 31 |
|
| 32 |
VERB_NOARG(verb_machine_id, "machine-id", "Print the ID of current machine"); |
| 33 |
static int verb_machine_id(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 34 |
sd_id128_t id; |
| 35 |
int r; |
| 36 |
|
| 37 |
if (sd_id128_is_null(arg_app)) |
| 38 |
r = sd_id128_get_machine(&id); |
| 39 |
else |
| 40 |
r = sd_id128_get_machine_app_specific(arg_app, &id); |
| 41 |
if (r < 0) |
| 42 |
return log_error_errno(r, "Failed to get %smachine-ID: %m", |
| 43 |
sd_id128_is_null(arg_app) ? "" : "app-specific "); |
| 44 |
|
| 45 |
return id128_pretty_print(id, arg_mode); |
| 46 |
} |
| 47 |
|
| 48 |
VERB_NOARG(verb_boot_id, "boot-id", "Print the ID of current boot"); |
| 49 |
static int verb_boot_id(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 50 |
sd_id128_t id; |
| 51 |
int r; |
| 52 |
|
| 53 |
if (sd_id128_is_null(arg_app)) |
| 54 |
r = sd_id128_get_boot(&id); |
| 55 |
else |
| 56 |
r = sd_id128_get_boot_app_specific(arg_app, &id); |
| 57 |
if (r < 0) |
| 58 |
return log_error_errno(r, "Failed to get %sboot-ID: %m", |
| 59 |
sd_id128_is_null(arg_app) ? "" : "app-specific "); |
| 60 |
|
| 61 |
return id128_pretty_print(id, arg_mode); |
| 62 |
} |
| 63 |
|
| 64 |
VERB_NOARG(verb_invocation_id, "invocation-id", "Print the ID of current invocation"); |
| 65 |
static int verb_invocation_id(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 66 |
sd_id128_t id; |
| 67 |
int r; |
| 68 |
|
| 69 |
if (!sd_id128_is_null(arg_app)) |
| 70 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 71 |
"Verb \"invocation-id\" cannot be combined with --app-specific=."); |
| 72 |
|
| 73 |
r = sd_id128_get_invocation(&id); |
| 74 |
if (r < 0) |
| 75 |
return log_error_errno(r, "Failed to get invocation-ID: %m"); |
| 76 |
|
| 77 |
return id128_pretty_print(id, arg_mode); |
| 78 |
} |
| 79 |
|
| 80 |
VERB_NOARG(verb_var_uuid, "var-partition-uuid", "Print the UUID for the /var/ partition"); |
| 81 |
static int verb_var_uuid(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 82 |
sd_id128_t id; |
| 83 |
int r; |
| 84 |
|
| 85 |
if (!sd_id128_is_null(arg_app)) |
| 86 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 87 |
"Verb \"var-partition-uuid\" cannot be combined with --app-specific=."); |
| 88 |
|
| 89 |
/* The DPS says that the UUID for /var/ should be keyed with machine-id. */ |
| 90 |
r = sd_id128_get_machine_app_specific(SD_GPT_VAR, &id); |
| 91 |
if (r < 0) |
| 92 |
return log_error_errno(r, "Failed to generate machine-specific /var/ UUID: %m"); |
| 93 |
|
| 94 |
return id128_pretty_print(id, arg_mode); |
| 95 |
} |
| 96 |
|
| 97 |
static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) { |
| 98 |
int r; |
| 99 |
|
| 100 |
assert(table); |
| 101 |
|
| 102 |
if (!name) |
| 103 |
name = "XYZ"; |
| 104 |
|
| 105 |
if (arg_mode == ID128_PRINT_PRETTY) { |
| 106 |
_cleanup_free_ char *id = NULL; |
| 107 |
|
| 108 |
id = strreplace(name, "-", "_"); |
| 109 |
if (!id) |
| 110 |
return log_oom(); |
| 111 |
|
| 112 |
ascii_strupper(id); |
| 113 |
|
| 114 |
r = id128_pretty_print_sample(id, uuid); |
| 115 |
if (r < 0) |
| 116 |
return r; |
| 117 |
if (!first) |
| 118 |
puts(""); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
if (arg_value) |
| 123 |
return id128_pretty_print(uuid, arg_mode); |
| 124 |
|
| 125 |
if (!*table) { |
| 126 |
*table = table_new("name", "id"); |
| 127 |
if (!*table) |
| 128 |
return log_oom(); |
| 129 |
|
| 130 |
table_set_width(*table, 0); |
| 131 |
} |
| 132 |
|
| 133 |
return table_add_many(*table, |
| 134 |
TABLE_STRING, name, |
| 135 |
arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID, uuid); |
| 136 |
} |
| 137 |
|
| 138 |
VERB(verb_show, "show", "[NAME|UUID]", VERB_ANY, VERB_ANY, 0, "Print one or more UUIDs"); |
| 139 |
static int verb_show(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 140 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 141 |
int r; |
| 142 |
|
| 143 |
argv = strv_skip(argv, 1); |
| 144 |
if (argv) |
| 145 |
STRV_FOREACH(p, argv) { |
| 146 |
sd_id128_t uuid; |
| 147 |
const char *id = NULL; |
| 148 |
|
| 149 |
/* Check if the argument is an actual UUID first */ |
| 150 |
bool is_uuid = sd_id128_from_string(*p, &uuid) >= 0; |
| 151 |
|
| 152 |
if (is_uuid) |
| 153 |
id = gpt_partition_type_uuid_to_string(uuid); |
| 154 |
else { |
| 155 |
GptPartitionType type; |
| 156 |
|
| 157 |
r = gpt_partition_type_from_string(*p, &type); |
| 158 |
if (r < 0) |
| 159 |
return log_error_errno(r, "Unknown identifier \"%s\".", *p); |
| 160 |
|
| 161 |
uuid = type.uuid; |
| 162 |
id = *p; |
| 163 |
} |
| 164 |
|
| 165 |
if (!sd_id128_is_null(arg_app)) |
| 166 |
assert_se(sd_id128_get_app_specific(uuid, arg_app, &uuid) >= 0); |
| 167 |
|
| 168 |
r = show_one(&table, id, uuid, p == argv); |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
} |
| 172 |
else { |
| 173 |
if (!sd_id128_is_null(arg_app)) |
| 174 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 175 |
"'show --app-specific=' can only be used with explicit UUID input."); |
| 176 |
|
| 177 |
for (const GptPartitionType *e = gpt_partition_type_table; e->name; e++) { |
| 178 |
r = show_one(&table, e->name, e->uuid, e == gpt_partition_type_table); |
| 179 |
if (r < 0) |
| 180 |
return r; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (table) { |
| 185 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 186 |
if (r < 0) |
| 187 |
return r; |
| 188 |
} |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
static int help(void) { |
| 194 |
_cleanup_free_ char *link = NULL; |
| 195 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 196 |
int r; |
| 197 |
|
| 198 |
r = terminal_urlify_man("systemd-id128", "1", &link); |
| 199 |
if (r < 0) |
| 200 |
return log_oom(); |
| 201 |
|
| 202 |
r = option_parser_get_help_table(&options); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
r = verbs_get_help_table(&verbs); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
/* Make the 1st column same width in both tables */ |
| 211 |
(void) table_sync_column_widths(0, options, verbs); |
| 212 |
|
| 213 |
printf("%s [OPTIONS...] COMMAND\n\n" |
| 214 |
"%sGenerate and print 128-bit identifiers.%s\n" |
| 215 |
"\nCommands:\n", |
| 216 |
program_invocation_short_name, |
| 217 |
ansi_highlight(), |
| 218 |
ansi_normal()); |
| 219 |
r = table_print_or_warn(verbs); |
| 220 |
if (r < 0) |
| 221 |
return r; |
| 222 |
|
| 223 |
printf("\nOptions:\n"); |
| 224 |
r = table_print_or_warn(options); |
| 225 |
if (r < 0) |
| 226 |
return r; |
| 227 |
|
| 228 |
printf("\nSee the %s for details.\n", link); |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
VERB_COMMON_HELP(help); |
| 233 |
|
| 234 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 235 |
int r; |
| 236 |
|
| 237 |
assert(argc >= 0); |
| 238 |
assert(argv); |
| 239 |
|
| 240 |
OptionParser opts = { argc, argv }; |
| 241 |
|
| 242 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 243 |
switch (c) { |
| 244 |
OPTION_COMMON_HELP: |
| 245 |
return help(); |
| 246 |
|
| 247 |
OPTION_COMMON_VERSION: |
| 248 |
return version(); |
| 249 |
|
| 250 |
OPTION_COMMON_NO_PAGER: |
| 251 |
arg_pager_flags |= PAGER_DISABLE; |
| 252 |
break; |
| 253 |
|
| 254 |
OPTION_COMMON_NO_LEGEND: |
| 255 |
arg_legend = false; |
| 256 |
break; |
| 257 |
|
| 258 |
OPTION_LONG("json", "FORMAT", |
| 259 |
"Output inspection data in JSON (takes one of pretty, short, off)"): |
| 260 |
r = parse_json_argument(opts.arg, &arg_json_format_flags); |
| 261 |
if (r <= 0) |
| 262 |
return r; |
| 263 |
break; |
| 264 |
|
| 265 |
OPTION_COMMON_LOWERCASE_J: |
| 266 |
arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; |
| 267 |
break; |
| 268 |
|
| 269 |
OPTION('p', "pretty", NULL, "Generate samples of program code"): |
| 270 |
arg_mode = ID128_PRINT_PRETTY; |
| 271 |
arg_value = false; |
| 272 |
break; |
| 273 |
|
| 274 |
OPTION('P', "value", NULL, "Only print the value"): |
| 275 |
arg_value = true; |
| 276 |
if (arg_mode == ID128_PRINT_PRETTY) |
| 277 |
arg_mode = ID128_PRINT_ID128; |
| 278 |
break; |
| 279 |
|
| 280 |
OPTION('a', "app-specific", "ID", "Generate app-specific IDs"): |
| 281 |
r = id128_from_string_nonzero(opts.arg, &arg_app); |
| 282 |
if (r == -ENXIO) |
| 283 |
return log_error_errno(r, "Application ID cannot be all zeros."); |
| 284 |
if (r < 0) |
| 285 |
return log_error_errno(r, "Failed to parse \"%s\" as application ID: %m", opts.arg); |
| 286 |
break; |
| 287 |
|
| 288 |
OPTION('u', "uuid", NULL, "Output in UUID format"): |
| 289 |
arg_mode = ID128_PRINT_UUID; |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
*ret_args = option_parser_get_args(&opts); |
| 294 |
return 1; |
| 295 |
} |
| 296 |
|
| 297 |
static int run(int argc, char *argv[]) { |
| 298 |
int r; |
| 299 |
|
| 300 |
log_setup(); |
| 301 |
|
| 302 |
char **args = NULL; /* unnecessary initialization to appease gcc <= 13 */ |
| 303 |
r = parse_argv(argc, argv, &args); |
| 304 |
if (r <= 0) |
| 305 |
return r; |
| 306 |
|
| 307 |
return dispatch_verb(args, NULL); |
| 308 |
} |
| 309 |
|
| 310 |
DEFINE_MAIN_FUNCTION(run); |