Greetings wise brothers.

I have an existing perl forking server that listens on a TCP/IP socket, forks a new process for each incoming connection and that process spends some time (5-60 minutes) handing the remote client.

I would like to convert the server to a systemd socket activated service. This is so that I can upgrade the code for the server via a Debian package upgrade without interrupting the child workers and their ongoing transaction.

My current version is configured to run as a long running systemd daemon. I have prepared a minimal version that shows how the current service works.

#!/perl # Minimal forking server - Shows what is needed in a systemd socket ve +rsion of the ET daemon. use strict; use warnings; use English; use IO::Socket::INET; use Socket qw(SOMAXCONN SOL_SOCKET SO_KEEPALIVE); use POSIX qw(:sys_wait_h ECHILD EINTR); # Flags used by the signal handlers my $quit = 0; # Signal handling functions sub REAPER { my $child; while ((my $waitedpid = waitpid(-1,WNOHANG)) > 0) { my $message = "reaped $waitedpid" . ($CHILD_ERROR ? " with exi +t $CHILD_ERROR" : ""); print "$message\n"; } } $SIG{CHLD} = \&REAPER; # loathe sysV $SIG{TERM} = sub { $quit = 1 }; sub handle_connection { my ($sock) = @_; setsockopt($sock, SOL_SOCKET, SO_KEEPALIVE, 1); ######################################################## # # Important. We need to know the remote IP address. # ######################################################## my $remote_addr = inet_ntoa ($sock->peeraddr()); printf $sock "Greetings %s what is your command?\n", $remote_addr; # Code here to check that $remote_addr is authorised to connect, a +nd which remote commands it may call. # Need to close the socket & refuse futher commands $remote_addr i +s not authorised. while( my $cmd = <$sock> ) { if( $cmd =~ m/^sleep (\d+)/ ) { my $snooze_time = $1; printf $sock "Will sleep for $snooze_time seconds\n"; sleep $snooze_time; print $sock "... ZZZ ... Yawn ... What next oh master?\n"; } # NB: In real life there are lots of other commands and many t +ake some time to process. else { print $sock "I did not understand that. What next oh maste +r?\n"; } } # Socket connection closed. exit(0); # This is in a forked child. } # MAIN # Create the server socket to listen for incoming connections my $s = IO::Socket::INET->new( LocalPort => 1234, Listen => SOMAXCONN, ReuseAddr => 1, Timeout => 5, # seconds Blocking => 0, # After the timeout the accept() call will r +eturn regardless ); $s->listen(); print "ET daemon startup - Listening on port 1234 - My PID:$PID\n"; ACCEPT: while ( 0 == $quit ) { my $new_s = $s->accept(); if( defined $new_s ) { print "Accepted socket connection on port 1234\n"; redo ACCEPT if $! == ECHILD or $! == EINTR; # fork off to handle the new connection my $pid = fork(); if ($pid == 0) { print "CHILD: Calling handle_connection()\n"; handle_connection($new_s); } else { print "PARENT: Forked off child PID:$pid\n"; close($new_s); print "PARENT: Closed the socket & waiting for new connect +ions.\n"; } } else { # The 5 second timeout on the the socket accept() call has exp +ired, or a HUP signal was received. } } print "Caught TERM will quit now\n";

I believe it is possible to configure systemd to handle the socket and the fork for each incoming connection on behalf of my service, so I would just need the handle_connection() method and the necessary boilerplate to retrieve the remote IP address and close the connection if the remote IP address is not authorised to connect.

I found some example code by one of the systemd developers, but it is all in C and makes use of systemd specific C libraries and data structures, so I am having trouble understanding it. Can anyone point me to a perl or python example that is easier to follow?


In reply to Systemd socket activated service in perl by chrestomanci

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.