diff mbox series

trace-cmd agent: Move becoming daemon after prints and errors

Message ID 20220314173714.01f226d6@gandalf.local.home (mailing list archive)
State Accepted
Commit 37623725105f54a0708f692dca2ffaa169e652f6
Headers show
Series trace-cmd agent: Move becoming daemon after prints and errors | expand

Commit Message

Steven Rostedt March 14, 2022, 9:37 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

When starting the agent in daemon mode, it should be done after the
connections to the vsocket, so that it can not only show the user what
port it is connected to, but also show any errors that may happen.

Once the task becomes a daemon, it will no longer print its error messages
or normal information to the console. Thus any errors that happen after
the daemon is running will go unnoticed.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 tracecmd/trace-agent.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c
index b83d3ac1338f..a46feea3d3c7 100644
--- a/tracecmd/trace-agent.c
+++ b/tracecmd/trace-agent.c
@@ -236,7 +236,7 @@  static pid_t do_fork()
 	return fork();
 }
 
-static void agent_serve(unsigned int port)
+static void agent_serve(unsigned int port, bool do_daemon)
 {
 	int sd, cd, nr_cpus;
 	unsigned int cid;
@@ -255,6 +255,9 @@  static void agent_serve(unsigned int port)
 	if (!get_local_cid(&cid))
 		printf("listening on @%u:%u\n", cid, port);
 
+	if (do_daemon && daemon(1, 0))
+		die("daemon");
+
 	for (;;) {
 		cd = accept(sd, NULL, NULL);
 		if (cd < 0) {
@@ -335,8 +338,5 @@  void trace_agent(int argc, char **argv)
 	if (optind < argc-1)
 		usage(argv);
 
-	if (do_daemon && daemon(1, 0))
-		die("daemon");
-
-	agent_serve(port);
+	agent_serve(port, do_daemon);
 }