budb: Only have one build rule for budb_errs.c
[openafs.git] / src / budb / server.c
index 4e123a7..9f1f214 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
@@ -10,8 +10,7 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 #include <fcntl.h>
 #include <sys/stat.h>
@@ -47,6 +46,7 @@ RCSID
 #include "budb_errs.h"
 #include "database.h"
 #include "error_macros.h"
+#include "budb_internal.h"
 #include "globals.h"
 #include "afs/audit.h"
 
@@ -54,9 +54,11 @@ 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;
+afs_uint32 myHost = 0;
 int helpOption;
 
 /* server's global configuration information. This is exported to other
@@ -70,31 +72,32 @@ char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
 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)
-char *
+static int
 threadNum(void)
 {
-    return pthread_getspecific(rx_thread_id_key);
+    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_uint32 *myHost,
+                    afs_uint32 *serverList)
 {
     int i;
     char hostname[64];
@@ -107,7 +110,7 @@ convert_cell_to_ubik(cellinfo, myHost, serverList)
        printf("prserver: couldn't get address of this host.\n");
        BUDB_EXIT(1);
     }
-    memcpy(myHost, th->h_addr, sizeof(afs_int32));
+    memcpy(myHost, th->h_addr, sizeof(afs_uint32));
 
     for (i = 0; i < cellinfo->numServers; i++)
        /* omit my host from serverList */
@@ -124,7 +127,7 @@ convert_cell_to_ubik(cellinfo, myHost, serverList)
  *      If it were, this routine would never have been called.
  */
 static int
-MyBeforeProc(register struct cmd_syndesc *as, void *arock)
+MyBeforeProc(struct cmd_syndesc *as, void *arock)
 {
     helpOption = 0;
     return 0;
@@ -135,7 +138,7 @@ MyBeforeProc(register struct cmd_syndesc *as, void *arock)
  */
 
 void
-initializeArgHandler()
+initializeArgHandler(void)
 {
     struct cmd_syndesc *cptr;
 
@@ -167,6 +170,14 @@ initializeArgHandler()
     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
@@ -218,34 +229,45 @@ argHandler(struct cmd_syndesc *as, void *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) {
-       int tempfd, flags;
-       FILE *auditout;
-       char oldName[MAXPATHLEN];
        char *fileName = as->parms[7].items->data;
-#ifndef AFS_NT40_ENV
-       struct stat statbuf;
 
-       if ((lstat(fileName, &statbuf) == 0) 
-           && (S_ISFIFO(statbuf.st_mode))) {
-           flags = O_WRONLY | O_NONBLOCK;
-       } else 
-#endif
-       {
-           strcpy(oldName, fileName);
-           strcat(oldName, ".old");
-           renamefile(fileName, oldName);
-           flags = O_WRONLY | O_TRUNC | O_CREAT;
-       }
-       tempfd = open(fileName, flags, 0666);
-       if (tempfd > -1) {
-           auditout = fdopen(tempfd, "a");
-           if (auditout) {
-               osi_audit_file(auditout);
-           } else
-               printf("Warning: auditlog %s not writable, ignored.\n", fileName);
-       } else
-           printf("Warning: auditlog %s not writable, ignored.\n", fileName);
+        osi_audit_file(fileName);
     }
 
     return 0;
@@ -253,8 +275,8 @@ argHandler(struct cmd_syndesc *as, void *arock)
 
 /* --- */
 
-parseServerList(itemPtr)
-     struct cmd_item *itemPtr;
+int
+parseServerList(struct cmd_item *itemPtr)
 {
     struct cmd_item *save;
     char **serverArgs;
@@ -305,7 +327,8 @@ parseServerList(itemPtr)
  *     truncates just the database file.
  */
 
-truncateDatabase()
+int
+truncateDatabase(void)
 {
     char *path;
     afs_int32 code = 0;
@@ -342,9 +365,8 @@ 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;
@@ -356,13 +378,10 @@ main(argc, argv)
     char  clones[MAXHOSTSPERCELL];
 
     struct rx_service *tservice;
-    struct rx_securityClass *sca[3];
-
-    extern int afsconf_ServerAuth();
-    extern int afsconf_CheckAuth();
+    struct rx_securityClass **securityClasses;
+    afs_int32 numClasses;
 
     extern int rx_stackSize;
-    extern int BUDB_ExecuteRequest();
 
 #ifdef AFS_NT40_ENV
     /* initialize winsock */
@@ -375,8 +394,8 @@ main(argc, argv)
 
 #ifdef AFS_AIX32_ENV
     /*
-     * The following signal action for AIX is necessary so that in case of a 
-     * crash (i.e. core is generated) we can include the user's data section 
+     * The following signal action for AIX is necessary so that in case of a
+     * crash (i.e. core is generated) we can include the user's data section
      * in the core dump. Unfortunately, by default, only a partial core is
      * generated which, in many cases, isn't too useful.
      */
@@ -417,7 +436,7 @@ main(argc, argv)
 /*
     globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
     if ( globalConfPtr->log == NULL )
-    {   
+    {
        printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
        BUDB_EXIT(-1);
     }
@@ -474,8 +493,8 @@ main(argc, argv)
 
        LogDebug(1, "Using server list from %s cell database.\n", lcell);
 
-       code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo, 
-                                           &clones); 
+       code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
+                                           clones);
        code =
            convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
                                 globalConfPtr->serverList);
@@ -485,13 +504,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;
@@ -512,16 +531,14 @@ main(argc, argv)
 
     if (rxBind) {
        afs_int32 ccode;
-#ifndef AFS_NT40_ENV
-        if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
+        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 
-#endif 
+        } else
        {
             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
         }
@@ -532,9 +549,9 @@ main(argc, argv)
     }
 
     code = ubik_ServerInitByInfo (globalConfPtr->myHost,
-                                 htons(AFSCONF_BUDBPORT), 
+                                 htons(AFSCONF_BUDBPORT),
                                  &cellinfo,
-                                 &clones,              
+                                 clones,
                                  dbNamePtr,           /* name prefix */
                                  &BU_dbase);
 
@@ -544,25 +561,23 @@ main(argc, argv)
        ERROR(code);
     }
 
-    sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
-    sca[RX_SCINDEX_VAB] = 0;
-    sca[RX_SCINDEX_KAD] =
-       rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
-                                     NULL);
+    afsconf_BuildServerSecurityObjects(BU_conf, 0,
+                                      &securityClasses, &numClasses);
 
     /* Disable jumbograms */
     rx_SetNoJumbo();
 
     tservice =
-       rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
-                     BUDB_ExecuteRequest);
+       rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
+                         securityClasses, numClasses, BUDB_ExecuteRequest);
+
     if (tservice == (struct rx_service *)0) {
        LogError(0, "Could not create backup database rx service\n");
        printf("Could not create backup database rx service\n");
        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 */
@@ -584,7 +599,7 @@ 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);
@@ -592,7 +607,7 @@ main(argc, argv)
 }
 
 void
-consistencyCheckDb()
+consistencyCheckDb(void)
 {
     /* do consistency checks on structure sizes */
     if ((sizeof(struct htBlock) > BLOCKSIZE)
@@ -606,22 +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);
        }
     }
-    return 0;
+    va_end(ap);
 }
 
 static char *
@@ -631,35 +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);
     }
-    return 0;
+    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) {
@@ -669,11 +686,10 @@ LogError(code, a, b, c, d, e, f, g, h, i)
        if (code)
            fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
                    afs_error_message(code));
-       fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
+       vfprintf(globalConfPtr->log, fmt, ap );
        fflush(globalConfPtr->log);
        fclose(globalConfPtr->log);
     }
-    return 0;
 }
 
 
@@ -682,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;
@@ -696,5 +711,5 @@ LogNetDump(dumpPtr)
        printDump(globalConfPtr->log, &hostDump);
        fclose(globalConfPtr->log);
     }
-    return 0;
 }
+