mruby-kernel-process provides an object-oriented interface around processes on FreeBSD systems. Unlike shelling out to ps(1), this library reads process information through FreeBSD's native libutil(3) interfaces – in particular kinfo_getproc(3) and kinfo_getallproc(3).
The two functions that we use are implemented as easy-to-use wrappers around sysctl(3) – which is used to query kernel state, including information about active processes, hence the name "Kernel Process". It also avoids confusion and conflict with mruby-process.
The Kernel::Process singleton class is enumerable. It provides
access to all processes that are visible to a user. We can
use the each method to iterate over processes, we can use
the select method to filter, and we can use the map method
to trasform:
##
# Iterate all
Kernel::Process.each do |process|
print "pid=#{process.pid}",
"ppid=#{process.ppid}",
"uid=#{process.uid}",
"\n\n"
end
##
# Select root processes
Kernel::Process.select do |process|
process.uid == 0
end
##
# Transform processes
parents = Kernel::Process.map(&:ppid)The Kernel::Process.self method returns an instance
of Kernel::Process that represents the current process:
this = Process.self
print "pid = ", this.pid, "\n"The Kernel::Process.find method can find a process by ID, and returns an
instance of Kernel::Process or raises an error when a process by the given
ID cannot be found:
process = Kernel::Process.find(12345)
print "pid " , "\t", process.pid , "\n" # => process ID
print "ppid ", "\t", process.ppid , "\n" # => parent process ID
print "uid " , "\t", process.uid , "\n" # => effective user id
print "ruid ", "\t", process.ruid , "\n" # => real user id
print "svuid", "\t", process.svuid, "\n" # => saved user idMRuby::Build.new("app") do |conf|
conf.toolchain
conf.gembox "default"
conf.gem github: "0x1eef/mruby-kernel-process", branch: "main"
end