Erlang/OTP Forums

Author Message

<  User Contributions  ~  Eavesdropping on console io

vladdu
Posted: Fri Nov 03, 2006 12:45 pm Reply with quote
User Joined: 28 Feb 2005 Posts: 397 Location: Gothenburg, Sweden
Following my own question about this, and using my lunch break to the full, I have implemented a module that at runtime modifies the io server to send copies of all requests to a specified process.

The implementation uses Smerl (thanks Yariv! Smile) and will be included in Erlide.

Usage:
*stdio_handler:install(Handler) when is_atom(Handler)
Handler has to be a process' name because we can't get an abstract form for pids...
*stdio_handler:uninstall()

We change the second clause of io:request/2 from
Code:
 request(Pid, Request) when pid(Pid) ->
     Mref = erlang:monitor(process,Pid),
     Pid ! {io_request,self(),Pid,io_request(Pid, Request)},
     wait_io_mon_reply(Pid,Mref);

with
Code:
 request(Pid, Request) when pid(Pid) ->
     case proccess_info(Pid, group_leader} of
         {group_leader, Pid} ->
             catch *Handler* ! {request, Pid, Request, self(), erlang:now()};
         _ ->
             ok
     end,
     Mref = erlang:monitor(process,Pid),
     Pid ! {io_request,self(),Pid,io_request(Pid, Request)},
     wait_io_mon_reply(Pid,Mref);

We only want to capture standard_io output, i.e. where the group_leader is its own group_leader. So now your process will get notified when something gets read or written.

Of course, this is very brittle (changing io:request/2 might break everything), but I can live with it.

best regards,
Vlad



stdio_handler.erl
 Description:

Download
 Filename:  stdio_handler.erl
 Filesize:  2.12 KB
 Downloaded:  1450 Time(s)

View user's profile Send private message
lokifirebringer
Posted: Mon Aug 03, 2009 9:52 pm Reply with quote
Joined: 29 Jul 2009 Posts: 9
I'm confused on how to get this to work; could you show an example?
View user's profile Send private message
vladdu
Posted: Fri Aug 21, 2009 6:50 am Reply with quote
User Joined: 28 Feb 2005 Posts: 397 Location: Gothenburg, Sweden
Hi!

stdio_handler was a quick hack, it worked until the next Erlang version only because it depends on the exact code in the io module.

We are using a "normal" remote shell now, with the disadvantage that output that goes directly to stdout must be caught in a different way (by watching the file descriptor, if available).

Another solution (untested) is to use an undocumented (and probably unsupported) option
> erl -user myuser
that lets you replace the 'user' process with your own implementation of an io server (see the user.erl module for reference). This way you can do whatever you want with the output data. The only data you can't catch that way is that written with erlang:display().

I hope this helps.

best regards,
Vlad
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum