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
43
44
45
46
47
48
49
|
-module(p11p_server_sup).
-behaviour(supervisor).
-export([start_link/0, start_server/1]).
-export([init/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
Names = ["vtoken0", "vtoken1"], % FIXME: get from config
ok = start_servers(Names),
{ok, {{simple_one_for_one, 1, 5},
[{sock_server, % Ignored childspec id.
{p11p_server, start_link, []},
temporary, 1000, worker, [p11p_server]}
]}}.
start_servers([]) ->
ok;
start_servers([Name|T]) ->
Path = socket_path(mkdir_socket_basepath(), Name),
file:delete(Path),
% TODO: consider flow control through {active, once}, don't forget activating after read!
{ok, Socket} = gen_tcp:listen(0, [{ifaddr, {local, Path}}, binary]),
spawn_link(?MODULE, start_server, [[Path, Socket]]),
start_servers(T).
start_server(Args) ->
{ok, Pid} = supervisor:start_child(?MODULE, [Args]),
Pid.
%% Private functions.
mkdir_socket_basepath() ->
%%"/run/user/$UNIXUID/p11p/$TokenCfg-$UNIXPID"
EUID = "1000", % FIXME: get euid
Path = "/run/user/" ++ EUID ++ "/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) ->
BasePath ++ Name ++ "-" ++ os:getpid(). % FIXME: filename(3erl)
|