aboutsummaryrefslogtreecommitdiff
path: root/src/extauth_sup.erl
blob: 309374cb6e85d32148cb0b9e11aea6d645f52030 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
%%%-------------------------------------------------------------------
%%% Created : 7 May 2018 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2020   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%-------------------------------------------------------------------
-module(extauth_sup).
-behaviour(supervisor).

%% API
-export([start/1, stop/1, reload/1, start_link/3]).
%% Supervisor callbacks
-export([init/1]).

-include("logger.hrl").

%%%===================================================================
%%% API functions
%%%===================================================================
start(Host) ->
    case extauth:prog_name(Host) of
	undefined ->
	    ?ERROR_MSG("Option 'extauth_program' is not set for '~ts'",
		       [Host]),
	    ignore;
	Prog ->
	    Pool = extauth:pool_name(Host),
	    ChildSpec = {Pool, {?MODULE, start_link, [Host, Prog, Pool]},
			 permanent, infinity, supervisor, [?MODULE]},
	    supervisor:start_child(ejabberd_backend_sup, ChildSpec)
    end.

stop(Host) ->
    Pool = extauth:pool_name(Host),
    supervisor:terminate_child(ejabberd_backend_sup, Pool),
    supervisor:delete_child(ejabberd_backend_sup, Pool).

reload(Host) ->
    Pool = extauth:pool_name(Host),
    Prog = extauth:prog_name(Host),
    PoolSize = extauth:pool_size(Host),
    try process_info(whereis(Pool), dictionary) of
	{dictionary, Dict} ->
	    case proplists:get_value(extauth_program, Dict) of
		Prog ->
		    OldPoolSize = try supervisor:which_children(Pool) of
				      Children -> length(Children)
				  catch _:_ -> PoolSize
				  end,
		    if OldPoolSize > PoolSize ->
			    lists:foreach(
			      fun(I) ->
				      Worker = extauth:worker_name(Pool, I),
				      supervisor:terminate_child(Pool, Worker),
				      supervisor:delete_child(Pool, Worker)
			      end, lists:seq(PoolSize+1, OldPoolSize));
		       OldPoolSize < PoolSize ->
			    lists:foreach(
			      fun(I) ->
				      Spec = worker_spec(Pool, Prog, I),
				      supervisor:start_child(Pool, Spec)
			      end, lists:seq(OldPoolSize+1, PoolSize));
		       OldPoolSize == PoolSize ->
			    ok
		    end;
		_ ->
		    stop(Host),
		    start(Host)
	    end
    catch _:badarg ->
	    ok
    end.

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

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
init([Host, Prog, Pool]) ->
    PoolSize = extauth:pool_size(Host),
    Children = lists:map(
		 fun(I) ->
			 worker_spec(Pool, Prog, I)
		 end, lists:seq(1, PoolSize)),
    put(extauth_program, Prog),
    {ok, {{one_for_one, PoolSize, 1}, Children}}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
worker_spec(Pool, Prog, I) ->
    Worker = extauth:worker_name(Pool, I),
    {Worker, {extauth, start_link, [Worker, Prog]},
     permanent, 5000, worker, [extauth]}.