[tarantool-patches] [PATCH 1/3] Configurable syslog destination

  • From: Olga Arkhangelskaia <arkholga@xxxxxxxxxxxxx>
  • To: tarantool-patches@xxxxxxxxxxxxx
  • Date: Fri, 13 Jul 2018 13:29:36 +0300

Added server option to syslog configuration.
Server option is responsible for log destination. At the momemt
there is two ways of usage:server=unix:/path/to/socket or
server=ipv4:port. If port is not set default udp port 514 is used.
If logging to syslog is set, however there is no sever options -
default location is used: Linux /dev/log and Mac /var/run/syslog.

Signed-off-by: Olga Arkhangelskaia <arkholga@xxxxxxxxxxxxx>
---
 src/say.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 src/say.h |  7 ++++++
 2 files changed, 75 insertions(+), 8 deletions(-)

diff --git a/src/say.c b/src/say.c
index 43124083c..aa820175c 100644
--- a/src/say.c
+++ b/src/say.c
@@ -451,7 +451,7 @@ log_file_init(struct log *log, const char *init_str)
 }
 
 /**
- * Connect to syslogd using UNIX socket.
+ * Connect to UNIX socket.
  * @param path UNIX socket path.
  * @retval not 0 Socket descriptor.
  * @retval    -1 Socket error.
@@ -473,16 +473,50 @@ syslog_connect_unix(const char *path)
        return fd;
 }
 
+/**
+ * Connect to remote syslogd using server:port.
+ * @param ip:port pair.
+ * @retval not 0 Socket descriptor.
+ * @retval    -1 Socket error.
+ */
+static int
+syslog_connect_remote(const char *remote, const char *portnum)
+{
+       /* IPv4 */
+       struct sockaddr_in in;
+       in_addr_t addr = inet_addr(remote);
+       int fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (fd < 0)
+               return -1;
+       memset(&in, 0, sizeof(in));
+       in.sin_family = AF_INET;
+       in.sin_port = htons(atoi(portnum));
+       in.sin_addr.s_addr = addr;
+       if (connect(fd, (struct sockaddr *) &in, sizeof(in)) != 0) {
+               close(fd);
+               return -1;
+       }
+       return fd;
+}
+
 static inline int
 log_syslog_connect(struct log *log)
 {
+
        /*
-        * Try two locations: '/dev/log' for Linux and
+        * If server option is not se use '/dev/log' for Linux and
         * '/var/run/syslog' for Mac.
         */
-       log->fd = syslog_connect_unix("/dev/log");
-       if (log->fd < 0)
-               log->fd = syslog_connect_unix("/var/run/syslog");
+       if (strncmp(log->addr_type, "default", sizeof("default")) == 0) {
+               log->fd = syslog_connect_unix("/dev/log");
+                       if (log->fd < 0)
+                               log->fd = 
syslog_connect_unix("/var/run/syslog");
+       } else if (strncmp(log->addr_type, "unix", sizeof("unix")) == 0) {
+               log->fd = syslog_connect_unix(log->addr_path);
+       } else {
+               log->fd = syslog_connect_remote(log->addr_type,
+                                               log->addr_path);
+       }
        return log->fd;
 }
 
@@ -498,6 +532,14 @@ log_syslog_init(struct log *log, const char *init_str)
        if (say_parse_syslog_opts(init_str, &opts) < 0)
                return -1;
 
+       if (opts.addr_type == NULL &&
+           opts.addr_path == NULL) {
+               log->addr_type = strdup("default");
+       } else {
+               log->addr_type = strdup(opts.addr_type);
+               log->addr_path = strdup(opts.addr_path);
+       }
+
        if (opts.identity == NULL)
                log->syslog_ident = strdup("tarantool");
        else
@@ -1044,6 +1086,8 @@ say_syslog_facility_by_name(const char *facility)
 int
 say_parse_syslog_opts(const char *init_str, struct say_syslog_opts *opts)
 {
+       opts->addr_type = NULL;
+       opts->addr_path = NULL;
        opts->identity = NULL;
        opts->facility = syslog_facility_MAX;
        opts->copy = strdup(init_str);
@@ -1051,8 +1095,9 @@ say_parse_syslog_opts(const char *init_str, struct 
say_syslog_opts *opts)
                diag_set(OutOfMemory, strlen(init_str), "malloc", "opts->copy");
                return -1;
        }
-       char *ptr = opts->copy;
-       const char *option, *value;
+       char *ptr = opts->copy; 
+       const char *option, *value, *srv_str;
+       char *srv_ptr, *srv_opt;
 
        /* strsep() overwrites the separator with '\0' */
        while ((option = strsep(&ptr, ","))) {
@@ -1060,7 +1105,22 @@ say_parse_syslog_opts(const char *init_str, struct 
say_syslog_opts *opts)
                        break;
 
                value = option;
-               if (say_parse_prefix(&value, "identity=")) {
+               srv_str = say_parse_prefix(&value, "server=");
+               if (srv_str != NULL) {
+                       if (opts->addr_path != NULL ||
+                           opts->addr_type != NULL)
+                               goto duplicate;
+                       if (say_parse_prefix(&srv_str, "unix:")) {
+                               opts->addr_type = strdup("unix");
+                               opts->addr_path = srv_str;
+                       } else {
+                               srv_ptr = strdup(srv_str);
+                               srv_opt = srv_ptr;
+                               opts->addr_type = strsep(&srv_ptr,":");
+                               opts->addr_path = srv_ptr;
+                               free(srv_opt);
+                       }
+               } else if (say_parse_prefix(&value, "identity=")) {
                        if (opts->identity != NULL)
                                goto duplicate;
                        opts->identity = value;
diff --git a/src/say.h b/src/say.h
index ad3ba3417..6bee2cc90 100644
--- a/src/say.h
+++ b/src/say.h
@@ -162,6 +162,11 @@ struct log {
        int rotating_threads;
        enum syslog_facility syslog_facility;
        struct rlist in_log_list;
+       /* Server options. Either ip/port pair or unix socket address.*/
+       struct {
+               char *addr_type;
+               char *addr_path;
+       };
 };
 
 /**
@@ -390,6 +395,8 @@ say_parse_logger_type(const char **str, enum 
say_logger_type *type);
 
 /** Syslog logger initialization params */
 struct say_syslog_opts {
+       const char *addr_type;
+       const char *addr_path;
        const char *identity;
        enum syslog_facility facility;
        /* Input copy (content unspecified). */
-- 
2.14.3 (Apple Git-98)


Other related posts: