my $processes = Proc::ProcessTable->new;
for ( @{ $processes->table } ) {
…;
}
I don't think that will always work safely:
- At least FreeBSD does not mount /proc any more, so Proc::ProcessTable will probably return no processes at all.
- On a system using Linux Containers, you will see processes running in containers as processes on the host. That may cause false positives.
- You introduce a TOCTTOU problem - by the time you have evaluated data from /proc, the situation may have changed dramatically.
- On some systems (at least Linux), content of /proc may be edited, e.g. by assigning to $0.
Trying to locking the executable should be free of race conditions (or else flock() would be severely broken) and should also work with soft and hard links, as the file is locked, not one of its directory entries.
Quick assign to $0 demo:
/root>perl -E 'say `cat /proc/$$/cmdline`'
perl-Esay `cat /proc/$$/cmdline`
/root>perl -E '$0="find me"; say `cat /proc/$$/cmdline`'
find me
/root>
pstree on a host with about 10 containers (running Proxmox VE, both host and containers using Debian 11)
Every lxc-start is a parent process of a container, every systemd that is a child of a lxc-start is the init process (pid 1) of a container, each of those systemds and all of their children are running in a container.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|