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

Vladimir Davydov vdavydov.dev at gmail.com
Fri Jul 13 16:12:08 MSK 2018


On Fri, Jul 13, 2018 at 01:29:36PM +0300, Olga Arkhangelskaia wrote:
> 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 at tarantool.org>

We don't sign off patches. Please fix your git config.

And we do add a reference to the related GitHub ticket to the commit
message, e.g.

Closes #3487
Needed for #3487
Part of #3487
Follow-up #3487

For more information on preparing and submitting patches, please read:

https://tarantool.io/en/doc/1.9/dev_guide/developer_guidelines/#how-to-write-a-commit-message
https://tarantool.io/en/doc/1.9/dev_guide/developer_guidelines/#how-to-submit-a-patch-for-review

> ---
>  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.

No point in changing this comment.

>   * @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.

If you write a comment for doxygen, please describe each paramter
separately, i.e.

 @param remote server address
 @param port server port

> + * @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);

I think you should use getaddrinfo() to resolve the address.

> +	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
                                   ^^
                          typo: se => set

>  	 * '/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) {

Comparing strings to determine the socket address looks ugly. Besides,
using strings is troublesome - you have to allocate and free them (which
you forgot BTW). I think you'd better introduce a enum for that.

> +		log->fd = syslog_connect_unix("/dev/log");

> +			if (log->fd < 0)
> +				log->fd = syslog_connect_unix("/var/run/syslog");

Malformed indentation.

> +	} 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);
> +	}
> +

We handle memory allocation errors gracefully in tarantool code.
Even for strdup() and malloc() although they typically never fail.
Please implement error handling.

>  	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;

You don't need to allocate addr_path. Instead you can make it point to
the appropriate position in opts->copy and add '\0' at the end (it's
mutable). We do this in case of 'identity' AFAICS.

> +				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;
> +	};

As I said above, I think you should turn addr_type into enum.
As for addr_path, there's log::path, which isn't used for socket
destination. You could reuse it instead of introducing a new
member.

>  };
>  
>  /**
> @@ -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). */



More information about the Tarantool-patches mailing list