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