1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2001-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21%%----------------------------------------------------------------------
22%% Purpose: The supervisor for auth and sec processes in the http server,
23%%          hangs under the httpd_instance_sup_<Addr>_<Port> supervisor.
24%%----------------------------------------------------------------------
25
26-module(httpd_misc_sup).
27
28-behaviour(supervisor).
29
30%% API
31-export([start_link/3, start_auth_server/3, stop_auth_server/3,
32	 start_sec_server/3,  stop_sec_server/3]).
33
34%% Supervisor callback
35-export([init/1]).
36
37%%%=========================================================================
38%%%  API
39%%%=========================================================================
40
41start_link(Addr, Port, Profile) ->
42    SupName = make_name(Addr, Port, Profile),
43    supervisor:start_link({local, SupName}, ?MODULE, []).
44
45%%----------------------------------------------------------------------
46%% Function: [start|stop]_[auth|sec]_server/3
47%% Description: Starts a [auth | security] worker (child) process
48%%----------------------------------------------------------------------
49start_auth_server(Addr, Port, Profile) ->
50    start_permanent_worker(mod_auth_server, Addr, Port, Profile, [gen_server]).
51
52stop_auth_server(Addr, Port, Profile) ->
53    stop_permanent_worker(mod_auth_server, Addr, Port, Profile).
54
55
56start_sec_server(Addr, Port, Profile) ->
57    start_permanent_worker(mod_security_server, Addr, Port, Profile, [gen_server]).
58
59stop_sec_server(Addr, Port, Profile) ->
60    stop_permanent_worker(mod_security_server, Addr, Port, Profile).
61
62
63%%%=========================================================================
64%%%  Supervisor callback
65%%%=========================================================================
66init(_) ->
67    Flags     = {one_for_one, 0, 1},
68    Workers   = [],
69    {ok, {Flags, Workers}}.
70
71%%%=========================================================================
72%%%  Internal functions
73%%%=========================================================================
74start_permanent_worker(Mod, Addr, Port, Profile, Modules) ->
75    SupName = make_name(Addr, Port, Profile),
76    Spec    = {{Mod, Addr, Port},
77	       {Mod, start_link, [Addr, Port, Profile]},
78	       permanent, timer:seconds(1), worker, [Mod] ++ Modules},
79    supervisor:start_child(SupName, Spec).
80
81stop_permanent_worker(Mod, Addr, Port, Profile) ->
82    SupName = make_name(Addr, Port, Profile),
83    Name    = {Mod, Addr, Port},
84    case supervisor:terminate_child(SupName, Name) of
85	ok ->
86	    supervisor:delete_child(SupName, Name);
87	Error ->
88	    Error
89    end.
90
91make_name(Addr,Port, Profile) ->
92    httpd_util:make_name("httpd_misc_sup",Addr,Port, Profile).
93