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"
28 //todo there should be no include below - needed functions should be moved to kdbus-common
29 #include <dbus/dbus-transport-kdbus.h>
31 __u64 sender_name_to_id(const char* name, DBusError* error)
35 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
36 sender_id = strtoull(&name[3], NULL, 10);
38 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
43 char* make_kdbus_bus(DBusBusType type, DBusError *error)
46 struct kdbus_cmd_bus_make head;
50 } __attribute__ ((__aligned__(8))) bus_make;
55 /*TODO Distinguish session and system bus make*/
56 /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED, "...") (?)*/
58 _dbus_verbose("Opening /dev/kdbus/control\n");
59 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
62 _dbus_verbose("--- error %d (%m)\n", fdc);
66 memset(&bus_make, 0, sizeof(bus_make));
67 bus_make.head.bloom_size = 64;
68 bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
70 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
71 bus_make.n_type = KDBUS_MAKE_NAME;
72 bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
73 bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
75 _dbus_verbose("Creating bus '%s'\n", bus_make.name);
76 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
79 _dbus_verbose("--- error %d (%m)\n", ret);
83 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
89 _dbus_verbose("Return value '%s'\n", bus);
93 DBusServer* empty_server_init(char* address)
95 return dbus_server_init_mini(address);
98 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
100 DBusConnection* connection;
101 DBusString daemon_name;
103 dbus_bus_set_bus_connection_address(type, address);
105 connection = dbus_bus_get(type, error);
106 if(connection == NULL)
109 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
110 if(!kdbus_register_policy (&daemon_name, connection))
113 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
116 // dbus_bus_add_match(connection, "type='signal', member='NameAcquired'", error); //not needed if request name ioctled by daemon not libdbus
117 // dbus_bus_add_match(connection, "type='signal', member='NameLost'", error); //todo dispatch and digest this or ioctl about name where daemon checks name presence
118 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'"))
120 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
124 if(dbus_error_is_set(error))
127 _dbus_connection_close_possibly_shared (connection);
128 dbus_connection_unref (connection);
132 _dbus_verbose ("Daemon connected as kdbus client.\n");
137 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
141 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
143 return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
146 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
150 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
152 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
155 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
159 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
161 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
164 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
168 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
170 return list_kdbus_names(fd, listp, array_len);
173 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
177 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
178 if(dbus_error_is_set(error))
181 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
183 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
184 sender_id, _dbus_strerror_from_errno ());
191 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
195 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
196 if(dbus_error_is_set(error))
199 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
201 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
208 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
211 struct nameInfo info;
213 dbus_bool_t ret = FALSE;
215 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
216 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
217 if(inter_ret == 0) //name found
219 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
223 else if(inter_ret == -ENOENT) //name has no owner
224 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
227 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
228 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
234 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
237 struct nameInfo info;
239 dbus_bool_t ret = FALSE;
241 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
242 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
243 if(inter_ret == 0) //name found
245 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
246 *pid = info.processId;
249 else if(inter_ret == -ENOENT) //name has no owner
250 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
253 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
254 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
260 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
263 struct nameInfo info;
265 dbus_bool_t ret = FALSE;
267 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
268 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
269 if(inter_ret == -ENOENT) //name has no owner
270 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
271 else if(inter_ret < 0)
273 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
274 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
278 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
280 _DBUS_SET_OOM (error);
289 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name)
291 DBusConnection *phantom_connection;
295 _dbus_string_init_const(&name, unique_name);
297 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
298 if(phantom_connection == NULL)
300 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
302 /*todo FIXME something should be done to clean up the phantom connection
303 * but we can't use standard disconnect, unref or last_unref because the transport is taken from connection
304 * so we probably should write own function on the basis of _dbus_connection_last_unref
306 phantom_connection = NULL;
308 if(!bus_connection_complete(phantom_connection, &name, &error))
310 /* todo FIXME exactly the same issue as above */
311 phantom_connection = NULL;
314 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
316 return phantom_connection;