#!/perl # Minimal forking server - Shows what is needed in a systemd socket version 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 exit $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, and which remote commands it may call. # Need to close the socket & refuse futher commands $remote_addr is 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 take some time to process. else { print $sock "I did not understand that. What next oh master?\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 return 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 connections.\n"; } } else { # The 5 second timeout on the the socket accept() call has expired, or a HUP signal was received. } } print "Caught TERM will quit now\n";