diff options
Diffstat (limited to 'btasktd.c')
| -rw-r--r-- | btasktd.c | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -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"); } |
