contrib/find_struct_file.py: also look in binfmt_misc

This is a surprising place where file references can be hiding that I've
run into before. There are some beginnings of an lsof-like script here.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
This commit is contained in:
Omar Sandoval 2023-10-16 10:45:45 -07:00
parent 185a5bf66d
commit 54f5fa044f

View File

@ -2,22 +2,41 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later
"""Find what process is using a struct file *, given as an address."""
"""Print what is using a struct file *, given as an address."""
import os
import sys
from drgn import Object
from drgn.helpers.linux.fs import for_each_file
from drgn.helpers.linux.list import list_for_each_entry
from drgn.helpers.linux.pid import for_each_task
file = Object(prog, "struct file *", int(sys.argv[1], 0))
for task in for_each_task(prog):
for fd, file2 in for_each_file(task):
if file2 == file:
break
else:
continue
break
else:
print("Not found:(")
print(f"PID {task.pid.value_()} COMM {task.comm.string_().decode()} FD {fd}")
def find_struct_file_fds(file: Object) -> None:
for task in for_each_task(file.prog_):
for fd, fd_file in for_each_file(task):
if fd_file == file:
print(
f"PID {task.pid.value_()} COMM {task.comm.string_().decode()} FD {fd}"
)
def find_struct_file_binfmt_misc(file: Object) -> None:
prog = file.prog_
for node in list_for_each_entry(
prog.type("Node", filename="binfmt_misc.c"),
prog.object("entries", filename="binfmt_misc.c").address_of_(),
"list",
):
if node.interp_file == file:
print(f"binfmt_misc {os.fsdecode(node.name.string_())}")
def find_struct_file(file: Object) -> None:
find_struct_file_fds(file)
find_struct_file_binfmt_misc(file)
if __name__ == "__main__":
find_struct_file(Object(prog, "struct file *", int(sys.argv[1], 0)))