]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/id128/id128.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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(SYNTHETIC_ERRNO(EINVAL),
56 "Verb \"invocation-id\" cannot be combined with --app-specific=.");
57
58 r = sd_id128_get_invocation(&id);
59 if (r < 0)
60 return log_error_errno(r, "Failed to get invocation-ID: %m");
61
62 return id128_pretty_print(id, arg_pretty);
63 }
64
65 static int help(void) {
66 _cleanup_free_ char *link = NULL;
67 int r;
68
69 r = terminal_urlify_man("systemd-id128", "1", &link);
70 if (r < 0)
71 return log_oom();
72
73 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
74 "Generate and print id128 strings.\n\n"
75 " -h --help Show this help\n"
76 " -p --pretty Generate samples of program code\n"
77 " -a --app-specific=ID Generate app-specific IDs\n"
78 "\nCommands:\n"
79 " new Generate a new id128 string\n"
80 " machine-id Print the ID of current machine\n"
81 " boot-id Print the ID of current boot\n"
82 " invocation-id Print the ID of current invocation\n"
83 " help Show this help\n"
84 "\nSee the %s for details.\n"
85 , program_invocation_short_name
86 , link
87 );
88
89 return 0;
90 }
91
92 static int verb_help(int argc, char **argv, void *userdata) {
93 return help();
94 }
95
96 static int parse_argv(int argc, char *argv[]) {
97 enum {
98 ARG_VERSION = 0x100,
99 };
100
101 static const struct option options[] = {
102 { "help", no_argument, NULL, 'h' },
103 { "version", no_argument, NULL, ARG_VERSION },
104 { "app-specific", required_argument, NULL, 'a' },
105 {},
106 };
107
108 int c, r;
109
110 assert(argc >= 0);
111 assert(argv);
112
113 while ((c = getopt_long(argc, argv, "hpa:", options, NULL)) >= 0)
114 switch (c) {
115
116 case 'h':
117 return help();
118
119 case ARG_VERSION:
120 return version();
121
122 case 'p':
123 arg_pretty = true;
124 break;
125
126 case 'a':
127 r = sd_id128_from_string(optarg, &arg_app);
128 if (r < 0)
129 return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
130 break;
131
132 case '?':
133 return -EINVAL;
134
135 default:
136 assert_not_reached("Unhandled option");
137 }
138
139 return 1;
140 }
141
142 static int id128_main(int argc, char *argv[]) {
143 static const Verb verbs[] = {
144 { "new", VERB_ANY, 1, 0, verb_new },
145 { "machine-id", VERB_ANY, 1, 0, verb_machine_id },
146 { "boot-id", VERB_ANY, 1, 0, verb_boot_id },
147 { "invocation-id", VERB_ANY, 1, 0, verb_invocation_id },
148 { "help", VERB_ANY, VERB_ANY, 0, verb_help },
149 {}
150 };
151
152 return dispatch_verb(argc, argv, verbs, NULL);
153 }
154
155 static int run(int argc, char *argv[]) {
156 int r;
157
158 log_parse_environment();
159 log_open();
160
161 r = parse_argv(argc, argv);
162 if (r <= 0)
163 return r;
164
165 return id128_main(argc, argv);
166 }
167
168 DEFINE_MAIN_FUNCTION(run);