1 From: Simon Peeters <peeters.simon@gmail.com>
2 Date: Sun, 7 Oct 2012 16:59:30 +0200
3 Subject: Set correct address when using --address=systemd:
5 When dbus gets launched through systemd, we need to create an address
6 string based on the sockets passed.
8 The _dbus_append_addres_from_socket() function is responsible for
9 extracting the address information from the file-descriptor and
10 formatting it in a dbus friendly way.
12 This fixes bus activation when running dbus under a systemd session.
14 Bug: https://bugs.freedesktop.org/show_bug.cgi?id=50962
15 Signed-off-by: Simon Peeters <peeters.simon@gmail.com>
16 Applied-upstream: 1.7.0, commit:d728fdc655f17031da3bb129ab2fd17dadf0fe3a
18 dbus/dbus-server-unix.c | 38 ++++++++++++++++++---------
19 dbus/dbus-sysdeps-unix.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
20 dbus/dbus-sysdeps-unix.h | 4 +++
21 3 files changed, 97 insertions(+), 13 deletions(-)
23 diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c
24 index 130f66e..d995240 100644
25 --- a/dbus/dbus-server-unix.c
26 +++ b/dbus/dbus-server-unix.c
27 @@ -149,7 +149,7 @@ _dbus_server_listen_platform_specific (DBusAddressEntry *entry,
29 else if (strcmp (method, "systemd") == 0)
35 n = _dbus_listen_systemd_sockets (&fds, error);
36 @@ -159,27 +159,39 @@ _dbus_server_listen_platform_specific (DBusAddressEntry *entry,
37 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
40 - _dbus_string_init_const (&address, "systemd:");
41 + if (!_dbus_string_init (&address))
44 - *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL);
45 - if (*server_p == NULL)
46 + for (i = 0; i < n; i++)
50 - for (i = 0; i < n; i++)
53 - _dbus_close_socket (fds[i], NULL);
54 + if (!_dbus_string_append (&address, ";"))
59 - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
60 - return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
61 + if (!_dbus_append_address_from_socket (fds[i], &address, error))
65 + *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL);
66 + if (*server_p == NULL)
71 return DBUS_SERVER_LISTEN_OK;
74 + _DBUS_SET_OOM (error);
76 + for (i = 0; i < n; i++)
78 + _dbus_close_socket (fds[i], NULL);
81 + _dbus_string_free (&address);
83 + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
85 #ifdef DBUS_ENABLE_LAUNCHD
86 else if (strcmp (method, "launchd") == 0)
88 diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c
89 index e31c735..7c9fb09 100644
90 --- a/dbus/dbus-sysdeps-unix.c
91 +++ b/dbus/dbus-sysdeps-unix.c
93 #include <netinet/in.h>
96 +#include <arpa/inet.h>
100 @@ -4173,4 +4174,71 @@ _dbus_check_setuid (void)
105 + * Read the address from the socket and append it to the string
107 + * @param fd the socket
109 + * @param error return location for error code
112 +_dbus_append_address_from_socket (int fd,
113 + DBusString *address,
117 + struct sockaddr sa;
118 + struct sockaddr_storage storage;
119 + struct sockaddr_un un;
120 + struct sockaddr_in ipv4;
121 + struct sockaddr_in6 ipv6;
123 + char hostip[INET6_ADDRSTRLEN];
124 + int size = sizeof (socket);
126 + if (getsockname (fd, &socket.sa, &size))
129 + switch (socket.sa.sa_family)
132 + if (socket.un.sun_path[0]=='\0')
134 + if (_dbus_string_append_printf (address, "unix:abstract=%s", &(socket.un.sun_path[1])))
139 + if (_dbus_string_append_printf (address, "unix:path=%s", socket.un.sun_path))
144 + if (inet_ntop (AF_INET, &socket.ipv4.sin_addr, hostip, sizeof (hostip)))
145 + if (_dbus_string_append_printf (address, "tcp:family=ipv4,host=%s,port=%u",
146 + hostip, ntohs (socket.ipv4.sin_port)))
151 + if (inet_ntop (AF_INET6, &socket.ipv6.sin6_addr, hostip, sizeof (hostip)))
152 + if (_dbus_string_append_printf (address, "tcp:family=ipv6,host=%s,port=%u",
153 + hostip, ntohs (socket.ipv6.sin6_port)))
158 + dbus_set_error (error,
159 + _dbus_error_from_errno (EINVAL),
160 + "Failed to read address from socket: Unknown socket type.");
164 + dbus_set_error (error,
165 + _dbus_error_from_errno (errno),
166 + "Failed to open socket: %s",
167 + _dbus_strerror (errno));
171 /* tests in dbus-sysdeps-util.c */
172 diff --git a/dbus/dbus-sysdeps-unix.h b/dbus/dbus-sysdeps-unix.h
173 index 9b70896..a265b33 100644
174 --- a/dbus/dbus-sysdeps-unix.h
175 +++ b/dbus/dbus-sysdeps-unix.h
176 @@ -138,6 +138,10 @@ dbus_bool_t _dbus_parse_uid (const DBusString *uid_str,
178 void _dbus_close_all (void);
180 +dbus_bool_t _dbus_append_address_from_socket (int fd,
181 + DBusString *address,