candidate.c revision 8857
1148871Scperciva/*-
2148871Scperciva * Copyright (c) 1985, 1993
3148871Scperciva *	The Regents of the University of California.  All rights reserved.
4148871Scperciva *
5148871Scperciva * Redistribution and use in source and binary forms, with or without
6148871Scperciva * modification, are permitted provided that the following conditions
7148871Scperciva * are met:
8148871Scperciva * 1. Redistributions of source code must retain the above copyright
9148871Scperciva *    notice, this list of conditions and the following disclaimer.
10148871Scperciva * 2. Redistributions in binary form must reproduce the above copyright
11148871Scperciva *    notice, this list of conditions and the following disclaimer in the
12148871Scperciva *    documentation and/or other materials provided with the distribution.
13148871Scperciva * 3. All advertising materials mentioning features or use of this software
14148871Scperciva *    must display the following acknowledgement:
15148871Scperciva *	This product includes software developed by the University of
16148871Scperciva *	California, Berkeley and its contributors.
17148871Scperciva * 4. Neither the name of the University nor the names of its contributors
18148871Scperciva *    may be used to endorse or promote products derived from this software
19148871Scperciva *    without specific prior written permission.
20148871Scperciva *
21148871Scperciva * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22148871Scperciva * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23148871Scperciva * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24148871Scperciva * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25148871Scperciva * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26148871Scperciva * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27148871Scperciva * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28148871Scperciva * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29148871Scperciva * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30148871Scperciva * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31148871Scperciva * SUCH DAMAGE.
32148871Scperciva */
33148871Scperciva
34148871Scperciva#ifndef lint
35148871Scpercivastatic char sccsid[] = "@(#)candidate.c	8.1 (Berkeley) 6/6/93";
36148871Scperciva#endif /* not lint */
37149027Scperciva
38148871Scperciva#ifdef sgi
39148871Scperciva#ident "$Revision: 1.1.1.1 $"
40148871Scperciva#endif
41148871Scperciva
42148871Scperciva#include "globals.h"
43148871Scperciva
44148871Scperciva/*
45148871Scperciva * `election' candidates a host as master: it is called by a slave
46158523Scperciva * which runs with the -M option set when its election timeout expires.
47148871Scperciva * Note the conservative approach: if a new timed comes up, or another
48148871Scperciva * candidate sends an election request, the candidature is withdrawn.
49148871Scperciva */
50148871Scpercivaint
51148871Scpercivaelection(net)
52148871Scperciva	struct netinfo *net;
53148871Scperciva{
54148871Scperciva	struct tsp *resp, msg;
55148871Scperciva	struct timeval then, wait;
56148871Scperciva	struct tsp *answer;
57148871Scperciva	struct hosttbl *htp;
58148871Scperciva	char loop_lim = 0;
59148871Scperciva
60148871Scperciva/* This code can get totally confused if it gets slightly behind.  For
61148871Scperciva *	example, if readmsg() has some QUIT messages waiting from the last
62148871Scperciva *	round, we would send an ELECTION message, get the stale QUIT,
63148871Scperciva *	and give up.  This results in network storms when several machines
64148871Scperciva *	do it at once.
65148871Scperciva */
66148871Scperciva	wait.tv_sec = 0;
67148871Scperciva	wait.tv_usec = 0;
68148871Scperciva	while (0 != readmsg(TSP_REFUSE, ANYADDR, &wait, net)) {
69148871Scperciva		if (trace)
70148871Scperciva			fprintf(fd, "election: discarded stale REFUSE\n");
71148871Scperciva	}
72148871Scperciva	while (0 != readmsg(TSP_QUIT, ANYADDR, &wait, net)) {
73148871Scperciva		if (trace)
74148871Scperciva			fprintf(fd, "election: discarded stale QUIT\n");
75148871Scperciva	}
76149027Scperciva
77148871Scpercivaagain:
78148871Scperciva	syslog(LOG_INFO, "This machine is a candidate time master");
79148871Scperciva	if (trace)
80148879Scperciva		fprintf(fd, "This machine is a candidate time master\n");
81148871Scperciva	msg.tsp_type = TSP_ELECTION;
82148871Scperciva	msg.tsp_vers = TSPVERSION;
83148871Scperciva	(void)strcpy(msg.tsp_name, hostname);
84148871Scperciva	bytenetorder(&msg);
85149824Scperciva	if (sendto(sock, (char *)&msg, sizeof(struct tsp), 0,
86158523Scperciva		   (struct sockaddr*)&net->dest_addr,
87148871Scperciva		   sizeof(struct sockaddr)) < 0) {
88148871Scperciva		trace_sendto_err(net->dest_addr.sin_addr);
89148871Scperciva		return(SLAVE);
90148871Scperciva	}
91148871Scperciva
92148871Scperciva	(void)gettimeofday(&then, 0);
93148871Scperciva	then.tv_sec += 3;
94148871Scperciva	for (;;) {
95148871Scperciva		(void)gettimeofday(&wait, 0);
96148871Scperciva		timevalsub(&wait,&then,&wait);
97148871Scperciva		resp = readmsg(TSP_ANY, ANYADDR, &wait, net);
98148871Scperciva		if (!resp)
99148871Scperciva			return(MASTER);
100148871Scperciva
101148871Scperciva		switch (resp->tsp_type) {
102148871Scperciva
103148879Scperciva		case TSP_ACCEPT:
104148871Scperciva			(void)addmach(resp->tsp_name, &from,fromnet);
105148871Scperciva			break;
106148871Scperciva
107148871Scperciva		case TSP_MASTERUP:
108148871Scperciva		case TSP_MASTERREQ:
109148871Scperciva			/*
110148871Scperciva			 * If another timedaemon is coming up at the same
111148871Scperciva			 * time, give up, and let it be the master.
112148871Scperciva			 */
113148871Scperciva			if (++loop_lim < 5
114148871Scperciva			    && !good_host_name(resp->tsp_name)) {
115148871Scperciva				(void)addmach(resp->tsp_name, &from,fromnet);
116148871Scperciva				suppress(&from, resp->tsp_name, net);
117148871Scperciva				goto again;
118148871Scperciva			}
119148871Scperciva			rmnetmachs(net);
120148871Scperciva			return(SLAVE);
121148871Scperciva
122158523Scperciva		case TSP_QUIT:
123158523Scperciva		case TSP_REFUSE:
124158523Scperciva			/*
125158523Scperciva			 * Collision: change value of election timer
126158523Scperciva			 * using exponential backoff.
127148871Scperciva			 *
128148871Scperciva			 *  Fooey.
129148871Scperciva			 * An exponential backoff on a delay starting at
130148871Scperciva			 * 6 to 15 minutes for a process that takes
131148871Scperciva			 * milliseconds is silly.  It is particularly
132148871Scperciva			 * strange that the original code would increase
133148871Scperciva			 * the backoff without bound.
134148871Scperciva			 */
135148871Scperciva			rmnetmachs(net);
136148871Scperciva			return(SLAVE);
137148871Scperciva
138148871Scperciva		case TSP_ELECTION:
139148871Scperciva			/* no master for another round */
140148871Scperciva			htp = addmach(resp->tsp_name,&from,fromnet);
141148871Scperciva			msg.tsp_type = TSP_REFUSE;
142148871Scperciva			(void)strcpy(msg.tsp_name, hostname);
143148871Scperciva			answer = acksend(&msg, &htp->addr, htp->name,
144149027Scperciva					 TSP_ACK, 0, htp->noanswer);
145148871Scperciva			if (!answer) {
146148871Scperciva				syslog(LOG_ERR, "error in election from %s",
147148871Scperciva				       htp->name);
148149027Scperciva			}
149149027Scperciva			break;
150149027Scperciva
151148871Scperciva		case TSP_SLAVEUP:
152148871Scperciva			(void)addmach(resp->tsp_name, &from,fromnet);
153148871Scperciva			break;
154148871Scperciva
155148871Scperciva		case TSP_SETDATE:
156148871Scperciva		case TSP_SETDATEREQ:
157149027Scperciva			break;
158148871Scperciva
159148871Scperciva		default:
160148871Scperciva			if (trace) {
161148871Scperciva				fprintf(fd, "candidate: ");
162148871Scperciva				print(resp, &from);
163148871Scperciva			}
164148871Scperciva			break;
165148871Scperciva		}
166148871Scperciva	}
167148871Scperciva}
168148871Scperciva