how to kill a port's external process?

Luke Gorrie <>
Thu Aug 9 23:23:27 CEST 2001


Luke Gorrie <> writes:

> I think a really nice and manly way to kill external ports would be to
> hack the "spawn driver" so that Erlang could make a driver ioctl to
> send a signal to a spawned program.

I couldn't resist and hacked in support for doing kill(2) on port
programs. I don't know if the approach is unwise for some reason but
it seems to work nicely.

The interface is a "spawn_drv" module in the kernel application with
these functions:

%% close_output(Port) -> ok | {error, Reason}
%%
%% Close the pipe from Erlang to the port's stdin

%% send_signal(Port, Signal) -> ok | {error, Reason}
%% Signal = hup | kill | term
%%
%% Send a Unix signal to the port with kill(2)

I was lazy and just defined 3 signals, but it's easy to add more.

Example:

  1> P = erlang:open_port({spawn, "/bin/cat"}, [exit_status]).
  #Port<0.7>
  2> flush().
  ok
  3> spawn_drv:send_signal(P, kill).
  ok
  4> flush().
  Shell got {#Port<0.7>,{exit_status,137}}
  ok

Patch against R7B-3 attached, I hope I built it properly.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: spawndrv.patch
Type: text/x-patch
Size: 5842 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20010809/3dcda4aa/attachment.bin>


More information about the erlang-questions mailing list