diff --git a/tools/deepin-os-release/main.cpp b/tools/deepin-os-release/main.cpp index bdc0e48f..8416f2f7 100644 --- a/tools/deepin-os-release/main.cpp +++ b/tools/deepin-os-release/main.cpp @@ -1,7 +1,9 @@ -// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2017-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later +#define _GNU_SOURCE + #include "dsysinfo.h" #include @@ -11,9 +13,57 @@ #include #include +#include +#include +#include DCORE_USE_NAMESPACE +class StderrFilterThread : public QThread +{ +public: + explicit StderrFilterThread(int origStderr, int readFd, QObject *parent = nullptr) + : QThread(parent), m_origStderr(origStderr), m_readFd(readFd) {} + +protected: + void run() override + { + FILE *readEnd = fdopen(m_readFd, "r"); + if (!readEnd) { + close(m_readFd); // Close unused file descriptors + return; + } + + char *line = nullptr; + size_t len = 0; + while (getline(&line, &len, readEnd) != -1) { + if (strstr(line, "WARNING: CPU random generator seem to be failing") || + strstr(line, "WARNING: RDRND generated:")) { + continue; // Discard this row + } + // Use a loop to ensure complete writing + size_t total = 0; + size_t remaining = strlen(line); + const char *buf = line; + while (total < remaining) { + ssize_t written = write(m_origStderr, buf + total, remaining - total); + if (written < 0) { + if (errno == EINTR) + continue; // Interrupted by signal, retry + break; // Other errors, abandon writing + } + total += written; + } + } + free(line); + fclose(readEnd); + } + +private: + int m_origStderr; + int m_readFd; +}; + bool distributionInfoValid() { return QFile::exists(DSysInfo::distributionInfoPath()); } @@ -27,6 +77,16 @@ void printDistributionOrgInfo(DSysInfo::OrgType type) { int main(int argc, char *argv[]) { + // Set filter + int origStderr = dup(STDERR_FILENO); + int pipefd[2]; + pipe(pipefd); + dup2(pipefd[1], STDERR_FILENO); + close(pipefd[1]); + + StderrFilterThread *filterThread = new StderrFilterThread(origStderr, pipefd[0]); + filterThread->start(); + QCoreApplication app(argc, argv); Q_UNUSED(app) @@ -123,5 +183,12 @@ int main(int argc, char *argv[]) } } + // clean + dup2(origStderr, STDERR_FILENO); // Restore stderr + close(origStderr); + + filterThread->wait(); + delete filterThread; + return 0; }