chiark / gitweb /
udev: really exclude device-mapper from block device ownership event locking
[elogind.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 <unistd.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <getopt.h>
26 #include <errno.h>
27
28 #include "machine-id-setup.h"
29 #include "log.h"
30 #include "build.h"
31
32 static const char *arg_root = "";
33
34 static int help(void) {
35
36         printf("%s [OPTIONS...]\n\n"
37                "Initialize /etc/machine-id from a random source.\n\n"
38                "  -h --help             Show this help\n"
39                "     --version          Show package version\n"
40                "     --root=ROOT        Filesystem root\n",
41                program_invocation_short_name);
42
43         return 0;
44 }
45
46 static int parse_argv(int argc, char *argv[]) {
47
48         enum {
49                 ARG_VERSION = 0x100,
50                 ARG_ROOT,
51         };
52
53         static const struct option options[] = {
54                 { "help",      no_argument,       NULL, 'h'           },
55                 { "version",   no_argument,       NULL, ARG_VERSION   },
56                 { "root",      required_argument, NULL, ARG_ROOT      },
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                         return help();
71
72                 case ARG_VERSION:
73                         puts(PACKAGE_STRING);
74                         puts(SYSTEMD_FEATURES);
75                         return 0;
76
77                 case ARG_ROOT:
78                         arg_root = optarg;
79                         break;
80
81                 case '?':
82                         return -EINVAL;
83
84                 default:
85                         assert_not_reached("Unhandled option");
86                 }
87         }
88
89         if (optind < argc) {
90                 log_error("Extraneous arguments");
91                 return -EINVAL;
92         }
93
94         return 1;
95 }
96
97 int main(int argc, char *argv[]) {
98         int r;
99
100         log_parse_environment();
101         log_open();
102
103         r = parse_argv(argc, argv);
104         if (r <= 0)
105                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
106
107         return machine_id_setup(arg_root) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
108 }