From 29bc5b49d7ade40e30edd1e339f1e6d6f9ec5093 Mon Sep 17 00:00:00 2001 From: Brandon Payton Date: Sat, 11 Jul 2026 07:53:42 -0400 Subject: [PATCH] lsof: package the upstream open-file inspector --- Formula/lsof.rb | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 79 insertions(+) create mode 100644 Formula/lsof.rb diff --git a/Formula/lsof.rb b/Formula/lsof.rb new file mode 100644 index 0000000..dc179ed --- /dev/null +++ b/Formula/lsof.rb @@ -0,0 +1,78 @@ +require_relative "../Kandelo/formula_support/kandelo_formula_support" + +class Lsof < Formula + include KandeloFormulaSupport + + desc "Utility to list open files inside Kandelo" + homepage "https://github.com/lsof-org/lsof" + url "https://github.com/lsof-org/lsof/archive/refs/tags/4.99.7.tar.gz" + sha256 "bac1b0acbc50aede42fc97dffaa0b0475e97973e36a6351de5f349c6155afc68" + license "lsof" + + skip_clean "bin/lsof" + + def install + kandelo_require_arch!("wasm32") + + kandelo_wasm_build do |root| + # Kandelo intentionally does not expose Linux AF_PACKET or AF_NETLINK + # UAPI. Upstream only uses these headers for optional, guarded names. + inreplace "lib/dialects/linux/dlsof.h" do |s| + s.gsub! <<~OLD, <<~NEW + # include + # include + OLD + # if defined(__has_include) + # if __has_include() + # include + # endif + # if __has_include() + # include + # endif + # else + # include + # include + # endif + NEW + end + + include_dir = "#{ENV.fetch("WASM_POSIX_SYSROOT")}/include" + ENV["LSOF_CC"] = kandelo_cc(root) + ENV["LINUX_CONF_CC"] = kandelo_cc(root) + ENV["LSOF_CCV"] = ENV.cc + ENV["LSOF_INCLUDE"] = include_dir + ENV["LINUX_INCL"] = include_dir + ENV["LINUX_VERSION_CODE"] = "393216" # Linux 6.0 procfs compatibility level. + ENV["LINUX_CLIB"] = "-U__GLIBC__" + ENV["LSOF_AR"] = "#{kandelo_ar(root)} cr" + ENV["LSOF_RANLIB"] = kandelo_ranlib(root) + ENV["LSOF_MAKE"] = "make" + + system "./Configure", "-n", "linux" + system "make", "-j#{ENV.make_jobs}" + kandelo_fork_instrument(buildpath/"lsof") + end + + kandelo_install_bin(buildpath, "lsof", "lsof") + man8.install "Lsof.8" => "lsof.8" + end + + test do + version_output = kandelo_run_wasm(bin/"lsof", ["-v"], merge_stderr: true) + assert_match(/revision: 4\.99\.7$/, version_output) + + workdir = testpath/"working-directory" + workdir.mkpath + fields = kandelo_run_wasm( + bin/"lsof", + ["-nP", "-a", "-p", "100", "-d", "cwd,0-2", "-Fpcfnt"], + env: { "KERNEL_CWD" => workdir }, + ) + assert_match(/^p100$/, fields) + assert_match(/^clsof\.wasm$/, fields) + assert_match(/^fcwd\ntDIR\nn#{Regexp.escape(workdir.to_s)}$/, fields) + assert_match(%r{^f0\nn/dev/stdin$}, fields) + assert_match(%r{^f1\nn/dev/stdout$}, fields) + assert_match(%r{^f2\nn/dev/stderr$}, fields) + end +end diff --git a/README.md b/README.md index 2fd277f..3dda1a8 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Current migration controls and pilots include: - `sed`, the GNU stream-editing CLI used by shell and build workflows; - `gzip`, the GNU compression CLI with native gunzip and zcat aliases; - `grep`, GNU regular-expression and file search for the leaf CLI wave. +- `lsof`, the upstream process and open-file inspector backed by Kandelo procfs. The SDK is not yet a Homebrew dependency. Trusted builds supply an `HOMEBREW_KANDELO_ROOT` checkout containing the SDK, sysroot, kernel, and Node