diff options
author | Masami Hiramatsu <mhiramat@redhat.com> | 2009-03-20 11:59:06 -0400 |
---|---|---|
committer | Masami Hiramatsu <mhiramat@redhat.com> | 2009-03-20 11:59:06 -0400 |
commit | 54892f28a2747079fae4aa35b80598cbb993a4c3 (patch) | |
tree | 00cb02c62e52b366e478adeff1c97f73c228e2b5 /runtime/staprun/mainloop.c | |
parent | f0b8d2671fa56e0e1dcb4cc09fd6f7edf70f8fa3 (diff) | |
download | systemtap-steved-54892f28a2747079fae4aa35b80598cbb993a4c3.tar.gz systemtap-steved-54892f28a2747079fae4aa35b80598cbb993a4c3.tar.xz systemtap-steved-54892f28a2747079fae4aa35b80598cbb993a4c3.zip |
PR6930: stapio: run in background as a daemon
Add '-D'(daemon mode) option to staprun/stapio for daemon mode.
In this mode, stapio shows just its pid and detachs from console.
Since it has no stdio, this mode requires -o option. stapio will
exit when it receives SIGTERM or detects some error.
Diffstat (limited to 'runtime/staprun/mainloop.c')
-rw-r--r-- | runtime/staprun/mainloop.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/runtime/staprun/mainloop.c b/runtime/staprun/mainloop.c index e91e6302..b0d88073 100644 --- a/runtime/staprun/mainloop.c +++ b/runtime/staprun/mainloop.c @@ -318,6 +318,41 @@ int init_stapio(void) if (target_cmd) start_cmd(); + /* Run in background */ + if (daemon_mode) { + pid_t pid; + int ret; + dbug(2, "daemonizing stapio\n"); + + /* daemonize */ + ret = daemon(0, 1); /* don't close stdout at this time. */ + if (ret) { + err("Failed to daemonize stapio\n"); + return -1; + } + + /* change error messages to syslog. */ + switch_syslog("stapio"); + + /* show new pid */ + pid = getpid(); + fprintf(stdout, "%d\n", pid); + fflush(stdout); + + /* redirect all outputs to /dev/null */ + ret = open("/dev/null", O_RDWR); + if (ret < 0) { + err("Failed to open /dev/null\n"); + return -1; + } + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + dup2(ret, STDOUT_FILENO); + dup2(ret, STDERR_FILENO); + close(ret); + } + return 0; } |