From 27fa3123f917c3af7e914d4a082fe96ef518e6f7 Mon Sep 17 00:00:00 2001 From: Vladimir Homutov Date: Tue, 26 Aug 2014 14:56:54 +0400 Subject: [PATCH] Syslog: improved error handling of unix domain sockets. If a syslog daemon is restarted and the unix socket is used, further logging might stop to work. In case of send error, socket is closed, forcing a reconnection at the next logging attempt. --- src/core/ngx_syslog.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core/ngx_syslog.c b/src/core/ngx_syslog.c index 2555f29b8..616fd712e 100644 --- a/src/core/ngx_syslog.c +++ b/src/core/ngx_syslog.c @@ -261,6 +261,8 @@ ngx_syslog_writer(ngx_log_t *log, ngx_uint_t level, u_char *buf, ssize_t ngx_syslog_send(ngx_syslog_peer_t *peer, u_char *buf, size_t len) { + ssize_t n; + if (peer->conn.fd == (ngx_socket_t) -1) { if (ngx_syslog_init_peer(peer) != NGX_OK) { return NGX_ERROR; @@ -271,12 +273,28 @@ ngx_syslog_send(ngx_syslog_peer_t *peer, u_char *buf, size_t len) peer->conn.log = ngx_cycle->log; if (ngx_send) { - return ngx_send(&peer->conn, buf, len); + n = ngx_send(&peer->conn, buf, len); } else { /* event module has not yet set ngx_io */ - return ngx_os_io.send(&peer->conn, buf, len); + n = ngx_os_io.send(&peer->conn, buf, len); } + +#if (NGX_HAVE_UNIX_DOMAIN) + + if (n == NGX_ERROR && peer->server.sockaddr->sa_family == AF_UNIX) { + + if (ngx_close_socket(peer->conn.fd) == -1) { + ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, + ngx_close_socket_n " failed"); + } + + peer->conn.fd = (ngx_socket_t) -1; + } + +#endif + + return n; } @@ -344,6 +362,10 @@ ngx_syslog_cleanup(void *data) /* prevents further use of this peer */ peer->busy = 1; + if (peer->conn.fd == (ngx_socket_t) -1) { + return; + } + if (ngx_close_socket(peer->conn.fd) == -1) { ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, ngx_close_socket_n " failed");