Avoid crashing on SIGPIPE

Under some circumstances, clients could cause SIGPIPE to slcl. Since
this signal was not handled by server.c (i.e., via sigaction(3)), slcl
would crash without any error messages printed to stderr.

In such situation, SIGPIPE should not be usually considered a fatal
error, so it is preferrable to close the connection and keep working.
This commit is contained in:
Xavier Del Campo Romero 2023-05-01 03:06:34 +02:00
parent 7d1e41f9c5
commit 9c7a2e9128
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
2 changed files with 20 additions and 2 deletions

2
http.c
View File

@ -496,6 +496,8 @@ static int rw_error(const int r, bool *const close)
{
switch (errno)
{
case EPIPE:
/* Fall through. */
case ECONNRESET:
*close = true;
return 1;

View File

@ -167,7 +167,17 @@ static volatile sig_atomic_t do_exit;
static void handle_signal(const int signum)
{
do_exit = 1;
switch (signum)
{
case SIGINT:
/* Fall through. */
case SIGTERM:
do_exit = 1;
break;
default:
break;
}
}
struct server_client *server_poll(struct server *const s, bool *const io,
@ -286,7 +296,13 @@ static int init_signals(void)
}
else if (sigaction(SIGTERM, &sa, NULL))
{
fprintf(stderr, "%s: sigaction(2) SIGINT: %s\n",
fprintf(stderr, "%s: sigaction(2) SIGTERM: %s\n",
__func__, strerror(errno));
return -1;
}
else if (sigaction(SIGPIPE, &sa, NULL))
{
fprintf(stderr, "%s: sigaction(2) SIGPIPE: %s\n",
__func__, strerror(errno));
return -1;
}