4 * Created on: Sep 4, 2013
7 * kdbus add-on to dbus daemon
11 #include <dbus/dbus-connection-internal.h>
13 #include <dbus/kdbus.h>
14 #include <dbus/dbus-bus.h>
16 #include <dbus/kdbus-common.h>
17 #include <dbus/dbus-transport.h>
18 #include <dbus/dbus-transport-kdbus.h>
19 #include "connection.h"
20 #include "activation.h"
29 __u64 sender_name_to_id(const char* name, DBusError* error)
33 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
34 sender_id = strtoull(&name[3], NULL, 10);
36 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
41 char* make_kdbus_bus(DBusBusType type, DBusError *error)
44 struct kdbus_cmd_bus_make head;
48 } __attribute__ ((__aligned__(8))) bus_make;
53 /*TODO Distinguish session and system bus make*/
54 /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED, "...") (?)*/
56 _dbus_verbose("Opening /dev/kdbus/control\n");
57 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
60 _dbus_verbose("--- error %d (%m)\n", fdc);
64 memset(&bus_make, 0, sizeof(bus_make));
65 bus_make.head.bloom_size = 64;
66 bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
68 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
69 bus_make.n_type = KDBUS_MAKE_NAME;
70 bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
71 bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
73 _dbus_verbose("Creating bus '%s'\n", bus_make.name);
74 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
77 _dbus_verbose("--- error %d (%m)\n", ret);
81 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
87 _dbus_verbose("Return value '%s'\n", bus);
91 DBusServer* empty_server_init(char* address)
93 return dbus_server_init_mini(address);
96 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
98 DBusConnection* connection;
99 DBusString daemon_name;
101 dbus_bus_set_bus_connection_address(type, address);
103 connection = dbus_bus_get_private(type, error); /*todo possibly could be optimised by using lower functions*/
104 if(connection == NULL)
107 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
108 if(!kdbus_register_policy (&daemon_name, connection))
111 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
114 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'")) //todo handle this in dispatch
116 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
120 if(dbus_error_is_set(error))
123 _dbus_connection_close_possibly_shared (connection);
124 dbus_connection_unref (connection);
128 _dbus_verbose ("Daemon connected as kdbus client.\n");
133 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
137 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
139 return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
142 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
146 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
148 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
151 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
155 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
157 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
160 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
164 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
166 return list_kdbus_names(fd, listp, array_len);
169 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
173 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
174 if(dbus_error_is_set(error))
177 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
179 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
180 sender_id, _dbus_strerror_from_errno ());
187 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
191 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
192 if(dbus_error_is_set(error))
195 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
197 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
204 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
207 struct nameInfo info;
209 dbus_bool_t ret = FALSE;
211 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
212 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
213 if(inter_ret == 0) //name found
215 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
219 else if(inter_ret == -ENOENT) //name has no owner
220 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
223 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
224 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
230 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
233 struct nameInfo info;
235 dbus_bool_t ret = FALSE;
237 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
238 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
239 if(inter_ret == 0) //name found
241 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
242 *pid = info.processId;
245 else if(inter_ret == -ENOENT) //name has no owner
246 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
249 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
250 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
256 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
259 struct nameInfo info;
261 dbus_bool_t ret = FALSE;
263 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
264 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
265 if(inter_ret == -ENOENT) //name has no owner
266 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
267 else if(inter_ret < 0)
269 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
270 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
274 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
276 _DBUS_SET_OOM (error);
285 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
287 DBusConnection *phantom_connection;
290 _dbus_string_init_const(&name, unique_name);
292 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
293 if(phantom_connection == NULL)
295 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
297 dbus_connection_unref_phantom(phantom_connection);
298 phantom_connection = NULL;
299 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
302 if(!bus_connection_complete(phantom_connection, &name, error))
304 dbus_connection_unref_phantom(phantom_connection);
305 phantom_connection = NULL;
309 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
312 return phantom_connection;
315 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
319 dbus_bool_t retval = FALSE;
322 if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
325 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
329 if(!register_kdbus_policy(services[i], fd))
332 if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
338 dbus_free_string_array (services);