1 #include "unp.h"
2
3 static int servfd;
4 static int nsec; /* #seconds betweeen each alarm */
5 static int maxnprobes; /* #probes w/no response before quit */
6 static int nprobes; /* #probes since last server response */
7 static void sig_urg(int), sig_alrm(int);
8
9 void
heartbeat_cli(int servfd_arg,int nsec_arg,int maxnprobes_arg)10 heartbeat_cli(int servfd_arg, int nsec_arg, int maxnprobes_arg)
11 {
12 servfd = servfd_arg; /* set globals for signal handlers */
13 if ( (nsec = nsec_arg) < 1)
14 nsec = 1;
15 if ( (maxnprobes = maxnprobes_arg) < nsec)
16 maxnprobes = nsec;
17 nprobes = 0;
18
19 Signal(SIGURG, sig_urg);
20 Fcntl(servfd, F_SETOWN, getpid());
21
22 Signal(SIGALRM, sig_alrm);
23 alarm(nsec);
24 }
25
26 static void
sig_urg(int signo)27 sig_urg(int signo)
28 {
29 int n;
30 char c;
31
32 if ( (n = recv(servfd, &c, 1, MSG_OOB)) < 0) {
33 if (errno != EWOULDBLOCK)
34 err_sys("recv error");
35 }
36 nprobes = 0; /* reset counter */
37 return; /* may interrupt client code */
38 }
39
40 static void
sig_alrm(int signo)41 sig_alrm(int signo)
42 {
43 if (++nprobes > maxnprobes) {
44 fprintf(stderr, "server is unreachable\n");
45 exit(0);
46 }
47 Send(servfd, "1", 1, MSG_OOB);
48 alarm(nsec);
49 return; /* may interrupt client code */
50 }
51