-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathclient_heartbeat_helper.c
84 lines (74 loc) · 1.69 KB
/
client_heartbeat_helper.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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "client_heartbeat_helper.h"
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
static int nprobes;
static int maxnprobes;
static int nsec;
static int sockfd;
typedef void(*sig_handler)(int);
void sig_urg(int);
void sig_alrm(int);
sig_handler fuck(int signo, sig_handler newhandler)
{
struct sigaction newact, oldact;
bzero(&newact, sizeof(newact));
newact.sa_handler = newhandler;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
newact.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
newact.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signo, &newact, &oldact) < 0)
return SIG_ERR;
return oldact.sa_handler;
}
void heartbeat_client(int fd, int sec, int max)
{
sockfd = fd;
nsec = sec;
maxnprobes = max;
if (fuck(SIGURG, sig_urg) == SIG_ERR) {
perror("fuck error!");
exit(1);
}
fcntl(sockfd, F_SETOWN, getpid());
if (fuck(SIGALRM, sig_alrm) == SIG_ERR) {
perror("fuck error!");
exit(1);
}
alarm(nsec);
}
void sig_urg(int signo)
{
int n;
char c;
if ((n = recv(sockfd, &c, 1, MSG_OOB)) < 0) {
if (errno != EAGAIN) {
perror("recv error!");
exit(1);
}
}
printf("client recv server heartbeat packet, cur_probes=%d\n", nprobes);
nprobes = 0;
}
void sig_alrm(int signo)
{
char c;
if (++nprobes > maxnprobes) {
printf("server is unreachable\n");
exit(0);
}
send(sockfd, &c, 1, MSG_OOB);
printf("client send probes packet, cur_probes=%d\n", nprobes);
alarm(nsec);
}