Detach properly from calling process - fiche - A pastebin adjusted for gopher use
(HTM) git clone git://vernunftzentrum.de/fiche.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit 1d247bf3df29866222634305bc66632101c05a36
(DIR) parent 80ee24683f7881c2e6dca88b4561f0c5ddd60b41
(HTM) Author: Christian Kellermann <ckeen@pestilenz.org>
Date: Fri, 2 Mar 2018 20:55:05 +0100
Detach properly from calling process
This will fork and create a new session ID. Together with the log file
patches this should make it a step closer to a proper daemon.
Diffstat:
fiche.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/fiche.c b/fiche.c
@@ -281,6 +281,31 @@ int fiche_run(Fiche_Settings settings) {
return -1;
}
+ pid_t pid = fork();
+ if (pid == -1){
+ char *err = strerror(0);
+ print_error("Unable to fork into background: %s", err);
+ if (logfile_handle) fclose(logfile_handle);
+ return -1;
+ }
+ if (pid > 0){
+ //parent
+ if (logfile_handle) fclose(logfile_handle);
+ return 0;
+ }
+
+ if (setsid() == -1){
+ char *err = strerror(0);
+ print_error("Creating new session id: %s", err);
+ if (logfile_handle) fclose(logfile_handle);
+ return -1;
+ }
+
+ // We are detached so close those to avoid noise
+ fclose(stdin);
+ fclose(stdout);
+ fclose(stderr);
+
// Main loop in this method
start_server(&settings);