-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize_signals.c
58 lines (50 loc) · 1.91 KB
/
initialize_signals.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
#include "ping.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
extern ping_context_t ping_ctx;
static void interrupt(int a __attribute__((unused))) {
struct timeval end_time;
if (gettimeofday(&end_time, NULL) != 0) {
perror("cannot get end time");
exit(EXIT_FAILURE);
}
printf("\n--- %s ping statistics ---\n", ping_ctx.canon_dest);
const float loss_percentage = (1 - (float)ping_ctx.messages_received / (float)ping_ctx.messages_sent) * 100;
printf("%zu packets transmitted, %zu received, ",
ping_ctx.messages_sent, ping_ctx.messages_received);
if (ping_ctx.error_messages_received != 0) {
printf("+%zu errors, ", ping_ctx.error_messages_received);
}
printf("%.2f%% packet loss, time %ldms\n",
loss_percentage,
time_diff(&ping_ctx.time_program_started, &end_time) / MICROSECONDS_IN_MILLISECOND);
if (ping_ctx.stats_count != 0) {
const uint64_t avg = ping_ctx.acc_ping_time / ping_ctx.stats_count;
const uint64_t avg2 = ping_ctx.acc_ping_time2 / ping_ctx.stats_count;
const uint64_t mdev = sqrt(avg2 - avg * avg);
printf("rtt min/avg/max/mdev = %ld.%03ld/%ld.%03ld/%ld.%03ld/%ld.%03ld ms\n",
ping_ctx.min_ping_time / 1000, ping_ctx.min_ping_time % 1000,
avg / 1000, avg % 1000,
ping_ctx.max_ping_time / 1000, ping_ctx.max_ping_time % 1000,
mdev / 1000, mdev % 1000
);
}
exit(EXIT_SUCCESS);
}
void initialize_signals() {
if (signal(SIGALRM, interrupt) == SIG_ERR) {
perror("signal alarm error");
exit(EXIT_FAILURE);
}
if (signal(SIGINT, interrupt) == SIG_ERR) {
perror("signal interrupt error");
exit(EXIT_FAILURE);
}
if (ping_ctx.flags[PING_LIFETIME_LIM]) {
alarm(ping_ctx.seconds_to_work);
}
}