!29 update to 0.2.4

From: @openeuler-basic 
Reviewed-by: @licunlong 
Signed-off-by: @licunlong
This commit is contained in:
openeuler-ci-bot 2023-06-28 01:47:48 +00:00 committed by Gitee
commit 59fe0c4f5d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
31 changed files with 5 additions and 6205 deletions

View File

@ -1,184 +0,0 @@
From cef18c1e65333230fc8a9c45219a1c91105026c6 Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Tue, 6 Jun 2023 05:18:09 +0800
Subject: [PATCH] docs: Add signal processing instructions for init
---
core/bin/unit/util/unit_file.rs | 2 +-
docs/man/signal.md | 147 +++++++++++++++++---------------
2 files changed, 79 insertions(+), 70 deletions(-)
diff --git a/core/bin/unit/util/unit_file.rs b/core/bin/unit/util/unit_file.rs
index 0871703..d1f0bba 100644
--- a/core/bin/unit/util/unit_file.rs
+++ b/core/bin/unit/util/unit_file.rs
@@ -157,7 +157,7 @@ impl UnitFileData {
continue;
}
let real_path = tmp.parent().unwrap().join(real_path);
- let real_path = fs::canonicalize(&real_path).unwrap();
+ let real_path = fs::canonicalize(real_path).unwrap();
let path_toml = format!("{}.toml", real_path.to_string_lossy());
let to = Path::new(&path_toml);
if let Err(e) = std::fs::copy(&real_path, to) {
diff --git a/docs/man/signal.md b/docs/man/signal.md
index 6a61a25..3bf5253 100644
--- a/docs/man/signal.md
+++ b/docs/man/signal.md
@@ -1,75 +1,84 @@
# sysmaster的信号处理
-sysMaster采用`1+1+N`架构sysmaster不再作为init进程因此内核为init进程的信号做的特殊处理不再生效。下表列出了sysMaster与systemd针对不同信号的响应逻辑差异。
+sysMaster采用`1+1+N`架构sysmaster不再作为1号进程因此内核为1号进程的信号做的特殊处理不再生效。下表列出了init(1号进程)、sysmaster、systemd的信号处理方式以及sysMaster与systemd针对不同信号的响应逻辑差异。
-|信号|sysmaster|systemd|对外表现是否有差异|
-|-|-|-|-|
-|1) SIGHUP|daemon-reexec|daemon-reload|N|
-|2) SIGINT|start ctrl-alt-del.target|start ctrl-alt-del.target|N|
-|3) SIGQUIT|crash handler|crash handler|N|
-|4) SIGILL|crash handler|crash handler|N|
-|5) SIGTRAP|IGN|DFL|N|
-|6) SIGABRT|crash handler|crash handler|N|
-|7) SIGBUS|crash handler|crash handler|N|
-|8) SIGFPE|crash handler|crash handler|N|
-|9) SIGKILL|DFL|内核主动屏蔽该信号|Y|
-|10) SIGUSR1|IGN|重连dbus|Y|
-|11) SIGSEGV|crash handler|crash handler|N|
-|12) SIGUSR2|IGN|输出所有单元的配置信息|Y|
-|13) SIGPIPE|IGN|IGN|N|
-|14) SIGALRM|IGN|DFL|N|
-|15) SIGTERM|daemon-reexec|daemon-reexec|N|
-|16) SIGSTKFLT|IGN|DFL|N|
-|17) SIGCHLD|子进程回收|子进程回收|N|
-|18) SIGCONT|IGN|DFL|N|
-|19) SIGSTOP|DFL|内核主动屏蔽该信号|Y|
-|20) SIGTSTP|IGN|DFL|N|
-|21) SIGTTIN|IGN|DFL|N|
-|22) SIGTTOU|IGN|DFL|N|
-|23) SIGURG|IGN|DFL|N|
-|24) SIGXCPU|IGN|DFL|N|
-|25) SIGXFSZ|IGN|DFL|N|
-|26) SIGVTALRM|IGN|DFL|N|
-|27) SIGPROF|IGN|DFL|N|
-|28) SIGWINCH|IGN|start kbrequest.target|Y|
-|29) SIGIO|IGN|DFL|N|
-|30) SIGPWR|IGN|start sigpwr.target|Y|
-|31) SIGSYS|IGN|DFL|N|
-|34) SIGRTMIN|IGN|start default.target|Y|
-|35) SIGRTMIN+1|IGN|isolate rescue.target|Y|
-|36) SIGRTMIN+2|IGN|isolate emergency.target|Y|
-|37) SIGRTMIN+3|IGN|start halt.target|Y|
-|38) SIGRTMIN+4|IGN|start poweroff.target|Y|
-|39) SIGRTMIN+5|IGN|start reboot.target|Y|
-|40) SIGRTMIN+6|IGN|start kexec.target|Y|
-|41) SIGRTMIN+7|daemon-reexec|DFL|Y|
-|42) SIGRTMIN+8|IGN|DFL|N|
-|43) SIGRTMIN+9|IGN|DFL|N|
-|44) SIGRTMIN+10|switch root|DFL|Y|
-|45) SIGRTMIN+11|IGN|DFL|N|
-|46) SIGRTMIN+12|IGN|DFL|N|
-|47) SIGRTMIN+13|IGN|Immediate halt|Y|
-|48) SIGRTMIN+14|IGN|Immediate poweroff|Y|
-|49) SIGRTMIN+15|IGN|Immediate reboot|Y|
-|50) SIGRTMAX-14 SIGRTMIN+16|IGN|Immediate kexec|Y|
-|51) SIGRTMAX-13 SIGRTMIN+17|IGN|DFL|N|
-|52) SIGRTMAX-12 SIGRTMIN+18|IGN|DFL|N|
-|53) SIGRTMAX-11 SIGRTMIN+19|IGN|DFL|N|
-|54) SIGRTMAX-10 SIGRTMIN+20|IGN|enable status messages|Y|
-|55) SIGRTMAX-9 SIGRTMIN+21|IGN|disable status messages|Y|
-|56) SIGRTMAX-8 SIGRTMIN+22|IGN|日志级别设为debug|Y|
-|57) SIGRTMAX-7 SIGRTMIN+23|IGN|日志级别设为info|Y|
-|58) SIGRTMAX-6 SIGRTMIN+24|IGN|Immediate exit (仅限于用户模式)|Y|
-|59) SIGRTMAX-5 SIGRTMIN+25|IGN|reexecute manager|Y|
-|60) SIGRTMAX-4 SIGRTMIN+26|IGN|日志输出设为journal-or-kmsg|Y|
-|61) SIGRTMAX-3 SIGRTMIN+27|IGN|日志输出设为console|Y|
-|62) SIGRTMAX-2 SIGRTMIN+28|IGN|日志输出设为kmsg|Y|
-|63) SIGRTMAX-1 SIGRTMIN+29|IGN|日志输出设为syslog-or-kmsg|Y|
-|64) SIGRTMAX SIGRTMIN+30|IGN|DFL|N|
+|信号|init|sysmaster|systemd|sysmaster与systemd对外表现是否有差异|
+|-|-|-|-|-|
+|1) SIGHUP|捕获不处理|daemon-reexec|daemon-reload|N|
+|2) SIGINT|捕获不处理|start ctrl-alt-del.target|start ctrl-alt-del.target|N|
+|3) SIGQUIT|捕获不处理|故障恢复|crash handler|Y|
+|4) SIGILL|捕获不处理|故障恢复|crash handler|Y|
+|5) SIGTRAP|捕获不处理|IGN|DFL|N|
+|6) SIGABRT|捕获不处理|故障恢复|crash handler|Y|
+|7) SIGBUS|捕获不处理|故障恢复|crash handler|Y|
+|8) SIGFPE|捕获不处理|故障恢复|crash handler|Y|
+|9) SIGKILL|内核主动屏蔽该信号|DFL|内核主动屏蔽该信号|Y|
+|10) SIGUSR1|捕获不处理|IGN|重连dbus|Y|
+|11) SIGSEGV|捕获不处理|故障恢复|crash handler|Y|
+|12) SIGUSR2|捕获不处理|IGN|输出所有单元的配置信息|Y|
+|13) SIGPIPE|捕获不处理|IGN|IGN|N|
+|14) SIGALRM|捕获不处理|IGN|DFL|N|
+|15) SIGTERM|捕获不处理|daemon-reexec|daemon-reexec|N|
+|16) SIGSTKFLT|捕获不处理|IGN|DFL|N|
+|17) SIGCHLD|僵尸子进程回收|僵尸子进程回收|僵尸子进程回收|N|
+|18) SIGCONT|捕获不处理|IGN|DFL|N|
+|19) SIGSTOP|内核主动屏蔽该信号|DFL|内核主动屏蔽该信号|Y|
+|20) SIGTSTP|捕获不处理|IGN|DFL|N|
+|21) SIGTTIN|捕获不处理|IGN|DFL|N|
+|22) SIGTTOU|捕获不处理|IGN|DFL|N|
+|23) SIGURG|捕获不处理|IGN|DFL|N|
+|24) SIGXCPU|捕获不处理|IGN|DFL|N|
+|25) SIGXFSZ|捕获不处理|IGN|DFL|N|
+|26) SIGVTALRM|捕获不处理|IGN|DFL|N|
+|27) SIGPROF|捕获不处理|IGN|DFL|N|
+|28) SIGWINCH|捕获不处理|IGN|start kbrequest.target|Y|
+|29) SIGIO|捕获不处理|IGN|DFL|N|
+|30) SIGPWR|捕获不处理|IGN|start sigpwr.target|Y|
+|31) SIGSYS|捕获不处理|故障恢复|DFL|N|
+|34) SIGRTMIN|捕获不处理|IGN|start default.target|Y|
+|35) SIGRTMIN+1|捕获不处理|IGN|isolate rescue.target|Y|
+|36) SIGRTMIN+2|捕获不处理|IGN|isolate emergency.target|Y|
+|37) SIGRTMIN+3|捕获不处理|IGN|start halt.target|Y|
+|38) SIGRTMIN+4|捕获不处理|IGN|start poweroff.target|Y|
+|39) SIGRTMIN+5|捕获不处理|IGN|start reboot.target|Y|
+|40) SIGRTMIN+6|捕获不处理|IGN|start kexec.target|Y|
+|41) SIGRTMIN+7|捕获不处理|daemon-reexec|DFL|Y|
+|42) SIGRTMIN+8|unrecover state|IGN|DFL|N|
+|43) SIGRTMIN+9|重执行sysmaster|IGN|DFL|N|
+|44) SIGRTMIN+10|switch root|IGN|DFL|Y|
+|45) SIGRTMIN+11|捕获不处理|IGN|DFL|N|
+|46) SIGRTMIN+12|捕获不处理|IGN|DFL|N|
+|47) SIGRTMIN+13|捕获不处理|IGN|Immediate halt|Y|
+|48) SIGRTMIN+14|捕获不处理|IGN|Immediate poweroff|Y|
+|49) SIGRTMIN+15|捕获不处理|IGN|Immediate reboot|Y|
+|50) SIGRTMAX-14 SIGRTMIN+16|捕获不处理|IGN|Immediate kexec|Y|
+|51) SIGRTMAX-13 SIGRTMIN+17|捕获不处理|IGN|DFL|N|
+|52) SIGRTMAX-12 SIGRTMIN+18|捕获不处理|IGN|DFL|N|
+|53) SIGRTMAX-11 SIGRTMIN+19|捕获不处理|IGN|DFL|N|
+|54) SIGRTMAX-10 SIGRTMIN+20|捕获不处理|IGN|enable status messages|Y|
+|55) SIGRTMAX-9 SIGRTMIN+21|捕获不处理|IGN|disable status messages|Y|
+|56) SIGRTMAX-8 SIGRTMIN+22|捕获不处理|IGN|日志级别设为debug|Y|
+|57) SIGRTMAX-7 SIGRTMIN+23|捕获不处理|IGN|日志级别设为info|Y|
+|58) SIGRTMAX-6 SIGRTMIN+24|捕获不处理|IGN|Immediate exit (仅限于用户模式)|Y|
+|59) SIGRTMAX-5 SIGRTMIN+25|捕获不处理|IGN|reexecute manager|Y|
+|60) SIGRTMAX-4 SIGRTMIN+26|捕获不处理|IGN|日志输出设为journal-or-kmsg|Y|
+|61) SIGRTMAX-3 SIGRTMIN+27|捕获不处理|IGN|日志输出设为console|Y|
+|62) SIGRTMAX-2 SIGRTMIN+28|捕获不处理|IGN|日志输出设为kmsg|Y|
+|63) SIGRTMAX-1 SIGRTMIN+29|捕获不处理|IGN|日志输出设为syslog-or-kmsg|Y|
+|64) SIGRTMAX SIGRTMIN+30|捕获不处理|IGN|DFL|N|
表格的具体说明:
-1. IGN、DFL分别表示信号处理函数SIG_IGN忽略、SIG_DFL缺省的信号处理函数。如果init进程没有注册信号处理函数即使用SIG_DFL内核会直接屏蔽掉该信号。因此在对外表现上SIG_IGN和SIG_DFL是一致的。
-2. SIGKILL、SIGSTOP信号是内核为init进程无条件屏蔽的且不允许通过sigaction修改其信号处理函数sysmaster当前没有方案消除该差异。
-3. SIGUSR1、SIGUSR2、SIGWINCH、SIGPWR、SIGRTMIN+{0-6、13-16、20-29}的差异后续能够消除。
-4. SIGRTMIN+7在sysMaster中为init进程发给sysmaster进程主动触发热重启。SIGRTMIN+10在sysMaster中为init进程发给sysmaster执行switch root。
+1. IGN、DFL分别表示信号处理函数SIG_IGN忽略、SIG_DFL缺省的信号处理函数。如果1号进程没有注册信号处理函数即使用SIG_DFL内核会直接屏蔽掉该信号。因此1号进程在对外表现上SIG_IGN和SIG_DFL是一致的。
+2. SIGKILL、SIGSTOP信号是内核为1号进程无条件屏蔽的且不允许通过sigaction修改其信号处理函数sysmaster当前没有方案消除该差异。
+3. init进程
+ - SIGCHLD: 回收所有僵尸子进程。
+ - SIGRTMIN+8: 进入不可恢复状态。
+ - SIGRTMIN+9: 下发重执行sysmaster信号。
+ - SIGRTMIN+10: 只处理sysmaster进程的SIGRTMIN+10信号其余进程忽略。重执行init自身重执行sysmaster。
+ - init捕获所有可捕获信号除上述信号做了处理以外其余都忽略不处理。
+4. sysmaster进程
+ - SIGCHLD: 回收所有僵尸子进程。
+ - SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT,SIGSYS: 故障恢复重执行sysmaster。
+ - SIGRTMIN+7: 只处理init进程下发的SIGRTMIN+7信号sysmaster接收到该信号后主动触发热重启。
+ - SIGUSR1、SIGUSR2、SIGWINCH、SIGPWR、SIGRTMIN+{0-6、13-16、20-29}sysmaster与systemd的差异后续能够消除。
--
2.33.0

View File

@ -1,22 +0,0 @@
From 3db42578519edc87886b06428db5c31630957684 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 14 Jun 2023 11:35:13 +0800
Subject: [PATCH] docs: note that we only support no more than 500 units by
default
---
docs/man/all.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/docs/man/all.md b/docs/man/all.md
index 2b3ec2c..337e156 100644
--- a/docs/man/all.md
+++ b/docs/man/all.md
@@ -1,3 +1,4 @@
# Warnings
1. 由于Rust使用String字符串,对于长度无限制, 建议用户在撰写配置文件时控制在合理的长度内.
+2. sysmaster默认支持的unit最大数量为500个超过该值可能会一场退出。用户可以通过修改/etc/sysmaster/system.toml的DbSize配置提高unit上限。修改方法请参考[外置db配置](./sysmaster.conf.md)
--
2.30.2

View File

@ -1,106 +0,0 @@
From 5fbd070f79a0526f647fb5bb734ad9edac9e2fe1 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:08:24 +0800
Subject: [PATCH 3/9] feature: add as_any() to SubUnit so we can downcast
SubUnit trait to concrete componnent
---
coms/mount/src/unit.rs | 4 ++++
coms/service/src/unit.rs | 4 ++++
coms/socket/src/unit.rs | 5 +++++
coms/target/src/unit.rs | 4 ++++
core/lib/unit/base.rs | 3 +++
5 files changed, 20 insertions(+)
diff --git a/coms/mount/src/unit.rs b/coms/mount/src/unit.rs
index dfa6087..dc84403 100644
--- a/coms/mount/src/unit.rs
+++ b/coms/mount/src/unit.rs
@@ -64,6 +64,10 @@ impl MountUnit {
}
impl SubUnit for MountUnit {
+ fn as_any(&self) -> &dyn std::any::Any {
+ self
+ }
+
fn load(&self, _paths: Vec<PathBuf>) -> Result<()> {
if let Some(u) = self.comm.owner() {
u.set_ignore_on_isolate(true)
diff --git a/coms/service/src/unit.rs b/coms/service/src/unit.rs
index c91a296..7ea3c44 100644
--- a/coms/service/src/unit.rs
+++ b/coms/service/src/unit.rs
@@ -71,6 +71,10 @@ impl ReStation for ServiceUnit {
}
impl SubUnit for ServiceUnit {
+ fn as_any(&self) -> &dyn std::any::Any {
+ self
+ }
+
fn init(&self) {
todo!()
}
diff --git a/coms/socket/src/unit.rs b/coms/socket/src/unit.rs
index 198fafc..8e33ee8 100644
--- a/coms/socket/src/unit.rs
+++ b/coms/socket/src/unit.rs
@@ -23,6 +23,7 @@ use crate::{
use basic::logger;
use nix::sys::wait::WaitStatus;
use std::{path::PathBuf, rc::Rc};
+use std::any::Any;
use sysmaster::error::*;
use sysmaster::exec::ExecContext;
use sysmaster::rel::{ReStation, Reliability};
@@ -68,6 +69,10 @@ impl ReStation for SocketUnit {
}
impl SubUnit for SocketUnit {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
fn load(&self, paths: Vec<PathBuf>) -> Result<()> {
log::debug!("socket begin to load conf file");
self.config.load(paths, true)?;
diff --git a/coms/target/src/unit.rs b/coms/target/src/unit.rs
index 26b9950..e4caf5d 100644
--- a/coms/target/src/unit.rs
+++ b/coms/target/src/unit.rs
@@ -120,6 +120,10 @@ impl Target {
}
impl SubUnit for Target {
+ fn as_any(&self) -> &dyn std::any::Any {
+ self
+ }
+
fn load(&self, _conf_str: Vec<PathBuf>) -> Result<()> {
//todo add default dependency funnction need add
log::debug!("load for target");
diff --git a/core/lib/unit/base.rs b/core/lib/unit/base.rs
index 0996f5d..d279295 100644
--- a/core/lib/unit/base.rs
+++ b/core/lib/unit/base.rs
@@ -17,6 +17,7 @@ use super::umif::UnitMngUtil;
use crate::error::*;
use nix::sys::wait::WaitStatus;
use nix::{sys::socket::UnixCredentials, unistd::Pid};
+use std::any::Any;
use std::{collections::HashMap, path::PathBuf, rc::Rc};
///The trait Defining Shared Behavior from Base Unit to SUB unit
@@ -72,6 +73,8 @@ pub trait UnitBase {
/// difference sub unit ref by dynamic trait
///
pub trait SubUnit: ReStation + UnitMngUtil {
+ ///
+ fn as_any(&self) -> &dyn Any;
///
fn init(&self) {}
--
2.30.2

View File

@ -1,447 +0,0 @@
From ca86515f0d1cfcaae7d5552ef9f7df89cf6398d9 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Thu, 1 Jun 2023 15:38:02 +0800
Subject: [PATCH] feature: get credential from the connection, and use it to
check if the request is send from root
---
core/bin/manager/commands.rs | 33 ++++++-----
core/bin/unit/entry/config.rs | 6 +-
core/bin/unit/util/unit_file.rs | 35 ++++++++++--
libs/cmdproto/src/error.rs | 3 +
libs/cmdproto/src/proto/execute.rs | 68 ++++++++++++++++++++--
libs/cmdproto/src/proto/frame.rs | 91 +++++++++++++++++++++++++-----
6 files changed, 194 insertions(+), 42 deletions(-)
diff --git a/core/bin/manager/commands.rs b/core/bin/manager/commands.rs
index 1279206..a8320e2 100644
--- a/core/bin/manager/commands.rs
+++ b/core/bin/manager/commands.rs
@@ -73,21 +73,28 @@ where
}
fn dispatch(&self, _e: &Events) -> i32 {
- log::trace!("Dispatching Command!");
-
self.reli.set_last_frame1(ReliLastFrame::CmdOp as u32);
- match self.fd.incoming().next() {
- None => log::info!("None CommandRequest!"),
- Some(stream) => {
- log::trace!("{stream:?}");
- if let Ok(s) = stream {
- let dispatch = ProstServerStream::new(s, self.command_action.clone());
- match dispatch.process() {
- Ok(_) => (),
- Err(e) => log::error!("Commands failed: {:?}", e),
- }
- }
+ let client = match socket::accept(self.socket_fd) {
+ Err(e) => {
+ log::error!("Failed to accept connection: {}, ignoring.", e);
+ return 0;
+ }
+ Ok(v) => v,
+ };
+ let cred = match socket::getsockopt(client, socket::sockopt::PeerCredentials) {
+ Err(e) => {
+ log::error!(
+ "Failed to get the credentials of the connection: {}, refuse any request.",
+ e
+ );
+ None
}
+ Ok(v) => Some(v),
+ };
+ let dispatch = ProstServerStream::new(client, self.command_action.clone(), cred);
+ match dispatch.process() {
+ Ok(_) => (),
+ Err(e) => log::error!("Commands failed: {:?}", e),
}
self.reli.clear_last_frame();
diff --git a/core/bin/unit/entry/config.rs b/core/bin/unit/entry/config.rs
index e629c77..705512d 100644
--- a/core/bin/unit/entry/config.rs
+++ b/core/bin/unit/entry/config.rs
@@ -136,14 +136,12 @@ impl UeConfig {
let unit_conf_frag = files.get_unit_id_fragment_pathbuf(name);
if unit_conf_frag.is_empty() {
- log::error!("config file for {} is not exist", name);
- return Err(format!("config file for {name} is not exist").into());
+ return Err(format!("{name} doesn't have corresponding config file").into());
}
// fragment
for v in unit_conf_frag {
if !v.exists() {
- log::error!("config file is not exist");
- return Err(format!("config file is not exist {name}").into());
+ return Err(format!("Config file {:?} of {name} doesn't exist", v).into());
}
builder = builder.file(&v);
}
diff --git a/core/bin/unit/util/unit_file.rs b/core/bin/unit/util/unit_file.rs
index 31f5838..0871703 100644
--- a/core/bin/unit/util/unit_file.rs
+++ b/core/bin/unit/util/unit_file.rs
@@ -133,12 +133,39 @@ impl UnitFileData {
format!("{v}/{name}")
};
let tmp = Path::new(&path);
- if tmp.exists() && !tmp.is_symlink() {
- let path = format!("{}.toml", tmp.to_string_lossy());
- if let Err(e) = std::fs::copy(tmp, &path) {
+ if !tmp.exists() {
+ continue;
+ }
+ /* Add .toml to the original path name */
+ if !tmp.is_symlink() {
+ let path_toml = format!("{}.toml", tmp.to_string_lossy());
+ let to = Path::new(&path_toml);
+ if let Err(e) = std::fs::copy(tmp, to) {
log::warn!("copy file content to toml file error: {}", e);
}
- let to = Path::new(&path);
+ pathbuf_fragment.push(to.to_path_buf());
+ } else {
+ let real_path = match std::fs::read_link(tmp) {
+ Err(e) => {
+ log::error!("Failed to chase the symlink {:?}: {e}", tmp);
+ continue;
+ }
+ Ok(v) => v,
+ };
+ /* Only support one-level symlink. */
+ if real_path.is_symlink() {
+ continue;
+ }
+ let real_path = tmp.parent().unwrap().join(real_path);
+ let real_path = fs::canonicalize(&real_path).unwrap();
+ let path_toml = format!("{}.toml", real_path.to_string_lossy());
+ let to = Path::new(&path_toml);
+ if let Err(e) = std::fs::copy(&real_path, to) {
+ log::warn!(
+ "copy file content {:?} to toml file {path_toml} error: {e}",
+ real_path
+ );
+ }
pathbuf_fragment.push(to.to_path_buf());
}
}
diff --git a/libs/cmdproto/src/error.rs b/libs/cmdproto/src/error.rs
index 5d64109..618e6ad 100644
--- a/libs/cmdproto/src/error.rs
+++ b/libs/cmdproto/src/error.rs
@@ -33,6 +33,9 @@ pub enum Error {
#[snafu(display("ReadStreamFailed"))]
ReadStream { msg: String },
+ #[snafu(display("SendStreamFailed"))]
+ SendStream { msg: String },
+
#[snafu(display("ManagerStartFailed"))]
ManagerStart { msg: String },
}
diff --git a/libs/cmdproto/src/proto/execute.rs b/libs/cmdproto/src/proto/execute.rs
index f9ca73a..280b878 100644
--- a/libs/cmdproto/src/proto/execute.rs
+++ b/libs/cmdproto/src/proto/execute.rs
@@ -18,7 +18,7 @@ use super::{
use crate::error::*;
use http::StatusCode;
-use nix;
+use nix::{self, sys::socket::UnixCredentials};
use std::{fmt::Display, rc::Rc};
pub(crate) trait Executer {
@@ -27,6 +27,7 @@ pub(crate) trait Executer {
self,
manager: Rc<impl ExecuterAction>,
call_back: Option<fn(&str) -> String>,
+ cred: Option<UnixCredentials>,
) -> CommandResponse;
}
@@ -73,7 +74,11 @@ pub trait ExecuterAction {
}
/// Depending on the type of request
-pub(crate) fn dispatch<T>(cmd: CommandRequest, manager: Rc<T>) -> CommandResponse
+pub(crate) fn dispatch<T>(
+ cmd: CommandRequest,
+ manager: Rc<T>,
+ cred: Option<UnixCredentials>,
+) -> CommandResponse
where
T: ExecuterAction,
{
@@ -89,10 +94,10 @@ where
};
match cmd.request_data {
- Some(RequestData::Ucomm(param)) => param.execute(manager, Some(call_back)),
- Some(RequestData::Mcomm(param)) => param.execute(manager, None),
- Some(RequestData::Syscomm(param)) => param.execute(manager, Some(call_back)),
- Some(RequestData::Ufile(param)) => param.execute(manager, Some(call_back)),
+ Some(RequestData::Ucomm(param)) => param.execute(manager, Some(call_back), cred),
+ Some(RequestData::Mcomm(param)) => param.execute(manager, None, cred),
+ Some(RequestData::Syscomm(param)) => param.execute(manager, Some(call_back), cred),
+ Some(RequestData::Ufile(param)) => param.execute(manager, Some(call_back), cred),
_ => CommandResponse::default(),
}
}
@@ -103,12 +108,45 @@ fn new_line_break(s: &mut String) {
}
}
+fn response_if_credential_dissatisfied(
+ cred: Option<UnixCredentials>,
+ command_is_allowed_for_nonroot: bool,
+) -> Option<CommandResponse> {
+ let sender = match cred {
+ None => {
+ return Some(CommandResponse {
+ status: StatusCode::OK.as_u16() as _,
+ error_code: 1,
+ message: "Failed to execute your command: cannot determine user credentials."
+ .to_string(),
+ })
+ }
+ Some(v) => v.uid(),
+ };
+ if sender != 0 && !command_is_allowed_for_nonroot {
+ return Some(CommandResponse {
+ status: StatusCode::OK.as_u16() as _,
+ error_code: 1,
+ message: "Failed to execute your command: Operation not permitted.".to_string(),
+ });
+ }
+ None
+}
+
impl Executer for UnitComm {
fn execute(
self,
manager: Rc<impl ExecuterAction>,
call_back: Option<fn(&str) -> String>,
+ cred: Option<UnixCredentials>,
) -> CommandResponse {
+ if let Some(v) = response_if_credential_dissatisfied(
+ cred,
+ [unit_comm::Action::Status].contains(&self.action()),
+ ) {
+ return v;
+ }
+
let mut reply = String::new();
let mut units: Vec<String> = Vec::new();
let mut error_code: u32 = 0;
@@ -197,7 +235,15 @@ impl Executer for MngrComm {
self,
manager: Rc<impl ExecuterAction>,
_call_back: Option<fn(&str) -> String>,
+ cred: Option<UnixCredentials>,
) -> CommandResponse {
+ if let Some(v) = response_if_credential_dissatisfied(
+ cred,
+ [mngr_comm::Action::Listunits].contains(&self.action()),
+ ) {
+ return v;
+ }
+
match self.action() {
mngr_comm::Action::Reexec => {
manager.daemon_reexec();
@@ -241,7 +287,12 @@ impl Executer for SysComm {
self,
manager: Rc<impl ExecuterAction>,
_call_back: Option<fn(&str) -> String>,
+ cred: Option<UnixCredentials>,
) -> CommandResponse {
+ if let Some(v) = response_if_credential_dissatisfied(cred, false) {
+ return v;
+ }
+
let ret = if self.force {
let unit_name = self.action().to_string() + ".target";
match manager.start(&unit_name) {
@@ -279,7 +330,12 @@ impl Executer for UnitFile {
self,
manager: Rc<impl ExecuterAction>,
call_back: Option<fn(&str) -> String>,
+ cred: Option<UnixCredentials>,
) -> CommandResponse {
+ if let Some(v) = response_if_credential_dissatisfied(cred, false) {
+ return v;
+ }
+
let mut reply = String::new();
let mut units: Vec<String> = Vec::new();
let mut error_code: u32 = 0;
diff --git a/libs/cmdproto/src/proto/frame.rs b/libs/cmdproto/src/proto/frame.rs
index 9b942a8..6b6912b 100644
--- a/libs/cmdproto/src/proto/frame.rs
+++ b/libs/cmdproto/src/proto/frame.rs
@@ -12,6 +12,7 @@
//! Encapsulate the command request into a frame
use crate::error::*;
+use nix::sys::socket::{self, UnixCredentials};
use prost::bytes::{BufMut, BytesMut};
use prost::Message;
use std::{
@@ -48,8 +49,49 @@ where
impl FrameCoder for CommandRequest {}
impl FrameCoder for CommandResponse {}
+/// Read frame from accept fd.
+pub fn read_frame_from_fd(fd: i32, buf: &mut BytesMut) -> Result<()> {
+ // 1. Got the message length
+ let mut msg_len = [0_u8; USIZE_TO_U8_LENGTH];
+ match socket::recv(fd, &mut msg_len, socket::MsgFlags::empty()) {
+ Ok(len) => {
+ if len != USIZE_TO_U8_LENGTH {
+ return Err(Error::ReadStream {
+ msg: "Invalid message length".to_string(),
+ });
+ }
+ }
+ Err(e) => {
+ return Err(Error::ReadStream { msg: e.to_string() });
+ }
+ }
+ let msg_len = get_msg_len(msg_len);
+
+ // 2. Got the message
+ let mut tmp = vec![0; MAX_FRAME];
+ let mut cur_len: usize = 0;
+ loop {
+ match socket::recv(fd, &mut tmp, socket::MsgFlags::empty()) {
+ Ok(len) => {
+ cur_len += len;
+ buf.put_slice(&tmp[..len]);
+ /* If there is no more message (len < MAX_FRAME), or
+ * we have got enough message (cur_len >= msg_len),
+ * then we finish reading. */
+ if len < MAX_FRAME || cur_len >= msg_len {
+ break;
+ }
+ }
+ Err(e) => {
+ return Err(Error::ReadStream { msg: e.to_string() });
+ }
+ }
+ }
+ Ok(())
+}
+
/// read frame from stream
-pub fn read_frame<S>(stream: &mut S, buf: &mut BytesMut) -> Result<()>
+pub fn read_frame_from_stream<S>(stream: &mut S, buf: &mut BytesMut) -> Result<()>
where
S: Read + Unpin + Send,
{
@@ -66,6 +108,9 @@ where
Ok(len) => {
cur_len += len;
buf.put_slice(&tmp[..len]);
+ /* If there is no more message (len < MAX_FRAME), or
+ * we have got enough message (cur_len >= msg_len),
+ * then we finish reading. */
if len < MAX_FRAME || cur_len >= msg_len {
break;
}
@@ -89,9 +134,10 @@ fn get_msg_len(message: [u8; USIZE_TO_U8_LENGTH]) -> usize {
}
/// Handle read and write of server-side socket
-pub struct ProstServerStream<S, T> {
- inner: S,
+pub struct ProstServerStream<T> {
+ accept_fd: i32,
manager: Rc<T>,
+ cred: Option<UnixCredentials>,
}
/// Handle read and write of client-side socket
@@ -99,23 +145,23 @@ pub struct ProstClientStream<S> {
inner: S,
}
-impl<S, T> ProstServerStream<S, T>
+impl<T> ProstServerStream<T>
where
- S: Read + Write + Unpin + Send,
T: ExecuterAction,
{
/// new ProstServerStream
- pub fn new(stream: S, manager: Rc<T>) -> Self {
+ pub fn new(accept_fd: i32, manager: Rc<T>, cred: Option<UnixCredentials>) -> Self {
Self {
- inner: stream,
+ accept_fd,
manager,
+ cred,
}
}
/// process frame in server-side
pub fn process(mut self) -> Result<()> {
if let Ok(cmd) = self.recv() {
- let res = execute::dispatch(cmd, Rc::clone(&self.manager));
+ let res = execute::dispatch(cmd, Rc::clone(&self.manager), self.cred);
self.send(res)?;
};
Ok(())
@@ -126,16 +172,32 @@ where
msg.encode_frame(&mut buf)?;
let encoded = buf.freeze();
let msg_len = msg_len_vec(encoded.len());
- self.inner.write_all(&msg_len).context(IoSnafu)?;
- self.inner.write_all(&encoded).context(IoSnafu)?;
- self.inner.flush().context(IoSnafu)?;
+ match socket::send(self.accept_fd, &msg_len, socket::MsgFlags::empty()) {
+ Ok(len) => {
+ if len != msg_len.len() {
+ return Err(Error::SendStream {
+ msg: "Invalid message length".to_string(),
+ });
+ }
+ }
+ Err(e) => return Err(Error::SendStream { msg: e.to_string() }),
+ }
+ match socket::send(self.accept_fd, &encoded, socket::MsgFlags::empty()) {
+ Ok(len) => {
+ if len != encoded.len() {
+ return Err(Error::SendStream {
+ msg: "Invalid message length".to_string(),
+ });
+ }
+ }
+ Err(e) => return Err(Error::SendStream { msg: e.to_string() }),
+ }
Ok(())
}
fn recv(&mut self) -> Result<CommandRequest> {
let mut buf = BytesMut::new();
- let stream = &mut self.inner;
- read_frame(stream, &mut buf)?;
+ read_frame_from_fd(self.accept_fd, &mut buf)?;
CommandRequest::decode_frame(&mut buf)
}
}
@@ -169,8 +231,7 @@ where
fn recv(&mut self) -> Result<CommandResponse> {
let mut buf = BytesMut::new();
- let stream = &mut self.inner;
- read_frame(stream, &mut buf)?;
+ read_frame_from_stream(&mut self.inner, &mut buf)?;
CommandResponse::decode_frame(&mut buf)
}
}
--
2.30.2

View File

@ -1,136 +0,0 @@
From 6026b784dfff831796ab04ddbf3913684360d845 Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Fri, 2 Jun 2023 05:00:56 +0800
Subject: [PATCH] fix: .socket stop faild when .socket is in listening or
running state
---
coms/socket/src/config.rs | 20 +++++++-------------
coms/socket/src/load.rs | 2 +-
coms/socket/src/unit.rs | 10 ++++++++++
core/bin/unit/entry/uentry.rs | 12 +++++++++++-
4 files changed, 29 insertions(+), 15 deletions(-)
diff --git a/coms/socket/src/config.rs b/coms/socket/src/config.rs
index 88c9e1a..a05fba5 100644
--- a/coms/socket/src/config.rs
+++ b/coms/socket/src/config.rs
@@ -98,7 +98,7 @@ impl ReStation for SocketConfig {
// UnitRef
if let Some(svc) = service {
- self.set_unit_ref(svc).unwrap();
+ self.set_unit_ref(svc);
}
// SocketPortConf
@@ -168,15 +168,9 @@ impl SocketConfig {
self.data.borrow().get_exec_cmds(cmd_type)
}
- pub(super) fn set_unit_ref(&self, service: String) -> Result<()> {
- if !self.comm.um().load_unit_success(&service) {
- return Err(format!("failed to load unit {service}").into());
- }
-
+ pub(super) fn set_unit_ref(&self, service: String) {
self.set_ref(service);
self.db_update();
-
- Ok(())
}
pub(super) fn unit_ref_target(&self) -> Option<String> {
@@ -190,12 +184,11 @@ impl SocketConfig {
fn parse_service(&self) -> Result<()> {
if let Some(service) = self.config_data().borrow().Socket.Service.clone() {
if !service.ends_with(".service") {
- return Err("socket service must be end with .service"
- .to_string()
- .into());
+ log::warn!("socket service must be end with .service, ignoring:{service}");
+ return Ok(());
}
- self.set_unit_ref(service)?;
+ self.set_unit_ref(service);
}
Ok(())
@@ -245,7 +238,8 @@ impl SocketConfig {
let socket_addr = match parse_func(v, socket_type) {
Err(_) => {
- return Err(format!("Invalid socket configuration: {v}").into());
+ log::warn!("Invalid socket configuration: {v}");
+ return Ok(());
}
Ok(v) => v,
};
diff --git a/coms/socket/src/load.rs b/coms/socket/src/load.rs
index 39e8e66..d495389 100644
--- a/coms/socket/src/load.rs
+++ b/coms/socket/src/load.rs
@@ -70,7 +70,7 @@ impl SocketLoad {
let u_name = unit_name.unwrap();
let stem_name = Path::new(&u_name).file_stem().unwrap().to_str().unwrap();
let relate_name = format!("{stem_name}.{suffix}");
- self.config.set_unit_ref(relate_name)?;
+ self.config.set_unit_ref(relate_name);
Ok(())
}
diff --git a/coms/socket/src/unit.rs b/coms/socket/src/unit.rs
index 9f0f6ce..d593350 100644
--- a/coms/socket/src/unit.rs
+++ b/coms/socket/src/unit.rs
@@ -204,6 +204,13 @@ impl SocketUnit {
}
fn verify(&self) -> Result<()> {
+ if self.config.ports().is_empty() {
+ log::error!("Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
+ return Err(Error::Nix {
+ source: nix::Error::ENOEXEC,
+ });
+ }
+
let config = self.config.config_data();
if config.borrow().Socket.Symlinks.is_some()
&& !config.borrow().Socket.Symlinks.as_ref().unwrap().is_empty()
@@ -212,6 +219,9 @@ impl SocketUnit {
/* Set to None, so we won't create symlinks by mistake. */
config.borrow_mut().Socket.Symlinks = None;
log::error!("Symlinks in [Socket] is configured, but there are none or more than one listen files.");
+ return Err(Error::Nix {
+ source: nix::Error::ENOEXEC,
+ });
}
Ok(())
}
diff --git a/core/bin/unit/entry/uentry.rs b/core/bin/unit/entry/uentry.rs
index f6b4db4..0c6e044 100644
--- a/core/bin/unit/entry/uentry.rs
+++ b/core/bin/unit/entry/uentry.rs
@@ -578,7 +578,17 @@ impl Unit {
Ok(_) => {
let paths = self.load.get_unit_id_fragment_pathbuf();
log::debug!("Begin exec sub class load");
- self.sub.load(paths)?;
+
+ if let Err(err) = self.sub.load(paths) {
+ if let Error::Nix { source } = err {
+ if source == nix::Error::ENOEXEC {
+ self.load.set_load_state(UnitLoadState::BadSetting);
+ return Err(err);
+ }
+ }
+ self.load.set_load_state(UnitLoadState::Error);
+ return Err(err);
+ }
self.load.set_load_state(UnitLoadState::Loaded);
Ok(())
--
2.33.0

View File

@ -1,120 +0,0 @@
From 754126dc701fe1d945b60c634b0c84d3c976b23b Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Wed, 7 Jun 2023 16:33:26 +0800
Subject: [PATCH] fix: Clear buffer correctly during reload or reexec
---
core/lib/rel/api.rs | 22 +++++++++++-----------
core/lib/rel/base.rs | 10 +++++++---
core/lib/rel/history.rs | 1 -
3 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/core/lib/rel/api.rs b/core/lib/rel/api.rs
index 59e4ac9..ad14796 100644
--- a/core/lib/rel/api.rs
+++ b/core/lib/rel/api.rs
@@ -187,11 +187,7 @@ impl Reliability {
self.db_compensate();
self.db_map(reload);
if reload {
- // If daemon-reload or daemon-reexec, we need to update all changes, clear db, and submit all changes to db.
- self.db_insert();
- self.history.flush();
- // Due to changes in db, we need to reload the data from db to cache.
- self.history.import();
+ self.db_flush();
}
self.make_consistent();
@@ -309,12 +305,10 @@ impl Reliability {
}
fn input_rebuild(&self) {
- // ignore history's input
self.history.switch_set(true);
self.station.input_rebuild();
- // restore history's ignore
self.history.switch_set(false);
}
@@ -331,22 +325,28 @@ impl Reliability {
/// map data from database
/// reload determine whether the configuration needs to be reloaded based on the situation.
fn db_map(&self, reload: bool) {
- // control use buf
self.history.switch_set(true);
self.station.db_map(reload);
- // control use cache
self.history.switch_set(false);
}
- /// map-data insert to buf, switch is true indicating either daemon-reload or daemon-reexec
- fn db_insert(&self) {
+ /// flush all data from buffer to db
+ fn db_flush(&self) {
+ // clear data before using buffer
self.history.switch_set(true);
+ // update all changes to buffer
self.station.db_insert();
+ // clear db, submit data from all buffers to db, clear buffer
+ self.history.flush();
+
self.history.switch_set(false);
+
+ // Due to changes in db, reload the data from db to cache.
+ self.history.import();
}
fn make_consistent(&self) {
diff --git a/core/lib/rel/base.rs b/core/lib/rel/base.rs
index 29200df..7137e13 100644
--- a/core/lib/rel/base.rs
+++ b/core/lib/rel/base.rs
@@ -27,8 +27,7 @@ use std::{env, fs};
/// the reliability database
/// K & V that can be deserialized without borrowing any data from the deserializer.
pub struct ReDb<K, V> {
- // control
- switch: RefCell<bool>,
+ switch: RefCell<bool>, // if switch is true use buffer, if switch is false use cache
// data
/* database */
@@ -97,10 +96,15 @@ where
self.cache.borrow_mut().clear();
self.add.borrow_mut().clear();
self.del.borrow_mut().clear();
+ // Do not clear the buffer because its data is transient.
}
- /// set the buffer-switch flag of data
+ /// switch between cache and buffer
pub fn switch_buffer(&self, switch: bool) {
+ if switch {
+ // Before using the buffer, data needs to be cleared.
+ self.buffer.borrow_mut().clear();
+ }
*self.switch.borrow_mut() = switch;
}
diff --git a/core/lib/rel/history.rs b/core/lib/rel/history.rs
index f9e2146..199e10e 100644
--- a/core/lib/rel/history.rs
+++ b/core/lib/rel/history.rs
@@ -158,7 +158,6 @@ impl ReliHistory {
}
pub fn switch_set(&self, switch: bool) {
- // set switch
*self.switch.borrow_mut() = switch;
for (_, db) in self.dbs.borrow().iter() {
db.switch_set(switch);
--
2.33.0

View File

@ -1,91 +0,0 @@
From 5798474ac1e7e5e41eb525884ecfff7a29f3800a Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:13:56 +0800
Subject: [PATCH 5/9] fix: add has_source to events, and use this in
socket.watch_fds() to avoid adding same source to events loop. This PR also
contains some nonfunctional cleanups
---
coms/socket/src/mng.rs | 5 +++--
libs/event/src/events.rs | 16 +++++++++++++++-
libs/event/src/poll/epoll.rs | 7 +++++--
3 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index fe77517..aace9c7 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -576,8 +576,9 @@ impl SocketMng {
continue;
}
let source = Rc::clone(mport);
- events.add_source(source).unwrap();
- let source = Rc::clone(mport);
+ if !events.has_source(source.clone()) {
+ events.add_source(source.clone()).unwrap();
+ }
events.set_enabled(source, EventState::On).unwrap();
}
}
diff --git a/libs/event/src/events.rs b/libs/event/src/events.rs
index fba671f..ed7fa1c 100644
--- a/libs/event/src/events.rs
+++ b/libs/event/src/events.rs
@@ -53,6 +53,11 @@ impl Events {
self.data.borrow_mut().add_source(source)
}
+ /// for all: check if the source exists
+ pub fn has_source(&self, source: Rc<dyn Source>) -> bool {
+ self.data.borrow().has_source(source)
+ }
+
/// for all: delete source
pub fn del_source(&self, source: Rc<dyn Source>) -> Result<i32> {
self.data.borrow_mut().del_source(source)
@@ -233,6 +238,11 @@ impl EventsData {
Ok(0)
}
+ pub(self) fn has_source(&self, source: Rc<dyn Source>) -> bool {
+ let token = source.token();
+ self.sources.contains_key(&token)
+ }
+
pub(self) fn del_source(&mut self, source: Rc<dyn Source>) -> Result<i32> {
self.source_offline(&source)?;
@@ -287,7 +297,11 @@ impl EventsData {
pub(self) fn set_enabled(&mut self, source: Rc<dyn Source>, state: EventState) -> Result<i32> {
let token = source.token();
-
+ if let Some(current_state) = self.state.get(&token) {
+ if current_state == &state {
+ return Ok(0);
+ }
+ }
match state {
EventState::On | EventState::OneShot => {
self.source_online(&source)?;
diff --git a/libs/event/src/poll/epoll.rs b/libs/event/src/poll/epoll.rs
index 61926df..ad236ed 100644
--- a/libs/event/src/poll/epoll.rs
+++ b/libs/event/src/poll/epoll.rs
@@ -77,8 +77,11 @@ impl Epoll {
}
pub(crate) fn register(&mut self, fd: RawFd, event: &mut epoll_event) -> Result<()> {
- self.n_sources.fetch_add(1, Ordering::Relaxed);
- syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_ADD, fd, event)).map(|_| ())
+ let res = syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_ADD, fd, event)).map(|_| ());
+ if res.is_ok() {
+ self.n_sources.fetch_add(1, Ordering::Relaxed);
+ }
+ res
}
pub(crate) fn reregister(&mut self, fd: RawFd, event: &mut epoll_event) -> Result<()> {
--
2.30.2

View File

@ -1,29 +0,0 @@
From ef57fb8a85d78dfc6e7cbbf2ac6756da93ffd45f Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:15:32 +0800
Subject: [PATCH 6/9] fix: change if port.fd() is valid, we should avoid
dealing with invalid port.
---
coms/socket/src/mng.rs | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index aace9c7..82fdcbe 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -593,8 +593,9 @@ impl SocketMng {
fn flush_ports(&self) {
for port in self.ports().iter() {
- port.flush_accept();
-
+ if port.fd() < 0 {
+ continue;
+ }
port.flush_fd();
}
}
--
2.30.2

View File

@ -1,165 +0,0 @@
From 6ac9cdc2b5c38153a5faaf41ce8421b09aaf053e Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:20:33 +0800
Subject: [PATCH 8/9] fix: change the socket logic to keep the same as systemd
1. check the events number got from wait_for_events(), quick return when it
is 0. 2. make flush_accept() return the error. 3. close the accepted fd
instead port fd itself.
---
coms/socket/src/mng.rs | 1 +
coms/socket/src/port.rs | 54 ++++++++++++++++++++++++++---------------
core/lib/error.rs | 28 +++++++++++++++++++++
3 files changed, 63 insertions(+), 20 deletions(-)
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index 0435827..da396de 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -597,6 +597,7 @@ impl SocketMng {
if port.fd() < 0 {
continue;
}
+ let _ = port.flush_accept();
port.flush_fd();
}
}
diff --git a/coms/socket/src/port.rs b/coms/socket/src/port.rs
index 870cb70..0500669 100644
--- a/coms/socket/src/port.rs
+++ b/coms/socket/src/port.rs
@@ -151,29 +151,43 @@ impl SocketPort {
}
}
- pub(super) fn flush_accept(&self) {
+ pub(super) fn flush_accept(&self) -> Result<()> {
if let Ok(true) = socket::getsockopt(self.fd(), sockopt::AcceptConn) {
for _i in 1..1024 {
- while let Err(e) = io_util::wait_for_events(self.fd(), PollFlags::POLLIN, 0) {
- if let basic::Error::Nix {
- source: Errno::EINTR,
- } = e
- {
- continue;
+ let events = match io_util::wait_for_events(self.fd(), PollFlags::POLLIN, 0) {
+ Err(e) => {
+ if let basic::Error::Nix {
+ source: Errno::EINTR,
+ } = e
+ {
+ continue;
+ }
+ return Err(e.into());
}
- return;
+ Ok(v) => v,
+ };
+ if events == 0 {
+ return Ok(());
}
- match socket::accept4(self.fd(), SockFlag::SOCK_NONBLOCK | SockFlag::SOCK_CLOEXEC)
- .map(|_| fd_util::close(self.fd()))
- {
- Ok(_) => {}
- Err(_e) => {
- // todo!(): if e == Errno::EAGAIN { return; }
- return;
+ let cfd = match socket::accept4(
+ self.fd(),
+ SockFlag::SOCK_NONBLOCK | SockFlag::SOCK_CLOEXEC,
+ ) {
+ Err(e) => {
+ if e == Errno::EAGAIN {
+ return Ok(());
+ }
+ if error_is_accept_again(&e) {
+ continue;
+ }
+ return Err(e.into());
}
- }
+ Ok(v) => v,
+ };
+ fd_util::close(cfd);
}
}
+ Ok(())
}
pub(super) fn flush_fd(&self) {
@@ -356,7 +370,7 @@ mod tests {
assert_ne!(port.fd(), SOCKET_INVALID_FD);
assert_eq!(port.family(), AddressFamily::Inet);
- port.flush_accept();
+ assert!(port.flush_accept().is_ok());
port.flush_fd();
port.close(false);
}
@@ -387,7 +401,7 @@ mod tests {
assert_ne!(port.fd(), SOCKET_INVALID_FD);
assert_eq!(port.family(), AddressFamily::Unix);
- port.flush_accept();
+ assert!(port.flush_accept().is_ok());
port.flush_fd();
port.close(false);
}
@@ -424,7 +438,7 @@ mod tests {
assert_ne!(port.fd(), SOCKET_INVALID_FD);
assert_eq!(port.family(), AddressFamily::Netlink);
- port.flush_accept();
+ assert!(port.flush_accept().is_ok());
port.flush_fd();
port.close(false);
}
@@ -519,7 +533,7 @@ mod tests {
}
// Rosource reclaim
- port.flush_accept();
+ assert!(port.flush_accept().is_ok());
port.flush_fd();
port.close(false);
}
diff --git a/core/lib/error.rs b/core/lib/error.rs
index 100eb3c..331f367 100644
--- a/core/lib/error.rs
+++ b/core/lib/error.rs
@@ -289,3 +289,31 @@ impl From<event::Error> for Error {
/// new Result
pub type Result<T, E = Error> = std::result::Result<T, E>;
+
+pub fn error_is_disconnect(e: &Errno) -> bool {
+ [
+ Errno::ECONNABORTED,
+ Errno::ECONNREFUSED,
+ Errno::ECONNRESET,
+ Errno::EHOSTDOWN,
+ Errno::EHOSTUNREACH,
+ Errno::ENETDOWN,
+ Errno::ENETRESET,
+ Errno::ENONET,
+ Errno::ENOPROTOOPT,
+ Errno::ENOTCONN,
+ Errno::EPIPE,
+ Errno::EPROTO,
+ Errno::ESHUTDOWN,
+ Errno::ETIMEDOUT,
+ ]
+ .contains(e)
+}
+
+pub fn error_is_transient(e: &Errno) -> bool {
+ [Errno::EAGAIN, Errno::EINTR].contains(e)
+}
+
+pub fn error_is_accept_again(e: &Errno) -> bool {
+ error_is_disconnect(e) || error_is_transient(e) || e == &Errno::EOPNOTSUPP
+}
--
2.30.2

View File

@ -1,193 +0,0 @@
From 8123dcec5160b0d29e541cec1c9a027a1dd42c48 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 14 Jun 2023 15:52:40 +0800
Subject: [PATCH] fix: check if the given unit name is valid before we prepare
the unit
---
core/bin/unit/base/load.rs | 8 -----
core/bin/unit/manager.rs | 8 ++++-
core/bin/unit/uload.rs | 6 +++-
core/lib/unit/base.rs | 70 ++++++++++++++++++++++++++++++++++++++
core/lib/unit/mod.rs | 3 +-
5 files changed, 84 insertions(+), 11 deletions(-)
diff --git a/core/bin/unit/base/load.rs b/core/bin/unit/base/load.rs
index 2281d06..700237d 100644
--- a/core/bin/unit/base/load.rs
+++ b/core/bin/unit/base/load.rs
@@ -10,14 +10,6 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
-#[allow(dead_code)]
-enum UnitNameFlags {
- Plain = 1,
- Instance = 2,
- Template = 4,
- Any = 1 | 2 | 4,
-}
-
#[allow(dead_code)]
enum UnitFileState {
Enabled,
diff --git a/core/bin/unit/manager.rs b/core/bin/unit/manager.rs
index 4b3f4fb..cb5d957 100644
--- a/core/bin/unit/manager.rs
+++ b/core/bin/unit/manager.rs
@@ -50,7 +50,10 @@ use sysmaster::error::*;
use sysmaster::exec::ExecParameters;
use sysmaster::exec::{ExecCommand, ExecContext};
use sysmaster::rel::{ReStation, ReStationKind, ReliLastFrame, Reliability};
-use sysmaster::unit::{UmIf, UnitActiveState, UnitDependencyMask, UnitStatus, UnitType};
+use sysmaster::unit::{
+ unit_name_is_valid, UmIf, UnitActiveState, UnitDependencyMask, UnitNameFlags, UnitStatus,
+ UnitType,
+};
use unit_submanager::UnitSubManagers;
//#[derive(Debug)]
@@ -525,6 +528,9 @@ impl UnitManager {
///
pub fn units_get(&self, name: &str) -> Option<Rc<Unit>> {
+ if !unit_name_is_valid(name, UnitNameFlags::PLAIN | UnitNameFlags::INSTANCE) {
+ return None;
+ }
self.db.units_get(name).map(|uxr| uxr.unit())
}
diff --git a/core/bin/unit/uload.rs b/core/bin/unit/uload.rs
index d972fb4..d6fea43 100644
--- a/core/bin/unit/uload.rs
+++ b/core/bin/unit/uload.rs
@@ -22,7 +22,7 @@ use crate::utils::table::{TableOp, TableSubscribe};
use basic::path_lookup::LookupPaths;
use std::cell::RefCell;
use std::rc::{Rc, Weak};
-use sysmaster::unit::UnitType;
+use sysmaster::unit::{unit_name_is_valid, UnitNameFlags, UnitType};
//#[derive(Debug)]
pub(super) struct UnitLoad {
@@ -100,6 +100,10 @@ impl UnitLoadData {
}
pub(self) fn prepare_unit(&self, name: &str) -> Option<Rc<UnitX>> {
+ if !unit_name_is_valid(name, UnitNameFlags::PLAIN | UnitNameFlags::INSTANCE) {
+ return None;
+ }
+
if let Some(u) = self.db.units_get(name) {
if u.load_state() != UnitLoadState::NotFound {
return Some(Rc::clone(&u));
diff --git a/core/lib/unit/base.rs b/core/lib/unit/base.rs
index f066ed7..555bb69 100644
--- a/core/lib/unit/base.rs
+++ b/core/lib/unit/base.rs
@@ -14,10 +14,14 @@ use super::super::rel::ReStation;
use super::kill::{KillContext, KillOperation};
use super::state::{UnitActiveState, UnitNotifyFlags};
use super::umif::UnitMngUtil;
+use super::UnitType;
use crate::error::*;
+use bitflags::bitflags;
use nix::sys::wait::WaitStatus;
use nix::{sys::socket::UnixCredentials, unistd::Pid};
use std::any::Any;
+use std::num::ParseIntError;
+use std::str::FromStr;
use std::{collections::HashMap, path::PathBuf, rc::Rc};
///The trait Defining Shared Behavior from Base Unit to SUB unit
@@ -206,3 +210,69 @@ macro_rules! declure_unitobj_plugin_with_param {
}
};
}
+
+bitflags! {
+ /// used to when check the given unit name is valid
+ pub struct UnitNameFlags: u8 {
+ /// Allow foo.service
+ const PLAIN = 1 << 0;
+ /// Allow foo@.service
+ const TEMPLATE = 1 << 1;
+ /// Allow foo@123.service
+ const INSTANCE = 1 << 2;
+ /// Any of the above
+ const ANY = Self::PLAIN.bits() | Self::TEMPLATE.bits() | Self::INSTANCE.bits();
+ }
+}
+
+/// The maximum length of a valid unit name
+const UNIT_NAME_MAX: usize = 255;
+
+/// check if the given unit name is valid
+pub fn unit_name_is_valid(name: &str, flag: UnitNameFlags) -> bool {
+ if name.is_empty() {
+ return false;
+ }
+ if name.len() > UNIT_NAME_MAX {
+ return false;
+ }
+ /* Take foo@123.service for example, "foo@123" is its first_name,
+ * "foo" is prefix, "service" is its last_name, suffix, or type. */
+ let (first_name, last_name) = match name.split_once('.') {
+ None => return false,
+ Some(v) => (v.0, v.1),
+ };
+
+ let unit_type = match unit_type_from_string(last_name) {
+ Err(_) => return false,
+ Ok(v) => v,
+ };
+
+ if unit_type == UnitType::UnitTypeInvalid {
+ return false;
+ }
+
+ match first_name.split_once('@') {
+ None => flag.contains(UnitNameFlags::PLAIN),
+ Some(v) => {
+ /* "@" is the first character */
+ if v.0.is_empty() {
+ return false;
+ }
+ /* "@" is the last character */
+ if v.1.is_empty() {
+ return flag.contains(UnitNameFlags::TEMPLATE);
+ }
+ /* there is more than one "@" */
+ if v.1.contains('@') {
+ return false;
+ }
+ flag.contains(UnitNameFlags::INSTANCE)
+ }
+ }
+}
+
+/// convert the type string of one unit to UnitType
+pub fn unit_type_from_string(type_string: &str) -> Result<UnitType, ParseIntError> {
+ UnitType::from_str(type_string)
+}
diff --git a/core/lib/unit/mod.rs b/core/lib/unit/mod.rs
index 7211aec..1eb565c 100644
--- a/core/lib/unit/mod.rs
+++ b/core/lib/unit/mod.rs
@@ -11,11 +11,12 @@
// See the Mulan PSL v2 for more details.
//!
-pub use base::{SubUnit, UnitBase};
+pub use base::{unit_name_is_valid, SubUnit, UnitBase, UnitNameFlags};
pub use deps::{UnitDependencyMask, UnitRelationAtom, UnitRelations, UnitType};
pub use kill::{KillContext, KillMode, KillOperation};
pub use state::{UnitActiveState, UnitNotifyFlags, UnitStatus};
pub use umif::{UmIf, UnitManagerObj, UnitMngUtil};
+
mod base;
mod deps;
mod kill;
--
2.30.2

View File

@ -1,28 +0,0 @@
From e0a828649165701afa9efa039650ddcd4afa4c6e Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 24 May 2023 16:45:10 +0800
Subject: [PATCH] fix: check if the returned state is None
---
libs/event/src/events.rs | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/libs/event/src/events.rs b/libs/event/src/events.rs
index 7b0346d..8b29835 100644
--- a/libs/event/src/events.rs
+++ b/libs/event/src/events.rs
@@ -108,7 +108,10 @@ impl Events {
}
let top = first.unwrap();
- let state = self.data.borrow().source_state(&top).unwrap();
+ let state = match self.data.borrow().source_state(&top) {
+ None => return Ok(0),
+ Some(v) => v,
+ };
match state {
EventState::Off => {
println!("set_enabled Off: {top:?}");
--
2.30.2

View File

@ -1,33 +0,0 @@
From 7dea4930d6118ffb650a34aafc7a37dcdb198cf9 Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Thu, 8 Jun 2023 10:36:38 +0800
Subject: [PATCH] fix: commands connection timeout causing event blocking
---
core/bin/manager/commands.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/core/bin/manager/commands.rs b/core/bin/manager/commands.rs
index 94877a2..0727295 100644
--- a/core/bin/manager/commands.rs
+++ b/core/bin/manager/commands.rs
@@ -41,7 +41,7 @@ impl<T> Commands<T> {
let socket_fd = socket::socket(
socket::AddressFamily::Unix,
socket::SockType::Stream,
- socket::SockFlag::empty(),
+ socket::SockFlag::SOCK_CLOEXEC | socket::SockFlag::SOCK_NONBLOCK,
None,
)
.unwrap();
@@ -112,6 +112,6 @@ where
}
fn priority(&self) -> i8 {
- 10i8
+ 0i8
}
}
--
2.33.0

View File

@ -1,557 +0,0 @@
From a8ca02c09ebffa5eabbb935be96b3864d8091549 Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Thu, 18 May 2023 16:45:47 +0800
Subject: [PATCH] fix: compatible with rusc 1.60 lint check
---
core/bin/main.rs | 2 +-
core/bin/unit/execute/spawn.rs | 5 +-
core/bin/unit/uload.rs | 16 +-
core/lib/exec/cmd.rs | 1 -
exts/devmaster/src/lib/builtin/path_id.rs | 6 +-
exts/devmaster/src/lib/rules/rule_execute.rs | 156 ++++++-------------
exts/init/src/runtime/param.rs | 8 +-
exts/init/src/runtime/timer.rs | 1 +
exts/random_seed/src/random_seed.rs | 4 +-
libs/basic/src/user_group_util.rs | 2 +-
libs/event/src/timer.rs | 1 +
libs/macros/src/unit_conf_parse.rs | 13 +-
tests/docker_config_test.rs | 1 +
tests/docker_example.rs | 1 +
tests/docker_perf.rs | 1 +
tests/docker_reliable.rs | 1 +
16 files changed, 81 insertions(+), 138 deletions(-)
diff --git a/core/bin/main.rs b/core/bin/main.rs
index 26638d6..eb06ed6 100644
--- a/core/bin/main.rs
+++ b/core/bin/main.rs
@@ -169,7 +169,7 @@ fn set_child_reaper() {
}
}
-fn do_reexecute(args: &Vec<String>, reload: bool) {
+fn do_reexecute(args: &[String], reload: bool) {
let args_size = args.len().max(MANAGER_ARGS_SIZE_MAX);
let path;
diff --git a/core/bin/unit/execute/spawn.rs b/core/bin/unit/execute/spawn.rs
index cbde2b5..8e55970 100644
--- a/core/bin/unit/execute/spawn.rs
+++ b/core/bin/unit/execute/spawn.rs
@@ -259,8 +259,7 @@ fn build_run_args(
let cmd = std::ffi::CString::new(cmdline.path().clone()).unwrap();
let exec_name = std::ffi::CString::new(cmdline.path().clone()).unwrap();
- let mut args = Vec::new();
- args.push(exec_name);
+ let mut args = vec![exec_name];
let var_regex = Regex::new(r"(\$[A-Z_]+)|(\$\{[A-Z_]+\})").unwrap();
for arg in cmdline.argv() {
@@ -356,7 +355,7 @@ fn close_all_fds(fds: &[i32]) -> bool {
true
}
-fn shift_fds(fds: &mut Vec<i32>) -> bool {
+fn shift_fds(fds: &mut [i32]) -> bool {
let mut start = 0;
loop {
let mut restart = -1;
diff --git a/core/bin/unit/uload.rs b/core/bin/unit/uload.rs
index a7d1bea..d972fb4 100644
--- a/core/bin/unit/uload.rs
+++ b/core/bin/unit/uload.rs
@@ -210,14 +210,14 @@ impl UnitLoadData {
// dependency
for (relation, list) in config.deps.iter() {
for o_name in list {
- let tmp_unit: Rc<UnitX>;
- if let Some(o_unit) = self.push_dep_unit_into_load_queue(o_name) {
- //can not call unit_load directly, will be nested.
- tmp_unit = Rc::clone(&o_unit);
- } else {
- log::error!("create unit obj error in unit manager");
- return;
- }
+ let tmp_unit: Rc<UnitX> =
+ if let Some(o_unit) = self.push_dep_unit_into_load_queue(o_name) {
+ //can not call unit_load directly, will be nested.
+ Rc::clone(&o_unit)
+ } else {
+ log::error!("create unit obj error in unit manager");
+ return;
+ };
if let Err(_e) = self
.db
diff --git a/core/lib/exec/cmd.rs b/core/lib/exec/cmd.rs
index 406ee00..040b7f9 100644
--- a/core/lib/exec/cmd.rs
+++ b/core/lib/exec/cmd.rs
@@ -116,7 +116,6 @@ impl DeserializeWith for ExecCommand {
continue;
}
- #[allow(clippy::trim_split_whitespace)]
let mut command: Vec<String> = Vec::new();
let re = Regex::new(r"'([^']*)'|\S+").unwrap();
for cap in re.captures_iter(cmd) {
diff --git a/exts/devmaster/src/lib/builtin/path_id.rs b/exts/devmaster/src/lib/builtin/path_id.rs
index 6d0de56..2ebaecd 100644
--- a/exts/devmaster/src/lib/builtin/path_id.rs
+++ b/exts/devmaster/src/lib/builtin/path_id.rs
@@ -146,6 +146,7 @@ impl Builtin for PathId {
}
impl PathId {
+ #[allow(clippy::ptr_arg)]
fn compose_path(
&self,
dev: Arc<Mutex<Device>>,
@@ -982,6 +983,7 @@ impl PathId {
) -> Option<Arc<Mutex<Device>>> {
let mut dev = device.clone();
let mut parent = device;
+ #[allow(clippy::while_let_loop)]
loop {
let subsystem = match parent.lock().unwrap().get_subsystem() {
Ok(str) => str,
diff --git a/exts/devmaster/src/lib/rules/rule_execute.rs b/exts/devmaster/src/lib/rules/rule_execute.rs
index 36acc8f..384d490 100644
--- a/exts/devmaster/src/lib/rules/rule_execute.rs
+++ b/exts/devmaster/src/lib/rules/rule_execute.rs
@@ -111,7 +111,7 @@ impl ExecuteUnit {
}
/// apply runtime substitution on all formatters in the string
- pub fn apply_format(&self, src: &String, replace_whitespace: bool) -> Result<String> {
+ pub fn apply_format(&self, src: &str, replace_whitespace: bool) -> Result<String> {
let mut idx: usize = 0;
let mut ret = String::new();
while idx < src.len() {
@@ -370,7 +370,7 @@ impl ExecuteUnit {
}
fn get_subst_type(
- s: &String,
+ s: &str,
idx: &mut usize,
strict: bool,
) -> Result<Option<(FormatSubstitutionType, Option<String>)>> {
@@ -1641,146 +1641,102 @@ mod tests {
let unit = ExecuteUnit::new(device);
// test long substitution formatter
// $kernel
- assert_eq!(
- unit.apply_format(&"$kernel".to_string(), false).unwrap(),
- "lo"
- );
+ assert_eq!(unit.apply_format("$kernel", false).unwrap(), "lo");
// $number
- assert_eq!(
- unit.apply_format(&"$number".to_string(), false).unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$number", false).unwrap(), "");
// $devpath
assert_eq!(
- unit.apply_format(&"$devpath".to_string(), false).unwrap(),
+ unit.apply_format("$devpath", false).unwrap(),
"/devices/virtual/net/lo"
);
// $id
- assert_eq!(unit.apply_format(&"$id".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("$id", false).unwrap(), "");
// $driver
- assert_eq!(
- unit.apply_format(&"$driver".to_string(), false).unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$driver", false).unwrap(), "");
// $attr{sysattr}
assert_eq!(
- unit.apply_format(&"$attr{address}".to_string(), false)
- .unwrap(),
+ unit.apply_format("$attr{address}", false).unwrap(),
"00:00:00:00:00:00"
);
// $env{key}
assert_eq!(
- unit.apply_format(&"$env{DEVPATH}".to_string(), false)
- .unwrap(),
+ unit.apply_format("$env{DEVPATH}", false).unwrap(),
"/devices/virtual/net/lo"
);
// $major
- assert_eq!(
- unit.apply_format(&"$major".to_string(), false).unwrap(),
- "0"
- );
+ assert_eq!(unit.apply_format("$major", false).unwrap(), "0");
// $minor
- assert_eq!(
- unit.apply_format(&"$minor".to_string(), false).unwrap(),
- "0"
- );
+ assert_eq!(unit.apply_format("$minor", false).unwrap(), "0");
// $result
- assert_eq!(
- unit.apply_format(&"$result".to_string(), false).unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$result", false).unwrap(), "");
// $result{index}
- assert_eq!(
- unit.apply_format(&"$result{1}".to_string(), false).unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$result{1}", false).unwrap(), "");
// $result{index+}
- assert_eq!(
- unit.apply_format(&"$result{1+}".to_string(), false)
- .unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$result{1+}", false).unwrap(), "");
// $parent
- assert_eq!(
- unit.apply_format(&"$parent".to_string(), false).unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$parent", false).unwrap(), "");
// $name
- assert_eq!(
- unit.apply_format(&"$name".to_string(), false).unwrap(),
- "lo"
- );
+ assert_eq!(unit.apply_format("$name", false).unwrap(), "lo");
// $links
- assert_eq!(unit.apply_format(&"$links".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("$links", false).unwrap(), "");
// $root
- assert_eq!(
- unit.apply_format(&"$root".to_string(), false).unwrap(),
- "/dev"
- );
+ assert_eq!(unit.apply_format("$root", false).unwrap(), "/dev");
// $sys
- assert_eq!(
- unit.apply_format(&"$sys".to_string(), false).unwrap(),
- "/sys"
- );
+ assert_eq!(unit.apply_format("$sys", false).unwrap(), "/sys");
// $devnode
- assert_eq!(
- unit.apply_format(&"$devnode".to_string(), false).unwrap(),
- ""
- );
+ assert_eq!(unit.apply_format("$devnode", false).unwrap(), "");
// test short substitution formatter
// %k
- assert_eq!(unit.apply_format(&"%k".to_string(), false).unwrap(), "lo");
+ assert_eq!(unit.apply_format("%k", false).unwrap(), "lo");
// %n
- assert_eq!(unit.apply_format(&"%n".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%n", false).unwrap(), "");
// %p
assert_eq!(
- unit.apply_format(&"%p".to_string(), false).unwrap(),
+ unit.apply_format("%p", false).unwrap(),
"/devices/virtual/net/lo"
);
// %b
- assert_eq!(unit.apply_format(&"%b".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%b", false).unwrap(), "");
// %d
- assert_eq!(unit.apply_format(&"%d".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%d", false).unwrap(), "");
// %s{sysattr}
assert_eq!(
- unit.apply_format(&"%s{address}".to_string(), false)
- .unwrap(),
+ unit.apply_format("%s{address}", false).unwrap(),
"00:00:00:00:00:00"
);
// %E{key}
assert_eq!(
- unit.apply_format(&"%E{DEVPATH}".to_string(), false)
- .unwrap(),
+ unit.apply_format("%E{DEVPATH}", false).unwrap(),
"/devices/virtual/net/lo"
);
// %M
- assert_eq!(unit.apply_format(&"%M".to_string(), false).unwrap(), "0");
+ assert_eq!(unit.apply_format("%M", false).unwrap(), "0");
// %m
- assert_eq!(unit.apply_format(&"%m".to_string(), false).unwrap(), "0");
+ assert_eq!(unit.apply_format("%m", false).unwrap(), "0");
// %c
- assert_eq!(unit.apply_format(&"%c".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%c", false).unwrap(), "");
// %c{index}
- assert_eq!(unit.apply_format(&"%c{1}".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%c{1}", false).unwrap(), "");
// %c{index+}
- assert_eq!(unit.apply_format(&"%c{1+}".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%c{1+}", false).unwrap(), "");
// %P
- assert_eq!(unit.apply_format(&"%P".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%P", false).unwrap(), "");
// %D
- assert_eq!(unit.apply_format(&"%D".to_string(), false).unwrap(), "lo");
+ assert_eq!(unit.apply_format("%D", false).unwrap(), "lo");
// %L
- assert_eq!(unit.apply_format(&"%L".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%L", false).unwrap(), "");
// %r
- assert_eq!(unit.apply_format(&"%r".to_string(), false).unwrap(), "/dev");
+ assert_eq!(unit.apply_format("%r", false).unwrap(), "/dev");
// %S
- assert_eq!(unit.apply_format(&"%S".to_string(), false).unwrap(), "/sys");
+ assert_eq!(unit.apply_format("%S", false).unwrap(), "/sys");
// %N
- assert_eq!(unit.apply_format(&"%N".to_string(), false).unwrap(), "");
+ assert_eq!(unit.apply_format("%N", false).unwrap(), "");
// $$
- assert_eq!(unit.apply_format(&"$$".to_string(), false).unwrap(), "$");
+ assert_eq!(unit.apply_format("$$", false).unwrap(), "$");
// %%
- assert_eq!(unit.apply_format(&"%%".to_string(), false).unwrap(), "%");
+ assert_eq!(unit.apply_format("%%", false).unwrap(), "%");
}
#[test]
@@ -1790,30 +1746,12 @@ mod tests {
Device::from_subsystem_sysname("block".to_string(), "sda1".to_string()).unwrap(),
));
let unit = ExecuteUnit::new(device);
- assert_eq!(
- unit.apply_format(&"$number".to_string(), false).unwrap(),
- "1"
- );
- assert_eq!(
- unit.apply_format(&"$major".to_string(), false).unwrap(),
- "8"
- );
- assert_eq!(
- unit.apply_format(&"$minor".to_string(), false).unwrap(),
- "1"
- );
- assert_eq!(
- unit.apply_format(&"$driver".to_string(), false).unwrap(),
- ""
- );
- assert_eq!(unit.apply_format(&"$id".to_string(), false).unwrap(), "");
- assert_eq!(
- unit.apply_format(&"$parent".to_string(), false).unwrap(),
- "sda"
- );
- assert_eq!(
- unit.apply_format(&"$devnode".to_string(), false).unwrap(),
- "/dev/sda1"
- );
+ assert_eq!(unit.apply_format("$number", false).unwrap(), "1");
+ assert_eq!(unit.apply_format("$major", false).unwrap(), "8");
+ assert_eq!(unit.apply_format("$minor", false).unwrap(), "1");
+ assert_eq!(unit.apply_format("$driver", false).unwrap(), "");
+ assert_eq!(unit.apply_format("$id", false).unwrap(), "");
+ assert_eq!(unit.apply_format("$parent", false).unwrap(), "sda");
+ assert_eq!(unit.apply_format("$devnode", false).unwrap(), "/dev/sda1");
}
}
diff --git a/exts/init/src/runtime/param.rs b/exts/init/src/runtime/param.rs
index 7aed5dc..c795a8c 100644
--- a/exts/init/src/runtime/param.rs
+++ b/exts/init/src/runtime/param.rs
@@ -13,7 +13,7 @@ const DEFAULT_TIMECNT: i64 = 5;
const DEFAULT_TIMEWAIT: i64 = 10;
const INIT_PARAM: i32 = 0;
// const SYSMASTER_PARAM: i32 = 1;
-type Callback = fn(arg: &str, key: &String, init_param: &mut InitParam);
+type Callback = fn(arg: &str, key: &str, init_param: &mut InitParam);
struct Dispatch<'a> {
key: &'a str,
@@ -34,7 +34,7 @@ const PARAM_TABLE: &[Dispatch] = &[
},
];
-fn parse_timecnt(arg: &str, key: &String, init_param: &mut InitParam) {
+fn parse_timecnt(arg: &str, key: &str, init_param: &mut InitParam) {
let str1 = &arg[key.len()..];
if let Ok(value) = str1.parse::<i64>() {
if value >= 2 {
@@ -43,7 +43,7 @@ fn parse_timecnt(arg: &str, key: &String, init_param: &mut InitParam) {
}
}
-fn parse_timewait(arg: &str, key: &String, init_param: &mut InitParam) {
+fn parse_timewait(arg: &str, key: &str, init_param: &mut InitParam) {
let str1 = &arg[key.len()..];
if let Ok(value) = str1.parse::<i64>() {
if value >= DEFAULT_TIMEWAIT {
@@ -78,7 +78,7 @@ impl Param {
for table in PARAM_TABLE {
if arg.starts_with(table.key) && table.callback.is_some() {
if INIT_PARAM == table.param_type {
- table.callback.unwrap()(&arg, &table.key.to_string(), &mut self.init_param);
+ table.callback.unwrap()(&arg, table.key, &mut self.init_param);
} else {
self.manager_param.push(arg);
}
diff --git a/exts/init/src/runtime/timer.rs b/exts/init/src/runtime/timer.rs
index c054c6c..f7b5f30 100644
--- a/exts/init/src/runtime/timer.rs
+++ b/exts/init/src/runtime/timer.rs
@@ -55,6 +55,7 @@ impl Timer {
}
}
+ #[allow(clippy::wrong_self_convention)]
pub fn is_time_out(&mut self, event: EpollEvent) -> Result<bool, Errno> {
if self.epoll.is_err(event) {
return Err(Errno::EIO);
diff --git a/exts/random_seed/src/random_seed.rs b/exts/random_seed/src/random_seed.rs
index 2130b2e..f862163 100644
--- a/exts/random_seed/src/random_seed.rs
+++ b/exts/random_seed/src/random_seed.rs
@@ -58,7 +58,7 @@ fn random_pool_size() -> usize {
}
}
-pub fn get_random(data: &mut Vec<u8>, flags: u32) -> Result<usize, ()> {
+pub fn get_random(data: &mut [u8], flags: u32) -> Result<usize, ()> {
let size;
unsafe {
size = libc::getrandom(data.as_mut_ptr() as *mut libc::c_void, data.len(), flags);
@@ -103,7 +103,7 @@ ioctl_write_ptr!(
ENTROPY_SETOPTIONS,
rand_pool_info
);
-fn random_write_entropy(random_fd: &mut File, data: &mut Vec<u8>, credit: bool) -> bool {
+fn random_write_entropy(random_fd: &mut File, data: &mut [u8], credit: bool) -> bool {
assert!(!data.is_empty());
if data.is_empty() {
diff --git a/libs/basic/src/user_group_util.rs b/libs/basic/src/user_group_util.rs
index bd9e4b2..7af8d62 100644
--- a/libs/basic/src/user_group_util.rs
+++ b/libs/basic/src/user_group_util.rs
@@ -117,7 +117,7 @@ pub fn parse_gid(gid_str: &String) -> Result<Group> {
}
/// Parse a string as Username
-pub fn parse_name(name_str: &String) -> Result<User> {
+pub fn parse_name(name_str: &str) -> Result<User> {
if name_str.is_empty() {
return Err(Error::Invalid {
what: "Username is empty".to_string(),
diff --git a/libs/event/src/timer.rs b/libs/event/src/timer.rs
index 31c808c..a94025b 100644
--- a/libs/event/src/timer.rs
+++ b/libs/event/src/timer.rs
@@ -126,6 +126,7 @@ impl Timer {
}
}
+ #[allow(clippy::wrong_self_convention)]
pub fn is_empty(&mut self, et: &EventType) -> bool {
if let Some(inner) = self.timer_set.get_mut(et) {
return inner.data.is_empty();
diff --git a/libs/macros/src/unit_conf_parse.rs b/libs/macros/src/unit_conf_parse.rs
index de9c6f0..ab0c0a2 100644
--- a/libs/macros/src/unit_conf_parse.rs
+++ b/libs/macros/src/unit_conf_parse.rs
@@ -128,13 +128,12 @@ fn gererate_setter_functions(
let mut final_stream = proc_macro2::TokenStream::new();
for (ident, _type) in idents.iter().zip(types.iter()) {
- let token_piece;
let set_field_name = format!("set_{}", ident.as_ref().unwrap());
let get_field_name = format!("get_{}", ident.as_ref().unwrap());
let set_field_ident = syn::Ident::new(&set_field_name, ident.span());
let get_field_ident = syn::Ident::new(&get_field_name, ident.span());
- if let Some(inner_ty) = get_option_inner_type(_type) {
- token_piece = quote! {
+ let token_piece = if let Some(inner_ty) = get_option_inner_type(_type) {
+ quote! {
#vis fn #set_field_ident(&mut self, #ident: #inner_ty) -> &mut Self{
self.#ident = std::option::Option::Some(#ident);
self
@@ -146,9 +145,9 @@ fn gererate_setter_functions(
return std::option::Option::Some(self.#ident.as_ref().unwrap().clone());
}
}
- };
+ }
} else {
- token_piece = quote! {
+ quote! {
#vis fn #set_field_ident(&mut self, #ident: #_type) -> &mut Self{
self.#ident = #ident;
self
@@ -156,8 +155,8 @@ fn gererate_setter_functions(
#vis fn #get_field_ident(&self) -> #_type{
return self.#ident.clone();
}
- };
- }
+ }
+ };
final_stream.extend(token_piece);
}
Ok(final_stream)
diff --git a/tests/docker_config_test.rs b/tests/docker_config_test.rs
index a3969ee..b98c612 100644
--- a/tests/docker_config_test.rs
+++ b/tests/docker_config_test.rs
@@ -10,6 +10,7 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
+#[rustfmt::skip]
mod common;
#[test]
diff --git a/tests/docker_example.rs b/tests/docker_example.rs
index a2f2952..97b1127 100644
--- a/tests/docker_example.rs
+++ b/tests/docker_example.rs
@@ -10,6 +10,7 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
+#[rustfmt::skip]
mod common;
#[test]
diff --git a/tests/docker_perf.rs b/tests/docker_perf.rs
index 2a45ab0..41021ed 100644
--- a/tests/docker_perf.rs
+++ b/tests/docker_perf.rs
@@ -10,6 +10,7 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
+#[rustfmt::skip]
mod common;
#[test]
diff --git a/tests/docker_reliable.rs b/tests/docker_reliable.rs
index 63a2420..8db2c0d 100644
--- a/tests/docker_reliable.rs
+++ b/tests/docker_reliable.rs
@@ -10,6 +10,7 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
+#[rustfmt::skip]
mod common;
#[test]
--
2.30.2

View File

@ -1,420 +0,0 @@
From cd97d90314143141cecd694ca5f4a0edad35c257 Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Thu, 18 May 2023 11:45:17 +0800
Subject: [PATCH] fix: compatible with rustc 1.60
---
coms/service/src/rentry.rs | 45 +++++++++++++++-----
coms/socket/src/rentry.rs | 9 +++-
core/bin/main.rs | 3 +-
core/bin/unit/entry/config.rs | 9 +++-
core/bin/unit/rentry.rs | 9 +++-
core/lib/unit/kill.rs | 9 +++-
exts/sctl/src/main.rs | 71 +++++++-------------------------
libs/device/src/device_action.rs | 9 +++-
8 files changed, 88 insertions(+), 76 deletions(-)
diff --git a/coms/service/src/rentry.rs b/coms/service/src/rentry.rs
index 175828e..a68a3df 100644
--- a/coms/service/src/rentry.rs
+++ b/coms/service/src/rentry.rs
@@ -39,10 +39,9 @@ struct ServiceReDb<K, V>(ReDb<K, V>);
const RELI_DB_HSERVICE_CONF: &str = "svcconf";
const RELI_DB_HSERVICE_MNG: &str = "svcmng";
-#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy, Default)]
+#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy)]
pub(super) enum ServiceType {
#[serde(alias = "simple")]
- #[default]
Simple,
#[serde(alias = "forking")]
Forking,
@@ -56,6 +55,12 @@ pub(super) enum ServiceType {
TypeInvalid = -1,
}
+impl Default for ServiceType {
+ fn default() -> Self {
+ Self::Simple
+ }
+}
+
impl DeserializeWith for ServiceType {
type Item = Self;
fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
@@ -82,10 +87,9 @@ pub(super) enum NotifyAccess {
Main,
}
-#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy, Default)]
+#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy)]
pub(super) enum ServiceRestart {
#[serde(alias = "no")]
- #[default]
No,
#[serde(alias = "on-success")]
OnSuccess,
@@ -101,6 +105,12 @@ pub(super) enum ServiceRestart {
Always,
}
+impl Default for ServiceRestart {
+ fn default() -> Self {
+ Self::No
+ }
+}
+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExitStatusSet {
status: Vec<u8>,
@@ -163,14 +173,19 @@ impl DeserializeWith for ExitStatusSet {
}
}
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
+#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum PreserveMode {
- #[default]
No,
Yes,
Restart,
}
+impl Default for PreserveMode {
+ fn default() -> Self {
+ Self::No
+ }
+}
+
impl DeserializeWith for PreserveMode {
type Item = Self;
@@ -330,9 +345,8 @@ impl ServiceReConf {
}
}
-#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize, EnumDisplay, Default)]
+#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize, EnumDisplay)]
pub(super) enum ServiceState {
- #[default]
Dead,
Condition,
StartPre,
@@ -354,7 +368,13 @@ pub(super) enum ServiceState {
Cleaning,
}
-#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize, Default)]
+impl Default for ServiceState {
+ fn default() -> Self {
+ Self::Dead
+ }
+}
+
+#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize)]
pub(super) enum ServiceResult {
Success,
FailureProtocol,
@@ -366,10 +386,15 @@ pub(super) enum ServiceResult {
FailureCoreDump,
FailureTimeout,
SkipCondition,
- #[default]
ResultInvalid,
}
+impl Default for ServiceResult {
+ fn default() -> Self {
+ Self::ResultInvalid
+ }
+}
+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy, Clone, Serialize, Deserialize)]
pub(super) enum ServiceCommand {
Condition,
diff --git a/coms/socket/src/rentry.rs b/coms/socket/src/rentry.rs
index 2409c84..44f5be3 100644
--- a/coms/socket/src/rentry.rs
+++ b/coms/socket/src/rentry.rs
@@ -146,15 +146,20 @@ pub(super) enum SocketCommand {
StopPost,
}
-#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Default)]
+#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub(super) enum PortType {
- #[default]
Socket,
Fifo,
Special,
Invalid,
}
+impl Default for PortType {
+ fn default() -> Self {
+ Self::Socket
+ }
+}
+
#[derive(Clone, Debug, Serialize, Deserialize)]
struct SocketReMng {
state: SocketState,
diff --git a/core/bin/main.rs b/core/bin/main.rs
index 1949cae..26638d6 100644
--- a/core/bin/main.rs
+++ b/core/bin/main.rs
@@ -186,7 +186,8 @@ fn do_reexecute(args: &Vec<String>, reload: bool) {
// Remove '--deserialize' from the previous parameter first, as this may be a fault recovery start.
for index in argv.iter().enumerate() {
if index.1 == "--deserialize" {
- argv.remove(index.0);
+ let idx = index.0;
+ argv.remove(idx);
break;
}
}
diff --git a/core/bin/unit/entry/config.rs b/core/bin/unit/entry/config.rs
index 9e8cc62..e629c77 100644
--- a/core/bin/unit/entry/config.rs
+++ b/core/bin/unit/entry/config.rs
@@ -31,10 +31,9 @@ pub(crate) struct UeConfig {
}
#[allow(missing_docs)]
-#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnitEmergencyAction {
#[serde(alias = "none")]
- #[default]
None,
#[serde(alias = "reboot")]
Reboot,
@@ -54,6 +53,12 @@ pub enum UnitEmergencyAction {
ExitForce,
}
+impl Default for UnitEmergencyAction {
+ fn default() -> Self {
+ Self::None
+ }
+}
+
impl DeserializeWith for UnitEmergencyAction {
type Item = Self;
fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
diff --git a/core/bin/unit/rentry.rs b/core/bin/unit/rentry.rs
index 739a323..5d154df 100644
--- a/core/bin/unit/rentry.rs
+++ b/core/bin/unit/rentry.rs
@@ -68,12 +68,11 @@ impl UnitReLoad {
}
/// jobMode why in UnitRentry? change the name?
-#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum JobMode {
#[serde(alias = "fail")]
Fail,
#[serde(alias = "replace")]
- #[default]
Replace,
#[serde(alias = "replace_irreversible")]
ReplaceIrreversible,
@@ -89,6 +88,12 @@ pub(crate) enum JobMode {
Trigger,
}
+impl Default for JobMode {
+ fn default() -> Self {
+ Self::Replace
+ }
+}
+
impl DeserializeWith for JobMode {
type Item = Self;
fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
diff --git a/core/lib/unit/kill.rs b/core/lib/unit/kill.rs
index 893125e..4d66861 100644
--- a/core/lib/unit/kill.rs
+++ b/core/lib/unit/kill.rs
@@ -42,10 +42,9 @@ impl KillOperation {
}
/// the method to kill the process
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum KillMode {
/// kill all the process in the cgroup of the unit
- #[default]
ControlGroup,
/// only kill the main process
Process,
@@ -53,6 +52,12 @@ pub enum KillMode {
Mixed,
}
+impl Default for KillMode {
+ fn default() -> Self {
+ Self::ControlGroup
+ }
+}
+
impl DeserializeWith for KillMode {
type Item = Self;
fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
diff --git a/exts/sctl/src/main.rs b/exts/sctl/src/main.rs
index 20f1934..f799629 100644
--- a/exts/sctl/src/main.rs
+++ b/exts/sctl/src/main.rs
@@ -13,17 +13,14 @@
//!
use clap::Parser;
-use cmdproto::{
- error::ERROR_CODE_MASK_PRINT_STDOUT,
- proto::{
- abi::{sys_comm, unit_comm, CommandRequest},
- mngr_comm, unit_file, ProstClientStream,
- },
+use cmdproto::proto::{
+ abi::{sys_comm, unit_comm, CommandRequest},
+ mngr_comm, unit_file, ProstClientStream,
};
use std::io::Write;
use std::{
net::{SocketAddr, TcpStream},
- process::{ExitCode, Termination},
+ process::exit,
};
/// parse program arguments
@@ -187,50 +184,13 @@ fn generate_command_request(args: Args) -> Option<CommandRequest> {
Some(command_request)
}
-/// Result used in sctl
-pub enum Result {
- ///
- OK,
- ///
- Failure(String, u32),
-}
-
-/*
- * Implementing Termination is important. Because the default implementation
- * always adds a "Error: " prefix to our error message, this is ugly. And it
- * always exits with 1 if we return Err, this is bad, we want sctl to return
- * many other positive errors like systemctl has done.
- */
-
-impl Termination for Result {
- fn report(self) -> ExitCode {
- match self {
- Result::OK => ExitCode::SUCCESS,
- Result::Failure(s, error_code) => {
- if error_code & ERROR_CODE_MASK_PRINT_STDOUT != 0 {
- let _ = writeln!(std::io::stdout(), "{s}");
- } else {
- let _ = writeln!(std::io::stderr(), "{s}");
- }
- let error_code = error_code ^ ERROR_CODE_MASK_PRINT_STDOUT;
- if error_code > u8::MAX.into() {
- return ExitCode::FAILURE;
- }
- ExitCode::from(error_code as u8)
- }
- }
- }
-}
-
-fn main() -> Result {
+fn main() {
let args = Args::parse();
let command_request = match generate_command_request(args) {
None => {
- return Result::Failure(
- "This command is currently not supported".to_string(),
- nix::Error::ENOTSUP as u32,
- );
+ eprintln!("This command is currently not supported");
+ exit(nix::Error::ENOTSUP as i32);
}
Some(v) => v,
};
@@ -242,8 +202,8 @@ fn main() -> Result {
let stream = match TcpStream::connect(&addrs[..]) {
Err(e) => {
- let err_msg = format!("Failed to connect to sysmaster: {}", e);
- return Result::Failure(err_msg, e.raw_os_error().unwrap() as u32);
+ eprintln!("Failed to connect to sysmaster: {}", e);
+ exit(e.raw_os_error().unwrap() as i32);
}
Ok(v) => v,
};
@@ -252,23 +212,24 @@ fn main() -> Result {
let data = match client.execute(command_request) {
Err(e) => {
- let err_msg = format!("Failed to execute the given command: {}", e);
- // don't want to map all kinds of errors the message is clear, just return 1.
- return Result::Failure(err_msg, 1);
+ eprintln!("Failed to execute the given command: {}", e);
+ exit(1);
}
Ok(v) => v,
};
/* We should always print the error message if the returned error code is not 0. */
if data.message.is_empty() {
- return Result::OK;
+ exit(0);
}
if data.error_code == 0 {
/* Don't care if we fail to write the error out. */
let _ = writeln!(std::io::stdout(), "{}", data.message);
- return Result::OK;
+ exit(0);
}
- Result::Failure(data.message, data.error_code)
+ eprintln!("{}", data.message);
+
+ exit(data.error_code as i32);
}
diff --git a/libs/device/src/device_action.rs b/libs/device/src/device_action.rs
index ac39820..d9c5289 100644
--- a/libs/device/src/device_action.rs
+++ b/libs/device/src/device_action.rs
@@ -19,7 +19,7 @@ use nix::errno::Errno;
/// device action based on kobject from kernel
#[allow(missing_docs)]
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceAction {
Add,
Remove,
@@ -29,10 +29,15 @@ pub enum DeviceAction {
Offline,
Bind,
Unbind,
- #[default]
Invalid,
}
+impl Default for DeviceAction {
+ fn default() -> Self {
+ Self::Invalid
+ }
+}
+
impl FromStr for DeviceAction {
type Err = crate::error::Error;
--
2.30.2

View File

@ -1,26 +0,0 @@
From a2563cafd9b64d0703b69bc4dddc653d5c092bf3 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Tue, 13 Jun 2023 20:21:31 +0800
Subject: [PATCH] fix: don't mount all subsystem to every /sys/fs/cgroup
directory
---
core/bin/mount/setup.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/core/bin/mount/setup.rs b/core/bin/mount/setup.rs
index ccc6a25..6a13626 100644
--- a/core/bin/mount/setup.rs
+++ b/core/bin/mount/setup.rs
@@ -428,7 +428,7 @@ pub fn mount_cgroup_controllers() -> Result<()> {
} else {
(controllers[index].to_string(), "".to_string())
};
-
+ m_point.options = Some(target.to_string());
let target_dir = Path::new(CG_BASE_DIR).join(target);
let target = target_dir.to_str().expect("invalid cgroup path");
m_point.set_target(target);
--
2.30.2

View File

@ -1,105 +0,0 @@
From 86f01db06f5c20416a17a88a207ffb012588d0d8 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:17:52 +0800
Subject: [PATCH 7/9] fix: enable trigger() for socket, and use this interface
to check service state make socket reenter listening state when it triggered
service enters dead state.
---
coms/socket/src/mng.rs | 3 ++-
coms/socket/src/unit.rs | 16 ++++++++++++++++
core/bin/unit/entry/uentry.rs | 5 +++++
core/bin/unit/entry/unitx.rs | 4 +++-
core/lib/unit/base.rs | 3 +++
5 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index 82fdcbe..0435827 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -341,8 +341,9 @@ impl SocketMng {
self.set_state(SocketState::StartPost);
}
- fn enter_listening(&self) {
+ pub(crate) fn enter_listening(&self) {
log::debug!("enter start listening state");
+ /* Seem logic as systemd's socket has configured FlushPending=yes */
if !self.config.config_data().borrow().Socket.Accept {
self.flush_ports();
}
diff --git a/coms/socket/src/unit.rs b/coms/socket/src/unit.rs
index 8e33ee8..d96c258 100644
--- a/coms/socket/src/unit.rs
+++ b/coms/socket/src/unit.rs
@@ -115,6 +115,22 @@ impl SubUnit for SocketUnit {
Ok(())
}
+ fn trigger(&self, other: &String) {
+ let um = self.comm.um();
+ let service_state = um.get_subunit_state(other);
+ if [
+ "dead".to_string(),
+ "failed".to_string(),
+ "finalsigterm".to_string(),
+ "finalsigkill".to_string(),
+ "autorestart".to_string(),
+ ]
+ .contains(&service_state)
+ {
+ self.mng.enter_listening();
+ }
+ }
+
fn sigchld_events(&self, wait_status: WaitStatus) {
self.mng.sigchld_event(wait_status)
}
diff --git a/core/bin/unit/entry/uentry.rs b/core/bin/unit/entry/uentry.rs
index 307240b..f6b4db4 100644
--- a/core/bin/unit/entry/uentry.rs
+++ b/core/bin/unit/entry/uentry.rs
@@ -545,6 +545,11 @@ impl Unit {
self.config.clone()
}
+ pub(super) fn trigger(&self, other: &Self) {
+ let other_unit_id = other.id();
+ self.sub.trigger(other_unit_id);
+ }
+
pub(super) fn in_load_queue(&self) -> bool {
self.load.in_load_queue()
}
diff --git a/core/bin/unit/entry/unitx.rs b/core/bin/unit/entry/unitx.rs
index d9878f4..92b4ebb 100644
--- a/core/bin/unit/entry/unitx.rs
+++ b/core/bin/unit/entry/unitx.rs
@@ -106,7 +106,9 @@ impl UnitX {
self.0.sigchld_events(wait_status)
}
- pub(crate) fn trigger(&self, _other: &Self) {}
+ pub(crate) fn trigger(&self, other: &Self) {
+ self.0.trigger(other);
+ }
pub(crate) fn in_load_queue(&self) -> bool {
self.0.in_load_queue()
}
diff --git a/core/lib/unit/base.rs b/core/lib/unit/base.rs
index d279295..8d76cab 100644
--- a/core/lib/unit/base.rs
+++ b/core/lib/unit/base.rs
@@ -127,6 +127,9 @@ pub trait SubUnit: ReStation + UnitMngUtil {
Vec::new()
}
+ ///
+ fn trigger(&self, _other: &String) {}
+
///Get the the unit state
///
/// Every sub unit can define self states and map to [`UnitActiveState`]
--
2.30.2

View File

@ -1,122 +0,0 @@
From 4490bcd3d40b15f22446af46500039d2cbd37bf9 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:24:36 +0800
Subject: [PATCH 9/9] fix: fix some cargo clippies
---
coms/socket/src/unit.rs | 4 ++--
core/bin/unit/data/table.rs | 3 +--
core/bin/unit/datastore/sets.rs | 2 +-
core/bin/utils/table.rs | 6 +-----
core/lib/error.rs | 3 +++
core/lib/unit/base.rs | 2 +-
6 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/coms/socket/src/unit.rs b/coms/socket/src/unit.rs
index d96c258..9f0f6ce 100644
--- a/coms/socket/src/unit.rs
+++ b/coms/socket/src/unit.rs
@@ -22,8 +22,8 @@ use crate::{
};
use basic::logger;
use nix::sys::wait::WaitStatus;
-use std::{path::PathBuf, rc::Rc};
use std::any::Any;
+use std::{path::PathBuf, rc::Rc};
use sysmaster::error::*;
use sysmaster::exec::ExecContext;
use sysmaster::rel::{ReStation, Reliability};
@@ -115,7 +115,7 @@ impl SubUnit for SocketUnit {
Ok(())
}
- fn trigger(&self, other: &String) {
+ fn trigger(&self, other: &str) {
let um = self.comm.um();
let service_state = um.get_subunit_state(other);
if [
diff --git a/core/bin/unit/data/table.rs b/core/bin/unit/data/table.rs
index 161ac5f..256e8df 100644
--- a/core/bin/unit/data/table.rs
+++ b/core/bin/unit/data/table.rs
@@ -60,8 +60,7 @@ impl DataManager {
ud_config: UnitDepConf,
) -> Option<UnitDepConf> {
{
- let old = self.tables.0.insert(u_name, ud_config);
- old
+ self.tables.0.insert(u_name, ud_config)
}
}
diff --git a/core/bin/unit/datastore/sets.rs b/core/bin/unit/datastore/sets.rs
index 538df8f..9a94a4b 100644
--- a/core/bin/unit/datastore/sets.rs
+++ b/core/bin/unit/datastore/sets.rs
@@ -62,7 +62,7 @@ impl UnitSets {
.borrow()
.get_all()
.iter()
- .map(|ur| Rc::clone(ur))
+ .map(Rc::clone)
.collect::<Vec<_>>()
}
diff --git a/core/bin/utils/table.rs b/core/bin/utils/table.rs
index e1768d2..482d168 100644
--- a/core/bin/utils/table.rs
+++ b/core/bin/utils/table.rs
@@ -82,11 +82,7 @@ where
}
pub fn get_all(&self) -> Vec<V> {
- self.data
- .borrow()
- .values()
- .map(|v| v.clone())
- .collect::<Vec<V>>()
+ self.data.borrow().values().cloned().collect::<Vec<V>>()
}
pub fn subscribe(
diff --git a/core/lib/error.rs b/core/lib/error.rs
index 331f367..72471db 100644
--- a/core/lib/error.rs
+++ b/core/lib/error.rs
@@ -290,6 +290,7 @@ impl From<event::Error> for Error {
/// new Result
pub type Result<T, E = Error> = std::result::Result<T, E>;
+/// check if the error is disconnect
pub fn error_is_disconnect(e: &Errno) -> bool {
[
Errno::ECONNABORTED,
@@ -310,10 +311,12 @@ pub fn error_is_disconnect(e: &Errno) -> bool {
.contains(e)
}
+/// check if the error is transient
pub fn error_is_transient(e: &Errno) -> bool {
[Errno::EAGAIN, Errno::EINTR].contains(e)
}
+/// check if the error is accept or again
pub fn error_is_accept_again(e: &Errno) -> bool {
error_is_disconnect(e) || error_is_transient(e) || e == &Errno::EOPNOTSUPP
}
diff --git a/core/lib/unit/base.rs b/core/lib/unit/base.rs
index 8d76cab..2ec3e73 100644
--- a/core/lib/unit/base.rs
+++ b/core/lib/unit/base.rs
@@ -128,7 +128,7 @@ pub trait SubUnit: ReStation + UnitMngUtil {
}
///
- fn trigger(&self, _other: &String) {}
+ fn trigger(&self, _other: &str) {}
///Get the the unit state
///
--
2.30.2

View File

@ -1,28 +0,0 @@
From 71b0283e4c34078497c4aced37234949d833f45c Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Wed, 7 Jun 2023 00:40:37 +0800
Subject: [PATCH] fix(fstab): use fixed inotify version 0.10.0 to be compatible
with rustc 1.60
Latest inotify crate 0.10.1 requires rustc 1.63.0. Previously we didn't
state the minimum version number of inotify, which lead to cargo
automatically use the latest version and fails to build.
---
exts/fstab/Cargo.toml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/exts/fstab/Cargo.toml b/exts/fstab/Cargo.toml
index 3445698..59ba4c4 100644
--- a/exts/fstab/Cargo.toml
+++ b/exts/fstab/Cargo.toml
@@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-inotify = "0.10"
+inotify = "=0.10.0"
log = "0.4"
nix = "0.24"
--
2.30.2

View File

@ -1,814 +0,0 @@
From a80ad5b0aef20f9f5d6e5f06450311c39075a16e Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Thu, 25 May 2023 08:22:31 +0800
Subject: [PATCH] fix: inconsistent data in app memory and db after
daemon-reload or daemon-reexec
---
coms/mount/src/rentry.rs | 16 ++++++--
coms/service/src/mng.rs | 1 +
coms/service/src/rentry.rs | 16 ++++++--
coms/socket/src/mng.rs | 11 ++---
coms/socket/src/rentry.rs | 24 ++++++++---
coms/target/src/rentry.rs | 8 +++-
core/bin/job/entry.rs | 4 +-
core/bin/job/manager.rs | 9 +++++
core/bin/job/table.rs | 30 ++++++++++++++
core/bin/unit/datastore/child.rs | 9 +++++
core/bin/unit/datastore/deps.rs | 5 +++
core/bin/unit/datastore/mod.rs | 8 ++++
core/bin/unit/entry/uentry.rs | 11 +++++
core/bin/unit/entry/unitx.rs | 4 ++
core/bin/unit/manager.rs | 28 +++++++++++++
core/bin/unit/runtime.rs | 11 +++++
core/lib/rel/api.rs | 30 ++++++++++----
core/lib/rel/base.rs | 69 +++++++++++++++++++++++---------
core/lib/rel/history.rs | 23 +++++++++--
core/lib/rel/station.rs | 12 ++++++
20 files changed, 277 insertions(+), 52 deletions(-)
diff --git a/coms/mount/src/rentry.rs b/coms/mount/src/rentry.rs
index 814ad21..45c0ce0 100644
--- a/coms/mount/src/rentry.rs
+++ b/coms/mount/src/rentry.rs
@@ -103,12 +103,16 @@ impl ReDbTable for MountReDb<String, MountReMng> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
@@ -121,11 +125,15 @@ impl ReDbTable for MountReDb<u32, MountReFrame> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
diff --git a/coms/service/src/mng.rs b/coms/service/src/mng.rs
index d73fa34..0f5db5f 100644
--- a/coms/service/src/mng.rs
+++ b/coms/service/src/mng.rs
@@ -130,6 +130,7 @@ impl ReStation for ServiceMng {
}
fn entry_clear(&self) {
+ // pid_file is a transient file that can be directly closed
self.unwatch_pid_file();
self.stop_watchdog();
diff --git a/coms/service/src/rentry.rs b/coms/service/src/rentry.rs
index df8a255..175828e 100644
--- a/coms/service/src/rentry.rs
+++ b/coms/service/src/rentry.rs
@@ -598,12 +598,16 @@ impl ReDbTable for ServiceReDb<String, ServiceReConf> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
@@ -616,11 +620,15 @@ impl ReDbTable for ServiceReDb<String, ServiceReMng> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index e079acd..e7a6a01 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -109,13 +109,14 @@ impl ReStation for SocketMng {
// reload: entry-only
fn entry_coldplug(&self) {
- if self.state() != SocketState::Listening {
+ if self.state() == SocketState::Listening {
self.watch_fds();
}
}
fn entry_clear(&self) {
- self.close_fds();
+ // port fd is a long-term monitoring file and cannot be closed
+ self.unwatch_fds();
}
}
@@ -690,11 +691,11 @@ impl SocketMng {
}
fn map_ports_fd(&self, rports: Vec<(PortType, String, RawFd)>) {
- assert_eq!(rports.len(), self.ports().len());
-
for (p_type, listen, fd) in rports.iter() {
match self.ports_find(*p_type, listen) {
- Some(port) => port.set_fd(self.comm.reli().fd_take(*fd)),
+ Some(port) => {
+ port.set_fd(self.comm.reli().fd_take(*fd));
+ }
None => log::debug!("Not find {:?}:{:?}", *p_type, listen),
}
}
diff --git a/coms/socket/src/rentry.rs b/coms/socket/src/rentry.rs
index 1794e69..2409c84 100644
--- a/coms/socket/src/rentry.rs
+++ b/coms/socket/src/rentry.rs
@@ -330,12 +330,16 @@ impl ReDbTable for SocketReDb<String, SocketReConf> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
@@ -348,12 +352,16 @@ impl ReDbTable for SocketReDb<String, SocketReMng> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
@@ -366,11 +374,15 @@ impl ReDbTable for SocketReDb<u32, SocketReFrame> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
diff --git a/coms/target/src/rentry.rs b/coms/target/src/rentry.rs
index 8f0ce69..9d3f90c 100644
--- a/coms/target/src/rentry.rs
+++ b/coms/target/src/rentry.rs
@@ -77,11 +77,15 @@ impl ReDbTable for TargetReDb<String, TargetReMng> {
self.0.cache_2_db(db_wtxn);
}
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.0.rebuf_2_db(db_wtxn);
+ }
+
fn import<'a>(&self, db_rtxn: &ReDbRoTxn) {
self.0.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.0.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.0.set_switch(switch);
}
}
diff --git a/core/bin/job/entry.rs b/core/bin/job/entry.rs
index 55a0015..54af665 100755
--- a/core/bin/job/entry.rs
+++ b/core/bin/job/entry.rs
@@ -472,7 +472,7 @@ impl Job {
last_rkind != rkind
}
- fn rentry_trigger_insert(&self) {
+ pub(super) fn rentry_trigger_insert(&self) {
self.rentry
.trigger_insert(self.unit.id(), self.kind, &self.attr.borrow());
}
@@ -485,7 +485,7 @@ impl Job {
self.rentry.trigger_get(self.unit.id())
}
- fn rentry_suspends_insert(&self) {
+ pub(super) fn rentry_suspends_insert(&self) {
self.rentry
.suspends_insert(self.unit.id(), self.kind, &self.attr.borrow());
}
diff --git a/core/bin/job/manager.rs b/core/bin/job/manager.rs
index f5a247d..e8eee8b 100755
--- a/core/bin/job/manager.rs
+++ b/core/bin/job/manager.rs
@@ -108,6 +108,10 @@ impl ReStation for JobManager {
self.data.db_map();
}
+ fn db_insert(&self) {
+ self.data.db_insert();
+ }
+
// reload: special entry_coldplug
// repeating protection
fn entry_clear(&self) {
@@ -386,6 +390,11 @@ impl JobManagerData {
self.stat.clear_cnt(); // no history
}
+ pub(self) fn db_insert(&self) {
+ self.jobs.rentry_insert_trigger();
+ self.jobs.rentry_insert_suspend();
+ }
+
pub(self) fn coldplug_unit(&self, unit: &UnitX) {
// trigger
self.jobs.coldplug_trigger(unit);
diff --git a/core/bin/job/table.rs b/core/bin/job/table.rs
index 91ac45b..7b77b1a 100644
--- a/core/bin/job/table.rs
+++ b/core/bin/job/table.rs
@@ -60,6 +60,12 @@ impl JobTable {
job
}
+ pub(super) fn rentry_insert_suspend(&self) {
+ for job in self.t_unit.borrow().get_all_suspends() {
+ job.rentry_suspends_insert();
+ }
+ }
+
pub(super) fn rentry_map_trigger(&self, ja: &JobAlloc, config: &JobConf) -> Rc<Job> {
let job = ja.alloc(config);
job.rentry_map_trigger();
@@ -67,6 +73,12 @@ impl JobTable {
job
}
+ pub(super) fn rentry_insert_trigger(&self) {
+ for job in self.t_unit.borrow().get_all_triggers() {
+ job.rentry_trigger_insert();
+ }
+ }
+
pub(super) fn coldplug_suspend(&self, unit: &UnitX) {
for job in self.t_unit.borrow().get_suspends(unit).iter() {
job.coldplug_suspend();
@@ -712,6 +724,24 @@ impl JobUnitTable {
jobs
}
+ pub(self) fn get_all_suspends(&self) -> Vec<Rc<Job>> {
+ let mut jobs = Vec::new();
+ for (_, uv) in self.t_data.iter() {
+ jobs.append(&mut uv.get_suspends());
+ }
+ jobs
+ }
+
+ pub(self) fn get_all_triggers(&self) -> Vec<Rc<Job>> {
+ let mut jobs = Vec::new();
+ for (_, uv) in self.t_data.iter() {
+ if let Some(job) = uv.get_trigger() {
+ jobs.push(job);
+ }
+ }
+ jobs
+ }
+
pub(self) fn get_trigger_info(&self, unit: &UnitX) -> Option<(Rc<Job>, bool)> {
if let Some(uv) = self.t_data.get(unit) {
uv.get_trigger()
diff --git a/core/bin/unit/datastore/child.rs b/core/bin/unit/datastore/child.rs
index af63792..60c73c5 100644
--- a/core/bin/unit/datastore/child.rs
+++ b/core/bin/unit/datastore/child.rs
@@ -37,6 +37,10 @@ impl ReStation for UnitChild {
self.data.db_map(&self.units);
}
+ fn db_insert(&self) {
+ self.data.db_insert();
+ }
+
// reload
fn entry_clear(&self) {
self.data.entry_clear();
@@ -132,6 +136,11 @@ impl UnitChildData {
}
}
+ pub(self) fn db_insert(&self) {
+ // [key:pid, data:unit[s]] and [key:unit, data:pid[s]](RELI_DB_HUNIT_CHILD) are equivalent
+ // So here [key:unit, data:pids] is reused and [key:pid, data:unit[s]] is not stored in the database.
+ }
+
pub(self) fn add_watch_pid(&self, unit: Rc<UnitX>, pid: Pid) {
let mut watch_pids = self.watch_pids.borrow_mut();
watch_pids.insert(pid, unit);
diff --git a/core/bin/unit/datastore/deps.rs b/core/bin/unit/datastore/deps.rs
index cd38d45..80836ef 100644
--- a/core/bin/unit/datastore/deps.rs
+++ b/core/bin/unit/datastore/deps.rs
@@ -39,6 +39,11 @@ impl ReStation for UnitDep {
self.sub.data.borrow_mut().db_map();
}
+ fn db_insert(&self) {
+ // If the data changes under db_map() when reload is true, it needs to be inserted.
+ // db_map currently does nothing to do.
+ }
+
// reload
fn entry_clear(&self) {
self.sub.data.borrow_mut().clear();
diff --git a/core/bin/unit/datastore/mod.rs b/core/bin/unit/datastore/mod.rs
index 1dc09d7..2a5f31b 100644
--- a/core/bin/unit/datastore/mod.rs
+++ b/core/bin/unit/datastore/mod.rs
@@ -70,6 +70,14 @@ impl UnitDb {
self.child.db_map(reload);
}
+ pub(super) fn db_insert_excl_units(&self) {
+ // dep
+ self.dep.db_insert();
+
+ // child
+ self.child.db_insert();
+ }
+
pub fn units_insert(&self, name: String, unit: Rc<UnitX>) -> Option<Rc<UnitX>> {
self.units.insert(name, unit)
}
diff --git a/core/bin/unit/entry/uentry.rs b/core/bin/unit/entry/uentry.rs
index fd05746..a22ad10 100644
--- a/core/bin/unit/entry/uentry.rs
+++ b/core/bin/unit/entry/uentry.rs
@@ -94,6 +94,17 @@ impl ReStation for Unit {
self.sub.db_map(reload);
}
+ // data insert
+ fn db_insert(&self) {
+ self.base.db_insert();
+ self.config.db_insert();
+ self.cgroup.db_insert();
+ self.load.db_insert();
+ self.child.db_insert();
+
+ self.sub.db_insert();
+ }
+
// reload: entry-only
fn entry_coldplug(&self) {
// rebuild external connections, like: timer, ...
diff --git a/core/bin/unit/entry/unitx.rs b/core/bin/unit/entry/unitx.rs
index 9ffd549..d9878f4 100644
--- a/core/bin/unit/entry/unitx.rs
+++ b/core/bin/unit/entry/unitx.rs
@@ -38,6 +38,10 @@ impl ReStation for UnitX {
self.0.db_map(reload);
}
+ fn db_insert(&self) {
+ self.0.db_insert();
+ }
+
// reload: entry-only
fn entry_coldplug(&self) {
self.0.entry_coldplug();
diff --git a/core/bin/unit/manager.rs b/core/bin/unit/manager.rs
index bee3093..6e83240 100644
--- a/core/bin/unit/manager.rs
+++ b/core/bin/unit/manager.rs
@@ -1198,6 +1198,28 @@ impl ReStation for UnitManager {
self.sms.db_map(reload);
}
+ // data
+ fn db_insert(&self) {
+ for unit in self.db.units_get_all(None).iter() {
+ unit.db_insert();
+ }
+
+ /* others: unit-dep and unit-child */
+ self.db.db_insert_excl_units();
+
+ // rt
+ self.rt.db_insert();
+
+ // job
+ self.jm.db_insert();
+
+ // notify
+ self.notify.db_insert();
+
+ // sub-manager
+ self.sms.db_insert();
+ }
+
// reload
fn register_ex(&self) {
// notify
@@ -1290,6 +1312,12 @@ mod unit_submanager {
}
}
+ pub(super) fn db_insert(&self) {
+ for (_, sub) in self.db.borrow().iter() {
+ sub.db_insert();
+ }
+ }
+
pub(super) fn db_compensate_last(
&self,
lframe: (u32, Option<u32>, Option<u32>),
diff --git a/core/bin/unit/runtime.rs b/core/bin/unit/runtime.rs
index 89e62c5..215a5a7 100644
--- a/core/bin/unit/runtime.rs
+++ b/core/bin/unit/runtime.rs
@@ -47,6 +47,10 @@ impl ReStation for UnitRT {
self.data.db_map(reload);
}
+ fn db_insert(&self) {
+ self.data.db_insert();
+ }
+
// reload
// repeating protection
fn entry_clear(&self) {
@@ -239,6 +243,13 @@ impl UnitRTData {
}
}
+ pub(self) fn db_insert(&self) {
+ // If the data changes under db_map() when reload is true, it needs to be inserted.
+ // db_map currently does nothing to do.
+ // QUEUE_LOAD is nothing to do.
+ // QUEUE_TARGET_DEPS is nothing to do.
+ }
+
pub(self) fn dispatch_load_queue(&self) {
log::trace!("dispatch load queue");
diff --git a/core/lib/rel/api.rs b/core/lib/rel/api.rs
index cabf24e..60a3543 100644
--- a/core/lib/rel/api.rs
+++ b/core/lib/rel/api.rs
@@ -151,6 +151,13 @@ impl Reliability {
self.input_rebuild();
self.db_compensate();
self.db_map(reload);
+ if reload {
+ // If daemon-reload or daemon-reexec, we need to update all changes, clear db, and submit all changes to db.
+ self.db_insert();
+ self.history.reflush();
+ // Due to changes in db, we need to reload the data from db to cache.
+ self.history.import();
+ }
self.make_consistent();
// restore last's ignore
@@ -263,12 +270,12 @@ impl Reliability {
fn input_rebuild(&self) {
// ignore history's input
- self.history.ignore_set(true);
+ self.history.switch_set(true);
self.station.input_rebuild();
// restore history's ignore
- self.history.ignore_set(false);
+ self.history.switch_set(false);
}
fn db_compensate(&self) {
@@ -282,15 +289,24 @@ impl Reliability {
}
/// map data from database
- /// If reload is true, determine whether the configuration needs to be reloaded based on the situation.
+ /// reload determine whether the configuration needs to be reloaded based on the situation.
fn db_map(&self, reload: bool) {
- // ignore history's input
- self.history.ignore_set(true);
+ // control use buf
+ self.history.switch_set(true);
self.station.db_map(reload);
- // restore history's ignore
- self.history.ignore_set(false);
+ // control use cache
+ self.history.switch_set(false);
+ }
+
+ /// map-data insert to buf, switch is true indicating either daemon-reload or daemon-reexec
+ fn db_insert(&self) {
+ self.history.switch_set(true);
+
+ self.station.db_insert();
+
+ self.history.switch_set(false);
}
fn make_consistent(&self) {
diff --git a/core/lib/rel/base.rs b/core/lib/rel/base.rs
index 782d54c..274b504 100644
--- a/core/lib/rel/base.rs
+++ b/core/lib/rel/base.rs
@@ -28,7 +28,7 @@ use std::{env, fs};
/// K & V that can be deserialized without borrowing any data from the deserializer.
pub struct ReDb<K, V> {
// control
- ignore: RefCell<bool>,
+ switch: RefCell<bool>,
// data
db: Database<SerdeBincode<K>, SerdeBincode<V>>,
@@ -36,7 +36,8 @@ pub struct ReDb<K, V> {
add: RefCell<HashMap<K, V>>,
del: RefCell<HashSet<K>>,
name: String,
- //_phantom: PhantomData<&'a K>,
+ buf: RefCell<HashMap<K, V>>, // daemon-reload or daemon-reexec will temporarily store the data here first, and finally refreshes it to db.
+ //_phantom: PhantomData<&'a K>,
}
impl<K, V> ReDbTable for ReDb<K, V>
@@ -52,12 +53,17 @@ where
self.cache_2_db(db_wtxn);
}
+ /// daemon-reload or daemon-reexec export all data to database
+ fn reexport(&self, db_wtxn: &mut ReDbRwTxn) {
+ self.rebuf_2_db(db_wtxn);
+ }
+
fn import(&self, db_rtxn: &ReDbRoTxn) {
self.db_2_cache(db_rtxn);
}
- fn ignore_set(&self, ignore: bool) {
- self.set_ignore(ignore);
+ fn switch_set(&self, switch: bool) {
+ self.set_switch(switch);
}
}
@@ -70,12 +76,13 @@ where
pub fn new(relir: &Reliability, db_name: &str) -> ReDb<K, V> {
let db = relir.create_database(Some(db_name)).unwrap();
ReDb {
- ignore: RefCell::new(false),
+ switch: RefCell::new(false),
db,
cache: RefCell::new(HashMap::new()),
add: RefCell::new(HashMap::new()),
del: RefCell::new(HashSet::new()),
name: String::from(db_name),
+ buf: RefCell::new(HashMap::new()),
//_phantom: PhantomData,
}
}
@@ -89,19 +96,26 @@ where
}
/// set the ignore flag of data
- pub fn set_ignore(&self, ignore: bool) {
- *self.ignore.borrow_mut() = ignore;
+ pub fn set_switch(&self, switch: bool) {
+ *self.switch.borrow_mut() = switch;
}
/// insert a entry
pub fn insert(&self, k: K, v: V) {
- if self.ignore() {
+ let switch = self.switch();
+ log::debug!(
+ "ReDb[{}] switch:{:?} insert, key:{:?}, value:{:?}.",
+ &self.name,
+ switch,
+ &k,
+ &v
+ );
+
+ if switch {
+ self.buf.borrow_mut().insert(k, v);
return;
}
- let n = &self.name;
- log::debug!("ReDb[{}] insert, key: {:?}, value: {:?}.", n, &k, &v);
-
// remove "del" + insert "add"
self.del.borrow_mut().remove(&k);
self.add.borrow_mut().insert(k.clone(), v.clone());
@@ -112,12 +126,15 @@ where
/// remove a entry
pub fn remove(&self, k: &K) {
- if self.ignore() {
+ let n = &self.name;
+ let switch = self.switch();
+ log::debug!("ReDb[{}] switch:{:?}, remove, key:{:?}.", n, switch, &k);
+
+ if switch {
+ self.buf.borrow_mut().remove(k);
return;
}
- log::debug!("ReDb[{}] remove, key: {:?}.", &self.name, &k);
-
// remove "add" + insert "del"
self.add.borrow_mut().remove(k);
self.del.borrow_mut().insert(k.clone());
@@ -136,6 +153,10 @@ where
/// get the existence of the key
pub fn contains_key(&self, k: &K) -> bool {
+ if self.switch() {
+ return self.buf.borrow().contains_key(k);
+ }
+
self.cache.borrow().contains_key(k)
}
@@ -178,6 +199,16 @@ where
self.del.borrow_mut().clear();
}
+ /// export all data from cache to database
+ pub fn rebuf_2_db(&self, wtxn: &mut ReDbRwTxn) {
+ // "buf" -> db.put + clear "buf"
+ for (k, v) in self.buf.borrow().iter() {
+ self.db.put(&mut wtxn.0, k, v).expect("history.put");
+ }
+
+ self.buf.borrow_mut().clear();
+ }
+
/// emport all data from database to cache
pub fn db_2_cache(&self, rtxn: &ReDbRoTxn)
where
@@ -197,8 +228,8 @@ where
}
}
- fn ignore(&self) -> bool {
- *self.ignore.borrow()
+ fn switch(&self) -> bool {
+ *self.switch.borrow()
}
}
@@ -228,10 +259,12 @@ pub trait ReDbTable {
fn clear(&self, wtxn: &mut ReDbRwTxn);
/// export all data to database
fn export(&self, wtxn: &mut ReDbRwTxn);
+ /// daemon-reload or daemon-reexec export all data to database
+ fn reexport(&self, wtxn: &mut ReDbRwTxn);
/// import all data from database
fn import(&self, rtxn: &ReDbRoTxn);
- /// set the ignore flag of data
- fn ignore_set(&self, ignore: bool);
+ /// set the switch flag of data, does switch control whether to use cache or buf
+ fn switch_set(&self, switch: bool);
}
const RELI_PATH_DIR: &str = "/run/sysmaster/reliability";
diff --git a/core/lib/rel/history.rs b/core/lib/rel/history.rs
index 2e2e03b..846f8c5 100644
--- a/core/lib/rel/history.rs
+++ b/core/lib/rel/history.rs
@@ -84,6 +84,21 @@ impl ReliHistory {
db_wtxn.0.commit().expect("history.commit");
}
+ /// daemon-reload or daemon-reexec clear db and data reflush to db
+ pub fn reflush(&self) {
+ // create transaction
+ let mut db_wtxn = ReDbRwTxn::new(&self.env).expect("history.write_txn");
+
+ // flush to db
+ for (_, db) in self.dbs.borrow().iter() {
+ db.clear(&mut db_wtxn);
+ db.reexport(&mut db_wtxn);
+ }
+
+ // commit
+ db_wtxn.0.commit().expect("history.commit");
+ }
+
pub fn import(&self) {
let db_rtxn = ReDbRoTxn::new(&self.env).expect("history.write_txn");
@@ -93,11 +108,11 @@ impl ReliHistory {
}
}
- pub fn ignore_set(&self, ignore: bool) {
- // set ignore
- *self.ignore.borrow_mut() = ignore;
+ pub fn switch_set(&self, switch: bool) {
+ // set switch
+ *self.ignore.borrow_mut() = switch;
for (_, db) in self.dbs.borrow().iter() {
- db.ignore_set(ignore);
+ db.switch_set(switch);
}
}
diff --git a/core/lib/rel/station.rs b/core/lib/rel/station.rs
index 10e1bed..b06be4a 100644
--- a/core/lib/rel/station.rs
+++ b/core/lib/rel/station.rs
@@ -90,6 +90,18 @@ impl ReliStation {
}
}
+ pub fn db_insert(&self) {
+ // level 1
+ for station in self.get_kind(ReStationKind::Level1).iter() {
+ station.db_insert();
+ }
+
+ // level 2
+ for station in self.get_kind(ReStationKind::Level2).iter() {
+ station.db_insert();
+ }
+ }
+
pub fn make_consistent(
&self,
lframe: Option<(u32, Option<u32>, Option<u32>)>,
--
2.33.0

View File

@ -1,399 +0,0 @@
From b2800ac267a76c19ce3229b031d809c4539ba5e7 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:10:42 +0800
Subject: [PATCH 4/9] fix: move RefCell to the inside of Table, no functional
change
---
core/bin/unit/data/dep_conf.rs | 2 +-
core/bin/unit/data/state.rs | 2 +-
core/bin/unit/data/table.rs | 49 +++++++++-------------
core/bin/unit/datastore/sets.rs | 2 +-
core/bin/unit/entry/ratelimit.rs | 2 +-
core/bin/utils/table.rs | 71 +++++++++++++++++---------------
6 files changed, 60 insertions(+), 68 deletions(-)
diff --git a/core/bin/unit/data/dep_conf.rs b/core/bin/unit/data/dep_conf.rs
index 9c203c5..e61ce5b 100644
--- a/core/bin/unit/data/dep_conf.rs
+++ b/core/bin/unit/data/dep_conf.rs
@@ -13,7 +13,7 @@
use std::collections::HashMap;
use sysmaster::unit::UnitRelations;
-#[derive(Default)]
+#[derive(Default, Clone)]
pub struct UnitDepConf {
pub deps: HashMap<UnitRelations, Vec<String>>,
}
diff --git a/core/bin/unit/data/state.rs b/core/bin/unit/data/state.rs
index fcdd51e..68e8607 100644
--- a/core/bin/unit/data/state.rs
+++ b/core/bin/unit/data/state.rs
@@ -12,7 +12,7 @@
use sysmaster::unit::{UnitActiveState, UnitNotifyFlags};
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub(crate) struct UnitState {
pub(crate) os: UnitActiveState,
pub(crate) ns: UnitActiveState,
diff --git a/core/bin/unit/data/table.rs b/core/bin/unit/data/table.rs
index 480a026..161ac5f 100644
--- a/core/bin/unit/data/table.rs
+++ b/core/bin/unit/data/table.rs
@@ -15,17 +15,16 @@ use super::state::UnitState;
use crate::job::JobResult;
use crate::unit::entry::StartLimitResult;
use crate::utils::table::{Table, TableSubscribe};
-use std::cell::RefCell;
use std::rc::Rc;
use sysmaster::rel::ReStation;
#[allow(clippy::type_complexity)]
pub struct DataManager {
tables: (
- RefCell<Table<String, UnitDepConf>>, // [0]unit-dep-config
- RefCell<Table<String, UnitState>>, // [1]unit-state
- RefCell<Table<String, StartLimitResult>>, // [2]unit-start-limit-hit
- RefCell<Table<String, JobResult>>, // [3] unit-job-timeout
+ Table<String, UnitDepConf>, // [0]unit-dep-config
+ Table<String, UnitState>, // [1]unit-state
+ Table<String, StartLimitResult>, // [2]unit-start-limit-hit
+ Table<String, JobResult>, // [3] unit-job-timeout
),
}
@@ -35,8 +34,8 @@ impl ReStation for DataManager {
// reload
fn entry_clear(&self) {
- self.tables.0.borrow_mut().data_clear();
- self.tables.1.borrow_mut().data_clear();
+ self.tables.0.data_clear();
+ self.tables.1.data_clear();
}
}
@@ -51,12 +50,7 @@ impl Drop for DataManager {
impl DataManager {
pub fn new() -> DataManager {
DataManager {
- tables: (
- RefCell::new(Table::new()),
- RefCell::new(Table::new()),
- RefCell::new(Table::new()),
- RefCell::new(Table::new()),
- ),
+ tables: (Table::new(), Table::new(), Table::new(), Table::new()),
}
}
@@ -66,7 +60,7 @@ impl DataManager {
ud_config: UnitDepConf,
) -> Option<UnitDepConf> {
{
- let old = self.tables.0.borrow_mut().insert(u_name, ud_config);
+ let old = self.tables.0.insert(u_name, ud_config);
old
}
}
@@ -76,8 +70,7 @@ impl DataManager {
name: &str,
subscriber: Rc<dyn TableSubscribe<String, UnitDepConf>>,
) -> Option<Rc<dyn TableSubscribe<String, UnitDepConf>>> {
- let mut table = self.tables.0.borrow_mut();
- table.subscribe(name.to_string(), subscriber)
+ self.tables.0.subscribe(name.to_string(), subscriber)
}
pub(crate) fn insert_unit_state(
@@ -85,8 +78,7 @@ impl DataManager {
u_name: String,
u_state: UnitState,
) -> Option<UnitState> {
- let mut table = self.tables.1.borrow_mut();
- table.insert(u_name, u_state)
+ self.tables.1.insert(u_name, u_state)
}
pub(crate) fn register_unit_state(
@@ -94,8 +86,7 @@ impl DataManager {
name: &str,
subscriber: Rc<dyn TableSubscribe<String, UnitState>>,
) -> Option<Rc<dyn TableSubscribe<String, UnitState>>> {
- let mut table = self.tables.1.borrow_mut();
- table.subscribe(name.to_string(), subscriber)
+ self.tables.1.subscribe(name.to_string(), subscriber)
}
pub(crate) fn insert_start_limit_result(
@@ -103,8 +94,7 @@ impl DataManager {
u_name: String,
start_limit_res: StartLimitResult,
) -> Option<StartLimitResult> {
- let mut table = self.tables.2.borrow_mut();
- table.insert(u_name, start_limit_res)
+ self.tables.2.insert(u_name, start_limit_res)
}
pub(crate) fn register_start_limit_result(
@@ -112,8 +102,7 @@ impl DataManager {
name: &str,
subscriber: Rc<dyn TableSubscribe<String, StartLimitResult>>,
) -> Option<Rc<dyn TableSubscribe<String, StartLimitResult>>> {
- let mut table = self.tables.2.borrow_mut();
- table.subscribe(name.to_string(), subscriber)
+ self.tables.2.subscribe(name.to_string(), subscriber)
}
pub(crate) fn insert_job_result(
@@ -121,8 +110,7 @@ impl DataManager {
u_name: String,
job_result: JobResult,
) -> Option<JobResult> {
- let mut table = self.tables.3.borrow_mut();
- table.insert(u_name, job_result)
+ self.tables.3.insert(u_name, job_result)
}
pub(crate) fn register_job_result(
@@ -130,19 +118,20 @@ impl DataManager {
name: &str,
subscriber: Rc<dyn TableSubscribe<String, JobResult>>,
) -> Option<Rc<dyn TableSubscribe<String, JobResult>>> {
- let mut table = self.tables.3.borrow_mut();
- table.subscribe(name.to_string(), subscriber)
+ self.tables.3.subscribe(name.to_string(), subscriber)
}
// repeating protection
pub(crate) fn clear(&self) {
- self.tables.0.borrow_mut().clear();
- self.tables.1.borrow_mut().clear();
+ self.tables.0.clear();
+ self.tables.1.clear();
}
}
#[cfg(test)]
mod tests {
+ use std::cell::RefCell;
+
use super::*;
use crate::unit::UnitRelations;
use crate::utils::table::TableOp;
diff --git a/core/bin/unit/datastore/sets.rs b/core/bin/unit/datastore/sets.rs
index 677d21b..538df8f 100644
--- a/core/bin/unit/datastore/sets.rs
+++ b/core/bin/unit/datastore/sets.rs
@@ -54,7 +54,7 @@ impl UnitSets {
}
pub(super) fn get(&self, name: &str) -> Option<Rc<UnitX>> {
- self.t.borrow().get(&name.to_string()).cloned()
+ self.t.borrow().get(&name.to_string())
}
pub(super) fn get_all(&self) -> Vec<Rc<UnitX>> {
diff --git a/core/bin/unit/entry/ratelimit.rs b/core/bin/unit/entry/ratelimit.rs
index 2f2a446..2c67232 100644
--- a/core/bin/unit/entry/ratelimit.rs
+++ b/core/bin/unit/entry/ratelimit.rs
@@ -17,7 +17,7 @@ pub(super) struct StartLimit {
start_limit: RefCell<RateLimit>,
}
-#[derive(PartialEq, Eq)]
+#[derive(PartialEq, Eq, Clone)]
pub(crate) enum StartLimitResult {
StartLimitNotHit,
StartLimitHit,
diff --git a/core/bin/utils/table.rs b/core/bin/utils/table.rs
index 90846cc..e1768d2 100644
--- a/core/bin/utils/table.rs
+++ b/core/bin/utils/table.rs
@@ -10,6 +10,7 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
+use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
use std::rc::Rc;
@@ -30,47 +31,45 @@ pub trait TableSubscribe<K, V> {
//#[derive(Debug)]
pub struct Table<K, V> {
- data: HashMap<K, V>, // key + value
- subscribers: HashMap<String, Rc<dyn TableSubscribe<K, V>>>, // key: name, value: subscriber
+ data: RefCell<HashMap<K, V>>, // key + value
+ subscribers: RefCell<HashMap<String, Rc<dyn TableSubscribe<K, V>>>>, // key: name, value: subscriber
}
impl<K, V> Table<K, V>
where
K: Eq + Hash + Clone,
+ V: Clone,
{
pub fn new() -> Table<K, V> {
Table {
- data: HashMap::new(),
- subscribers: HashMap::new(),
+ data: RefCell::new(HashMap::new()),
+ subscribers: RefCell::new(HashMap::new()),
}
}
- pub fn data_clear(&mut self) {
+ pub fn data_clear(&self) {
// clear all data without notifying subscribers
- self.data.clear();
+ self.data.borrow_mut().clear();
}
- pub fn clear(&mut self) {
+ pub fn clear(&self) {
// clear all, including data and subscribers
self.data_clear();
- self.subscribers.clear();
+ self.subscribers.borrow_mut().clear();
}
- pub fn insert(&mut self, k: K, v: V) -> Option<V> {
+ pub fn insert(&self, k: K, v: V) -> Option<V> {
let key = k.clone();
- let ret = self.data.insert(k, v);
- let value = self
- .data
- .get(&key)
- .expect("something inserted is not found.");
- let op = TableOp::TableInsert(&key, value);
+ let ret = self.data.borrow_mut().insert(k, v);
+ let value = self.get(&key).expect("something inserted is not found.");
+ let op = TableOp::TableInsert(&key, &value);
self.notify(&op);
ret
}
#[allow(dead_code)]
- pub fn remove(&mut self, k: &K) -> Option<V> {
- let ret = self.data.remove(k);
+ pub fn remove(&self, k: &K) -> Option<V> {
+ let ret = self.data.borrow_mut().remove(k);
if let Some(v) = &ret {
let op = TableOp::TableRemove(k, v);
self.notify(&op);
@@ -78,29 +77,33 @@ where
ret
}
- pub fn get(&self, k: &K) -> Option<&V> {
- self.data.get(k)
+ pub fn get(&self, k: &K) -> Option<V> {
+ self.data.borrow().get(k).cloned()
}
- pub fn get_all(&self) -> Vec<&V> {
- self.data.values().collect::<Vec<_>>()
+ pub fn get_all(&self) -> Vec<V> {
+ self.data
+ .borrow()
+ .values()
+ .map(|v| v.clone())
+ .collect::<Vec<V>>()
}
pub fn subscribe(
- &mut self,
+ &self,
name: String,
subscriber: Rc<dyn TableSubscribe<K, V>>,
) -> Option<Rc<dyn TableSubscribe<K, V>>> {
- self.subscribers.insert(name, subscriber)
+ self.subscribers.borrow_mut().insert(name, subscriber)
}
#[allow(dead_code)]
- pub fn unsubscribe(&mut self, name: &str) -> Option<Rc<dyn TableSubscribe<K, V>>> {
- self.subscribers.remove(name)
+ pub fn unsubscribe(&self, name: &str) -> Option<Rc<dyn TableSubscribe<K, V>>> {
+ self.subscribers.borrow_mut().remove(name)
}
fn notify(&self, op: &TableOp<'_, K, V>) {
- for (_, subscriber) in self.subscribers.iter() {
+ for (_, subscriber) in self.subscribers.borrow().iter() {
if subscriber.filter(op) {
subscriber.notify(op);
}
@@ -115,7 +118,7 @@ mod tests {
#[test]
fn table_insert() {
- let mut table: Table<u32, char> = Table::new();
+ let table: Table<u32, char> = Table::new();
let old = table.insert(1, 'a');
assert_eq!(old, None);
@@ -129,7 +132,7 @@ mod tests {
#[test]
fn table_remove() {
- let mut table: Table<u32, char> = Table::new();
+ let table: Table<u32, char> = Table::new();
let old = table.remove(&1);
assert_eq!(old, None);
@@ -148,25 +151,25 @@ mod tests {
#[test]
fn table_get() {
- let mut table: Table<u32, char> = Table::new();
+ let table: Table<u32, char> = Table::new();
let value = table.get(&1);
assert_eq!(value, None);
table.insert(1, 'a');
let value = table.get(&1);
- assert_eq!(value.cloned(), Some('a'));
+ assert_eq!(value, Some('a'));
let value = table.get(&2);
assert_eq!(value, None);
table.insert(2, 'b');
let value = table.get(&2);
- assert_eq!(value.cloned(), Some('b'));
+ assert_eq!(value, Some('b'));
}
#[test]
fn table_subscribe() {
- let mut table: Table<u32, char> = Table::new();
+ let table: Table<u32, char> = Table::new();
let sub_test1 = Rc::new(TableTest::new());
let sub_test2 = Rc::new(TableTest::new());
@@ -185,7 +188,7 @@ mod tests {
#[test]
fn table_unsubscribe() {
- let mut table: Table<u32, char> = Table::new();
+ let table: Table<u32, char> = Table::new();
let sub_test1 = Rc::new(TableTest::new());
let sub_test2 = Rc::new(TableTest::new());
@@ -209,7 +212,7 @@ mod tests {
#[test]
fn table_notify() {
- let mut table: Table<u32, char> = Table::new();
+ let table: Table<u32, char> = Table::new();
let sub_test1 = Rc::new(TableTest::new());
let sub_test2 = Rc::new(TableTest::new());
--
2.30.2

View File

@ -1,52 +0,0 @@
From 8e872af3529d75a05fad98824786415045c8cf10 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 15:57:55 +0800
Subject: [PATCH] fix: only remove one unit's pid from db PR #568 removes all
unit's pids when one unit enters dead state, which is definitely wrong. We
should only remove one unit's pid from db, and other unit's pids should still
be watched.
---
core/bin/unit/datastore/child.rs | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/core/bin/unit/datastore/child.rs b/core/bin/unit/datastore/child.rs
index 60c73c5..f4460f0 100644
--- a/core/bin/unit/datastore/child.rs
+++ b/core/bin/unit/datastore/child.rs
@@ -59,7 +59,7 @@ impl UnitChild {
}
pub(super) fn add_watch_pid(&self, id: &str, pid: Pid) {
- log::debug!("borrow add watch_pids for pid:{}, id:{}", pid, id);
+ log::debug!("Adding watch_pids {} to {}", pid, id);
let unit = self.units.get(id).unwrap();
self.data.add_watch_pid(unit.clone(), pid);
unit.child_add_pids(pid);
@@ -67,17 +67,19 @@ impl UnitChild {
pub(super) fn unwatch_pid(&self, id: &str, pid: Pid) {
let unit = self.units.get(id).unwrap();
- log::debug!("borrow remove watch_pids for {}", pid);
+ log::debug!("Removing watch_pids {} from {}", pid, id);
self.data.unwatch_pid(unit.clone(), pid);
unit.child_remove_pids(pid);
}
pub(super) fn unwatch_all_pids(&self, id: &str) {
+ log::debug!("Unwatching all watch_pids of {}", id);
let unit = self.units.get(id).unwrap();
- for i in self.data.watch_pids.borrow().keys() {
- unit.child_remove_pids(*i);
+ let delete_pids = unit.get_pids();
+ for pid in delete_pids {
+ unit.child_remove_pids(pid);
+ self.data.unwatch_pid(unit.clone(), pid);
}
- self.data.watch_pids.borrow_mut().clear();
}
pub(super) fn get_unit_by_pid(&self, pid: Pid) -> Option<Rc<UnitX>> {
--
2.30.2

View File

@ -1,79 +0,0 @@
From a9134b19fc0c942f842a7b51a956347951c89f8b Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 14 Jun 2023 17:24:08 +0800
Subject: [PATCH] fix: really use usec
watchdog_usec was actually watchdog_sec formerly
---
coms/service/src/mng.rs | 14 +++++++-------
coms/service/src/spawn.rs | 2 +-
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/coms/service/src/mng.rs b/coms/service/src/mng.rs
index 5c71531..bfa2c9f 100644
--- a/coms/service/src/mng.rs
+++ b/coms/service/src/mng.rs
@@ -1233,9 +1233,9 @@ impl ServiceMng {
}
fn restart_watchdog(&self) {
- self.monitor
- .borrow_mut()
- .set_original_watchdog(self.config.config_data().borrow().Service.WatchdogSec);
+ self.monitor.borrow_mut().set_original_watchdog(
+ self.config.config_data().borrow().Service.WatchdogSec * 1000000,
+ );
let watchdog_usec = self.monitor.borrow().watchdog_usec();
if watchdog_usec == 0 || watchdog_usec == u64::MAX {
self.stop_watchdog();
@@ -2455,7 +2455,7 @@ impl Source for ServiceTimer {
struct ServiceMonitorData {
mng: RefCell<Weak<ServiceMng>>,
// owned objects
- time: RefCell<u64>,
+ time: RefCell<u64>, /* usec */
}
// the declaration "pub(self)" is for identification only.
@@ -2511,7 +2511,7 @@ impl Source for ServiceMonitorData {
}
fn time_relative(&self) -> u64 {
- *self.time.borrow() * 1000000
+ *self.time.borrow()
}
fn dispatch(&self, _: &Events) -> i32 {
@@ -2575,7 +2575,7 @@ mod tests {
assert!(rt.armd_watchdog());
assert_eq!(
rt.watchdog().time(),
- config.config_data().borrow().Service.WatchdogSec
+ config.config_data().borrow().Service.WatchdogSec * 1000000
);
}
@@ -2594,7 +2594,7 @@ mod tests {
assert!(rt.armd_watchdog());
assert_eq!(
rt.watchdog().time(),
- config.config_data().borrow().Service.WatchdogSec
+ config.config_data().borrow().Service.WatchdogSec * 1000000
);
messages.remove("WATCHDOG");
diff --git a/coms/service/src/spawn.rs b/coms/service/src/spawn.rs
index 9b895aa..2ff9e92 100644
--- a/coms/service/src/spawn.rs
+++ b/coms/service/src/spawn.rs
@@ -164,6 +164,6 @@ impl ServiceSpawn {
}
fn watchdog_timer(&self) -> u64 {
- self.config.config_data().borrow().Service.WatchdogSec
+ self.config.config_data().borrow().Service.WatchdogSec * 1000000
}
}
--
2.30.2

View File

@ -1,117 +0,0 @@
From aedd0986761b7310321c31496917e93ae47a0d4b Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Wed, 31 May 2023 01:15:09 +0800
Subject: [PATCH] fix: recycle the zombie of sysmaster
---
exts/init/src/runtime/mod.rs | 18 ++++++------------
exts/init/src/runtime/signals.rs | 10 ++--------
exts/sctl/src/main.rs | 2 +-
3 files changed, 9 insertions(+), 21 deletions(-)
diff --git a/exts/init/src/runtime/mod.rs b/exts/init/src/runtime/mod.rs
index 11c7a8d..e37ff01 100644
--- a/exts/init/src/runtime/mod.rs
+++ b/exts/init/src/runtime/mod.rs
@@ -18,7 +18,6 @@ mod timer;
use comm::{Comm, CommType};
use epoll::Epoll;
-use libc::signalfd_siginfo;
use nix::errno::Errno;
use nix::libc;
use nix::sys::epoll::EpollEvent;
@@ -190,7 +189,7 @@ impl RunTime {
if let Some(siginfo) = self.signals.read(event)? {
let signo = siginfo.ssi_signo as i32;
match signo {
- _x if self.signals.is_zombie(signo) => self.do_recycle(siginfo),
+ _x if self.signals.is_zombie(signo) => self.do_recycle(),
_x if self.signals.is_restart(signo) => self.do_reexec(),
_x if self.signals.is_unrecover(signo) => self.change_to_unrecover(),
_ => {}
@@ -203,7 +202,7 @@ impl RunTime {
if let Some(siginfo) = self.signals.read(event)? {
let signo = siginfo.ssi_signo as i32;
match signo {
- _x if self.signals.is_zombie(signo) => self.do_recycle(siginfo),
+ _x if self.signals.is_zombie(signo) => self.do_recycle(),
_x if self.signals.is_restart(signo) => self.do_reexec(),
_x if self.signals.is_switch_root(signo) => self.send_switch_root_signal(),
_ => {}
@@ -217,10 +216,9 @@ impl RunTime {
let signo = siginfo.ssi_signo as i32;
match signo {
_x if self.signals.is_zombie(signo) => {
+ self.signals.recycle_zombie();
if self.is_sysmaster(siginfo.ssi_pid as i32) && self.switching {
self.reexec_self()
- } else {
- self.signals.recycle_zombie(Pid::from_raw(0))
}
}
_x if self.signals.is_restart(signo) => self.do_recreate(),
@@ -233,8 +231,7 @@ impl RunTime {
fn change_to_unrecover(&mut self) {
println!("change run state to unrecover");
self.state = InitState::Unrecover;
- // Attempt to recycle the zombie sysmaster.
- self.signals.recycle_zombie(Pid::from_raw(0));
+ self.signals.recycle_zombie();
}
fn do_reexec(&mut self) {
@@ -250,11 +247,8 @@ impl RunTime {
}
}
- fn do_recycle(&mut self, siginfo: signalfd_siginfo) {
- let pid = siginfo.ssi_pid as i32;
- if !self.is_sysmaster(pid) {
- self.signals.recycle_zombie(Pid::from_raw(pid));
- }
+ fn do_recycle(&mut self) {
+ self.signals.recycle_zombie();
}
fn create_sysmaster(&mut self) -> Result<(), Errno> {
diff --git a/exts/init/src/runtime/signals.rs b/exts/init/src/runtime/signals.rs
index a30c7c2..eee088c 100644
--- a/exts/init/src/runtime/signals.rs
+++ b/exts/init/src/runtime/signals.rs
@@ -148,17 +148,11 @@ impl Signals {
}
}
- pub fn recycle_zombie(&mut self, dest_pid: unistd::Pid) {
+ pub fn recycle_zombie(&mut self) {
// peek signal
let flags = WaitPidFlag::WEXITED | WaitPidFlag::WNOHANG | WaitPidFlag::WNOWAIT;
loop {
- // get wait information
- let mut id_flag = Id::All;
- if dest_pid.as_raw() > 0 {
- id_flag = Id::Pid(dest_pid);
- }
-
- let wait_status = match wait::waitid(id_flag, flags) {
+ let wait_status = match wait::waitid(Id::All, flags) {
Ok(status) => status,
Err(_) => return,
};
diff --git a/exts/sctl/src/main.rs b/exts/sctl/src/main.rs
index f799629..ef360c7 100644
--- a/exts/sctl/src/main.rs
+++ b/exts/sctl/src/main.rs
@@ -203,7 +203,7 @@ fn main() {
let stream = match TcpStream::connect(&addrs[..]) {
Err(e) => {
eprintln!("Failed to connect to sysmaster: {}", e);
- exit(e.raw_os_error().unwrap() as i32);
+ exit(e.raw_os_error().unwrap());
}
Ok(v) => v,
};
--
2.33.0

View File

@ -1,78 +0,0 @@
From c7600975afe9a2b13d33c10cc081e5de4b0d8270 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 10:51:33 +0800
Subject: [PATCH 1/9] fix: remove some debug message, no functional change
---
coms/socket/src/mng.rs | 2 --
core/bin/unit/notify.rs | 2 --
libs/event/tests/inotify.rs | 1 -
libs/event/tests/io.rs | 1 -
libs/event/tests/signal.rs | 1 -
5 files changed, 7 deletions(-)
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index e7a6a01..33b9812 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -848,8 +848,6 @@ impl Source for SocketMngPort {
}
fn dispatch(&self, _: &Events) -> i32 {
- println!("Dispatching IO!");
-
self.reli().set_last_frame2(
ReliLastFrame::SubManager as u32,
UnitType::UnitSocket as u32,
diff --git a/core/bin/unit/notify.rs b/core/bin/unit/notify.rs
index cb05330..f824725 100644
--- a/core/bin/unit/notify.rs
+++ b/core/bin/unit/notify.rs
@@ -332,8 +332,6 @@ impl Source for Notify {
}
fn dispatch(&self, _e: &Events) -> i32 {
- log::debug!("begin to dispatch notify event");
-
self.reli.set_last_frame1(ReliLastFrame::Notify as u32);
let ret = self.notify_dispatch();
self.reli.clear_last_frame();
diff --git a/libs/event/tests/inotify.rs b/libs/event/tests/inotify.rs
index 5fb3f5f..e9bdf72 100644
--- a/libs/event/tests/inotify.rs
+++ b/libs/event/tests/inotify.rs
@@ -49,7 +49,6 @@ mod test {
}
fn dispatch(&self, e: &Events) -> i32 {
- println!("Dispatching inotify!");
e.set_exit();
0
}
diff --git a/libs/event/tests/io.rs b/libs/event/tests/io.rs
index 292ed16..be79494 100644
--- a/libs/event/tests/io.rs
+++ b/libs/event/tests/io.rs
@@ -54,7 +54,6 @@ mod test {
}
fn dispatch(&self, _: &Events) -> i32 {
- println!("Dispatching IO!");
0
}
diff --git a/libs/event/tests/signal.rs b/libs/event/tests/signal.rs
index 3a54812..961bd5a 100644
--- a/libs/event/tests/signal.rs
+++ b/libs/event/tests/signal.rs
@@ -47,7 +47,6 @@ impl Source for Signals {
}
fn dispatch(&self, e: &Events) -> i32 {
- println!("Dispatching signal!");
match e.read_signals() {
Ok(Some(info)) => {
println!("read signo: {:?}", info.ssi_signo);
--
2.30.2

View File

@ -1,145 +0,0 @@
From c38d3c9dd677b28cbdd638b853475f74c42936bf Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Mon, 22 May 2023 11:56:18 +0800
Subject: [PATCH] fix: unwatch all pids when the service enters dead
---
coms/service/src/mng.rs | 10 ++++++++++
coms/service/src/pid.rs | 6 ++++++
core/bin/unit/datastore/child.rs | 18 ++++++++++++------
core/bin/unit/datastore/mod.rs | 4 ++++
core/bin/unit/manager.rs | 9 +++++++++
core/lib/unit/umif.rs | 3 +++
6 files changed, 44 insertions(+), 6 deletions(-)
diff --git a/coms/service/src/mng.rs b/coms/service/src/mng.rs
index dff610e..d73fa34 100644
--- a/coms/service/src/mng.rs
+++ b/coms/service/src/mng.rs
@@ -845,6 +845,16 @@ impl ServiceMng {
self.set_cmd_type(None);
}
+ if vec![
+ ServiceState::Dead,
+ ServiceState::Failed,
+ ServiceState::AutoRestart,
+ ]
+ .contains(&state)
+ {
+ self.pid.child_unwatch_all_pids();
+ }
+
// todo!()
// trigger the unit the dependency trigger_by
diff --git a/coms/service/src/pid.rs b/coms/service/src/pid.rs
index deba828..a25de50 100644
--- a/coms/service/src/pid.rs
+++ b/coms/service/src/pid.rs
@@ -94,6 +94,12 @@ impl ServicePid {
}
}
+ pub(super) fn child_unwatch_all_pids(&self) {
+ if let Some(u) = self.comm.owner() {
+ self.comm.um().child_unwatch_all_pids(u.id());
+ }
+ }
+
pub(super) fn main(&self) -> Option<Pid> {
self.data.borrow().main()
}
diff --git a/core/bin/unit/datastore/child.rs b/core/bin/unit/datastore/child.rs
index fd8fffa..af63792 100644
--- a/core/bin/unit/datastore/child.rs
+++ b/core/bin/unit/datastore/child.rs
@@ -57,17 +57,23 @@ impl UnitChild {
pub(super) fn add_watch_pid(&self, id: &str, pid: Pid) {
log::debug!("borrow add watch_pids for pid:{}, id:{}", pid, id);
let unit = self.units.get(id).unwrap();
- let u = Rc::clone(&unit);
- self.data.add_watch_pid(unit, pid);
- u.child_add_pids(pid);
+ self.data.add_watch_pid(unit.clone(), pid);
+ unit.child_add_pids(pid);
}
pub(super) fn unwatch_pid(&self, id: &str, pid: Pid) {
let unit = self.units.get(id).unwrap();
- let u = Rc::clone(&unit);
log::debug!("borrow remove watch_pids for {}", pid);
- self.data.unwatch_pid(unit, pid);
- u.child_remove_pids(pid);
+ self.data.unwatch_pid(unit.clone(), pid);
+ unit.child_remove_pids(pid);
+ }
+
+ pub(super) fn unwatch_all_pids(&self, id: &str) {
+ let unit = self.units.get(id).unwrap();
+ for i in self.data.watch_pids.borrow().keys() {
+ unit.child_remove_pids(*i);
+ }
+ self.data.watch_pids.borrow_mut().clear();
}
pub(super) fn get_unit_by_pid(&self, pid: Pid) -> Option<Rc<UnitX>> {
diff --git a/core/bin/unit/datastore/mod.rs b/core/bin/unit/datastore/mod.rs
index d3f8839..1dc09d7 100644
--- a/core/bin/unit/datastore/mod.rs
+++ b/core/bin/unit/datastore/mod.rs
@@ -160,6 +160,10 @@ impl UnitDb {
}
}
+ pub fn child_unwatch_all_pids(&self, id: &str) {
+ self.child.unwatch_all_pids(id);
+ }
+
pub fn get_unit_by_pid(&self, pid: Pid) -> Option<Rc<UnitX>> {
self.child.get_unit_by_pid(pid)
}
diff --git a/core/bin/unit/manager.rs b/core/bin/unit/manager.rs
index 0677c1b..bee3093 100644
--- a/core/bin/unit/manager.rs
+++ b/core/bin/unit/manager.rs
@@ -392,6 +392,10 @@ impl UmIf for UnitManager {
self.child_watch_all_pids(id)
}
+ fn child_unwatch_all_pids(&self, id: &str) {
+ self.child_unwatch_all_pids(id)
+ }
+
fn notify_socket(&self) -> Option<PathBuf> {
self.notify_socket()
}
@@ -480,6 +484,11 @@ impl UnitManager {
self.db.child_watch_all_pids(id)
}
+ /// remove all pids
+ fn child_unwatch_all_pids(&self, id: &str) {
+ self.db.child_unwatch_all_pids(id)
+ }
+
/// delete the pid from the db
fn child_unwatch_pid(&self, id: &str, pid: Pid) {
self.db.child_unwatch_pid(id, pid)
diff --git a/core/lib/unit/umif.rs b/core/lib/unit/umif.rs
index 027c59f..4821ff9 100644
--- a/core/lib/unit/umif.rs
+++ b/core/lib/unit/umif.rs
@@ -134,6 +134,9 @@ pub trait UmIf {
/// add all the pid of unit id, read pids from cgroup path.
fn child_watch_all_pids(&self, _id: &str) {}
+ /// remove all the pid of unit id
+ fn child_unwatch_all_pids(&self, _id: &str) {}
+
///rentry_trigger_merge
fn rentry_trigger_merge(&self, _unit_id: &str, _force: bool) {}
--
2.30.2

View File

@ -1,189 +0,0 @@
From 95210185215c933fa00be50c96505ef390df349c Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 10:57:10 +0800
Subject: [PATCH 2/9] refactor: rename UNitNotifyFlags to shorten code a bit
change UNIT_NOTIFY_SUCCESS to EMPTY, the original SUCCESS doesn't make sense
---
coms/mount/src/mng.rs | 2 +-
coms/service/src/mng.rs | 6 +++---
coms/socket/src/mng.rs | 2 +-
coms/target/src/mng.rs | 2 +-
core/bin/job/entry.rs | 2 +-
core/bin/job/manager.rs | 5 ++---
core/bin/unit/data/table.rs | 2 +-
core/bin/unit/entry/uentry.rs | 6 +-----
core/bin/unit/manager.rs | 1 -
core/lib/unit/state.rs | 8 ++++----
10 files changed, 15 insertions(+), 21 deletions(-)
diff --git a/coms/mount/src/mng.rs b/coms/mount/src/mng.rs
index e09d1f7..5383c4d 100644
--- a/coms/mount/src/mng.rs
+++ b/coms/mount/src/mng.rs
@@ -108,7 +108,7 @@ impl MountMng {
u.notify(
old_unit_state,
new_unit_state,
- UnitNotifyFlags::UNIT_NOTIFY_RELOAD_FAILURE,
+ UnitNotifyFlags::RELOAD_FAILURE,
)
}
diff --git a/coms/service/src/mng.rs b/coms/service/src/mng.rs
index 0f5db5f..fa6237a 100644
--- a/coms/service/src/mng.rs
+++ b/coms/service/src/mng.rs
@@ -862,14 +862,14 @@ impl ServiceMng {
let os = service_state_to_unit_state(self.config.service_type(), original_state);
let ns = service_state_to_unit_state(self.config.service_type(), state);
if let Some(u) = self.comm.owner() {
- let mut flags = UnitNotifyFlags::UNIT_NOTIFY_SUCCESS;
+ let mut flags = UnitNotifyFlags::EMPTY;
if self.rd.will_auto_restart() {
- flags |= UnitNotifyFlags::UNIT_NOTIFY_WILL_AUTO_RESTART;
+ flags |= UnitNotifyFlags::WILL_AUTO_RESTART;
}
if self.reload_result() != ServiceResult::Success {
- flags |= UnitNotifyFlags::UNIT_NOTIFY_RELOAD_FAILURE;
+ flags |= UnitNotifyFlags::RELOAD_FAILURE;
}
u.notify(os, ns, flags)
}
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index 33b9812..fe77517 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -650,7 +650,7 @@ impl SocketMng {
u.notify(
original_state.to_unit_active_state(),
state.to_unit_active_state(),
- UnitNotifyFlags::UNIT_NOTIFY_RELOAD_FAILURE,
+ UnitNotifyFlags::RELOAD_FAILURE,
)
}
}
diff --git a/coms/target/src/mng.rs b/coms/target/src/mng.rs
index 039fa9e..dc52339 100644
--- a/coms/target/src/mng.rs
+++ b/coms/target/src/mng.rs
@@ -112,7 +112,7 @@ impl TargetMng {
unit.notify(
old_unit_state,
new_unit_state,
- UnitNotifyFlags::UNIT_NOTIFY_RELOAD_FAILURE,
+ UnitNotifyFlags::RELOAD_FAILURE,
);
self.db_update();
diff --git a/core/bin/job/entry.rs b/core/bin/job/entry.rs
index 54af665..81f443a 100755
--- a/core/bin/job/entry.rs
+++ b/core/bin/job/entry.rs
@@ -621,7 +621,7 @@ fn job_process_unit_reload(
flags: UnitNotifyFlags,
) -> (Option<JobResult>, bool) {
let mut result = JobResult::Done;
- if flags.intersects(UnitNotifyFlags::UNIT_NOTIFY_RELOAD_FAILURE) {
+ if flags.intersects(UnitNotifyFlags::RELOAD_FAILURE) {
result = JobResult::Failed;
}
match ns {
diff --git a/core/bin/job/manager.rs b/core/bin/job/manager.rs
index e8eee8b..028c4d5 100755
--- a/core/bin/job/manager.rs
+++ b/core/bin/job/manager.rs
@@ -268,7 +268,6 @@ impl Source for JobManagerData {
fn dispatch(&self, _event: &Events) -> i32 {
log::debug!("job manager data dispatch");
-
self.reli.set_last_frame1(ReliLastFrame::JobManager as u32);
self.run(None);
self.reli.clear_last_frame();
@@ -728,7 +727,7 @@ impl JobManagerData {
) {
// OnFailure=
if ns != os
- && !flags.intersects(UnitNotifyFlags::UNIT_NOTIFY_WILL_AUTO_RESTART)
+ && !flags.intersects(UnitNotifyFlags::WILL_AUTO_RESTART)
&& ns == UnitActiveState::UnitFailed
{
let job_mode = unit
@@ -746,7 +745,7 @@ impl JobManagerData {
// OnSuccess=
if ns == UnitActiveState::UnitInActive
- && !flags.intersects(UnitNotifyFlags::UNIT_NOTIFY_WILL_AUTO_RESTART)
+ && !flags.intersects(UnitNotifyFlags::WILL_AUTO_RESTART)
{
match os {
UnitActiveState::UnitFailed
diff --git a/core/bin/unit/data/table.rs b/core/bin/unit/data/table.rs
index cfacf80..480a026 100644
--- a/core/bin/unit/data/table.rs
+++ b/core/bin/unit/data/table.rs
@@ -178,7 +178,7 @@ mod tests {
let dm = DataManager::new();
let os = UnitActiveState::UnitInActive;
let ns = UnitActiveState::UnitActive;
- let flags = UnitNotifyFlags::UNIT_NOTIFY_RELOAD_FAILURE;
+ let flags = UnitNotifyFlags::RELOAD_FAILURE;
let us_sub = Rc::new(UnitStatesTest::new(ns));
let old = dm.insert_unit_state(String::from("test"), UnitState::new(os, ns, flags));
diff --git a/core/bin/unit/entry/uentry.rs b/core/bin/unit/entry/uentry.rs
index a22ad10..307240b 100644
--- a/core/bin/unit/entry/uentry.rs
+++ b/core/bin/unit/entry/uentry.rs
@@ -694,11 +694,7 @@ impl Unit {
Ok(_) => Ok(()),
Err(e) => match e {
Error::UnitActionEOpNotSupp => {
- self.notify(
- active_state,
- active_state,
- UnitNotifyFlags::UNIT_NOTIFY_SUCCESS,
- );
+ self.notify(active_state, active_state, UnitNotifyFlags::EMPTY);
Ok(())
}
_ => Err(e),
diff --git a/core/bin/unit/manager.rs b/core/bin/unit/manager.rs
index 6e83240..d5c3e10 100644
--- a/core/bin/unit/manager.rs
+++ b/core/bin/unit/manager.rs
@@ -1051,7 +1051,6 @@ impl UnitManager {
if let Err(_e) = self.jm.try_finish(&unitx, state.os, state.ns, state.flags) {
// debug
}
-
let atom = UnitRelationAtom::UnitAtomTriggeredBy;
for other in self.db.dep_gets_atom(&unitx, atom) {
other.trigger(&unitx);
diff --git a/core/lib/unit/state.rs b/core/lib/unit/state.rs
index d5e204a..1ae747b 100644
--- a/core/lib/unit/state.rs
+++ b/core/lib/unit/state.rs
@@ -102,12 +102,12 @@ impl std::fmt::Display for UnitActiveState {
bitflags! {
/// notify unit state to manager
pub struct UnitNotifyFlags: u8 {
- /// the default flags propagate to jobs, it meas nothing.
- const UNIT_NOTIFY_SUCCESS = 0;
+ /// the default flags propagate to jobs, it means nothing.
+ const EMPTY = 0;
/// notify that the unit running reload failure
- const UNIT_NOTIFY_RELOAD_FAILURE = 1 << 0;
+ const RELOAD_FAILURE = 1 << 0;
/// notify that the unit is in auto restart state
- const UNIT_NOTIFY_WILL_AUTO_RESTART = 1 << 1;
+ const WILL_AUTO_RESTART = 1 << 1;
}
}
--
2.30.2

View File

@ -1,141 +0,0 @@
From 95ccf8bc7738d23844411a3770d95dd702cdabb1 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Thu, 1 Jun 2023 15:37:11 +0800
Subject: [PATCH] refactor: use UnixStream instead TcpListener to get the
message from sctl
---
core/bin/manager/commands.rs | 37 ++++++++++++++++++++++++++----------
exts/sctl/Cargo.toml | 1 +
exts/sctl/src/main.rs | 15 ++++-----------
libs/constants/src/lib.rs | 3 +++
4 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/core/bin/manager/commands.rs b/core/bin/manager/commands.rs
index d6ef907..1279206 100644
--- a/core/bin/manager/commands.rs
+++ b/core/bin/manager/commands.rs
@@ -13,32 +13,49 @@
use cmdproto::proto::execute::ExecuterAction;
use cmdproto::proto::ProstServerStream;
use event::{EventType, Events, Source};
-use std::net::{SocketAddr, TcpListener};
+use nix::sys::{socket, stat};
use std::os::unix::io::RawFd;
+use std::path::Path;
use std::{os::unix::prelude::AsRawFd, rc::Rc};
use sysmaster::rel::{ReliLastFrame, Reliability};
+use constants::SCTL_SOCKET;
+
pub(super) struct Commands<T> {
// associated objects
reli: Rc<Reliability>,
command_action: Rc<T>,
// owned objects
- fd: TcpListener,
+ socket_fd: i32,
}
impl<T> Commands<T> {
pub(super) fn new(relir: &Rc<Reliability>, comm_action: T) -> Self {
- let addrs = [
- SocketAddr::from(([127, 0, 0, 1], 9526)),
- SocketAddr::from(([127, 0, 0, 1], 9527)),
- ];
- let fd = TcpListener::bind(&addrs[..]).unwrap();
- fd.set_nonblocking(true).expect("set non-blocking");
+ let sctl_socket_path = Path::new(SCTL_SOCKET);
+ /* remove the old socket if it exists */
+ if sctl_socket_path.exists() && !sctl_socket_path.is_symlink() {
+ let _ = std::fs::remove_file(sctl_socket_path);
+ }
+ let sctl_socket_addr = socket::UnixAddr::new(Path::new(SCTL_SOCKET)).unwrap();
+ let socket_fd = socket::socket(
+ socket::AddressFamily::Unix,
+ socket::SockType::Stream,
+ socket::SockFlag::empty(),
+ None,
+ )
+ .unwrap();
+ /* create the socket with mode 666 */
+ let old_mask = stat::umask(stat::Mode::from_bits_truncate(!0o666));
+ let _ = socket::bind(socket_fd, &sctl_socket_addr);
+ /* restore our umask */
+ let _ = stat::umask(old_mask);
+ /* Allow at most 10 incomming connections can queue */
+ let _ = socket::listen(socket_fd, 10);
Commands {
reli: Rc::clone(relir),
command_action: Rc::new(comm_action),
- fd,
+ socket_fd,
}
}
}
@@ -83,7 +100,7 @@ where
}
fn fd(&self) -> RawFd {
- self.fd.as_raw_fd()
+ self.socket_fd.as_raw_fd()
}
fn priority(&self) -> i8 {
diff --git a/exts/sctl/Cargo.toml b/exts/sctl/Cargo.toml
index 775bc85..b0c0bfc 100644
--- a/exts/sctl/Cargo.toml
+++ b/exts/sctl/Cargo.toml
@@ -10,3 +10,4 @@ clap = { version = "3.1.8", features = ["derive"] }
cmdproto = { path = "../../libs/cmdproto" }
basic = { path = "../../libs/basic" }
nix = "0.24"
+constants = { version = "0.1.0", path = "../../libs/constants" }
diff --git a/exts/sctl/src/main.rs b/exts/sctl/src/main.rs
index ef360c7..c6d9281 100644
--- a/exts/sctl/src/main.rs
+++ b/exts/sctl/src/main.rs
@@ -17,11 +17,9 @@ use cmdproto::proto::{
abi::{sys_comm, unit_comm, CommandRequest},
mngr_comm, unit_file, ProstClientStream,
};
-use std::io::Write;
-use std::{
- net::{SocketAddr, TcpStream},
- process::exit,
-};
+use constants::SCTL_SOCKET;
+use std::process::exit;
+use std::{io::Write, os::unix::net::UnixStream};
/// parse program arguments
#[derive(Parser, Debug)]
@@ -195,12 +193,7 @@ fn main() {
Some(v) => v,
};
- let addrs = [
- SocketAddr::from(([127, 0, 0, 1], 9526)),
- SocketAddr::from(([127, 0, 0, 1], 9527)),
- ];
-
- let stream = match TcpStream::connect(&addrs[..]) {
+ let stream = match UnixStream::connect(SCTL_SOCKET) {
Err(e) => {
eprintln!("Failed to connect to sysmaster: {}", e);
exit(e.raw_os_error().unwrap());
diff --git a/libs/constants/src/lib.rs b/libs/constants/src/lib.rs
index bd3fba2..7c918aa 100644
--- a/libs/constants/src/lib.rs
+++ b/libs/constants/src/lib.rs
@@ -25,3 +25,6 @@ pub const SIG_SWITCH_ROOT_OFFSET: i32 = 10;
pub const INIT_SOCKET: &str = "/run/sysmaster/init";
/// sysmaster send this to init to keep alive
pub const ALIVE: &str = "ALIVE01234567890";
+
+/// Socket used to transfer message between sysmaster and sctl
+pub const SCTL_SOCKET: &str = "/run/sysmaster/sctl";
--
2.33.0

View File

@ -1,25 +0,0 @@
From e5368bb7b9c2681f802993f0225e832d5e647b70 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Fri, 2 Jun 2023 14:11:24 +0800
Subject: [PATCH] typo: fix some typoes, no functional change
---
core/bin/manager/commands.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/core/bin/manager/commands.rs b/core/bin/manager/commands.rs
index a8320e2..5eeb927 100644
--- a/core/bin/manager/commands.rs
+++ b/core/bin/manager/commands.rs
@@ -50,7 +50,7 @@ impl<T> Commands<T> {
let _ = socket::bind(socket_fd, &sctl_socket_addr);
/* restore our umask */
let _ = stat::umask(old_mask);
- /* Allow at most 10 incomming connections can queue */
+ /* Allow at most 10 incoming connections can queue */
let _ = socket::listen(socket_fd, 10);
Commands {
reli: Rc::clone(relir),
--
2.30.2

View File

@ -10,50 +10,14 @@
%global __cargo_common_opts %{?__cargo_common_opts} --all
Name: sysmaster
Version: 0.2.3
Release: 4
Version: 0.2.4
Release: 1
Summary: redesign and reimplement process1.
License: Mulan PSL v2
URL: https://gitee.com/openeuler/sysmaster
Source0: %{name}-%{version}.tar.gz
Patch6000: backport-fix-check-if-the-returned-state-is-None.patch
Patch6001: backport-fix-unwatch-all-pids-when-the-service-enters-dead.patch
Patch6002: backport-fix-only-remove-one-unit-s-pid-from-db-PR-568-remove.patch
# Patch6003-Patch6011: https://gitee.com/openeuler/sysmaster/pulls/585
Patch6003: backport-fix-remove-some-debug-message-no-functional-change.patch
Patch6004: backport-refactor-rename-UNitNotifyFlags-to-shorten-code-a-bi.patch
Patch6005: backport-feature-add-as_any-to-SubUnit-so-we-can-downcast-Sub.patch
Patch6006: backport-fix-move-RefCell-to-the-inside-of-Table-no-functiona.patch
Patch6007: backport-fix-add-has_source-to-events-and-use-this-in-socket..patch
Patch6008: backport-fix-change-if-port.fd-is-valid-we-should-avoid-deali.patch
Patch6009: backport-fix-enable-trigger-for-socket-and-use-this-interface.patch
Patch6010: backport-fix-change-the-socket-logic-to-keep-the-same-as-syst.patch
Patch6011: backport-fix-fix-some-cargo-clippies.patch
Patch6012: backport-fix-compatible-with-rustc-1.60.patch
Patch6013: backport-fix-compatible-with-rusc-1.60-lint-check.patch
Patch6014: backport-fix-recycle-the-zombie-of-sysmaster.patch
#Patch6015-Patch6017: https://gitee.com/openeuler/sysmaster/pulls/598
Patch6015: backport-refactor-use-UnixStream-instead-TcpListener-to-get-t.patch
Patch6016: backport-feature-get-credential-from-the-connection-and-use-i.patch
Patch6017: backport-typo-fix-some-typoes-no-functional-change.patch
Patch6018: backport-fix-fstab-use-fixed-inotify-version-0.10.0-to-be-com.patch
Patch6019: backport-fix-don-t-mount-all-subsystem-to-every-sys-fs-cgroup.patch
Patch6020: backport-fix-check-if-the-given-unit-name-is-valid-before-we-.patch
Patch6021: backport-fix-really-use-usec.patch
Patch6022: backport-docs-note-that-we-only-support-no-more-than-500-unit.patch
Patch6023: backport-fix-inconsistent-data-in-app-memory-and-db-after-dae.patch
Patch6024: backport-fix-.socket-stop-faild-when-.socket-is-in-listening.patch
Patch6025: backport-docs-Add-signal-processing-instructions-for-init.patch
Patch6026: backport-fix-support-for-adjusting-the-memory-usage-of-databa.patch
Patch6027: backport-fix-Clear-buffer-correctly-during-reload-or-reexec.patch
Patch6028: backport-fix-commands-connection-timeout-causing-event-blocki.patch
ExclusiveArch: x86_64 aarch64
BuildRequires: rust
@ -85,20 +49,10 @@ sed -i '/\[source.local-registry\]/a directory = "vendor"' ./.cargo/config
install -Dm0550 -t %{buildroot}/usr/bin %{sysmaster_install_source}/sctl
install -Dm0550 -t %{sysmaster_install_target} %{sysmaster_install_source}/init
install -Dm0550 -t %{sysmaster_install_target} %{sysmaster_install_source}/sysmaster
install -Dm0550 -t %{sysmaster_install_target} %{sysmaster_install_source}/fstab
install -Dm0550 -t %{sysmaster_install_target} %{sysmaster_install_source}/sysmonitor
install -Dm0550 -t %{sysmaster_install_target} %{sysmaster_install_source}/random_seed
install -Dm0550 -t %{sysmaster_install_target} %{sysmaster_install_source}/rc-local-generator
#install -Dm0640 -t %{sysmaster_install_target} %{unit_install_source}/basic.target
#install -Dm0640 -t %{sysmaster_install_target} %{unit_install_source}/Multi-user.target
#install -Dm0640 -t %{sysmaster_install_target} %{unit_install_source}/shutdown.target
#install -Dm0640 -t %{sysmaster_install_target} %{unit_install_source}/sysinit.target
install -Dm0640 -t %{sysmaster_install_target} %{unit_install_source}/*
install -Dm0550 -t %{sysmaster_install_target}/plugin %{sysmaster_install_source}/libmount.so
install -Dm0550 -t %{sysmaster_install_target}/plugin %{sysmaster_install_source}/libservice.so
install -Dm0550 -t %{sysmaster_install_target}/plugin %{sysmaster_install_source}/libsocket.so
install -Dm0550 -t %{sysmaster_install_target}/plugin %{sysmaster_install_source}/libtarget.so
install -Dm0640 -t %{sysmaster_install_target}/plugin %{conf_install_source}/plugin.conf
@ -107,6 +61,9 @@ install -Dm0640 -t %{sysmaster_install_target}/plugin %{conf_install_source}/plu
/usr/lib/sysmaster/*
%changelog
* Wed Jun 28 2023 shenyangyang<shenyangyang4@huawei.com> - 0.2.4-1
- update version to 0.2.4 for docker use
* Mon Jun 19 2023 huyubiao<huyubiao@huawei.com> - 0.2.3-4
- sync patches from upstream