From: Bruno Haible <bruno@clisp.org>
To: Daiki Ueno <ueno@gnu.org>
Cc: "Tim Rühsen" <tim.ruehsen@gmx.de>, bug-gnulib@gnu.org
Subject: Re: portability of fopen and 'e' (O_CLOEXEC) flag
Date: Tue, 26 May 2020 18:09:03 +0200 [thread overview]
Message-ID: <2446740.5YXOBombHr@omega> (raw)
In-Reply-To: <2318672.ckYohbHQXy@omega>
[-- Attachment #1: Type: text/plain, Size: 345 bytes --]
Hi Daiki,
> > Thank you for this; would it make sense to use it in the modules that do
> > one-shot fopen/fclose, such as read-file?
Here are proposed patches for other modules. Does this look right?
Note: The patch to sethostname.c is only relevant for Minix. Minix does not
have multithreading now, but a future version likely will.
Bruno
[-- Attachment #2: 0001-bitset-Make-more-robust-in-multithreaded-application.patch --]
[-- Type: text/x-patch, Size: 1902 bytes --]
From acb3be326fbbeaf82af33fbd7bb7b3cb180d2616 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 17:51:03 +0200
Subject: [PATCH 1/8] bitset: Make more robust in multithreaded applications.
* lib/bitset/stats.c (bitset_stats_read, bitset_stats_write): Pass an
'e' flag to fopen.
* modules/bitset (Depends-on): Add fopen-gnu.
---
ChangeLog | 7 +++++++
lib/bitset/stats.c | 4 ++--
modules/bitset | 1 +
3 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 07d4d51..ca9fbb3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2020-05-26 Bruno Haible <bruno@clisp.org>
+
+ bitset: Make more robust in multithreaded applications.
+ * lib/bitset/stats.c (bitset_stats_read, bitset_stats_write): Pass an
+ 'e' flag to fopen.
+ * modules/bitset (Depends-on): Add fopen-gnu.
+
2020-05-26 Daiki Ueno <ueno@gnu.org>
read-file: make use of fopen-gnu
diff --git a/lib/bitset/stats.c b/lib/bitset/stats.c
index 10aa5d7..5bd44c0 100644
--- a/lib/bitset/stats.c
+++ b/lib/bitset/stats.c
@@ -245,7 +245,7 @@ bitset_stats_read (const char *file_name)
if (!file_name)
file_name = BITSET_STATS_FILE;
- FILE *file = fopen (file_name, "r");
+ FILE *file = fopen (file_name, "re");
if (file)
{
if (fread (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
@@ -273,7 +273,7 @@ bitset_stats_write (const char *file_name)
if (!file_name)
file_name = BITSET_STATS_FILE;
- FILE *file = fopen (file_name, "w");
+ FILE *file = fopen (file_name, "we");
if (file)
{
if (fwrite (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
diff --git a/modules/bitset b/modules/bitset
index ec7f34b..20c6806 100644
--- a/modules/bitset
+++ b/modules/bitset
@@ -19,6 +19,7 @@ lib/bitset/vector.h
Depends-on:
attribute
c99
+fopen-gnu
gettext-h
obstack
xalloc
--
2.7.4
[-- Attachment #3: 0002-exclude-Make-more-robust-in-multithreaded-applicatio.patch --]
[-- Type: text/x-patch, Size: 1658 bytes --]
From 67d0dc62291be365765dac3fb9e8c006d8427097 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 17:52:23 +0200
Subject: [PATCH 2/8] exclude: Make more robust in multithreaded applications.
* lib/exclude.c (add_exclude_file): Pass an 'e' flag to fopen.
* modules/exclude (Depends-on): Add fopen-gnu.
---
ChangeLog | 6 ++++++
lib/exclude.c | 2 +-
modules/exclude | 1 +
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index ca9fbb3..e766479 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2020-05-26 Bruno Haible <bruno@clisp.org>
+ exclude: Make more robust in multithreaded applications.
+ * lib/exclude.c (add_exclude_file): Pass an 'e' flag to fopen.
+ * modules/exclude (Depends-on): Add fopen-gnu.
+
+2020-05-26 Bruno Haible <bruno@clisp.org>
+
bitset: Make more robust in multithreaded applications.
* lib/bitset/stats.c (bitset_stats_read, bitset_stats_write): Pass an
'e' flag to fopen.
diff --git a/lib/exclude.c b/lib/exclude.c
index c63c004..2b57a9b 100644
--- a/lib/exclude.c
+++ b/lib/exclude.c
@@ -683,7 +683,7 @@ add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
if (use_stdin)
in = stdin;
- else if (! (in = fopen (file_name, "r")))
+ else if (! (in = fopen (file_name, "re")))
return -1;
rc = add_exclude_fp (call_addfn, ex, in, options, line_end, &add_func);
diff --git a/modules/exclude b/modules/exclude
index 5ff0539..8fe2708 100644
--- a/modules/exclude
+++ b/modules/exclude
@@ -8,6 +8,7 @@ lib/exclude.c
Depends-on:
filename
fnmatch
+fopen-gnu
hash
mbscasecmp
mbuiter
--
2.7.4
[-- Attachment #4: 0003-getloadavg-Make-more-robust-in-multithreaded-applica.patch --]
[-- Type: text/x-patch, Size: 1651 bytes --]
From 350c6f8dba8cc2a6266cb8a350d01c49805af9db Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 17:53:47 +0200
Subject: [PATCH 3/8] getloadavg: Make more robust in multithreaded
applications.
* lib/getloadavg.c (getloadavg): Pass an 'e' flag to fopen.
* modules/getloadavg (Depends-on): Add fopen-gnu.
---
ChangeLog | 6 ++++++
lib/getloadavg.c | 2 +-
modules/getloadavg | 1 +
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index e766479..7dabc5e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2020-05-26 Bruno Haible <bruno@clisp.org>
+ getloadavg: Make more robust in multithreaded applications.
+ * lib/getloadavg.c (getloadavg): Pass an 'e' flag to fopen.
+ * modules/getloadavg (Depends-on): Add fopen-gnu.
+
+2020-05-26 Bruno Haible <bruno@clisp.org>
+
exclude: Make more robust in multithreaded applications.
* lib/exclude.c (add_exclude_file): Pass an 'e' flag to fopen.
* modules/exclude (Depends-on): Add fopen-gnu.
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index ebb6f5d..7e11c32 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -569,7 +569,7 @@ getloadavg (double loadavg[], int nelem)
int count;
FILE *fp;
- fp = fopen (NETBSD_LDAV_FILE, "r");
+ fp = fopen (NETBSD_LDAV_FILE, "re");
if (fp == NULL)
return -1;
count = fscanf (fp, "%lu %lu %lu %lu\n",
diff --git a/modules/getloadavg b/modules/getloadavg
index 1b0f581..e14ddc8 100644
--- a/modules/getloadavg
+++ b/modules/getloadavg
@@ -7,6 +7,7 @@ m4/getloadavg.m4
Depends-on:
extensions
+fopen-gnu
intprops
stdbool
stdlib
--
2.7.4
[-- Attachment #5: 0004-getpass-Make-more-robust-in-multithreaded-applicatio.patch --]
[-- Type: text/x-patch, Size: 1639 bytes --]
From 54e5d001797c755994968a34259bae745ee515e6 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 17:56:31 +0200
Subject: [PATCH 4/8] getpass: Make more robust in multithreaded applications.
* lib/getpass.c (getpass): Pass an 'e' flag to fopen.
* modules/getpass (Depends-on): Add fopen-gnu.
---
ChangeLog | 6 ++++++
lib/getpass.c | 2 +-
modules/getpass | 1 +
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 7dabc5e..93d8b5a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2020-05-26 Bruno Haible <bruno@clisp.org>
+ getpass: Make more robust in multithreaded applications.
+ * lib/getpass.c (getpass): Pass an 'e' flag to fopen.
+ * modules/getpass (Depends-on): Add fopen-gnu.
+
+2020-05-26 Bruno Haible <bruno@clisp.org>
+
getloadavg: Make more robust in multithreaded applications.
* lib/getloadavg.c (getloadavg): Pass an 'e' flag to fopen.
* modules/getloadavg (Depends-on): Add fopen-gnu.
diff --git a/lib/getpass.c b/lib/getpass.c
index af8d72e..3b0552e 100644
--- a/lib/getpass.c
+++ b/lib/getpass.c
@@ -96,7 +96,7 @@ getpass (const char *prompt)
/* Try to write to and read from the terminal if we can.
If we can't open the terminal, use stderr and stdin. */
- tty = fopen ("/dev/tty", "w+");
+ tty = fopen ("/dev/tty", "w+e");
if (tty == NULL)
{
in = stdin;
diff --git a/modules/getpass b/modules/getpass
index 7e73451..74d781a 100644
--- a/modules/getpass
+++ b/modules/getpass
@@ -9,6 +9,7 @@ m4/getpass.m4
Depends-on:
unistd
extensions
+fopen-gnu
fseeko
getline
stdbool
--
2.7.4
[-- Attachment #6: 0005-readutmp-Make-more-robust-in-multithreaded-applicati.patch --]
[-- Type: text/x-patch, Size: 1613 bytes --]
From 56bcba9b1013176d52f1b25a3085695a3bd8d9b8 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 17:57:58 +0200
Subject: [PATCH 5/8] readutmp: Make more robust in multithreaded applications.
* lib/readutmp.c (read_utmp): Pass an 'e' flag to fopen.
* modules/readutmp (Depends-on): Add fopen-gnu.
---
ChangeLog | 6 ++++++
lib/readutmp.c | 2 +-
modules/readutmp | 1 +
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 93d8b5a..893d080 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2020-05-26 Bruno Haible <bruno@clisp.org>
+ readutmp: Make more robust in multithreaded applications.
+ * lib/readutmp.c (read_utmp): Pass an 'e' flag to fopen.
+ * modules/readutmp (Depends-on): Add fopen-gnu.
+
+2020-05-26 Bruno Haible <bruno@clisp.org>
+
getpass: Make more robust in multithreaded applications.
* lib/getpass.c (getpass): Pass an 'e' flag to fopen.
* modules/getpass (Depends-on): Add fopen-gnu.
diff --git a/lib/readutmp.c b/lib/readutmp.c
index 308390d..793d480 100644
--- a/lib/readutmp.c
+++ b/lib/readutmp.c
@@ -132,7 +132,7 @@ read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
size_t n_alloc = 0;
STRUCT_UTMP *utmp = NULL;
int saved_errno;
- FILE *f = fopen (file, "r");
+ FILE *f = fopen (file, "re");
if (! f)
return -1;
diff --git a/modules/readutmp b/modules/readutmp
index 51f6290..e88897c 100644
--- a/modules/readutmp
+++ b/modules/readutmp
@@ -11,6 +11,7 @@ extensions
xalloc
stdbool
stdint
+fopen-gnu
configure.ac:
gl_READUTMP
--
2.7.4
[-- Attachment #7: 0006-sethostname-Make-more-robust-in-multithreaded-applic.patch --]
[-- Type: text/x-patch, Size: 1918 bytes --]
From acdcd9f872f5338f901a9019627953dcb5e8d95f Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 18:00:04 +0200
Subject: [PATCH 6/8] 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 893d080..162b861 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2020-05-26 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-26 Bruno Haible <bruno@clisp.org>
+
readutmp: Make more robust in multithreaded applications.
* lib/readutmp.c (read_utmp): Pass an 'e' flag to fopen.
* modules/readutmp (Depends-on): Add fopen-gnu.
diff --git a/lib/sethostname.c b/lib/sethostname.c
index 87b3af9..1be69be 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 f3c0d13..92b4a2b 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.7.4
[-- Attachment #8: 0007-mountlist-Make-more-robust-in-multithreaded-applicat.patch --]
[-- Type: text/x-patch, Size: 2788 bytes --]
From 7fac4fbbd7203d2fcf3be257872e3c7508b86402 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 18:04:26 +0200
Subject: [PATCH 7/8] mountlist: Make more robust in multithreaded
applications.
* lib/mountlist.c (setmntent, read_file_system_list): Pass an 'e' flag
to fopen.
* modules/mountlist (Depends-on): Add fopen-gnu.
---
ChangeLog | 7 +++++++
lib/mountlist.c | 10 +++++-----
modules/mountlist | 1 +
3 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 162b861..b30f4b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2020-05-26 Bruno Haible <bruno@clisp.org>
+ mountlist: Make more robust in multithreaded applications.
+ * lib/mountlist.c (setmntent, read_file_system_list): Pass an 'e' flag
+ to fopen.
+ * modules/mountlist (Depends-on): Add fopen-gnu.
+
+2020-05-26 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.
diff --git a/lib/mountlist.c b/lib/mountlist.c
index 7abe024..4cb19c8 100644
--- a/lib/mountlist.c
+++ b/lib/mountlist.c
@@ -125,7 +125,7 @@
#ifdef MOUNTED_GETMNTENT1
# if !HAVE_SETMNTENT /* Android <= 4.4 */
-# define setmntent(fp,mode) fopen (fp, mode)
+# define setmntent(fp,mode) fopen (fp, mode "e")
# endif
# if !HAVE_ENDMNTENT /* Android <= 4.4 */
# define endmntent(fp) fclose (fp)
@@ -460,7 +460,7 @@ read_file_system_list (bool need_fs_type)
(and that code is in previous versions of this function), however
libmount depends on libselinux which pulls in many dependencies. */
char const *mountinfo = "/proc/self/mountinfo";
- fp = fopen (mountinfo, "r");
+ fp = fopen (mountinfo, "re");
if (fp != NULL)
{
char *line = NULL;
@@ -794,7 +794,7 @@ read_file_system_list (bool need_fs_type)
char *table = "/etc/mnttab";
FILE *fp;
- fp = fopen (table, "r");
+ fp = fopen (table, "re");
if (fp == NULL)
return NULL;
@@ -852,7 +852,7 @@ read_file_system_list (bool need_fs_type)
by the kernel. */
errno = 0;
- fp = fopen (table, "r");
+ fp = fopen (table, "re");
if (fp == NULL)
ret = errno;
else
@@ -924,7 +924,7 @@ read_file_system_list (bool need_fs_type)
# endif
errno = 0;
- fp = fopen (table, "r");
+ fp = fopen (table, "re");
if (fp == NULL)
ret = errno;
else
diff --git a/modules/mountlist b/modules/mountlist
index 11cf809..5bb45ed 100644
--- a/modules/mountlist
+++ b/modules/mountlist
@@ -8,6 +8,7 @@ m4/fstypename.m4
m4/mountlist.m4
Depends-on:
+fopen-gnu
getline
stdbool
stdint
--
2.7.4
[-- Attachment #9: 0008-javacomp-Make-more-robust-in-multithreaded-applicati.patch --]
[-- Type: text/x-patch, Size: 1676 bytes --]
From 0f0f1ad56a75eb04e091ba84a6707b4431eeb68e Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 26 May 2020 18:05:34 +0200
Subject: [PATCH 8/8] javacomp: Make more robust in multithreaded applications.
* lib/javacomp.c (write_temp_file): Pass an 'e' flag to fopen_temp.
* modules/javacomp (Depends-on): Add fopen-gnu.
---
ChangeLog | 6 ++++++
lib/javacomp.c | 2 +-
modules/javacomp | 1 +
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index b30f4b1..b95b945 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2020-05-26 Bruno Haible <bruno@clisp.org>
+ javacomp: Make more robust in multithreaded applications.
+ * lib/javacomp.c (write_temp_file): Pass an 'e' flag to fopen_temp.
+ * modules/javacomp (Depends-on): Add fopen-gnu.
+
+2020-05-26 Bruno Haible <bruno@clisp.org>
+
mountlist: Make more robust in multithreaded applications.
* lib/mountlist.c (setmntent, read_file_system_list): Pass an 'e' flag
to fopen.
diff --git a/lib/javacomp.c b/lib/javacomp.c
index 9239616..ac56196 100644
--- a/lib/javacomp.c
+++ b/lib/javacomp.c
@@ -573,7 +573,7 @@ write_temp_file (struct temp_dir *tmpdir, const char *file_name,
FILE *fp;
register_temp_file (tmpdir, file_name);
- fp = fopen_temp (file_name, "w");
+ fp = fopen_temp (file_name, "we");
if (fp == NULL)
{
error (0, errno, _("failed to create \"%s\""), file_name);
diff --git a/modules/javacomp b/modules/javacomp
index 4f49bd9..fccdaac 100644
--- a/modules/javacomp
+++ b/modules/javacomp
@@ -22,6 +22,7 @@ xmalloca
getline
xconcat-filename
fwriteerror
+fopen-gnu
clean-temp
stat
error
--
2.7.4
next prev parent reply other threads:[~2020-05-26 16:10 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-11 14:49 portability of fopen and 'e' (O_CLOEXEC) flag Tim Rühsen
2020-05-11 16:37 ` Bruno Haible
2020-05-11 17:41 ` Eric Blake
2020-05-12 8:28 ` Tim Rühsen
2020-05-24 19:22 ` Bruno Haible
2020-05-26 6:01 ` Daiki Ueno
2020-05-26 8:18 ` Bruno Haible
2020-05-26 16:09 ` Bruno Haible [this message]
2020-05-27 18:39 ` Bruno Haible
2020-05-28 10:00 ` Daiki Ueno
2020-05-28 10:04 ` Daiki Ueno
2020-05-28 11:54 ` Bruno Haible
2020-05-29 3:04 ` Daiki Ueno
2020-05-29 6:25 ` Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2446740.5YXOBombHr@omega \
--to=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
--cc=tim.ruehsen@gmx.de \
--cc=ueno@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).