diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-11-19 21:12:54 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-11-20 00:01:05 +0100 |
commit | 0171da06ef8bb19c175c5aa8aff8cf95f3de7dc1 (patch) | |
tree | e75c9b00308937a82c841c4b36ba22503d6a44b7 | |
parent | 9b772efb41c2d9f743ba5e96804bdf89b12630d8 (diff) |
busctl: add new "introspect" verb for introspecting objects
-rw-r--r-- | man/busctl.xml | 36 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/busctl-introspect.c | 228 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/busctl-introspect.h | 9 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/busctl.c | 362 |
4 files changed, 565 insertions, 70 deletions
diff --git a/man/busctl.xml b/man/busctl.xml index 730a3344d6..ccbd832a75 100644 --- a/man/busctl.xml +++ b/man/busctl.xml @@ -186,13 +186,10 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. </varlistentry> <varlistentry> - <term><command>tree</command> <arg choice="opt" rep="repeat"><replaceable>SERVICE</replaceable></arg></term> + <term><command>status</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg></term> - <listitem><para>Shows an object tree of one or more - services. If <replaceable>SERVICE</replaceable> is specified, - show object tree of the specified services only. Otherwise, - show all object trees of all services on the bus that acquired - at least one well-known name.</para></listitem> + <listitem><para>Show process information and credentials of a + bus service.</para></listitem> </varlistentry> <varlistentry> @@ -201,7 +198,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. <listitem><para>Dump messages being exchanged. If <replaceable>SERVICE</replaceable> is specified, show messages to or from this endpoint. Otherwise, show all messages on the - bus.</para></listitem> + bus. Use Ctrl-C to terminate dump.</para></listitem> </varlistentry> <varlistentry> @@ -218,24 +215,35 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. </varlistentry> <varlistentry> - <term><command>status</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg></term> + <term><command>tree</command> <arg choice="opt" rep="repeat"><replaceable>SERVICE</replaceable></arg></term> - <listitem><para>Show process information and credentials of a - bus service.</para></listitem> + <listitem><para>Shows an object tree of one or more + services. If <replaceable>SERVICE</replaceable> is specified, + show object tree of the specified services only. Otherwise, + show all object trees of all services on the bus that acquired + at least one well-known name.</para></listitem> + </varlistentry> + + <varlistentry> + <term><command>introspect</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg> <arg choice="plain"><replaceable>OBJECT</replaceable></arg></term> + + <listitem><para>Show interfaces, methods, properties and + signals of the specified object (identified by its path) on + the specified service.</para></listitem> </varlistentry> <varlistentry> - <term><command>call</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg> <arg choice="plain"><replaceable>OBJECT</replaceable></arg> <arg choice="plain"><replaceable>INTERFACE</replaceable></arg> <arg choice="plain"><replaceable>METHOD</replaceable></arg> <arg choice="opt"><replaceable>SIGNATURE</replaceable> <arg choice="opt" rep="repeat"><replaceable>PARAMETERS</replaceable></arg></arg></term> + <term><command>call</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg> <arg choice="plain"><replaceable>OBJECT</replaceable></arg> <arg choice="plain"><replaceable>INTERFACE</replaceable></arg> <arg choice="plain"><replaceable>METHOD</replaceable></arg> <arg choice="opt"><replaceable>SIGNATURE</replaceable> <arg choice="opt" rep="repeat"><replaceable>ARGUMENT</replaceable></arg></arg></term> <listitem><para>Invoke a method and show the response. Takes a service name, object path, interface name and method name. If parameters shall be passed to the method call a signature - string is required, followed by the individual parameters, - individually formatted as textual arguments.</para></listitem> + string is required, followed by the individual arguments, + individually formatted as textual parameters.</para></listitem> </varlistentry> <varlistentry> - <term><command>get-property</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg> <arg choice="plain"><replaceable>OBJECT</replaceable></arg> <arg choice="opt"><replaceable>INTERFACE</replaceable> <arg choice="opt" rep="repeat"><replaceable>PROPERTIES</replaceable></arg></arg></term> + <term><command>get-property</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg> <arg choice="plain"><replaceable>OBJECT</replaceable></arg> <arg choice="opt"><replaceable>INTERFACE</replaceable> <arg choice="opt" rep="repeat"><replaceable>PROPERTY</replaceable></arg></arg></term> <listitem><para>Retrieve the current value one or more object properties. Takes a service name and object path. Optionally diff --git a/src/libsystemd/sd-bus/busctl-introspect.c b/src/libsystemd/sd-bus/busctl-introspect.c index 041702864b..15c10da7e9 100644 --- a/src/libsystemd/sd-bus/busctl-introspect.c +++ b/src/libsystemd/sd-bus/busctl-introspect.c @@ -38,11 +38,30 @@ typedef struct Context { char *member_signature; char *member_result; uint64_t member_flags; + bool member_writable; const char *current; void *xml_state; } Context; +static void context_reset_member(Context *c) { + free(c->member_name); + free(c->member_signature); + free(c->member_result); + + c->member_name = c->member_signature = c->member_result = NULL; + c->member_flags = 0; + c->member_writable = false; +} + +static void context_reset_interface(Context *c) { + free(c->interface_name); + c->interface_name = NULL; + c->interface_flags = 0; + + context_reset_member(c); +} + static int parse_xml_annotation(Context *context, uint64_t *flags) { enum { @@ -105,11 +124,11 @@ static int parse_xml_annotation(Context *context, uint64_t *flags) { } else if (streq_ptr(field, "org.freedesktop.DBus.Property.EmitsChangedSignal")) { if (streq_ptr(value, "const")) - *flags |= SD_BUS_VTABLE_PROPERTY_CONST; + *flags = (*flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)) | SD_BUS_VTABLE_PROPERTY_CONST; else if (streq_ptr(value, "invalidates")) - *flags |= SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION; + *flags = (*flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_CONST)) | SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION; else if (streq_ptr(value, "false")) - *flags |= SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE; + *flags = *flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION); } } @@ -182,7 +201,7 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth STATE_PROPERTY_ACCESS, } state = STATE_NODE; - _cleanup_free_ char *node_path = NULL; + _cleanup_free_ char *node_path = NULL, *argument_type = NULL, *argument_direction = NULL; const char *np = prefix; int r; @@ -225,7 +244,6 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth if (streq_ptr(name, "interface")) state = STATE_INTERFACE; - else if (streq_ptr(name, "node")) { r = parse_xml_node(context, np, n_depth+1); @@ -297,9 +315,10 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth state = STATE_METHOD; else if (streq_ptr(name, "signal")) state = STATE_SIGNAL; - else if (streq_ptr(name, "property")) + else if (streq_ptr(name, "property")) { + context->member_flags |= SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE; state = STATE_PROPERTY; - else if (streq_ptr(name, "annotation")) { + } else if (streq_ptr(name, "annotation")) { r = parse_xml_annotation(context, &context->interface_flags); if (r < 0) return r; @@ -308,11 +327,21 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth return -EINVAL; } } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "interface"))) + (t == XML_TAG_CLOSE && streq_ptr(name, "interface"))) { + + if (n_depth == 0) { + if (context->ops->on_interface) { + r = context->ops->on_interface(context->interface_name, context->interface_flags, context->userdata); + if (r < 0) + return r; + } + + context_reset_interface(context); + } state = STATE_NODE; - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { + } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token in <interface>. (1)"); return -EINVAL; } @@ -321,9 +350,15 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_INTERFACE_NAME: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + if (n_depth == 0) { + free(context->interface_name); + context->interface_name = name; + name = NULL; + } + state = STATE_INTERFACE; - else { + } else { log_error("Unexpected token in <interface>. (2)"); return -EINVAL; } @@ -351,11 +386,21 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth return -EINVAL; } } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "method"))) + (t == XML_TAG_CLOSE && streq_ptr(name, "method"))) { + + if (n_depth == 0) { + if (context->ops->on_method) { + r = context->ops->on_method(context->interface_name, context->member_name, context->member_signature, context->member_result, context->member_flags, context->userdata); + if (r < 0) + return r; + } + + context_reset_member(context); + } state = STATE_INTERFACE; - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { + } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token in <method> (1)."); return -EINVAL; } @@ -364,9 +409,16 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_METHOD_NAME: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + + if (n_depth == 0) { + free(context->member_name); + context->member_name = name; + name = NULL; + } + state = STATE_METHOD; - else { + } else { log_error("Unexpected token in <method> (2)."); return -EINVAL; } @@ -396,11 +448,27 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth return -EINVAL; } } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) + (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) { + + if (n_depth == 0) { + + if (argument_type) { + if (!argument_direction || streq(argument_direction, "in")) { + if (!strextend(&context->member_signature, argument_type, NULL)) + return log_oom(); + } else if (streq(argument_direction, "out")) { + if (!strextend(&context->member_result, argument_type, NULL)) + return log_oom(); + } + } - state = STATE_METHOD; + free(argument_type); + free(argument_direction); + argument_type = argument_direction = NULL; + } - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { + state = STATE_METHOD; + } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token in method <arg>. (1)"); return -EINVAL; } @@ -420,9 +488,13 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_METHOD_ARG_TYPE: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + free(argument_type); + argument_type = name; + name = NULL; + state = STATE_METHOD_ARG; - else { + } else { log_error("Unexpected token in method <arg>. (3)"); return -EINVAL; } @@ -431,9 +503,13 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_METHOD_ARG_DIRECTION: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + free(argument_direction); + argument_direction = name; + name = NULL; + state = STATE_METHOD_ARG; - else { + } else { log_error("Unexpected token in method <arg>. (4)"); return -EINVAL; } @@ -461,11 +537,21 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth return -EINVAL; } } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "signal"))) + (t == XML_TAG_CLOSE && streq_ptr(name, "signal"))) { + + if (n_depth == 0) { + if (context->ops->on_signal) { + r = context->ops->on_signal(context->interface_name, context->member_name, context->member_signature, context->member_flags, context->userdata); + if (r < 0) + return r; + } + + context_reset_member(context); + } state = STATE_INTERFACE; - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { + } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token in <signal>. (1)"); return -EINVAL; } @@ -474,9 +560,16 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_SIGNAL_NAME: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + + if (n_depth == 0) { + free(context->member_name); + context->member_name = name; + name = NULL; + } + state = STATE_SIGNAL; - else { + } else { log_error("Unexpected token in <signal>. (2)"); return -EINVAL; } @@ -505,11 +598,18 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth return -EINVAL; } } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) + (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) { - state = STATE_SIGNAL; + if (argument_type) { + if (!strextend(&context->member_signature, argument_type, NULL)) + return log_oom(); - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { + free(argument_type); + argument_type = NULL; + } + + state = STATE_SIGNAL; + } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token in signal <arg> (1)."); return -EINVAL; } @@ -529,9 +629,13 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_SIGNAL_ARG_TYPE: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + free(argument_type); + argument_type = name; + name = NULL; + state = STATE_SIGNAL_ARG; - else { + } else { log_error("Unexpected token in signal <arg> (3)."); return -EINVAL; } @@ -563,11 +667,21 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth } } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "property"))) + (t == XML_TAG_CLOSE && streq_ptr(name, "property"))) { + + if (n_depth == 0) { + if (context->ops->on_property) { + r = context->ops->on_property(context->interface_name, context->member_name, context->member_signature, context->member_writable, context->member_flags, context->userdata); + if (r < 0) + return r; + } + + context_reset_member(context); + } state = STATE_INTERFACE; - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { + } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token in <property>. (1)"); return -EINVAL; } @@ -576,9 +690,15 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_PROPERTY_NAME: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + + if (n_depth == 0) { + free(context->member_name); + context->member_name = name; + name = NULL; + } state = STATE_PROPERTY; - else { + } else { log_error("Unexpected token in <property>. (2)"); return -EINVAL; } @@ -587,9 +707,16 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_PROPERTY_TYPE: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + + if (n_depth == 0) { + free(context->member_signature); + context->member_signature = name; + name = NULL; + } + state = STATE_PROPERTY; - else { + } else { log_error("Unexpected token in <property>. (3)"); return -EINVAL; } @@ -598,9 +725,13 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth case STATE_PROPERTY_ACCESS: - if (t == XML_ATTRIBUTE_VALUE) + if (t == XML_ATTRIBUTE_VALUE) { + + if (streq(name, "readwrite") || streq(name, "write")) + context->member_writable = true; + state = STATE_PROPERTY; - else { + } else { log_error("Unexpected token in <property>. (4)"); return -EINVAL; } @@ -629,27 +760,34 @@ int parse_xml_introspect(const char *prefix, const char *xml, const XMLIntrospec r = xml_tokenize(&context.current, &name, &context.xml_state, NULL); if (r < 0) { log_error("XML parse error"); - return r; + goto finish; } - if (r == XML_END) + if (r == XML_END) { + r = 0; break; + } if (r == XML_TAG_OPEN) { if (streq(name, "node")) { r = parse_xml_node(&context, prefix, 0); if (r < 0) - return r; + goto finish; } else { log_error("Unexpected tag '%s' in introspection data.", name); - return -EBADMSG; + r = -EBADMSG; + goto finish; } } else if (r != XML_TEXT || !in_charset(name, WHITESPACE)) { log_error("Unexpected token."); - return -EINVAL; + r = -EBADMSG; + goto finish; } } - return 0; +finish: + context_reset_interface(&context); + + return r; } diff --git a/src/libsystemd/sd-bus/busctl-introspect.h b/src/libsystemd/sd-bus/busctl-introspect.h index 7c4cd6e5fd..9bea43717d 100644 --- a/src/libsystemd/sd-bus/busctl-introspect.h +++ b/src/libsystemd/sd-bus/busctl-introspect.h @@ -25,11 +25,10 @@ typedef struct XMLIntrospectOps { int (*on_path)(const char *path, void *userdata); - int (*on_interface)(const char *name, uint64_t flags); - int (*on_method)(const char *name, const char *signature, const char *result, uint64_t flags, void *userdata); - int (*on_signal)(const char *name, const char *signature, uint64_t flags, void *userdata); - int (*on_property)(const char *name, const char *signature, uint64_t flags); - int (*on_writable_property)(const char *name, const char *signature, uint64_t flags); + int (*on_interface)(const char *name, uint64_t flags, void *userdata); + int (*on_method)(const char *interface, const char *name, const char *signature, const char *result, uint64_t flags, void *userdata); + int (*on_signal)(const char *interface, const char *name, const char *signature, uint64_t flags, void *userdata); + int (*on_property)(const char *interface, const char *name, const char *signature, bool writable, uint64_t flags, void *userdata); } XMLIntrospectOps; int parse_xml_introspect(const char *prefix, const char *xml, const XMLIntrospectOps *ops, void *userdata); diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index c58a97bc14..bb89ab9d3c 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -319,7 +319,7 @@ static int on_path(const char *path, void *userdata) { } static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *paths) { - const XMLIntrospectOps ops = { + static const XMLIntrospectOps ops = { .on_path = on_path, }; @@ -338,7 +338,6 @@ static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *p if (r < 0) return bus_log_parse_error(r); - /* fputs(xml, stdout); */ return parse_xml_introspect(path, xml, &ops, paths); } @@ -470,6 +469,353 @@ static int tree(sd_bus *bus, char **argv) { return r; } +typedef struct Member { + const char *type; + char *interface; + char *name; + char *signature; + char *result; + bool writable; + uint64_t flags; +} Member; + +static unsigned long member_hash_func(const void *p, const uint8_t hash_key[]) { + const Member *m = p; + unsigned long ul; + + assert(m); + assert(m->type); + + ul = string_hash_func(m->type, hash_key); + + if (m->name) + ul ^= string_hash_func(m->name, hash_key); + + if (m->interface) + ul ^= string_hash_func(m->interface, hash_key); + + return ul; +} + +static int member_compare_func(const void *a, const void *b) { + const Member *x = a, *y = b; + int d; + + assert(x); + assert(y); + assert(x->type); + assert(y->type); + + if (!x->interface && y->interface) + return -1; + if (x->interface && !y->interface) + return 1; + if (x->interface && y->interface) { + d = strcmp(x->interface, y->interface); + if (d != 0) + return d; + } + + d = strcmp(x->type, y->type); + if (d != 0) + return d; + + if (!x->name && y->name) + return -1; + if (x->name && !y->name) + return 1; + if (x->name && y->name) + return strcmp(x->name, y->name); + + return 0; +} + +static int member_compare_funcp(const void *a, const void *b) { + const Member *const * x = (const Member *const *) a, * const *y = (const Member *const *) b; + + return member_compare_func(*x, *y); +} + +static void member_free(Member *m) { + if (!m) + return; + + free(m->interface); + free(m->name); + free(m->signature); + free(m->result); + free(m); +} + +DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free); + +static void member_set_free(Set *s) { + Member *m; + + while ((m = set_steal_first(s))) + member_free(m); + + set_free(s); +} + +DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, member_set_free); + +static int on_interface(const char *interface, uint64_t flags, void *userdata) { + _cleanup_(member_freep) Member *m; + Set *members = userdata; + int r; + + assert(interface); + assert(members); + + m = new0(Member, 1); + if (!m) + return log_oom(); + + m->type = "interface"; + m->flags = flags; + + r = free_and_strdup(&m->interface, interface); + if (r < 0) + return log_oom(); + + r = set_put(members, m); + if (r <= 0) { + log_error("Duplicate interface"); + return -EINVAL; + } + + m = NULL; + return 0; +} + +static int on_method(const char *interface, const char *name, const char *signature, const char *result, uint64_t flags, void *userdata) { + _cleanup_(member_freep) Member *m; + Set *members = userdata; + int r; + + assert(interface); + assert(name); + + m = new0(Member, 1); + if (!m) + return log_oom(); + + m->type = "method"; + m->flags = flags; + + r = free_and_strdup(&m->interface, interface); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->name, name); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->signature, signature); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->result, result); + if (r < 0) + return log_oom(); + + r = set_put(members, m); + if (r <= 0) { + log_error("Duplicate method"); + return -EINVAL; + } + + m = NULL; + return 0; +} + +static int on_signal(const char *interface, const char *name, const char *signature, uint64_t flags, void *userdata) { + _cleanup_(member_freep) Member *m; + Set *members = userdata; + int r; + + assert(interface); + assert(name); + + m = new0(Member, 1); + if (!m) + return log_oom(); + + m->type = "signal"; + m->flags = flags; + + r = free_and_strdup(&m->interface, interface); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->name, name); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->signature, signature); + if (r < 0) + return log_oom(); + + r = set_put(members, m); + if (r <= 0) { + log_error("Duplicate signal"); + return -EINVAL; + } + + m = NULL; + return 0; +} + +static int on_property(const char *interface, const char *name, const char *signature, bool writable, uint64_t flags, void *userdata) { + _cleanup_(member_freep) Member *m; + Set *members = userdata; + int r; + + assert(interface); + assert(name); + + m = new0(Member, 1); + if (!m) + return log_oom(); + + m->type = "property"; + m->flags = flags; + m->writable = writable; + + r = free_and_strdup(&m->interface, interface); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->name, name); + if (r < 0) + return log_oom(); + + r = free_and_strdup(&m->signature, signature); + if (r < 0) + return log_oom(); + + r = set_put(members, m); + if (r <= 0) { + log_error("Duplicate property"); + return -EINVAL; + } + + m = NULL; + return 0; +} + +static const char *strdash(const char *x) { + return isempty(x) ? "-" : x; +} + +static int introspect(sd_bus *bus, char **argv) { + static const struct hash_ops member_hash_ops = { + .hash = member_hash_func, + .compare = member_compare_func, + }; + + static const XMLIntrospectOps ops = { + .on_interface = on_interface, + .on_method = on_method, + .on_signal = on_signal, + .on_property = on_property, + }; + + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(member_set_freep) Set *members = NULL; + Iterator i; + Member *m; + const char *xml; + int r; + unsigned name_width, type_width, signature_width, result_width; + Member **sorted = NULL; + unsigned k = 0, j; + + if (strv_length(argv) != 3) { + log_error("Requires service and object path argument."); + return -EINVAL; + } + + members = set_new(&member_hash_ops); + if (!members) + return log_oom(); + + r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, ""); + if (r < 0) { + log_error("Failed to introspect object %s of service %s: %s", argv[2], argv[1], bus_error_message(&error, r)); + return r; + } + + r = sd_bus_message_read(reply, "s", &xml); + if (r < 0) + return bus_log_parse_error(r); + + r = parse_xml_introspect(argv[2], xml, &ops, members); + if (r < 0) + return r; + + pager_open_if_enabled(); + + name_width = strlen("NAME"); + type_width = strlen("TYPE"); + signature_width = strlen("SIGNATURE"); + result_width = strlen("RESULT"); + + sorted = newa(Member*, set_size(members)); + + SET_FOREACH(m, members, i) { + if (m->interface) + name_width = MAX(name_width, strlen(m->interface)); + if (m->name) + name_width = MAX(name_width, strlen(m->name) + 1); + if (m->type) + type_width = MAX(type_width, strlen(m->type)); + if (m->signature) + signature_width = MAX(signature_width, strlen(m->signature)); + if (m->result) + result_width = MAX(result_width, strlen(m->result)); + + sorted[k++] = m; + } + + assert(k == set_size(members)); + qsort(sorted, k, sizeof(Member*), member_compare_funcp); + + printf("%-*s %-*s %-*s %-*s %s\n", + (int) name_width, "NAME", + (int) type_width, "TYPE", + (int) signature_width, "SIGNATURE", + (int) result_width, "RESULT", + "FLAGS"); + + for (j = 0; j < k; j++) { + bool is_interface; + + m = sorted[j]; + + is_interface = streq(m->type, "interface"); + + printf("%s%s%-*s%s %-*s %-*s %-*s%s%s%s%s%s%s\n", + is_interface ? ansi_highlight() : "", + is_interface ? "" : ".", + - !is_interface + (int) name_width, strdash(streq_ptr(m->type, "interface") ? m->interface : m->name), + is_interface ? ansi_highlight_off() : "", + (int) type_width, strdash(m->type), + (int) signature_width, strdash(m->signature), + (int) result_width, strdash(m->result), + (m->flags & SD_BUS_VTABLE_DEPRECATED) ? " deprecated" : (m->flags || m->writable ? "" : " -"), + (m->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ? " no-reply" : "", + (m->flags & SD_BUS_VTABLE_PROPERTY_CONST) ? " const" : "", + (m->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) ? " emits-change" : "", + (m->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) ? " emits-invalidation" : "", + m->writable ? " writable" : ""); + } + + return 0; +} + static int message_dump(sd_bus_message *m, FILE *f) { return bus_message_dump(m, f, BUS_MESSAGE_DUMP_WITH_HEADER); } @@ -1021,13 +1367,14 @@ static int help(void) { " --quiet Don't show method call reply\n\n" "Commands:\n" " list List bus names\n" - " tree [SERVICE...] Show object tree of service\n" + " status SERVICE Show service name status\n" " monitor [SERVICE...] Show bus traffic\n" " capture [SERVICE...] Capture bus traffic as pcap\n" - " status SERVICE Show service name status\n" - " call SERVICE PATH INTERFACE METHOD [SIGNATURE [ARGUMENTS...]]\n" + " tree [SERVICE...] Show object tree of service\n" + " introspect SERVICE PATH\n" + " call SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]\n" " Call a method\n" - " get-property SERVICE PATH [INTERFACE [PROPERTY...]]\n" + " get-property SERVICE OBJECT [INTERFACE [PROPERTY...]]\n" " Get property value\n" " help Show this help\n" , program_invocation_short_name); @@ -1197,6 +1544,9 @@ static int busctl_main(sd_bus *bus, int argc, char *argv[]) { if (streq(argv[optind], "tree")) return tree(bus, argv + optind); + if (streq(argv[optind], "introspect")) + return introspect(bus, argv + optind); + if (streq(argv[optind], "call")) return call(bus, argv + optind); |