Elaborato_SO/src/custom_sig.c

123 lines
3.1 KiB
C
Raw Normal View History

2023-05-21 11:53:23 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
2023-05-21 11:53:23 +02:00
#include <custom_sig.h>
#include <structures.h>
#include <nonsodovemetterle.h>
#include <errExit.h>
2023-05-22 10:07:57 +02:00
#include <custom_msgq.h>
2023-05-21 11:53:23 +02:00
static int sigint_count = 2;
void setSignal(int sig);
2023-05-22 10:07:57 +02:00
void sigIntHandler2(int sig) {
2023-05-21 17:59:02 +02:00
kill(getpid(), SIGTERM);
}
2023-05-21 11:53:23 +02:00
void sigHandlerServer(int sig) {
2023-05-22 09:32:58 +02:00
// if (sig == SIGINT) { // FIXME: non riesco a farlo funzionare, quando premo ctr+c è come se gli arrivasse al server la mossa di un giocatore
// UPDATE: funzia ma è piu bella la mia sol?
2023-05-21 17:46:36 +02:00
// sigint_count--;
// if (sigint_count > 0) {
2023-05-22 09:32:58 +02:00
// printfServer("Press again Ctrl^C to exit ");
// printf("(whitin %d sec)\n", TIME_TO_RESET);
2023-05-21 17:46:36 +02:00
// alarm(TIME_TO_RESET);
// } else {
// alarm(0); // toglie l'allarme
// sig = SIGTERM;
// }
// }
2023-05-21 17:59:02 +02:00
// if (sig == SIGALRM) {
// printfServer("");
// printf("Time to exit (%d sec) expired", TIME_TO_RESET);
// sigint_count = 2;
// }
if (sig == SIGINT) {
2023-05-22 09:32:58 +02:00
printfServer("Press again Ctrl^C to exit ");
printf("(whitin %d sec)\n", TIME_TO_RESET);
2023-05-22 10:07:57 +02:00
signal(SIGINT, sigIntHandler2);
2023-05-22 09:32:58 +02:00
alarm(TIME_TO_RESET);
2023-05-21 11:53:23 +02:00
}
if (sig == SIGALRM) {
2023-05-22 09:32:58 +02:00
printfServer("");
printf("Time to exit (%d sec) expired", TIME_TO_RESET);
2023-05-21 17:59:02 +02:00
signal(SIGINT, sigHandlerServer);
2023-05-21 11:53:23 +02:00
}
2023-05-21 17:59:02 +02:00
2023-05-21 11:53:23 +02:00
if (sig == SIGTERM || sig == SIGHUP) {
if (pids[0] > 0) {
printfServer("Terminating player one");
kill(pids[0], SIGTERM);
}
2023-05-21 11:59:15 +02:00
if (pids[1] > 0) {
2023-05-21 11:53:23 +02:00
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");
2023-05-21 11:59:15 +02:00
exit(0);
}
2023-05-21 11:53:23 +02:00
}
void setServerSignalHandler() {
sigset_t mySet;
sigfillset(&mySet);
2023-05-22 09:32:58 +02:00
2023-05-21 11:53:23 +02:00
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");
}
}