From patchwork Fri Jan 19 16:39:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 25458 Received: (qmail 40872 invoked by alias); 19 Jan 2018 16:40:02 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 40847 invoked by uid 89); 19 Jan 2018 16:40:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=rights X-HELO: mx1.redhat.com Subject: Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890] To: Paul Eggert , libc-alpha@sourceware.org References: <20180112110757.D4D2440172552@oldenburg.str.redhat.com> <65555278-8167-d0a5-2ec6-e0f2c812c703@cs.ucla.edu> From: Florian Weimer Message-ID: Date: Fri, 19 Jan 2018 17:39:56 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <65555278-8167-d0a5-2ec6-e0f2c812c703@cs.ucla.edu> On 01/13/2018 03:28 AM, Paul Eggert wrote: > Florian Weimer wrote: >>     if (write (fd, strings, total_strlen) != (ssize_t) total_strlen >> -      || close (fd)) >> +      || fsync (fd) != 0 >> +      || close (fd) != 0) >>       error (EXIT_FAILURE, errno, _("Writing of cache data failed")); > > Would fdatasync suffice, instead of fsync? One can be fdatasync, I think. The first one should actually happen after the chmod call, and then we need fsync. Thanks, Florian Reviewed-by: Adhemerval Zanella Subject: [PATCH] ldconfig: Sync temporary files to disk before renaming them [BZ #20890] To: libc-alpha@sourceware.org If the system crashes before the file data has been written to disk, the file system recovery upon the next mount may restore a partially rewritten temporary file under the non-temporary (final) name (after the rename operation). 2018-01-12 Florian Weimer [BZ #20890] * elf/cache.c (save_cache): Call fsync on temporary file before renaming it. (save_aux_cache): Call fdatasync on temporary file before renaming it. diff --git a/elf/cache.c b/elf/cache.c index 1ec6ab36e7..f032081e5c 100644 --- a/elf/cache.c +++ b/elf/cache.c @@ -448,8 +448,7 @@ save_cache (const char *cache_name) error (EXIT_FAILURE, errno, _("Writing of cache data failed")); } - if (write (fd, strings, total_strlen) != (ssize_t) total_strlen - || close (fd)) + if (write (fd, strings, total_strlen) != (ssize_t) total_strlen) error (EXIT_FAILURE, errno, _("Writing of cache data failed")); /* Make sure user can always read cache file */ @@ -458,6 +457,10 @@ save_cache (const char *cache_name) _("Changing access rights of %s to %#o failed"), temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR); + /* Make sure that data is written to disk. */ + if (fsync (fd) != 0 || close (fd) != 0) + error (EXIT_FAILURE, errno, _("Writing of cache data failed")); + /* Move temporary to its final location. */ if (rename (temp_name, cache_name)) error (EXIT_FAILURE, errno, _("Renaming of %s to %s failed"), temp_name, @@ -812,7 +815,8 @@ save_aux_cache (const char *aux_cache_name) if (write (fd, file_entries, file_entries_size + total_strlen) != (ssize_t) (file_entries_size + total_strlen) - || close (fd)) + || fdatasync (fd) != 0 + || close (fd) != 0) { unlink (temp_name); goto out_fail;