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