From 9b68d3d08f509c130cd2ce8e8102f2cc9cee7581 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 17 Jan 2024 14:57:22 +0100 Subject: [PATCH] Allow setting libdir through env This is needed when running the gen_socket app under an escript, since the escript bundle doesn't contain a priv directory with the libs. --- src/gen_socket.erl | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/gen_socket.erl b/src/gen_socket.erl index 37eb083..e31bb8d 100644 --- a/src/gen_socket.erl +++ b/src/gen_socket.erl @@ -89,8 +89,23 @@ -type posix_error() :: file:posix(). +get_libdir() -> + case application:get_env(gen_socket, libdir) of + {ok, LibDir} -> + LibDir; + undefined -> + CodePath = filename:dirname(code:which(?MODULE)), + case filelib:is_dir(CodePath) of + true -> + filename:join([CodePath, "..", "priv", "lib"]); + false -> + error_logger:error_msg("Default Libdir ~p is not a directory, if you are running an escript set gen_socket application env 'libdir'", [CodePath]), + error({load_nif, CodePath}) + end + end. + init() -> - LibDir = filename:join([filename:dirname(code:which(?MODULE)), "..", "priv", "lib"]), + LibDir = get_libdir(), %% load our nif library case erlang:load_nif(filename:join(LibDir, "gen_socket_nif"), 0) of @@ -635,7 +650,7 @@ decode_sockopt(?SOL_SOCKET, ?SO_LINGER, < OptValue. - + %% Protocol family (aka domain) family_to_int(unspec) -> 0;