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 0000000000000000000000000000000000000000..0e4ddf541e1738d8673cd0199bb373eaec7587b3 --- /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 f8d67c5087f31f130ba896b03674f78803203372..7ce35d88f0a7cefaf7a80d75f65ddf75bdfcd99f 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