From 163555c7ab56132ee27e3e7d9a26eb985682c1b5 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Fri, 17 Aug 2007 16:43:57 +0000 Subject: 2007-08-17 Havoc Pennington * tools/dbus-launch-x11.c (set_address_in_x11): fix from Michael Lorenz to use long not int with XChangeProperty format 32 * dbus/dbus-sysdeps-util-unix.c (_dbus_write_pid_to_file_and_pipe): factor this out, and use the same code in _dbus_become_daemon (where the parent writes the pid file and to the pid pipe) and in bus_context_new (where the daemon writes its own pid file and to its own pid pipe) * bus/bus.c (bus_context_new): close the pid pipe after we print to it. Also, don't write the pid to the pipe twice when we fork, someone reported this bug a long time ago. --- bus/bus.c | 116 +++++++++++++++++++++++--------------------------------------- 1 file changed, 43 insertions(+), 73 deletions(-) (limited to 'bus/bus.c') diff --git a/bus/bus.c b/bus/bus.c index 21e8037e..c2ea1157 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -665,7 +665,7 @@ bus_context_new (const DBusString *config_file, if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe)) _dbus_pipe_close (print_addr_pipe, NULL); - + _dbus_string_free (&addr); } @@ -695,78 +695,48 @@ bus_context_new (const DBusString *config_file, } } - /* Now become a daemon if appropriate */ - if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS) - { - DBusString u; - - if (context->pidfile) - _dbus_string_init_const (&u, context->pidfile); - - if (!_dbus_become_daemon (context->pidfile ? &u : NULL, - print_pid_pipe, - error)) - { - _DBUS_ASSERT_ERROR_IS_SET (error); - goto failed; - } - } - else - { - /* Need to write PID file for ourselves, not for the child process */ - if (context->pidfile != NULL) - { - DBusString u; - - _dbus_string_init_const (&u, context->pidfile); - - if (!_dbus_write_pid_file (&u, _dbus_getpid (), error)) - { - _DBUS_ASSERT_ERROR_IS_SET (error); - goto failed; - } - } - } - - /* Write PID if requested */ - if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe)) - { - DBusString pid; - int bytes; - - if (!_dbus_string_init (&pid)) - { - BUS_SET_OOM (error); - goto failed; - } - - if (!_dbus_string_append_int (&pid, _dbus_getpid ()) || - !_dbus_string_append (&pid, "\n")) - { - _dbus_string_free (&pid); - BUS_SET_OOM (error); - goto failed; - } - - bytes = _dbus_string_get_length (&pid); - if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes) - { - /* pipe_write sets error on failure but not short write */ - if (error != NULL && !dbus_error_is_set (error)) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Printing message bus PID: did not write enough bytes\n"); - } - _dbus_string_free (&pid); - goto failed; - } - - if (!_dbus_pipe_is_stdout_or_stderr (print_pid_pipe)) - _dbus_pipe_close (print_pid_pipe, NULL); - - _dbus_string_free (&pid); - } - + /* Now become a daemon if appropriate and write out pid file in any case */ + { + DBusString u; + + if (context->pidfile) + _dbus_string_init_const (&u, context->pidfile); + + if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS) + { + _dbus_verbose ("Forking and becoming daemon\n"); + + if (!_dbus_become_daemon (context->pidfile ? &u : NULL, + print_pid_pipe, + error)) + { + _DBUS_ASSERT_ERROR_IS_SET (error); + goto failed; + } + } + else + { + _dbus_verbose ("Fork not requested\n"); + + /* Need to write PID file and to PID pipe for ourselves, + * not for the child process. This is a no-op if the pidfile + * is NULL and print_pid_pipe is NULL. + */ + if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL, + print_pid_pipe, + _dbus_getpid (), + error)) + { + _DBUS_ASSERT_ERROR_IS_SET (error); + goto failed; + } + } + } + + if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) && + !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe)) + _dbus_pipe_close (print_pid_pipe, NULL); + if (!process_config_postinit (context, parser, error)) { _DBUS_ASSERT_ERROR_IS_SET (error); -- cgit