FU: Add -procname import option and setting $0 to something useful

This commit is contained in:
Yorhel 2025-04-22 09:32:56 +02:00
parent ea8ad9e483
commit 91b2421a84

32
FU.pm
View file

@ -8,15 +8,22 @@ use FU::Log 'log_write';
use FU::Util;
use FU::Validate;
my $procname;
my $scriptpath = $0;
sub import($pkg, @opt) {
my $c = caller;
no strict 'refs';
*{$c.'::fu'} = \&fu;
my $spawn;
for (@opt) {
if ($_ eq '-spawn') { _spawn() }
if (ref $procname eq 'FU::ARG') { $procname = $_ }
elsif ($_ eq '-procname') { $procname = bless {}, 'FU::ARG' }
elsif ($_ eq '-spawn') { $spawn = 1; }
else { croak "Unknown import option: '$_'" }
}
croak "Missing argument for -procname option" if ref $procname eq 'FU::ARG';
_spawn() if $spawn;
}
@ -209,7 +216,7 @@ sub _monitor {
die if $m > $data{$_};
},
no_chdir => 1
}, $0, values %INC, @monitor_paths);
}, $scriptpath, values %INC, @monitor_paths);
0
} // 1;
}
@ -409,6 +416,7 @@ sub _do_req($c) {
sub _run_loop($c) {
my $stop = 0;
my $count = 0;
local $SIG{HUP} = 'IGNORE';
local $SIG{TERM} = $SIG{INT} = sub { $stop = 1 };
@ -418,7 +426,13 @@ sub _run_loop($c) {
exit;
}
my sub setstate($state) {
$0 = sprintf "%s: %s [#%d%s]", $procname, $state, $count, $c->{max_reqs} ? "/$c->{max_reqs}" : '' if $procname;
}
while (!$stop) {
setstate 'idle';
$c->{client_sock} ||= $c->{listen_sock}->accept || next;
$c->{fcgi_obj} ||= $c->{listen_proto} eq 'fcgi' && FU::fcgi::new(fileno $c->{client_sock}, $c->{proc});
@ -427,11 +441,13 @@ sub _run_loop($c) {
passclient;
}
setstate 'working';
_do_req $c;
$c->{client_sock} = $c->{fcgi_obj} = undef if !($c->{fcgi_obj} && $c->{fcgi_obj}->keepalive);
passclient if $c->{max_reqs} && !--$c->{max_reqs};
$count++;
passclient if $c->{max_reqs} && $count >= $c->{max_reqs};
}
}
@ -489,12 +505,14 @@ sub _supervisor($c) {
fcntl $client, Fcntl::F_SETFD, 0;
$ENV{FU_CLIENT_FD} = fileno $client;
}
exec $^X, (map "-I$_", @INC), $0;
exec $^X, (map "-I$_", @INC), $scriptpath;
exit 1;
}
$childs{$pid} = 1;
}
$0 = sprintf "%s: supervisor [%d/%d]", $procname, scalar keys %childs, $c->{proc} if $procname;
my ($fd, $msgadd) = FU::Util::fdpass_recv(fileno($rsock), 500);
push @client_fd, $fd if $fd;
next if !defined $msgadd;
@ -1048,6 +1066,12 @@ returning strings deal with perl Unicode strings, not raw bytes.
=over
=item use FU -procname => $name
When the C<-procname> import option is set, FU automatically updates the
process name (as displayed in L<top(1)> and L<ps(1)>, see `$0`) with
information about the current process, prefixed with the given C<$name>.
=item FU::init_db($info)
Set database configuration. C<$info> can either be a connection string for C<<