From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id B8636A00C2;
	Fri, 14 Oct 2022 19:23:33 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 5A9174021E;
	Fri, 14 Oct 2022 19:23:33 +0200 (CEST)
Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com
 [209.85.215.173])
 by mails.dpdk.org (Postfix) with ESMTP id 69C64400D4
 for <dev@dpdk.org>; Fri, 14 Oct 2022 19:23:32 +0200 (CEST)
Received: by mail-pg1-f173.google.com with SMTP id l6so4867051pgu.7
 for <dev@dpdk.org>; Fri, 14 Oct 2022 10:23:32 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=networkplumber-org.20210112.gappssmtp.com; s=20210112;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=WnLV3+MkmAhSYd+U85tcQKdaxgU9aQwyN97rSBM4xrM=;
 b=QSPM8+k1ZQcqlpemAZC3sbkYC99SwmEsQGXniQ6NoRjph+HV92t6GYlL1U521nSh2N
 9lTXscTKAdzCbf+NTv2Z2kO3ljxMNd7o099TAKHXqR29tE2YnyKSb9eFjsFMxwaZMkRP
 UFAOHEDX1royMYLfW8StZS3Frcj/IpvfhZfaCG90y5rr+rJeT+4dXPP36iP3xHuO36tk
 l09ZcxxbzWfSEM6vpo2E4AKztXBYVSyOfDvFs3RvmYe8tACn9mfRuZwAjRUZcixUsl6c
 H8UkIWN/XDFnDshXX7zWfgxSJQ0qgRKzHn+9kTlMyqpb3DxyKDZSb/xhPbLDm6gqZUub
 kWiw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 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=WnLV3+MkmAhSYd+U85tcQKdaxgU9aQwyN97rSBM4xrM=;
 b=5jQ12jdUGcwK07vH0FsiocLk9JQx6KC3Y9LkGG5eQ3sAYKSvB5xxwiV3lwvwcycYIb
 Azby+pmhHtKJ8yPEl/nINs6vKntw3LBd49ny/3cZviilxI09vGQeZpkbbZbWQ8oBFsBe
 sDKmCJhlerLBGKRh+adnp+I04jUbaszrculas9hzVJ5+zf9daTyQY7GXnqB7L/KJRxs7
 yUqDE6dqb7SAsqCh27rrD2G0LattI4ZZx5Z0cXO60AEj94GveRFjunIA8w7kC04kwsSE
 S5aFXGZtJG/QV5gwmjqohvrhT5tA5PWpfIR6ABxL9GXKDd2CLusxR+7cTx+GEVCqgPQe
 IFqA==
X-Gm-Message-State: ACrzQf3UuiXQFuWuxnFdA5R1uDZRekCAwoeU/NY7JbziyUdzPMJvU3ly
 ccCncFRmUtBaRYUP2bPIXmN785KYAacpLw==
X-Google-Smtp-Source: AMsMyM5FAHsnA0fF1SZz7bQFzB/7uBL3jj9QJw0cSm+cws/HS33jyu9pWONRHhG1Wf4ZmTFR3Qc4Lg==
X-Received: by 2002:a63:df10:0:b0:43b:e82f:e01c with SMTP id
 u16-20020a63df10000000b0043be82fe01cmr5463686pgg.19.1665768211227; 
 Fri, 14 Oct 2022 10:23:31 -0700 (PDT)
Received: from hermes.local (204-195-120-218.wavecable.com. [204.195.120.218])
 by smtp.gmail.com with ESMTPSA id
 g20-20020aa796b4000000b00561beff1e09sm1986520pfk.164.2022.10.14.10.23.30
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 14 Oct 2022 10:23:30 -0700 (PDT)
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC 2/2] testpmd: cleanup cleanly from signal
Date: Fri, 14 Oct 2022 10:23:28 -0700
Message-Id: <20221014172328.185219-2-stephen@networkplumber.org>
X-Mailer: git-send-email 2.35.1
In-Reply-To: <20221014172328.185219-1-stephen@networkplumber.org>
References: <20221014172328.185219-1-stephen@networkplumber.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

The original behavior of testpmd was to kill itself when
it received a SIGINT or SIGTERM. This makes it hard to use
testpmd in test automation where forwarding loop is started
and then stopped via SIGTERM.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test-pmd/testpmd.c | 76 +++++++++++++++++++++++++-----------------
 1 file changed, 46 insertions(+), 30 deletions(-)

diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 815dd6dab4e3..8c19ded1655e 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <errno.h>
 #include <stdbool.h>
+#include <poll.h>
 
 #include <sys/queue.h>
 #include <sys/stat.h>
@@ -4243,25 +4244,37 @@ print_stats(void)
 static void
 signal_handler(int signum)
 {
-	if (signum == SIGINT || signum == SIGTERM) {
-		fprintf(stderr, "\nSignal %d received, preparing to exit...\n",
-			signum);
-#ifdef RTE_LIB_PDUMP
-		/* uninitialize packet capture framework */
-		rte_pdump_uninit();
-#endif
-#ifdef RTE_LIB_LATENCYSTATS
-		if (latencystats_enabled != 0)
-			rte_latencystats_uninit();
-#endif
-		force_quit();
-		/* Set flag to indicate the force termination. */
-		f_quit = 1;
-		/* exit with the expected status */
-#ifndef RTE_EXEC_ENV_WINDOWS
-		signal(signum, SIG_DFL);
-		kill(getpid(), signum);
-#endif
+	fprintf(stderr, "\nSignal %d %s received, preparing to exit...\n",
+		signum, strsignal(signum));
+
+	/* Set flag to indicate the force termination. */
+	f_quit = 1;
+}
+
+static int
+wait_for_input(void)
+{
+	struct pollfd pfd = {
+		.fd = STDIN_FILENO,
+		.events =  POLLIN,
+	};
+	char c;
+
+	printf("Press enter to exit\n");
+	for (;;) {
+		if (f_quit)
+			return 0;
+
+		if (poll(&pfd, 1, -1) < 0) {
+			if (errno == EINTR)
+				continue;
+			return -1;
+		}
+
+		if (read(STDIN_FILENO, &c, 1) < 0)
+			return -1;
+
+		return 0;
 	}
 }
 
@@ -4441,11 +4454,6 @@ main(int argc, char** argv)
 	} else
 #endif
 	{
-		char c;
-		int rc;
-
-		f_quit = 0;
-
 		printf("No commandline core given, start packet forwarding\n");
 		start_packet_forwarding(tx_first);
 		if (stats_period != 0) {
@@ -4468,15 +4476,23 @@ main(int argc, char** argv)
 				prev_time = cur_time;
 				rte_delay_us_sleep(US_PER_S);
 			}
+		} else {
+			if (wait_for_input() < 0)
+				return 1;
 		}
-
-		printf("Press enter to exit\n");
-		rc = read(0, &c, 1);
-		pmd_test_exit();
-		if (rc < 0)
-			return 1;
+		stop_packet_forwarding();
+		force_quit();
 	}
 
+#ifdef RTE_LIB_PDUMP
+	/* uninitialize packet capture framework */
+	rte_pdump_uninit();
+#endif
+#ifdef RTE_LIB_LATENCYSTATS
+	if (latencystats_enabled != 0)
+		rte_latencystats_uninit();
+#endif
+
 	ret = rte_eal_cleanup();
 	if (ret != 0)
 		rte_exit(EXIT_FAILURE,
-- 
2.35.1