[lib-fix] libdbus refactored to eliminate "is kdbus" queries and branches in non...
[platform/upstream/dbus.git] / bus / kdbus-d.c
1 /*
2  * kdbus-d.c
3  *
4  *  Created on: Sep 4, 2013
5  *      Author: r.pajak
6  *
7  *  kdbus add-on to dbus daemon
8  *
9  */
10
11 #include "kdbus-d.h"
12 #include <dbus/kdbus.h>
13 #include <dbus/dbus-connection-internal.h>
14 #include <dbus/dbus-bus.h>
15
16 #include <utils.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21
22 char* make_kdbus_bus(DBusBusType type, DBusError *error)
23 {
24     struct {
25         struct kdbus_cmd_bus_make head;
26         uint64_t n_size;
27         uint64_t n_type;
28         char name[64];
29     } __attribute__ ((__aligned__(8))) bus_make;
30
31     int fdc, ret;
32     char *bus;
33
34     /*TODO Distinguish session and system bus make*/
35     /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED,  "...") (?)*/
36
37     _dbus_verbose("Opening /dev/kdbus/control\n");
38     fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
39     if (fdc < 0)
40     {
41         _dbus_verbose("--- error %d (%m)\n", fdc);
42         return NULL;
43     }
44
45     memset(&bus_make, 0, sizeof(bus_make));
46     bus_make.head.bloom_size = 64;
47     bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
48
49     snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
50     bus_make.n_type = KDBUS_MAKE_NAME;
51     bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
52     bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
53
54     _dbus_verbose("Creating bus '%s'\n", bus_make.name);
55     ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
56     if (ret)
57     {
58         _dbus_verbose("--- error %d (%m)\n", ret);
59         return NULL;
60     }
61
62     if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
63     {
64         BUS_SET_OOM (error);
65         return NULL;
66     }
67
68     _dbus_verbose("Return value '%s'\n", bus);
69         return bus;
70 }
71
72 DBusServer* fake_server(char* address)
73 {
74         return dbus_server_init_mini(address);
75 }
76
77 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
78 {
79         DBusConnection* connection;
80
81         dbus_bus_set_bus_connection_address(type, address);
82
83         connection = dbus_bus_get(type, error);
84         if(connection == NULL)
85                 return NULL;
86
87         if(dbus_bus_request_name(connection, DBUS_SERVICE_DBUS, 0, error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
88                 goto failed;
89
90         dbus_bus_add_match(connection, "type='signal', member='NameAcquired'", error);
91         dbus_bus_add_match(connection, "type='signal', member='NameLost'", error);
92         if(dbus_error_is_set(error))
93         {
94 failed:
95                 _dbus_connection_close_possibly_shared (connection);
96                 dbus_connection_unref (connection);
97                 connection = NULL;
98         }
99         else
100                 _dbus_verbose ("Daemon connected as kdbus client.\n");
101
102         return connection;
103 }
104
105 dbus_bool_t setup_connection(BusContext* context, DBusError* error)
106 {
107         //on the basis of bus_connections_setup_connection from connection.c
108
109         dbus_bool_t retval = FALSE; //todo opitimize
110 //      DBusConnection* connection;
111
112         //todo what to do with error
113
114 /*      connection = context->myConnection;  //todo
115         dbus_connection_set_route_peer_messages (connection, TRUE);
116
117         if (!dbus_connection_set_watch_functions (connection,
118                                                                                         add_connection_watch,
119                                                                                         remove_connection_watch,
120                                                                                         toggle_connection_watch,
121                                                                                         connection,
122                                                                                         NULL))
123                 goto out;
124
125         if (!dbus_connection_set_timeout_functions (connection,
126                                                                                           add_connection_timeout,
127                                                                                           remove_connection_timeout,
128                                                                                           NULL,
129                                                                                           connection, NULL))
130                 goto out;
131
132         dbus_connection_set_dispatch_status_function (connection,
133                                                                                                 dispatch_status_function,
134                                                                                                 bus_context_get_loop (context),
135                                                                                                 NULL);
136
137         if (!bus_dispatch_add_connection (connection))
138                 goto out;
139
140         retval = TRUE;
141
142         out:
143         if (!retval)
144         {
145           if (!dbus_connection_set_watch_functions (connection,
146                                                                                                 NULL, NULL, NULL,
147                                                                                                 connection,
148                                                                                                 NULL))
149                 _dbus_assert_not_reached ("setting watch functions to NULL failed");
150
151           if (!dbus_connection_set_timeout_functions (connection,
152                                                                                                   NULL, NULL, NULL,
153                                                                                                   connection,
154                                                                                                   NULL))
155                 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
156
157
158           dbus_connection_set_dispatch_status_function (connection,
159                                                                                                         NULL, NULL, NULL);
160         }
161 */
162         return retval;
163 }