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