update
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
index a332e2e..4f2a205 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)rwhod.c    5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)rwhod.c    5.9 (Berkeley) %G%";
 #endif not lint
 
 #include <sys/types.h>
 #endif not lint
 
 #include <sys/types.h>
@@ -84,8 +84,9 @@ struct        in_addr inet_makeaddr();
 main()
 {
        struct sockaddr_in from;
 main()
 {
        struct sockaddr_in from;
+       struct stat st;
        char path[64];
        char path[64];
-       int addr, on = 1;
+       int on = 1;
        char *cp;
        extern char *index();
 
        char *cp;
        extern char *index();
 
@@ -190,13 +191,11 @@ main()
                        continue;
                }
                (void) sprintf(path, "whod.%s", wd.wd_hostname);
                        continue;
                }
                (void) sprintf(path, "whod.%s", wd.wd_hostname);
-               whod = open(path, O_WRONLY);    /* much faster than creat() */
-               {       struct stat s;
-                       if (whod < 0 || fstat(whod, &s) < 0 || s.st_size > cc) {
-                               (void) close(whod);
-                               whod = creat(path, 0666);
-                       }
-               }
+               /*
+                * Rather than truncating and growing the file each time,
+                * use ftruncate if size is less than previous size.
+                */
+               whod = open(path, O_WRONLY | O_CREAT, 0644);
                if (whod < 0) {
                        syslog(LOG_WARNING, "%s: %m", path);
                        continue;
                if (whod < 0) {
                        syslog(LOG_WARNING, "%s: %m", path);
                        continue;
@@ -222,6 +221,8 @@ main()
 #endif
                (void) time(&wd.wd_recvtime);
                (void) write(whod, (char *)&wd, cc);
 #endif
                (void) time(&wd.wd_recvtime);
                (void) write(whod, (char *)&wd, cc);
+               if (fstat(whod, &st) < 0 || st.st_size > cc)
+                       ftruncate(whod, cc);
                (void) close(whod);
        }
 }
                (void) close(whod);
        }
 }
@@ -299,11 +300,19 @@ onalrm()
                        }
                utmpent = we - mywd.wd_we;
        }
                        }
                utmpent = we - mywd.wd_we;
        }
+
+       /*
+        * The test on utmpent looks silly---after all, if no one is
+        * logged on, why worry about efficiency?---but is useful on
+        * (e.g.) compute servers.
+        */
+       if (utmpent && chdir("/dev")) {
+               syslog(LOG_ERR, "chdir(/dev): %m");
+               exit(1);
+       }
        we = mywd.wd_we;
        for (i = 0; i < utmpent; i++) {
        we = mywd.wd_we;
        for (i = 0; i < utmpent; i++) {
-               char path[64];
-               (void) sprintf(path, "/dev/%s", we->we_utmp.out_line);
-               if (stat(path, &stb) >= 0)
+               if (stat(we->we_utmp.out_line, &stb) >= 0)
                        we->we_idle = htonl(now - stb.st_atime);
                we++;
        }
                        we->we_idle = htonl(now - stb.st_atime);
                we++;
        }
@@ -318,13 +327,16 @@ onalrm()
        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);
+       if (utmpent && chdir(RWHODIR)) {
+               syslog(LOG_ERR, "chdir(%s): %m", RWHODIR);
+               exit(1);
+       }
 done:
        (void) alarm(AL_INTERVAL);
 }
 
 getkmem()
 {
 done:
        (void) alarm(AL_INTERVAL);
 }
 
 getkmem()
 {
-       struct nlist *nlp;
        static ino_t vmunixino;
        static time_t vmunixctime;
        struct stat sb;
        static ino_t vmunixino;
        static time_t vmunixctime;
        struct stat sb;