Swap to using proper windows pipes
authorMatt Caswell <matt@openssl.org>
Fri, 13 Nov 2015 11:22:21 +0000 (11:22 +0000)
committerMatt Caswell <matt@openssl.org>
Fri, 20 Nov 2015 23:37:17 +0000 (23:37 +0000)
We were using _pipe to create a pipe on windows. This uses the "int" type
for its file descriptor for compatibility. However most windows functions
expect to use a "HANDLE". Probably we could get away with just casting but
it seems more robust to use the proper type and main stream windows
functions.

Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/async/arch/async_null.c
crypto/async/arch/async_posix.c
crypto/async/arch/async_win.c
crypto/async/async.c
crypto/async/async_locl.h
include/openssl/async.h
test/asynctest.c

index 05b4964eb2dc7da5d78d9481d55d36d1103af790..f015c90fb738746347cebc9f5e32fc99841af3fe 100644 (file)
@@ -91,17 +91,17 @@ int async_pool_can_grow(void) {
     return 0;
 }
 
-int async_pipe(int *pipefds)
+int async_pipe(OSSL_ASYNC_FD *pipefds)
 {
     return -1;
 }
 
-int async_write1(int fd, const void *buf)
+int async_write1(OSSL_ASYNC_FD fd, const void *buf)
 {
     return -1;
 }
 
-int async_read1(int fd, void *buf)
+int async_read1(OSSL_ASYNC_FD fd, void *buf)
 {
     return -1;
 }
index 78bf61c44ef25adb8f14e707d00b6c56c9af427e..3f6cb6274aa19bc403f8542d10a8b30eca7b5bc4 100644 (file)
@@ -95,7 +95,7 @@ void async_fibre_free(async_fibre *fibre)
         OPENSSL_free(fibre->fibre.uc_stack.ss_sp);
 }
 
-int async_pipe(int *pipefds)
+int async_pipe(OSSL_ASYNC_FD *pipefds)
 {
     if (pipe(pipefds) == 0)
         return 1;
@@ -103,7 +103,7 @@ int async_pipe(int *pipefds)
     return 0;
 }
 
-int async_write1(int fd, const void *buf)
+int async_write1(OSSL_ASYNC_FD fd, const void *buf)
 {
     if (write(fd, buf, 1) > 0)
         return 1;
@@ -111,7 +111,7 @@ int async_write1(int fd, const void *buf)
     return 0;
 }
 
-int async_read1(int fd, void *buf)
+int async_read1(OSSL_ASYNC_FD fd, void *buf)
 {
     if (read(fd, buf, 1) > 0)
         return 1;
index fce3c481d66ea08f0253118366d17066f23ec955..9841a9ca929aa8317f77bfd7c19559407cb07602 100644 (file)
@@ -87,25 +87,29 @@ VOID CALLBACK async_start_func_win(PVOID unused)
     async_start_func();
 }
 
-int async_pipe(int *pipefds)
+int async_pipe(OSSL_ASYNC_FD *pipefds)
 {
-    if (_pipe(pipefds, 256, _O_BINARY) == 0)
-        return 1;
+    if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0)
+        return 0;
 
-    return 0;
+    return 1;
 }
 
-int async_write1(int fd, const void *buf)
+int async_write1(OSSL_ASYNC_FD fd, const void *buf)
 {
-    if (_write(fd, buf, 1) > 0)
+    DWORD numwritten = 0;
+
+    if (WriteFile(fd, buf, 1, &numwritten, NULL) && numwritten == 1)
         return 1;
 
     return 0;
 }
 
-int async_read1(int fd, void *buf)
+int async_read1(OSSL_ASYNC_FD fd, void *buf)
 {
-    if (_read(fd, buf, 1) > 0)
+    DWORD numread = 0;
+
+    if (ReadFile(fd, buf, 1, &numread, NULL) && numread == 1)
         return 1;
 
     return 0;
index 86f4c09675412b1dd27cd344481a38f0bd2459c3..5bf64afa226cb84803e8c9d9b94ff2ed1d5c4e4c 100644 (file)
@@ -109,7 +109,7 @@ static int async_ctx_free(void)
 static ASYNC_JOB *async_job_new(void)
 {
     ASYNC_JOB *job = NULL;
-    int pipefds[2];
+    OSSL_ASYNC_FD pipefds[2];
 
     job = OPENSSL_malloc(sizeof (ASYNC_JOB));
     if (job == NULL) {
@@ -387,7 +387,7 @@ ASYNC_JOB *ASYNC_get_current_job(void)
     return ctx->currjob;
 }
 
-int ASYNC_get_wait_fd(ASYNC_JOB *job)
+OSSL_ASYNC_FD ASYNC_get_wait_fd(ASYNC_JOB *job)
 {
     return job->wait_fd;
 }
index ba329788b3a4811437c52c88da86b7b6b2e1f88f..3cf97479edaaa9a4c0b3d3ffbd12223d8fcc6424 100644 (file)
@@ -73,8 +73,8 @@ struct async_job_st {
     int ret;
     int status;
     int wake_set;
-    int wait_fd;
-    int wake_fd;
+    OSSL_ASYNC_FD wait_fd;
+    OSSL_ASYNC_FD wake_fd;
 };
 
 DECLARE_STACK_OF(ASYNC_JOB)
@@ -88,6 +88,6 @@ void async_release_job_to_pool(ASYNC_JOB *job);
 size_t async_pool_max_size(void);
 void async_release_pool(void);
 int async_pool_can_grow(void);
-int async_pipe(int *pipefds);
-int async_write1(int fd, const void *buf);
-int async_read1(int fd, void *buf);
+int async_pipe(OSSL_ASYNC_FD *pipefds);
+int async_write1(OSSL_ASYNC_FD fd, const void *buf);
+int async_read1(OSSL_ASYNC_FD fd, void *buf);
index 6e7cf72b0b9deb5ff6628a733eea7a00570686c8..0cfd7dc6830f07b66d401fa075c57bd8707f0388 100644 (file)
 
 #include <stdlib.h>
 
+#if defined(_WIN32) || defined(__CYGWIN__)
+#include <windows.h>
+#define OSSL_ASYNC_FD   HANDLE
+#else
+#define OSSL_ASYNC_FD   int
+#endif
+
+
 # ifdef  __cplusplus
 extern "C" {
 # endif
@@ -74,7 +82,7 @@ int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
                          void *args, size_t size);
 int ASYNC_pause_job(void);
 
-int ASYNC_get_wait_fd(ASYNC_JOB *job);
+OSSL_ASYNC_FD ASYNC_get_wait_fd(ASYNC_JOB *job);
 ASYNC_JOB *ASYNC_get_current_job(void);
 void ASYNC_wake(ASYNC_JOB *job);
 void ASYNC_clear_wake(ASYNC_JOB *job);
index d89e8ad78937771c532813dfbacc87de0bb47352..5dd5c4adff67a6c2ca27e0b29ba7ec3f7aa558dc 100644 (file)
@@ -67,7 +67,7 @@
 # if _POSIX_VERSION >= 200112L
 #  define ASYNC_POSIX
 # endif
-#elif (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
+#elif defined(_WIN32) || defined(__CYGWIN__)
 # define ASYNC_WIN
 #endif
 
@@ -201,8 +201,9 @@ static int test_ASYNC_get_current_job()
     return 1;
 }
 
-static int hasdata(int fd)
+static int hasdata(OSSL_ASYNC_FD fd)
 {
+#ifdef ASYNC_POSIX
     fd_set checkfds;
     struct timeval tv;
     FD_ZERO(&checkfds);
@@ -213,12 +214,21 @@ static int hasdata(int fd)
     if (FD_ISSET(fd, &checkfds))
         return 1;
     return 0;
+#else
+    DWORD avail = 0;
+
+    if (PeekNamedPipe(fd, NULL, 0, NULL, &avail, NULL) && avail > 0)
+        return 1;
+
+    return 0;
+#endif
 }
 
 static int test_ASYNC_get_wait_fd()
 {
     ASYNC_JOB *job = NULL;
-    int funcret, fd;
+    int funcret;
+    OSSL_ASYNC_FD fd;
 
     if (       !ASYNC_init_pool(1, 0)
             || ASYNC_start_job(&job, &funcret, wake, NULL, 0)