diff options
author | Jean Pihet <jean.pihet@linaro.org> | 2014-10-10 11:34:48 +0200 |
---|---|---|
committer | Borislav Petkov <bp@suse.de> | 2014-11-06 12:02:05 +0100 |
commit | d86963454fe1c6d94c3a3a219d12621b076558af (patch) | |
tree | 88d4f323c8488c0ffe84ad222e77761db2ddc60c | |
parent | 6e9f578ade268681402fa3370e83e9f92966a2b4 (diff) | |
download | rasd-d86963454fe1c6d94c3a3a219d12621b076558af.tar.gz |
rasd: Daemonize the process
The process is put in the background and is detached from the
TTYs. The process working dir is set to '/'.
stdin, stdout and stderr are closed and cannot be used anymore.
Note: the config file 'rasd.cfg' is expected in the working
dir.
Boris:
- simplify error paths
- cleanup excessive comments
Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
Link: http://lkml.kernel.org/r/1412933690-25576-12-git-send-email-jean.pihet@linaro.org
Signed-off-by: Borislav Petkov <bp@suse.de>
-rw-r--r-- | src/rasd.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -161,6 +161,50 @@ next_event: return nr_samples; } +/* Run as a daemon: fork off the parent, detach from TTY etc. */ +static void daemonize(void) +{ + pid_t pid; + + pid = fork(); + if (pid < 0) + exit(EXIT_FAILURE); + else if (pid > 0) + /* Let the parent terminate */ + exit(EXIT_SUCCESS); + + /* The child process becomes session leader */ + if (setsid() < 0) { + perror("setsid()"); + exit(EXIT_FAILURE); + } + + /* + * Catch, ignore and handle signals, ignore for now + */ + signal(SIGCHLD, SIG_IGN); + signal(SIGHUP, SIG_IGN); + + /* Fork off for the second time to get rid of the TTY sessions */ + pid = fork(); + if (pid < 0) + exit(EXIT_FAILURE); + else if (pid > 0) + exit(EXIT_SUCCESS); + + /* Set new file permissions */ + umask(0); + + /* Change the working directory to the root directory */ + if (!chdir("/")) + exit(EXIT_FAILURE); + + /* Close the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); +} + int main() { struct perf_evsel *c; @@ -168,6 +212,8 @@ int main() struct cpu_map *cpus; int i; + daemonize(); + page_size = sysconf(_SC_PAGE_SIZE); if (!debugfs_mount(NULL)) |