From 5bce9eb803ff87338cbef6aa2e55f2ae8d7ddf00 Mon Sep 17 00:00:00 2001 From: Theo de Raadt Date: Fri, 23 Nov 2001 03:45:52 +0000 Subject: use siginterrupt() enabling around all blocking calls, and check the flags; millert ok --- usr.sbin/timed/timed/globals.h | 5 +- usr.sbin/timed/timed/measure.c | 36 ++++++--- usr.sbin/timed/timed/timed.c | 6 +- usr.sbin/timed/timedc/cmds.c | 135 +++++++++++++++++++++++++++------- usr.sbin/timed/timedc/timedc-extern.h | 7 +- usr.sbin/timed/timedc/timedc.c | 35 ++++++--- 6 files changed, 170 insertions(+), 54 deletions(-) diff --git a/usr.sbin/timed/timed/globals.h b/usr.sbin/timed/timed/globals.h index 9ee802d0324..6a085040870 100644 --- a/usr.sbin/timed/timed/globals.h +++ b/usr.sbin/timed/timed/globals.h @@ -1,4 +1,4 @@ -/* $OpenBSD: globals.h,v 1.3 2001/05/05 05:10:04 mickey Exp $ */ +/* $OpenBSD: globals.h,v 1.4 2001/11/23 03:45:51 deraadt Exp $ */ /*- * Copyright (c) 1985 The Regents of the University of California. @@ -36,7 +36,7 @@ */ #ifdef sgi -#ident "$Revision: 1.3 $" +#ident "$Revision: 1.4 $" #endif #include @@ -139,6 +139,7 @@ extern struct hosttbl hosttbl[NHOSTS+1]; #define self hosttbl[0] #define hostname (self.name) +volatile sig_atomic_t gotintr; struct netinfo { struct netinfo *next; diff --git a/usr.sbin/timed/timed/measure.c b/usr.sbin/timed/timed/measure.c index fd35b5fcd33..7d633b0db02 100644 --- a/usr.sbin/timed/timed/measure.c +++ b/usr.sbin/timed/timed/measure.c @@ -1,4 +1,4 @@ -/* $OpenBSD: measure.c,v 1.4 2001/05/05 05:10:04 mickey Exp $ */ +/* $OpenBSD: measure.c,v 1.5 2001/11/23 03:45:51 deraadt Exp $ */ /*- * Copyright (c) 1985, 1993 The Regents of the University of California. @@ -38,7 +38,7 @@ static char sccsid[] = "@(#)measure.c 5.1 (Berkeley) 5/11/93"; #endif /* not lint */ #ifdef sgi -#ident "$Revision: 1.4 $" +#ident "$Revision: 1.5 $" #endif #include "globals.h" @@ -101,7 +101,6 @@ measure(u_long maxmsec, /* wait this many msec at most */ } } - /* * empty the icmp input queue */ @@ -111,10 +110,16 @@ measure(u_long maxmsec, /* wait this many msec at most */ FD_SET(sock_raw, &ready); if (select(sock_raw+1, &ready, 0,0, &tout)) { length = sizeof(struct sockaddr_in); + siginterrupt(SIGINT, 1); cc = recvfrom(sock_raw, (char *)packet, PACKET_IN, 0, - 0,&length); - if (cc < 0) + 0, &length); + if (cc < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); goto quit; + } + siginterrupt(SIGINT, 0); continue; } break; @@ -161,14 +166,19 @@ measure(u_long maxmsec, /* wait this many msec at most */ oicp->icmp_cksum = in_cksum((u_short*)oicp, sizeof(*oicp)); + siginterrupt(SIGINT, 1); count = sendto(sock_raw, opacket, sizeof(*oicp), 0, (struct sockaddr*)addr, sizeof(struct sockaddr)); if (count < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); if (measure_status == HOSTDOWN) measure_status = UNREACHABLE; goto quit; } + siginterrupt(SIGINT, 0); ++oicp->icmp_seq; timeradd(&tcur, &twait, &ttrans); @@ -189,10 +199,16 @@ measure(u_long maxmsec, /* wait this many msec at most */ break; length = sizeof(struct sockaddr_in); + siginterrupt(SIGINT, 1); cc = recvfrom(sock_raw, (char *)packet, PACKET_IN, 0, 0,&length); - if (cc < 0) + if (cc < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); goto quit; + } + siginterrupt(SIGINT, 0); /* * got something. See if it is ours @@ -205,7 +221,6 @@ measure(u_long maxmsec, /* wait this many msec at most */ || icp->icmp_seq >= oicp->icmp_seq) continue; - sendtime = ntohl(icp->icmp_otime); recvtime = ((tcur.tv_sec % SECDAY) * 1000 + tcur.tv_usec / 1000); @@ -294,12 +309,11 @@ quit: } return(measure_status); +bail: + siginterrupt(SIGINT, 0); + return (0); } - - - - /* * round a number of milliseconds into a struct timeval */ diff --git a/usr.sbin/timed/timed/timed.c b/usr.sbin/timed/timed/timed.c index d7cbaf29d4c..a843c718ec9 100644 --- a/usr.sbin/timed/timed/timed.c +++ b/usr.sbin/timed/timed/timed.c @@ -1,4 +1,4 @@ -/* $OpenBSD: timed.c,v 1.10 2001/05/05 05:10:05 mickey Exp $ */ +/* $OpenBSD: timed.c,v 1.11 2001/11/23 03:45:51 deraadt Exp $ */ /*- * Copyright (c) 1985, 1993 The Regents of the University of California. @@ -44,7 +44,7 @@ static char sccsid[] = "@(#)timed.c 5.1 (Berkeley) 5/11/93"; #endif /* not lint */ #ifdef sgi -#ident "$Revision: 1.10 $" +#ident "$Revision: 1.11 $" #endif /* sgi */ #define TSPTYPES @@ -82,6 +82,8 @@ FILE *fd; /* trace file FD */ jmp_buf jmpenv; +volatile sig_atomic_t gotintr; + struct netinfo *nettab = 0; struct netinfo *slavenet; int Mflag; diff --git a/usr.sbin/timed/timedc/cmds.c b/usr.sbin/timed/timedc/cmds.c index c339675b6d2..574ec85c33f 100644 --- a/usr.sbin/timed/timedc/cmds.c +++ b/usr.sbin/timed/timedc/cmds.c @@ -1,4 +1,4 @@ -/* $Id: cmds.c,v 1.10 2001/04/07 20:02:09 ho Exp $ */ +/* $Id: cmds.c,v 1.11 2001/11/23 03:45:51 deraadt Exp $ */ /*- * Copyright (c) 1985, 1993 The Regents of the University of California. @@ -38,7 +38,7 @@ static char sccsid[] = "@(#)cmds.c 5.1 (Berkeley) 5/11/93"; #endif /* not lint */ #ifdef sgi -#ident "$Revision: 1.10 $" +#ident "$Revision: 1.11 $" #endif #include "timedc.h" @@ -49,6 +49,7 @@ static char sccsid[] = "@(#)cmds.c 5.1 (Berkeley) 5/11/93"; #include #include +#include #include #include #include @@ -98,38 +99,51 @@ daydiff(char *hostname) for (trials = 0; trials < 10; trials++) { /* ask for the time */ sec = 0; + + siginterrupt(SIGINT, 1); if (sendto(sock, &sec, sizeof(sec), 0, (struct sockaddr *)&dayaddr, sizeof(dayaddr)) < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("sendto(sock)"); - return (0); + goto bail; } + siginterrupt(SIGINT, 0); for (;;) { pfd.fd = sock; pfd.events = POLLIN; i = poll(&pfd, 1, 2 * 1000); if (i < 0) { - if (errno == EINTR) + if (errno == EINTR) { + if (gotintr) + goto bail; continue; + } perror("poll(date read)"); - return (0); + goto bail; } if (i == 0) break; fromlen = sizeof(from); + siginterrupt(SIGINT, 1); if (recvfrom(sock, &sec, sizeof(sec), 0, (struct sockaddr *)&from, &fromlen) < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("recvfrom(date read)"); - return (0); + goto bail; } + siginterrupt(SIGINT, 0); sec = ntohl(sec); if (sec < BU) { - fprintf(stderr, - "%s says it is before 1970: %lu", + fprintf(stderr, "%s says it is before 1970: %lu", hostname, sec); - return (0); + goto bail; } sec -= BU; @@ -140,6 +154,9 @@ daydiff(char *hostname) /* if we get here, we tried too many times */ fprintf(stderr,"%s will not tell us the date\n", hostname); + +bail: + siginterrupt(SIGINT, 0); return (0); } @@ -194,13 +211,22 @@ clockdiff(argc, argv) measure_status = 0; while (argc > 1) { - argc--; argv++; + argc--; + argv++; + + siginterrupt(SIGINT, 1); hp = gethostbyname(*argv); if (hp == NULL) { + if (errno == EINTR && gotintr) { + siginterrupt(SIGINT, 0); + return; + } + siginterrupt(SIGINT, 0); fprintf(stderr, "timedc: %s: ", *argv); herror(0); continue; } + siginterrupt(SIGINT, 0); server.sin_family = hp->h_addrtype; bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length); @@ -258,7 +284,6 @@ clockdiff(argc, argv) hp->h_name, -measure_delta, myname); } } - return; } @@ -268,14 +293,12 @@ clockdiff(argc, argv) void msite(int argc, char *argv[]) { - struct sockaddr_in dest; - struct sockaddr_in from; - struct tsp msg; + struct sockaddr_in dest, from; struct servent *srvp; + int i, length, cc; struct pollfd pfd; + struct tsp msg; char *tgtname; - int i, length; - int cc; if (argc < 1) { printf("Usage: msite [hostname]\n"); @@ -287,41 +310,62 @@ msite(int argc, char *argv[]) fprintf(stderr, "udp/timed: unknown service\n"); return; } + memset(&dest, 0, sizeof dest); dest.sin_port = srvp->s_port; dest.sin_family = AF_INET; (void)gethostname(myname, sizeof(myname)); i = 1; + do { tgtname = (i >= argc) ? myname : argv[i]; + siginterrupt(SIGINT, 1); hp = gethostbyname(tgtname); if (hp == 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); fprintf(stderr, "timedc: %s: ", tgtname); herror(0); continue; } - bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length); + bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length); (void)strlcpy(msg.tsp_name, myname, sizeof msg.tsp_name); msg.tsp_type = TSP_MSITE; msg.tsp_vers = TSPVERSION; bytenetorder(&msg); + if (sendto(sock, &msg, sizeof(struct tsp), 0, (struct sockaddr *)&dest, sizeof(dest)) < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("sendto"); continue; } pfd.fd = sock; pfd.events = POLLIN; - if (poll(&pfd, 1, 15 * 1000)) { + switch (poll(&pfd, 1, 15 * 1000)) { + case -1: + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); + continue; + case 1: length = sizeof(from); cc = recvfrom(sock, &msg, sizeof(struct tsp), 0, (struct sockaddr *)&from, &length); if (cc < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("recvfrom"); continue; } + siginterrupt(SIGINT, 0); + if (cc < sizeof(struct tsp)) { fprintf(stderr, "short packet (%u/%u bytes) from %s\n", @@ -341,10 +385,16 @@ msite(int argc, char *argv[]) printf("received wrong ack: %s\n", tsptype[msg.tsp_type]); } - } else { + break; + case 0: + siginterrupt(SIGINT, 0); printf("communication error with %s\n", tgtname); + break; } } while (++i < argc); + +bail: + siginterrupt(SIGINT, 0); } /* @@ -381,14 +431,23 @@ testing(int argc, char *argv[]) } while (argc > 1) { - argc--; argv++; + argc--; + argv++; + + siginterrupt(SIGINT, 1); hp = gethostbyname(*argv); if (hp == NULL) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); fprintf(stderr, "timedc: %s: ", *argv); herror(0); - argc--; argv++; + argc--; + argv++; continue; } + + memset(&sin, 0, sizeof sin); sin.sin_port = srvp->s_port; sin.sin_family = hp->h_addrtype; bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length); @@ -398,11 +457,17 @@ testing(int argc, char *argv[]) (void)gethostname(myname, sizeof(myname)); (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name)); bytenetorder(&msg); + if (sendto(sock, &msg, sizeof(struct tsp), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("sendto"); } } +bail: + siginterrupt(SIGINT, 0); } @@ -430,11 +495,17 @@ tracing(int argc, char *argv[]) fprintf(stderr, "udp/timed: unknown service\n"); return; } + + memset(&dest, 0, sizeof dest); dest.sin_port = srvp->s_port; dest.sin_family = AF_INET; (void)gethostname(myname,sizeof(myname)); + siginterrupt(SIGINT, 1); hp = gethostbyname(myname); + if (hp == NULL && errno == EINTR && gotintr) + goto bail; + bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length); if (strcmp(argv[1], "on") == 0) { @@ -450,24 +521,32 @@ tracing(int argc, char *argv[]) bytenetorder(&msg); if (sendto(sock, &msg, sizeof(struct tsp), 0, (struct sockaddr *)&dest, sizeof(dest)) < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("sendto"); return; } pfd.fd = sock; pfd.events = POLLIN; - if (poll(&pfd, 1, 5 * 1000)) { + switch (poll(&pfd, 1, 5 * 1000)) { + case 1: length = sizeof(from); cc = recvfrom(sock, &msg, sizeof(struct tsp), 0, (struct sockaddr *)&from, &length); if (cc < 0) { + if (errno == EINTR && gotintr) + goto bail; + siginterrupt(SIGINT, 0); perror("recvfrom"); return; } + siginterrupt(SIGINT, 0); if (cc < sizeof(struct tsp)) { fprintf(stderr, "short packet (%u/%u bytes) from %s\n", cc, sizeof(struct tsp), inet_ntoa(from.sin_addr)); - return; + goto bail; } bytehostorder(&msg); if (msg.tsp_type == TSP_ACK) { @@ -483,8 +562,14 @@ tracing(int argc, char *argv[]) printf("wrong ack received: %s\n", tsptype[msg.tsp_type]); } - } else + break; + case 0: + siginterrupt(SIGINT, 0); printf("communication error\n"); + break; + } +bail: + siginterrupt(SIGINT, 0); } int @@ -498,7 +583,7 @@ priv_resources() return (-1); } - bzero(&sin, sizeof sin); + memset(&sin, 0, sizeof sin); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; if (bindresvport(sock, &sin) < 0) { diff --git a/usr.sbin/timed/timedc/timedc-extern.h b/usr.sbin/timed/timedc/timedc-extern.h index 448a01f16f4..3ebad669227 100644 --- a/usr.sbin/timed/timedc/timedc-extern.h +++ b/usr.sbin/timed/timedc/timedc-extern.h @@ -1,4 +1,4 @@ -/* $Id: timedc-extern.h,v 1.2 2001/04/07 20:02:09 ho Exp $ */ +/* $Id: timedc-extern.h,v 1.3 2001/11/23 03:45:51 deraadt Exp $ */ /*- * Copyright (c) 1993 The Regents of the University of California. @@ -41,10 +41,13 @@ void bytehostorder(struct tsp *); void bytenetorder(struct tsp *); void clockdiff(int, char *[]); void help(int, char *[]); -void intr(int); void makeargv(void); void msite(int, char *[]); int priv_resources(void); void quit(void); void testing(int, char *[]); void tracing(int, char *[]); + +void sigintr(int); +extern volatile sig_atomic_t gotintr; + diff --git a/usr.sbin/timed/timedc/timedc.c b/usr.sbin/timed/timedc/timedc.c index 06c1a77b348..33a38a9067a 100644 --- a/usr.sbin/timed/timedc/timedc.c +++ b/usr.sbin/timed/timedc/timedc.c @@ -1,4 +1,4 @@ -/* $Id: timedc.c,v 1.4 2001/04/07 20:02:09 ho Exp $ */ +/* $Id: timedc.c,v 1.5 2001/11/23 03:45:51 deraadt Exp $ */ /*- * Copyright (c) 1985, 1993 The Regents of the University of California. @@ -44,14 +44,13 @@ static char sccsid[] = "@(#)timedc.c 5.1 (Berkeley) 5/11/93"; #endif /* not lint */ #ifdef sgi -#ident "$Revision: 1.4 $" +#ident "$Revision: 1.5 $" #endif #include "timedc.h" #include #include #include -#include #include #include #include @@ -62,9 +61,11 @@ int margc; int fromatty; char *margv[20]; char cmdline[200]; -jmp_buf toplevel; + static struct cmd *getcmd(char *); +volatile sig_atomic_t gotintr; + int main(int argc, char *argv[]) { @@ -103,16 +104,27 @@ main(int argc, char *argv[]) } fromatty = isatty(fileno(stdin)); - if (setjmp(toplevel)) - putchar('\n'); - (void) signal(SIGINT, intr); + (void) signal(SIGINT, sigintr); for (;;) { + if (gotintr) { + putchar('\n'); + gotintr = 0; + } if (fromatty) { printf("timedc> "); (void) fflush(stdout); } - if (fgets(cmdline, sizeof(cmdline), stdin) == 0) + + siginterrupt(SIGINT, 1); + if (fgets(cmdline, sizeof(cmdline), stdin) == NULL) { + if (errno == EINTR && gotintr) { + siginterrupt(SIGINT, 0); + continue; + } quit(); + } + siginterrupt(SIGINT, 0); + if (cmdline[0] == 0) break; makeargv(); @@ -137,15 +149,14 @@ main(int argc, char *argv[]) } void -intr(signo) +sigintr(signo) int signo; { if (!fromatty) - exit(0); - longjmp(toplevel, 1); + _exit(0); + gotintr = 1; } - static struct cmd * getcmd(char *name) { -- cgit v1.2.3