From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id DCD3E268F5 for ; Tue, 24 Jul 2018 13:12:11 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jCxGl0tq4YM3 for ; Tue, 24 Jul 2018 13:12:11 -0400 (EDT) Received: from smtp45.i.mail.ru (smtp45.i.mail.ru [94.100.177.105]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 7C47421B45 for ; Tue, 24 Jul 2018 13:12:11 -0400 (EDT) From: Olga Arkhangelskaia Subject: [tarantool-patches] [PATCH v3 1/4] Configurable syslog destination Date: Tue, 24 Jul 2018 20:11:49 +0300 Message-Id: <20180724171152.24921-2-arkholga@tarantool.org> In-Reply-To: <20180724171152.24921-1-arkholga@tarantool.org> References: <20180724171152.24921-1-arkholga@tarantool.org> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Olga Arkhangelskaia 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. Closes #3487 --- src/say.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/say.h | 13 +++++++- 2 files changed, 113 insertions(+), 7 deletions(-) diff --git a/src/say.c b/src/say.c index ac221dd19..b4836d9ee 100644 --- a/src/say.c +++ b/src/say.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -473,16 +474,86 @@ syslog_connect_unix(const char *path) return fd; } +/** + * Connect to remote syslogd using server:port. + * @param ip:port address string of remote host. + * @retval not 0 Socket descriptor. + * @retval -1 Socket error. + */ +static int +syslog_connect_remote(const char *server_address) +{ + struct addrinfo *inf, hints, *ptr; + const char *remote; + char *portnum, *copy; + int fd = -1; + + copy = strdup(server_address); + if (copy == NULL) { + diag_set(OutOfMemory, strlen(server_address), "malloc", + "stslog server address"); + return fd; + } + portnum = copy; + remote = strsep(&portnum, ":"); + if (remote == NULL) { + remote = strdup("514"); + if (remote == NULL) { + diag_set(OutOfMemory, strlen(remote), "strdup", + "port"); + free(copy); + return fd; + } + } + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = IPPROTO_UDP; + hints.ai_flags = 0; + + if (getaddrinfo(remote, portnum, NULL, &inf) < 0) { + free(copy); + return -1; + } + for (ptr = inf; ptr; ptr = ptr->ai_next) { + fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); + if (fd < 0) { + continue; + } + + if (connect(fd, inf->ai_addr, inf->ai_addrlen) != 0) { + close(fd); + fd = -1; + continue; + } + break; + } + freeaddrinfo(inf); + free(copy); + return fd; +} + static inline int log_syslog_connect(struct log *log) { + /* - * Try two locations: '/dev/log' for Linux and + * If server option is not set 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"); + switch (log->server_type) { + case SAY_SYSLOG_UNIX: + log->fd = syslog_connect_unix(log->path); + break; + case SAY_SYSLOG_REMOTE: + log->fd = syslog_connect_remote(log->path); + break; + default: + log->fd = syslog_connect_unix("/dev/log"); + if (log->fd < 0) + log->fd = syslog_connect_unix("/var/run/syslog"); + + } return log->fd; } @@ -498,6 +569,17 @@ log_syslog_init(struct log *log, const char *init_str) if (say_parse_syslog_opts(init_str, &opts) < 0) return -1; + log->server_type = opts.syslog_server; + if (log->server_type == SAY_SYSLOG_DEFAULT) + log->path = NULL; + else { + log->path = strdup(opts.server); + if (log->path == NULL) { + diag_set(OutOfMemory, strlen(opts.server), + "malloc", "server address"); + return -1; + } + } if (opts.identity == NULL) log->syslog_ident = strdup("tarantool"); else @@ -1031,6 +1113,8 @@ say_syslog_facility_by_name(const char *facility) int say_parse_syslog_opts(const char *init_str, struct say_syslog_opts *opts) { + opts->server = NULL; + opts->syslog_server = SAY_SYSLOG_DEFAULT; opts->identity = NULL; opts->facility = syslog_facility_MAX; opts->copy = strdup(init_str); @@ -1038,7 +1122,7 @@ 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; + char *ptr = opts->copy; const char *option, *value; /* strsep() overwrites the separator with '\0' */ @@ -1047,7 +1131,18 @@ say_parse_syslog_opts(const char *init_str, struct say_syslog_opts *opts) break; value = option; - if (say_parse_prefix(&value, "identity=")) { + if (say_parse_prefix(&value, "server=")) { + if (opts->server != NULL || + opts->syslog_server != SAY_SYSLOG_DEFAULT) + goto duplicate; + if (say_parse_prefix(&value, "unix:")) { + opts->syslog_server = SAY_SYSLOG_UNIX; + opts->server = value; + } else { + opts->syslog_server = SAY_SYSLOG_REMOTE; + opts->server = value; + } + } 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 2c2395fe0..6681aae98 100644 --- a/src/say.h +++ b/src/say.h @@ -79,6 +79,12 @@ say_log_level_is_enabled(int level) extern enum say_format log_format; +enum say_logger_syslog_server { + SAY_SYSLOG_DEFAULT, + SAY_SYSLOG_UNIX, + SAY_SYSLOG_REMOTE +}; + enum say_logger_type { /** * Before the app server core is initialized, we do not @@ -141,7 +147,10 @@ struct log { /** The current log level. */ int level; enum say_logger_type type; - /** path to file if logging to file. */ + enum say_logger_syslog_server server_type; + /** Path to file if logging to file, socket + * or server address in case of syslog. + */ char *path; bool nonblock; log_format_func_t format_func; @@ -384,6 +393,8 @@ say_parse_logger_type(const char **str, enum say_logger_type *type); /** Syslog logger initialization params */ struct say_syslog_opts { + enum say_logger_syslog_server syslog_server; + const char *server; const char *identity; enum syslog_facility facility; /* Input copy (content unspecified). */ -- 2.14.3 (Apple Git-98)