mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-02-07 16:59:18 +00:00
PRESENCE/presenced: fixed crash of presenced when receiving an invalid command (thanks to Sven); code makeup
git-svn-id: https://svn.fhem.de/fhem/trunk@10988 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
fc6aa6b733
commit
af05573479
@ -42,6 +42,9 @@ use strict;
|
||||
|
||||
|
||||
sub Log($$);
|
||||
sub timestamp();
|
||||
sub daemonize();
|
||||
sub doQuery($$);
|
||||
|
||||
my $new_client;
|
||||
my $server;
|
||||
@ -68,14 +71,11 @@ GetOptions(
|
||||
"l=s" => \$opt_l, "logfile=s" => \$opt_l,
|
||||
"p=i" => \$opt_p, "port=i" => \$opt_p,
|
||||
"P=s" => \$opt_P, "pid-file=s" => \$opt_P,
|
||||
"h" => \$opt_h, "help" => \$opt_h);
|
||||
|
||||
"h" => \$opt_h, "help" => \$opt_h
|
||||
);
|
||||
|
||||
|
||||
Log 0, "=================================================" if($opt_l);
|
||||
|
||||
|
||||
|
||||
Log 1, "started with PID $$";
|
||||
|
||||
if(-e "$opt_P")
|
||||
@ -85,11 +85,8 @@ if(-e "$opt_P")
|
||||
exit 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
sub print_usage () {
|
||||
sub print_usage ()
|
||||
{
|
||||
print "Usage:\n";
|
||||
print " presenced -d [-p <port>] [-P <filename>] \n";
|
||||
print " presenced [-h | --help]\n";
|
||||
@ -104,10 +101,8 @@ sub print_usage () {
|
||||
print " Print detailed log output\n";
|
||||
print " -h, --help\n";
|
||||
print " Print detailed help screen\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
if($opt_d)
|
||||
{
|
||||
daemonize();
|
||||
@ -119,12 +114,10 @@ if($opt_h)
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
open(PIDFILE, ">$opt_P") or die("Could not open PID file $opt_P: $!");
|
||||
print PIDFILE $$."\n";
|
||||
close PIDFILE;
|
||||
|
||||
|
||||
$server = new IO::Socket::INET (
|
||||
LocalPort => $opt_p,
|
||||
Proto => 'tcp',
|
||||
@ -140,8 +133,6 @@ Log 0, "created socket on ".$server->sockhost().":".$server->sockport();
|
||||
my $listener = IO::Select->new();
|
||||
$listener->add($server);
|
||||
|
||||
|
||||
|
||||
my @new_handles;
|
||||
my %child_handles;
|
||||
my %child_config;
|
||||
@ -168,7 +159,6 @@ $server_pid = $$ unless(defined($server_pid));
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
if($log_queue->pending)
|
||||
{
|
||||
Log 2, $log_queue->dequeue;
|
||||
@ -184,7 +174,6 @@ while(1)
|
||||
|
||||
$listener->add($new_client);
|
||||
Log 1, "new connection from ".$new_client->peerhost().":".$new_client->peerport();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -193,11 +182,8 @@ while(1)
|
||||
|
||||
if($buf)
|
||||
{
|
||||
|
||||
$buf =~ s/(^\s*|\s*$)//g;
|
||||
|
||||
|
||||
|
||||
if($buf =~ /^\s*([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\s*\|\s*\d+\s*$/)
|
||||
{
|
||||
$client->send("command accepted\n");
|
||||
@ -222,9 +208,6 @@ while(1)
|
||||
my $new_thread = threads->new(\&doQuery, ($write_handle, $address, $timeout));
|
||||
Log 2, "created thread ".$new_thread->tid()." for processing device $address within $timeout seconds for peer ".$client->peerhost().":".$client->peerport();
|
||||
|
||||
|
||||
|
||||
|
||||
$new_thread->detach();
|
||||
|
||||
$child_handles{$client} = $new_thread;
|
||||
@ -255,7 +238,6 @@ while(1)
|
||||
$queues{$child_handles{$client}->tid()}->enqueue("stop");
|
||||
$client->send("command accepted\n");
|
||||
delete($child_handles{$client});
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -264,12 +246,9 @@ while(1)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$client->send("command rejected\n");
|
||||
$queues{$child_handles{$client}->tid()}->enqueue("stop");
|
||||
Log 1, "received invalid command >>$buf<< from client ".$client->peerhost().":".$client->peerport();
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -287,17 +266,10 @@ while(1)
|
||||
$client = undef;
|
||||
|
||||
Log 1, "closed successfully all threads";
|
||||
;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(defined($sig_received))
|
||||
{
|
||||
@ -307,10 +279,9 @@ while(1)
|
||||
Log 0, "exiting";
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sub daemonize
|
||||
sub daemonize()
|
||||
{
|
||||
use POSIX;
|
||||
POSIX::setsid or die "setsid $!";
|
||||
@ -346,8 +317,6 @@ my $nextrun = gettimeofday();
|
||||
my $cmd;
|
||||
my $run = 1;
|
||||
|
||||
|
||||
|
||||
if($address and $timeout)
|
||||
{
|
||||
THREADLOOP: while($run)
|
||||
@ -373,7 +342,6 @@ my $run = 1;
|
||||
$nextrun = gettimeofday();
|
||||
|
||||
$log_queue->enqueue(threads->tid()."|new address: $address - new timeout $timeout");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -383,15 +351,16 @@ my $run = 1;
|
||||
{
|
||||
{
|
||||
lock($querylocker);
|
||||
|
||||
if($querylocker gt (gettimeofday() - 2))
|
||||
{
|
||||
$log_queue->enqueue(threads->tid()."|waiting before hcitool command execution because last command was executed within the last 2 seconds");
|
||||
|
||||
sleep rand(1) + 1;
|
||||
|
||||
}
|
||||
|
||||
$hcitool = qx(which hcitool);
|
||||
chomp $hcitool;
|
||||
|
||||
if( -x "$hcitool")
|
||||
{
|
||||
$return = qx(hcitool name $address 2>/dev/null);
|
||||
@ -400,9 +369,12 @@ my $run = 1;
|
||||
{
|
||||
$write_handle->send("error\n") if(defined($write_handle));
|
||||
}
|
||||
|
||||
$querylocker = gettimeofday();
|
||||
}
|
||||
|
||||
chomp $return;
|
||||
|
||||
if(not $return =~ /^\s*$/)
|
||||
{
|
||||
$write_handle->send("present;$return\n") if(defined($write_handle));
|
||||
@ -414,19 +386,16 @@ my $run = 1;
|
||||
|
||||
$nextrun = gettimeofday() + $timeout;
|
||||
}
|
||||
|
||||
}
|
||||
sleep 1;
|
||||
}
|
||||
}
|
||||
|
||||
delete($queues{threads->tid()}) if(exists($queues{threads->tid()}));
|
||||
|
||||
}
|
||||
|
||||
sub timestamp
|
||||
sub timestamp()
|
||||
{
|
||||
|
||||
return POSIX::strftime("%Y-%m-%d %H:%M:%S",localtime);
|
||||
}
|
||||
|
||||
@ -440,6 +409,7 @@ sub Log($$)
|
||||
{
|
||||
($thread, $message) = split("\\|", $message);
|
||||
}
|
||||
|
||||
if($loglevel <= $opt_v)
|
||||
{
|
||||
if($opt_l)
|
||||
@ -455,5 +425,4 @@ sub Log($$)
|
||||
|
||||
close(LOGFILE);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user