odhcpd: allow the use of an alternative cfg file
authorDavid Härdeman <[email protected]>
Sat, 20 Sep 2025 14:17:52 +0000 (16:17 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Wed, 1 Oct 2025 21:10:14 +0000 (23:10 +0200)
This is mainly useful for development/testing, since it obviates the
need for a cfg file below the /etc hierarchy.

Also, fixup the help message (long options like "--help" are not
supported).

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/257
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/config.c
src/odhcpd.c
src/odhcpd.h

index 72d3bde343d0feb4f3b0b9af594cc562c4531398..b57372d8fae80360dc11855b0f4116666e6d220e 100644 (file)
@@ -39,6 +39,7 @@ struct config config = {
        .dhcp_hostsfile = NULL,
        .ra_piofolder = NULL,
        .ra_piofolder_fd = -1,
+       .uci_cfgfile = "dhcp",
        .log_level = LOG_WARNING,
 };
 
@@ -2038,7 +2039,7 @@ void odhcpd_reload(void)
                clean_interface(i);
 
        struct uci_package *dhcp = NULL;
-       if (!uci_load(uci, "dhcp", &dhcp)) {
+       if (!uci_load(uci, config.uci_cfgfile, &dhcp)) {
                struct uci_element *e;
 
                /* 1. Global settings */
index 7b6420ce75725fbb78cde79d9fc90775bda537ed..1cab203a92c02ceb41d0a8e81c2ab915c83ddacf 100644 (file)
@@ -54,12 +54,12 @@ static void sighandler(_unused int signal)
 
 static void print_usage(const char *app)
 {
-       printf(
-       "== %s Usage ==\n\n"
-       "  -h, --help   Print this help\n"
-       "  -l level     Specify log level 0..7 (default %d)\n",
-               app, config.log_level
-       );
+       printf("== %s Usage ==\n"
+              "\n"
+              "        -c <path>       Use an alternative configuration file\n"
+              "        -l <int>        Specify log level 0..7 (default %d)\n"
+              "        -h              Print this help text and exit\n",
+              app, config.log_level);
 }
 
 static bool ipv6_enabled(void)
@@ -79,15 +79,19 @@ int main(int argc, char **argv)
        openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
        int opt;
 
-       while ((opt = getopt(argc, argv, "hl:")) != -1) {
+       while ((opt = getopt(argc, argv, "c:l:h")) != -1) {
                switch (opt) {
-               case 'h':
-                       print_usage(argv[0]);
-                       return 0;
+               case 'c':
+                       config.uci_cfgfile = realpath(optarg, NULL);
+                       fprintf(stderr, "Configuration will be read from %s\n", config.uci_cfgfile);
+                       break;
                case 'l':
                        config.log_level = (atoi(optarg) & LOG_PRIMASK);
                        fprintf(stderr, "Log level set to %d\n", config.log_level);
                        break;
+               case 'h':
+                       print_usage(argv[0]);
+                       return 0;
                }
        }
        setlogmask(LOG_UPTO(config.log_level));
index 6a1d48b5ed69aec19676d2557417f4f60140cb7d..916d6daeb9c889da137cee5654307f49b5587b2c 100644 (file)
@@ -176,6 +176,7 @@ struct config {
        char *ra_piofolder;
        int ra_piofolder_fd;
 
+       char *uci_cfgfile;
        int log_level;
 };