From 2ec76f96d47003df6e828e26cb00d36d557f912e Mon Sep 17 00:00:00 2001 From: Kaelan Mikowicz Date: Mon, 9 Aug 2021 18:54:46 -0700 Subject: [PATCH] Make sure that UNIX sockets are removed before binding to them Socket and ControlSocket cleanup code will attempt to remove the unix socket files they create. However, if click were to crash or otherwise exit without calling cleanup, click will require an operator to intervene and delete any unix socket resources not removed. This change places an unlink() call before calls to bind() where a unix socket is used. No error or logging is handled since it is a best effort attempt to cleanup the unix socket. --- elements/userlevel/chattersocket.cc | 1 + elements/userlevel/controlsocket.cc | 1 + elements/userlevel/socket.cc | 2 ++ 3 files changed, 4 insertions(+) diff --git a/elements/userlevel/chattersocket.cc b/elements/userlevel/chattersocket.cc index 15dc170521..0b52bb7f4b 100644 --- a/elements/userlevel/chattersocket.cc +++ b/elements/userlevel/chattersocket.cc @@ -217,6 +217,7 @@ ChatterSocket::initialize_socket(ErrorHandler *errh) struct sockaddr_un sa; sa.sun_family = AF_UNIX; memcpy(sa.sun_path, _unix_pathname.c_str(), _unix_pathname.length() + 1); + unlink(_unix_pathname.c_str()); if (bind(_socket_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) return initialize_socket_error(errh, "bind"); } diff --git a/elements/userlevel/controlsocket.cc b/elements/userlevel/controlsocket.cc index 6d3190162c..ecadabf21f 100644 --- a/elements/userlevel/controlsocket.cc +++ b/elements/userlevel/controlsocket.cc @@ -215,6 +215,7 @@ ControlSocket::initialize_socket(ErrorHandler *errh) struct sockaddr_un sa; sa.sun_family = AF_UNIX; memcpy(sa.sun_path, _unix_pathname.c_str(), _unix_pathname.length() + 1); + unlink(_unix_pathname.c_str()); if (bind(_socket_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) return initialize_socket_error(errh, "bind"); diff --git a/elements/userlevel/socket.cc b/elements/userlevel/socket.cc index 83e38f2c86..987c8a7dd5 100644 --- a/elements/userlevel/socket.cc +++ b/elements/userlevel/socket.cc @@ -208,6 +208,8 @@ Socket::initialize(ErrorHandler *errh) } if (ret < 0) #endif + if (_local_pathname != "") + unlink(_local_pathname.c_str()); if (bind(_fd, (struct sockaddr *)&_local, _local_len) < 0) return initialize_socket_error(errh, "bind"); }