From 52bfd028172c16b4151282a128ced71b978a2e59 Mon Sep 17 00:00:00 2001 From: Benjamin Reed Date: Sat, 26 Jan 2008 14:15:39 -0500 Subject: [PATCH] Add support for launchd Add support for launchd (http://developer.apple.com/macosx/launchd.html) Launchd is superficially like inetd in that it controls all resources for system-wide and session-level daemons. In the case of this dbus implementation, launchd allocates and listens on a socket, dynamically launching dbus when a client tries to access the socket for the first time. When dbus-daemon is started, it asks launchd for the socket file descriptor, and then initializes itself using that pre-existing FD. Note that this only occurs if in session.conf is set to "autolaunch:" -- it is still possible to us DBus on Mac OS X in the normal UNIXy way by specifying a tcp: or unix: session address. From the client-side, if DBUS_SESSION_BUS_ADDRESS is set, the client will connect normally, just like any other unix client. If it is not, it checks for the environment variable DBUS_LAUNCHD_SESSION_BUS_SOCKET and crafts a unix: socket URL from it, and then continues normally. --- bus/session.conf.in | 2 +- configure.in | 42 ++++++++++++++ dbus/Makefile.am | 9 +++- dbus/dbus-bus.c | 32 ++++++++++- dbus/dbus-server-launchd.c | 136 ++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-server-launchd.h | 35 +++++++++++ dbus/dbus-server-unix.c | 26 ++++++++ 7 files changed, 279 insertions(+), 3 deletions(-) create mode 100644 dbus/dbus-server-launchd.c create mode 100644 dbus/dbus-server-launchd.h diff --git a/bus/session.conf.in b/bus/session.conf.in index b2dee5b..ea29849 100644 --- a/bus/session.conf.in +++ b/bus/session.conf.in @@ -8,7 +8,7 @@ session - unix:tmpdir=@DBUS_SESSION_SOCKET_DIR@ + @DBUS_SESSION_BUS_DEFAULT_ADDRESS@ diff --git a/configure.in b/configure.in index 2c3d00f..776d196 100644 --- a/configure.in +++ b/configure.in @@ -77,6 +77,7 @@ AC_ARG_ENABLE(inotify, AS_HELP_STRING([--enable-inotify],[build with inotify sup 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])) @@ -955,6 +956,27 @@ fi 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]) + [ -z "$LAUNCHCTL" ] && have_launchd=no + + 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) + dnl console owner file if test x$enable_console_owner_file = xno ; then have_console_owner_file=no; @@ -1277,8 +1299,27 @@ fi AC_DEFINE_UNQUOTED(DBUS_SESSION_SOCKET_DIR, "$DBUS_SESSION_SOCKET_DIR", [Where per-session bus puts its sockets]) AC_SUBST(DBUS_SESSION_SOCKET_DIR) +if test x$have_launchd = xyes; then + DBUS_SESSION_BUS_DEFAULT_ADDRESS="autolaunch:" +else + DBUS_SESSION_BUS_DEFAULT_ADDRESS="unix:tmpdir=$DBUS_SESSION_SOCKET_DIR" +fi +AC_SUBST(DBUS_SESSION_BUS_DEFAULT_ADDRESS) + AC_DEFINE_UNQUOTED(DBUS_UNIX, "1", [Defined on UNIX and Linux systems and not on Windows]) +# darwin needs this to initialize the environment +AC_CHECK_HEADERS(crt_externs.h) +AC_CHECK_FUNC(_NSGetEnviron, [AC_DEFINE(HAVE_NSGETENVIRON, 1, [Define if your system needs _NSGetEnviron to set up the environment])]) +AH_VERBATIM(_DARWIN_ENVIRON, +[ +#if defined(HAVE_NSGETENVIRON) && defined(HAVE_CRT_EXTERNS_H) +# include +# include +# define environ (*_NSGetEnviron()) +#endif +]) + AC_OUTPUT([ Doxyfile dbus/dbus-arch-deps.h @@ -1351,6 +1392,7 @@ echo " 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} diff --git a/dbus/Makefile.am b/dbus/Makefile.am index e966a43..3a9c3ad 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -9,6 +9,12 @@ INCLUDES=-I$(top_builddir) -I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) @PIC_CFLAGS@ -D dbusincludedir=$(includedir)/dbus-1.0/dbus dbusarchincludedir=$(libdir)/dbus-1.0/include/dbus +if DBUS_ENABLE_LAUNCHD +LAUNCHD_SOURCES = \ + dbus-server-launchd.h \ + dbus-server-launchd.c +endif + lib_LTLIBRARIES=libdbus-1.la dbusinclude_HEADERS= \ @@ -92,7 +98,8 @@ DBUS_LIB_SOURCES= \ dbus-uuidgen.c \ dbus-uuidgen.h \ dbus-watch.c \ - dbus-watch.h + dbus-watch.h \ + $(LAUNCHD_SOURCES) ## dbus-md5.c \ ## dbus-md5.h \ diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c index c7f43e8..48cab0d 100644 --- a/dbus/dbus-bus.c +++ b/dbus/dbus-bus.c @@ -22,11 +22,13 @@ * */ +#include #include "dbus-bus.h" #include "dbus-protocol.h" #include "dbus-internals.h" #include "dbus-message.h" #include "dbus-marshal-validate.h" +#include "dbus-string.h" #include "dbus-threads-internal.h" #include "dbus-connection-internal.h" #include @@ -195,10 +197,38 @@ init_connections_unlocked (void) if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL) { _dbus_verbose ("Filling in session bus address...\n"); - + if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION], "DBUS_SESSION_BUS_ADDRESS")) + { return FALSE; + } + +#ifdef DBUS_ENABLE_LAUNCHD + if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL) + { + const char *bus_socket = _dbus_getenv ("DBUS_LAUNCHD_SESSION_BUS_SOCKET"); + if (bus_socket != NULL) + { + DBusString session_address; + if (!_dbus_string_init (&session_address)) + return FALSE; + + if (!_dbus_string_append (&session_address, "unix:path=")) + { + _dbus_string_free (&session_address); + return FALSE; + } + + if (!_dbus_string_append (&session_address, bus_socket)) + { + _dbus_string_free (&session_address); + return FALSE; + } + bus_connection_addresses[DBUS_BUS_SESSION] = _dbus_string_get_data(&session_address); + } + } +#endif if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL) bus_connection_addresses[DBUS_BUS_SESSION] = diff --git a/dbus/dbus-server-launchd.c b/dbus/dbus-server-launchd.c new file mode 100644 index 0000000..e5101f3 --- /dev/null +++ b/dbus/dbus-server-launchd.c @@ -0,0 +1,136 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-server-win.c Server implementation for WIN network protocols. + * + * Copyright (C) 2008 Benjamin Reed + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include + +#include "dbus-server-socket.h" +#include "dbus-server-launchd.h" + +/** + * @defgroup DBusServerLaunchd DBusServer implementations for Launchd + * @ingroup DBusInternals + * @brief Implementation details of DBusServer with Launchd support + * + * @{ + */ + +/** + * Creates a new server from launchd. + * + * @param socket_key they key to use when looking up the file descriptor from launchd + * @param error location to store reason for failure. + * @returns the new server, or #NULL on failure. + */ + +DBusServer* +_dbus_server_new_for_launchd_fd (const char *socket_key, DBusError *error) +{ + DBusServer *server; + DBusString address; + int launchd_fd; + launch_data_t sockets_dict, checkin_response; + launch_data_t checkin_request; + launch_data_t listening_fd_array, listening_fd; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (!_dbus_string_init (&address)) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return NULL; + } + if (!_dbus_string_append (&address, "autolaunch:key=")) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto l_failed_0; + } + if (!_dbus_string_append (&address, socket_key)) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto l_failed_0; + } + + if ((checkin_request = launch_data_new_string(LAUNCH_KEY_CHECKIN)) == NULL) { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, "launch_data_new_string(\"%s\") Unable to create string.\n", LAUNCH_KEY_CHECKIN); + goto l_failed_0; + } + + if ((checkin_response = launch_msg(checkin_request)) == NULL) { + dbus_set_error (error, DBUS_ERROR_IO_ERROR, "launch_msg(\"%s\") IPC failure: %s\n", LAUNCH_KEY_CHECKIN, strerror(errno)); + goto l_failed_0; + } + + if (LAUNCH_DATA_ERRNO == launch_data_get_type(checkin_response)) { + dbus_set_error (error, DBUS_ERROR_FAILED, "Check-in failed: %s\n", strerror(launch_data_get_errno(checkin_response))); + goto l_failed_0; + } + + sockets_dict = launch_data_dict_lookup(checkin_response, LAUNCH_JOBKEY_SOCKETS); + if (NULL == sockets_dict) { + dbus_set_error (error, DBUS_ERROR_IO_ERROR, "No sockets found to answer requests on!\n"); + goto l_failed_0; + } + + listening_fd_array = launch_data_dict_lookup(sockets_dict, socket_key); + if (NULL == listening_fd_array) { + dbus_set_error (error, DBUS_ERROR_IO_ERROR, "No known sockets found to answer requests on!\n"); + goto l_failed_0; + } + + if (launch_data_array_get_count(listening_fd_array)!=1) { + dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED, "Expected 1 socket from launchd, got %d.\n", launch_data_array_get_count(listening_fd_array)); + goto l_failed_0; + } + + listening_fd=launch_data_array_get_index(listening_fd_array, 0); + launchd_fd=launch_data_get_fd(listening_fd); + + _dbus_fd_set_close_on_exec (launchd_fd); + + if (launchd_fd < 0) + { + _DBUS_ASSERT_ERROR_IS_SET (error); + goto l_failed_0; + } + + server = _dbus_server_new_for_socket (&launchd_fd, 1, &address); + if (server == NULL) + { + dbus_set_error (error, DBUS_ERROR_NO_SERVER, "Unable to listen on launchd fd %d.", launchd_fd); + goto l_failed_0; + } + + _dbus_string_free (&address); + + return server; + + l_failed_0: + _dbus_string_free (&address); + + return NULL; +} + +/** @} */ + diff --git a/dbus/dbus-server-launchd.h b/dbus/dbus-server-launchd.h new file mode 100644 index 0000000..8719ecb --- /dev/null +++ b/dbus/dbus-server-launchd.h @@ -0,0 +1,35 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-server-launchd.h Server implementation for launchd integration. + * + * Copyright (C) 2008 Benjamin Reed + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef DBUS_SERVER_LAUNCHD_H +#define DBUS_SERVER_LAUNCHD_H + +#include +#include + +DBUS_BEGIN_DECLS + +DBusServer* _dbus_server_new_for_launchd_fd (const char *socket_fd, DBusError *error); + +DBUS_END_DECLS + +#endif /* DBUS_SERVER_LAUNCHD_H */ diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index 1dda5d1..ddc0944 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -21,6 +21,7 @@ * */ +#include #include "dbus-internals.h" #include "dbus-server-unix.h" #include "dbus-server-socket.h" @@ -29,6 +30,10 @@ #include "dbus-sysdeps-unix.h" #include "dbus-string.h" +#ifdef DBUS_ENABLE_LAUNCHD +#include "dbus-server-launchd.h" +#endif + /** * @defgroup DBusServerUnix DBusServer implementations for UNIX * @ingroup DBusInternals @@ -145,6 +150,27 @@ _dbus_server_listen_platform_specific (DBusAddressEntry *entry, return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; } } +#ifdef DBUS_ENABLE_LAUNCHD + else if (strcmp (method, "autolaunch") == 0) + { + + const char *launchd_key = dbus_address_entry_get_value (entry, "key"); + if (launchd_key == NULL) + launchd_key = "session"; + *server_p = _dbus_server_new_for_launchd_fd (launchd_key, error); + + if (*server_p != NULL) + { + _DBUS_ASSERT_ERROR_IS_CLEAR(error); + return DBUS_SERVER_LISTEN_OK; + } + else + { + _DBUS_ASSERT_ERROR_IS_SET(error); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; + } + } +#endif else { /* If we don't handle the method, we return NULL with the -- 1.5.3.7