diff --git a/src/app/dman.cpp b/src/app/dman.cpp index 340fa504..cf3b54c2 100755 --- a/src/app/dman.cpp +++ b/src/app/dman.cpp @@ -88,8 +88,11 @@ int main(int argc, char **argv) if (Utils::judgeLoongson()) { //add by wujian 20200907 for 解决龙芯平台,QWebEngine因字体库字体太多,造成启动失败的问题 QString strHomePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); - QString strExeShell = QString("rm -fr %1/.cache/fontconfig").arg(strHomePath); - shellObj::execSystem(strExeShell); + QString strCmd = QString("rm"); + QStringList args; + args << "-rf"; + args << QString("%1/.cache/fontconfig").arg(strHomePath); + shellObj::execSystem(strCmd, args); } //设置窗口属性 diff --git a/src/base/command.cpp b/src/base/command.cpp index 65d004a4..d8601728 100644 --- a/src/base/command.cpp +++ b/src/base/command.cpp @@ -11,45 +11,6 @@ namespace dman { -bool RunScriptFile(const QStringList &args) -{ - Q_ASSERT(!args.isEmpty()); - if (args.isEmpty()) { - qCritical() << "RunScriptFile() args is empty!"; - return false; - } - - // Change working directory. - const QString current_dir(QFileInfo(args.at(0)).absolutePath()); - if (!QDir::setCurrent(current_dir)) { - qCritical() << "Failed to change working directory:" << current_dir; - return false; - } - - // NOTE(xushaohua): If args[0] is not a script file, bash may raise - // error message. - return SpawnCmd("/bin/bash", args); -} - -bool RunScriptFile(const QStringList &args, QString &output, QString &err) -{ - Q_ASSERT(!args.isEmpty()); - if (args.isEmpty()) { - qCritical() << "RunScriptFile() arg is empty!"; - return false; - } - - // Change working directory. - const QString current_dir(QFileInfo(args.at(0)).absolutePath()); - if (!QDir::setCurrent(current_dir)) { - qCritical() << "Failed to change working directory:" << current_dir; - return false; - } - - // TODO(xushaohua): Remove bash - return SpawnCmd("/bin/bash", args, output, err); -} - bool SpawnCmd(const QString &cmd, const QStringList &args) { QProcess process; diff --git a/src/base/command.h b/src/base/command.h index b0e85fa9..a8f93f1f 100644 --- a/src/base/command.h +++ b/src/base/command.h @@ -9,15 +9,6 @@ namespace dman { -// Run a script file in bash, no matter it is executable or not. -// First argument in |args| is the path to script file. -// Current working directory is changed to folder of |args[0]|. -// Returns true if |args[0]| executed and exited with 0. -// |output| and |err| are content of stdout and stderr. - -bool RunScriptFile(const QStringList &args); -bool RunScriptFile(const QStringList &args, QString &output, QString &err); - // Run |cmd| with |args| in background and returns its result. bool SpawnCmd(const QString &cmd, const QStringList &args); bool SpawnCmd(const QString &cmd, const QStringList &args, QString &output); diff --git a/src/controller/shellobj.cpp b/src/controller/shellobj.cpp index 20a053c6..577afcce 100644 --- a/src/controller/shellobj.cpp +++ b/src/controller/shellobj.cpp @@ -21,18 +21,19 @@ shellObj::~shellObj() thread = nullptr; } -shellObj &shellObj::execSystem(const QString &cmds) +shellObj &shellObj::execSystem(const QString &cmds, const QStringList &args) { shellObj *shell = new shellObj(); qDebug() << "shell exec:" << cmds; //执行shell命令 - shell->startSystemThread(cmds); + shell->startSystemThread(cmds, args); return *shell; } -int shellObj::startSystemThread(const QString &cmd) +int shellObj::startSystemThread(const QString &cmd, const QStringList &args) { - this->cmd = cmd; + m_cmd = cmd; + m_args = args; thread = new QThread(); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); moveToThread(thread); @@ -45,8 +46,10 @@ int shellObj::startSystemThread(const QString &cmd) void shellObj::runSystem() { - system(cmd.toStdString().data()); - qDebug() << "shell " << cmd << endl; -// emit onCompleted(iRst < 0 ? "error" : ""); + QProcess process; + process.start(m_cmd, m_args); + process.waitForFinished(-1); + + qDebug() << "shell cmd:" << m_cmd << "args:" << m_args; delete this; } diff --git a/src/controller/shellobj.h b/src/controller/shellobj.h index 2c3027e9..b708f915 100644 --- a/src/controller/shellobj.h +++ b/src/controller/shellobj.h @@ -24,11 +24,11 @@ class shellObj : public QObject * @param cmds 命令内容 * @return 新建的DMailShell对象 */ - static shellObj &execSystem(const QString &cmds); + static shellObj &execSystem(const QString &cmds, const QStringList &m_args = QStringList()); private: - int startSystemThread(const QString &cmd); + int startSystemThread(const QString &m_cmd, const QStringList &m_args); /** * @brief runSystem 采用system来执行shell命令 */ @@ -38,7 +38,12 @@ class shellObj : public QObject /** * @brief cmd 保存shell命令 */ - QString cmd; + QString m_cmd; + + /** + * @brief args 命令参数 + */ + QStringList m_args; /** * @brief thread 线程对象 diff --git a/src/dbus/dmanwatcher.cpp b/src/dbus/dmanwatcher.cpp index 856c6808..d6cd0245 100644 --- a/src/dbus/dmanwatcher.cpp +++ b/src/dbus/dmanwatcher.cpp @@ -21,11 +21,21 @@ DManWatcher::DManWatcher():m_Timer(new QTimer (this)) void DManWatcher::onTimeOut() { QString cmd, outPut; + QStringList args; //判断dman客户端是否存在,如果不存在退出服务。 - cmd = QString("ps aux | grep -w dman$"); - outPut= executCmd(cmd); - int ret = outPut.length(); - if (!ret) + cmd = "ps"; + args << "aux"; + outPut= executCmd(cmd, args); + bool bHasDman = false; + QStringList rows = outPut.split('\n'); + for (auto line : rows) { + QStringList items = line.split(' '); + if (items.contains("dman")) { + bHasDman = true; + break; + } + } + if (!bHasDman) QCoreApplication::exit(0); } @@ -33,10 +43,10 @@ void DManWatcher::onTimeOut() * @brief 执行外部命令 * @param strCmd:外部命令字符串 */ -QString DManWatcher::executCmd(const QString &strCmd) +QString DManWatcher::executCmd(const QString &strCmd, const QStringList &args) { QProcess proc; - proc.start("bash", QStringList() << "-c" << strCmd); + proc.start(strCmd, args); proc.waitForFinished(-1); return proc.readAllStandardOutput(); } diff --git a/src/dbus/dmanwatcher.h b/src/dbus/dmanwatcher.h index a1f546f9..83f0046f 100644 --- a/src/dbus/dmanwatcher.h +++ b/src/dbus/dmanwatcher.h @@ -21,7 +21,7 @@ class DManWatcher :public QObject public Q_SLOTS: void onTimeOut(); private: - QString executCmd(const QString &strCmd); + QString executCmd(const QString &strCmd, const QStringList &args = QStringList()); private: QTimer *m_Timer=nullptr; };