summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2021-02-03 23:23:06 +0300
committerMax Romanov <max.romanov@nginx.com>2021-02-03 23:23:06 +0300
commit8c88537e6ee0c0a2ae1c323b8cce09522240471b (patch)
tree4d8e62e6a9aa283c354132f25704b99eaab32db7 /src
parentd4d704bc7f37f457a50b1226165d827a3c2b25e1 (diff)
downloadunit-8c88537e6ee0c0a2ae1c323b8cce09522240471b.tar.gz
unit-8c88537e6ee0c0a2ae1c323b8cce09522240471b.tar.bz2
Using shared memory to pass configuration to main process.
This patch is required to remove fragmented messages functionality.
Diffstat (limited to 'src')
-rw-r--r--src/nxt_controller.c37
-rw-r--r--src/nxt_main_process.c69
2 files changed, 84 insertions, 22 deletions
diff --git a/src/nxt_controller.c b/src/nxt_controller.c
index 9a34a877..772d10c8 100644
--- a/src/nxt_controller.c
+++ b/src/nxt_controller.c
@@ -1686,7 +1686,10 @@ nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
static void
nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf)
{
+ void *mem;
+ u_char *end;
size_t size;
+ nxt_fd_t fd;
nxt_buf_t *b;
nxt_port_t *main_port;
nxt_runtime_t *rt;
@@ -1697,14 +1700,38 @@ nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf)
size = nxt_conf_json_length(conf, NULL);
- b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size);
+ fd = nxt_shm_open(task, size);
+ if (nxt_slow_path(fd == -1)) {
+ return;
+ }
+
+ mem = nxt_mem_mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (nxt_slow_path(mem == MAP_FAILED)) {
+ goto fail;
+ }
+
+ end = nxt_conf_json_print(mem, conf, NULL);
+
+ nxt_mem_munmap(mem, size);
- if (nxt_fast_path(b != NULL)) {
- b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
+ size = end - (u_char *) mem;
- (void) nxt_port_socket_write(task, main_port, NXT_PORT_MSG_CONF_STORE,
- -1, 0, -1, b);
+ b = nxt_buf_mem_alloc(task->thread->engine->mem_pool, sizeof(size_t), 0);
+ if (nxt_slow_path(b == NULL)) {
+ goto fail;
}
+
+ b->mem.free = nxt_cpymem(b->mem.pos, &size, sizeof(size_t));
+
+ (void) nxt_port_socket_write(task, main_port,
+ NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_CLOSE_FD,
+ fd, 0, -1, b);
+
+ return;
+
+fail:
+
+ nxt_fd_close(fd);
}
diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c
index 2916f0ab..9a78f9da 100644
--- a/src/nxt_main_process.c
+++ b/src/nxt_main_process.c
@@ -1408,12 +1408,45 @@ nxt_app_lang_compare(const void *v1, const void *v2)
static void
nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
{
- ssize_t n, size, offset;
- nxt_buf_t *b;
+ void *p;
+ size_t size;
+ ssize_t n;
nxt_int_t ret;
nxt_file_t file;
nxt_runtime_t *rt;
+ p = MAP_FAILED;
+
+ /*
+ * Ancient compilers like gcc 4.8.5 on CentOS 7 wants 'size' to be
+ * initialized in 'cleanup' section.
+ */
+ size = 0;
+
+ if (nxt_slow_path(msg->fd[0] == -1)) {
+ nxt_alert(task, "conf_store_handler: invalid shm fd");
+ goto error;
+ }
+
+ if (nxt_buf_mem_used_size(&msg->buf->mem) != sizeof(size_t)) {
+ nxt_alert(task, "conf_store_handler: unexpected buffer size (%d)",
+ (int) nxt_buf_mem_used_size(&msg->buf->mem));
+ goto error;
+ }
+
+ nxt_memcpy(&size, msg->buf->mem.pos, sizeof(size_t));
+
+ p = nxt_mem_mmap(NULL, size, PROT_READ, MAP_SHARED, msg->fd[0], 0);
+
+ nxt_fd_close(msg->fd[0]);
+ msg->fd[0] = -1;
+
+ if (nxt_slow_path(p == MAP_FAILED)) {
+ goto error;
+ }
+
+ nxt_debug(task, "conf_store_handler(%uz): %*s", size, size, p);
+
nxt_memzero(&file, sizeof(nxt_file_t));
rt = task->thread->runtime;
@@ -1427,33 +1460,35 @@ nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
goto error;
}
- offset = 0;
-
- for (b = msg->buf; b != NULL; b = b->next) {
- size = nxt_buf_mem_used_size(&b->mem);
-
- n = nxt_file_write(&file, b->mem.pos, size, offset);
+ n = nxt_file_write(&file, p, size, 0);
- if (nxt_slow_path(n != size)) {
- nxt_file_close(task, &file);
- (void) nxt_file_delete(file.name);
- goto error;
- }
+ nxt_file_close(task, &file);
- offset += n;
+ if (nxt_slow_path(n != (ssize_t) size)) {
+ (void) nxt_file_delete(file.name);
+ goto error;
}
- nxt_file_close(task, &file);
-
ret = nxt_file_rename(file.name, (nxt_file_name_t *) rt->conf);
if (nxt_fast_path(ret == NXT_OK)) {
- return;
+ goto cleanup;
}
error:
nxt_alert(task, "failed to store current configuration");
+
+cleanup:
+
+ if (p != MAP_FAILED) {
+ nxt_mem_munmap(p, size);
+ }
+
+ if (msg->fd[0] != -1) {
+ nxt_fd_close(msg->fd[0]);
+ msg->fd[0] = -1;
+ }
}