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 <jo...@kernel.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

Reply via email to