unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Henrik Grimler <henrik@grimler.se>
To: Lars Ingebrigtsen <larsi@gnus.org>, eliz@gnu.org
Cc: 47596@debbugs.gnu.org
Subject: bug#47596: File descriptor error when exiting emacs on android 11
Date: Tue, 11 May 2021 19:06:11 +0200	[thread overview]
Message-ID: <3eca4ad4c693735b4c9d76b3d3301287086fed4b.camel@grimler.se> (raw)
In-Reply-To: <8a9fbefead009fce76aa5a28315c0fedf3ce7bec.camel@grimler.se>

[-- Attachment #1: Type: text/plain, Size: 492 bytes --]

Hi again,

Enh at google suggested [1] that `setlinebuf (stderr)` could be an
alternative instead of opening a copy of stderr.  I tried out the
attached patch, which removes the buferr variable and uses setlinebuf
on stderr instead, and it seem to work (in the sense that emacs opens
and closes without errors) on archlinux as well as android. 

Could this work or do we need to have both buferr and stderr?

Best regards,
Henrik Grimler

[1] https://issuetracker.google.com/issues/184380442

[-- Attachment #2: line_buffered_stderr.patch --]
[-- Type: text/x-patch, Size: 2343 bytes --]

commit ef3beaf664347f7fce1f3475fe65de729320837e
Author: Henrik Grimler <henrik@grimler.se>
Date:   Tue May 11 09:36:52 2021 +0200

    Use setlinebuf to make stderr line buffered
    
    If atomic writes are supported.  Android >= 10 has a file descriptor
    sanitizer which errors if a stream is closed while there are still
    fd's associated with it. In emacs stderr is opened twice if line
    buffered streams are supported.  Rather than opening a copy we can
    make stderr line buffered with setlinebuf instead.

diff --git a/src/sysdep.c b/src/sysdep.c
index d940acc4e0..77e1a0be3c 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -232,10 +232,6 @@ force_open (int fd, int flags)
     }
 }
 
-/* A stream that is like stderr, except line buffered.  It is NULL
-   during startup, or if line buffering is not in use.  */
-static FILE *buferr;
-
 /* Make sure stdin, stdout, and stderr are open to something, so that
    their file descriptors are not hijacked by later system calls.  */
 void
@@ -249,12 +245,10 @@ init_standard_fds (void)
   force_open (STDOUT_FILENO, O_RDONLY);
   force_open (STDERR_FILENO, O_RDONLY);
 
-  /* Set buferr if possible on platforms defining _PC_PIPE_BUF, as
+  /* Make stderr line buffered on platforms defining _PC_PIPE_BUF, as
      they support the notion of atomic writes to pipes.  */
   #ifdef _PC_PIPE_BUF
-    buferr = fdopen (STDERR_FILENO, "w");
-    if (buferr)
-      setvbuf (buferr, NULL, _IOLBF, 0);
+    setlinebuf (stderr);
   #endif
 }
 
@@ -2650,11 +2644,11 @@ safe_strsignal (int code)
 static FILE *
 errstream (void)
 {
-  FILE *err = buferr;
-  if (!err)
-    return stderr;
+#ifdef _PC_PIPE_BUF
+  /* flush line buffered stderr */
   fflush_unlocked (stderr);
-  return err;
+#endif
+  return stderr;
 }
 
 /* These functions are like fputc, vfprintf, and fwrite,
@@ -2696,9 +2690,12 @@ close_output_streams (void)
       _exit (EXIT_FAILURE);
     }
 
+  bool err;
+#ifdef _PC_PIPE_BUF
+  err = (fflush (stderr) != 0 || ferror (stderr));
+#endif
   /* Do not close stderr if addresses are being sanitized, as the
      sanitizer might report to stderr after this function is invoked.  */
-  bool err = buferr && (fflush (buferr) != 0 || ferror (buferr));
   if (err | (ADDRESS_SANITIZER
 	     ? fflush (stderr) != 0 || ferror (stderr)
 	     : close_stream (stderr) != 0))

  reply	other threads:[~2021-05-11 17:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-04 19:20 bug#47596: File descriptor error when exiting emacs on android 11 Henrik Grimler
2021-04-04 19:31 ` Eli Zaretskii
2021-04-04 20:11   ` Henrik Grimler
2021-04-05  8:14     ` Henrik Grimler
2021-04-05  8:59       ` Henrik Grimler
2021-04-05  9:48         ` Henrik Grimler
2021-04-05 12:57           ` Eli Zaretskii
2021-04-05 13:38             ` Henrik Grimler
2021-04-05 12:52         ` Eli Zaretskii
2021-04-05 12:50       ` Eli Zaretskii
2021-04-05 17:29         ` Henrik Grimler
2021-05-06 10:45           ` Lars Ingebrigtsen
2021-05-10  7:23             ` Henrik Grimler
2021-05-11 17:06               ` Henrik Grimler [this message]
2021-05-12 13:52                 ` Lars Ingebrigtsen
2021-05-12 14:01                   ` Eli Zaretskii
2022-06-29 10:47                     ` Lars Ingebrigtsen
2022-06-29 13:25                       ` Henrik Grimler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-06-30  9:07                         ` Lars Ingebrigtsen

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://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3eca4ad4c693735b4c9d76b3d3301287086fed4b.camel@grimler.se \
    --to=henrik@grimler.se \
    --cc=47596@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=larsi@gnus.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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).