viced-malloc-dont-make-assumptions-about-the-compiler-20020524
[openafs.git] / src / viced / host.c
index 6f0c3f0..2748f2b 100644 (file)
@@ -534,12 +534,15 @@ void h_flushhostcps(hostaddr, hport)
     register afs_uint32  hostaddr, hport;  /* net byte order */
 {
     register struct host *host;
+    int held;
     
     H_LOCK
-    host = h_Lookup_r(hostaddr, hport);
+    host = h_Lookup_r(hostaddr, hport, &held);
     if (host) {
       host->hcpsfailed = 1;
     }
+    if (!held)
+      h_Release_r(host);
     H_UNLOCK
 
 return;
@@ -635,18 +638,21 @@ struct host *h_Alloc_r(r_con)
 
 
 /* Lookup a host given an IP address and UDP port number. */
-struct host *h_Lookup(hostaddr, hport)
+struct host *h_Lookup(hostaddr, hport, heldp)
     afs_uint32 hostaddr, hport;     /* network byte order */
+    int *heldp;
 {
     struct host *retVal;
     H_LOCK
-    retVal = h_Lookup_r(hostaddr, hport);
+    retVal = h_Lookup_r(hostaddr, hport, heldp);
     H_UNLOCK
     return retVal;
 }
 
-struct host *h_Lookup_r(hostaddr, hport)
+/* Note: host should be released by caller if 0 == *heldp and non-null */
+struct host *h_Lookup_r(hostaddr, hport, heldp)
     afs_uint32 hostaddr, hport;     /* network byte order */
+    int *heldp;
 {
     register afs_int32 now;
     register struct host *host=0;
@@ -654,17 +660,30 @@ struct host *h_Lookup_r(hostaddr, hport)
     register index = h_HashIndex(hostaddr);
     extern int hostaclRefresh;
 
+restart:
     for (chain=hostHashTable[index]; chain; chain=chain->next) {
        host = chain->hostPtr;
        assert(host);
         if (!(host->hostFlags & HOSTDELETED) && chain->addr == hostaddr
            && host->port == hport) {
+           *heldp = h_Held_r(host);
+           if (!*heldp)
+               h_Hold_r(host);
+           h_Lock_r(host);
+           if (host->hostFlags & HOSTDELETED) {
+               h_Unlock_r(host);
+               if (!*heldp)
+                   h_Release_r(host);
+               goto restart;
+           }
+           h_Unlock_r(host);
             now = FT_ApproxTime();             /* always evaluate "now" */
            if (host->hcpsfailed || (host->cpsCall+hostaclRefresh < now )) {
                /*
-                * Every hostaclRefresh period (def 2 hrs) get the new membership list for the host.
-                * Note this could be the first time that the host is added to a group.
-                * Also here we also retry on previous legitimate hcps failures
+                * Every hostaclRefresh period (def 2 hrs) get the new
+                * membership list for the host.  Note this could be the
+                * first time that the host is added to a group.  Also
+                * here we also retry on previous legitimate hcps failures.
                 */
                h_gethostcps_r(host,now);
            }
@@ -940,27 +959,27 @@ struct host *h_GetHost_r(tcon)
     afs_int32 haddr;
     afs_int32 hport;
     int i, j, count;
+    char hoststr[16], hoststr2[16];
 
     haddr = rxr_HostOf(tcon);
     hport = rxr_PortOf(tcon);
 retry:
     code = 0;
     identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
-    host = h_Lookup_r(haddr, hport);
+    host = h_Lookup_r(haddr, hport, &held);
     if (host && !identP && !(host->Console&1)) {
        /* This is a new connection, and we already have a host
         * structure for this address. Verify that the identity
         * of the caller matches the identity in the host structure.
         */
-       if (!(held = h_Held_r(host)))
-               h_Hold_r(host);
        h_Lock_r(host);
        if ( !(host->hostFlags & ALTADDR) )
        {
                /* Another thread is doing initialization */
                h_Unlock_r(host);
                if ( !held) h_Release_r(host);
-               ViceLog(125, ("Host %x starting h_Lookup again\n", host));
+               ViceLog(125, ("Host %s:%d starting h_Lookup again\n",
+                            afs_inet_ntoa_r(host->host, hoststr), host->port));
                goto retry;
        }
        host->hostFlags &= ~ALTADDR;
@@ -968,7 +987,7 @@ retry:
        code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
        H_LOCK
        if ( code == RXGEN_OPCODE ) {
-               identP = (struct Identity *)malloc(1);
+               identP = (struct Identity *)malloc(sizeof(struct Identity));
                identP->valid = 0;
                rx_SetSpecific(tcon, rxcon_ident_key, identP);
                /* The host on this connection was unable to respond to 
@@ -977,8 +996,8 @@ retry:
                 * that we maintain some extra callback state information */
                if (host->interface) {
                    ViceLog(0,
-                           ("Host %x used to support WhoAreYou, deleting.\n",
-                           host));
+                           ("Host %s:%d used to support WhoAreYou, deleting.\n",
+                           afs_inet_ntoa_r(host->host, hoststr), host->port));
                    host->hostFlags |= HOSTDELETED;
                    h_Unlock_r(host);
                    if (!held) h_Release_r(host);
@@ -997,8 +1016,8 @@ retry:
                if ( !host->interface
                  || !afs_uuid_equal(&interf.uuid, &host->interface->uuid) ) {
                    ViceLog(25,
-                           ("Host %x has changed its identity, deleting.\n",
-                           host));
+                           ("Host %s:%d has changed its identity, deleting.\n",
+                           afs_inet_ntoa_r(host->host, hoststr), host->port));
                    host->hostFlags |= HOSTDELETED;
                    h_Unlock_r(host);
                    if (!held) h_Release_r(host);
@@ -1006,7 +1025,6 @@ retry:
                    goto retry;
                }
        } else {
-           char hoststr[16];
            afs_inet_ntoa_r(host->host, hoststr);
            ViceLog(0,("CB: WhoAreYou failed for %s:%d, error %d\n", 
                       hoststr, ntohs(host->port), code));
@@ -1015,17 +1033,16 @@ retry:
        host->hostFlags |= ALTADDR;
        h_Unlock_r(host);
     } else if (host) {
-       if (!(held = h_Held_r(host)))
-               h_Hold_r(host);
        if ( ! (host->hostFlags & ALTADDR) ) 
        {
                /* another thread is doing the initialisation */
-               ViceLog(125, ("Host %x waiting for host-init to complete\n",
-                               host));
+               ViceLog(125, ("Host %s:%d waiting for host-init to complete\n",
+                            afs_inet_ntoa_r(host->host, hoststr), host->port));
                h_Lock_r(host);
                h_Unlock_r(host);
                if ( !held) h_Release_r(host);
-               ViceLog(125, ("Host %x starting h_Lookup again\n", host));
+               ViceLog(125, ("Host %s:%d starting h_Lookup again\n",
+                            afs_inet_ntoa_r(host->host, hoststr), host->port));
                goto retry;
        }
        /* We need to check whether the identity in the host structure
@@ -1040,8 +1057,8 @@ retry:
                host->hostFlags |= HOSTDELETED;
                h_Unlock_r(host);
                if (!held) h_Release_r(host);
-               ViceLog(0,("CB: new identity for host %x, deleting\n",
-                          host->host));
+               ViceLog(0, ("CB: new identity for host %s:%d, deleting\n",
+                          afs_inet_ntoa_r(host->host, hoststr), host->port));
                goto retry;
        }
     } else {
@@ -1055,12 +1072,12 @@ retry:
                code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
                H_LOCK
                if ( code == RXGEN_OPCODE ) {
-                   identP = (struct Identity *)malloc(1);
+                 identP = (struct Identity *)malloc(sizeof(struct Identity));
                    identP->valid = 0;
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                    ViceLog(25,
-                           ("Host %x does not support WhoAreYou.\n",
-                           host->host));
+                           ("Host %s:%d does not support WhoAreYou.\n",
+                           afs_inet_ntoa_r(host->host, hoststr), host->port));
                    code = 0;
                } else if (code == 0) {
                    interfValid = 1;
@@ -1068,7 +1085,8 @@ retry:
                    identP->valid = 1;
                    identP->uuid = interf.uuid;
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
-                   ViceLog(25,("WhoAreYou success on %x\n", host->host));
+                   ViceLog(25, ("WhoAreYou success on %s:%d\n",
+                               afs_inet_ntoa_r(host->host, hoststr), host->port));
                }
            }
            if (code == 0 && !identP->valid) {
@@ -1084,8 +1102,9 @@ retry:
                    if (!(held = h_Held_r(oldHost)))
                        h_Hold_r(oldHost);
                    h_Lock_r(oldHost);
-                   ViceLog(25,("CB: new addr %x for old host %x\n",
-                               host->host, oldHost->host));
+                   ViceLog(25, ("CB: new addr %s:%d for old host %s:%d\n",
+                               afs_inet_ntoa_r(host->host, hoststr), host->port,
+                               afs_inet_ntoa_r(oldHost->host, hoststr2), oldHost->port));
                    host->hostFlags |= HOSTDELETED;
                    h_Unlock_r(host);
                    h_Release_r(host);
@@ -1099,15 +1118,14 @@ retry:
                                                      &FS_HostUUID);
                    H_LOCK
                    if (code == 0) {
-                       ViceLog(25,("InitCallBackState3 success on %x\n",
-                               host->host));
+                       ViceLog(25, ("InitCallBackState3 success on %s:%d\n",
+                                   afs_inet_ntoa_r(host->host, hoststr), host->port));
                        assert(interfValid == 1);
                        initInterfaceAddr_r(host, &interf);
                    }
                }
           }
           if (code) {
-               char hoststr[16];
                afs_inet_ntoa_r(host->host, hoststr);
                ViceLog(0,("CB: RCallBackConnectBack failed for %s:%d\n", 
                          hoststr, ntohs(host->port)));
@@ -1390,8 +1408,11 @@ ticket name length != 64
          code = pr_GetCPS(viceid, &client->CPS);
          H_LOCK
          if (code) {
-           ViceLog(0, ("pr_GetCPS failed(%d) for user %d, host %x.%d\n",
-                   code, viceid, client->host->host, ntohs(client->host->port)));
+           char hoststr[16];
+           ViceLog(0, ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
+                      code, viceid,
+                      afs_inet_ntoa_r(client->host->host, hoststr),
+                      client->host->port));
 
            /* Although ubik_Call (called by pr_GetCPS) traverses thru
             * all protection servers and reevaluates things if no
@@ -1464,8 +1485,11 @@ int GetClient(tcon, cp)
     assert(client && client->tcon && rxr_CidOf(client->tcon) == client->sid);
     if (client &&
        client->LastCall > client->expTime && client->expTime) {
-       ViceLog(1, ("Token for %s at %x.%d expired %d\n",
-               h_UserName(client), client->host->host, client->host->port, client->expTime));
+       char hoststr[16];
+       ViceLog(1, ("Token for %s at %s:%d expired %d\n",
+               h_UserName(client),
+               afs_inet_ntoa_r(client->host->host, hoststr),
+               client->host->port, client->expTime));
        H_UNLOCK
        return VICETOKENDEAD;
     }
@@ -1522,14 +1546,16 @@ static int h_PrintClient(host, held, file)
     int i;
     char tmpStr[256];
     char tbuffer[32];
+    char hoststr[16];
 
     H_LOCK
     if (host->hostFlags & HOSTDELETED) {
        H_UNLOCK
        return held;
     }
-    sprintf(tmpStr,"Host %x.%d down = %d, LastCall %s", host->host,
-           host->port, (host->hostFlags & VENUSDOWN),
+    sprintf(tmpStr,"Host %s:%d down = %d, LastCall %s",
+           afs_inet_ntoa_r(host->host, hoststr), host->port,
+           (host->hostFlags & VENUSDOWN),
            afs_ctime((time_t *)&host->LastCall, tbuffer, sizeof(tbuffer)));
     STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
     for (client = host->FirstClient; client; client=client->next) {