gdbus: Introduce G_DBUS_SIGNAL_FLAG_EXPERIMENTAL
[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 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
41
42 #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
43 #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
44 #endif
45
46 #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
47 #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
48 #endif
49
50 struct generic_data {
51         unsigned int refcount;
52         DBusConnection *conn;
53         char *path;
54         GSList *interfaces;
55         GSList *objects;
56         GSList *added;
57         GSList *removed;
58         guint process_id;
59         gboolean pending_prop;
60         char *introspect;
61         struct generic_data *parent;
62 };
63
64 struct interface_data {
65         char *name;
66         const GDBusMethodTable *methods;
67         const GDBusSignalTable *signals;
68         const GDBusPropertyTable *properties;
69         GSList *pending_prop;
70         void *user_data;
71         GDBusDestroyFunction destroy;
72 };
73
74 struct security_data {
75         GDBusPendingReply pending;
76         DBusMessage *message;
77         const GDBusMethodTable *method;
78         void *iface_user_data;
79 };
80
81 struct property_data {
82         DBusConnection *conn;
83         GDBusPendingPropertySet id;
84         DBusMessage *message;
85 };
86
87 static int global_flags = 0;
88 static struct generic_data *root;
89
90 static gboolean process_changes(gpointer user_data);
91 static void process_properties_from_interface(struct generic_data *data,
92                                                 struct interface_data *iface);
93 static void process_property_changes(struct generic_data *data);
94
95 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
96                                                 const char *direction)
97 {
98         for (; args && args->name; args++) {
99                 g_string_append_printf(gstr,
100                                         "\t\t\t<arg name=\"%s\" type=\"%s\"",
101                                         args->name, args->signature);
102
103                 if (direction)
104                         g_string_append_printf(gstr,
105                                         " direction=\"%s\"/>\n", direction);
106                 else
107                         g_string_append_printf(gstr, "/>\n");
108
109         }
110 }
111
112 #define G_DBUS_ANNOTATE(prefix_, name_, value_)                         \
113         prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" "  \
114         "value=\"" value_ "\"/>\n"
115
116 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
117         G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
118
119 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
120         G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
121
122 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
123 {
124         const GDBusMethodTable *method;
125         const GDBusSignalTable *signal;
126         const GDBusPropertyTable *property;
127
128         for (method = iface->methods; method && method->name; method++) {
129                 gboolean deprecated = method->flags &
130                                                 G_DBUS_METHOD_FLAG_DEPRECATED;
131                 gboolean noreply = method->flags &
132                                                 G_DBUS_METHOD_FLAG_NOREPLY;
133                 gboolean experimental = method->flags &
134                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL;
135
136                 if (!(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) &&
137                                                         experimental)
138                         continue;
139
140                 if (!deprecated && !noreply &&
141                                 !(method->in_args && method->in_args->name) &&
142                                 !(method->out_args && method->out_args->name))
143                         g_string_append_printf(gstr,
144                                                 "\t\t<method name=\"%s\"/>\n",
145                                                 method->name);
146                 else {
147                         g_string_append_printf(gstr,
148                                                 "\t\t<method name=\"%s\">\n",
149                                                 method->name);
150                         print_arguments(gstr, method->in_args, "in");
151                         print_arguments(gstr, method->out_args, "out");
152
153                         if (deprecated)
154                                 g_string_append_printf(gstr,
155                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
156                         if (noreply)
157                                 g_string_append_printf(gstr,
158                                         G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
159
160                         g_string_append_printf(gstr, "\t\t</method>\n");
161                 }
162         }
163
164         for (signal = iface->signals; signal && signal->name; signal++) {
165                 gboolean deprecated = signal->flags &
166                                                 G_DBUS_SIGNAL_FLAG_DEPRECATED;
167                 gboolean experimental = signal->flags &
168                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL;
169
170                 if (!(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) &&
171                                                         experimental)
172                         continue;
173
174                 if (!deprecated && !(signal->args && signal->args->name))
175                         g_string_append_printf(gstr,
176                                                 "\t\t<signal name=\"%s\"/>\n",
177                                                 signal->name);
178                 else {
179                         g_string_append_printf(gstr,
180                                                 "\t\t<signal name=\"%s\">\n",
181                                                 signal->name);
182                         print_arguments(gstr, signal->args, NULL);
183
184                         if (deprecated)
185                                 g_string_append_printf(gstr,
186                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
187
188                         g_string_append_printf(gstr, "\t\t</signal>\n");
189                 }
190         }
191
192         for (property = iface->properties; property && property->name;
193                                                                 property++) {
194                 gboolean deprecated = property->flags &
195                                         G_DBUS_PROPERTY_FLAG_DEPRECATED;
196
197                 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
198                                         " type=\"%s\" access=\"%s%s\"",
199                                         property->name, property->type,
200                                         property->get ? "read" : "",
201                                         property->set ? "write" : "");
202
203                 if (!deprecated)
204                         g_string_append_printf(gstr, "/>\n");
205                 else
206                         g_string_append_printf(gstr,
207                                 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
208         }
209 }
210
211 static void generate_introspection_xml(DBusConnection *conn,
212                                 struct generic_data *data, const char *path)
213 {
214         GSList *list;
215         GString *gstr;
216         char **children;
217         int i;
218
219         g_free(data->introspect);
220
221         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
222
223         g_string_append_printf(gstr, "<node>\n");
224
225         for (list = data->interfaces; list; list = list->next) {
226                 struct interface_data *iface = list->data;
227
228                 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
229                                                                 iface->name);
230
231                 generate_interface_xml(gstr, iface);
232
233                 g_string_append_printf(gstr, "\t</interface>\n");
234         }
235
236         if (!dbus_connection_list_registered(conn, path, &children))
237                 goto done;
238
239         for (i = 0; children[i]; i++)
240                 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
241                                                                 children[i]);
242
243         dbus_free_string_array(children);
244
245 done:
246         g_string_append_printf(gstr, "</node>\n");
247
248         data->introspect = g_string_free(gstr, FALSE);
249 }
250
251 static DBusMessage *introspect(DBusConnection *connection,
252                                 DBusMessage *message, void *user_data)
253 {
254         struct generic_data *data = user_data;
255         DBusMessage *reply;
256
257         if (data->introspect == NULL)
258                 generate_introspection_xml(connection, data,
259                                                 dbus_message_get_path(message));
260
261         reply = dbus_message_new_method_return(message);
262         if (reply == NULL)
263                 return NULL;
264
265         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
266                                         DBUS_TYPE_INVALID);
267
268         return reply;
269 }
270
271 static DBusHandlerResult process_message(DBusConnection *connection,
272                         DBusMessage *message, const GDBusMethodTable *method,
273                                                         void *iface_user_data)
274 {
275         DBusMessage *reply;
276
277         reply = method->function(connection, message, iface_user_data);
278
279         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
280                 if (reply != NULL)
281                         dbus_message_unref(reply);
282                 return DBUS_HANDLER_RESULT_HANDLED;
283         }
284
285         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
286                 if (reply == NULL)
287                         return DBUS_HANDLER_RESULT_HANDLED;
288         }
289
290         if (reply == NULL)
291                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
292
293         dbus_connection_send(connection, reply, NULL);
294         dbus_message_unref(reply);
295
296         return DBUS_HANDLER_RESULT_HANDLED;
297 }
298
299 static GDBusPendingReply next_pending = 1;
300 static GSList *pending_security = NULL;
301
302 static const GDBusSecurityTable *security_table = NULL;
303
304 void g_dbus_pending_success(DBusConnection *connection,
305                                         GDBusPendingReply pending)
306 {
307         GSList *list;
308
309         for (list = pending_security; list; list = list->next) {
310                 struct security_data *secdata = list->data;
311
312                 if (secdata->pending != pending)
313                         continue;
314
315                 pending_security = g_slist_remove(pending_security, secdata);
316
317                 process_message(connection, secdata->message,
318                                 secdata->method, secdata->iface_user_data);
319
320                 dbus_message_unref(secdata->message);
321                 g_free(secdata);
322                 return;
323         }
324 }
325
326 void g_dbus_pending_error_valist(DBusConnection *connection,
327                                 GDBusPendingReply pending, const char *name,
328                                         const char *format, va_list args)
329 {
330         GSList *list;
331
332         for (list = pending_security; list; list = list->next) {
333                 struct security_data *secdata = list->data;
334                 DBusMessage *reply;
335
336                 if (secdata->pending != pending)
337                         continue;
338
339                 pending_security = g_slist_remove(pending_security, secdata);
340
341                 reply = g_dbus_create_error_valist(secdata->message,
342                                                         name, format, args);
343                 if (reply != NULL) {
344                         dbus_connection_send(connection, reply, NULL);
345                         dbus_message_unref(reply);
346                 }
347
348                 dbus_message_unref(secdata->message);
349                 g_free(secdata);
350                 return;
351         }
352 }
353
354 void g_dbus_pending_error(DBusConnection *connection,
355                                 GDBusPendingReply pending,
356                                 const char *name, const char *format, ...)
357 {
358         va_list args;
359
360         va_start(args, format);
361
362         g_dbus_pending_error_valist(connection, pending, name, format, args);
363
364         va_end(args);
365 }
366
367 int polkit_check_authorization(DBusConnection *conn,
368                                 const char *action, gboolean interaction,
369                                 void (*function) (dbus_bool_t authorized,
370                                                         void *user_data),
371                                                 void *user_data, int timeout);
372
373 struct builtin_security_data {
374         DBusConnection *conn;
375         GDBusPendingReply pending;
376 };
377
378 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
379 {
380         struct builtin_security_data *data = user_data;
381
382         if (authorized == TRUE)
383                 g_dbus_pending_success(data->conn, data->pending);
384         else
385                 g_dbus_pending_error(data->conn, data->pending,
386                                                 DBUS_ERROR_AUTH_FAILED, NULL);
387
388         g_free(data);
389 }
390
391 static void builtin_security_function(DBusConnection *conn,
392                                                 const char *action,
393                                                 gboolean interaction,
394                                                 GDBusPendingReply pending)
395 {
396         struct builtin_security_data *data;
397
398         data = g_new0(struct builtin_security_data, 1);
399         data->conn = conn;
400         data->pending = pending;
401
402         if (polkit_check_authorization(conn, action, interaction,
403                                 builtin_security_result, data, 30000) < 0)
404                 g_dbus_pending_error(conn, pending, NULL, NULL);
405 }
406
407 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
408                         const GDBusMethodTable *method, void *iface_user_data)
409 {
410         const GDBusSecurityTable *security;
411
412         for (security = security_table; security && security->privilege;
413                                                                 security++) {
414                 struct security_data *secdata;
415                 gboolean interaction;
416
417                 if (security->privilege != method->privilege)
418                         continue;
419
420                 secdata = g_new(struct security_data, 1);
421                 secdata->pending = next_pending++;
422                 secdata->message = dbus_message_ref(msg);
423                 secdata->method = method;
424                 secdata->iface_user_data = iface_user_data;
425
426                 pending_security = g_slist_prepend(pending_security, secdata);
427
428                 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
429                         interaction = TRUE;
430                 else
431                         interaction = FALSE;
432
433                 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
434                                                         security->function)
435                         security->function(conn, security->action,
436                                                 interaction, secdata->pending);
437                 else
438                         builtin_security_function(conn, security->action,
439                                                 interaction, secdata->pending);
440
441                 return TRUE;
442         }
443
444         return FALSE;
445 }
446
447 static GDBusPendingPropertySet next_pending_property = 1;
448 static GSList *pending_property_set;
449
450 static struct property_data *remove_pending_property_data(
451                                                 GDBusPendingPropertySet id)
452 {
453         struct property_data *propdata;
454         GSList *l;
455
456         for (l = pending_property_set; l != NULL; l = l->next) {
457                 propdata = l->data;
458                 if (propdata->id != id)
459                         continue;
460
461                 break;
462         }
463
464         if (l == NULL)
465                 return NULL;
466
467         pending_property_set = g_slist_delete_link(pending_property_set, l);
468
469         return propdata;
470 }
471
472 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
473 {
474         struct property_data *propdata;
475
476         propdata = remove_pending_property_data(id);
477         if (propdata == NULL)
478                 return;
479
480         g_dbus_send_reply(propdata->conn, propdata->message,
481                                                         DBUS_TYPE_INVALID);
482         dbus_message_unref(propdata->message);
483         g_free(propdata);
484 }
485
486 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
487                                         const char *name, const char *format,
488                                         va_list args)
489 {
490         struct property_data *propdata;
491         DBusMessage *reply;
492
493         propdata = remove_pending_property_data(id);
494         if (propdata == NULL)
495                 return;
496
497         reply = g_dbus_create_error_valist(propdata->message, name, format,
498                                                                         args);
499         if (reply != NULL) {
500                 dbus_connection_send(propdata->conn, reply, NULL);
501                 dbus_message_unref(reply);
502         }
503
504         dbus_message_unref(propdata->message);
505         g_free(propdata);
506 }
507
508 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
509                                                 const char *format, ...)
510 {
511         va_list args;
512
513         va_start(args, format);
514
515         g_dbus_pending_property_error_valist(id, name, format, args);
516
517         va_end(args);
518 }
519
520 static void reset_parent(gpointer data, gpointer user_data)
521 {
522         struct generic_data *child = data;
523         struct generic_data *parent = user_data;
524
525         child->parent = parent;
526 }
527
528 static void append_property(struct interface_data *iface,
529                         const GDBusPropertyTable *p, DBusMessageIter *dict)
530 {
531         DBusMessageIter entry, value;
532
533         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
534                                                                 &entry);
535         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
536         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
537                                                                 &value);
538
539         p->get(p, &value, iface->user_data);
540
541         dbus_message_iter_close_container(&entry, &value);
542         dbus_message_iter_close_container(dict, &entry);
543 }
544
545 static void append_properties(struct interface_data *data,
546                                                         DBusMessageIter *iter)
547 {
548         DBusMessageIter dict;
549         const GDBusPropertyTable *p;
550
551         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
552                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
553                                 DBUS_TYPE_STRING_AS_STRING
554                                 DBUS_TYPE_VARIANT_AS_STRING
555                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
556
557         for (p = data->properties; p && p->name; p++) {
558                 if (p->get == NULL)
559                         continue;
560
561                 if (p->exists != NULL && !p->exists(p, data->user_data))
562                         continue;
563
564                 append_property(data, p, &dict);
565         }
566
567         dbus_message_iter_close_container(iter, &dict);
568 }
569
570 static void append_interface(gpointer data, gpointer user_data)
571 {
572         struct interface_data *iface = data;
573         DBusMessageIter *array = user_data;
574         DBusMessageIter entry;
575
576         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
577                                                                 &entry);
578         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
579         append_properties(data, &entry);
580         dbus_message_iter_close_container(array, &entry);
581 }
582
583 static void emit_interfaces_added(struct generic_data *data)
584 {
585         DBusMessage *signal;
586         DBusMessageIter iter, array;
587
588         if (root == NULL || data == root)
589                 return;
590
591         signal = dbus_message_new_signal(root->path,
592                                         DBUS_INTERFACE_OBJECT_MANAGER,
593                                         "InterfacesAdded");
594         if (signal == NULL)
595                 return;
596
597         dbus_message_iter_init_append(signal, &iter);
598         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
599                                                                 &data->path);
600
601         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
602                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
603                                 DBUS_TYPE_STRING_AS_STRING
604                                 DBUS_TYPE_ARRAY_AS_STRING
605                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
606                                 DBUS_TYPE_STRING_AS_STRING
607                                 DBUS_TYPE_VARIANT_AS_STRING
608                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
609                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
610
611         g_slist_foreach(data->added, append_interface, &array);
612         g_slist_free(data->added);
613         data->added = NULL;
614
615         dbus_message_iter_close_container(&iter, &array);
616
617         g_dbus_send_message(data->conn, signal);
618 }
619
620 static struct interface_data *find_interface(GSList *interfaces,
621                                                 const char *name)
622 {
623         GSList *list;
624
625         if (name == NULL)
626                 return NULL;
627
628         for (list = interfaces; list; list = list->next) {
629                 struct interface_data *iface = list->data;
630                 if (!strcmp(name, iface->name))
631                         return iface;
632         }
633
634         return NULL;
635 }
636
637 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
638                                                         DBusMessage *message)
639 {
640         const char *sig = dbus_message_get_signature(message);
641         const char *p = NULL;
642
643         for (; args && args->signature && *sig; args++) {
644                 p = args->signature;
645
646                 for (; *sig && *p; sig++, p++) {
647                         if (*p != *sig)
648                                 return FALSE;
649                 }
650         }
651
652         if (*sig || (p && *p) || (args && args->signature))
653                 return FALSE;
654
655         return TRUE;
656 }
657
658 static gboolean remove_interface(struct generic_data *data, const char *name)
659 {
660         struct interface_data *iface;
661
662         iface = find_interface(data->interfaces, name);
663         if (iface == NULL)
664                 return FALSE;
665
666         process_properties_from_interface(data, iface);
667
668         data->interfaces = g_slist_remove(data->interfaces, iface);
669
670         if (iface->destroy) {
671                 iface->destroy(iface->user_data);
672                 iface->user_data = NULL;
673         }
674
675         /*
676          * Interface being removed was just added, on the same mainloop
677          * iteration? Don't send any signal
678          */
679         if (g_slist_find(data->added, iface)) {
680                 data->added = g_slist_remove(data->added, iface);
681                 g_free(iface->name);
682                 g_free(iface);
683                 return TRUE;
684         }
685
686         if (data->parent == NULL) {
687                 g_free(iface->name);
688                 g_free(iface);
689                 return TRUE;
690         }
691
692         data->removed = g_slist_prepend(data->removed, iface->name);
693         g_free(iface);
694
695         if (data->process_id > 0)
696                 return TRUE;
697
698         data->process_id = g_idle_add(process_changes, data);
699
700         return TRUE;
701 }
702
703 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
704                                                 const char *child_path)
705 {
706         struct generic_data *data = NULL, *child = NULL, *parent = NULL;
707         char *parent_path, *slash;
708
709         parent_path = g_strdup(child_path);
710         slash = strrchr(parent_path, '/');
711         if (slash == NULL)
712                 goto done;
713
714         if (slash == parent_path && parent_path[1] != '\0')
715                 parent_path[1] = '\0';
716         else
717                 *slash = '\0';
718
719         if (!strlen(parent_path))
720                 goto done;
721
722         if (dbus_connection_get_object_path_data(conn, parent_path,
723                                                         (void *) &data) == FALSE) {
724                 goto done;
725         }
726
727         parent = invalidate_parent_data(conn, parent_path);
728
729         if (data == NULL) {
730                 data = parent;
731                 if (data == NULL)
732                         goto done;
733         }
734
735         g_free(data->introspect);
736         data->introspect = NULL;
737
738         if (!dbus_connection_get_object_path_data(conn, child_path,
739                                                         (void *) &child))
740                 goto done;
741
742         if (child == NULL || g_slist_find(data->objects, child) != NULL)
743                 goto done;
744
745         data->objects = g_slist_prepend(data->objects, child);
746         child->parent = data;
747
748 done:
749         g_free(parent_path);
750         return data;
751 }
752
753 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
754                                                         const char *name)
755 {
756         const GDBusPropertyTable *p;
757
758         for (p = properties; p && p->name; p++) {
759                 if (strcmp(name, p->name) == 0)
760                         return p;
761         }
762
763         return NULL;
764 }
765
766 static DBusMessage *properties_get(DBusConnection *connection,
767                                         DBusMessage *message, void *user_data)
768 {
769         struct generic_data *data = user_data;
770         struct interface_data *iface;
771         const GDBusPropertyTable *property;
772         const char *interface, *name;
773         DBusMessageIter iter, value;
774         DBusMessage *reply;
775
776         if (!dbus_message_get_args(message, NULL,
777                                         DBUS_TYPE_STRING, &interface,
778                                         DBUS_TYPE_STRING, &name,
779                                         DBUS_TYPE_INVALID))
780                 return NULL;
781
782         iface = find_interface(data->interfaces, interface);
783         if (iface == NULL)
784                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
785                                 "No such interface '%s'", interface);
786
787         property = find_property(iface->properties, name);
788         if (property == NULL)
789                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
790                                 "No such property '%s'", name);
791
792         if (property->exists != NULL &&
793                         !property->exists(property, iface->user_data))
794                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
795                                         "No such property '%s'", name);
796
797         if (property->get == NULL)
798                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
799                                 "Property '%s' is not readable", name);
800
801         reply = dbus_message_new_method_return(message);
802         if (reply == NULL)
803                 return NULL;
804
805         dbus_message_iter_init_append(reply, &iter);
806         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
807                                                 property->type, &value);
808
809         if (!property->get(property, &value, iface->user_data)) {
810                 dbus_message_unref(reply);
811                 return NULL;
812         }
813
814         dbus_message_iter_close_container(&iter, &value);
815
816         return reply;
817 }
818
819 static DBusMessage *properties_get_all(DBusConnection *connection,
820                                         DBusMessage *message, void *user_data)
821 {
822         struct generic_data *data = user_data;
823         struct interface_data *iface;
824         const char *interface;
825         DBusMessageIter iter;
826         DBusMessage *reply;
827
828         if (!dbus_message_get_args(message, NULL,
829                                         DBUS_TYPE_STRING, &interface,
830                                         DBUS_TYPE_INVALID))
831                 return NULL;
832
833         iface = find_interface(data->interfaces, interface);
834         if (iface == NULL)
835                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
836                                         "No such interface '%s'", interface);
837
838         reply = dbus_message_new_method_return(message);
839         if (reply == NULL)
840                 return NULL;
841
842         dbus_message_iter_init_append(reply, &iter);
843
844         append_properties(iface, &iter);
845
846         return reply;
847 }
848
849 static DBusMessage *properties_set(DBusConnection *connection,
850                                         DBusMessage *message, void *user_data)
851 {
852         struct generic_data *data = user_data;
853         DBusMessageIter iter, sub;
854         struct interface_data *iface;
855         const GDBusPropertyTable *property;
856         const char *name, *interface;
857         struct property_data *propdata;
858
859         if (!dbus_message_iter_init(message, &iter))
860                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
861                                                         "No arguments given");
862
863         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
864                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
865                                         "Invalid argument type: '%c'",
866                                         dbus_message_iter_get_arg_type(&iter));
867
868         dbus_message_iter_get_basic(&iter, &interface);
869         dbus_message_iter_next(&iter);
870
871         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
872                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
873                                         "Invalid argument type: '%c'",
874                                         dbus_message_iter_get_arg_type(&iter));
875
876         dbus_message_iter_get_basic(&iter, &name);
877         dbus_message_iter_next(&iter);
878
879         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
880                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
881                                         "Invalid argument type: '%c'",
882                                         dbus_message_iter_get_arg_type(&iter));
883
884         dbus_message_iter_recurse(&iter, &sub);
885
886         iface = find_interface(data->interfaces, interface);
887         if (iface == NULL)
888                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
889                                         "No such interface '%s'", interface);
890
891         property = find_property(iface->properties, name);
892         if (property == NULL)
893                 return g_dbus_create_error(message,
894                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
895                                                 "No such property '%s'", name);
896
897         if (property->set == NULL)
898                 return g_dbus_create_error(message,
899                                         DBUS_ERROR_PROPERTY_READ_ONLY,
900                                         "Property '%s' is not writable", name);
901
902         if (property->exists != NULL &&
903                         !property->exists(property, iface->user_data))
904                 return g_dbus_create_error(message,
905                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
906                                                 "No such property '%s'", name);
907
908         propdata = g_new(struct property_data, 1);
909         propdata->id = next_pending_property++;
910         propdata->message = dbus_message_ref(message);
911         propdata->conn = connection;
912         pending_property_set = g_slist_prepend(pending_property_set, propdata);
913
914         property->set(property, &sub, propdata->id, iface->user_data);
915
916         return NULL;
917 }
918
919 static const GDBusMethodTable properties_methods[] = {
920         { GDBUS_METHOD("Get",
921                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
922                         GDBUS_ARGS({ "value", "v" }),
923                         properties_get) },
924         { GDBUS_ASYNC_METHOD("Set",
925                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
926                                                         { "value", "v" }),
927                         NULL,
928                         properties_set) },
929         { GDBUS_METHOD("GetAll",
930                         GDBUS_ARGS({ "interface", "s" }),
931                         GDBUS_ARGS({ "properties", "a{sv}" }),
932                         properties_get_all) },
933         { }
934 };
935
936 static const GDBusSignalTable properties_signals[] = {
937         { GDBUS_SIGNAL("PropertiesChanged",
938                         GDBUS_ARGS({ "interface", "s" },
939                                         { "changed_properties", "a{sv}" },
940                                         { "invalidated_properties", "as"})) },
941         { }
942 };
943
944 static void append_name(gpointer data, gpointer user_data)
945 {
946         char *name = data;
947         DBusMessageIter *iter = user_data;
948
949         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
950 }
951
952 static void emit_interfaces_removed(struct generic_data *data)
953 {
954         DBusMessage *signal;
955         DBusMessageIter iter, array;
956
957         if (root == NULL || data == root)
958                 return;
959
960         signal = dbus_message_new_signal(root->path,
961                                         DBUS_INTERFACE_OBJECT_MANAGER,
962                                         "InterfacesRemoved");
963         if (signal == NULL)
964                 return;
965
966         dbus_message_iter_init_append(signal, &iter);
967         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
968                                                                 &data->path);
969         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
970                                         DBUS_TYPE_STRING_AS_STRING, &array);
971
972         g_slist_foreach(data->removed, append_name, &array);
973         g_slist_free_full(data->removed, g_free);
974         data->removed = NULL;
975
976         dbus_message_iter_close_container(&iter, &array);
977
978         g_dbus_send_message(data->conn, signal);
979 }
980
981 static gboolean process_changes(gpointer user_data)
982 {
983         struct generic_data *data = user_data;
984
985         data->process_id = 0;
986
987         if (data->added != NULL)
988                 emit_interfaces_added(data);
989
990         /* Flush pending properties */
991         if (data->pending_prop == TRUE)
992                 process_property_changes(data);
993
994         if (data->removed != NULL)
995                 emit_interfaces_removed(data);
996
997         return FALSE;
998 }
999
1000 static void generic_unregister(DBusConnection *connection, void *user_data)
1001 {
1002         struct generic_data *data = user_data;
1003         struct generic_data *parent = data->parent;
1004
1005         if (parent != NULL)
1006                 parent->objects = g_slist_remove(parent->objects, data);
1007
1008         if (data->process_id > 0) {
1009                 g_source_remove(data->process_id);
1010                 process_changes(data);
1011         }
1012
1013         g_slist_foreach(data->objects, reset_parent, data->parent);
1014         g_slist_free(data->objects);
1015
1016         dbus_connection_unref(data->conn);
1017         g_free(data->introspect);
1018         g_free(data->path);
1019         g_free(data);
1020 }
1021
1022 static DBusHandlerResult generic_message(DBusConnection *connection,
1023                                         DBusMessage *message, void *user_data)
1024 {
1025         struct generic_data *data = user_data;
1026         struct interface_data *iface;
1027         const GDBusMethodTable *method;
1028         const char *interface;
1029
1030         interface = dbus_message_get_interface(message);
1031
1032         iface = find_interface(data->interfaces, interface);
1033         if (iface == NULL)
1034                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1035
1036         for (method = iface->methods; method &&
1037                         method->name && method->function; method++) {
1038                 gboolean experimental = method->flags &
1039                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL;
1040
1041                 if (dbus_message_is_method_call(message, iface->name,
1042                                                         method->name) == FALSE)
1043                         continue;
1044
1045                 if (experimental) {
1046                         const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1047                         if (g_strcmp0(env, "1") != 0)
1048                                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1049                 }
1050
1051                 if (g_dbus_args_have_signature(method->in_args,
1052                                                         message) == FALSE)
1053                         continue;
1054
1055                 if (check_privilege(connection, message, method,
1056                                                 iface->user_data) == TRUE)
1057                         return DBUS_HANDLER_RESULT_HANDLED;
1058
1059                 return process_message(connection, message, method,
1060                                                         iface->user_data);
1061         }
1062
1063         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1064 }
1065
1066 static DBusObjectPathVTable generic_table = {
1067         .unregister_function    = generic_unregister,
1068         .message_function       = generic_message,
1069 };
1070
1071 static const GDBusMethodTable introspect_methods[] = {
1072         { GDBUS_METHOD("Introspect", NULL,
1073                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1074         { }
1075 };
1076
1077 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1078 {
1079         DBusMessageIter array;
1080
1081         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1082                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1083                                 DBUS_TYPE_STRING_AS_STRING
1084                                 DBUS_TYPE_ARRAY_AS_STRING
1085                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1086                                 DBUS_TYPE_STRING_AS_STRING
1087                                 DBUS_TYPE_VARIANT_AS_STRING
1088                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1089                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1090
1091         g_slist_foreach(data->interfaces, append_interface, &array);
1092
1093         dbus_message_iter_close_container(iter, &array);
1094 }
1095
1096 static void append_object(gpointer data, gpointer user_data)
1097 {
1098         struct generic_data *child = data;
1099         DBusMessageIter *array = user_data;
1100         DBusMessageIter entry;
1101
1102         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1103                                                                 &entry);
1104         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1105                                                                 &child->path);
1106         append_interfaces(child, &entry);
1107         dbus_message_iter_close_container(array, &entry);
1108
1109         g_slist_foreach(child->objects, append_object, user_data);
1110 }
1111
1112 static DBusMessage *get_objects(DBusConnection *connection,
1113                                 DBusMessage *message, void *user_data)
1114 {
1115         struct generic_data *data = user_data;
1116         DBusMessage *reply;
1117         DBusMessageIter iter;
1118         DBusMessageIter array;
1119
1120         reply = dbus_message_new_method_return(message);
1121         if (reply == NULL)
1122                 return NULL;
1123
1124         dbus_message_iter_init_append(reply, &iter);
1125
1126         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1127                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1128                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1129                                         DBUS_TYPE_ARRAY_AS_STRING
1130                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1131                                         DBUS_TYPE_STRING_AS_STRING
1132                                         DBUS_TYPE_ARRAY_AS_STRING
1133                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1134                                         DBUS_TYPE_STRING_AS_STRING
1135                                         DBUS_TYPE_VARIANT_AS_STRING
1136                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1137                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1138                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1139                                         &array);
1140
1141         g_slist_foreach(data->objects, append_object, &array);
1142
1143         dbus_message_iter_close_container(&iter, &array);
1144
1145         return reply;
1146 }
1147
1148 static const GDBusMethodTable manager_methods[] = {
1149         { GDBUS_METHOD("GetManagedObjects", NULL,
1150                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1151         { }
1152 };
1153
1154 static const GDBusSignalTable manager_signals[] = {
1155         { GDBUS_SIGNAL("InterfacesAdded",
1156                 GDBUS_ARGS({ "object", "o" },
1157                                 { "interfaces", "a{sa{sv}}" })) },
1158         { GDBUS_SIGNAL("InterfacesRemoved",
1159                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1160         { }
1161 };
1162
1163 static void add_interface(struct generic_data *data,
1164                                 const char *name,
1165                                 const GDBusMethodTable *methods,
1166                                 const GDBusSignalTable *signals,
1167                                 const GDBusPropertyTable *properties,
1168                                 void *user_data,
1169                                 GDBusDestroyFunction destroy)
1170 {
1171         struct interface_data *iface;
1172
1173         iface = g_new0(struct interface_data, 1);
1174         iface->name = g_strdup(name);
1175         iface->methods = methods;
1176         iface->signals = signals;
1177         iface->properties = properties;
1178         iface->user_data = user_data;
1179         iface->destroy = destroy;
1180
1181         data->interfaces = g_slist_append(data->interfaces, iface);
1182         if (data->parent == NULL)
1183                 return;
1184
1185         data->added = g_slist_append(data->added, iface);
1186         if (data->process_id > 0)
1187                 return;
1188
1189         data->process_id = g_idle_add(process_changes, data);
1190 }
1191
1192 static struct generic_data *object_path_ref(DBusConnection *connection,
1193                                                         const char *path)
1194 {
1195         struct generic_data *data;
1196
1197         if (dbus_connection_get_object_path_data(connection, path,
1198                                                 (void *) &data) == TRUE) {
1199                 if (data != NULL) {
1200                         data->refcount++;
1201                         return data;
1202                 }
1203         }
1204
1205         data = g_new0(struct generic_data, 1);
1206         data->conn = dbus_connection_ref(connection);
1207         data->path = g_strdup(path);
1208         data->refcount = 1;
1209
1210         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1211
1212         if (!dbus_connection_register_object_path(connection, path,
1213                                                 &generic_table, data)) {
1214                 g_free(data->introspect);
1215                 g_free(data);
1216                 return NULL;
1217         }
1218
1219         invalidate_parent_data(connection, path);
1220
1221         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1222                                                 NULL, NULL, data, NULL);
1223
1224         return data;
1225 }
1226
1227 static void object_path_unref(DBusConnection *connection, const char *path)
1228 {
1229         struct generic_data *data = NULL;
1230
1231         if (dbus_connection_get_object_path_data(connection, path,
1232                                                 (void *) &data) == FALSE)
1233                 return;
1234
1235         if (data == NULL)
1236                 return;
1237
1238         data->refcount--;
1239
1240         if (data->refcount > 0)
1241                 return;
1242
1243         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1244         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1245
1246         invalidate_parent_data(data->conn, data->path);
1247
1248         dbus_connection_unregister_object_path(data->conn, data->path);
1249 }
1250
1251 static gboolean check_signal(DBusConnection *conn, const char *path,
1252                                 const char *interface, const char *name,
1253                                 const GDBusArgInfo **args)
1254 {
1255         struct generic_data *data = NULL;
1256         struct interface_data *iface;
1257         const GDBusSignalTable *signal;
1258
1259         *args = NULL;
1260         if (!dbus_connection_get_object_path_data(conn, path,
1261                                         (void *) &data) || data == NULL) {
1262                 error("dbus_connection_emit_signal: path %s isn't registered",
1263                                 path);
1264                 return FALSE;
1265         }
1266
1267         iface = find_interface(data->interfaces, interface);
1268         if (iface == NULL) {
1269                 error("dbus_connection_emit_signal: %s does not implement %s",
1270                                 path, interface);
1271                 return FALSE;
1272         }
1273
1274         for (signal = iface->signals; signal && signal->name; signal++) {
1275                 if (strcmp(signal->name, name) != 0)
1276                         continue;
1277
1278                 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1279                         const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1280                         if (g_strcmp0(env, "1") != 0)
1281                                 break;
1282                 }
1283
1284                 *args = signal->args;
1285                 return TRUE;
1286         }
1287
1288         error("No signal named %s on interface %s", name, interface);
1289         return FALSE;
1290 }
1291
1292 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1293                                                 const char *path,
1294                                                 const char *interface,
1295                                                 const char *name,
1296                                                 int first,
1297                                                 va_list var_args)
1298 {
1299         DBusMessage *signal;
1300         dbus_bool_t ret;
1301         const GDBusArgInfo *args;
1302
1303         if (!check_signal(conn, path, interface, name, &args))
1304                 return FALSE;
1305
1306         signal = dbus_message_new_signal(path, interface, name);
1307         if (signal == NULL) {
1308                 error("Unable to allocate new %s.%s signal", interface,  name);
1309                 return FALSE;
1310         }
1311
1312         ret = dbus_message_append_args_valist(signal, first, var_args);
1313         if (!ret)
1314                 goto fail;
1315
1316         if (g_dbus_args_have_signature(args, signal) == FALSE) {
1317                 error("%s.%s: got unexpected signature '%s'", interface, name,
1318                                         dbus_message_get_signature(signal));
1319                 ret = FALSE;
1320                 goto fail;
1321         }
1322
1323         ret = dbus_connection_send(conn, signal, NULL);
1324
1325 fail:
1326         dbus_message_unref(signal);
1327
1328         return ret;
1329 }
1330
1331 gboolean g_dbus_register_interface(DBusConnection *connection,
1332                                         const char *path, const char *name,
1333                                         const GDBusMethodTable *methods,
1334                                         const GDBusSignalTable *signals,
1335                                         const GDBusPropertyTable *properties,
1336                                         void *user_data,
1337                                         GDBusDestroyFunction destroy)
1338 {
1339         struct generic_data *data;
1340
1341         data = object_path_ref(connection, path);
1342         if (data == NULL)
1343                 return FALSE;
1344
1345         if (find_interface(data->interfaces, name)) {
1346                 object_path_unref(connection, path);
1347                 return FALSE;
1348         }
1349
1350         if (properties != NULL && !find_interface(data->interfaces,
1351                                                 DBUS_INTERFACE_PROPERTIES))
1352                 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1353                                 properties_methods, properties_signals, NULL,
1354                                 data, NULL);
1355
1356         add_interface(data, name, methods, signals, properties, user_data,
1357                                                                 destroy);
1358
1359         g_free(data->introspect);
1360         data->introspect = NULL;
1361
1362         return TRUE;
1363 }
1364
1365 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1366                                         const char *path, const char *name)
1367 {
1368         struct generic_data *data = NULL;
1369
1370         if (path == NULL)
1371                 return FALSE;
1372
1373         if (dbus_connection_get_object_path_data(connection, path,
1374                                                 (void *) &data) == FALSE)
1375                 return FALSE;
1376
1377         if (data == NULL)
1378                 return FALSE;
1379
1380         if (remove_interface(data, name) == FALSE)
1381                 return FALSE;
1382
1383         g_free(data->introspect);
1384         data->introspect = NULL;
1385
1386         object_path_unref(connection, data->path);
1387
1388         return TRUE;
1389 }
1390
1391 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1392 {
1393         if (security_table != NULL)
1394                 return FALSE;
1395
1396         security_table = security;
1397
1398         return TRUE;
1399 }
1400
1401 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1402 {
1403         security_table = NULL;
1404
1405         return TRUE;
1406 }
1407
1408 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1409                                         const char *format, va_list args)
1410 {
1411         char str[1024];
1412
1413         vsnprintf(str, sizeof(str), format, args);
1414
1415         return dbus_message_new_error(message, name, str);
1416 }
1417
1418 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1419                                                 const char *format, ...)
1420 {
1421         va_list args;
1422         DBusMessage *reply;
1423
1424         va_start(args, format);
1425
1426         reply = g_dbus_create_error_valist(message, name, format, args);
1427
1428         va_end(args);
1429
1430         return reply;
1431 }
1432
1433 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1434                                                 int type, va_list args)
1435 {
1436         DBusMessage *reply;
1437
1438         reply = dbus_message_new_method_return(message);
1439         if (reply == NULL)
1440                 return NULL;
1441
1442         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1443                 dbus_message_unref(reply);
1444                 return NULL;
1445         }
1446
1447         return reply;
1448 }
1449
1450 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1451 {
1452         va_list args;
1453         DBusMessage *reply;
1454
1455         va_start(args, type);
1456
1457         reply = g_dbus_create_reply_valist(message, type, args);
1458
1459         va_end(args);
1460
1461         return reply;
1462 }
1463
1464 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1465 {
1466         dbus_bool_t result;
1467
1468         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1469                 dbus_message_set_no_reply(message, TRUE);
1470
1471         result = dbus_connection_send(connection, message, NULL);
1472
1473         dbus_message_unref(message);
1474
1475         return result;
1476 }
1477
1478 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1479                                         DBusMessage *message, const char *name,
1480                                         const char *format, va_list args)
1481 {
1482         DBusMessage *error;
1483         char str[1024];
1484
1485         vsnprintf(str, sizeof(str), format, args);
1486
1487         error = dbus_message_new_error(message, name, str);
1488         if (error == NULL)
1489                 return FALSE;
1490
1491         return g_dbus_send_message(connection, error);
1492 }
1493
1494 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1495                                 const char *name, const char *format, ...)
1496 {
1497         va_list args;
1498         gboolean result;
1499
1500         va_start(args, format);
1501
1502         result = g_dbus_send_error_valist(connection, message, name,
1503                                                         format, args);
1504
1505         va_end(args);
1506
1507         return result;
1508 }
1509
1510 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1511                                 DBusMessage *message, int type, va_list args)
1512 {
1513         DBusMessage *reply;
1514
1515         reply = dbus_message_new_method_return(message);
1516         if (reply == NULL)
1517                 return FALSE;
1518
1519         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1520                 dbus_message_unref(reply);
1521                 return FALSE;
1522         }
1523
1524         return g_dbus_send_message(connection, reply);
1525 }
1526
1527 gboolean g_dbus_send_reply(DBusConnection *connection,
1528                                 DBusMessage *message, int type, ...)
1529 {
1530         va_list args;
1531         gboolean result;
1532
1533         va_start(args, type);
1534
1535         result = g_dbus_send_reply_valist(connection, message, type, args);
1536
1537         va_end(args);
1538
1539         return result;
1540 }
1541
1542 gboolean g_dbus_emit_signal(DBusConnection *connection,
1543                                 const char *path, const char *interface,
1544                                 const char *name, int type, ...)
1545 {
1546         va_list args;
1547         gboolean result;
1548
1549         va_start(args, type);
1550
1551         result = emit_signal_valist(connection, path, interface,
1552                                                         name, type, args);
1553
1554         va_end(args);
1555
1556         return result;
1557 }
1558
1559 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1560                                 const char *path, const char *interface,
1561                                 const char *name, int type, va_list args)
1562 {
1563         return emit_signal_valist(connection, path, interface,
1564                                                         name, type, args);
1565 }
1566
1567 static void process_properties_from_interface(struct generic_data *data,
1568                                                 struct interface_data *iface)
1569 {
1570         GSList *l;
1571         DBusMessage *signal;
1572         DBusMessageIter iter, dict, array;
1573         GSList *invalidated;
1574
1575         if (iface->pending_prop == NULL)
1576                 return;
1577
1578         signal = dbus_message_new_signal(data->path,
1579                         DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1580         if (signal == NULL) {
1581                 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1582                                                 ".PropertiesChanged signal");
1583                 return;
1584         }
1585
1586         iface->pending_prop = g_slist_reverse(iface->pending_prop);
1587
1588         dbus_message_iter_init_append(signal, &iter);
1589         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1590         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1591                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1592                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1593                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1594
1595         invalidated = NULL;
1596
1597         for (l = iface->pending_prop; l != NULL; l = l->next) {
1598                 GDBusPropertyTable *p = l->data;
1599
1600                 if (p->get == NULL)
1601                         continue;
1602
1603                 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1604                         invalidated = g_slist_prepend(invalidated, p);
1605                         continue;
1606                 }
1607
1608                 append_property(iface, p, &dict);
1609         }
1610
1611         dbus_message_iter_close_container(&iter, &dict);
1612
1613         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1614                                 DBUS_TYPE_STRING_AS_STRING, &array);
1615         for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1616                 GDBusPropertyTable *p = l->data;
1617
1618                 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1619                                                                 &p->name);
1620         }
1621         g_slist_free(invalidated);
1622         dbus_message_iter_close_container(&iter, &array);
1623
1624         g_dbus_send_message(data->conn, signal);
1625
1626         g_slist_free(iface->pending_prop);
1627         iface->pending_prop = NULL;
1628 }
1629
1630 static void process_property_changes(struct generic_data *data)
1631 {
1632         GSList *l;
1633
1634         for (l = data->interfaces; l != NULL; l = l->next) {
1635                 struct interface_data *iface = l->data;
1636
1637                 process_properties_from_interface(data, iface);
1638         }
1639
1640         data->pending_prop = FALSE;
1641 }
1642
1643 void g_dbus_emit_property_changed(DBusConnection *connection,
1644                                 const char *path, const char *interface,
1645                                 const char *name)
1646 {
1647         const GDBusPropertyTable *property;
1648         struct generic_data *data;
1649         struct interface_data *iface;
1650
1651         if (!dbus_connection_get_object_path_data(connection, path,
1652                                         (void **) &data) || data == NULL)
1653                 return;
1654
1655         iface = find_interface(data->interfaces, interface);
1656         if (iface == NULL)
1657                 return;
1658
1659         property = find_property(iface->properties, name);
1660         if (property == NULL) {
1661                 error("Could not find property %s in %p", name,
1662                                                         iface->properties);
1663                 return;
1664         }
1665
1666         if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1667                 return;
1668
1669         data->pending_prop = TRUE;
1670         iface->pending_prop = g_slist_prepend(iface->pending_prop,
1671                                                 (void *) property);
1672
1673         if (!data->process_id) {
1674                 data->process_id = g_idle_add(process_changes, data);
1675                 return;
1676         }
1677 }
1678
1679 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1680                                 const char *interface, DBusMessageIter *iter)
1681 {
1682         struct generic_data *data;
1683         struct interface_data *iface;
1684
1685         if (!dbus_connection_get_object_path_data(connection, path,
1686                                         (void **) &data) || data == NULL)
1687                 return FALSE;
1688
1689         iface = find_interface(data->interfaces, interface);
1690         if (iface == NULL)
1691                 return FALSE;
1692
1693         append_properties(iface, iter);
1694
1695         return TRUE;
1696 }
1697
1698 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1699 {
1700         struct generic_data *data;
1701
1702         data = object_path_ref(connection, "/");
1703         if (data == NULL)
1704                 return FALSE;
1705
1706         add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1707                                         manager_methods, manager_signals,
1708                                         NULL, data, NULL);
1709         root = data;
1710
1711         return TRUE;
1712 }
1713
1714 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1715 {
1716         if (!g_dbus_unregister_interface(connection, "/",
1717                                         DBUS_INTERFACE_OBJECT_MANAGER))
1718                 return FALSE;
1719
1720         root = NULL;
1721
1722         return TRUE;
1723 }
1724
1725 void g_dbus_set_flags(int flags)
1726 {
1727         global_flags = flags;
1728 }