config: make loglevel configurable via uci (FS#481)
authorHans Dedecker <dedeckeh@gmail.com>
Tue, 14 Feb 2017 10:59:14 +0000 (11:59 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Tue, 14 Feb 2017 12:38:26 +0000 (13:38 +0100)
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
README
src/config.c
src/odhcpd.c
src/odhcpd.h

diff --git a/README b/README
index 4ae5ef2859740bba91a6e122787e392fd634d291..8c3b12c6c245c6b58cb8ac9dddf146144571af8c 100644 (file)
--- a/README
+++ b/README
@@ -62,6 +62,7 @@ legacy                bool    0                       Enable DHCPv4 if start but
                                                no dhcpv4 option set
 leasefile      string                          DHCP/v6 lease/hostfile
 leasetrigger   string                          Lease trigger script
+loglevel       integer 6                       Syslog level priority (0-7)
 
 
 Sections of type dhcp (configure DHCP / DHCPv6 / RA / NDP service)
index 5174f2c7dde12c38c00938c7f7a7568a42dbb9b5..69a3ad1a4db93ee5288d313003f81063a83e8cce 100644 (file)
@@ -6,6 +6,7 @@
 #include <libgen.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <syslog.h>
 
 #include <uci.h>
 #include <uci_blob.h>
@@ -16,7 +17,8 @@ static struct blob_buf b;
 static int reload_pipe[2];
 struct list_head leases = LIST_HEAD_INIT(leases);
 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
-struct config config = {false, NULL, NULL};
+struct config config = {.legacy = false, .dhcp_cb = NULL,
+                       .dhcp_statefile = NULL, .log_level = LOG_INFO};
 
 enum {
        IFACE_ATTR_INTERFACE,
@@ -123,6 +125,7 @@ enum {
        ODHCPD_ATTR_MAINDHCP,
        ODHCPD_ATTR_LEASEFILE,
        ODHCPD_ATTR_LEASETRIGGER,
+       ODHCPD_ATTR_LOGLEVEL,
        ODHCPD_ATTR_MAX
 };
 
@@ -130,6 +133,7 @@ static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
        [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
        [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
        [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
+       [ODHCPD_ATTR_LOGLEVEL] = { .name = "loglevel", .type = BLOBMSG_TYPE_INT32 },
 };
 
 const struct uci_blob_param_list odhcpd_attr_list = {
@@ -247,6 +251,15 @@ static void set_config(struct uci_section *s)
                free(config.dhcp_cb);
                config.dhcp_cb = strdup(blobmsg_get_string(c));
        }
+
+       if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) {
+               int log_level = (blobmsg_get_u32(c) & LOG_PRIMASK);
+
+               if (config.log_level != log_level) {
+                       config.log_level = log_level;
+                       setlogmask(LOG_UPTO(config.log_level));
+               }
+       }
 }
 
 static double parse_leasetime(struct blob_attr *c) {
index 9a76e4d638ee54b3a0ffcd4592c6b383479e126e..8a18fbf4d23446236e7bbf3440486a3f79917c02 100644 (file)
@@ -24,6 +24,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <stdbool.h>
+#include <syslog.h>
 
 #include <arpa/inet.h>
 #include <net/if.h>
@@ -52,7 +53,6 @@
 static int ioctl_sock;
 static struct nl_sock *rtnl_socket = NULL;
 static int urandom_fd = -1;
-static int log_level = LOG_INFO;
 
 static void sighandler(_unused int signal)
 {
@@ -65,7 +65,7 @@ static void print_usage(const char *app)
        "== %s Usage ==\n\n"
        "  -h, --help   Print this help\n"
        "  -l level     Specify log level 0..7 (default %d)\n",
-               app, log_level
+               app, config.log_level
        );
 }
 
@@ -80,12 +80,12 @@ int main(int argc, char **argv)
                        print_usage(argv[0]);
                        return 0;
                case 'l':
-                       log_level = atoi(optarg);
-                       fprintf(stderr, "Log level set to %d\n", log_level);
+                       config.log_level = (atoi(optarg) & LOG_PRIMASK);
+                       fprintf(stderr, "Log level set to %d\n", config.log_level);
                        break;
                }
        }
-       setlogmask(LOG_UPTO(log_level));
+       setlogmask(LOG_UPTO(config.log_level));
        uloop_init();
 
        if (getuid() != 0) {
index ceef0bfb0e380f25dff75593fcfcb0f36de0450a..8db3e7d2c7eee3c4dd32964a71222a58593a9748 100644 (file)
@@ -97,6 +97,7 @@ struct config {
        bool legacy;
        char *dhcp_cb;
        char *dhcp_statefile;
+       int log_level;
 } config;