Skip to content

Commit 7bde041

Browse files
val213val213MemoryShore1037827920fslongjin
authored
feat(net): 调试现有函数调用链条,完善部分设计,为下一步继续实现udev监听并关联sysfs节点做准备 (#941)
* add netlink todo directory * save the work 2 another system workplace * save the work 2 another system workplace * add the demo of netlink_create and locate the kobject_uevent\kobject_uevent_env * add the uevent directory and demo of kobject_uevent and kobject_uevent_env * delete sth else in target * handle existing errors, ready for the next period of dev * 阶段性提交,完成了kobjectuevent_env的部分逻辑,接下来需要实现uevent_ops结构体的转写 * 调整uevent_suppress和subsystem,修改分支日志打印信息 * 调整uevent_suppress和subsystem,修改分支日志打印信息 * 在kset中引入了KsetUeventOps的trait,待实现 * 阶段性提交, 基本完成了处理kset的uevent_ops相关逻辑,新增一个trait和一个结构体,正在逐步完善kobject_uevent_env * 阶段性提交:完善大部分kobject_uevent_env函数的功能 * add_uevent_var,kobject_uevent_net_broadcast,zap_modalias_env,完善其他细节的逻辑 * 阶段性提交,开始逐渐深入与netlink * 阶段性提交,重构netlinktable和netlinksocket * replace kdebug with log::info! * 理清了sock和ueventsock的关系,重构了一部分原来的代码 * 阶段性提交,新增了几个函数,进一步完善了netlink * 阶段性提交,新增了Sk_Buff封装了一层PacketBuffer,需要解决mc_list迭代器实现方案和PacketBuffer提供的接口不足的问题 * 初步解决mc_list迭代问题和consume_skb * fmt * 同步更改 * asy to antoher workplace * 初步解决现有代码中所有权和生命周期等问题 * fix nlk_sk and init test for uevent * 初步在driver中成功调用kobject_uevent()进行测试,标记需要发送uevent的地方 * ipml socket for netlinksock * 阶段性提交,完善netlink_proto相关内容 * 阶段性提交,初步实现netlink_add_usersock_entry等 * 实现netlink_proto_init() * add NetProtoFamily and NetlinkFamulyOps * 修复mprotect系统调用未正确设置vm_flags的错误 (#847) * fix(time): modify update wall time (#836) 更改了时间子系统的update_wall_time函数,通过读取当前周期数,计算delta值进行更新,而不是通过传入delta值进行更新 * chore: 调整triagebot.toml以适应新的组织架构 (#848) * netlink_insert大体框架,遇到nlk无法修改的问题 * doc: 完善README.md (#849) * doc: 完善README.md * chore: 更新sphinx相关配置,适应read the docs的更新 (#850) 根据read the docs在7月15日blog,进行此修改 https://about.readthedocs.com/blog/2024/07/addons-by-default/ * 完善netlink_insert 和 netlink_lookup * feat(driver/net): 实现Loopback网卡接口 (#845) * 初步实现loopback设备 * fix: build-scripts和tools目录下的make check指定工具链版本 (#861) * fix: tcp poll没有正确处理posix socket的listen状态的问题 (#859) * chore: 将工具链更新到2024-07-23 (#864) * chore: 将工具链更新到2024-07-23 * 考虑更换Sk_Buffer实现,留档原有方案 * feat(fs): add eventfd syscall support (#858) * feat(fs): add eventfd syscall support * 更换Sk_Buffer实现 * refactor: 删除过时的va-pa转换函数,改为统一使用MMArch (#862) * 需要解决Arc内部可变性问题 * 解决sk:Arc内部可变性问题 * 默认nightly-2024-07-23 & config改为config.toml (#872) * fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 (#870) * fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 * 基本实现netlink单播,todo回调函数 * remove unused import * 考虑是否引入回调函数,补充sockflags * updates test-uevent * add endpoint and complete to_endpoint function for NetLinkEndpoint * feat(cred): 初步实现Cred (#846) * 初步实现Cred * 添加seteuid和setegid * 添加cred测试程序 * 修改Cred::fscmp返回结果为CredFsCmp枚举 * 完善root用户相关信息 * fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题 (#877) * fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题 * fix: 解决ntty潜在的panic问题 * success to call NetlinkSock bind fn and add a fn into Socket trait * modify handle in NetlinkSock * fix af_netlink problems in the new branch reported by RA * updates test-uevent * add netlink_send adn netlink_recv * fix bind * fix sth * fix var type * 初步完成了netlink/uevent在用户空间的创建、绑定和读写测试,下一步是内核中kobject/kset相关的调用调试和uevent文件挂载到sysfs中的需求 * restore recvfrom parameter * add postcore init fn, todo: callback and clourse define * cfg使用闭包实现回调函数字段,修复部分nlk在动态类型分发侯进行类型转换失败的问题 * 调试现有函数调用链条,完善部分设计,为下一步继续实现udev监听并关联sysfs节点做准备 --------- Co-authored-by: val213 <[email protected]> Co-authored-by: MemoryShore <[email protected]> Co-authored-by: 黄铭涛 <[email protected]> Co-authored-by: LoGin <[email protected]> Co-authored-by: SMALLC <[email protected]> Co-authored-by: linfeng <[email protected]> Co-authored-by: Jomo <[email protected]>
1 parent e27c68c commit 7bde041

File tree

7 files changed

+445
-277
lines changed

7 files changed

+445
-277
lines changed

kernel/src/driver/base/kobject.rs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ bitflags! {
104104
const ADD_UEVENT_SENT = 1 << 1;
105105
const REMOVE_UEVENT_SENT = 1 << 2;
106106
const INITIALIZED = 1 << 3;
107+
const UEVENT_SUPPRESS = 1 << 4;
107108
}
108109
}
109110
#[derive(Debug)]
@@ -262,19 +263,36 @@ impl KObjectManager {
262263
}
263264

264265
fn get_kobj_path_length(kobj: &Arc<dyn KObject>) -> usize {
266+
log::info!("get_kobj_path_length() kobj:{:?}", kobj.name());
265267
let mut length = 1;
266268
let mut parent = kobj.parent().unwrap().upgrade().unwrap();
267269
/* walk up the ancestors until we hit the one pointing to the
268270
* root.
269271
* Add 1 to strlen for leading '/' of each level.
270272
*/
273+
let mut length = 0; // 确保 length 被正确初始化
274+
let mut iteration_count = 0; // 用于记录迭代次数
275+
const MAX_ITERATIONS: usize = 10; // 最大迭代次数
276+
271277
loop {
272-
if parent.name().is_empty() {
273-
break;
274-
}
278+
log::info!("Iteration {}: parent.name():{:?}", iteration_count, parent.name());
275279
length += parent.name().len() + 1;
276280
if let Some(weak_parent) = parent.parent() {
277-
parent = weak_parent.upgrade().unwrap();
281+
if let Some(upgraded_parent) = weak_parent.upgrade() {
282+
parent = upgraded_parent;
283+
} else {
284+
log::error!("Failed to upgrade weak reference to parent");
285+
break;
286+
}
287+
} else {
288+
log::error!("Parent has no parent");
289+
break;
290+
}
291+
292+
iteration_count += 1;
293+
if iteration_count >= MAX_ITERATIONS {
294+
log::error!("Reached maximum iteration count, breaking to avoid infinite loop");
295+
break;
278296
}
279297
}
280298
return length;
@@ -298,31 +316,45 @@ impl KObjectManager {
298316
kobj, __func__, path);
299317
}
300318
*/
301-
fn fill_kobj_path(kobj: &Arc<dyn KObject>, path: *mut u8, length: usize) {
319+
fn fill_kobj_path(kobj: &Arc<dyn KObject>, path: &mut [u8], length: usize) {
302320
let mut parent = kobj.parent().unwrap().upgrade().unwrap();
303321
let mut length = length;
304322
length -= 1;
305323
loop {
324+
log::info!("fill_kobj_path parent.name():{:?}", parent.name());
306325
let cur = parent.name().len();
326+
if length < cur + 1 {
327+
// 如果剩余长度不足以容纳当前名称和分隔符,则退出
328+
break;
329+
}
307330
length -= cur;
308-
unsafe {
309-
core::ptr::copy_nonoverlapping(parent.name().as_ptr(), path.add(length), cur);
310-
*path.add(length - 1) = b'/';
331+
let parent_name = parent.name();
332+
let name = parent_name.as_bytes();
333+
for i in 0..cur {
334+
path[length + i] = name[i];
311335
}
336+
length -= 1;
337+
path[length] = '/' as u8;
312338
if let Some(weak_parent) = parent.parent() {
313-
parent = weak_parent.upgrade().unwrap();
339+
if let Some(upgraded_parent) = weak_parent.upgrade() {
340+
parent = upgraded_parent;
341+
} else {
342+
break;
343+
}
344+
} else {
345+
break;
314346
}
315347
}
316348
}
317349
// TODO: 实现kobject_get_path
318350
// https://code.dragonos.org.cn/xref/linux-6.1.9/lib/kobject.c#139
319351
pub fn kobject_get_path(kobj: &Arc<dyn KObject>) -> String {
352+
log::debug!("kobject_get_path() kobj:{:?}", kobj.name());
320353
let length = Self::get_kobj_path_length(kobj);
321-
let path_raw = vec![0u8; length].into_boxed_slice();
322-
let path = Box::into_raw(path_raw) as *mut u8;
354+
let path:&mut [u8] = &mut vec![0; length];
323355
Self::fill_kobj_path(kobj, path, length);
324-
let path_string = unsafe { String::from_raw_parts(path, length, length) };
325-
path_string
356+
let path_string = String::from_utf8(path.to_vec()).unwrap();
357+
return path_string;
326358
}
327359
}
328360

0 commit comments

Comments
 (0)