Re: Pipes on Wince (I have them), and device auto-loading.



You could look at the source code for the Telnet server in Windows CE. It
uses an external service/driver to which it sets SetStdioPathW() for the
appropriate paths for each child process. That driver then has a means of
communicating back with the application so that the output of some program
that you run via Telnet can be sent over the network to the Telnet client
who ordered the run. What happens when it does a run is this:

oldstdin = GetStdioPath( stdin );
oldstdout = GetStdioPathW( stdout );
oldstderr = GetStdioPathW( stderr );

SetStdioPathW( stdin );
SetStdioPathW( stdout );
SetStdioPathW( stderr );

CreateProcess( child );

SetStdioPathW( oldstdin );
etc.

This driver handles accepting the right connections from Telnet clients,
figuring out when a command needs to be run, etc. It's the best reference I
can think of to how to handle this, although it also gets you involved in
network I/O handling, which is unfortunate for the clarity of the code.

Paul T.


"Pedro Alves" <alves.ped@xxxxxxxxx> wrote in message
news:1180953912.671497.215370@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi guys,

I'm new to this group.

(If you're quick browsing mode, I know pipes aren't implemented
on WinCE, read on.)

I needed to somehow redirect a child process' stdout/stderr/stdin to
a parent process. I don't have the source of this child process. In
fact, the tool I'm building should be able to launch any child
process.

At first I tryed redirecting to temp files and use
GetStdioPathW/SetStdioPathW. Somehow it wasn't working correctly,
because the parent process wouldn't see the temp file grow passed
the few bytes (I think the parent would only see the first WriteFile
the child does.). I even tryied closing/reopening the file in a
loop waiting for it to grow, but no can do. It just wouldn't.

The second try I've done, is to implement a new stream device that
implements a pipe. I implemented a lib (hereafter called PipeLib)
that hides the device from the user and exposes an interface
similar to native WinNT/9x pipes, namelly, CreatePipe.

To get child's stdio redirected to the parent using PipeLib, the
parent will do:

/* showing only stdout for simplicity sake. */

HANDLE rd, wr;
WCHAR pipe_name[MAX_PATH];
WCHAR old_stdout_path[MAX_PATH];

/* create it - CreatePipe returns BOOL. */
CreatePipe (&rd, &wr, NULL, 0);
GetStdioPathW (1, old_stdout_path, &len);

/* get internal pipe name - this is really the device's name, like
PD00: */
GetPipeName (rd, pipe_name);

/* set it in the parent so the child inherits it */
SetStdioPathW (1, pipe_name);

CreateProcess (...);

/* restore */
SetStdioPathW (1, old_stdout_path);

while (1)
{
DWORD read;
/* read from the read end of the pipe */
/* block waiting for input */
if (!ReadFile (rd, ..., &read))
return;

if (read)
{
/* do whatever with the data. */
}
}


It all works well. I just have one issue, that is:

I'm using ActivateDevice to load the driver. Since I want to hide
the device nature of the pipe from the user, and I want to reuse
closed pipe devices, I don't pass the HANDLE returned by
ActivateDevice to the pipe creator (the CreatePipe caller). I would
like to somehow have the device auto deactivate when the last
file handle to it is closed.

Currently, I'm keeping a device internal reference count, and when
it reaches 0, I post a Windows message to an internal window that
is implemented in PipeLib. In PipeLib there is a thread that waits
for these messages and DeactivateDevice's the device accordingly.

This deactivating only works if the child closes all the pipes
handles before the parent. If the parent dies, the device will leak.

I'm a WinCE device writing newbie, sorry if I missed something
obvious.

The real solution I'm after is auto device unloading. Any ideas?

B.t.w: I'll release the Pipe driver and lib as BSD or similar once
these issues are fixed.

Cheers,
Pedro Alves



.



Relevant Pages

  • Re: continuous write to hard disk
    ... | herd" problem and the only cause for scheduling latency with the O ... Parent sets up some shared memory. ... Parent sets up pipes and forks child B. ... Child B blocks in readon pipe for message from parent. ...
    (comp.os.linux.development.system)
  • Re: Python, Tkinter and popen problem
    ... but that is not `both way': popen connects the parent to the child ... The pipe works one way: from the child to the parent ... A child process always inherits stdin, stdout and stderr from the parent ...
    (comp.lang.python)
  • Re: Forking
    ... On Wednesday 03 March 2004 20:47, Price, Jason generously enriched virtual ... pipe takes two filehandles: ... inherit filehandles of the parent process as a copy. ... waits for the child to teminate and returns its pid once it died. ...
    (perl.beginners)
  • Re: Questions about perl daemons with child processes and open files / signals
    ... but not when used in the parent? ... process the pipe in the child process. ... The parent sits in the waitpid, waiting for the child to exit. ...
    (comp.lang.perl.misc)
  • pipe from child to parent: the parent exits, but the child does not
    ... Each child writes then the 3 fetched int values ... via the pipe to the parent. ... So I have prepared a simple test case program, which forks NKIDS ...
    (comp.unix.programmer)