rt/msrserv.pl
author Florian Pose <fp@igh-essen.com>
Fri, 24 Feb 2006 16:10:52 +0000
changeset 84 b4ae98855cea
parent 28 801dc7eabf51
child 195 674071846ee3
permissions -rwxr-xr-x
printk durch Makros ersetzt.
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
#!/usr/bin/perl -w
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     2
#------------------------------------------------------------
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     3
#
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     4
# (C) Copyright
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     5
#     Diese Software ist geistiges Eigentum der 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     6
#     Ingenieurgemeinschaft IgH. Sie darf von 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     7
#     Toyota Motorsport GmbH
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     8
#     beliebig kopiert und veraendert werden. 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
     9
#     Die Weitergabe an Dritte ist untersagt.
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    10
#     Dieser Urhebrrechtshinweis muss erhalten
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    11
#     bleiben.
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    12
#
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    13
#     Ingenieurgemeinschaft IgH
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    14
#     Heinz-Baecker-Strasse 34
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    15
#     D-45356 Essen
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    16
#     Tel.:  +49-201/61 99 31
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    17
#     Fax.:  +49-201/61 98 36
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    18
#     WWW:   http://www.igh-essen.com
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    19
#     Email: msr@igh-essen.com
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    20
#
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    21
#------------------------------------------------------------
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    22
#
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
# Multithreaded Server 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
# according to the example from "Programming Perl"
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    25
# this code is improved according to the example from 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    26
# perldoc perlipc, so now safely being usable under Perl 5.8 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    27
# (see note (*))
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
#
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
# works with read/write on a device-file  
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
# $Revision: 1.1 $
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    32
# $Date: 2004/10/01 16:00:42 $
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
# $RCSfile: msrserv.pl,v $
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    35
#------------------------------------------------------------
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
require 5.002;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
use strict;
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    39
BEGIN { $ENV{PATH} = '/opt/msr/bin:/usr/bin:/bin' }
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
use Socket;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
use Carp;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
use FileHandle;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
use Getopt::Std; 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
use Sys::Syslog qw(:DEFAULT setlogsock); 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
use vars qw (
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
	     $self $pid $dolog $port $dev %opts $selfbase
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
	     $len $offset $stream $written $read $log $blksize
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    50
	     $instdir
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
	     $authfile %authhosts
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
	     );
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
# Do logging to local syslogd by unix-domain socket instead of inetd
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
setlogsock("unix");  
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
# Prototypes and some little Tools
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
sub spawn;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
sub logmsg { 
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    61
  my ($level, $debug, @text) = @_;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    62
  syslog("daemon|$level", @text) if $debug > $dolog;
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
#  print STDERR "daemon|$level", @text, "\n" if $dolog;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
sub out {
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
  my $waitpid = wait; 
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    67
  logmsg("notice", 2, "$waitpid exited");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
  unlink "$selfbase.pid";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
  exit 0;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
sub help {
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
  print "\n  usage: $0 [-l og] [-h elp] [-p port] [-d device]\n"; 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
  exit; 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
# Process Options
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
%opts = (
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
	 "l" => 1,
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
	 "h" => 0,
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
	 "p" => 2345,
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
	 "d" => "/dev/msr"
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
	 );
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
  
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
getopts("lhp:d:", \%opts);
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
help if $opts{"h"};
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
( $self =  $0 ) =~ s+.*/++ ;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
( $selfbase = $self ) =~ s/\..*//;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
$log = "$selfbase.log";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
$dolog = $opts{"l"};
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
$port = $opts{"p"};
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
$dev = $opts{"d"};
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
$blksize = 1024; # try to write as much bytes
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    96
$instdir = "/opt/msr";
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
    97
$authfile = "$instdir/etc/hosts.auth"; 
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
# Start logging
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
openlog($self, 'pid');
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
# Flush Output, dont buffer
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
$| = 1;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
# first fork and run in background
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
if ($pid = fork) {
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
#  open LOG, ">$log" if $dolog;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
#  close LOG;
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   109
  logmsg("notice", 2, "forked process: $pid\n");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
  exit 0;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
# Server tells about startup success
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   114
open (PID, ">/$instdir/var/run/$selfbase.pid");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
print PID "$$\n";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
close PID;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
# Cleanup on exit (due to kill -TERM signal)
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
$SIG{TERM} = \&out;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
# We use streams
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
my $proto = getprotobyname('tcp');
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
# Open Server socket
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
  or die "setsocketopt: $!";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
bind (Server, sockaddr_in($port, INADDR_ANY))
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
  or die "bind: $!";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
listen (Server, SOMAXCONN) 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
  or die "listen: $!";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
%authhosts = ();
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
# get authorized hosts
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
open (AUTH, $authfile) 
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   136
  or logmsg ("notice", 2, "Could not read allowed hosts file: $authfile");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
while (<AUTH>) {
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
    chomp;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
    my $host = lc $_;
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   140
     if ($host =~ /^[\d\w]/) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   141
	 $authhosts{$_} = 1;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   142
	 logmsg ("notice", 2, "Authorized host: >$host<");
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   143
     }
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
close (AUTH);
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
# tell about open server socket
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   148
logmsg ("notice", 2, "Server started at port $port");
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   149
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   150
my $waitedpid = 0;
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
my $paddr;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
# wait for children to return, thus avoiding zombies
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   154
# improvement (*)
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   155
use POSIX ":sys_wait_h";
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
sub REAPER {
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   157
  my $child;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   158
  while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   159
    logmsg ("notice", 2, "reaped $waitedpid", ($? ? " with exit $?" : ""));
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   160
  }
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   161
  $SIG{CHLD} = \&REAPER;  # loathe sysV
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
# also all sub-processes should wait for their children
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
$SIG{CHLD} = \&REAPER;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
# start a new server for every incoming request
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   168
# improvement (*) -- loop forever
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   169
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   170
while ( 1 ) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   171
  for ( $waitedpid = 0;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   172
	($paddr = accept(Client,Server)) || $waitedpid;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   173
	$waitedpid = 0, close Client ) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   174
    next if $waitedpid and not $paddr;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   175
    my ($port, $iaddr) = sockaddr_in($paddr);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   176
    my $name = lc gethostbyaddr($iaddr, AF_INET);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   177
    my $ipaddr = inet_ntoa($iaddr);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   178
    my $n = 0;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   179
    
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
# tell about the requesting client
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   181
    logmsg ("info", 2, "Connection from >$ipaddr< ($name) at port $port");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   183
    spawn sub {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   184
      my ($head, $hlen, $pos, $pegel, $typ, $siz, $nch, $nrec, $dat, $i, $j, $n, $llen); 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   185
      my ($watchpegel, $shmpegel);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   186
      my ($rin, $rout, $in, $line, $data_requested, $oversample);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   187
      my (@channels);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   188
      
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
#   to use stdio on writing to Client
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   190
      Client->autoflush();
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   191
      
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
#   Open Device 
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   193
      sysopen (DEV, "$dev", O_RDWR | O_NONBLOCK, 0666) or die("can't open $dev");
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   194
      
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
#   Bitmask to check for input on stdin
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   196
      $rin = "";
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   197
      vec($rin, fileno(Client), 1) = 1; 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   198
      
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
#   check for authorized hosts
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   200
      my $access = 'deny';
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   201
      $access = 'allow' if $authhosts{$ipaddr};
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   202
      $line = "<remote_host host=\"$ipaddr\" access=\"$access\">\n";
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   203
      logmsg ("info", 2, $line);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   204
      $len = length $line;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   205
      $offset = 0;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   206
      while ($len) {
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
	$written = syswrite (DEV, $line, $len, $offset);
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
	$len -= $written;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
	$offset += $written;
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   210
      }
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   211
      
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   212
      while ( 1 ) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   213
	$in = select ($rout=$rin, undef, undef, 0.0); # poll client
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
#     look for any Input from Client
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   215
	if ($in) {
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
#       exit on EOF
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   217
	  $len = sysread (Client, $line, $blksize) or exit;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   218
	  logmsg("info", 0, "got $len bytes: \"$line\""); 
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   219
	  $offset = 0;
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
#       copy request to device
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   221
	  while ($len) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   222
	    $written = syswrite (DEV, $line, $len, $offset);
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   223
	    $len -= $written;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   224
	    $offset += $written;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   225
	  }
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   226
	}
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   227
#     look for some output from device
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   228
	if ($len = sysread DEV, $stream, $blksize) {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   229
	  print Client $stream;
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   230
	} else {
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   231
	  select undef, undef, undef, 0.1; # calm down if nothing on device
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
	}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
      }
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   234
    };
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   235
    logmsg("info", 2, "spawned\n");
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   236
  }
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   237
  logmsg("info", 2, "server loop\n");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
sub spawn {
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
  my $coderef = shift;
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   242
  
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
  unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
    confess "usage: spawn CODEREF";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
  }
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
  my $pid; 
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
  if (!defined($pid = fork)) {
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   248
    logmsg ("notice", 2, "fork failed: $!");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
    return;
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
  } elsif ($pid) {
28
801dc7eabf51 IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents: 0
diff changeset
   251
    logmsg ("notice", 2, "Request $pid");
0
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
    return; # Parent
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
  }
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
# do not use fdup as in the original example
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
# open (STDIN, "<&Client") or die "Can't dup client to stdin";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
# open (STDOUT, ">&Client") or die "Can't dup client to stdout";
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
# STDOUT->autoflush();
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
  exit &$coderef();
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
}
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
05c992bf5847 trunk, tags und branches
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272