diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2017-06-01 11:00:58 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2017-06-01 11:00:58 +0200 |
commit | 8cfd584400a9852c4bd7c4b3d50ff0cacc8ec019 (patch) | |
tree | 81850b8f211b095c149bb24c334a4608323c87e6 | |
parent | 63bca29310bc899ce6c7068f376cbfb426da3571 (diff) |
postfix-sender-login: terminate the worker after 32*$nProc connections to release ressources.
-rwxr-xr-x | roles/MSA/files/usr/local/bin/postfix-sender-login.pl | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/roles/MSA/files/usr/local/bin/postfix-sender-login.pl b/roles/MSA/files/usr/local/bin/postfix-sender-login.pl index 5f563c3..e792457 100755 --- a/roles/MSA/files/usr/local/bin/postfix-sender-login.pl +++ b/roles/MSA/files/usr/local/bin/postfix-sender-login.pl @@ -32,54 +32,59 @@ use Authen::SASL (); # clean up PATH $ENV{PATH} = join ':', qw{/usr/bin /bin}; delete @ENV{qw/IFS CDPATH ENV BASH_ENV/}; my $nProc = 2; # number of pre-forked servers my $POSTMASTER = 'postmaster@fripost.org'; # returned for forbidden envelope sender addresses my $BASEDN = 'ou=virtual,dc=fripost,dc=org'; my $BUFSIZE = 65536; # try to read that many bytes at the time my $LDAPI = 'ldapi://%2Fvar%2Fspool%2Fpostfix-msa%2Fprivate%2Fldapi/'; sub server(); # fdopen(3) the file descriptor FD die "This service must be socket-activated.\n" unless defined $ENV{LISTEN_PID} and $ENV{LISTEN_PID} == $$ and defined $ENV{LISTEN_FDS} and $ENV{LISTEN_FDS} == 1; open my $S, '+<&=', 3 or die "fdopen: $!"; +my @CHILDREN; for (my $i = 0; $i < $nProc-1; $i++) { my $pid = fork() // die "fork: $!"; - unless ($pid) { + if ($pid) { + push @CHILDREN, $pid; + } else { server(); # child, never return exit; } } server(); +waitpid $_ => 0 foreach @CHILDREN; +exit $?; ############################################################################# sub server() { - while(1) { + for (my $n = 0; $n < 32; $n++) { accept(my $conn, $S) or do { next if $! == EINTR; die "accept: $!"; }; my $reply = process_request($conn); # encode the reply as a netstring and send it back # https://cr.yp.to/proto/netstrings.txt $reply = length($reply).':'.$reply.','; my $len = length($reply); for (my $i = 0; $i < $len;) { my $n = syswrite($conn, $reply, $len-$i, $i) // do { next if $! == EINTR; warn "Can't write: $!"; last; }; $i += $n; } close $conn or warn "Can't close: $!"; |