mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Handle stale unix socket file
Delete existing unix socket file if it is tested to be stale, meaning no one is listening on it.
This commit is contained in:
parent
0afcef807b
commit
8188bb5b76
1 changed files with 47 additions and 1 deletions
46
src/socket.c
46
src/socket.c
|
|
@ -78,6 +78,44 @@ static void socket_exit(void)
|
|||
}
|
||||
}
|
||||
|
||||
static bool socket_stale(const char *path)
|
||||
{
|
||||
struct sockaddr_un addr;
|
||||
bool stale = false;
|
||||
int sockfd;
|
||||
|
||||
/* Test if socket file exists */
|
||||
if (access(path, F_OK) == 0)
|
||||
{
|
||||
/* Create test socket */
|
||||
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
tio_warning_printf("Failure opening socket (%s)", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Prepare address */
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
/* Perform connect to test if socket is active */
|
||||
if (connect(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1)
|
||||
{
|
||||
if (errno == ECONNREFUSED)
|
||||
{
|
||||
// No one is listening on socket file
|
||||
stale = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Cleanup */
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
return stale;
|
||||
}
|
||||
|
||||
void socket_configure(void)
|
||||
{
|
||||
struct sockaddr_un sockaddr_unix = {};
|
||||
|
|
@ -146,6 +184,14 @@ void socket_configure(void)
|
|||
strncpy(sockaddr_unix.sun_path, socket_filename(), sizeof(sockaddr_unix.sun_path) - 1);
|
||||
sockaddr_p = (struct sockaddr *) &sockaddr_unix;
|
||||
socklen = sizeof(sockaddr_unix);
|
||||
|
||||
/* Test for stale unix socket file */
|
||||
if (socket_stale(socket_filename()))
|
||||
{
|
||||
tio_printf("Cleaning up old socket file");
|
||||
unlink(socket_filename());
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AF_INET:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue