author | Florian Pose <fp@igh-essen.com> |
Tue, 12 Jan 2010 23:42:35 +0100 | |
branch | kernel2.6 |
changeset 1767 | 7993a7c9eadc |
parent 28 | 801dc7eabf51 |
child 195 | 674071846ee3 |
permissions | -rwxr-xr-x |
0 | 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 | 23 |
# Multithreaded Server |
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 | 28 |
# |
29 |
# works with read/write on a device-file |
|
30 |
# |
|
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 | 33 |
# $RCSfile: msrserv.pl,v $ |
34 |
# |
|
28
801dc7eabf51
IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents:
0
diff
changeset
|
35 |
#------------------------------------------------------------ |
0 | 36 |
|
37 |
require 5.002; |
|
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 | 40 |
use Socket; |
41 |
use Carp; |
|
42 |
use FileHandle; |
|
43 |
use Getopt::Std; |
|
44 |
||
45 |
use Sys::Syslog qw(:DEFAULT setlogsock); |
|
46 |
||
47 |
use vars qw ( |
|
48 |
$self $pid $dolog $port $dev %opts $selfbase |
|
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 | 51 |
$authfile %authhosts |
52 |
); |
|
53 |
||
54 |
||
55 |
# Do logging to local syslogd by unix-domain socket instead of inetd |
|
56 |
setlogsock("unix"); |
|
57 |
||
58 |
# Prototypes and some little Tools |
|
59 |
sub spawn; |
|
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 | 63 |
# print STDERR "daemon|$level", @text, "\n" if $dolog; |
64 |
} |
|
65 |
sub out { |
|
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 | 68 |
unlink "$selfbase.pid"; |
69 |
exit 0; |
|
70 |
} |
|
71 |
||
72 |
sub help { |
|
73 |
print "\n usage: $0 [-l og] [-h elp] [-p port] [-d device]\n"; |
|
74 |
exit; |
|
75 |
} |
|
76 |
||
77 |
# Process Options |
|
78 |
%opts = ( |
|
79 |
"l" => 1, |
|
80 |
"h" => 0, |
|
81 |
"p" => 2345, |
|
82 |
"d" => "/dev/msr" |
|
83 |
); |
|
84 |
||
85 |
getopts("lhp:d:", \%opts); |
|
86 |
||
87 |
help if $opts{"h"}; |
|
88 |
||
89 |
( $self = $0 ) =~ s+.*/++ ; |
|
90 |
( $selfbase = $self ) =~ s/\..*//; |
|
91 |
$log = "$selfbase.log"; |
|
92 |
$dolog = $opts{"l"}; |
|
93 |
$port = $opts{"p"}; |
|
94 |
$dev = $opts{"d"}; |
|
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 | 98 |
|
99 |
# Start logging |
|
100 |
openlog($self, 'pid'); |
|
101 |
||
102 |
# Flush Output, dont buffer |
|
103 |
$| = 1; |
|
104 |
||
105 |
# first fork and run in background |
|
106 |
if ($pid = fork) { |
|
107 |
# open LOG, ">$log" if $dolog; |
|
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 | 110 |
exit 0; |
111 |
} |
|
112 |
||
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 | 115 |
print PID "$$\n"; |
116 |
close PID; |
|
117 |
||
118 |
# Cleanup on exit (due to kill -TERM signal) |
|
119 |
$SIG{TERM} = \&out; |
|
120 |
||
121 |
# We use streams |
|
122 |
my $proto = getprotobyname('tcp'); |
|
123 |
||
124 |
# Open Server socket |
|
125 |
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; |
|
126 |
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) |
|
127 |
or die "setsocketopt: $!"; |
|
128 |
bind (Server, sockaddr_in($port, INADDR_ANY)) |
|
129 |
or die "bind: $!"; |
|
130 |
listen (Server, SOMAXCONN) |
|
131 |
or die "listen: $!"; |
|
132 |
||
133 |
%authhosts = (); |
|
134 |
# get authorized hosts |
|
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 | 137 |
while (<AUTH>) { |
138 |
chomp; |
|
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 | 144 |
} |
145 |
close (AUTH); |
|
146 |
||
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 | 151 |
my $paddr; |
152 |
||
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 | 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 | 162 |
} |
163 |
||
164 |
# also all sub-processes should wait for their children |
|
165 |
$SIG{CHLD} = \&REAPER; |
|
166 |
||
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 | 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 | 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 | 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 | 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 | 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 | 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 | 207 |
$written = syswrite (DEV, $line, $len, $offset); |
208 |
$len -= $written; |
|
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 | 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 | 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 | 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 | 232 |
} |
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 | 238 |
} |
239 |
||
240 |
sub spawn { |
|
241 |
my $coderef = shift; |
|
28
801dc7eabf51
IPIPE,floatpoint,rtlib
Wilhelm Hagemeister <hm@igh-essen.com>
parents:
0
diff
changeset
|
242 |
|
0 | 243 |
unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') { |
244 |
confess "usage: spawn CODEREF"; |
|
245 |
} |
|
246 |
my $pid; |
|
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 | 249 |
return; |
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 | 252 |
return; # Parent |
253 |
} |
|
254 |
||
255 |
# do not use fdup as in the original example |
|
256 |
# open (STDIN, "<&Client") or die "Can't dup client to stdin"; |
|
257 |
# open (STDOUT, ">&Client") or die "Can't dup client to stdout"; |
|
258 |
# STDOUT->autoflush(); |
|
259 |
exit &$coderef(); |
|
260 |
} |
|
261 |
||
262 |
||
263 |
||
264 |
||
265 |
||
266 |
||
267 |
||
268 |
||
269 |
||
270 |
||
271 |
||
272 |