From ebc56a925c6a1aac61ef66b228b336259264c625 Mon Sep 17 00:00:00 2001 From: DuanRay Date: Wed, 18 Oct 2023 13:44:43 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BF=AE=E5=A4=8D=E5=BD=93=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=B4=A9=E6=BA=83=E6=97=B6=E4=B8=8D=E8=83=BD=E5=86=8D?= =?UTF-8?q?=E6=AC=A1=E6=89=93=E5=BC=80=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...x-cannot-reopen-when-program-crashes.patch | 144 ++++++++++++++++++ nfs-logtools.spec | 6 +- 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 0005-fix-cannot-reopen-when-program-crashes.patch diff --git a/0005-fix-cannot-reopen-when-program-crashes.patch b/0005-fix-cannot-reopen-when-program-crashes.patch new file mode 100644 index 0000000..0e4ddf5 --- /dev/null +++ b/0005-fix-cannot-reopen-when-program-crashes.patch @@ -0,0 +1,144 @@ +diff --git a/nfs-logtools-1.0.0/getcontent.cpp b/nfs-logtools-1.0.0/getcontent.cpp +index 8931bbb..4263925 100644 +--- a/nfs-logtools-1.0.0/getcontent.cpp ++++ b/nfs-logtools-1.0.0/getcontent.cpp +@@ -10,8 +10,9 @@ QString GetContent::getHomePath() + QStringList params; + QStringList getUserName; + QStringList curHome; ++ QString curUserByWho = ""; ++ QString curUserByLast = ""; + QString curUser = ""; +- QString ret =""; + QString tmpLine =""; + QStringList contentList; + QStringList tmpLineList; +@@ -19,44 +20,48 @@ QString GetContent::getHomePath() + QString curUsrPawdInfo = ""; + + /* 使用who命令获取当前用户名,如果多用户登灵会产生问题*/ +- /* execCmd->start("who", params); ++ execCmd->start("who", params); + execCmd->waitForFinished(); +- curUser = execCmd->readAllStandardOutput(); +- getUserName = curUser.split(" "); +- curUser = getUserName[0];*/ +- ++ curUserByWho = execCmd->readAllStandardOutput(); ++ + /*使用last命令获取首行still logged in的用户*/ + execCmd->start("last", params); + execCmd->waitForFinished(); +- ret = execCmd->readAllStandardOutput(); ++ curUserByLast = execCmd->readAllStandardOutput(); + +- //将整体返回的字符串按\n为一行,存储到list中 +- for (auto ch:ret) { +- if ('\n'!= ch) { +- tmpLine.append(ch); +- } +- else { +- contentList.append(tmpLine); +- tmpLine.clear(); ++ if (curUserByWho != NULL) { ++ getUserName = curUserByWho.split(" "); ++ curUser = getUserName[0]; ++ } ++ else if(curUserByLast != NULL) { ++ //将整体返回的字符串按\n为一行,存储到List中 ++ for (auto ch:curUserByLast) { ++ if ('\n' != ch) { ++ tmpLine.append(ch); ++ } ++ else { ++ contentList.append(tmpLine); ++ tmpLine.clear(); ++ } + } ++ ++ int i = 0; ++ do { ++ if (contentList[i].contains("root", Qt::CaseInsensitive)) { ++ continue; ++ } ++ else { ++ getUserName = contentList[i].split(" "); ++ curUser = getUserName[0]; ++ break; ++ } ++ i++; ++ }while(i < contentList.size()); + } +- +- //取第一个非root的用户 +- int i= 0; +- do { +- if (contentList[i].contains("root", Qt::CaseInsensitive)) { +- continue; +- } +- else { +- getUserName = contentList[i].split(" "); +- curUser = getUserName[0]; +- break; +- } +- i++; +- } while (istart("bash", QStringList() << "-c" << "cat /etc/passwd | grep " + curUser); + execCmd->waitForFinished(); + curUsrPawdInfo = execCmd->readAllStandardOutput(); +diff --git a/nfs-logtools-1.0.0/main.cpp b/nfs-logtools-1.0.0/main.cpp +index decdbc1..ed9baf4 100644 +--- a/nfs-logtools-1.0.0/main.cpp ++++ b/nfs-logtools-1.0.0/main.cpp +@@ -1,8 +1,25 @@ + #include + #include +-#include ++#include ++#include + #include "logtools.h" + ++bool checkOnly() ++{ ++ const char fileName[] = "/tmp/.logtoolslockfile"; ++ int fd = open (fileName, O_WRONLY | O_CREAT, 0644); ++ int flock = lockf(fd, F_TLOCK, 0); ++ if (-1 == fd) { ++ perror("open temp/.logtoolslockfile\n"); ++ return false; ++ } ++ if (-1 == flock) { ++ perror("lock temp/.logtoolslockfile\n"); ++ return false; ++ } ++ return true; ++} ++ + int main(int argc, char *argv[]) + { + qputenv("XDG_RUNTIME_DIR", "/tmp/runtime-root"); +@@ -12,15 +29,8 @@ int main(int argc, char *argv[]) + } + + QApplication a(argc, argv); +- +- //防止进程多开 +- QSharedMemory shareMem("nfs-logtools"); +- if (shareMem.isAttached()) { +- shareMem.detach(); +- } +- if (!shareMem.create(1)) { +- qDebug() << shareMem.errorString(); +- return -1; ++ if (!checkOnly()) { ++ return 0; + } + + QTranslator trans; diff --git a/nfs-logtools.spec b/nfs-logtools.spec index f8d67c5..7ce35d8 100644 --- a/nfs-logtools.spec +++ b/nfs-logtools.spec @@ -3,7 +3,7 @@ Name: nfs-logtools Version: 1.0.0 -Release: 4%{?dist} +Release: 5%{?dist} Summary: A log tools of Nfs. License: GPLv2+ Source0: %{name}-%{version}.tar.gz @@ -11,6 +11,7 @@ Patch0: 0001-fix-display-version-error.patch Patch1: 0002-adj-xsession-error-log-path.patch Patch2: 0003-auto-scaling-and-fix-error.patch Patch3: 0004-after-export-all-log-program-abort.patch +Patch4: 0005-fix-cannot-reopen-when-program-crashes.patch BuildRequires: qt5-qttools-devel qt5-qtbase-devel glibc-devel Requires: coreutils @@ -44,5 +45,8 @@ make %{?_smp_mflags} /usr/sbin/nfs-logtools %changelog +* Wed Oct 18 2023 ruizhe - 1.0.0-5 +- Fix cannot reopen when program crashes + * Wed Oct 18 2023 ruizhe - 1.0.0-4 - Fix: after export all log, program abort -- Gitee