signals
This commit is contained in:
parent
bc4edb053d
commit
348243fbad
|
@ -1,7 +1,7 @@
|
|||
#ifndef SIGNAL_H
|
||||
#define SIGNAL_H
|
||||
|
||||
#include <signal.h>
|
||||
#define TIME_TO_RESET 10
|
||||
|
||||
void serverSigHandler(int sig);
|
||||
void setServerSignalHandler();
|
|
@ -3,6 +3,14 @@
|
|||
|
||||
#include <sys/types.h>
|
||||
|
||||
int semid = 0;
|
||||
int _MSGQID = 0;
|
||||
int shmid = 0;
|
||||
|
||||
tile_t *board = NULL;
|
||||
|
||||
pid_t pids[2];
|
||||
|
||||
// semaphore
|
||||
union semun {
|
||||
int val;
|
||||
|
@ -27,8 +35,4 @@ typedef enum {
|
|||
PLAYER2
|
||||
} tile_t;
|
||||
|
||||
// signal
|
||||
#define DEFAULT_SIGINT 2
|
||||
#define TIME_TO_RESET 10
|
||||
|
||||
#endif
|
|
@ -17,97 +17,15 @@
|
|||
#include <forza4.h>
|
||||
#include <nonsodovemetterle.h>
|
||||
#include <custom_msgq.h>
|
||||
|
||||
int semid = 0;
|
||||
int _MSGQID = 0;
|
||||
int shmid = 0;
|
||||
|
||||
tile_t *board = NULL;
|
||||
|
||||
pid_t pids[2];
|
||||
|
||||
void sigHandlerServer(int sig) {
|
||||
if (sig == SIGINT) {
|
||||
//TODO:sigdelset(&mySet, SIGTERM);
|
||||
|
||||
|
||||
|
||||
sig = SIGTERM; // per il secondo SIGINT basta modificare il sig in SIGTERM
|
||||
}
|
||||
|
||||
if (sig == SIGTERM || sig == SIGHUP) {
|
||||
if (pids[0] > 0) {
|
||||
printf("\n");
|
||||
printfServer("Terminating player one");
|
||||
kill(pids[0], SIGTERM);
|
||||
}
|
||||
if (pids[1] > 0){
|
||||
printf("\n");
|
||||
printfServer("Terminating player two");
|
||||
kill(pids[1], SIGTERM);
|
||||
}
|
||||
|
||||
// msgq
|
||||
if (_MSGQID) {
|
||||
printf("\n");
|
||||
printfServer("Deleting msg");
|
||||
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) { //TODO: funzione
|
||||
errExit("msgctl", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
|
||||
// sem
|
||||
if (semid) {
|
||||
printf("\n");
|
||||
printfServer("Deleting sem");
|
||||
if (semctl(semid, 0, IPC_RMID, 0) == -1) {
|
||||
errExit("semctl", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
|
||||
// shm
|
||||
if (board) {
|
||||
printf("\n");
|
||||
printfServer("Detathing shm");
|
||||
if (shmdt(board) == -1) {
|
||||
errExit("shmdt", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
if (shmid){
|
||||
printf("\n");
|
||||
printfServer("Deleting shm");
|
||||
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
|
||||
errExit("shmctl", "SigHandlerSever");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#include <custom_sig.h>
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
// CHECK_INPUT
|
||||
input_server_t input = check_input(argc, argv);
|
||||
|
||||
// SIGNAL
|
||||
printfServer("Setting up signals\n");
|
||||
sigset_t mySet;
|
||||
sigfillset(&mySet);
|
||||
sigdelset(&mySet, SIGINT);
|
||||
sigdelset(&mySet, SIGTERM);
|
||||
sigdelset(&mySet, SIGHUP);
|
||||
sigprocmask(SIG_SETMASK, &mySet, NULL);
|
||||
|
||||
if (signal(SIGINT, sigHandlerServer) == SIG_ERR) {
|
||||
errExit("signal SIGINT", "f4Server");
|
||||
}
|
||||
if (signal(SIGTERM, sigHandlerServer) == SIG_ERR) {
|
||||
errExit("signal SIGTERM", "f4Server");
|
||||
}
|
||||
setServerSignalHandler();
|
||||
//
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,107 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/msg.h>
|
||||
#include <sys/msg.h>
|
||||
|
||||
#include <custom_sig.h>
|
||||
#include <structures.h>
|
||||
|
||||
static int sigint_count = 2;
|
||||
|
||||
void setSignal(int sig);
|
||||
|
||||
void sigHandlerServer(int sig) {
|
||||
if (sig == SIGINT) {
|
||||
sigint_count--;
|
||||
if (sigint_count > 0) {
|
||||
printf("\n");
|
||||
printfServer("Press again Ctrl^C to exit ");
|
||||
printf("(whitin %d sec)\n", TIME_TO_RESET);
|
||||
alarm(TIME_TO_RESET);
|
||||
} else {
|
||||
sig = SIGTERM;
|
||||
}
|
||||
}
|
||||
|
||||
if (sig == SIGALRM) {
|
||||
printf("<Server> Time to exit (%d sec) expired\n", TIME_TO_RESET);
|
||||
sigint_count = 2;
|
||||
}
|
||||
|
||||
if (sig == SIGTERM || sig == SIGHUP) {
|
||||
if (pids[0] > 0) {
|
||||
printf("\n");
|
||||
printfServer("Terminating player one");
|
||||
kill(pids[0], SIGTERM);
|
||||
}
|
||||
if (pids[1] > 0){
|
||||
printf("\n");
|
||||
printfServer("Terminating player two");
|
||||
kill(pids[1], SIGTERM);
|
||||
}
|
||||
|
||||
// msgq
|
||||
if (_MSGQID) {
|
||||
printf("\n");
|
||||
printfServer("Deleting msg");
|
||||
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) { //TODO: funzione
|
||||
errExit("msgctl", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
|
||||
// sem
|
||||
if (semid) {
|
||||
printf("\n");
|
||||
printfServer("Deleting sem");
|
||||
if (semctl(semid, 0, IPC_RMID, 0) == -1) {
|
||||
errExit("semctl", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
|
||||
// shm
|
||||
if (board) {
|
||||
printf("\n");
|
||||
printfServer("Detathing shm");
|
||||
if (shmdt(board) == -1) {
|
||||
errExit("shmdt", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
if (shmid){
|
||||
printf("\n");
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <custom_signal.h>
|
||||
#include <structures.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 sec) expired\n", TIME_TO_RESET);
|
||||
sigint_count = DEFAULT_SIGINT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setServerSignalHandler() {
|
||||
signal(SIGINT, serverSigHandler);
|
||||
signal(SIGALRM, serverSigHandler);
|
||||
}
|
Loading…
Reference in New Issue