summaryrefslogtreecommitdiff
path: root/p11p-daemon/src
diff options
context:
space:
mode:
Diffstat (limited to 'p11p-daemon/src')
-rw-r--r--p11p-daemon/src/p11p.app.src17
-rw-r--r--p11p-daemon/src/p11p_app.erl17
-rw-r--r--p11p-daemon/src/p11p_config.erl110
-rw-r--r--p11p-daemon/src/p11p_defs.hrl11
-rw-r--r--p11p-daemon/src/p11p_sup.erl24
5 files changed, 179 insertions, 0 deletions
diff --git a/p11p-daemon/src/p11p.app.src b/p11p-daemon/src/p11p.app.src
new file mode 100644
index 0000000..d81c1f4
--- /dev/null
+++ b/p11p-daemon/src/p11p.app.src
@@ -0,0 +1,17 @@
+{application, p11p,
+ [{description, "PKCS #11 proxy"},
+ {vsn, "0.0-dev"},
+ {registered, []},
+ {mod, { p11p_app, []}}, % args end up in p11p_app:start/2
+ {applications,
+ [kernel,
+ stdlib,
+ lager
+ ]},
+ {env,[]},
+ {modules, []},
+
+ {maintainers, []},
+ {licenses, []},
+ {links, []}
+ ]}.
diff --git a/p11p-daemon/src/p11p_app.erl b/p11p-daemon/src/p11p_app.erl
new file mode 100644
index 0000000..ad48080
--- /dev/null
+++ b/p11p-daemon/src/p11p_app.erl
@@ -0,0 +1,17 @@
+-module(p11p_app).
+-behaviour(application).
+
+-export([start/2, stop/1]).
+
+-include("p11p_defs.hrl").
+
+start(_Type, _Args) -> % Args from application spec (key 'p11p'?).
+ lager:notice("p11p version ~s starting", [p11p:version()]),
+ {ok, Sup} = p11p_sup:start_link(),
+ {ok, Sup}.
+
+-spec stop([]) -> ok.
+stop(_State) ->
+ ok.
+
+%% Private.
diff --git a/p11p-daemon/src/p11p_config.erl b/p11p-daemon/src/p11p_config.erl
new file mode 100644
index 0000000..aa222e3
--- /dev/null
+++ b/p11p-daemon/src/p11p_config.erl
@@ -0,0 +1,110 @@
+-module(p11p_config).
+-behaviour(gen_server).
+
+%% API
+-export([start_link/0]).
+-export([tokens/0]).
+-export([modules_for_token/1]).
+
+%% Genserver callbacks.
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+%% Records and types.
+-include("p11p_defs.hrl").
+
+%% Genserver state.
+-record(state, { tokens :: [token()] }).
+
+%%%%%%%%%%%%%%%%%%%%
+%% API.
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+config() ->
+ gen_server:call(?MODULE, config).
+
+-spec tokens() -> [token()].
+tokens() ->
+ gen_server:call(?MODULE, tokens).
+
+-spec modules_for_token(token()) -> [module()].
+modules_for_token(Token) ->
+ gen_server:call(?MODULE, {modules_for_token, Token}).
+
+
+%%%%%%%%%%%%%%%%%%%%
+%% Genserver callbacks.
+init(_Args) ->
+ State = init_state(),
+ {ok, State}.
+
+handle_call(config, _From, State) ->
+ {reply, State, State};
+handle_call(tokens, _From, #state{tokens = Tokens} = State) ->
+ {reply, Tokens, State};
+handle_call({modules_for_token, Token}, _From, #state{tokens = Tokens} = State) ->
+ Reply = #p11module{name = "FIXME"},
+ {reply, Reply, State};
+handle_call(Request, _From, State) ->
+ lager:warning("Unhandled call: ~p", [Request]),
+ {reply, unhandled, State}.
+
+handle_cast(Message, State) ->
+ lager:warning("Unhandled cast: ~p", [Message]),
+ {noreply, State}.
+
+handle_info(Info, State) ->
+ lager:warning("Unhandled info: ~p", [Info]),
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVersion, State, _Extra) ->
+ {ok, State}.
+
+%%%%%%%%%%%%%%%%%%%%
+%% Private.
+
+init_state() ->
+ #state {
+ tokens = init_tokens(application:get_env(p11p, group, []))
+ }.
+
+init_tokens(Tokens) ->
+ [new_token(T) || T <- Tokens].
+
+-spec new_token({string(), [tuple()]}) -> token().
+new_token({Name, Settings}) ->
+ #token{
+ name = Name,
+ modules = [new_module(M) || M <- proplists:get_value(modules, Settings, [])]
+ }.
+
+new_module(Name) ->
+ #p11module{
+ name = Name
+ }.
+
+%%%%%%%%%%%%%%
+%% Unit tests.
+
+-include_lib("eunit/include/eunit.hrl").
+
+tokens_test_() ->
+ {setup,
+ fun() -> init_tokens(
+ [
+ {"vtoken0",
+ [{modules, ["token_0_0", "token_0_1"]}]},
+ {"vtoken1",
+ [{modules, ["token_1_0"]}]}
+ ]) end,
+ fun(_) -> ok end,
+ fun(Conf) ->
+ [?_assertEqual(
+ [
+ {token,"vtoken0",[{module,"token_0_0"},{module,"token_0_1"}]},
+ {token,"vtoken1",[{module,"token_1_0"}]}],
+ Conf)] end}.
diff --git a/p11p-daemon/src/p11p_defs.hrl b/p11p-daemon/src/p11p_defs.hrl
new file mode 100644
index 0000000..bd390a7
--- /dev/null
+++ b/p11p-daemon/src/p11p_defs.hrl
@@ -0,0 +1,11 @@
+-record(p11module, {
+ name :: string()
+ }).
+
+-record(token, {
+ name :: string(),
+ modules = [] :: [p11module()]
+ }).
+
+-type token() :: #token{}.
+-type p11module() :: #p11module{}.
diff --git a/p11p-daemon/src/p11p_sup.erl b/p11p-daemon/src/p11p_sup.erl
new file mode 100644
index 0000000..b8bffe2
--- /dev/null
+++ b/p11p-daemon/src/p11p_sup.erl
@@ -0,0 +1,24 @@
+-module(p11p_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% Supervisor callbacks.
+-export([init/1]).
+
+%% From supervisor.
+-type start_link_err() :: {already_started, pid()} | shutdown | term().
+-type start_link_ret() :: {ok, pid()} | ignore | {error, start_link_err()}.
+
+-spec start_link() -> start_link_ret().
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
+
+%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
+init([]) ->
+ {ok, {{one_for_all, 10, 10}, [
+ ?CHILD(p11p_config, worker)
+ ]}}.