|
| 1 | +// SPDX-FileCopyrightText: Team OpenVPI |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include "ChildProcess.h" |
| 5 | + |
| 6 | +#if defined(Q_OS_WIN) |
| 7 | +# include <cstddef> |
| 8 | +# include <memory> |
| 9 | +#elif defined(Q_OS_LINUX) |
| 10 | +# include <signal.h> |
| 11 | +# include <sys/prctl.h> |
| 12 | +# include <unistd.h> |
| 13 | +# include <cerrno> |
| 14 | +#endif |
| 15 | + |
| 16 | +#include <QLoggingCategory> |
| 17 | +#include <QProcess> |
| 18 | +#include <QString> |
| 19 | + |
| 20 | +#if defined(Q_OS_WIN) |
| 21 | +# include <qt_windows.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +namespace Core { |
| 25 | + |
| 26 | +#if defined(Q_OS_WIN) |
| 27 | + Q_STATIC_LOGGING_CATEGORY(lcChildProcess, "diffscope.core.childprocess") |
| 28 | + |
| 29 | + namespace { |
| 30 | + |
| 31 | + struct ChildProcessJob { |
| 32 | + HANDLE m_handle{}; |
| 33 | + STARTUPINFOEXW m_startupInfo{}; |
| 34 | + std::unique_ptr<std::byte[]> m_attributeStorage; |
| 35 | + |
| 36 | + ~ChildProcessJob() { |
| 37 | + if (m_startupInfo.lpAttributeList) |
| 38 | + DeleteProcThreadAttributeList(m_startupInfo.lpAttributeList); |
| 39 | + if (m_handle) |
| 40 | + CloseHandle(m_handle); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + bool reportWindowsError(const char *operation, QString *errorMessage) { |
| 45 | + const auto error = GetLastError(); |
| 46 | + qCWarning(lcChildProcess) << operation << "failed with error" << error; |
| 47 | + if (errorMessage) { |
| 48 | + wchar_t buffer[1024]{}; |
| 49 | + const auto length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error, 0, buffer, sizeof(buffer) / sizeof(buffer[0]), nullptr); |
| 50 | + const auto message = length ? QString::fromWCharArray(buffer, length).trimmed() : QString::number(error); |
| 51 | + *errorMessage = QString::fromLatin1(operation) + QStringLiteral(": ") + message; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | +#endif |
| 58 | + |
| 59 | + bool configureChildProcess(QProcess *process, QString *errorMessage) { |
| 60 | + Q_ASSERT(process); |
| 61 | + Q_ASSERT(process->state() == QProcess::NotRunning); |
| 62 | + if (errorMessage) |
| 63 | + errorMessage->clear(); |
| 64 | +#if defined(Q_OS_WIN) |
| 65 | + auto job = std::make_shared<ChildProcessJob>(); |
| 66 | + job->m_handle = CreateJobObjectW(nullptr, nullptr); |
| 67 | + if (!job->m_handle) |
| 68 | + return reportWindowsError("CreateJobObjectW", errorMessage); |
| 69 | + |
| 70 | + JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits{}; |
| 71 | + limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; |
| 72 | + if (!SetInformationJobObject(job->m_handle, JobObjectExtendedLimitInformation, &limits, sizeof(limits))) |
| 73 | + return reportWindowsError("SetInformationJobObject", errorMessage); |
| 74 | + |
| 75 | + SIZE_T attributeSize{}; |
| 76 | + InitializeProcThreadAttributeList(nullptr, 1, 0, &attributeSize); |
| 77 | + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| 78 | + return reportWindowsError("InitializeProcThreadAttributeList", errorMessage); |
| 79 | + job->m_attributeStorage = std::make_unique<std::byte[]>(attributeSize); |
| 80 | + auto attributes = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(job->m_attributeStorage.get()); |
| 81 | + if (!InitializeProcThreadAttributeList(attributes, 1, 0, &attributeSize)) |
| 82 | + return reportWindowsError("InitializeProcThreadAttributeList", errorMessage); |
| 83 | + job->m_startupInfo.lpAttributeList = attributes; |
| 84 | + if (!UpdateProcThreadAttribute(attributes, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, &job->m_handle, sizeof(job->m_handle), nullptr, nullptr)) |
| 85 | + return reportWindowsError("UpdateProcThreadAttribute", errorMessage); |
| 86 | + |
| 87 | + process->setCreateProcessArgumentsModifier([job](QProcess::CreateProcessArguments *arguments) { |
| 88 | + job->m_startupInfo.StartupInfo = *arguments->startupInfo; |
| 89 | + job->m_startupInfo.StartupInfo.cb = sizeof(STARTUPINFOEXW); |
| 90 | + arguments->startupInfo = &job->m_startupInfo.StartupInfo; |
| 91 | + arguments->flags |= EXTENDED_STARTUPINFO_PRESENT; |
| 92 | + }); |
| 93 | +#elif defined(Q_OS_LINUX) |
| 94 | + const auto parentPid = getpid(); |
| 95 | + process->setChildProcessModifier([process, parentPid] { |
| 96 | + if (prctl(PR_SET_PDEATHSIG, static_cast<unsigned long>(SIGKILL), 0UL, 0UL, 0UL) == -1) |
| 97 | + process->failChildProcessModifier("prctl(PR_SET_PDEATHSIG)", errno); |
| 98 | + if (getppid() != parentPid) |
| 99 | + process->failChildProcessModifier("getppid", ESRCH); |
| 100 | + }); |
| 101 | +#else |
| 102 | + Q_UNUSED(process) |
| 103 | +#endif |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments