]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine-id-setup/machine-id-setup-main.c
de6d869d307a470ad9043728a77a70f611afddbc
[thirdparty/systemd.git] / src / machine-id-setup / machine-id-setup-main.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "id128-util.h"
27 #include "log.h"
28 #include "machine-id-setup.h"
29 #include "path-util.h"
30 #include "util.h"
31
32 static char *arg_root = NULL;
33 static bool arg_commit = false;
34 static bool arg_print = false;
35
36 static void help(void) {
37 printf("%s [OPTIONS...]\n\n"
38 "Initialize /etc/machine-id from a random source.\n\n"
39 " -h --help Show this help\n"
40 " --version Show package version\n"
41 " --root=ROOT Filesystem root\n"
42 " --commit Commit transient ID\n"
43 " --print Print used machine ID\n"
44 , program_invocation_short_name);
45 }
46
47 static int parse_argv(int argc, char *argv[]) {
48
49 enum {
50 ARG_VERSION = 0x100,
51 ARG_ROOT,
52 ARG_COMMIT,
53 ARG_PRINT,
54 };
55
56 static const struct option options[] = {
57 { "help", no_argument, NULL, 'h' },
58 { "version", no_argument, NULL, ARG_VERSION },
59 { "root", required_argument, NULL, ARG_ROOT },
60 { "commit", no_argument, NULL, ARG_COMMIT },
61 { "print", no_argument, NULL, ARG_PRINT },
62 {}
63 };
64
65 int c, r;
66
67 assert(argc >= 0);
68 assert(argv);
69
70 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
71
72 switch (c) {
73
74 case 'h':
75 help();
76 return 0;
77
78 case ARG_VERSION:
79 return version();
80
81 case ARG_ROOT:
82 r = parse_path_argument_and_warn(optarg, true, &arg_root);
83 if (r < 0)
84 return r;
85 break;
86
87 case ARG_COMMIT:
88 arg_commit = true;
89 break;
90
91 case ARG_PRINT:
92 arg_print = true;
93 break;
94
95 case '?':
96 return -EINVAL;
97
98 default:
99 assert_not_reached("Unhandled option");
100 }
101
102 if (optind < argc) {
103 log_error("Extraneous arguments");
104 return -EINVAL;
105 }
106
107 return 1;
108 }
109
110 int main(int argc, char *argv[]) {
111 char buf[SD_ID128_STRING_MAX];
112 sd_id128_t id;
113 int r;
114
115 log_parse_environment();
116 log_open();
117
118 r = parse_argv(argc, argv);
119 if (r <= 0)
120 goto finish;
121
122 if (arg_commit) {
123 const char *etc_machine_id;
124
125 r = machine_id_commit(arg_root);
126 if (r < 0)
127 goto finish;
128
129 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
130 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
131 if (r < 0) {
132 log_error_errno(r, "Failed to read machine ID back: %m");
133 goto finish;
134 }
135 } else {
136 r = machine_id_setup(arg_root, SD_ID128_NULL, &id);
137 if (r < 0)
138 goto finish;
139 }
140
141 if (arg_print)
142 puts(sd_id128_to_string(id, buf));
143
144 finish:
145 free(arg_root);
146 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
147 }