1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2024-12-25 01:29:53 +03:00

Fix a deadlock when Run() to create a process. Child forked will inherit

state of global mutex which may be in intermidiate taken status by one of
threads of the parent, then in child process it tries to get this mutex
and is always pending. One example of mutex is malloc_lock, and there are more.
This commit is contained in:
ajeecai 2016-10-09 14:23:25 +08:00
parent 1e17c9bcfd
commit 69d132e997

47
src/Mayaqua/Unix.c Normal file → Executable file
View File

@ -1162,7 +1162,9 @@ bool UnixRunW(wchar_t *filename, wchar_t *arg, bool hide, bool wait)
bool UnixRun(char *filename, char *arg, bool hide, bool wait)
{
TOKEN_LIST *t;
char **args;
UINT ret;
// Validate arguments
if (filename == NULL)
{
@ -1173,6 +1175,25 @@ bool UnixRun(char *filename, char *arg, bool hide, bool wait)
arg = "";
}
Print("", filename, arg);
t = ParseToken(arg, " ");
if (t == NULL)
{
return false;
}
else
{
UINT num_args;
UINT i;
num_args = t->NumTokens + 2;
args = ZeroMalloc(sizeof(char *) * num_args);
args[0] = filename;
for (i = 1;i < num_args - 1;i++)
{
args[i] = t->Token[i - 1];
}
}
// Create a child process
ret = fork();
if (ret == -1)
@ -1183,39 +1204,21 @@ bool UnixRun(char *filename, char *arg, bool hide, bool wait)
if (ret == 0)
{
Print("", filename, arg);
// Child process
if (hide)
{
// Close the standard I/O
UnixCloseIO();
}
t = ParseToken(arg, " ");
if (t == NULL)
{
AbortExit();
}
else
{
char **args;
UINT num_args;
UINT i;
num_args = t->NumTokens + 2;
args = ZeroMalloc(sizeof(char *) * num_args);
args[0] = filename;
for (i = 1;i < num_args - 1;i++)
{
args[i] = t->Token[i - 1];
}
execvp(filename, args);
AbortExit();
}
execvp(filename, args);
AbortExit();
}
else
{
// Parent process
pid_t pid = (pid_t)ret;
Free(args);
FreeToken(t);
if (wait)
{