diff options
author | Vladimir Homutov <vl@nginx.com> | 2014-05-26 23:34:44 +0400 |
---|---|---|
committer | Vladimir Homutov <vl@nginx.com> | 2014-05-26 23:34:44 +0400 |
commit | 8e82f06d3e80f39a12ef7ac35f473008b58c7827 (patch) | |
tree | f01e387c1dea5a441b4ea5a7c831c97f5d6acc7e /src/core/ngx_syslog.c | |
parent | 5c170306718a8fe5f606f7bdd2d492a54c1cf0ce (diff) |
Syslog: fixed possible resource leak and more verbose logging.
Found by Coverity (CID 1215646).
Diffstat (limited to 'src/core/ngx_syslog.c')
-rw-r--r-- | src/core/ngx_syslog.c | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/core/ngx_syslog.c b/src/core/ngx_syslog.c index f341a25e5..7c5de38b6 100644 --- a/src/core/ngx_syslog.c +++ b/src/core/ngx_syslog.c @@ -289,14 +289,6 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *peer) ngx_syslog_dummy_event.log = &ngx_syslog_dummy_log; - cln = ngx_pool_cleanup_add(peer->pool, 0); - if (cln == NULL) { - return NGX_ERROR; - } - - cln->data = peer; - cln->handler = ngx_syslog_cleanup; - fd = ngx_socket(peer->server.sockaddr->sa_family, SOCK_DGRAM, 0); if (fd == (ngx_socket_t) -1) { ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, @@ -307,17 +299,34 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *peer) if (ngx_nonblocking(fd) == -1) { ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, ngx_nonblocking_n " failed"); - return NGX_ERROR; + goto failed; } if (connect(fd, peer->server.sockaddr, peer->server.socklen) == -1) { ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, "connect() failed"); - return NGX_ERROR; + goto failed; } + cln = ngx_pool_cleanup_add(peer->pool, 0); + if (cln == NULL) { + goto failed; + } + + cln->data = peer; + cln->handler = ngx_syslog_cleanup; + peer->conn.fd = fd; return NGX_OK; + +failed: + + if (ngx_close_socket(fd) == -1) { + ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, + ngx_close_socket_n " failed"); + } + + return NGX_ERROR; } @@ -326,7 +335,8 @@ ngx_syslog_cleanup(void *data) { ngx_syslog_peer_t *peer = data; - if (peer->conn.fd != (ngx_socket_t) -1) { - (void) ngx_close_socket(peer->conn.fd); + 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"); } } |