Skip to content

Commit

Permalink
inet: delayed/avoided read of /etc/resolv.conf and /etc/hosts
Browse files Browse the repository at this point in the history
The 'file' and 'dns' lookup methods configuration files
are now read and parsed at the time of the first lookup
instead of at boot time when per default none of these
lookup methods are used.
  • Loading branch information
RaimoNiskanen authored and bjorng committed Feb 9, 2010
1 parent a53dfdc commit 0377ecd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
22 changes: 13 additions & 9 deletions lib/kernel/src/inet_config.erl
Expand Up @@ -130,21 +130,25 @@ init() ->
{unix,_} ->
%% The Etc variable enables us to run tests with other
%% configuration files than the normal ones
Etc = case os:getenv("ERL_INET_ETC_DIR") of
false -> ?DEFAULT_ETC;
_EtcDir ->
_EtcDir
end,
Etc =
case os:getenv("ERL_INET_ETC_DIR") of
false ->
?DEFAULT_ETC;
_EtcDir ->
_EtcDir
end,
case inet_db:res_option(resolv_conf) of
undefined ->
inet_db:set_resolv_conf(filename:join(Etc,
?DEFAULT_RESOLV));
inet_db:res_option(
resolv_conf_name,
filename:join(Etc, ?DEFAULT_RESOLV));
_ -> ok
end,
case inet_db:res_option(hosts_file) of
undefined ->
inet_db:set_hosts_file(filename:join(Etc,
?DEFAULT_HOSTS));
inet_db:res_option(
hosts_file_name,
filename:join(Etc, ?DEFAULT_HOSTS));
_ -> ok
end;
_ -> ok
Expand Down
88 changes: 56 additions & 32 deletions lib/kernel/src/inet_db.erl
Expand Up @@ -425,7 +425,9 @@ res_optname(usevc) -> res_usevc;
res_optname(edns) -> res_edns;
res_optname(udp_payload_size) -> res_udp_payload_size;
res_optname(resolv_conf) -> res_resolv_conf;
res_optname(resolv_conf_name) -> res_resolv_conf;
res_optname(hosts_file) -> res_hosts_file;
res_optname(hosts_file_name) -> res_hosts_file;
res_optname(_) -> undefined.

res_check_option(nameserver, NSs) -> %% Legacy
Expand Down Expand Up @@ -458,9 +460,15 @@ res_check_option(udp_payload_size, S) when is_integer(S), S >= 512 -> true;
res_check_option(resolv_conf, "") -> true;
res_check_option(resolv_conf, F) ->
res_check_option_absfile(F);
res_check_option(resolv_conf_name, "") -> true;
res_check_option(resolv_conf_name, F) ->
res_check_option_absfile(F);
res_check_option(hosts_file, "") -> true;
res_check_option(hosts_file, F) ->
res_check_option_absfile(F);
res_check_option(hosts_file_name, "") -> true;
res_check_option(hosts_file_name, F) ->
res_check_option_absfile(F);
res_check_option(_, _) -> false.

res_check_option_absfile(F) ->
Expand Down Expand Up @@ -503,7 +511,7 @@ res_update_hosts() ->
res_update(res_hosts_file, res_hosts_file_tm, res_hosts_file_info,
set_hosts_file_tm, fun set_hosts_file/1).

res_update(Tag, TagTm, TagInfo, CallTag, SetFun) ->
res_update(Tag, TagTm, TagInfo, TagSetTm, SetFun) ->
case db_get(TagTm) of
undefined -> ok;
TM ->
Expand All @@ -522,12 +530,12 @@ res_update(Tag, TagTm, TagInfo, CallTag, SetFun) ->
atime = undefined},
case db_get(TagInfo) of
Finfo ->
call({CallTag, Now});
call({TagSetTm, Now});
_ ->
SetFun(File)
end;
_ ->
call({CallTag, Now}),
call({TagSetTm, Now}),
error
end
end;
Expand Down Expand Up @@ -974,37 +982,46 @@ handle_call(Request, From, #state{db=Db}=State) ->
{reply, error, State}
end;

{res_set, hosts_file_name=Option, Fname} ->
handle_set_file(
Option, Fname, res_hosts_file_tm, res_hosts_file_info,
undefined, From, State);
{res_set, resolv_conf_name=Option, Fname} ->
handle_set_file(
Option, Fname, res_resolv_conf_tm, res_resolv_conf_info,
undefined, From, State);

{res_set, hosts_file=Option, Fname} ->
handle_set_file(Option, Fname,
res_hosts_file_tm, res_hosts_file_info,
fun (Bin) ->
case inet_parse:hosts(Fname,
{chars,Bin}) of
{ok,Opts} ->
[{load_hosts_file,Opts}];
_ -> error
end
end,
From, State);
handle_set_file(
Option, Fname, res_hosts_file_tm, res_hosts_file_info,
fun (Bin) ->
case inet_parse:hosts(
Fname, {chars,Bin}) of
{ok,Opts} ->
[{load_hosts_file,Opts}];
_ -> error
end
end,
From, State);
%%
{res_set, resolv_conf=Option, Fname} ->
handle_set_file(Option, Fname,
res_resolv_conf_tm, res_resolv_conf_info,
fun (Bin) ->
case inet_parse:resolv(Fname,
{chars,Bin}) of
{ok,Opts} ->
[del_ns,
clear_search,
clear_cache
|[Opt ||
{T,_}=Opt <- Opts,
(T =:= nameserver orelse
T =:= search)]];
_ -> error
end
end,
From, State);
handle_set_file(
Option, Fname, res_resolv_conf_tm, res_resolv_conf_info,
fun (Bin) ->
case inet_parse:resolv(
Fname, {chars,Bin}) of
{ok,Opts} ->
[del_ns,
clear_search,
clear_cache
|[Opt ||
{T,_}=Opt <- Opts,
(T =:= nameserver orelse
T =:= search)]];
_ -> error
end
end,
From, State);
%%
{res_set, Opt, Value} ->
case res_optname(Opt) of
Expand Down Expand Up @@ -1156,6 +1173,12 @@ handle_set_file(Option, Fname, TagTm, TagInfo, ParseFun, From,
ets:delete(Db, TagInfo),
ets:delete(Db, TagTm),
handle_set_file(ParseFun, <<>>, From, State);
true when ParseFun =:= undefined ->
File = filename:flatten(Fname),
ets:insert(Db, {res_optname(Option), File}),
ets:insert(Db, {TagInfo, undefined}),
ets:insert(Db, {TagTm, 0}),
{reply,ok,State};
true ->
File = filename:flatten(Fname),
ets:insert(Db, {res_optname(Option), File}),
Expand All @@ -1178,7 +1201,8 @@ handle_set_file(Option, Fname, TagTm, TagInfo, ParseFun, From,

handle_set_file(ParseFun, Bin, From, State) ->
case ParseFun(Bin) of
error -> {reply,error,State};
error ->
{reply,error,State};
Opts ->
handle_rc_list(Opts, From, State)
end.
Expand Down

0 comments on commit 0377ecd

Please sign in to comment.