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