From: Andrea R. <ar...@sy...> - 2005-08-04 09:58:56
|
Author: arighi Date: 2005-08-04 08:14:56 -0500 (Thu, 04 Aug 2005) New Revision: 3218 Modified: trunk/sbin/si_monitor Log: Fixed a zombie reaping problem. Now child processes are killed properly. Modified: trunk/sbin/si_monitor =================================================================== --- trunk/sbin/si_monitor 2005-08-04 11:19:30 UTC (rev 3217) +++ trunk/sbin/si_monitor 2005-08-04 13:14:56 UTC (rev 3218) @@ -16,7 +16,7 @@ use constant DEFAULT_PORT => 8181; my $VERSION = "SYSTEMIMAGER_VERSION_STRING"; -my $program_name = "si_monitord"; +my $program_name = "si_monitor"; my $version_info = << "EOF"; $program_name (part of SystemImager) v$VERSION @@ -110,17 +110,17 @@ } ### END evaluate command line options ### +# Reaping zombie processes. +$SIG{CHLD} = 'IGNORE'; + # Define lock files. my $lock_file = $conf{'lock_dir'} . "/db.si_monitor.lock"; # Remove old locks. unlink("$lock_file"); -# Use TCP protocol; -my $proto = getprotobyname('tcp'); - -# Open the socket. -socket(IN, PF_INET, SOCK_STREAM, $proto) or +# Open a TCP socket. +socket(IN, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "error: could not create the socket: $!\n"; setsockopt(IN, SOL_SOCKET, SO_REUSEADDR, 1); my $client_addr = sockaddr_in($port, INADDR_ANY); @@ -136,6 +136,12 @@ close(CLIENT); next; } + defined($pid) or die "error: cannot fork: $!\n"; + + # Child closes unused server handle. + close(IN); + + # Get other end identity. my $other_end = getpeername(CLIENT); if ($other_end) { # Get client informations. @@ -143,7 +149,7 @@ my ($other_port, $other_iaddr) = unpack_sockaddr_in($other_end); my $other_ip_address = inet_ntoa($other_iaddr); my $other_host = gethostbyaddr($other_iaddr, AF_INET); - + # Report info in the log if defined. print LOG gmtime() . ": connection accepted for ${other_host}:${other_port}\n" @@ -163,14 +169,17 @@ # Close client connection. close(CLIENT); } else { + # Refuse the client connection. close(CLIENT); print LOG gmtime() . ": warning: could not identify other end of a client request, ignoring.\n" if ($log_file); } + close(LOG) if ($log_file); exit(0); } +# Error accepting client connections. die "error: cannot accept clients connections!\n"; # Usage: |