use search path
[oweals/gnunet.git] / src / util / os_priority.c
index d501b3511fcfe76b196cbcf41179543e0db2daa6..14aab8ce1c86c5b6f44e79036f57f300b0338587 100644 (file)
 #include "gnunet_os_lib.h"
 #include "disk.h"
 
+#if WINDOWS
+#include "gnunet_signal_lib.h"
+
+extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
+
+/**
+ * @brief Waits for a process to terminate and invokes the SIGCHLD handler
+ * @param h handle to the process
+ */
+static DWORD WINAPI
+ChildWaitThread (HANDLE h)
+{
+  WaitForSingleObject (h, INFINITE);
+
+  if (w32_sigchld_handler)
+    w32_sigchld_handler ();
+
+  CloseHandle (h);
+}
+#endif
+
 /**
  * Set process priority
  *
@@ -228,6 +249,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
 #endif
   char *fn;
   int len;
+  char path[MAX_PATH + 1];
 
   cmdlen = 0;
   va_start (ap, filename);
@@ -260,23 +282,26 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
       start.hStdOutput = stdout_handle;
     }
 #endif
-  len = strlen (filename);
-  if (strnicmp (filename + len - 4, ".exe", 4) == 0)
-    fn = filename;
-  else
-    GNUNET_asprintf (&fn, "%s.exe", filename);
+  if (FindExecutable(filename, NULL, path) <= 32)
+    {
+      SetErrnoFromWinError (GetLastError ());
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "FindExecutable", fn);
+      return -1;
+    }
 
   if (!CreateProcess
-      (fn, cmd, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &start,
+      (path, cmd, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &start,
        &proc))
     {
       SetErrnoFromWinError (GetLastError ());
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", fn);
       return -1;
     }
+
+  CreateThread (NULL, 64000, ChildWaitThread, proc.hProcess, 0, NULL);
+
   if (fn != filename)
     GNUNET_free (fn);
-  CloseHandle (proc.hProcess);
   CloseHandle (proc.hThread);
 
   GNUNET_free (cmd);
@@ -365,9 +390,10 @@ GNUNET_OS_start_process_v (const char *filename, char *const argv[])
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
       return -1;
     }
-  CloseHandle (proc.hProcess);
-  CloseHandle (proc.hThread);
 
+  CreateThread (NULL, 64000, ChildWaitThread, proc.hProcess, 0, NULL);
+
+  CloseHandle (proc.hThread);
   GNUNET_free (cmd);
 
   return proc.dwProcessId;