From patchwork Wed Apr 19 16:02:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68019 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 81F843854164 for ; Wed, 19 Apr 2023 16:03:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81F843854164 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681920189; bh=W3iCOK5mxCxbiA0+JaMNXURGBSxQx609z4RqD+khiRc=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=fa/6IVvkKqZeSYvVpbRR5lO3YGssTXLkf222+/G0WE056mKw4RZVxTMD15VsGydEM f/Y4H9FMISpmnOEPlQTL6tZz4QGb0F8VxPQKY5EObCBnSzgpSRqRtcVyepgm7o/WPR d/UucuuhwdQFwTBuGvOc2gj4x543NIQjDEZUiy0Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 749D03858CDA for ; Wed, 19 Apr 2023 16:02:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 749D03858CDA Received: by mail-lj1-x236.google.com with SMTP id c3so11532612ljf.7 for ; Wed, 19 Apr 2023 09:02:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681920151; x=1684512151; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=W3iCOK5mxCxbiA0+JaMNXURGBSxQx609z4RqD+khiRc=; b=Vx0RTd6Ht5prg+DMmKjHtMXNezJl6p0YRH942H8wj36mFNFhfXJUK/+GADzhFaIbO+ /ExF/KBBi+RCwqSQY1SqS9XYVrb1Fe5PYiu3hciw2dXsynYhwSVTmjmf2vHXstpb0t3o Ea49JPBZ4vL9Y8C6Aw2iA6mTGWulxb+rfZcsI1i2Ku75Knkl0X2Gnoc2vvMeM9WoLWk6 OqK0zzQ+Bj8Npgn96WLpZHwjHFwwICuHKqlPpZ0haa0hBDS42pnFPIv3ktcYGdAwCFEy AATUnqX8jfgvdS3x4byzoWuSAj8Ors4GHd4J/eBgwN7y6UIB2ygk5CfIspxWmujXW2o0 c4jQ== X-Gm-Message-State: AAQBX9dMy22FP+jPmvMW+/S6456h686SBumpIirRQHJOQmfXwVB24nU6 r/9JU+KtUc/RmBzW3UrjnYVlUI74xfk= X-Google-Smtp-Source: AKy350Z90XVPneGtMBGoy2dW4+6PwyqMmtpev2c8dOM2l/pa1zpp39Vo+AzwzjaWdXtvxhLZ+/HPnA== X-Received: by 2002:a05:651c:205:b0:299:aa20:22a0 with SMTP id y5-20020a05651c020500b00299aa2022a0mr2224379ljn.53.1681920151358; Wed, 19 Apr 2023 09:02:31 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id l1-20020a2e7001000000b002a6007383a0sm2959663ljc.135.2023.04.19.09.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 09:02:30 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault , Adhemerval Zanella Netto , =?utf-8?q?Cristia?= =?utf-8?q?n_Rodr=C3=ADguez?= , Sergey Bugaev Subject: [RFC PATCH v2 2/7] misc: Ignore SIGHUP in daemon () while forking Date: Wed, 19 Apr 2023 19:02:02 +0300 Message-Id: <20230419160207.65988-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419160207.65988-1-bugaevc@gmail.com> References: <20230419160207.65988-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 Signed-off-by: Sergey Bugaev --- 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 #include #include +#include #include #include @@ -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) {