-DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
- DBusError error;
- DBusMessage *reply = NULL;
- int r;
-
- assert(m);
- assert(message);
-
- dbus_error_init(&error);
-
- if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
-
- if (!(reply = dbus_message_new_method_return(message)))
- goto oom;
-
- if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
- const char *interface, *property;
- const BusProperty *p;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &interface,
- DBUS_TYPE_STRING, &property,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(m, c, message, &error, -EINVAL);
-
- for (p = properties; p->property; p++)
- if (streq(p->interface, interface) && streq(p->property, property))
- break;
-
- if (p->property) {
- DBusMessageIter iter, sub;
-
- if (!(reply = dbus_message_new_method_return(message)))
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
- goto oom;
-
- if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
-
- if (r == -ENOMEM)
- goto oom;
-
- dbus_message_unref(reply);
- return bus_send_error_reply(m, c, message, NULL, r);
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
- }
- } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
- const char *interface;
- const BusProperty *p;
- DBusMessageIter iter, sub, sub2, sub3;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &interface,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(m, c, message, &error, -EINVAL);
-
- if (!(reply = dbus_message_new_method_return(message)))
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
- goto oom;
-
- for (p = properties; p->property; p++) {
- if (interface[0] && !streq(p->interface, interface))
- continue;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
- !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
- goto oom;
-
- if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
-
- if (r == -ENOMEM)
- goto oom;
-
- dbus_message_unref(reply);
- return bus_send_error_reply(m, c, message, NULL, r);
- }
-
- if (!dbus_message_iter_close_container(&sub2, &sub3) ||
- !dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
- }
-
- if (reply) {
- if (!dbus_connection_send(c, reply, NULL))
- goto oom;
-
- dbus_message_unref(reply);
- return DBUS_HANDLER_RESULT_HANDLED;
- }
-
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
-oom:
- if (reply)
- dbus_message_unref(reply);
-
- dbus_error_free(&error);
-
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
-}
-
-static const char *error_to_dbus(int error) {
-
- switch(error) {
-
- case -EINVAL:
- return DBUS_ERROR_INVALID_ARGS;
-
- case -ENOMEM:
- return DBUS_ERROR_NO_MEMORY;
-
- case -EPERM:
- case -EACCES:
- return DBUS_ERROR_ACCESS_DENIED;
-
- case -ESRCH:
- return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
-
- case -ENOENT:
- return DBUS_ERROR_FILE_NOT_FOUND;
-
- case -EEXIST:
- return DBUS_ERROR_FILE_EXISTS;
-
- case -ETIMEDOUT:
- return DBUS_ERROR_TIMEOUT;
-
- case -EIO:
- return DBUS_ERROR_IO_ERROR;
-
- case -ENETRESET:
- case -ECONNABORTED:
- case -ECONNRESET:
- return DBUS_ERROR_DISCONNECTED;
- }
-
- return DBUS_ERROR_FAILED;
-}
-
-DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *bus_error, int error) {
- DBusMessage *reply = NULL;
- const char *name, *text;
-
- if (bus_error && dbus_error_is_set(bus_error)) {
- name = bus_error->name;
- text = bus_error->message;
- } else {
- name = error_to_dbus(error);
- text = strerror(-error);
- }
-
- if (!(reply = dbus_message_new_error(message, name, text)))
- goto oom;
-
- if (!dbus_connection_send(c, reply, NULL))
- goto oom;
-
- dbus_message_unref(reply);
-
- if (bus_error)
- dbus_error_free(bus_error);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-
-oom:
- if (reply)
- dbus_message_unref(reply);
-
- if (bus_error)
- dbus_error_free(bus_error);
-
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
-}
-