aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sasl/src/si_sasl_supp.erl
blob: cce628f8c421c4e6ddc92edd3c91c18aa90e7ef6 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(si_sasl_supp).

-behaviour(gen_server).

%%%---------------------------------------------------------------------------
%%% Description:
%%% This module contains the BOS specific parts of the Status Inspection Tool.
%%%---------------------------------------------------------------------------


%% user interface
-export([h/0, help/0, start_log/1, stop_log/0, abbrevs/0, pi/1, pi/2, pi/3,
	 pi/4, ppi/1, ppi/3, start/0, start/1, stop/0, start_link/1]).

%% intermodule exports
-export([make_pid/1, make_pid/3, process_abbrevs/0, expand_abbrev/2,
	 status_info/1, valid_opt/1, p/1, do_best_printout/4,
	 si_exec/2, handle_call/3, terminate/2]).

%% exports for use within module
-export([init/1, start_log_impl/1, pi_impl/2, ppi_impl/1]).

%% other gen_server callbacks (not used)
-export([handle_cast/2, handle_info/2, code_change/3]).

%%--------------------------------------------------
%% Table of contents
%% 1. Interface
%% 2. SI - Server
%% 3. Code
%% 4. Selectors
%%--------------------------------------------------


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. Interface
%% -----------------------------------------------------

h() -> print_help().
help() -> print_help().

si_exec(Fun, Args) -> call({si_exec, Fun, Args}).

start_log(FileName) ->
    call({start_log, FileName}).

stop_log() ->
    call(stop_log).

abbrevs() ->
    io:format("~p", [process_abbrevs()]).

%%-----------------------------------------------------------------
%% All functions can be called with an option 'normal' or 'all';
%% default is 'normal'.
%%-----------------------------------------------------------------
%% Process Info that tries to determine processtype (=Module), then
%% it uses this Module:format_info to format data from status_info/1.
%%-----------------------------------------------------------------
pi(XPid) -> 
    si_exec({si_sasl_supp, pi_impl}, [normal, XPid]).

pi(Opt, XPid) ->
    si_exec({si_sasl_supp, pi_impl}, [valid_opt(Opt), XPid]).

pi(A, B, C) when is_integer(A), is_integer(B), is_integer(C) ->
    si_exec({si_sasl_supp, pi_impl}, [normal, {A, B, C}]).

pi(Opt, A, B, C) when is_integer(A), is_integer(B), is_integer(C) ->
    si_exec({si_sasl_supp, pi_impl}, [valid_opt(Opt), {A, B, C}]).

%%-----------------------------------------------------------------
%% Pretty print Process_Info.
%%-----------------------------------------------------------------
ppi(XPid) ->
    case whereis(si_server) of
	undefined ->           % You can always run ppi.
	    ppi_impl(XPid);    % if si_server is down, use standard_io
	_ ->
	    si_exec({si_sasl_supp, ppi_impl}, [XPid])
    end.
ppi(A, B, C) ->
    case whereis(si_server) of
	undefined ->               % You can always run ppi.
	    ppi_impl({A, B, C});   % if si_server is down, use standard_io
	_ ->
	    si_exec({si_sasl_supp, ppi_impl}, [{A, B, C}])
    end.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. SI - Server
%%--------------------------------------------------
-record(state, {}).

start() -> start([]).
start(Options) ->
    supervisor:start_child(sasl_sup, 
			   {si_server, {si_sasl_supp, start_link, [Options]},
			    temporary, brutal_kill, worker, [si_sasl_supp]}).

start_link(_Options) ->
    gen_server:start_link({local, si_server}, si_sasl_supp, [], []).

stop() ->
    call(stop),
    supervisor:delete_child(sasl_sup, si_server).


init(Options) ->
    process_flag(trap_exit, true),
    start_log_impl(get_option(Options, start_log, standard_io)),
    {ok, #state{}}.

%%-----------------------------------------------------------------
%% If an error occurs and we're logging to file: write the error
%% to the file.
%% Always return the error.
%% The only data held by the si_server is the device in its process dictionary.
%%-----------------------------------------------------------------
handle_call({si_exec, Fun, Args}, _From, State) ->
    case catch apply(Fun, Args) of
	{'EXIT', Reason} ->
	    print_error(get(device),
			"SI internal error. Reason: ~w~n",
			[Reason]),
	    {stop, shutdown, {internal_error, Reason}, State};
	{error, Reason} ->
	    print_error(get(device), "~nSI error: ~w~n", [Reason]),
	    {reply, {error, Reason}, State};
	X -> 
	    {reply, X, State}
    end;
handle_call({start_log, FileName}, _From, State) ->
    start_log_impl(FileName),
    {reply, ok, State};
handle_call(stop_log, _From, State) ->
    start_log_impl(standard_io),
    {reply, ok, State};
handle_call(stop, _From, State) ->
    start_log_impl(standard_io),
    {stop, normal, stopped, State}.

terminate(_Reason, _State) ->
    _ = close_device(get(device)),
    ok.

handle_cast(_Msg, State) ->
    {noreply, State}.
handle_info(_Info, State) ->
    {noreply, State}.
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

close_device(standard_io) -> ok;
close_device(Fd) -> file:close(Fd).

print_error(standard_io, _, _) -> ok;
print_error(Device, Format, Args) ->
    io:format(Device, Format, Args).

get_option(Options, Key, Default) ->
    case lists:keysearch(Key, 1, Options) of
	{value, {_Key, Value}} -> Value;
	_ -> Default
    end.

open_log_file(undefined, NewFile) ->
    open_log_file(NewFile);
open_log_file(standard_io, NewFile) ->
    open_log_file(NewFile);
open_log_file(OldFile, NewFile) ->
    _ = file:close(OldFile),
    open_log_file(NewFile).

open_log_file(standard_io) -> standard_io;
open_log_file(FileName) ->
    case file:open(FileName, [write]) of
	{ok, Fd} -> Fd;
	Error -> 
	    io:format("si_sasl_supp: Cannot open file '~s' (~w).~n",
		      [FileName, Error]),
	    io:format("si_sasl_supp: Using standard_io~n"),
	    standard_io
    end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. Code
%%--------------------------------------------------

%%-----------------------------------------------------------------
%% call(Request) -> Term
%%-----------------------------------------------------------------
call(Req) ->
    gen_server:call(si_server, Req, infinity).

%%--------------------------------------------------
%% Makes a Pid of almost anything.
%% Returns: Pid|{error, Reason}
%% Fails: Never.
%%--------------------------------------------------
make_pid(A,B,C) when is_integer(A), is_integer(B), is_integer(C) ->
    list_to_pid(lists:concat(["<",A,".",B,".",C,">"])).
make_pid(P) when is_pid(P) -> P;
make_pid(undefined) -> undefined;
make_pid(P) when is_atom(P) ->
    case whereis(P) of
	undefined ->
	    case expand_abbrev(P, process_abbrevs()) of
		{error, Reason} -> {error, Reason};
		{value, {_Abbrev, FullName}} ->
		    case whereis(FullName) of
			undefined ->
			    {error, {'process not registered', P}};
			Pid -> Pid
		    end
	    end;
	Pid -> Pid
    end;
make_pid(P) when is_list(P) -> list_to_pid(P);
make_pid({A, B, C}) -> make_pid(A, B, C);
make_pid(X) -> {error, {'can not make a pid of', X}}.

process_abbrevs() ->
    [{init, init},
     {fs, file_server}].
     
%%--------------------------------------------------
%% Args: Abbrevs is an assoc-list of {Abbrev, RealName}
%% Returns: {value, {Abbrev, FullName}}|{error, Reason}
%%--------------------------------------------------
expand_abbrev(ProcessName, Abbrevs) ->
    case lists:keysearch(ProcessName, 1, Abbrevs) of
	{value, {Abbrev, FullName}} ->
	    {value, {Abbrev, FullName}};
	_ ->
	    case lists:keysearch(ProcessName, 2, Abbrevs) of
		{value, {Abbrev, FullName}} ->
		    {value, {Abbrev, FullName}};
		_ ->
		    {error, {'invalid process name', ProcessName}}
	    end
    end.

%%-----------------------------------------------------------------
%% This is the function that actually gets the information out
%% of the agent/server/...
%% Returns: {status_info, Pid, Type, Data}
%%        | {error, Reason}
%%-----------------------------------------------------------------
status_info(Pid) when is_pid(Pid) ->
    case catch sys:get_status(Pid, 5000) of
	{status, Pid, Type, Info} ->
	    {status_info, Pid, Type, Info};
	_ ->
	    {error, {'process does not respond', Pid}}
    end;

status_info(X) ->
    {error, {'not a pid', X}}.

%%--------------------------------------------------
%% Implementation starts here.
%%--------------------------------------------------
start_log_impl(FileName) ->
    put(device, open_log_file(get(device), FileName)).

valid_opt(all) -> all;
valid_opt(_Opt) -> normal.


print_help() ->
    p("- - - - - - - - PROCESSES - - - - - - - - - "),
    p("si_sasl_supp:pi([Opt,] Pid)   - Formatted information about any process that"),
    p("                      SI recognises."),
    p("si_sasl_supp:pi([Opt,] A,B,C) - Same as si_sasl_supp:pi({A, B, C})."), 
    p("si_sasl_supp:ppi(Pid)         - Pretty formating of process_info."),
    p("                      Works for any process."),
    p("- - - - - - - - MISC  - - - - - - - - - - - "),
    p("si_sasl_supp:abbrevs()        - Lists valid abbreviations."),
    p("si_sasl_supp:start_log(FileNname)"),
    p("si_sasl_supp:stop_log()"),
    p("si_sasl_supp:start()          - Starts Status Inspection (the si_server)."),
    p("si_sasl_supp:start([{start_log, FileName}])"),
    p("si_sasl_supp:stop()           - Shut down SI.").



%% Convenient shorthand
p(X) ->
    io:format(lists:append(X, "~n")).

pi_impl(Opt, XPid) ->
    case make_pid(XPid) of
	Pid when is_pid(Pid) ->
	    case status_info(Pid) of
		{status_info, Pid, {module, Module}, Data} ->
		    do_best_printout(Opt, Pid, Module, Data);
		{error, Reason} ->
		    _ = ppi_impl(Pid),
		    {error, {"can not get status info from process:",
			     XPid,
			     Reason}}
	    end;
	{error, Reason} ->
	    {error, Reason}
    end.

%%--------------------------------------------------
%% Is there a format_info for this process? In that case, run it!
%% Return ok|{error, Reason}
%% Fails: Never.
%%--------------------------------------------------
do_best_printout(Opt, Pid, Mod, Data) when is_pid(Pid) ->
    case print_info(get(device), Pid, {Mod, format_status}, Opt, Data) of
	ok -> ok;
	{error, Reason} ->
	    _ = ppi_impl(Pid),
	    {error, Reason}
    end.

ppi_impl(XPid) -> 
    case make_pid(XPid) of
	P when is_pid(P) -> 
	    case process_info(P) of
		undefined ->
		    {error, {'dead process', P}};
		PI ->
		    Device = case get(device) of
				 undefined -> standard_io;
				 X -> X
			     end,
		    io:format(Device, "~nPretty Process Info~n", []),
		    io:format(Device, "-------------------~n", []),
		    io:format(Device, "~p~n", [PI])
	    end;
	_ -> {error, {no_pid, XPid}}
    end.

print_info(Device, Pid, {Module, Func}, Opt, Data) ->
    case erlang:function_exported(Module, Func, 2) of
	true ->
	    case catch apply(Module, Func, [Opt, Data]) of
		Format when is_list(Format) ->
		    format_lib_supp:print_info(Device, 79,
					       add_pid_to_format(Pid, Format)),
		    ok;
		Other -> {error, {'invalid format', Other}}
	    end;
	_ ->
	    {error, {no_such_function, Module, Func}}
    end.

add_pid_to_format(Pid, [{header, H} | T]) ->
    [{header, H}, {data, [{"Pid", Pid}]} | T];
add_pid_to_format(Pid, List) ->
    [{data, [{"Pid", Pid}]} | List].