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