budb: Only have one build rule for budb_errs.c
[openafs.git] / src / budb / dbs_dump.c
index 9d189c2..17db2bd 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>
 
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
@@ -27,6 +26,9 @@ RCSID
 #include <sys/types.h>
 #include <afs/stds.h>
 #include <stdio.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
 #include <lock.h>
 #include <ubik.h>
 #include <lwp.h>
@@ -34,16 +36,19 @@ RCSID
 #include <rx/rx.h>
 #include <rx/rxkad.h>
 #include <string.h>
-#include <des.h>
 #include <afs/cellconfig.h>
 #include <errno.h>
 #include "budb.h"
 #include "budb_errs.h"
 #include "database.h"
+#include "budb_internal.h"
 #include "error_macros.h"
 #include "globals.h"
 #include "afs/audit.h"
 
+afs_int32 DumpDB(struct rx_call *, int, afs_int32, charListT *, afs_int32 *);
+afs_int32 RestoreDbHeader(struct rx_call *, struct DbHeader *);
+void *dumpWatcher(void *);
 
 /* dump ubik database - interface routines */
 
@@ -52,8 +57,7 @@ RCSID
  */
 
 afs_int32
-badEntry(dbAddr)
-     afs_uint32 dbAddr;
+badEntry(afs_uint32 dbAddr)
 {
     /* return entry ok */
     return (0);
@@ -63,9 +67,10 @@ badEntry(dbAddr)
  *     decode the arguments passed via LWP and dump the database.
  */
 
-setupDbDump(writeFid)
-     int writeFid;
+void *
+setupDbDump(void *param)
 {
+    int writeFid = (intptr_t)param;
     afs_int32 code = 0;
 
     code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
@@ -85,18 +90,13 @@ setupDbDump(writeFid)
   error_exit:
     if (dumpSyncPtr->ut)
        ubik_EndTrans(dumpSyncPtr->ut);
-    return (code);
+    return (void *)(intptr_t)(code);
 }
 
 
-afs_int32 DumpDB(), RestoreDbHeader();
 afs_int32
-SBUDB_DumpDB(call, firstcall, maxLength, charListPtr, done)
-     struct rx_call *call;
-     int firstcall;
-     afs_int32 maxLength;
-     charListT *charListPtr;
-     afs_int32 *done;
+SBUDB_DumpDB(struct rx_call *call, int firstcall, afs_int32 maxLength,
+            charListT *charListPtr, afs_int32 *done)
 {
     afs_int32 code;
 
@@ -106,17 +106,21 @@ SBUDB_DumpDB(call, firstcall, maxLength, charListPtr, done)
 }
 
 afs_int32
-DumpDB(call, firstcall, maxLength, charListPtr, done)
-     struct rx_call *call;
-     int firstcall;            /* 1 - init.  0 - no init */
-     afs_int32 maxLength;
-     charListT *charListPtr;
-     afs_int32 *done;
+DumpDB(struct rx_call *call,
+       int firstcall,          /* 1 - init.  0 - no init */
+       afs_int32 maxLength,
+       charListT *charListPtr,
+       afs_int32 *done)
 {
+#ifdef AFS_PTHREAD_ENV
+    pthread_t dumperPid, watcherPid;
+    pthread_attr_t dumperPid_tattr;
+    pthread_attr_t watcherPid_tattr;
+#else
     PROCESS dumperPid, watcherPid;
+#endif
     int readSize;
     afs_int32 code = 0;
-    extern dumpWatcher();
 
     if (callPermitted(call) == 0)
        ERROR(BUDB_NOTPERMITTED);
@@ -124,7 +128,7 @@ DumpDB(call, firstcall, maxLength, charListPtr, done)
     ObtainWriteLock(&dumpSyncPtr->ds_lock);
 
     /* If asking for zero bytes, then this is a call to reset the timeToLive
-     * timer. Reset it if there is a dump in progress. 
+     * timer. Reset it if there is a dump in progress.
      */
     if (maxLength == 0) {
        charListPtr->charListT_val = NULL;
@@ -154,20 +158,45 @@ DumpDB(call, firstcall, maxLength, charListPtr, done)
        if (code)
            ERROR(errno);
 
+#ifdef AFS_PTHREAD_ENV
+       /* Initialize the condition variables and the mutexes we use
+        * to signal and synchronize the reader and writer threads.
+        */
+       CV_INIT(&dumpSyncPtr->ds_readerStatus_cond, "reader cond", CV_DEFAULT, 0);
+       CV_INIT(&dumpSyncPtr->ds_writerStatus_cond, "writer cond", CV_DEFAULT, 0);
+       MUTEX_INIT(&dumpSyncPtr->ds_readerStatus_mutex, "reader", MUTEX_DEFAULT, 0);
+       MUTEX_INIT(&dumpSyncPtr->ds_writerStatus_mutex, "writer", MUTEX_DEFAULT, 0);
+
+       /* Initialize the thread attributes and launch the thread */
+
+       osi_Assert(pthread_attr_init(&dumperPid_tattr) == 0);
+       osi_Assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
+       osi_Assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
+
+#else
        code =
            LWP_CreateProcess(setupDbDump, 16384, 1,
-                             (void *)dumpSyncPtr->pipeFid[1],
+                             (void *)(intptr_t)dumpSyncPtr->pipeFid[1],
                              "Database Dumper", &dumperPid);
        if (code)
            goto error_exit;
+#endif
 
        dumpSyncPtr->dumperPid = dumperPid;
        dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
 
+#ifdef AFS_PTHREAD_ENV
+       /* Initialize the thread attributes and launch the thread */
+
+       osi_Assert(pthread_attr_init(&watcherPid_tattr) == 0);
+       osi_Assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
+       osi_Assert(pthread_create(&watcherPid, &watcherPid_tattr, (void *)dumpWatcher, NULL) == 0);
+#else
        /* now create the watcher thread */
        code =
            LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
                              "Database Dump Watchdog", &watcherPid);
+#endif
     } else if (firstcall)
        ERROR(BUDB_LOCKED);
 
@@ -184,14 +213,24 @@ DumpDB(call, firstcall, maxLength, charListPtr, done)
        if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
            LogDebug(6, "wakup writer\n");
            dumpSyncPtr->ds_writerStatus = 0;
+#ifdef AFS_PTHREAD_ENV
+           CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
+#else
            code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
            if (code)
                LogError(code, "BUDB_DumpDB: signal delivery failed\n");
+#endif
        }
        LogDebug(6, "wait for writer\n");
        dumpSyncPtr->ds_readerStatus = DS_WAITING;
        ReleaseWriteLock(&dumpSyncPtr->ds_lock);
+#ifdef AFS_PTHREAD_ENV
+        MUTEX_ENTER(&dumpSyncPtr->ds_readerStatus_mutex);
+        CV_WAIT(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex);
+        MUTEX_EXIT(&dumpSyncPtr->ds_readerStatus_mutex);
+#else
        LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
+#endif
        ObtainWriteLock(&dumpSyncPtr->ds_lock);
     }
 
@@ -216,9 +255,13 @@ DumpDB(call, firstcall, maxLength, charListPtr, done)
     dumpSyncPtr->ds_bytes -= readSize;
     if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
        dumpSyncPtr->ds_writerStatus = 0;
+#ifdef AFS_PTHREAD_ENV
+       CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
+#else
        code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
        if (code)
            LogError(code, "BUDB_DumpDB: signal delivery failed\n");
+#endif
     }
 
   error_exit:
@@ -229,9 +272,7 @@ DumpDB(call, firstcall, maxLength, charListPtr, done)
 }
 
 afs_int32
-SBUDB_RestoreDbHeader(call, header)
-     struct rx_call *call;
-     struct DbHeader *header;
+SBUDB_RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
 {
     afs_int32 code;
 
@@ -241,9 +282,7 @@ SBUDB_RestoreDbHeader(call, header)
 }
 
 afs_int32
-RestoreDbHeader(call, header)
-     struct rx_call *call;
-     struct DbHeader *header;
+RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
 {
     struct ubik_trans *ut = 0;
     afs_int32 code = 0;
@@ -281,14 +320,15 @@ RestoreDbHeader(call, header)
 }
 
 /* dumpWatcher
- *     monitors the state of a database dump. If the dump calls do not 
+ *     monitors the state of a database dump. If the dump calls do not
  *     reset the time to live value, the dump times out. In that case,
- *     we kill the database traversal thread and clean up all the other 
+ *     we kill the database traversal thread and clean up all the other
  *     state. Most importantly, the database is unlocked so that other
  *     transactions can proceed.
  */
 
-dumpWatcher()
+void *
+dumpWatcher(void *unused)
 {
     afs_int32 code;
 
@@ -311,10 +351,13 @@ dumpWatcher()
 
            close(dumpSyncPtr->pipeFid[0]);
            close(dumpSyncPtr->pipeFid[1]);
-
+#ifdef AFS_PTHREAD_ENV
+           osi_Assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
+#else
            code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
            if (code)
                LogError(code, "dumpWatcher: failed to kill dump thread\n");
+#endif
 
            if (dumpSyncPtr->ut) {
                code = ubik_AbortTrans(dumpSyncPtr->ut);
@@ -327,7 +370,11 @@ dumpWatcher()
        }
        /*i */
        ReleaseWriteLock(&dumpSyncPtr->ds_lock);
+#ifdef AFS_PTHREAD_ENV
+       sleep(5);
+#else
        IOMGR_Sleep(5);
+#endif
     }                          /*w */
 
   exit: