Skip to contentSkip to conversation

trace-cmd: Cleanups for --daemonize option

PATCH New Steven Rostedt 1 participant 3 messages Oct 17, 2023

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Cleanups for --daemonize option

From: "Steven Rostedt (Google)" <[email protected]>

Just a few clean ups for the --daemonize option that I plan on committing upstream soon. This is based on:

https://lore.kernel.org/all/[email protected]/

The first patch lets the daemon be killed with SIGTERM as well as with SIGINT, as that's the default of "kill" without any option.

The second patch just renames a variable from "rc" to "ret" to be consistent with the rest of the code.

Steven Rostedt (Google) (2):
trace-cmd record: Use SIGTERM as well for --daemonize trace-cmd record: Cleanup - be consistent with return status variable

 tracecmd/trace-record.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/2 trace-cmd record: Use SIGTERM as well for --daemonize

From: "Steven Rostedt (Google)" <[email protected]>

Currently --daemonize expects a SIGINT to stop the daemon and build the trace.dat file. But if the user just does a:

kill <pid>

it will send a SIGTERM. This will actually kill the process and it will not create the trace.dat file, leaving a bunch of trace.dat.cpuX files just lying around.

Update it to also do the same with SIGTERM.

---
 tracecmd/trace-record.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tracecmd/trace-record.c +4 -1
index 69a10bf1f0bd..c943f486291e 100644
@@ -3344,6 +3344,7 @@ static void do_sig(int sig)
 	case SIGALRM:
 	case SIGUSR1:
 	case SIGINT:
+	case SIGTERM:
 		return finish();
 	case SIGUSR2:
 		return flush();
@@ -3591,6 +3592,7 @@ static int create_recorder(struct buffer_instance *instance, int cpu,
 			return pid;
 
 		signal(SIGINT, SIG_IGN);
+		signal(SIGTERM, SIG_IGN);
 		signal(SIGUSR1, do_sig);
 		signal(SIGUSR2, do_sig);
 		signal(SIGALRM, do_sig);
@@ -7146,6 +7148,7 @@ static void record_trace(int argc, char **argv,
 
 	if (type & (TRACE_TYPE_RECORD | TRACE_TYPE_STREAM)) {
 		signal(SIGINT, do_sig);
+		signal(SIGTERM, do_sig);
 		if (!latency)
 			start_threads(type, ctx);
 	}
@@ -7182,7 +7185,7 @@ static void record_trace(int argc, char **argv,
 
 		if (do_daemonize) {
 			daemonize_finish();
-			printf("Send SIGINT to pid %d to stop recording\n", getpid());
+			printf("Send SIGINT/SIGTERM to pid %d to stop recording\n", getpid());
 		} else {
 			/* sleep till we are woken with Ctrl^C */
 			printf("Hit Ctrl^C to stop recording\n");
Signature · 1 line
2.42.0
#2 lore raw

PATCH 2/2 trace-cmd record: Cleanup - be consistent with return status variable

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

The variable that checks return status in daemonize_start() is "rc". To be consistent with the rest of the code, change it to "ret".

Also check for less than zero instead of -1.

---
 tracecmd/trace-record.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tracecmd/trace-record.c +4 -4
index c943f486291e..022c27ffc294 100644
@@ -1655,7 +1655,7 @@ static void daemonize_start(void)
 	int devnull;
 	int status;
 	int pid;
-	int rc;
+	int ret;
 
 	pid = fork();
 	if (pid == -1)
@@ -1719,12 +1719,12 @@ static void daemonize_start(void)
 			die("daemonize: sigaction failed");
 
 		do {
-			rc = waitpid(pid, &status, 0);
-		} while (!child_detached && ((rc == -1) && (errno == EINTR)));
+			ret = waitpid(pid, &status, 0);
+		} while (!child_detached && ((ret < 0) && (errno == EINTR)));
 
 		if (child_detached)
 			exit(0);
-		else if (rc == pid)
+		else if (ret == pid)
 			exit(WIFEXITED(status));
 		else
 			die("daemonize: waitpid failed");
Signature · 1 line
2.42.0