pid_t SID;
int r;
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
log_notice("Double forking elogind");
log_notice("Parent PID : %5d", getpid_cached());
log_notice("Parent SID : %5d", getsid(getpid_cached()));
#endif // ENABLE_DEBUG_ELOGIND
- child = fork();
+ r = safe_fork_full("elogind-forker", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_NULL_STDIO|FORK_WAIT, &child);
- if (child < 0)
- return log_error_errno(errno, "Failed to fork: %m");
+ if (r < 0)
+ return log_error_errno(errno, "Failed to fork daemon leader: %m");
- if (child) {
- /* Wait for the child to terminate, so the decoupling
- * is guaranteed to succeed.
- */
- r = wait_for_terminate_and_warn("elogind control child", child, true);
- if (r < 0)
- return r;
+ /* safe_fork_full() Has waited for the child to terminate, so we
+ * are safe to return here. The child already has forked off the
+ * daemon itself.
+ */
+ if (r)
return child;
- }
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
log_notice("Child PID : %5d", getpid_cached());
log_notice("Child SID : %5d", getsid(getpid_cached()));
#endif // ENABLE_DEBUG_ELOGIND
- /* The first child has to become a new session leader. */
- close_all_fds(NULL, 0);
-
- /* close_all_fds() does not close 0,1,2 */
- close(0);
- close(1);
- close(2);
-
SID = setsid();
if ((pid_t)-1 == SID)
return log_error_errno(errno, "Failed to create new SID: %m");
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
log_notice("Child new SID : %5d", getsid(getpid_cached()));
#endif // ENABLE_DEBUG_ELOGIND
umask(0022);
/* Now the grandchild, the true daemon, can be created. */
- grandchild = fork();
+ r = safe_fork_full("elogind-daemon", NULL, 0, FORK_REOPEN_LOG, &grandchild);
- if (grandchild < 0)
- return log_error_errno(errno, "Failed to double fork: %m");
+ if (r < 0)
+ return log_error_errno(errno, "Failed to fork daemon: %m");
- if (grandchild)
+ if (r)
/* Exit immediately! */
return grandchild;
- close_all_fds(NULL, 0);
umask(0022);
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
log_notice("Grand child PID: %5d", getpid_cached());
log_notice("Grand child SID: %5d", getsid(getpid_cached()));
#endif // ENABLE_DEBUG_ELOGIND
/* Note: At this point, the logging is not initialized, so we can not
use log_debug_elogind(). */
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
log_notice("elogind startup: Daemonize: %s, Show Help: %s, Wrong arg: %s",
daemonize ? "True" : "False",
show_help ? "True" : "False",
m->test_run_flags = 0;
/* Init sleep modes and states */
- m->suspend_mode = NULL;
- m->suspend_state = NULL;
- m->hibernate_mode = NULL;
- m->hibernate_state = NULL;
- m->hybrid_sleep_mode = NULL;
- m->hybrid_sleep_state = NULL;
+ m->suspend_mode = NULL;
+ m->suspend_state = NULL;
+ m->hibernate_mode = NULL;
+ m->hibernate_state = NULL;
+ m->hybrid_sleep_mode = NULL;
+ m->hybrid_sleep_state = NULL;
+ m->hibernate_delay_sec = 0;
/* If elogind should be its own controller, mount its cgroup */
if (streq(SYSTEMD_CGROUP_CONTROLLER, "_elogind")) {
/// Add-On for manager_reset_config()
void elogind_manager_reset_config(Manager* m) {
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
int dbg_cnt;
#endif // ENABLE_DEBUG_ELOGIND
m->hybrid_sleep_mode = strv_new("suspend", "platform", "shutdown", NULL);
if (!m->hybrid_sleep_state)
m->hybrid_sleep_state = strv_new("disk", NULL);
+ if (!m->hibernate_delay_sec)
+ m->hibernate_delay_sec = 180 * USEC_PER_MINUTE;
-#ifdef ENABLE_DEBUG_ELOGIND
+#if ENABLE_DEBUG_ELOGIND
dbg_cnt = -1;
while (m->suspend_mode && m->suspend_mode[++dbg_cnt])
log_debug_elogind("suspend_mode[%d] = %s",
while (m->hybrid_sleep_state[++dbg_cnt])
log_debug_elogind("hybrid_sleep_state[%d] = %s",
dbg_cnt, m->hybrid_sleep_state[dbg_cnt]);
+ log_debug_elogind("hibernate_delay_sec: %ul seconds (%ul minutes)",
+ m->hibernate_delay_sec / USEC_PER_SEC,
+ m->hibernate_delay_sec / USEC_PER_MINUTE);
#endif // ENABLE_DEBUG_ELOGIND
}