chiark / gitweb /
Prep v238: Fix elogind_daemonize(), it failed due to some misunderstandings on my...
authorSven Eden <yamakuzure@gmx.net>
Fri, 8 Jun 2018 14:45:25 +0000 (16:45 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 8 Jun 2018 14:45:56 +0000 (16:45 +0200)
(cherry picked from commit 1c9629692145891f10a36227749470d87979dd0b)

src/login/elogind.c
src/shared/musl_missing.c

index 162de3fd91d9c36c9d7837f1e6c0bfd1b2006e23..2fae0f193a9ae239502d0840108cdf7ef67832b4 100644 (file)
@@ -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
index e089367f2db846307f0c912a7f303493f3c21982..ffa0ff78f0ca845655ba285361f10cbe3442aea4 100644 (file)
@@ -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__