Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Mechanism for ensuring only one instance of a Perl script can only run?

by afoken (Chancellor)
on Dec 02, 2022 at 22:56 UTC ( [id://11148510]=note: print w/replies, xml ) Need Help??


in reply to Re: Mechanism for ensuring only one instance of a Perl script can only run?
in thread Mechanism for ensuring only one instance of a Perl script can only run?

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

Replies are listed 'Best First'.
Re^3: Mechanism for ensuring only one instance of a Perl script can only run?
by karlgoethebier (Abbot) on Dec 03, 2022 at 09:26 UTC
    "… return no processes at all."

    From README.freebsd-kvm of the current version of Proc::ProcessTable:

    "FreeBSD 5.X not mounting /proc by default. Procfs is vulnerable system and its use is not recommended in future. In addition, mapping processes space to /proc is not correct (at least, in 7 of 7 my freebsd servers with FreeBSD5 installed). So, I decided to write this code. This module works via the kvm system."

    And it should be possible to obtain the PIDs of the children of every loc-start command.

    «The Crux of the Biscuit is the Apostrophe»

Re^3: Mechanism for ensuring only one instance of a Perl script can only run?
by parv (Parson) on Dec 03, 2022 at 07:50 UTC

    Proc::ProcessTable on FreeBSD stable/13 -- "/proc" is a plain empty directory here -- showed all the "xterm" processes using the "A cheap and sleazy version of ps" example; did not check any further.

    If anyone has suggestions for things I should check when the module may fail, I am all ears^Weyes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148510]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-20 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found