[DTrace-devel] [PATCH 07/12] Set interrupt signal handler earlier

eugene.loh at oracle.com eugene.loh at oracle.com
Sat Jul 11 17:38:31 PDT 2020


From: Eugene Loh <eugene.loh at oracle.com>

We were creating probes and attaching BPF programs to them before
setting the interrupt signal handler.  That meant that there was a
window during which an interrupt signal would kill dtrace without
probe cleanup taking place, polluting /sys/kernel/debug/tracing with
custom DTrace uprobes and kprobes.

Set the interrupt signal handler before probes are attached and BPF
programs created.  One can test g_intr==0 more liberally to bypass
operations and move to dt_close() more quickly.

Signed-off-by: Eugene Loh <eugene.loh at oracle.com>
---
 cmd/dtrace.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/cmd/dtrace.c b/cmd/dtrace.c
index cef86ef2..9c49d16b 100644
--- a/cmd/dtrace.c
+++ b/cmd/dtrace.c
@@ -1373,10 +1373,18 @@ main(int argc, char *argv[])
 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
 			fatal("failed to open output file '%s'", g_ofile);
 
+		sigemptyset(&act.sa_mask);
+		act.sa_flags = 0;
+		act.sa_handler = intr;
+		if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
+			sigaction(SIGINT, &act, NULL);
+		if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
+			sigaction(SIGTERM, &act, NULL);
+
 		for (i = 0; i < g_cmdc; i++)
 			exec_prog(&g_cmdv[i]);
 
-		if (done) {
+		if (done || g_intr) {
 			dtrace_close(g_dtp);
 			return (g_status);
 		}
@@ -1480,6 +1488,8 @@ main(int argc, char *argv[])
 	/*
 	 * Start tracing.
 	 */
+	if (g_intr)
+		goto out;
 	go();
 
 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
@@ -1492,16 +1502,6 @@ main(int argc, char *argv[])
 	if (opt != DTRACEOPT_UNSET)
 		notice("allowing destructive actions\n");
 
-	(void) sigemptyset(&act.sa_mask);
-	act.sa_flags = 0;
-	act.sa_handler = intr;
-
-	if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
-		(void) sigaction(SIGINT, &act, NULL);
-
-	if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
-		(void) sigaction(SIGTERM, &act, NULL);
-
 	/*
 	 * Now that tracing is active and we are ready to consume trace data,
 	 * continue any grabbed or created processes, setting them running
@@ -1509,6 +1509,8 @@ main(int argc, char *argv[])
 	 */
 	for (i = 0; i < g_psc; i++)
 		dtrace_proc_continue(g_dtp, g_psv[i]);
+	if (g_intr)
+		goto release_procs;
 
 	g_pslive = g_psc; /* count for prochandler() */
 
@@ -1558,9 +1560,11 @@ main(int argc, char *argv[])
 	}
 #endif
 
+release_procs:
 	for (i = 0; i < g_psc; i++)
 		dtrace_proc_release(g_dtp, g_psv[i]);
 
+out:
 	dtrace_close(g_dtp);
 
 	free(g_argv);
-- 
2.18.2




More information about the DTrace-devel mailing list