Why try to be intelligent about address families too late
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
index 1680659..e4d7a8f 100644 (file)
@@ -1,11 +1,24 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rwhod.c    4.11 (Berkeley) 83/05/28";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)rwhod.c    5.6 (Berkeley) %G%";
+#endif not lint
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#include <sys/file.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
@@ -17,7 +30,14 @@ static char sccsid[] = "@(#)rwhod.c  4.11 (Berkeley) 83/05/28";
 #include <utmp.h>
 #include <ctype.h>
 #include <netdb.h>
 #include <utmp.h>
 #include <ctype.h>
 #include <netdb.h>
-#include <rwhod.h>
+#include <syslog.h>
+#include <protocols/rwhod.h>
+
+/*
+ * Alarm interval. Don't forget to change the down time check in ruptime
+ * if this is changed.
+ */
+#define AL_INTERVAL (3 * 60)
 
 struct sockaddr_in sin = { AF_INET };
 
 
 struct sockaddr_in sin = { AF_INET };
 
@@ -65,9 +85,14 @@ main()
 {
        struct sockaddr_in from;
        char path[64];
 {
        struct sockaddr_in from;
        char path[64];
-       int addr;
-       struct hostent *hp;
+       int addr, on = 1;
+       char *cp;
+       extern char *index();
 
 
+       if (getuid()) {
+               fprintf(stderr, "rwhod: not super user\n");
+               exit(1);
+       }
        sp = getservbyname("who", "udp");
        if (sp == 0) {
                fprintf(stderr, "rwhod: udp/who: unknown service\n");
        sp = getservbyname("who", "udp");
        if (sp == 0) {
                fprintf(stderr, "rwhod: udp/who: unknown service\n");
@@ -91,46 +116,43 @@ main()
 #endif
        (void) chdir("/dev");
        (void) signal(SIGHUP, getkmem);
 #endif
        (void) chdir("/dev");
        (void) signal(SIGHUP, getkmem);
-       if (getuid()) {
-               fprintf(stderr, "rwhod: not super user\n");
-               exit(1);
-       }
+       openlog("rwhod", LOG_PID, LOG_DAEMON);
        /*
         * Establish host name as returned by system.
         */
        if (gethostname(myname, sizeof (myname) - 1) < 0) {
        /*
         * Establish host name as returned by system.
         */
        if (gethostname(myname, sizeof (myname) - 1) < 0) {
-               perror("gethostname");
+               syslog(LOG_ERR, "gethostname: %m");
                exit(1);
        }
                exit(1);
        }
+       if ((cp = index(myname, '.')) != NULL)
+               *cp = '\0';
        strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
        strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
-       utmpf = open("/etc/utmp", 0);
+       utmpf = open("/etc/utmp", O_RDONLY);
        if (utmpf < 0) {
                (void) close(creat("/etc/utmp", 0644));
        if (utmpf < 0) {
                (void) close(creat("/etc/utmp", 0644));
-               utmpf = open("/etc/utmp", 0);
+               utmpf = open("/etc/utmp", O_RDONLY);
        }
        if (utmpf < 0) {
        }
        if (utmpf < 0) {
-               perror("rwhod: /etc/utmp");
+               syslog(LOG_ERR, "/etc/utmp: %m");
                exit(1);
        }
        getkmem();
                exit(1);
        }
        getkmem();
-       if ((s = socket(AF_INET, SOCK_DGRAM, 0, 0)) < 0) {
-               perror("rwhod: socket");
+       if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+               syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
                exit(1);
        }
-       hp = gethostbyname(myname);
-       if (hp == NULL) {
-               fprintf(stderr, "%s: don't know my own name\n", myname);
+       if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
+               syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
                exit(1);
        }
                exit(1);
        }
-       sin.sin_family = hp->h_addrtype;
        sin.sin_port = sp->s_port;
        sin.sin_port = sp->s_port;
-       if (bind(s, &sin, sizeof (sin), 0) < 0) {
-               perror("rwhod: bind");
+       if (bind(s, &sin, sizeof (sin)) < 0) {
+               syslog(LOG_ERR, "bind: %m");
                exit(1);
        }
        if (!configure(s))
                exit(1);
                exit(1);
        }
        if (!configure(s))
                exit(1);
-       sigset(SIGALRM, onalrm);
+       signal(SIGALRM, onalrm);
        onalrm();
        for (;;) {
                struct whod wd;
        onalrm();
        for (;;) {
                struct whod wd;
@@ -140,43 +162,39 @@ main()
                        &from, &len);
                if (cc <= 0) {
                        if (cc < 0 && errno != EINTR)
                        &from, &len);
                if (cc <= 0) {
                        if (cc < 0 && errno != EINTR)
-                               perror("rwhod: recv");
+                               syslog(LOG_WARNING, "recv: %m");
                        continue;
                }
                if (from.sin_port != sp->s_port) {
                        continue;
                }
                if (from.sin_port != sp->s_port) {
-                       fprintf(stderr, "rwhod: %d: bad from port\n",
+                       syslog(LOG_WARNING, "%d: bad from port",
                                ntohs(from.sin_port));
                        continue;
                }
 #ifdef notdef
                if (gethostbyname(wd.wd_hostname) == 0) {
                                ntohs(from.sin_port));
                        continue;
                }
 #ifdef notdef
                if (gethostbyname(wd.wd_hostname) == 0) {
-                       fprintf(stderr, "rwhod: %s: unknown host\n",
+                       syslog(LOG_WARNING, "%s: unknown host",
                                wd.wd_hostname);
                        continue;
                }
 #endif
                                wd.wd_hostname);
                        continue;
                }
 #endif
-#ifdef notyet
                if (wd.wd_vers != WHODVERSION)
                        continue;
                if (wd.wd_type != WHODTYPE_STATUS)
                        continue;
                if (wd.wd_vers != WHODVERSION)
                        continue;
                if (wd.wd_type != WHODTYPE_STATUS)
                        continue;
-#endif
                if (!verify(wd.wd_hostname)) {
                if (!verify(wd.wd_hostname)) {
-                       fprintf(stderr, "rwhod: malformed host name from %x\n",
+                       syslog(LOG_WARNING, "malformed host name from %x",
                                from.sin_addr);
                        continue;
                }
                (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
                whod = creat(path, 0666);
                if (whod < 0) {
                                from.sin_addr);
                        continue;
                }
                (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
                whod = creat(path, 0666);
                if (whod < 0) {
-                       fprintf(stderr, "rwhod: ");
-                       perror(path);
+                       syslog(LOG_WARNING, "%s: %m", path);
                        continue;
                }
                        continue;
                }
-#ifdef notyet
 #if vax || pdp11
                {
 #if vax || pdp11
                {
-                       int i, n = (cc - WHDRSIZE)/sizeof(struct utmp);
+                       int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
                        struct whoent *we;
 
                        /* undo header byte swapping before writing to file */
                        struct whoent *we;
 
                        /* undo header byte swapping before writing to file */
@@ -187,11 +205,11 @@ main()
                        we = wd.wd_we;
                        for (i = 0; i < n; i++) {
                                we->we_idle = ntohl(we->we_idle);
                        we = wd.wd_we;
                        for (i = 0; i < n; i++) {
                                we->we_idle = ntohl(we->we_idle);
-                               we->we_utmp.ut_time = ntohl(we->we_utmp.ut_time);
+                               we->we_utmp.out_time =
+                                   ntohl(we->we_utmp.out_time);
                                we++;
                        }
                }
                                we++;
                        }
                }
-#endif
 #endif
                (void) time(&wd.wd_recvtime);
                (void) write(whod, (char *)&wd, cc);
 #endif
                (void) time(&wd.wd_recvtime);
                (void) write(whod, (char *)&wd, cc);
@@ -210,7 +228,7 @@ verify(name)
        register int size = 0;
 
        while (*name) {
        register int size = 0;
 
        while (*name) {
-               if (!isascii(*name) || !isalnum(*name))
+               if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
                        return (0);
                name++, size++;
        }
                        return (0);
                name++, size++;
        }
@@ -219,7 +237,8 @@ verify(name)
 
 int    utmptime;
 int    utmpent;
 
 int    utmptime;
 int    utmpent;
-struct utmp utmp[100];
+int    utmpsize = 0;
+struct utmp *utmp;
 int    alarmcount;
 
 onalrm()
 int    alarmcount;
 
 onalrm()
@@ -236,12 +255,25 @@ onalrm()
                getkmem();
        alarmcount++;
        (void) fstat(utmpf, &stb);
                getkmem();
        alarmcount++;
        (void) fstat(utmpf, &stb);
-       if (stb.st_mtime != utmptime) {
-               (void) lseek(utmpf, (long)0, 0);
-               cc = read(utmpf, (char *)utmp, sizeof (utmp));
+       if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
+               utmptime = stb.st_mtime;
+               if (stb.st_size > utmpsize) {
+                       utmpsize = stb.st_size + 10 * sizeof(struct utmp);
+                       if (utmp)
+                               utmp = (struct utmp *)realloc(utmp, utmpsize);
+                       else
+                               utmp = (struct utmp *)malloc(utmpsize);
+                       if (! utmp) {
+                               fprintf(stderr, "rwhod: malloc failed\n");
+                               utmpsize = 0;
+                               goto done;
+                       }
+               }
+               (void) lseek(utmpf, (long)0, L_SET);
+               cc = read(utmpf, (char *)utmp, stb.st_size);
                if (cc < 0) {
                        perror("/etc/utmp");
                if (cc < 0) {
                        perror("/etc/utmp");
-                       return;
+                       goto done;
                }
                wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
                utmpent = cc / sizeof (struct utmp);
                }
                wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
                utmpent = cc / sizeof (struct utmp);
@@ -251,11 +283,7 @@ onalrm()
                                   sizeof (utmp[i].ut_line));
                                bcopy(utmp[i].ut_name, we->we_utmp.out_name,
                                   sizeof (utmp[i].ut_name));
                                   sizeof (utmp[i].ut_line));
                                bcopy(utmp[i].ut_name, we->we_utmp.out_name,
                                   sizeof (utmp[i].ut_name));
-#ifdef notyet
                                we->we_utmp.out_time = htonl(utmp[i].ut_time);
                                we->we_utmp.out_time = htonl(utmp[i].ut_time);
-#else
-                               we->we_utmp.out_time = utmp[i].ut_time;
-#endif
                                if (we >= wlast)
                                        break;
                                we++;
                                if (we >= wlast)
                                        break;
                                we++;
@@ -265,64 +293,57 @@ onalrm()
        we = mywd.wd_we;
        for (i = 0; i < utmpent; i++) {
                if (stat(we->we_utmp.out_line, &stb) >= 0)
        we = mywd.wd_we;
        for (i = 0; i < utmpent; i++) {
                if (stat(we->we_utmp.out_line, &stb) >= 0)
-#ifdef notyet
                        we->we_idle = htonl(now - stb.st_atime);
                        we->we_idle = htonl(now - stb.st_atime);
-#else
-                       we->we_idle = now - stb.st_atime;
-#endif
                we++;
        }
                we++;
        }
-       (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, 0);
+       (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
        (void) read(kmemf, (char *)avenrun, sizeof (avenrun));
        for (i = 0; i < 3; i++)
        (void) read(kmemf, (char *)avenrun, sizeof (avenrun));
        for (i = 0; i < 3; i++)
-#ifdef notyet
-               mywd.wd_loadav[i] = htonl(avenrun[i] * 100);
-#else
-               mywd.wd_loadav[i] = avenrun[i] * 100;
-#endif
+               mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
        cc = (char *)we - (char *)&mywd;
        cc = (char *)we - (char *)&mywd;
-#ifdef notyet
        mywd.wd_sendtime = htonl(time(0));
        mywd.wd_vers = WHODVERSION;
        mywd.wd_type = WHODTYPE_STATUS;
        mywd.wd_sendtime = htonl(time(0));
        mywd.wd_vers = WHODVERSION;
        mywd.wd_type = WHODTYPE_STATUS;
-#else
-       mywd.wd_sendtime = time(0);
-#endif
        for (np = neighbors; np != NULL; np = np->n_next)
                (void) sendto(s, (char *)&mywd, cc, 0,
                        np->n_addr, np->n_addrlen);
        for (np = neighbors; np != NULL; np = np->n_next)
                (void) sendto(s, (char *)&mywd, cc, 0,
                        np->n_addr, np->n_addrlen);
-       (void) alarm(60);
+done:
+       (void) alarm(AL_INTERVAL);
 }
 
 getkmem()
 {
        struct nlist *nlp;
 }
 
 getkmem()
 {
        struct nlist *nlp;
+       static ino_t vmunixino;
+       static time_t vmunixctime;
+       struct stat sb;
 
 
-       signal(SIGHUP, getkmem);
+       if (stat("/vmunix", &sb) < 0) {
+               if (vmunixctime)
+                       return;
+       } else {
+               if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
+                       return;
+               vmunixctime = sb.st_ctime;
+               vmunixino= sb.st_ino;
+       }
        if (kmemf >= 0)
                (void) close(kmemf);
 loop:
        if (kmemf >= 0)
                (void) close(kmemf);
 loop:
-       for (nlp = &nl[sizeof (nl) / sizeof (nl[0])]; --nlp >= nl; ) {
-               nlp->n_value = 0;
-               nlp->n_type = 0;
-       }
-       nlist("/vmunix", nl);
-       if (nl[0].n_value == 0) {
-               fprintf(stderr, "/vmunix namelist botch\n");
+       if (nlist("/vmunix", nl)) {
+               syslog(LOG_WARNING, "/vmunix namelist botch");
                sleep(300);
                goto loop;
        }
                sleep(300);
                goto loop;
        }
-       kmemf = open("/dev/kmem", 0);
+       kmemf = open("/dev/kmem", O_RDONLY);
        if (kmemf < 0) {
        if (kmemf < 0) {
-               perror("/dev/kmem");
-               sleep(300);
-               goto loop;
+               syslog(LOG_ERR, "/dev/kmem: %m");
+               exit(1);
        }
        }
-       (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, 0);
-       (void) read(kmemf, (char *)&mywd.wd_boottime, sizeof (mywd.wd_boottime));
-#ifdef notyet
+       (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
+       (void) read(kmemf, (char *)&mywd.wd_boottime,
+           sizeof (mywd.wd_boottime));
        mywd.wd_boottime = htonl(mywd.wd_boottime);
        mywd.wd_boottime = htonl(mywd.wd_boottime);
-#endif
 }
 
 /*
 }
 
 /*
@@ -335,14 +356,14 @@ configure(s)
        char buf[BUFSIZ];
        struct ifconf ifc;
        struct ifreq ifreq, *ifr;
        char buf[BUFSIZ];
        struct ifconf ifc;
        struct ifreq ifreq, *ifr;
-       int n;
        struct sockaddr_in *sin;
        register struct neighbor *np;
        struct sockaddr_in *sin;
        register struct neighbor *np;
+       int n;
 
        ifc.ifc_len = sizeof (buf);
        ifc.ifc_buf = buf;
        if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
 
        ifc.ifc_len = sizeof (buf);
        ifc.ifc_buf = buf;
        if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
-               perror("rwhod: ioctl (get interface configuration)");
+               syslog(LOG_ERR, "ioctl (get interface configuration)");
                return (0);
        }
        ifr = ifc.ifc_req;
                return (0);
        }
        ifr = ifc.ifc_req;
@@ -372,18 +393,19 @@ configure(s)
                }
                bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
                if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
                }
                bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
                if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
-                       perror("rwhod: ioctl (get interface flags)");
+                       syslog(LOG_ERR, "ioctl (get interface flags)");
                        free((char *)np);
                        continue;
                }
                        free((char *)np);
                        continue;
                }
-               if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
+               if ((ifreq.ifr_flags & IFF_UP) == 0 ||
+                   (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
                        free((char *)np);
                        continue;
                }
                np->n_flags = ifreq.ifr_flags;
                if (np->n_flags & IFF_POINTOPOINT) {
                        if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
                        free((char *)np);
                        continue;
                }
                np->n_flags = ifreq.ifr_flags;
                if (np->n_flags & IFF_POINTOPOINT) {
                        if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
-                               perror("rwhod: ioctl (get dstaddr)");
+                               syslog(LOG_ERR, "ioctl (get dstaddr)");
                                free((char *)np);
                                continue;
                        }
                                free((char *)np);
                                continue;
                        }
@@ -392,10 +414,14 @@ configure(s)
                          np->n_addr, np->n_addrlen);
                }
                if (np->n_flags & IFF_BROADCAST) {
                          np->n_addr, np->n_addrlen);
                }
                if (np->n_flags & IFF_BROADCAST) {
+                       if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
+                               syslog(LOG_ERR, "ioctl (get broadaddr)");
+                               free((char *)np);
+                               continue;
+                       }
                        /* we assume addresses are all the same size */
                        /* we assume addresses are all the same size */
-                       sin = (struct sockaddr_in *)np->n_addr;
-                       sin->sin_addr =
-                         inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
+                       bcopy((char *)&ifreq.ifr_broadaddr,
+                         np->n_addr, np->n_addrlen);
                }
                /* gag, wish we could get rid of Internet dependencies */
                sin = (struct sockaddr_in *)np->n_addr;
                }
                /* gag, wish we could get rid of Internet dependencies */
                sin = (struct sockaddr_in *)np->n_addr;
@@ -421,17 +447,18 @@ sendto(s, buf, cc, flags, to, tolen)
 
        printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
        printf("hostname %s %s\n", w->wd_hostname,
 
        printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
        printf("hostname %s %s\n", w->wd_hostname,
-          interval(w->wd_sendtime - w->wd_boottime, "  up"));
+          interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
        printf("load %4.2f, %4.2f, %4.2f\n",
        printf("load %4.2f, %4.2f, %4.2f\n",
-           w->wd_loadav[0] / 100.0, w->wd_loadav[1] / 100.0,
-           w->wd_loadav[2] / 100.0);
+           ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
+           ntohl(w->wd_loadav[2]) / 100.0);
        cc -= WHDRSIZE;
        for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
        cc -= WHDRSIZE;
        for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
+               time_t t = ntohl(we->we_utmp.out_time);
                printf("%-8.8s %s:%s %.12s",
                printf("%-8.8s %s:%s %.12s",
-                        we->we_utmp.out_name,
-                        w->wd_hostname, we->we_utmp.out_line,
-                       ctime((time_t *)&we->we_utmp.out_time)+4);
-               we->we_idle /= 60;
+                       we->we_utmp.out_name,
+                       w->wd_hostname, we->we_utmp.out_line,
+                       ctime(&t)+4);
+               we->we_idle = ntohl(we->we_idle) / 60;
                if (we->we_idle) {
                        if (we->we_idle >= 100*60)
                                we->we_idle = 100*60 - 1;
                if (we->we_idle) {
                        if (we->we_idle >= 100*60)
                                we->we_idle = 100*60 - 1;