summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Eden <yamakuzure@gmx.net>2018-06-08 16:45:25 +0200
committerSven Eden <yamakuzure@gmx.net>2018-06-08 16:45:56 +0200
commit0f50f4ce724cc1131e9e0c7dffe02b07d2279101 (patch)
tree0394c45de0a6e25de8423d999afec31dda76c932
parente97bcf5d16f97963ddfbf0a0a14e8b05f1bc0726 (diff)
Prep v238: Fix elogind_daemonize(), it failed due to some misunderstandings on my side.
(cherry picked from commit 1c9629692145891f10a36227749470d87979dd0b)
-rw-r--r--src/login/elogind.c18
-rw-r--r--src/shared/musl_missing.c18
2 files changed, 15 insertions, 21 deletions
diff --git a/src/login/elogind.c b/src/login/elogind.c
index 162de3f..2fae0f1 100644
--- a/src/login/elogind.c
+++ b/src/login/elogind.c
@@ -103,7 +103,7 @@ static int elogind_daemonize(void) {
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");
@@ -112,7 +112,7 @@ static int elogind_daemonize(void) {
* are safe to return here. The child already has forked off the
* daemon itself.
*/
- if (child)
+ if (r)
return child;
#ifdef ENABLE_DEBUG_ELOGIND
@@ -120,11 +120,6 @@ static int elogind_daemonize(void) {
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");
@@ -136,20 +131,15 @@ static int elogind_daemonize(void) {
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
diff --git a/src/shared/musl_missing.c b/src/shared/musl_missing.c
index e089367..ffa0ff7 100644
--- a/src/shared/musl_missing.c
+++ b/src/shared/musl_missing.c
@@ -28,29 +28,33 @@ char *program_invocation_name = NULL;
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__