gdbus: Implement DBus.Properties.Set method
[platform/upstream/connman.git] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <dbus/dbus.h>
33
34 #include "gdbus.h"
35
36 #define info(fmt...)
37 #define error(fmt...)
38 #define debug(fmt...)
39
40 struct generic_data {
41         unsigned int refcount;
42         GSList *interfaces;
43         char *introspect;
44 };
45
46 struct interface_data {
47         char *name;
48         const GDBusMethodTable *methods;
49         const GDBusSignalTable *signals;
50         const GDBusPropertyTable *properties;
51         void *user_data;
52         GDBusDestroyFunction destroy;
53 };
54
55 struct security_data {
56         GDBusPendingReply pending;
57         DBusMessage *message;
58         const GDBusMethodTable *method;
59         void *iface_user_data;
60 };
61
62 struct property_data {
63         GDBusPendingPropertySet id;
64         DBusMessage *message;
65 };
66
67 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
68                                                 const char *direction)
69 {
70         for (; args && args->name; args++) {
71                 g_string_append_printf(gstr,
72                                         "\t\t\t<arg name=\"%s\" type=\"%s\"",
73                                         args->name, args->signature);
74
75                 if (direction)
76                         g_string_append_printf(gstr,
77                                         " direction=\"%s\"/>\n", direction);
78                 else
79                         g_string_append_printf(gstr, "/>\n");
80
81         }
82 }
83
84 #define G_DBUS_ANNOTATE(prefix_, name_, value_)                         \
85         prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" "  \
86         "value=\"" value_ "\"/>\n"
87
88 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
89         G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
90
91 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
92         G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
93
94 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
95 {
96         const GDBusMethodTable *method;
97         const GDBusSignalTable *signal;
98
99         for (method = iface->methods; method && method->name; method++) {
100                 gboolean deprecated = method->flags &
101                                                 G_DBUS_METHOD_FLAG_DEPRECATED;
102                 gboolean noreply = method->flags &
103                                                 G_DBUS_METHOD_FLAG_NOREPLY;
104
105                 if (!deprecated && !noreply &&
106                                 !(method->in_args && method->in_args->name) &&
107                                 !(method->out_args && method->out_args->name))
108                         g_string_append_printf(gstr,
109                                                 "\t\t<method name=\"%s\"/>\n",
110                                                 method->name);
111                 else {
112                         g_string_append_printf(gstr,
113                                                 "\t\t<method name=\"%s\">\n",
114                                                 method->name);
115                         print_arguments(gstr, method->in_args, "in");
116                         print_arguments(gstr, method->out_args, "out");
117
118                         if (deprecated)
119                                 g_string_append_printf(gstr,
120                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
121                         if (noreply)
122                                 g_string_append_printf(gstr,
123                                         G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
124
125                         g_string_append_printf(gstr, "\t\t</method>\n");
126                 }
127         }
128
129         for (signal = iface->signals; signal && signal->name; signal++) {
130                 gboolean deprecated = signal->flags &
131                                                 G_DBUS_SIGNAL_FLAG_DEPRECATED;
132
133                 if (!deprecated && !(signal->args && signal->args->name))
134                         g_string_append_printf(gstr,
135                                                 "\t\t<signal name=\"%s\"/>\n",
136                                                 signal->name);
137                 else {
138                         g_string_append_printf(gstr,
139                                                 "\t\t<signal name=\"%s\">\n",
140                                                 signal->name);
141                         print_arguments(gstr, signal->args, NULL);
142
143                         if (deprecated)
144                                 g_string_append_printf(gstr,
145                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
146
147                         g_string_append_printf(gstr, "\t\t</signal>\n");
148                 }
149         }
150 }
151
152 static void generate_introspection_xml(DBusConnection *conn,
153                                 struct generic_data *data, const char *path)
154 {
155         GSList *list;
156         GString *gstr;
157         char **children;
158         int i;
159
160         g_free(data->introspect);
161
162         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
163
164         g_string_append_printf(gstr, "<node>\n");
165
166         for (list = data->interfaces; list; list = list->next) {
167                 struct interface_data *iface = list->data;
168
169                 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
170                                                                 iface->name);
171
172                 generate_interface_xml(gstr, iface);
173
174                 g_string_append_printf(gstr, "\t</interface>\n");
175         }
176
177         if (!dbus_connection_list_registered(conn, path, &children))
178                 goto done;
179
180         for (i = 0; children[i]; i++)
181                 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
182                                                                 children[i]);
183
184         dbus_free_string_array(children);
185
186 done:
187         g_string_append_printf(gstr, "</node>\n");
188
189         data->introspect = g_string_free(gstr, FALSE);
190 }
191
192 static DBusMessage *introspect(DBusConnection *connection,
193                                 DBusMessage *message, void *user_data)
194 {
195         struct generic_data *data = user_data;
196         DBusMessage *reply;
197
198         if (data->introspect == NULL)
199                 generate_introspection_xml(connection, data,
200                                                 dbus_message_get_path(message));
201
202         reply = dbus_message_new_method_return(message);
203         if (reply == NULL)
204                 return NULL;
205
206         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
207                                         DBUS_TYPE_INVALID);
208
209         return reply;
210 }
211
212 static DBusHandlerResult process_message(DBusConnection *connection,
213                         DBusMessage *message, const GDBusMethodTable *method,
214                                                         void *iface_user_data)
215 {
216         DBusMessage *reply;
217
218         reply = method->function(connection, message, iface_user_data);
219
220         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
221                 if (reply != NULL)
222                         dbus_message_unref(reply);
223                 return DBUS_HANDLER_RESULT_HANDLED;
224         }
225
226         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
227                 if (reply == NULL)
228                         return DBUS_HANDLER_RESULT_HANDLED;
229         }
230
231         if (reply == NULL)
232                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
233
234         dbus_connection_send(connection, reply, NULL);
235         dbus_message_unref(reply);
236
237         return DBUS_HANDLER_RESULT_HANDLED;
238 }
239
240 static GDBusPendingReply next_pending = 1;
241 static GSList *pending_security = NULL;
242
243 static const GDBusSecurityTable *security_table = NULL;
244
245 void g_dbus_pending_success(DBusConnection *connection,
246                                         GDBusPendingReply pending)
247 {
248         GSList *list;
249
250         for (list = pending_security; list; list = list->next) {
251                 struct security_data *secdata = list->data;
252
253                 if (secdata->pending != pending)
254                         continue;
255
256                 pending_security = g_slist_remove(pending_security, secdata);
257
258                 process_message(connection, secdata->message,
259                                 secdata->method, secdata->iface_user_data);
260
261                 dbus_message_unref(secdata->message);
262                 g_free(secdata);
263                 return;
264         }
265 }
266
267 void g_dbus_pending_error_valist(DBusConnection *connection,
268                                 GDBusPendingReply pending, const char *name,
269                                         const char *format, va_list args)
270 {
271         GSList *list;
272
273         for (list = pending_security; list; list = list->next) {
274                 struct security_data *secdata = list->data;
275                 DBusMessage *reply;
276
277                 if (secdata->pending != pending)
278                         continue;
279
280                 pending_security = g_slist_remove(pending_security, secdata);
281
282                 reply = g_dbus_create_error_valist(secdata->message,
283                                                         name, format, args);
284                 if (reply != NULL) {
285                         dbus_connection_send(connection, reply, NULL);
286                         dbus_message_unref(reply);
287                 }
288
289                 dbus_message_unref(secdata->message);
290                 g_free(secdata);
291                 return;
292         }
293 }
294
295 void g_dbus_pending_error(DBusConnection *connection,
296                                 GDBusPendingReply pending,
297                                 const char *name, const char *format, ...)
298 {
299         va_list args;
300
301         va_start(args, format);
302
303         g_dbus_pending_error_valist(connection, pending, name, format, args);
304
305         va_end(args);
306 }
307
308 int polkit_check_authorization(DBusConnection *conn,
309                                 const char *action, gboolean interaction,
310                                 void (*function) (dbus_bool_t authorized,
311                                                         void *user_data),
312                                                 void *user_data, int timeout);
313
314 struct builtin_security_data {
315         DBusConnection *conn;
316         GDBusPendingReply pending;
317 };
318
319 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
320 {
321         struct builtin_security_data *data = user_data;
322
323         if (authorized == TRUE)
324                 g_dbus_pending_success(data->conn, data->pending);
325         else
326                 g_dbus_pending_error(data->conn, data->pending,
327                                                 DBUS_ERROR_AUTH_FAILED, NULL);
328
329         g_free(data);
330 }
331
332 static void builtin_security_function(DBusConnection *conn,
333                                                 const char *action,
334                                                 gboolean interaction,
335                                                 GDBusPendingReply pending)
336 {
337         struct builtin_security_data *data;
338
339         data = g_new0(struct builtin_security_data, 1);
340         data->conn = conn;
341         data->pending = pending;
342
343         if (polkit_check_authorization(conn, action, interaction,
344                                 builtin_security_result, data, 30000) < 0)
345                 g_dbus_pending_error(conn, pending, NULL, NULL);
346 }
347
348 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
349                         const GDBusMethodTable *method, void *iface_user_data)
350 {
351         const GDBusSecurityTable *security;
352
353         for (security = security_table; security && security->privilege;
354                                                                 security++) {
355                 struct security_data *secdata;
356                 gboolean interaction;
357
358                 if (security->privilege != method->privilege)
359                         continue;
360
361                 secdata = g_new(struct security_data, 1);
362                 secdata->pending = next_pending++;
363                 secdata->message = dbus_message_ref(msg);
364                 secdata->method = method;
365                 secdata->iface_user_data = iface_user_data;
366
367                 pending_security = g_slist_prepend(pending_security, secdata);
368
369                 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
370                         interaction = TRUE;
371                 else
372                         interaction = FALSE;
373
374                 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
375                                                         security->function)
376                         security->function(conn, security->action,
377                                                 interaction, secdata->pending);
378                 else
379                         builtin_security_function(conn, security->action,
380                                                 interaction, secdata->pending);
381
382                 return TRUE;
383         }
384
385         return FALSE;
386 }
387
388 static GDBusPendingPropertySet next_pending_property = 1;
389 static GSList *pending_property_set;
390
391 static struct property_data *remove_pending_property_data(
392                                                 GDBusPendingPropertySet id)
393 {
394         struct property_data *propdata;
395         GSList *l;
396
397         for (l = pending_property_set; l != NULL; l = l->next) {
398                 propdata = l->data;
399                 if (propdata->id != id)
400                         continue;
401         }
402
403         if (l == NULL)
404                 return NULL;
405
406         pending_property_set = g_slist_delete_link(pending_property_set, l);
407
408         return propdata;
409 }
410
411 void g_dbus_pending_property_success(DBusConnection *connection,
412                                                 GDBusPendingPropertySet id)
413 {
414         struct property_data *propdata;
415
416         propdata = remove_pending_property_data(id);
417         if (propdata == NULL)
418                 return;
419
420         g_dbus_send_reply(connection, propdata->message, DBUS_TYPE_INVALID);
421         dbus_message_unref(propdata->message);
422         g_free(propdata);
423 }
424
425 void g_dbus_pending_property_error_valist(DBusConnection *connection,
426                                         GDBusPendingReply id, const char *name,
427                                         const char *format, va_list args)
428 {
429         struct property_data *propdata;
430         DBusMessage *reply;
431
432         propdata = remove_pending_property_data(id);
433         if (propdata == NULL)
434                 return;
435
436         reply = g_dbus_create_error_valist(propdata->message, name, format,
437                                                                         args);
438         if (reply != NULL) {
439                 dbus_connection_send(connection, reply, NULL);
440                 dbus_message_unref(reply);
441         }
442
443         dbus_message_unref(propdata->message);
444         g_free(propdata);
445 }
446
447 void g_dbus_pending_property_error(DBusConnection *connection,
448                                         GDBusPendingReply id, const char *name,
449                                         const char *format, ...)
450 {
451         va_list args;
452
453         va_start(args, format);
454
455         g_dbus_pending_property_error_valist(connection, id, name, format,
456                                                                         args);
457
458         va_end(args);
459 }
460
461 static void generic_unregister(DBusConnection *connection, void *user_data)
462 {
463         struct generic_data *data = user_data;
464
465         g_free(data->introspect);
466         g_free(data);
467 }
468
469 static struct interface_data *find_interface(GSList *interfaces,
470                                                 const char *name)
471 {
472         GSList *list;
473
474         if (name == NULL)
475                 return NULL;
476
477         for (list = interfaces; list; list = list->next) {
478                 struct interface_data *iface = list->data;
479                 if (!strcmp(name, iface->name))
480                         return iface;
481         }
482
483         return NULL;
484 }
485
486 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
487                                                         DBusMessage *message)
488 {
489         const char *sig = dbus_message_get_signature(message);
490         const char *p = NULL;
491
492         for (; args && args->signature && *sig; args++) {
493                 p = args->signature;
494
495                 for (; *sig && *p; sig++, p++) {
496                         if (*p != *sig)
497                                 return FALSE;
498                 }
499         }
500
501         if (*sig || (p && *p) || (args && args->signature))
502                 return FALSE;
503
504         return TRUE;
505 }
506
507 static DBusHandlerResult generic_message(DBusConnection *connection,
508                                         DBusMessage *message, void *user_data)
509 {
510         struct generic_data *data = user_data;
511         struct interface_data *iface;
512         const GDBusMethodTable *method;
513         const char *interface;
514
515         interface = dbus_message_get_interface(message);
516
517         iface = find_interface(data->interfaces, interface);
518         if (iface == NULL)
519                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
520
521         for (method = iface->methods; method &&
522                         method->name && method->function; method++) {
523                 if (dbus_message_is_method_call(message, iface->name,
524                                                         method->name) == FALSE)
525                         continue;
526
527                 if (g_dbus_args_have_signature(method->in_args,
528                                                         message) == FALSE)
529                         continue;
530
531                 if (check_privilege(connection, message, method,
532                                                 iface->user_data) == TRUE)
533                         return DBUS_HANDLER_RESULT_HANDLED;
534
535                 return process_message(connection, message, method,
536                                                         iface->user_data);
537         }
538
539         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
540 }
541
542 static DBusObjectPathVTable generic_table = {
543         .unregister_function    = generic_unregister,
544         .message_function       = generic_message,
545 };
546
547 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
548 {
549         struct generic_data *data = NULL;
550         char *parent_path, *slash;
551
552         parent_path = g_strdup(child_path);
553         slash = strrchr(parent_path, '/');
554         if (slash == NULL)
555                 goto done;
556
557         if (slash == parent_path && parent_path[1] != '\0')
558                 parent_path[1] = '\0';
559         else
560                 *slash = '\0';
561
562         if (!strlen(parent_path))
563                 goto done;
564
565         if (dbus_connection_get_object_path_data(conn, parent_path,
566                                                         (void *) &data) == FALSE) {
567                 goto done;
568         }
569
570         invalidate_parent_data(conn, parent_path);
571
572         if (data == NULL)
573                 goto done;
574
575         g_free(data->introspect);
576         data->introspect = NULL;
577
578 done:
579         g_free(parent_path);
580 }
581
582 static const GDBusMethodTable introspect_methods[] = {
583         { GDBUS_METHOD("Introspect", NULL,
584                         GDBUS_ARGS({ "xml", "s" }), introspect) },
585         { }
586 };
587
588 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
589                                                         const char *name)
590 {
591         const GDBusPropertyTable *p;
592
593         for (p = properties; p && p->name; p++) {
594                 if (strcmp(name, p->name) == 0)
595                         return p;
596         }
597
598         return NULL;
599 }
600
601 static DBusMessage *properties_get(DBusConnection *connection,
602                                         DBusMessage *message, void *user_data)
603 {
604         struct generic_data *data = user_data;
605         struct interface_data *iface;
606         const GDBusPropertyTable *property;
607         const char *interface, *name;
608         DBusMessageIter iter, value;
609         DBusMessage *reply;
610
611         if (!dbus_message_get_args(message, NULL,
612                                         DBUS_TYPE_STRING, &interface,
613                                         DBUS_TYPE_STRING, &name,
614                                         DBUS_TYPE_INVALID))
615                 return NULL;
616
617         iface = find_interface(data->interfaces, interface);
618         if (iface == NULL)
619                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
620                                 "No such interface '%s'", interface);
621
622         property = find_property(iface->properties, name);
623         if (property == NULL)
624                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
625                                 "No such property '%s'", name);
626
627         if (property->exists != NULL &&
628                         !property->exists(property, iface->user_data))
629                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
630                                         "No such property '%s'", name);
631
632         if (property->get == NULL)
633                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
634                                 "Property '%s' is not readable", name);
635
636         reply = dbus_message_new_method_return(message);
637         if (reply == NULL)
638                 return NULL;
639
640         dbus_message_iter_init_append(reply, &iter);
641         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
642                                                 property->type, &value);
643
644         if (!property->get(property, &value, iface->user_data)) {
645                 dbus_message_unref(reply);
646                 return NULL;
647         }
648
649         dbus_message_iter_close_container(&iter, &value);
650
651         return reply;
652 }
653
654 static DBusMessage *properties_get_all(DBusConnection *connection,
655                                         DBusMessage *message, void *user_data)
656 {
657         struct generic_data *data = user_data;
658         struct interface_data *iface;
659         const GDBusPropertyTable *p;
660         const char *interface;
661         DBusMessageIter iter, dict;
662         DBusMessage *reply;
663
664         if (!dbus_message_get_args(message, NULL,
665                                         DBUS_TYPE_STRING, &interface,
666                                         DBUS_TYPE_INVALID))
667                 return NULL;
668
669         iface = find_interface(data->interfaces, interface);
670         if (iface == NULL)
671                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
672                                         "No such interface '%s'", interface);
673
674         reply = dbus_message_new_method_return(message);
675         if (reply == NULL)
676                 return NULL;
677
678         dbus_message_iter_init_append(reply, &iter);
679         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
680                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
681                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
682                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
683
684         for (p = iface->properties; p && p->name; p++) {
685                 DBusMessageIter entry, value;
686
687                 if (p->get == NULL)
688                         continue;
689
690                 if (p->exists != NULL && !p->exists(p, iface->user_data))
691                         continue;
692
693                 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
694                                                                 NULL, &entry);
695                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
696                                                                 p->name);
697                 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
698                                                         p->type, &value);
699
700                 if (!p->get(p, &value, iface->user_data)) {
701                         dbus_message_unref(reply);
702                         return NULL;
703                 }
704
705                 dbus_message_iter_close_container(&entry, &value);
706                 dbus_message_iter_close_container(&dict, &entry);
707         }
708
709         dbus_message_iter_close_container(&iter, &dict);
710
711         return reply;
712 }
713
714 static DBusMessage *properties_set(DBusConnection *connection,
715                                         DBusMessage *message, void *user_data)
716 {
717         struct generic_data *data = user_data;
718         DBusMessageIter iter, sub;
719         struct interface_data *iface;
720         const GDBusPropertyTable *property;
721         const char *name, *interface;
722         struct property_data *propdata;
723
724         if (!dbus_message_iter_init(message, &iter))
725                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
726                                                         "No arguments given");
727
728         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
729                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
730                                         "Invalid argument type: '%c'",
731                                         dbus_message_iter_get_arg_type(&iter));
732
733         dbus_message_iter_get_basic(&iter, &name);
734         dbus_message_iter_next(&iter);
735
736         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
737                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
738                                         "Invalid argument type: '%c'",
739                                         dbus_message_iter_get_arg_type(&iter));
740
741         dbus_message_iter_get_basic(&iter, &interface);
742         dbus_message_iter_next(&iter);
743
744         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
745                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
746                                         "Invalid argument type: '%c'",
747                                         dbus_message_iter_get_arg_type(&iter));
748
749         dbus_message_iter_recurse(&iter, &sub);
750
751         iface = find_interface(data->interfaces, interface);
752         if (iface == NULL)
753                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
754                                         "No such interface '%s'", interface);
755
756         property = find_property(iface->properties, name);
757         if (property == NULL)
758                 return g_dbus_create_error(message,
759                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
760                                                 "No such property '%s'", name);
761
762         if (property->set == NULL)
763                 return g_dbus_create_error(message,
764                                         DBUS_ERROR_PROPERTY_READ_ONLY,
765                                         "Property '%s' is not writable", name);
766
767         if (property->exists != NULL &&
768                         !property->exists(property, iface->user_data))
769                 return g_dbus_create_error(message,
770                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
771                                                 "No such property '%s'", name);
772
773         propdata = g_new(struct property_data, 1);
774         propdata->id = next_pending_property++;
775         propdata->message = dbus_message_ref(message);
776
777         property->set(property, &sub, propdata->id, iface->user_data);
778
779         return NULL;
780 }
781
782 static const GDBusMethodTable properties_methods[] = {
783         { GDBUS_METHOD("Get",
784                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
785                         GDBUS_ARGS({ "value", "v" }),
786                         properties_get) },
787         { GDBUS_ASYNC_METHOD("Set", NULL,
788                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
789                                                         { "value", "v" }),
790                         properties_set) },
791         { GDBUS_METHOD("GetAll",
792                         GDBUS_ARGS({ "interface", "s" }),
793                         GDBUS_ARGS({ "properties", "a{sv}" }),
794                         properties_get_all) },
795         { }
796 };
797
798 static const GDBusSignalTable properties_signals[] = {
799         { GDBUS_SIGNAL("PropertiesChanged",
800                         GDBUS_ARGS({ "interface", "s" },
801                                         { "changed_properties", "a{sv}" },
802                                         { "invalidated_properties", "as"})) },
803         { }
804 };
805
806 static void add_interface(struct generic_data *data, const char *name,
807                                 const GDBusMethodTable *methods,
808                                 const GDBusSignalTable *signals,
809                                 const GDBusPropertyTable *properties,
810                                 void *user_data,
811                                 GDBusDestroyFunction destroy)
812 {
813         struct interface_data *iface;
814
815         iface = g_new0(struct interface_data, 1);
816         iface->name = g_strdup(name);
817         iface->methods = methods;
818         iface->signals = signals;
819         iface->properties = properties;
820         iface->user_data = user_data;
821         iface->destroy = destroy;
822
823         data->interfaces = g_slist_append(data->interfaces, iface);
824 }
825
826 static struct generic_data *object_path_ref(DBusConnection *connection,
827                                                         const char *path)
828 {
829         struct generic_data *data;
830
831         if (dbus_connection_get_object_path_data(connection, path,
832                                                 (void *) &data) == TRUE) {
833                 if (data != NULL) {
834                         data->refcount++;
835                         return data;
836                 }
837         }
838
839         data = g_new0(struct generic_data, 1);
840         data->refcount = 1;
841
842         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
843
844         if (!dbus_connection_register_object_path(connection, path,
845                                                 &generic_table, data)) {
846                 g_free(data->introspect);
847                 g_free(data);
848                 return NULL;
849         }
850
851         invalidate_parent_data(connection, path);
852
853         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
854                         introspect_methods, NULL, NULL, data, NULL);
855
856         add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
857                                         properties_signals, NULL, data, NULL);
858
859         return data;
860 }
861
862 static gboolean remove_interface(struct generic_data *data, const char *name)
863 {
864         struct interface_data *iface;
865
866         iface = find_interface(data->interfaces, name);
867         if (iface == NULL)
868                 return FALSE;
869
870         data->interfaces = g_slist_remove(data->interfaces, iface);
871
872         if (iface->destroy)
873                 iface->destroy(iface->user_data);
874
875         g_free(iface->name);
876         g_free(iface);
877
878         return TRUE;
879 }
880
881 static void object_path_unref(DBusConnection *connection, const char *path)
882 {
883         struct generic_data *data = NULL;
884
885         if (dbus_connection_get_object_path_data(connection, path,
886                                                 (void *) &data) == FALSE)
887                 return;
888
889         if (data == NULL)
890                 return;
891
892         data->refcount--;
893
894         if (data->refcount > 0)
895                 return;
896
897         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
898         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
899
900         invalidate_parent_data(connection, path);
901
902         dbus_connection_unregister_object_path(connection, path);
903 }
904
905 static gboolean check_signal(DBusConnection *conn, const char *path,
906                                 const char *interface, const char *name,
907                                 const GDBusArgInfo **args)
908 {
909         struct generic_data *data = NULL;
910         struct interface_data *iface;
911         const GDBusSignalTable *signal;
912
913         *args = NULL;
914         if (!dbus_connection_get_object_path_data(conn, path,
915                                         (void *) &data) || data == NULL) {
916                 error("dbus_connection_emit_signal: path %s isn't registered",
917                                 path);
918                 return FALSE;
919         }
920
921         iface = find_interface(data->interfaces, interface);
922         if (iface == NULL) {
923                 error("dbus_connection_emit_signal: %s does not implement %s",
924                                 path, interface);
925                 return FALSE;
926         }
927
928         for (signal = iface->signals; signal && signal->name; signal++) {
929                 if (!strcmp(signal->name, name)) {
930                         *args = signal->args;
931                         return TRUE;
932                 }
933         }
934
935         error("No signal named %s on interface %s", name, interface);
936         return FALSE;
937 }
938
939 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
940                                                 const char *path,
941                                                 const char *interface,
942                                                 const char *name,
943                                                 int first,
944                                                 va_list var_args)
945 {
946         DBusMessage *signal;
947         dbus_bool_t ret;
948         const GDBusArgInfo *args;
949
950         if (!check_signal(conn, path, interface, name, &args))
951                 return FALSE;
952
953         signal = dbus_message_new_signal(path, interface, name);
954         if (signal == NULL) {
955                 error("Unable to allocate new %s.%s signal", interface,  name);
956                 return FALSE;
957         }
958
959         ret = dbus_message_append_args_valist(signal, first, var_args);
960         if (!ret)
961                 goto fail;
962
963         if (g_dbus_args_have_signature(args, signal) == FALSE) {
964                 error("%s.%s: got unexpected signature '%s'", interface, name,
965                                         dbus_message_get_signature(signal));
966                 ret = FALSE;
967                 goto fail;
968         }
969
970         ret = dbus_connection_send(conn, signal, NULL);
971
972 fail:
973         dbus_message_unref(signal);
974
975         return ret;
976 }
977
978 gboolean g_dbus_register_interface(DBusConnection *connection,
979                                         const char *path, const char *name,
980                                         const GDBusMethodTable *methods,
981                                         const GDBusSignalTable *signals,
982                                         const GDBusPropertyTable *properties,
983                                         void *user_data,
984                                         GDBusDestroyFunction destroy)
985 {
986         struct generic_data *data;
987
988         data = object_path_ref(connection, path);
989         if (data == NULL)
990                 return FALSE;
991
992         if (find_interface(data->interfaces, name)) {
993                 object_path_unref(connection, path);
994                 return FALSE;
995         }
996
997         add_interface(data, name, methods, signals,
998                         properties, user_data, destroy);
999
1000         g_free(data->introspect);
1001         data->introspect = NULL;
1002
1003         return TRUE;
1004 }
1005
1006 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1007                                         const char *path, const char *name)
1008 {
1009         struct generic_data *data = NULL;
1010
1011         if (path == NULL)
1012                 return FALSE;
1013
1014         if (dbus_connection_get_object_path_data(connection, path,
1015                                                 (void *) &data) == FALSE)
1016                 return FALSE;
1017
1018         if (data == NULL)
1019                 return FALSE;
1020
1021         if (remove_interface(data, name) == FALSE)
1022                 return FALSE;
1023
1024         g_free(data->introspect);
1025         data->introspect = NULL;
1026
1027         object_path_unref(connection, path);
1028
1029         return TRUE;
1030 }
1031
1032 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1033 {
1034         if (security_table != NULL)
1035                 return FALSE;
1036
1037         security_table = security;
1038
1039         return TRUE;
1040 }
1041
1042 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1043 {
1044         security_table = NULL;
1045
1046         return TRUE;
1047 }
1048
1049 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1050                                         const char *format, va_list args)
1051 {
1052         char str[1024];
1053
1054         vsnprintf(str, sizeof(str), format, args);
1055
1056         return dbus_message_new_error(message, name, str);
1057 }
1058
1059 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1060                                                 const char *format, ...)
1061 {
1062         va_list args;
1063         DBusMessage *reply;
1064
1065         va_start(args, format);
1066
1067         reply = g_dbus_create_error_valist(message, name, format, args);
1068
1069         va_end(args);
1070
1071         return reply;
1072 }
1073
1074 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1075                                                 int type, va_list args)
1076 {
1077         DBusMessage *reply;
1078
1079         reply = dbus_message_new_method_return(message);
1080         if (reply == NULL)
1081                 return NULL;
1082
1083         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1084                 dbus_message_unref(reply);
1085                 return NULL;
1086         }
1087
1088         return reply;
1089 }
1090
1091 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1092 {
1093         va_list args;
1094         DBusMessage *reply;
1095
1096         va_start(args, type);
1097
1098         reply = g_dbus_create_reply_valist(message, type, args);
1099
1100         va_end(args);
1101
1102         return reply;
1103 }
1104
1105 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1106 {
1107         dbus_bool_t result;
1108
1109         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1110                 dbus_message_set_no_reply(message, TRUE);
1111
1112         result = dbus_connection_send(connection, message, NULL);
1113
1114         dbus_message_unref(message);
1115
1116         return result;
1117 }
1118
1119 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1120                                 DBusMessage *message, int type, va_list args)
1121 {
1122         DBusMessage *reply;
1123
1124         reply = dbus_message_new_method_return(message);
1125         if (reply == NULL)
1126                 return FALSE;
1127
1128         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1129                 dbus_message_unref(reply);
1130                 return FALSE;
1131         }
1132
1133         return g_dbus_send_message(connection, reply);
1134 }
1135
1136 gboolean g_dbus_send_reply(DBusConnection *connection,
1137                                 DBusMessage *message, int type, ...)
1138 {
1139         va_list args;
1140         gboolean result;
1141
1142         va_start(args, type);
1143
1144         result = g_dbus_send_reply_valist(connection, message, type, args);
1145
1146         va_end(args);
1147
1148         return result;
1149 }
1150
1151 gboolean g_dbus_emit_signal(DBusConnection *connection,
1152                                 const char *path, const char *interface,
1153                                 const char *name, int type, ...)
1154 {
1155         va_list args;
1156         gboolean result;
1157
1158         va_start(args, type);
1159
1160         result = emit_signal_valist(connection, path, interface,
1161                                                         name, type, args);
1162
1163         va_end(args);
1164
1165         return result;
1166 }
1167
1168 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1169                                 const char *path, const char *interface,
1170                                 const char *name, int type, va_list args)
1171 {
1172         return emit_signal_valist(connection, path, interface,
1173                                                         name, type, args);
1174 }