Log binding ip address and port during startup
[openafs.git] / src / vlserver / vlclient.c
index 3614362..e22af50 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,34 +9,16 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
+#include <roken.h>
 
-#include <afs/stds.h>
-#include <sys/types.h>
-#ifdef HAVE_TIME_H
-#include <time.h>
-#endif
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
 #endif
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
 #endif
-#ifdef HAVE_NETDB_H
-#include <netdb.h>
-#endif
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-#include <stdio.h>
-#include <string.h>
 #include <ctype.h>
 
 #include <afs/afsutil.h>
@@ -49,6 +31,7 @@
 #include <afs/cmd.h>
 #include <lock.h>
 #include <ubik.h>
+
 #include "vlserver.h"
 
 void fill_listattributes_entry(struct VldbListByAttributes *, char **, int);
@@ -77,7 +60,7 @@ static char *opcode_names[VL_NUMBER_OPCODESX] = {
     "ListAttributes",
     "LinkedList",
     "GetStats",
-    "Probe",
+    "Probe",   /* RPC is ProbeServer */
     "GetAddrs",
     "ChangeAddr",
     "CreateEntryN",
@@ -151,12 +134,11 @@ GetVolume(int vol, struct vldbentry *entry)
 
 /* Almost identical's to pr_Initialize in vlserver/pruser.c */
 afs_int32
-vl_Initialize(int auth, char *confDir, int server, char *cellp)
+vl_Initialize(char *confDir, int secFlags, int server, char *cellp)
 {
-    return ugen_ClientInit(auth?0:1, confDir, cellp, 0,
-                         &cstruct, NULL, "vl_Initialize", rxkad_clear, 
-                         MAXSERVERS, AFSCONF_VLDBSERVICE, 50, server,
-                         htons(AFSCONF_VLDBPORT), USER_SERVICE_ID);
+    return ugen_ClientInitServer(confDir, cellp, secFlags, &cstruct,
+                                MAXSERVERS, AFSCONF_VLDBSERVICE, 90,
+                                server, htons(AFSCONF_VLDBPORT));
 }
 
 /* return host address in network byte order */
@@ -191,14 +173,14 @@ handleit(struct cmd_syndesc *as, void *arock)
     char *cellp = 0;
     struct VldbUpdateEntry updateentry;
     struct VldbListByAttributes listbyattributes;
-    int noAuth = 1;            /* Default is authenticated connections */
+    int secFlags = AFSCONF_SECOPTS_FALLBACK_NULL;
 
     if ((ti = as->parms[0].items))     /* -cellpath <dir> */
        strcpy(confdir, ti->data);
     if (as->parms[1].items)    /* -server */
        strcpy(confdir, AFSDIR_SERVER_ETC_DIRPATH);
     if (as->parms[2].items)    /* -noauth */
-       noAuth = 0;
+       secFlags |= AFSCONF_SECOPTS_NOAUTH;
     if ((ti = as->parms[3].items)) {   /* -host */
        server = GetServer(ti->data);
        if (server == 0) {
@@ -207,7 +189,7 @@ handleit(struct cmd_syndesc *as, void *arock)
        }
        sawserver = 1;
     }
-    if (!sawserver && noAuth && (!(ti = as->parms[4].items))) {
+    if (sawserver && !as->parms[2].items && (!(ti = as->parms[4].items))) {
        printf
            ("Must also specify the -cell' option along with -host for authenticated conns\n");
        exit(1);
@@ -215,12 +197,47 @@ handleit(struct cmd_syndesc *as, void *arock)
     if ((ti = as->parms[4].items)) {   /* -cell */
        cellp = ti->data;
     }
-    if ((code = vl_Initialize(noAuth, confdir, server, cellp))) {
+    if ((code = vl_Initialize(confdir, secFlags, server, cellp))) {
        printf("Couldn't initialize vldb library (code=%d).\n", code);
        exit(1);
     }
 
-    if (as->parms[5].items) {  /* -gstats */
+    if (as->parms[6].items) {  /* -probe */
+       int nconns = 0;
+       int status = 0;
+       int i;
+       char hoststr[16];
+       afs_uint32 addr;
+
+       for (i = 0; cstruct->conns[i]; i++, nconns++) {
+           rx_SetConnDeadTime(cstruct->conns[i], 6);
+           addr = rx_HostOf(rx_PeerOf(cstruct->conns[i]));
+           if (!nconns) {
+               printf("probing");
+           }
+           printf(" %s", afs_inet_ntoa_r(addr, hoststr));
+       }
+       if (nconns == 0) {
+           printf("no connections\n");
+           return 255;
+       }
+       printf("\n");
+       multi_Rx(cstruct->conns, nconns) {
+           multi_VL_ProbeServer();
+           addr = rx_HostOf(rx_PeerOf(cstruct->conns[multi_i]));
+           if (!multi_error) {
+               printf(" ok: %s\n", afs_inet_ntoa_r(addr, hoststr));
+           } else {
+               status = 255;
+               printf(" not ok (%d): %s\n", multi_error,
+                      afs_inet_ntoa_r(addr, hoststr));
+           }
+       }
+       multi_End;
+       return status;
+    }
+
+    if (as->parms[5].items) {  /* -getstats */
        vldstats stats;
        vital_vlheader vital_header;
        code = ubik_VL_GetStats(cstruct, 0, &stats, &vital_header);
@@ -304,8 +321,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                afs_int32 index, count, num = 0, num1 = 0, next_index;
                struct Vlent *vl1;
 
-               VL = SVL =
-                   (struct Vlent *)malloc(ALLOCNT * sizeof(struct Vlent));
+               VL = SVL = malloc(ALLOCNT * sizeof(struct Vlent));
                if (VL == NULL) {
                    printf("Can't allocate memory...\n");
                    exit(1);
@@ -353,8 +369,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                    0, num31 = 0, num4 = 0, num41 = 0, next_index;
                struct vldbentry tentry;
 
-               VL = SVL =
-                   (struct Vlent *)malloc(ALLOCNT * sizeof(struct Vlent));
+               VL = SVL = malloc(ALLOCNT * sizeof(struct Vlent));
                if (VL == NULL) {
                    printf("Can't allocate memory...\n");
                    exit(1);
@@ -426,8 +441,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                    0;
                struct vldbentry tentry;
 
-               VL = SVL =
-                   (struct Vlent *)malloc(ALLOCNT * sizeof(struct Vlent));
+               VL = SVL = malloc(ALLOCNT * sizeof(struct Vlent));
                if (VL == NULL) {
                    printf("Can't allocate memory...\n");
                    exit(1);
@@ -436,8 +450,6 @@ handleit(struct cmd_syndesc *as, void *arock)
                    ("Volumes not found in main hash tables in vldb will be fixed...\n");
                memset(&updateentry, 0, sizeof(updateentry));
                for (index = 0; 1; index = next_index) {
-                   /* FIXME: n2 is never changed for some reason */
-                   int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
                    memset(&entry, 0, sizeof(entry));
                    code =
                        ubik_VL_ListEntry(cstruct, 0, index, &count,
@@ -454,7 +466,6 @@ handleit(struct cmd_syndesc *as, void *arock)
                                  &tentry);
                    if (code == VL_NOENT) {
                        num1++;
-                       n1 = 1;
                        updateentry.Mask = VLUPDATE_VOLNAMEHASH;
                        printf("\tVolume %s %d (not in namehash)\n",
                               entry.name, entry.volumeId[RWVOL]);
@@ -488,13 +499,12 @@ handleit(struct cmd_syndesc *as, void *arock)
                        }
                        x++;
                    }
-                   if (entry.volumeId[BACKVOL] && !n2) {
+                   if (entry.volumeId[BACKVOL]) {
                        code =
                            ubik_VL_GetEntryByID(cstruct, 0,
                                      entry.volumeId[BACKVOL], BACKVOL,
                                      &tentry);
                        if (code == VL_NOENT) {
-                           n3 = 1;
                            num1++;
                            updateentry.Mask = VLUPDATE_BACKUPID;
                            updateentry.BackupId = entry.volumeId[BACKVOL];
@@ -512,12 +522,11 @@ handleit(struct cmd_syndesc *as, void *arock)
                            }
                        }
                    }
-                   if (entry.volumeId[ROVOL && !n2]) {
+                   if (entry.volumeId[ROVOL]) {
                        code =
                            ubik_VL_GetEntryByID(cstruct, 0,
                                      entry.volumeId[ROVOL], ROVOL, &tentry);
                        if (code == VL_NOENT) {
-                           n4 = 1;
                            num1++;
                            updateentry.Mask = VLUPDATE_READONLYID;
                            updateentry.ReadOnlyId = entry.volumeId[ROVOL];
@@ -558,7 +567,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                for (i = 0; i < nentries; i++, entry++)
                    display_entry(entry, 0);
                if (entries.bulkentries_val)
-                   free((char *)entries.bulkentries_val);
+                   free(entries.bulkentries_val);
            } else if (!strcmp(oper, "lan2")) {
                int nentries, i, si, nsi, t = 0;
                nbulkentries entries;
@@ -595,7 +604,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                    for (i = 0; i < nentries; i++, entry++)
                        display_entryN(entry, 0);
                    if (entries.nbulkentries_val)
-                       free((char *)entries.nbulkentries_val);
+                       free(entries.nbulkentries_val);
                }
                printf("--- %d volumes ---\n", t);
            } else if (!strcmp(oper, "ln")) {
@@ -640,7 +649,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                for (vllist = linkedvldbs.node; vllist; vllist = vllist1) {
                    vllist1 = vllist->next_vldb;
                    display_entry((struct vldbentry *)&vllist->VldbEntry, 0);
-                   free((char *)vllist);
+                   free(vllist);
                }
            } else if (!strcmp(oper, "di")) {
                sscanf(&(*argp)[0], "%d", &id);
@@ -766,7 +775,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                        printf("[0x%x %u] %s\n", *addrp, *addrp,
                               hostutil_GetNameByINet(ntohl(*addrp)));
                }
-               free((char *)addrs.bulkaddrs_val);
+               free(addrs.bulkaddrs_val);
            } else if (!strcmp(oper, "gau")) {
                int nentries, i, j;
                afs_uint32 *addrp;
@@ -794,6 +803,7 @@ handleit(struct cmd_syndesc *as, void *arock)
 
                        printf("[0x%x %u] (special multi-homed entry)\n",
                               *addrp, *addrp);
+                       memset(&attrs, 0, sizeof(attrs));
                        attrs.Mask = VLADDR_INDEX;
                        mhaddrs.bulkaddrs_val = 0;
                        mhaddrs.bulkaddrs_len = 0;
@@ -822,13 +832,13 @@ handleit(struct cmd_syndesc *as, void *arock)
                                   hostutil_GetNameByINet(mhaddrp[j]));
                        }
                        if (mhaddrs.bulkaddrs_val)
-                           free((char *)mhaddrs.bulkaddrs_val);
+                           free(mhaddrs.bulkaddrs_val);
                    } else {
                        printf("[0x%x %u] %s\n", *addrp, *addrp,
                               hostutil_GetNameByINet(ntohl(*addrp)));
                    }
                }
-               free((char *)addrs.bulkaddrs_val);
+               free(addrs.bulkaddrs_val);
            } else if (!strcmp(oper, "mhc")) {
                afs_uint32 serveraddrs[MAXSERVERID + 1][VL_MAXIPADDRS_PERMH];
                afs_int32 serveraddrtype[MAXSERVERID + 1];
@@ -869,6 +879,7 @@ handleit(struct cmd_syndesc *as, void *arock)
 
                        addrs2.bulkaddrs_val = 0;
                        addrs2.bulkaddrs_len = 0;
+                       memset(&attrs, 0, sizeof(attrs));
                        attrs.Mask = VLADDR_INDEX;
                        attrs.index = (base * VL_MHSRV_PERBLK) + index;
                        code =
@@ -883,7 +894,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                        for (j = 0; j < nentries2; j++) {
                            serveraddrs[i][j] = ntohl(addrp2[j]);
                        }
-                       free((char *)addrs2.bulkaddrs_val);
+                       free(addrs2.bulkaddrs_val);
                    }
 
                    if (nargs) {
@@ -898,7 +909,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                        }
                    }
                }
-               free((char *)addrs1.bulkaddrs_val);
+               free(addrs1.bulkaddrs_val);
 
                /* Look for any duplicates */
                for (i = 0; i < MAXSERVERID + 1; i++) {
@@ -947,7 +958,7 @@ handleit(struct cmd_syndesc *as, void *arock)
                    printf("Illegal # entries = %d\n", nargs);
                    continue;
                }
-               addrp = (afs_uint32 *) malloc(20 * 4);
+               addrp = malloc(20 * 4);
                addrs.bulkaddrs_val = addrp;
                addrs.bulkaddrs_len = nargs;
                while (nargs > 0) {
@@ -1028,7 +1039,7 @@ main(int argc, char **argv)
     afs_int32 code;
 
     strcpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH);
-    ts = cmd_CreateSyntax("initcmd", handleit, NULL, "initialize the program");
+    ts = cmd_CreateSyntax("initcmd", handleit, NULL, 0, "initialize the program");
     cmd_AddParm(ts, "-cellpath", CMD_LIST, CMD_OPTIONAL,
                "Cell configuration directory");
     cmd_AddParm(ts, "-server", CMD_LIST, CMD_OPTIONAL,
@@ -1041,6 +1052,8 @@ main(int argc, char **argv)
                "cellname '-host' belongs to (required for auth conns)");
     cmd_AddParm(ts, "-getstats", CMD_FLAG, CMD_OPTIONAL,
                "print vldb statistics (non interactive)");
+    cmd_AddParm(ts, "-probe", CMD_FLAG, CMD_OPTIONAL,
+               "probe vldb servers, use with -host to probe only one. (non interactive)");
     code = cmd_Dispatch(argc, argv);
     exit(code);
 }
@@ -1296,9 +1309,10 @@ dump_stats(vldstats *stats, vital_vlheader *vital_header)
     int i;
     char strg[30];
     time_t start_time = stats->start_time;
+    struct tm tm;
 
-    afs_ctime(&start_time, strg, sizeof(strg));
-    strg[strlen(strg) - 1] = 0;
+    strftime(strg, sizeof(strg), "%a %b %d %H:%M:%S %Y",
+            localtime_r(&start_time, &tm));
     printf("Dynamic statistics stats (starting time: %s):\n", strg);
     printf("OpcodeName\t# Requests\t# Aborts\n");
     for (i = 0; i < VL_NUMBER_OPCODESX; i++)