]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine-id-setup/machine-id-setup-main.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / machine-id-setup / machine-id-setup-main.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d7ccca2e
LP
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
5430f7f2
LP
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
d7ccca2e
LP
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
5430f7f2 15 Lesser General Public License for more details.
d7ccca2e 16
5430f7f2 17 You should have received a copy of the GNU Lesser General Public License
d7ccca2e
LP
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
984bf931 21#include <errno.h>
3f6fd1ba
LP
22#include <getopt.h>
23#include <stdio.h>
24#include <stdlib.h>
d7ccca2e 25
a6f72863 26#include "id128-util.h"
d7ccca2e 27#include "log.h"
3f6fd1ba 28#include "machine-id-setup.h"
0f03c2a4 29#include "path-util.h"
cf0fbc49 30#include "util.h"
984bf931 31
0f03c2a4 32static char *arg_root = NULL;
4a9b1dd4 33static bool arg_commit = false;
487ddeb8 34static bool arg_print = false;
92f2f92e 35
601185b4 36static void help(void) {
984bf931
LP
37 printf("%s [OPTIONS...]\n\n"
38 "Initialize /etc/machine-id from a random source.\n\n"
39 " -h --help Show this help\n"
92f2f92e 40 " --version Show package version\n"
4a9b1dd4
LP
41 " --root=ROOT Filesystem root\n"
42 " --commit Commit transient ID\n"
487ddeb8 43 " --print Print used machine ID\n"
4a9b1dd4 44 , program_invocation_short_name);
984bf931
LP
45}
46
47static int parse_argv(int argc, char *argv[]) {
48
49 enum {
92f2f92e
GKH
50 ARG_VERSION = 0x100,
51 ARG_ROOT,
4a9b1dd4 52 ARG_COMMIT,
487ddeb8 53 ARG_PRINT,
984bf931
LP
54 };
55
56 static const struct option options[] = {
57 { "help", no_argument, NULL, 'h' },
58 { "version", no_argument, NULL, ARG_VERSION },
92f2f92e 59 { "root", required_argument, NULL, ARG_ROOT },
4a9b1dd4 60 { "commit", no_argument, NULL, ARG_COMMIT },
487ddeb8 61 { "print", no_argument, NULL, ARG_PRINT },
eb9da376 62 {}
984bf931
LP
63 };
64
0f03c2a4 65 int c, r;
984bf931
LP
66
67 assert(argc >= 0);
68 assert(argv);
69
601185b4 70 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
984bf931
LP
71
72 switch (c) {
73
74 case 'h':
601185b4
ZJS
75 help();
76 return 0;
984bf931
LP
77
78 case ARG_VERSION:
3f6fd1ba 79 return version();
984bf931 80
92f2f92e 81 case ARG_ROOT:
0f03c2a4
LP
82 r = parse_path_argument_and_warn(optarg, true, &arg_root);
83 if (r < 0)
84 return r;
92f2f92e
GKH
85 break;
86
4a9b1dd4
LP
87 case ARG_COMMIT:
88 arg_commit = true;
89 break;
90
487ddeb8
LP
91 case ARG_PRINT:
92 arg_print = true;
93 break;
94
984bf931
LP
95 case '?':
96 return -EINVAL;
97
98 default:
eb9da376 99 assert_not_reached("Unhandled option");
984bf931 100 }
984bf931
LP
101
102 if (optind < argc) {
fe970a8a 103 log_error("Extraneous arguments");
984bf931
LP
104 return -EINVAL;
105 }
106
107 return 1;
108}
d7ccca2e
LP
109
110int main(int argc, char *argv[]) {
487ddeb8
LP
111 char buf[SD_ID128_STRING_MAX];
112 sd_id128_t id;
984bf931 113 int r;
d7ccca2e 114
d7ccca2e
LP
115 log_parse_environment();
116 log_open();
117
984bf931
LP
118 r = parse_argv(argc, argv);
119 if (r <= 0)
0f03c2a4 120 goto finish;
984bf931 121
487ddeb8 122 if (arg_commit) {
a6f72863
EV
123 const char *etc_machine_id;
124
4a9b1dd4 125 r = machine_id_commit(arg_root);
487ddeb8
LP
126 if (r < 0)
127 goto finish;
128
a6f72863
EV
129 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
130 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
487ddeb8
LP
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));
4a9b1dd4 143
0f03c2a4
LP
144finish:
145 free(arg_root);
4a9b1dd4 146 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
d7ccca2e 147}