From 8573f684dcca187bee246352d77c990f234878f6 Mon Sep 17 00:00:00 2001 From: Shinwell Hu Date: Fri, 26 Mar 2021 03:49:45 +0000 Subject: [PATCH 1/4] signal handling initial test --- Cargo.lock | 35 +++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++++ src/main.rs | 22 +++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b4712a6b..fbab3a40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,40 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "cc" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" + +[[package]] +name = "libc" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8916b1f6ca17130ec6568feccee27c156ad12037880833a3b842a823236502e7" + [[package]] name = "process1" version = "0.1.0" +dependencies = [ + "signal-hook", +] + +[[package]] +name = "signal-hook" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aa894ef3fade0ee7243422f4fbbd6c2b48e6de767e621d37ef65f2310f53cea" +dependencies = [ + "cc", + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +dependencies = [ + "libc", +] diff --git a/Cargo.toml b/Cargo.toml index 058d04a0..7ad78a13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Shinwell Hu "] edition = "2018" +license = "MulanPSL-2.0" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +signal-hook = { version = "0.3.7", features = ["extended-siginfo"] } + diff --git a/src/main.rs b/src/main.rs index e7a11a96..1734b7b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,23 @@ -fn main() { +use std::io::Error; + +use signal_hook::consts::signal::SIGCHLD; +use signal_hook::iterator::SignalsInfo; +use signal_hook::iterator::exfiltrator::WithOrigin; + +fn main() -> Result<(), Error> { + let sigs = vec![SIGCHLD]; + + let mut signals = SignalsInfo::::new(&sigs)?; + + for info in &mut signals { + println!("Received a signal {:?}", info); + #[cfg(feature="extended-siginfo")] + match info.signal { + SIGCHLD => { + println!("CHLD received"); + } + } + } println!("Hello, world!"); + Ok(()) } -- Gitee From 7f454c7444c83d296a648e70dfb154fbfce54608 Mon Sep 17 00:00:00 2001 From: Shinwell Hu Date: Sat, 27 Mar 2021 03:16:27 +0000 Subject: [PATCH 2/4] fix main.rs error --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 1734b7b0..126ba603 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ fn main() -> Result<(), Error> { for info in &mut signals { println!("Received a signal {:?}", info); - #[cfg(feature="extended-siginfo")] + #[cfg(feature = "extended-siginfo")] match info.signal { SIGCHLD => { println!("CHLD received"); -- Gitee From 0e6d52ff1459c055e5f47b49b52629d3b6bdef87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E5=BE=97=E6=84=9F=E6=83=85=E7=9A=84openEuler?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Mon, 29 Mar 2021 09:39:47 +0800 Subject: [PATCH 3/4] Update README for initial thoughts --- README.en.md | 37 ++---------------------- README.md | 79 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 59 deletions(-) diff --git a/README.en.md b/README.en.md index 640261e0..dde253e8 100644 --- a/README.en.md +++ b/README.en.md @@ -1,36 +1,5 @@ -# process1 +# Thoughts on a new Process 1 -#### Description -redesign and reimplement process1 +## Background -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +This is Chinese version only for now. \ No newline at end of file diff --git a/README.md b/README.md index a2f4690a..a30d77cb 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,66 @@ -# process1 +# Process 1 设计思考 -#### 介绍 -redesign and reimplement process1 +## 背景说明 -#### 软件架构 -软件架构说明 +在所有 Unix 系统中都有一个进程号为 1 的进程。这是操作系统内核完成启动后,执行的第一个用户态应用程序,所以进程号在数值上仅次于 idle 这个特殊进程。 +从功能定位上看,init 所负责的最基本功能有两类: -#### 安装教程 +1. 启停系统服务: 比如 init 负责在启动阶段拉起用户空间的各个守护进程,这个过程需要依赖文件系统挂载、设备发现、网络配置等活动。相应的,在系统关闭或者重启过程中,init 同样要负责关闭网络接口,数据落盘,资源清理,卸载文件系统等等。系统关闭过程的不完善,会影响下一次能否成功启动。 +2. 回收孤儿进程: 在 Unix 系统上,如果父进程比子进程更早退出,子进程成为孤儿的情况下,init 有回收孤儿进程运行状态的责任。 -1. xxxx -2. xxxx -3. xxxx +从用户的角度看,回收孤儿进程是个基本功能,这点如果没做到,系统会出现大量僵尸进程,影响系统稳定性。但更重要的还是对系统服务启停的管理。一个优秀的init进程,应该具备快速、可靠的特性。为达成快速的目的,有几个关键的手段: -#### 使用说明 +- 按需启动:启动时,只启动必须的服务,将其他事务延后到合适的时机(真正需要它的时候),比如更新、上报等服务等到系统空闲的时候再处理。 +- 更多的并行:保持足够高的并发度,最大化的利用CPU、内存、带宽资源,使系统快速行进至登陆界面,而不是白白浪费时间。 +- 保证每个启动脚本的高性能:避免不必要的fork和脚本。 -1. xxxx -2. xxxx -3. xxxx +## 主流init实现方式对比 -#### 参与贡献 +很多人一直努力地从某些方面改进传统的 init 守护进程,使它变得更完善。有简洁可靠但低效的sysvinit,有高效但略显复杂的systemd。 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +*todo* 增加对 Android init 的分析对比 +| Init软件 | 说明 | 启动管理 | 进程回收 | 服务管理 | 并行启动 | 设备管理 | 资源控制 | 日志管理 | +| -------- | ------------------------------------------------------------ | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| sysvinit | 早期版本使用的初始化进程工具, 逐渐淡出舞台。 | ✓ | ✓ | | | | | | +| upstart | debian, Ubuntu等系统使用的initdaemon | ✓ | ✓ | ✓ | ✓ | | | | +| systemd | 提高系统的启动速度,相比传统的System V是一大革新,已被大多数Linux发行版所使用。 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -#### 特技 +## systemd 的问题分析 -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +systemd 目前已成为大多数发行版的标准配置。 +systemd是为了解决启动时间长,启动脚本复杂问题而诞生的。它的设计目标是,为系统的启动和管理提供一整套完整的解决方案。 + +从 openEuler 集成 systemd 的实践结果看,我们认为 systemd 虽然对用户使用感受比较友好,但对开发者来说,存在很多待改进的问题: + +1. 版本迭代快,每月近千提交,不提供成熟稳定的社区版本; +2. C语言编写,代码质量不高,提交中常有低级编程错误,bug多; +3. 社区的维护策略是强制大家使用最新的不稳定版本,maintainer 经常用 version too old 来应付求助和讨论; +以上三点使得 systemd 的维护成本非常高; + +4. 系统韧性不强。模块耦合严重,问题容易在模块间扩散,sd-event、sd-bus、unit等机制缠绕在一起,一旦出问题,就导致系统不可用或崩溃; +5. 集成100+的特性,并且与其它软件包功能有重合,在复杂场景中使用时,集成测试难度非常高; +6. 调试检测手段匮乏,经常满屏幕打印assert,反而容易造成关键信息被淹没; +7. 边界不清晰,部分功能和其他软件包重合,一旦出现问题,需要很多不同背景的人共同分析定位。对一些系统能力管的宽但管的不彻底,比如cgroup就是一个例证,配置项竟然另起一套; +以上四点使得 systemd 在新增特性新增场景时格外容易出错,而一旦出错就影响系统整体的可靠性; + +8. 向下兼容的工作量大,对于刚从传统 sysvinit 转过来的服务,有些行为显得莫名其妙; +9. 新人培养的门槛高,机制复杂,远不如 sysv init 那么简单直观; +以上两点使得我们的 legacy 业务向新系统的迁移变得没有必要的复杂。 + +总的来说,我们认为 systemd 的架构和社区运作定位,使得它不适应 云场景中业务快速开发迭代的诉求。云原生场景下,需要一个能够在 业务快速开发迭代中不使绊子、不拖后腿的 init 系统。 + +## 设计的目标和约束 + +**设计目标** + +- **皮实耐操**: 一个未充分测试的新特性,只会影响到他自身的稳定性,不会影响系统整体的稳定性。 +- **管理便捷**:可以方便的安装/部署/升级/运维 +- **足够轻量**:占用更少的资源,systemd 大约需要 100M 以上的安装空间。我们新的设计实现能够控制在 个位数。 +- **使用方便**:更灵活简洁的架构,易于理解、便于运维 +- **极致安全**:保持对进程的跟踪,提供最小的运行环境 + +## 架构设想 与 社区合作的 思路 + +**TODO** -- Gitee From 07fdd4e2798dc6c673c4089b98b93d4a8c9ac6a9 Mon Sep 17 00:00:00 2001 From: Shinwell Hu Date: Thu, 1 Apr 2021 08:46:59 +0000 Subject: [PATCH 4/4] think about different type of init --- src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main.rs b/src/main.rs index 126ba603..2a927300 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,44 @@ use std::io::Error; +use std::io::BufRead; +use std::fs::File; use signal_hook::consts::signal::SIGCHLD; use signal_hook::iterator::SignalsInfo; use signal_hook::iterator::exfiltrator::WithOrigin; +fn parse_inittab() -> Result<(), Error> { + let inittab_file = File::open("/etc/inittab")?; + let buf_reader = std::io::BufReader::new(inittab_file); + let lines = buf_reader.lines(); + + // actual read out configuration from /etc/inittab + for (num, line) in lines.enumerate() { + let l = line.unwrap(); + if l.chars().nth(0).unwrap() == '#' { + // skip + } else { + println!("{:?}", l); + } + } + //FIXME: + return Ok(()) +} + fn main() -> Result<(), Error> { + // parse system init configuration + // inittab is obsolated by systemd, should we honor the legacy config in that ? + // systemd use /etc/systemd/system/default.target, which is a sym link to actual target. + // fork process to do system initializaion + + match parse_inittab() { + Err(why) => panic!("{:?}", why), + Ok(config) => println!("Using /etc/inittab"), + }; + + // reacting on signls + // 1. collect SICHLD + // 2. react on service failure (restart on event) + // 3. handle system reboot let sigs = vec![SIGCHLD]; let mut signals = SignalsInfo::::new(&sigs)?; -- Gitee