From nobody Mon May 13 09:13:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1646823282; cv=none; d=zohomail.com; s=zohoarc; b=cXW7xkNXedh0+R+oJXHx7aPDm86jny7xpIewqSzy/B2VOi4mT6OyY0xcFO/2FBxFxaHSa6DksfzITMl48n58racKPw1okrUkaElysVezQ5LECrZ0yX1m+kLY4jaAFeqXZ6MlfL74FJc5aGSAUw8Xg40ytvibbWklx2jUdgHFiGc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646823282; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=sWpBbdSIDqD1VBr6ZEBCray/QBuyfm01uceqiyzESaw=; b=iv9m+KFQf3oEHT+XPWQtQ402ItFgqAEnSyZd3fVj1BQqkOKPULvkcHqAW8WjvIj6XEAKamDf6XyIw1eRVxD8WzCC1H0hlALlbPpZEH3cXMdqK1oWqzV9Q6YKtes8/xtQUSAXo/mOMPub2ygi+wQ/Q55goAro0dl8rRECswQUATc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1646823282311302.6532850948205; Wed, 9 Mar 2022 02:54:42 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-7-4DOJzFM5Mw606CK8zfBknw-1; Wed, 09 Mar 2022 05:54:40 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8B42D811E80; Wed, 9 Mar 2022 10:54:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0E293C08F13; Wed, 9 Mar 2022 10:54:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C798B1932121; Wed, 9 Mar 2022 10:54:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 2B3C81932104 for ; Wed, 9 Mar 2022 10:54:36 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id B8A247B6FA; Wed, 9 Mar 2022 10:54:35 +0000 (UTC) Received: from domokun.gsslab.fab.redhat.com (gx270-2.gsslab.fab.redhat.com [10.33.8.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 341527B6F4; Wed, 9 Mar 2022 10:54:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646823282; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=sWpBbdSIDqD1VBr6ZEBCray/QBuyfm01uceqiyzESaw=; b=KAHfVuKsKo0IBdrHUDXrm72IkLh9xOuixeKLApsOZdisVa8rXEC/iSJch94L5Ta7Wqlo+C /mLYY468m403thVJ+LBHoiAptBBcywaDg4ynUMVfA50XWTBwqkfgFwRbP+IXJokwx6hId3 8xN46cSR6+/QsyVHHIVzvZumWsPTX7k= X-MC-Unique: 4DOJzFM5Mw606CK8zfBknw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [PATCH] run: gracefully handle SIGHUP, SIGQUIT, SIGTERM Date: Wed, 9 Mar 2022 10:54:33 +0000 Message-Id: <20220309105433.788649-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1646823284337100001 When using thue 'run' script to launch a daemon, it is intended to temporarily stop the systemd units and re-start them again after. When using this script over an SSH connection, it will get SIGHUP if the connection goes away, and in this case it fails to re-start the systemd units. We need to catch SIGHUP and turn it into a normal python exception. For good measure we do the same for SIGQUIT and SIGTERM too. SIGINT already gets turned into an exception by default which we handle. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: J=C3=A1n Tomko --- run.in | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/run.in b/run.in index 3be7818d32..c6d3411082 100644 --- a/run.in +++ b/run.in @@ -43,6 +43,7 @@ import os import os.path import random +import signal import sys import subprocess =20 @@ -155,6 +156,13 @@ else: print("Temporarily stopping systemd units...") stopped_units =3D [] =20 + def sighandler(signum, frame): + raise OSError("Signal %d received, terminating" % signum) + + signal.signal(signal.SIGHUP, sighandler) + signal.signal(signal.SIGTERM, sighandler) + signal.signal(signal.SIGQUIT, sighandler) + try: for unit in try_stop_units: print(" > %s" % unit) @@ -167,6 +175,8 @@ else: ret =3D subprocess.call(args, env=3Denv) except KeyboardInterrupt: pass + except Exception as e: + print("%s" % e, file=3Dsys.stderr) finally: print("Re-starting original systemd units...") stopped_units.reverse() --=20 2.35.1