Can you detect that a process is waiting on stdin?

Can you detect that a process is waiting on stdin?

am 24.11.2007 05:10:37 von Thrill5

I have a Perl library that I wrote that I use to manage an application that
we have. The app has about 100 or so CLI's that you can use to get data
into the app, get data out, produce reports etc. Anyway, when the CLI's are
run, stdout and stderr are redirected to a file which I then parse for
informational and error messages after the CLI completes. Today I ran into
a situation where I did not pass all of the correct parameters and the CLI
then prompted for the required parameter. My Perl program then just waited
forever since it was waiting for the output that never came. Yes this was a
logic problem in my Perl program, but I would a way to detect this and end
the Perl program and report the error (this has happened before.) I can't
set an alarm and catch a signal for several reasons; the first is that the
module needs to be able to run on Win32 systems running Perl 5.6 which does
not support SIGALRM, and second, some of the CLI's can run for extremely
long periods of time (hours) while the CLI processes the data. The CLI's
are run from Perl using the "system" function.

Is there some way that I can hook into STDIN and then detect when the CLI is
waiting for input? If this is possible, then all I need to do is send it a
carriage return and it will then exit because not all the correct parameters
were passed. I will be able to then parse the error message and exit the
program. I don't think there is, but I figured I would ask the question
anyway. My other alternative is to add some more logic to ensure that all
the required parameters are there before invoking the CLI's. I don't want
to go down this path if at all possible because each CLI has different
required parameters, and some have situations where specifying certain
switches and parameters then require other switches and parameters and it
would get pretty ugly. I guess I could also redirect STDIN from a file that
has a bunch of CR's in it as well, but then I would need to create the file
(to ensure that it exists) and delete it when I'm done. Please keep in mind
that a solution must be able to run on Win32 systems.

Thanks!

Re: Can you detect that a process is waiting on stdin?

am 24.11.2007 20:31:38 von Charles DeRykus

On Nov 23, 8:10 pm, "Thrill5" wrote:
> I have a Perl library that I wrote that I use to manage an application that
> we have. The app has about 100 or so CLI's that you can use to get data
> into the app, get data out, produce reports etc. Anyway, when the CLI's are
> run, stdout and stderr are redirected to a file which I then parse for
> informational and error messages after the CLI completes. Today I ran into
> a situation where I did not pass all of the correct parameters and the CLI
> then prompted for the required parameter. My Perl program then just waited
> forever since it was waiting for the output that never came. Yes this was a
> logic problem in my Perl program, but I would a way to detect this and end
> the Perl program and report the error (this has happened before.) I can't
> set an alarm and catch a signal for several reasons; the first is that the
> module needs to be able to run on Win32 systems running Perl 5.6 which does
> not support SIGALRM, and second, some of the CLI's can run for extremely
> long periods of time (hours) while the CLI processes the data. The CLI's
> are run from Perl using the "system" function.
>
> Is there some way that I can hook into STDIN and then detect when the CLI is
> waiting for input? If this is possible, then all I need to do is send it a
> carriage return and it will then exit because not all the correct parameters
> were passed. I will be able to then parse the error message and exit the
> program. I don't think there is, but I figured I would ask the question
> anyway. My other alternative is to add some more logic to ensure that all
> the required parameters are there before invoking the CLI's. I don't want
> to go down this path if at all possible because each CLI has different
> required parameters, and some have situations where specifying certain
> switches and parameters then require other switches and parameters and it
> would get pretty ugly. I guess I could also redirect STDIN from a file that
> has a bunch of CR's in it as well, but then I would need to create the file
> (to ensure that it exists) and delete it when I'm done. Please keep in mind
> that a solution must be able to run on Win32 systems.
>

You didn't mention if the CLI's themselves could be tweaked... If so,
perhaps you could pass an argument (eg, 'perl') to them from the
system call. The CLI's, detecting a perl invoker, could error out and
exit rather than prompting.

--
Charles DeRykus

Re: Can you detect that a process is waiting on stdin?

am 25.11.2007 00:53:29 von xhoster

"Thrill5" wrote:

> Is there some way that I can hook into STDIN and then detect when the CLI
> is waiting for input? If this is possible, then all I need to do is send
> it a carriage return and it will then exit because not all the correct
> parameters were passed.

If the CLI is truly trying to read from STDIN, then if your perl program
closes stdin (or reopens it to /dev/null or whatever the Win32 equiv is)
before it starts the CLI, the program will inherit that STDIN and might
realize it has no real STDIN and so exit. On the other hand, it might be
trying to read from a tty rather than STDIN.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Re: Can you detect that a process is waiting on stdin?

am 25.11.2007 01:47:22 von Ben Morrow

Quoth xhoster@gmail.com:
> "Thrill5" wrote:
>
> > Is there some way that I can hook into STDIN and then detect when the CLI
> > is waiting for input? If this is possible, then all I need to do is send
> > it a carriage return and it will then exit because not all the correct
> > parameters were passed.
>
> If the CLI is truly trying to read from STDIN, then if your perl program
> closes stdin (or reopens it to /dev/null or whatever the Win32 equiv is)

nul. As in Cfile 2>file2";>. Yes, this means you
can't create a file called 'nul', in any directory. The DOS equivalent
of -d is C. Bleech.

> before it starts the CLI, the program will inherit that STDIN and might
> realize it has no real STDIN and so exit.

Hmmm. Inheriting filehandles is a tricky business under Win32. It may be
worth using Win32::Process rather than system to have more control over
what's going on. I stringly suspect Windows won't let you start a
console process with a closed STDIN, and that a process that tries to
read from nul when it's not expecting to will not behave sensibly.

> On the other hand, it might be trying to read from a tty rather than
> STDIN.

No such thing under Win32. I *believe* a process can ask to talk to the
'current console', which is kinda-equivalent to opening /dev/tty,
though.

Ben

Re: Can you detect that a process is waiting on stdin?

am 25.11.2007 07:44:14 von Thrill5

"Ben Morrow" wrote in message
news:q9tm15-g3i1.ln1@osiris.mauzo.dyndns.org...
>
> Quoth xhoster@gmail.com:
>> "Thrill5" wrote:
>>
>> > Is there some way that I can hook into STDIN and then detect when the
>> > CLI
>> > is waiting for input? If this is possible, then all I need to do is
>> > send
>> > it a carriage return and it will then exit because not all the correct
>> > parameters were passed.
>>
>> If the CLI is truly trying to read from STDIN, then if your perl program
>> closes stdin (or reopens it to /dev/null or whatever the Win32 equiv is)
>
> nul. As in Cfile 2>file2";>. Yes, this means you
> can't create a file called 'nul', in any directory. The DOS equivalent
> of -d is C. Bleech.
>
>> before it starts the CLI, the program will inherit that STDIN and might
>> realize it has no real STDIN and so exit.
>
> Hmmm. Inheriting filehandles is a tricky business under Win32. It may be
> worth using Win32::Process rather than system to have more control over
> what's going on. I stringly suspect Windows won't let you start a
> console process with a closed STDIN, and that a process that tries to
> read from nul when it's not expecting to will not behave sensibly.
>
>> On the other hand, it might be trying to read from a tty rather than
>> STDIN.
>
> No such thing under Win32. I *believe* a process can ask to talk to the
> 'current console', which is kinda-equivalent to opening /dev/tty,
> though.
>
> Ben
>

Thanks everyone for the replies and redirecting stdin from a charm!!!