From 20e203cd5beb194b3ee307297dd446e0e8513638 Mon Sep 17 00:00:00 2001 From: Matt Ezell Date: Tue, 10 Oct 2017 21:59:56 -0400 Subject: [PATCH] 'makehosts -n' Improvements - 'makehosts -n' will leave the server without a hosts file while it is generating the new one, but there's really no reason for that. - While /etc/hosts should never be missing, 'makehosts -n' should include the loopback addresses no matter what. - makehosts with and without '-n' should use the same locking to prevent multiple concurrent execution. --- xCAT-server/lib/xcat/plugins/hosts.pm | 37 +++++++++++---------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/hosts.pm b/xCAT-server/lib/xcat/plugins/hosts.pm index 945509f36..6853146bd 100755 --- a/xCAT-server/lib/xcat/plugins/hosts.pm +++ b/xCAT-server/lib/xcat/plugins/hosts.pm @@ -415,39 +415,32 @@ sub process_request my $domain; my $lockh; + # lockfile to prevent concurrent executions + open($lockh, ">", "/tmp/xcat/hostsfile.lock"); + flock($lockh, LOCK_EX); + + # save a backup copy + my $bakname = "/etc/hosts.xcatbak"; + copy("/etc/hosts", $bakname); + @hosts = (); if ($REMOVE) { - if (-e "/etc/hosts") + # add the localhost entry if trying to create the /etc/hosts from scratch + if ($^O =~ /^aix/i) { - my $bakname = "/etc/hosts.xcatbak"; - rename("/etc/hosts", $bakname); - - # add the localhost entry if trying to create the /etc/hosts from scratch - if ($^O =~ /^aix/i) - { - push @hosts, "127.0.0.1 loopback localhost\n"; - } - else - { - push @hosts, "127.0.0.1 localhost\n"; - } + push @hosts, "127.0.0.1 loopback localhost\n"; + } + else + { + push @hosts, "127.0.0.1 localhost\n"; } } else { - if (-e "/etc/hosts") - { - my $bakname = "/etc/hosts.xcatbak"; - copy("/etc/hosts", $bakname); - } - - # the contents of the /etc/hosts file is saved in the @hosts array # the @hosts elements are updated and used to re-create the # /etc/hosts file at the end by the writeout subroutine. - open($lockh, ">", "/tmp/xcat/hostsfile.lock"); - flock($lockh, LOCK_EX); my $rconf; open($rconf, "/etc/hosts"); # Read file into memory if ($rconf)