aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMr. Goblin <mrgoblin@mrgoblin.net>2026-09-29 20:58:39 +0300
committerMr. Goblin <mrgoblin@mrgoblin.net>2026-09-29 20:58:39 +0300
commit96df76536da0a3f49390717d8d601d5e59538c27 (patch)
tree1dacf041a0038cef08d2aacd546d958b605ab44c
parent76eae6e283c42d1c4a5a0efb94f7d6366d40487b (diff)
sigint handler
-rw-r--r--btasktd.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/btasktd.c b/btasktd.c
index 75af779..82092b2 100644
--- a/btasktd.c
+++ b/btasktd.c
@@ -39,6 +39,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
+#include <signal.h>
#include "cliflags.h"
#include "codes.h"
@@ -223,6 +224,30 @@ handle_user(struct btasktd *this, int id)
handle_buf(this, id, 1);
}
+static void
+bye_all_clients(struct btasktd *this)
+{
+ int i = 0;
+ for (i = 0; i < this->users_cnt; i++) {
+ if (-1 == this->users[i].fd)
+ continue;
+ send_response_code(this->users[i].fd,
+ CODE_503_SERVICE_UNAVAILABLE);
+ close(this->users[i].fd);
+ this->users[i].fd = -1;
+ }
+}
+
+static volatile int last_sig = 0;
+
+static void
+handle_sigint(int sig)
+{
+ signal(sig, handle_sigint);
+ fprintf(stderr, "Caught signal %d, shutting down...\n", sig);
+ last_sig = SIGINT;
+}
+
int
main(int argc, char **argv)
{
@@ -344,6 +369,8 @@ main(int argc, char **argv)
server.users = users;
printf("btaskt server btasktd started on port %d\n", port);
+ signal(SIGINT, handle_sigint);
+
for (;;) {
FD_ZERO(&readfds);
FD_SET(server_fd, &readfds);
@@ -359,6 +386,10 @@ main(int argc, char **argv)
if (-1 == res) {
if (EINTR == errno) {
/* signal handler called */
+ if (SIGINT == last_sig) {
+ bye_all_clients(&server);
+ break;
+ }
} else {
perror("select");
}