DEVEL15-kill-ubik-pthread-env-20080718
[openafs.git] / src / budb / dbs_dump.c
index 462360a..2b94325 100644 (file)
@@ -10,7 +10,8 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID("$Header$");
+RCSID
+    ("$Header$");
 
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
@@ -35,7 +36,6 @@ RCSID("$Header$");
 #include <string.h>
 #include <des.h>
 #include <afs/cellconfig.h>
-#include <afs/auth.h>
 #include <errno.h>
 #include "budb.h"
 #include "budb_errs.h"
@@ -44,6 +44,7 @@ RCSID("$Header$");
 #include "globals.h"
 #include "afs/audit.h"
 
+void *dumpWatcher(void *);
 
 /* dump ubik database - interface routines */
 
@@ -56,67 +57,75 @@ badEntry(dbAddr)
      afs_uint32 dbAddr;
 {
     /* return entry ok */
-    return(0);
+    return (0);
 }
 
 /* setupDbDump
  *     decode the arguments passed via LWP and dump the database.
  */
 
-setupDbDump(writeFid)
-     int writeFid;
+void *
+setupDbDump(void *param)
 {
+    int writeFid = (int)param;
     afs_int32 code = 0;
 
     code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
-    if ( code )
+    if (code)
        goto error_exit;
 
     code = writeDatabase(dumpSyncPtr->ut, writeFid);
-    if ( code )
+    if (code)
        LogError(code, "writeDatabase failed\n");
 
     code = close(writeFid);
-    if ( code )
+    if (code)
        LogError(code, "pipe writer close failed\n");
 
     LogDebug(5, "writeDatabase complete\n");
 
-error_exit:
-    if ( dumpSyncPtr->ut )
+  error_exit:
+    if (dumpSyncPtr->ut)
        ubik_EndTrans(dumpSyncPtr->ut);
-    return(code);
+    return (void *)(code);
 }
 
 
 afs_int32 DumpDB(), RestoreDbHeader();
-afs_int32 BUDB_DumpDB (call, firstcall, maxLength, charListPtr, done)
+afs_int32
+SBUDB_DumpDB(call, firstcall, maxLength, charListPtr, done)
      struct rx_call *call;
-     int  firstcall;
+     int firstcall;
      afs_int32 maxLength;
-     charListT *charListPtr;
+     charListT *charListPtr;
      afs_int32 *done;
 {
-  afs_int32 code;
+    afs_int32 code;
 
-  code = DumpDB (call, firstcall, maxLength, charListPtr, done);
-  osi_auditU (call, BUDB_DmpDBEvent, code, AUD_END);
-  return code;
+    code = DumpDB(call, firstcall, maxLength, charListPtr, done);
+    osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
+    return code;
 }
 
-afs_int32 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 */
+     int firstcall;            /* 1 - init.  0 - no init */
      afs_int32 maxLength;
-     charListT *charListPtr;
+     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 )
+    if (callPermitted(call) == 0)
        ERROR(BUDB_NOTPERMITTED);
 
     ObtainWriteLock(&dumpSyncPtr->ds_lock);
@@ -124,21 +133,20 @@ afs_int32 DumpDB (call, firstcall, maxLength, charListPtr, done)
     /* If asking for zero bytes, then this is a call to reset the timeToLive
      * timer. Reset it if there is a dump in progress. 
      */
-    if (maxLength == 0)
-    {
-       charListPtr->charListT_val = (char *)0;
+    if (maxLength == 0) {
+       charListPtr->charListT_val = NULL;
        charListPtr->charListT_len = 0;
 
-        *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
+       *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
 
        /* reset the clock on dump timeout */
        dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
        goto error_exit;
     }
 
-    if (dumpSyncPtr->statusFlags == 0)
-    {
-        if (!firstcall) ERROR(BUDB_DUMPFAILED);
+    if (dumpSyncPtr->statusFlags == 0) {
+       if (!firstcall)
+           ERROR(BUDB_DUMPFAILED);
 
        LogDebug(5, "Setup dump\n");
 
@@ -150,95 +158,136 @@ afs_int32 DumpDB (call, firstcall, maxLength, charListPtr, done)
        dumpSyncPtr->statusFlags = 1;
 
        code = pipe(dumpSyncPtr->pipeFid);
-       if (code) ERROR(errno);
-       
-       code = LWP_CreateProcess(setupDbDump, 16384, 1, dumpSyncPtr->pipeFid[1],
-                                "Database Dumper", &dumperPid);
-       if (code) goto error_exit;
+       if (code)
+           ERROR(errno);
 
-       dumpSyncPtr->dumperPid  = dumperPid;
+#ifdef AFS_PTHREAD_ENV
+       /* Initialize the condition variables and the mutexes we use
+        * to signal and synchronize the reader and writer threads.
+        */
+       assert(pthread_cond_init(&dumpSyncPtr->ds_readerStatus_cond, (const pthread_condattr_t *)0) == 0);
+       assert(pthread_cond_init(&dumpSyncPtr->ds_writerStatus_cond, (const pthread_condattr_t *)0) == 0);
+       assert(pthread_mutex_init(&dumpSyncPtr->ds_readerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
+       assert(pthread_mutex_init(&dumpSyncPtr->ds_writerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
+
+       /* Initialize the thread attributes and launch the thread */
+
+       assert(pthread_attr_init(&dumperPid_tattr) == 0);
+       assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
+       assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
+
+#else
+       code =
+           LWP_CreateProcess(setupDbDump, 16384, 1,
+                             (void *)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 */
+
+       assert(pthread_attr_init(&watcherPid_tattr) == 0);
+       assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
+       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);
-    }
-    else if (firstcall)
-        ERROR(BUDB_LOCKED);
+       code =
+           LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
+                             "Database Dump Watchdog", &watcherPid);
+#endif
+    } else if (firstcall)
+       ERROR(BUDB_LOCKED);
 
     /* now read the database and feed it to the rpc connection */
 
     /* wait for data */
-    while ( dumpSyncPtr->ds_bytes == 0 )
-    {
+    while (dumpSyncPtr->ds_bytes == 0) {
        /* if no more data */
-       if ( (dumpSyncPtr->ds_writerStatus == DS_DONE      ) ||
-            (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR) )
-        {
+       if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
+           || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
            break;
        }
 
-       if ( dumpSyncPtr->ds_writerStatus == DS_WAITING )
-       {
+       if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
            LogDebug(6, "wakup writer\n");
            dumpSyncPtr->ds_writerStatus = 0;
+#ifdef AFS_PTHREAD_ENV
+           assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
+#else
            code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
-           if ( code )
+           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
+        assert(pthread_mutex_lock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
+        assert(pthread_cond_wait(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex) == 0);
+        assert(pthread_mutex_unlock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
+#else
        LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
+#endif
        ObtainWriteLock(&dumpSyncPtr->ds_lock);
     }
-    
-    charListPtr->charListT_val = (char *) malloc(maxLength);
-    readSize = read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
+
+    charListPtr->charListT_val = (char *)malloc(maxLength);
+    readSize =
+       read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
 
     /* reset the clock on dump timeout */
     dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
-    
+
     LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
 
     charListPtr->charListT_len = readSize;
 
-    if ( readSize == 0 )                               /* last chunk */
-    {
+    if (readSize == 0) {       /* last chunk */
        *done = 1;
        close(dumpSyncPtr->pipeFid[0]);
        dumpSyncPtr->statusFlags = 0;
-    }
-    else
+    } else
        *done = 0;
 
     dumpSyncPtr->ds_bytes -= readSize;
-    if ( dumpSyncPtr->ds_writerStatus == DS_WAITING )
-    {
+    if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
        dumpSyncPtr->ds_writerStatus = 0;
+#ifdef AFS_PTHREAD_ENV
+       assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
+#else
        code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
-       if ( code )
-               LogError(code, "BUDB_DumpDB: signal delivery failed\n");
+       if (code)
+           LogError(code, "BUDB_DumpDB: signal delivery failed\n");
+#endif
     }
 
-error_exit:
-    if ( !code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR) ) code = -1;
+  error_exit:
+    if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
+       code = -1;
     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
-    return(code);
+    return (code);
 }
 
-afs_int32 BUDB_RestoreDbHeader (call, header)
+afs_int32
+SBUDB_RestoreDbHeader(call, header)
      struct rx_call *call;
      struct DbHeader *header;
 {
-  afs_int32 code;
+    afs_int32 code;
 
-  code = RestoreDbHeader (call, header);
-  osi_auditU (call, BUDB_RstDBHEvent, code, AUD_END);
-  return code;
+    code = RestoreDbHeader(call, header);
+    osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
+    return code;
 }
 
-afs_int32 RestoreDbHeader (call, header)
+afs_int32
+RestoreDbHeader(call, header)
      struct rx_call *call;
      struct DbHeader *header;
 {
@@ -247,34 +296,34 @@ afs_int32 RestoreDbHeader (call, header)
 
     extern struct memoryDB db;
 
-    if ( callPermitted(call) == 0 )
+    if (callPermitted(call) == 0)
        ERROR(BUDB_NOTPERMITTED);
 
     code = InitRPC(&ut, LOCKWRITE, 1);
-    if ( code )
+    if (code)
        goto error_exit;
 
-    if ( header->dbversion != ntohl(db.h.version) )
+    if (header->dbversion != ntohl(db.h.version))
        ERROR(BUDB_VERSIONMISMATCH);
 
     /* merge rather than replace the header information */
-    if ( db.h.lastDumpId < htonl(header->lastDumpId))
+    if (db.h.lastDumpId < htonl(header->lastDumpId))
        db.h.lastDumpId = htonl(header->lastDumpId);
 
-    if ( db.h.lastTapeId < htonl(header->lastTapeId))
+    if (db.h.lastTapeId < htonl(header->lastTapeId))
        db.h.lastTapeId = htonl(header->lastTapeId);
 
-    if ( db.h.lastInstanceId < htonl(header->lastInstanceId))
+    if (db.h.lastInstanceId < htonl(header->lastInstanceId))
        db.h.lastInstanceId = htonl(header->lastInstanceId);
 
-    code = dbwrite(ut, 0, (char *) &db.h, sizeof(db.h));
-    if ( code )
+    code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
+    if (code)
        code = BUDB_IO;
 
-error_exit:
-    if ( ut )
+  error_exit:
+    if (ut)
        ubik_EndTrans(ut);
-    return(code);
+    return (code);
 }
 
 /* dumpWatcher
@@ -285,51 +334,58 @@ error_exit:
  *     transactions can proceed.
  */
 
-dumpWatcher()
+void *
+dumpWatcher(void *unused)
 {
     afs_int32 code;
 
-    while ( 1 )
-    { /*w*/
+    while (1) {                        /*w */
 
        /* printf("dumpWatcher\n"); */
        ObtainWriteLock(&dumpSyncPtr->ds_lock);
 
-       if ( dumpSyncPtr->statusFlags == 0 )
-       {
+       if (dumpSyncPtr->statusFlags == 0) {
            /* dump has finished */
            goto exit;
        }
 
        /* check time to live */
-       if ( time(0) > dumpSyncPtr->timeToLive )
-       { /*i*/
+       if (time(0) > dumpSyncPtr->timeToLive) {        /*i */
            /* dump has exceeded the allocated time - terminate it */
-           LogError(0, "Database dump timeout exceeded: %s", ctime(&dumpSyncPtr->timeToLive));
+           LogError(0, "Database dump timeout exceeded: %s",
+                    ctime(&dumpSyncPtr->timeToLive));
            LogError(0, "Terminating database dump\n");
-                    
+
            close(dumpSyncPtr->pipeFid[0]);
            close(dumpSyncPtr->pipeFid[1]);
-
+#ifdef AFS_PTHREAD_ENV
+           assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
+#else
            code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
-           if (code) LogError(code, "dumpWatcher: failed to kill dump thread\n");
+           if (code)
+               LogError(code, "dumpWatcher: failed to kill dump thread\n");
+#endif
 
-           if ( dumpSyncPtr->ut )
-           {
+           if (dumpSyncPtr->ut) {
                code = ubik_AbortTrans(dumpSyncPtr->ut);
-               if (code) LogError(code, "Aborting dump transaction\n");
+               if (code)
+                   LogError(code, "Aborting dump transaction\n");
            }
 
            memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
            goto exit;
-       } /*i*/
-
-        ReleaseWriteLock(&dumpSyncPtr->ds_lock);
+       }
+       /*i */
+       ReleaseWriteLock(&dumpSyncPtr->ds_lock);
+#ifdef AFS_PTHREAD_ENV
+       sleep(5);
+#else
        IOMGR_Sleep(5);
-    } /*w*/
+#endif
+    }                          /*w */
 
-exit:
+  exit:
     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
     /* printf("dumpWatcher exit\n"); */
-    return(0);
+    return (0);
 }