summaryrefslogtreecommitdiff
path: root/src/couch_inets/httpd_instance_sup.erl
blob: ddf81a49b6d129f55f1aa9319c6d8112a2bc399b (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%% 
%%     $Id$
%%
%%----------------------------------------------------------------------
%% Purpose: The top supervisor for an instance of the http server. (You may
%%          have several instances running on the same machine.) Hangs under
%%          httpd_sup.
%%----------------------------------------------------------------------

-module(httpd_instance_sup).

-behaviour(supervisor).

-export([init/1]).

%% Internal API
-export([start/1, start_link/1, start_link/3, start2/1, start_link2/1, 
	 stop/1, stop/2, stop2/1]).

%%%=========================================================================
%%%  Supervisor callback
%%%=========================================================================
init([ConfigFile, ConfigList, AcceptTimeout, Debug, Addr, Port]) -> 
    httpd_util:enable_debug(Debug),
    Flags = {one_for_one, 0, 1},
    Children  = [sup_spec(httpd_acceptor_sup, Addr, Port), 
		 sup_spec(httpd_misc_sup, Addr, Port), 
		 worker_spec(httpd_manager, Addr, Port, 
			     ConfigFile, ConfigList,AcceptTimeout)],
    {ok, {Flags, Children}}.


%%%=========================================================================
%%%  ??? functions
%%%=========================================================================

start(ConfigFile) ->
    case start_link(ConfigFile) of
	{ok, Pid} ->
	    unlink(Pid),
	    {ok, Pid};

	Else ->
	    Else
    end.

start_link(Config) ->
    case catch httpd_options(Config) of
	{error,Reason} ->
	    error_logger:error_report(Reason),
	    {stop, Reason};
	{ConfigFile,AcceptTimeout,Debug} -> 
	    start_link(ConfigFile, AcceptTimeout, Debug)
    end.
start_link(ConfigFile, AcceptTimeout, Debug) ->
    case get_addr_and_port(ConfigFile) of
	{ok, ConfigList, Addr, Port} ->
	    Name    = make_name(Addr, Port),
	    SupName = {local, Name},
	    supervisor:start_link(SupName, ?MODULE, 
				  [ConfigFile, ConfigList ,AcceptTimeout ,
				   Debug, Addr, Port]);	
	{error, Reason} ->
	    error_logger:error_report(Reason),
	    {stop, Reason}
    end.

    
start2(ConfigList) ->
    case start_link2(ConfigList) of
	{ok, Pid} ->
	    unlink(Pid),
	    {ok, Pid};

	Else ->
	    Else
    end.

    
start_link2(ConfigList) ->
    {ok, Addr, Port} = get_addr_and_port2(ConfigList),
    Name    = make_name(Addr, Port),
    SupName = {local, Name},
    Debug = [],
    AcceptTimeout = 15000,
    supervisor:start_link(SupName, ?MODULE, 
			  [undefined, ConfigList, AcceptTimeout, 
			   Debug, Addr, Port]).
    

stop(Pid) when pid(Pid) ->
    do_stop(Pid);
stop(ConfigFile) when list(ConfigFile) ->
    case get_addr_and_port(ConfigFile) of
	{ok, _, Addr, Port} ->
	    stop(Addr, Port);
	    
	Error ->
	    Error
    end;
stop(_StartArgs) ->
    ok.


stop(Addr, Port) when integer(Port) ->
    Name = make_name(Addr, Port), 
    case whereis(Name) of
	Pid when pid(Pid) ->
	    do_stop(Pid),
	    ok;
	_ ->
	    not_started
    end.
    

stop2(ConfigList) when list(ConfigList) ->
    {ok, Addr, Port} = get_addr_and_port2(ConfigList),
    stop(Addr, Port).

%%%=========================================================================
%%%  Internal functions
%%%=========================================================================
do_stop(Pid) ->
    exit(Pid, shutdown).

sup_spec(SupModule, Addr, Port) ->
    Name = {SupModule, Addr, Port},
    StartFunc = {SupModule, start_link, [Addr, Port]},
    Restart = permanent, 
    Shutdown = infinity,
    Modules = [SupModule],
    Type = supervisor,
    {Name, StartFunc, Restart, Shutdown, Type, Modules}.
    
worker_spec(WorkerModule, Addr, Port, ConfigFile, ConfigList, AcceptTimeout) ->
    Name = {WorkerModule, Addr, Port},
    StartFunc = {WorkerModule, start_link, 
		 [ConfigFile, ConfigList, AcceptTimeout]}, 
    Restart = permanent, 
    Shutdown = 4000,
    Modules = [WorkerModule],
    Type = worker,
    {Name, StartFunc, Restart, Shutdown, Type, Modules}.

httpd_options(Config) ->
    OptionList = mk_tuple_list(Config),
    Debug = http_util:key1search(OptionList,debug,[]),
    AcceptTimeout = http_util:key1search(OptionList,accept_timeout,15000),
    ConfigFile =
    case http_util:key1search(OptionList,file) of
	undefined -> throw({error,{mandatory_conf_file_missed}});
	File -> File
    end,
    httpd_util:valid_options(Debug,AcceptTimeout,ConfigFile),
    {ConfigFile, AcceptTimeout, Debug}.

mk_tuple_list([]) -> 
    [];
mk_tuple_list([H={_,_}|T]) -> 
    [H|mk_tuple_list(T)];
mk_tuple_list(F) when list(F) ->
    [{file,F}].

make_name(Addr,Port) ->
    httpd_util:make_name("httpd_instance_sup",Addr,Port).

get_addr_and_port(ConfigFile) ->
    case httpd_conf:load(ConfigFile) of
	{ok, ConfigList} ->
	    {ok, Addr, Port} = get_addr_and_port2(ConfigList),
	    {ok, ConfigList, Addr, Port};
	Error ->
	    Error
    end.

get_addr_and_port2(ConfigList) ->
    Port = httpd_util:key1search(ConfigList, port, 80),
    Addr = httpd_util:key1search(ConfigList, bind_address),
    {ok, Addr, Port}.