]>
Commit | Line | Data |
---|---|---|
d7ccca2e LP |
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 | |
5430f7f2 LP |
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 | |
d7ccca2e LP |
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 | |
5430f7f2 | 14 | Lesser General Public License for more details. |
d7ccca2e | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
d7ccca2e LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
984bf931 | 20 | #include <errno.h> |
3f6fd1ba LP |
21 | #include <getopt.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
d7ccca2e | 24 | |
d7ccca2e | 25 | #include "log.h" |
3f6fd1ba | 26 | #include "machine-id-setup.h" |
0f03c2a4 | 27 | #include "path-util.h" |
cf0fbc49 | 28 | #include "util.h" |
984bf931 | 29 | |
0f03c2a4 | 30 | static char *arg_root = NULL; |
4a9b1dd4 | 31 | static bool arg_commit = false; |
487ddeb8 | 32 | static bool arg_print = false; |
92f2f92e | 33 | |
601185b4 | 34 | static void help(void) { |
984bf931 LP |
35 | printf("%s [OPTIONS...]\n\n" |
36 | "Initialize /etc/machine-id from a random source.\n\n" | |
37 | " -h --help Show this help\n" | |
92f2f92e | 38 | " --version Show package version\n" |
4a9b1dd4 LP |
39 | " --root=ROOT Filesystem root\n" |
40 | " --commit Commit transient ID\n" | |
487ddeb8 | 41 | " --print Print used machine ID\n" |
4a9b1dd4 | 42 | , program_invocation_short_name); |
984bf931 LP |
43 | } |
44 | ||
45 | static int parse_argv(int argc, char *argv[]) { | |
46 | ||
47 | enum { | |
92f2f92e GKH |
48 | ARG_VERSION = 0x100, |
49 | ARG_ROOT, | |
4a9b1dd4 | 50 | ARG_COMMIT, |
487ddeb8 | 51 | ARG_PRINT, |
984bf931 LP |
52 | }; |
53 | ||
54 | static const struct option options[] = { | |
55 | { "help", no_argument, NULL, 'h' }, | |
56 | { "version", no_argument, NULL, ARG_VERSION }, | |
92f2f92e | 57 | { "root", required_argument, NULL, ARG_ROOT }, |
4a9b1dd4 | 58 | { "commit", no_argument, NULL, ARG_COMMIT }, |
487ddeb8 | 59 | { "print", no_argument, NULL, ARG_PRINT }, |
eb9da376 | 60 | {} |
984bf931 LP |
61 | }; |
62 | ||
0f03c2a4 | 63 | int c, r; |
984bf931 LP |
64 | |
65 | assert(argc >= 0); | |
66 | assert(argv); | |
67 | ||
601185b4 | 68 | while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0) |
984bf931 LP |
69 | |
70 | switch (c) { | |
71 | ||
72 | case 'h': | |
601185b4 ZJS |
73 | help(); |
74 | return 0; | |
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 LP |
99 | |
100 | if (optind < argc) { | |
fe970a8a | 101 | log_error("Extraneous arguments"); |
984bf931 LP |
102 | return -EINVAL; |
103 | } | |
104 | ||
105 | return 1; | |
106 | } | |
d7ccca2e LP |
107 | |
108 | int main(int argc, char *argv[]) { | |
487ddeb8 LP |
109 | char buf[SD_ID128_STRING_MAX]; |
110 | sd_id128_t id; | |
984bf931 | 111 | int r; |
d7ccca2e | 112 | |
d7ccca2e LP |
113 | log_parse_environment(); |
114 | log_open(); | |
115 | ||
984bf931 LP |
116 | r = parse_argv(argc, argv); |
117 | if (r <= 0) | |
0f03c2a4 | 118 | goto finish; |
984bf931 | 119 | |
487ddeb8 | 120 | if (arg_commit) { |
4a9b1dd4 | 121 | r = machine_id_commit(arg_root); |
487ddeb8 LP |
122 | if (r < 0) |
123 | goto finish; | |
124 | ||
125 | r = sd_id128_get_machine(&id); | |
126 | if (r < 0) { | |
127 | log_error_errno(r, "Failed to read machine ID back: %m"); | |
128 | goto finish; | |
129 | } | |
130 | } else { | |
131 | r = machine_id_setup(arg_root, SD_ID128_NULL, &id); | |
132 | if (r < 0) | |
133 | goto finish; | |
134 | } | |
135 | ||
136 | if (arg_print) | |
137 | puts(sd_id128_to_string(id, buf)); | |
4a9b1dd4 | 138 | |
0f03c2a4 LP |
139 | finish: |
140 | free(arg_root); | |
4a9b1dd4 | 141 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
d7ccca2e | 142 | } |