budb: Store dumper information correctly
[openafs.git] / src / budb / procs.c
index 7f5f350..d3d8283 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <netinet/in.h>
-#include <sys/file.h>
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#endif
-
-#include <string.h>
-#include <sys/types.h>
 #include <afs/stds.h>
 #include <afs/bubasics.h>
-#include <stdio.h>
 #include <lock.h>
 #include <ubik.h>
 #include <lwp.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
 #include <rx/rxkad.h>
-#include <des.h>
 #include <afs/cellconfig.h>
 #include <afs/auth.h>
-#include <errno.h>
+#include <afs/audit.h>
+#include <afs/afsutil.h>
+
 #include "budb.h"
 #include "budb_errs.h"
 #include "database.h"
 #include "budb_internal.h"
 #include "error_macros.h"
 #include "globals.h"
-#include "afs/audit.h"
-#include <afs/afsutil.h>
 
 #undef min
 #undef max
@@ -63,21 +50,21 @@ afs_int32 AddVolumes(struct rx_call *, struct budb_volumeList *);
 afs_int32 CreateDump(struct rx_call *, struct budb_dumpEntry *);
 afs_int32 DoDeleteDump(struct rx_call *, dumpId, Date, Date, budb_dumpsList *);
 afs_int32 DoDeleteTape(struct rx_call *, struct budb_tapeEntry *);
-afs_int32 ListDumps(struct rx_call *, afs_int32, afs_int32, Date, Date, 
+afs_int32 ListDumps(struct rx_call *, afs_int32, afs_int32, Date, Date,
                    budb_dumpsList *, budb_dumpsList *);
 afs_int32 DeleteVDP(struct rx_call *, char *, char *, afs_int32);
 afs_int32 FindClone(struct rx_call *, afs_int32, char *, afs_int32 *);
-afs_int32 FindDump(struct rx_call *, char *, afs_int32, 
+afs_int32 FindDump(struct rx_call *, char *, afs_int32,
                   struct budb_dumpEntry *);
-afs_int32 FindLatestDump(struct rx_call *, char *, char *, 
+afs_int32 FindLatestDump(struct rx_call *, char *, char *,
                         struct budb_dumpEntry *);
 afs_int32 FinishDump(struct rx_call *, struct budb_dumpEntry *);
 afs_int32 FinishTape(struct rx_call *, struct budb_tapeEntry *);
-afs_int32 GetDumps(struct rx_call *, afs_int32, afs_int32, char *, 
+afs_int32 GetDumps(struct rx_call *, afs_int32, afs_int32, char *,
                   afs_int32, afs_int32, afs_int32, afs_int32 *,
                   afs_int32 *, budb_dumpList *);
 afs_int32 getExpiration(struct ubik_trans *ut, struct tape *);
-afs_int32 makeAppended(struct ubik_trans *ut, afs_int32, afs_int32, 
+afs_int32 makeAppended(struct ubik_trans *ut, afs_int32, afs_int32,
                       afs_int32);
 afs_int32 MakeDumpAppended(struct rx_call *, afs_int32, afs_int32,
                           afs_int32);
@@ -125,7 +112,7 @@ static int procsInited = 0;
 
 static int (*rebuildDatabase) (struct ubik_trans *);
 
-/* AwaitInitialization 
+/* AwaitInitialization
  * Wait unitl budb has initialized (InitProcs). If it hasn't
  * within 5 seconds, then return no quorum.
  */
@@ -359,7 +346,7 @@ FreeReturnList(struct returnList *list)
 static afs_int32
 AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP)
 {
-    char *tmp;
+    dbadr *tmp;
     afs_int32 size;
 
     if (a == 0)
@@ -369,7 +356,7 @@ AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP)
        return 0;
     }
 
-    /* Up to 5 plus a maximum so SendReturnList() knows if we 
+    /* Up to 5 plus a maximum so SendReturnList() knows if we
      * need to come back for more.
      */
     if (list->nElements >= BUDB_MAX_RETURN_LIST + 5)
@@ -378,14 +365,14 @@ AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP)
     if (list->nElements >= list->allocSize) {
        if (list->elements == 0) {
            size = 10;
-           tmp = (char *)malloc(sizeof(dbadr) * size);
+           tmp = malloc(sizeof(dbadr) * size);
        } else {
            size = list->allocSize + 10;
-           tmp = (char *)realloc(list->elements, sizeof(dbadr) * size);
+           tmp = realloc(list->elements, sizeof(dbadr) * size);
        }
        if (!tmp)
            return BUDB_NOMEM;
-       list->elements = (dbadr *) tmp;
+       list->elements = tmp;
        list->allocSize = size;
     }
 
@@ -395,8 +382,9 @@ AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP)
 }
 
 afs_int32
-FillVolEntry(struct ubik_trans *ut, dbadr va, struct budb_volumeEntry *vol)
+FillVolEntry(struct ubik_trans *ut, dbadr va, void *rock)
 {
+    struct budb_volumeEntry *vol = (struct budb_volumeEntry *) rock;
     struct dump d;
     struct tape t;
     struct volInfo vi;
@@ -431,8 +419,9 @@ FillVolEntry(struct ubik_trans *ut, dbadr va, struct budb_volumeEntry *vol)
 }
 
 afs_int32
-FillDumpEntry(struct ubik_trans *ut, dbadr da, struct budb_dumpEntry *dump)
+FillDumpEntry(struct ubik_trans *ut, dbadr da, void *rock)
 {
+    struct budb_dumpEntry *dump = (struct budb_dumpEntry *)rock;
     struct dump d, ad;
 
     if (dbread(ut, da, &d, sizeof(d)))
@@ -473,8 +462,9 @@ FillDumpEntry(struct ubik_trans *ut, dbadr da, struct budb_dumpEntry *dump)
 }
 
 afs_int32
-FillTapeEntry(struct ubik_trans *ut, dbadr ta, struct budb_tapeEntry *tape)
+FillTapeEntry(struct ubik_trans *ut, dbadr ta, void *rock)
 {
+    struct budb_tapeEntry *tape=(struct budb_tapeEntry *) rock;
     struct tape t;
     struct dump d;
     afs_int32 code;
@@ -516,8 +506,8 @@ FillTapeEntry(struct ubik_trans *ut, dbadr ta, struct budb_tapeEntry *tape)
 static afs_int32
 SendReturnList(struct ubik_trans *ut,
               struct returnList *list, /* list of elements to return */
-              afs_int32(*FillProc) (struct ubik_trans *, dbadr da, 
-                                    budb_dumpEntry *), 
+              afs_int32(*FillProc) (struct ubik_trans *, dbadr da,
+                                    void *),
                                        /* proc to fill entry */
               int e_size,              /* size of each element */
               afs_int32 index,         /* index from previous call */
@@ -544,12 +534,16 @@ SendReturnList(struct ubik_trans *ut,
 
     /* Allocate space for the return values if needed and zero it */
     if (eList->budb_dumpList_val == 0) {
-       eList->budb_dumpList_val =
-           (struct budb_dumpEntry *)malloc(e_size * to_return);
-       if (!eList->budb_dumpList_val)
-           return (BUDB_NOMEM);
+       if (to_return > 0) {
+           eList->budb_dumpList_val = calloc(to_return, e_size);
+           if (!eList->budb_dumpList_val)
+               return (BUDB_NOMEM);
+       } else
+           eList->budb_dumpList_val = NULL;
+    } else {
+        memset(eList->budb_dumpList_val, 0, e_size * to_return);
     }
-    memset(eList->budb_dumpList_val, 0, e_size * to_return);
+
     eList->budb_dumpList_len = to_return;
 
     e = (char *)(eList->budb_dumpList_val);
@@ -754,7 +748,7 @@ GetVolInfo(struct ubik_trans *ut, struct budb_volumeEntry *volP, dbadr *viaP,
        if (eval)
            ERROR(eval);
 
-       LogDebug(4, "volume Info for %s placed at %d\n", db.volName, via);
+       LogDebug(4, "volume Info for %s placed at %d\n", volP->name, via);
     }
 
     else if (!VolInfoMatch(volP, viP)) {       /* Not the head volinfo struct */
@@ -804,7 +798,7 @@ GetVolInfo(struct ubik_trans *ut, struct budb_volumeEntry *volP, dbadr *viaP,
 }
 
 /* deletesomevolumesfromtape
- *     Deletes a specified number of volumes from a tape. The tape 
+ *     Deletes a specified number of volumes from a tape. The tape
  *     and dump are modified to reflect the smaller number of volumes.
  *     The transaction is not terminated, it is up to the caller to
  *     finish the transaction and start a new one (if desired).
@@ -948,13 +942,12 @@ deleteDump(struct rx_call *call, dumpId id, budb_dumpsList *dumps)
        /* Record the dump just deleted */
        if (dumps && (dumps->budb_dumpsList_len < BUDB_MAX_RETURN_LIST)) {
            if (dumps->budb_dumpsList_len == 0)
-               dumps->budb_dumpsList_val =
-                   (afs_int32 *) malloc(sizeof(afs_int32));
+               dumps->budb_dumpsList_val = malloc(sizeof(afs_int32));
            else
                dumps->budb_dumpsList_val =
-                   (afs_int32 *) realloc(dumps->budb_dumpsList_val,
-                                         (dumps->budb_dumpsList_len +
-                                          1) * sizeof(afs_int32));
+                   realloc(dumps->budb_dumpsList_val,
+                           (dumps->budb_dumpsList_len + 1)
+                            * sizeof(afs_int32));
 
            if (!dumps->budb_dumpsList_val)
                ABORT(BUDB_NOMEM);
@@ -1050,10 +1043,9 @@ rememberDump(dbadr dumpAddr, void *dumpParam, void *dumpListPtrParam)
     dumpPtr = (struct dump *)dumpParam;
     rockPtr = (struct wantDumpRock *)dumpListPtrParam;
 
-    ptr = (struct chosenDump *)malloc(sizeof(*ptr));
+    ptr = calloc(1, sizeof(*ptr));
     if (!ptr)
        return (0);
-    memset(ptr, 0, sizeof(*ptr));
     ptr->addr = dumpAddr;
     ptr->date = (afs_uint32) ntohl(dumpPtr->created);
 
@@ -1484,7 +1476,7 @@ CreateDump(struct rx_call *call, struct budb_dumpEntry *dump)
        dump->created = dump->id;
     d.created = htonl(dump->created);
 
-    principal_hton(&principal, &d.dumper);
+    d.dumper = principal;
     tapeSet_hton(&dump->tapes, &d.tapes);
 
     d.flags = htonl(dump->flags | BUDB_DUMP_INPROGRESS);
@@ -1547,8 +1539,8 @@ DoDeleteDump(struct rx_call *call, dumpId id, Date fromTime, Date toTime,
 }
 
 afs_int32
-SBUDB_ListDumps(struct rx_call *call, afs_int32 sflags, char *name, 
-               afs_int32 groupid, Date fromTime, Date toTime, 
+SBUDB_ListDumps(struct rx_call *call, afs_int32 sflags, char *name,
+               afs_int32 groupid, Date fromTime, Date toTime,
                budb_dumpsList *dumps, budb_dumpsList *flags)
 {
     afs_int32 code;
@@ -1559,8 +1551,8 @@ SBUDB_ListDumps(struct rx_call *call, afs_int32 sflags, char *name,
 }
 
 afs_int32
-ListDumps(struct rx_call *call, afs_int32 sflags, afs_int32 groupid, 
-         Date fromTime, Date toTime, budb_dumpsList *dumps, 
+ListDumps(struct rx_call *call, afs_int32 sflags, afs_int32 groupid,
+         Date fromTime, Date toTime, budb_dumpsList *dumps,
          budb_dumpsList *flags)
 {
     struct ubik_trans *ut;
@@ -1640,22 +1632,16 @@ ListDumps(struct rx_call *call, afs_int32 sflags, afs_int32 groupid,
                        count += 10;
                        if (count == 10) {
                            dumps->budb_dumpsList_val =
-                               (afs_int32 *) malloc(count *
-                                                    sizeof(afs_int32));
+                               malloc(count * sizeof(afs_int32));
                            flags->budb_dumpsList_val =
-                               (afs_int32 *) malloc(count *
-                                                    sizeof(afs_int32));
+                               malloc(count * sizeof(afs_int32));
                        } else {
                            dumps->budb_dumpsList_val =
-                               (afs_int32 *) realloc(dumps->
-                                                     budb_dumpsList_val,
-                                                     count *
-                                                     sizeof(afs_int32));
+                               realloc(dumps->budb_dumpsList_val,
+                                       count * sizeof(afs_int32));
                            flags->budb_dumpsList_val =
-                               (afs_int32 *) realloc(flags->
-                                                     budb_dumpsList_val,
-                                                     count *
-                                                     sizeof(afs_int32));
+                               realloc(flags->budb_dumpsList_val,
+                                       count * sizeof(afs_int32));
                        }
                        if (!dumps->budb_dumpsList_val
                            || !dumps->budb_dumpsList_val)
@@ -1830,11 +1816,11 @@ DeleteVDP(struct rx_call *call, char *dsname, char *dumpPath,
 
 /* BUDB_FindClone
  * notes:
- *      Given a volume name, and a dumpID, find the volume in that dump and 
- *      return the clone date of the volume (this is the clone date of the 
+ *      Given a volume name, and a dumpID, find the volume in that dump and
+ *      return the clone date of the volume (this is the clone date of the
  *      volume at the time it was dumped).
- *   
- *      Hashes on the volume name and traverses the fragments. Will need to read 
+ *
+ *      Hashes on the volume name and traverses the fragments. Will need to read
  *      the volumes tape entry to determine if it belongs to the dump. If the
  *      volume is not found in the dump, then look for it in its parent dump.
  */
@@ -2400,12 +2386,12 @@ FinishTape(struct rx_call *call, struct budb_tapeEntry *tape)
  *     flags - for search and select operations. Broken down into flags
  *             for name, start point, end point and time.
  *     name - name to search for. Interpretation based on flags
- *     end 
+ *     end
  *     index
  *     nextIndexP
  *     dbTimeP
  * exit:
- *     nextIndexP 
+ *     nextIndexP
  *     dbTimeP - time at which the database was last modified. Up to
  *             caller (client) to take appropriate action if database
  *             modified between successive calls
@@ -2619,9 +2605,9 @@ GetDumps(struct rx_call *call,
     return code;
 }
 
-/* 
+/*
  * Get the expiration of a tape.  Since the dump could have appended dumps,
- * we should use the most recent expiration date. Put the most recent 
+ * we should use the most recent expiration date. Put the most recent
  * expiration tape into the given tape structure.
  */
 afs_int32
@@ -2676,7 +2662,7 @@ getExpiration(struct ubik_trans *ut, struct tape *tapePtr)
 
 /* Mark the following dump as appended to another, intial dump */
 afs_int32
-makeAppended(struct ubik_trans *ut, afs_int32 appendedDumpID, 
+makeAppended(struct ubik_trans *ut, afs_int32 appendedDumpID,
             afs_int32 initialDumpID, afs_int32 startTapeSeq)
 {
     dbadr ada, da, lastDumpAddr;
@@ -2714,8 +2700,8 @@ makeAppended(struct ubik_trans *ut, afs_int32 appendedDumpID,
     /* Update the appended dump's tape format with that of the initial */
     strcpy(ad.tapes.format, d.tapes.format);
 
-    /* starting with the initial dump step through its appended dumps till 
-     * we reach the last appended dump. 
+    /* starting with the initial dump step through its appended dumps till
+     * we reach the last appended dump.
      */
     lastDumpAddr = da;
     while (d.appendedDumpChain) {
@@ -2751,7 +2737,7 @@ makeAppended(struct ubik_trans *ut, afs_int32 appendedDumpID,
 }
 
 afs_int32
-SBUDB_MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID, 
+SBUDB_MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID,
                       afs_int32 initialDumpID, afs_int32 startTapeSeq)
 {
     afs_int32 code;
@@ -2764,7 +2750,7 @@ SBUDB_MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID,
 }
 
 afs_int32
-MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID, 
+MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID,
                 afs_int32 initialDumpID, afs_int32 startTapeSeq)
 {
     struct ubik_trans *ut;
@@ -2791,9 +2777,9 @@ MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID,
 
 /* Find the last tape of a dump-set. This includes any appended dumps */
 afs_int32
-SBUDB_FindLastTape(struct rx_call *call, afs_int32 dumpID, 
-                  struct budb_dumpEntry *dumpEntry, 
-                  struct budb_tapeEntry *tapeEntry, 
+SBUDB_FindLastTape(struct rx_call *call, afs_int32 dumpID,
+                  struct budb_dumpEntry *dumpEntry,
+                  struct budb_tapeEntry *tapeEntry,
                   struct budb_volumeEntry *volEntry)
 {
     afs_int32 code;
@@ -2804,9 +2790,9 @@ SBUDB_FindLastTape(struct rx_call *call, afs_int32 dumpID,
 }
 
 afs_int32
-FindLastTape(struct rx_call *call, afs_int32 dumpID, 
-            struct budb_dumpEntry *dumpEntry, 
-            struct budb_tapeEntry *tapeEntry, 
+FindLastTape(struct rx_call *call, afs_int32 dumpID,
+            struct budb_dumpEntry *dumpEntry,
+            struct budb_tapeEntry *tapeEntry,
             struct budb_volumeEntry *volEntry)
 {
     struct ubik_trans *ut;
@@ -3402,6 +3388,9 @@ T_DumpHashTable(struct rx_call *call, int type, char *filename)
     afs_uint32 hash;
     dbadr a, first_a;
     char e[sizeof(struct block)];      /* unnecessarily conservative */
+    struct dump e_dump;
+    struct tape e_tape;
+    struct volInfo e_volinfo;
     int e_size;
     int old;
 
@@ -3445,16 +3434,20 @@ T_DumpHashTable(struct rx_call *call, int type, char *filename)
                    fprintf(DUMP, "    at %d is ", a);
                switch (type) {
                case HT_dumpIden_FUNCTION:
-                   fprintf(DUMP, "%d\n", ntohl(((struct dump *)e)->id));
+                   memcpy(&e_dump, e, sizeof(e_dump));
+                   fprintf(DUMP, "%d\n", ntohl(e_dump.id));
                    break;
                case HT_dumpName_FUNCTION:
-                   fprintf(DUMP, "%s\n", ((struct dump *)e)->dumpName);
+                   memcpy(&e_dump, e, sizeof(e_dump));
+                   fprintf(DUMP, "%s\n", e_dump.dumpName);
                    break;
                case HT_tapeName_FUNCTION:
-                   fprintf(DUMP, "%s\n", ((struct tape *)e)->name);
+                   memcpy(&e_tape, e, sizeof(e_tape));
+                   fprintf(DUMP, "%s\n", e_tape.name);
                    break;
                case HT_volName_FUNCTION:
-                   fprintf(DUMP, "%s\n", ((struct volInfo *)e)->name);
+                   memcpy(&e_volinfo, e, sizeof(e_volinfo));
+                   fprintf(DUMP, "%s\n", e_volinfo.name);
                    break;
                }
                if ((ht_HashEntry(mht, e) % length) != hash)
@@ -3537,12 +3530,10 @@ T_DumpDatabase(struct rx_call *call, char *filename)
     if (!callPermitted(call))
        return BUDB_NOTPERMITTED;
 
-    path = (char *)malloc(strlen(gettmpdir()) + 1 + strlen(filename) + 1);
+    asprintf(&path, "%s/%s", gettmpdir(), filename);
     if (!path)
        return (BUDB_INTERNALERROR);
 
-    sprintf(path, "%s/%s", gettmpdir(), filename);
-
     dumpfid = fopen(path, "w");
     if (!dumpfid)
        return (BUDB_BADARGUMENT);
@@ -3705,7 +3696,7 @@ volFragsDump(struct ubik_trans *ut, FILE *dumpfid, dbadr dbAddr)
  */
 
 void
-volFragmentDiskToHost(struct volFragment *diskVfPtr, 
+volFragmentDiskToHost(struct volFragment *diskVfPtr,
                      struct volFragment *hostVfPtr)
 {
     hostVfPtr->vol = ntohl(diskVfPtr->vol);