Write out cache file to temporary file + atomic replace
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 7 Apr 2021 10:05:36 +0000 (12:05 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 7 Apr 2021 10:05:36 +0000 (12:05 +0200)
Just in case multiple processes might race for the writeout.
Actually seen in production as pingcheck currently seems
to get restarted by connd for every new INTRACLIENT.

src/dns/dnscache.cpp

index c097aea..0e9a270 100644 (file)
@@ -26,6 +26,7 @@
 #include <fstream>
 #include <logfunc.hpp>
 #include <filefunc.hxx>   // I2n::file_exists
+#include <tmpfstream.hpp>
 #include <boost/foreach.hpp>
 #include <boost/bind.hpp>
 #include <boost/asio/placeholders.hpp>
@@ -137,7 +138,7 @@ void DnsCache::save_to_cachefile()
             std::string cache_save_time_str = boost::posix_time::to_iso_string(
                             boost::posix_time::second_clock::universal_time() );
 
-            std::ofstream ofs( CacheFile.c_str() );
+            I2n::tmpofcopystream ofs( CacheFile.c_str() );
             boost::archive::xml_oarchive oa(ofs);
             oa & BOOST_SERIALIZATION_NVP(IpCache);
             oa & BOOST_SERIALIZATION_NVP(CnameCache);