From f8ea392738ec16ce4633bd91f8fc6e2d301c329c Mon Sep 17 00:00:00 2001 From: MatMoul Date: Sun, 14 May 2017 18:55:16 +0200 Subject: [PATCH] fork two time, daemon unit --- sample_scenarios/scenario0 | 6 +++--- src/helpers/daemonizer.cpp | 25 ++++++++++++++++++++++--- systemd/g810-led-scenarios.service | 9 +++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/sample_scenarios/scenario0 b/sample_scenarios/scenario0 index 3a42512..2403b35 100644 --- a/sample_scenarios/scenario0 +++ b/sample_scenarios/scenario0 @@ -1,6 +1,6 @@ -p sample_profiles/all_blue +p /etc/g810-led/sample_profiles/all_blue w 1000 -p sample_profiles/all_green +p /etc/g810-led/sample_profiles/all_green w 1000 -p sample_profiles/all_red +p /etc/g810-led/sample_profiles/all_red w 1000 diff --git a/src/helpers/daemonizer.cpp b/src/helpers/daemonizer.cpp index 5038f31..5640efa 100644 --- a/src/helpers/daemonizer.cpp +++ b/src/helpers/daemonizer.cpp @@ -17,16 +17,35 @@ namespace daemonizer { void start(char *arg0, std::string scenarioFile) { pid_t pid, sid; + + pid = fork(); + if (pid < 0) exit(EXIT_FAILURE); + if (pid > 0) exit(EXIT_SUCCESS); + sid = setsid(); + if (sid < 0) exit(EXIT_FAILURE); + signal(SIGCHLD,SIG_IGN); /* ignore child */ + signal(SIGHUP,SIG_IGN); /* ignore child */ + + pid = fork(); if (pid < 0) exit(EXIT_FAILURE); if (pid > 0) exit(EXIT_SUCCESS); umask(0); - sid = setsid(); - if (sid < 0) exit(EXIT_FAILURE); + if (chdir("/") < 0) exit(EXIT_FAILURE); - //signal(SIGINT, stop); std::cout<