log_notice("Parent SID : %5d", getsid(getpid_cached()));
#endif // ENABLE_DEBUG_ELOGIND
- r = safe_fork_full("daemon leader", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_WAIT, &child);
+ r = safe_fork_full("elogind-forker", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_NULL_STDIO|FORK_WAIT, &child);
if (r < 0)
return log_error_errno(errno, "Failed to fork daemon leader: %m");
* are safe to return here. The child already has forked off the
* daemon itself.
*/
- if (child)
+ if (r)
return child;
#ifdef ENABLE_DEBUG_ELOGIND
log_notice("Child SID : %5d", getsid(getpid_cached()));
#endif // ENABLE_DEBUG_ELOGIND
- /* safe_fork_full() does not close stdin/stdout/stderr */
- close(0);
- close(1);
- close(2);
-
SID = setsid();
if ((pid_t)-1 == SID)
return log_error_errno(errno, "Failed to create new SID: %m");
umask(0022);
/* Now the grandchild, the true daemon, can be created. */
- r = safe_fork_full("daemon leader", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, &grandchild);
+ r = safe_fork_full("elogind-daemon", NULL, 0, FORK_REOPEN_LOG, &grandchild);
if (r < 0)
return log_error_errno(errno, "Failed to fork daemon: %m");
- if (grandchild)
+ if (r)
/* Exit immediately! */
return grandchild;
- /* safe_fork_full() does not close stdin/stdout/stderr */
- close(0);
- close(1);
- close(2);
-
umask(0022);
#ifdef ENABLE_DEBUG_ELOGIND
char *program_invocation_short_name = NULL;
#endif // __GLIBC__
+const char *program_arg_name = NULL;
+
#include "musl_missing.h"
static void elogind_free_program_name(void) {
- if (program_invocation_name)
+
+ if (program_invocation_name && (program_invocation_name != program_arg_name) && strlen(program_invocation_name))
program_invocation_name = mfree(program_invocation_name);
- if (program_invocation_short_name)
+ if (program_invocation_short_name && (program_invocation_short_name != program_arg_name) && strlen(program_invocation_short_name))
program_invocation_short_name = mfree(program_invocation_short_name);
}
void elogind_set_program_name(const char* pcall) {
assert(pcall && pcall[0]);
+ program_arg_name = pcall;
+
if ( ( program_invocation_name
- && strcmp(program_invocation_name, pcall))
+ && strcmp(program_invocation_name, program_arg_name))
|| ( program_invocation_short_name
- && strcmp(program_invocation_short_name, basename(pcall)) ) )
+ && strcmp(program_invocation_short_name, basename(program_arg_name)) ) )
elogind_free_program_name();
if (NULL == program_invocation_name)
- program_invocation_name = strdup(pcall);
+ program_invocation_name = strdup(program_arg_name);
if (NULL == program_invocation_short_name)
- program_invocation_short_name = strdup(basename(pcall));
-
+ program_invocation_short_name = strdup(basename(program_arg_name));
#ifndef __GLIBC__
atexit(elogind_free_program_name);
#endif // __GLIBC__