lilalo

diff l3-backend @ 62:c4bea959dbb1

Beautyfication of l3-agent code. Many lines were erased. Need to be tested
author devi
date Thu Jan 26 00:00:53 2006 +0200 (2006-01-26)
parents
children 4c02cf4123ee
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/l3-backend	Thu Jan 26 00:00:53 2006 +0200
     1.3 @@ -0,0 +1,106 @@
     1.4 +#!/usr/bin/perl
     1.5 +
     1.6 +use strict;
     1.7 +use lib '.';
     1.8 +use l3config;
     1.9 +use IO::Socket;
    1.10 +use POSIX qw(:sys_wait_h);
    1.11 +
    1.12 +sub main;
    1.13 +
    1.14 +main();
    1.15 +
    1.16 +sub REAPER {
    1.17 +	1 until (-1 == waitpid(-1, WNOHANG));
    1.18 +	$SIG{CHLD} = \&REAPER;
    1.19 +}
    1.20 +
    1.21 +sub process_was_killed
    1.22 +{
    1.23 +	# Здесь должна быть красивая процедура 
    1.24 +	# завершения демона
    1.25 +	unlink $Config{backend_pidfile};
    1.26 +	exit(0);
    1.27 +}
    1.28 +
    1.29 +sub main {
    1.30 +	init_config();
    1.31 +
    1.32 +
    1.33 +	# Проверяем, возможно демон уже запущен
    1.34 +	# Если он работает, просто завершаемся
    1.35 +	if (open(PIDFILE, $Config{backend_pidfile})) {
    1.36 +		my $pid = <PIDFILE>;
    1.37 +		close(PIDFILE);
    1.38 +		if ( ! -e "/proc/$pid" || !`grep $Config{"l3-backend"} /proc/$pid/cmdline && grep "uid:.*\b$<\b" /proc/$pid/status`) {
    1.39 +			print "Removing stale pidfile\n";
    1.40 +			unlink $Config{backend_pidfile}
    1.41 +				or die "Can't remove stale pidfile ". $Config{backend_pidfile}. " : $!";
    1.42 +		}
    1.43 +		else {
    1.44 +			print "l3-backend is already running\n";
    1.45 +			exit(0);
    1.46 +		}
    1.47 +	}
    1.48 +
    1.49 +	# Уходим в background, если необходимо
    1.50 +	if ($Config{detach} =~ /^y/i) {
    1.51 +		#$Config{verbose} = "no";
    1.52 +		my $pid = fork;
    1.53 +		exit if $pid;
    1.54 +		die "Couldn't fork: $!" unless defined ($pid);
    1.55 +
    1.56 +		open(PIDFILE, ">", $Config{backend_pidfile})
    1.57 +			or die "Can't open pidfile ". $Config{backend_pidfile}. " for wrting: $!";
    1.58 +		print PIDFILE $$;
    1.59 +		close(PIDFILE);
    1.60 +
    1.61 +		for my $handle (*STDIN, *STDOUT, *STDERR) {
    1.62 +			open ($handle, "+<", "/dev/null")
    1.63 +				or die "can't reopen $handle to /dev/null: $!"
    1.64 +		}
    1.65 +
    1.66 +		POSIX::setsid()
    1.67 +			or die "Can't start a new session: $!";
    1.68 +
    1.69 +		$0 = $Config{"l3-backend"};
    1.70 +		
    1.71 +		$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&process_was_killed;
    1.72 +	}
    1.73 +
    1.74 +	# Открываем сетевой сокет и слушаем
    1.75 +	my $server = IO::Socket::INET->new(
    1.76 +			LocalPort 	=> $Config{backend_port},
    1.77 +			Type		=> SOCK_STREAM,
    1.78 +			Reuse		=> 1,
    1.79 +			Listen		=> 10 );
    1.80 +	
    1.81 +	if (!$server) {
    1.82 +		die "Couldn't bind to socket ".$Config{backend_port}."\n";
    1.83 +	}
    1.84 +
    1.85 +	$SIG{CHLD} = 'IGNORE';
    1.86 +
    1.87 +	# При получении новых соединенений,
    1.88 +	# порождаем дочерние процессы
    1.89 +	while (my $client = $server->accept()) {
    1.90 +		my $pid;
    1.91 +		next if $pid = fork;
    1.92 +		die "fork: $!" unless defined $pid;
    1.93 +
    1.94 +		# Это наш ответвлённый клиент
    1.95 +		close($server);
    1.96 +
    1.97 +		# Считываем данные и передаём их в точку получения
    1.98 +		open(OUT, ">>", $Config{"backend_datafile"});
    1.99 +		select OUT; $|=1;
   1.100 +		while(<$client>) {
   1.101 +			print OUT $_;
   1.102 +		}
   1.103 +		close(OUT);
   1.104 +	} 
   1.105 +	continue {
   1.106 +		# Наш родитель
   1.107 +		close ($client);
   1.108 +	}
   1.109 +}