Squash pthreaded ubik warnings
[openafs.git] / src / budb / server.c
index 95be539..63e538a 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
+#include <fcntl.h>
+#include <sys/stat.h>
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
-#include <fcntl.h>
 #include <WINNT/afsevent.h>
 #else
 #include <netinet/in.h>
@@ -23,15 +22,7 @@ RCSID
 #include <sys/time.h>
 #include <netdb.h>
 #endif
-
-#ifdef HAVE_STRING_H
 #include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-
 #include <afs/stds.h>
 #include <sys/types.h>
 #include <time.h>
@@ -44,7 +35,6 @@ RCSID
 #include <rx/rxkad.h>
 #include <rx/rx_globals.h>
 #include <afs/cellconfig.h>
-#include <afs/auth.h>
 #include <afs/bubasics.h>
 #include <afs/afsutil.h>
 #include <afs/com_err.h>
@@ -55,12 +45,17 @@ RCSID
 #include "budb_errs.h"
 #include "database.h"
 #include "error_macros.h"
+#include "budb_internal.h"
 #include "globals.h"
 #include "afs/audit.h"
 
 struct ubik_dbase *BU_dbase;
 struct afsconf_dir *BU_conf;   /* for getting cell info */
 
+int argHandler(struct cmd_syndesc *, void *);
+int truncateDatabase(void);
+int parseServerList(struct cmd_item *);
+
 char lcell[MAXKTCREALMLEN];
 afs_int32 myHost = 0;
 int helpOption;
@@ -75,19 +70,33 @@ char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
 /* debugging control */
 int debugging = 0;
 
+int rxBind = 0;
+int lwps   = 3;
+
+#define MINLWP  3
+#define MAXLWP 16
+
+#define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
+afs_uint32 SHostAddrs[ADDRSPERSITE];
+
+#if defined(AFS_PTHREAD_ENV)
+static int
+threadNum(void)
+{
+    return (intptr_t)pthread_getspecific(rx_thread_id_key);
+}
+#endif
+
 /* check whether caller is authorized to manage RX statistics */
 int
-BU_rxstat_userok(call)
-     struct rx_call *call;
+BU_rxstat_userok(struct rx_call *call)
 {
     return afsconf_SuperUser(BU_conf, call, NULL);
 }
 
 int
-convert_cell_to_ubik(cellinfo, myHost, serverList)
-     struct afsconf_cell *cellinfo;
-     afs_int32 *myHost;
-     afs_int32 *serverList;
+convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_int32 *myHost, 
+                    afs_int32 *serverList)
 {
     int i;
     char hostname[64];
@@ -117,8 +126,7 @@ convert_cell_to_ubik(cellinfo, myHost, serverList)
  *      If it were, this routine would never have been called.
  */
 static int
-MyBeforeProc(as)
-     register struct cmd_syndesc *as;
+MyBeforeProc(register struct cmd_syndesc *as, void *arock)
 {
     helpOption = 0;
     return 0;
@@ -128,12 +136,11 @@ MyBeforeProc(as)
  *     initialize all the supported commands and their arguments
  */
 
-initializeArgHandler()
+void
+initializeArgHandler(void)
 {
     struct cmd_syndesc *cptr;
 
-    int argHandler();
-
     cmd_SetBeforeProc(MyBeforeProc, NULL);
 
     cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
@@ -159,12 +166,21 @@ initializeArgHandler()
     cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
                "the number of ubik buffers");
 
+    cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
+               "audit log path");
+
+    cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
+               "number of processes");
+
+    cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
+               "bind the Rx socket (primary interface only)");
+
+    cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
+               "audit interface (file or sysvmq)");
 }
 
 int
-argHandler(as, arock)
-     struct cmd_syndesc *as;
-     char *arock;
+argHandler(struct cmd_syndesc *as, void *arock)
 {
 
     /* globalConfPtr provides the handle for the configuration information */
@@ -212,13 +228,54 @@ argHandler(as, arock)
     else
        ubik_nBuffers = 0;
 
+    /* param 7 (-auditlog) handled below */
+
+    /* user provided the number of threads    */
+    if (as->parms[8].items != 0) {
+       lwps = atoi(as->parms[8].items->data);
+       if (lwps > MAXLWP) {
+           printf ("Warning: '-p %d' is too big; using %d instead\n",
+               lwps, MAXLWP);
+           lwps = MAXLWP;
+       }
+       if (lwps < MINLWP) {
+           printf ("Warning: '-p %d' is too small; using %d instead\n",
+               lwps, MINLWP);
+           lwps = MINLWP;
+       }
+    }
+
+    /* user provided rxbind option    */
+    if (as->parms[9].items != 0) {
+       rxBind = 1;
+    }
+
+    /* -audit-interface */
+    if (as->parms[10].items != 0) {
+       char *interface = as->parms[10].items->data;
+
+       if (osi_audit_interface(interface)) {
+           printf("Invalid audit interface '%s'\n", interface);
+           BUDB_EXIT(-1);
+       }
+    }
+
+    /* -auditlog */
+    /* needs to be after -audit-interface, so we osi_audit_interface
+     * before we osi_audit_file */
+    if (as->parms[7].items != 0) {
+       char *fileName = as->parms[7].items->data;
+
+        osi_audit_file(fileName);
+    }
+
     return 0;
 }
 
 /* --- */
 
-parseServerList(itemPtr)
-     struct cmd_item *itemPtr;
+int
+parseServerList(struct cmd_item *itemPtr)
 {
     struct cmd_item *save;
     char **serverArgs;
@@ -269,7 +326,8 @@ parseServerList(itemPtr)
  *     truncates just the database file.
  */
 
-truncateDatabase()
+int
+truncateDatabase(void)
 {
     char *path;
     afs_int32 code = 0;
@@ -306,24 +364,22 @@ truncateDatabase()
 
 #include "AFS_component_version_number.c"
 
-main(argc, argv)
-     int argc;
-     char *argv[];
+int
+main(int argc, char **argv)
 {
     char *whoami = argv[0];
     char *dbNamePtr = 0;
     struct afsconf_cell cellinfo;
     time_t currentTime;
     afs_int32 code = 0;
+    afs_uint32 host = ntohl(INADDR_ANY);
+
+    char  clones[MAXHOSTSPERCELL];
 
     struct rx_service *tservice;
     struct rx_securityClass *sca[3];
 
-    extern int afsconf_ServerAuth();
-    extern int afsconf_CheckAuth();
-
     extern int rx_stackSize;
-    extern int BUDB_ExecuteRequest();
 
 #ifdef AFS_NT40_ENV
     /* initialize winsock */
@@ -349,6 +405,7 @@ main(argc, argv)
     sigaction(SIGSEGV, &nsa, NULL);
     sigaction(SIGABRT, &nsa, NULL);
 #endif
+    osi_audit_init();
     osi_audit(BUDB_StartEvent, 0, AUD_END);
 
     initialize_BUDB_error_table();
@@ -359,7 +416,7 @@ main(argc, argv)
 #ifdef AFS_NT40_ENV
        ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
 #endif
-       com_err(whoami, errno, "; Unable to obtain AFS server directory.");
+       afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
        exit(2);
     }
 
@@ -385,6 +442,10 @@ main(argc, argv)
 
     srandom(1);
 
+#ifdef AFS_PTHREAD_ENV
+    SetLogThreadNumProgram( threadNum );
+#endif
+
     /* process the user supplied args */
     helpOption = 1;
     code = cmd_Dispatch(argc, argv);
@@ -413,7 +474,7 @@ main(argc, argv)
     BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
     if (BU_conf == 0) {
        LogError(code, "Failed getting cell info\n");
-       com_err(whoami, code, "Failed getting cell info");
+       afs_com_err(whoami, code, "Failed getting cell info");
        ERROR(BUDB_NOCELLS);
     }
 
@@ -430,7 +491,8 @@ main(argc, argv)
 
        LogDebug(1, "Using server list from %s cell database.\n", lcell);
 
-       code = afsconf_GetCellInfo(BU_conf, lcell, 0, &cellinfo);
+       code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
+                                           clones);
        code =
            convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
                                 globalConfPtr->serverList);
@@ -440,13 +502,13 @@ main(argc, argv)
 
     /* initialize ubik */
     ubik_CRXSecurityProc = afsconf_ClientAuth;
-    ubik_CRXSecurityRock = (char *)BU_conf;
+    ubik_CRXSecurityRock = BU_conf;
 
     ubik_SRXSecurityProc = afsconf_ServerAuth;
-    ubik_SRXSecurityRock = (char *)BU_conf;
+    ubik_SRXSecurityRock = BU_conf;
 
     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
-    ubik_CheckRXSecurityRock = (char *)BU_conf;
+    ubik_CheckRXSecurityRock = BU_conf;
 
     if (ubik_nBuffers == 0)
        ubik_nBuffers = 400;
@@ -465,11 +527,35 @@ main(argc, argv)
 
     rx_SetRxDeadTime(60);      /* 60 seconds inactive before timeout */
 
-    code = ubik_ServerInit(globalConfPtr->myHost, htons(AFSCONF_BUDBPORT), globalConfPtr->serverList, dbNamePtr,       /* name prefix */
-                          &BU_dbase);
+    if (rxBind) {
+       afs_int32 ccode;
+        if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
+            AFSDIR_SERVER_NETINFO_FILEPATH) {
+            char reason[1024];
+            ccode = parseNetFiles(SHostAddrs, NULL, NULL,
+                                           ADDRSPERSITE, reason,
+                                           AFSDIR_SERVER_NETINFO_FILEPATH,
+                                           AFSDIR_SERVER_NETRESTRICT_FILEPATH);
+        } else 
+       {
+            ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
+        }
+        if (ccode == 1) {
+            host = SHostAddrs[0];
+           rx_InitHost(host, htons(AFSCONF_BUDBPORT));
+       }
+    }
+
+    code = ubik_ServerInitByInfo (globalConfPtr->myHost,
+                                 htons(AFSCONF_BUDBPORT), 
+                                 &cellinfo,
+                                 clones,              
+                                 dbNamePtr,           /* name prefix */
+                                 &BU_dbase);
+
     if (code) {
        LogError(code, "Ubik init failed\n");
-       com_err(whoami, code, "Ubik init failed");
+       afs_com_err(whoami, code, "Ubik init failed");
        ERROR(code);
     }
 
@@ -483,7 +569,7 @@ main(argc, argv)
     rx_SetNoJumbo();
 
     tservice =
-       rx_NewService(0, BUDB_SERVICE, "BackupDatabase", sca, 3,
+       rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
                      BUDB_ExecuteRequest);
     if (tservice == (struct rx_service *)0) {
        LogError(0, "Could not create backup database rx service\n");
@@ -491,7 +577,7 @@ main(argc, argv)
        BUDB_EXIT(3);
     }
     rx_SetMinProcs(tservice, 1);
-    rx_SetMaxProcs(tservice, 3);
+    rx_SetMaxProcs(tservice, lwps);
     rx_SetStackSize(tservice, 10000);
 
     /* allow super users to manage RX statistics */
@@ -513,15 +599,15 @@ main(argc, argv)
     currentTime = time(0);
     LogError(0, "Ready to process requests at %s\n", ctime(&currentTime));
 
-    rx_ServerProc();           /* donate this LWP */
+    rx_ServerProc(NULL);               /* donate this LWP */
 
   error_exit:
     osi_audit(BUDB_FinishEvent, code, AUD_END);
     return (code);
 }
 
-
-consistencyCheckDb()
+void
+consistencyCheckDb(void)
 {
     /* do consistency checks on structure sizes */
     if ((sizeof(struct htBlock) > BLOCKSIZE)
@@ -535,21 +621,23 @@ consistencyCheckDb()
     }
 }
 
- /*VARARGS*/
-LogDebug(level, a, b, c, d, e, f, g, h, i)
-     int level;
-     char *a, *b, *c, *d, *e, *f, *g, *h, *i;
+void
+LogDebug(int level, char *fmt, ... )
 {
+    va_list ap;
 
+    va_start(ap, fmt);
+       
     if (debugging >= level) {
        /* log normally closed so can remove it */
        globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
        if (globalConfPtr->log != NULL) {
-           fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
+           vfprintf(globalConfPtr->log, fmt, ap);
            fflush(globalConfPtr->log);
            fclose(globalConfPtr->log);
        }
     }
+    va_end(ap);
 }
 
 static char *
@@ -559,34 +647,36 @@ TimeStamp(time_t t)
     static char timestamp[20];
 
     lt = localtime(&t);
-    strftime(timestamp, 20, "%m/%d/%Y %T", lt);
+    strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
     return timestamp;
 }
 
- /*VARARGS*/
-Log(a, b, c, d, e, f, g, h, i)
-     char *a, *b, *c, *d, *e, *f, *g, *h, *i;
+void
+Log(char *fmt, ...)
 {
+    va_list ap;
     time_t now;
 
+    va_start(ap, fmt);
     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
     if (globalConfPtr->log != NULL) {
        now = time(0);
        fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
 
-       fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
+       vfprintf(globalConfPtr->log, fmt, ap);
        fflush(globalConfPtr->log);
        fclose(globalConfPtr->log);
     }
+    va_end(ap);
 }
 
- /*VARARGS*/
-LogError(code, a, b, c, d, e, f, g, h, i)
-     long code;
-     char *a, *b, *c, *d, *e, *f, *g, *h, *i;
+void
+LogError(long code, char *fmt, ... )
 {
+    va_list ap;
     time_t now;
 
+    va_start(ap, fmt);
     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
 
     if (globalConfPtr->log != NULL) {
@@ -594,9 +684,9 @@ LogError(code, a, b, c, d, e, f, g, h, i)
        fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
 
        if (code)
-           fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
-                   error_message(code));
-       fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
+           fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
+                   afs_error_message(code));
+       vfprintf(globalConfPtr->log, fmt, ap );
        fflush(globalConfPtr->log);
        fclose(globalConfPtr->log);
     }
@@ -608,9 +698,8 @@ LogError(code, a, b, c, d, e, f, g, h, i)
  * ----------------
  */
 
-
-LogNetDump(dumpPtr)
-     struct dump *dumpPtr;
+void
+LogNetDump(struct dump *dumpPtr)
 {
     struct dump hostDump;
     extern buServerConfP globalConfPtr;
@@ -623,3 +712,4 @@ LogNetDump(dumpPtr)
        fclose(globalConfPtr->log);
     }
 }
+