From ccaad38bc529897218a1e41c7e8704566695d1a9 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Sat, 25 Feb 2023 23:37:00 +0000 Subject: Socket: Remove Unix domain listen sockets at shutdown. If we don't remove the Unix domain listen socket file then when Unit restarts it get an error like 2023/02/25 23:10:11 [alert] 36388#36388 bind(\"unix:/tmp/unit.sock\") failed (98: Address already in use) This patch makes use of the listen_sockets array, that is already allocated in the main process but never populated, to place the Unix domain listen sockets into. At shutdown we can then loop through this array and unlink(2) any Unix domain sockets found therein. Closes: Reviewed-by: Alejandro Colomar Signed-off-by: Andrew Clayton --- src/nxt_main_process.c | 8 ++++++-- src/nxt_runtime.c | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c index 4c89121e..7548be9a 100644 --- a/src/nxt_main_process.c +++ b/src/nxt_main_process.c @@ -1182,8 +1182,9 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) if (sa->u.sockaddr.sa_family == AF_UNIX && sa->u.sockaddr_un.sun_path[0] != '\0') { - char *filename; - mode_t access; + char *filename; + mode_t access; + nxt_thread_t *thr; filename = sa->u.sockaddr_un.sun_path; access = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); @@ -1194,6 +1195,9 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) filename, nxt_errno); goto fail; } + + thr = nxt_thread(); + nxt_runtime_listen_socket_add(thr->runtime, sa); } #endif diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c index cb5fe421..739238af 100644 --- a/src/nxt_runtime.c +++ b/src/nxt_runtime.c @@ -563,6 +563,7 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) #if (NXT_HAVE_UNIX_DOMAIN) { + size_t i; nxt_sockaddr_t *sa; nxt_file_name_t *name; @@ -572,6 +573,22 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; (void) nxt_file_delete(name); } + + for (i = 0; i < rt->listen_sockets->nelts; i++) { + nxt_listen_socket_t *ls; + + 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 = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; + (void) nxt_file_delete(name); + } } #endif } -- cgit