summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_main_process.c
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-02-28 01:59:04 +0000
committerAndrew Clayton <a.clayton@nginx.com>2023-03-17 04:28:23 +0000
commit2e3e1c7e7bd5ee177b2703fa3d261fe51164426f (patch)
tree689fbf42744604f0faf8ea09efe6427ab9f4e805 /src/nxt_main_process.c
parent8f0192bb4cd953b7e8ca0f82f789a21fc5745220 (diff)
downloadunit-2e3e1c7e7bd5ee177b2703fa3d261fe51164426f.tar.gz
unit-2e3e1c7e7bd5ee177b2703fa3d261fe51164426f.tar.bz2
Socket: Remove Unix domain listen sockets upon reconfigure.
Currently when using Unix domain sockets for requests, if unit is reconfigured then it will fail if it tries to bind(2) again to a Unix domain socket with something like 2023/02/25 19:15:50 [alert] 35274#35274 bind(\"unix:/tmp/unit.sock\") failed (98: Address already in use) When closing such a socket we really need to unlink(2) it. However that presents a problem in that when running as root, while the main process runs as root and creates the socket, it's the router process, that runs as an unprivileged user, e.g nobody, that closes the socket and would thus remove it, but couldn't due to not having permission, even if the socket is mode 0666, you need write permissions on the containing directory to remove a file. There are several options to solve this, all with varying degrees of complexity and utility. 1) Give the user who the router process runs as write permission to the directory containing the listen sockets. These can then be unlink(2)'d from the router process. Simple and would work, but perhaps not the most elegant. 2) Using capabilities(7). The router process could temporarily attain the CAP_DAC_OVERRIDE capability, unlink(7) the socket, then relinquish the capability until required again. These are Linux specific (other systems may have similar mechanisms which would be extra work to support). There is also a, albeit small, window where the router process is running with elevated privileges. 3) Have the main process do the unlink(2), it is after all the process that created the socket. This is what this commit implements. We create a new port IPC message type of NXT_PORT_MSG_SOCKET_UNLINK, that is used by the router process to notify the main process about a Unix domain socket to unlink(2). Upon doing a reconfigure the router process will call nxt_router_listen_socket_release() which will close the socket, we extend this function in the case of non-abstract Unix domain sockets, so that it will send a message to the main process containing a copy of the nxt_sockaddr_t structure that will contain the filename of the socket. In the main process the handler that we have defined, nxt_main_port_socket_unlink_handler(), for this message type will run and allow us to look for the socket in question in the listen_sockets array and remove it and unlink(2) the socket. This then allows the reconfigure to work if it tries to bind(2) again to a socket that previously existed. Link: <https://github.com/nginx/unit/issues/669> Link: <https://github.com/nginx/unit/pull/735> Reviewed-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'src/nxt_main_process.c')
-rw-r--r--src/nxt_main_process.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c
index 7548be9a..db1cfcb9 100644
--- a/src/nxt_main_process.c
+++ b/src/nxt_main_process.c
@@ -48,6 +48,8 @@ static void nxt_main_process_signal_handler(nxt_task_t *task, void *obj,
static void nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process);
static void nxt_main_port_socket_handler(nxt_task_t *task,
nxt_port_recv_msg_t *msg);
+static void nxt_main_port_socket_unlink_handler(nxt_task_t *task,
+ nxt_port_recv_msg_t *msg);
static nxt_int_t nxt_main_listening_socket(nxt_sockaddr_t *sa,
nxt_listening_socket_t *ls);
static void nxt_main_port_modules_handler(nxt_task_t *task,
@@ -587,6 +589,7 @@ static nxt_port_handlers_t nxt_main_process_port_handlers = {
.remove_pid = nxt_port_remove_pid_handler,
.start_process = nxt_main_start_process_handler,
.socket = nxt_main_port_socket_handler,
+ .socket_unlink = nxt_main_port_socket_unlink_handler,
.modules = nxt_main_port_modules_handler,
.conf_store = nxt_main_port_conf_store_handler,
#if (NXT_TLS)
@@ -1214,6 +1217,49 @@ fail:
}
+static void
+nxt_main_port_socket_unlink_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
+{
+#if (NXT_HAVE_UNIX_DOMAIN)
+ size_t i;
+ nxt_buf_t *b;
+ const char *filename;
+ nxt_runtime_t *rt;
+ nxt_sockaddr_t *sa;
+ nxt_listen_socket_t *ls;
+
+ b = msg->buf;
+ sa = (nxt_sockaddr_t *) b->mem.pos;
+
+ filename = sa->u.sockaddr_un.sun_path;
+ unlink(filename);
+
+ rt = task->thread->runtime;
+
+ for (i = 0; i < rt->listen_sockets->nelts; i++) {
+ const char *name;
+
+ ls = (nxt_listen_socket_t *) rt->listen_sockets->elts + i;
+ sa = ls->sockaddr;
+
+ if (sa->u.sockaddr.sa_family != AF_UNIX
+ || sa->u.sockaddr_un.sun_path[0] == '\0')
+ {
+ continue;
+ }
+
+ name = sa->u.sockaddr_un.sun_path;
+ if (strcmp(name, filename) != 0) {
+ continue;
+ }
+
+ nxt_array_remove(rt->listen_sockets, ls);
+ break;
+ }
+#endif
+}
+
+
static nxt_conf_map_t nxt_app_lang_module_map[] = {
{
nxt_string("type"),