aboutsummaryrefslogtreecommitdiffstats
path: root/lib/snmp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/snmp')
-rw-r--r--lib/snmp/src/agent/snmp_standard_mib.erl10
-rw-r--r--lib/snmp/src/app/snmp.appup.src2
-rw-r--r--lib/snmp/src/misc/snmp_config.erl102
3 files changed, 100 insertions, 14 deletions
diff --git a/lib/snmp/src/agent/snmp_standard_mib.erl b/lib/snmp/src/agent/snmp_standard_mib.erl
index 7c78e03b3b..ca93546923 100644
--- a/lib/snmp/src/agent/snmp_standard_mib.erl
+++ b/lib/snmp/src/agent/snmp_standard_mib.erl
@@ -156,7 +156,7 @@ read_standard(Dir) ->
Gen = fun(D, Reason) ->
throw({error, {failed_reading_config_file,
D, FileName,
- file:list_dir(Dir), Reason}})
+ list_dir(Dir), Reason}})
end,
Filter = fun(Standard) -> sort_standard(Standard) end,
Check = fun(Entry) -> check_standard(Entry) end,
@@ -164,6 +164,14 @@ read_standard(Dir) ->
snmp_conf:read_files(Dir, [{Gen, Filter, Check, FileName}]),
Standard.
+list_dir(Dir) ->
+ case file:list_dir(Dir) of
+ {ok, Files} ->
+ Files;
+ Error ->
+ Error
+ end.
+
%%-----------------------------------------------------------------
%% Make sure that each mandatory standard attribute is present, and
diff --git a/lib/snmp/src/app/snmp.appup.src b/lib/snmp/src/app/snmp.appup.src
index a0c065dba3..60877c547e 100644
--- a/lib/snmp/src/app/snmp.appup.src
+++ b/lib/snmp/src/app/snmp.appup.src
@@ -24,6 +24,7 @@
[
{"4.21.7",
[
+ {load_module, snmp_config, soft_purge, soft_purge, []},
{load_module, snmp_conf, soft_purge, soft_purge, []},
{load_module, snmp_community_mib, soft_purge, soft_purge, [snmp_conf]},
{load_module, snmp_framework_mib, soft_purge, soft_purge, [snmp_conf]},
@@ -149,6 +150,7 @@
[
{"4.21.7",
[
+ {load_module, snmp_config, soft_purge, soft_purge, []},
{load_module, snmp_conf, soft_purge, soft_purge, []},
{load_module, snmp_community_mib, soft_purge, soft_purge, [snmp_conf]},
{load_module, snmp_framework_mib, soft_purge, soft_purge, [snmp_conf]},
diff --git a/lib/snmp/src/misc/snmp_config.erl b/lib/snmp/src/misc/snmp_config.erl
index 6ab20e3e48..8bf0ea945b 100644
--- a/lib/snmp/src/misc/snmp_config.erl
+++ b/lib/snmp/src/misc/snmp_config.erl
@@ -90,6 +90,17 @@
]).
+-export_type([void/0,
+ verify_config_entry_function/0,
+ verify_config_function/0,
+ write_config_function/0]).
+
+
+%%----------------------------------------------------------------------
+
+-type void() :: term(). % Any value - ignored
+
+
%%----------------------------------------------------------------------
%% Handy SNMP configuration
%%----------------------------------------------------------------------
@@ -2356,53 +2367,118 @@ write_sys_config_file_manager_atl_opt(Fid, {seqno, SeqNo}) ->
header() ->
- {Y,Mo,D} = date(),
- {H,Mi,S} = time(),
+ {Y, Mo, D} = date(),
+ {H, Mi, S} = time(),
io_lib:format("%% This file was generated by "
- "snmp_config (version-~s) ~w-~2.2.0w-~2.2.0w "
+ "~w (version-~s) ~w-~2.2.0w-~2.2.0w "
"~2.2.0w:~2.2.0w:~2.2.0w\n",
- [?version,Y,Mo,D,H,Mi,S]).
+ [?MODULE, ?version, Y, Mo, D, H, Mi, S]).
+
+%% *If* these functions are successfull, they successfully return anything
+%% (which is ignored), but they fail with either a throw or an exit or
+%% something similar.
+
+%% Verification of one config entry read from a file
+-type(verify_config_entry_function() ::
+ fun((Entry :: term()) -> ok | {error, Reason :: term()})).
+
+%% Verification of config to be written
+-type(verify_config_function() ::
+ fun(() -> void())).
+
+%% Write config to file (as defined by Fd)
+-type(write_config_function() ::
+ fun((Fd :: file:io_device()) -> void())).
+
+-spec write_config_file(Dir :: string(),
+ FileName :: string(),
+ Verify :: verify_config_function(),
+ Write :: write_config_function()) ->
+ ok | {error, Reason :: term()}.
write_config_file(Dir, FileName, Verify, Write)
when (is_list(Dir) andalso
is_list(FileName) andalso
is_function(Verify) andalso
is_function(Write)) ->
- (catch do_write_config_file(Dir, FileName, Verify, Write)).
+ try
+ begin
+ do_write_config_file(Dir, FileName, Verify, Write)
+ end
+ catch
+ throw:Error ->
+ Error;
+ T:E ->
+ {error, {failed_write, Dir, FileName, T, E}}
+ end.
+
do_write_config_file(Dir, FileName, Verify, Write) ->
+ io:format("do_write_config_file -> entry with"
+ "~n Dir: ~p"
+ "~n FileName: ~p"
+ "~nwhen"
+ "~n FileInfo(Dir): ~p"
+ "~n FileList(Dir): ~p"
+ "~n", [Dir, FileName,
+ file:read_file_info(filename:join(Dir, FileName)),
+ file:list_dir(Dir)]),
Verify(),
case file:open(filename:join(Dir, FileName), [write]) of
{ok, Fd} ->
- (catch Write(Fd)),
- file:close(Fd),
- ok;
+ file_write_and_close(Write, Fd, Dir, FileName);
Error ->
Error
end.
-
append_config_file(Dir, FileName, Verify, Write)
when (is_list(Dir) andalso
is_list(FileName) andalso
is_function(Verify) andalso
is_function(Write)) ->
- (catch do_append_config_file(Dir, FileName, Verify, Write)).
+ try
+ begin
+ do_append_config_file(Dir, FileName, Verify, Write)
+ end
+ catch
+ throw:Error ->
+ Error;
+ T:E ->
+ {error, {failed_append, Dir, FileName, T, E}}
+ end.
do_append_config_file(Dir, FileName, Verify, Write) ->
Verify(),
case file:open(filename:join(Dir, FileName), [read, write]) of
{ok, Fd} ->
file:position(Fd, eof),
- (catch Write(Fd)),
- file:close(Fd),
- ok;
+ file_write_and_close(Write, Fd, Dir, FileName);
Error ->
Error
end.
+file_write_and_close(Write, Fd, Dir, FileName) ->
+ ok = Write(Fd),
+ case file:sync(Fd) of
+ ok ->
+ case file:close(Fd) of
+ ok ->
+ ok;
+ {error, Reason} ->
+ {error, {failed_closing, Dir, FileName, Reason}}
+ end;
+ {error, Reason} ->
+ {error, {failed_syncing, Dir, FileName, Reason}}
+ end.
+
+
+-spec read_config_file(Dir :: string(),
+ FileName :: string(),
+ Verify :: verify_config_entry_function()) ->
+ {ok, Config :: list()} | {error, Reason :: term()}.
+
read_config_file(Dir, FileName, Verify)
when is_list(Dir) andalso is_list(FileName) andalso is_function(Verify) ->
(catch do_read_config_file(Dir, FileName, Verify)).