Permalink
Browse files

Close any file descriptor

Re-use procket:close/2 to close any file descriptor. Consequently,
reverse the arguments to close/2, to take the fd first.
  • Loading branch information...
1 parent 5af88ac commit d7487f776e095c6c0341d08c74c226bf5d5f0bdb @msantos committed Jun 26, 2010
Showing with 20 additions and 11 deletions.
  1. +15 −9 c_src/procket.c
  2. +5 −2 src/procket.erl
View
24 c_src/procket.c
@@ -117,24 +117,29 @@ nif_poll(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}
-/* 0: path to socket
- * 1: file descriptor
+/* 0: file descriptor
+ * 1: path to socket
*/
static ERL_NIF_TERM
nif_close(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
- struct sockaddr_un sa = { 0 };
int sockfd = -1;
+ struct sockaddr_un sa = { 0 };
- if (enif_get_string(env, argv[0], sa.sun_path, sizeof(sa.sun_path), ERL_NIF_LATIN1) < 1)
- return enif_make_badarg(env);
+ switch (argc) {
+ case 2:
+ if (enif_get_string(env, argv[1], sa.sun_path, sizeof(sa.sun_path),
+ ERL_NIF_LATIN1) < 1)
+ return enif_make_badarg(env);
- if (!enif_get_int(env, argv[1], &sockfd))
- return enif_make_badarg(env);
+ if (unlink(sa.sun_path) < 0)
+ return error_message(env, "unlink", strerror(errno));
- if (unlink(sa.sun_path) < 0)
- return error_message(env, "unlink", strerror(errno));
+ case 1:
+ if (!enif_get_int(env, argv[0], &sockfd))
+ return enif_make_badarg(env);
+ }
(void)close(sockfd);
@@ -319,6 +324,7 @@ error_message(ErlNifEnv *env, char *err, char *msg)
static ErlNifFunc nif_funcs[] = {
{"open", 1, nif_open},
{"poll", 1, nif_poll},
+ {"close", 1, nif_close},
{"close", 2, nif_close},
{"bind", 2, nif_bind},
{"recvfrom", 2, nif_recvfrom},
View
7 src/procket.erl
@@ -31,7 +31,7 @@
-module(procket).
-export([
- init/0,open/1,poll/1,close/2,listen/1,listen/2,
+ init/0,open/1,poll/1,close/1,close/2,listen/1,listen/2,
recvfrom/2,sendto/4,bind/2,
ioctl/3,setsockopt/4
]).
@@ -49,6 +49,9 @@ on_load() ->
open(_) ->
erlang:error(not_implemented).
+close(_) ->
+ erlang:error(not_implemented).
+
poll(_) ->
erlang:error(not_implemented).
@@ -82,7 +85,7 @@ listen(Port, Options) when is_integer(Port), is_list(Options) ->
case os:cmd(Cmd) of
[] ->
FD = poll(Sockfd),
- close(proplists:get_value(pipe, Opt), Sockfd),
+ close(Sockfd, proplists:get_value(pipe, Opt)),
FD;
Error ->
{error, {procket_cmd, Error}}

0 comments on commit d7487f7

Please sign in to comment.