Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)

systemd─┬─agetty
        ├─cron
        ├─dbus-daemon
        ├─dovecot─┬─anvil
        │         ├─config
        │         └─log
        ├─8*[dtach───lxc-console]
        ├─2*[iscsid]
        ├─lxc-monitord
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─dhclient───3*[{dhclient}]
        │                     ├─es-ct18-linux-a
        │                     ├─nullmailer-send
        │                     ├─perl
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─apache2───10*[apache2]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─dhclient───3*[{dhclient}]
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─dhclient───3*[{dhclient}]
        │                     ├─mini_httpd
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─dhclient───3*[{dhclient}]
        │                     ├─dovecot─┬─anvil
        │                     │         ├─auth
        │                     │         ├─config
        │                     │         ├─imap
        │                     │         ├─log
        │                     │         └─stats
        │                     ├─exim4
        │                     ├─multi-fetchmail───8*[fetchmail]
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─mini_httpd
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     ├─systemd-resolve
        │                     └─urbackupsrv───20*[{urbackupsrv}]
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─apache2───5*[apache2]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─dhclient───3*[{dhclient}]
        │                     ├─nullmailer-send
        │                     ├─postgres───6*[postgres]
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─dhclient───3*[{dhclient}]
        │                     ├─lighttpd
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─mini_httpd
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─apache2───2*[apache2───26*[{apache2}]]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─mini_httpd
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     └─systemd-resolve
        ├─lxc-start───systemd─┬─3*[agetty]
        │                     ├─cron
        │                     ├─dbus-daemon
        │                     ├─lighttpd
        │                     ├─nmbd
        │                     ├─nullmailer-send
        │                     ├─rsyslogd───2*[{rsyslogd}]
        │                     ├─smbd─┬─cleanupd
        │                     │      ├─lpqd
        │                     │      ├─smbd
        │                     │      └─smbd-notifyd
        │                     ├─systemd-journal
        │                     ├─systemd-logind
        │                     ├─systemd-network
        │                     ├─systemd-resolve
        │                     └─vsftpd
        ├─lxcfs───6*[{lxcfs}]
        ├─mdadm
        ├─mini_httpd
        ├─nullmailer-send
        ├─pmxcfs───12*[{pmxcfs}]
        ├─pve-firewall
        ├─pve-ha-crm
        ├─pve-ha-lrm
        ├─pve-lxc-syscall───2*[{pve-lxc-syscall}]
        ├─pvedaemon───3*[pvedaemon worke]
        ├─pvefw-logger───{pvefw-logger}
        ├─pveproxy───3*[pveproxy worker]
        ├─pvescheduler
        ├─pvestatd
        ├─qmeventd
        ├─rpcbind
        ├─rrdcached───12*[{rrdcached}]
        ├─rsyslogd───3*[{rsyslogd}]
        ├─smartd
        ├─spiceproxy───spiceproxy work
        ├─sshd───sshd───sshd───bash───sudo───bash───pstree
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-timesyn───{systemd-timesyn}
        ├─systemd-udevd
        ├─watchdog-mux
        └─zed───2*[{zed}]

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". ;-)

In reply to Re^2: Mechanism for ensuring only one instance of a Perl script can only run? by afoken
in thread Mechanism for ensuring only one instance of a Perl script can only run? by redapplesonly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found