1%% ``Licensed under the Apache License, Version 2.0 (the "License");
2%% you may not use this file except in compliance with the License.
3%% You may obtain a copy of the License at
4%%
5%%     http://www.apache.org/licenses/LICENSE-2.0
6%%
7%% Unless required by applicable law or agreed to in writing, software
8%% distributed under the License is distributed on an "AS IS" BASIS,
9%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10%% See the License for the specific language governing permissions and
11%% limitations under the License.
12%%
13%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
14%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
15%% AB. All Rights Reserved.''
16%%
17%%     $Id: inets_sup.erl,v 1.1 2008/12/17 09:53:34 mikpe Exp $
18%%
19-module(inets_sup).
20
21-export([crock/0]).
22-export([start/2, stop/1, init/1]).
23-export([start_child/2, stop_child/2, which_children/0]).
24
25
26%% crock (Used for debugging!)
27
28crock() ->
29    application:start(sasl),
30    application:start(inets).
31
32
33%% start
34
35start(Type, State) ->
36    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
37
38
39%% stop
40
41stop(State) ->
42    ok.
43
44
45%% start_child
46
47start_child(ConfigFile, Verbosity) ->
48    {ok, Spec} = httpd_child_spec(ConfigFile, Verbosity),
49    supervisor:start_child(?MODULE, Spec).
50
51
52%% stop_child
53
54stop_child(Addr, Port) ->
55    Name = {httpd_sup, Addr, Port},
56    case supervisor:terminate_child(?MODULE, Name) of
57        ok ->
58            supervisor:delete_child(?MODULE, Name);
59        Error ->
60            Error
61    end.
62
63
64%% which_children
65
66which_children() ->
67    supervisor:which_children(?MODULE).
68
69
70%% init
71
72init([]) ->
73    case get_services() of
74	{error, Reason} ->
75	    {error,Reason};
76	Services ->
77	    SupFlags = {one_for_one, 10, 3600},
78	    {ok, {SupFlags, child_spec(Services, [])}}
79    end.
80
81get_services() ->
82    case (catch application:get_env(inets, services)) of
83	{ok, Services} ->
84	    Services;
85	_ ->
86	    []
87    end.
88
89
90child_spec([], Acc) ->
91    Acc;
92child_spec([{httpd, ConfigFile, Verbosity}|Rest], Acc) ->
93    case httpd_child_spec(ConfigFile, Verbosity) of
94	{ok, Spec} ->
95	    child_spec(Rest, [Spec | Acc]);
96	{error, Reason} ->
97	    error_msg("Failed creating child spec "
98		      "using ~p for reason: ~p", [ConfigFile, Reason]),
99	    child_spec(Rest, Acc)
100    end;
101child_spec([{httpd, ConfigFile}|Rest], Acc) ->
102    case httpd_child_spec(ConfigFile, []) of
103	{ok, Spec} ->
104	    child_spec(Rest, [Spec | Acc]);
105	{error, Reason} ->
106	    error_msg("Failed creating child spec "
107		      "using ~p for reason: ~p", [ConfigFile, Reason]),
108	    child_spec(Rest, Acc)
109    end.
110
111
112httpd_child_spec(ConfigFile, Verbosity) ->
113    case httpd_conf:load(ConfigFile) of
114	{ok, ConfigList} ->
115	    Port = httpd_util:key1search(ConfigList, port, 80),
116	    Addr = httpd_util:key1search(ConfigList, bind_address),
117	    {ok, httpd_child_spec(ConfigFile, Addr, Port, Verbosity)};
118	Error ->
119	    Error
120    end.
121
122
123httpd_child_spec(ConfigFile, Addr, Port, Verbosity) ->
124    {{httpd_sup, Addr, Port},{httpd_sup, start_link,[ConfigFile, Verbosity]},
125     permanent, 20000, supervisor,
126     [ftp,
127      httpd,
128      httpd_conf,
129      httpd_example,
130      httpd_manager,
131      httpd_misc_sup,
132      httpd_listener,
133      httpd_parse,
134      httpd_request,
135      httpd_response,
136      httpd_socket,
137      httpd_sup,
138      httpd_util,
139      httpd_verbosity,
140      inets_sup,
141      mod_actions,
142      mod_alias,
143      mod_auth,
144      mod_cgi,
145      mod_dir,
146      mod_disk_log,
147      mod_esi,
148      mod_get,
149      mod_head,
150      mod_include,
151      mod_log,
152      mod_auth_mnesia,
153      mod_auth_plain,
154      mod_auth_dets,
155      mod_security]}.
156
157
158error_msg(F, A) ->
159    error_logger:error_msg(F ++ "~n", A).
160