Skip to content

Commit

Permalink
fix: 修复设置服务启动方式失败问题
Browse files Browse the repository at this point in the history
非开发者模式下设置systemd服务失败(cli同样)调整
非开发者模式下的服务设置方式,改由后端服务执行.
* deepin-service-manager 唤起插件服务存在问题,
调整为独立应用管理.

Log: 修复设置服务启动方式失败问题
Bug: https://pms.uniontech.com/bug-view-241793.html
Change-Id: I577ac58d6061c8445e46c5e9d131483e6fe53715
  • Loading branch information
rb-union authored and deepin-bot[bot] committed May 7, 2024
1 parent 8b79632 commit 31c1575
Show file tree
Hide file tree
Showing 89 changed files with 1,658 additions and 54 deletions.
7 changes: 6 additions & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Copyright: UnionTech Software Technology Co., Ltd.
License: LGPL-3.0-or-later

# translations
Files: deepin-system-monitor-main/translations/* deepin-system-monitor-plugin/translations/* deepin-system-monitor-plugin-popup/translations/*
Files: deepin-system-monitor-main/translations/* deepin-system-monitor-plugin/translations/* deepin-system-monitor-plugin-popup/translations/* deepin-system-monitor-system-server/translations/*
Copyright: UnionTech Software Technology Co., Ltd.
License: LGPL-3.0-or-later

Expand Down Expand Up @@ -148,6 +148,11 @@ Files: deepin-system-monitor-plugin-popup/system/*
Copyright: UnionTech Software Technology Co., Ltd.
License: GPL-3.0-or-later

# deepin-system-monitor-system-server
Files: deepin-system-monitor-system-server/misc/*
Copyright: UnionTech Software Technology Co., Ltd.
License: GPL-3.0-or-later

# *.service
Files: *.service
Copyright: UnionTech Software Technology Co., Ltd.
Expand Down
17 changes: 12 additions & 5 deletions .tx/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@ host = https://www.transifex.com
minimum_perc = 80
mode = developer

[deepin-system-monitor.deepin-system-monitor]
[o:linuxdeepin:p:deepin-system-monitor:r:deepin-system-monitor]
file_filter = deepin-system-monitor-main/translations/deepin-system-monitor_<lang>.ts
source_file = deepin-system-monitor-main/translations/deepin-system-monitor.ts
source_lang = en_US
type = QT

[deepin-system-monitor.deepin-system-monitor-desktop]
[o:linuxdeepin:p:deepin-system-monitor:r:deepin-system-monitor-desktop]
file_filter = deepin-system-monitor-main/translations/desktop/desktop_<lang>.ts
source_file = deepin-system-monitor-main/translations/desktop/desktop.ts
source_lang = en_US
type = QT

[deepin-system-monitor.deepin-system-monitor-policy]
[o:linuxdeepin:p:deepin-system-monitor:r:deepin-system-monitor-policy]
file_filter = deepin-system-monitor-main/translations/policy/policy_<lang>.ts
source_file = deepin-system-monitor-main/translations/policy/policy.ts
preTranlate_file = deepin-system-monitor-main/translations/policy/policy.policy
source_lang = en_US
type = QT

#popup
[deepin-system-monitor.deepin-system-monitor-plugin-popup]
[o:linuxdeepin:p:deepin-system-monitor:r:deepin-system-monitor-plugin-popup]
file_filter = deepin-system-monitor-plugin-popup/translations/deepin-system-monitor-plugin-popup_<lang>.ts
source_file = deepin-system-monitor-plugin-popup/translations/deepin-system-monitor-plugin-popup.ts
source_lang = en_US
type = QT

#plugin
[deepin-system-monitor.deepin-system-monitor-plugin]
[o:linuxdeepin:p:deepin-system-monitor:r:deepin-system-monitor-plugin]
file_filter = deepin-system-monitor-plugin/translations/deepin-system-monitor-plugin_<lang>.ts
source_file = deepin-system-monitor-plugin/translations/deepin-system-monitor-plugin.ts
source_lang = en_US
type = QT

#system-server
[o:linuxdeepin:p:deepin-system-monitor:r:deepin-system-monitor-system-server-policy]
file_filter = deepin-system-monitor-system-server/translations/policy/policy_<lang>.ts
source_file = deepin-system-monitor-system-server/translations/policy/policy.ts
preTranlate_file = deepin-system-monitor-system-server/misc/com.deepin.deepin-system-monitor-system-server.policy
source_lang = en_US
type = QT
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ ADD_SUBDIRECTORY(deepin-system-monitor-plugin-popup)
ADD_SUBDIRECTORY(deepin-system-monitor-daemon)
#系统监视器提示
ADD_SUBDIRECTORY(deepin-system-monitor-server)
#系统监视器后端提权处理服务
ADD_SUBDIRECTORY(deepin-system-monitor-system-server)

#单元测试
if(DOTEST)
ADD_SUBDIRECTORY(tests)
Expand Down
3 changes: 2 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Build-Depends:
libgtest-dev,
libgmock-dev,
libdwayland-dev | libkf5wayland-dev,
libwayland-dev
libwayland-dev,
libpolkit-qt5-1-dev
Standards-Version: 3.9.8
Homepage: https://github.com/linuxdeepin/deepin-system-monitor

Expand Down
13 changes: 10 additions & 3 deletions debian/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
set -e

if [ "$1" = configure ]; then
if command -v setcap > /dev/null; then
if setcap cap_net_raw,cap_dac_read_search+ep /usr/bin/deepin-system-monitor;then
if command -v setcap >/dev/null; then
if setcap cap_net_raw,cap_dac_read_search+ep /usr/bin/deepin-system-monitor; then
chmod u-s /usr/bin/deepin-system-monitor
else
echo "Setcap failed, falling back to setuid" >&2
Expand All @@ -12,7 +12,14 @@ if [ "$1" = configure ]; then
else
echo "Setcap is not installed, falling back to setuid" >&2
chmod u+s /usr/bin/deepin-system-monitor
fi
fi
fi

# Restart service
if [ "$1" = "configure" ]; then
if [ -d /run/systemd/system ]; then
deb-systemd-invoke restart 'deepin-service-manager.service' >/dev/null || true
fi
fi

exit 0
136 changes: 92 additions & 44 deletions deepin-system-monitor-main/service/service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <QString>
#include <QtDBus>
#include <QTimer>
#include <QDBusConnection>
#include <QDBusInterface>

#include <memory>

Expand All @@ -41,6 +43,32 @@ using namespace dbus::common;
std::atomic<ServiceManager *> ServiceManager::m_instance;
std::mutex ServiceManager::m_mutex;

/**
@brief 非开发者模式下,使用后端 DBus 服务设置 systemd 服务 \a serviceName 的启动模式
*/
static bool setServiceEnable(const QString &servieName, bool enable, QString &errorString)
{
QDBusInterface interface("org.deepin.SystemMonitorSystemServer",
"/org/deepin/SystemMonitorSystemServer",
"org.deepin.SystemMonitorSystemServer",
QDBusConnection::systemBus());
QDBusReply<QString> retMsg = interface.call("setServiceEnable", servieName, enable);
errorString.clear();
if (!retMsg.isValid()) {
errorString = retMsg.error().message();
} else if (!retMsg.value().isEmpty()) {
errorString = retMsg.value();
}

if (!errorString.isEmpty()) {
qWarning() << QString("Set service %1 failed, error %1").arg(enable ? "enable" : "disable").arg(errorString);
return false;
} else {
qDebug() << QString("Set service %1 ret: %2").arg(enable ? "enable" : "disable").arg(retMsg.value());
return true;
}
}

CustomTimer::CustomTimer(ServiceManager *mgr, QObject *parent)
: QObject(parent), m_mgr(mgr)
{
Expand Down Expand Up @@ -324,61 +352,81 @@ ErrorContext ServiceManager::setServiceStartupMode(const QString &id, bool autoS
proc.setProcessChannelMode(QProcess::MergedChannels);
// {BIN_PKEXEC_PATH} {BIN_SYSTEMCTL_PATH} {enable/disable} {service}
QString action = autoStart ? "enable" : "disable";
bool useProcess = true;
if (developerMode) {
proc.start(BIN_PKEXEC_PATH, {BIN_SYSTEMCTL_PATH, action, id});
} else {
// Bug 241793 非开发者模式,使用后端DBus服务设置启动方式
#if 0
proc.start(BIN_SYSTEMCTL_PATH, {action, id});
#else
useProcess = false;
QString errorString;
bool dbusRet = setServiceEnable(id, autoStart, errorString);
if (!dbusRet) {
errno = 0;
ErrorContext errCtx {};
errCtx = errfmt(errCtx, errno, title, errorString);
return errCtx;
}
#endif
}
proc.waitForFinished(-1);
auto exitStatus = proc.exitStatus();

ErrorContext le {};
// exitStatus:
// crashed
//
// exitCode:
// 127 (pkexec) - not auth/cant auth/error
// 126 (pkexec) - auth dialog dismissed
// 0 (systemctl) - ok
// !0 (systemctl) - systemctl error, read stdout from child process
if (exitStatus == QProcess::CrashExit) {
errno = 0;
le = errfmt(le, errno, title, QApplication::translate("Service.Action.Set.Startup.Mode",
"Error: Failed to set service startup type due to the crashed sub process."));
return le;
} else {
auto exitCode = proc.exitCode();
if (exitCode == 127 || exitCode == 126) {
errno = EPERM;
le = errfmt(le, errno, title);
return le;
} else if (exitCode != 0) {
auto buf = proc.readAllStandardOutput();
if (useProcess) {
proc.waitForFinished(-1);
auto exitStatus = proc.exitStatus();

// exitStatus:
// crashed
//
// exitCode:
// 127 (pkexec) - not auth/cant auth/error
// 126 (pkexec) - auth dialog dismissed
// 0 (systemctl) - ok
// !0 (systemctl) - systemctl error, read stdout from child process
if (exitStatus == QProcess::CrashExit) {
errno = 0;
le = errfmt(le, errno, title, buf);
le = errfmt(le, errno, title, QApplication::translate("Service.Action.Set.Startup.Mode",
"Error: Failed to set service startup type due to the crashed sub process."));
return le;
} else {
// success - refresh service stat -send signal

// special case, do nothing there
if (id.endsWith("@"))
return ErrorContext();

Systemd1ManagerInterface mgrIf(DBUS_SYSTEMD1_SERVICE,
kSystemDObjectPath.path(),
QDBusConnection::systemBus());
auto buf = normalizeServiceId(id, {});
auto re = mgrIf.GetUnit(buf);
le = re.first;
if (le) {
if (le.getCode() == 3) {
auto o = Systemd1UnitInterface::normalizeUnitPath(buf);
updateServiceEntry(o.path());
} else {
return le;
}
auto exitCode = proc.exitCode();
if (exitCode == 127 || exitCode == 126) {
errno = EPERM;
le = errfmt(le, errno, title);
return le;
} else if (exitCode != 0) {
auto buf = proc.readAllStandardOutput();
errno = 0;
le = errfmt(le, errno, title, buf);
return le;
}
}
}

{
// success - refresh service stat -send signal

// special case, do nothing there
if (id.endsWith("@"))
return ErrorContext();

Systemd1ManagerInterface mgrIf(DBUS_SYSTEMD1_SERVICE,
kSystemDObjectPath.path(),
QDBusConnection::systemBus());
auto buf = normalizeServiceId(id, {});
auto re = mgrIf.GetUnit(buf);
le = re.first;
if (le) {
if (le.getCode() == 3) {
auto o = Systemd1UnitInterface::normalizeUnitPath(buf);
updateServiceEntry(o.path());
} else {
updateServiceEntry(re.second.path());
return le;
}
} else {
updateServiceEntry(re.second.path());
}
}
#endif
Expand Down
70 changes: 70 additions & 0 deletions deepin-system-monitor-system-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
cmake_minimum_required(VERSION 3.7)

set(BIN_NAME deepin-system-monitor-system-server)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong -D_FORTITY_SOURCE=1 -z noexecstack -pie -fPIC -z lazy")

# 设置包含头文件的时候不用包含路径 begin ****************************************************************************************
MACRO(SUBDIRLIST result curdir)
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${curdir}/${child})
LIST(APPEND dirlist ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
SUBDIRLIST(dirs ${CMAKE_CURRENT_SOURCE_DIR}/src)
foreach(dir ${dirs})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/${dir})
endforeach()
# 设置包含头文件的时候不用包含路径 end ****************************************************************************************

file(GLOB_RECURSE SRC_CPP ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
file(GLOB_RECURSE SRC_H ${CMAKE_CURRENT_LIST_DIR}/src/*.h)

find_package(Qt5 COMPONENTS Core DBus REQUIRED)
find_package(PolkitQt5-1 REQUIRED)

add_executable(${BIN_NAME}
${SRC_CPP}
${SRC_H}
)

target_include_directories(${BIN_NAME} PUBLIC
Qt5::Core
Qt5::DBus
PolkitQt5-1::Agent
)

target_link_libraries(${BIN_NAME} PRIVATE
Qt5::Core
Qt5::DBus
PolkitQt5-1::Agent
)

# Use deepin-service-manager mange DBus servicde
install(TARGETS ${BIN_NAME} DESTINATION /usr/lib/deepin-daemon/)
install(FILES ./misc/${BIN_NAME}.json DESTINATION share/deepin-service-manager/other/)
# DBus service and conf
install(FILES ./misc/org.deepin.SystemMonitorSystemServer.service DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/system-services)
install(FILES ./misc/org.deepin.SystemMonitorSystemServer.conf DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/system.d)
# Systemd
install(FILES ./misc/deepin-system-monitor-system-server.service DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/systemd/system/)
# Polkit; Update translation
set(POLICY_FILE org.deepin.deepin-system-monitor-system-server.policy)
find_program(POLICY_TS_CONV deepin-policy-ts-convert REQUIRED)
add_custom_target(APP_POLICY_FILE_TEST
ALL
COMMAND ${POLICY_TS_CONV} ts2policy ${CMAKE_CURRENT_SOURCE_DIR}/misc/${POLICY_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/translations/policy/ ${POLICY_FILE}.tmp
COMMAND mv ${POLICY_FILE}.tmp ${CMAKE_CURRENT_SOURCE_DIR}/misc/${POLICY_FILE}
)
install(FILES ./misc/${POLICY_FILE} DESTINATION ${CMAKE_INSTALL_DATADIR}/polkit-1/actions)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "org.deepin.SystemMonitorSystemServer",
"startType": "OnDemand",
"idleTime": 10,
"policy": [
{
"path": "/org/deepin/SystemMonitorSystemServer"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Deepin system monitor system server

[Service]
Type=dbus
BusName=com.deepin.SystemMonitorSystemServer
ExecStart=/usr/lib/deepin-daemon/deepin-system-monitor-system-server
StandardOutput=syslog
CapabilityBoundingSet=~
MemoryLimit=1G
IOWeight=200
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- -*- XML -*- -->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- Only root can own the service -->
<policy user="root">
<allow own="org.deepin.SystemMonitorSystemServer" />
</policy>
<!-- Allow anyone to invoke methods on the interfaces -->
<policy context="default">
<allow own="org.deepin.SystemMonitorSystemServer" />
<allow send_destination="org.deepin.SystemMonitorSystemServer" />
<allow send_destination="org.deepin.SystemMonitorSystemServer" send_interface="org.deepin.SystemMonitorSystemServer" />
<allow send_destination="org.deepin.SystemMonitorSystemServer" send_interface="org.freedesktop.DBus.Properties" />
<allow send_destination="org.deepin.SystemMonitorSystemServer" send_interface="org.freedesktop.DBus.Introspectable" />
</policy>
</busconfig>
Loading

0 comments on commit 31c1575

Please sign in to comment.