From 2445af21a55646a4ae8075da521b973a7c021865 Mon Sep 17 00:00:00 2001 From: ruizhe Date: Thu, 19 Oct 2023 13:58:03 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E4=BF=AE=E5=A4=8D=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E6=97=B6=E6=97=A0=E6=B3=95=E9=87=8D=E6=96=B0?= =?UTF-8?q?=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 --- debian/changelog | 6 +++++ getcontent.cpp | 69 ++++++++++++++++++++++++++---------------------- main.cpp | 30 ++++++++++++++------- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/debian/changelog b/debian/changelog index 023d17f..73f16a5 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +nfs-logtools (1.0.0-6) unstable; urgency=medium + + * fix: cannot reopen when program crashes. + + -- ruizhe Thu, 19 Oct 2023 13:56:13 +0800 + nfs-logtools (1.0.0-5) unstable; urgency=medium * fix: after export all log program abort. diff --git a/getcontent.cpp b/getcontent.cpp index 8931bbb..4263925 100644 --- a/getcontent.cpp +++ b/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/main.cpp b/main.cpp index decdbc1..ed9baf4 100644 --- a/main.cpp +++ b/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; -- Gitee