Skip to content

Commit d7d75c0

Browse files
joannekoongosandov
authored andcommitted
drgn.helpers.linux.kernfs: add kernfs_children() helper
Add a helper, kernfs_children(), that returns an iterator over the children of a directory in kernfs. The implementation of this must check kn.flags to ensure it is a KERNFS_DIR. struct kernfs_node unions dir and attr and without this check, calling kernfs_children() on a KERNFS_FILE kernfs node may return corrupt data or trigger a page fault that is confusing to the user. Signed-off-by: Joanne Koong <[email protected]>
1 parent 9936a49 commit d7d75c0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

drgn/helpers/linux/kernfs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
import os
13+
from typing import Iterator, Optional
1314

1415
from drgn import NULL, Object, Path
1516
from drgn.helpers.linux.rbtree import rbtree_inorder_for_each_entry
@@ -20,6 +21,7 @@
2021
"kernfs_path",
2122
"kernfs_root",
2223
"kernfs_walk",
24+
"kernfs_children",
2325
)
2426

2527

@@ -110,3 +112,22 @@ def kernfs_walk(parent: Object, path: Path) -> Object:
110112
else:
111113
return NULL(parent.prog_, kernfs_nodep_type)
112114
return parent
115+
116+
117+
def kernfs_children(kn: Object) -> Optional[Iterator[Object]]:
118+
"""
119+
Get an iterator over the children of the given kernfs node if the node
120+
represents a directory.
121+
122+
:param kn: ``struct kernfs_node *``
123+
:return: Iterator of ``struct kernfs_node *`` objects.
124+
"""
125+
KERNFS_TYPE_MASK = 0x000F
126+
kernfs_dir = kn.prog_.constant("KERNFS_DIR")
127+
128+
if (kn.flags & KERNFS_TYPE_MASK) != kernfs_dir:
129+
raise ValueError("not a directory")
130+
131+
return rbtree_inorder_for_each_entry(
132+
"struct kernfs_node", kn.dir.children.address_of_(), "rb"
133+
)

0 commit comments

Comments
 (0)