On Mon, 1 May 2023 23:31:16 +0300 avidanborisov@xxxxxxxxx wrote: > +static void daemonize_start(void) > +{ > + int devnull; > + int status; > + int pid; > + int rc; > + > + pid = fork(); > + if (pid == -1) > + die("daemonize: fork failed"); > + > + if (pid == 0) { /* child */ > + /* > + * We keep stdout and stderr open to allow the user to > + * see output and errors after the daemonization (he can > + * choose to supress it with >/dev/null if he wants). Please don't use "he", we do have women users. The above should be: * We keep stdout and stderr open to allow the user to * see output and errors after the daemonization (the user can * choose to supress it with >/dev/null if the user wants). > + * > + * No reason to keep stdin open (it might interfere with the > + * shell), we redirect it to /dev/null. > + */ > + devnull = open("/dev/null", O_RDONLY); > + if (devnull == -1) > + die("daemonize: open /dev/null failed"); > + > + if (devnull > 0) { > + if (dup2(devnull, 0) == -1) > + die("daemonize: dup2"); > + close(0); > + } > + > + return; [..] > @@ -6656,6 +6766,10 @@ static void parse_record_options(int argc, > die("--fork option used for 'start' command only"); > fork_process = true; > break; > + case OPT_daemonize: > + if (!IS_RECORD(ctx)) > + die("--daemonize option used for 'record' command only"); > + do_daemonize = true; Missing "break". I found this because I tested it on a VM that doesn't have tsc_nsec support. Thanks! -- Steve > case OPT_tsc2nsec: > ret = get_tsc_nsec(&ctx->tsc2nsec.shift, > &ctx->tsc2nsec.mult); > @@ -6899,6 +7013,9 @@ static void record_trace(int argc, char **argv, > struct buffer_instance *instance; > struct filter_pids *pid; > > + if (do_daemonize) > + daemonize_start(); > + > /* > * If top_instance doesn't have any plugins or events, then > * remove it from being processed. > @@ -7017,8 +7134,15 @@ static void record_trace(int argc, char **argv, > } > } > } > - /* sleep till we are woken with Ctrl^C */ > - printf("Hit Ctrl^C to stop recording\n"); > + > + if (do_daemonize) { > + daemonize_finish(); > + printf("Send SIGINT 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"); > + } > + > for_all_instances(instance) { > /* If an instance is not tracing individual processes > * or there is an error while waiting for a process to > diff --git a/tracecmd/trace-usage.c b/tracecmd/trace-usage.c > index 42a8e7d..0630e8e 100644 > --- a/tracecmd/trace-usage.c > +++ b/tracecmd/trace-usage.c > @@ -81,6 +81,7 @@ static struct usage_help usage_help[] = { > " available algorithms can be listed with trace-cmd list -c\n" > " --proxy vsocket to reach the agent. Acts the same as -A (for an agent)\n" > " but will send the proxy connection to the agent.\n" > + " --daemonize run trace-cmd in the background as a daemon after recording has started\n" > }, > { > "set",