summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Dounin <mdounin@mdounin.ru>2023-03-10 07:43:40 +0300
committerMaxim Dounin <mdounin@mdounin.ru>2023-03-10 07:43:40 +0300
commit853912986d9568b049ecb5499b6af987cb13cb14 (patch)
treedfedf3423b300e927cd14144da3f7b7246b25e86
parentff9e426337b84ed1d9ff3bbd17e7d7632c7ba19d (diff)
downloadnginx-853912986d9568b049ecb5499b6af987cb13cb14.tar.gz
Syslog: removed usage of ngx_cycle->log and ngx_cycle->hostname.
During initial startup the ngx_cycle->hostname is not available, and previously this resulted in incorrect logging. Instead, hostname from the configuration being parsed is now preserved in the syslog peer structure and then used during logging. Similarly, ngx_cycle->log might not match the configuration where the syslog peer is defined if the configuration is not yet fully applied, and previously this resulted in unexpected logging of syslog errors and debug information. Instead, cf->cycle->new_log is now referenced in the syslog peer structure and used for logging, similarly to how it is done in other modules.
-rw-r--r--src/core/ngx_syslog.c21
-rw-r--r--src/core/ngx_syslog.h21
2 files changed, 24 insertions, 18 deletions
diff --git a/src/core/ngx_syslog.c b/src/core/ngx_syslog.c
index 3c7b63a62..0a64e10b8 100644
--- a/src/core/ngx_syslog.c
+++ b/src/core/ngx_syslog.c
@@ -66,6 +66,9 @@ ngx_syslog_process_conf(ngx_conf_t *cf, ngx_syslog_peer_t *peer)
ngx_str_set(&peer->tag, "nginx");
}
+ peer->hostname = &cf->cycle->hostname;
+ peer->log = &cf->cycle->new_log;
+
peer->conn.fd = (ngx_socket_t) -1;
peer->conn.read = &ngx_syslog_dummy_event;
@@ -243,7 +246,7 @@ ngx_syslog_add_header(ngx_syslog_peer_t *peer, u_char *buf)
}
return ngx_sprintf(buf, "<%ui>%V %V %V: ", pri, &ngx_cached_syslog_time,
- &ngx_cycle->hostname, &peer->tag);
+ peer->hostname, &peer->tag);
}
@@ -292,9 +295,6 @@ ngx_syslog_send(ngx_syslog_peer_t *peer, u_char *buf, size_t len)
}
}
- /* log syslog socket events with valid log */
- peer->conn.log = ngx_cycle->log;
-
if (ngx_send) {
n = ngx_send(&peer->conn, buf, len);
@@ -306,7 +306,7 @@ ngx_syslog_send(ngx_syslog_peer_t *peer, u_char *buf, size_t len)
if (n == NGX_ERROR) {
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, peer->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
@@ -324,24 +324,25 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *peer)
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,
+ ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
ngx_socket_n " failed");
return NGX_ERROR;
}
if (ngx_nonblocking(fd) == -1) {
- ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
ngx_nonblocking_n " failed");
goto failed;
}
if (connect(fd, peer->server.sockaddr, peer->server.socklen) == -1) {
- ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
"connect() failed");
goto failed;
}
peer->conn.fd = fd;
+ peer->conn.log = peer->log;
/* UDP sockets are always ready to write */
peer->conn.write->ready = 1;
@@ -351,7 +352,7 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *peer)
failed:
if (ngx_close_socket(fd) == -1) {
- ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
@@ -372,7 +373,7 @@ ngx_syslog_cleanup(void *data)
}
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, peer->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
}
diff --git a/src/core/ngx_syslog.h b/src/core/ngx_syslog.h
index 50dcd3511..181ebe7b3 100644
--- a/src/core/ngx_syslog.h
+++ b/src/core/ngx_syslog.h
@@ -9,14 +9,19 @@
typedef struct {
- ngx_uint_t facility;
- ngx_uint_t severity;
- ngx_str_t tag;
-
- ngx_addr_t server;
- ngx_connection_t conn;
- unsigned busy:1;
- unsigned nohostname:1;
+ ngx_uint_t facility;
+ ngx_uint_t severity;
+ ngx_str_t tag;
+
+ ngx_str_t *hostname;
+
+ ngx_addr_t server;
+ ngx_connection_t conn;
+
+ ngx_log_t *log;
+
+ unsigned busy:1;
+ unsigned nohostname:1;
} ngx_syslog_peer_t;