Tarantool development patches archive
 help / color / mirror / Atom feed
From: Olga Arkhangelskaia <arkholga@tarantool.org>
To: tarantool-patches@freelists.org
Cc: Olga Arkhangelskaia <arkholga@tarantool.org>
Subject: [tarantool-patches] [PATCH 1/3] Configurable syslog destination
Date: Fri, 13 Jul 2018 13:29:36 +0300	[thread overview]
Message-ID: <20180713102938.31897-2-arkholga@tarantool.org> (raw)
In-Reply-To: <20180713102938.31897-1-arkholga@tarantool.org>

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@tarantool.org>
---
 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)

  reply	other threads:[~2018-07-13 10:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-13 10:29 [tarantool-patches] [PATCH 0/3] Syslog destination Olga Arkhangelskaia
2018-07-13 10:29 ` Olga Arkhangelskaia [this message]
2018-07-13 13:12   ` [tarantool-patches] [PATCH 1/3] Configurable syslog destination Vladimir Davydov
2018-07-13 10:29 ` [tarantool-patches] [PATCH 2/3] Syslog remote destination test Olga Arkhangelskaia
2018-07-13 13:27   ` Vladimir Davydov
2018-07-13 10:29 ` [tarantool-patches] [PATCH 3/3] Syslog destination test unix socket Olga Arkhangelskaia
2018-07-13 13:34   ` Vladimir Davydov
2018-07-13 12:42 ` [tarantool-patches] [PATCH 0/3] Syslog destination Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180713102938.31897-2-arkholga@tarantool.org \
    --to=arkholga@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 1/3] Configurable syslog destination' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox