mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
MDEV-27227 mysqltest, Windows - support background execution in 'exec' command
If last character in command is ampersand, do not use my_popen/my_pclose in "exec", instead do CreateProcess() without a window.
This commit is contained in:
committed by
Sergei Golubchik
parent
6b4b625da3
commit
825f9c7b50
@@ -3251,6 +3251,47 @@ static int replace(DYNAMIC_STRING *ds_str,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
/**
|
||||||
|
Check if background execution of command was requested.
|
||||||
|
Like in Unix shell, we assume background execution of the last
|
||||||
|
character in command is a ampersand (we do not tokenize though)
|
||||||
|
*/
|
||||||
|
static bool is_background_command(const DYNAMIC_STRING *ds)
|
||||||
|
{
|
||||||
|
for (size_t i= ds->length - 1; i > 1; i--)
|
||||||
|
{
|
||||||
|
char c= ds->str[i];
|
||||||
|
if (!isspace(c))
|
||||||
|
return (c == '&');
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Execute OS command in background. We assume that the last character
|
||||||
|
is ampersand, i.e is_background_command() returned
|
||||||
|
*/
|
||||||
|
#include <string>
|
||||||
|
static int execute_in_background(char *cmd)
|
||||||
|
{
|
||||||
|
STARTUPINFO s{};
|
||||||
|
PROCESS_INFORMATION pi{};
|
||||||
|
char *end= strrchr(cmd, '&');
|
||||||
|
DBUG_ASSERT(end);
|
||||||
|
*end =0;
|
||||||
|
std::string scmd("cmd /c ");
|
||||||
|
scmd.append(cmd);
|
||||||
|
BOOL ok=
|
||||||
|
CreateProcess(0, (char *)scmd.c_str(), 0, 0, 0, CREATE_NO_WINDOW, 0, 0, &s, &pi);
|
||||||
|
*end= '&';
|
||||||
|
if (!ok)
|
||||||
|
return (int) GetLastError();
|
||||||
|
CloseHandle(pi.hProcess);
|
||||||
|
CloseHandle(pi.hThread);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Execute given command.
|
Execute given command.
|
||||||
@@ -3325,6 +3366,14 @@ void do_exec(struct st_command *command)
|
|||||||
DBUG_PRINT("info", ("Executing '%s' as '%s'",
|
DBUG_PRINT("info", ("Executing '%s' as '%s'",
|
||||||
command->first_argument, ds_cmd.str));
|
command->first_argument, ds_cmd.str));
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (is_background_command(&ds_cmd))
|
||||||
|
{
|
||||||
|
error= execute_in_background(ds_cmd.str);
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!(res_file= my_popen(ds_cmd.str, "r")))
|
if (!(res_file= my_popen(ds_cmd.str, "r")))
|
||||||
{
|
{
|
||||||
dynstr_free(&ds_cmd);
|
dynstr_free(&ds_cmd);
|
||||||
@@ -3351,7 +3400,9 @@ void do_exec(struct st_command *command)
|
|||||||
dynstr_append_sorted(&ds_res, &ds_sorted, 0);
|
dynstr_append_sorted(&ds_res, &ds_sorted, 0);
|
||||||
dynstr_free(&ds_sorted);
|
dynstr_free(&ds_sorted);
|
||||||
}
|
}
|
||||||
|
#ifdef _WIN32
|
||||||
|
end:
|
||||||
|
#endif
|
||||||
if (error)
|
if (error)
|
||||||
{
|
{
|
||||||
uint status= WEXITSTATUS(error);
|
uint status= WEXITSTATUS(error);
|
||||||
|
Reference in New Issue
Block a user