in reply to Systemd socket activated service in perl

Check out Systemd::Daemon.

However since I am familiar with xinetd and not systemd, I did your server in xinetd instead.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11157093 use warnings; $| = 1; # NOTE # slightly reformatted from your code - $sock not needed while( my $cmd = <> ) { if( $cmd =~ m/^sleep (\d+)/ ) { my $snooze_time = $1; printf "Will sleep for $snooze_time seconds\n"; sleep $snooze_time; print "... ZZZ ... Yawn ... What next oh master?\n"; } # NB: In real life there are lots of other commands and many take some + time to process. else { print "I did not understand that. What next oh master?\n"; } } __END__ # # unlisted service # (goes in /etc/xinetd.d) NOTE change user, server, and only_from # if this file changed, use 'systemctl reload xinetd' so prev still ru +n # using 'restart' will kill running servers, 'reload' will not # service unlisted-pm11157093 { type = UNLISTED socket_type = stream protocol = tcp wait = no user = rick log_type = FILE /dev/null server = /home/rick/code/pm11157093.pl port = 1234 disable = no only_from = 127.0.0.1 192.168.1.0/24 }

Note that xinetd does listening and forking for you, runs your child with the data socket connected to STDIN and STDOUT, and even has an 'only_from' parameter to verify remote machines.
Leaves very little to do in your server :)

UPDATE: corrected spelling of 'xinitd' to 'xinetd'

Replies are listed 'Best First'.
Re^2: Systemd socket activated service in perl
by tybalt89 (Monsignor) on Jan 20, 2024 at 12:30 UTC

    And after some digging, here's a systemd version. It uses the very same server code in /home/rick/code/pm11157093.pl from this node's parent (change to your liking), and is called pfs (for perl forking server).

    Make file /etc/systemd/system/pfs.socket

    # perl forking server https://perlmonks.org/?node_id=11157093 [Unit] Description=perl forking socket [Socket] ListenStream=1234 Accept=yes

    and file /etc/systemd/system/pfs@.service

    # perl forking server https://perlmonks.org/?node_id=11157093 [Unit] Description=perl forking server Requires=pfs.socket [Service] ExecStart=-/home/rick/code/pm11157093.pl StandardInput=socket

    run systemctl start pfs.socket and fire up a telnet to test it.

    Some extra config lines may be required to start automatically at boot, plus systemctl enable pfs.socket

    Perhaps a systemd expert can fill in any holes I've missed.

    UPDATE: the remote addr is in $ENV{REMOTE_ADDR} (see man systemd.socket)

Re^2: Systemd socket activated service in perl
by hippo (Archbishop) on Jan 19, 2024 at 23:07 UTC

    ++ for using the right tool for the job! When all you have is the systemd hammer, everything starts looking like a nail. We never used to be so hamstrung.


    🦛

Re^2: Systemd socket activated service in perl
by chrestomanci (Priest) on Jan 20, 2024 at 11:20 UTC

    Thanks for the link to Systemd::Daemon I will check it out.

    Sadly, the example code you have given me for xinetd won't be sufficient. I really need the remote IP address as for the production service the same request will get substantially different answers depending on who is asking, and that answer can change from minute to minute via a database query. A simple whitelist of allowed IP addresses is no where near sufficient.

      The remote host is in $ENV{REMOTE_HOST}

        Thanks for the tip that it will be in the environment of the spawned process, though actually it is in REMOTE_ADDR (not REMOTE_HOST)