0f4689c6100feb9902b09432f32ee737fe742d18
[scm/bb/meta-tizen.git] / meta-tizen-adaptation-oe-core / recipes-core / dbus / files / 0001-Set-correct-address-when-using-address-systemd.patch
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:
4
5 When dbus gets launched through systemd, we need to create an address
6 string based on the sockets passed.
7
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.
11
12 This fixes bus activation when running dbus under a systemd session.
13
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
17 ---
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(-)
22
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,
28      }
29    else if (strcmp (method, "systemd") == 0)
30      {
31 -      int n, *fds;
32 +      int i, n, *fds;
33        DBusString address;
34  
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;
38          }
39  
40 -      _dbus_string_init_const (&address, "systemd:");
41 +      if (!_dbus_string_init (&address))
42 +          goto systemd_oom;
43  
44 -      *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL);
45 -      if (*server_p == NULL)
46 +      for (i = 0; i < n; i++)
47          {
48 -          int i;
49 -
50 -          for (i = 0; i < n; i++)
51 +          if (i > 0)
52              {
53 -              _dbus_close_socket (fds[i], NULL);
54 +              if (!_dbus_string_append (&address, ";"))
55 +                goto systemd_oom;
56              }
57 -          dbus_free (fds);
58 -
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))
62 +            goto systemd_err;
63          }
64  
65 +      *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL);
66 +      if (*server_p == NULL)
67 +        goto systemd_oom;
68 +
69        dbus_free (fds);
70  
71        return DBUS_SERVER_LISTEN_OK;
72 -       }
73 +  systemd_oom:
74 +      _DBUS_SET_OOM (error);
75 +  systemd_err:
76 +      for (i = 0; i < n; i++)
77 +        {
78 +          _dbus_close_socket (fds[i], NULL);
79 +        }
80 +      dbus_free (fds);
81 +      _dbus_string_free (&address);
82 +
83 +      return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
84 +    }
85  #ifdef DBUS_ENABLE_LAUNCHD
86    else if (strcmp (method, "launchd") == 0)
87      {
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
92 @@ -55,6 +55,7 @@
93  #include <netinet/in.h>
94  #include <netdb.h>
95  #include <grp.h>
96 +#include <arpa/inet.h>
97  
98  #ifdef HAVE_ERRNO_H
99  #include <errno.h>
100 @@ -4173,4 +4174,71 @@ _dbus_check_setuid (void)
101  #endif
102  }
103  
104 +/**
105 + * Read the address from the socket and append it to the string
106 + *
107 + * @param fd the socket
108 + * @param address
109 + * @param error return location for error code
110 + */
111 +dbus_bool_t
112 +_dbus_append_address_from_socket (int         fd,
113 +                                  DBusString *address,
114 +                                  DBusError  *error)
115 +{
116 +  union {
117 +      struct sockaddr sa;
118 +      struct sockaddr_storage storage;
119 +      struct sockaddr_un un;
120 +      struct sockaddr_in ipv4;
121 +      struct sockaddr_in6 ipv6;
122 +  } socket;
123 +  char hostip[INET6_ADDRSTRLEN];
124 +  int size = sizeof (socket);
125 +
126 +  if (getsockname (fd, &socket.sa, &size))
127 +    goto err;
128 +
129 +  switch (socket.sa.sa_family)
130 +    {
131 +    case AF_UNIX:
132 +      if (socket.un.sun_path[0]=='\0')
133 +        {
134 +          if (_dbus_string_append_printf (address, "unix:abstract=%s", &(socket.un.sun_path[1])))
135 +            return TRUE;
136 +        }
137 +      else
138 +        {
139 +          if (_dbus_string_append_printf (address, "unix:path=%s", socket.un.sun_path))
140 +            return TRUE;
141 +        }
142 +      break;
143 +    case AF_INET:
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)))
147 +          return TRUE;
148 +      break;
149 +#ifdef AF_INET6
150 +    case AF_INET6:
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)))
154 +          return TRUE;
155 +      break;
156 +#endif
157 +    default:
158 +      dbus_set_error (error,
159 +                      _dbus_error_from_errno (EINVAL),
160 +                      "Failed to read address from socket: Unknown socket type.");
161 +      return FALSE;
162 +    }
163 + err:
164 +  dbus_set_error (error,
165 +                  _dbus_error_from_errno (errno),
166 +                  "Failed to open socket: %s",
167 +                  _dbus_strerror (errno));
168 +  return FALSE;
169 +}
170 +
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,
177  
178  void _dbus_close_all (void);
179  
180 +dbus_bool_t _dbus_append_address_from_socket (int         fd,
181 +                                              DBusString *address,
182 +                                              DBusError  *error);
183 +
184  /** @} */
185  
186  DBUS_END_DECLS