gdbus: Simplify code for appending properties
[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 struct generic_data {
43         unsigned int refcount;
44         DBusConnection *conn;
45         char *path;
46         GSList *interfaces;
47         GSList *objects;
48         GSList *added;
49         GSList *removed;
50         guint process_id;
51         char *introspect;
52         struct generic_data *parent;
53 };
54
55 struct interface_data {
56         char *name;
57         const GDBusMethodTable *methods;
58         const GDBusSignalTable *signals;
59         const GDBusPropertyTable *properties;
60         void *user_data;
61         GDBusDestroyFunction destroy;
62 };
63
64 struct security_data {
65         GDBusPendingReply pending;
66         DBusMessage *message;
67         const GDBusMethodTable *method;
68         void *iface_user_data;
69 };
70
71 struct property_data {
72         GDBusPendingPropertySet id;
73         DBusMessage *message;
74 };
75
76 static gboolean process_changes(gpointer user_data);
77
78 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
79                                                 const char *direction)
80 {
81         for (; args && args->name; args++) {
82                 g_string_append_printf(gstr,
83                                         "\t\t\t<arg name=\"%s\" type=\"%s\"",
84                                         args->name, args->signature);
85
86                 if (direction)
87                         g_string_append_printf(gstr,
88                                         " direction=\"%s\"/>\n", direction);
89                 else
90                         g_string_append_printf(gstr, "/>\n");
91
92         }
93 }
94
95 #define G_DBUS_ANNOTATE(prefix_, name_, value_)                         \
96         prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" "  \
97         "value=\"" value_ "\"/>\n"
98
99 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
100         G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
101
102 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
103         G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
104
105 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
106 {
107         const GDBusMethodTable *method;
108         const GDBusSignalTable *signal;
109         const GDBusPropertyTable *property;
110
111         for (method = iface->methods; method && method->name; method++) {
112                 gboolean deprecated = method->flags &
113                                                 G_DBUS_METHOD_FLAG_DEPRECATED;
114                 gboolean noreply = method->flags &
115                                                 G_DBUS_METHOD_FLAG_NOREPLY;
116
117                 if (!deprecated && !noreply &&
118                                 !(method->in_args && method->in_args->name) &&
119                                 !(method->out_args && method->out_args->name))
120                         g_string_append_printf(gstr,
121                                                 "\t\t<method name=\"%s\"/>\n",
122                                                 method->name);
123                 else {
124                         g_string_append_printf(gstr,
125                                                 "\t\t<method name=\"%s\">\n",
126                                                 method->name);
127                         print_arguments(gstr, method->in_args, "in");
128                         print_arguments(gstr, method->out_args, "out");
129
130                         if (deprecated)
131                                 g_string_append_printf(gstr,
132                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
133                         if (noreply)
134                                 g_string_append_printf(gstr,
135                                         G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
136
137                         g_string_append_printf(gstr, "\t\t</method>\n");
138                 }
139         }
140
141         for (signal = iface->signals; signal && signal->name; signal++) {
142                 gboolean deprecated = signal->flags &
143                                                 G_DBUS_SIGNAL_FLAG_DEPRECATED;
144
145                 if (!deprecated && !(signal->args && signal->args->name))
146                         g_string_append_printf(gstr,
147                                                 "\t\t<signal name=\"%s\"/>\n",
148                                                 signal->name);
149                 else {
150                         g_string_append_printf(gstr,
151                                                 "\t\t<signal name=\"%s\">\n",
152                                                 signal->name);
153                         print_arguments(gstr, signal->args, NULL);
154
155                         if (deprecated)
156                                 g_string_append_printf(gstr,
157                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
158
159                         g_string_append_printf(gstr, "\t\t</signal>\n");
160                 }
161         }
162
163         for (property = iface->properties; property && property->name;
164                                                                 property++) {
165                 gboolean deprecated = property->flags &
166                                         G_DBUS_PROPERTY_FLAG_DEPRECATED;
167
168                 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
169                                         " type=\"%s\" access=\"%s%s\"",
170                                         property->name, property->type,
171                                         property->get ? "read" : "",
172                                         property->set ? "write" : "");
173
174                 if (!deprecated)
175                         g_string_append_printf(gstr, "/>\n");
176                 else
177                         g_string_append_printf(gstr,
178                                 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
179         }
180 }
181
182 static void generate_introspection_xml(DBusConnection *conn,
183                                 struct generic_data *data, const char *path)
184 {
185         GSList *list;
186         GString *gstr;
187         char **children;
188         int i;
189
190         g_free(data->introspect);
191
192         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
193
194         g_string_append_printf(gstr, "<node>\n");
195
196         for (list = data->interfaces; list; list = list->next) {
197                 struct interface_data *iface = list->data;
198
199                 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
200                                                                 iface->name);
201
202                 generate_interface_xml(gstr, iface);
203
204                 g_string_append_printf(gstr, "\t</interface>\n");
205         }
206
207         if (!dbus_connection_list_registered(conn, path, &children))
208                 goto done;
209
210         for (i = 0; children[i]; i++)
211                 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
212                                                                 children[i]);
213
214         dbus_free_string_array(children);
215
216 done:
217         g_string_append_printf(gstr, "</node>\n");
218
219         data->introspect = g_string_free(gstr, FALSE);
220 }
221
222 static DBusMessage *introspect(DBusConnection *connection,
223                                 DBusMessage *message, void *user_data)
224 {
225         struct generic_data *data = user_data;
226         DBusMessage *reply;
227
228         if (data->introspect == NULL)
229                 generate_introspection_xml(connection, data,
230                                                 dbus_message_get_path(message));
231
232         reply = dbus_message_new_method_return(message);
233         if (reply == NULL)
234                 return NULL;
235
236         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
237                                         DBUS_TYPE_INVALID);
238
239         return reply;
240 }
241
242 static DBusHandlerResult process_message(DBusConnection *connection,
243                         DBusMessage *message, const GDBusMethodTable *method,
244                                                         void *iface_user_data)
245 {
246         DBusMessage *reply;
247
248         reply = method->function(connection, message, iface_user_data);
249
250         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
251                 if (reply != NULL)
252                         dbus_message_unref(reply);
253                 return DBUS_HANDLER_RESULT_HANDLED;
254         }
255
256         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
257                 if (reply == NULL)
258                         return DBUS_HANDLER_RESULT_HANDLED;
259         }
260
261         if (reply == NULL)
262                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
263
264         dbus_connection_send(connection, reply, NULL);
265         dbus_message_unref(reply);
266
267         return DBUS_HANDLER_RESULT_HANDLED;
268 }
269
270 static GDBusPendingReply next_pending = 1;
271 static GSList *pending_security = NULL;
272
273 static const GDBusSecurityTable *security_table = NULL;
274
275 void g_dbus_pending_success(DBusConnection *connection,
276                                         GDBusPendingReply pending)
277 {
278         GSList *list;
279
280         for (list = pending_security; list; list = list->next) {
281                 struct security_data *secdata = list->data;
282
283                 if (secdata->pending != pending)
284                         continue;
285
286                 pending_security = g_slist_remove(pending_security, secdata);
287
288                 process_message(connection, secdata->message,
289                                 secdata->method, secdata->iface_user_data);
290
291                 dbus_message_unref(secdata->message);
292                 g_free(secdata);
293                 return;
294         }
295 }
296
297 void g_dbus_pending_error_valist(DBusConnection *connection,
298                                 GDBusPendingReply pending, const char *name,
299                                         const char *format, va_list args)
300 {
301         GSList *list;
302
303         for (list = pending_security; list; list = list->next) {
304                 struct security_data *secdata = list->data;
305                 DBusMessage *reply;
306
307                 if (secdata->pending != pending)
308                         continue;
309
310                 pending_security = g_slist_remove(pending_security, secdata);
311
312                 reply = g_dbus_create_error_valist(secdata->message,
313                                                         name, format, args);
314                 if (reply != NULL) {
315                         dbus_connection_send(connection, reply, NULL);
316                         dbus_message_unref(reply);
317                 }
318
319                 dbus_message_unref(secdata->message);
320                 g_free(secdata);
321                 return;
322         }
323 }
324
325 void g_dbus_pending_error(DBusConnection *connection,
326                                 GDBusPendingReply pending,
327                                 const char *name, const char *format, ...)
328 {
329         va_list args;
330
331         va_start(args, format);
332
333         g_dbus_pending_error_valist(connection, pending, name, format, args);
334
335         va_end(args);
336 }
337
338 int polkit_check_authorization(DBusConnection *conn,
339                                 const char *action, gboolean interaction,
340                                 void (*function) (dbus_bool_t authorized,
341                                                         void *user_data),
342                                                 void *user_data, int timeout);
343
344 struct builtin_security_data {
345         DBusConnection *conn;
346         GDBusPendingReply pending;
347 };
348
349 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
350 {
351         struct builtin_security_data *data = user_data;
352
353         if (authorized == TRUE)
354                 g_dbus_pending_success(data->conn, data->pending);
355         else
356                 g_dbus_pending_error(data->conn, data->pending,
357                                                 DBUS_ERROR_AUTH_FAILED, NULL);
358
359         g_free(data);
360 }
361
362 static void builtin_security_function(DBusConnection *conn,
363                                                 const char *action,
364                                                 gboolean interaction,
365                                                 GDBusPendingReply pending)
366 {
367         struct builtin_security_data *data;
368
369         data = g_new0(struct builtin_security_data, 1);
370         data->conn = conn;
371         data->pending = pending;
372
373         if (polkit_check_authorization(conn, action, interaction,
374                                 builtin_security_result, data, 30000) < 0)
375                 g_dbus_pending_error(conn, pending, NULL, NULL);
376 }
377
378 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
379                         const GDBusMethodTable *method, void *iface_user_data)
380 {
381         const GDBusSecurityTable *security;
382
383         for (security = security_table; security && security->privilege;
384                                                                 security++) {
385                 struct security_data *secdata;
386                 gboolean interaction;
387
388                 if (security->privilege != method->privilege)
389                         continue;
390
391                 secdata = g_new(struct security_data, 1);
392                 secdata->pending = next_pending++;
393                 secdata->message = dbus_message_ref(msg);
394                 secdata->method = method;
395                 secdata->iface_user_data = iface_user_data;
396
397                 pending_security = g_slist_prepend(pending_security, secdata);
398
399                 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
400                         interaction = TRUE;
401                 else
402                         interaction = FALSE;
403
404                 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
405                                                         security->function)
406                         security->function(conn, security->action,
407                                                 interaction, secdata->pending);
408                 else
409                         builtin_security_function(conn, security->action,
410                                                 interaction, secdata->pending);
411
412                 return TRUE;
413         }
414
415         return FALSE;
416 }
417
418 static GDBusPendingPropertySet next_pending_property = 1;
419 static GSList *pending_property_set;
420
421 static struct property_data *remove_pending_property_data(
422                                                 GDBusPendingPropertySet id)
423 {
424         struct property_data *propdata;
425         GSList *l;
426
427         for (l = pending_property_set; l != NULL; l = l->next) {
428                 propdata = l->data;
429                 if (propdata->id != id)
430                         continue;
431         }
432
433         if (l == NULL)
434                 return NULL;
435
436         pending_property_set = g_slist_delete_link(pending_property_set, l);
437
438         return propdata;
439 }
440
441 void g_dbus_pending_property_success(DBusConnection *connection,
442                                                 GDBusPendingPropertySet id)
443 {
444         struct property_data *propdata;
445
446         propdata = remove_pending_property_data(id);
447         if (propdata == NULL)
448                 return;
449
450         g_dbus_send_reply(connection, propdata->message, DBUS_TYPE_INVALID);
451         dbus_message_unref(propdata->message);
452         g_free(propdata);
453 }
454
455 void g_dbus_pending_property_error_valist(DBusConnection *connection,
456                                         GDBusPendingReply id, const char *name,
457                                         const char *format, va_list args)
458 {
459         struct property_data *propdata;
460         DBusMessage *reply;
461
462         propdata = remove_pending_property_data(id);
463         if (propdata == NULL)
464                 return;
465
466         reply = g_dbus_create_error_valist(propdata->message, name, format,
467                                                                         args);
468         if (reply != NULL) {
469                 dbus_connection_send(connection, reply, NULL);
470                 dbus_message_unref(reply);
471         }
472
473         dbus_message_unref(propdata->message);
474         g_free(propdata);
475 }
476
477 void g_dbus_pending_property_error(DBusConnection *connection,
478                                         GDBusPendingReply id, const char *name,
479                                         const char *format, ...)
480 {
481         va_list args;
482
483         va_start(args, format);
484
485         g_dbus_pending_property_error_valist(connection, id, name, format,
486                                                                         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 (p->get == NULL)
530                         continue;
531
532                 if (p->exists != NULL && !p->exists(p, data->user_data))
533                         continue;
534
535                 append_property(data, p, &dict);
536         }
537
538         dbus_message_iter_close_container(iter, &dict);
539 }
540
541 static void append_interface(gpointer data, gpointer user_data)
542 {
543         struct interface_data *iface = data;
544         DBusMessageIter *array = user_data;
545         DBusMessageIter entry;
546
547         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
548                                                                 &entry);
549         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
550         append_properties(data, &entry);
551         dbus_message_iter_close_container(array, &entry);
552 }
553
554 static void emit_interfaces_added(struct generic_data *data)
555 {
556         DBusMessage *signal;
557         DBusMessageIter iter, array;
558         struct generic_data *parent = data->parent;
559
560         if (parent == NULL)
561                 return;
562
563         /* Find root data */
564         while (parent->parent)
565                 parent = parent->parent;
566
567         signal = dbus_message_new_signal(parent->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         g_dbus_send_message(data->conn, signal);
594 }
595
596 static struct interface_data *find_interface(GSList *interfaces,
597                                                 const char *name)
598 {
599         GSList *list;
600
601         if (name == NULL)
602                 return NULL;
603
604         for (list = interfaces; list; list = list->next) {
605                 struct interface_data *iface = list->data;
606                 if (!strcmp(name, iface->name))
607                         return iface;
608         }
609
610         return NULL;
611 }
612
613 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
614                                                         DBusMessage *message)
615 {
616         const char *sig = dbus_message_get_signature(message);
617         const char *p = NULL;
618
619         for (; args && args->signature && *sig; args++) {
620                 p = args->signature;
621
622                 for (; *sig && *p; sig++, p++) {
623                         if (*p != *sig)
624                                 return FALSE;
625                 }
626         }
627
628         if (*sig || (p && *p) || (args && args->signature))
629                 return FALSE;
630
631         return TRUE;
632 }
633
634 static gboolean remove_interface(struct generic_data *data, const char *name)
635 {
636         struct interface_data *iface;
637
638         iface = find_interface(data->interfaces, name);
639         if (iface == NULL)
640                 return FALSE;
641
642         data->interfaces = g_slist_remove(data->interfaces, iface);
643
644         if (iface->destroy) {
645                 iface->destroy(iface->user_data);
646                 iface->user_data = NULL;
647         }
648
649         if (data->parent == NULL) {
650                 g_free(iface->name);
651                 g_free(iface);
652                 return TRUE;
653         }
654
655         data->removed = g_slist_prepend(data->removed, iface->name);
656         g_free(iface);
657
658         if (data->process_id > 0)
659                 return TRUE;
660
661         data->process_id = g_idle_add(process_changes, data);
662
663         return TRUE;
664 }
665
666 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
667                                                 const char *child_path)
668 {
669         struct generic_data *data = NULL, *child = NULL, *parent = NULL;
670         char *parent_path, *slash;
671
672         parent_path = g_strdup(child_path);
673         slash = strrchr(parent_path, '/');
674         if (slash == NULL)
675                 goto done;
676
677         if (slash == parent_path && parent_path[1] != '\0')
678                 parent_path[1] = '\0';
679         else
680                 *slash = '\0';
681
682         if (!strlen(parent_path))
683                 goto done;
684
685         if (dbus_connection_get_object_path_data(conn, parent_path,
686                                                         (void *) &data) == FALSE) {
687                 goto done;
688         }
689
690         parent = invalidate_parent_data(conn, parent_path);
691
692         if (data == NULL) {
693                 data = parent;
694                 if (data == NULL)
695                         goto done;
696         }
697
698         g_free(data->introspect);
699         data->introspect = NULL;
700
701         if (!dbus_connection_get_object_path_data(conn, child_path,
702                                                         (void *) &child))
703                 goto done;
704
705         if (child == NULL || g_slist_find(data->objects, child) != NULL)
706                 goto done;
707
708         data->objects = g_slist_prepend(data->objects, child);
709         child->parent = data;
710
711 done:
712         g_free(parent_path);
713         return data;
714 }
715
716 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
717                                                         const char *name)
718 {
719         const GDBusPropertyTable *p;
720
721         for (p = properties; p && p->name; p++) {
722                 if (strcmp(name, p->name) == 0)
723                         return p;
724         }
725
726         return NULL;
727 }
728
729 static DBusMessage *properties_get(DBusConnection *connection,
730                                         DBusMessage *message, void *user_data)
731 {
732         struct generic_data *data = user_data;
733         struct interface_data *iface;
734         const GDBusPropertyTable *property;
735         const char *interface, *name;
736         DBusMessageIter iter, value;
737         DBusMessage *reply;
738
739         if (!dbus_message_get_args(message, NULL,
740                                         DBUS_TYPE_STRING, &interface,
741                                         DBUS_TYPE_STRING, &name,
742                                         DBUS_TYPE_INVALID))
743                 return NULL;
744
745         iface = find_interface(data->interfaces, interface);
746         if (iface == NULL)
747                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
748                                 "No such interface '%s'", interface);
749
750         property = find_property(iface->properties, name);
751         if (property == NULL)
752                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
753                                 "No such property '%s'", name);
754
755         if (property->exists != NULL &&
756                         !property->exists(property, iface->user_data))
757                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
758                                         "No such property '%s'", name);
759
760         if (property->get == NULL)
761                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
762                                 "Property '%s' is not readable", name);
763
764         reply = dbus_message_new_method_return(message);
765         if (reply == NULL)
766                 return NULL;
767
768         dbus_message_iter_init_append(reply, &iter);
769         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
770                                                 property->type, &value);
771
772         if (!property->get(property, &value, iface->user_data)) {
773                 dbus_message_unref(reply);
774                 return NULL;
775         }
776
777         dbus_message_iter_close_container(&iter, &value);
778
779         return reply;
780 }
781
782 static DBusMessage *properties_get_all(DBusConnection *connection,
783                                         DBusMessage *message, void *user_data)
784 {
785         struct generic_data *data = user_data;
786         struct interface_data *iface;
787         const char *interface;
788         DBusMessageIter iter;
789         DBusMessage *reply;
790
791         if (!dbus_message_get_args(message, NULL,
792                                         DBUS_TYPE_STRING, &interface,
793                                         DBUS_TYPE_INVALID))
794                 return NULL;
795
796         iface = find_interface(data->interfaces, interface);
797         if (iface == NULL)
798                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
799                                         "No such interface '%s'", interface);
800
801         reply = dbus_message_new_method_return(message);
802         if (reply == NULL)
803                 return NULL;
804
805         dbus_message_iter_init_append(reply, &iter);
806
807         append_properties(iface, &iter);
808
809         return reply;
810 }
811
812 static DBusMessage *properties_set(DBusConnection *connection,
813                                         DBusMessage *message, void *user_data)
814 {
815         struct generic_data *data = user_data;
816         DBusMessageIter iter, sub;
817         struct interface_data *iface;
818         const GDBusPropertyTable *property;
819         const char *name, *interface;
820         struct property_data *propdata;
821
822         if (!dbus_message_iter_init(message, &iter))
823                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
824                                                         "No arguments given");
825
826         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
827                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
828                                         "Invalid argument type: '%c'",
829                                         dbus_message_iter_get_arg_type(&iter));
830
831         dbus_message_iter_get_basic(&iter, &name);
832         dbus_message_iter_next(&iter);
833
834         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
835                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
836                                         "Invalid argument type: '%c'",
837                                         dbus_message_iter_get_arg_type(&iter));
838
839         dbus_message_iter_get_basic(&iter, &interface);
840         dbus_message_iter_next(&iter);
841
842         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
843                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
844                                         "Invalid argument type: '%c'",
845                                         dbus_message_iter_get_arg_type(&iter));
846
847         dbus_message_iter_recurse(&iter, &sub);
848
849         iface = find_interface(data->interfaces, interface);
850         if (iface == NULL)
851                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
852                                         "No such interface '%s'", interface);
853
854         property = find_property(iface->properties, name);
855         if (property == NULL)
856                 return g_dbus_create_error(message,
857                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
858                                                 "No such property '%s'", name);
859
860         if (property->set == NULL)
861                 return g_dbus_create_error(message,
862                                         DBUS_ERROR_PROPERTY_READ_ONLY,
863                                         "Property '%s' is not writable", name);
864
865         if (property->exists != NULL &&
866                         !property->exists(property, iface->user_data))
867                 return g_dbus_create_error(message,
868                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
869                                                 "No such property '%s'", name);
870
871         propdata = g_new(struct property_data, 1);
872         propdata->id = next_pending_property++;
873         propdata->message = dbus_message_ref(message);
874
875         property->set(property, &sub, propdata->id, iface->user_data);
876
877         return NULL;
878 }
879
880 static const GDBusMethodTable properties_methods[] = {
881         { GDBUS_METHOD("Get",
882                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
883                         GDBUS_ARGS({ "value", "v" }),
884                         properties_get) },
885         { GDBUS_ASYNC_METHOD("Set", NULL,
886                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
887                                                         { "value", "v" }),
888                         properties_set) },
889         { GDBUS_METHOD("GetAll",
890                         GDBUS_ARGS({ "interface", "s" }),
891                         GDBUS_ARGS({ "properties", "a{sv}" }),
892                         properties_get_all) },
893         { }
894 };
895
896 static const GDBusSignalTable properties_signals[] = {
897         { GDBUS_SIGNAL("PropertiesChanged",
898                         GDBUS_ARGS({ "interface", "s" },
899                                         { "changed_properties", "a{sv}" },
900                                         { "invalidated_properties", "as"})) },
901         { }
902 };
903
904 static void append_name(gpointer data, gpointer user_data)
905 {
906         char *name = data;
907         DBusMessageIter *iter = user_data;
908
909         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
910 }
911
912 static void emit_interfaces_removed(struct generic_data *data)
913 {
914         DBusMessage *signal;
915         DBusMessageIter iter, array;
916         struct generic_data *parent = data->parent;
917
918         if (parent == NULL)
919                 return;
920
921         /* Find root data */
922         while (parent->parent)
923                 parent = parent->parent;
924
925         signal = dbus_message_new_signal(parent->path,
926                                         DBUS_INTERFACE_OBJECT_MANAGER,
927                                         "InterfacesRemoved");
928         if (signal == NULL)
929                 return;
930
931         dbus_message_iter_init_append(signal, &iter);
932         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
933                                                                 &data->path);
934         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
935                                         DBUS_TYPE_STRING_AS_STRING, &array);
936
937         g_slist_foreach(data->removed, append_name, &array);
938         g_slist_free_full(data->removed, g_free);
939         data->removed = NULL;
940
941         dbus_message_iter_close_container(&iter, &array);
942
943         g_dbus_send_message(data->conn, signal);
944 }
945
946 static gboolean process_changes(gpointer user_data)
947 {
948         struct generic_data *data = user_data;
949
950         data->process_id = 0;
951
952         if (data->added != NULL)
953                 emit_interfaces_added(data);
954
955         if (data->removed != NULL)
956                 emit_interfaces_removed(data);
957
958         return FALSE;
959 }
960
961 static void generic_unregister(DBusConnection *connection, void *user_data)
962 {
963         struct generic_data *data = user_data;
964         struct generic_data *parent = data->parent;
965
966         if (parent != NULL)
967                 parent->objects = g_slist_remove(parent->objects, data);
968
969         if (data->process_id > 0) {
970                 g_source_remove(data->process_id);
971                 if (data->removed != NULL)
972                         emit_interfaces_removed(data);
973         }
974
975         g_slist_foreach(data->objects, reset_parent, data->parent);
976         g_slist_free(data->objects);
977
978         dbus_connection_unref(data->conn);
979         g_free(data->introspect);
980         g_free(data->path);
981         g_free(data);
982 }
983
984 static DBusHandlerResult generic_message(DBusConnection *connection,
985                                         DBusMessage *message, void *user_data)
986 {
987         struct generic_data *data = user_data;
988         struct interface_data *iface;
989         const GDBusMethodTable *method;
990         const char *interface;
991
992         interface = dbus_message_get_interface(message);
993
994         iface = find_interface(data->interfaces, interface);
995         if (iface == NULL)
996                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
997
998         for (method = iface->methods; method &&
999                         method->name && method->function; method++) {
1000                 if (dbus_message_is_method_call(message, iface->name,
1001                                                         method->name) == FALSE)
1002                         continue;
1003
1004                 if (g_dbus_args_have_signature(method->in_args,
1005                                                         message) == FALSE)
1006                         continue;
1007
1008                 if (check_privilege(connection, message, method,
1009                                                 iface->user_data) == TRUE)
1010                         return DBUS_HANDLER_RESULT_HANDLED;
1011
1012                 return process_message(connection, message, method,
1013                                                         iface->user_data);
1014         }
1015
1016         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1017 }
1018
1019 static DBusObjectPathVTable generic_table = {
1020         .unregister_function    = generic_unregister,
1021         .message_function       = generic_message,
1022 };
1023
1024 static const GDBusMethodTable introspect_methods[] = {
1025         { GDBUS_METHOD("Introspect", NULL,
1026                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1027         { }
1028 };
1029
1030 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1031 {
1032         DBusMessageIter array;
1033
1034         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1035                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1036                                 DBUS_TYPE_STRING_AS_STRING
1037                                 DBUS_TYPE_ARRAY_AS_STRING
1038                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1039                                 DBUS_TYPE_STRING_AS_STRING
1040                                 DBUS_TYPE_VARIANT_AS_STRING
1041                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1042                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1043
1044         g_slist_foreach(data->interfaces, append_interface, &array);
1045
1046         dbus_message_iter_close_container(iter, &array);
1047 }
1048
1049 static void append_object(gpointer data, gpointer user_data)
1050 {
1051         struct generic_data *child = data;
1052         DBusMessageIter *array = user_data;
1053         DBusMessageIter entry;
1054
1055         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1056                                                                 &entry);
1057         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1058                                                                 &child->path);
1059         append_interfaces(child, &entry);
1060         dbus_message_iter_close_container(array, &entry);
1061
1062         g_slist_foreach(child->objects, append_object, user_data);
1063 }
1064
1065 static DBusMessage *get_objects(DBusConnection *connection,
1066                                 DBusMessage *message, void *user_data)
1067 {
1068         struct generic_data *data = user_data;
1069         DBusMessage *reply;
1070         DBusMessageIter iter;
1071         DBusMessageIter array;
1072
1073         reply = dbus_message_new_method_return(message);
1074         if (reply == NULL)
1075                 return NULL;
1076
1077         dbus_message_iter_init_append(reply, &iter);
1078
1079         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1080                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1081                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1082                                         DBUS_TYPE_ARRAY_AS_STRING
1083                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1084                                         DBUS_TYPE_STRING_AS_STRING
1085                                         DBUS_TYPE_ARRAY_AS_STRING
1086                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1087                                         DBUS_TYPE_STRING_AS_STRING
1088                                         DBUS_TYPE_VARIANT_AS_STRING
1089                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1090                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1091                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1092                                         &array);
1093
1094         g_slist_foreach(data->objects, append_object, &array);
1095
1096         dbus_message_iter_close_container(&iter, &array);
1097
1098         return reply;
1099 }
1100
1101 static const GDBusMethodTable manager_methods[] = {
1102         { GDBUS_METHOD("GetManagedObjects", NULL,
1103                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1104         { }
1105 };
1106
1107 static const GDBusSignalTable manager_signals[] = {
1108         { GDBUS_SIGNAL("InterfacesAdded",
1109                 GDBUS_ARGS({ "object", "o" },
1110                                 { "interfaces", "a{sa{sv}}" })) },
1111         { GDBUS_SIGNAL("InterfacesRemoved",
1112                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1113         { }
1114 };
1115
1116 static void add_interface(struct generic_data *data,
1117                                 const char *name,
1118                                 const GDBusMethodTable *methods,
1119                                 const GDBusSignalTable *signals,
1120                                 const GDBusPropertyTable *properties,
1121                                 void *user_data,
1122                                 GDBusDestroyFunction destroy)
1123 {
1124         struct interface_data *iface;
1125
1126         iface = g_new0(struct interface_data, 1);
1127         iface->name = g_strdup(name);
1128         iface->methods = methods;
1129         iface->signals = signals;
1130         iface->properties = properties;
1131         iface->user_data = user_data;
1132         iface->destroy = destroy;
1133
1134         data->interfaces = g_slist_append(data->interfaces, iface);
1135         if (data->parent == NULL)
1136                 return;
1137
1138         data->added = g_slist_append(data->added, iface);
1139         if (data->process_id > 0)
1140                 return;
1141
1142         data->process_id = g_idle_add(process_changes, data);
1143 }
1144
1145 static struct generic_data *object_path_ref(DBusConnection *connection,
1146                                                         const char *path)
1147 {
1148         struct generic_data *data;
1149
1150         if (dbus_connection_get_object_path_data(connection, path,
1151                                                 (void *) &data) == TRUE) {
1152                 if (data != NULL) {
1153                         data->refcount++;
1154                         return data;
1155                 }
1156         }
1157
1158         data = g_new0(struct generic_data, 1);
1159         data->conn = dbus_connection_ref(connection);
1160         data->path = g_strdup(path);
1161         data->refcount = 1;
1162
1163         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1164
1165         if (!dbus_connection_register_object_path(connection, path,
1166                                                 &generic_table, data)) {
1167                 g_free(data->introspect);
1168                 g_free(data);
1169                 return NULL;
1170         }
1171
1172         invalidate_parent_data(connection, path);
1173
1174         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1175                                                 NULL, NULL, data, NULL);
1176
1177         /* Only root path export ObjectManager interface */
1178         if (data->parent == NULL)
1179                 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1180                                         manager_methods, manager_signals,
1181                                         NULL, data, NULL);
1182
1183         add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1184                                         properties_signals, NULL, data, NULL);
1185
1186         return data;
1187 }
1188
1189 static void object_path_unref(DBusConnection *connection, const char *path)
1190 {
1191         struct generic_data *data = NULL;
1192
1193         if (dbus_connection_get_object_path_data(connection, path,
1194                                                 (void *) &data) == FALSE)
1195                 return;
1196
1197         if (data == NULL)
1198                 return;
1199
1200         data->refcount--;
1201
1202         if (data->refcount > 0)
1203                 return;
1204
1205         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1206         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1207         remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1208
1209         invalidate_parent_data(data->conn, data->path);
1210
1211         dbus_connection_unregister_object_path(data->conn, data->path);
1212 }
1213
1214 static gboolean check_signal(DBusConnection *conn, const char *path,
1215                                 const char *interface, const char *name,
1216                                 const GDBusArgInfo **args)
1217 {
1218         struct generic_data *data = NULL;
1219         struct interface_data *iface;
1220         const GDBusSignalTable *signal;
1221
1222         *args = NULL;
1223         if (!dbus_connection_get_object_path_data(conn, path,
1224                                         (void *) &data) || data == NULL) {
1225                 error("dbus_connection_emit_signal: path %s isn't registered",
1226                                 path);
1227                 return FALSE;
1228         }
1229
1230         iface = find_interface(data->interfaces, interface);
1231         if (iface == NULL) {
1232                 error("dbus_connection_emit_signal: %s does not implement %s",
1233                                 path, interface);
1234                 return FALSE;
1235         }
1236
1237         for (signal = iface->signals; signal && signal->name; signal++) {
1238                 if (!strcmp(signal->name, name)) {
1239                         *args = signal->args;
1240                         return TRUE;
1241                 }
1242         }
1243
1244         error("No signal named %s on interface %s", name, interface);
1245         return FALSE;
1246 }
1247
1248 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1249                                                 const char *path,
1250                                                 const char *interface,
1251                                                 const char *name,
1252                                                 int first,
1253                                                 va_list var_args)
1254 {
1255         DBusMessage *signal;
1256         dbus_bool_t ret;
1257         const GDBusArgInfo *args;
1258
1259         if (!check_signal(conn, path, interface, name, &args))
1260                 return FALSE;
1261
1262         signal = dbus_message_new_signal(path, interface, name);
1263         if (signal == NULL) {
1264                 error("Unable to allocate new %s.%s signal", interface,  name);
1265                 return FALSE;
1266         }
1267
1268         ret = dbus_message_append_args_valist(signal, first, var_args);
1269         if (!ret)
1270                 goto fail;
1271
1272         if (g_dbus_args_have_signature(args, signal) == FALSE) {
1273                 error("%s.%s: got unexpected signature '%s'", interface, name,
1274                                         dbus_message_get_signature(signal));
1275                 ret = FALSE;
1276                 goto fail;
1277         }
1278
1279         ret = dbus_connection_send(conn, signal, NULL);
1280
1281 fail:
1282         dbus_message_unref(signal);
1283
1284         return ret;
1285 }
1286
1287 gboolean g_dbus_register_interface(DBusConnection *connection,
1288                                         const char *path, const char *name,
1289                                         const GDBusMethodTable *methods,
1290                                         const GDBusSignalTable *signals,
1291                                         const GDBusPropertyTable *properties,
1292                                         void *user_data,
1293                                         GDBusDestroyFunction destroy)
1294 {
1295         struct generic_data *data;
1296
1297         data = object_path_ref(connection, path);
1298         if (data == NULL)
1299                 return FALSE;
1300
1301         if (find_interface(data->interfaces, name)) {
1302                 object_path_unref(connection, path);
1303                 return FALSE;
1304         }
1305
1306         add_interface(data, name, methods, signals, properties, user_data,
1307                                                                 destroy);
1308
1309         g_free(data->introspect);
1310         data->introspect = NULL;
1311
1312         return TRUE;
1313 }
1314
1315 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1316                                         const char *path, const char *name)
1317 {
1318         struct generic_data *data = NULL;
1319
1320         if (path == NULL)
1321                 return FALSE;
1322
1323         if (dbus_connection_get_object_path_data(connection, path,
1324                                                 (void *) &data) == FALSE)
1325                 return FALSE;
1326
1327         if (data == NULL)
1328                 return FALSE;
1329
1330         if (remove_interface(data, name) == FALSE)
1331                 return FALSE;
1332
1333         g_free(data->introspect);
1334         data->introspect = NULL;
1335
1336         object_path_unref(connection, data->path);
1337
1338         return TRUE;
1339 }
1340
1341 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1342 {
1343         if (security_table != NULL)
1344                 return FALSE;
1345
1346         security_table = security;
1347
1348         return TRUE;
1349 }
1350
1351 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1352 {
1353         security_table = NULL;
1354
1355         return TRUE;
1356 }
1357
1358 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1359                                         const char *format, va_list args)
1360 {
1361         char str[1024];
1362
1363         vsnprintf(str, sizeof(str), format, args);
1364
1365         return dbus_message_new_error(message, name, str);
1366 }
1367
1368 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1369                                                 const char *format, ...)
1370 {
1371         va_list args;
1372         DBusMessage *reply;
1373
1374         va_start(args, format);
1375
1376         reply = g_dbus_create_error_valist(message, name, format, args);
1377
1378         va_end(args);
1379
1380         return reply;
1381 }
1382
1383 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1384                                                 int type, va_list args)
1385 {
1386         DBusMessage *reply;
1387
1388         reply = dbus_message_new_method_return(message);
1389         if (reply == NULL)
1390                 return NULL;
1391
1392         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1393                 dbus_message_unref(reply);
1394                 return NULL;
1395         }
1396
1397         return reply;
1398 }
1399
1400 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1401 {
1402         va_list args;
1403         DBusMessage *reply;
1404
1405         va_start(args, type);
1406
1407         reply = g_dbus_create_reply_valist(message, type, args);
1408
1409         va_end(args);
1410
1411         return reply;
1412 }
1413
1414 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1415 {
1416         dbus_bool_t result;
1417
1418         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1419                 dbus_message_set_no_reply(message, TRUE);
1420
1421         result = dbus_connection_send(connection, message, NULL);
1422
1423         dbus_message_unref(message);
1424
1425         return result;
1426 }
1427
1428 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1429                                 DBusMessage *message, int type, va_list args)
1430 {
1431         DBusMessage *reply;
1432
1433         reply = dbus_message_new_method_return(message);
1434         if (reply == NULL)
1435                 return FALSE;
1436
1437         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1438                 dbus_message_unref(reply);
1439                 return FALSE;
1440         }
1441
1442         return g_dbus_send_message(connection, reply);
1443 }
1444
1445 gboolean g_dbus_send_reply(DBusConnection *connection,
1446                                 DBusMessage *message, int type, ...)
1447 {
1448         va_list args;
1449         gboolean result;
1450
1451         va_start(args, type);
1452
1453         result = g_dbus_send_reply_valist(connection, message, type, args);
1454
1455         va_end(args);
1456
1457         return result;
1458 }
1459
1460 gboolean g_dbus_emit_signal(DBusConnection *connection,
1461                                 const char *path, const char *interface,
1462                                 const char *name, int type, ...)
1463 {
1464         va_list args;
1465         gboolean result;
1466
1467         va_start(args, type);
1468
1469         result = emit_signal_valist(connection, path, interface,
1470                                                         name, type, args);
1471
1472         va_end(args);
1473
1474         return result;
1475 }
1476
1477 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1478                                 const char *path, const char *interface,
1479                                 const char *name, int type, va_list args)
1480 {
1481         return emit_signal_valist(connection, path, interface,
1482                                                         name, type, args);
1483 }