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