Kristian Rink wrote:
> hello all,...
>
> ...being writing a small script which is supposed to read data from
> a telephone system connected to the serial port of my linux box.
> basically, all the script does by now is to detach from the terminal
> and lurk in the background, reading from a pipe filled by cat:
>
>
> open(INPUT, "cat /dev/ttyS0 |")||die "cannot cat port..."; .
> .
> .
> while (<INPUT>)
> {
> print OUTPUT $_;
> filter_input($_,$parsetable,$keytable,$dataset);
> }
> .
> .
> .
>
>
> basically, it works more or less fine. I set up some signal handlers
> to catch SIGHUP and SIGTERM enabling me to reset or cleanly stop the
> process. unfortunately, while running through this loop, the script
> seems to be stuck for all eternity waiting to read input from
> <INPUT> if nothing happens, there... basically, i would like to have
> something like this
>
>
> while( (<INPUT>) && (!$caughtterm) && (!$caughthup)) {
> print OUTPUT $_;
> filter_input($_,$parsetable,$keytable,$dataset);
> }
>
> ($caughtterm and $caughthup are set by the handlers for SIGTERM /
> SIGHUP) to make the process interruptable / resetable even while
> waiting for input. anyhow, this does not work, for obvious reasons
> (nothing happens until any input is recieved). I was even for a
> short time thinking about polling the serial port in a loop similar
> to the one above but this probably also might end up in the script
> hanging and waiting for input until eternity ends...
The problem is that some system calls (like read(2)) are automatically
restarted after a signal is received. So your signal handler is being
called, but the read is restarted, so your program continues to wait.
You're installing your signal handler something like this, right?
$SIG{HUP} = sub { $caughthup = 1 };
Internally, Perl is calling your system's sigaction(2) function and
passing the SA_RESTART flag, which makes slow system calls restart.
In order to get around this, you can use the POSIX::sigaction
function directly. Install the above signal handler like this:
sub huphandler { $caughthup = 1 }
use POSIX ':signal_h';
sigaction SIGHUP, new POSIX::SigAction 'main::huphandler';
Since the SA_RESTART flag is left off, the read (<INPUT>) will be
interrupted by the signal (and will return undef, terminating the
loop).
>
> can anyone give me a hint how to make this serial input
> interruptable? hints on documentation to be read for this also are
> greatly appreciated,
Some docs are in:
perldoc perlvar (under %SIG)
perldoc perlipc
perldoc POSIX
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]