11553Srgrimes/*- 21553Srgrimes * Copyright (c) 1985, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 4. Neither the name of the University nor the names of its contributors 141553Srgrimes * may be used to endorse or promote products derived from this software 151553Srgrimes * without specific prior written permission. 161553Srgrimes * 171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271553Srgrimes * SUCH DAMAGE. 281553Srgrimes */ 291553Srgrimes 301553Srgrimes#ifndef lint 3130642Scharnier#if 0 321553Srgrimesstatic char sccsid[] = "@(#)candidate.c 8.1 (Berkeley) 6/6/93"; 3330642Scharnier#endif 3430642Scharnierstatic const char rcsid[] = 3550479Speter "$FreeBSD$"; 361553Srgrimes#endif /* not lint */ 371553Srgrimes 381553Srgrimes#include "globals.h" 391553Srgrimes 401553Srgrimes/* 411553Srgrimes * `election' candidates a host as master: it is called by a slave 421553Srgrimes * which runs with the -M option set when its election timeout expires. 431553Srgrimes * Note the conservative approach: if a new timed comes up, or another 441553Srgrimes * candidate sends an election request, the candidature is withdrawn. 451553Srgrimes */ 461553Srgrimesint 47246209Scharnierelection(struct netinfo *net) 481553Srgrimes{ 491553Srgrimes struct tsp *resp, msg; 501553Srgrimes struct timeval then, wait; 511553Srgrimes struct tsp *answer; 521553Srgrimes struct hosttbl *htp; 531553Srgrimes char loop_lim = 0; 541553Srgrimes 551553Srgrimes/* This code can get totally confused if it gets slightly behind. For 561553Srgrimes * example, if readmsg() has some QUIT messages waiting from the last 571553Srgrimes * round, we would send an ELECTION message, get the stale QUIT, 581553Srgrimes * and give up. This results in network storms when several machines 591553Srgrimes * do it at once. 601553Srgrimes */ 611553Srgrimes wait.tv_sec = 0; 621553Srgrimes wait.tv_usec = 0; 631553Srgrimes while (0 != readmsg(TSP_REFUSE, ANYADDR, &wait, net)) { 641553Srgrimes if (trace) 651553Srgrimes fprintf(fd, "election: discarded stale REFUSE\n"); 661553Srgrimes } 671553Srgrimes while (0 != readmsg(TSP_QUIT, ANYADDR, &wait, net)) { 681553Srgrimes if (trace) 691553Srgrimes fprintf(fd, "election: discarded stale QUIT\n"); 701553Srgrimes } 711553Srgrimes 721553Srgrimesagain: 731553Srgrimes syslog(LOG_INFO, "This machine is a candidate time master"); 741553Srgrimes if (trace) 751553Srgrimes fprintf(fd, "This machine is a candidate time master\n"); 761553Srgrimes msg.tsp_type = TSP_ELECTION; 771553Srgrimes msg.tsp_vers = TSPVERSION; 7830830Scharnier (void)strcpy(msg.tsp_name, hostname); 791553Srgrimes bytenetorder(&msg); 801553Srgrimes if (sendto(sock, (char *)&msg, sizeof(struct tsp), 0, 811553Srgrimes (struct sockaddr*)&net->dest_addr, 821553Srgrimes sizeof(struct sockaddr)) < 0) { 831553Srgrimes trace_sendto_err(net->dest_addr.sin_addr); 841553Srgrimes return(SLAVE); 851553Srgrimes } 861553Srgrimes 871553Srgrimes (void)gettimeofday(&then, 0); 881553Srgrimes then.tv_sec += 3; 891553Srgrimes for (;;) { 901553Srgrimes (void)gettimeofday(&wait, 0); 911553Srgrimes timevalsub(&wait,&then,&wait); 921553Srgrimes resp = readmsg(TSP_ANY, ANYADDR, &wait, net); 931553Srgrimes if (!resp) 941553Srgrimes return(MASTER); 951553Srgrimes 961553Srgrimes switch (resp->tsp_type) { 971553Srgrimes 981553Srgrimes case TSP_ACCEPT: 991553Srgrimes (void)addmach(resp->tsp_name, &from,fromnet); 1001553Srgrimes break; 1011553Srgrimes 1021553Srgrimes case TSP_MASTERUP: 1031553Srgrimes case TSP_MASTERREQ: 1041553Srgrimes /* 1051553Srgrimes * If another timedaemon is coming up at the same 1061553Srgrimes * time, give up, and let it be the master. 1071553Srgrimes */ 1081553Srgrimes if (++loop_lim < 5 1091553Srgrimes && !good_host_name(resp->tsp_name)) { 1101553Srgrimes (void)addmach(resp->tsp_name, &from,fromnet); 1111553Srgrimes suppress(&from, resp->tsp_name, net); 1121553Srgrimes goto again; 1131553Srgrimes } 1141553Srgrimes rmnetmachs(net); 1151553Srgrimes return(SLAVE); 1161553Srgrimes 1171553Srgrimes case TSP_QUIT: 1181553Srgrimes case TSP_REFUSE: 1191553Srgrimes /* 1201553Srgrimes * Collision: change value of election timer 1218857Srgrimes * using exponential backoff. 1221553Srgrimes * 1231553Srgrimes * Fooey. 1241553Srgrimes * An exponential backoff on a delay starting at 1251553Srgrimes * 6 to 15 minutes for a process that takes 1261553Srgrimes * milliseconds is silly. It is particularly 1271553Srgrimes * strange that the original code would increase 1281553Srgrimes * the backoff without bound. 1291553Srgrimes */ 1301553Srgrimes rmnetmachs(net); 1311553Srgrimes return(SLAVE); 1321553Srgrimes 1331553Srgrimes case TSP_ELECTION: 1341553Srgrimes /* no master for another round */ 1351553Srgrimes htp = addmach(resp->tsp_name,&from,fromnet); 1361553Srgrimes msg.tsp_type = TSP_REFUSE; 13730830Scharnier (void)strcpy(msg.tsp_name, hostname); 1381553Srgrimes answer = acksend(&msg, &htp->addr, htp->name, 1391553Srgrimes TSP_ACK, 0, htp->noanswer); 1401553Srgrimes if (!answer) { 1411553Srgrimes syslog(LOG_ERR, "error in election from %s", 1421553Srgrimes htp->name); 1431553Srgrimes } 1441553Srgrimes break; 1451553Srgrimes 1461553Srgrimes case TSP_SLAVEUP: 1471553Srgrimes (void)addmach(resp->tsp_name, &from,fromnet); 1481553Srgrimes break; 1491553Srgrimes 1501553Srgrimes case TSP_SETDATE: 1511553Srgrimes case TSP_SETDATEREQ: 1521553Srgrimes break; 1531553Srgrimes 1541553Srgrimes default: 1551553Srgrimes if (trace) { 1561553Srgrimes fprintf(fd, "candidate: "); 1571553Srgrimes print(resp, &from); 1581553Srgrimes } 1591553Srgrimes break; 1601553Srgrimes } 1611553Srgrimes } 1621553Srgrimes} 163