diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c index 6801839aa..5ac02645e 100644 --- a/agent/gpg-agent.c +++ b/agent/gpg-agent.c @@ -446,6 +446,9 @@ main (int argc, char **argv ) gpg_strerror (err)); } #endif /*USE_GNU_PTH && !HAVE_W32_SYSTEM*/ +#ifdef HAVE_W32_SYSTEM + pth_init (); +#endif /* Check that the libraries are suitable. Do it here because the option parsing may need services of the library. */ @@ -728,7 +731,6 @@ main (int argc, char **argv ) ; /* NOTREACHED */ else { /* Regular server mode */ -#ifndef HAVE_W32_SYSTEM int fd; pid_t pid; int len; @@ -739,8 +741,10 @@ main (int argc, char **argv ) default to a specific display. There is still a default display when gpg-agent weas started using --display or a client requested this using an OPTION command. */ +#ifndef HAVE_W32_SYSTEM if (!opt.keep_display) unsetenv ("DISPLAY"); +#endif *socket_name = 0; snprintf (socket_name, DIM(socket_name)-1, @@ -750,12 +754,15 @@ main (int argc, char **argv ) if (!p) BUG (); *p = 0;; + +#ifndef HAVE_W32_SYSTEM if (!mkdtemp(socket_name)) { log_error ("can't create directory `%s': %s\n", socket_name, strerror(errno) ); exit (1); } +#endif *p = '/'; if (strchr (socket_name, ':') ) @@ -769,8 +776,11 @@ main (int argc, char **argv ) exit (1); } - +#ifdef HAVE_W32_SYSTEM + fd = _w32_sock_new (AF_UNIX, SOCK_STREAM, 0); +#else fd = socket (AF_UNIX, SOCK_STREAM, 0); +#endif if (fd == -1) { log_error ("can't create socket: %s\n", strerror(errno) ); @@ -783,7 +793,13 @@ main (int argc, char **argv ) len = (offsetof (struct sockaddr_un, sun_path) + strlen(serv_addr.sun_path) + 1); - if (bind (fd, (struct sockaddr*)&serv_addr, len) == -1) + if ( +#ifdef HAVE_W32_SYSTEM + _w32_sock_bind +#else + bind +#endif + (fd, (struct sockaddr*)&serv_addr, len) == -1) { log_error ("error binding socket to `%s': %s\n", serv_addr.sun_path, strerror (errno) ); @@ -803,6 +819,7 @@ main (int argc, char **argv ) fflush (NULL); +#ifndef HAVE_W32_SYSTEM pid = fork (); if (pid == (pid_t)-1) { @@ -857,7 +874,6 @@ main (int argc, char **argv ) } /*NEVER REACHED*/ } /* end parent */ - /* This is the child @@ -893,6 +909,8 @@ main (int argc, char **argv ) exit (1); } +#endif /*!HAVE_W32_SYSTEM*/ + #ifdef USE_GNU_PTH if (!disable_pth) @@ -931,7 +949,6 @@ main (int argc, char **argv ) start_command_handler (fd, -1); } close (fd); -#endif } return 0; diff --git a/jnlib/w32-pth.c b/jnlib/w32-pth.c index 329233b35..ed3fe9b61 100644 --- a/jnlib/w32-pth.c +++ b/jnlib/w32-pth.c @@ -573,8 +573,8 @@ pth_attr_set (pth_attr_t hd, int field, ...) } -pth_t -pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg) +static pth_t +do_pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg) { SECURITY_ATTRIBUTES sa; DWORD tid; @@ -584,9 +584,6 @@ pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg) if (!hd) return NULL; - implicit_init (); - enter_pth (__FUNCTION__); - memset (&sa, 0, sizeof sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; @@ -594,21 +591,32 @@ pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg) ctx = calloc (1, sizeof * ctx); if (!ctx) - { - leave_pth (__FUNCTION__); - return NULL; - } + return NULL; ctx->thread = func; ctx->arg = arg; /* XXX: we don't use all thread attributes. */ - fprintf (stderr, "pth_spawn creating thread ...\n"); + fprintf (stderr, "do_pth_spawn creating thread ...\n"); th = CreateThread (&sa, hd->stack_size, (LPTHREAD_START_ROUTINE)helper_thread, ctx, 0, &tid); - fprintf (stderr, "pth_spawn created thread %p\n", th); + fprintf (stderr, "do_pth_spawn created thread %p\n", th); + return th; +} + +pth_t +pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg) +{ + HANDLE th; + + if (!hd) + return NULL; + + implicit_init (); + enter_pth (__FUNCTION__); + th = do_pth_spawn (hd, func, arg); leave_pth (__FUNCTION__); return th; } @@ -895,33 +903,6 @@ helper_thread (void * ctx) return NULL; } - -static void * -wait_fd_thread (void * ctx) -{ - pth_event_t ev = ctx; - - wait_for_fd (ev->u.fd, ev->flags & PTH_UNTIL_FD_READABLE, 3600); - fprintf (stderr, "wait_fd_thread: exit.\n"); - SetEvent (ev->hd); - ExitThread (0); - return NULL; -} - - -static void * -wait_timer_thread (void * ctx) -{ - pth_event_t ev = ctx; - int n = ev->u.tv.tv_sec*1000; - Sleep (n); - SetEvent (ev->hd); - fprintf (stderr, "wait_timer_thread: exit.\n"); - ExitThread (0); - return NULL; -} - - /* void */ /* sigemptyset (struct sigset_s * ss) */ /* { */ @@ -1079,16 +1060,6 @@ pth_event_isolate (pth_event_t ev) } -static void -free_threads (HANDLE *waitbuf, int *hdidx, int n) -{ - int i; - - for (i=0; i < n; i++) - CloseHandle (waitbuf[hdidx[i]]); -} - - static int pth_event_count (pth_event_t ev) { @@ -1103,6 +1074,65 @@ pth_event_count (pth_event_t ev) } + +static pth_t +spawn_helper_thread (pth_attr_t hd, void *(*func)(void *), void *arg) +{ + SECURITY_ATTRIBUTES sa; + DWORD tid; + HANDLE th; + + memset (&sa, 0, sizeof sa); + sa.bInheritHandle = TRUE; + sa.lpSecurityDescriptor = NULL; + sa.nLength = sizeof sa; + + fprintf (stderr, "spawn_helper_thread creating thread ...\n"); + th = CreateThread (&sa, hd->stack_size, + (LPTHREAD_START_ROUTINE)func, + arg, 0, &tid); + fprintf (stderr, "spawn_helper_thread created thread %p\n", th); + + return th; +} + + +static void +free_helper_threads (HANDLE *waitbuf, int *hdidx, int n) +{ + int i; + + for (i=0; i < n; i++) + CloseHandle (waitbuf[hdidx[i]]); +} + + +static void * +wait_fd_thread (void * ctx) +{ + pth_event_t ev = ctx; + + wait_for_fd (ev->u.fd, ev->flags & PTH_UNTIL_FD_READABLE, 3600); + fprintf (stderr, "wait_fd_thread: exit.\n"); + SetEvent (ev->hd); + ExitThread (0); + return NULL; +} + + +static void * +wait_timer_thread (void * ctx) +{ + pth_event_t ev = ctx; + int n = ev->u.tv.tv_sec*1000; + Sleep (n); + SetEvent (ev->hd); + fprintf (stderr, "wait_timer_thread: exit.\n"); + ExitThread (0); + return NULL; +} + + static int do_pth_wait (pth_event_t ev) { @@ -1125,7 +1155,7 @@ do_pth_wait (pth_event_t ev) { if (pos+1 > MAXIMUM_WAIT_OBJECTS/2) { - free_threads (waitbuf, hdidx, i); + free_helper_threads (waitbuf, hdidx, i); pth_attr_destroy (attr); return -1; } @@ -1143,13 +1173,13 @@ do_pth_wait (pth_event_t ev) case PTH_EVENT_FD: fprintf (stderr, "pth_wait: spawn event wait thread.\n"); hdidx[i++] = pos; - waitbuf[pos++] = pth_spawn (attr, wait_fd_thread, tmp); + waitbuf[pos++] = spawn_helper_thread (attr, wait_fd_thread, tmp); break; case PTH_EVENT_TIME: fprintf (stderr, "pth_wait: spawn event timer thread.\n"); hdidx[i++] = pos; - waitbuf[pos++] = pth_spawn (attr, wait_timer_thread, tmp); + waitbuf[pos++] = spawn_helper_thread (attr, wait_timer_thread, tmp); break; case PTH_EVENT_MUTEX: @@ -1162,7 +1192,7 @@ do_pth_wait (pth_event_t ev) } fprintf (stderr, "pth_wait: set %d\n", pos); n = WaitForMultipleObjects (pos, waitbuf, FALSE, INFINITE); - free_threads (waitbuf, hdidx, i); + free_helper_threads (waitbuf, hdidx, i); pth_attr_destroy (attr); fprintf (stderr, "pth_wait: n %ld\n", n);