chiark / gitweb /
Remove src/initctl
[elogind.git] / src / machine-id-commit / machine-id-commit.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Didier Roche
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 <stdlib.h>
23 #include <stdio.h>
24 #include <getopt.h>
25 #include <errno.h>
26
27 #include "machine-id-setup.h"
28 #include "log.h"
29 #include "build.h"
30
31 static const char *arg_root = NULL;
32
33 static void help(void) {
34         printf("%s [OPTIONS...]\n\n"
35                "Commit a transient /etc/machine-id on disk if writable.\n\n"
36                "  -h --help             Show this help\n"
37                "     --version          Show package version\n"
38                "     --root=ROOT        Filesystem root\n",
39                program_invocation_short_name);
40 }
41
42 static int parse_argv(int argc, char *argv[]) {
43
44         enum {
45                 ARG_VERSION = 0x100,
46                 ARG_ROOT,
47         };
48
49         static const struct option options[] = {
50                 { "help",      no_argument,       NULL, 'h'           },
51                 { "version",   no_argument,       NULL, ARG_VERSION   },
52                 { "root",      required_argument, NULL, ARG_ROOT      },
53                 {}
54         };
55
56         int c;
57
58         assert(argc >= 0);
59         assert(argv);
60
61         while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
62                 switch (c) {
63
64                 case 'h':
65                         help();
66                         return 0;
67
68                 case ARG_VERSION:
69                         puts(PACKAGE_STRING);
70                         puts(SYSTEMD_FEATURES);
71                         return 0;
72
73                 case ARG_ROOT:
74                         arg_root = optarg;
75                         break;
76
77                 case '?':
78                         return -EINVAL;
79
80                 default:
81                         assert_not_reached("Unhandled option");
82                 }
83
84         if (optind < argc) {
85                 log_error("Extraneous arguments");
86                 return -EINVAL;
87         }
88
89         return 1;
90 }
91
92 int main(int argc, char *argv[]) {
93         int r;
94
95         log_set_target(LOG_TARGET_AUTO);
96         log_parse_environment();
97         log_open();
98
99         r = parse_argv(argc, argv);
100         if (r <= 0)
101                 goto finish;
102
103         r = machine_id_commit(arg_root);
104
105 finish:
106         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
107 }