Usage of BIF port_control/3?

Peter H|gfeldt peter@REDACTED
Mon Feb 28 09:15:54 CET 2000



On Mon, 28 Feb 2000, Scott Lystig Fritchie wrote:

> I hope this isn't a "I didn't dig quite deep enough" question...
> 
> ... I'm experimenting with the BIF port_control/3.  One of the very
> few examples I can find is in inet.erl:ctl_getstat/2, which seems
> pretty straightforward.  So I'm trying something like this:
> 
> sync_cmd2(Port, Cmd, Bytes, Rep) ->
>     case catch port_control(Port, Cmd, Bytes) of
> 	{'EXIT', _Stuff} -> io:format("XXX sync_cmd2 err clause 1\n"),
>                             {error, einval};
> 	[Rep|Data]  -> {ok, Data};
> 	_Error      -> io:format("XXX sync_cmd2 err clause 3: ~w\n", [_Error]),
>                        {error, einval}
>     end.
> 
> But it doesn't work.  All I get are matches in clause 3, _Error = [],
> which causes my experiment to reimplement inet:ll_name/1 to barf.
> 
> ll_name(Inet) -> 
>     case sync_cmd2(Inet, ?INET_REQ_NAME, [], ?INET_REP_NAME) of  
>         {ok, [P1, P0 | Addr]} ->
>             {ok, { bytes_to_ip(Addr), ?u16(P1, P0) }};
>         Error -> Error
>     end.    
> 
> If sync_cmd2(Port, Cmd, Bytes, Rep) starts out as ->
>     Port ! {self(), {command, [Cmd|Bytes]}},
>     receive
> 	{Port, {data, [Rep|T]}} -> {ok, T};
> 	%% ... and so on....
> 
> ... then it works.  Also, if I use sync_cmd2(Port, Cmd, Bytes, Rep) ->
>     port_command(Port, [Cmd|Bytes]),
>     receive 
>         {Port, {data, [Rep | T]}} -> {ok, T};
> 	%% ... and so on....
> 
> ... it also works.  Am I overlooking something simple?
> 
> -Scott
> 

Port ! {self(), {command, Bytes}} and port_command(Port, Bytes) are
equivalent, except that the second one can be used by any process, not
only the port controller.

The port_control/3 BIF only works for drivers that have implemented 
the control() function (see erl_driver.h for the ANSI prototype).

The file crypto.erl in the Crypto application uses port_control/3 for
the driver crypto_drv (see crypto_drv.c). Please, have a look.
 
/Peter

Peter Högfeldt, 		e-mail  : peter@REDACTED
Open Telecom Platform





More information about the erlang-questions mailing list