]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine-id-setup/machine-id-setup-main.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / machine-id-setup / machine-id-setup-main.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "alloc-util.h"
9 #include "id128-util.h"
10 #include "log.h"
11 #include "machine-id-setup.h"
12 #include "path-util.h"
13 #include "terminal-util.h"
14 #include "util.h"
15
16 static char *arg_root = NULL;
17 static bool arg_commit = false;
18 static bool arg_print = false;
19
20 static int help(void) {
21 _cleanup_free_ char *link = NULL;
22 int r;
23
24 r = terminal_urlify_man("systemd-machine-id-setup", "1", &link);
25 if (r < 0)
26 return log_oom();
27
28 printf("%s [OPTIONS...]\n\n"
29 "Initialize /etc/machine-id from a random source.\n\n"
30 " -h --help Show this help\n"
31 " --version Show package version\n"
32 " --root=ROOT Filesystem root\n"
33 " --commit Commit transient ID\n"
34 " --print Print used machine ID\n"
35 "\nSee the %s for details.\n"
36 , program_invocation_short_name
37 , link
38 );
39
40 return 0;
41 }
42
43 static int parse_argv(int argc, char *argv[]) {
44
45 enum {
46 ARG_VERSION = 0x100,
47 ARG_ROOT,
48 ARG_COMMIT,
49 ARG_PRINT,
50 };
51
52 static const struct option options[] = {
53 { "help", no_argument, NULL, 'h' },
54 { "version", no_argument, NULL, ARG_VERSION },
55 { "root", required_argument, NULL, ARG_ROOT },
56 { "commit", no_argument, NULL, ARG_COMMIT },
57 { "print", no_argument, NULL, ARG_PRINT },
58 {}
59 };
60
61 int c, r;
62
63 assert(argc >= 0);
64 assert(argv);
65
66 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
67
68 switch (c) {
69
70 case 'h':
71 return help();
72
73 case ARG_VERSION:
74 return version();
75
76 case ARG_ROOT:
77 r = parse_path_argument_and_warn(optarg, true, &arg_root);
78 if (r < 0)
79 return r;
80 break;
81
82 case ARG_COMMIT:
83 arg_commit = true;
84 break;
85
86 case ARG_PRINT:
87 arg_print = true;
88 break;
89
90 case '?':
91 return -EINVAL;
92
93 default:
94 assert_not_reached("Unhandled option");
95 }
96
97 if (optind < argc) {
98 log_error("Extraneous arguments");
99 return -EINVAL;
100 }
101
102 return 1;
103 }
104
105 int main(int argc, char *argv[]) {
106 char buf[SD_ID128_STRING_MAX];
107 sd_id128_t id;
108 int r;
109
110 log_parse_environment();
111 log_open();
112
113 r = parse_argv(argc, argv);
114 if (r <= 0)
115 goto finish;
116
117 if (arg_commit) {
118 const char *etc_machine_id;
119
120 r = machine_id_commit(arg_root);
121 if (r < 0)
122 goto finish;
123
124 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
125 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
126 if (r < 0) {
127 log_error_errno(r, "Failed to read machine ID back: %m");
128 goto finish;
129 }
130 } else {
131 r = machine_id_setup(arg_root, SD_ID128_NULL, &id);
132 if (r < 0)
133 goto finish;
134 }
135
136 if (arg_print)
137 puts(sd_id128_to_string(id, buf));
138
139 finish:
140 free(arg_root);
141 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
142 }