Exemplo n.º 1
0
int
ExecuteCommand(
    char* path,
    char* CommandLine,
    DWORD millisecTimeout,
    void* envFlags)
{
    int rc;
    FILE* childOutput = NULL;
    char putEnvStr[BUFFER_SIZE];
    char ExecuteProgramCmdLine[BUFFER_SIZE];
    UINT prevmode;

    prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);

    // Always flush output buffers before executing a command. Note: this
    // shouldn't really be necessary because all output should be buffered
    // by the COutputBuffer class on a per-thread basis.
    fflush(stdout);
    fflush(stderr);

    strcpy_s(ExecuteProgramCmdLine, "cmd.exe /c "); // for .cmd/.bat scripts
    strcat_s(ExecuteProgramCmdLine, CommandLine);

    EnterCriticalSection(&csCurrentDirectory);

    // If we're doing executable tests, set the TARGET_VM environment variable.
    // We must do this inside a critical section since the environment is
    // not thread specific.

    if ((Mode == RM_EXE) && TargetVM) {
        sprintf_s(putEnvStr, "TARGET_VM=%s", TargetVM);
        _putenv(putEnvStr);
    }

    rc = _chdir(path);
    if (rc == 0) {
        childOutput = PipeSpawn(ExecuteProgramCmdLine, envFlags);
    }
    LeaveCriticalSection(&csCurrentDirectory);

    if (rc != 0) {
        LogError("Could not change directory to '%s' - errno == %d\n", path, errno);
    } else if (childOutput != NULL) {
        rc = FilterThread(childOutput, millisecTimeout);
        rc = PipeSpawnClose(childOutput, rc == WAIT_TIMEOUT);
    }

    SetErrorMode(prevmode);

    return rc;
}
Exemplo n.º 2
0
STREAM *STREAMSpawnCommand(const char *Command, const char *User, const char *Group, int Flags)
{
int to_fd, from_fd;
pid_t pid;
STREAM *S=NULL;
char *Tempstr=NULL;


if (Flags & SPAWN_TRUST_COMMAND) Tempstr=CopyStr(Tempstr,Command);
else Tempstr=MakeShellSafeString(Tempstr, Command, 0);

if (Flags & COMMS_BY_PTY)
{
	pid=PseudoTTYSpawn(&to_fd,Tempstr,User,Group,Flags);
	if (pid > 0) S=STREAMFromFD(to_fd);
}
else 
{
	if (Flags & COMMS_COMBINE_STDERR)
	{
		pid=PipeSpawn(&to_fd, &from_fd, COMMS_COMBINE_STDERR, Tempstr,User,Group);
	}
	else pid=PipeSpawn(&to_fd, &from_fd, NULL, Tempstr,User,Group);
	if (pid > 0) S=STREAMFromDualFD(from_fd, to_fd);
}

if (S)
{
	STREAMSetFlushType(S,FLUSH_LINE,0,0);
	Tempstr=FormatStr(Tempstr,"%d",pid);
	STREAMSetValue(S,"PeerPID",Tempstr);
}

DestroyString(Tempstr);
return(S);
}