Move WSAStartup/WSACleanup from DllMain to krb5_init_ctx/krb5_free_ctx
authorJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 23 Apr 2005 00:39:18 +0000 (00:39 +0000)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 23 Apr 2005 00:39:18 +0000 (00:39 +0000)
WSAStartup/WSACleanup cannot be called from DllMain without risking
a deadlock when FreeLibrary().

ticket:2980

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@17200 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/ChangeLog
src/lib/krb5/os/ChangeLog
src/lib/krb5/os/init_os_ctx.c
src/lib/win_glue.c

index 06d067515e7d567bdff2ea1e2faa0d0c3d2dfb3f..4ef1d3109dde5885a4fa87a89510f4f81978a85a 100644 (file)
@@ -1,3 +1,8 @@
+2005-04-22  Jeffrey Altman <jaltman@mit.edu>
+
+        * win_glue.c:  Remove calls to WSAStartup/WSACleanup because
+        they can result in deadlocks when called from DllMain().
+
 2005-04-13  Ken Raeburn  <raeburn@mit.edu>
 
        * win_glue.c (NEED_SOCKETS): Don't define.
index 294be82dbac680b77ae9914df910384053ca7905..a082c4952ff142a0af1088aa63f776a5e9a24522 100644 (file)
@@ -1,3 +1,11 @@
+2005-04-22  Jeffrey Altman <jaltman@mit.edu>
+
+        * init_os_ctx.c:  use krb5_init_ctx and krb5_free_ctx
+        to initialize and cleanup the winsock stack.  WSAStartup/
+        WSACleanup are only supposed to increment/decrement a 
+        reference counter if they have been previously called
+        within the application.
+
 2005-04-13  Ken Raeburn  <raeburn@mit.edu>
 
        * accessor.c (NEED_SOCKETS): Don't define.
index 0dd7cccd62a3982063ea99107d2d3f09dec4c73e..f4f9b690a085e8f717f757629e96f2dd27f80239 100644 (file)
@@ -36,6 +36,7 @@
 #endif
 
 #if defined(_WIN32)
+#include <winsock.h>
 
 static krb5_error_code
 get_from_windows_dir(
@@ -342,6 +343,10 @@ krb5_os_init_context(krb5_context ctx)
 {
        krb5_os_context os_ctx;
        krb5_error_code retval = 0;
+#ifdef _WIN32
+    WORD wVersionRequested;
+    WSADATA wsaData;
+#endif /* _WIN32 */
 
        os_ctx = ctx->os_context;
        os_ctx->magic = KV5M_OS_CONTEXT;
@@ -350,15 +355,23 @@ krb5_os_init_context(krb5_context ctx)
        os_ctx->os_flags = 0;
        os_ctx->default_ccname = 0;
 
-       krb5_cc_set_default_name(ctx, NULL);
-
        retval = os_init_paths(ctx);
-
        /*
         * If there's an error in the profile, return an error.  Just
         * ignoring the error is a Bad Thing (tm).
         */
-
+     
+        if (!retval) {
+                krb5_cc_set_default_name(ctx, NULL);
+
+#ifdef _WIN32
+                /* We initialize winsock to version 1.1 but 
+                 * we do not care if we succeed or fail.
+                 */
+                wVersionRequested = 0x0101;
+                WSAStartup (wVersionRequested, &wsaData);
+#endif /* _WIN32 */
+        }
        return retval;
 }
 
@@ -464,4 +477,8 @@ krb5_os_free_context(krb5_context ctx)
                profile_release(ctx->profile);
            ctx->profile = 0;
        }
+
+#ifdef _WIN32
+        WSACleanup();
+#endif /* _WIN32 */
 }
index 4d1800a92095f1d2170c98127f63a48a4b95416c..3b2cbc599fabb9f7e8f4d1f4f779b1a3b06af0db 100644 (file)
@@ -1,32 +1,8 @@
-/* 
- * WinSock support.
- *
- * Do the WinSock initialization call, keeping all the hair here.
- *
- * This routine is called by SOCKET_INITIALIZE in include/c-windows.h.
- * The code is pretty much copied from winsock.txt from winsock-1.1,
- * available from:
- * ftp://sunsite.unc.edu/pub/micro/pc-stuff/ms-windows/winsock/winsock-1.1
- *
- * Note: WSAStartup and WSACleanup is called here (and only here).
- * This assumes that under Windows, we only use this library via the
- * DLL.  Note that calls to WSAStartup and WSACleanup must be in
- * matched pairs.  If there is a missing WSACleanup call when a
- * program exits, under Lan Workplace, the name resolver will stop
- * working. 
- */
-
 #ifdef KRB4
 #include <kerberosIV/krb.h>
 #endif
 #include "k5-int.h"
 
-#ifndef NEED_WINSOCK
-#if defined(KRB4) || defined(KRB5) || defined(GSSAPI)
-#define NEED_WINSOCK 1
-#endif
-#endif
-
 #ifdef KRB4
 #include <kerberosIV/krb_err.h>
 #include <kerberosIV/kadm_err.h>
@@ -365,32 +341,13 @@ HINSTANCE get_lib_instance()
 static int
 control(int mode)
 {
-#ifdef NEED_WINSOCK
-    WORD wVersionRequested;
-    WSADATA wsaData;
-    int err;
-#endif
-
     switch(mode) {
     case DLL_STARTUP:
-#ifdef NEED_WINSOCK
-       wVersionRequested = 0x0101;             /* We need version 1.1 */
-       if ((err = WSAStartup (wVersionRequested, &wsaData)))
-           return err;
-       if (wVersionRequested != wsaData.wVersion) {
-           /* DLL couldn't support our version of the spec */
-           WSACleanup ();
-           return -104;                        /* FIXME -- better error? */
-       }
-#endif
        break;
 
     case DLL_SHUTDOWN:
 #ifdef KRB5
        krb5_stdcc_shutdown();
-#endif
-#ifdef NEED_WINSOCK
-       WSACleanup ();
 #endif
        break;