BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / timed / timed / timed.c
index 26e3136..2ba21a8 100644 (file)
@@ -2,12 +2,19 @@
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at 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'' without express or implied warranty.
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * 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
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -17,7 +24,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)timed.c    2.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)timed.c    2.20 (Berkeley) 6/29/90";
 #endif /* not lint */
 
 #include "globals.h"
 #endif /* not lint */
 
 #include "globals.h"
@@ -27,6 +34,7 @@ static char sccsid[] = "@(#)timed.c   2.13 (Berkeley) %G%";
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <setjmp.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <setjmp.h>
+#include "pathnames.h"
 
 int id;
 int trace;
 
 int id;
 int trace;
@@ -41,7 +49,7 @@ long delay2;
 long random();
 char hostname[MAXHOSTNAMELEN];
 struct host hp[NHOSTS];
 long random();
 char hostname[MAXHOSTNAMELEN];
 struct host hp[NHOSTS];
-char tracefile[] = "/usr/adm/timed.log";
+char tracefile[] = _PATH_TIMEDLOG;
 FILE *fd;
 jmp_buf jmpenv;
 struct netinfo *nettab = NULL;
 FILE *fd;
 jmp_buf jmpenv;
 struct netinfo *nettab = NULL;
@@ -88,7 +96,7 @@ char **argv;
        char *date();
        int n;
        int flag;
        char *date();
        int n;
        int flag;
-       char buf[BUFSIZ];
+       char buf[BUFSIZ], *cp, *cplim;
        struct ifconf ifc;
        struct ifreq ifreq, *ifr;
        register struct netinfo *ntp;
        struct ifconf ifc;
        struct ifreq ifreq, *ifr;
        register struct netinfo *ntp;
@@ -156,6 +164,10 @@ char **argv;
                } while (*++(*argv));
        }
 
                } while (*++(*argv));
        }
 
+#ifndef DEBUG
+       daemon(0, 0);
+#endif
+
        if (trace == ON) {
                fd = fopen(tracefile, "w");
                setlinebuf(fd);
        if (trace == ON) {
                fd = fopen(tracefile, "w");
                setlinebuf(fd);
@@ -229,9 +241,13 @@ char **argv;
                syslog(LOG_ERR, "get interface configuration: %m");
                exit(1);
        }
                syslog(LOG_ERR, "get interface configuration: %m");
                exit(1);
        }
-       n = ifc.ifc_len/sizeof(struct ifreq);
        ntp = NULL;
        ntp = NULL;
-       for (ifr = ifc.ifc_req; n > 0; n--, ifr++) {
+#define max(a, b) (a > b ? a : b)
+#define size(p)        max((p).sa_len, sizeof(p))
+       cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
+       for (cp = buf; cp < cplim;
+                       cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
+               ifr = (struct ifreq *)cp;
                if (ifr->ifr_addr.sa_family != AF_INET)
                        continue;
                ifreq = *ifr;
                if (ifr->ifr_addr.sa_family != AF_INET)
                        continue;
                ifreq = *ifr;
@@ -320,22 +336,6 @@ char **argv;
 
        /* election timer delay in secs. */
        delay2 = casual((long)MINTOUT, (long)MAXTOUT);
 
        /* election timer delay in secs. */
        delay2 = casual((long)MINTOUT, (long)MAXTOUT);
-#ifndef DEBUG
-       if (fork())
-               exit(0);
-       { int s;
-         for (s = getdtablesize(); s >= 0; --s)
-               (void) close(s);
-         (void) open("/dev/null", 0);
-         (void) dup2(0, 1);
-         (void) dup2(0, 2);
-         s = open("/dev/tty", 2);
-         if (s >= 0) {
-               (void) close(s);
-         }
-       }
-#endif
-
 
        if (Mflag) {
                /*
 
        if (Mflag) {
                /*
@@ -414,7 +414,6 @@ char **argv;
                        checkignorednets();
                }
                makeslave(firstslavenet());
                        checkignorednets();
                }
                makeslave(firstslavenet());
-               makeslave(firstslavenet());
                for (ntp = nettab; ntp != NULL; ntp = ntp->next)
                        if (ntp->status == MASTER)
                                ntp->status = IGNORE;
                for (ntp = nettab; ntp != NULL; ntp = ntp->next)
                        if (ntp->status == MASTER)
                                ntp->status = IGNORE;