Changeset 14530


Ignore:
Timestamp:
May 30, 2015, 11:15:57 AM (6 years ago)
Author:
mikedld
Message:

Use CreateProcess? instead of _spawnvpe to execute scripts on Windows

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/torrent.c

    r14525 r14530  
    1515 #include <unistd.h> /* fork (), execvp (), _exit () */
    1616#else
    17  #include <process.h> /* _spawnvpe () */
     17 #include <windows.h> /* CreateProcess (), GetLastError () */
    1818#endif
    1919
     
    21062106  if (script && *script)
    21072107    {
    2108       int i;
     2108      size_t i;
    21092109      char * cmd[] = { tr_strdup (script), NULL };
    21102110      char * env[] = {
     
    21122112        tr_strdup_printf ("TR_TIME_LOCALTIME=%s", timeStr),
    21132113        tr_strdup_printf ("TR_TORRENT_DIR=%s", tor->currentDir),
     2114        tr_strdup_printf ("TR_TORRENT_HASH=%s", tor->info.hashString),
    21142115        tr_strdup_printf ("TR_TORRENT_ID=%d", tr_torrentId (tor)),
    2115         tr_strdup_printf ("TR_TORRENT_HASH=%s", tor->info.hashString),
    21162116        tr_strdup_printf ("TR_TORRENT_NAME=%s", tr_torrentName (tor)),
    21172117        NULL };
     
    21192119      tr_logAddTorInfo (tor, "Calling script \"%s\"", script);
    21202120
     2121#ifndef NDEBUG
     2122      /* Win32 environment block strings should be sorted alphabetically */
     2123      for (i = 1; env[i] != NULL; ++i)
     2124        assert (strcmp (env[i - 1], env[i]) < 0);
     2125#endif
     2126
    21212127#ifdef _WIN32
    2122       if (_spawnvpe (_P_NOWAIT, script, (const char* const*)cmd, (const char* const*)env) == -1)
    2123         tr_logAddTorErr (tor, "error executing script \"%s\": %s", cmd[0], tr_strerror (errno));
    2124 #else
     2128
     2129      wchar_t * wide_script = tr_win32_utf8_to_native (script, -1);
     2130
     2131      size_t env_block_size = 0;
     2132      char * env_block = NULL;
     2133      for (i = 0; env[i] != NULL; ++i)
     2134        {
     2135          const size_t len = strlen (env[i]) + 1;
     2136          env_block = tr_renew (char, env_block, env_block_size + len + 1);
     2137          memcpy (env_block + env_block_size, env[i], len + 1);
     2138          env_block_size += len;
     2139        }
     2140
     2141      wchar_t * wide_env_block = NULL;
     2142      if (env_block != NULL)
     2143        {
     2144          env_block[env_block_size] = '\0';
     2145          wide_env_block = tr_win32_utf8_to_native (env_block, env_block_size + 1);
     2146          tr_free (env_block);
     2147        }
     2148
     2149      STARTUPINFOW si = { 0, };
     2150      si.cb = sizeof (si);
     2151      si.dwFlags = STARTF_USESHOWWINDOW;
     2152      si.wShowWindow = SW_HIDE;
     2153
     2154      PROCESS_INFORMATION pi;
     2155
     2156      if (CreateProcessW (wide_script, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS |
     2157                          CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW | CREATE_DEFAULT_ERROR_MODE |
     2158                          DETACHED_PROCESS, wide_env_block, L"\\", &si, &pi))
     2159        {
     2160          CloseHandle (pi.hThread);
     2161          CloseHandle (pi.hProcess);
     2162        }
     2163      else
     2164        {
     2165          char * const message = tr_win32_format_message (GetLastError ());
     2166          tr_logAddTorErr (tor, "error executing script \"%s\": %s", script, message);
     2167          tr_free (message);
     2168        }
     2169
     2170      tr_free (wide_env_block);
     2171      tr_free (wide_script);
     2172
     2173#else /* _WIN32 */
     2174
    21252175      signal (SIGCHLD, onSigCHLD);
    21262176
    21272177      if (!fork ())
    21282178        {
    2129           for (i=0; env[i]; ++i)
     2179          for (i = 0; env[i] != NULL; ++i)
    21302180            putenv (env[i]);
    21312181
     2182          chdir ("/");
     2183
    21322184          if (execvp (script, cmd) == -1)
    2133             tr_logAddTorErr (tor, "error executing script \"%s\": %s", cmd[0], tr_strerror (errno));
     2185            tr_logAddTorErr (tor, "error executing script \"%s\": %s", script, tr_strerror (errno));
    21342186
    21352187          _exit (0);
    21362188        }
    2137 #endif
    2138 
    2139       for (i=0; cmd[i]; ++i) tr_free (cmd[i]);
    2140       for (i=0; env[i]; ++i) tr_free (env[i]);
     2189
     2190#endif /* _WIN32 */
     2191
     2192      for (i = 0; cmd[i] != NULL; ++i)
     2193        tr_free (cmd[i]);
     2194      for (i = 0; env[i] != NULL; ++i)
     2195        tr_free (env[i]);
    21412196    }
    21422197}
Note: See TracChangeset for help on using the changeset viewer.