fixup comments to reflect that softclock no longer gets a frame ptr
[unix-history] / usr / src / old / implog / implog.c
index 610f20b..c9047a6 100644 (file)
@@ -1,45 +1,50 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1983, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)implog.c   5.2 (Berkeley) %G%";
-#endif not lint
-
-#include <stdio.h>
-#include <signal.h>
-#include <sgtty.h>
+static char sccsid[] = "@(#)implog.c   5.14 (Berkeley) %G%";
+#endif /* not lint */
 
 
+#include <sys/param.h>
 #include <sys/time.h>
 #include <sys/time.h>
-#include <sys/types.h>
+#include <sys/signal.h>
+#include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
 
 #include <sys/stat.h>
 #include <sys/socket.h>
 
+#include <net/if.h>
+
 #include <netinet/in.h>
 #include <netinet/in.h>
+#define        IMPMESSAGES
 #define        IMPLEADERS
 #include <netimp/if_imp.h>
 
 #define        IMPLEADERS
 #include <netimp/if_imp.h>
 
-#define        min(a, b)       ((a) < (b) ? (a) : (b))
+#include <sgtty.h>
+#include <stdio.h>
+#include "pathnames.h"
 
 u_char buf[1024];
 int    showdata = 1;
 int    showcontents = 0;
 
 u_char buf[1024];
 int    showdata = 1;
 int    showcontents = 0;
+int    rawheader = 0;
 int    follow = 0;
 int    follow = 0;
-int    link = -1;
+int    skip = 0;
+int    linkn = -1;
 int    host = -1;
 int    imp = -1;
 int    packettype = -1;
 extern int errno;
 int    log;
 int    host = -1;
 int    imp = -1;
 int    packettype = -1;
 extern int errno;
 int    log;
-char   *logfile = "/usr/adm/implog";
 
 /*
  * Socket address, internet style, with
 
 /*
  * Socket address, internet style, with
@@ -56,103 +61,101 @@ struct sockstamp {
 struct sockstamp from;
 
 main(argc, argv)
 struct sockstamp from;
 
 main(argc, argv)
-       char *argv[];
+       int argc;
+       char **argv;
 {
 {
+       extern int errno, optind;
+       extern char *optarg;
        struct stat b;
        struct stat b;
-       int size;
-       char *cp;
-
-       argc--, argv++;
-       while (argc > 0 && argv[0][0] == '-') {
-               if (strcmp(*argv, "-D") == 0) {
+       off_t size, lseek();
+       char *logfile, *strerror();
+       int ch;
+       long hostfrom, impfrom;
+
+       while ((ch = getopt(argc, argv, "DFLcfh:i:l:rt:")) != EOF)
+               switch(ch) {
+               case 'D':
                        showdata = 0;
                        showdata = 0;
-                       argv++, argc--;
-                       continue;
-               }
-               if (strcmp(*argv, "-f") == 0) {
+                       break;
+               case 'F':
+                       skip++;
+                       /* FALLTHROUGH */
+               case 'f':
                        follow++;
                        follow++;
-                       argv++, argc--;
-                       continue;
-               }
-               if (strcmp(*argv, "-c") == 0) {
+                       break;
+               case 'L':
+                       linkn = IMPLINK_IP;
+                       break;
+               case 'c':
                        showcontents++;
                        showcontents++;
-                       argv++, argc--;
-                       continue;
-               }
-               if (strcmp(*argv, "-l") == 0) {
-                       argc--, argv++;
-                       if (argc > 0) {
-                               link = atoi(*argv);
-                               argc--, argv++;
-                       } else
-                               link = IMPLINK_IP;
-                       continue;
-               }
-               if (strcmp(*argv, "-h") == 0) {
-                       argc--, argv++;
-                       if (argc < 1) {
-                               printf("-h: missing host #\n");
-                               exit(2);
-                       }
-                       host = atoi(*argv);
-                       argv++, argc--;
-                       continue;
-               }
-               if (strcmp(*argv, "-i") == 0) {
-                       argc--, argv++;
-                       if (argc < 1) {
-                               printf("-i: missing imp #\n");
-                               exit(2);
-                       }
-                       imp = atoi(*argv);
-                       argv++, argc--;
-                       continue;
-               }
-               if (strcmp(*argv, "-t") == 0) {
-                       argc--, argv++;;
-                       if (argc < 1) {
-                               printf("-t: missing packet type\n");
-                               exit(2);
-                       }
-                       packettype = atoi(*argv);
-                       argv++, argc--;;
-                       continue;
+                       break;
+               case 'h':
+                       host = atoi(optarg);
+                       break;
+               case 'i':
+                       imp = atoi(optarg);
+                       break;
+               case 'l':
+                       linkn = atoi(optarg);
+                       break;
+               case 'r':
+                       rawheader++;
+                       break;
+               case 't':
+                       packettype = atoi(optarg);
+                       break;
+               case '?':
+               default:
+                       fprintf(stderr,
+"usage: implog [-DFLcfr] [-h host] [-i imp] [-l link] [-t type] [logfile]\n");
+                       exit(2);
                }
                }
-               printf("usage: implog [ -D ] [ -c ] [ -f ] [-h #] [-i #] [ -t # ] [-l [#]] [logfile]\n");
-               exit(2);
-       }
-       if (argc > 0)
-               logfile = argv[0];
-       log = open(logfile, 0);
-       if (log < 0) {
-               perror(logfile);
+       argc -= optind;
+       argv += optind;
+
+       logfile = argc ? *argv : _PATH_IMPLOG;
+       log = open(logfile, O_RDONLY, 0);
+       if (log < 0 || fstat(log, &b)) {
+               fprintf(stderr, "implog: %s: %s\n", logfile, strerror(errno));
                exit(1);
        }
                exit(1);
        }
-       fstat(log, &b);
        size = b.st_size;
        size = b.st_size;
+       if (skip)
+               (void)lseek(log, size, L_SET);
 again:
        while (read(log, (char *)&from, sizeof(from)) == sizeof(from)) {
                if (from.sin_family == 0) {
                        printf("restarted: %.24s\n", ctime(&from.sin_time));
                        continue;
                }
 again:
        while (read(log, (char *)&from, sizeof(from)) == sizeof(from)) {
                if (from.sin_family == 0) {
                        printf("restarted: %.24s\n", ctime(&from.sin_time));
                        continue;
                }
-               if (host >= 0 && from.sin_addr.s_host != host) {
-                       lseek(log, from.sin_cc, 1);
+               if (host >= 0 || imp >= 0) {
+                       long addr = ntohl(from.sin_addr.s_addr);
+
+                       if (IN_CLASSA(addr)) {
+                               hostfrom = ((addr>>16) & 0xFF);
+                               impfrom = addr & 0xFF;
+                       } else if (IN_CLASSB(addr)) {
+                               hostfrom = ((addr>>8) & 0xFF);
+                               impfrom = addr & 0xFF;
+                       } else {
+                               hostfrom = ((addr>>4) & 0xF);
+                               impfrom = addr & 0xF;
+                       }
+               }
+               if (host >= 0 && hostfrom != host) {
+                       (void)lseek(log, (long)from.sin_cc, L_INCR);
                        continue;
                }
                        continue;
                }
-               if (imp >= 0) {
-                       from.sin_addr.s_imp = ntohs(from.sin_addr.s_imp);
-                       if (from.sin_addr.s_imp != imp) {
-                               lseek(log, from.sin_cc, 1);
-                               continue;
-                       }
+               if (imp >= 0 && impfrom != imp) {
+                       (void)lseek(log, (long)from.sin_cc, L_INCR);
+                       continue;
                }
                process(log, &from);
        }
        while (follow) {
                }
                process(log, &from);
        }
        while (follow) {
-               fflush(stdout);
-               sleep(5);
-               fstat(log, &b);
+               (void)fflush(stdout);
+               (void)sleep(5);
+               (void)fstat(log, &b);
                if (b.st_size > size) {
                        size = b.st_size;
                        goto again;
                if (b.st_size > size) {
                        size = b.st_size;
                        goto again;
@@ -195,43 +198,56 @@ process(l, f)
 {
        register struct messages *mp;
        struct imp_leader *ip;
 {
        register struct messages *mp;
        struct imp_leader *ip;
+       int (*fn)();
 
        if (read(l, (char *)buf, f->sin_cc) != f->sin_cc) {
 
        if (read(l, (char *)buf, f->sin_cc) != f->sin_cc) {
-               perror("read");
+               perror("implog: read");
                return;
        }
        ip = (struct imp_leader *)buf;
                return;
        }
        ip = (struct imp_leader *)buf;
-       ip->il_imp = ntohs((u_short)ip->il_imp);
-       for (mp = mtypes; mp->m_type != -1; mp++)
-               if (mp->m_type == ip->il_mtype)
-                       break;
-       if (mp->m_type == IMPTYPE_DATA) {
-               if (link >= 0 && ip->il_link != link)
+       ip->il_imp = ntohs(ip->il_imp);
+       if (ip->il_format != IMP_NFF)
+               fn = impundef;
+       else {
+               for (mp = mtypes; mp->m_type != (u_char)-1; mp++)
+                       if (mp->m_type == ip->il_mtype)
+                               break;
+               fn = mp->m_func;
+       }
+       if (ip->il_mtype == IMPTYPE_DATA) {
+               if (linkn >= 0 && ip->il_link != linkn)
                        return;
                if (!showdata)
                        return;
        }
                        return;
                if (!showdata)
                        return;
        }
-       if (packettype >= 0 && mp->m_type != packettype)
+       if (packettype >= 0 && ip->il_mtype != packettype)
                return;
        printf("%.24s: ", ctime(&f->sin_time));
                return;
        printf("%.24s: ", ctime(&f->sin_time));
-       (*mp->m_func)(ip, f->sin_cc);
+       if (f->sin_cc < sizeof(struct control_leader))
+               printf("(truncated header, %d bytes): ", f->sin_cc);
+       (*fn)(ip, f->sin_cc);
+       if (rawheader && fn != impundef) {
+               putchar('\t');
+               impundef(ip, f->sin_cc);
+       }
 }
 
 impdata(ip, cc)
        register struct imp_leader *ip;
 }
 
 impdata(ip, cc)
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("<%d/%d, DATA, link=", ip->il_host, ntohs((u_short)ip->il_imp));
+       printf("<DATA, source=%d/%u, link=", ip->il_host, (u_short)ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
                printf("%d,", ip->il_link);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
                printf("%d,", ip->il_link);
-       printf(" len=%d bytes>\n", ntohs((u_short)ip->il_length) >> 3);
+       printf(" len=%u bytes>\n", ntohs((u_short)ip->il_length) >> 3);
        if (showcontents) {
                register u_char *cp = ((u_char *)ip) + sizeof(*ip);
                register int i;
 
                i = (ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader);
        if (showcontents) {
                register u_char *cp = ((u_char *)ip) + sizeof(*ip);
                register int i;
 
                i = (ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader);
-               cc = min(i, cc);
+               cc = MIN(i, cc);
                printf("data: (%d bytes)", cc);
                for (i = 0; i < cc; i++, cp++) {
                        if (i % 25 == 0)
                printf("data: (%d bytes)", cc);
                for (i = 0; i < cc; i++, cp++) {
                        if (i % 25 == 0)
@@ -249,8 +265,10 @@ char *badleader[] = {
        "opposite leader type"
 };
 
        "opposite leader type"
 };
 
-impbadleader(ip)
+/* ARGSUSED */
+impbadleader(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
        printf("bad leader: ");
        if (ip->il_subtype > IMPLEADER_OPPOSITE)
 {
        printf("bad leader: ");
        if (ip->il_subtype > IMPLEADER_OPPOSITE)
@@ -259,23 +277,19 @@ impbadleader(ip)
                printf("%s\n", badleader[ip->il_subtype]);
 }
 
                printf("%s\n", badleader[ip->il_subtype]);
 }
 
-char *down[] = {
-       "in 30 secs",
-       "for hardware pm",
-       "for software reload",
-       "for emergency restart"
-};
-
-impdown(ip)
+/* ARGSUSED */
+impdown(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
        int tdown, tbackup;
 
 {
        int tdown, tbackup;
 
-       printf("imp going down %s", down[ip->il_link & IMP_DMASK]);
-       tdown = ((ip->il_link >> 2) & 0xf) * 5;
-       if (ip->il_link & IMP_DMASK)
+       printf("imp going down %s", impmessage[ip->il_link & IMP_DMASK]);
+       tdown = ((ip->il_link >> IMPDOWN_WHENSHIFT) & IMPDOWN_WHENMASK) *
+           IMPDOWN_WHENUNIT;
+       if ((ip->il_link & IMP_DMASK) != IMPDOWN_GOING)
                printf(" in %d minutes", tdown);
                printf(" in %d minutes", tdown);
-       tbackup = ip->il_subtype * 5;
+       tbackup = ip->il_subtype * IMPDOWN_WHENUNIT;
        printf(": back up ");
        if (tbackup)
                printf("%d minutes\n", tbackup);
        printf(": back up ");
        if (tbackup)
                printf("%d minutes\n", tbackup);
@@ -283,22 +297,25 @@ impdown(ip)
                printf("immediately\n");
 }
 
                printf("immediately\n");
 }
 
-impnoop(ip)
+/* ARGSUSED */
+impnoop(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("noop: host %d, imp %d\n", ip->il_host,
-               ntohs((u_short)ip->il_imp));
+       printf("noop: host %d, imp %u\n", ip->il_host, ip->il_imp);
 }
 
 }
 
-imprfnm(ip)
+/* ARGSUSED */
+imprfnm(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("rfnm: htype=%x, source=%d/%d, link=",
+       printf("rfnm: htype=%x, source=%d/%u, link=",
                ip->il_htype, ip->il_host, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
                ip->il_htype, ip->il_host, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
-               printf("%x,", ip->il_link);
+               printf("%d,", ip->il_link);
        printf(" subtype=%x\n", ip->il_subtype);
 }
 
        printf(" subtype=%x\n", ip->il_subtype);
 }
 
@@ -321,12 +338,14 @@ char *hostdead[] = {
        "host in the process of coming up"
 };
 
        "host in the process of coming up"
 };
 
-imphostdead(ip)
+/* ARGSUSED */
+imphostdead(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("host dead: ");
+       printf("host %u/%u dead: ", ip->il_host, ip->il_imp);
        if (ip->il_link & IMP_DMASK)
        if (ip->il_link & IMP_DMASK)
-               printf("down %s, ", down[ip->il_link & IMP_DMASK]);
+               printf("down %s, ", impmessage[ip->il_link & IMP_DMASK]);
        if (ip->il_subtype <= IMPHOST_COMINGUP)
                printf("%s\n", hostdead[ip->il_subtype]);
        else
        if (ip->il_subtype <= IMPHOST_COMINGUP)
                printf("%s\n", hostdead[ip->il_subtype]);
        else
@@ -340,25 +359,29 @@ char *hostunreach[] = {
        "communication is prohibited"
 };
 
        "communication is prohibited"
 };
 
-imphostunreach(ip)
+/* ARGSUSED */
+imphostunreach(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("host unreachable: ");
+       printf("host %u/%u unreachable: ", ip->il_host, ip->il_imp);
        if (ip->il_subtype <= IMPREACH_PROHIBITED)
                printf("%s\n", hostunreach[ip->il_subtype]);
        else
                printf("subtype=%x\n", ip->il_subtype);
 }
 
        if (ip->il_subtype <= IMPREACH_PROHIBITED)
                printf("%s\n", hostunreach[ip->il_subtype]);
        else
                printf("subtype=%x\n", ip->il_subtype);
 }
 
-impbaddata(ip)
+/* ARGSUSED */
+impbaddata(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("error in data: htype=%x, source=%d/%d, link=",
+       printf("error in data: htype=%x, source=%u/%u, link=",
                ip->il_htype, ip->il_host, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip, ");
        else
                ip->il_htype, ip->il_host, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip, ");
        else
-               printf("%x, ", ip->il_link);
+               printf("%d, ", ip->il_link);
        printf("subtype=%x\n", ip->il_subtype);
 }
 
        printf("subtype=%x\n", ip->il_subtype);
 }
 
@@ -371,23 +394,27 @@ char *incomplete[] = {
        "source imp i/o failure during receipt"
 };
 
        "source imp i/o failure during receipt"
 };
 
-impincomplete(ip)
+/* ARGSUSED */
+impincomplete(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
 {
-       printf("incomplete: htype=%x, source=%d/%d, link=",
+       printf("incomplete: htype=%x, source=%u/%u, link=",
                ip->il_htype, ip->il_host, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
                ip->il_htype, ip->il_host, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
-               printf("%x,", ip->il_link);
+               printf("%d,", ip->il_link);
        if (ip->il_subtype <= IMPCOMPLETE_IMPIO)
                printf(" %s\n", incomplete[ip->il_subtype]);
        else
                printf(" subtype=%x\n", ip->il_subtype);
 }
 
        if (ip->il_subtype <= IMPCOMPLETE_IMPIO)
                printf(" %s\n", incomplete[ip->il_subtype]);
        else
                printf(" subtype=%x\n", ip->il_subtype);
 }
 
-impreset(ip)
-       register struct imp_leader *ip;
+/* ARGSUSED */
+impreset(ip, cc)
+       struct imp_leader *ip;
+       int cc;
 {
        printf("reset complete\n");
 }
 {
        printf("reset complete\n");
 }
@@ -397,8 +424,10 @@ char *retry[] = {
        "connection block unavailable"
 };
 
        "connection block unavailable"
 };
 
-impretry(ip)
+/* ARGSUSED */
+impretry(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
        printf("refused, try again: ");
        if (ip->il_subtype <= IMPRETRY_BLOCK)
 {
        printf("refused, try again: ");
        if (ip->il_subtype <= IMPRETRY_BLOCK)
@@ -416,8 +445,10 @@ char *notify[] = {
        "transaction block for message not available"
 };
 
        "transaction block for message not available"
 };
 
-impnotify(ip)
+/* ARGSUSED */
+impnotify(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
        printf("refused, will notify: ");
        if (ip->il_subtype <= 5)
 {
        printf("refused, will notify: ");
        if (ip->il_subtype <= 5)
@@ -426,28 +457,38 @@ impnotify(ip)
                printf("subtype=%x\n", ip->il_subtype);
 }
 
                printf("subtype=%x\n", ip->il_subtype);
 }
 
-imptrying(ip)
-       register struct imp_leader *ip;
+/* ARGSUSED */
+imptrying(ip, cc)
+       struct imp_leader *ip;
+       int cc;
 {
        printf("refused, still trying\n");
 }
 
 {
        printf("refused, still trying\n");
 }
 
-impready(ip)
-       register struct imp_leader *ip;
+/* ARGSUSED */
+impready(ip, cc)
+       struct imp_leader *ip;
+       int cc;
 {
        printf("ready\n");
 }
 
 {
        printf("ready\n");
 }
 
-impundef(ip)
+/* ARGSUSED */
+impundef(ip, cc)
        register struct imp_leader *ip;
        register struct imp_leader *ip;
+       int cc;
 {
        printf("<fmt=%x, net=%x, flags=%x, mtype=", ip->il_format,
                ip->il_network, ip->il_flags);
 {
        printf("<fmt=%x, net=%x, flags=%x, mtype=", ip->il_format,
                ip->il_network, ip->il_flags);
-       printf("%x, htype=%x, host=%x, imp=%x, link=", ip->il_mtype,
-               ip->il_htype, ip->il_host, ip->il_imp);
+       printf("%x, htype=%x,\n\t host=%d(x%x), imp=%u(x%x), link=",
+               ip->il_mtype, ip->il_htype, ip->il_host, ip->il_host,
+               ip->il_imp, ip->il_imp);
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
        if (ip->il_link == IMPLINK_IP)
                printf("ip,");
        else
-               printf("%x,", ip->il_link);
-       printf(" subtype=%x>\n", ip->il_subtype);
+               printf("%d (x%x),", ip->il_link, ip->il_link);
+       printf(" subtype=%x", ip->il_subtype);
+       if (cc >= sizeof(struct imp_leader) && ip->il_length)
+               printf(" len=%u bytes", ntohs((u_short)ip->il_length) >> 3);
+       printf(">\n");
 }
 }