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