ctdb-common: Add a function to validate logging specification
authorAmitay Isaacs <amitay@gmail.com>
Wed, 18 Apr 2018 01:53:57 +0000 (11:53 +1000)
committerMartin Schwenke <martins@samba.org>
Sat, 12 May 2018 10:06:28 +0000 (12:06 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/logging.c
ctdb/common/logging.h

index 0a46b00eb116042e57d139ee9a0ff6c1b757f8c5..3cc19331bab8d9d3122ca5c46d05a3b8c53f41f5 100644 (file)
@@ -25,6 +25,7 @@
 #include "system/time.h"
 #include "system/filesys.h"
 #include "system/syslog.h"
+#include "system/dir.h"
 
 #include "lib/util/time_basic.h"
 #include "lib/util/sys_rw.h"
@@ -154,6 +155,37 @@ static int file_log_state_destructor(struct file_log_state *state)
        return 0;
 }
 
+static bool file_log_validate(const char *option)
+{
+       char *t, *dir;
+       struct stat st;
+       int ret;
+
+       if (option == NULL || strcmp(option, "-") == 0) {
+               return true;
+       }
+
+       t = strdup(option);
+       if (t == NULL) {
+               return false;
+       }
+
+       dir = dirname(t);
+
+       ret = stat(dir, &st);
+       if (ret != 0) {
+               free(t);
+               return false;
+       }
+
+       if (! S_ISDIR(st.st_mode)) {
+               free(t);
+               return false;
+       }
+
+       return true;
+}
+
 static int file_log_setup(TALLOC_CTX *mem_ctx, const char *option,
                          const char *app_name)
 {
@@ -510,6 +542,23 @@ static int syslog_log_setup_udp(TALLOC_CTX *mem_ctx, const char *app_name,
        return 0;
 }
 
+static bool syslog_log_validate(const char *option)
+{
+       if (option == NULL) {
+               return true;
+#ifdef _PATH_LOG
+       } else if (strcmp(option, "nonblocking") == 0) {
+               return true;
+#endif
+       } else if (strcmp(option, "udp") == 0) {
+               return true;
+       } else if (strcmp(option, "udp-rfc5424") == 0) {
+               return true;
+       }
+
+       return false;
+}
+
 static int syslog_log_setup(TALLOC_CTX *mem_ctx, const char *option,
                            const char *app_name)
 {
@@ -530,6 +579,7 @@ static int syslog_log_setup(TALLOC_CTX *mem_ctx, const char *option,
 
 struct log_backend {
        const char *name;
+       bool (*validate)(const char *option);
        int (*setup)(TALLOC_CTX *mem_ctx,
                     const char *option,
                     const char *app_name);
@@ -538,10 +588,12 @@ struct log_backend {
 static struct log_backend log_backend[] = {
        {
                .name = "file",
+               .validate = file_log_validate,
                .setup = file_log_setup,
        },
        {
                .name = "syslog",
+               .validate = syslog_log_validate,
                .setup = syslog_log_setup,
        },
 };
@@ -593,6 +645,30 @@ static int log_backend_parse(TALLOC_CTX *mem_ctx,
        return 0;
 }
 
+bool logging_validate(const char *logging)
+{
+       TALLOC_CTX *tmp_ctx;
+       struct log_backend *backend;
+       char *option;
+       int ret;
+       bool status;
+
+       tmp_ctx = talloc_new(NULL);
+       if (tmp_ctx == NULL) {
+               return false;
+       }
+
+       ret = log_backend_parse(tmp_ctx, logging, &backend, &option);
+       if (ret != 0) {
+               talloc_free(tmp_ctx);
+               return false;
+       }
+
+       status = backend->validate(option);
+       talloc_free(tmp_ctx);
+       return status;
+}
+
 /* Initialise logging */
 int logging_init(TALLOC_CTX *mem_ctx, const char *logging,
                 const char *debug_level, const char *app_name)
index 70d3207cacc61f4d718e9b241414cc0a5e298a88..368171fe406ee771bea48cef30c39258248dbb62 100644 (file)
@@ -37,6 +37,7 @@ bool debug_level_parse(const char *log_string, int *log_level);
 const char *debug_level_to_string(int log_level);
 int debug_level_from_string(const char *log_string);
 
+bool logging_validate(const char *logging);
 int logging_init(TALLOC_CTX *mem_ctx, const char *logging,
                 const char *debuglevel, const char *app_name);