summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2018-04-11 18:23:58 +0300
committerValentin Bartenev <vbart@nginx.com>2018-04-11 18:23:58 +0300
commitda61cfd98b19903d54d4d54750c2ab375b0da76f (patch)
tree973b6a4a9fb5c531612a2f36867447febb107c6b
parent204c39472166440f54435edcef8d286f429e0aae (diff)
downloadunit-da61cfd98b19903d54d4d54750c2ab375b0da76f.tar.gz
unit-da61cfd98b19903d54d4d54750c2ab375b0da76f.tar.bz2
Access log reopening.
Diffstat (limited to '')
-rw-r--r--src/nxt_main_process.c14
-rw-r--r--src/nxt_router.c113
-rw-r--r--src/nxt_router.h2
-rw-r--r--src/nxt_worker_process.c1
4 files changed, 127 insertions, 3 deletions
diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c
index c6caaf56..46f49a72 100644
--- a/src/nxt_main_process.c
+++ b/src/nxt_main_process.c
@@ -698,20 +698,28 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data)
nxt_mp_t *mp;
nxt_int_t ret;
nxt_uint_t n;
+ nxt_port_t *port;
nxt_file_t *file, *new_file;
- nxt_runtime_t *rt;
nxt_array_t *new_files;
+ nxt_runtime_t *rt;
nxt_log(task, NXT_LOG_NOTICE, "signal %d (%s) recevied, %s",
(int) (uintptr_t) obj, data, "log files rotation");
+ rt = task->thread->runtime;
+
+ port = rt->port_by_type[NXT_PROCESS_ROUTER];
+
+ if (nxt_fast_path(port != NULL)) {
+ (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_ACCESS_LOG,
+ -1, 0, 0, NULL);
+ }
+
mp = nxt_mp_create(1024, 128, 256, 32);
if (mp == NULL) {
return;
}
- rt = task->thread->runtime;
-
n = nxt_list_nelts(rt->log_files);
new_files = nxt_array_create(mp, n, sizeof(nxt_file_t));
diff --git a/src/nxt_router.c b/src/nxt_router.c
index d305c2f0..692d3a48 100644
--- a/src/nxt_router.c
+++ b/src/nxt_router.c
@@ -220,6 +220,10 @@ static void nxt_router_access_log_error(nxt_task_t *task,
nxt_port_recv_msg_t *msg, void *data);
static void nxt_router_access_log_release(nxt_task_t *task,
nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log);
+static void nxt_router_access_log_reopen_ready(nxt_task_t *task,
+ nxt_port_recv_msg_t *msg, void *data);
+static void nxt_router_access_log_reopen_error(nxt_task_t *task,
+ nxt_port_recv_msg_t *msg, void *data);
static void nxt_router_app_port_ready(nxt_task_t *task,
nxt_port_recv_msg_t *msg, void *data);
@@ -2893,6 +2897,115 @@ nxt_router_access_log_release(nxt_task_t *task, nxt_thread_spinlock_t *lock,
}
+typedef struct {
+ nxt_mp_t *mem_pool;
+ nxt_router_access_log_t *access_log;
+} nxt_router_access_log_reopen_t;
+
+
+void
+nxt_router_access_log_reopen_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
+{
+ nxt_mp_t *mp;
+ uint32_t stream;
+ nxt_int_t ret;
+ nxt_buf_t *b;
+ nxt_port_t *main_port, *router_port;
+ nxt_runtime_t *rt;
+ nxt_router_access_log_t *access_log;
+ nxt_router_access_log_reopen_t *reopen;
+
+ access_log = nxt_router->access_log;
+
+ if (access_log == NULL) {
+ return;
+ }
+
+ mp = nxt_mp_create(1024, 128, 256, 32);
+ if (nxt_slow_path(mp == NULL)) {
+ return;
+ }
+
+ reopen = nxt_mp_get(mp, sizeof(nxt_router_access_log_reopen_t));
+ if (nxt_slow_path(reopen == NULL)) {
+ goto fail;
+ }
+
+ reopen->mem_pool = mp;
+ reopen->access_log = access_log;
+
+ b = nxt_buf_mem_alloc(mp, access_log->path.length + 1, 0);
+ if (nxt_slow_path(b == NULL)) {
+ goto fail;
+ }
+
+ nxt_buf_cpystr(b, &access_log->path);
+ *b->mem.free++ = '\0';
+
+ rt = task->thread->runtime;
+ main_port = rt->port_by_type[NXT_PROCESS_MAIN];
+ router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
+
+ stream = nxt_port_rpc_register_handler(task, router_port,
+ nxt_router_access_log_reopen_ready,
+ nxt_router_access_log_reopen_error,
+ -1, reopen);
+ if (nxt_slow_path(stream == 0)) {
+ goto fail;
+ }
+
+ ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_ACCESS_LOG, -1,
+ stream, router_port->id, b);
+
+ if (nxt_slow_path(ret != NXT_OK)) {
+ nxt_port_rpc_cancel(task, router_port, stream);
+ goto fail;
+ }
+
+ return;
+
+fail:
+
+ nxt_mp_destroy(mp);
+}
+
+
+static void
+nxt_router_access_log_reopen_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg,
+ void *data)
+{
+ nxt_router_access_log_t *access_log;
+ nxt_router_access_log_reopen_t *reopen;
+
+ reopen = data;
+
+ access_log = reopen->access_log;
+
+ if (access_log == nxt_router->access_log) {
+
+ if (nxt_slow_path(dup2(msg->fd, access_log->fd) == -1)) {
+ nxt_alert(task, "dup2(%FD, %FD) failed %E",
+ msg->fd, access_log->fd, nxt_errno);
+ }
+ }
+
+ nxt_fd_close(msg->fd);
+ nxt_mp_destroy(reopen->mem_pool);
+}
+
+
+static void
+nxt_router_access_log_reopen_error(nxt_task_t *task, nxt_port_recv_msg_t *msg,
+ void *data)
+{
+ nxt_router_access_log_reopen_t *reopen;
+
+ reopen = data;
+
+ nxt_mp_destroy(reopen->mem_pool);
+}
+
+
static void
nxt_router_thread_exit_handler(nxt_task_t *task, void *obj, void *data)
{
diff --git a/src/nxt_router.h b/src/nxt_router.h
index da3dc26f..8f10229c 100644
--- a/src/nxt_router.h
+++ b/src/nxt_router.h
@@ -176,6 +176,8 @@ struct nxt_router_access_log_s {
void nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
void nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
void nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
+void nxt_router_access_log_reopen_handler(nxt_task_t *task,
+ nxt_port_recv_msg_t *msg);
void nxt_router_process_http_request(nxt_task_t *task, nxt_app_parse_ctx_t *ar);
void nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port);
diff --git a/src/nxt_worker_process.c b/src/nxt_worker_process.c
index 17d9e601..0e23aea0 100644
--- a/src/nxt_worker_process.c
+++ b/src/nxt_worker_process.c
@@ -37,6 +37,7 @@ nxt_port_handlers_t nxt_router_process_port_handlers = {
.mmap = nxt_port_mmap_handler,
.data = nxt_router_conf_data_handler,
.remove_pid = nxt_router_remove_pid_handler,
+ .access_log = nxt_router_access_log_reopen_handler,
.rpc_ready = nxt_port_rpc_handler,
.rpc_error = nxt_port_rpc_handler,
};