Skip to content

Instantly share code, notes, and snippets.

@lfranchi
Created October 3, 2009 18:14
Show Gist options
  • Save lfranchi/200778 to your computer and use it in GitHub Desktop.
Save lfranchi/200778 to your computer and use it in GitHub Desktop.
diff -Nurd README.launchd dbus-1.2.12-new/README.launchd
--- README.launchd 1969-12-31 19:00:00.000000000 -0500
+++ dbus-1.2.12-new/README.launchd 2009-02-23 08:33:30.000000000 -0500
@@ -0,0 +1,61 @@
+Launchd[1,2] replaces init, inetd and cron on Mac OS X since 10.4 "Tiger".
+dbus uses this service to provide a common session bus address for each user
+and so deprecates the X11 enabled dbus-launcher.
+
+[1] http://developer.apple.com/MacOsX/launchd.html
+[2] http://launchd.macosforge.org/
+
+
+Setup
+===
+
+Configure with --enable-launchd and --without-x (X11 should not harm but it's
+simply not necessary any more)
+After installation, to prevent a reboot, load the dbus session starter into
+launchd by executing:
+$ launchctl load /Library/LaunchAgents/org.freedesktop.dbus-session.plist
+
+You can change the launch agent dir via configure, but it's not recommended.
+Make sure to execute the above line as the actual user for which you want to
+use a session bus since launchd manages its agents on a per user basis.
+
+
+How it works
+===
+
+Launchd allocates a socket and provides the unix path to it via the variable
+DBUS_LAUNCHD_SESSION_BUS_SOCKET in launchd's environment. Every process
+spawned by launchd (or dbus-daemon, if stared by launchd) can access it through
+its own environment. Other processes can query launchd for it by executing:
+$ launchctl getenv DBUS_LAUNCHD_SESSION_BUS_SOCKET
+However, this is normally done by the dbus client lib for you.
+
+If launchd start dbus-daemon with a config file containing a "launchd:env=FOO"
+address, as the default session config does with env=DBUS_LAUNCHD_SESSION_BUS_SOCKET,
+the daemon will get the file descriptor from launchd and start listening on it.
+The environment variable is used to get the actual socket path which is passed
+to every service spawned by dbus-daemon as a result from autolaunch messages.
+Please note that it's not possible to start dbus-daemon manually when using a
+"launchd:" address. Only child processes of launchd can access the above
+mentioned file descriptor!
+
+To create custom buses just set up an other launch agent. As a quick start copy
+/Library/LaunchAgents/org.freedesktop.dbus-session.plist, change the label
+to i.e. "org.freedesktop.dbus-foo" and change the SecureSocketWithKey value,
+i.e. to "DBUS_LAUNCHD_FOO_BUS_SOCKET". This environment variable has to be set
+in the config file for your new bus in the <listen> element (see session.config).
+Then edit your /Library/LaunchAgents/org.freedesktop.dbus-foo.plist to start
+dbus-daemon with "--config-file=/opt/local/etc/dbus-1/foo.conf" instead of
+"--session". Now load the new plist onto launchd as described in the setup
+section of this document.
+Executing "launchctl export" should now give you two sockets, one in
+DBUS_LAUNCHD_SESSION_BUS_SOCKET and the new DBUS_LAUNCHD_FOO_BUS_SOCKET.
+To connect to this new bus use "launchd:env=DBUS_LAUNCHD_FOO_BUS_SOCKET".
+
+Since Mac OS X 10.5 "Leopard" you can also configure launchd to start
+dbus-daemon on demand as soon as some process connects to the socket. Since
+it's broken on 10.4 this feature is disabled per default. Look at
+/Library/LaunchAgents/org.freedesktop.dbus-session.plist to change it.
+
+On the client side, the envvar DBUS_SESSION_BUS_ADDRESS can be normally used
+but if it's not set, launchd is queried for the session bus socket.
diff -Nurd bus/Makefile.am dbus-1.2.12-new/bus/Makefile.am
--- bus/Makefile.am 2008-08-07 14:44:35.000000000 -0400
+++ dbus-1.2.12-new/bus/Makefile.am 2009-02-23 08:33:30.000000000 -0500
@@ -9,12 +9,18 @@
CONFIG_IN_FILES= \
session.conf.in \
- system.conf.in
+ system.conf.in \
+ org.freedesktop.dbus-session.plist.in
config_DATA= \
session.conf \
system.conf
+if DBUS_ENABLE_LAUNCHD
+agentdir=$(LAUNCHD_AGENT_DIR)
+agent_DATA=org.freedesktop.dbus-session.plist
+endif
+
if DBUS_USE_LIBXML
XML_SOURCES=config-loader-libxml.c
endif
diff -Nurd bus/org.freedesktop.dbus-session.plist.in dbus-1.2.12-new/bus/org.freedesktop.dbus-session.plist.in
--- bus/org.freedesktop.dbus-session.plist.in 1969-12-31 19:00:00.000000000 -0500
+++ dbus-1.2.12-new/bus/org.freedesktop.dbus-session.plist.in 2009-02-23 08:33:30.000000000 -0500
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>Label</key>
+ <string>org.freedesktop.dbus-session</string>
+
+ <key>ServiceIPC</key>
+ <true/>
+
+ <!-- bug in 10.4's launchd - on-demand loading does not work -->
+ <key>OnDemand</key>
+ <false />
+
+ <key>ProgramArguments</key>
+ <array>
+ <string>@DBUS_DAEMONDIR@/dbus-daemon</string>
+ <string>--nofork</string>
+ <string>--session</string>
+ </array>
+
+ <key>Sockets</key>
+ <dict>
+ <key>unix_domain_listener</key>
+ <dict>
+ <key>SecureSocketWithKey</key>
+ <string>DBUS_LAUNCHD_SESSION_BUS_SOCKET</string>
+ </dict>
+ </dict>
+</dict>
+</plist>
diff -Nurd bus/session.conf.in dbus-1.2.12-new/bus/session.conf.in
--- bus/session.conf.in 2009-01-06 17:52:22.000000000 -0500
+++ dbus-1.2.12-new/bus/session.conf.in 2009-02-23 08:33:30.000000000 -0500
@@ -12,7 +12,7 @@
the behavior of child processes. -->
<keep_umask/>
- <listen>unix:tmpdir=@DBUS_SESSION_SOCKET_DIR@</listen>
+ <listen>@DBUS_SESSION_BUS_DEFAULT_ADDRESS@</listen>
<standard_session_servicedirs />
diff -Nurd configure.in dbus-1.2.12-new/configure.in
--- configure.in 2009-01-06 19:30:26.000000000 -0500
+++ dbus-1.2.12-new/configure.in 2009-02-23 08:33:30.000000000 -0500
@@ -78,6 +78,7 @@
AC_ARG_ENABLE(kqueue, AS_HELP_STRING([--enable-kqueue],[build with kqueue support]),enable_kqueue=$enableval,enable_kqueue=auto)
AC_ARG_ENABLE(console-owner-file, AS_HELP_STRING([--enable-console-owner-file],[enable console owner file]),enable_console_owner_file=$enableval,enable_console_owner_file=auto)
AC_ARG_ENABLE(userdb-cache, AS_HELP_STRING([--enable-userdb-cache],[build with userdb-cache support]),enable_userdb_cache=$enableval,enable_userdb_cache=yes)
+AC_ARG_ENABLE(launchd, AS_HELP_STRING([--enable-launchd],[build with launchd auto-launch support]),enable_launchd=$enableval,enable_launchd=auto)
AC_ARG_WITH(xml, AS_HELP_STRING([--with-xml=[libxml/expat]],[XML library to use]))
AC_ARG_WITH(init-scripts, AS_HELP_STRING([--with-init-scripts=[redhat]],[Style of init scripts to install]))
@@ -87,6 +88,7 @@
AC_ARG_WITH(system-socket, AS_HELP_STRING([--with-system-socket=[filename]],[UNIX domain socket for systemwide daemon]))
AC_ARG_WITH(console-auth-dir, AS_HELP_STRING([--with-console-auth-dir=[dirname]],[directory to check for console ownerhip]))
AC_ARG_WITH(console-owner-file, AS_HELP_STRING([--with-console-owner-file=[filename]],[file whose owner determines current console owner]))
+AC_ARG_WITH(launchd-agent-dir, AS_HELP_STRING([--with-launchd-agent-dir=[dirname]],[directory to put the launchd agent (default: /Library/LaunchAgents)]))
AC_ARG_WITH(dbus_user, AS_HELP_STRING([--with-dbus-user=<user>],[User for running the DBUS daemon (messagebus)]))
AC_ARG_WITH(dbus_daemondir, AS_HELP_STRING([--with-dbus-daemondir=[dirname]],[Directory for installing the DBUS daemon]))
@@ -1026,6 +1028,46 @@
AM_CONDITIONAL(DBUS_BUS_ENABLE_KQUEUE, test x$have_kqueue = xyes)
+# launchd checks
+if test x$enable_launchd = xno ; then
+ have_launchd=no
+else
+ have_launchd=yes
+ AC_CHECK_HEADER([launch.h], , have_launchd=no)
+ AC_PATH_PROG([LAUNCHCTL], [launchctl])
+ if test "x$LAUNCHCTL" = "x"; then
+ have_launchd=no
+ fi
+
+ if test x$enable_launchd = xyes -a x$have_launchd = xno ; then
+ AC_MSG_ERROR([launchd support explicitly enabled but not available])
+ fi
+fi
+
+dnl check if launchd is enabled
+if test x$have_launchd = xyes; then
+ AC_DEFINE(DBUS_ENABLE_LAUNCHD,1,[Use launchd autolaunch])
+fi
+
+AM_CONDITIONAL(DBUS_ENABLE_LAUNCHD, test x$have_launchd = xyes)
+
+if test x$have_launchd = xyes; then
+ DBUS_SESSION_BUS_DEFAULT_ADDRESS="launchd:env=DBUS_LAUNCHD_SESSION_BUS_SOCKET"
+else
+ DBUS_SESSION_BUS_DEFAULT_ADDRESS="unix:tmpdir=$DBUS_SESSION_SOCKET_DIR"
+fi
+AC_SUBST(DBUS_SESSION_BUS_DEFAULT_ADDRESS)
+
+
+#### Directory to place launchd agent file
+if test "x$with_launchd_agent_dir" = "x"; then
+ LAUNCHD_AGENT_DIR="/Library/LaunchAgents"
+else
+ LAUNCHD_AGENT_DIR="$with_launchd_agent_dir"
+fi
+
+AC_SUBST(LAUNCHD_AGENT_DIR)
+
dnl console owner file
if test x$enable_console_owner_file = xno ; then
have_console_owner_file=no;
@@ -1387,6 +1429,7 @@
bus/system.conf
bus/session.conf
bus/messagebus
+bus/org.freedesktop.dbus-session.plist
bus/rc.messagebus
bus/dbus-daemon.1
Makefile
@@ -1454,6 +1497,7 @@
Building Doxygen docs: ${enable_doxygen_docs}
Building XML docs: ${enable_xml_docs}
Building cache support: ${enable_userdb_cache}
+ Building launchd support: ${have_launchd}
Gettext libs (empty OK): ${INTLLIBS}
Using XML parser: ${with_xml}
Init scripts style: ${with_init_scripts}
@@ -1467,8 +1511,11 @@
Console owner file path: ${DBUS_CONSOLE_OWNER_FILE}
System bus user: ${DBUS_USER}
Session bus services dir: ${EXPANDED_DATADIR}/dbus-1/services
- 'make check' socket dir: ${TEST_SOCKET_DIR}
-"
+ 'make check' socket dir: ${TEST_SOCKET_DIR}"
+if test x$have_launchd = xyes; then
+ echo " launchd agent dir: ${LAUNCHD_AGENT_DIR}"
+fi
+echo
if test x$enable_tests = xyes; then
echo "NOTE: building with unit tests increases the size of the installed library and renders it insecure."
diff -Nurd dbus/Makefile.am dbus-1.2.12-new/dbus/Makefile.am
--- dbus/Makefile.am 2008-08-07 14:44:36.000000000 -0400
+++ dbus-1.2.12-new/dbus/Makefile.am 2009-02-23 08:33:30.000000000 -0500
@@ -70,6 +70,8 @@
dbus-server.c \
dbus-server-debug-pipe.c \
dbus-server-debug-pipe.h \
+ dbus-server-launchd.c \
+ dbus-server-launchd.h \
dbus-server-protected.h \
dbus-server-socket.c \
dbus-server-socket.h \
diff -Nurd dbus/dbus-bus.c dbus-1.2.12-new/dbus/dbus-bus.c
--- dbus/dbus-bus.c 2008-08-07 14:44:36.000000000 -0400
+++ dbus-1.2.12-new/dbus/dbus-bus.c 2009-02-23 08:33:54.000000000 -0500
@@ -22,6 +22,7 @@
*
*/
+#include <config.h>
#include "dbus-bus.h"
#include "dbus-protocol.h"
#include "dbus-internals.h"
@@ -29,7 +30,7 @@
#include "dbus-marshal-validate.h"
#include "dbus-threads-internal.h"
#include "dbus-connection-internal.h"
-#include <string.h>
+#include "dbus-string.h"
/**
* @defgroup DBusBus Message bus APIs
@@ -147,6 +148,63 @@
}
static dbus_bool_t
+init_session_address (void)
+{
+ dbus_bool_t retval;
+
+ retval = FALSE;
+
+ /* First, look in the environment. This is the normal case on
+ * freedesktop.org/Unix systems. */
+ get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
+ "DBUS_SESSION_BUS_ADDRESS");
+ if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+ {
+ dbus_bool_t supported;
+ DBusString addr;
+ DBusError error = DBUS_ERROR_INIT;
+
+ if (!_dbus_string_init (&addr))
+ return FALSE;%0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment