]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine-id-setup/machine-id-setup-main.c
util: unify implementation of NOP signal handler
[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
d7ccca2e 22#include <stdlib.h>
984bf931
LP
23#include <stdio.h>
24#include <getopt.h>
25#include <errno.h>
d7ccca2e 26
b6e66135 27#include "machine-id-setup.h"
d7ccca2e 28#include "log.h"
984bf931
LP
29#include "build.h"
30
92f2f92e
GKH
31static const char *arg_root = "";
32
601185b4 33static void help(void) {
984bf931
LP
34 printf("%s [OPTIONS...]\n\n"
35 "Initialize /etc/machine-id from a random source.\n\n"
36 " -h --help Show this help\n"
92f2f92e 37 " --version Show package version\n"
fe970a8a 38 " --root=ROOT Filesystem root\n",
984bf931 39 program_invocation_short_name);
984bf931
LP
40}
41
42static int parse_argv(int argc, char *argv[]) {
43
44 enum {
92f2f92e
GKH
45 ARG_VERSION = 0x100,
46 ARG_ROOT,
984bf931
LP
47 };
48
49 static const struct option options[] = {
50 { "help", no_argument, NULL, 'h' },
51 { "version", no_argument, NULL, ARG_VERSION },
92f2f92e 52 { "root", required_argument, NULL, ARG_ROOT },
eb9da376 53 {}
984bf931
LP
54 };
55
56 int c;
57
58 assert(argc >= 0);
59 assert(argv);
60
601185b4 61 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
984bf931
LP
62
63 switch (c) {
64
65 case 'h':
601185b4
ZJS
66 help();
67 return 0;
984bf931
LP
68
69 case ARG_VERSION:
70 puts(PACKAGE_STRING);
984bf931
LP
71 puts(SYSTEMD_FEATURES);
72 return 0;
73
92f2f92e
GKH
74 case ARG_ROOT:
75 arg_root = optarg;
76 break;
77
984bf931
LP
78 case '?':
79 return -EINVAL;
80
81 default:
eb9da376 82 assert_not_reached("Unhandled option");
984bf931 83 }
984bf931
LP
84
85 if (optind < argc) {
fe970a8a 86 log_error("Extraneous arguments");
984bf931
LP
87 return -EINVAL;
88 }
89
90 return 1;
91}
d7ccca2e
LP
92
93int main(int argc, char *argv[]) {
984bf931 94 int r;
d7ccca2e 95
d7ccca2e
LP
96 log_parse_environment();
97 log_open();
98
984bf931
LP
99 r = parse_argv(argc, argv);
100 if (r <= 0)
101 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
102
92f2f92e 103 return machine_id_setup(arg_root) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
d7ccca2e 104}