| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- *
- * D-Bus helper library
- *
- * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
- *
- *
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <glib.h>
- #include <dbus/dbus.h>
- #include "gdbus.h"
- #define info(fmt...)
- #define error(fmt...)
- #define debug(fmt...)
- #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
- #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
- #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
- #endif
- #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
- #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
- #endif
- struct generic_data {
- unsigned int refcount;
- DBusConnection *conn;
- char *path;
- GSList *interfaces;
- GSList *objects;
- GSList *added;
- GSList *removed;
- guint process_id;
- gboolean pending_prop;
- char *introspect;
- struct generic_data *parent;
- };
- struct interface_data {
- char *name;
- const GDBusMethodTable *methods;
- const GDBusSignalTable *signals;
- const GDBusPropertyTable *properties;
- GSList *pending_prop;
- void *user_data;
- GDBusDestroyFunction destroy;
- };
- struct security_data {
- GDBusPendingReply pending;
- DBusMessage *message;
- const GDBusMethodTable *method;
- void *iface_user_data;
- };
- struct property_data {
- DBusConnection *conn;
- GDBusPendingPropertySet id;
- DBusMessage *message;
- };
- static int global_flags = 0;
- static struct generic_data *root;
- static GSList *pending = NULL;
- static gboolean process_changes(gpointer user_data);
- static void process_properties_from_interface(struct generic_data *data,
- struct interface_data *iface);
- static void process_property_changes(struct generic_data *data);
- static void print_arguments(GString *gstr, const GDBusArgInfo *args,
- const char *direction)
- {
- for (; args && args->name; args++) {
- g_string_append_printf(gstr,
- "<arg name=\"%s\" type=\"%s\"",
- args->name, args->signature);
- if (direction)
- g_string_append_printf(gstr,
- " direction=\"%s\"/>\n", direction);
- else
- g_string_append_printf(gstr, "/>\n");
- }
- }
- #define G_DBUS_ANNOTATE(name_, value_) \
- "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
- "value=\"" value_ "\"/>"
- #define G_DBUS_ANNOTATE_DEPRECATED \
- G_DBUS_ANNOTATE("Deprecated", "true")
- #define G_DBUS_ANNOTATE_NOREPLY \
- G_DBUS_ANNOTATE("Method.NoReply", "true")
- static gboolean check_experimental(int flags, int flag)
- {
- if (!(flags & flag))
- return FALSE;
- return !(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL);
- }
- static void generate_interface_xml(GString *gstr, struct interface_data *iface)
- {
- const GDBusMethodTable *method;
- const GDBusSignalTable *signal;
- const GDBusPropertyTable *property;
- for (method = iface->methods; method && method->name; method++) {
- if (check_experimental(method->flags,
- G_DBUS_METHOD_FLAG_EXPERIMENTAL))
- continue;
- g_string_append_printf(gstr, "<method name=\"%s\">",
- method->name);
- print_arguments(gstr, method->in_args, "in");
- print_arguments(gstr, method->out_args, "out");
- if (method->flags & G_DBUS_METHOD_FLAG_DEPRECATED)
- g_string_append_printf(gstr,
- G_DBUS_ANNOTATE_DEPRECATED);
- if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY)
- g_string_append_printf(gstr, G_DBUS_ANNOTATE_NOREPLY);
- g_string_append_printf(gstr, "</method>");
- }
- for (signal = iface->signals; signal && signal->name; signal++) {
- if (check_experimental(signal->flags,
- G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
- continue;
- g_string_append_printf(gstr, "<signal name=\"%s\">",
- signal->name);
- print_arguments(gstr, signal->args, NULL);
- if (signal->flags & G_DBUS_SIGNAL_FLAG_DEPRECATED)
- g_string_append_printf(gstr,
- G_DBUS_ANNOTATE_DEPRECATED);
- g_string_append_printf(gstr, "</signal>\n");
- }
- for (property = iface->properties; property && property->name;
- property++) {
- if (check_experimental(property->flags,
- G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
- continue;
- g_string_append_printf(gstr, "<property name=\"%s\""
- " type=\"%s\" access=\"%s%s\">",
- property->name, property->type,
- property->get ? "read" : "",
- property->set ? "write" : "");
- if (property->flags & G_DBUS_PROPERTY_FLAG_DEPRECATED)
- g_string_append_printf(gstr,
- G_DBUS_ANNOTATE_DEPRECATED);
- g_string_append_printf(gstr, "</property>");
- }
- }
- static void generate_introspection_xml(DBusConnection *conn,
- struct generic_data *data, const char *path)
- {
- GSList *list;
- GString *gstr;
- char **children;
- int i;
- g_free(data->introspect);
- gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
- g_string_append_printf(gstr, "<node>");
- for (list = data->interfaces; list; list = list->next) {
- struct interface_data *iface = list->data;
- g_string_append_printf(gstr, "<interface name=\"%s\">",
- iface->name);
- generate_interface_xml(gstr, iface);
- g_string_append_printf(gstr, "</interface>");
- }
- if (!dbus_connection_list_registered(conn, path, &children))
- goto done;
- for (i = 0; children[i]; i++)
- g_string_append_printf(gstr, "<node name=\"%s\"/>",
- children[i]);
- dbus_free_string_array(children);
- done:
- g_string_append_printf(gstr, "</node>");
- data->introspect = g_string_free(gstr, FALSE);
- }
- static DBusMessage *introspect(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct generic_data *data = user_data;
- DBusMessage *reply;
- if (data->introspect == NULL)
- generate_introspection_xml(connection, data,
- dbus_message_get_path(message));
- reply = dbus_message_new_method_return(message);
- if (reply == NULL)
- return NULL;
- dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
- DBUS_TYPE_INVALID);
- return reply;
- }
- static DBusHandlerResult process_message(DBusConnection *connection,
- DBusMessage *message, const GDBusMethodTable *method,
- void *iface_user_data)
- {
- DBusMessage *reply;
- reply = method->function(connection, message, iface_user_data);
- if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY ||
- dbus_message_get_no_reply(message)) {
- if (reply != NULL)
- dbus_message_unref(reply);
- return DBUS_HANDLER_RESULT_HANDLED;
- }
- if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
- if (reply == NULL)
- return DBUS_HANDLER_RESULT_HANDLED;
- }
- if (reply == NULL)
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
- g_dbus_send_message(connection, reply);
- return DBUS_HANDLER_RESULT_HANDLED;
- }
- static GDBusPendingReply next_pending = 1;
- static GSList *pending_security = NULL;
- static const GDBusSecurityTable *security_table = NULL;
- void g_dbus_pending_success(DBusConnection *connection,
- GDBusPendingReply pending)
- {
- GSList *list;
- for (list = pending_security; list; list = list->next) {
- struct security_data *secdata = list->data;
- if (secdata->pending != pending)
- continue;
- pending_security = g_slist_remove(pending_security, secdata);
- process_message(connection, secdata->message,
- secdata->method, secdata->iface_user_data);
- dbus_message_unref(secdata->message);
- g_free(secdata);
- return;
- }
- }
- void g_dbus_pending_error_valist(DBusConnection *connection,
- GDBusPendingReply pending, const char *name,
- const char *format, va_list args)
- {
- GSList *list;
- for (list = pending_security; list; list = list->next) {
- struct security_data *secdata = list->data;
- if (secdata->pending != pending)
- continue;
- pending_security = g_slist_remove(pending_security, secdata);
- g_dbus_send_error_valist(connection, secdata->message,
- name, format, args);
- dbus_message_unref(secdata->message);
- g_free(secdata);
- return;
- }
- }
- void g_dbus_pending_error(DBusConnection *connection,
- GDBusPendingReply pending,
- const char *name, const char *format, ...)
- {
- va_list args;
- va_start(args, format);
- g_dbus_pending_error_valist(connection, pending, name, format, args);
- va_end(args);
- }
- int polkit_check_authorization(DBusConnection *conn,
- const char *action, gboolean interaction,
- void (*function) (dbus_bool_t authorized,
- void *user_data),
- void *user_data, int timeout);
- struct builtin_security_data {
- DBusConnection *conn;
- GDBusPendingReply pending;
- };
- static void builtin_security_result(dbus_bool_t authorized, void *user_data)
- {
- struct builtin_security_data *data = user_data;
- if (authorized == TRUE)
- g_dbus_pending_success(data->conn, data->pending);
- else
- g_dbus_pending_error(data->conn, data->pending,
- DBUS_ERROR_AUTH_FAILED, NULL);
- g_free(data);
- }
- static void builtin_security_function(DBusConnection *conn,
- const char *action,
- gboolean interaction,
- GDBusPendingReply pending)
- {
- struct builtin_security_data *data;
- data = g_new0(struct builtin_security_data, 1);
- data->conn = conn;
- data->pending = pending;
- if (polkit_check_authorization(conn, action, interaction,
- builtin_security_result, data, 30000) < 0)
- g_dbus_pending_error(conn, pending, NULL, NULL);
- }
- static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
- const GDBusMethodTable *method, void *iface_user_data)
- {
- const GDBusSecurityTable *security;
- for (security = security_table; security && security->privilege;
- security++) {
- struct security_data *secdata;
- gboolean interaction;
- if (security->privilege != method->privilege)
- continue;
- secdata = g_new(struct security_data, 1);
- secdata->pending = next_pending++;
- secdata->message = dbus_message_ref(msg);
- secdata->method = method;
- secdata->iface_user_data = iface_user_data;
- pending_security = g_slist_prepend(pending_security, secdata);
- if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
- interaction = TRUE;
- else
- interaction = FALSE;
- if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
- security->function)
- security->function(conn, security->action,
- interaction, secdata->pending);
- else
- builtin_security_function(conn, security->action,
- interaction, secdata->pending);
- return TRUE;
- }
- return FALSE;
- }
- static GDBusPendingPropertySet next_pending_property = 1;
- static GSList *pending_property_set;
- static struct property_data *remove_pending_property_data(
- GDBusPendingPropertySet id)
- {
- struct property_data *propdata;
- GSList *l;
- for (l = pending_property_set; l != NULL; l = l->next) {
- propdata = l->data;
- if (propdata->id != id)
- continue;
- break;
- }
- if (l == NULL)
- return NULL;
- pending_property_set = g_slist_delete_link(pending_property_set, l);
- return propdata;
- }
- void g_dbus_pending_property_success(GDBusPendingPropertySet id)
- {
- struct property_data *propdata;
- propdata = remove_pending_property_data(id);
- if (propdata == NULL)
- return;
- g_dbus_send_reply(propdata->conn, propdata->message,
- DBUS_TYPE_INVALID);
- dbus_message_unref(propdata->message);
- g_free(propdata);
- }
- void g_dbus_pending_property_error_valist(GDBusPendingReply id,
- const char *name, const char *format,
- va_list args)
- {
- struct property_data *propdata;
- propdata = remove_pending_property_data(id);
- if (propdata == NULL)
- return;
- g_dbus_send_error_valist(propdata->conn, propdata->message, name,
- format, args);
- dbus_message_unref(propdata->message);
- g_free(propdata);
- }
- void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
- const char *format, ...)
- {
- va_list args;
- va_start(args, format);
- g_dbus_pending_property_error_valist(id, name, format, args);
- va_end(args);
- }
- static void reset_parent(gpointer data, gpointer user_data)
- {
- struct generic_data *child = data;
- struct generic_data *parent = user_data;
- child->parent = parent;
- }
- static void append_property(struct interface_data *iface,
- const GDBusPropertyTable *p, DBusMessageIter *dict)
- {
- DBusMessageIter entry, value;
- dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
- &entry);
- dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
- dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
- &value);
- p->get(p, &value, iface->user_data);
- dbus_message_iter_close_container(&entry, &value);
- dbus_message_iter_close_container(dict, &entry);
- }
- static void append_properties(struct interface_data *data,
- DBusMessageIter *iter)
- {
- DBusMessageIter dict;
- const GDBusPropertyTable *p;
- dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
- for (p = data->properties; p && p->name; p++) {
- if (check_experimental(p->flags,
- G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
- continue;
- if (p->get == NULL)
- continue;
- if (p->exists != NULL && !p->exists(p, data->user_data))
- continue;
- append_property(data, p, &dict);
- }
- dbus_message_iter_close_container(iter, &dict);
- }
- static void append_interface(gpointer data, gpointer user_data)
- {
- struct interface_data *iface = data;
- DBusMessageIter *array = user_data;
- DBusMessageIter entry;
- dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
- &entry);
- dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
- append_properties(data, &entry);
- dbus_message_iter_close_container(array, &entry);
- }
- static void emit_interfaces_added(struct generic_data *data)
- {
- DBusMessage *signal;
- DBusMessageIter iter, array;
- if (root == NULL || data == root)
- return;
- signal = dbus_message_new_signal(root->path,
- DBUS_INTERFACE_OBJECT_MANAGER,
- "InterfacesAdded");
- if (signal == NULL)
- return;
- dbus_message_iter_init_append(signal, &iter);
- dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
- &data->path);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_ARRAY_AS_STRING
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
- g_slist_foreach(data->added, append_interface, &array);
- g_slist_free(data->added);
- data->added = NULL;
- dbus_message_iter_close_container(&iter, &array);
- /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
- dbus_connection_send(data->conn, signal, NULL);
- dbus_message_unref(signal);
- }
- static struct interface_data *find_interface(GSList *interfaces,
- const char *name)
- {
- GSList *list;
- if (name == NULL)
- return NULL;
- for (list = interfaces; list; list = list->next) {
- struct interface_data *iface = list->data;
- if (!strcmp(name, iface->name))
- return iface;
- }
- return NULL;
- }
- static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
- DBusMessage *message)
- {
- const char *sig = dbus_message_get_signature(message);
- const char *p = NULL;
- for (; args && args->signature && *sig; args++) {
- p = args->signature;
- for (; *sig && *p; sig++, p++) {
- if (*p != *sig)
- return FALSE;
- }
- }
- if (*sig || (p && *p) || (args && args->signature))
- return FALSE;
- return TRUE;
- }
- static void add_pending(struct generic_data *data)
- {
- guint old_id = data->process_id;
- data->process_id = g_idle_add(process_changes, data);
- if (old_id > 0) {
- /*
- * If the element already had an old idler, remove the old one,
- * no need to re-add it to the pending list.
- */
- g_source_remove(old_id);
- return;
- }
- pending = g_slist_append(pending, data);
- }
- static gboolean remove_interface(struct generic_data *data, const char *name)
- {
- struct interface_data *iface;
- iface = find_interface(data->interfaces, name);
- if (iface == NULL)
- return FALSE;
- process_properties_from_interface(data, iface);
- data->interfaces = g_slist_remove(data->interfaces, iface);
- if (iface->destroy) {
- iface->destroy(iface->user_data);
- iface->user_data = NULL;
- }
- /*
- * Interface being removed was just added, on the same mainloop
- * iteration? Don't send any signal
- */
- if (g_slist_find(data->added, iface)) {
- data->added = g_slist_remove(data->added, iface);
- g_free(iface->name);
- g_free(iface);
- return TRUE;
- }
- if (data->parent == NULL) {
- g_free(iface->name);
- g_free(iface);
- return TRUE;
- }
- data->removed = g_slist_prepend(data->removed, iface->name);
- g_free(iface);
- add_pending(data);
- return TRUE;
- }
- static struct generic_data *invalidate_parent_data(DBusConnection *conn,
- const char *child_path)
- {
- struct generic_data *data = NULL, *child = NULL, *parent = NULL;
- char *parent_path, *slash;
- parent_path = g_strdup(child_path);
- slash = strrchr(parent_path, '/');
- if (slash == NULL)
- goto done;
- if (slash == parent_path && parent_path[1] != '\0')
- parent_path[1] = '\0';
- else
- *slash = '\0';
- if (!strlen(parent_path))
- goto done;
- if (dbus_connection_get_object_path_data(conn, parent_path,
- (void *) &data) == FALSE) {
- goto done;
- }
- parent = invalidate_parent_data(conn, parent_path);
- if (data == NULL) {
- data = parent;
- if (data == NULL)
- goto done;
- }
- g_free(data->introspect);
- data->introspect = NULL;
- if (!dbus_connection_get_object_path_data(conn, child_path,
- (void *) &child))
- goto done;
- if (child == NULL || g_slist_find(data->objects, child) != NULL)
- goto done;
- data->objects = g_slist_prepend(data->objects, child);
- child->parent = data;
- done:
- g_free(parent_path);
- return data;
- }
- static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
- const char *name)
- {
- const GDBusPropertyTable *p;
- for (p = properties; p && p->name; p++) {
- if (strcmp(name, p->name) != 0)
- continue;
- if (check_experimental(p->flags,
- G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
- break;
- return p;
- }
- return NULL;
- }
- static DBusMessage *properties_get(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct generic_data *data = user_data;
- struct interface_data *iface;
- const GDBusPropertyTable *property;
- const char *interface, *name;
- DBusMessageIter iter, value;
- DBusMessage *reply;
- if (!dbus_message_get_args(message, NULL,
- DBUS_TYPE_STRING, &interface,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return NULL;
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "No such interface '%s'", interface);
- property = find_property(iface->properties, name);
- if (property == NULL)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "No such property '%s'", name);
- if (property->exists != NULL &&
- !property->exists(property, iface->user_data))
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "No such property '%s'", name);
- if (property->get == NULL)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "Property '%s' is not readable", name);
- reply = dbus_message_new_method_return(message);
- if (reply == NULL)
- return NULL;
- dbus_message_iter_init_append(reply, &iter);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
- property->type, &value);
- if (!property->get(property, &value, iface->user_data)) {
- dbus_message_unref(reply);
- return NULL;
- }
- dbus_message_iter_close_container(&iter, &value);
- return reply;
- }
- static DBusMessage *properties_get_all(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct generic_data *data = user_data;
- struct interface_data *iface;
- const char *interface;
- DBusMessageIter iter;
- DBusMessage *reply;
- if (!dbus_message_get_args(message, NULL,
- DBUS_TYPE_STRING, &interface,
- DBUS_TYPE_INVALID))
- return NULL;
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "No such interface '%s'", interface);
- reply = dbus_message_new_method_return(message);
- if (reply == NULL)
- return NULL;
- dbus_message_iter_init_append(reply, &iter);
- append_properties(iface, &iter);
- return reply;
- }
- static DBusMessage *properties_set(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct generic_data *data = user_data;
- DBusMessageIter iter, sub;
- struct interface_data *iface;
- const GDBusPropertyTable *property;
- const char *name, *interface;
- struct property_data *propdata;
- gboolean valid_signature;
- char *signature;
- if (!dbus_message_iter_init(message, &iter))
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "No arguments given");
- if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "Invalid argument type: '%c'",
- dbus_message_iter_get_arg_type(&iter));
- dbus_message_iter_get_basic(&iter, &interface);
- dbus_message_iter_next(&iter);
- if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "Invalid argument type: '%c'",
- dbus_message_iter_get_arg_type(&iter));
- dbus_message_iter_get_basic(&iter, &name);
- dbus_message_iter_next(&iter);
- if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "Invalid argument type: '%c'",
- dbus_message_iter_get_arg_type(&iter));
- dbus_message_iter_recurse(&iter, &sub);
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL)
- return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
- "No such interface '%s'", interface);
- property = find_property(iface->properties, name);
- if (property == NULL)
- return g_dbus_create_error(message,
- DBUS_ERROR_UNKNOWN_PROPERTY,
- "No such property '%s'", name);
- if (property->set == NULL)
- return g_dbus_create_error(message,
- DBUS_ERROR_PROPERTY_READ_ONLY,
- "Property '%s' is not writable", name);
- if (property->exists != NULL &&
- !property->exists(property, iface->user_data))
- return g_dbus_create_error(message,
- DBUS_ERROR_UNKNOWN_PROPERTY,
- "No such property '%s'", name);
- signature = dbus_message_iter_get_signature(&sub);
- valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
- dbus_free(signature);
- if (!valid_signature)
- return g_dbus_create_error(message,
- DBUS_ERROR_INVALID_SIGNATURE,
- "Invalid signature for '%s'", name);
- propdata = g_new(struct property_data, 1);
- propdata->id = next_pending_property++;
- propdata->message = dbus_message_ref(message);
- propdata->conn = connection;
- pending_property_set = g_slist_prepend(pending_property_set, propdata);
- property->set(property, &sub, propdata->id, iface->user_data);
- return NULL;
- }
- static const GDBusMethodTable properties_methods[] = {
- { GDBUS_METHOD("Get",
- GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
- GDBUS_ARGS({ "value", "v" }),
- properties_get) },
- { GDBUS_ASYNC_METHOD("Set",
- GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
- { "value", "v" }),
- NULL,
- properties_set) },
- { GDBUS_METHOD("GetAll",
- GDBUS_ARGS({ "interface", "s" }),
- GDBUS_ARGS({ "properties", "a{sv}" }),
- properties_get_all) },
- { }
- };
- static const GDBusSignalTable properties_signals[] = {
- { GDBUS_SIGNAL("PropertiesChanged",
- GDBUS_ARGS({ "interface", "s" },
- { "changed_properties", "a{sv}" },
- { "invalidated_properties", "as"})) },
- { }
- };
- static void append_name(gpointer data, gpointer user_data)
- {
- char *name = data;
- DBusMessageIter *iter = user_data;
- dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
- }
- static void emit_interfaces_removed(struct generic_data *data)
- {
- DBusMessage *signal;
- DBusMessageIter iter, array;
- if (root == NULL || data == root)
- return;
- signal = dbus_message_new_signal(root->path,
- DBUS_INTERFACE_OBJECT_MANAGER,
- "InterfacesRemoved");
- if (signal == NULL)
- return;
- dbus_message_iter_init_append(signal, &iter);
- dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
- &data->path);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
- DBUS_TYPE_STRING_AS_STRING, &array);
- g_slist_foreach(data->removed, append_name, &array);
- g_slist_free_full(data->removed, g_free);
- data->removed = NULL;
- dbus_message_iter_close_container(&iter, &array);
- /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
- dbus_connection_send(data->conn, signal, NULL);
- dbus_message_unref(signal);
- }
- static void remove_pending(struct generic_data *data)
- {
- if (data->process_id > 0) {
- g_source_remove(data->process_id);
- data->process_id = 0;
- }
- pending = g_slist_remove(pending, data);
- }
- static gboolean process_changes(gpointer user_data)
- {
- struct generic_data *data = user_data;
- remove_pending(data);
- if (data->added != NULL)
- emit_interfaces_added(data);
- /* Flush pending properties */
- if (data->pending_prop == TRUE)
- process_property_changes(data);
- if (data->removed != NULL)
- emit_interfaces_removed(data);
- data->process_id = 0;
- return FALSE;
- }
- static void generic_unregister(DBusConnection *connection, void *user_data)
- {
- struct generic_data *data = user_data;
- struct generic_data *parent = data->parent;
- if (parent != NULL)
- parent->objects = g_slist_remove(parent->objects, data);
- if (data->process_id > 0) {
- g_source_remove(data->process_id);
- data->process_id = 0;
- process_changes(data);
- }
- g_slist_foreach(data->objects, reset_parent, data->parent);
- g_slist_free(data->objects);
- dbus_connection_unref(data->conn);
- g_free(data->introspect);
- g_free(data->path);
- g_free(data);
- }
- static DBusHandlerResult generic_message(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct generic_data *data = user_data;
- struct interface_data *iface;
- const GDBusMethodTable *method;
- const char *interface;
- interface = dbus_message_get_interface(message);
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- for (method = iface->methods; method &&
- method->name && method->function; method++) {
- if (dbus_message_is_method_call(message, iface->name,
- method->name) == FALSE)
- continue;
- if (check_experimental(method->flags,
- G_DBUS_METHOD_FLAG_EXPERIMENTAL))
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- if (g_dbus_args_have_signature(method->in_args,
- message) == FALSE)
- continue;
- if (check_privilege(connection, message, method,
- iface->user_data) == TRUE)
- return DBUS_HANDLER_RESULT_HANDLED;
- return process_message(connection, message, method,
- iface->user_data);
- }
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
- static DBusObjectPathVTable generic_table = {
- .unregister_function = generic_unregister,
- .message_function = generic_message,
- };
- static const GDBusMethodTable introspect_methods[] = {
- { GDBUS_METHOD("Introspect", NULL,
- GDBUS_ARGS({ "xml", "s" }), introspect) },
- { }
- };
- static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
- {
- DBusMessageIter array;
- dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_ARRAY_AS_STRING
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
- g_slist_foreach(data->interfaces, append_interface, &array);
- dbus_message_iter_close_container(iter, &array);
- }
- static void append_object(gpointer data, gpointer user_data)
- {
- struct generic_data *child = data;
- DBusMessageIter *array = user_data;
- DBusMessageIter entry;
- dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
- &entry);
- dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
- &child->path);
- append_interfaces(child, &entry);
- dbus_message_iter_close_container(array, &entry);
- g_slist_foreach(child->objects, append_object, user_data);
- }
- static DBusMessage *get_objects(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct generic_data *data = user_data;
- DBusMessage *reply;
- DBusMessageIter iter;
- DBusMessageIter array;
- reply = dbus_message_new_method_return(message);
- if (reply == NULL)
- return NULL;
- dbus_message_iter_init_append(reply, &iter);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_OBJECT_PATH_AS_STRING
- DBUS_TYPE_ARRAY_AS_STRING
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_ARRAY_AS_STRING
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
- &array);
- g_slist_foreach(data->objects, append_object, &array);
- dbus_message_iter_close_container(&iter, &array);
- return reply;
- }
- static const GDBusMethodTable manager_methods[] = {
- { GDBUS_METHOD("GetManagedObjects", NULL,
- GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
- { }
- };
- static const GDBusSignalTable manager_signals[] = {
- { GDBUS_SIGNAL("InterfacesAdded",
- GDBUS_ARGS({ "object", "o" },
- { "interfaces", "a{sa{sv}}" })) },
- { GDBUS_SIGNAL("InterfacesRemoved",
- GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
- { }
- };
- static gboolean add_interface(struct generic_data *data,
- const char *name,
- const GDBusMethodTable *methods,
- const GDBusSignalTable *signals,
- const GDBusPropertyTable *properties,
- void *user_data,
- GDBusDestroyFunction destroy)
- {
- struct interface_data *iface;
- const GDBusMethodTable *method;
- const GDBusSignalTable *signal;
- const GDBusPropertyTable *property;
- for (method = methods; method && method->name; method++) {
- if (!check_experimental(method->flags,
- G_DBUS_METHOD_FLAG_EXPERIMENTAL))
- goto done;
- }
- for (signal = signals; signal && signal->name; signal++) {
- if (!check_experimental(signal->flags,
- G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
- goto done;
- }
- for (property = properties; property && property->name; property++) {
- if (!check_experimental(property->flags,
- G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
- goto done;
- }
- /* Nothing to register */
- return FALSE;
- done:
- iface = g_new0(struct interface_data, 1);
- iface->name = g_strdup(name);
- iface->methods = methods;
- iface->signals = signals;
- iface->properties = properties;
- iface->user_data = user_data;
- iface->destroy = destroy;
- data->interfaces = g_slist_append(data->interfaces, iface);
- if (data->parent == NULL)
- return TRUE;
- data->added = g_slist_append(data->added, iface);
- add_pending(data);
- return TRUE;
- }
- static struct generic_data *object_path_ref(DBusConnection *connection,
- const char *path)
- {
- struct generic_data *data;
- if (dbus_connection_get_object_path_data(connection, path,
- (void *) &data) == TRUE) {
- if (data != NULL) {
- data->refcount++;
- return data;
- }
- }
- data = g_new0(struct generic_data, 1);
- data->conn = dbus_connection_ref(connection);
- data->path = g_strdup(path);
- data->refcount = 1;
- data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
- if (!dbus_connection_register_object_path(connection, path,
- &generic_table, data)) {
- dbus_connection_unref(data->conn);
- g_free(data->path);
- g_free(data->introspect);
- g_free(data);
- return NULL;
- }
- invalidate_parent_data(connection, path);
- add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
- NULL, NULL, data, NULL);
- return data;
- }
- static void object_path_unref(DBusConnection *connection, const char *path)
- {
- struct generic_data *data = NULL;
- if (dbus_connection_get_object_path_data(connection, path,
- (void *) &data) == FALSE)
- return;
- if (data == NULL)
- return;
- data->refcount--;
- if (data->refcount > 0)
- return;
- remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
- remove_interface(data, DBUS_INTERFACE_PROPERTIES);
- invalidate_parent_data(data->conn, data->path);
- dbus_connection_unregister_object_path(data->conn, data->path);
- }
- static gboolean check_signal(DBusConnection *conn, const char *path,
- const char *interface, const char *name,
- const GDBusArgInfo **args)
- {
- struct generic_data *data = NULL;
- struct interface_data *iface;
- const GDBusSignalTable *signal;
- *args = NULL;
- if (!dbus_connection_get_object_path_data(conn, path,
- (void *) &data) || data == NULL) {
- error("dbus_connection_emit_signal: path %s isn't registered",
- path);
- return FALSE;
- }
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL) {
- error("dbus_connection_emit_signal: %s does not implement %s",
- path, interface);
- return FALSE;
- }
- for (signal = iface->signals; signal && signal->name; signal++) {
- if (strcmp(signal->name, name) != 0)
- continue;
- if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
- const char *env = g_getenv("GDBUS_EXPERIMENTAL");
- if (g_strcmp0(env, "1") != 0)
- break;
- }
- *args = signal->args;
- return TRUE;
- }
- error("No signal named %s on interface %s", name, interface);
- return FALSE;
- }
- gboolean g_dbus_register_interface(DBusConnection *connection,
- const char *path, const char *name,
- const GDBusMethodTable *methods,
- const GDBusSignalTable *signals,
- const GDBusPropertyTable *properties,
- void *user_data,
- GDBusDestroyFunction destroy)
- {
- struct generic_data *data;
- if (!dbus_validate_path(path, NULL)) {
- error("Invalid object path: %s", path);
- return FALSE;
- }
- if (!dbus_validate_interface(name, NULL)) {
- error("Invalid interface: %s", name);
- return FALSE;
- }
- data = object_path_ref(connection, path);
- if (data == NULL)
- return FALSE;
- if (find_interface(data->interfaces, name)) {
- object_path_unref(connection, path);
- return FALSE;
- }
- if (!add_interface(data, name, methods, signals, properties, user_data,
- destroy)) {
- object_path_unref(connection, path);
- return FALSE;
- }
- if (properties != NULL && !find_interface(data->interfaces,
- DBUS_INTERFACE_PROPERTIES))
- add_interface(data, DBUS_INTERFACE_PROPERTIES,
- properties_methods, properties_signals, NULL,
- data, NULL);
- g_free(data->introspect);
- data->introspect = NULL;
- return TRUE;
- }
- gboolean g_dbus_unregister_interface(DBusConnection *connection,
- const char *path, const char *name)
- {
- struct generic_data *data = NULL;
- if (path == NULL)
- return FALSE;
- if (dbus_connection_get_object_path_data(connection, path,
- (void *) &data) == FALSE)
- return FALSE;
- if (data == NULL)
- return FALSE;
- if (remove_interface(data, name) == FALSE)
- return FALSE;
- g_free(data->introspect);
- data->introspect = NULL;
- object_path_unref(connection, data->path);
- return TRUE;
- }
- gboolean g_dbus_register_security(const GDBusSecurityTable *security)
- {
- if (security_table != NULL)
- return FALSE;
- security_table = security;
- return TRUE;
- }
- gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
- {
- security_table = NULL;
- return TRUE;
- }
- DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
- const char *format, va_list args)
- {
- char str[1024];
- /* Check if the message can be replied */
- if (dbus_message_get_no_reply(message))
- return NULL;
- if (format)
- vsnprintf(str, sizeof(str), format, args);
- else
- str[0] = '\0';
- return dbus_message_new_error(message, name, str);
- }
- DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
- const char *format, ...)
- {
- va_list args;
- DBusMessage *reply;
- va_start(args, format);
- reply = g_dbus_create_error_valist(message, name, format, args);
- va_end(args);
- return reply;
- }
- DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
- int type, va_list args)
- {
- DBusMessage *reply;
- /* Check if the message can be replied */
- if (dbus_message_get_no_reply(message))
- return NULL;
- reply = dbus_message_new_method_return(message);
- if (reply == NULL)
- return NULL;
- if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
- dbus_message_unref(reply);
- return NULL;
- }
- return reply;
- }
- DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
- {
- va_list args;
- DBusMessage *reply;
- va_start(args, type);
- reply = g_dbus_create_reply_valist(message, type, args);
- va_end(args);
- return reply;
- }
- static void g_dbus_flush(DBusConnection *connection)
- {
- GSList *l;
- for (l = pending; l;) {
- struct generic_data *data = l->data;
- l = l->next;
- if (data->conn != connection)
- continue;
- process_changes(data);
- }
- }
- gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
- {
- dbus_bool_t result = FALSE;
- if (!message)
- return FALSE;
- if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
- dbus_message_set_no_reply(message, TRUE);
- else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
- const char *path = dbus_message_get_path(message);
- const char *interface = dbus_message_get_interface(message);
- const char *name = dbus_message_get_member(message);
- const GDBusArgInfo *args;
- if (!check_signal(connection, path, interface, name, &args))
- goto out;
- }
- /* Flush pending signal to guarantee message order */
- g_dbus_flush(connection);
- result = dbus_connection_send(connection, message, NULL);
- out:
- dbus_message_unref(message);
- return result;
- }
- gboolean g_dbus_send_message_with_reply(DBusConnection *connection,
- DBusMessage *message,
- DBusPendingCall **call, int timeout)
- {
- dbus_bool_t ret;
- /* Flush pending signal to guarantee message order */
- g_dbus_flush(connection);
- ret = dbus_connection_send_with_reply(connection, message, call,
- timeout);
- if (ret == TRUE && call != NULL && *call == NULL) {
- error("Unable to send message (passing fd blocked?)");
- return FALSE;
- }
- return ret;
- }
- gboolean g_dbus_send_error_valist(DBusConnection *connection,
- DBusMessage *message, const char *name,
- const char *format, va_list args)
- {
- DBusMessage *error;
- error = g_dbus_create_error_valist(message, name, format, args);
- if (error == NULL)
- return FALSE;
- return g_dbus_send_message(connection, error);
- }
- gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
- const char *name, const char *format, ...)
- {
- va_list args;
- gboolean result;
- va_start(args, format);
- result = g_dbus_send_error_valist(connection, message, name,
- format, args);
- va_end(args);
- return result;
- }
- gboolean g_dbus_send_reply_valist(DBusConnection *connection,
- DBusMessage *message, int type, va_list args)
- {
- DBusMessage *reply;
- reply = g_dbus_create_reply_valist(message, type, args);
- if (!reply)
- return FALSE;
- return g_dbus_send_message(connection, reply);
- }
- gboolean g_dbus_send_reply(DBusConnection *connection,
- DBusMessage *message, int type, ...)
- {
- va_list args;
- gboolean result;
- va_start(args, type);
- result = g_dbus_send_reply_valist(connection, message, type, args);
- va_end(args);
- return result;
- }
- gboolean g_dbus_emit_signal(DBusConnection *connection,
- const char *path, const char *interface,
- const char *name, int type, ...)
- {
- va_list args;
- gboolean result;
- va_start(args, type);
- result = g_dbus_emit_signal_valist(connection, path, interface,
- name, type, args);
- va_end(args);
- return result;
- }
- gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
- const char *path, const char *interface,
- const char *name, int type, va_list args)
- {
- DBusMessage *signal;
- dbus_bool_t ret;
- const GDBusArgInfo *args_info;
- if (!check_signal(connection, path, interface, name, &args_info))
- return FALSE;
- signal = dbus_message_new_signal(path, interface, name);
- if (signal == NULL) {
- error("Unable to allocate new %s.%s signal", interface, name);
- return FALSE;
- }
- ret = dbus_message_append_args_valist(signal, type, args);
- if (!ret)
- goto fail;
- if (g_dbus_args_have_signature(args_info, signal) == FALSE) {
- error("%s.%s: got unexpected signature '%s'", interface, name,
- dbus_message_get_signature(signal));
- ret = FALSE;
- goto fail;
- }
- return g_dbus_send_message(connection, signal);
- fail:
- dbus_message_unref(signal);
- return ret;
- }
- static void process_properties_from_interface(struct generic_data *data,
- struct interface_data *iface)
- {
- GSList *l;
- DBusMessage *signal;
- DBusMessageIter iter, dict, array;
- GSList *invalidated;
- if (iface->pending_prop == NULL)
- return;
- signal = dbus_message_new_signal(data->path,
- DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
- if (signal == NULL) {
- error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
- ".PropertiesChanged signal");
- return;
- }
- iface->pending_prop = g_slist_reverse(iface->pending_prop);
- dbus_message_iter_init_append(signal, &iter);
- dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
- invalidated = NULL;
- for (l = iface->pending_prop; l != NULL; l = l->next) {
- GDBusPropertyTable *p = l->data;
- if (p->get == NULL)
- continue;
- if (p->exists != NULL && !p->exists(p, iface->user_data)) {
- invalidated = g_slist_prepend(invalidated, p);
- continue;
- }
- append_property(iface, p, &dict);
- }
- dbus_message_iter_close_container(&iter, &dict);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
- DBUS_TYPE_STRING_AS_STRING, &array);
- for (l = invalidated; l != NULL; l = g_slist_next(l)) {
- GDBusPropertyTable *p = l->data;
- dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
- &p->name);
- }
- g_slist_free(invalidated);
- dbus_message_iter_close_container(&iter, &array);
- g_slist_free(iface->pending_prop);
- iface->pending_prop = NULL;
- /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
- dbus_connection_send(data->conn, signal, NULL);
- dbus_message_unref(signal);
- }
- static void process_property_changes(struct generic_data *data)
- {
- GSList *l;
- data->pending_prop = FALSE;
- for (l = data->interfaces; l != NULL; l = l->next) {
- struct interface_data *iface = l->data;
- process_properties_from_interface(data, iface);
- }
- }
- void g_dbus_emit_property_changed_full(DBusConnection *connection,
- const char *path, const char *interface,
- const char *name,
- GDbusPropertyChangedFlags flags)
- {
- const GDBusPropertyTable *property;
- struct generic_data *data;
- struct interface_data *iface;
- if (path == NULL)
- return;
- if (!dbus_connection_get_object_path_data(connection, path,
- (void **) &data) || data == NULL)
- return;
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL)
- return;
- /*
- * If ObjectManager is attached, don't emit property changed if
- * interface is not yet published
- */
- if (root && g_slist_find(data->added, iface))
- return;
- property = find_property(iface->properties, name);
- if (property == NULL) {
- error("Could not find property %s in %p", name,
- iface->properties);
- return;
- }
- if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
- return;
- data->pending_prop = TRUE;
- iface->pending_prop = g_slist_prepend(iface->pending_prop,
- (void *) property);
- if (flags & G_DBUS_PROPERTY_CHANGED_FLAG_FLUSH)
- process_property_changes(data);
- else
- add_pending(data);
- }
- void g_dbus_emit_property_changed(DBusConnection *connection, const char *path,
- const char *interface, const char *name)
- {
- g_dbus_emit_property_changed_full(connection, path, interface, name, 0);
- }
- gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
- const char *interface, DBusMessageIter *iter)
- {
- struct generic_data *data;
- struct interface_data *iface;
- if (path == NULL)
- return FALSE;
- if (!dbus_connection_get_object_path_data(connection, path,
- (void **) &data) || data == NULL)
- return FALSE;
- iface = find_interface(data->interfaces, interface);
- if (iface == NULL)
- return FALSE;
- append_properties(iface, iter);
- return TRUE;
- }
- gboolean g_dbus_attach_object_manager(DBusConnection *connection)
- {
- struct generic_data *data;
- data = object_path_ref(connection, "/");
- if (data == NULL)
- return FALSE;
- add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
- manager_methods, manager_signals,
- NULL, data, NULL);
- root = data;
- return TRUE;
- }
- gboolean g_dbus_detach_object_manager(DBusConnection *connection)
- {
- if (!g_dbus_unregister_interface(connection, "/",
- DBUS_INTERFACE_OBJECT_MANAGER))
- return FALSE;
- root = NULL;
- return TRUE;
- }
- void g_dbus_set_flags(int flags)
- {
- global_flags = flags;
- }
- int g_dbus_get_flags(void)
- {
- return global_flags;
- }
|