summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--p11p-daemon/config/sys.config3
-rw-r--r--p11p-daemon/src/p11p_config.erl12
-rw-r--r--p11p-daemon/src/p11p_remote_manager.erl97
-rw-r--r--p11p-daemon/src/p11p_sup.erl1
4 files changed, 106 insertions, 7 deletions
diff --git a/p11p-daemon/config/sys.config b/p11p-daemon/config/sys.config
index 93671ad..9cc3f6e 100644
--- a/p11p-daemon/config/sys.config
+++ b/p11p-daemon/config/sys.config
@@ -5,7 +5,8 @@
{"vtoken0",
[{modules, [
{"softhsm2", "/usr/lib/softhsm/libsofthsm2.so"},
- {"bogusmod_0_1", "/path/to/bogusmod_0_1"}
+ %%{"bogusmod_0_1", "/path/to/bogusmod_0_1"}
+ {"bogusmod_0", "/usr/lib/softhsm/libsofthsm2.so"}
]}]},
{"vtoken1",
[{modules, [
diff --git a/p11p-daemon/src/p11p_config.erl b/p11p-daemon/src/p11p_config.erl
index e9279f3..be7b0c7 100644
--- a/p11p-daemon/src/p11p_config.erl
+++ b/p11p-daemon/src/p11p_config.erl
@@ -6,7 +6,7 @@
%%-export([config/0]).
-export([nameof/1]).
-export([tokens/0]).
--export([modules_for_token/1]).
+-export([modules_for_token/1, module_path/1]).
%% Genserver callbacks.
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -43,12 +43,12 @@ tokens() ->
gen_server:call(?MODULE, tokens).
-spec modules_for_token(string()) -> [module()].
-modules_for_token(Token) ->
- gen_server:call(?MODULE, {modules_for_token, Token}).
+modules_for_token(TokName) ->
+ gen_server:call(?MODULE, {modules_for_token, TokName}).
-%% -spec module_path(string()) -> string().
-%% module_path(Module) ->
-%% Module#p11module.path.
+-spec module_path(p11module()) -> string().
+module_path(Module) ->
+ Module#p11module.path.
nameof(#token{name = Name}) ->
Name;
diff --git a/p11p-daemon/src/p11p_remote_manager.erl b/p11p-daemon/src/p11p_remote_manager.erl
new file mode 100644
index 0000000..281ae2b
--- /dev/null
+++ b/p11p-daemon/src/p11p_remote_manager.erl
@@ -0,0 +1,97 @@
+%% A remote manager is a gen_server for coordination of remotes for
+%% all tokens.
+
+%% Spawn one remote per configured p11p module per configured virtual
+%% token. Provide a lookup service for servers that need a remote to
+%% send a request to, by keeping track of which module is current for
+%% a given vtoken.
+
+-module(p11p_remote_manager).
+
+-behaviour(gen_server).
+
+%% API.
+-export([start_link/0]).
+-export([port_for_token/1]). % For servers.
+-export([p11init_done/1, timeout/0]). % For remotes.
+
+%% Genserver callbacks.
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
+ code_change/3]).
+
+%% Records and types.
+-record(token, {
+ p11init_done = false :: boolean(),
+ remotes :: [port()] % Active remote in hd().
+ }).
+
+-record(state, {
+ tokens :: #{string() => #token{}}
+ }).
+
+-define(P11KITREMOTE_PATH, "/home/linus/usr/libexec/p11-kit/p11-kit-remote").
+
+%% API implementation.
+-spec start_link() -> {ok, pid()} | {error, term()}.
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+port_for_token(_Token) ->
+ todo.
+p11init_done(_Done) ->
+ todo.
+timeout() ->
+ todo.
+
+%% Genserver callbacks.
+init([]) ->
+ {ok, #state{tokens = init_tokens(p11p_config:tokens())}}.
+
+handle_call(Request, _From, State) ->
+ lager:debug("Unhandled call: ~p~n", [Request]),
+ {reply, unhandled, State}.
+
+handle_cast(Request, State) ->
+ lager:debug("Unhandled cast: ~p~n", [Request]),
+ {noreply, State}.
+
+handle_info({Port, {exit_status, Status}}, State) ->
+ lager:info("~p: process exited with ~p", [Port, Status]),
+ {stop, child_exit, State};
+handle_info(Info, State) ->
+ lager:debug("Unhandled info: ~p~n", [Info]),
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVersion, State, _Extra) ->
+ {ok, State}.
+
+%% Private functions
+-spec init_tokens([any()]) -> #{string() => #token{}}.
+init_tokens(ConfTokens) ->
+ init_tokens(ConfTokens, #{}).
+init_tokens([], Acc)->
+ Acc;
+init_tokens([H|T], Acc)->
+ init_tokens(T, Acc#{p11p_config:nameof(H) => new_token(H)}).
+
+new_token(ConfToken) ->
+ Remotes = start_remotes(p11p_config:modules_for_token(p11p_config:nameof(ConfToken))),
+ #token{remotes = Remotes}.
+
+start_remotes(ConfModules) ->
+ start_remotes(ConfModules, []).
+start_remotes([], Acc) ->
+ lists:reverse(Acc);
+start_remotes([H|T], Acc) ->
+ ModPath = p11p_config:module_path(H),
+ Port = start_remote(ModPath),
+ start_remotes(T, [Port | Acc]).
+
+start_remote(ModPath) ->
+ Port = open_port({spawn_executable, ?P11KITREMOTE_PATH},
+ [stream, exit_status, {args, [ModPath, "-v"]}]),
+ lager:debug("~s: New port: ~p", [?P11KITREMOTE_PATH, Port]),
+ Port.
diff --git a/p11p-daemon/src/p11p_sup.erl b/p11p-daemon/src/p11p_sup.erl
index 6b00376..6a317c6 100644
--- a/p11p-daemon/src/p11p_sup.erl
+++ b/p11p-daemon/src/p11p_sup.erl
@@ -22,5 +22,6 @@ init([]) ->
{ok, {{rest_for_one, 1, 5},
[
?CHILD(p11p_config, worker),
+ ?CHILD(p11p_remote_manager, worker),
?CHILD(p11p_server_sup, supervisor)
]}}.