4 * Created on: Sep 4, 2013
7 * kdbus add-on to dbus daemon
12 #include <dbus/kdbus.h>
13 #include <dbus/dbus-connection-internal.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>
27 //todo there should be no include below - needed functions should be moved to kdbus-common
28 #include <dbus/dbus-transport-kdbus.h>
30 __u64 sender_name_to_id(const char* name, DBusError* error)
34 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
35 sender_id = strtoull(&name[3], NULL, 10);
37 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
42 char* make_kdbus_bus(DBusBusType type, DBusError *error)
45 struct kdbus_cmd_bus_make head;
49 } __attribute__ ((__aligned__(8))) bus_make;
54 /*TODO Distinguish session and system bus make*/
55 /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED, "...") (?)*/
57 _dbus_verbose("Opening /dev/kdbus/control\n");
58 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
61 _dbus_verbose("--- error %d (%m)\n", fdc);
65 memset(&bus_make, 0, sizeof(bus_make));
66 bus_make.head.bloom_size = 64;
67 bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
69 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
70 bus_make.n_type = KDBUS_MAKE_NAME;
71 bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
72 bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
74 _dbus_verbose("Creating bus '%s'\n", bus_make.name);
75 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
78 _dbus_verbose("--- error %d (%m)\n", ret);
82 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
88 _dbus_verbose("Return value '%s'\n", bus);
92 DBusServer* empty_server_init(char* address)
94 return dbus_server_init_mini(address);
97 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
99 DBusConnection* connection;
100 DBusString daemon_name;
102 dbus_bus_set_bus_connection_address(type, address);
104 connection = dbus_bus_get(type, error);
105 if(connection == NULL)
108 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
109 if(!kdbus_register_policy (&daemon_name, connection))
112 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
115 // dbus_bus_add_match(connection, "type='signal', member='NameAcquired'", error); //not needed if request name ioctled by daemon not libdbus
116 // dbus_bus_add_match(connection, "type='signal', member='NameLost'", error); //todo dispatch and digest this or ioctl about name where daemon checks name presence
117 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'"))
119 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
123 if(dbus_error_is_set(error))
126 _dbus_connection_close_possibly_shared (connection);
127 dbus_connection_unref (connection);
131 _dbus_verbose ("Daemon connected as kdbus client.\n");
136 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
140 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
142 return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
145 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
149 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
151 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
154 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
158 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
160 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
163 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
167 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
169 return list_kdbus_names(fd, listp, array_len);
172 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
176 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
177 if(dbus_error_is_set(error))
180 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
182 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
183 sender_id, _dbus_strerror_from_errno ());
190 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
194 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
195 if(dbus_error_is_set(error))
198 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
200 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
207 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
210 struct nameInfo info;
212 dbus_bool_t ret = FALSE;
214 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
215 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
216 if(inter_ret == 0) //name found
218 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
222 else if(inter_ret == -ENOENT) //name has no owner
223 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
226 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
227 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
233 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
236 struct nameInfo info;
238 dbus_bool_t ret = FALSE;
240 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
241 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
242 if(inter_ret == 0) //name found
244 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
245 *pid = info.processId;
248 else if(inter_ret == -ENOENT) //name has no owner
249 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
252 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
253 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
259 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
262 struct nameInfo info;
264 dbus_bool_t ret = FALSE;
266 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
267 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
268 if(inter_ret == -ENOENT) //name has no owner
269 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
270 else if(inter_ret < 0)
272 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
273 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
277 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
279 _DBUS_SET_OOM (error);