Skip to content

Commit

Permalink
Fix duplicate command line options
Browse files Browse the repository at this point in the history
Specifying the chroot and timeout options would result in duplicate
options being passed to the epcap executable.
  • Loading branch information
msantos committed Jan 3, 2015
1 parent cd3cbed commit 4d82172
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/epcap.app.src
@@ -1,6 +1,6 @@
{application, epcap, [
{description, "libpcap port"},
{vsn, "0.7.6"},
{vsn, "0.7.7"},
{registered, []},
{applications, [
kernel,
Expand Down
43 changes: 27 additions & 16 deletions src/epcap.erl
Expand Up @@ -62,18 +62,15 @@ stop(Pid) ->

init([Pid, Options]) ->
process_flag(trap_exit, true),
Chroot = case proplists:get_value(chroot, Options) of
undefined ->
filename:join([basedir(), "tmp"]);
Value ->
Value
end,
ok = filelib:ensure_dir(filename:join(Chroot, "dummy")),
Timeout = case os:type() of
{unix, linux} -> 0;
_ -> 500
end,
Cmd = getopts(Options ++ [{chroot, Chroot}, {timeout, Timeout}]),
Options1 = setopts([
{chroot, filename:join([basedir(), "tmp"])},
{timeout, timeout()}
], Options),
ok = filelib:ensure_dir(filename:join(proplists:get_value(
chroot,
Options1
), ".")),
Cmd = string:join(getopts(Options1), " "),
Port = open_port({spawn, Cmd}, [{packet, 2}, binary, exit_status]),
{ok, #state{pid = Pid, port = Port}}.

Expand Down Expand Up @@ -122,18 +119,26 @@ handle_info(Info, State) ->
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
setopts([], Options) ->
proplists:compact(Options);
setopts([{Key,Val}|Rest], Options) ->
case proplists:get_value(Key, Options) of
undefined ->
setopts(Rest, [{Key,Val}|Options]);
_ ->
setopts(Rest, Options)
end.

getopts(Options) when is_list(Options) ->
Exec = exec(Options),
Progname = proplists:get_value(progname, Options, progname()),
Cpu_affinity = cpu_affinity(Options),
Filter = proplists:get_value(filter, Options, ""),

Switches0 = [ optarg(Opt) || Opt <- proplists:compact(Options) ],
Switches0 = [ optarg(Opt) || Opt <- Options ],
Switches = Switches0 ++ [quote(Filter)],

Cmd = [ N || N <- [Exec, Cpu_affinity, Progname|Switches], N /= ""],

string:join(Cmd, " ").
[ N || N <- [Exec, Cpu_affinity, Progname|Switches], N /= ""].

optarg({buffer, Arg}) -> switch("b", Arg);
optarg({chroot, Arg}) -> switch("d", Arg);
Expand Down Expand Up @@ -198,3 +203,9 @@ cpu_affinity(Options) ->
CPUs ->
"taskset -c " ++ CPUs
end.

timeout() ->
case os:type() of
{unix, linux} -> 0;
_ -> 500
end.

0 comments on commit 4d82172

Please sign in to comment.