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