]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine-id-setup/machine-id-setup-main.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / machine-id-setup / machine-id-setup-main.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "log.h"
26 #include "machine-id-setup.h"
27 #include "path-util.h"
28 #include "util.h"
29
30 static char *arg_root = NULL;
31 static bool arg_commit = false;
32
33 static void help(void) {
34 printf("%s [OPTIONS...]\n\n"
35 "Initialize /etc/machine-id from a random source.\n\n"
36 " -h --help Show this help\n"
37 " --version Show package version\n"
38 " --root=ROOT Filesystem root\n"
39 " --commit Commit transient ID\n"
40 , program_invocation_short_name);
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 };
50
51 static const struct option options[] = {
52 { "help", no_argument, NULL, 'h' },
53 { "version", no_argument, NULL, ARG_VERSION },
54 { "root", required_argument, NULL, ARG_ROOT },
55 { "commit", no_argument, NULL, ARG_COMMIT },
56 {}
57 };
58
59 int c, r;
60
61 assert(argc >= 0);
62 assert(argv);
63
64 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
65
66 switch (c) {
67
68 case 'h':
69 help();
70 return 0;
71
72 case ARG_VERSION:
73 return version();
74
75 case ARG_ROOT:
76 r = parse_path_argument_and_warn(optarg, true, &arg_root);
77 if (r < 0)
78 return r;
79 break;
80
81 case ARG_COMMIT:
82 arg_commit = true;
83 break;
84
85 case '?':
86 return -EINVAL;
87
88 default:
89 assert_not_reached("Unhandled option");
90 }
91
92 if (optind < argc) {
93 log_error("Extraneous arguments");
94 return -EINVAL;
95 }
96
97 return 1;
98 }
99
100 int main(int argc, char *argv[]) {
101 int r;
102
103 log_parse_environment();
104 log_open();
105
106 r = parse_argv(argc, argv);
107 if (r <= 0)
108 goto finish;
109
110 if (arg_commit)
111 r = machine_id_commit(arg_root);
112 else
113 r = machine_id_setup(arg_root, SD_ID128_NULL);
114
115 finish:
116 free(arg_root);
117 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
118 }