Skip to content

Commit 825f9c7

Browse files
vaintroubvuvova
authored andcommitted
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.
1 parent 6b4b625 commit 825f9c7

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

client/mysqltest.cc

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3251,6 +3251,47 @@ static int replace(DYNAMIC_STRING *ds_str,
32513251
return 0;
32523252
}
32533253

3254+
#ifdef _WIN32
3255+
/**
3256+
Check if background execution of command was requested.
3257+
Like in Unix shell, we assume background execution of the last
3258+
character in command is a ampersand (we do not tokenize though)
3259+
*/
3260+
static bool is_background_command(const DYNAMIC_STRING *ds)
3261+
{
3262+
for (size_t i= ds->length - 1; i > 1; i--)
3263+
{
3264+
char c= ds->str[i];
3265+
if (!isspace(c))
3266+
return (c == '&');
3267+
}
3268+
return false;
3269+
}
3270+
3271+
/**
3272+
Execute OS command in background. We assume that the last character
3273+
is ampersand, i.e is_background_command() returned
3274+
*/
3275+
#include <string>
3276+
static int execute_in_background(char *cmd)
3277+
{
3278+
STARTUPINFO s{};
3279+
PROCESS_INFORMATION pi{};
3280+
char *end= strrchr(cmd, '&');
3281+
DBUG_ASSERT(end);
3282+
*end =0;
3283+
std::string scmd("cmd /c ");
3284+
scmd.append(cmd);
3285+
BOOL ok=
3286+
CreateProcess(0, (char *)scmd.c_str(), 0, 0, 0, CREATE_NO_WINDOW, 0, 0, &s, &pi);
3287+
*end= '&';
3288+
if (!ok)
3289+
return (int) GetLastError();
3290+
CloseHandle(pi.hProcess);
3291+
CloseHandle(pi.hThread);
3292+
return 0;
3293+
}
3294+
#endif
32543295

32553296
/*
32563297
Execute given command.
@@ -3325,6 +3366,14 @@ void do_exec(struct st_command *command)
33253366
DBUG_PRINT("info", ("Executing '%s' as '%s'",
33263367
command->first_argument, ds_cmd.str));
33273368

3369+
#ifdef _WIN32
3370+
if (is_background_command(&ds_cmd))
3371+
{
3372+
error= execute_in_background(ds_cmd.str);
3373+
goto end;
3374+
}
3375+
#endif
3376+
33283377
if (!(res_file= my_popen(ds_cmd.str, "r")))
33293378
{
33303379
dynstr_free(&ds_cmd);
@@ -3351,7 +3400,9 @@ void do_exec(struct st_command *command)
33513400
dynstr_append_sorted(&ds_res, &ds_sorted, 0);
33523401
dynstr_free(&ds_sorted);
33533402
}
3354-
3403+
#ifdef _WIN32
3404+
end:
3405+
#endif
33553406
if (error)
33563407
{
33573408
uint status= WEXITSTATUS(error);

0 commit comments

Comments
 (0)