bf32432cbad961d7e384611460909d04e9c58ebf
[openafs.git] / src / ubik / beacon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <afs/opr.h>
16 #ifdef AFS_PTHREAD_ENV
17 # include <opr/lock.h>
18 #else
19 # include <opr/lockstub.h>
20 #endif
21
22 #include <lock.h>
23 #include <rx/rx.h>
24 #include <rx/rxkad.h>
25 #include <rx/rx_multi.h>
26 #include <afs/cellconfig.h>
27 #ifndef AFS_NT40_ENV
28 #include <afs/afsutil.h>
29 #endif
30
31 #define UBIK_INTERNALS
32 #include "ubik.h"
33 #include "ubik_int.h"
34
35 /* These global variables were used to set the function to use to initialise
36  * the client security layer. They are retained for backwards compatiblity with
37  * legacy callers - the ubik_SetClientSecurityProcs() interface should be used
38  * instead
39  */
40 int (*ubik_CRXSecurityProc) (void *rock, struct rx_securityClass **,
41                              afs_int32 *);
42 void *ubik_CRXSecurityRock;
43
44 /*! \name statics used to determine if we're the sync site */
45 static int nServers;            /*!< total number of servers */
46 static char amIMagic = 0;       /*!< is this host the magic host */
47 char amIClone = 0;              /*!< is this a clone which doesn't vote */
48 static char ubik_singleServer = 0;
49 /*\}*/
50 static int (*secLayerProc) (void *rock, struct rx_securityClass **,
51                             afs_int32 *) = NULL;
52 static int (*tokenCheckProc) (void *rock) = NULL;
53 static void * securityRock = NULL;
54
55 afs_int32 ubikSecIndex;
56 struct rx_securityClass *ubikSecClass;
57
58 /* Values protected by the address lock */
59 struct addr_data addr_globals;
60
61 /* Values protected by the beacon lock */
62 struct beacon_data beacon_globals;
63
64 static int ubeacon_InitServerListCommon(afs_uint32 ame,
65                                         struct afsconf_cell *info,
66                                         char clones[],
67                                         afs_uint32 aservers[]);
68 static int verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
69                                   afs_uint32 aservers[]);
70
71 /*! \file
72  * Module responsible for both deciding if we're currently the sync site,
73  * and keeping collecting votes so as to stay sync site.
74  *
75  * The basic module contacts all of the servers it can, trying to get them to vote
76  * for this server for sync site.  The vote request message (called a beacon message)
77  * also specifies until which time this site claims to be the sync site, if at all, thus enabling
78  * receiving sites to know how long the sync site guarantee is made for.
79  *
80  * Each  of these beacon messages is thus both a declaration of how long this site will
81  * remain sync site, and an attempt to extend that time by collecting votes for a later
82  * sync site extension.
83  *
84  * The voting module is responsible for choosing a reasonable time until which it promises
85  * not to vote for someone else.  This parameter (BIG seconds) is not actually passed in
86  * the interface (perhaps it should be?) but is instead a compile time constant that both
87  * sides know about.
88
89  * The beacon and vote modules work intimately together; the vote module decides how long
90  * it should promise the beacon module its vote, and the beacon module takes all of these
91  * votes and decides for how long it is the synchronization site.
92  */
93
94 /*! \brief procedure called from debug rpc call to get this module's state for debugging */
95 void
96 ubeacon_Debug(struct ubik_debug *aparm)
97 {
98     /* fill in beacon's state fields in the ubik_debug structure */
99     aparm->syncSiteUntil = beacon_globals.syncSiteUntil;
100     aparm->nServers = nServers;
101 }
102
103 static int
104 amSyncSite(void)
105 {
106     afs_int32 now;
107     afs_int32 rcode;
108
109     /* special case for fast startup */
110     if (nServers == 1 && !amIClone)
111         return 1;               /* one guy is always the sync site */
112
113     UBIK_BEACON_LOCK;
114     if (beacon_globals.ubik_amSyncSite == 0 || amIClone)
115         rcode = 0;              /* if I don't think I'm the sync site, say so */
116     else {
117         now = FT_ApproxTime();
118         if (beacon_globals.syncSiteUntil <= now) {      /* if my votes have expired, say so */
119             if (beacon_globals.ubik_amSyncSite)
120                 ubik_dprint("Ubik: I am no longer the sync site\n");
121             beacon_globals.ubik_amSyncSite = 0;
122             beacon_globals.ubik_syncSiteAdvertised = 0;
123             rcode = 0;
124         } else {
125             rcode = 1;          /* otherwise still have the required votes */
126         }
127     }
128     UBIK_BEACON_UNLOCK;
129     ubik_dprint("beacon: amSyncSite is %d\n", rcode);
130     return rcode;
131 }
132
133 /*!
134  * \brief Procedure that determines whether this site has enough current votes to remain sync site.
135  *
136  * Called from higher-level modules (everything but the vote module).
137  *
138  * If we're the sync site, check that our guarantees, obtained by the ubeacon_Interact()
139  * light-weight process, haven't expired.  We're sync site as long as a majority of the
140  * servers in existence have promised us unexpired guarantees.  The variable #ubik_syncSiteUntil
141  * contains the time at which the latest of the majority of the sync site guarantees expires
142  * (if the variable #ubik_amSyncSite is true)
143  * This module also calls up to the recovery module if it thinks that the recovery module
144  * may have to pick up a new database (which offucr sif [sic] we lose the sync site votes).
145  *
146  * \return 1 if local site is the sync site
147  * \return 0 if sync site is elsewhere
148  */
149 int
150 ubeacon_AmSyncSite(void)
151 {
152     afs_int32 rcode;
153
154     rcode = amSyncSite();
155
156     if (!rcode)
157         urecovery_ResetState();
158
159     return rcode;
160 }
161
162 /*!
163  * \brief Determine whether at least quorum are aware we have a sync-site.
164  *
165  * Called from higher-level modules.
166  *
167  * There is a gap between the time when a new sync-site is elected and the time
168  * when the remotes are aware of that. Therefore, any write transaction between
169  * this gap will fail. This will force a new re-election which might be time
170  * consuming. This procedure determines whether the remotes (quorum) are aware
171  * we have a sync-site.
172  *
173  * \return 1 if remotes are aware we have a sync-site
174  * \return 0 if remotes are not aware we have a sync-site
175  */
176 int
177 ubeacon_SyncSiteAdvertised(void)
178 {
179     afs_int32 rcode;
180
181     UBIK_BEACON_LOCK;
182     rcode = beacon_globals.ubik_syncSiteAdvertised;
183     UBIK_BEACON_UNLOCK;
184
185     return rcode;
186 }
187
188 /*!
189  * \see ubeacon_InitServerListCommon()
190  */
191 int
192 ubeacon_InitServerListByInfo(afs_uint32 ame, struct afsconf_cell *info,
193                              char clones[])
194 {
195     afs_int32 code;
196
197     code = ubeacon_InitServerListCommon(ame, info, clones, 0);
198     return code;
199 }
200
201 /*!
202  * \param ame "address of me"
203  * \param aservers list of other servers
204  *
205  * \see ubeacon_InitServerListCommon()
206  */
207 int
208 ubeacon_InitServerList(afs_uint32 ame, afs_uint32 aservers[])
209 {
210     afs_int32 code;
211
212     code =
213         ubeacon_InitServerListCommon(ame, (struct afsconf_cell *)0, 0,
214                                      aservers);
215     return code;
216 }
217
218 /* Must be called with address lock held */
219 void
220 ubeacon_InitSecurityClass(void)
221 {
222     int i;
223     /* get the security index to use, if we can */
224     if (secLayerProc) {
225         i = (*secLayerProc) (securityRock, &addr_globals.ubikSecClass, &addr_globals.ubikSecIndex);
226     } else if (ubik_CRXSecurityProc) {
227         i = (*ubik_CRXSecurityProc) (ubik_CRXSecurityRock, &addr_globals.ubikSecClass,
228                                      &addr_globals.ubikSecIndex);
229     } else
230         i = 1;
231     if (i) {
232         /* don't have sec module yet */
233         addr_globals.ubikSecIndex = 0;
234         addr_globals.ubikSecClass = rxnull_NewClientSecurityObject();
235     }
236 }
237
238 void
239 ubeacon_ReinitServer(struct ubik_server *ts)
240 {
241     if (tokenCheckProc && !(*tokenCheckProc) (securityRock)) {
242         struct rx_connection *disk_rxcid;
243         struct rx_connection *vote_rxcid;
244         struct rx_connection *tmp;
245         UBIK_ADDR_LOCK;
246         ubeacon_InitSecurityClass();
247         disk_rxcid =
248             rx_NewConnection(rx_HostOf(rx_PeerOf(ts->disk_rxcid)),
249                              ubik_callPortal, DISK_SERVICE_ID,
250                              addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
251         if (disk_rxcid) {
252             tmp = ts->disk_rxcid;
253             ts->disk_rxcid = disk_rxcid;
254             rx_PutConnection(tmp);
255         }
256         vote_rxcid =
257             rx_NewConnection(rx_HostOf(rx_PeerOf(ts->vote_rxcid)),
258                              ubik_callPortal, VOTE_SERVICE_ID,
259                              addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
260         if (vote_rxcid) {
261             tmp = ts->vote_rxcid;
262             ts->vote_rxcid = vote_rxcid;
263             rx_PutConnection(tmp);
264         }
265         UBIK_ADDR_UNLOCK;
266     }
267 }
268
269 /*!
270  * \brief setup server list
271  *
272  * \param ame "address of me"
273  * \param aservers list of other servers
274  *
275  * called only at initialization to set up the list of servers to
276  * contact for votes.  Just creates the server structure.
277  *
278  * The "magic" host is the one with the lowest internet address.  It is
279  * magic because its vote counts epsilon more than the others.  This acts
280  * as a tie-breaker when we have an even number of hosts in the system.
281  * For example, if the "magic" host is up in a 2 site system, then it
282  * is sync site.  Without the magic host hack, if anyone crashed in a 2
283  * site system, we'd be out of business.
284  *
285  * \note There are two connections in every server structure, one for
286  * vote calls (which must always go through quickly) and one for database
287  * operations, which are subject to waiting for locks.  If we used only
288  * one, the votes would sometimes get held up behind database operations,
289  * and the sync site guarantees would timeout even though the host would be
290  * up for communication.
291  *
292  * \see ubeacon_InitServerList(), ubeacon_InitServerListByInfo()
293  */
294 int
295 ubeacon_InitServerListCommon(afs_uint32 ame, struct afsconf_cell *info,
296                              char clones[], afs_uint32 aservers[])
297 {
298     struct ubik_server *ts;
299     afs_int32 me = -1;
300     afs_int32 servAddr;
301     afs_int32 i, code;
302     afs_int32 magicHost;
303     struct ubik_server *magicServer;
304
305     /* verify that the addresses passed in are correct */
306     if ((code = verifyInterfaceAddress(&ame, info, aservers)))
307         return code;
308
309     ubeacon_InitSecurityClass();
310
311     magicHost = ntohl(ame);     /* do comparisons in host order */
312     magicServer = (struct ubik_server *)0;
313
314     if (info) {
315         for (i = 0; i < info->numServers; i++) {
316             if (ntohl((afs_uint32) info->hostAddr[i].sin_addr.s_addr) ==
317                 ntohl((afs_uint32) ame)) {
318                 me = i;
319                 if (clones[i]) {
320                     amIClone = 1;
321                     magicHost = 0;
322                 }
323             }
324         }
325         nServers = 0;
326         for (i = 0; i < info->numServers; i++) {
327             if (i == me)
328                 continue;
329             ts = calloc(1, sizeof(struct ubik_server));
330             ts->next = ubik_servers;
331             ubik_servers = ts;
332             ts->addr[0] = info->hostAddr[i].sin_addr.s_addr;
333             if (clones[i]) {
334                 ts->isClone = 1;
335             } else {
336                 if (!magicHost
337                     || ntohl((afs_uint32) ts->addr[0]) <
338                     (afs_uint32) magicHost) {
339                     magicHost = ntohl(ts->addr[0]);
340                     magicServer = ts;
341                 }
342                 ++nServers;
343             }
344             /* for vote reqs */
345             ts->vote_rxcid =
346                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
347                                  ubik_callPortal, VOTE_SERVICE_ID,
348                                  addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
349             /* for disk reqs */
350             ts->disk_rxcid =
351                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
352                                  ubik_callPortal, DISK_SERVICE_ID,
353                                  addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
354             ts->up = 1;
355         }
356     } else {
357         i = 0;
358         while ((servAddr = *aservers++)) {
359             if (i >= MAXSERVERS)
360                 return UNHOSTS; /* too many hosts */
361             ts = calloc(1, sizeof(struct ubik_server));
362             ts->next = ubik_servers;
363             ubik_servers = ts;
364             ts->addr[0] = servAddr;     /* primary address in  net byte order */
365             ts->vote_rxcid = rx_NewConnection(servAddr, ubik_callPortal, VOTE_SERVICE_ID,
366                         addr_globals.ubikSecClass, addr_globals.ubikSecIndex);  /* for vote reqs */
367             ts->disk_rxcid = rx_NewConnection(servAddr, ubik_callPortal, DISK_SERVICE_ID,
368                         addr_globals.ubikSecClass, addr_globals.ubikSecIndex);  /* for disk reqs */
369             ts->isClone = 0;    /* don't know about clones */
370             ts->up = 1;
371             if (ntohl((afs_uint32) servAddr) < (afs_uint32) magicHost) {
372                 magicHost = ntohl(servAddr);
373                 magicServer = ts;
374             }
375             i++;
376         }
377     }
378     if (magicServer)
379         magicServer->magic = 1; /* remember for when counting votes */
380
381     if (!amIClone && !magicServer)
382         amIMagic = 1;
383     if (info) {
384         if (!amIClone)
385             ++nServers;         /* count this server as well as the remotes */
386     } else
387         nServers = i + 1;       /* count this server as well as the remotes */
388
389     ubik_quorum = (nServers >> 1) + 1;  /* compute the majority figure */
390
391 /* Shoud we set some defaults for RX??
392     r_retryInterval = 2;
393     r_nRetries = (RPCTIMEOUT/r_retryInterval);
394 */
395     if (info) {
396         if (!ubik_servers)      /* special case 1 server */
397             ubik_singleServer = 1;
398         if (nServers == 1 && !amIClone) {
399             beacon_globals.ubik_amSyncSite = 1; /* let's start as sync site */
400             beacon_globals.syncSiteUntil = 0x7fffffff;  /* and be it quite a while */
401             beacon_globals.ubik_syncSiteAdvertised = 1;
402         }
403     } else {
404         if (nServers == 1)      /* special case 1 server */
405             ubik_singleServer = 1;
406     }
407
408     if (ubik_singleServer) {
409         if (!beacon_globals.ubik_amSyncSite)
410             ubik_dprint("Ubik: I am the sync site - 1 server\n");
411         beacon_globals.ubik_amSyncSite = 1;
412         beacon_globals.syncSiteUntil = 0x7fffffff;      /* quite a while */
413         beacon_globals.ubik_syncSiteAdvertised = 1;
414     }
415     return 0;
416 }
417
418 /*!
419  * \brief main lwp loop for code that sends out beacons.
420  *
421  * This code only runs while we're sync site or we want to be the sync site.
422  * It runs in its very own light-weight process.
423  */
424 void *
425 ubeacon_Interact(void *dummy)
426 {
427     afs_int32 code;
428     struct timeval tt;
429     struct rx_connection *connections[MAXSERVERS];
430     struct ubik_server *servers[MAXSERVERS];
431     afs_int32 i;
432     struct ubik_server *ts;
433     afs_int32 temp, yesVotes, lastWakeupTime, oldestYesVote, syncsite;
434     struct ubik_tid ttid;
435     struct ubik_version tversion;
436     afs_int32 startTime;
437
438     afs_pthread_setname_self("beacon");
439
440     /* loop forever getting votes */
441     lastWakeupTime = 0;         /* keep track of time we last started a vote collection */
442     while (1) {
443
444         /* don't wakeup more than every POLLTIME seconds */
445         temp = (lastWakeupTime + POLLTIME) - FT_ApproxTime();
446         /* don't sleep if last collection phase took too long (probably timed someone out ) */
447         if (temp > 0) {
448             if (temp > POLLTIME)
449                 temp = POLLTIME;
450             tt.tv_sec = temp;
451             tt.tv_usec = 0;
452 #ifdef AFS_PTHREAD_ENV
453             select(0, 0, 0, 0, &tt);
454 #else
455             IOMGR_Select(0, 0, 0, 0, &tt);
456 #endif
457         }
458
459         lastWakeupTime = FT_ApproxTime();       /* started a new collection phase */
460
461         if (ubik_singleServer)
462             continue;           /* special-case 1 server for speedy startup */
463
464         if (!uvote_ShouldIRun())
465             continue;           /* if voter has heard from a better candidate than us, don't bother running */
466
467         /* otherwise we should run for election, or we're the sync site (and have already won);
468          * send out the beacon packets */
469         /* build list of all up hosts (noticing dead hosts are running again
470          * is a task for the recovery module, not the beacon module), and
471          * prepare to send them an r multi-call containing the beacon message */
472         i = 0;                  /* collect connections */
473         UBIK_BEACON_LOCK;
474         UBIK_ADDR_LOCK;
475         for (ts = ubik_servers; ts; ts = ts->next) {
476             if (ts->up && ts->addr[0] != ubik_host[0]) {
477                 servers[i] = ts;
478                 connections[i++] = ts->vote_rxcid;
479             }
480         }
481         UBIK_ADDR_UNLOCK;
482         UBIK_BEACON_UNLOCK;
483         servers[i] = (struct ubik_server *)0;   /* end of list */
484         /* note that we assume in the vote module that we'll always get at least BIGTIME
485          * seconds of vote from anyone who votes for us, which means we can conservatively
486          * assume we'll be fine until SMALLTIME seconds after we start collecting votes */
487         /* this next is essentially an expansion of rgen's ServBeacon routine */
488
489         UBIK_VERSION_LOCK;
490         ttid.epoch = version_globals.ubik_epochTime;
491         if (ubik_dbase->flags & DBWRITING) {
492             /*
493              * if a write is in progress, we have to send the writeTidCounter
494              * which holds the tid counter of the write transaction , and not
495              * send the tidCounter value which holds the tid counter of the
496              * last transaction.
497              */
498             ttid.counter = ubik_dbase->writeTidCounter;
499         } else
500             ttid.counter = ubik_dbase->tidCounter + 1;
501         tversion.epoch = ubik_dbase->version.epoch;
502         tversion.counter = ubik_dbase->version.counter;
503         UBIK_VERSION_UNLOCK;
504
505         /* now analyze return codes, counting up our votes */
506         yesVotes = 0;           /* count how many to ensure we have quorum */
507         oldestYesVote = 0x7fffffff;     /* time quorum expires */
508         syncsite = amSyncSite();
509         if (!syncsite) {
510             /* Ok to use the DB lock here since we aren't sync site */
511             DBHOLD(ubik_dbase);
512             urecovery_ResetState();
513             DBRELE(ubik_dbase);
514         }
515         startTime = FT_ApproxTime();
516         /*
517          * Don't waste time using mult Rx calls if there are no connections out there
518          */
519         if (i > 0) {
520             char hoststr[16];
521             multi_Rx(connections, i) {
522                 multi_VOTE_Beacon(syncsite, startTime, &tversion,
523                                   &ttid);
524                 temp = FT_ApproxTime(); /* now, more or less */
525                 ts = servers[multi_i];
526                 UBIK_BEACON_LOCK;
527                 ts->lastBeaconSent = temp;
528                 code = multi_error;
529
530                 if (code > 0 && ((code < temp && code < temp - 3600) ||
531                                  (code > temp && code > temp + 3600))) {
532                     /* if we reached here, supposedly the remote host voted
533                      * for us based on a computation from over an hour ago in
534                      * the past, or over an hour in the future. this is
535                      * unlikely; what actually probably happened is that the
536                      * call generated some error and was aborted. this can
537                      * happen due to errors with the rx security class in play
538                      * (rxkad, rxgk, etc). treat the host as if we got a
539                      * timeout, since this is not a valid vote. */
540                     ubik_print("assuming distant vote time %d from %s is an error; marking host down\n",
541                                (int)code, afs_inet_ntoa_r(ts->addr[0], hoststr));
542                     code = -1;
543                 }
544                 if (code > 0 && rx_ConnError(connections[multi_i])) {
545                     ubik_print("assuming vote from %s is invalid due to conn error %d; marking host down\n",
546                                afs_inet_ntoa_r(ts->addr[0], hoststr),
547                                (int)rx_ConnError(connections[multi_i]));
548                     code = -1;
549                 }
550
551                 /* note that the vote time (the return code) represents the time
552                  * the vote was computed, *not* the time the vote expires.  We compute
553                  * the latter down below if we got enough votes to go with */
554                 if (code > 0) {
555                     if ((code & ~0xff) == ERROR_TABLE_BASE_RXK) {
556                         ubik_dprint("token error %d from host %s\n",
557                                     code, afs_inet_ntoa_r(ts->addr[0], hoststr));
558                         ts->up = 0;
559                         ts->beaconSinceDown = 0;
560                         urecovery_LostServer(ts);
561                     } else {
562                         ts->lastVoteTime = code;
563                         if (code < oldestYesVote)
564                             oldestYesVote = code;
565                         ts->lastVote = 1;
566                         if (!ts->isClone)
567                             yesVotes += 2;
568                         if (ts->magic)
569                             yesVotes++; /* the extra epsilon */
570                         ts->up = 1;     /* server is up (not really necessary: recovery does this for real) */
571                         ts->beaconSinceDown = 1;
572                         ubik_dprint("yes vote from host %s\n",
573                                     afs_inet_ntoa_r(ts->addr[0], hoststr));
574                     }
575                 } else if (code == 0) {
576                     ts->lastVoteTime = temp;
577                     ts->lastVote = 0;
578                     ts->beaconSinceDown = 1;
579                     ubik_dprint("no vote from %s\n",
580                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
581                 } else if (code < 0) {
582                     ts->up = 0;
583                     ts->beaconSinceDown = 0;
584                     urecovery_LostServer(ts);
585                     ubik_dprint("time out from %s\n",
586                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
587                 }
588                 UBIK_BEACON_UNLOCK;
589             }
590             multi_End;
591         }
592         /* now call our own voter module to see if we'll vote for ourself.  Note that
593          * the same restrictions apply for our voting for ourself as for our voting
594          * for anyone else. */
595         i = SVOTE_Beacon((struct rx_call *)0, ubeacon_AmSyncSite(), startTime,
596                          &tversion, &ttid);
597         if (i) {
598             yesVotes += 2;
599             if (amIMagic)
600                 yesVotes++;     /* extra epsilon */
601             if (i < oldestYesVote)
602                 oldestYesVote = i;
603         }
604
605         /* now decide if we have enough votes to become sync site.
606          * Note that we can still get enough votes even if we didn't for ourself. */
607         if (yesVotes > nServers) {      /* yesVotes is bumped by 2 or 3 for each site */
608             UBIK_BEACON_LOCK;
609             if (!beacon_globals.ubik_amSyncSite)
610                 ubik_dprint("Ubik: I am the sync site\n");
611             else {
612                 /* at this point, we have the guarantee that at least quorum
613                  * received a beacon packet informing we have a sync-site. */
614                 beacon_globals.ubik_syncSiteAdvertised = 1;
615             }
616             beacon_globals.ubik_amSyncSite = 1;
617             beacon_globals.syncSiteUntil = oldestYesVote + SMALLTIME;
618 #ifndef AFS_PTHREAD_ENV
619                 /* I did not find a corresponding LWP_WaitProcess(&ubik_amSyncSite) --
620                    this may be a spurious signal call -- sjenkins */
621                 LWP_NoYieldSignal(&beacon_globals.ubik_amSyncSite);
622 #endif
623             UBIK_BEACON_UNLOCK;
624         } else {
625             UBIK_BEACON_LOCK;
626             if (beacon_globals.ubik_amSyncSite)
627                 ubik_dprint("Ubik: I am no longer the sync site\n");
628             beacon_globals.ubik_amSyncSite = 0;
629             beacon_globals.ubik_syncSiteAdvertised = 0;
630             UBIK_BEACON_UNLOCK;
631             DBHOLD(ubik_dbase);
632             urecovery_ResetState();     /* tell recovery we're no longer the sync site */
633             DBRELE(ubik_dbase);
634         }
635
636     }                           /* while loop */
637     return NULL;
638 }
639
640 /*!
641  * \brief Verify that a given IP addresses does actually exist on this machine.
642  *
643  * \param ame      the pointer to my IP address specified in the
644  *                 CellServDB file.
645  * \param aservers an array containing IP
646  *                 addresses of remote ubik servers. The array is
647  *                 terminated by a zero address.
648  *
649  * Algorithm     : Verify that my IP addresses \p ame does actually exist
650  *                 on this machine.  If any of my IP addresses are there
651  *                 in the remote server list \p aserver, remove them from
652  *                 this list.  Update global variable \p ubik_host[] with
653  *                 my IP addresses.
654  *
655  * \return 0 on success, non-zero on failure
656  */
657 static int
658 verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
659                        afs_uint32 aservers[]) {
660     afs_uint32 myAddr[UBIK_MAX_INTERFACE_ADDR], *servList, tmpAddr;
661     afs_uint32 myAddr2[UBIK_MAX_INTERFACE_ADDR];
662     char hoststr[16];
663     int tcount, count, found, i, j, totalServers, start, end, usednetfiles =
664         0;
665
666     if (info)
667         totalServers = info->numServers;
668     else {                      /* count the number of servers */
669         for (totalServers = 0, servList = aservers; *servList; servList++)
670             totalServers++;
671     }
672
673     if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
674         /*
675          * Find addresses we are supposed to register as per the netrestrict file
676          * if it exists, else just register all the addresses we find on this
677          * host as returned by rx_getAllAddr (in NBO)
678          */
679         char reason[1024];
680         count = afsconf_ParseNetFiles(myAddr, NULL, NULL,
681                                       UBIK_MAX_INTERFACE_ADDR, reason,
682                                       AFSDIR_SERVER_NETINFO_FILEPATH,
683                                       AFSDIR_SERVER_NETRESTRICT_FILEPATH);
684         if (count < 0) {
685             ubik_print("ubik: Can't register any valid addresses:%s\n",
686                        reason);
687             ubik_print("Aborting..\n");
688             return UBADHOST;
689         }
690         usednetfiles++;
691     } else {
692         /* get all my interface addresses in net byte order */
693         count = rx_getAllAddr(myAddr, UBIK_MAX_INTERFACE_ADDR);
694     }
695
696     if (count <= 0) {           /* no address found */
697         ubik_print("ubik: No network addresses found, aborting..\n");
698         return UBADHOST;
699     }
700
701     /* verify that the My-address passed in by ubik is correct */
702     for (j = 0, found = 0; j < count; j++) {
703         if (*ame == myAddr[j]) {        /* both in net byte order */
704             found = 1;
705             break;
706         }
707     }
708
709     if (!found) {
710         ubik_print("ubik: primary address %s does not exist\n",
711                    afs_inet_ntoa_r(*ame, hoststr));
712         /* if we had the result of rx_getAllAddr already, avoid subverting
713          * the "is gethostbyname(gethostname()) us" check. If we're
714          * using NetInfo/NetRestrict, we assume they have enough clue
715          * to avoid that big hole in their foot from the loaded gun. */
716         if (usednetfiles) {
717             /* take the address we did get, then see if ame was masked */
718             *ame = myAddr[0];
719             tcount = rx_getAllAddr(myAddr2, UBIK_MAX_INTERFACE_ADDR);
720             if (tcount <= 0) {  /* no address found */
721                 ubik_print("ubik: No network addresses found, aborting..\n");
722                 return UBADHOST;
723             }
724
725             /* verify that the My-address passed in by ubik is correct */
726             for (j = 0, found = 0; j < tcount; j++) {
727                 if (*ame == myAddr2[j]) {       /* both in net byte order */
728                     found = 1;
729                     break;
730                 }
731             }
732         }
733         if (!found)
734             return UBADHOST;
735     }
736
737     /* if any of my addresses are there in serverList, then
738      ** use that as my primary addresses : the higher level
739      ** application screwed up in dealing with multihomed concepts
740      */
741     for (j = 0, found = 0; j < count; j++) {
742         for (i = 0; i < totalServers; i++) {
743             if (info)
744                 tmpAddr = (afs_uint32) info->hostAddr[i].sin_addr.s_addr;
745             else
746                 tmpAddr = aservers[i];
747             if (myAddr[j] == tmpAddr) {
748                 *ame = tmpAddr;
749                 if (!info)
750                     aservers[i] = 0;
751                 found = 1;
752             }
753         }
754     }
755     if (found)
756         ubik_print("Using %s as my primary address\n", afs_inet_ntoa_r(*ame, hoststr));
757
758     if (!info) {
759         /* get rid of servers which were purged because all
760          ** those interface addresses are myself
761          */
762         for (start = 0, end = totalServers - 1; (start < end); start++, end--) {
763             /* find the first zero entry from the beginning */
764             for (; (start < end) && (aservers[start]); start++);
765
766             /* find the last non-zero entry from the end */
767             for (; (end >= 0) && (!aservers[end]); end--);
768
769             /* if there is nothing more to purge, exit from loop */
770             if (start >= end)
771                 break;
772
773             /* move the entry */
774             aservers[start] = aservers[end];
775             aservers[end] = 0;  /* this entry was moved */
776         }
777     }
778
779     /* update all my addresses in ubik_host in such a way
780      ** that ubik_host[0] has the primary address
781      */
782     ubik_host[0] = *ame;
783     for (j = 0, i = 1; j < count; j++)
784         if (*ame != myAddr[j])
785             ubik_host[i++] = myAddr[j];
786
787     return 0;                   /* return success */
788 }
789
790
791 /*!
792  * \brief Exchange IP address information with remote servers.
793  *
794  * \param ubik_host an array containing all my IP addresses.
795  *
796  * Algorithm     : Do an RPC to all remote ubik servers informing them
797  *                 about my IP addresses. Get their IP addresses and
798  *                 update my linked list of ubik servers \p ubik_servers
799  *
800  * \return 0 on success, non-zero on failure
801  */
802 int
803 ubeacon_updateUbikNetworkAddress(afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR])
804 {
805     int j, count, code = 0;
806     UbikInterfaceAddr inAddr, outAddr;
807     struct rx_connection *conns[MAXSERVERS];
808     struct ubik_server *ts, *server[MAXSERVERS];
809     char buffer[32];
810     char hoststr[16];
811
812     UBIK_ADDR_LOCK;
813     for (count = 0, ts = ubik_servers; ts; count++, ts = ts->next) {
814         conns[count] = ts->disk_rxcid;
815         server[count] = ts;
816     }
817     UBIK_ADDR_UNLOCK;
818
819
820     /* inform all other servers only if there are more than one
821      * database servers in the cell */
822
823     if (count > 0) {
824
825         for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
826             inAddr.hostAddr[j] = ntohl(ubik_host[j]);
827
828
829         /* do the multi-RX RPC to all other servers */
830         multi_Rx(conns, count) {
831             multi_DISK_UpdateInterfaceAddr(&inAddr, &outAddr);
832             ts = server[multi_i];       /* reply received from this server */
833             if (!multi_error) {
834                 UBIK_ADDR_LOCK;
835                 if (ts->addr[0] != htonl(outAddr.hostAddr[0])) {
836                     code = UBADHOST;
837                     strcpy(buffer, afs_inet_ntoa_r(ts->addr[0], hoststr));
838                     ubik_print("ubik:Two primary addresses for same server \
839                     %s %s\n", buffer,
840                     afs_inet_ntoa_r(htonl(outAddr.hostAddr[0]), hoststr));
841                 } else {
842                     for (j = 1; j < UBIK_MAX_INTERFACE_ADDR; j++)
843                         ts->addr[j] = htonl(outAddr.hostAddr[j]);
844                 }
845                 UBIK_ADDR_UNLOCK;
846             } else if (multi_error == RXGEN_OPCODE) {   /* pre 3.5 remote server */
847                 UBIK_ADDR_LOCK;
848                 ubik_print
849                     ("ubik server %s does not support UpdateInterfaceAddr RPC\n",
850                      afs_inet_ntoa_r(ts->addr[0], hoststr));
851                 UBIK_ADDR_UNLOCK;
852             } else if (multi_error == UBADHOST) {
853                 code = UBADHOST;        /* remote CellServDB inconsistency */
854                 ubik_print("Inconsistent Cell Info on server:\n");
855                 UBIK_ADDR_LOCK;
856                 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR && ts->addr[j]; j++)
857                     ubik_print("... %s\n", afs_inet_ntoa_r(ts->addr[j], hoststr));
858                 UBIK_ADDR_UNLOCK;
859             } else {
860                 UBIK_BEACON_LOCK;
861                 ts->up = 0;     /* mark the remote server as down */
862                 UBIK_BEACON_UNLOCK;
863             }
864         }
865         multi_End;
866     }
867     return code;
868 }
869
870 void
871 ubik_SetClientSecurityProcs(int (*secproc) (void *,
872                                             struct rx_securityClass **,
873                                             afs_int32 *),
874                             int (*checkproc) (void *),
875                             void *rock)
876 {
877     secLayerProc = secproc;
878     tokenCheckProc = checkproc;
879     securityRock = rock;
880 }