From: David Härdeman Date: Sat, 20 Sep 2025 14:17:52 +0000 (+0200) Subject: odhcpd: allow the use of an alternative cfg file X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=3b96480a74545636466dfc23d79aea70913d4f0d;p=project%2Fodhcpd.git odhcpd: allow the use of an alternative cfg file 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 Link: https://github.com/openwrt/odhcpd/pull/257 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/config.c b/src/config.c index 72d3bde..b57372d 100644 --- a/src/config.c +++ b/src/config.c @@ -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 */ diff --git a/src/odhcpd.c b/src/odhcpd.c index 7b6420c..1cab203 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -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 Use an alternative configuration file\n" + " -l 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)); diff --git a/src/odhcpd.h b/src/odhcpd.h index 6a1d48b..916d6da 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -176,6 +176,7 @@ struct config { char *ra_piofolder; int ra_piofolder_fd; + char *uci_cfgfile; int log_level; };