BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / timed / timed / slave.c
index e42b881..1ed85b4 100644 (file)
@@ -1,20 +1,34 @@
 /*
 /*
- * 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) 1985 Regents of the University of California.
+ * All rights reserved.
+ *
+ * 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
-static char sccsid[] = "@(#)slave.c    2.11 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)slave.c    2.21 (Berkeley) 6/1/90";
+#endif /* not lint */
 
 #include "globals.h"
 #include <protocols/timed.h>
 #include <setjmp.h>
 
 #include "globals.h"
 #include <protocols/timed.h>
 #include <setjmp.h>
+#include "pathnames.h"
 
 extern jmp_buf jmpenv;
 
 
 extern jmp_buf jmpenv;
 
-extern short sequence;
+extern u_short sequence;
 
 slave()
 {
 
 slave()
 {
@@ -25,8 +39,7 @@ slave()
        char candidate[MAXHOSTNAMELEN];
        struct tsp *msg, to, *readmsg();
        struct sockaddr_in saveaddr, msaveaddr;
        char candidate[MAXHOSTNAMELEN];
        struct tsp *msg, to, *readmsg();
        struct sockaddr_in saveaddr, msaveaddr;
-       struct timeval wait;
-       struct timeval time, mytime, otime;
+       struct timeval time, wait;
        struct tsp *answer, *acksend();
        int timeout();
        char *date();
        struct tsp *answer, *acksend();
        int timeout();
        char *date();
@@ -40,7 +53,6 @@ slave()
        extern int Mflag;
        extern int justquit;
 #ifdef MEASURE
        extern int Mflag;
        extern int justquit;
 #ifdef MEASURE
-       int tempstat;
        extern FILE *fp;
 #endif
        if (slavenet) {
        extern FILE *fp;
 #endif
        if (slavenet) {
@@ -57,10 +69,12 @@ slave()
 
        if (status & MASTER) {
 #ifdef MEASURE
 
        if (status & MASTER) {
 #ifdef MEASURE
-               fp = fopen("/usr/adm/timed.masterlog", "w");
-               setlinebuf(fp);
+               if (fp == NULL) {
+                       fp = fopen(_PATH_MASTERLOG, "w");
+                       setlinebuf(fp);
+               }
 #endif
 #endif
-               syslog(LOG_NOTICE, "THIS MACHINE IS A SUBMASTER");
+               syslog(LOG_INFO, "THIS MACHINE IS A SUBMASTER");
                if (trace) {
                        fprintf(fd, "THIS MACHINE IS A SUBMASTER\n");
                }
                if (trace) {
                        fprintf(fd, "THIS MACHINE IS A SUBMASTER\n");
                }
@@ -69,7 +83,7 @@ slave()
                                masterup(ntp);
 
        } else {
                                masterup(ntp);
 
        } else {
-               syslog(LOG_NOTICE, "THIS MACHINE IS A SLAVE");
+               syslog(LOG_INFO, "THIS MACHINE IS A SLAVE");
                if (trace) {
                        fprintf(fd, "THIS MACHINE IS A SLAVE\n");
                }
                if (trace) {
                        fprintf(fd, "THIS MACHINE IS A SLAVE\n");
                }
@@ -111,16 +125,14 @@ loop:
                                                ntp->status = IGNORE;
                                }
                        }
                                                ntp->status = IGNORE;
                                }
                        }
-#ifdef MEASURE
-                       tempstat = status;
-#endif
                        setstatus();
 #ifdef MEASURE
                        /*
                         * Check to see if we just became master
                        setstatus();
 #ifdef MEASURE
                        /*
                         * Check to see if we just became master
+                        * (file not open)
                         */
                         */
-                       if ((status & MASTER) && !(tempstat & MASTER)) {
-                               fp = fopen("/usr/adm/timed.masterlog", "w");
+                       if (fp == NULL) {
+                               fp = fopen(_PATH_MASTERLOG, "w");
                                setlinebuf(fp);
                        }
 #endif
                                setlinebuf(fp);
                        }
 #endif
@@ -130,7 +142,7 @@ loop:
                    if (ntp->status == MASTER) {
                        to.tsp_type = TSP_LOOP;
                        to.tsp_vers = TSPVERSION;
                    if (ntp->status == MASTER) {
                        to.tsp_type = TSP_LOOP;
                        to.tsp_vers = TSPVERSION;
-                       to.tsp_seq = sequence;
+                       to.tsp_seq = sequence++;
                        to.tsp_hopcnt = 10;
                        (void)strcpy(to.tsp_name, hostname);
                        bytenetorder(&to);
                        to.tsp_hopcnt = 10;
                        (void)strcpy(to.tsp_name, hostname);
                        bytenetorder(&to);
@@ -138,8 +150,6 @@ loop:
                            &ntp->dest_addr, sizeof(struct sockaddr_in)) < 0) {
                                syslog(LOG_ERR, "sendto: %m");
                                exit(1);
                            &ntp->dest_addr, sizeof(struct sockaddr_in)) < 0) {
                                syslog(LOG_ERR, "sendto: %m");
                                exit(1);
-                       if (++sequence > MAXSEQ)
-                               sequence = 1;
                        }
                    }
                }
                        }
                    }
                }
@@ -205,12 +215,12 @@ loop:
                        seq = msg->tsp_seq;
 
                        (void)strcpy(olddate, date());
                        seq = msg->tsp_seq;
 
                        (void)strcpy(olddate, date());
-                       (void)gettimeofday(&otime, (struct timezone *)0);
+                       logwtmp("|", "date", "");
                        (void)settimeofday(&msg->tsp_time,
                                (struct timezone *)0);
                        (void)settimeofday(&msg->tsp_time,
                                (struct timezone *)0);
+                       logwtmp("{", "date", "");
                        syslog(LOG_NOTICE, "date changed by %s from: %s",
                                msg->tsp_name, olddate);
                        syslog(LOG_NOTICE, "date changed by %s from: %s",
                                msg->tsp_name, olddate);
-                       logwtmp(otime, msg->tsp_time);
                        if ((status & SUBMASTER) == SUBMASTER)
                                spreadtime();
                        (void)gettimeofday(&time, (struct timezone *)0);
                        if ((status & SUBMASTER) == SUBMASTER)
                                spreadtime();
                        (void)gettimeofday(&time, (struct timezone *)0);
@@ -260,7 +270,6 @@ loop:
                        break;
                case TSP_SETDATE:
                        saveaddr = from;
                        break;
                case TSP_SETDATE:
                        saveaddr = from;
-                       msg->tsp_time.tv_usec = 0;
                        msg->tsp_type = TSP_SETDATEREQ;
                        msg->tsp_vers = TSPVERSION;
                        (void)strcpy(msg->tsp_name, hostname);
                        msg->tsp_type = TSP_SETDATEREQ;
                        msg->tsp_vers = TSPVERSION;
                        (void)strcpy(msg->tsp_name, hostname);
@@ -295,11 +304,11 @@ loop:
                        }
                        ind = findhost(msg->tsp_name);
                        if (ind < 0) {
                        }
                        ind = findhost(msg->tsp_name);
                        if (ind < 0) {
-                           syslog(LOG_ERR,
+                           syslog(LOG_WARNING,
                                "DATEREQ from uncontrolled machine");
                            break;
                        }
                                "DATEREQ from uncontrolled machine");
                            break;
                        }
-                       syslog(LOG_NOTICE,
+                       syslog(LOG_DEBUG,
                            "forwarding date change request for %s",
                            msg->tsp_name);
                        (void)strcpy(msg->tsp_name, hostname);
                            "forwarding date change request for %s",
                            msg->tsp_name);
                        (void)strcpy(msg->tsp_name, hostname);
@@ -361,18 +370,18 @@ loop:
                                server = from;
                                answer = acksend(msg, &server, candidate, TSP_ACK,
                                    (struct netinfo *)NULL);
                                server = from;
                                answer = acksend(msg, &server, candidate, TSP_ACK,
                                    (struct netinfo *)NULL);
-                               if (answer == NULL) {
-                                       syslog(LOG_ERR, "problem in election\n");
-                               }
-                       }
-                       else {  /* fromnet->status == MASTER */
+                               if (answer == NULL)
+                                       syslog(LOG_WARNING,
+                                          "no answer from master candidate\n");
+                       } else {        /* fromnet->status == MASTER */
                                to.tsp_type = TSP_QUIT;
                                (void)strcpy(to.tsp_name, hostname);
                                server = from;
                                answer = acksend(&to, &server, msg->tsp_name,
                                    TSP_ACK, (struct netinfo *)NULL);
                                if (answer == NULL) {
                                to.tsp_type = TSP_QUIT;
                                (void)strcpy(to.tsp_name, hostname);
                                server = from;
                                answer = acksend(&to, &server, msg->tsp_name,
                                    TSP_ACK, (struct netinfo *)NULL);
                                if (answer == NULL) {
-                                       syslog(LOG_ERR, "election error");
+                                       syslog(LOG_WARNING,
+                                           "election error: no reply to QUIT");
                                } else {
                                        (void) addmach(msg->tsp_name, &from);
                                }
                                } else {
                                        (void) addmach(msg->tsp_name, &from);
                                }
@@ -401,8 +410,9 @@ loop:
                                 msg = acksend(&to, &server, answer->tsp_name,
                                     TSP_ACK, (struct netinfo *)NULL);
                                 if (msg == NULL) {
                                 msg = acksend(&to, &server, answer->tsp_name,
                                     TSP_ACK, (struct netinfo *)NULL);
                                 if (msg == NULL) {
-                                        syslog(LOG_ERR, "error on sending QUIT");
-                                } else {
+                                        syslog(LOG_WARNING,
+                                           "conflict error: no reply to QUIT");
+                               } else {
                                         (void) addmach(answer->tsp_name, &from);
                                }
                         }
                                         (void) addmach(answer->tsp_name, &from);
                                }
                         }
@@ -437,7 +447,10 @@ loop:
                case TSP_QUIT:
                        /* become slave */
 #ifdef MEASURE
                case TSP_QUIT:
                        /* become slave */
 #ifdef MEASURE
-                       (void)fclose(fp);
+                       if (fp != NULL) {
+                               (void)fclose(fp);
+                               fp = NULL;
+                       }
 #endif
                        longjmp(jmpenv, 2);
                        break;
 #endif
                        longjmp(jmpenv, 2);
                        break;
@@ -500,8 +513,6 @@ loop:
                             * we are master on.  There must be two masters
                             * in this case.
                             */
                             * we are master on.  There must be two masters
                             * in this case.
                             */
-                           if (trace)
-                               fprintf(fd, "loop kill %x %x\n", fromnet->my_addr.s_addr, from.sin_addr.s_addr);
                            if (fromnet->my_addr.s_addr == from.sin_addr.s_addr)
                                break;
                            for (;;) {
                            if (fromnet->my_addr.s_addr == from.sin_addr.s_addr)
                                break;
                            for (;;) {