Skip to content
Browse files

Return posix error from init/1

Instead of crashing, return errno from init. Since open is a wrapper
around start_link, the calling process will crash too.
  • Loading branch information...
1 parent 541c20d commit 8c10951b3d37c7f1d2302d52416a8de83d656b09 @msantos committed
Showing with 38 additions and 14 deletions.
  1. +3 −3 README.md
  2. +25 −9 src/gen_icmp.erl
  3. +10 −2 test/gen_icmp_tests.erl
View
6 README.md
@@ -15,9 +15,9 @@ version. If you just need a simple example of sending a ping, also see:
## EXPORTS
- open() -> {ok, Socket} | {error, Error}
- open(SocketOptions) -> {ok, Socket} | {error, Error}
- open(RawOptions, SocketOptions) -> {ok, Socket} | {error, Error}
+ open() -> {ok, Socket}
+ open(SocketOptions) -> {ok, Socket}
+ open(RawOptions, SocketOptions) -> {ok, Socket}
Types Socket = pid()
RawOptions = [ RawOption | {setuid,boolean()} ]
View
34 src/gen_icmp.erl
@@ -53,7 +53,7 @@
parse/1, parse/2
]).
--export([start_link/2]).
+-export([start_link/2, start/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
@@ -172,6 +172,13 @@ start_link(RawOpts, SockOpts) ->
Pid = self(),
gen_server:start_link(?MODULE, [Pid, RawOpts, SockOpts], []).
+start(RawOpts, SockOpts) ->
+ Pid = self(),
+ case gen_server:start(?MODULE, [Pid, RawOpts, SockOpts], []) of
+ {ok, Pid} -> {ok, Pid};
+ {error, Error} -> Error
+ end.
+
init([Pid, RawOpts, SockOpts]) ->
process_flag(trap_exit, true),
@@ -180,20 +187,29 @@ init([Pid, RawOpts, SockOpts]) ->
true -> {'ipv6-icmp', inet6}
end,
- {ok, FD} = case proplists:get_value(setuid, RawOpts, true) of
+ Result = case proplists:get_value(setuid, RawOpts, true) of
true ->
procket:open(0, RawOpts ++ [{protocol, Protocol}, {type, raw}, {family, Family}]);
false ->
procket:socket(Family, raw, Protocol)
end,
- {ok, Socket} = gen_udp:open(0, SockOpts ++ [binary, {fd, FD}, Family]),
- {ok, #state{
- family = Family,
- pid = Pid,
- raw = FD,
- s = Socket
- }}.
+ init_1(Pid, Family, SockOpts, Result).
+
+init_1(Pid, Family, SockOpts, {ok, FD}) ->
+ case gen_udp:open(0, SockOpts ++ [binary, {fd, FD}, Family]) of
+ {ok, Socket} ->
+ {ok, #state{
+ family = Family,
+ pid = Pid,
+ raw = FD,
+ s = Socket
+ }};
+ Error ->
+ Error
+ end;
+init_1(_Pid, _Family, _SockOpts, Error) ->
+ {stop, Error}.
handle_call(close, {Pid,_}, #state{pid = Pid, s = Socket} = State) ->
{stop, normal, gen_udp:close(Socket), State};
View
12 test/gen_icmp_tests.erl
@@ -81,8 +81,16 @@ reuse_socket_test() ->
% beam executable will need to have the appropriate permissions to open
% a raw socket.
nosetuid_socket_test() ->
- {ok, Socket} = gen_icmp:open([{setuid, false}], []),
-
+ case gen_icmp:start([{setuid, false}], []) of
+ {error,eperm} ->
+ error_logger:info_report([{nosetuid_socket_test,
+ "beam not running with appropriate privileges, skipping"}]),
+ ok;
+ {ok, Socket} ->
+ nosetuid_socket_test_1(Socket)
+ end.
+
+nosetuid_socket_test_1(Socket) ->
[{ok,"www.google.com",{_,_,_,_},{_,_,_,_}, {{_,_,_}, <<" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJK">>}},
{ok,"127.0.1.1",{127,0,1,1},{127,0,1,1}, {{_,_,_}, <<" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJK">>}}] =
gen_icmp:ping(Socket, ["127.0.1.1", "www.google.com"], []),

0 comments on commit 8c10951

Please sign in to comment.
Something went wrong with that request. Please try again.