Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: sync professional #25

Merged
merged 15 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ License: CC0-1.0
Files: .obs/workflows.yml
Copyright: None
License: CC0-1.0

# ts
Files: *.ts .tx/*
Copyright: None
License: CC0-1.0
10 changes: 10 additions & 0 deletions .tx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[main]
host = https://www.transifex.com
minimum_perc = 80
mode = developer

[o:linuxdeepin:p:deepin-desktop-environment:r:plugin-oom]
file_filter = src/plugin-sdbus/plugin-oom/translations/plugin-oom_<lang>.ts
source_file = src/plugin-sdbus/plugin-oom/translations/plugin-oom.ts
source_lang = en
type = QT
2 changes: 2 additions & 0 deletions .tx/deepin.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[transifex]
branch = m20
7 changes: 6 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Build-Depends: debhelper (>= 11),
pkg-config,
cmake,
qtbase5-dev,
qttools5-dev-tools,
qttools5-dev,
libsystemd-dev
Standards-Version: 4.1.3
Homepage: https://github.com/linuxdeepin
Expand All @@ -15,6 +17,8 @@ Architecture: any
Depends:
${shlibs:Depends},
${misc:Depends}
Recommends:
deepin-service-plugin-oom
Description: deepin desktop-environment - service manager frame
Load system plug-ins for unified management

Expand All @@ -37,6 +41,7 @@ Architecture: any
Depends:
${shlibs:Depends},
${misc:Depends},
deepin-service-manager
deepin-service-manager,
qttools5-dev-tools
Description: deepin desktop-environment - oom service

3 changes: 3 additions & 0 deletions debian/deepin-service-plugin-oom.install
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/usr/share/deepin-service-manager/*/*oom*.json
/usr/share/dbus-1/system.d/org.deepin.oom1.conf
/usr/share/dsg/configs/org.deepin.oom/org.deepin.oom.json
/usr/lib/deepin-daemon/service-trigger/earlyoom.service.json
/usr/share/deepin-service-manager/oom/translations/*.qm
/usr/bin/getfromqm
9 changes: 9 additions & 0 deletions lupdate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: MIT

cd ./src/plugin-sdbus/plugin-oom
sh lupdate.sh
cd ../../../

tx push -s --branch m20
2 changes: 1 addition & 1 deletion src/plugin-sdbus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

option(ENABLE_PLUGIN_OOM "Build plugin-oom" OFF)
option(ENABLE_PLUGIN_OOM "Build plugin-oom" ON)

if (ENABLE_PLUGIN_OOM)
add_subdirectory("plugin-oom")
Expand Down
10 changes: 5 additions & 5 deletions src/plugin-sdbus/plugin-oom/3rdparty/earlyoom/kill.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ typedef struct
{
/* if the available memory AND swap goes below these percentages,
* we start killing processes */
int mem_term_percent;
int mem_kill_percent;
int swap_term_percent;
int swap_kill_percent;
double mem_term_percent;
double mem_kill_percent;
double swap_term_percent;
double swap_kill_percent;
/* send d-bus notifications? */
char *notify_ext;
/* kill all processes within a process group */
Expand All @@ -28,7 +28,7 @@ typedef struct
/* will ignore these processes. NULL = no-op. */
regex_t *ignore_regex;
/* memory report interval, in milliseconds */
int report_interval_ms;
double report_interval_ms;
/* Flag --dryrun was passed */
bool dryrun;
} poll_loop_args_t;
Expand Down
15 changes: 14 additions & 1 deletion src/plugin-sdbus/plugin-oom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ set(Plugin_NAME "plugin-oom")
project(${Plugin_NAME})

find_package(PkgConfig REQUIRED)
find_package(Qt5 COMPONENTS LinguistTools REQUIRED)
pkg_check_modules(Systemd REQUIRED IMPORTED_TARGET libsystemd)

# Sources files
file(GLOB_RECURSE SRCS "*.c" "*.h")

file(GLOB TS_FILES "translations/*.ts")
qt5_add_translation(QM_FILES ${TS_FILES})
add_custom_target(oomtranslations ALL DEPENDS ${QM_FILES})

add_library(${Plugin_NAME} MODULE
${SRCS}
)
Expand Down Expand Up @@ -40,6 +45,14 @@ install(FILES org.deepin.oom1.conf
DESTINATION share/dbus-1/system.d/
)

install(FILES org.deepin.oom.json
install(FILES org.deepin.oom.json
DESTINATION share/dsg/configs/org.deepin.oom/
)

install(FILES earlyoom.service.json
DESTINATION lib/deepin-daemon/service-trigger/)

# 安装.qm文件
install(FILES ${QM_FILES} DESTINATION share/deepin-service-manager/oom/translations)
# 安装getfromqm脚本
install(FILES getfromqm DESTINATION bin)
76 changes: 59 additions & 17 deletions src/plugin-sdbus/plugin-oom/dconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
sd_bus *bus, const char *path, const char *key, const char *outtype, void *output);
int match_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);

void compile_regex(const char *regex_str, regex_t **reg)
{
bool regex_is_empty = (regex_str == NULL || strcmp(regex_str, "") == 0);
if (!regex_is_empty) {
if (*reg == NULL) {
*reg = (regex_t *)malloc(sizeof(regex_t));
}
regcomp(*reg, regex_str, REG_EXTENDED);
} else if (*reg != NULL) {
free(*reg);
*reg = NULL;
}
}

void parse_config(sd_bus *bus, poll_loop_args_t *args)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
Expand Down Expand Up @@ -43,19 +57,24 @@
}

/* Get config value */
get_config_value(bus, path, "mem_term_percent", "x", &args->mem_term_percent);
get_config_value(bus, path, "mem_kill_percent", "x", &args->mem_kill_percent);
get_config_value(bus, path, "swap_term_percent", "x", &args->swap_term_percent);
get_config_value(bus, path, "swap_kill_percent", "x", &args->swap_kill_percent);
get_config_value(bus, path, "report_interval_ms", "x", &args->report_interval_ms);
get_config_value(bus, path, "mem_term_percent", "d", &args->mem_term_percent);
get_config_value(bus, path, "mem_kill_percent", "d", &args->mem_kill_percent);
get_config_value(bus, path, "swap_term_percent", "d", &args->swap_term_percent);
get_config_value(bus, path, "swap_kill_percent", "d", &args->swap_kill_percent);
get_config_value(bus, path, "report_interval_ms", "d", &args->report_interval_ms);
get_config_value(bus, path, "kill_process_group", "b", &args->kill_process_group);
get_config_value(bus, path, "ignore_root_user", "b", &args->ignore_root_user);
get_config_value(bus, path, "prefer_regex", "s", &args->prefer_regex);
get_config_value(bus, path, "avoid_regex", "s", &args->avoid_regex);
get_config_value(bus, path, "ignore_regex", "s", &args->ignore_regex);

char *regex_str = NULL;
get_config_value(bus, path, "prefer_regex", "s", &regex_str);
compile_regex(regex_str, &args->prefer_regex);
get_config_value(bus, path, "avoid_regex", "s", &regex_str);
compile_regex(regex_str, &args->avoid_regex);
get_config_value(bus, path, "ignore_regex", "s", &regex_str);
compile_regex(regex_str, &args->ignore_regex);

/* connect signal */
r = sd_bus_match_signal(bus,

Check warning on line 77 in src/plugin-sdbus/plugin-oom/dconfig.c

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'r' is assigned a value that is never used.
NULL,
"org.desktopspec.ConfigManager",
path,
Expand Down Expand Up @@ -108,37 +127,60 @@
void *value;
} config_value_t;

typedef struct
{
char *name;
regex_t **rex;
} regex_value_t;

int match_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
poll_loop_args_t *args = userdata;
const char *value;
sd_bus *bus = sd_bus_message_get_bus(m);
const char *path = sd_bus_message_get_path(m);
const config_value_t configs[] = {
{ "mem_term_percent", "x", &args->mem_term_percent },
{ "mem_kill_percent", "x", &args->mem_kill_percent },
{ "swap_term_percent", "x", &args->swap_term_percent },
{ "swap_kill_percent", "x", &args->swap_kill_percent },
{ "report_interval_ms", "x", &args->report_interval_ms },
{ "mem_term_percent", "d", &args->mem_term_percent },
{ "mem_kill_percent", "d", &args->mem_kill_percent },
{ "swap_term_percent", "d", &args->swap_term_percent },
{ "swap_kill_percent", "d", &args->swap_kill_percent },
{ "report_interval_ms", "d", &args->report_interval_ms },
{ "kill_process_group", "b", &args->kill_process_group },
{ "ignore_root_user", "b", &args->ignore_root_user },
{ "prefer_regex", "s", &args->prefer_regex },
{ "avoid_regex", "s", &args->avoid_regex },
{ "ignore_regex", "s", &args->ignore_regex },
};
int r;
r = sd_bus_message_read(m, "s", &value);
if (r < 0) {
warn("Failed to parse response message: %s\n", strerror(-r));
return r;
}
for (int i = 0; i < sizeof(configs) / sizeof(config_value_t); i++) {
bool find = false;
for (unsigned long i = 0; i < sizeof(configs) / sizeof(config_value_t); i++) {
if (strcmp(configs[i].name, value) == 0) {
get_config_value(bus, path, value, configs[i].type, configs[i].value);
find = true;
printf("update config %s = %d\n", configs[i].name, *(int *)configs[i].value);
break;
}
}

if (!find) {
const regex_value_t regex_configs[] = {
{ "prefer_regex", &args->prefer_regex },
{ "avoid_regex", &args->avoid_regex },
{ "ignore_regex", &args->ignore_regex },
};
for (unsigned long i = 0; i < sizeof (regex_configs) / sizeof (regex_value_t); i++) {
if (strcmp(regex_configs[i].name, value) == 0) {
char *regexStr = NULL;
get_config_value(bus, path, value, "s", &regexStr);
compile_regex(regexStr, regex_configs[i].rex);
find = true;

Check warning on line 178 in src/plugin-sdbus/plugin-oom/dconfig.c

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'find' is assigned a value that is never used.
printf("update config %s = %s\n", configs[i].name, regexStr);
break;
}
}
}

return 0;
}
19 changes: 19 additions & 0 deletions src/plugin-sdbus/plugin-oom/earlyoom.service.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Monitor": {
"Type": "DBus",
"DBus": {
"BusType": "System",
"Sender": "org.deepin.oom1",
"Interface": "org.deepin.oom1",
"Path": "/org/deepin/oom1",
"Signal": "Killed"
}
},

"Name": "OOM Killed",
"Description": "",
"Exec": ["sh", "-c",
"msg=$(getfromqm deepin-service-manager/oom plugin-oom '%s process consuming too much memory has been terminated to ensure system stability and performance');msg=$(printf \"$msg\" $1);title=$(getfromqm deepin-service-manager/oom plugin-oom 'Process terminated');title=$(printf \"$title\" $1);gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.Notify dde-control-center 0 dialog-warning \"$title\" \"$msg\" '[]' '{}' 5000" ,
"",
"%{arg2}"]
}
35 changes: 35 additions & 0 deletions src/plugin-sdbus/plugin-oom/getfromqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: MIT

lanugage=locale.$LANGUAGE
lanugage=${lanugage#*.}

# 定义要提取的文本字符串
source_text=$3

# 定义.qm文件的路径
qm_file="/usr/share/$1/translations/$2_$lanugage.qm"

# 使用 lconvert 命令将 .qm 文件转换为临时 .ts 文件
export QT_SELECT=qt5
lconvert -i "$qm_file" -o /tmp/$2_$lanugage.ts
tmptsfile=/tmp/$2_$lanugage.ts

# 在临时 .ts 文件中查找并提取源文本的翻译
translation=$(grep -A 1 "<source>$source_text</source>" $tmptsfile | grep '<translation>' | head -n 1)
start="\<translation\>"
end="</translation>"
translation=$(echo "$translation" | grep -oP "(?<=$start).*?(?=$end)")

# 删除临时 .ts 文件
rm $tmptsfile

# 输出翻译结果
if [ -z "$translation" ]; then
echo $source_text
else
echo $translation
fi
44 changes: 44 additions & 0 deletions src/plugin-sdbus/plugin-oom/lupdate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: MIT

#判断文件夹是否存在,如果不存在,就新建文件夹

if [ ! -d "translations" ]; then
mkdir translations
fi
cd translations
if [ -e "plugin-oom.ts" ]; then
rm plugin-oom.ts
fi
touch plugin-oom.ts

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" >> ./plugin-oom.ts
echo "<!DOCTYPE TS>" >> ./plugin-oom.ts
echo "<TS version=\"2.1\">" >> ./plugin-oom.ts
echo "<context>" >> ./plugin-oom.ts
echo " <name>oom</name>" >> ./plugin-oom.ts

# 读取json文件内容
json=$(cat "../earlyoom.service.json")

# 解析json内容,并拿到要执行的内容
value=$(echo "$json" | jq '.Exec[2]')
# 将字符串按照分号进行分割并循环遍历
echo "$value" | tr ';' '\n' | while read -r element; do
result=$(echo $element | grep "getfromqm")
if [ "$result" != "" ]
then
content=$(echo $result | awk -F "'" '{print $2}')
echo " <message>" >> ./plugin-oom.ts
echo " <source>$content</source>" >> ./plugin-oom.ts
echo " <translation type=\"unfinished\"></translation>" >> ./plugin-oom.ts
echo " </message>" >> ./plugin-oom.ts
fi
done

echo "</context>" >> ./plugin-oom.ts
echo "</TS>" >> ./plugin-oom.ts
cd ../
8 changes: 4 additions & 4 deletions src/plugin-sdbus/plugin-oom/oom.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ void start_oom(poll_loop_args_t *args)
m.UserMemTotalKiB / 1024,
m.SwapTotalKiB / 1024);
fprintf(stderr,
"sending SIGTERM when mem <= %d%% and swap <= %d%%\n",
"sending SIGTERM when mem <= %f%% and swap <= %f%%\n",
args->mem_term_percent,
args->swap_term_percent);
fprintf(stderr,
" SIGKILL when mem <= %d%% and swap <= %d%%\n",
" SIGKILL when mem <= %f%% and swap <= %f%%\n",
args->mem_kill_percent,
args->swap_kill_percent);
int err = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT);
Expand Down Expand Up @@ -157,12 +157,12 @@ static void poll_loop(const poll_loop_args_t *args)
int sig = lowmem_sig(args, &m);
if (sig == SIGKILL) {
print_mem_stats(warn, m);
warn("low memory! at or below SIGKILL limits: mem %d%%, swap %d%%\n",
warn("low memory! at or below SIGKILL limits: mem %f%%, swap %f%%\n",
args->mem_kill_percent,
args->swap_kill_percent);
} else if (sig == SIGTERM) {
print_mem_stats(warn, m);
warn("low memory! at or below SIGTERM limits: mem %d%%, swap %d%%\n",
warn("low memory! at or below SIGTERM limits: mem %f%%, swap %f%%\n",
args->mem_term_percent,
args->swap_term_percent);
}
Expand Down
3 changes: 1 addition & 2 deletions src/plugin-sdbus/plugin-oom/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ static char *dbus_path = "/org/deepin/oom1";
static char *dbus_interface = "org.deepin.oom1";
static const sd_bus_vtable oom_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_SIGNAL_WITH_ARGS(
"Killed", SD_BUS_ARGS("i", pid, "i", uid, "s", name, "i", badness, "t", rss), 0),
SD_BUS_SIGNAL("Killed", "iisit", 0),
SD_BUS_VTABLE_END
};

Expand Down
Loading