]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Parse command line arguments
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 04:58:39 +0000 (04:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 04:58:39 +0000 (04:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c
src/networkd/daemon.h
src/networkd/main.c

index 749a70b84f172ed9fff31a7a067972703e7d4d91..31fda8ecd6306ae373a5579430284a4c009bdce3 100644 (file)
@@ -19,6 +19,8 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <getopt.h>
+#include <limits.h>
 #include <stdlib.h>
 
 #include <systemd/sd-bus.h>
@@ -36,6 +38,7 @@
 #include "logging.h"
 #include "ports.h"
 #include "stats-collector.h"
+#include "string.h"
 #include "zone.h"
 #include "zones.h"
 
@@ -45,6 +48,7 @@
 struct nw_daemon {
        int nrefs;
 
+       char config_path[PATH_MAX];
        nw_config* config;
 
        // Event Loop
@@ -91,6 +95,40 @@ static int __nw_daemon_reload(sd_event_source* source, const struct signalfd_sig
        return 0;
 }
 
+static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) {
+       enum {
+               ARG_CONFIG,
+       };
+       int r;
+
+       static const struct option options[] = {
+               { "config", required_argument, NULL, ARG_CONFIG },
+               { NULL },
+       };
+       int c;
+
+       for (;;) {
+               c = getopt_long(argc, argv, "", options, NULL);
+               if (c < 0)
+                       break;
+
+               switch (c) {
+                       case ARG_CONFIG:
+                               r = nw_string_set(daemon->config_path, optarg);
+                               if (r < 0)
+                                       return r;
+
+                               break;
+
+                       // Abort on any unrecognised option
+                       default:
+                               return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
 static int nw_daemon_setup_loop(nw_daemon* daemon) {
        int r;
 
@@ -398,7 +436,7 @@ static int nw_daemon_setup(nw_daemon* daemon) {
        return 0;
 }
 
-int nw_daemon_create(nw_daemon** daemon) {
+int nw_daemon_create(nw_daemon** daemon, int argc, char* argv[]) {
        int r;
 
        nw_daemon* d = calloc(1, sizeof(*d));
@@ -408,6 +446,11 @@ int nw_daemon_create(nw_daemon** daemon) {
        // Initialize reference counter
        d->nrefs = 1;
 
+       // Parse command line arguments
+       r = nw_daemon_parse_argv(d, argc, argv);
+       if (r)
+               goto ERROR;
+
        // Setup the daemon
        r = nw_daemon_setup(d);
        if (r)
index 74e19e692db987388258ef34e524c43e89bdac9e..8653af386fb5e07e62f567e79adfebbce626caa8 100644 (file)
@@ -33,7 +33,7 @@ typedef struct nw_daemon nw_daemon;
 #include "zone.h"
 #include "zones.h"
 
-int nw_daemon_create(nw_daemon** daemon);
+int nw_daemon_create(nw_daemon** daemon, int argc, char* argv[]);
 
 nw_daemon* nw_daemon_ref(nw_daemon* daemon);
 nw_daemon* nw_daemon_unref(nw_daemon* daemon);
index c8b9a791bd9c5312b8dafa0ff78d64a8960ac5b1..f5f09f5af31c0e54d951bd427d47cf1d1b55fd30 100644 (file)
@@ -222,7 +222,7 @@ int main(int argc, char** argv) {
                return r;
 
        // Create the daemon
-       r = nw_daemon_create(&daemon);
+       r = nw_daemon_create(&daemon, argc, argv);
        if (r)
                return r;