fsprobe: add fsprobe_Wait function
[openafs.git] / src / fsprobe / fsprobe.c
index a308af2..8ddb320 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>
 
-
-#include <string.h>
-
-#include "fsprobe.h"           /*Interface for this module */
 #include <lwp.h>               /*Lightweight process package */
 #include <afs/cellconfig.h>
 #include <afs/afsint.h>
 #include <afs/afsutil.h>
 #include <afs/volser.h>
 #include <afs/volser_prototypes.h>
-
 #define FSINT_COMMON_XG
 #include <afs/afscbint.h>
 
+#include "fsprobe.h"           /*Interface for this module */
+
 #define LWP_STACK_SIZE (16 * 1024)
 
 /*
@@ -51,14 +49,6 @@ static PROCESS probeLWP_ID;  /*Probe LWP process ID */
 static int fsprobe_statsBytes; /*Num bytes in stats block */
 static int fsprobe_probeOKBytes;       /*Num bytes in probeOK block */
 
-/*
- * We have to pass a port to Rx to start up our callback listener
- * service, but 7001 is already taken up by the Cache Manager.  So,
- * we make up our own.
- */
-#define FSPROBE_CBPORT 7101
-
-
 /*------------------------------------------------------------------------
  * [private] fsprobe_CleanupInit
  *
@@ -233,8 +223,8 @@ fsprobe_LWP(void *unused)
     struct ProbeViceStatistics *curr_stats;    /*Current stats region */
     int *curr_probeOK;         /*Current probeOK field */
     ViceStatistics64 stats64;      /*Current stats region */
-    stats64.ViceStatistics64_val = (afs_uint64 *)malloc(STATS64_VERSION *
-                                                       sizeof(afs_uint64));
+    stats64.ViceStatistics64_val = malloc(STATS64_VERSION *
+                                         sizeof(afs_uint64));
     while (1) {                        /*Service loop */
        /*
         * Iterate through the server connections, gathering data.
@@ -293,7 +283,7 @@ fsprobe_LWP(void *unused)
                char pname[10];
                struct diskPartition partition;
                struct diskPartition64 *partition64p =
-                   (struct diskPartition64 *)malloc(sizeof(struct diskPartition64));
+                   malloc(sizeof(struct diskPartition64));
 
                if (fsprobe_debug)
                    fprintf(stderr,
@@ -315,8 +305,8 @@ fsprobe_LWP(void *unused)
                            strcpy(curr_stats->Disk[i].Name, pname);
                        }
                        if (code == RXGEN_OPCODE) {
-                           code = 
-                               AFSVolPartitionInfo(curr_conn->rxVolconn, 
+                           code =
+                               AFSVolPartitionInfo(curr_conn->rxVolconn,
                                                    pname, &partition);
                            if (!code) {
                                curr_stats->Disk[i].BlocksAvailable =
@@ -377,9 +367,8 @@ fsprobe_LWP(void *unused)
        if (code)
            fprintf(stderr, "[%s] IOMGR_Select returned code %d\n", rn, code);
     }                          /*Service loop */
-    free(stats64.ViceStatistics64_val);
-
-    return NULL;
+    AFS_UNREACHED(free(stats64.ViceStatistics64_val));
+    AFS_UNREACHED(return(NULL));
 }                              /*fsprobe_LWP */
 
 /*list all the partitions on <aserver> */
@@ -476,7 +465,7 @@ XListPartitions(struct rx_connection *aconn, struct partList *ptrPartList,
  *------------------------------------------------------------------------*/
 
 int
-fsprobe_Init(int a_numServers, struct sockaddr_in *a_socketArray, 
+fsprobe_Init(int a_numServers, struct sockaddr_in *a_socketArray,
             int a_ProbeFreqInSecs, int (*a_ProbeHandler)(void),
             int a_debug)
 {                              /*fsprobe_Init */
@@ -491,7 +480,6 @@ fsprobe_Init(int a_numServers, struct sockaddr_in *a_socketArray,
     struct fsprobe_ConnectionInfo *curr_conn;  /*Ptr to current conn */
     char *hostNameFound;       /*Ptr to returned host name */
     int conn_err;              /*Connection error? */
-    int PortToUse;             /*Callback port to use */
 
     /*
      * If we've already been called, snicker at the bozo, gently
@@ -575,7 +563,7 @@ fsprobe_Init(int a_numServers, struct sockaddr_in *a_socketArray,
                rn, fsprobe_statsBytes);
 
     fsprobe_probeOKBytes = a_numServers * sizeof(int);
-    fsprobe_Results.probeOK = (int *)malloc(fsprobe_probeOKBytes);
+    fsprobe_Results.probeOK = malloc(fsprobe_probeOKBytes);
     if (fsprobe_Results.probeOK == (int *)0) {
        fprintf(stderr,
                "[%s] Can't allocate %d probeOK array entries (%d bytes)\n",
@@ -596,24 +584,13 @@ fsprobe_Init(int a_numServers, struct sockaddr_in *a_socketArray,
      */
     if (fsprobe_debug)
        fprintf(stderr, "[%s] Initializing Rx\n", rn);
-    PortToUse = FSPROBE_CBPORT;
-    do {
-       code = rx_Init(htons(PortToUse));
-       if (code) {
-           if (code == RX_ADDRINUSE) {
-               if (fsprobe_debug)
-                   fprintf(stderr,
-                           "[%s] Callback port %d in use, advancing\n", rn,
-                           PortToUse);
-               PortToUse++;
-           } else {
-               fprintf(stderr, "[%s] Fatal error in rx_Init()\n", rn);
-               return (-1);
-           }
-       }
-    } while (code);
+    code = rx_Init(0);
+    if (code) {
+       fprintf(stderr, "[%s] Fatal error in rx_Init()\n", rn);
+       return (-1);
+    }
     if (fsprobe_debug)
-       fprintf(stderr, "[%s] Rx initialized on port %d\n", rn, PortToUse);
+       fprintf(stderr, "[%s] Rx initialized.\n", rn);
 
     /*
      * Create a null Rx server security object, to be used by the
@@ -854,3 +831,44 @@ fsprobe_ForceProbeNow(void)
     return (0);
 
 }                              /*fsprobe_ForceProbeNow */
+
+/*------------------------------------------------------------------------
+ * [exported] fsprobe_Wait
+ *
+ * Description:
+ *     Wait for the collection to complete.
+ *
+ * Arguments:
+ *    int sleep_secs : time to wait in seconds. 0 means sleep forever.
+ *
+ * Returns:
+ *     0 on success,
+ *     Error value otherwise.
+ *
+ * Environment:
+ *     The module must have been initialized.
+ *
+ * Side Effects:
+ *     As advertised.
+ *------------------------------------------------------------------------*/
+int
+fsprobe_Wait(int sleep_secs)
+{
+    int code;
+    struct timeval tv;
+
+    if (sleep_secs == 0) {
+       while (1) {
+           tv.tv_sec = 30;
+           tv.tv_usec = 0;
+           code = IOMGR_Select(0, 0, 0, 0, &tv);
+           if (code != 0)
+               break;
+       }
+    } else {
+       tv.tv_sec = sleep_secs;
+       tv.tv_usec = 0;
+       code = IOMGR_Select(0, 0, 0, 0, &tv);
+    }
+    return code;
+}