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