public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Sergey Bugaev <bugaevc@gmail.com>
To: libc-alpha@sourceware.org
Cc: bug-hurd@gnu.org, "Samuel Thibault" <samuel.thibault@gnu.org>,
	"Adhemerval Zanella Netto" <adhemerval.zanella@linaro.org>,
	"Cristian Rodríguez" <crrodriguez@opensuse.org>,
	"Sergey Bugaev" <bugaevc@gmail.com>
Subject: [RFC PATCH v2 2/7] misc: Ignore SIGHUP in daemon () while forking
Date: Wed, 19 Apr 2023 19:02:02 +0300	[thread overview]
Message-ID: <20230419160207.65988-3-bugaevc@gmail.com> (raw)
In-Reply-To: <20230419160207.65988-1-bugaevc@gmail.com>

Under certain conditions, SIGHUP will be sent to the child process when
the parent process (the one calling daemon ()) terminates -- namely, if
the parent process was the session leader and the child process was in
the same session (which will be the case after fork and until setsid)
and in the foreground process group. To prevent this SIGHUP from killing
the child, temporarily ignore it. Once we leave the parent's session, we
can restore the original SIGHUP sigaction.

Or that's what you'd hope would happen. Unfortunately for us, nothing
guarantess that signal delivery is synchronous. This means that a SIGHUP
may be generated and enqueued for the child process while it's still a
member of the original session, but only delivered to it some time
later, once it has already left the session and restored the original
sigaction.

Still, on many systems signal delivery is synchronous enough, and all
pending signals will get reliably delivered upon performing a syscall,
specifically setsid () in this case, so this change is still worth it.

Also, do not ignore erros from chdir ("/").

Suggested-by: Cristian Rodríguez <crrodriguez@opensuse.org>
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 misc/daemon.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/misc/daemon.c b/misc/daemon.c
index 14577e40..58dde4f0 100644
--- a/misc/daemon.c
+++ b/misc/daemon.c
@@ -35,6 +35,7 @@ static char sccsid[] = "@(#)daemon.c	8.1 (Berkeley) 6/4/93";
 #include <fcntl.h>
 #include <paths.h>
 #include <unistd.h>
+#include <signal.h>
 #include <sys/stat.h>
 
 #include <device-nrs.h>
@@ -44,6 +45,14 @@ int
 daemon (int nochdir, int noclose)
 {
   int fd;
+  int set_sigaction;
+  struct sigaction act, oact;
+
+  /* When the parent process exits, the child might get a SIGHUP if the parent
+     was a session leader.  Arrange things so that it doesn't terminate us.  */
+  memset (&act, 0, sizeof (act));
+  act.sa_handler = SIG_IGN;
+  set_sigaction = __libc_sigaction (SIGHUP, &act, &oact) == 0;
 
   switch (__fork ())
     {
@@ -60,8 +69,14 @@ daemon (int nochdir, int noclose)
   if (__setsid () == -1)
     return -1;
 
+  /* Now that we have left the parent's session, we should no longer be at
+     risk of receiving SIGHUP because of the parent process exiting.  */
+  if (__glibc_likely (set_sigaction))
+    __libc_sigaction (SIGHUP, &oact, NULL);
+
   if (!nochdir)
-    (void) __chdir ("/");
+    if (__glibc_unlikely (__chdir ("/") == -1))
+      return -1;
 
   if (!noclose)
     {
-- 
2.40.0


  parent reply	other threads:[~2023-04-19 16:02 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-19 16:02 [RFC PATCH v2 0/7] O_IGNORE_CTTY everywhere & misc fixes Sergey Bugaev
2023-04-19 16:02 ` [RFC PATCH v2 1/7] misc: Convert daemon () to GNU coding style Sergey Bugaev
2023-04-21 12:18   ` Adhemerval Zanella Netto
2023-04-22 11:47     ` Samuel Thibault
2023-04-19 16:02 ` Sergey Bugaev [this message]
2023-04-21 12:55   ` [RFC PATCH v2 2/7] misc: Ignore SIGHUP in daemon () while forking Adhemerval Zanella Netto
2023-04-19 16:02 ` [RFC PATCH v2 3/7] Use O_CLOEXEC in more places (BZ #15722) Sergey Bugaev
2023-04-21 12:55   ` Adhemerval Zanella Netto
2023-04-22 11:50     ` Samuel Thibault
2023-04-19 16:02 ` [RFC PATCH v2 4/7] csu: Fix standard fds' mode Sergey Bugaev
2023-04-19 19:13   ` Cristian Rodríguez
2023-04-19 19:40     ` Sergey Bugaev
2023-04-19 20:45       ` Adhemerval Zanella Netto
2023-04-19 21:16         ` Sergey Bugaev
2023-04-20 11:47           ` Adhemerval Zanella Netto
2023-04-20 12:06             ` Cristian Rodríguez
2023-04-20 15:13               ` Adhemerval Zanella Netto
2023-04-21 17:16               ` Paul Eggert
2023-04-19 16:02 ` [RFC PATCH v2 5/7] hurd: Make dl-sysdep's open () cope with O_IGNORE_CTTY Sergey Bugaev
2023-04-20 21:06   ` Samuel Thibault
2023-04-19 16:02 ` [RFC PATCH v2 6/7] include/fcntl.h: Define O_IGNORE_CTTY Sergey Bugaev
2023-04-19 16:02 ` [RFC PATCH v2 7/7] Use O_IGNORE_CTTY where appropriate Sergey Bugaev

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

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

  git send-email \
    --in-reply-to=20230419160207.65988-3-bugaevc@gmail.com \
    --to=bugaevc@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=bug-hurd@gnu.org \
    --cc=crrodriguez@opensuse.org \
    --cc=libc-alpha@sourceware.org \
    --cc=samuel.thibault@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).