119 lines
2.9 KiB
C
119 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include <sys/msg.h>
|
|
#include <sys/sem.h>
|
|
#include <sys/shm.h>
|
|
|
|
#include <custom_sig.h>
|
|
#include <structures.h>
|
|
#include <nonsodovemetterle.h>
|
|
#include <errExit.h>
|
|
|
|
static int sigint_count = 2;
|
|
|
|
void setSignal(int sig);
|
|
|
|
void siginthandler2(int sig) {
|
|
kill(getpid(), SIGTERM);
|
|
}
|
|
|
|
void sigHandlerServer(int sig) {
|
|
// if (sig == SIGINT) { //FIXME: non riesco a farlo funzionare, quando premo ctr+c è come se gli arrivasse al server la mossa di un giocatore
|
|
// sigint_count--;
|
|
// if (sigint_count > 0) {
|
|
// printfServer("Press again Ctrl^C to exit ");
|
|
// printf("(whitin %d sec)\n", TIME_TO_RESET);
|
|
// alarm(TIME_TO_RESET);
|
|
// } else {
|
|
// alarm(0); // toglie l'allarme
|
|
// sig = SIGTERM;
|
|
// }
|
|
// }
|
|
|
|
// if (sig == SIGALRM) {
|
|
// printfServer("");
|
|
// printf("Time to exit (%d sec) expired", TIME_TO_RESET);
|
|
// sigint_count = 2;
|
|
// }
|
|
|
|
if (sig == SIGINT) {
|
|
printf("\nentarto\n");
|
|
signal(SIGINT, siginthandler2);
|
|
alarm(3);
|
|
}
|
|
|
|
if (sig == SIGALRM) {
|
|
printf("Resetting handler\n");
|
|
signal(SIGINT, sigHandlerServer);
|
|
}
|
|
|
|
|
|
if (sig == SIGTERM || sig == SIGHUP) {
|
|
if (pids[0] > 0) {
|
|
printfServer("Terminating player one");
|
|
kill(pids[0], SIGTERM);
|
|
}
|
|
if (pids[1] > 0) {
|
|
printfServer("Terminating player two");
|
|
kill(pids[1], SIGTERM);
|
|
}
|
|
|
|
// msgq
|
|
if (_MSGQID) {
|
|
printfServer("Deleting msg");
|
|
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) { //TODO: funzione
|
|
errExit("msgctl", "sigHandlerServer");
|
|
}
|
|
}
|
|
|
|
// sem
|
|
if (semid) {
|
|
printfServer("Deleting sem");
|
|
if (semctl(semid, 0, IPC_RMID, 0) == -1) {
|
|
errExit("semctl", "sigHandlerServer");
|
|
}
|
|
}
|
|
|
|
// shm
|
|
if (board) {
|
|
printfServer("Detathing shm");
|
|
if (shmdt(board) == -1) {
|
|
errExit("shmdt", "sigHandlerServer");
|
|
}
|
|
}
|
|
if (shmid){
|
|
printfServer("Deleting shm");
|
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
|
|
errExit("shmctl", "SigHandlerSever");
|
|
}
|
|
}
|
|
printf("\n");
|
|
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
void setServerSignalHandler() {
|
|
sigset_t mySet;
|
|
sigfillset(&mySet);
|
|
|
|
sigdelset(&mySet, SIGINT);
|
|
sigdelset(&mySet, SIGTERM);
|
|
sigdelset(&mySet, SIGHUP);
|
|
sigdelset(&mySet, SIGALRM);
|
|
|
|
sigprocmask(SIG_SETMASK, &mySet, NULL);
|
|
|
|
setSignal(SIGINT);
|
|
setSignal(SIGTERM);
|
|
setSignal(SIGHUP);
|
|
setSignal(SIGALRM);
|
|
}
|
|
|
|
void setSignal(int sig) {
|
|
if (signal(sig, sigHandlerServer) == SIG_ERR) {
|
|
errExit("signal", "f4Server");
|
|
}
|
|
} |