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