Log binding ip address and port during startup
[openafs.git] / src / vlserver / cnvldb.c
index 0ba1fde..ca42080 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
@@ -9,16 +9,12 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
+#include <roken.h>
 
-#include <afs/stds.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <stdio.h>
 #include <sys/file.h>
-#include <string.h>
 
-#include <netinet/in.h>
 #include <afs/venus.h>
 #include <afs/cmd.h>
 #include <afs/afsutil.h>
@@ -27,7 +23,6 @@
 #include "vlserver.h"
 #include "cnvldb.h"            /* CHANGEME! */
 
-#define MAXSIZE 2048           /* most I'll get back from PIOCTL */
 #define        BADSERVERID     255     /* XXX */
 
 
@@ -35,7 +30,7 @@ static char pn[] = "cnvldb";
 static char tempname[] = "XXnewvldb";
 static int MaxServers[3] = { 30, 254, 254 };   /* max server # permitted in this version */
 
-static afs_int32 Conv4to3(afs_int32 addr);
+static afs_int32 Conv4to3(afs_uint32 addr);
 
 static void convert_vlentry(int, int, int, struct vlheader_1 *,
                            struct vlheader_1 *, struct vlentry_1 *);
@@ -56,7 +51,7 @@ static int convert_header(int ofd, int fd, int fromv, int tov, void *fromaddr,
 static char *
 Parent(const char *apath)
 {
-    register char *tp;
+    char *tp;
     strcpy(tspace, apath);
     tp = strrchr(tspace, '/');
     if (tp) {
@@ -77,6 +72,8 @@ static int
 handleit(struct cmd_syndesc *as, void *arock)
 {
     int w, old, new, rc, dump = 0, fromv = 0;
+    ssize_t count;
+
     char ubik[80];             /* space for some ubik header */
     union {
        struct vlheader_1 header1;
@@ -106,18 +103,38 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
 
     /* Read the version */
-    lseek(old, 64, L_SET);
-    read(old, &fromv, sizeof(int));
+    if (lseek(old, 64, L_SET) == (off_t)-1) {
+       perror(pn);
+       exit(-1);
+    }
+    count = read(old, &fromv, sizeof(int));
+    if (count < 0) {
+       perror(pn);
+       exit(-1);
+    } else if (count != sizeof(int)) {
+       fprintf(stderr, "%s: Premature EOF reading database version.\n", pn);
+       exit(-1);
+    }
     fromv = ntohl(fromv);
     if ((fromv < 1) || (fromv > 4)) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Unrecognized VLDB version %d.\n", fromv);
        exit(-1);
     }
 
     /* Sequentially read the database converting the entries as we go */
-    lseek(old, 0, L_SET);
-    read(old, ubik, 64);
+    if (lseek(old, 0, L_SET) == (off_t)-1) {
+       perror(pn);
+       exit(-1);
+    }
+    count = read(old, ubik, 64);
+    if (count < 0) {
+       perror(pn);
+       exit(-1);
+    } else if (count != 64) {
+       fprintf(stderr, "%s: Premature EOF reading database header.\n", pn);
+       exit(-1);
+    }
     readheader(old, fromv, &oldheader);
     if (fromv == 1) {
        dbsize = ntohl(oldheader.header1.vital_header.eofPtr);
@@ -167,9 +184,9 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
 
     if ((fromvers < 1) || (fromvers > 4)) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": VLDB version %d is not supported.\n", fromvers);
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Only versions 1-4 are currently supported.\n");
        exit(-1);
     }
@@ -178,15 +195,15 @@ handleit(struct cmd_syndesc *as, void *arock)
        tovers = fromvers + 1;
 
     if (tovers < 1 || tovers > 4) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": VLDB version %d is not supported.\n", tovers);
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Only versions 1 - 4 are currently supported.\n");
        exit(-1);
     }
 
     if (mhaddr && (tovers < 3)) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Cannot convert. VLDB contains multihome info.\n");
        exit(-1);
     }
@@ -211,7 +228,7 @@ handleit(struct cmd_syndesc *as, void *arock)
        exit(1);
     }
 
-    /* Because we know that all the vldb entries are the same size and type we 
+    /* Because we know that all the vldb entries are the same size and type we
      * can just read them sequentially, fiddle with the fields, and write
      * them out again.  If we invent a vldb format that has different
      * types of entries, then we're going to have to invent new logic for
@@ -244,7 +261,7 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
     close(new);
 
-    renamefile(tempname, pathname);
+    rk_rename(tempname, pathname);
     sleep(5);
     exit(0);
 }
@@ -334,7 +351,7 @@ printentry(int version, void *addr)
 
        if (vl3p->flags == VLFREE)
            return;
-       printf("%s\tPos=%lu NextIdHash=[%d:%d:%d] NextNameHash=%d\n",
+       printf("%s\tPos=%" AFS_SIZET_FMT " NextIdHash=[%d:%d:%d] NextNameHash=%d\n",
               vl3p->name, (oldpos - sizeof(struct vlentry_3)),
               vl3p->nextIdHash[0], vl3p->nextIdHash[1], vl3p->nextIdHash[2],
               vl3p->nextNameHash);
@@ -378,7 +395,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
     if (!mh_addr)
        return;
 
-    /* Check if the first extent block is beyond eof. If 
+    /* Check if the first extent block is beyond eof. If
      * it is, it's not real.
      */
     if (mh_addr > dbsize - VL_ADDREXTBLK_SIZE)
@@ -390,7 +407,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
        perror("seek MH block");
        exit(1);
     }
-    base[0] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
+    base[0] = malloc(VL_ADDREXTBLK_SIZE);
     if (!base[0]) {
        perror("malloc1");
        exit(1);
@@ -404,7 +421,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
     }
 
     /* Verify that this block is the right one */
-    if (ntohl(base[0]->ex_flags) != VLCONTBLOCK) {     /* check if flag is correct */
+    if (ntohl(base[0]->ex_hdrflags) != VLCONTBLOCK) {  /* check if flag is correct */
        free(base[0]);
        base[0] = 0;
        return;
@@ -420,7 +437,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
 
        sit = ntohl(base[0]->ex_contaddrs[j]);
 
-       /* Every time we allocate a new extent block, it is allocated after 
+       /* Every time we allocate a new extent block, it is allocated after
         * the previous ones. But it must be before the EOF.
         */
        if ((sit < (a + VL_ADDREXTBLK_SIZE))
@@ -435,7 +452,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
            perror("seek MH block");
            exit(1);
        }
-       base[j] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
+       base[j] = malloc(VL_ADDREXTBLK_SIZE);
        if (!base[j]) {
            perror("malloc1");
            exit(1);
@@ -447,7 +464,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
        }
 
        /* Verify that this block knows its an extent block */
-       if (ntohl(base[j]->ex_flags) != VLCONTBLOCK) {
+       if (ntohl(base[j]->ex_hdrflags) != VLCONTBLOCK) {
            free(base[j]);
            base[j] = 0;
            continue;
@@ -462,7 +479,7 @@ read_mhentries(afs_uint32 mh_addr, int oldfd)
  * extent blocks and verify that the pointers are good. And fix.
  * Then convert the multihomed addresses to single address if we
  * are converting back from version 4.
- * 
+ *
  * Before this can be called, the routine read_mhentries must be called.
  */
 void
@@ -526,7 +543,7 @@ convert_mhentries(int oldfd, int newfd, struct vlheader_2 *header,
        }
     }
 
-    /* If we are converting from version 4 to version 3, then 
+    /* If we are converting from version 4 to version 3, then
      * translate any multihome ptrs in the IpMappedAddr array
      * to true IP addresses.
      */
@@ -540,7 +557,7 @@ convert_mhentries(int oldfd, int newfd, struct vlheader_2 *header,
                basei = (addr >> 16) & 0xff;
                index = addr & 0xffff;
 
-               if ((basei >= VL_ADDREXTBLK_SIZE) || !base[basei]) {
+               if ((basei >= VL_MAX_ADDREXTBLKS) || !base[basei]) {
                    fprintf(stderr,
                            "Warning: mh entry %d has no IP address; ignored!!\n",
                            i);
@@ -735,7 +752,7 @@ convert_header(int ofd, int fd, int fromv, int tov, void *fromaddr,
 
 /* Convert an address pointer to a vlentry from version 4 to version 3.
  * This involves checking if the address is after any of the four
- * MH block and if it is, subtract the size of the MH block. 
+ * MH block and if it is, subtract the size of the MH block.
  *
  * In going from version 4 to 3, the mh blocks go away and all entries
  * move up in their place. The adresses then need to be updated.
@@ -743,9 +760,9 @@ convert_header(int ofd, int fd, int fromv, int tov, void *fromaddr,
  * Before this can be called, the routine read_mhentries must be called.
  */
 static afs_int32
-Conv4to3(afs_int32 addr)
+Conv4to3(afs_uint32 addr)
 {
-    afs_int32 raddr;
+    afs_uint32 raddr;
     int i;
 
     if (!base[0] || !addr)
@@ -761,7 +778,7 @@ Conv4to3(afs_int32 addr)
     return (raddr);
 }
 
-/* this only works because the vlheader struct is essentially the same 
+/* this only works because the vlheader struct is essentially the same
  * from version 1 to version 2 -- that is, the first bunch of fields
  * aren't any more or any larger, so they match up pretty well.
 */
@@ -824,7 +841,7 @@ convert_vlentry(int new, int fromvers, int tovers,
        struct vlentry_2 vl;
        struct vlentry_3 *xnvlentry = (struct vlentry_3 *)vlentryp;
 
-       memset((char *)&vl, 0, sizeof(struct vlentry_2));
+       memset(&vl, 0, sizeof(struct vlentry_2));
        vl.volumeId[0] = xnvlentry->volumeId[0];
        vl.volumeId[1] = xnvlentry->volumeId[1];
        vl.volumeId[2] = xnvlentry->volumeId[2];
@@ -859,7 +876,7 @@ convert_vlentry(int new, int fromvers, int tovers,
             1 ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2))
            - (fromvers ==
               1 ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2));
-       memset((char *)&vl, 0, sizeof(struct vlentry_1));
+       memset(&vl, 0, sizeof(struct vlentry_1));
        vl.volumeId[0] = xnvlentry->volumeId[0];
        vl.volumeId[1] = xnvlentry->volumeId[1];
        vl.volumeId[2] = xnvlentry->volumeId[2];
@@ -980,10 +997,10 @@ rewrite_header(int new, int tovers, void *newheader)
 int
 main(int argc, char **argv)
 {
-    register struct cmd_syndesc *ts;
+    struct cmd_syndesc *ts;
     afs_int32 code;
 
-    ts = cmd_CreateSyntax("initcmd", handleit, NULL, "optional");
+    ts = cmd_CreateSyntax("initcmd", handleit, NULL, 0, "optional");
     cmd_AddParm(ts, "-to", CMD_SINGLE, CMD_OPTIONAL, "goal version");
     cmd_AddParm(ts, "-from", CMD_SINGLE, CMD_OPTIONAL, "current version");
     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "pathname");