Permalink
Browse files

Return POSIX atom on error from forked setuid bin

Add a function to the NIF returning an atom from erl_errno_id().

Do not distinguish between procket_cmd and procket errors. Return the
same tuple {error, posix()} on errors.

Return the POSIX value in hackish way: by echo'ing the return code
in os:cmd/1. Otherwise, sudo may return failure (for example, if the
user has not placed the procket command in sudoers.conf. sudo exits
with a return value of EPERM in this case).
  • Loading branch information...
1 parent d4c76dd commit 7a640702ee0d38ae1c871418c57ee94347083ccc @msantos committed May 14, 2011
Showing with 31 additions and 11 deletions.
  1. +1 −2 c_src/Makefile.cmd
  2. +18 −1 c_src/procket.c
  3. +1 −3 c_src/procket_cmd.c
  4. +1 −1 rebar.config
  5. +10 −4 src/procket.erl
View
@@ -9,5 +9,4 @@ dirs:
-@mkdir -p $(CMD_DIR)
compile:
- $(CC) $(ARCH) -g -Wall -o $(CMD_PATH) -L. procket_cmd.c -lancillary
-
+ $(CC) -m$(ERLANG_ARCH) -g -Wall -o $(CMD_PATH) -L. procket_cmd.c -lancillary
View
@@ -580,6 +580,21 @@ nif_memcpy(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
return atom_ok;
}
+
+/* 0: errno */
+ static ERL_NIF_TERM
+nif_errno_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ int err = -1;
+
+
+ if (!enif_get_int(env, argv[0], &err))
+ return enif_make_badarg(env);
+
+ return enif_make_atom(env, erl_errno_id(err));
+}
+
+
static ERL_NIF_TERM
error_tuple(ErlNifEnv *env, int errnum)
{
@@ -623,7 +638,9 @@ static ErlNifFunc nif_funcs[] = {
{"alloc", 1, nif_alloc},
{"memcpy", 2, nif_memcpy},
- {"buf", 1, nif_buf}
+ {"buf", 1, nif_buf},
+
+ {"errno_id", 1, nif_errno_id}
};
ERL_NIF_INIT(procket, nif_funcs, load, NULL, NULL, NULL)
View
@@ -368,10 +368,8 @@ error_result(PROCKET_STATE *ps, int err)
{
if (ps->verbose > 0)
(void)fprintf(stderr, "%s", strerror(err));
- else
- (void)fprintf(stderr, "%d", err);
- exit(-err);
+ exit(err);
}
View
@@ -5,5 +5,5 @@
{"LDFLAGS", "$LDFLAGS -Lc_src -lancillary"}
]}.
-{port_pre_script, {"make ARCH=-m$ERLANG_ARCH -C c_src -f Makefile.ancillary -f Makefile.cmd", ""}}.
+{port_pre_script, {"make -C c_src -f Makefile.ancillary -f Makefile.cmd", ""}}.
{port_cleanup_script, "rm c_src/*.a c_src/*.o priv/procket"}.
View
@@ -49,7 +49,9 @@
alloc/1,
buf/1,
- memcpy/2
+ memcpy/2,
+
+ errno_id/1
]).
-export([
unix_path_max/0,
@@ -136,6 +138,9 @@ write(_,_) ->
setsockopt(_,_,_,_) ->
erlang:error(not_implemented).
+errno_id(_) ->
+ erlang:error(not_implemented).
+
dev(Dev) when is_list(Dev) ->
open(0, [{dev, Dev}]).
@@ -159,13 +164,13 @@ open1(Port, Options) ->
{ok, Sockfd} = fdopen(Pipe),
Cmd = make_args(Port, Options),
case os:cmd(Cmd) of
- [] ->
+ "0" ->
FD = fdget(Sockfd),
cleanup(Sockfd, Pipe, Options),
FD;
Error ->
cleanup(Sockfd, Pipe, Options),
- {error, {procket_cmd, Error}}
+ {error, errno_id(list_to_integer(Error))}
end.
cleanup(Sockfd, Pipe, Options) ->
@@ -203,7 +208,8 @@ make_args(Port, Options) ->
get_switch(IP) ++ ":" ++ integer_to_list(Port)
end,
proplists:get_value(progname, Options, "sudo " ++ progname()) ++ " " ++
- string:join([ get_switch(Arg) || Arg <- Options, element(1,Arg) /= ip ], " ") ++ Bind.
+ string:join([ get_switch(Arg) || Arg <- Options, element(1,Arg) /= ip ], " ") ++ Bind ++
+ " > /dev/null 2>&1; echo -n $?".
get_switch({pipe, Arg}) ->
"-p " ++ Arg;

0 comments on commit 7a64070

Please sign in to comment.