📄 stafprocess.cpp
字号:
se.trace("STAFProcess::ProcessMonitorThread()"); } catch (...) { STAFTrace::trace(kSTAFTraceError, "Caught unknown exception in " "STAFProcess::ProcessMonitorThread()"); } return kSTAFUnknownError;}void AddProcessMonitor(const ProcessMonitorInfo &monitor){ STAFMutexSemLock lock(sMonitorDataSem); ProcessMonitorThreadList::iterator iter = sThreadList.begin(); for (; iter != sThreadList.end(); ++iter) { if ((*iter)->monitorMap.size() < (MAXIMUM_WAIT_OBJECTS - 1)) break; } ProcessMonitorThreadDataPtr threadData; if (iter == sThreadList.end()) { threadData = ProcessMonitorThreadDataPtr( new ProcessMonitorThreadData(), ProcessMonitorThreadDataPtr::INIT); sThreadList.push_back(threadData); sTempThreadData = threadData; sThreadSyncSem.reset(); STAFThreadID_t newThread = 0; unsigned int osRC = 0; STAFRC_t stafRC = STAFThreadStart(&newThread, ProcessMonitorThread, 0, 0, &osRC); if (stafRC != kSTAFOk) { WriteTraceError3("Error starting initial Process Manager thread in " "InitProcessManager, RC: " + STAFString(stafRC) + ", OS RC: " + STAFString(osRC)); } sThreadSyncSem.wait(); } else threadData = *iter; threadData->monitorMap[monitor.handle].push_back(monitor); SetEvent(threadData->monitorWakeUp);}STAFRC_t STAFProcessStart(STAFProcessID_t *processID, STAFProcessHandle_t *processHandle, void *data, unsigned int startDataLevel, unsigned int *osRC){ STAFString_t errorBuffer = 0; return STAFProcessStart2(processID, processHandle, data, startDataLevel, osRC, &errorBuffer);}STAFRC_t STAFProcessStart2(STAFProcessID_t *processID, STAFProcessHandle_t *processHandle, void *data, unsigned int startDataLevel, unsigned int *osRC, STAFString_t *errorBuffer){ if (data == 0) { if (errorBuffer) { STAFString errMsg = STAFString( "Invalid data provided to STAFProcessStart2()"); *errorBuffer = errMsg.adoptImpl(); } return kSTAFInvalidValue; } if (startDataLevel != 1) { if (errorBuffer) { STAFString errMsg = STAFString( "Invalid level provided to STAFProcessStart2(): ") + startDataLevel; *errorBuffer = errMsg.adoptImpl(); } return kSTAFInvalidValue; } STAFProcessStartInfoLevel1 *startData = reinterpret_cast<STAFProcessStartInfoLevel1 *>(data); InitProcessManager(); BOOL rc = TRUE; STAFRC_t ret = kSTAFOk; unsigned int systemErrorCode = 0; // determine what to do with authentication based on the following // variables: authMode, default username/password, request user - // name/password, and operating system STAFString username(startData->username); STAFString password(startData->password); bool mustValidate = true; STAFStringBufferPtr usernamePtr; if (startData->authMode == kSTAFProcessAuthDisabled) { mustValidate = false; if (startData->disabledAuthAction == kSTAFProcessDisabledAuthError) { if (username.length() != 0) { if (osRC) *osRC = EACCES; if (errorBuffer) { STAFString errMsg = STAFString( "Process authentication denied. You cannot specify a " "userid when process authentication is disabled and " "the process authentication disabled action is set " "to 'Error'."); *errorBuffer = errMsg.adoptImpl(); } return kSTAFProcessAuthenticationDenied; } } } STAFUserID_t hUsrToken = 0; STAFString errorMsg; unsigned int validUser = UserAuthenticate(hUsrToken, username, password, mustValidate, osRC, errorMsg); if (!validUser) { if (errorBuffer) *errorBuffer = errorMsg.adoptImpl(); return kSTAFProcessAuthenticationDenied; } PROFILEINFO profinfo = { sizeof profinfo, 0, NULL }; if (hUsrToken != 0) { usernamePtr = username.toCurrentCodePage(); profinfo.lpUserName = const_cast<char *>(usernamePtr->buffer()); } STAFString commandString; STAFString output; if (startData->commandType == kSTAFProcessShell) { if (startData->shellCommand != 0) { // Substitute the command and possibly other data commandString = startData->shellCommand; STAFProcessShellSubstitutionData subData; subData.command = startData->command; if (startData->parms != 0) { subData.command += " "; subData.command += startData->parms; } subData.title = startData->title; subData.workload = startData->workload; subData.username = username; subData.password = password; if (startData->stdinMode == kSTAFProcessIOReadFile) subData.stdinfile = "< " + STAFString(startData->stdinRedirect); if (startData->stdoutMode != kSTAFProcessIONoRedirect) { if (startData->stdoutMode == kSTAFProcessIOAppendFile) subData.stdoutfile = ">> " + STAFString(startData->stdoutRedirect); else subData.stdoutfile = "> " + STAFString(startData->stdoutRedirect); } if (startData->stderrMode == kSTAFProcessIOStdout) { subData.stdoutfile = subData.stdoutfile + " 2>&1"; } else if (startData->stderrMode != kSTAFProcessIONoRedirect) { if (startData->stderrMode == kSTAFProcessIOAppendFile) subData.stderrfile = "2>> " + STAFString(startData->stderrRedirect); else subData.stderrfile = "2> " + STAFString(startData->stderrRedirect); } int rc = STAFProcessReplaceShellSubstitutionChars(commandString, subData, output); if (rc != kSTAFOk) { if (errorBuffer) { STAFString errMsg = STAFString( "Invalid shell command: ") + startData->shellCommand; *errorBuffer = errMsg.adoptImpl(); } return rc; } commandString = output; } else { // Added double quotes to the beginning and end of the entire command/ // parms to handle cases where specifying cmd.exe with the /c option // does not preserve quotes. Do a cmd /? to get more info on this. if (STAFUtilWin32GetWinType() & kSTAFWinNTPlus) commandString = "cmd.exe /c \""; else commandString = "command.com /c "; commandString += startData->command; if (startData->parms != 0) { commandString += " "; commandString += startData->parms; } if (STAFUtilWin32GetWinType() & kSTAFWinNTPlus) commandString += "\""; } } else { // Not a shell command commandString = startData->command; if (startData->parms != 0) { commandString += " "; commandString += startData->parms; } } STAFStringBufferPtr commandPtr = commandString.toCurrentCodePage(); STAFStringBufferPtr titlePtr; STAFStringBufferPtr workdirPtr; char *command = const_cast<char *>(commandPtr->buffer()); char *workdir = 0; char *environment = startData->environment; STARTUPINFO startInfo = { 0 }; PROCESS_INFORMATION processInfo = { 0 }; startInfo.cb = sizeof(startInfo); if (startData->title != 0) { titlePtr = STAFString(startData->title).toCurrentCodePage(); startInfo.lpTitle = const_cast<char *>(titlePtr->buffer()); } if (startData->workdir != 0) { workdirPtr = STAFString(startData->workdir).toCurrentCodePage(); workdir = const_cast<char *>(workdirPtr->buffer()); } // Make sure that files specified for stdout and stderr are not the same // Note: In STAFProcessService, could only do a case-insensitive check // since Unix files are case-sensitive. if ((startData->stdoutMode != kSTAFProcessIONoRedirect) && (startData->stderrMode != kSTAFProcessIONoRedirect) && (STAFString(startData->stdoutRedirect).toLowerCase() == STAFString(startData->stderrRedirect).toLowerCase())) { if (errorBuffer) { STAFString errMsg = STAFString( "You cannot specify the same file name for stdout " "and stderr."); *errorBuffer = errMsg.adoptImpl(); } return kSTAFInvalidValue; } HANDLE newInpHandle; HANDLE newOutHandle; HANDLE newErrHandle; // set standard input, output, and error if (startData->stdinMode != kSTAFProcessIONoRedirect) { startInfo.dwFlags |= STARTF_USESTDHANDLES; newInpHandle = CreateFile( STAFString(startData->stdinRedirect). toCurrentCodePage()->buffer(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (newInpHandle == INVALID_HANDLE_VALUE) { if (ret == kSTAFOk) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString("Error creating ") + "stdin file: " + startData->stdinRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } } else newInpHandle = GetStdHandle(STD_INPUT_HANDLE); if (startData->stdoutMode != kSTAFProcessIONoRedirect) { startInfo.dwFlags |= STARTF_USESTDHANDLES; newOutHandle = CreateFile( STAFString(startData->stdoutRedirect). toCurrentCodePage()->buffer(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (newOutHandle == INVALID_HANDLE_VALUE) { if (ret == kSTAFOk) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -