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.
This commit is contained in:
Vladimir Homutov 2014-08-26 14:56:54 +04:00
parent d79cbf15e6
commit 27fa3123f9

View File

@ -261,6 +261,8 @@ ngx_syslog_writer(ngx_log_t *log, ngx_uint_t level, u_char *buf,
ssize_t ssize_t
ngx_syslog_send(ngx_syslog_peer_t *peer, u_char *buf, size_t len) 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 (peer->conn.fd == (ngx_socket_t) -1) {
if (ngx_syslog_init_peer(peer) != NGX_OK) { if (ngx_syslog_init_peer(peer) != NGX_OK) {
return NGX_ERROR; 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; peer->conn.log = ngx_cycle->log;
if (ngx_send) { if (ngx_send) {
return ngx_send(&peer->conn, buf, len); n = ngx_send(&peer->conn, buf, len);
} else { } else {
/* event module has not yet set ngx_io */ /* 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 */ /* prevents further use of this peer */
peer->busy = 1; peer->busy = 1;
if (peer->conn.fd == (ngx_socket_t) -1) {
return;
}
if (ngx_close_socket(peer->conn.fd) == -1) { if (ngx_close_socket(peer->conn.fd) == -1) {
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
ngx_close_socket_n " failed"); ngx_close_socket_n " failed");