2004-10-29 Colin Walters <walters@redhat.com>
authorColin Walters <walters@verbum.org>
Fri, 29 Oct 2004 19:59:15 +0000 (19:59 +0000)
committerColin Walters <walters@verbum.org>
Fri, 29 Oct 2004 19:59:15 +0000 (19:59 +0000)
* dbus/dbus-sysdeps.h (_dbus_become_daemon): Also take
parameter for fd to write pid to.

* dbus/dbus-sysdeps.c (_dbus_become_daemon): Implement it.

* bus/bus.c (bus_context_new): Pass print_pid_fd
to _dbus_become_daemon (bug #1720)

ChangeLog
bus/bus.c
dbus/dbus-sysdeps.c
dbus/dbus-sysdeps.h

index 2c54efa..eeb1d54 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2004-10-29  Colin Walters  <walters@redhat.com>
 
+       * dbus/dbus-sysdeps.h (_dbus_become_daemon): Also take
+       parameter for fd to write pid to.       
+
+       * dbus/dbus-sysdeps.c (_dbus_become_daemon): Implement it.
+       
+       * bus/bus.c (bus_context_new): Pass print_pid_fd
+       to _dbus_become_daemon (bug #1720)
+
+2004-10-29  Colin Walters  <walters@redhat.com>
+
        Patch from Ed Catmur <ed@catmur.co.uk>
 
        * mono/doc/Makefile.am (install-data-local): Handle
index 043f2e1..b34e635 100644 (file)
--- a/bus/bus.c
+++ b/bus/bus.c
@@ -663,7 +663,9 @@ bus_context_new (const DBusString *config_file,
       if (context->pidfile)
         _dbus_string_init_const (&u, context->pidfile);
       
-      if (!_dbus_become_daemon (context->pidfile ? &u : NULL, error))
+      if (!_dbus_become_daemon (context->pidfile ? &u : NULL, 
+                               print_pid_fd,
+                               error))
        {
          _DBUS_ASSERT_ERROR_IS_SET (error);
          goto failed;
index e1b786f..2bdd817 100644 (file)
@@ -3131,11 +3131,13 @@ _dbus_print_backtrace (void)
  * Does the chdir, fork, setsid, etc. to become a daemon process.
  *
  * @param pidfile #NULL, or pidfile to create
+ * @param print_pid_fd file descriptor to print pid to, or -1 for none
  * @param error return location for errors
  * @returns #FALSE on failure
  */
 dbus_bool_t
 _dbus_become_daemon (const DBusString *pidfile,
+                    int               print_pid_fd,
                      DBusError        *error)
 {
   const char *s;
@@ -3201,6 +3203,42 @@ _dbus_become_daemon (const DBusString *pidfile,
               return FALSE;
             }
         }
+
+      /* Write PID if requested */
+      if (print_pid_fd >= 0)
+       {
+         DBusString pid;
+         int bytes;
+         
+         if (!_dbus_string_init (&pid))
+           {
+             _DBUS_SET_OOM (error);
+              kill (child_pid, SIGTERM);
+             return FALSE;
+           }
+         
+         if (!_dbus_string_append_int (&pid, _dbus_getpid ()) ||
+             !_dbus_string_append (&pid, "\n"))
+           {
+             _dbus_string_free (&pid);
+             _DBUS_SET_OOM (error);
+              kill (child_pid, SIGTERM);
+             return FALSE;
+           }
+         
+         bytes = _dbus_string_get_length (&pid);
+         if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes)
+           {
+             dbus_set_error (error, DBUS_ERROR_FAILED,
+                             "Printing message bus PID: %s\n",
+                             _dbus_strerror (errno));
+             _dbus_string_free (&pid);
+              kill (child_pid, SIGTERM);
+             return FALSE;
+           }
+         
+         _dbus_string_free (&pid);
+       }
       _dbus_verbose ("parent exiting\n");
       _exit (0);
       break;
index 7e89d61..5227a69 100644 (file)
@@ -298,6 +298,7 @@ dbus_bool_t _dbus_close            (int               fd,
 void        _dbus_print_backtrace  (void);
 
 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
+                                  int               print_pid_fd,
                                    DBusError        *error);
 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
                                    unsigned long     pid,