1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1985, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)candidate.c	8.1 (Berkeley) 6/6/93";
35 #endif
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39 
40 #include "globals.h"
41 
42 /*
43  * `election' candidates a host as master: it is called by a slave
44  * which runs with the -M option set when its election timeout expires.
45  * Note the conservative approach: if a new timed comes up, or another
46  * candidate sends an election request, the candidature is withdrawn.
47  */
48 int
election(struct netinfo * net)49 election(struct netinfo *net)
50 {
51 	struct tsp *resp, msg;
52 	struct timeval then, wait;
53 	struct tsp *answer;
54 	struct hosttbl *htp;
55 	char loop_lim = 0;
56 
57 /* This code can get totally confused if it gets slightly behind.  For
58  *	example, if readmsg() has some QUIT messages waiting from the last
59  *	round, we would send an ELECTION message, get the stale QUIT,
60  *	and give up.  This results in network storms when several machines
61  *	do it at once.
62  */
63 	wait.tv_sec = 0;
64 	wait.tv_usec = 0;
65 	while (0 != readmsg(TSP_REFUSE, ANYADDR, &wait, net)) {
66 		if (trace)
67 			fprintf(fd, "election: discarded stale REFUSE\n");
68 	}
69 	while (0 != readmsg(TSP_QUIT, ANYADDR, &wait, net)) {
70 		if (trace)
71 			fprintf(fd, "election: discarded stale QUIT\n");
72 	}
73 
74 again:
75 	syslog(LOG_INFO, "This machine is a candidate time master");
76 	if (trace)
77 		fprintf(fd, "This machine is a candidate time master\n");
78 	msg.tsp_type = TSP_ELECTION;
79 	msg.tsp_vers = TSPVERSION;
80 	(void)strcpy(msg.tsp_name, hostname);
81 	bytenetorder(&msg);
82 	if (sendto(sock, (char *)&msg, sizeof(struct tsp), 0,
83 		   (struct sockaddr*)&net->dest_addr,
84 		   sizeof(struct sockaddr)) < 0) {
85 		trace_sendto_err(net->dest_addr.sin_addr);
86 		return(SLAVE);
87 	}
88 
89 	(void)gettimeofday(&then, 0);
90 	then.tv_sec += 3;
91 	for (;;) {
92 		(void)gettimeofday(&wait, 0);
93 		timevalsub(&wait,&then,&wait);
94 		resp = readmsg(TSP_ANY, ANYADDR, &wait, net);
95 		if (!resp)
96 			return(MASTER);
97 
98 		switch (resp->tsp_type) {
99 
100 		case TSP_ACCEPT:
101 			(void)addmach(resp->tsp_name, &from,fromnet);
102 			break;
103 
104 		case TSP_MASTERUP:
105 		case TSP_MASTERREQ:
106 			/*
107 			 * If another timedaemon is coming up at the same
108 			 * time, give up, and let it be the master.
109 			 */
110 			if (++loop_lim < 5
111 			    && !good_host_name(resp->tsp_name)) {
112 				(void)addmach(resp->tsp_name, &from,fromnet);
113 				suppress(&from, resp->tsp_name, net);
114 				goto again;
115 			}
116 			rmnetmachs(net);
117 			return(SLAVE);
118 
119 		case TSP_QUIT:
120 		case TSP_REFUSE:
121 			/*
122 			 * Collision: change value of election timer
123 			 * using exponential backoff.
124 			 *
125 			 *  Fooey.
126 			 * An exponential backoff on a delay starting at
127 			 * 6 to 15 minutes for a process that takes
128 			 * milliseconds is silly.  It is particularly
129 			 * strange that the original code would increase
130 			 * the backoff without bound.
131 			 */
132 			rmnetmachs(net);
133 			return(SLAVE);
134 
135 		case TSP_ELECTION:
136 			/* no master for another round */
137 			htp = addmach(resp->tsp_name,&from,fromnet);
138 			msg.tsp_type = TSP_REFUSE;
139 			(void)strcpy(msg.tsp_name, hostname);
140 			answer = acksend(&msg, &htp->addr, htp->name,
141 					 TSP_ACK, 0, htp->noanswer);
142 			if (!answer) {
143 				syslog(LOG_ERR, "error in election from %s",
144 				       htp->name);
145 			}
146 			break;
147 
148 		case TSP_SLAVEUP:
149 			(void)addmach(resp->tsp_name, &from,fromnet);
150 			break;
151 
152 		case TSP_SETDATE:
153 		case TSP_SETDATEREQ:
154 			break;
155 
156 		default:
157 			if (trace) {
158 				fprintf(fd, "candidate: ");
159 				print(resp, &from);
160 			}
161 			break;
162 		}
163 	}
164 }
165