Skip to content

Commit

Permalink
avoid signal handler race. comment and help from deraadt.
Browse files Browse the repository at this point in the history
  • Loading branch information
itojun committed Jan 12, 2001
1 parent be85004 commit 9a41615
Showing 1 changed file with 85 additions and 32 deletions.
117 changes: 85 additions & 32 deletions kame/kame/ping6/ping6.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $KAME: ping6.c,v 1.110 2001/01/05 01:23:19 jinmei Exp $ */
/* $KAME: ping6.c,v 1.111 2001/01/12 19:04:03 itojun Exp $ */

/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
Expand Down Expand Up @@ -243,13 +243,19 @@ struct msghdr smsghdr;
struct iovec smsgiov;
char *scmsg = 0;

volatile int signo;
volatile int seenalrm;
volatile int seenint;
volatile int seeninfo;

int main __P((int, char *[]));
void fill __P((char *, char *));
int get_hoplim __P((struct msghdr *));
int get_pathmtu __P((struct msghdr *));
void set_pathmtu __P((int));
struct in6_pktinfo *get_rcvpktinfo __P((struct msghdr *));
void onalrm __P((int));
void onsignal __P((int));
void retransmit __P((void));
void oninfo __P((int));
void onint __P((int));
void pinger __P((void));
Expand Down Expand Up @@ -281,7 +287,7 @@ main(argc, argv)
{
struct itimerval itimer;
struct sockaddr_in6 from;
struct timeval timeout;
struct timeval timeout, *tv;
struct addrinfo hints;
fd_set *fdmaskp;
int fdmasks;
Expand All @@ -297,9 +303,6 @@ main(argc, argv)
#ifdef USE_RFC2292BIS
struct ip6_rthdr *rthdr = NULL;
#endif
#ifndef __OpenBSD__
struct timeval tv;
#endif
#ifdef IPSEC_POLICY_IPSEC
char *policy_in = NULL;
char *policy_out = NULL;
Expand Down Expand Up @@ -638,8 +641,8 @@ main(argc, argv)

ident = getpid() & 0xFFFF;
#ifndef __OpenBSD__
gettimeofday(&tv, NULL);
srand((unsigned int)(tv.tv_sec ^ tv.tv_usec ^ (long)ident));
gettimeofday(&timeout, NULL);
srand((unsigned int)(timeout.tv_sec ^ timeout.tv_usec ^ (long)ident));
memset(nonce, 0, sizeof(nonce));
for (i = 0; i < sizeof(nonce); i += sizeof(int))
*((int *)&nonce[i]) = rand();
Expand Down Expand Up @@ -968,23 +971,25 @@ main(argc, argv)
while (preload--) /* Fire off them quickies. */
pinger();

(void)signal(SIGINT, onint);
(void)signal(SIGINT, onsignal);
#ifdef SIGINFO
(void)signal(SIGINFO, oninfo);
(void)signal(SIGINFO, onsignal);
#endif

if ((options & F_FLOOD) == 0) {
(void)signal(SIGALRM, onalrm);
(void)signal(SIGALRM, onsignal);
itimer.it_interval = interval;
itimer.it_value.tv_sec = 0;
itimer.it_value.tv_usec = 1;
itimer.it_value = interval;
(void)setitimer(ITIMER_REAL, &itimer, NULL);
retransmit();
}

fdmasks = howmany(s+1, NFDBITS);
fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
if ((fdmaskp = malloc(fdmasks)) == NULL)
err(1, "malloc");

signo = seenalrm = seenint = seeninfo = 0;

for (;;) {
struct msghdr m;
struct cmsghdr *cm;
Expand All @@ -995,11 +1000,31 @@ main(argc, argv)
pinger();
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
memset(fdmaskp, 0, fdmasks);
FD_SET(s, fdmaskp);
if (select(s + 1, fdmaskp, NULL, NULL, &timeout) < 1)
continue;
}
tv = &timeout;
} else
tv = NULL;
memset(fdmaskp, 0, fdmasks);
FD_SET(s, fdmaskp);
cc = select(s + 1, fdmaskp, NULL, NULL, tv);
if ((cc < 0 && errno == EINTR) || (options & F_FLOOD)) {
if (!signo)
break;
if (seenalrm) {
retransmit();
seenalrm = 0;
}
if (seenint) {
onint(SIGINT);
seenint = 0;
}
if (seeninfo) {
oninfo(SIGINFO);
seeninfo = 0;
}
continue;
} else if (cc == 0)
continue;

fromlen = sizeof(from);

m.msg_name = (caddr_t)&from;
Expand All @@ -1013,13 +1038,28 @@ main(argc, argv)
m.msg_control = (caddr_t)buf;
m.msg_controllen = sizeof(buf);

if ((cc = recvmsg(s, &m, 0)) < 0) {
if (errno == EINTR)
cc = recvmsg(s, &m, 0);
if (cc < 0) {
if (errno != EINTR) {
warn("recvmsg");
continue;
}
if (!signo)
continue;
warn("recvmsg");
if (seenalrm) {
retransmit();
seenalrm = 0;
}
if (seenint) {
onint(SIGINT);
seenint = 0;
}
if (seeninfo) {
oninfo(SIGINFO);
seeninfo = 0;
}
continue;
}
else if (cc == 0) {
} else if (cc == 0) {
int mtu;

/*
Expand All @@ -1035,8 +1075,7 @@ main(argc, argv)
set_pathmtu(mtu);
}
continue;
}
else {
} else {
/*
* an ICMPv6 message (probably an echoreply) arrived.
*/
Expand All @@ -1049,14 +1088,30 @@ main(argc, argv)
exit(nreceived == 0);
}

void
onsignal(sig)
int sig;
{
signo = sig;
switch (sig) {
case SIGALRM:
seenalrm++;
break;
case SIGINT:
seenint++;
break;
case SIGINFO:
seeninfo++;
break;
}
}

/*
* onalrm --
* retransmit --
* This routine transmits another ping6.
*/
/* ARGSUSED */
void
onalrm(signo)
int signo;
retransmit()
{
struct itimerval itimer;

Expand Down Expand Up @@ -2044,10 +2099,8 @@ void
oninfo(notused)
int notused;
{
int save_errno = errno;

summary();
errno = save_errno;
}

/*
Expand Down

0 comments on commit 9a41615

Please sign in to comment.