Imported Upstream version 1.35
[platform/upstream/connman.git] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <dbus/dbus.h>
33
34 #include "gdbus.h"
35
36 #define info(fmt...)
37 #define error(fmt...)
38 #define debug(fmt...)
39
40 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
41
42 #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
43 #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
44 #endif
45
46 #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
47 #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
48 #endif
49
50 struct generic_data {
51         unsigned int refcount;
52         DBusConnection *conn;
53         char *path;
54         GSList *interfaces;
55         GSList *objects;
56         GSList *added;
57         GSList *removed;
58         guint process_id;
59         gboolean pending_prop;
60         char *introspect;
61         struct generic_data *parent;
62 };
63
64 struct interface_data {
65         char *name;
66         const GDBusMethodTable *methods;
67         const GDBusSignalTable *signals;
68         const GDBusPropertyTable *properties;
69         GSList *pending_prop;
70         void *user_data;
71         GDBusDestroyFunction destroy;
72 };
73
74 struct security_data {
75         GDBusPendingReply pending;
76         DBusMessage *message;
77         const GDBusMethodTable *method;
78         void *iface_user_data;
79 };
80
81 struct property_data {
82         DBusConnection *conn;
83         GDBusPendingPropertySet id;
84         DBusMessage *message;
85 };
86
87 static int global_flags = 0;
88 static struct generic_data *root;
89 static GSList *pending = NULL;
90
91 static gboolean process_changes(gpointer user_data);
92 static void process_properties_from_interface(struct generic_data *data,
93                                                 struct interface_data *iface);
94 static void process_property_changes(struct generic_data *data);
95
96 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
97                                                 const char *direction)
98 {
99         for (; args && args->name; args++) {
100                 g_string_append_printf(gstr,
101                                         "<arg name=\"%s\" type=\"%s\"",
102                                         args->name, args->signature);
103
104                 if (direction)
105                         g_string_append_printf(gstr,
106                                         " direction=\"%s\"/>\n", direction);
107                 else
108                         g_string_append_printf(gstr, "/>\n");
109
110         }
111 }
112
113 #define G_DBUS_ANNOTATE(name_, value_)                          \
114         "<annotation name=\"org.freedesktop.DBus." name_ "\" "  \
115         "value=\"" value_ "\"/>"
116
117 #define G_DBUS_ANNOTATE_DEPRECATED \
118         G_DBUS_ANNOTATE("Deprecated", "true")
119
120 #define G_DBUS_ANNOTATE_NOREPLY \
121         G_DBUS_ANNOTATE("Method.NoReply", "true")
122
123 static gboolean check_experimental(int flags, int flag)
124 {
125         if (!(flags & flag))
126                 return FALSE;
127
128         return !(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL);
129 }
130
131 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
132 {
133         const GDBusMethodTable *method;
134         const GDBusSignalTable *signal;
135         const GDBusPropertyTable *property;
136
137         for (method = iface->methods; method && method->name; method++) {
138                 if (check_experimental(method->flags,
139                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
140                         continue;
141
142                 g_string_append_printf(gstr, "<method name=\"%s\">",
143                                                                 method->name);
144                 print_arguments(gstr, method->in_args, "in");
145                 print_arguments(gstr, method->out_args, "out");
146
147                 if (method->flags & G_DBUS_METHOD_FLAG_DEPRECATED)
148                         g_string_append_printf(gstr,
149                                                 G_DBUS_ANNOTATE_DEPRECATED);
150
151                 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY)
152                         g_string_append_printf(gstr, G_DBUS_ANNOTATE_NOREPLY);
153
154                 g_string_append_printf(gstr, "</method>");
155         }
156
157         for (signal = iface->signals; signal && signal->name; signal++) {
158                 if (check_experimental(signal->flags,
159                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
160                         continue;
161
162                 g_string_append_printf(gstr, "<signal name=\"%s\">",
163                                                                 signal->name);
164                 print_arguments(gstr, signal->args, NULL);
165
166                 if (signal->flags & G_DBUS_SIGNAL_FLAG_DEPRECATED)
167                         g_string_append_printf(gstr,
168                                                 G_DBUS_ANNOTATE_DEPRECATED);
169
170                 g_string_append_printf(gstr, "</signal>\n");
171         }
172
173         for (property = iface->properties; property && property->name;
174                                                                 property++) {
175                 if (check_experimental(property->flags,
176                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
177                         continue;
178
179                 g_string_append_printf(gstr, "<property name=\"%s\""
180                                         " type=\"%s\" access=\"%s%s\">",
181                                         property->name, property->type,
182                                         property->get ? "read" : "",
183                                         property->set ? "write" : "");
184
185                 if (property->flags & G_DBUS_PROPERTY_FLAG_DEPRECATED)
186                         g_string_append_printf(gstr,
187                                                 G_DBUS_ANNOTATE_DEPRECATED);
188
189                 g_string_append_printf(gstr, "</property>");
190         }
191 }
192
193 static void generate_introspection_xml(DBusConnection *conn,
194                                 struct generic_data *data, const char *path)
195 {
196         GSList *list;
197         GString *gstr;
198         char **children;
199         int i;
200
201         g_free(data->introspect);
202
203         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
204
205         g_string_append_printf(gstr, "<node>");
206
207         for (list = data->interfaces; list; list = list->next) {
208                 struct interface_data *iface = list->data;
209
210                 g_string_append_printf(gstr, "<interface name=\"%s\">",
211                                                                 iface->name);
212
213                 generate_interface_xml(gstr, iface);
214
215                 g_string_append_printf(gstr, "</interface>");
216         }
217
218         if (!dbus_connection_list_registered(conn, path, &children))
219                 goto done;
220
221         for (i = 0; children[i]; i++)
222                 g_string_append_printf(gstr, "<node name=\"%s\"/>",
223                                                                 children[i]);
224
225         dbus_free_string_array(children);
226
227 done:
228         g_string_append_printf(gstr, "</node>");
229
230         data->introspect = g_string_free(gstr, FALSE);
231 }
232
233 static DBusMessage *introspect(DBusConnection *connection,
234                                 DBusMessage *message, void *user_data)
235 {
236         struct generic_data *data = user_data;
237         DBusMessage *reply;
238
239         if (data->introspect == NULL)
240                 generate_introspection_xml(connection, data,
241                                                 dbus_message_get_path(message));
242
243         reply = dbus_message_new_method_return(message);
244         if (reply == NULL)
245                 return NULL;
246
247         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
248                                         DBUS_TYPE_INVALID);
249
250         return reply;
251 }
252
253 static DBusHandlerResult process_message(DBusConnection *connection,
254                         DBusMessage *message, const GDBusMethodTable *method,
255                                                         void *iface_user_data)
256 {
257         DBusMessage *reply;
258
259         reply = method->function(connection, message, iface_user_data);
260
261         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY ||
262                                         dbus_message_get_no_reply(message)) {
263                 if (reply != NULL)
264                         dbus_message_unref(reply);
265                 return DBUS_HANDLER_RESULT_HANDLED;
266         }
267
268         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
269                 if (reply == NULL)
270                         return DBUS_HANDLER_RESULT_HANDLED;
271         }
272
273         if (reply == NULL)
274                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
275
276         g_dbus_send_message(connection, reply);
277
278         return DBUS_HANDLER_RESULT_HANDLED;
279 }
280
281 static GDBusPendingReply next_pending = 1;
282 static GSList *pending_security = NULL;
283
284 static const GDBusSecurityTable *security_table = NULL;
285
286 void g_dbus_pending_success(DBusConnection *connection,
287                                         GDBusPendingReply pending)
288 {
289         GSList *list;
290
291         for (list = pending_security; list; list = list->next) {
292                 struct security_data *secdata = list->data;
293
294                 if (secdata->pending != pending)
295                         continue;
296
297                 pending_security = g_slist_remove(pending_security, secdata);
298
299                 process_message(connection, secdata->message,
300                                 secdata->method, secdata->iface_user_data);
301
302                 dbus_message_unref(secdata->message);
303                 g_free(secdata);
304                 return;
305         }
306 }
307
308 void g_dbus_pending_error_valist(DBusConnection *connection,
309                                 GDBusPendingReply pending, const char *name,
310                                         const char *format, va_list args)
311 {
312         GSList *list;
313
314         for (list = pending_security; list; list = list->next) {
315                 struct security_data *secdata = list->data;
316
317                 if (secdata->pending != pending)
318                         continue;
319
320                 pending_security = g_slist_remove(pending_security, secdata);
321
322                 g_dbus_send_error_valist(connection, secdata->message,
323                                                         name, format, args);
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
469         propdata = remove_pending_property_data(id);
470         if (propdata == NULL)
471                 return;
472
473         g_dbus_send_error_valist(propdata->conn, propdata->message, name,
474                                                                 format, args);
475
476         dbus_message_unref(propdata->message);
477         g_free(propdata);
478 }
479
480 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
481                                                 const char *format, ...)
482 {
483         va_list args;
484
485         va_start(args, format);
486
487         g_dbus_pending_property_error_valist(id, name, format, args);
488
489         va_end(args);
490 }
491
492 static void reset_parent(gpointer data, gpointer user_data)
493 {
494         struct generic_data *child = data;
495         struct generic_data *parent = user_data;
496
497         child->parent = parent;
498 }
499
500 static void append_property(struct interface_data *iface,
501                         const GDBusPropertyTable *p, DBusMessageIter *dict)
502 {
503         DBusMessageIter entry, value;
504
505         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
506                                                                 &entry);
507         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
508         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
509                                                                 &value);
510
511         p->get(p, &value, iface->user_data);
512
513         dbus_message_iter_close_container(&entry, &value);
514         dbus_message_iter_close_container(dict, &entry);
515 }
516
517 static void append_properties(struct interface_data *data,
518                                                         DBusMessageIter *iter)
519 {
520         DBusMessageIter dict;
521         const GDBusPropertyTable *p;
522
523         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
524                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
525                                 DBUS_TYPE_STRING_AS_STRING
526                                 DBUS_TYPE_VARIANT_AS_STRING
527                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
528
529         for (p = data->properties; p && p->name; p++) {
530                 if (check_experimental(p->flags,
531                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
532                         continue;
533
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
564         if (root == NULL || data == root)
565                 return;
566
567         signal = dbus_message_new_signal(root->path,
568                                         DBUS_INTERFACE_OBJECT_MANAGER,
569                                         "InterfacesAdded");
570         if (signal == NULL)
571                 return;
572
573         dbus_message_iter_init_append(signal, &iter);
574         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
575                                                                 &data->path);
576
577         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
578                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
579                                 DBUS_TYPE_STRING_AS_STRING
580                                 DBUS_TYPE_ARRAY_AS_STRING
581                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
582                                 DBUS_TYPE_STRING_AS_STRING
583                                 DBUS_TYPE_VARIANT_AS_STRING
584                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
585                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
586
587         g_slist_foreach(data->added, append_interface, &array);
588         g_slist_free(data->added);
589         data->added = NULL;
590
591         dbus_message_iter_close_container(&iter, &array);
592
593         /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
594         dbus_connection_send(data->conn, signal, NULL);
595         dbus_message_unref(signal);
596 }
597
598 static struct interface_data *find_interface(GSList *interfaces,
599                                                 const char *name)
600 {
601         GSList *list;
602
603         if (name == NULL)
604                 return NULL;
605
606         for (list = interfaces; list; list = list->next) {
607                 struct interface_data *iface = list->data;
608                 if (!strcmp(name, iface->name))
609                         return iface;
610         }
611
612         return NULL;
613 }
614
615 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
616                                                         DBusMessage *message)
617 {
618         const char *sig = dbus_message_get_signature(message);
619         const char *p = NULL;
620
621         for (; args && args->signature && *sig; args++) {
622                 p = args->signature;
623
624                 for (; *sig && *p; sig++, p++) {
625                         if (*p != *sig)
626                                 return FALSE;
627                 }
628         }
629
630         if (*sig || (p && *p) || (args && args->signature))
631                 return FALSE;
632
633         return TRUE;
634 }
635
636 static void add_pending(struct generic_data *data)
637 {
638         if (data->process_id > 0)
639                 return;
640
641         data->process_id = g_idle_add(process_changes, data);
642
643         pending = g_slist_append(pending, data);
644 }
645
646 static gboolean remove_interface(struct generic_data *data, const char *name)
647 {
648         struct interface_data *iface;
649
650         iface = find_interface(data->interfaces, name);
651         if (iface == NULL)
652                 return FALSE;
653
654         process_properties_from_interface(data, iface);
655
656         data->interfaces = g_slist_remove(data->interfaces, iface);
657
658         if (iface->destroy) {
659                 iface->destroy(iface->user_data);
660                 iface->user_data = NULL;
661         }
662
663         /*
664          * Interface being removed was just added, on the same mainloop
665          * iteration? Don't send any signal
666          */
667         if (g_slist_find(data->added, iface)) {
668                 data->added = g_slist_remove(data->added, iface);
669                 g_free(iface->name);
670                 g_free(iface);
671                 return TRUE;
672         }
673
674         if (data->parent == NULL) {
675                 g_free(iface->name);
676                 g_free(iface);
677                 return TRUE;
678         }
679
680         data->removed = g_slist_prepend(data->removed, iface->name);
681         g_free(iface);
682
683         add_pending(data);
684
685         return TRUE;
686 }
687
688 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
689                                                 const char *child_path)
690 {
691         struct generic_data *data = NULL, *child = NULL, *parent = NULL;
692         char *parent_path, *slash;
693
694         parent_path = g_strdup(child_path);
695         slash = strrchr(parent_path, '/');
696         if (slash == NULL)
697                 goto done;
698
699         if (slash == parent_path && parent_path[1] != '\0')
700                 parent_path[1] = '\0';
701         else
702                 *slash = '\0';
703
704         if (!strlen(parent_path))
705                 goto done;
706
707         if (dbus_connection_get_object_path_data(conn, parent_path,
708                                                         (void *) &data) == FALSE) {
709                 goto done;
710         }
711
712         parent = invalidate_parent_data(conn, parent_path);
713
714         if (data == NULL) {
715                 data = parent;
716                 if (data == NULL)
717                         goto done;
718         }
719
720         g_free(data->introspect);
721         data->introspect = NULL;
722
723         if (!dbus_connection_get_object_path_data(conn, child_path,
724                                                         (void *) &child))
725                 goto done;
726
727         if (child == NULL || g_slist_find(data->objects, child) != NULL)
728                 goto done;
729
730         data->objects = g_slist_prepend(data->objects, child);
731         child->parent = data;
732
733 done:
734         g_free(parent_path);
735         return data;
736 }
737
738 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
739                                                         const char *name)
740 {
741         const GDBusPropertyTable *p;
742
743         for (p = properties; p && p->name; p++) {
744                 if (strcmp(name, p->name) != 0)
745                         continue;
746
747                 if (check_experimental(p->flags,
748                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
749                         break;
750
751                 return p;
752         }
753
754         return NULL;
755 }
756
757 static DBusMessage *properties_get(DBusConnection *connection,
758                                         DBusMessage *message, void *user_data)
759 {
760         struct generic_data *data = user_data;
761         struct interface_data *iface;
762         const GDBusPropertyTable *property;
763         const char *interface, *name;
764         DBusMessageIter iter, value;
765         DBusMessage *reply;
766
767         if (!dbus_message_get_args(message, NULL,
768                                         DBUS_TYPE_STRING, &interface,
769                                         DBUS_TYPE_STRING, &name,
770                                         DBUS_TYPE_INVALID))
771                 return NULL;
772
773         iface = find_interface(data->interfaces, interface);
774         if (iface == NULL)
775                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
776                                 "No such interface '%s'", interface);
777
778         property = find_property(iface->properties, name);
779         if (property == NULL)
780                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
781                                 "No such property '%s'", name);
782
783         if (property->exists != NULL &&
784                         !property->exists(property, iface->user_data))
785                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
786                                         "No such property '%s'", name);
787
788         if (property->get == NULL)
789                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
790                                 "Property '%s' is not readable", name);
791
792         reply = dbus_message_new_method_return(message);
793         if (reply == NULL)
794                 return NULL;
795
796         dbus_message_iter_init_append(reply, &iter);
797         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
798                                                 property->type, &value);
799
800         if (!property->get(property, &value, iface->user_data)) {
801                 dbus_message_unref(reply);
802                 return NULL;
803         }
804
805         dbus_message_iter_close_container(&iter, &value);
806
807         return reply;
808 }
809
810 static DBusMessage *properties_get_all(DBusConnection *connection,
811                                         DBusMessage *message, void *user_data)
812 {
813         struct generic_data *data = user_data;
814         struct interface_data *iface;
815         const char *interface;
816         DBusMessageIter iter;
817         DBusMessage *reply;
818
819         if (!dbus_message_get_args(message, NULL,
820                                         DBUS_TYPE_STRING, &interface,
821                                         DBUS_TYPE_INVALID))
822                 return NULL;
823
824         iface = find_interface(data->interfaces, interface);
825         if (iface == NULL)
826                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
827                                         "No such interface '%s'", interface);
828
829         reply = dbus_message_new_method_return(message);
830         if (reply == NULL)
831                 return NULL;
832
833         dbus_message_iter_init_append(reply, &iter);
834
835         append_properties(iface, &iter);
836
837         return reply;
838 }
839
840 static DBusMessage *properties_set(DBusConnection *connection,
841                                         DBusMessage *message, void *user_data)
842 {
843         struct generic_data *data = user_data;
844         DBusMessageIter iter, sub;
845         struct interface_data *iface;
846         const GDBusPropertyTable *property;
847         const char *name, *interface;
848         struct property_data *propdata;
849         gboolean valid_signature;
850         char *signature;
851
852         if (!dbus_message_iter_init(message, &iter))
853                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
854                                                         "No arguments given");
855
856         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
857                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
858                                         "Invalid argument type: '%c'",
859                                         dbus_message_iter_get_arg_type(&iter));
860
861         dbus_message_iter_get_basic(&iter, &interface);
862         dbus_message_iter_next(&iter);
863
864         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
865                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
866                                         "Invalid argument type: '%c'",
867                                         dbus_message_iter_get_arg_type(&iter));
868
869         dbus_message_iter_get_basic(&iter, &name);
870         dbus_message_iter_next(&iter);
871
872         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
873                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
874                                         "Invalid argument type: '%c'",
875                                         dbus_message_iter_get_arg_type(&iter));
876
877         dbus_message_iter_recurse(&iter, &sub);
878
879         iface = find_interface(data->interfaces, interface);
880         if (iface == NULL)
881                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
882                                         "No such interface '%s'", interface);
883
884         property = find_property(iface->properties, name);
885         if (property == NULL)
886                 return g_dbus_create_error(message,
887                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
888                                                 "No such property '%s'", name);
889
890         if (property->set == NULL)
891                 return g_dbus_create_error(message,
892                                         DBUS_ERROR_PROPERTY_READ_ONLY,
893                                         "Property '%s' is not writable", name);
894
895         if (property->exists != NULL &&
896                         !property->exists(property, iface->user_data))
897                 return g_dbus_create_error(message,
898                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
899                                                 "No such property '%s'", name);
900
901         signature = dbus_message_iter_get_signature(&sub);
902         valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
903         dbus_free(signature);
904         if (!valid_signature)
905                 return g_dbus_create_error(message,
906                                         DBUS_ERROR_INVALID_SIGNATURE,
907                                         "Invalid signature for '%s'", name);
908
909         propdata = g_new(struct property_data, 1);
910         propdata->id = next_pending_property++;
911         propdata->message = dbus_message_ref(message);
912         propdata->conn = connection;
913         pending_property_set = g_slist_prepend(pending_property_set, propdata);
914
915         property->set(property, &sub, propdata->id, iface->user_data);
916
917         return NULL;
918 }
919
920 static const GDBusMethodTable properties_methods[] = {
921         { GDBUS_METHOD("Get",
922                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
923                         GDBUS_ARGS({ "value", "v" }),
924                         properties_get) },
925         { GDBUS_ASYNC_METHOD("Set",
926                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
927                                                         { "value", "v" }),
928                         NULL,
929                         properties_set) },
930         { GDBUS_METHOD("GetAll",
931                         GDBUS_ARGS({ "interface", "s" }),
932                         GDBUS_ARGS({ "properties", "a{sv}" }),
933                         properties_get_all) },
934         { }
935 };
936
937 static const GDBusSignalTable properties_signals[] = {
938         { GDBUS_SIGNAL("PropertiesChanged",
939                         GDBUS_ARGS({ "interface", "s" },
940                                         { "changed_properties", "a{sv}" },
941                                         { "invalidated_properties", "as"})) },
942         { }
943 };
944
945 static void append_name(gpointer data, gpointer user_data)
946 {
947         char *name = data;
948         DBusMessageIter *iter = user_data;
949
950         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
951 }
952
953 static void emit_interfaces_removed(struct generic_data *data)
954 {
955         DBusMessage *signal;
956         DBusMessageIter iter, array;
957
958         if (root == NULL || data == root)
959                 return;
960
961         signal = dbus_message_new_signal(root->path,
962                                         DBUS_INTERFACE_OBJECT_MANAGER,
963                                         "InterfacesRemoved");
964         if (signal == NULL)
965                 return;
966
967         dbus_message_iter_init_append(signal, &iter);
968         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
969                                                                 &data->path);
970         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
971                                         DBUS_TYPE_STRING_AS_STRING, &array);
972
973         g_slist_foreach(data->removed, append_name, &array);
974         g_slist_free_full(data->removed, g_free);
975         data->removed = NULL;
976
977         dbus_message_iter_close_container(&iter, &array);
978
979         /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
980         dbus_connection_send(data->conn, signal, NULL);
981         dbus_message_unref(signal);
982 }
983
984 static void remove_pending(struct generic_data *data)
985 {
986         if (data->process_id > 0) {
987                 g_source_remove(data->process_id);
988                 data->process_id = 0;
989         }
990
991         pending = g_slist_remove(pending, data);
992 }
993
994 static gboolean process_changes(gpointer user_data)
995 {
996         struct generic_data *data = user_data;
997
998         remove_pending(data);
999
1000         if (data->added != NULL)
1001                 emit_interfaces_added(data);
1002
1003         /* Flush pending properties */
1004         if (data->pending_prop == TRUE)
1005                 process_property_changes(data);
1006
1007         if (data->removed != NULL)
1008                 emit_interfaces_removed(data);
1009
1010         data->process_id = 0;
1011
1012         return FALSE;
1013 }
1014
1015 static void generic_unregister(DBusConnection *connection, void *user_data)
1016 {
1017         struct generic_data *data = user_data;
1018         struct generic_data *parent = data->parent;
1019
1020         if (parent != NULL)
1021                 parent->objects = g_slist_remove(parent->objects, data);
1022
1023         if (data->process_id > 0) {
1024                 g_source_remove(data->process_id);
1025                 data->process_id = 0;
1026                 process_changes(data);
1027         }
1028
1029         g_slist_foreach(data->objects, reset_parent, data->parent);
1030         g_slist_free(data->objects);
1031
1032         dbus_connection_unref(data->conn);
1033         g_free(data->introspect);
1034         g_free(data->path);
1035         g_free(data);
1036 }
1037
1038 static DBusHandlerResult generic_message(DBusConnection *connection,
1039                                         DBusMessage *message, void *user_data)
1040 {
1041         struct generic_data *data = user_data;
1042         struct interface_data *iface;
1043         const GDBusMethodTable *method;
1044         const char *interface;
1045
1046         interface = dbus_message_get_interface(message);
1047
1048         iface = find_interface(data->interfaces, interface);
1049         if (iface == NULL)
1050                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1051
1052         for (method = iface->methods; method &&
1053                         method->name && method->function; method++) {
1054
1055                 if (dbus_message_is_method_call(message, iface->name,
1056                                                         method->name) == FALSE)
1057                         continue;
1058
1059                 if (check_experimental(method->flags,
1060                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1061                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1062
1063                 if (g_dbus_args_have_signature(method->in_args,
1064                                                         message) == FALSE)
1065                         continue;
1066
1067                 if (check_privilege(connection, message, method,
1068                                                 iface->user_data) == TRUE)
1069                         return DBUS_HANDLER_RESULT_HANDLED;
1070
1071                 return process_message(connection, message, method,
1072                                                         iface->user_data);
1073         }
1074
1075         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1076 }
1077
1078 static DBusObjectPathVTable generic_table = {
1079         .unregister_function    = generic_unregister,
1080         .message_function       = generic_message,
1081 };
1082
1083 static const GDBusMethodTable introspect_methods[] = {
1084         { GDBUS_METHOD("Introspect", NULL,
1085                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1086         { }
1087 };
1088
1089 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1090 {
1091         DBusMessageIter array;
1092
1093         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1094                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1095                                 DBUS_TYPE_STRING_AS_STRING
1096                                 DBUS_TYPE_ARRAY_AS_STRING
1097                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1098                                 DBUS_TYPE_STRING_AS_STRING
1099                                 DBUS_TYPE_VARIANT_AS_STRING
1100                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1101                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1102
1103         g_slist_foreach(data->interfaces, append_interface, &array);
1104
1105         dbus_message_iter_close_container(iter, &array);
1106 }
1107
1108 static void append_object(gpointer data, gpointer user_data)
1109 {
1110         struct generic_data *child = data;
1111         DBusMessageIter *array = user_data;
1112         DBusMessageIter entry;
1113
1114         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1115                                                                 &entry);
1116         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1117                                                                 &child->path);
1118         append_interfaces(child, &entry);
1119         dbus_message_iter_close_container(array, &entry);
1120
1121         g_slist_foreach(child->objects, append_object, user_data);
1122 }
1123
1124 static DBusMessage *get_objects(DBusConnection *connection,
1125                                 DBusMessage *message, void *user_data)
1126 {
1127         struct generic_data *data = user_data;
1128         DBusMessage *reply;
1129         DBusMessageIter iter;
1130         DBusMessageIter array;
1131
1132         reply = dbus_message_new_method_return(message);
1133         if (reply == NULL)
1134                 return NULL;
1135
1136         dbus_message_iter_init_append(reply, &iter);
1137
1138         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1139                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1140                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1141                                         DBUS_TYPE_ARRAY_AS_STRING
1142                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1143                                         DBUS_TYPE_STRING_AS_STRING
1144                                         DBUS_TYPE_ARRAY_AS_STRING
1145                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1146                                         DBUS_TYPE_STRING_AS_STRING
1147                                         DBUS_TYPE_VARIANT_AS_STRING
1148                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1149                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1150                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1151                                         &array);
1152
1153         g_slist_foreach(data->objects, append_object, &array);
1154
1155         dbus_message_iter_close_container(&iter, &array);
1156
1157         return reply;
1158 }
1159
1160 static const GDBusMethodTable manager_methods[] = {
1161         { GDBUS_METHOD("GetManagedObjects", NULL,
1162                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1163         { }
1164 };
1165
1166 static const GDBusSignalTable manager_signals[] = {
1167         { GDBUS_SIGNAL("InterfacesAdded",
1168                 GDBUS_ARGS({ "object", "o" },
1169                                 { "interfaces", "a{sa{sv}}" })) },
1170         { GDBUS_SIGNAL("InterfacesRemoved",
1171                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1172         { }
1173 };
1174
1175 static gboolean add_interface(struct generic_data *data,
1176                                 const char *name,
1177                                 const GDBusMethodTable *methods,
1178                                 const GDBusSignalTable *signals,
1179                                 const GDBusPropertyTable *properties,
1180                                 void *user_data,
1181                                 GDBusDestroyFunction destroy)
1182 {
1183         struct interface_data *iface;
1184         const GDBusMethodTable *method;
1185         const GDBusSignalTable *signal;
1186         const GDBusPropertyTable *property;
1187
1188         for (method = methods; method && method->name; method++) {
1189                 if (!check_experimental(method->flags,
1190                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1191                         goto done;
1192         }
1193
1194         for (signal = signals; signal && signal->name; signal++) {
1195                 if (!check_experimental(signal->flags,
1196                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1197                         goto done;
1198         }
1199
1200         for (property = properties; property && property->name; property++) {
1201                 if (!check_experimental(property->flags,
1202                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1203                         goto done;
1204         }
1205
1206         /* Nothing to register */
1207         return FALSE;
1208
1209 done:
1210         iface = g_new0(struct interface_data, 1);
1211         iface->name = g_strdup(name);
1212         iface->methods = methods;
1213         iface->signals = signals;
1214         iface->properties = properties;
1215         iface->user_data = user_data;
1216         iface->destroy = destroy;
1217
1218         data->interfaces = g_slist_append(data->interfaces, iface);
1219         if (data->parent == NULL)
1220                 return TRUE;
1221
1222         data->added = g_slist_append(data->added, iface);
1223
1224         add_pending(data);
1225
1226         return TRUE;
1227 }
1228
1229 static struct generic_data *object_path_ref(DBusConnection *connection,
1230                                                         const char *path)
1231 {
1232         struct generic_data *data;
1233
1234         if (dbus_connection_get_object_path_data(connection, path,
1235                                                 (void *) &data) == TRUE) {
1236                 if (data != NULL) {
1237                         data->refcount++;
1238                         return data;
1239                 }
1240         }
1241
1242         data = g_new0(struct generic_data, 1);
1243         data->conn = dbus_connection_ref(connection);
1244         data->path = g_strdup(path);
1245         data->refcount = 1;
1246
1247         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1248
1249         if (!dbus_connection_register_object_path(connection, path,
1250                                                 &generic_table, data)) {
1251                 dbus_connection_unref(data->conn);
1252                 g_free(data->path);
1253                 g_free(data->introspect);
1254                 g_free(data);
1255                 return NULL;
1256         }
1257
1258         invalidate_parent_data(connection, path);
1259
1260         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1261                                                 NULL, NULL, data, NULL);
1262
1263         return data;
1264 }
1265
1266 static void object_path_unref(DBusConnection *connection, const char *path)
1267 {
1268         struct generic_data *data = NULL;
1269
1270         if (dbus_connection_get_object_path_data(connection, path,
1271                                                 (void *) &data) == FALSE)
1272                 return;
1273
1274         if (data == NULL)
1275                 return;
1276
1277         data->refcount--;
1278
1279         if (data->refcount > 0)
1280                 return;
1281
1282         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1283         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1284
1285         invalidate_parent_data(data->conn, data->path);
1286
1287         dbus_connection_unregister_object_path(data->conn, data->path);
1288 }
1289
1290 static gboolean check_signal(DBusConnection *conn, const char *path,
1291                                 const char *interface, const char *name,
1292                                 const GDBusArgInfo **args)
1293 {
1294         struct generic_data *data = NULL;
1295         struct interface_data *iface;
1296         const GDBusSignalTable *signal;
1297
1298         *args = NULL;
1299         if (!dbus_connection_get_object_path_data(conn, path,
1300                                         (void *) &data) || data == NULL) {
1301                 error("dbus_connection_emit_signal: path %s isn't registered",
1302                                 path);
1303                 return FALSE;
1304         }
1305
1306         iface = find_interface(data->interfaces, interface);
1307         if (iface == NULL) {
1308                 error("dbus_connection_emit_signal: %s does not implement %s",
1309                                 path, interface);
1310                 return FALSE;
1311         }
1312
1313         for (signal = iface->signals; signal && signal->name; signal++) {
1314                 if (strcmp(signal->name, name) != 0)
1315                         continue;
1316
1317                 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1318                         const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1319                         if (g_strcmp0(env, "1") != 0)
1320                                 break;
1321                 }
1322
1323                 *args = signal->args;
1324                 return TRUE;
1325         }
1326
1327         error("No signal named %s on interface %s", name, interface);
1328         return FALSE;
1329 }
1330
1331 gboolean g_dbus_register_interface(DBusConnection *connection,
1332                                         const char *path, const char *name,
1333                                         const GDBusMethodTable *methods,
1334                                         const GDBusSignalTable *signals,
1335                                         const GDBusPropertyTable *properties,
1336                                         void *user_data,
1337                                         GDBusDestroyFunction destroy)
1338 {
1339         struct generic_data *data;
1340
1341         data = object_path_ref(connection, path);
1342         if (data == NULL)
1343                 return FALSE;
1344
1345         if (find_interface(data->interfaces, name)) {
1346                 object_path_unref(connection, path);
1347                 return FALSE;
1348         }
1349
1350         if (!add_interface(data, name, methods, signals, properties, user_data,
1351                                                                 destroy)) {
1352                 object_path_unref(connection, path);
1353                 return FALSE;
1354         }
1355
1356         if (properties != NULL && !find_interface(data->interfaces,
1357                                                 DBUS_INTERFACE_PROPERTIES))
1358                 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1359                                 properties_methods, properties_signals, NULL,
1360                                 data, NULL);
1361
1362         g_free(data->introspect);
1363         data->introspect = NULL;
1364
1365         return TRUE;
1366 }
1367
1368 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1369                                         const char *path, const char *name)
1370 {
1371         struct generic_data *data = NULL;
1372
1373         if (path == NULL)
1374                 return FALSE;
1375
1376         if (dbus_connection_get_object_path_data(connection, path,
1377                                                 (void *) &data) == FALSE)
1378                 return FALSE;
1379
1380         if (data == NULL)
1381                 return FALSE;
1382
1383         if (remove_interface(data, name) == FALSE)
1384                 return FALSE;
1385
1386         g_free(data->introspect);
1387         data->introspect = NULL;
1388
1389         object_path_unref(connection, data->path);
1390
1391         return TRUE;
1392 }
1393
1394 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1395 {
1396         if (security_table != NULL)
1397                 return FALSE;
1398
1399         security_table = security;
1400
1401         return TRUE;
1402 }
1403
1404 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1405 {
1406         security_table = NULL;
1407
1408         return TRUE;
1409 }
1410
1411 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1412                                         const char *format, va_list args)
1413 {
1414         char str[1024];
1415
1416         if (format)
1417                 vsnprintf(str, sizeof(str), format, args);
1418         else
1419                 str[0] = '\0';
1420
1421         return dbus_message_new_error(message, name, str);
1422 }
1423
1424 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1425                                                 const char *format, ...)
1426 {
1427         va_list args;
1428         DBusMessage *reply;
1429
1430         va_start(args, format);
1431
1432         reply = g_dbus_create_error_valist(message, name, format, args);
1433
1434         va_end(args);
1435
1436         return reply;
1437 }
1438
1439 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1440                                                 int type, va_list args)
1441 {
1442         DBusMessage *reply;
1443
1444         reply = dbus_message_new_method_return(message);
1445         if (reply == NULL)
1446                 return NULL;
1447
1448         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1449                 dbus_message_unref(reply);
1450                 return NULL;
1451         }
1452
1453         return reply;
1454 }
1455
1456 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1457 {
1458         va_list args;
1459         DBusMessage *reply;
1460
1461         va_start(args, type);
1462
1463         reply = g_dbus_create_reply_valist(message, type, args);
1464
1465         va_end(args);
1466
1467         return reply;
1468 }
1469
1470 static void g_dbus_flush(DBusConnection *connection)
1471 {
1472         GSList *l;
1473
1474         for (l = pending; l;) {
1475                 struct generic_data *data = l->data;
1476
1477                 l = l->next;
1478                 if (data->conn != connection)
1479                         continue;
1480
1481                 process_changes(data);
1482         }
1483 }
1484
1485 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1486 {
1487         dbus_bool_t result = FALSE;
1488
1489         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1490                 dbus_message_set_no_reply(message, TRUE);
1491         else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1492                 const char *path = dbus_message_get_path(message);
1493                 const char *interface = dbus_message_get_interface(message);
1494                 const char *name = dbus_message_get_member(message);
1495                 const GDBusArgInfo *args;
1496
1497                 if (!check_signal(connection, path, interface, name, &args))
1498                         goto out;
1499         }
1500
1501         /* Flush pending signal to guarantee message order */
1502         g_dbus_flush(connection);
1503
1504         result = dbus_connection_send(connection, message, NULL);
1505
1506 out:
1507         dbus_message_unref(message);
1508
1509         return result;
1510 }
1511
1512 gboolean g_dbus_send_message_with_reply(DBusConnection *connection,
1513                                         DBusMessage *message,
1514                                         DBusPendingCall **call, int timeout)
1515 {
1516         dbus_bool_t ret;
1517
1518         /* Flush pending signal to guarantee message order */
1519         g_dbus_flush(connection);
1520
1521         ret = dbus_connection_send_with_reply(connection, message, call,
1522                                                                 timeout);
1523
1524         if (ret == TRUE && call != NULL && *call == NULL) {
1525                 error("Unable to send message (passing fd blocked?)");
1526                 return FALSE;
1527         }
1528
1529         return ret;
1530 }
1531
1532 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1533                                         DBusMessage *message, const char *name,
1534                                         const char *format, va_list args)
1535 {
1536         DBusMessage *error;
1537
1538         error = g_dbus_create_error_valist(message, name, format, args);
1539         if (error == NULL)
1540                 return FALSE;
1541
1542         return g_dbus_send_message(connection, error);
1543 }
1544
1545 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1546                                 const char *name, const char *format, ...)
1547 {
1548         va_list args;
1549         gboolean result;
1550
1551         va_start(args, format);
1552
1553         result = g_dbus_send_error_valist(connection, message, name,
1554                                                         format, args);
1555
1556         va_end(args);
1557
1558         return result;
1559 }
1560
1561 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1562                                 DBusMessage *message, int type, va_list args)
1563 {
1564         DBusMessage *reply;
1565
1566         reply = dbus_message_new_method_return(message);
1567         if (reply == NULL)
1568                 return FALSE;
1569
1570         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1571                 dbus_message_unref(reply);
1572                 return FALSE;
1573         }
1574
1575         return g_dbus_send_message(connection, reply);
1576 }
1577
1578 gboolean g_dbus_send_reply(DBusConnection *connection,
1579                                 DBusMessage *message, int type, ...)
1580 {
1581         va_list args;
1582         gboolean result;
1583
1584         va_start(args, type);
1585
1586         result = g_dbus_send_reply_valist(connection, message, type, args);
1587
1588         va_end(args);
1589
1590         return result;
1591 }
1592
1593 gboolean g_dbus_emit_signal(DBusConnection *connection,
1594                                 const char *path, const char *interface,
1595                                 const char *name, int type, ...)
1596 {
1597         va_list args;
1598         gboolean result;
1599
1600         va_start(args, type);
1601
1602         result = g_dbus_emit_signal_valist(connection, path, interface,
1603                                                         name, type, args);
1604
1605         va_end(args);
1606
1607         return result;
1608 }
1609
1610 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1611                                 const char *path, const char *interface,
1612                                 const char *name, int type, va_list args)
1613 {
1614         DBusMessage *signal;
1615         dbus_bool_t ret;
1616         const GDBusArgInfo *args_info;
1617
1618         if (!check_signal(connection, path, interface, name, &args_info))
1619                 return FALSE;
1620
1621         signal = dbus_message_new_signal(path, interface, name);
1622         if (signal == NULL) {
1623                 error("Unable to allocate new %s.%s signal", interface,  name);
1624                 return FALSE;
1625         }
1626
1627         ret = dbus_message_append_args_valist(signal, type, args);
1628         if (!ret)
1629                 goto fail;
1630
1631         if (g_dbus_args_have_signature(args_info, signal) == FALSE) {
1632                 error("%s.%s: got unexpected signature '%s'", interface, name,
1633                                         dbus_message_get_signature(signal));
1634                 ret = FALSE;
1635                 goto fail;
1636         }
1637
1638         return g_dbus_send_message(connection, signal);
1639
1640 fail:
1641         dbus_message_unref(signal);
1642
1643         return ret;
1644 }
1645
1646 static void process_properties_from_interface(struct generic_data *data,
1647                                                 struct interface_data *iface)
1648 {
1649         GSList *l;
1650         DBusMessage *signal;
1651         DBusMessageIter iter, dict, array;
1652         GSList *invalidated;
1653
1654         data->pending_prop = FALSE;
1655
1656         if (iface->pending_prop == NULL)
1657                 return;
1658
1659         signal = dbus_message_new_signal(data->path,
1660                         DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1661         if (signal == NULL) {
1662                 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1663                                                 ".PropertiesChanged signal");
1664                 return;
1665         }
1666
1667         iface->pending_prop = g_slist_reverse(iface->pending_prop);
1668
1669         dbus_message_iter_init_append(signal, &iter);
1670         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1671         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1672                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1673                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1674                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1675
1676         invalidated = NULL;
1677
1678         for (l = iface->pending_prop; l != NULL; l = l->next) {
1679                 GDBusPropertyTable *p = l->data;
1680
1681                 if (p->get == NULL)
1682                         continue;
1683
1684                 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1685                         invalidated = g_slist_prepend(invalidated, p);
1686                         continue;
1687                 }
1688
1689                 append_property(iface, p, &dict);
1690         }
1691
1692         dbus_message_iter_close_container(&iter, &dict);
1693
1694         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1695                                 DBUS_TYPE_STRING_AS_STRING, &array);
1696         for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1697                 GDBusPropertyTable *p = l->data;
1698
1699                 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1700                                                                 &p->name);
1701         }
1702         g_slist_free(invalidated);
1703         dbus_message_iter_close_container(&iter, &array);
1704
1705         g_slist_free(iface->pending_prop);
1706         iface->pending_prop = NULL;
1707
1708         /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
1709         dbus_connection_send(data->conn, signal, NULL);
1710         dbus_message_unref(signal);
1711 }
1712
1713 static void process_property_changes(struct generic_data *data)
1714 {
1715         GSList *l;
1716
1717         for (l = data->interfaces; l != NULL; l = l->next) {
1718                 struct interface_data *iface = l->data;
1719
1720                 process_properties_from_interface(data, iface);
1721         }
1722 }
1723
1724 void g_dbus_emit_property_changed_full(DBusConnection *connection,
1725                                 const char *path, const char *interface,
1726                                 const char *name,
1727                                 GDbusPropertyChangedFlags flags)
1728 {
1729         const GDBusPropertyTable *property;
1730         struct generic_data *data;
1731         struct interface_data *iface;
1732
1733         if (path == NULL)
1734                 return;
1735
1736         if (!dbus_connection_get_object_path_data(connection, path,
1737                                         (void **) &data) || data == NULL)
1738                 return;
1739
1740         iface = find_interface(data->interfaces, interface);
1741         if (iface == NULL)
1742                 return;
1743
1744         /*
1745          * If ObjectManager is attached, don't emit property changed if
1746          * interface is not yet published
1747          */
1748         if (root && g_slist_find(data->added, iface))
1749                 return;
1750
1751         property = find_property(iface->properties, name);
1752         if (property == NULL) {
1753                 error("Could not find property %s in %p", name,
1754                                                         iface->properties);
1755                 return;
1756         }
1757
1758         if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1759                 return;
1760
1761         data->pending_prop = TRUE;
1762         iface->pending_prop = g_slist_prepend(iface->pending_prop,
1763                                                 (void *) property);
1764
1765         if (flags & G_DBUS_PROPERTY_CHANGED_FLAG_FLUSH)
1766                 process_property_changes(data);
1767         else
1768                 add_pending(data);
1769 }
1770
1771 void g_dbus_emit_property_changed(DBusConnection *connection, const char *path,
1772                                 const char *interface, const char *name)
1773 {
1774         g_dbus_emit_property_changed_full(connection, path, interface, name, 0);
1775 }
1776
1777 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1778                                 const char *interface, DBusMessageIter *iter)
1779 {
1780         struct generic_data *data;
1781         struct interface_data *iface;
1782
1783         if (path == NULL)
1784                 return FALSE;
1785
1786         if (!dbus_connection_get_object_path_data(connection, path,
1787                                         (void **) &data) || data == NULL)
1788                 return FALSE;
1789
1790         iface = find_interface(data->interfaces, interface);
1791         if (iface == NULL)
1792                 return FALSE;
1793
1794         append_properties(iface, iter);
1795
1796         return TRUE;
1797 }
1798
1799 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1800 {
1801         struct generic_data *data;
1802
1803         data = object_path_ref(connection, "/");
1804         if (data == NULL)
1805                 return FALSE;
1806
1807         add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1808                                         manager_methods, manager_signals,
1809                                         NULL, data, NULL);
1810         root = data;
1811
1812         return TRUE;
1813 }
1814
1815 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1816 {
1817         if (!g_dbus_unregister_interface(connection, "/",
1818                                         DBUS_INTERFACE_OBJECT_MANAGER))
1819                 return FALSE;
1820
1821         root = NULL;
1822
1823         return TRUE;
1824 }
1825
1826 void g_dbus_set_flags(int flags)
1827 {
1828         global_flags = flags;
1829 }
1830
1831 int g_dbus_get_flags(void)
1832 {
1833         return global_flags;
1834 }