20 #include <QCoreApplication>
24 #include <QStringList>
40 delete fdMap.value((x)); \
80 MThread(QString(
"SystemIOHandler%1").arg(
read ?
"R" :
"W")),
81 m_pWaitLock(), m_pWait(), m_pLock(), m_pMap(
PMap_t()),
91 LOG(VB_GENERAL, LOG_INFO, QString(
"Starting IO manager (%1)")
92 .arg(
m_read ?
"read" :
"write"));
111 bool datafound =
true;
119 PMap_t::iterator i, next;
140 if ( !PeekNamedPipe( h,
nullptr, 0,
nullptr, &lenAvail,
nullptr) )
143 if ( lenAvail > 65536 )
148 if ( !ReadFile( h, &
m_readbuf, lenAvail, &lenRead,
nullptr ) || lenRead == 0 )
154 buff->buffer().append(
m_readbuf, lenRead);
177 int pos = buff->pos();
178 DWORD len = buff->size() - pos;
180 len = (len > 32768 ? 32768 : len);
182 if( !WriteFile(h, buff->read(len).constData(), len, &rlen,
nullptr) )
189 buff->seek(pos+rlen);
205 while (
m_pMap.contains(h))
244 LOG(VB_GENERAL, LOG_INFO,
"Starting process manager");
263 if ( result == WAIT_TIMEOUT || result == WAIT_FAILED )
269 int index = result - WAIT_OBJECT_0;
286 LOG(VB_SYSTEM, LOG_ERR,
287 QString(
"Structure for child handle %1 already deleted!")
288 .arg((
long long)child));
302 GetExitCodeProcess( child, &status );
305 LOG(VB_SYSTEM, LOG_INFO,
306 QString(
"Managed child (Handle: %1) has exited! "
307 "command=%2, status=%3, result=%4")
308 .arg((
long long)child) .arg(ms->
GetLogCmd()) .arg(status)
313 auto now = SystemClock::now();
328 LOG(VB_SYSTEM, LOG_INFO,
329 QString(
"Managed child (Handle: %1) timed out, "
330 "issuing KILL signal").arg((
long long)child));
339 LOG(VB_SYSTEM, LOG_INFO,
340 QString(
"Managed child (Handle: %1) timed out"
341 ", issuing TERM signal").arg((
long long)child));
384 HANDLE *new_children;
389 LOG(VB_SYSTEM, LOG_CRIT,
"No memory to allocate new children");
415 QByteArray ba = ms->
GetBuffer(0)->data();
417 wtb.open(QIODevice::ReadOnly);
459 LOG(VB_GENERAL, LOG_INFO,
"Starting process signal handler");
581 LOG(VB_SYSTEM, LOG_INFO, QString(
"Child Handle %1 killed with %2")
582 .arg((
long long)
m_child).arg(sig));
583 TerminateProcess(
m_child, sig * 256 );
587 #define MAX_BUFLEN 1024
590 BOOL bInherit = FALSE;
594 LOG(VB_SYSTEM, LOG_DEBUG, QString(
"Launching: %1").arg(
GetLogCmd()));
596 HANDLE p_stdin[2] = {
nullptr,
nullptr };
597 HANDLE p_stdout[2] = {
nullptr,
nullptr };
598 HANDLE p_stderr[2] = {
nullptr,
nullptr };
600 SECURITY_ATTRIBUTES saAttr;
603 ZeroMemory(&si,
sizeof(STARTUPINFO));
604 si.cb =
sizeof(STARTUPINFO);
607 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
608 saAttr.bInheritHandle =
true;
609 saAttr.lpSecurityDescriptor =
nullptr;
616 if (!CreatePipe(&p_stdin[0], &p_stdin[1], &saAttr, 0))
618 LOG(VB_GENERAL, LOG_ERR,
LOC_ERR +
"stdin pipe() failed");
624 if (!SetHandleInformation(p_stdin[1], HANDLE_FLAG_INHERIT, 0))
626 LOG(VB_SYSTEM, LOG_ERR,
LOC_ERR +
"stdin inheritance error");
631 si.hStdInput = p_stdin[0];
632 si.dwFlags |= STARTF_USESTDHANDLES;
641 if (!CreatePipe(&p_stdout[0], &p_stdout[1], &saAttr, 0))
643 LOG(VB_SYSTEM, LOG_ERR,
LOC_ERR +
"stdout pipe() failed");
649 if (!SetHandleInformation(p_stdout[0], HANDLE_FLAG_INHERIT, 0))
651 LOG(VB_SYSTEM, LOG_ERR,
LOC_ERR +
"stdout inheritance error");
656 si.hStdOutput = p_stdout[1];
657 si.dwFlags |= STARTF_USESTDHANDLES;
666 if (!CreatePipe(&p_stderr[0], &p_stderr[1], &saAttr, 0))
668 LOG(VB_SYSTEM, LOG_ERR,
LOC_ERR +
"stderr pipe() failed");
674 if (!SetHandleInformation(p_stderr[0], HANDLE_FLAG_INHERIT, 0))
676 LOG(VB_SYSTEM, LOG_ERR,
LOC_ERR +
"stderr inheritance error");
681 si.hStdError = p_stderr[1];
682 si.dwFlags |= STARTF_USESTDHANDLES;
691 cmd.prepend(
"cmd.exe /c ");
699 PROCESS_INFORMATION pi;
700 ZeroMemory(&pi,
sizeof(PROCESS_INFORMATION));
704 : SystemClock::time_point();
706 LPCWSTR pDir =
nullptr;
707 if (dir.length() > 0)
708 pDir = (LPCWSTR)dir.utf16();
711 sprintf(sCmdChar,
"%ls", (LPWSTR)sCmd.utf16() );
714 sprintf(pDirChar,
"%ls", pDir);
716 bool success = CreateProcess(
nullptr,
729 DWORD dwErr = GetLastError();
730 LOG(VB_SYSTEM, LOG_ERR,
731 QString(
"%1 CreateProcess() failed (%2)")
742 LOG(VB_SYSTEM, LOG_INFO,
743 QString(
"Managed child (Handle: %1) has started! "
744 "%2%3 command=%4, timeout=%5")
747 .arg(
GetSetting(
"RunInBackground") ?
"&" :
"")