]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/id128/id128.c
Merge pull request #10860 from keszybz/more-cleanup-2
[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 "util.h"
11 #include "verbs.h"
12
13 static bool arg_pretty = false;
14 static sd_id128_t arg_app = {};
15
16 static int verb_new(int argc, char **argv, void *userdata) {
17 return id128_print_new(arg_pretty);
18 }
19
20 static int verb_machine_id(int argc, char **argv, void *userdata) {
21 sd_id128_t id;
22 int r;
23
24 if (sd_id128_is_null(arg_app))
25 r = sd_id128_get_machine(&id);
26 else
27 r = sd_id128_get_machine_app_specific(arg_app, &id);
28 if (r < 0)
29 return log_error_errno(r, "Failed to get %smachine-ID: %m",
30 sd_id128_is_null(arg_app) ? "" : "app-specific ");
31
32 return id128_pretty_print(id, arg_pretty);
33 }
34
35 static int verb_boot_id(int argc, char **argv, void *userdata) {
36 sd_id128_t id;
37 int r;
38
39 if (sd_id128_is_null(arg_app))
40 r = sd_id128_get_boot(&id);
41 else
42 r = sd_id128_get_boot_app_specific(arg_app, &id);
43 if (r < 0)
44 return log_error_errno(r, "Failed to get %sboot-ID: %m",
45 sd_id128_is_null(arg_app) ? "" : "app-specific ");
46
47 return id128_pretty_print(id, arg_pretty);
48 }
49
50 static int verb_invocation_id(int argc, char **argv, void *userdata) {
51 sd_id128_t id;
52 int r;
53
54 if (!sd_id128_is_null(arg_app))
55 return log_error_errno(EINVAL, "Verb \"invocation-id\" cannot be combined with --app-specific=.");
56
57 r = sd_id128_get_invocation(&id);
58 if (r < 0)
59 return log_error_errno(r, "Failed to get invocation-ID: %m");
60
61 return id128_pretty_print(id, arg_pretty);
62 }
63
64 static int help(void) {
65 _cleanup_free_ char *link = NULL;
66 int r;
67
68 r = terminal_urlify_man("systemd-id128", "1", &link);
69 if (r < 0)
70 return log_oom();
71
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"
77 "\nCommands:\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
85 , link
86 );
87
88 return 0;
89 }
90
91 static int verb_help(int argc, char **argv, void *userdata) {
92 return help();
93 }
94
95 static int parse_argv(int argc, char *argv[]) {
96 enum {
97 ARG_VERSION = 0x100,
98 };
99
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' },
104 {},
105 };
106
107 int c, r;
108
109 assert(argc >= 0);
110 assert(argv);
111
112 while ((c = getopt_long(argc, argv, "hpa:", options, NULL)) >= 0)
113 switch (c) {
114
115 case 'h':
116 return help();
117
118 case ARG_VERSION:
119 return version();
120
121 case 'p':
122 arg_pretty = true;
123 break;
124
125 case 'a':
126 r = sd_id128_from_string(optarg, &arg_app);
127 if (r < 0)
128 return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
129 break;
130
131 case '?':
132 return -EINVAL;
133
134 default:
135 assert_not_reached("Unhandled option");
136 }
137
138 return 1;
139 }
140
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 },
148 {}
149 };
150
151 return dispatch_verb(argc, argv, verbs, NULL);
152 }
153
154 static int run(int argc, char *argv[]) {
155 int r;
156
157 log_parse_environment();
158 log_open();
159
160 r = parse_argv(argc, argv);
161 if (r <= 0)
162 return r;
163
164 return id128_main(argc, argv);
165 }
166
167 DEFINE_MAIN_FUNCTION(run);