[tor-commits] [tor/master] We don't need to find our own path, just tell Windows to search

nickm at torproject.org nickm at torproject.org
Tue Aug 30 19:58:40 UTC 2011


commit 476807211c30491a92344b0222274aefbc5b5e8a
Author: Steven Murdoch <Steven.Murdoch at cl.cam.ac.uk>
Date:   Wed Aug 24 20:50:58 2011 +0100

    We don't need to find our own path, just tell Windows to search
---
 src/common/util.c    |    7 ++++-
 src/test/test_util.c |   68 +++++--------------------------------------------
 2 files changed, 13 insertions(+), 62 deletions(-)

diff --git a/src/common/util.c b/src/common/util.c
index f25dd43..8b9979c 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3137,7 +3137,7 @@ tor_spawn_background(const char *const filename, const char **argv)
    
   if (!retval) {
     log_warn(LD_GENERAL,
-      "Failed to create child process %s: %s", filename,
+      "Failed to create child process %s: %s", filename?filename:argv[0],
       format_win32_error(GetLastError()));
   } else  {
     // TODO: Close hProcess and hThread in process_handle.pid?
@@ -3657,7 +3657,12 @@ tor_check_port_forwarding(const char *filename, int dir_port, int or_port,
     /* Assume tor-fw-helper will succeed, start it later*/
     time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
 
+#ifdef MS_WINDOWS
+    /* Passing NULL as lpApplicationName makes Windows search for the .exe */
+    child_handle = tor_spawn_background(NULL, argv);
+#else
     child_handle = tor_spawn_background(filename, argv);
+#endif
     if (child_handle.status < 0) {
       log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
               filename);
diff --git a/src/test/test_util.c b/src/test/test_util.c
index d30cc11..5d3f95c 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1388,7 +1388,11 @@ run_util_spawn_background(const char *argv[], const char *expected_out,
   char stdout_buf[100], stderr_buf[100];
 
   /* Start the program */
+#ifdef MS_WINDOWS
+  process_handle = tor_spawn_background(NULL, argv);
+#else
   process_handle = tor_spawn_background(argv[0], argv);
+#endif
 
   tt_int_op(process_handle.status, ==, expected_status);
   
@@ -1425,55 +1429,14 @@ run_util_spawn_background(const char *argv[], const char *expected_out,
   ;
 }
 
-static char*
-get_windows_path(const char *process_name, const char **dest)
-{
-#ifdef MS_WINDOWS
-  char fn[MAX_PATH];
-  DWORD retval;
-  char *new_fn = NULL;
-  int i;
-
-  /* Get the file name of the current module */
-  retval = GetModuleFileName(NULL, fn, sizeof(fn));
-  if (retval >= sizeof(fn)) {
-    log_warn(LD_GENERAL, "Executable path name was longer than maximum (%d)", sizeof(fn));
-    return NULL;
-  }
-
-  /* Remove the filename component */
-  for (i = retval - 1; i >= 0; i--) {
-    /* \0 terminate at first path separator from end */
-    if ('\\' == fn[i] || '/' == fn[i]) { 
-      fn[i] = '\0';
-      break;
-    }
-  }
-
-  tor_asprintf(&new_fn, "%s\\%s", fn, process_name);
-  *dest = new_fn;
-  return new_fn;
-#else
-  (void)process_name;
-  (void)dest;
-  return NULL;
-#endif
-}
-
 /** Check that we can launch a process and read the output */
 static void
 test_util_spawn_background_ok(void *ptr)
 {
 #ifdef MS_WINDOWS
-  const char *argv[] = {NULL, "--test", NULL};
+  const char *argv[] = {"test-child.exe", "--test", NULL};
   const char *expected_out = "OUT\r\n--test\r\nSLEEPING\r\nDONE\r\n";
   const char *expected_err = "ERR\r\n";
-  char *filename;
-
-  /* Find path to test-child.exe (same directory as this executable */
-  filename = get_windows_path("test-child.exe", argv);
-  tt_assert(filename != NULL);
-  log_warn(LD_GENERAL, "Using %s as path", filename);
 #else
   const char *argv[] = {BUILDDIR "/src/test/test-child", "--test", NULL};
   const char *expected_out = "OUT\n--test\nSLEEPING\nDONE\n";
@@ -1483,12 +1446,6 @@ test_util_spawn_background_ok(void *ptr)
   (void)ptr;
 
   run_util_spawn_background(argv, expected_out, expected_err, 0, 1);
- done:
-#ifdef MS_WINDOWS
-  tor_free(filename);
-#else
-  ;
-#endif
 }
 
 /** Check that failing to find the executable works as expected */
@@ -1527,18 +1484,12 @@ test_util_spawn_background_partial_read(void *ptr)
   process_handle_t process_handle;
   char stdout_buf[100], stderr_buf[100];
 #ifdef MS_WINDOWS
-  const char *argv[] = {NULL, "--test", NULL};
+  const char *argv[] = {"test-child.exe", "--test", NULL};
   const char *expected_out[] = { "OUT\r\n--test\r\nSLEEPING\r\n",
                                  "DONE\r\n",
                                  NULL };
   const char *expected_err = "ERR\r\n";
   int expected_out_ctr;
-  char *filename;
-
-  /* Find path to test-child.exe (same directory as this executable */
-  filename = get_windows_path("test-child.exe", argv);
-  tt_assert(filename != NULL);
-  log_warn(LD_GENERAL, "Using %s as path", filename);
 #else
   const char *argv[] = {BUILDDIR "/src/test/test-child", "--test", NULL};
   const char *expected_out = "OUT\n--test\nSLEEPING\nDONE\n";
@@ -1547,9 +1498,8 @@ test_util_spawn_background_partial_read(void *ptr)
   (void)ptr;
 
   /* Start the program */
-  process_handle = tor_spawn_background(argv[0], argv);
+  process_handle = tor_spawn_background(NULL, argv);
   tt_int_op(process_handle.status, ==, expected_status);
-  tor_free(filename);
 
   /* Check stdout */
 #ifdef MS_WINDOWS
@@ -1597,11 +1547,7 @@ test_util_spawn_background_partial_read(void *ptr)
   tt_int_op(pos, ==, strlen(expected_err));
 
  done:
-#ifdef MS_WINDOWS
-  tor_free(filename);
-#else
   ;
-#endif
 }
 
 static void





More information about the tor-commits mailing list