4 * Created on: Sep 13, 2013
7 * Kdbus internal util functions used by daemon and libdbus
11 #include "kdbus-common.h"
17 #include <dbus/dbus-internals.h>
18 #include <dbus/dbus-shared.h>
20 static struct kdbus_policy *make_policy_name(const char *name)
22 struct kdbus_policy *p;
25 size = offsetof(struct kdbus_policy, name) + strlen(name) + 1;
31 p->type = KDBUS_POLICY_NAME;
32 strcpy(p->name, name);
37 static struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id)
39 struct kdbus_policy *p;
40 __u64 size = sizeof(*p);
48 p->type = KDBUS_POLICY_ACCESS;
49 p->access.type = type;
50 p->access.bits = bits;
56 static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size)
58 struct kdbus_policy *dst = (struct kdbus_policy *) ((char *) cmd_policy + cmd_policy->size);
60 if (cmd_policy->size + policy->size > max_size)
63 memcpy(dst, policy, policy->size);
64 cmd_policy->size += KDBUS_ALIGN8(policy->size);
69 * Registers kdbus policy for given connection.
71 * Policy sets rights of the name (unique or well known) on the bus. Without policy it is
72 * not possible to send or receive messages. It must be set separately for unique id and
73 * well known name of the connection. It is set after registering on the bus, but before
74 * requesting for name. The policy is valid for the given name, not for the connection.
76 * Name of the policy equals name on the bus.
78 * @param name name of the policy = name of the connection
79 * @param connection the connection
80 * @param error place to store errors
82 * @returns #TRUE on success
84 dbus_bool_t register_kdbus_policy(const char* name, int fd)
86 struct kdbus_cmd_policy *cmd_policy;
87 struct kdbus_policy *policy;
90 cmd_policy = alloca(size);
91 memset(cmd_policy, 0, size);
93 policy = (struct kdbus_policy *) cmd_policy->policies;
94 cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);
96 policy = make_policy_name(name);
97 append_policy(cmd_policy, policy, size);
99 policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, getuid());
100 append_policy(cmd_policy, policy, size);
102 policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
103 append_policy(cmd_policy, policy, size);
105 policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
106 append_policy(cmd_policy, policy, size);
108 if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
110 _dbus_verbose ("Error setting policy: %m, %d", errno);
114 _dbus_verbose("Policy %s set correctly\n", name);
118 dbus_bool_t list_kdbus_names(int fd, char ***listp, int *array_len)
120 struct kdbus_cmd_names* pCmd;
122 dbus_bool_t ret_val = FALSE;
128 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
129 pCmd = malloc(cmd_size);
132 pCmd->size = cmd_size;
136 if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
140 if(errno == ENOBUFS) //buffer to small to put all names into it
141 cmd_size = pCmd->size; //here kernel tells how much memory it needs
144 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
148 if(cmd_size) //kernel needs more memory
150 pCmd = realloc(pCmd, cmd_size); //prepare memory
153 goto again; //and try again
157 struct kdbus_cmd_name* pCmd_name;
159 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
162 list = malloc(sizeof(char*) * (list_len + 1));
166 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
168 list[i] = strdup(pCmd_name->name);
176 _dbus_verbose ("Name %d: %s\n", i, list[i]);
182 *array_len = list_len;
193 * kdbus version of dbus_bus_request_name.
195 * Asks the bus to assign the given name to this connection.
197 * Use same flags as original dbus version with one exception below.
198 * Result flag #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER is currently
199 * never returned by kdbus, instead DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
200 * is returned by kdbus.
202 * @param connection the connection
203 * @param name the name to request
205 * @param error location to store the error
206 * @returns a result code, -1 if error is set
208 int request_kdbus_name(int fd, const char *name, const __u64 flags, __u64 id)
210 struct kdbus_cmd_name *cmd_name;
212 __u64 size = sizeof(*cmd_name) + strlen(name) + 1;
213 __u64 flags_kdbus = 0;
215 cmd_name = alloca(size);
217 // memset(cmd_name, 0, size);
218 strcpy(cmd_name->name, name);
219 cmd_name->size = size;
221 if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
222 flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
223 if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
224 flags_kdbus |= KDBUS_NAME_QUEUE;
225 if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
226 flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
228 cmd_name->flags = flags_kdbus;
230 // cmd_name->conn_flags = 0;
232 _dbus_verbose("Request name - flags sent: 0x%llx !!!!!!!!!\n", cmd_name->flags);
234 if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
236 _dbus_verbose ("error acquiring name '%s': %m, %d\n", name, errno);
238 return DBUS_REQUEST_NAME_REPLY_EXISTS;
242 _dbus_verbose("Request name - received flag: 0x%llx !!!!!!!!!\n", cmd_name->flags);
244 if(cmd_name->flags & KDBUS_NAME_IN_QUEUE)
245 return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
247 return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
248 /*todo now 1 code is never returned - DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
249 * because kdbus never returns it now
253 int release_kdbus_name(int fd, const char *name, __u64 id)
255 struct kdbus_cmd_name *cmd_name;
257 __u64 size = sizeof(*cmd_name) + strlen(name) + 1;
259 cmd_name = alloca(size);
261 strcpy(cmd_name->name, name);
262 cmd_name->size = size;
264 if (ioctl(fd, KDBUS_CMD_NAME_RELEASE, cmd_name))
267 return DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
268 else if (errno == EPERM)
269 return DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
270 _dbus_verbose ("error releasing name '%s' for id:%llu. Error: %m, %d\n", name, (unsigned long long)id, errno);
274 _dbus_verbose("Name '%s' released\n", name);
276 return DBUS_RELEASE_NAME_REPLY_RELEASED;