Blob Blame History Raw
From 512128fa5b80d39161ad61e43fc82eb7f4c665c6 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Thu, 12 Apr 2012 15:07:24 +0200
Subject: [PATCH] machine-id-setup: add the usual command line parameters
 (cherry picked from commit
 984bf9312e3f2c7cc165b54546270ce7804d5a4d)

---
 man/systemd-machine-id-setup.xml             |   20 +++++++-
 src/machine-id-setup/Makefile                |    1 +
 src/machine-id-setup/machine-id-setup-main.c |   68 ++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 1 deletion(-)
 create mode 120000 src/machine-id-setup/Makefile

diff --git a/man/systemd-machine-id-setup.xml b/man/systemd-machine-id-setup.xml
index 49b92f6..d025802 100644
--- a/man/systemd-machine-id-setup.xml
+++ b/man/systemd-machine-id-setup.xml
@@ -92,7 +92,25 @@
         <refsect1>
                 <title>Options</title>
 
-                <para>This tool does not take any options or arguments.</para>
+                <para>The following options are understood:</para>
+
+                <variablelist>
+                        <varlistentry>
+                                <term><option>-h</option></term>
+                                <term><option>--help</option></term>
+
+                                <listitem><para>Prints a short help
+                                text and exits.</para></listitem>
+                        </varlistentry>
+
+                        <varlistentry>
+                                <term><option>--version</option></term>
+
+                                <listitem><para>Prints a short version
+                                string and exits.</para></listitem>
+                        </varlistentry>
+                </variablelist>
+
         </refsect1>
 
         <refsect1>
diff --git a/src/machine-id-setup/Makefile b/src/machine-id-setup/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/src/machine-id-setup/Makefile
@@ -0,0 +1 @@
+../Makefile
\ No newline at end of file
diff --git a/src/machine-id-setup/machine-id-setup-main.c b/src/machine-id-setup/machine-id-setup-main.c
index 03970a2..4e1fefc 100644
--- a/src/machine-id-setup/machine-id-setup-main.c
+++ b/src/machine-id-setup/machine-id-setup-main.c
@@ -21,15 +21,83 @@
 
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdio.h>
+#include <getopt.h>
+#include <errno.h>
 
 #include "machine-id-setup.h"
 #include "log.h"
+#include "build.h"
+
+static int help(void) {
+
+        printf("%s [OPTIONS...]\n\n"
+               "Initialize /etc/machine-id from a random source.\n\n"
+               "  -h --help             Show this help\n"
+               "     --version          Show package version\n",
+               program_invocation_short_name);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        enum {
+                ARG_VERSION = 0x100
+        };
+
+        static const struct option options[] = {
+                { "help",      no_argument,       NULL, 'h'           },
+                { "version",   no_argument,       NULL, ARG_VERSION   },
+                { NULL,        0,                 NULL, 0             }
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0) {
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(DISTRIBUTION);
+                        puts(SYSTEMD_FEATURES);
+                        return 0;
+
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        log_error("Unknown option code %c", c);
+                        return -EINVAL;
+                }
+        }
+
+        if (optind < argc) {
+                help();
+                return -EINVAL;
+        }
+
+        return 1;
+}
 
 int main(int argc, char *argv[]) {
+        int r;
 
         log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
         log_open();
 
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+
         return machine_id_setup() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }