From 4211a9bc22f274107997189514031141b2664b94 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Mon, 17 Feb 2020 15:43:11 +0100 Subject: Make socket base path configurable --- p11p-daemon/config/sys.config | 1 + p11p-daemon/src/p11p_config.erl | 20 ++++++++++++++++++++ p11p-daemon/src/p11p_server_sup.erl | 15 +++++---------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/p11p-daemon/config/sys.config b/p11p-daemon/config/sys.config index e697383..c10ce49 100644 --- a/p11p-daemon/config/sys.config +++ b/p11p-daemon/config/sys.config @@ -3,6 +3,7 @@ {p11p, [ {loglevel, 3}, + %%{socket_dir, "/run/p11p"}, {proxyapp_bin_path, "/usr/lib/x86_64-linux-gnu/p11-kit/p11-kit-remote"}, {testing_drop_prob, 0}, {vtokens, diff --git a/p11p-daemon/src/p11p_config.erl b/p11p-daemon/src/p11p_config.erl index 3c2b641..12a90b9 100644 --- a/p11p-daemon/src/p11p_config.erl +++ b/p11p-daemon/src/p11p_config.erl @@ -13,6 +13,7 @@ module_env/1, nameof/1, proxyapp_bin_path/0, + socket_dir/0, testing_drop_prob/0, tokens/0, token_balance/1, @@ -37,6 +38,7 @@ -type token() :: #token{}. -record(state, { + socket_dir :: string(), proxyapp_bin_path :: string(), testing_drop_prob :: non_neg_integer(), tokens :: #{string() => token()} @@ -53,6 +55,8 @@ init(_Args) -> handle_call(proxyapp_bin_path, _From, S = #state{proxyapp_bin_path = Path}) -> {reply, Path, S}; +handle_call(socket_dir, _From, S = #state{socket_dir = Dir}) -> + {reply, Dir, S}; handle_call(testing_drop_prob, _From, S = #state{testing_drop_prob = P}) -> {reply, P, S}; handle_call(tokens, _From, State = #state{tokens = Tokens}) -> @@ -95,6 +99,9 @@ start_link() -> proxyapp_bin_path() -> gen_server:call(?MODULE, proxyapp_bin_path). +socket_dir() -> + gen_server:call(?MODULE, socket_dir). + testing_drop_prob() -> gen_server:call(?MODULE, testing_drop_prob). @@ -138,6 +145,9 @@ nameof(List) -> -define(PROXYAPP_DEFAULT, "/usr/local/libexec/p11-kit/p11-kit-remote"). init_state() -> #state{ + socket_dir = application:get_env(p11p, + socket_dir, + default_socket_dir()), proxyapp_bin_path = application:get_env(p11p, proxyapp_bin_path, ?PROXYAPP_DEFAULT), @@ -150,6 +160,10 @@ init_state() -> init_state(Filename) -> {ok, Config} = p11p_config_file:load_config(Filename), #state{ + socket_dir = p11p_config_file:get(Config, + string, + "socket_dir", + default_socket_dir()), proxyapp_bin_path = p11p_config_file:get(Config, string, "proxyapp_bin_path", @@ -204,6 +218,12 @@ new_module(Name, Path, Env) -> env = Env }. +geteuid() -> + %% TODO: Maybe find a POSIX library instead of invoking a shell? + list_to_integer(string:strip(os:cmd("/usr/bin/id -u"), right, $\n)). + +default_socket_dir() -> + "/run/user/" ++ integer_to_list(geteuid()) ++ "/p11p". %%% Unit tests %%% -include_lib("eunit/include/eunit.hrl"). diff --git a/p11p-daemon/src/p11p_server_sup.erl b/p11p-daemon/src/p11p_server_sup.erl index 263d183..11e1830 100644 --- a/p11p-daemon/src/p11p_server_sup.erl +++ b/p11p-daemon/src/p11p_server_sup.erl @@ -44,21 +44,16 @@ cleanup([Token|Tail]) -> cleanup(Tail). mkdir_socket_basepath() -> - EUID = integer_to_list(geteuid()), - Path = "/run/user/" ++ EUID ++ "/p11p/", - ok = case file:make_dir(Path) of + Dir = p11p_config:socket_dir(), + ok = case file:make_dir(Dir) of ok -> ok; {error, eexist} -> ok; Err -> - lager:error("~s: unable to create directory: ~p", [Path, Err]), + lager:error("~s: unable to create directory: ~p", [Dir, Err]), err end, - Path. + Dir. -spec socket_path(string(), string()) -> string(). socket_path(BasePath, Name) -> - BasePath ++ Name ++ "-" ++ os:getpid(). - -geteuid() -> - %% TODO: Maybe find a POSIX library instead of invoking a shell? - list_to_integer(string:strip(os:cmd("/usr/bin/id -u"), right, $\n)). + BasePath ++ "/" ++ Name ++ "-" ++ os:getpid(). -- cgit v1.1