Imported Upstream version 1.24
[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                 if (reply != NULL)
263                         dbus_message_unref(reply);
264                 return DBUS_HANDLER_RESULT_HANDLED;
265         }
266
267         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
268                 if (reply == NULL)
269                         return DBUS_HANDLER_RESULT_HANDLED;
270         }
271
272         if (reply == NULL)
273                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
274
275         g_dbus_send_message(connection, reply);
276
277         return DBUS_HANDLER_RESULT_HANDLED;
278 }
279
280 static GDBusPendingReply next_pending = 1;
281 static GSList *pending_security = NULL;
282
283 static const GDBusSecurityTable *security_table = NULL;
284
285 void g_dbus_pending_success(DBusConnection *connection,
286                                         GDBusPendingReply pending)
287 {
288         GSList *list;
289
290         for (list = pending_security; list; list = list->next) {
291                 struct security_data *secdata = list->data;
292
293                 if (secdata->pending != pending)
294                         continue;
295
296                 pending_security = g_slist_remove(pending_security, secdata);
297
298                 process_message(connection, secdata->message,
299                                 secdata->method, secdata->iface_user_data);
300
301                 dbus_message_unref(secdata->message);
302                 g_free(secdata);
303                 return;
304         }
305 }
306
307 void g_dbus_pending_error_valist(DBusConnection *connection,
308                                 GDBusPendingReply pending, const char *name,
309                                         const char *format, va_list args)
310 {
311         GSList *list;
312
313         for (list = pending_security; list; list = list->next) {
314                 struct security_data *secdata = list->data;
315
316                 if (secdata->pending != pending)
317                         continue;
318
319                 pending_security = g_slist_remove(pending_security, secdata);
320
321                 g_dbus_send_error_valist(connection, secdata->message,
322                                                         name, format, args);
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                 break;
438         }
439
440         if (l == NULL)
441                 return NULL;
442
443         pending_property_set = g_slist_delete_link(pending_property_set, l);
444
445         return propdata;
446 }
447
448 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
449 {
450         struct property_data *propdata;
451
452         propdata = remove_pending_property_data(id);
453         if (propdata == NULL)
454                 return;
455
456         g_dbus_send_reply(propdata->conn, propdata->message,
457                                                         DBUS_TYPE_INVALID);
458         dbus_message_unref(propdata->message);
459         g_free(propdata);
460 }
461
462 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
463                                         const char *name, const char *format,
464                                         va_list args)
465 {
466         struct property_data *propdata;
467
468         propdata = remove_pending_property_data(id);
469         if (propdata == NULL)
470                 return;
471
472         g_dbus_send_error_valist(propdata->conn, propdata->message, name,
473                                                                 format, args);
474
475         dbus_message_unref(propdata->message);
476         g_free(propdata);
477 }
478
479 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
480                                                 const char *format, ...)
481 {
482         va_list args;
483
484         va_start(args, format);
485
486         g_dbus_pending_property_error_valist(id, name, format, args);
487
488         va_end(args);
489 }
490
491 static void reset_parent(gpointer data, gpointer user_data)
492 {
493         struct generic_data *child = data;
494         struct generic_data *parent = user_data;
495
496         child->parent = parent;
497 }
498
499 static void append_property(struct interface_data *iface,
500                         const GDBusPropertyTable *p, DBusMessageIter *dict)
501 {
502         DBusMessageIter entry, value;
503
504         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
505                                                                 &entry);
506         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
507         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
508                                                                 &value);
509
510         p->get(p, &value, iface->user_data);
511
512         dbus_message_iter_close_container(&entry, &value);
513         dbus_message_iter_close_container(dict, &entry);
514 }
515
516 static void append_properties(struct interface_data *data,
517                                                         DBusMessageIter *iter)
518 {
519         DBusMessageIter dict;
520         const GDBusPropertyTable *p;
521
522         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
523                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
524                                 DBUS_TYPE_STRING_AS_STRING
525                                 DBUS_TYPE_VARIANT_AS_STRING
526                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
527
528         for (p = data->properties; p && p->name; p++) {
529                 if (check_experimental(p->flags,
530                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
531                         continue;
532
533                 if (p->get == NULL)
534                         continue;
535
536                 if (p->exists != NULL && !p->exists(p, data->user_data))
537                         continue;
538
539                 append_property(data, p, &dict);
540         }
541
542         dbus_message_iter_close_container(iter, &dict);
543 }
544
545 static void append_interface(gpointer data, gpointer user_data)
546 {
547         struct interface_data *iface = data;
548         DBusMessageIter *array = user_data;
549         DBusMessageIter entry;
550
551         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
552                                                                 &entry);
553         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
554         append_properties(data, &entry);
555         dbus_message_iter_close_container(array, &entry);
556 }
557
558 static void emit_interfaces_added(struct generic_data *data)
559 {
560         DBusMessage *signal;
561         DBusMessageIter iter, array;
562
563         if (root == NULL || data == root)
564                 return;
565
566         signal = dbus_message_new_signal(root->path,
567                                         DBUS_INTERFACE_OBJECT_MANAGER,
568                                         "InterfacesAdded");
569         if (signal == NULL)
570                 return;
571
572         dbus_message_iter_init_append(signal, &iter);
573         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
574                                                                 &data->path);
575
576         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
577                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
578                                 DBUS_TYPE_STRING_AS_STRING
579                                 DBUS_TYPE_ARRAY_AS_STRING
580                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
581                                 DBUS_TYPE_STRING_AS_STRING
582                                 DBUS_TYPE_VARIANT_AS_STRING
583                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
584                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
585
586         g_slist_foreach(data->added, append_interface, &array);
587         g_slist_free(data->added);
588         data->added = NULL;
589
590         dbus_message_iter_close_container(&iter, &array);
591
592         /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
593         dbus_connection_send(data->conn, signal, NULL);
594         dbus_message_unref(signal);
595 }
596
597 static struct interface_data *find_interface(GSList *interfaces,
598                                                 const char *name)
599 {
600         GSList *list;
601
602         if (name == NULL)
603                 return NULL;
604
605         for (list = interfaces; list; list = list->next) {
606                 struct interface_data *iface = list->data;
607                 if (!strcmp(name, iface->name))
608                         return iface;
609         }
610
611         return NULL;
612 }
613
614 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
615                                                         DBusMessage *message)
616 {
617         const char *sig = dbus_message_get_signature(message);
618         const char *p = NULL;
619
620         for (; args && args->signature && *sig; args++) {
621                 p = args->signature;
622
623                 for (; *sig && *p; sig++, p++) {
624                         if (*p != *sig)
625                                 return FALSE;
626                 }
627         }
628
629         if (*sig || (p && *p) || (args && args->signature))
630                 return FALSE;
631
632         return TRUE;
633 }
634
635 static void add_pending(struct generic_data *data)
636 {
637         if (data->process_id > 0)
638                 return;
639
640         data->process_id = g_idle_add(process_changes, data);
641
642         pending = g_slist_append(pending, data);
643 }
644
645 static gboolean remove_interface(struct generic_data *data, const char *name)
646 {
647         struct interface_data *iface;
648
649         iface = find_interface(data->interfaces, name);
650         if (iface == NULL)
651                 return FALSE;
652
653         process_properties_from_interface(data, iface);
654
655         data->interfaces = g_slist_remove(data->interfaces, iface);
656
657         if (iface->destroy) {
658                 iface->destroy(iface->user_data);
659                 iface->user_data = NULL;
660         }
661
662         /*
663          * Interface being removed was just added, on the same mainloop
664          * iteration? Don't send any signal
665          */
666         if (g_slist_find(data->added, iface)) {
667                 data->added = g_slist_remove(data->added, iface);
668                 g_free(iface->name);
669                 g_free(iface);
670                 return TRUE;
671         }
672
673         if (data->parent == NULL) {
674                 g_free(iface->name);
675                 g_free(iface);
676                 return TRUE;
677         }
678
679         data->removed = g_slist_prepend(data->removed, iface->name);
680         g_free(iface);
681
682         add_pending(data);
683
684         return TRUE;
685 }
686
687 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
688                                                 const char *child_path)
689 {
690         struct generic_data *data = NULL, *child = NULL, *parent = NULL;
691         char *parent_path, *slash;
692
693         parent_path = g_strdup(child_path);
694         slash = strrchr(parent_path, '/');
695         if (slash == NULL)
696                 goto done;
697
698         if (slash == parent_path && parent_path[1] != '\0')
699                 parent_path[1] = '\0';
700         else
701                 *slash = '\0';
702
703         if (!strlen(parent_path))
704                 goto done;
705
706         if (dbus_connection_get_object_path_data(conn, parent_path,
707                                                         (void *) &data) == FALSE) {
708                 goto done;
709         }
710
711         parent = invalidate_parent_data(conn, parent_path);
712
713         if (data == NULL) {
714                 data = parent;
715                 if (data == NULL)
716                         goto done;
717         }
718
719         g_free(data->introspect);
720         data->introspect = NULL;
721
722         if (!dbus_connection_get_object_path_data(conn, child_path,
723                                                         (void *) &child))
724                 goto done;
725
726         if (child == NULL || g_slist_find(data->objects, child) != NULL)
727                 goto done;
728
729         data->objects = g_slist_prepend(data->objects, child);
730         child->parent = data;
731
732 done:
733         g_free(parent_path);
734         return data;
735 }
736
737 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
738                                                         const char *name)
739 {
740         const GDBusPropertyTable *p;
741
742         for (p = properties; p && p->name; p++) {
743                 if (strcmp(name, p->name) != 0)
744                         continue;
745
746                 if (check_experimental(p->flags,
747                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
748                         break;
749
750                 return p;
751         }
752
753         return NULL;
754 }
755
756 static DBusMessage *properties_get(DBusConnection *connection,
757                                         DBusMessage *message, void *user_data)
758 {
759         struct generic_data *data = user_data;
760         struct interface_data *iface;
761         const GDBusPropertyTable *property;
762         const char *interface, *name;
763         DBusMessageIter iter, value;
764         DBusMessage *reply;
765
766         if (!dbus_message_get_args(message, NULL,
767                                         DBUS_TYPE_STRING, &interface,
768                                         DBUS_TYPE_STRING, &name,
769                                         DBUS_TYPE_INVALID))
770                 return NULL;
771
772         iface = find_interface(data->interfaces, interface);
773         if (iface == NULL)
774                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
775                                 "No such interface '%s'", interface);
776
777         property = find_property(iface->properties, name);
778         if (property == NULL)
779                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
780                                 "No such property '%s'", name);
781
782         if (property->exists != NULL &&
783                         !property->exists(property, iface->user_data))
784                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
785                                         "No such property '%s'", name);
786
787         if (property->get == NULL)
788                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
789                                 "Property '%s' is not readable", name);
790
791         reply = dbus_message_new_method_return(message);
792         if (reply == NULL)
793                 return NULL;
794
795         dbus_message_iter_init_append(reply, &iter);
796         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
797                                                 property->type, &value);
798
799         if (!property->get(property, &value, iface->user_data)) {
800                 dbus_message_unref(reply);
801                 return NULL;
802         }
803
804         dbus_message_iter_close_container(&iter, &value);
805
806         return reply;
807 }
808
809 static DBusMessage *properties_get_all(DBusConnection *connection,
810                                         DBusMessage *message, void *user_data)
811 {
812         struct generic_data *data = user_data;
813         struct interface_data *iface;
814         const char *interface;
815         DBusMessageIter iter;
816         DBusMessage *reply;
817
818         if (!dbus_message_get_args(message, NULL,
819                                         DBUS_TYPE_STRING, &interface,
820                                         DBUS_TYPE_INVALID))
821                 return NULL;
822
823         iface = find_interface(data->interfaces, interface);
824         if (iface == NULL)
825                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
826                                         "No such interface '%s'", interface);
827
828         reply = dbus_message_new_method_return(message);
829         if (reply == NULL)
830                 return NULL;
831
832         dbus_message_iter_init_append(reply, &iter);
833
834         append_properties(iface, &iter);
835
836         return reply;
837 }
838
839 static DBusMessage *properties_set(DBusConnection *connection,
840                                         DBusMessage *message, void *user_data)
841 {
842         struct generic_data *data = user_data;
843         DBusMessageIter iter, sub;
844         struct interface_data *iface;
845         const GDBusPropertyTable *property;
846         const char *name, *interface;
847         struct property_data *propdata;
848         gboolean valid_signature;
849         char *signature;
850
851         if (!dbus_message_iter_init(message, &iter))
852                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
853                                                         "No arguments given");
854
855         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
856                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
857                                         "Invalid argument type: '%c'",
858                                         dbus_message_iter_get_arg_type(&iter));
859
860         dbus_message_iter_get_basic(&iter, &interface);
861         dbus_message_iter_next(&iter);
862
863         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
864                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
865                                         "Invalid argument type: '%c'",
866                                         dbus_message_iter_get_arg_type(&iter));
867
868         dbus_message_iter_get_basic(&iter, &name);
869         dbus_message_iter_next(&iter);
870
871         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
872                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
873                                         "Invalid argument type: '%c'",
874                                         dbus_message_iter_get_arg_type(&iter));
875
876         dbus_message_iter_recurse(&iter, &sub);
877
878         iface = find_interface(data->interfaces, interface);
879         if (iface == NULL)
880                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
881                                         "No such interface '%s'", interface);
882
883         property = find_property(iface->properties, name);
884         if (property == NULL)
885                 return g_dbus_create_error(message,
886                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
887                                                 "No such property '%s'", name);
888
889         if (property->set == NULL)
890                 return g_dbus_create_error(message,
891                                         DBUS_ERROR_PROPERTY_READ_ONLY,
892                                         "Property '%s' is not writable", name);
893
894         if (property->exists != NULL &&
895                         !property->exists(property, iface->user_data))
896                 return g_dbus_create_error(message,
897                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
898                                                 "No such property '%s'", name);
899
900         signature = dbus_message_iter_get_signature(&sub);
901         valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
902         dbus_free(signature);
903         if (!valid_signature)
904                 return g_dbus_create_error(message,
905                                         DBUS_ERROR_INVALID_SIGNATURE,
906                                         "Invalid signature for '%s'", name);
907
908         propdata = g_new(struct property_data, 1);
909         propdata->id = next_pending_property++;
910         propdata->message = dbus_message_ref(message);
911         propdata->conn = connection;
912         pending_property_set = g_slist_prepend(pending_property_set, propdata);
913
914         property->set(property, &sub, propdata->id, iface->user_data);
915
916         return NULL;
917 }
918
919 static const GDBusMethodTable properties_methods[] = {
920         { GDBUS_METHOD("Get",
921                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
922                         GDBUS_ARGS({ "value", "v" }),
923                         properties_get) },
924         { GDBUS_ASYNC_METHOD("Set",
925                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
926                                                         { "value", "v" }),
927                         NULL,
928                         properties_set) },
929         { GDBUS_METHOD("GetAll",
930                         GDBUS_ARGS({ "interface", "s" }),
931                         GDBUS_ARGS({ "properties", "a{sv}" }),
932                         properties_get_all) },
933         { }
934 };
935
936 static const GDBusSignalTable properties_signals[] = {
937         { GDBUS_SIGNAL("PropertiesChanged",
938                         GDBUS_ARGS({ "interface", "s" },
939                                         { "changed_properties", "a{sv}" },
940                                         { "invalidated_properties", "as"})) },
941         { }
942 };
943
944 static void append_name(gpointer data, gpointer user_data)
945 {
946         char *name = data;
947         DBusMessageIter *iter = user_data;
948
949         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
950 }
951
952 static void emit_interfaces_removed(struct generic_data *data)
953 {
954         DBusMessage *signal;
955         DBusMessageIter iter, array;
956
957         if (root == NULL || data == root)
958                 return;
959
960         signal = dbus_message_new_signal(root->path,
961                                         DBUS_INTERFACE_OBJECT_MANAGER,
962                                         "InterfacesRemoved");
963         if (signal == NULL)
964                 return;
965
966         dbus_message_iter_init_append(signal, &iter);
967         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
968                                                                 &data->path);
969         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
970                                         DBUS_TYPE_STRING_AS_STRING, &array);
971
972         g_slist_foreach(data->removed, append_name, &array);
973         g_slist_free_full(data->removed, g_free);
974         data->removed = NULL;
975
976         dbus_message_iter_close_container(&iter, &array);
977
978         /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
979         dbus_connection_send(data->conn, signal, NULL);
980         dbus_message_unref(signal);
981 }
982
983 static void remove_pending(struct generic_data *data)
984 {
985         if (data->process_id > 0) {
986                 g_source_remove(data->process_id);
987                 data->process_id = 0;
988         }
989
990         pending = g_slist_remove(pending, data);
991 }
992
993 static gboolean process_changes(gpointer user_data)
994 {
995         struct generic_data *data = user_data;
996
997         remove_pending(data);
998
999         if (data->added != NULL)
1000                 emit_interfaces_added(data);
1001
1002         /* Flush pending properties */
1003         if (data->pending_prop == TRUE)
1004                 process_property_changes(data);
1005
1006         if (data->removed != NULL)
1007                 emit_interfaces_removed(data);
1008
1009         data->process_id = 0;
1010
1011         return FALSE;
1012 }
1013
1014 static void generic_unregister(DBusConnection *connection, void *user_data)
1015 {
1016         struct generic_data *data = user_data;
1017         struct generic_data *parent = data->parent;
1018
1019         if (parent != NULL)
1020                 parent->objects = g_slist_remove(parent->objects, data);
1021
1022         if (data->process_id > 0) {
1023                 g_source_remove(data->process_id);
1024                 data->process_id = 0;
1025                 process_changes(data);
1026         }
1027
1028         g_slist_foreach(data->objects, reset_parent, data->parent);
1029         g_slist_free(data->objects);
1030
1031         dbus_connection_unref(data->conn);
1032         g_free(data->introspect);
1033         g_free(data->path);
1034         g_free(data);
1035 }
1036
1037 static DBusHandlerResult generic_message(DBusConnection *connection,
1038                                         DBusMessage *message, void *user_data)
1039 {
1040         struct generic_data *data = user_data;
1041         struct interface_data *iface;
1042         const GDBusMethodTable *method;
1043         const char *interface;
1044
1045         interface = dbus_message_get_interface(message);
1046
1047         iface = find_interface(data->interfaces, interface);
1048         if (iface == NULL)
1049                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1050
1051         for (method = iface->methods; method &&
1052                         method->name && method->function; method++) {
1053
1054                 if (dbus_message_is_method_call(message, iface->name,
1055                                                         method->name) == FALSE)
1056                         continue;
1057
1058                 if (check_experimental(method->flags,
1059                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1060                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1061
1062                 if (g_dbus_args_have_signature(method->in_args,
1063                                                         message) == FALSE)
1064                         continue;
1065
1066                 if (check_privilege(connection, message, method,
1067                                                 iface->user_data) == TRUE)
1068                         return DBUS_HANDLER_RESULT_HANDLED;
1069
1070                 return process_message(connection, message, method,
1071                                                         iface->user_data);
1072         }
1073
1074         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1075 }
1076
1077 static DBusObjectPathVTable generic_table = {
1078         .unregister_function    = generic_unregister,
1079         .message_function       = generic_message,
1080 };
1081
1082 static const GDBusMethodTable introspect_methods[] = {
1083         { GDBUS_METHOD("Introspect", NULL,
1084                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1085         { }
1086 };
1087
1088 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1089 {
1090         DBusMessageIter array;
1091         GSList *l;
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         for (l = data->interfaces; l != NULL; l = l->next) {
1104                 if (g_slist_find(data->added, l->data))
1105                         continue;
1106
1107                 append_interface(l->data, &array);
1108         }
1109
1110         dbus_message_iter_close_container(iter, &array);
1111 }
1112
1113 static void append_object(gpointer data, gpointer user_data)
1114 {
1115         struct generic_data *child = data;
1116         DBusMessageIter *array = user_data;
1117         DBusMessageIter entry;
1118
1119         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1120                                                                 &entry);
1121         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1122                                                                 &child->path);
1123         append_interfaces(child, &entry);
1124         dbus_message_iter_close_container(array, &entry);
1125
1126         g_slist_foreach(child->objects, append_object, user_data);
1127 }
1128
1129 static DBusMessage *get_objects(DBusConnection *connection,
1130                                 DBusMessage *message, void *user_data)
1131 {
1132         struct generic_data *data = user_data;
1133         DBusMessage *reply;
1134         DBusMessageIter iter;
1135         DBusMessageIter array;
1136
1137         reply = dbus_message_new_method_return(message);
1138         if (reply == NULL)
1139                 return NULL;
1140
1141         dbus_message_iter_init_append(reply, &iter);
1142
1143         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1144                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1145                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1146                                         DBUS_TYPE_ARRAY_AS_STRING
1147                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1148                                         DBUS_TYPE_STRING_AS_STRING
1149                                         DBUS_TYPE_ARRAY_AS_STRING
1150                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1151                                         DBUS_TYPE_STRING_AS_STRING
1152                                         DBUS_TYPE_VARIANT_AS_STRING
1153                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1154                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1155                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1156                                         &array);
1157
1158         g_slist_foreach(data->objects, append_object, &array);
1159
1160         dbus_message_iter_close_container(&iter, &array);
1161
1162         return reply;
1163 }
1164
1165 static const GDBusMethodTable manager_methods[] = {
1166         { GDBUS_METHOD("GetManagedObjects", NULL,
1167                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1168         { }
1169 };
1170
1171 static const GDBusSignalTable manager_signals[] = {
1172         { GDBUS_SIGNAL("InterfacesAdded",
1173                 GDBUS_ARGS({ "object", "o" },
1174                                 { "interfaces", "a{sa{sv}}" })) },
1175         { GDBUS_SIGNAL("InterfacesRemoved",
1176                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1177         { }
1178 };
1179
1180 static gboolean add_interface(struct generic_data *data,
1181                                 const char *name,
1182                                 const GDBusMethodTable *methods,
1183                                 const GDBusSignalTable *signals,
1184                                 const GDBusPropertyTable *properties,
1185                                 void *user_data,
1186                                 GDBusDestroyFunction destroy)
1187 {
1188         struct interface_data *iface;
1189         const GDBusMethodTable *method;
1190         const GDBusSignalTable *signal;
1191         const GDBusPropertyTable *property;
1192
1193         for (method = methods; method && method->name; method++) {
1194                 if (!check_experimental(method->flags,
1195                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1196                         goto done;
1197         }
1198
1199         for (signal = signals; signal && signal->name; signal++) {
1200                 if (!check_experimental(signal->flags,
1201                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1202                         goto done;
1203         }
1204
1205         for (property = properties; property && property->name; property++) {
1206                 if (!check_experimental(property->flags,
1207                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1208                         goto done;
1209         }
1210
1211         /* Nothing to register */
1212         return FALSE;
1213
1214 done:
1215         iface = g_new0(struct interface_data, 1);
1216         iface->name = g_strdup(name);
1217         iface->methods = methods;
1218         iface->signals = signals;
1219         iface->properties = properties;
1220         iface->user_data = user_data;
1221         iface->destroy = destroy;
1222
1223         data->interfaces = g_slist_append(data->interfaces, iface);
1224         if (data->parent == NULL)
1225                 return TRUE;
1226
1227         data->added = g_slist_append(data->added, iface);
1228
1229         add_pending(data);
1230
1231         return TRUE;
1232 }
1233
1234 static struct generic_data *object_path_ref(DBusConnection *connection,
1235                                                         const char *path)
1236 {
1237         struct generic_data *data;
1238
1239         if (dbus_connection_get_object_path_data(connection, path,
1240                                                 (void *) &data) == TRUE) {
1241                 if (data != NULL) {
1242                         data->refcount++;
1243                         return data;
1244                 }
1245         }
1246
1247         data = g_new0(struct generic_data, 1);
1248         data->conn = dbus_connection_ref(connection);
1249         data->path = g_strdup(path);
1250         data->refcount = 1;
1251
1252         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1253
1254         if (!dbus_connection_register_object_path(connection, path,
1255                                                 &generic_table, data)) {
1256                 dbus_connection_unref(data->conn);
1257                 g_free(data->path);
1258                 g_free(data->introspect);
1259                 g_free(data);
1260                 return NULL;
1261         }
1262
1263         invalidate_parent_data(connection, path);
1264
1265         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1266                                                 NULL, NULL, data, NULL);
1267
1268         return data;
1269 }
1270
1271 static void object_path_unref(DBusConnection *connection, const char *path)
1272 {
1273         struct generic_data *data = NULL;
1274
1275         if (dbus_connection_get_object_path_data(connection, path,
1276                                                 (void *) &data) == FALSE)
1277                 return;
1278
1279         if (data == NULL)
1280                 return;
1281
1282         data->refcount--;
1283
1284         if (data->refcount > 0)
1285                 return;
1286
1287         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1288         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1289
1290         invalidate_parent_data(data->conn, data->path);
1291
1292         dbus_connection_unregister_object_path(data->conn, data->path);
1293 }
1294
1295 static gboolean check_signal(DBusConnection *conn, const char *path,
1296                                 const char *interface, const char *name,
1297                                 const GDBusArgInfo **args)
1298 {
1299         struct generic_data *data = NULL;
1300         struct interface_data *iface;
1301         const GDBusSignalTable *signal;
1302
1303         *args = NULL;
1304         if (!dbus_connection_get_object_path_data(conn, path,
1305                                         (void *) &data) || data == NULL) {
1306                 error("dbus_connection_emit_signal: path %s isn't registered",
1307                                 path);
1308                 return FALSE;
1309         }
1310
1311         iface = find_interface(data->interfaces, interface);
1312         if (iface == NULL) {
1313                 error("dbus_connection_emit_signal: %s does not implement %s",
1314                                 path, interface);
1315                 return FALSE;
1316         }
1317
1318         for (signal = iface->signals; signal && signal->name; signal++) {
1319                 if (strcmp(signal->name, name) != 0)
1320                         continue;
1321
1322                 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1323                         const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1324                         if (g_strcmp0(env, "1") != 0)
1325                                 break;
1326                 }
1327
1328                 *args = signal->args;
1329                 return TRUE;
1330         }
1331
1332         error("No signal named %s on interface %s", name, interface);
1333         return FALSE;
1334 }
1335
1336 gboolean g_dbus_register_interface(DBusConnection *connection,
1337                                         const char *path, const char *name,
1338                                         const GDBusMethodTable *methods,
1339                                         const GDBusSignalTable *signals,
1340                                         const GDBusPropertyTable *properties,
1341                                         void *user_data,
1342                                         GDBusDestroyFunction destroy)
1343 {
1344         struct generic_data *data;
1345
1346         data = object_path_ref(connection, path);
1347         if (data == NULL)
1348                 return FALSE;
1349
1350         if (find_interface(data->interfaces, name)) {
1351                 object_path_unref(connection, path);
1352                 return FALSE;
1353         }
1354
1355         if (!add_interface(data, name, methods, signals, properties, user_data,
1356                                                                 destroy)) {
1357                 object_path_unref(connection, path);
1358                 return FALSE;
1359         }
1360
1361         if (properties != NULL && !find_interface(data->interfaces,
1362                                                 DBUS_INTERFACE_PROPERTIES))
1363                 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1364                                 properties_methods, properties_signals, NULL,
1365                                 data, NULL);
1366
1367         g_free(data->introspect);
1368         data->introspect = NULL;
1369
1370         return TRUE;
1371 }
1372
1373 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1374                                         const char *path, const char *name)
1375 {
1376         struct generic_data *data = NULL;
1377
1378         if (path == NULL)
1379                 return FALSE;
1380
1381         if (dbus_connection_get_object_path_data(connection, path,
1382                                                 (void *) &data) == FALSE)
1383                 return FALSE;
1384
1385         if (data == NULL)
1386                 return FALSE;
1387
1388         if (remove_interface(data, name) == FALSE)
1389                 return FALSE;
1390
1391         g_free(data->introspect);
1392         data->introspect = NULL;
1393
1394         object_path_unref(connection, data->path);
1395
1396         return TRUE;
1397 }
1398
1399 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1400 {
1401         if (security_table != NULL)
1402                 return FALSE;
1403
1404         security_table = security;
1405
1406         return TRUE;
1407 }
1408
1409 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1410 {
1411         security_table = NULL;
1412
1413         return TRUE;
1414 }
1415
1416 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1417                                         const char *format, va_list args)
1418 {
1419         char str[1024];
1420
1421         vsnprintf(str, sizeof(str), format, args);
1422
1423         return dbus_message_new_error(message, name, str);
1424 }
1425
1426 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1427                                                 const char *format, ...)
1428 {
1429         va_list args;
1430         DBusMessage *reply;
1431
1432         va_start(args, format);
1433
1434         reply = g_dbus_create_error_valist(message, name, format, args);
1435
1436         va_end(args);
1437
1438         return reply;
1439 }
1440
1441 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1442                                                 int type, va_list args)
1443 {
1444         DBusMessage *reply;
1445
1446         reply = dbus_message_new_method_return(message);
1447         if (reply == NULL)
1448                 return NULL;
1449
1450         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1451                 dbus_message_unref(reply);
1452                 return NULL;
1453         }
1454
1455         return reply;
1456 }
1457
1458 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1459 {
1460         va_list args;
1461         DBusMessage *reply;
1462
1463         va_start(args, type);
1464
1465         reply = g_dbus_create_reply_valist(message, type, args);
1466
1467         va_end(args);
1468
1469         return reply;
1470 }
1471
1472 static void g_dbus_flush(DBusConnection *connection)
1473 {
1474         GSList *l;
1475
1476         for (l = pending; l;) {
1477                 struct generic_data *data = l->data;
1478
1479                 l = l->next;
1480                 if (data->conn != connection)
1481                         continue;
1482
1483                 process_changes(data);
1484         }
1485 }
1486
1487 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1488 {
1489         dbus_bool_t result = FALSE;
1490
1491         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1492                 dbus_message_set_no_reply(message, TRUE);
1493         else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1494                 const char *path = dbus_message_get_path(message);
1495                 const char *interface = dbus_message_get_interface(message);
1496                 const char *name = dbus_message_get_member(message);
1497                 const GDBusArgInfo *args;
1498
1499                 if (!check_signal(connection, path, interface, name, &args))
1500                         goto out;
1501         }
1502
1503         /* Flush pending signal to guarantee message order */
1504         g_dbus_flush(connection);
1505
1506         result = dbus_connection_send(connection, message, NULL);
1507
1508 out:
1509         dbus_message_unref(message);
1510
1511         return result;
1512 }
1513
1514 gboolean g_dbus_send_message_with_reply(DBusConnection *connection,
1515                                         DBusMessage *message,
1516                                         DBusPendingCall **call, int timeout)
1517 {
1518         dbus_bool_t ret;
1519
1520         /* Flush pending signal to guarantee message order */
1521         g_dbus_flush(connection);
1522
1523         ret = dbus_connection_send_with_reply(connection, message, call,
1524                                                                 timeout);
1525
1526         if (ret == TRUE && call != NULL && *call == NULL) {
1527                 error("Unable to send message (passing fd blocked?)");
1528                 return FALSE;
1529         }
1530
1531         return ret;
1532 }
1533
1534 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1535                                         DBusMessage *message, const char *name,
1536                                         const char *format, va_list args)
1537 {
1538         DBusMessage *error;
1539         char str[1024];
1540
1541         vsnprintf(str, sizeof(str), format, args);
1542
1543         error = dbus_message_new_error(message, name, str);
1544         if (error == NULL)
1545                 return FALSE;
1546
1547         return g_dbus_send_message(connection, error);
1548 }
1549
1550 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1551                                 const char *name, const char *format, ...)
1552 {
1553         va_list args;
1554         gboolean result;
1555
1556         va_start(args, format);
1557
1558         result = g_dbus_send_error_valist(connection, message, name,
1559                                                         format, args);
1560
1561         va_end(args);
1562
1563         return result;
1564 }
1565
1566 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1567                                 DBusMessage *message, int type, va_list args)
1568 {
1569         DBusMessage *reply;
1570
1571         reply = dbus_message_new_method_return(message);
1572         if (reply == NULL)
1573                 return FALSE;
1574
1575         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1576                 dbus_message_unref(reply);
1577                 return FALSE;
1578         }
1579
1580         return g_dbus_send_message(connection, reply);
1581 }
1582
1583 gboolean g_dbus_send_reply(DBusConnection *connection,
1584                                 DBusMessage *message, int type, ...)
1585 {
1586         va_list args;
1587         gboolean result;
1588
1589         va_start(args, type);
1590
1591         result = g_dbus_send_reply_valist(connection, message, type, args);
1592
1593         va_end(args);
1594
1595         return result;
1596 }
1597
1598 gboolean g_dbus_emit_signal(DBusConnection *connection,
1599                                 const char *path, const char *interface,
1600                                 const char *name, int type, ...)
1601 {
1602         va_list args;
1603         gboolean result;
1604
1605         va_start(args, type);
1606
1607         result = g_dbus_emit_signal_valist(connection, path, interface,
1608                                                         name, type, args);
1609
1610         va_end(args);
1611
1612         return result;
1613 }
1614
1615 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1616                                 const char *path, const char *interface,
1617                                 const char *name, int type, va_list args)
1618 {
1619         DBusMessage *signal;
1620         dbus_bool_t ret;
1621         const GDBusArgInfo *args_info;
1622
1623         if (!check_signal(connection, path, interface, name, &args_info))
1624                 return FALSE;
1625
1626         signal = dbus_message_new_signal(path, interface, name);
1627         if (signal == NULL) {
1628                 error("Unable to allocate new %s.%s signal", interface,  name);
1629                 return FALSE;
1630         }
1631
1632         ret = dbus_message_append_args_valist(signal, type, args);
1633         if (!ret)
1634                 goto fail;
1635
1636         if (g_dbus_args_have_signature(args_info, signal) == FALSE) {
1637                 error("%s.%s: got unexpected signature '%s'", interface, name,
1638                                         dbus_message_get_signature(signal));
1639                 ret = FALSE;
1640                 goto fail;
1641         }
1642
1643         return g_dbus_send_message(connection, signal);
1644
1645 fail:
1646         dbus_message_unref(signal);
1647
1648         return ret;
1649 }
1650
1651 static void process_properties_from_interface(struct generic_data *data,
1652                                                 struct interface_data *iface)
1653 {
1654         GSList *l;
1655         DBusMessage *signal;
1656         DBusMessageIter iter, dict, array;
1657         GSList *invalidated;
1658
1659         data->pending_prop = FALSE;
1660
1661         if (iface->pending_prop == NULL)
1662                 return;
1663
1664         signal = dbus_message_new_signal(data->path,
1665                         DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1666         if (signal == NULL) {
1667                 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1668                                                 ".PropertiesChanged signal");
1669                 return;
1670         }
1671
1672         iface->pending_prop = g_slist_reverse(iface->pending_prop);
1673
1674         dbus_message_iter_init_append(signal, &iter);
1675         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1676         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1677                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1678                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1679                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1680
1681         invalidated = NULL;
1682
1683         for (l = iface->pending_prop; l != NULL; l = l->next) {
1684                 GDBusPropertyTable *p = l->data;
1685
1686                 if (p->get == NULL)
1687                         continue;
1688
1689                 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1690                         invalidated = g_slist_prepend(invalidated, p);
1691                         continue;
1692                 }
1693
1694                 append_property(iface, p, &dict);
1695         }
1696
1697         dbus_message_iter_close_container(&iter, &dict);
1698
1699         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1700                                 DBUS_TYPE_STRING_AS_STRING, &array);
1701         for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1702                 GDBusPropertyTable *p = l->data;
1703
1704                 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1705                                                                 &p->name);
1706         }
1707         g_slist_free(invalidated);
1708         dbus_message_iter_close_container(&iter, &array);
1709
1710         g_slist_free(iface->pending_prop);
1711         iface->pending_prop = NULL;
1712
1713         /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
1714         dbus_connection_send(data->conn, signal, NULL);
1715         dbus_message_unref(signal);
1716 }
1717
1718 static void process_property_changes(struct generic_data *data)
1719 {
1720         GSList *l;
1721
1722         for (l = data->interfaces; l != NULL; l = l->next) {
1723                 struct interface_data *iface = l->data;
1724
1725                 process_properties_from_interface(data, iface);
1726         }
1727 }
1728
1729 void g_dbus_emit_property_changed(DBusConnection *connection,
1730                                 const char *path, const char *interface,
1731                                 const char *name)
1732 {
1733         const GDBusPropertyTable *property;
1734         struct generic_data *data;
1735         struct interface_data *iface;
1736
1737         if (path == NULL)
1738                 return;
1739
1740         if (!dbus_connection_get_object_path_data(connection, path,
1741                                         (void **) &data) || data == NULL)
1742                 return;
1743
1744         iface = find_interface(data->interfaces, interface);
1745         if (iface == NULL)
1746                 return;
1747
1748         /*
1749          * If ObjectManager is attached, don't emit property changed if
1750          * interface is not yet published
1751          */
1752         if (root && g_slist_find(data->added, iface))
1753                 return;
1754
1755         property = find_property(iface->properties, name);
1756         if (property == NULL) {
1757                 error("Could not find property %s in %p", name,
1758                                                         iface->properties);
1759                 return;
1760         }
1761
1762         if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1763                 return;
1764
1765         data->pending_prop = TRUE;
1766         iface->pending_prop = g_slist_prepend(iface->pending_prop,
1767                                                 (void *) property);
1768
1769         add_pending(data);
1770 }
1771
1772 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1773                                 const char *interface, DBusMessageIter *iter)
1774 {
1775         struct generic_data *data;
1776         struct interface_data *iface;
1777
1778         if (path == NULL)
1779                 return FALSE;
1780
1781         if (!dbus_connection_get_object_path_data(connection, path,
1782                                         (void **) &data) || data == NULL)
1783                 return FALSE;
1784
1785         iface = find_interface(data->interfaces, interface);
1786         if (iface == NULL)
1787                 return FALSE;
1788
1789         append_properties(iface, iter);
1790
1791         return TRUE;
1792 }
1793
1794 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1795 {
1796         struct generic_data *data;
1797
1798         data = object_path_ref(connection, "/");
1799         if (data == NULL)
1800                 return FALSE;
1801
1802         add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1803                                         manager_methods, manager_signals,
1804                                         NULL, data, NULL);
1805         root = data;
1806
1807         return TRUE;
1808 }
1809
1810 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1811 {
1812         if (!g_dbus_unregister_interface(connection, "/",
1813                                         DBUS_INTERFACE_OBJECT_MANAGER))
1814                 return FALSE;
1815
1816         root = NULL;
1817
1818         return TRUE;
1819 }
1820
1821 void g_dbus_set_flags(int flags)
1822 {
1823         global_flags = flags;
1824 }