From 939b0c71b4275d340514387c871e29cf8f20006f Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 18:00:04 +0200
Subject: [PATCH] sethostname: Make more robust in multithreaded applications.

* lib/sethostname.c (sethostname): Pass an 'e' flag to fopen.
* modules/sethostname (Depends-on): Add fopen-gnu.
---
 ChangeLog           | 6 ++++++
 lib/sethostname.c   | 2 +-
 modules/sethostname | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 8f89928f4f..80bb80eaf2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-05-27  Bruno Haible  <bruno@clisp.org>
+
+	sethostname: Make more robust in multithreaded applications.
+	* lib/sethostname.c (sethostname): Pass an 'e' flag to fopen.
+	* modules/sethostname (Depends-on): Add fopen-gnu.
+
 2020-05-27  Bruno Haible  <bruno@clisp.org>
 
 	readutmp: Make more robust in multithreaded applications.
diff --git a/lib/sethostname.c b/lib/sethostname.c
index 87b3af958b..1be69beabb 100644
--- a/lib/sethostname.c
+++ b/lib/sethostname.c
@@ -52,7 +52,7 @@ sethostname (const char *name, size_t len)
        these are appropriate for us to set, even if they may match the
        situation, during failed open/write/close operations, so we
        leave errno alone and rely on what the system sets up. */
-    hostf = fopen ("/etc/hostname.file", "w");
+    hostf = fopen ("/etc/hostname.file", "we");
     if (hostf == NULL)
       r = -1;
     else
diff --git a/modules/sethostname b/modules/sethostname
index f3c0d134b5..92b4a2b98f 100644
--- a/modules/sethostname
+++ b/modules/sethostname
@@ -9,6 +9,7 @@ m4/gethostname.m4
 Depends-on:
 unistd
 errno           [test $HAVE_SETHOSTNAME = 0]
+fopen-gnu       [test $HAVE_SETHOSTNAME = 0]
 
 configure.ac:
 gl_FUNC_SETHOSTNAME
-- 
2.39.5