summaryrefslogtreecommitdiff
path: root/p11p-daemon/src/p11p_server_sup.erl
blob: 15398bcc7119c9cc6b7fa3ece72ecb33bddebd7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-module(p11p_server_sup).
-behaviour(supervisor).

-export([start_link/0, start_server/0]).
-export([init/1]).

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    Name = "vtoken0",				% FIXME
    SocketPath = socket_path(mkdir_socket_basepath(), Name),
    file:delete(SocketPath),
    % TODO: consider flow control through {active, once}
    {ok, ListenSocket} = gen_tcp:listen(0, [{ifaddr, {local, SocketPath}},
					    binary]),
    spawn_link(fun start_server/0),
    {ok, {{simple_one_for_one, 60, 3600},
	  [{sock_server,
	    {p11p_server, start_link, [ListenSocket]},
	    temporary, 1000, worker, [p11p_server]}
	   ]}}.

start_server() ->
    supervisor:start_child(?MODULE, []).

%% Private functions.
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.