]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine-id-setup/machine-id-setup-main.c
Merge pull request #2222 from snakeroot/eventsplat
[thirdparty/systemd.git] / src / machine-id-setup / machine-id-setup-main.c
CommitLineData
d7ccca2e
LP
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
5430f7f2
LP
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
d7ccca2e
LP
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
5430f7f2 16 Lesser General Public License for more details.
d7ccca2e 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
d7ccca2e
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
984bf931 22#include <errno.h>
3f6fd1ba
LP
23#include <getopt.h>
24#include <stdio.h>
25#include <stdlib.h>
d7ccca2e 26
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;
92f2f92e 34
601185b4 35static void help(void) {
984bf931
LP
36 printf("%s [OPTIONS...]\n\n"
37 "Initialize /etc/machine-id from a random source.\n\n"
38 " -h --help Show this help\n"
92f2f92e 39 " --version Show package version\n"
4a9b1dd4
LP
40 " --root=ROOT Filesystem root\n"
41 " --commit Commit transient ID\n"
42 , program_invocation_short_name);
984bf931
LP
43}
44
45static int parse_argv(int argc, char *argv[]) {
46
47 enum {
92f2f92e
GKH
48 ARG_VERSION = 0x100,
49 ARG_ROOT,
4a9b1dd4 50 ARG_COMMIT,
984bf931
LP
51 };
52
53 static const struct option options[] = {
54 { "help", no_argument, NULL, 'h' },
55 { "version", no_argument, NULL, ARG_VERSION },
92f2f92e 56 { "root", required_argument, NULL, ARG_ROOT },
4a9b1dd4 57 { "commit", no_argument, NULL, ARG_COMMIT },
eb9da376 58 {}
984bf931
LP
59 };
60
0f03c2a4 61 int c, r;
984bf931
LP
62
63 assert(argc >= 0);
64 assert(argv);
65
601185b4 66 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
984bf931
LP
67
68 switch (c) {
69
70 case 'h':
601185b4
ZJS
71 help();
72 return 0;
984bf931
LP
73
74 case ARG_VERSION:
3f6fd1ba 75 return version();
984bf931 76
92f2f92e 77 case ARG_ROOT:
0f03c2a4
LP
78 r = parse_path_argument_and_warn(optarg, true, &arg_root);
79 if (r < 0)
80 return r;
92f2f92e
GKH
81 break;
82
4a9b1dd4
LP
83 case ARG_COMMIT:
84 arg_commit = true;
85 break;
86
984bf931
LP
87 case '?':
88 return -EINVAL;
89
90 default:
eb9da376 91 assert_not_reached("Unhandled option");
984bf931 92 }
984bf931
LP
93
94 if (optind < argc) {
fe970a8a 95 log_error("Extraneous arguments");
984bf931
LP
96 return -EINVAL;
97 }
98
99 return 1;
100}
d7ccca2e
LP
101
102int main(int argc, char *argv[]) {
984bf931 103 int r;
d7ccca2e 104
d7ccca2e
LP
105 log_parse_environment();
106 log_open();
107
984bf931
LP
108 r = parse_argv(argc, argv);
109 if (r <= 0)
0f03c2a4 110 goto finish;
984bf931 111
4a9b1dd4
LP
112 if (arg_commit)
113 r = machine_id_commit(arg_root);
114 else
ee48dbd5 115 r = machine_id_setup(arg_root, SD_ID128_NULL);
4a9b1dd4 116
0f03c2a4
LP
117finish:
118 free(arg_root);
4a9b1dd4 119 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
d7ccca2e 120}