[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210130234856.271282-9-jolsa@kernel.org>
Date: Sun, 31 Jan 2021 00:48:40 +0100
From: Jiri Olsa <jolsa@...nel.org>
To: Arnaldo Carvalho de Melo <acme@...nel.org>
Cc: lkml <linux-kernel@...r.kernel.org>,
Peter Zijlstra <a.p.zijlstra@...llo.nl>,
Ingo Molnar <mingo@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Namhyung Kim <namhyung@...nel.org>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Michael Petlan <mpetlan@...hat.com>,
Ian Rogers <irogers@...gle.com>,
Stephane Eranian <eranian@...gle.com>,
Alexei Budankov <abudankov@...wei.com>
Subject: [PATCH 08/24] perf daemon: Add background support
Adding support to put daemon process in the background.
It's now enabled by default and -f option is added to
keep daemon process on the console for debugging.
Signed-off-by: Jiri Olsa <jolsa@...nel.org>
---
tools/perf/builtin-daemon.c | 66 +++++++++++++++++++++++++++++++++++--
1 file changed, 63 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c
index d0a0a998e073..324666058842 100644
--- a/tools/perf/builtin-daemon.c
+++ b/tools/perf/builtin-daemon.c
@@ -488,6 +488,13 @@ static void daemon__kill(struct daemon *daemon)
daemon__signal(daemon, SIGTERM);
}
+static void __daemon__free(struct daemon *daemon)
+{
+ free(daemon->config_real);
+ free(daemon->config_base);
+ free(daemon->base);
+}
+
static void daemon__free(struct daemon *daemon)
{
struct session *session, *h;
@@ -495,9 +502,7 @@ static void daemon__free(struct daemon *daemon)
list_for_each_entry_safe(session, h, &daemon->sessions, list)
session__remove(session);
- free(daemon->config_real);
- free(daemon->config_base);
- free(daemon->base);
+ __daemon__free(daemon);
}
static void daemon__exit(struct daemon *daemon)
@@ -643,10 +648,54 @@ static int setup_config(struct daemon *daemon)
return daemon->config_real ? 0 : -1;
}
+static int go_background(struct daemon *daemon)
+{
+ int pid, fd;
+
+ pid = fork();
+ if (pid < 0)
+ return -1;
+
+ if (pid > 0)
+ return 1;
+
+ if (setsid() < 0)
+ return -1;
+
+ umask(0);
+
+ if (chdir(daemon->base)) {
+ perror("failed: chdir");
+ return -1;
+ }
+
+ fd = open("output", O_RDWR|O_CREAT|O_TRUNC, 0644);
+ if (fd < 0) {
+ perror("failed: open");
+ return -1;
+ }
+
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+
+ close(0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ close(fd);
+
+ daemon->out = fdopen(1, "w");
+ if (!daemon->out)
+ return -1;
+
+ setbuf(daemon->out, NULL);
+ return 0;
+}
+
static int __cmd_start(struct daemon *daemon, struct option parent_options[],
int argc, const char **argv)
{
+ bool foreground = false;
struct option start_options[] = {
+ OPT_BOOLEAN('f', "foreground", &foreground, "stay on console"),
OPT_PARENT(parent_options),
OPT_END()
};
@@ -667,6 +716,17 @@ static int __cmd_start(struct daemon *daemon, struct option parent_options[],
if (setup_server_config(daemon))
return -1;
+ if (!foreground) {
+ err = go_background(daemon);
+ if (err) {
+ /* original process, exit normally */
+ if (err == 1)
+ err = 0;
+ __daemon__free(daemon);
+ return err;
+ }
+ }
+
debug_set_file(daemon->out);
debug_set_display_time(true);
--
2.29.2
Powered by blists - more mailing lists