1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* kdbus-d.c kdbus related daemon functions
4 * Copyright (C) 2013 Samsung Electronics
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version and under the terms of the GNU
12 * Lesser General Public License as published by the
13 * Free Software Foundation; either version 2.1 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus-connection-internal.h>
29 #include <dbus/kdbus.h>
30 #include <dbus/dbus-bus.h>
32 #include <dbus/kdbus-common.h>
33 #include <dbus/dbus-transport.h>
34 #include <dbus/dbus-transport-kdbus.h>
35 #include "connection.h"
36 #include "activation.h"
47 * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
49 __u64 sender_name_to_id(const char* name, DBusError* error)
53 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
54 sender_id = strtoull(&name[3], NULL, 10);
56 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
62 * Creates kdbus bus of given type.
64 char* make_kdbus_bus(DBusBusType type, DBusError *error)
67 struct kdbus_cmd_bus_make head;
71 } __attribute__ ((__aligned__(8))) bus_make;
76 _dbus_verbose("Opening /dev/kdbus/control\n");
77 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
80 _dbus_verbose("--- error %d (%m)\n", fdc);
81 dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
85 memset(&bus_make, 0, sizeof(bus_make));
86 bus_make.head.bloom_size = 64;
87 bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
89 if(type == DBUS_BUS_SYSTEM)
90 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
91 else if(type == DBUS_BUS_SESSION)
92 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
94 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());
96 bus_make.n_type = KDBUS_MAKE_NAME;
97 bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
98 bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
100 _dbus_verbose("Creating bus '%s'\n", bus_make.name);
101 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
104 _dbus_verbose("--- error %d (%m)\n", ret);
105 dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc);
109 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
115 _dbus_verbose("Return value '%s'\n", bus);
120 * Minimal server init needed by context to go further.
122 DBusServer* empty_server_init(char* address)
124 return dbus_server_init_mini(address);
128 * Connects daemon to bus created by him and adds matches for "system" broadcasts.
129 * Do not requests org.freedesktop.DBus name, because it's to early
130 * (some structures of BusContext are not ready yet).
132 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
134 DBusConnection* connection;
136 dbus_bus_set_bus_connection_address(type, address);
138 connection = dbus_bus_get_private(type, error); /*todo possibly could be optimised by using lower functions*/
139 if(connection == NULL)
142 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
144 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
147 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'"))
149 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
152 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameLost'"))
154 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
157 if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameAcquired'"))
159 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
163 if(dbus_error_is_set(error))
166 _dbus_connection_close_possibly_shared (connection);
167 dbus_connection_unref (connection);
171 _dbus_verbose ("Daemon connected as kdbus client.\n");
177 * Asks bus for org.freedesktop.DBus well-known name.
179 dbus_bool_t register_daemon_name(DBusConnection* connection)
181 DBusString daemon_name;
182 dbus_bool_t retval = FALSE;
183 BusTransaction *transaction;
185 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
186 if(!kdbus_register_policy (&daemon_name, connection))
189 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
192 transaction = bus_transaction_new (bus_connection_get_context(connection));
193 if (transaction == NULL)
195 kdbus_release_name(connection, &daemon_name, 0);
199 if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
201 kdbus_release_name(connection, &daemon_name, 0);
208 bus_transaction_cancel_and_free(transaction);
212 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
216 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
218 return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
221 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
225 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
227 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
230 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
234 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
236 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
240 * Asks kdbus for well-known names registered on the bus
242 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
245 struct kdbus_cmd_names* pCmd;
247 dbus_bool_t ret_val = FALSE;
253 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
254 pCmd = malloc(cmd_size);
257 pCmd->size = cmd_size;
259 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
263 if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
267 if(errno == ENOBUFS) //buffer to small to put all names into it
268 cmd_size = pCmd->size; //here kernel tells how much memory it needs
271 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
275 if(cmd_size) //kernel needs more memory
277 pCmd = realloc(pCmd, cmd_size); //prepare memory
280 goto again; //and try again
284 struct kdbus_cmd_name* pCmd_name;
286 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
289 list = malloc(sizeof(char*) * (list_len + 1));
293 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
295 list[i] = strdup(pCmd_name->name);
303 _dbus_verbose ("Name %d: %s\n", i, list[i]);
309 *array_len = list_len;
320 * Register match rule in kdbus on behalf of sender of the message
322 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
326 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
327 if(dbus_error_is_set(error))
330 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
332 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
333 sender_id, _dbus_strerror_from_errno ());
341 * Removes match rule in kdbus on behalf of sender of the message
343 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
347 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
348 if(dbus_error_is_set(error))
351 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
353 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
360 int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
363 struct nameInfo info;
365 ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
366 if(ret == 0) //unique id of the name
368 sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
369 _dbus_verbose("Unique name discovered:%s\n", owner);
371 else if(ret != -ENOENT)
372 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
378 * Asks kdbus for uid of the owner of the name given in the message
380 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
382 struct nameInfo info;
384 dbus_bool_t ret = FALSE;
386 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
387 if(inter_ret == 0) //name found
389 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
393 else if(inter_ret == -ENOENT) //name has no owner
394 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
397 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
398 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
405 * Asks kdbus for pid of the owner of the name given in the message
407 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, unsigned long* pid, DBusError* error)
409 struct nameInfo info;
411 dbus_bool_t ret = FALSE;
413 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
414 if(inter_ret == 0) //name found
416 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
417 *pid = info.processId;
420 else if(inter_ret == -ENOENT) //name has no owner
421 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
424 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
425 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
432 * Asks kdbus for selinux_security_context of the owner of the name given in the message
434 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
437 struct nameInfo info;
439 dbus_bool_t ret = FALSE;
441 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
442 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
443 if(inter_ret == -ENOENT) //name has no owner
444 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
445 else if(inter_ret < 0)
447 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
448 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
452 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
454 _DBUS_SET_OOM (error);
464 * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
465 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
466 * for now., though in theory someone could hook Windows to NIS or
467 * something. Always returns #FALSE prior to authenticating the
470 * The UID of is only read by bus daemon from kdbus. You can not
471 * call this function from client side of the connection.
473 * You can ask the bus to tell you the UID of another connection though
474 * if you like; this is done with dbus_bus_get_unix_user().
476 * @param connection the connection
477 * @param uid return location for the user ID
478 * @returns #TRUE if uid is filled in with a valid user ID
481 dbus_connection_get_unix_user (DBusConnection *connection,
484 _dbus_return_val_if_fail (connection != NULL, FALSE);
485 _dbus_return_val_if_fail (uid != NULL, FALSE);
487 return kdbus_get_connection_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
491 * Gets the process ID of the connection if any.
492 * Returns #TRUE if the pid is filled in.
494 * @param connection the connection
495 * @param pid return location for the process ID
496 * @returns #TRUE if uid is filled in with a valid process ID
499 dbus_connection_get_unix_process_id (DBusConnection *connection,
502 _dbus_return_val_if_fail (connection != NULL, FALSE);
503 _dbus_return_val_if_fail (pid != NULL, FALSE);
505 return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
509 * Create connection structure for given name. It is needed to control starters - activatable services
510 * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
511 * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
513 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
515 DBusConnection *phantom_connection;
518 _dbus_string_init_const(&Sname, name);
520 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
521 if(phantom_connection == NULL)
523 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
525 dbus_connection_unref_phantom(phantom_connection);
526 phantom_connection = NULL;
527 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
530 if(!bus_connection_complete(phantom_connection, &Sname, error))
532 bus_connection_disconnected(phantom_connection);
533 phantom_connection = NULL;
537 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
540 return phantom_connection;
544 * Registers activatable services as kdbus starters.
546 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
550 dbus_bool_t retval = FALSE;
552 BusTransaction *transaction;
555 transaction = bus_transaction_new (bus_connection_get_context(connection));
556 if (transaction == NULL)
559 if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
562 _dbus_transport_get_socket_fd (dbus_connection_get_transport(connection), &fd);
563 _dbus_string_init(&name);
567 if(!register_kdbus_policy(services[i], fd))
570 if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
573 if(!_dbus_string_append(&name, services[i]))
575 if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
576 (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
578 if(!_dbus_string_set_length(&name, 0))
587 release_kdbus_name(fd, services[j], 0);
589 dbus_free_string_array (services);
590 _dbus_string_free(&name);
591 bus_transaction_cancel_and_free(transaction);
596 * Updates kdbus starters (activatable services) after configuration was reloaded.
597 * It releases all previous starters and registers all new.
599 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
601 dbus_bool_t retval = FALSE;
602 DBusList **services_old;
604 BusService *service = NULL;
605 BusTransaction *transaction;
608 transaction = bus_transaction_new (bus_connection_get_context(connection));
609 if (transaction == NULL)
612 if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
615 services_old = bus_connection_get_services_owned(connection);
616 link = _dbus_list_get_first_link(services_old);
617 link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
623 service = (BusService*) link->data;
627 ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
629 if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
631 if(!bus_service_remove_owner(service, connection, transaction, NULL))
632 _dbus_verbose ("Unable to remove\n");
637 link = _dbus_list_get_next_link (services_old, link);
640 if(!register_kdbus_starters(connection))
642 _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
648 bus_transaction_cancel_and_free(transaction);
653 * Analyzes system broadcasts about id and name changes.
654 * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
656 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
658 const char *name, *old, *new;
660 if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
662 _dbus_verbose ("Couldn't get args of NameOwnerChanged signal: .\n");//, error.message);
666 _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
668 if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
670 if(!strcmp(name, old)) //yes it is - someone has disconnected
672 DBusConnection* conn;
674 conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
676 bus_connection_disconnected(conn);
679 else //it is well-known name
681 if((*old != 0) && (strcmp(old, ":1.1")))
683 DBusMessage *message;
685 if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
688 _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
690 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
694 if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
695 DBUS_TYPE_STRING, &name,
698 dbus_message_unref (message);
702 bus_transaction_send_from_driver (transaction, connection, message);
703 dbus_message_unref (message);
706 if((*new != 0) && (strcmp(new, ":1.1")))
708 DBusMessage *message;
710 _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
712 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
716 if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
717 DBUS_TYPE_STRING, &name,
720 dbus_message_unref (message);
724 bus_transaction_send_from_driver (transaction, connection, message);
725 dbus_message_unref (message);