gdbus: Always unreference the message in g_dbus_send_message()
[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         gboolean valid_signature;
850         char *signature;
851
852         if (!dbus_message_iter_init(message, &iter))
853                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
854                                                         "No arguments given");
855
856         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
857                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
858                                         "Invalid argument type: '%c'",
859                                         dbus_message_iter_get_arg_type(&iter));
860
861         dbus_message_iter_get_basic(&iter, &interface);
862         dbus_message_iter_next(&iter);
863
864         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
865                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
866                                         "Invalid argument type: '%c'",
867                                         dbus_message_iter_get_arg_type(&iter));
868
869         dbus_message_iter_get_basic(&iter, &name);
870         dbus_message_iter_next(&iter);
871
872         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
873                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
874                                         "Invalid argument type: '%c'",
875                                         dbus_message_iter_get_arg_type(&iter));
876
877         dbus_message_iter_recurse(&iter, &sub);
878
879         iface = find_interface(data->interfaces, interface);
880         if (iface == NULL)
881                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
882                                         "No such interface '%s'", interface);
883
884         property = find_property(iface->properties, name);
885         if (property == NULL)
886                 return g_dbus_create_error(message,
887                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
888                                                 "No such property '%s'", name);
889
890         if (property->set == NULL)
891                 return g_dbus_create_error(message,
892                                         DBUS_ERROR_PROPERTY_READ_ONLY,
893                                         "Property '%s' is not writable", name);
894
895         if (property->exists != NULL &&
896                         !property->exists(property, iface->user_data))
897                 return g_dbus_create_error(message,
898                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
899                                                 "No such property '%s'", name);
900
901         signature = dbus_message_iter_get_signature(&sub);
902         valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
903         dbus_free(signature);
904         if (!valid_signature)
905                 return g_dbus_create_error(message,
906                                         DBUS_ERROR_INVALID_SIGNATURE,
907                                         "Invalid signature for '%s'", name);
908
909         propdata = g_new(struct property_data, 1);
910         propdata->id = next_pending_property++;
911         propdata->message = dbus_message_ref(message);
912         propdata->conn = connection;
913         pending_property_set = g_slist_prepend(pending_property_set, propdata);
914
915         property->set(property, &sub, propdata->id, iface->user_data);
916
917         return NULL;
918 }
919
920 static const GDBusMethodTable properties_methods[] = {
921         { GDBUS_METHOD("Get",
922                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
923                         GDBUS_ARGS({ "value", "v" }),
924                         properties_get) },
925         { GDBUS_ASYNC_METHOD("Set",
926                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
927                                                         { "value", "v" }),
928                         NULL,
929                         properties_set) },
930         { GDBUS_METHOD("GetAll",
931                         GDBUS_ARGS({ "interface", "s" }),
932                         GDBUS_ARGS({ "properties", "a{sv}" }),
933                         properties_get_all) },
934         { }
935 };
936
937 static const GDBusSignalTable properties_signals[] = {
938         { GDBUS_SIGNAL("PropertiesChanged",
939                         GDBUS_ARGS({ "interface", "s" },
940                                         { "changed_properties", "a{sv}" },
941                                         { "invalidated_properties", "as"})) },
942         { }
943 };
944
945 static void append_name(gpointer data, gpointer user_data)
946 {
947         char *name = data;
948         DBusMessageIter *iter = user_data;
949
950         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
951 }
952
953 static void emit_interfaces_removed(struct generic_data *data)
954 {
955         DBusMessage *signal;
956         DBusMessageIter iter, array;
957
958         if (root == NULL || data == root)
959                 return;
960
961         signal = dbus_message_new_signal(root->path,
962                                         DBUS_INTERFACE_OBJECT_MANAGER,
963                                         "InterfacesRemoved");
964         if (signal == NULL)
965                 return;
966
967         dbus_message_iter_init_append(signal, &iter);
968         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
969                                                                 &data->path);
970         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
971                                         DBUS_TYPE_STRING_AS_STRING, &array);
972
973         g_slist_foreach(data->removed, append_name, &array);
974         g_slist_free_full(data->removed, g_free);
975         data->removed = NULL;
976
977         dbus_message_iter_close_container(&iter, &array);
978
979         g_dbus_send_message(data->conn, signal);
980 }
981
982 static gboolean process_changes(gpointer user_data)
983 {
984         struct generic_data *data = user_data;
985
986         data->process_id = 0;
987
988         if (data->added != NULL)
989                 emit_interfaces_added(data);
990
991         /* Flush pending properties */
992         if (data->pending_prop == TRUE)
993                 process_property_changes(data);
994
995         if (data->removed != NULL)
996                 emit_interfaces_removed(data);
997
998         return FALSE;
999 }
1000
1001 static void generic_unregister(DBusConnection *connection, void *user_data)
1002 {
1003         struct generic_data *data = user_data;
1004         struct generic_data *parent = data->parent;
1005
1006         if (parent != NULL)
1007                 parent->objects = g_slist_remove(parent->objects, data);
1008
1009         if (data->process_id > 0) {
1010                 g_source_remove(data->process_id);
1011                 process_changes(data);
1012         }
1013
1014         g_slist_foreach(data->objects, reset_parent, data->parent);
1015         g_slist_free(data->objects);
1016
1017         dbus_connection_unref(data->conn);
1018         g_free(data->introspect);
1019         g_free(data->path);
1020         g_free(data);
1021 }
1022
1023 static DBusHandlerResult generic_message(DBusConnection *connection,
1024                                         DBusMessage *message, void *user_data)
1025 {
1026         struct generic_data *data = user_data;
1027         struct interface_data *iface;
1028         const GDBusMethodTable *method;
1029         const char *interface;
1030
1031         interface = dbus_message_get_interface(message);
1032
1033         iface = find_interface(data->interfaces, interface);
1034         if (iface == NULL)
1035                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1036
1037         for (method = iface->methods; method &&
1038                         method->name && method->function; method++) {
1039
1040                 if (dbus_message_is_method_call(message, iface->name,
1041                                                         method->name) == FALSE)
1042                         continue;
1043
1044                 if (check_experimental(method->flags,
1045                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1046                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1047
1048                 if (g_dbus_args_have_signature(method->in_args,
1049                                                         message) == FALSE)
1050                         continue;
1051
1052                 if (check_privilege(connection, message, method,
1053                                                 iface->user_data) == TRUE)
1054                         return DBUS_HANDLER_RESULT_HANDLED;
1055
1056                 return process_message(connection, message, method,
1057                                                         iface->user_data);
1058         }
1059
1060         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1061 }
1062
1063 static DBusObjectPathVTable generic_table = {
1064         .unregister_function    = generic_unregister,
1065         .message_function       = generic_message,
1066 };
1067
1068 static const GDBusMethodTable introspect_methods[] = {
1069         { GDBUS_METHOD("Introspect", NULL,
1070                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1071         { }
1072 };
1073
1074 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1075 {
1076         DBusMessageIter array;
1077         GSList *l;
1078
1079         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1080                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1081                                 DBUS_TYPE_STRING_AS_STRING
1082                                 DBUS_TYPE_ARRAY_AS_STRING
1083                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1084                                 DBUS_TYPE_STRING_AS_STRING
1085                                 DBUS_TYPE_VARIANT_AS_STRING
1086                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1087                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1088
1089         for (l = data->interfaces; l != NULL; l = l->next) {
1090                 if (g_slist_find(data->added, l->data))
1091                         continue;
1092
1093                 append_interface(l->data, &array);
1094         }
1095
1096         dbus_message_iter_close_container(iter, &array);
1097 }
1098
1099 static void append_object(gpointer data, gpointer user_data)
1100 {
1101         struct generic_data *child = data;
1102         DBusMessageIter *array = user_data;
1103         DBusMessageIter entry;
1104
1105         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1106                                                                 &entry);
1107         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1108                                                                 &child->path);
1109         append_interfaces(child, &entry);
1110         dbus_message_iter_close_container(array, &entry);
1111
1112         g_slist_foreach(child->objects, append_object, user_data);
1113 }
1114
1115 static DBusMessage *get_objects(DBusConnection *connection,
1116                                 DBusMessage *message, void *user_data)
1117 {
1118         struct generic_data *data = user_data;
1119         DBusMessage *reply;
1120         DBusMessageIter iter;
1121         DBusMessageIter array;
1122
1123         reply = dbus_message_new_method_return(message);
1124         if (reply == NULL)
1125                 return NULL;
1126
1127         dbus_message_iter_init_append(reply, &iter);
1128
1129         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1130                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1131                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1132                                         DBUS_TYPE_ARRAY_AS_STRING
1133                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1134                                         DBUS_TYPE_STRING_AS_STRING
1135                                         DBUS_TYPE_ARRAY_AS_STRING
1136                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1137                                         DBUS_TYPE_STRING_AS_STRING
1138                                         DBUS_TYPE_VARIANT_AS_STRING
1139                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1140                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1141                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1142                                         &array);
1143
1144         g_slist_foreach(data->objects, append_object, &array);
1145
1146         dbus_message_iter_close_container(&iter, &array);
1147
1148         return reply;
1149 }
1150
1151 static const GDBusMethodTable manager_methods[] = {
1152         { GDBUS_METHOD("GetManagedObjects", NULL,
1153                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1154         { }
1155 };
1156
1157 static const GDBusSignalTable manager_signals[] = {
1158         { GDBUS_SIGNAL("InterfacesAdded",
1159                 GDBUS_ARGS({ "object", "o" },
1160                                 { "interfaces", "a{sa{sv}}" })) },
1161         { GDBUS_SIGNAL("InterfacesRemoved",
1162                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1163         { }
1164 };
1165
1166 static gboolean add_interface(struct generic_data *data,
1167                                 const char *name,
1168                                 const GDBusMethodTable *methods,
1169                                 const GDBusSignalTable *signals,
1170                                 const GDBusPropertyTable *properties,
1171                                 void *user_data,
1172                                 GDBusDestroyFunction destroy)
1173 {
1174         struct interface_data *iface;
1175         const GDBusMethodTable *method;
1176         const GDBusSignalTable *signal;
1177         const GDBusPropertyTable *property;
1178
1179         for (method = methods; method && method->name; method++) {
1180                 if (!check_experimental(method->flags,
1181                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1182                         goto done;
1183         }
1184
1185         for (signal = signals; signal && signal->name; signal++) {
1186                 if (!check_experimental(signal->flags,
1187                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1188                         goto done;
1189         }
1190
1191         for (property = properties; property && property->name; property++) {
1192                 if (!check_experimental(property->flags,
1193                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1194                         goto done;
1195         }
1196
1197         /* Nothing to register */
1198         return FALSE;
1199
1200 done:
1201         iface = g_new0(struct interface_data, 1);
1202         iface->name = g_strdup(name);
1203         iface->methods = methods;
1204         iface->signals = signals;
1205         iface->properties = properties;
1206         iface->user_data = user_data;
1207         iface->destroy = destroy;
1208
1209         data->interfaces = g_slist_append(data->interfaces, iface);
1210         if (data->parent == NULL)
1211                 return TRUE;
1212
1213         data->added = g_slist_append(data->added, iface);
1214         if (data->process_id > 0)
1215                 return TRUE;
1216
1217         data->process_id = g_idle_add(process_changes, data);
1218
1219         return TRUE;
1220 }
1221
1222 static struct generic_data *object_path_ref(DBusConnection *connection,
1223                                                         const char *path)
1224 {
1225         struct generic_data *data;
1226
1227         if (dbus_connection_get_object_path_data(connection, path,
1228                                                 (void *) &data) == TRUE) {
1229                 if (data != NULL) {
1230                         data->refcount++;
1231                         return data;
1232                 }
1233         }
1234
1235         data = g_new0(struct generic_data, 1);
1236         data->conn = dbus_connection_ref(connection);
1237         data->path = g_strdup(path);
1238         data->refcount = 1;
1239
1240         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1241
1242         if (!dbus_connection_register_object_path(connection, path,
1243                                                 &generic_table, data)) {
1244                 g_free(data->introspect);
1245                 g_free(data);
1246                 return NULL;
1247         }
1248
1249         invalidate_parent_data(connection, path);
1250
1251         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1252                                                 NULL, NULL, data, NULL);
1253
1254         return data;
1255 }
1256
1257 static void object_path_unref(DBusConnection *connection, const char *path)
1258 {
1259         struct generic_data *data = NULL;
1260
1261         if (dbus_connection_get_object_path_data(connection, path,
1262                                                 (void *) &data) == FALSE)
1263                 return;
1264
1265         if (data == NULL)
1266                 return;
1267
1268         data->refcount--;
1269
1270         if (data->refcount > 0)
1271                 return;
1272
1273         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1274         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1275
1276         invalidate_parent_data(data->conn, data->path);
1277
1278         dbus_connection_unregister_object_path(data->conn, data->path);
1279 }
1280
1281 static gboolean check_signal(DBusConnection *conn, const char *path,
1282                                 const char *interface, const char *name,
1283                                 const GDBusArgInfo **args)
1284 {
1285         struct generic_data *data = NULL;
1286         struct interface_data *iface;
1287         const GDBusSignalTable *signal;
1288
1289         *args = NULL;
1290         if (!dbus_connection_get_object_path_data(conn, path,
1291                                         (void *) &data) || data == NULL) {
1292                 error("dbus_connection_emit_signal: path %s isn't registered",
1293                                 path);
1294                 return FALSE;
1295         }
1296
1297         iface = find_interface(data->interfaces, interface);
1298         if (iface == NULL) {
1299                 error("dbus_connection_emit_signal: %s does not implement %s",
1300                                 path, interface);
1301                 return FALSE;
1302         }
1303
1304         for (signal = iface->signals; signal && signal->name; signal++) {
1305                 if (strcmp(signal->name, name) != 0)
1306                         continue;
1307
1308                 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1309                         const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1310                         if (g_strcmp0(env, "1") != 0)
1311                                 break;
1312                 }
1313
1314                 *args = signal->args;
1315                 return TRUE;
1316         }
1317
1318         error("No signal named %s on interface %s", name, interface);
1319         return FALSE;
1320 }
1321
1322 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1323                                                 const char *path,
1324                                                 const char *interface,
1325                                                 const char *name,
1326                                                 int first,
1327                                                 va_list var_args)
1328 {
1329         DBusMessage *signal;
1330         dbus_bool_t ret;
1331         const GDBusArgInfo *args;
1332
1333         if (!check_signal(conn, path, interface, name, &args))
1334                 return FALSE;
1335
1336         signal = dbus_message_new_signal(path, interface, name);
1337         if (signal == NULL) {
1338                 error("Unable to allocate new %s.%s signal", interface,  name);
1339                 return FALSE;
1340         }
1341
1342         ret = dbus_message_append_args_valist(signal, first, var_args);
1343         if (!ret)
1344                 goto fail;
1345
1346         if (g_dbus_args_have_signature(args, signal) == FALSE) {
1347                 error("%s.%s: got unexpected signature '%s'", interface, name,
1348                                         dbus_message_get_signature(signal));
1349                 ret = FALSE;
1350                 goto fail;
1351         }
1352
1353         ret = dbus_connection_send(conn, signal, NULL);
1354
1355 fail:
1356         dbus_message_unref(signal);
1357
1358         return ret;
1359 }
1360
1361 gboolean g_dbus_register_interface(DBusConnection *connection,
1362                                         const char *path, const char *name,
1363                                         const GDBusMethodTable *methods,
1364                                         const GDBusSignalTable *signals,
1365                                         const GDBusPropertyTable *properties,
1366                                         void *user_data,
1367                                         GDBusDestroyFunction destroy)
1368 {
1369         struct generic_data *data;
1370
1371         data = object_path_ref(connection, path);
1372         if (data == NULL)
1373                 return FALSE;
1374
1375         if (find_interface(data->interfaces, name)) {
1376                 object_path_unref(connection, path);
1377                 return FALSE;
1378         }
1379
1380         if (!add_interface(data, name, methods, signals, properties, user_data,
1381                                                                 destroy)) {
1382                 object_path_unref(connection, path);
1383                 return FALSE;
1384         }
1385
1386         if (properties != NULL && !find_interface(data->interfaces,
1387                                                 DBUS_INTERFACE_PROPERTIES))
1388                 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1389                                 properties_methods, properties_signals, NULL,
1390                                 data, NULL);
1391
1392         g_free(data->introspect);
1393         data->introspect = NULL;
1394
1395         return TRUE;
1396 }
1397
1398 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1399                                         const char *path, const char *name)
1400 {
1401         struct generic_data *data = NULL;
1402
1403         if (path == NULL)
1404                 return FALSE;
1405
1406         if (dbus_connection_get_object_path_data(connection, path,
1407                                                 (void *) &data) == FALSE)
1408                 return FALSE;
1409
1410         if (data == NULL)
1411                 return FALSE;
1412
1413         if (remove_interface(data, name) == FALSE)
1414                 return FALSE;
1415
1416         g_free(data->introspect);
1417         data->introspect = NULL;
1418
1419         object_path_unref(connection, data->path);
1420
1421         return TRUE;
1422 }
1423
1424 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1425 {
1426         if (security_table != NULL)
1427                 return FALSE;
1428
1429         security_table = security;
1430
1431         return TRUE;
1432 }
1433
1434 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1435 {
1436         security_table = NULL;
1437
1438         return TRUE;
1439 }
1440
1441 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1442                                         const char *format, va_list args)
1443 {
1444         char str[1024];
1445
1446         vsnprintf(str, sizeof(str), format, args);
1447
1448         return dbus_message_new_error(message, name, str);
1449 }
1450
1451 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1452                                                 const char *format, ...)
1453 {
1454         va_list args;
1455         DBusMessage *reply;
1456
1457         va_start(args, format);
1458
1459         reply = g_dbus_create_error_valist(message, name, format, args);
1460
1461         va_end(args);
1462
1463         return reply;
1464 }
1465
1466 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1467                                                 int type, va_list args)
1468 {
1469         DBusMessage *reply;
1470
1471         reply = dbus_message_new_method_return(message);
1472         if (reply == NULL)
1473                 return NULL;
1474
1475         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1476                 dbus_message_unref(reply);
1477                 return NULL;
1478         }
1479
1480         return reply;
1481 }
1482
1483 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1484 {
1485         va_list args;
1486         DBusMessage *reply;
1487
1488         va_start(args, type);
1489
1490         reply = g_dbus_create_reply_valist(message, type, args);
1491
1492         va_end(args);
1493
1494         return reply;
1495 }
1496
1497 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1498 {
1499         dbus_bool_t result = FALSE;
1500
1501         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1502                 dbus_message_set_no_reply(message, TRUE);
1503         else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1504                 const char *path = dbus_message_get_path(message);
1505                 const char *interface = dbus_message_get_interface(message);
1506                 const char *name = dbus_message_get_member(message);
1507                 const GDBusArgInfo *args;
1508
1509                 if (!check_signal(connection, path, interface, name, &args))
1510                         goto out;
1511         }
1512
1513         result = dbus_connection_send(connection, message, NULL);
1514
1515 out:
1516         dbus_message_unref(message);
1517
1518         return result;
1519 }
1520
1521 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1522                                         DBusMessage *message, const char *name,
1523                                         const char *format, va_list args)
1524 {
1525         DBusMessage *error;
1526         char str[1024];
1527
1528         vsnprintf(str, sizeof(str), format, args);
1529
1530         error = dbus_message_new_error(message, name, str);
1531         if (error == NULL)
1532                 return FALSE;
1533
1534         return g_dbus_send_message(connection, error);
1535 }
1536
1537 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1538                                 const char *name, const char *format, ...)
1539 {
1540         va_list args;
1541         gboolean result;
1542
1543         va_start(args, format);
1544
1545         result = g_dbus_send_error_valist(connection, message, name,
1546                                                         format, args);
1547
1548         va_end(args);
1549
1550         return result;
1551 }
1552
1553 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1554                                 DBusMessage *message, int type, va_list args)
1555 {
1556         DBusMessage *reply;
1557
1558         reply = dbus_message_new_method_return(message);
1559         if (reply == NULL)
1560                 return FALSE;
1561
1562         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1563                 dbus_message_unref(reply);
1564                 return FALSE;
1565         }
1566
1567         return g_dbus_send_message(connection, reply);
1568 }
1569
1570 gboolean g_dbus_send_reply(DBusConnection *connection,
1571                                 DBusMessage *message, int type, ...)
1572 {
1573         va_list args;
1574         gboolean result;
1575
1576         va_start(args, type);
1577
1578         result = g_dbus_send_reply_valist(connection, message, type, args);
1579
1580         va_end(args);
1581
1582         return result;
1583 }
1584
1585 gboolean g_dbus_emit_signal(DBusConnection *connection,
1586                                 const char *path, const char *interface,
1587                                 const char *name, int type, ...)
1588 {
1589         va_list args;
1590         gboolean result;
1591
1592         va_start(args, type);
1593
1594         result = emit_signal_valist(connection, path, interface,
1595                                                         name, type, args);
1596
1597         va_end(args);
1598
1599         return result;
1600 }
1601
1602 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1603                                 const char *path, const char *interface,
1604                                 const char *name, int type, va_list args)
1605 {
1606         return emit_signal_valist(connection, path, interface,
1607                                                         name, type, args);
1608 }
1609
1610 static void process_properties_from_interface(struct generic_data *data,
1611                                                 struct interface_data *iface)
1612 {
1613         GSList *l;
1614         DBusMessage *signal;
1615         DBusMessageIter iter, dict, array;
1616         GSList *invalidated;
1617
1618         if (iface->pending_prop == NULL)
1619                 return;
1620
1621         signal = dbus_message_new_signal(data->path,
1622                         DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1623         if (signal == NULL) {
1624                 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1625                                                 ".PropertiesChanged signal");
1626                 return;
1627         }
1628
1629         iface->pending_prop = g_slist_reverse(iface->pending_prop);
1630
1631         dbus_message_iter_init_append(signal, &iter);
1632         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1633         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1634                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1635                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1636                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1637
1638         invalidated = NULL;
1639
1640         for (l = iface->pending_prop; l != NULL; l = l->next) {
1641                 GDBusPropertyTable *p = l->data;
1642
1643                 if (p->get == NULL)
1644                         continue;
1645
1646                 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1647                         invalidated = g_slist_prepend(invalidated, p);
1648                         continue;
1649                 }
1650
1651                 append_property(iface, p, &dict);
1652         }
1653
1654         dbus_message_iter_close_container(&iter, &dict);
1655
1656         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1657                                 DBUS_TYPE_STRING_AS_STRING, &array);
1658         for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1659                 GDBusPropertyTable *p = l->data;
1660
1661                 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1662                                                                 &p->name);
1663         }
1664         g_slist_free(invalidated);
1665         dbus_message_iter_close_container(&iter, &array);
1666
1667         g_dbus_send_message(data->conn, signal);
1668
1669         g_slist_free(iface->pending_prop);
1670         iface->pending_prop = NULL;
1671 }
1672
1673 static void process_property_changes(struct generic_data *data)
1674 {
1675         GSList *l;
1676
1677         for (l = data->interfaces; l != NULL; l = l->next) {
1678                 struct interface_data *iface = l->data;
1679
1680                 process_properties_from_interface(data, iface);
1681         }
1682
1683         data->pending_prop = FALSE;
1684 }
1685
1686 void g_dbus_emit_property_changed(DBusConnection *connection,
1687                                 const char *path, const char *interface,
1688                                 const char *name)
1689 {
1690         const GDBusPropertyTable *property;
1691         struct generic_data *data;
1692         struct interface_data *iface;
1693
1694         if (path == NULL)
1695                 return;
1696
1697         if (!dbus_connection_get_object_path_data(connection, path,
1698                                         (void **) &data) || data == NULL)
1699                 return;
1700
1701         iface = find_interface(data->interfaces, interface);
1702         if (iface == NULL)
1703                 return;
1704
1705         /*
1706          * If ObjectManager is attached, don't emit property changed if
1707          * interface is not yet published
1708          */
1709         if (root && g_slist_find(data->added, iface))
1710                 return;
1711
1712         property = find_property(iface->properties, name);
1713         if (property == NULL) {
1714                 error("Could not find property %s in %p", name,
1715                                                         iface->properties);
1716                 return;
1717         }
1718
1719         if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1720                 return;
1721
1722         data->pending_prop = TRUE;
1723         iface->pending_prop = g_slist_prepend(iface->pending_prop,
1724                                                 (void *) property);
1725
1726         if (!data->process_id) {
1727                 data->process_id = g_idle_add(process_changes, data);
1728                 return;
1729         }
1730 }
1731
1732 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1733                                 const char *interface, DBusMessageIter *iter)
1734 {
1735         struct generic_data *data;
1736         struct interface_data *iface;
1737
1738         if (path == NULL)
1739                 return FALSE;
1740
1741         if (!dbus_connection_get_object_path_data(connection, path,
1742                                         (void **) &data) || data == NULL)
1743                 return FALSE;
1744
1745         iface = find_interface(data->interfaces, interface);
1746         if (iface == NULL)
1747                 return FALSE;
1748
1749         append_properties(iface, iter);
1750
1751         return TRUE;
1752 }
1753
1754 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1755 {
1756         struct generic_data *data;
1757
1758         data = object_path_ref(connection, "/");
1759         if (data == NULL)
1760                 return FALSE;
1761
1762         add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1763                                         manager_methods, manager_signals,
1764                                         NULL, data, NULL);
1765         root = data;
1766
1767         return TRUE;
1768 }
1769
1770 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1771 {
1772         if (!g_dbus_unregister_interface(connection, "/",
1773                                         DBUS_INTERFACE_OBJECT_MANAGER))
1774                 return FALSE;
1775
1776         root = NULL;
1777
1778         return TRUE;
1779 }
1780
1781 void g_dbus_set_flags(int flags)
1782 {
1783         global_flags = flags;
1784 }