]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/id128/id128.c
Merge pull request #13953 from SpencerMichaels/systemd-boot-efistub-id-fix
[thirdparty/systemd.git] / src / id128 / id128.c
CommitLineData
0d1d512f
ZJS
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"
5e332028 8#include "main-func.h"
294bf0c3 9#include "pretty-print.h"
a19fdd66 10#include "terminal-util.h"
0d1d512f
ZJS
11#include "util.h"
12#include "verbs.h"
13
a19fdd66 14static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
206a29b2 15static sd_id128_t arg_app = {};
0d1d512f
ZJS
16
17static int verb_new(int argc, char **argv, void *userdata) {
a19fdd66 18 return id128_print_new(arg_mode);
0d1d512f
ZJS
19}
20
21static 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
a19fdd66 33 return id128_pretty_print(id, arg_mode);
0d1d512f
ZJS
34}
35
36static 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
a19fdd66 48 return id128_pretty_print(id, arg_mode);
0d1d512f
ZJS
49}
50
51static 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))
886cf317
ZJS
56 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
57 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
0d1d512f
ZJS
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
a19fdd66 63 return id128_pretty_print(id, arg_mode);
0d1d512f
ZJS
64}
65
66static 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
a19fdd66
LP
74 printf("%s [OPTIONS...] COMMAND\n\n"
75 "%sGenerate and print 128bit identifiers.%s\n"
20a51f6a 76 "\nCommands:\n"
0d1d512f
ZJS
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"
a19fdd66
LP
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"
0d1d512f
ZJS
87 "\nSee the %s for details.\n"
88 , program_invocation_short_name
a19fdd66 89 , ansi_highlight(), ansi_normal()
0d1d512f
ZJS
90 , link
91 );
92
93 return 0;
94}
95
96static int verb_help(int argc, char **argv, void *userdata) {
97 return help();
98}
99
100static 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 },
be440e09 108 { "pretty", no_argument, NULL, 'p' },
0d1d512f 109 { "app-specific", required_argument, NULL, 'a' },
a19fdd66 110 { "uuid", no_argument, NULL, 'u' },
0d1d512f
ZJS
111 {},
112 };
113
114 int c, r;
115
116 assert(argc >= 0);
117 assert(argv);
118
a19fdd66 119 while ((c = getopt_long(argc, argv, "hpa:u", options, NULL)) >= 0)
0d1d512f
ZJS
120 switch (c) {
121
122 case 'h':
123 return help();
124
125 case ARG_VERSION:
126 return version();
127
128 case 'p':
a19fdd66 129 arg_mode = ID128_PRINT_PRETTY;
0d1d512f
ZJS
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
a19fdd66
LP
138 case 'u':
139 arg_mode = ID128_PRINT_UUID;
140 break;
141
0d1d512f
ZJS
142 case '?':
143 return -EINVAL;
144
145 default:
146 assert_not_reached("Unhandled option");
147 }
148
149 return 1;
150}
151
152static 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
3c79f0b3 165static int run(int argc, char *argv[]) {
0d1d512f
ZJS
166 int r;
167
1a043959 168 log_show_color(true);
0d1d512f
ZJS
169 log_parse_environment();
170 log_open();
171
172 r = parse_argv(argc, argv);
173 if (r <= 0)
3c79f0b3 174 return r;
0d1d512f 175
3c79f0b3 176 return id128_main(argc, argv);
0d1d512f 177}
3c79f0b3
ZJS
178
179DEFINE_MAIN_FUNCTION(run);