forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeatserv.c
50 lines (42 loc) · 1.07 KB
/
heartbeatserv.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
#include "unp.h"
static int servfd;
static int nsec; /* #seconds between each alarm */
static int maxnalarms; /* #alarms w/no client probe before quit */
static int nprobes; /* #alarms since last client probe */
static void sig_urg(int), sig_alrm(int);
void
heartbeat_serv(int servfd_arg, int nsec_arg, int maxnalarms_arg)
{
servfd = servfd_arg; /* set globals for signal handlers */
if ( (nsec = nsec_arg) < 1)
nsec = 1;
if ( (maxnalarms = maxnalarms_arg) < nsec)
maxnalarms = nsec;
Signal(SIGURG, sig_urg);
Fcntl(servfd, F_SETOWN, getpid());
Signal(SIGALRM, sig_alrm);
alarm(nsec);
}
static void
sig_urg(int signo)
{
int n;
char c;
if ( (n = recv(servfd, &c, 1, MSG_OOB)) < 0) {
if (errno != EWOULDBLOCK)
err_sys("recv error");
}
Send(servfd, &c, 1, MSG_OOB); /* echo back out-of-band byte */
nprobes = 0; /* reset counter */
return; /* may interrupt server code */
}
static void
sig_alrm(int signo)
{
if (++nprobes > maxnalarms) {
printf("no probes from client\n");
exit(0);
}
alarm(nsec);
return; /* may interrupt server code */
}