-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal_handler.c
119 lines (110 loc) · 3.58 KB
/
signal_handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @file signal_handler.c
* @brief File contenente l'implementazioni di alcune funzioni dichiarate in signal_handler.h
* @author Emilio Panti 531844
* Si dichiara che il contenuto di questo file e' in ogni sua parte opera
* originale dell'autore
*/
#define _POSIX_C_SOURCE 200809L
#include <signal_handler.h>
#include <errno.h>
#include <stats.h>
#include <config.h>
/**
* @function signal_handler
* @brief Funzione eseguita dal thread signal_handler
*
* @param arg argomenti necessari al thread signal_handler
*
* @return 0 in caso di successo, altrimenti error
*/
void *signal_handler(void *arg) {
//configurazioni del server (definita in chatty.c)
extern configs_t conf_server;
sigset_t *set = ((args_sig_handler_t*)arg)->set;
void (*fun_clean) () = ((args_sig_handler_t*)arg)->fun_clean;
//variabile di ritorno pthread_exit
long ret = 0;
//apro il file delle statistiche in scrittura se è stato
//specificato il nome di esso nel file di configurazione
FILE *fl = NULL;
if (conf_server.stat_file_name != NULL) {
fl = fopen(conf_server.stat_file_name, "a");
if (fl == NULL) {
perror("apertura file statistiche");
fun_clean();
ret = errno;
pthread_exit((void *) ret);
}
}
//variabile di appoggio
int sig = 0, checklock = 0;
while(1) {
//aspetto un segnale del set
int r = sigwait(set, &sig);
if (r != 0) {
errno = r;
perror("sigwait");
if (fl != NULL) fclose(fl);
fun_clean();
ret = errno;
pthread_exit((void *) ret);
}
//controllo quale segnale è arrivato
switch(sig) {
case SIGINT:
case SIGTERM:
case SIGQUIT:
//terminazione per richiesta (successo)
if (fl != NULL) fclose(fl);
fun_clean();
ret = 0;
pthread_exit((void *) ret);
break;
case SIGUSR1:
//stampare le statistiche
if (fl != NULL) {
checklock = lock_stats();
if (checklock != 0) {
fprintf(stderr,"errore lock_stats");
fclose(fl);
fun_clean();
ret = checklock;
pthread_exit((void *) ret);
}
if (printStats(fl) == -1) {
fprintf(stderr,"errore stampa statistiche nel relativo file");
unlock_stats();
fclose(fl);
fun_clean();
ret = errno;
pthread_exit((void *) ret);
}
checklock = unlock_stats();
if (checklock != 0) {
fprintf(stderr,"errore unlock_stats");
fclose(fl);
fun_clean();
ret = checklock;
pthread_exit((void *) ret);
}
}
break;
case SIGPIPE:
//da ignorare
;
break;
case SIGUSR2:
//terminazione per errore (inviata dagli threads in caso di errore)
if (fl != NULL) fclose(fl);
fun_clean();
ret = -1;
pthread_exit((void *) ret);
break;
default:
;
break;
}
}
return 0;
}