]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
Daemonize after starting boot server
authorRay Strode <rstrode@redhat.com>
Wed, 4 Jun 2008 02:58:01 +0000 (22:58 -0400)
committerRay Strode <rstrode@redhat.com>
Wed, 4 Jun 2008 02:58:01 +0000 (22:58 -0400)
Previously we would daemonize when spawning the
terminal session.  We don't do that now, since
we attach to an existing already running terminal
session.

src/libply/ply-utils.c
src/libply/ply-utils.h
src/main.c

index 4e02fbadf1d038889cc957486464846e1f60541b..1f0ce2206736e757e3b34a19565ca8e06d358b67 100644 (file)
@@ -984,4 +984,64 @@ ply_unmount_filesystem (const char *directory)
   return true;
 }
 
+ply_daemon_handle_t *
+ply_create_daemon (void)
+{
+  pid_t pid;
+  int sender_fd, receiver_fd;
+  int *handle;
+
+  if (!ply_open_unidirectional_pipe (&sender_fd, &receiver_fd))
+    return NULL;
+
+  handle = calloc (1, sizeof (int));
+
+  pid = fork ();
+
+  if (pid < 0)
+    return NULL;
+
+  if (pid != 0)
+    {
+      uint8_t byte;
+      close (sender_fd);
+
+      if (!ply_read (receiver_fd, &byte, sizeof (uint8_t)))
+        {
+          ply_error ("could not read byte from child: %m");
+          _exit (1);
+        }
+
+      _exit ((int) byte);
+    }
+  close (receiver_fd);
+
+  *handle = sender_fd;
+
+  return (ply_daemon_handle_t *) handle;
+}
+
+bool
+ply_detach_daemon (ply_daemon_handle_t *handle,
+                   int                  exit_code)
+{
+  int sender_fd;
+  uint8_t byte;
+
+  assert (handle != NULL);
+  assert (exit_code >= 0 && exit_code < 256);
+
+  sender_fd = *(int *) handle;
+
+  byte = (uint8_t) exit_code;
+
+  if (!ply_write (sender_fd, &byte, sizeof (uint8_t)))
+    return false;
+
+  close (sender_fd);
+  free (handle);
+
+  return true;
+}
+
 /* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
index 3e2265aac6cf9f1bc1b7ac0827b207f39c23af5f..e9803c96834f571d6825158bf5718956d0bd19f9 100644 (file)
@@ -41,6 +41,8 @@
 typedef intptr_t ply_module_handle_t;
 typedef void (* ply_module_function_t) (void);
 
+typedef intptr_t ply_daemon_handle_t;
+
 #ifndef PLY_HIDE_FUNCTION_DECLARATIONS
 bool ply_open_unidirectional_pipe (int *sender_fd,
                                    int *receiver_fd);
@@ -86,6 +88,11 @@ int ply_detach_directory (const char *directory);
 bool ply_copy_file (const char *source, const char *destination);
 bool ply_copy_directory (const char *source, const char *destination);
 bool ply_unmount_filesystem (const char *directory);
+
+ply_daemon_handle_t *ply_create_daemon (void);
+bool ply_detach_daemon (ply_daemon_handle_t *handle,
+                        int                  exit_code);
+
 #endif
 
 #endif /* PLY_UTILS_H */
index c2c7b8d1a3339e3adf80cc74f567de3734da3114..52d80347ff3c5a6d1210d17dfb6307a5a867f67d 100644 (file)
@@ -568,6 +568,7 @@ main (int    argc,
   };
   int exit_code;
   bool attach_to_session = false;
+  ply_daemon_handle_t *daemon_handle;
 
   if (argc >= 2 && !strcmp(argv[1], "--attach-to-session"))
       attach_to_session = true;
@@ -588,6 +589,14 @@ main (int    argc,
       return EX_USAGE;
     }
 
+  daemon_handle = ply_create_daemon ();
+
+  if (daemon_handle == NULL)
+    {
+      ply_error ("cannot daemonize: %m");
+      return EX_UNAVAILABLE;
+    }
+
   state.loop = ply_event_loop_new ();
 
   /* before do anything we need to make sure we have a working
@@ -599,6 +608,7 @@ main (int    argc,
     {
       ply_error ("could not setup basic operating environment: %m");
       ply_list_directory (PLY_WORKING_DIRECTORY);
+      ply_detach_daemon (daemon_handle, EX_OSERR);
       return EX_OSERR;
     }
 
@@ -611,6 +621,7 @@ main (int    argc,
       if (state.session == NULL)
         {
           ply_error ("could not create session: %m");
+          ply_detach_daemon (daemon_handle, EX_UNAVAILABLE);
           return EX_UNAVAILABLE;
         }
     }
@@ -620,6 +631,13 @@ main (int    argc,
   if (state.boot_server == NULL)
     {
       ply_error ("could not log bootup: %m");
+      ply_detach_daemon (daemon_handle, EX_UNAVAILABLE);
+      return EX_UNAVAILABLE;
+    }
+
+  if (!ply_detach_daemon (daemon_handle, 0))
+    {
+      ply_error ("could not tell parent to exit: %m");
       return EX_UNAVAILABLE;
     }