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