summaryrefslogtreecommitdiff
path: root/p11p-daemon
diff options
context:
space:
mode:
authorLinus Nordberg <linus@sunet.se>2019-06-19 16:39:29 +0200
committerLinus Nordberg <linus@sunet.se>2019-06-19 16:39:29 +0200
commitb2227029785d81f6b650e931c57d68692ac2c4ec (patch)
tree7ab2ec3410b4bfa8df8512fc683304d22800c05f /p11p-daemon
parent464d3e8fcc589bdcc7eb0a9861b13cba8f006fd2 (diff)
add listener
Diffstat (limited to 'p11p-daemon')
-rw-r--r--p11p-daemon/src/p11p_listener.erl73
-rw-r--r--p11p-daemon/src/p11p_sup.erl3
2 files changed, 75 insertions, 1 deletions
diff --git a/p11p-daemon/src/p11p_listener.erl b/p11p-daemon/src/p11p_listener.erl
new file mode 100644
index 0000000..8efb7d8
--- /dev/null
+++ b/p11p-daemon/src/p11p_listener.erl
@@ -0,0 +1,73 @@
+%% Create an AF_UNIX socket and accept connections. On connect, spawn
+%% a p11p_server process.
+
+-module(p11p_listener).
+-behaviour(gen_server).
+
+%% API.
+-export([start_link/0]).
+
+%% 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").
+-record(state, {tokens}).
+
+%% API.
+-spec start_link() -> {ok, pid()} | {error, term()}.
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+%% Genserver callbacks.
+%%-spec init([[token()]]) -> {ok, #state{}}.
+init([]) ->
+ TokensCfg = p11p_config:tokens(),
+ lager:debug("TokensCfg: ~p", [TokensCfg]),
+ {ok, Tokens} = init_tokens(TokensCfg, []),
+ {ok, #state{tokens = 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(Info, State) ->
+ lager:debug("Unhandled info: ~p~n", [Info]),
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVersion, State, _Extra) ->
+ {ok, State}.
+
+%% Private functions.
+init_tokens([], Acc) ->
+ {ok, lists:reverse(Acc)};
+init_tokens([TokenCfg | T], Acc) ->
+ #token{name = Name} = TokenCfg,
+ SocketPath = socket_path(mkdir_socket_basepath(), Name),
+ file:delete(SocketPath),
+ {ok, ListenSocket} = gen_udp:open(0, [{ifaddr, {local, SocketPath}}]),
+ init_tokens(T, [ListenSocket | Acc]).
+
+mkdir_socket_basepath() ->
+ Path = "/run/user/1000/p11p",
+ ok = case file:make_dir(Path) of
+ ok -> ok;
+ {error, eexist} -> ok;
+ Err ->
+ lager:error("~s: unable to create directory: ~p", [Path, Err]),
+ err
+ end,
+ Path.
+
+-spec socket_path(string(), string()) -> string().
+socket_path(BasePath, Name) ->
+ %%"/run/user/$UID/p11p/$TokenCfg-$UNIXPID"
+ BasePath ++ "/" ++ Name.
diff --git a/p11p-daemon/src/p11p_sup.erl b/p11p-daemon/src/p11p_sup.erl
index b8bffe2..76c7551 100644
--- a/p11p-daemon/src/p11p_sup.erl
+++ b/p11p-daemon/src/p11p_sup.erl
@@ -20,5 +20,6 @@ start_link() ->
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
{ok, {{one_for_all, 10, 10}, [
- ?CHILD(p11p_config, worker)
+ ?CHILD(p11p_config, worker),
+ ?CHILD(p11p_listener, worker)
]}}.