2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* Interface and supporting routines for the backup system's ubik database */
12 #include <afsconfig.h>
16 #include <sys/types.h>
20 #elif defined(AFS_SUN5_ENV)
23 #include <sys/param.h> /* for hostnames etc */
26 #include <afs/cellconfig.h>
28 #include <afs/volser.h>
29 #include <afs/afsutil.h>
30 #include <afs/bubasics.h>
31 #include <afs/budb_client.h>
33 #include <afs/com_err.h>
37 #include "error_macros.h"
38 #include "bucoord_internal.h"
39 #include "bucoord_prototypes.h"
43 /* -------------------------------------
45 * -------------------------------------
48 struct udbHandleS udbHandle;
50 /* -------------------------------------
51 * interface routines (alphabetic)
52 * -------------------------------------
55 afs_int32 bcdb_AddVolume(register struct budb_volumeEntry *veptr)
59 code = ubik_BUDB_AddVolume(udbHandle.uh_client, 0, veptr);
63 afs_int32 bcdb_AddVolumes(register struct budb_volumeEntry *veptr, afs_int32 count)
65 struct budb_volumeList volumeList;
68 volumeList.budb_volumeList_len = count;
69 volumeList.budb_volumeList_val = veptr;
70 code = ubik_BUDB_AddVolumes(udbHandle.uh_client, 0, &volumeList);
75 afs_int32 bcdb_CreateDump(register struct budb_dumpEntry *deptr)
79 code = ubik_BUDB_CreateDump(udbHandle.uh_client, 0, deptr);
83 afs_int32 bcdb_deleteDump(afs_int32 dumpID, afs_int32 fromTime, afs_int32 toTime,
84 budb_dumpsList *dumps)
87 budb_dumpsList dumpsList, *dumpsPtr;
89 dumpsList.budb_dumpsList_len = 0;
90 dumpsList.budb_dumpsList_val = 0;
91 dumpsPtr = (dumps ? dumps : &dumpsList);
94 ubik_BUDB_DeleteDump(udbHandle.uh_client, 0, dumpID, fromTime,
96 if (dumpsList.budb_dumpsList_val)
97 free(dumpsList.budb_dumpsList_val);
101 afs_int32 bcdb_listDumps (afs_int32 sflags, afs_int32 groupId,
102 afs_int32 fromTime, afs_int32 toTime,
103 budb_dumpsList *dumps, budb_dumpsList *flags)
106 budb_dumpsList dumpsList, *dumpsPtr;
107 budb_dumpsList flagsList, *flagsPtr;
109 dumpsList.budb_dumpsList_len = 0;
110 dumpsList.budb_dumpsList_val = 0;
111 dumpsPtr = (dumps ? dumps : &dumpsList);
113 flagsList.budb_dumpsList_len = 0;
114 flagsList.budb_dumpsList_val = 0;
115 flagsPtr = (flags ? flags : &flagsList);
118 ubik_BUDB_ListDumps(udbHandle.uh_client, 0, sflags, "", groupId,
119 fromTime, toTime, dumpsPtr, flagsPtr);
121 if (dumpsList.budb_dumpsList_val)
122 free(dumpsList.budb_dumpsList_val);
123 if (flagsList.budb_dumpsList_val)
124 free(flagsList.budb_dumpsList_val);
129 afs_int32 bcdb_DeleteVDP(char *dumpSetName, char *dumpPath, afs_int32 dumpID)
134 ubik_BUDB_DeleteVDP(udbHandle.uh_client, 0, dumpSetName,
140 * Returns the clone time of a volume by going up the parent chain.
141 * If no clone time is found, a clone time of 0 is returned, forcing
144 * dumpID - of the first dump to examine.
145 * volName - name of the volume for whom a clone time is required
146 * clonetime - ptr to vbl for returning result
148 * 0 - clonetime set appropriately
149 * -1 - error occured in traversing chain, clone time set to 0.
150 * -2 - no clone times found, clone time set to 0
153 afs_int32 bcdb_FindClone(afs_int32 dumpID, char *volName, afs_int32 *clonetime)
157 ubik_BUDB_FindClone(udbHandle.uh_client, 0, dumpID, volName,
163 * scan entire database for latest volume dump before adate. Optimize
164 * further by reading only the first line of the dump and if it is older
165 * than the oldest acceptable dump we've found so far, we don't bother
166 * scanning the dump file we've just opened
168 * Used by restore code when restoring a user requested volume(s)
170 * volumeName - name of volume to match on
171 * beforeDate - look for dumps older than this date
173 * deptr - desciptor of most recent dump
175 * should be able to implement this in a single call rather than
176 * the current multiple bcdb_ call algorithm.
180 bcdb_FindDump(char *volumeName, afs_int32 beforeDate,
181 struct budb_dumpEntry *deptr)
185 ubik_BUDB_FindDump(udbHandle.uh_client, 0, volumeName,
191 * find a dump by id. Currently insists on a single return value.
193 * dumpID - id to lookup
197 bcdb_FindDumpByID(afs_int32 dumpID, struct budb_dumpEntry *deptr)
199 register afs_int32 code;
204 /* initialize the dump list */
205 dl.budb_dumpList_len = 0;
206 dl.budb_dumpList_val = 0;
208 /* outline algorithm */
209 code = ubik_BUDB_GetDumps(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_DUMPID, "", /* no name */
213 &nextindex, &dbTime, &dl);
216 || (dl.budb_dumpList_len != 1) /* single retn val expected */
218 /* printf("bcdb_FindDumpByID: code %d, nvalues %d\n",
219 code, dl.budb_dumpList_len); */
221 code = 1; /* multiple id's */
225 memcpy(deptr, dl.budb_dumpList_val, sizeof(*deptr));
228 if (dl.budb_dumpList_val) {
229 /* free any allocated structures */
230 free(dl.budb_dumpList_val);
235 memset(deptr, 0, sizeof(*deptr));
239 /* bcdb_FindLastVolClone
240 * Returns the clone time, from the most recent dump of volName, when
241 * dumped in the volume set volSetName, with dump schedule dumpName.
242 * The clone time can be used to check if the volume has been correctly
243 * re-cloned, and also is used as the time from which to do the current
246 * volSetName - name of volume set
247 * dumpName - full path of dump node
248 * volName - name of volume for whom a clone time is required
249 * clonetime - ptr to vbl for result
251 * 0 - clonetime set appropriately
253 * used only for warning generation. Suggest that this be omitted.
257 bcdb_FindLastVolClone(char *volSetName, char *dumpName, char *volName,
258 afs_int32 *clonetime)
262 * match on volumeset and dump path
263 * search for the volume name
268 /* bcdb_FindLatestDump
269 * find the latest dump with volume set component avname and the
270 * specified dump pathname. Used to find a dump, relative to which an
271 * incremental dump can be done. Defines the parent <-> child relations
274 * avname: volume set name
275 * dumpPath: full path of dump node
277 * 0: adentry: dump entry structure filled in.
278 * -1: probably an internal error
281 * Need to store volumeset name in dump in order to implement this.
282 * Need new routine since params are two strings
286 bcdb_FindLatestDump(char *volSetName, char *dumpPath,
287 struct budb_dumpEntry *deptr)
291 ubik_BUDB_FindLatestDump(udbHandle.uh_client, 0, volSetName,
300 * dumpid: dump id to which tape beint32s
301 * tapeName: name of tape
305 bcdb_FindTape(afs_int32 dumpid, char *tapeName,
306 struct budb_tapeEntry *teptr)
313 memset(teptr, 0, sizeof(*teptr));
314 tl.budb_tapeList_len = 0;
315 tl.budb_tapeList_val = 0;
318 ubik_BUDB_GetTapes(udbHandle.uh_client, 0, BUDB_MAJORVERSION,
319 BUDB_OP_TAPENAME | BUDB_OP_DUMPID, tapeName, dumpid, 0, 0,
320 &next, &dbTime, &tl);
325 if (tl.budb_tapeList_len != 1)
326 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
328 memcpy(teptr, tl.budb_tapeList_val, sizeof(*teptr));
331 if (tl.budb_tapeList_val)
332 free(tl.budb_tapeList_val);
337 bcdb_FindTapeSeq(afs_int32 dumpid, afs_int32 tapeSeq,
338 struct budb_tapeEntry *teptr)
345 memset(teptr, 0, sizeof(*teptr));
346 tl.budb_tapeList_len = 0;
347 tl.budb_tapeList_val = 0;
350 ubik_BUDB_GetTapes(udbHandle.uh_client, 0, BUDB_MAJORVERSION,
351 BUDB_OP_TAPESEQ | BUDB_OP_DUMPID, "", dumpid, tapeSeq, 0,
352 &next, &dbTime, &tl);
356 if (tl.budb_tapeList_len != 1)
357 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
359 memcpy(teptr, tl.budb_tapeList_val, sizeof(*teptr));
362 if (tl.budb_tapeList_val)
363 free(tl.budb_tapeList_val);
369 * - this is part of dblookup. The existing semantics will not work since
370 * they do lookups based on dump id.
371 * - in the restore code, it uses this to extract information about
372 * the volume. Need current semantics. Could filter the output, selecting
374 * - Suggest that the lookup be based on volume name only, with optional
375 * match on backup, and readonly volumes.
376 * - Further, need to check if the volume structure returns enough
381 bcdb_FindVolumes(afs_int32 dumpID, char *volumeName,
382 struct budb_volumeEntry *returnArray,
383 afs_int32 last, afs_int32 *next, afs_int32 maxa,
390 vl.budb_volumeList_len = maxa;
391 vl.budb_volumeList_val = returnArray;
393 /* outline algorithm */
394 code = ubik_BUDB_GetVolumes(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_VOLUMENAME | BUDB_OP_DUMPID, volumeName, /* name */
398 next, /* nextindex */
401 *nEntries = vl.budb_volumeList_len;
406 bcdb_FinishDump(register struct budb_dumpEntry *deptr)
409 code = ubik_BUDB_FinishDump(udbHandle.uh_client, 0, deptr);
414 bcdb_FinishTape(register struct budb_tapeEntry *teptr)
417 code = ubik_BUDB_FinishTape(udbHandle.uh_client, 0, teptr);
422 /* bcdb_LookupVolumes
426 bcdb_LookupVolume(char *volumeName, struct budb_volumeEntry *returnArray,
427 afs_int32 last, afs_int32 *next, afs_int32 maxa,
434 vl.budb_volumeList_len = maxa;
435 vl.budb_volumeList_val = returnArray;
437 /* outline algorithm */
438 code = ubik_BUDB_GetVolumes(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_VOLUMENAME, volumeName, /* name */
442 next, /* nextindex */
448 *nEntries = vl.budb_volumeList_len;
453 bcdb_UseTape(struct budb_tapeEntry *teptr, afs_int32 *newFlag)
456 code = ubik_BUDB_UseTape(udbHandle.uh_client, 0, teptr, newFlag);
461 /* ---- text configuration handling routines ----
464 * The caller should pass in/out a fid for an unlinked, open file to prevent
465 * tampering with the files contents;
469 * extract the specified textType and put it in a temporary, local
472 * ctPtr - ptr to client structure with all the required information
476 bcdb_GetTextFile(register udbClientTextP ctPtr)
478 afs_int32 bufferSize;
479 afs_int32 offset, nextOffset;
483 /* Initialize charlistT_val. We try to deallocate this structure based on
485 memset((void *)&charList, 0, sizeof(charList));
487 /* check params and cleanup any previous state */
488 if (ctPtr->lockHandle == 0)
489 ERROR(BUDB_INTERNALERROR);
491 if (ctPtr->textStream == NULL) /* Should have an open stream */
492 ERROR(BUDB_INTERNALERROR);
494 /* allocate a buffer */
496 charList.charListT_val = (char *)malloc(bufferSize);
497 if (charList.charListT_val == 0)
498 ERROR(BUDB_INTERNALERROR);
499 charList.charListT_len = bufferSize;
504 while (nextOffset != -1) {
506 charList.charListT_len = bufferSize;
508 ubik_BUDB_GetText(udbHandle.uh_client, 0, ctPtr->lockHandle,
509 ctPtr->textType, bufferSize, offset, &nextOffset,
516 fwrite(charList.charListT_val, sizeof(char),
517 charList.charListT_len, ctPtr->textStream);
518 if (ferror(ctPtr->textStream))
519 ERROR(BUDB_INTERNALERROR);
521 ctPtr->textSize += charList.charListT_len;
524 /* get text version */
526 ubik_BUDB_GetTextVersion(udbHandle.uh_client, 0,
527 ctPtr->textType, &ctPtr->textVersion);
532 fflush(ctPtr->textStream); /* debug */
534 /* exit, leaving the configuration text file open */
535 if (charList.charListT_val)
536 free(charList.charListT_val);
540 if (ctPtr->textStream != NULL) {
541 fclose(ctPtr->textStream);
542 ctPtr->textStream = NULL;
549 * save the text file in ubik database
551 * textType - identifies type of configuration file
552 * filename - where to get the text from
556 bcdb_SaveTextFile(udbClientTextP ctPtr)
558 afs_int32 bufferSize;
559 afs_int32 offset, chunkSize, fileSize;
563 /* allocate a buffer */
565 charList.charListT_val = (char *)malloc(bufferSize);
566 if (charList.charListT_val == 0)
567 ERROR(BUDB_INTERNALERROR);
568 charList.charListT_len = bufferSize;
570 if (ctPtr->textStream == NULL)
571 ERROR(BUDB_INTERNALERROR);
572 rewind(ctPtr->textStream);
574 fileSize = (afs_int32) filesize(ctPtr->textStream);
576 afs_dprintf(("filesize is %d\n", fileSize));
578 rewind(ctPtr->textStream);
580 /* special case empty files */
582 charList.charListT_len = 0;
584 ubik_BUDB_SaveText(udbHandle.uh_client, 0,
585 ctPtr->lockHandle, ctPtr->textType, 0,
586 BUDB_TEXT_COMPLETE, &charList);
591 while (fileSize != 0) {
592 chunkSize = MIN(fileSize, bufferSize);
594 fread(charList.charListT_val, sizeof(char), chunkSize,
597 if (code != chunkSize)
598 printf("code = %d\n", code);
599 if (ferror(ctPtr->textStream))
600 ERROR(BUDB_INTERNALERROR);
602 charList.charListT_len = chunkSize;
604 ubik_BUDB_SaveText(udbHandle.uh_client, 0,
605 ctPtr->lockHandle, ctPtr->textType, offset,
606 (chunkSize == fileSize) ? BUDB_TEXT_COMPLETE : 0,
611 fileSize -= chunkSize;
616 /* if ( ctPtr->textStream >= 0 )
617 * close(ctPtr->textStream); */
618 if (charList.charListT_val)
619 free(charList.charListT_val);
624 bcdb_FindLastTape(afs_int32 dumpID, struct budb_dumpEntry *dumpEntry,
625 struct budb_tapeEntry *tapeEntry,
626 struct budb_volumeEntry *volEntry)
628 return (ubik_BUDB_FindLastTape(udbHandle.uh_client, 0, dumpID, dumpEntry,
629 tapeEntry, volEntry));
633 bcdb_MakeDumpAppended(afs_int32 appendedDumpID, afs_int32 initialDumpID,
634 afs_int32 startTapeSeq)
636 return (ubik_BUDB_MakeDumpAppended(udbHandle.uh_client, 0, appendedDumpID,
637 initialDumpID, startTapeSeq));
641 /* -------------------------------------
642 * misc. support routines
643 * -------------------------------------
647 filesize(FILE *stream)
652 offset = ftell(stream);
653 fseek(stream, (afs_int32) 0, 2); /* end of file */
654 size = ftell(stream);
655 fseek(stream, offset, 0);
660 /* ------------------------------------
661 * misc. support routines - general text management
662 * ------------------------------------
667 * locks the text described by the ctPtr
669 * ctptr - client text ptr
676 bc_LockText(udbClientTextP ctPtr)
679 afs_int32 timeout, j = 0;
681 if (ctPtr->lockHandle != 0)
682 return (1); /* already locked */
685 ((ctPtr->textSize == 0) ? 30 : ((ctPtr->textSize / 50000) + 10));
689 ubik_BUDB_GetLock(udbHandle.uh_client, 0,
690 udbHandle.uh_instanceId, ctPtr->textType, timeout,
692 if ((code != BUDB_LOCKED) && (code != BUDB_SELFLOCKED)) {
696 /* Mention something every 30 seconds */
698 afs_com_err(whoami, code,
699 "; Waiting for db configuration text unlock");
702 #ifdef AFS_PTHREAD_ENV
711 ctPtr->lockHandle = 0;
716 * unlocks the text described by the ctPtr
718 * ctptr - client text ptr
725 bc_UnlockText(udbClientTextP ctPtr)
729 if (ctPtr->lockHandle == 0)
733 ubik_BUDB_FreeLock(udbHandle.uh_client, 0, ctPtr->lockHandle);
734 ctPtr->lockHandle = 0;
736 /* Don't try to analyse the error. Let the lock timeout */
740 /* bc_CheckTextVersion
743 * n - out of date or error
747 bc_CheckTextVersion(udbClientTextP ctPtr)
752 if (ctPtr->textVersion == -1)
753 return (BC_VERSIONMISMATCH);
756 ubik_BUDB_GetTextVersion(udbHandle.uh_client, 0,
757 ctPtr->textType, &tversion);
760 if (tversion != ctPtr->textVersion)
761 return (BC_VERSIONMISMATCH);
765 /* -------------------------------------
766 * initialization routines
767 * -------------------------------------
771 * Initialize a client for the vl ubik database.
774 vldbClientInit(int noAuthFlag, int localauth, char *cellName,
775 struct ubik_client **cstruct,
776 struct ktc_token *ttoken)
779 struct afsconf_dir *acdir;
780 struct rx_securityClass *sc;
781 afs_int32 i, scIndex = 0; /* Index of Rx security object - noauth */
782 struct afsconf_cell info;
783 struct ktc_principal sname;
784 struct rx_connection *serverconns[VLDB_MAXSERVERS];
787 /* Find out about the given cell */
789 afsconf_Open((localauth ? AFSDIR_SERVER_ETC_DIRPATH :
790 AFSDIR_CLIENT_ETC_DIRPATH));
792 afs_com_err(whoami, 0, "Can't open configuration directory '%s'",
793 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
794 AFSDIR_CLIENT_ETC_DIRPATH));
795 ERROR(BC_NOCELLCONFIG);
801 code = afsconf_GetLocalCell(acdir, cname, sizeof(cname));
803 afs_com_err(whoami, code,
804 "; Can't get the local cell name - check %s/%s",
805 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
806 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_THISCELL_FILE);
809 strcpy(cellName, cname);
812 code = afsconf_GetCellInfo(acdir, cellName, AFSCONF_VLDBSERVICE, &info);
814 afs_com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
816 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
817 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_CELLSERVDB_FILE);
818 ERROR(BC_NOCELLCONFIG);
822 * Grab tickets if we care about authentication.
826 code = afsconf_GetLatestKey(acdir, 0, 0);
828 afs_com_err(whoami, code, "; Can't get key from local key file");
831 code = afsconf_ClientAuth(acdir, &sc, &scIndex);
833 afs_com_err(whoami, code, "; Calling ClientAuth");
837 ttoken->endTime = NEVERDATE;
841 strcpy(sname.cell, info.name);
842 sname.instance[0] = 0;
843 strcpy(sname.name, "afs");
846 ktc_GetToken(&sname, ttoken, sizeof(struct ktc_token), NULL);
848 afs_com_err(whoami, code, 0,
849 "; Can't get AFS tokens - running unauthenticated");
851 if ((ttoken->kvno < 0) || (ttoken->kvno > 256))
852 afs_com_err(whoami, 0,
853 "Funny kvno (%d) in ticket, proceeding",
862 sc = rxnull_NewClientSecurityObject();
865 sc = (struct rx_securityClass *)
866 rxkad_NewClientSecurityObject(rxkad_clear,
868 ttoken->kvno, ttoken->ticketLen,
872 afs_com_err(whoami, 0, "Unsupported authentication type %d", scIndex);
879 afs_com_err(whoami, 0,
880 "Can't create a security object with security index %d",
885 /* tell UV module about default authentication */
886 UV_SetSecurity(sc, scIndex);
888 if (info.numServers > VLDB_MAXSERVERS) {
889 afs_com_err(whoami, 0,
890 "Warning: %d VLDB servers exist for cell '%s', can only remember the first %d",
891 info.numServers, cellName, VLDB_MAXSERVERS);
892 info.numServers = VLDB_MAXSERVERS;
895 for (i = 0; i < info.numServers; i++)
897 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
898 info.hostAddr[i].sin_port, USER_SERVICE_ID, sc,
903 code = ubik_ClientInit(serverconns, cstruct);
905 afs_com_err(whoami, code, "; Can't initialize ubik connection to vldb");
911 afsconf_Close(acdir);
916 * initialize a client for the backup systems ubik database.
920 udbClientInit(int noAuthFlag, int localauth, char *cellName)
922 struct ktc_principal principal;
923 struct ktc_token token;
924 struct afsconf_cell info;
925 struct afsconf_dir *acdir;
930 afsconf_Open((localauth ? AFSDIR_SERVER_ETC_DIRPATH :
931 AFSDIR_CLIENT_ETC_DIRPATH));
933 afs_com_err(whoami, 0, "Can't open configuration directory '%s'",
934 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
935 AFSDIR_CLIENT_ETC_DIRPATH));
936 ERROR(BC_NOCELLCONFIG);
942 code = afsconf_GetLocalCell(acdir, cname, sizeof(cname));
944 afs_com_err(whoami, code,
945 "; Can't get the local cell name - check %s/%s",
946 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
947 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_THISCELL_FILE);
950 strcpy(cellName, cname);
953 code = afsconf_GetCellInfo(acdir, cellName, 0, &info);
955 afs_com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
957 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
958 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_CELLSERVDB_FILE);
959 ERROR(BC_NOCELLCONFIG);
962 udbHandle.uh_scIndex = RX_SCINDEX_NULL;
965 code = afsconf_GetLatestKey(acdir, 0, 0);
967 afs_com_err(whoami, code, "; Can't get key from local key file");
971 afsconf_ClientAuth(acdir, &udbHandle.uh_secobj,
972 &udbHandle.uh_scIndex);
974 afs_com_err(whoami, code, "; Calling ClientAuth");
980 /* setup principal */
981 strcpy(principal.cell, info.name);
982 principal.instance[0] = 0;
983 strcpy(principal.name, "afs");
986 code = ktc_GetToken(&principal, &token, sizeof(token), NULL);
988 afs_com_err(whoami, code,
989 "; Can't get tokens - running unauthenticated");
991 if ((token.kvno < 0) || (token.kvno > 256))
992 afs_com_err(whoami, 0,
993 "Unexpected kvno (%d) in ticket - proceeding",
995 udbHandle.uh_scIndex = RX_SCINDEX_KAD; /* Kerberos */
999 switch (udbHandle.uh_scIndex) {
1001 udbHandle.uh_secobj = rxnull_NewClientSecurityObject();
1005 udbHandle.uh_secobj = (struct rx_securityClass *)
1006 rxkad_NewClientSecurityObject(rxkad_clear, &token.sessionKey,
1007 token.kvno, token.ticketLen,
1012 afs_com_err(whoami, 0, "Unsupported authentication type %d",
1013 udbHandle.uh_scIndex);
1019 if (!udbHandle.uh_secobj) {
1020 afs_com_err(whoami, 0,
1021 "Can't create a security object with security index %d",
1022 udbHandle.uh_secobj);
1026 if (info.numServers > MAXSERVERS) {
1027 afs_com_err(whoami, 0,
1028 "Warning: %d BDB servers exist for cell '%s', can only remember the first %d",
1029 info.numServers, cellName, MAXSERVERS);
1030 info.numServers = MAXSERVERS;
1033 /* establish connections to the servers. Check for failed connections? */
1034 for (i = 0; i < info.numServers; i++) {
1035 udbHandle.uh_serverConn[i] =
1036 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
1037 htons(AFSCONF_BUDBPORT), BUDB_SERVICE,
1038 udbHandle.uh_secobj, udbHandle.uh_scIndex);
1040 udbHandle.uh_serverConn[i] = 0;
1042 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1044 afs_com_err(whoami, code,
1045 "; Can't initialize ubik connection to backup database");
1049 /* Try to quickly find a good site by setting deadtime low */
1050 for (i = 0; i < info.numServers; i++)
1051 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 1);
1053 ubik_BUDB_GetInstanceId(udbHandle.uh_client, 0,
1054 &udbHandle.uh_instanceId);
1056 /* Reset dead time back up to default */
1057 for (i = 0; i < info.numServers; i++)
1058 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 60);
1060 /* If did not find a site on first quick pass, try again */
1063 ubik_BUDB_GetInstanceId(udbHandle.uh_client, 0,
1064 &udbHandle.uh_instanceId);
1066 afs_com_err(whoami, code, "; Can't access backup database");
1072 afsconf_Close(acdir);
1076 /* -------------------------------------
1077 * specialized ubik support
1078 * -------------------------------------
1086 * 1) first call with SINGLESERVER set, record the server to be used.
1087 * 2) subsequent calls use that server. If a failure is encountered,
1088 * the state is cleaned up and the error returned back to the caller.
1089 * 3) upon completion, the user must make a dummy call with
1090 * END_SINGLESERVER set, to clean up state.
1091 * 4) if the vanilla ubik_Call is ever modified, the END_SINGLESERVER
1092 * flag can be discarded. The first call without SINGLESERVER set
1093 * can clean up the state.
1096 struct ubikCallState {
1097 afs_int32 ucs_flags; /* state flags */
1098 afs_int32 ucs_selectedServer; /* which server selected */
1101 static struct ubikCallState uServer;
1103 /* ubik_Call_SingleServer
1104 * variant of ubik_Call. This is used by the backup system to initiate
1105 * a series of calls to a single ubik server. The first call sets up
1106 * process state (not recorded in ubik) that requires all further calls
1107 * of that group to be made to the same server process.
1109 * call this instead of stub and we'll guarantee to find a host that's up.
1110 * in the future, we should also put in a protocol to find the sync site
1114 ubik_Call_SingleServer(int (*aproc) (), struct ubik_client *aclient,
1115 afs_int32 aflags, char *p1, char *p2, char *p3,
1116 char *p4, char *p5, char *p6, char *p7, char *p8,
1117 char *p9, char *p10, char *p11, char *p12, char *p13,
1118 char *p14, char *p15, char *p16)
1120 register afs_int32 code;
1121 afs_int32 someCode, newHost, thisHost;
1122 register afs_int32 i;
1123 register afs_int32 count;
1126 struct rx_connection *tc;
1127 struct rx_peer *rxp;
1129 if ((aflags & (UF_SINGLESERVER | UF_END_SINGLESERVER)) != 0) {
1130 if (((aflags & UF_SINGLESERVER) != 0)
1131 && ((uServer.ucs_flags & UF_SINGLESERVER) != 0)
1134 /* have a selected server */
1135 tc = aclient->conns[uServer.ucs_selectedServer];
1138 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
1139 p12, p13, p14, p15, p16);
1141 /* error. Clean up single server state */
1142 memset(&uServer, 0, sizeof(uServer));
1145 } else if ((aflags & UF_END_SINGLESERVER) != 0) {
1146 memset(&uServer, 0, sizeof(uServer));
1151 someCode = UNOSERVERS;
1157 /* tc is the next conn to try */
1158 tc = aclient->conns[count];
1161 pass = 1; /* in pass 1, we look at down hosts, too */
1165 break; /* nothing left to try */
1167 if (pass == 0 && (aclient->states[count] & CFLastFailed)) {
1169 continue; /* this guy's down, try someone else first */
1173 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12,
1174 p13, p14, p15, p16);
1176 /* note that getting a UNOTSYNC error code back does *not* guarantee
1177 * that there is a sync site yet elected. However, if there is a
1178 * sync site out there somewhere, and you're trying an operation that
1179 * requires a sync site, ubik will return UNOTSYNC, indicating the
1180 * operation won't work until you find a sync site
1182 if (code == UNOTSYNC) { /*ns */
1183 /* means that this requires a sync site to work */
1184 someCode = code; /* remember an error, if this fails */
1186 /* now see if we can find the sync site host */
1187 code = VOTE_GetSyncSite(tc, &newHost);
1188 if (code == 0 && newHost != 0) {
1189 newHost = htonl(newHost); /* convert back to network order */
1191 /* position count at the appropriate slot in the client
1192 * structure and retry. If we can't find in slot, we'll just
1193 * continue through the whole list
1195 for (i = 0; i < MAXSERVERS; i++) { /*f */
1196 rxp = rx_PeerOf(aclient->conns[i]);
1197 if (!(thisHost = rx_HostOf(rxp))) {
1198 count++; /* host not found, try the next dude */
1201 if (thisHost == newHost) {
1202 /* avoid asking in a loop */
1203 if (chaseCount++ > 2)
1205 count = i; /* we were told to use this one */
1210 count++; /* not directed, keep looking for a sync site */
1213 else if (code == UNOQUORUM) { /* this guy is still recovering */
1217 } else if (code < 0) { /* network errors */
1219 aclient->states[count] |= CFLastFailed;
1223 /* ok, operation worked */
1224 aclient->states[count] &= ~CFLastFailed;
1225 /* either misc ubik code, or misc application code (incl success)
1228 /* if the call succeeded, setup connection state for subsequent
1232 && ((aflags & UF_SINGLESERVER) != 0)
1234 /* need to save state */
1235 uServer.ucs_flags = UF_SINGLESERVER;
1236 uServer.ucs_selectedServer = count;
1246 /* -------------------------------------
1247 * debug and test routines
1248 * -------------------------------------
1252 * For testing only. Open a connect to the database server running on
1255 * 0 - ubik connection established in the global udbHandle structure
1262 afs_int32 serverList[MAXSERVERS];
1268 /* get our host name */
1269 gethostname(hostname, sizeof(hostname));
1270 /* strcpy(hostname, "hops"); */
1273 args[1] = "-servers";
1276 code = ubik_ParseClientList(3, args, serverList);
1278 afs_com_err(whoami, code, "; udbLocalInit: parsing ubik server list");
1282 udbHandle.uh_scIndex = RX_SCINDEX_NULL;
1283 udbHandle.uh_secobj = (struct rx_securityClass *)
1284 rxnull_NewClientSecurityObject();
1286 for (i = 0; serverList[i] != 0; i++) {
1287 udbHandle.uh_serverConn[i] =
1288 rx_NewConnection(serverList[i], htons(AFSCONF_BUDBPORT),
1289 BUDB_SERVICE, udbHandle.uh_secobj,
1290 udbHandle.uh_scIndex);
1291 if (udbHandle.uh_serverConn[i] == 0) {
1292 afs_com_err(whoami, 0, "connection %d failed", i);
1296 udbHandle.uh_serverConn[i] = 0;
1297 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1299 afs_com_err(whoami, code, "; in ubik_ClientInit");
1304 ubik_BUDB_GetInstanceId(udbHandle.uh_client, 0,
1305 &udbHandle.uh_instanceId);
1307 afs_com_err(whoami, code, "; Can't estblish instance Id");
1315 /* bc_openTextFile - This function opens a temp file to read in the
1316 * config text recd from the bu server. On Unix, an unlink() is done on
1317 * the file as soon as it is opened, so when the program exits, the file will
1318 * be removed automatically, while being invisible while in use.
1319 * On NT, however, the file must be explicitly deleted after use with an unlink()
1321 * Pointer to a udhClientTextP struct. The open stream ptr is stored in
1322 * the udbClientTextP.textStream member.
1323 * Output: The temp file name is returned in tmpFileName. This should be used
1324 * to delete the file when done with it.
1330 bc_openTextFile(udbClientTextP ctPtr, char *tmpFileName)
1334 if (ctPtr->textStream != NULL)
1335 fclose(ctPtr->textStream);
1337 sprintf(tmpFileName, "%s/bu_XXXXXX", gettmpdir());
1338 #ifdef AFS_LINUX20_ENV
1339 mkstemp(tmpFileName);
1341 mktemp(tmpFileName);
1343 ctPtr->textStream = fopen(tmpFileName, "w+");
1344 if (ctPtr->textStream == NULL)
1345 ERROR(BUDB_INTERNALERROR);
1347 #ifndef AFS_NT40_ENV /* This can't be done on NT */
1348 /* make the file invisible to others */
1349 code = unlink(tmpFileName);
1354 afs_dprintf(("file is %s\n", tmpFileName));
1360 if (ctPtr->textStream != NULL) {
1361 fclose(ctPtr->textStream);
1362 ctPtr->textStream = NULL;
1368 /* bc_closeTextFile: This function closes any actual temp files associated with
1369 * a udbClientText structure.
1370 * Input: ctPtr->textStream - stream to close
1371 * tmpFileName - temp file name to delete
1377 bc_closeTextFile(udbClientTextP ctPtr, char *tmpFileName)
1381 if (ctPtr->textStream)
1382 fclose(ctPtr->textStream);
1384 if (*tmpFileName) { /* we have a valid name */
1385 code = unlink(tmpFileName);