windows-misc-20050102
[openafs.git] / src / WINNT / afsd / cm_rpc.c
index 1c250ca..ad4d2a1 100644 (file)
@@ -26,7 +26,7 @@
 
 #include "smb.h"
 
-#include <rxkad.h>
+#include <rx/rxkad.h>
 
 /*
  * The motivation for this whole module is that in transmitting tokens
  * can match them up.  A list of session keys, searched by UUID, is maintained.
  */
 
-extern void afsi_log();
+extern void afsi_log(char *pattern, ...);
 
 typedef struct tokenEvent {
-       afs_uuid_t uuid;
-       char sessionKey[8];
-       struct tokenEvent *next;
+    afs_uuid_t uuid;
+    char sessionKey[8];
+    struct tokenEvent *next;
 } tokenEvent_t;
 
 tokenEvent_t *tokenEvents = NULL;
 
 osi_mutex_t tokenEventLock;
 
+EVENT_HANDLE rpc_ShutdownEvent = NULL;
+
 /*
  * Add a new uuid and session key to the list.
  */
@@ -56,13 +58,13 @@ void cm_RegisterNewTokenEvent(
        afs_uuid_t uuid,
        char sessionKey[8])
 {
-       tokenEvent_t *te = malloc(sizeof(tokenEvent_t));
-       te->uuid = uuid;
-       memcpy(te->sessionKey, sessionKey, sizeof(te->sessionKey));
-       lock_ObtainMutex(&tokenEventLock);
-       te->next = tokenEvents;
-       tokenEvents = te;
-       lock_ReleaseMutex(&tokenEventLock);
+    tokenEvent_t *te = malloc(sizeof(tokenEvent_t));
+    te->uuid = uuid;
+    memcpy(te->sessionKey, sessionKey, sizeof(te->sessionKey));
+    lock_ObtainMutex(&tokenEventLock);
+    te->next = tokenEvents;
+    tokenEvents = te;
+    lock_ReleaseMutex(&tokenEventLock);
 }
 
 /*
@@ -73,27 +75,27 @@ void cm_RegisterNewTokenEvent(
  */
 BOOL cm_FindTokenEvent(afs_uuid_t uuid, char sessionKey[8])
 {
-       RPC_STATUS status;
-       tokenEvent_t *te;
-       tokenEvent_t **ltep;
-
-       lock_ObtainMutex(&tokenEventLock);
-       te = tokenEvents;
-       ltep = &tokenEvents;
-       while (te) {
-           if (UuidEqual((UUID *)&uuid, (UUID *)&te->uuid, &status)) {
-                       *ltep = te->next;
-                       lock_ReleaseMutex(&tokenEventLock);
-                       memcpy(sessionKey, te->sessionKey,
-                               sizeof(te->sessionKey));
-                       free(te);
-                       return TRUE;
-           }
-           ltep = &te->next;
-           te = te->next;
-       }
-       lock_ReleaseMutex(&tokenEventLock);
-       return FALSE;
+    RPC_STATUS status;
+    tokenEvent_t *te;
+    tokenEvent_t **ltep;
+
+    lock_ObtainMutex(&tokenEventLock);
+    te = tokenEvents;
+    ltep = &tokenEvents;
+    while (te) {
+        if (UuidEqual((UUID *)&uuid, (UUID *)&te->uuid, &status)) {
+            *ltep = te->next;
+            lock_ReleaseMutex(&tokenEventLock);
+            memcpy(sessionKey, te->sessionKey,
+                    sizeof(te->sessionKey));
+            free(te);
+            return TRUE;
+        }
+        ltep = &te->next;
+        te = te->next;
+    }
+    lock_ReleaseMutex(&tokenEventLock);
+    return FALSE;
 }
 
 /*
@@ -104,117 +106,137 @@ long AFSRPC_SetToken(
        afs_uuid_t uuid,
        unsigned char __RPC_FAR sessionKey[8])
 {
-       cm_RegisterNewTokenEvent(uuid, sessionKey);
-       return 0;
+    cm_RegisterNewTokenEvent(uuid, sessionKey);
+    return 0;
 }
 
 long AFSRPC_GetToken(
        afs_uuid_t uuid,
        unsigned char __RPC_FAR sessionKey[8])
 {
-       BOOL found;
+    BOOL found;
 
-       found = cm_FindTokenEvent(uuid, sessionKey);
-       if (!found)
-               return 1;
+    found = cm_FindTokenEvent(uuid, sessionKey);
+    if (!found)
+        return 1;
 
-       return 0;
+    return 0;
 }
 
 void __RPC_FAR * __RPC_USER midl_user_allocate (size_t cBytes)
 {
-       return ((void __RPC_FAR *) malloc(cBytes));
+    return ((void __RPC_FAR *) malloc(cBytes));
 }
 
 void __RPC_USER midl_user_free(void __RPC_FAR * p)
 {
-       free(p);
+    free(p);
 } 
 
 void RpcListen()
 {
-       RPC_STATUS              status;
-       char                    *task;
-       RPC_BINDING_VECTOR      *ptrBindingVector = NULL;
-       BOOLEAN                 ifaceRegistered = FALSE;
-       BOOLEAN                 epRegistered = FALSE;
+    RPC_STATUS         status;
+    char                       *task;
+    RPC_BINDING_VECTOR *ptrBindingVector = NULL;
+    BOOLEAN                    ifaceRegistered = FALSE;
+    BOOLEAN                    epRegistered = FALSE;
 
 #ifdef NOOSIDEBUGSERVER        /* Use All Protseqs already done in OSI */
 
-       status = RpcServerUseAllProtseqs(1, NULL);
-       if (status != RPC_S_OK) {
-               task = "Use All Protocol Sequences";
-               goto cleanup;
-       }
+    status = RpcServerUseAllProtseqs(1, NULL);
+    if (status != RPC_S_OK) {
+        task = "Use All Protocol Sequences";
+        goto cleanup;
+    }
 
 #endif /* NOOSIDEBUGSERVER */
 
-       status = RpcServerRegisterIf(afsrpc_v1_0_s_ifspec, NULL, NULL);
-       if (status != RPC_S_OK) {
-               task = "Register Interface";
-               goto cleanup;
-       }
-       ifaceRegistered = TRUE;
-
-       status = RpcServerInqBindings(&ptrBindingVector);
-       if (status != RPC_S_OK) {
-               task = "Inquire Bindings";
-               goto cleanup;
-       }
-
-       status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
-       if (status != RPC_S_OK) {
-               task = "Register Authentication Info";
-               goto cleanup;
-       }
-
-       status = RpcEpRegister(afsrpc_v1_0_s_ifspec, ptrBindingVector,
-                               NULL, "AFS session key interface");
-       if (status != RPC_S_OK) {
-               task = "Register Endpoints";
-               goto cleanup;
-       }
-       epRegistered = TRUE;
-
-       afsi_log("RPC server listening");
-
-       status = RpcServerListen(OSI_MAXRPCCALLS, OSI_MAXRPCCALLS, 0);
-       if (status != RPC_S_OK) {
-               task = "Server Listen";
-       }
+    status = RpcServerRegisterIf(afsrpc_v1_0_s_ifspec, NULL, NULL);
+    if (status != RPC_S_OK) {
+        task = "Register Interface";
+        goto cleanup;
+    }
+    ifaceRegistered = TRUE;
+
+    status = RpcServerInqBindings(&ptrBindingVector);
+    if (status != RPC_S_OK) {
+        task = "Inquire Bindings";
+        goto cleanup;
+    }
+
+    status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
+    if (status != RPC_S_OK) {
+        task = "Register Authentication Info";
+        goto cleanup;
+    }
+
+    status = RpcEpRegister(afsrpc_v1_0_s_ifspec, ptrBindingVector,
+                            NULL, "AFS session key interface");
+    if (status != RPC_S_OK) {
+        task = "Register Endpoints";
+        goto cleanup;
+    }
+    epRegistered = TRUE;
+
+    afsi_log("RPC server listening");
+
+    status = RpcServerListen(OSI_MAXRPCCALLS, OSI_MAXRPCCALLS, 0);
+    if (status != RPC_S_OK) {
+        task = "Server Listen";
+    }
 
 cleanup:
-       if (epRegistered)
-               (void) RpcEpUnregister(afsrpc_v1_0_s_ifspec, ptrBindingVector,
-                                       NULL);
+    if (epRegistered)
+        (void) RpcEpUnregister(afsrpc_v1_0_s_ifspec, ptrBindingVector,
+                                NULL);
 
-       if (ptrBindingVector)
-               (void) RpcBindingVectorFree(&ptrBindingVector);
+    if (ptrBindingVector)
+        (void) RpcBindingVectorFree(&ptrBindingVector);
 
-       if (ifaceRegistered)
-               (void) RpcServerUnregisterIf(afsrpc_v1_0_s_ifspec, NULL, FALSE);
+    if (ifaceRegistered)
+        (void) RpcServerUnregisterIf(afsrpc_v1_0_s_ifspec, NULL, FALSE);
 
-       if (status != RPC_S_OK)
-               afsi_log("RPC problem, code %d for %s", status, task);
+    if (status != RPC_S_OK)
+        afsi_log("RPC problem, code %d for %s", status, task);
+    else
+        afsi_log("RPC shutdown");
 
-       return;
+    if (rpc_ShutdownEvent != NULL)
+        thrd_SetEvent(rpc_ShutdownEvent);
+    return;
 }
 
 long RpcInit()
 {
-       LONG status = ERROR_SUCCESS;
-       HANDLE listenThread;
-       ULONG listenThreadID = 0;
+    LONG status = ERROR_SUCCESS;
+    HANDLE listenThread;
+    ULONG listenThreadID = 0;
+    char * name = "afsd_rpc_ShutdownEvent";
+
+    lock_InitializeMutex(&tokenEventLock, "token event lock");
 
-       lock_InitializeMutex(&tokenEventLock, "token event lock");
+    rpc_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
+    if ( GetLastError() == ERROR_ALREADY_EXISTS )
+        afsi_log("Event Object Already Exists: %s", name);
 
-       listenThread = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)RpcListen,
-                                   0, 0, &listenThreadID);
+    listenThread = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)RpcListen,
+                                0, 0, &listenThreadID);
 
-       if (listenThread == NULL) {
-               status = GetLastError();
-       }
-       CloseHandle(listenThread);
+    if (listenThread == NULL) {
+        status = GetLastError();
+    }
+    CloseHandle(listenThread);
 
-       return status;
+    return status;
 }
+
+void RpcShutdown(void)
+{
+    RpcMgmtStopServerListening(NULL);
+
+    if (rpc_ShutdownEvent != NULL) {
+        thrd_WaitForSingleObject_Event(rpc_ShutdownEvent, INFINITE);
+        CloseHandle(rpc_ShutdownEvent);
+    }
+}
+