aboutsummaryrefslogtreecommitdiffstats
path: root/lib/runtime_tools/src/inviso_autostart.erl
blob: 134133ad1f17164349694f3713323f29522662bc (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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2006-2009. All Rights Reserved.
%% 
%% 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 online 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.
%% 
%% %CopyrightEnd%
%%
%% Author: Lennart �hman, [email protected]
-module(inviso_autostart).

-export([autostart/1,which_config_file/0]).

%% This module implements the default autostart module for the inviso runtime
%% component.
%% It will:
%% (1) Open the autostart configuration file (either the default or the one
%%     pointed out by the runtime_tools application parameter inviso_autostart_config).
%% (2) Check that the incarnation counter has not reached 0. If so, we do not
%%     allow (yet) one autostart.
%% (3) Rewrite the configuration file if there was an incarnation counter.
%%     (With the counter decreased).
%% (4) Inspect the content of the configuration file and pass paramters in the
%%     return value (which is interpreted by the runtime component).
%%
%% CONTENT OF A CONFIGURATION FILE:
%% A plain text file containing erlang tuple terms, each ended with a period(.).
%% The following parameters are recognized:
%% {repeat,N} N=interger(),
%%   The number of remaining allowed autostart incarnations of inviso.
%% {options,Options} Options=list()
%%   The options which controls the runtime component, such as overload and
%%   dependency.
%% {mfa,{Mod,Func,Args}} Args=list()
%%   Controls how a spy process initiating tracing, patterns and flags shall
%%   be started.
%% {tag,Tag}
%%   The tag identifying the runtime component to control components.
%% =============================================================================

%% This function is run in the runtime component's context during autostart
%% to determine whether to continue and if, then how.
autostart(_AutoModArgs) ->
    ConfigFile=
	case application:get_env(inviso_autostart_conf) of
	    {ok,FileName} when is_list(FileName) -> % Use this filename then.
		FileName;
	    {ok,{load,FileNames,{M,F}}} ->  % First load the module, then...
		case try_load_module(FileNames) of
		    ok ->
			autostart_apply(M,F);
		    false ->                % No such module available
			"inviso_autostart.config"
		end;
	    {ok,{gettia_asc,asc_file}} ->   % Uggly hack to not have to change in GSN-CPS.
		case try_load_module(["/tmp/DPE_COMMONLOG/gettia_asc",
				      "/tmp/DPE_COMMONLOG/gettia_overload"]) of
		    ok ->
			autostart_apply(gettia_asc,asc_file);
		    false ->                % No such module available
			false
		end;
	    {ok,{M,F}} ->                   % Use M:F(node())
		autostart_apply(M,F);
	    {ok,no_autostart} ->
		false;
	    _ ->                            % Use a default name, in CWD!
		"inviso_autostart.config"
	end,
    if
	is_list(ConfigFile) ->
	    case file:consult(ConfigFile) of
		{ok,Terms} ->               % There is a configuration.
		    case handle_repeat(ConfigFile,Terms) of
			ok ->               % Handled or not, we shall continue.
			    {get_mfa(Terms),get_options(Terms),get_tag(Terms)};
			stop ->             % We are out of allowed starts.
			    true            % Then no autostart.
		    end;
		{error,_} ->                % There is no config file
		    true                    % Then no autostart!
	    end;
	true ->                             % Skip it then.
	    true
    end.

autostart_apply(M,F) ->
    case catch M:F(node()) of
	FileName when is_list(FileName) ->
	    FileName;
	no_autostart ->                     % No autostart after all.
	    false;
	_ ->
	    "inviso_autostart.config"
    end.

%% This function is necessary since it is not always the case that all code-paths
%% are set at the time of an autostart.
try_load_module([AbsFileName|Rest]) when is_list(AbsFileName) ->
    case catch code:load_abs(AbsFileName) of % May not be a proper filename.
	{module,_Mod} ->
	    try_load_module(Rest);
	_ ->
	    false
    end;
try_load_module([]) ->                      % Load all beam files successfully.
    ok;
try_load_module(AbsFileName) when is_list(AbsFileName) ->
    try_load_module([AbsFileName]).
%% -----------------------------------------------------------------------------

%% Function returning the filename probably used as autostart config file.
%% Note that this function must be executed at the node in question.
which_config_file() ->
    case application:get_env(runtime_tools,inviso_autostart_conf) of
	{ok,FileName} when is_list(FileName) -> % Use this filename then.
	    FileName;
	{ok,{M,F}} ->                       % Use M:F(node())
	    case catch M:F(node()) of
		FileName when is_list(FileName) ->
		    FileName;
		_ ->
		    {ok,CWD}=file:get_cwd(),
		    filename:join(CWD,"inviso_autostart.config")
	    end;
	_ ->                                % Use a default name, in CWD!
	    {ok,CWD}=file:get_cwd(),
	    filename:join(CWD,"inviso_autostart.config")
    end.
%% -----------------------------------------------------------------------------


%% Help function which finds out if there is a limit on the number of times
%% we shall autostart. If there is a repeat parameter and it is greater than
%% zero, the file must be rewritten with the parameter decreased with one.
%% Returns 'ok' or 'stop'.
handle_repeat(FileName,Terms) ->
    case lists:keysearch(repeat,1,Terms) of
	{value,{_,N}} when N>0 ->           % Controlls how many time more.
	    handle_repeat_rewritefile(FileName,Terms,N-1),
	    ok;                             % Indicate that we shall continue.
	{value,_} ->                        % No we have reached the limit.
	    stop;
	false ->                            % There is no repeat parameter.
	    ok                              % No restrictions then!
    end.

%% Help function which writes the configuration file again, but with the
%% repeat parameter set to NewN.
%% Returns nothing significant.
handle_repeat_rewritefile(FileName,Term,NewN) ->
    case file:open(FileName,[write]) of
	{ok,FD} ->
	    NewTerm=lists:keyreplace(repeat,1,Term,{repeat,NewN}),
	    handle_repeat_rewritefile_2(FD,NewTerm),
	    file:close(FD);
	{error,_Reason} ->                  % Not much we can do then?!
	    error
    end.

handle_repeat_rewritefile_2(FD,[Tuple|Rest]) ->
    io:format(FD,"~w.~n",[Tuple]),
    handle_repeat_rewritefile_2(FD,Rest);
handle_repeat_rewritefile_2(_,[]) ->
    true.
%% -----------------------------------------------------------------------------

%% Three help functions finding the parameters possible to give to the runtime
%% component. Note that some of them have default values, should the parameter
%% not exist.
get_mfa(Terms) ->
    case lists:keysearch(mfa,1,Terms) of
	{value,{_,MFA}} ->
	    MFA;
	false ->
	    false
    end.

get_options(Terms) ->
    case lists:keysearch(options,1,Terms) of
	{value,{_,Options}} ->
	    Options;
	false ->
	    []
    end.

get_tag(Terms) ->
    case lists:keysearch(tag,1,Terms) of
	{value,{_,Tag}} ->
	    Tag;
	false ->
	    default_tag
    end.
%% -----------------------------------------------------------------------------


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%