First commit

This commit is contained in:
Edi De Candido 2023-05-12 12:39:48 +02:00
parent 2394cc7845
commit 12910fd7b6
3 changed files with 40 additions and 0 deletions

0
Makefile Normal file
View File

12
inc/custom_signal.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef SIGNAL_H
#define SIGNAL_H
#define DEFAULT_SIGINT 2
#define TIME_TO_RESET 30
#include <signal.h>
void serverSigHandler(int sig);
void setServerSignalHandler();
#endif

28
src/custom_signal.c Normal file
View File

@ -0,0 +1,28 @@
#include <unistd.h>
#include "../inc/custom_signal.h"
void serverSigHandler(int sig) {
static int sigint_count = DEFAULT_SIGINT;
switch (sig) {
case SIGINT:
sigint_count--;
if (sigint_count > 0) {
printf("<Server> Press again Ctrl^C to exit (whitin %d sec)\n", TIME_TO_RESET);
alarm(TIME_TO_RESET);
} else {
// TODO bisogna fare un "at exit"
exit(0);
}
break;
default: //SIGALRM
printf("<Server> Time to exit (%d s) expired", TIME_TO_RESET);
sigint_count = DEFAULT_SIGINT;
break;
}
}
void setServerSignalHandler() {
signal(SIGINT, serverSigHandler);
signal(SIGALRM, serverSigHandler);
}