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