2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2011-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cwchar>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <map>
|
2022-08-05 19:16:25 +00:00
|
|
|
#include <iterator>
|
2022-08-16 05:07:06 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-04-01 00:13:37 +00:00
|
|
|
#include <fmt/format.h>
|
2022-08-05 19:16:25 +00:00
|
|
|
#include <fmt/xchar.h> // for wchar_t support
|
2022-04-01 00:13:37 +00:00
|
|
|
|
2017-11-27 22:22:44 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "subprocess.h"
|
|
|
|
#include "tr-assert.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
namespace
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
|
|
|
|
enum class tr_app_type
|
|
|
|
{
|
|
|
|
EXE,
|
|
|
|
BATCH
|
2017-11-27 22:22:44 +00:00
|
|
|
};
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
void set_system_error(tr_error** error, DWORD code, std::string_view what)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const message = tr_win32_format_message(code); !std::empty(message))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-04-01 00:13:37 +00:00
|
|
|
tr_error_set(error, code, fmt::format(FMT_STRING("{:s} failed: {:s}"), what, message));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_error_set(error, code, fmt::format(FMT_STRING("{:s} failed: Unknown error: {:#08x}"), what, code));
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
// "The sort is case-insensitive, Unicode order, without regard to locale" © MSDN
|
|
|
|
class WStrICompare
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
public:
|
|
|
|
[[nodiscard]] auto compare(std::wstring_view a, std::wstring_view b) const noexcept // <=>
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
int diff = wcsnicmp(std::data(a), std::data(b), std::min(std::size(a), std::size(b)));
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (diff == 0)
|
|
|
|
{
|
|
|
|
diff = std::size(a) < std::size(b) ? -1 : (std::size(a) > std::size(b) ? 1 : 0);
|
|
|
|
}
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return diff;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
[[nodiscard]] auto operator()(std::wstring_view a, std::wstring_view b) const noexcept // <
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
return compare(a, b) < 0;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
2022-08-05 19:16:25 +00:00
|
|
|
};
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
using SortedWideEnv = std::map<std::wstring, std::wstring, WStrICompare>;
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
/*
|
|
|
|
* Var1=Value1\0
|
|
|
|
* Var2=Value2\0
|
|
|
|
* Var3=Value3\0
|
|
|
|
* ...
|
|
|
|
* VarN=ValueN\0\0
|
|
|
|
*/
|
|
|
|
auto to_env_string(SortedWideEnv const& wide_env)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
auto ret = std::vector<wchar_t>{};
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
for (auto const& [key, val] : wide_env)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
fmt::format_to(std::back_inserter(ret), FMT_STRING(L"{:s}={:s}"), key, val);
|
|
|
|
ret.insert(std::end(ret), L'\0');
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
ret.insert(std::end(ret), L'\0');
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
/*
|
|
|
|
* Var1=Value1\0
|
|
|
|
* Var2=Value2\0
|
|
|
|
* Var3=Value3\0
|
|
|
|
* ...
|
|
|
|
* VarN=ValueN\0\0
|
|
|
|
*/
|
|
|
|
auto parse_env_string(wchar_t const* env)
|
2021-12-17 05:47:51 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
auto sorted = SortedWideEnv{};
|
2021-12-17 05:47:51 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
for (;;)
|
2021-12-17 05:47:51 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const line = std::wstring_view{ env };
|
|
|
|
if (std::empty(line))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
break;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto const pos = line.find(L'='); pos != std::string_view::npos)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
sorted.insert_or_assign(std::wstring{ line.substr(0, pos) }, std::wstring{ line.substr(pos + 1) });
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
env += std::size(line) + 1 /*'\0'*/;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return sorted;
|
|
|
|
}
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto get_current_env()
|
|
|
|
{
|
|
|
|
auto env = SortedWideEnv{};
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto* pwch = GetEnvironmentStringsW(); pwch != nullptr)
|
|
|
|
{
|
|
|
|
env = parse_env_string(pwch);
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
FreeEnvironmentStringsW(pwch);
|
|
|
|
}
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return env;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
void append_argument(std::string& arguments, char const* argument)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-16 05:07:06 +00:00
|
|
|
if (!std::empty(arguments))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-16 05:07:06 +00:00
|
|
|
arguments += ' ';
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!tr_str_is_empty(argument) && strpbrk(argument, " \t\n\v\"") == nullptr)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-16 05:07:06 +00:00
|
|
|
arguments += argument;
|
2017-11-27 22:22:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
arguments += '"';
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
for (char const* src = argument; *src != '\0';)
|
|
|
|
{
|
|
|
|
size_t backslash_count = 0;
|
|
|
|
|
|
|
|
while (*src == '\\')
|
|
|
|
{
|
|
|
|
++backslash_count;
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*src)
|
|
|
|
{
|
|
|
|
case '\0':
|
|
|
|
backslash_count = backslash_count * 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
backslash_count = backslash_count * 2 + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backslash_count != 0)
|
|
|
|
{
|
2022-08-16 05:07:06 +00:00
|
|
|
arguments.append(backslash_count, '\\');
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*src != '\0')
|
|
|
|
{
|
2022-08-16 05:07:06 +00:00
|
|
|
arguments += *src++;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
arguments += '"';
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
bool contains_batch_metachars(char const* text)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
/* First part - chars explicitly documented by `cmd.exe /?` as "special" */
|
2021-08-15 09:41:48 +00:00
|
|
|
return strpbrk(
|
|
|
|
text,
|
|
|
|
"&<>()@^|"
|
2021-09-15 00:18:09 +00:00
|
|
|
"%!^\"") != nullptr;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto get_app_type(char const* app)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const lower = tr_strlower(app);
|
|
|
|
|
|
|
|
if (tr_strvEndsWith(lower, ".cmd") || tr_strvEndsWith(lower, ".bat"))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
return tr_app_type::BATCH;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Support other types? */
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return tr_app_type::EXE;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
void append_app_launcher_arguments(tr_app_type app_type, std::string& args)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
switch (app_type)
|
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
case tr_app_type::EXE:
|
2017-11-27 22:22:44 +00:00
|
|
|
break;
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
case tr_app_type::BATCH:
|
2017-11-27 22:22:44 +00:00
|
|
|
append_argument(args, "cmd.exe");
|
|
|
|
append_argument(args, "/d");
|
|
|
|
append_argument(args, "/e:off");
|
|
|
|
append_argument(args, "/v:off");
|
|
|
|
append_argument(args, "/s");
|
|
|
|
append_argument(args, "/c");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-08-05 19:16:25 +00:00
|
|
|
TR_ASSERT_MSG(false, "unsupported application type");
|
2017-11-27 22:22:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
std::wstring construct_cmd_line(char const* const* cmd)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const app_type = get_app_type(cmd[0]);
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
auto args = std::string{};
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
append_app_launcher_arguments(app_type, args);
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
for (size_t i = 0; cmd[i] != nullptr; ++i)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
if (app_type == tr_app_type::BATCH && i > 0 && contains_batch_metachars(cmd[i]))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
/* FIXME: My attempts to escape them one or another way didn't lead to anything good so far */
|
2022-08-16 05:07:06 +00:00
|
|
|
args.clear();
|
2022-08-05 19:16:25 +00:00
|
|
|
break;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
append_argument(args, cmd[i]);
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 05:07:06 +00:00
|
|
|
if (!std::empty(args))
|
2022-08-05 19:16:25 +00:00
|
|
|
{
|
2022-08-16 05:07:06 +00:00
|
|
|
return tr_win32_utf8_to_native(args);
|
2022-08-05 19:16:25 +00:00
|
|
|
}
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return {};
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
bool tr_spawn_async(
|
|
|
|
char const* const* cmd,
|
|
|
|
std::map<std::string_view, std::string_view> const& env,
|
2022-08-05 19:16:25 +00:00
|
|
|
std::string_view work_dir,
|
2021-11-24 19:25:23 +00:00
|
|
|
tr_error** error)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
// full_env = current_env + env;
|
|
|
|
auto full_env = get_current_env();
|
|
|
|
for (auto const& [key, val] : env)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
full_env.insert_or_assign(tr_win32_utf8_to_native(key), tr_win32_utf8_to_native(val));
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto cmd_line = construct_cmd_line(cmd);
|
|
|
|
if (std::empty(cmd_line))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER, "Constructing command line");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const current_dir = tr_win32_utf8_to_native(work_dir);
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto si = STARTUPINFOW{};
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
si.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = SW_HIDE;
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
bool const ret = CreateProcessW(
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2022-08-05 19:16:25 +00:00
|
|
|
std::data(cmd_line),
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
FALSE,
|
|
|
|
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW | CREATE_DEFAULT_ERROR_MODE,
|
2022-08-05 19:16:25 +00:00
|
|
|
std::empty(full_env) ? nullptr : to_env_string(full_env).data(),
|
|
|
|
std::empty(current_dir) ? nullptr : current_dir.c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
&si,
|
|
|
|
&pi);
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError(), "Call to CreateProcess()");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|