add Berkeley copyright
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
index e4d7a8f..5a39fad 100644 (file)
@@ -1,18 +1,29 @@
 /*
 /*
- * 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 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+"@(#) Copyright (c) 1983 The 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[] = "@(#)rwhod.c    5.6 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)rwhod.c    5.11 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -76,7 +87,7 @@ int   s, utmpf, kmemf = -1;
 #define        RWHODIR         "/usr/spool/rwho"
 
 int    onalrm();
 #define        RWHODIR         "/usr/spool/rwho"
 
 int    onalrm();
-char   *strcpy(), *sprintf(), *malloc();
+char   *strcpy(), *malloc();
 long   lseek();
 int    getkmem();
 struct in_addr inet_makeaddr();
 long   lseek();
 int    getkmem();
 struct in_addr inet_makeaddr();
@@ -84,8 +95,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();
 
@@ -114,7 +126,10 @@ main()
          }
        }
 #endif
          }
        }
 #endif
-       (void) chdir("/dev");
+       if (chdir(RWHODIR) < 0) {
+               perror(RWHODIR);
+               exit(1);
+       }
        (void) signal(SIGHUP, getkmem);
        openlog("rwhod", LOG_PID, LOG_DAEMON);
        /*
        (void) signal(SIGHUP, getkmem);
        openlog("rwhod", LOG_PID, LOG_DAEMON);
        /*
@@ -186,8 +201,12 @@ main()
                                from.sin_addr);
                        continue;
                }
                                from.sin_addr);
                        continue;
                }
-               (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
-               whod = creat(path, 0666);
+               (void) sprintf(path, "whod.%s", wd.wd_hostname);
+               /*
+                * 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;
@@ -213,6 +232,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);
        }
 }
@@ -290,6 +311,16 @@ 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++) {
                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)
@@ -307,13 +338,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;