[systemd-commits] 5 commits - man/busctl.xml man/hostnamectl.xml src/libsystemd src/shared src/systemd

Lennart Poettering lennart at kemper.freedesktop.org
Fri Nov 14 04:21:26 PST 2014


 man/busctl.xml                      |   29 ++-
 man/hostnamectl.xml                 |    1 
 src/libsystemd/libsystemd.sym.m4    |    1 
 src/libsystemd/sd-bus/bus-message.c |    6 
 src/libsystemd/sd-bus/busctl.c      |  341 +++++++++++++++++++++++++++++++++++-
 src/shared/util.c                   |   40 ++++
 src/shared/util.h                   |    3 
 src/systemd/sd-bus.h                |    1 
 8 files changed, 406 insertions(+), 16 deletions(-)

New commits:
commit 56c8b52d4dc652bf15aa8c2ef44d35b166b419c1
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Nov 14 13:18:33 2014 +0100

    busctl: various tweaks to "busctl tree" output

diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c
index ea8425a..2bf890e 100644
--- a/src/libsystemd/sd-bus/busctl.c
+++ b/src/libsystemd/sd-bus/busctl.c
@@ -291,8 +291,15 @@ static void print_tree(const char *prefix, char **l) {
                 return;
         }
 
-        if (!strv_isempty(l))
-                printf("%s/\n", prefix);
+        if (strv_isempty(l)) {
+                printf("No objects discovered.\n");
+                return;
+        }
+
+        if (streq(l[0], "/") && !l[1]) {
+                printf("Only root object discovered.\n");
+                return;
+        }
 
         print_subtree(prefix, "/", l);
 }
@@ -964,7 +971,7 @@ static int tree(sd_bus *bus, char **argv) {
 
         if (strv_length(argv) <= 1) {
                 _cleanup_strv_free_ char **names = NULL;
-                bool not_first = true;
+                bool not_first = false;
 
                 r = sd_bus_list_names(bus, &names, NULL);
                 if (r < 0) {
@@ -986,9 +993,9 @@ static int tree(sd_bus *bus, char **argv) {
                         if (not_first)
                                 printf("\n");
 
-                        printf("Service %s:\n", *i);
+                        printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_highlight_off());
 
-                        q = tree_one(bus, *i, "\t");
+                        q = tree_one(bus, *i, NULL);
                         if (q < 0 && r >= 0)
                                 r = q;
 
@@ -1004,7 +1011,7 @@ static int tree(sd_bus *bus, char **argv) {
                                 printf("\n");
 
                         if (argv[2])
-                                printf("Service %s:\n", *i);
+                                printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_highlight_off());
 
                         q = tree_one(bus, *i, NULL);
                         if (q < 0 && r >= 0)

commit 86349ffe49092345269762e3274121133604c3d4
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Nov 14 13:11:45 2014 +0100

    man: various tweaks for busctl(1) man page

diff --git a/man/busctl.xml b/man/busctl.xml
index 783b2a2..6f940c3 100644
--- a/man/busctl.xml
+++ b/man/busctl.xml
@@ -147,7 +147,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
         <term><option>--list</option></term>
 
         <listitem>
-          <para>When used with the <command>tree</command> shows a
+          <para>When used with the <command>tree</command> command shows a
           flat list of object paths instead of a tree.</para>
         </listitem>
       </varlistentry>
@@ -186,26 +186,26 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
       </varlistentry>
 
       <varlistentry>
-        <term><command>tree</command>  <arg choice="opt" rep="repeat"><replaceable>NAME</replaceable></arg></term>
+        <term><command>tree</command> <arg choice="opt" rep="repeat"><replaceable>SERVICE</replaceable></arg></term>
 
         <listitem><para>Shows an object tree of one or more
-        services. If <replaceable>NAME</replaceable> is specified,
+        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>monitor</command>  <arg choice="opt" rep="repeat"><replaceable>NAME</replaceable></arg></term>
+        <term><command>monitor</command> <arg choice="opt" rep="repeat"><replaceable>SERVICE</replaceable></arg></term>
 
         <listitem><para>Dump messages being exchanged. If
-        <replaceable>NAME</replaceable> is specified, show messages
+        <replaceable>SERVICE</replaceable> is specified, show messages
         to or from this endpoint. Otherwise, show all messages on the
         bus.</para></listitem>
       </varlistentry>
 
       <varlistentry>
-        <term><command>capture</command>  <arg choice="opt" rep="repeat"><replaceable>NAME</replaceable></arg></term>
+        <term><command>capture</command> <arg choice="opt" rep="repeat"><replaceable>SERVICE</replaceable></arg></term>
 
         <listitem><para>Similar to <command>monitor</command> but
         writes the output in pcap format (for details see the <ulink
@@ -218,7 +218,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
       </varlistentry>
 
       <varlistentry>
-        <term><command>status</command>  <arg choice="plain"><replaceable>NAME</replaceable></arg></term>
+        <term><command>status</command> <arg choice="plain"><replaceable>SERVICE</replaceable></arg></term>
 
         <listitem><para>Show process information and credentials of a
         bus service.</para></listitem>

commit 781fa93815fafd02b5287ef5781b92ef7b99973b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Nov 14 13:11:10 2014 +0100

    busctl: add new "call" command to invoke methods on a service

diff --git a/man/busctl.xml b/man/busctl.xml
index f9bcea3..783b2a2 100644
--- a/man/busctl.xml
+++ b/man/busctl.xml
@@ -152,6 +152,15 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--quiet</option></term>
+
+        <listitem>
+          <para>When used with the <command>call</command> command suppresses
+          display of the response message.</para>
+        </listitem>
+      </varlistentry>
+
       <xi:include href="user-system-options.xml" xpointer="user" />
       <xi:include href="user-system-options.xml" xpointer="system" />
       <xi:include href="user-system-options.xml" xpointer="host" />
@@ -216,6 +225,12 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
       </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>
+
+        <listitem><para>Invoke a method and show the response.</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><command>help</command></term>
 
         <listitem><para>Show command syntax help.</para></listitem>
diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c
index b241466..ea8425a 100644
--- a/src/libsystemd/sd-bus/busctl.c
+++ b/src/libsystemd/sd-bus/busctl.c
@@ -34,6 +34,7 @@
 #include "bus-internal.h"
 #include "bus-util.h"
 #include "bus-dump.h"
+#include "bus-signature.h"
 
 static bool arg_no_pager = false;
 static bool arg_legend = true;
@@ -48,6 +49,7 @@ static char *arg_host = NULL;
 static bool arg_user = false;
 static size_t arg_snaplen = 4096;
 static bool arg_list = false;
+static bool arg_quiet = false;
 
 static void pager_open_if_enabled(void) {
 
@@ -1139,6 +1141,309 @@ static int status(sd_bus *bus, char *argv[]) {
         return 0;
 }
 
+static int message_append_cmdline(sd_bus_message *m, const char *signature, char ***x) {
+        char **p;
+        int r;
+
+        assert(m);
+        assert(signature);
+        assert(x);
+
+        p = *x;
+
+        for (;;) {
+                const char *v;
+                char t;
+
+                t = *signature;
+                v = *p;
+
+                if (t == 0)
+                        break;
+                if (!v) {
+                        log_error("Too few parameters for signature.");
+                        return -EINVAL;
+                }
+
+                signature++;
+                p++;
+
+                switch (t) {
+
+                case SD_BUS_TYPE_BOOLEAN:
+
+                        r = parse_boolean(v);
+                        if (r < 0) {
+                                log_error("Failed to parse as boolean: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &r);
+                        break;
+
+                case SD_BUS_TYPE_BYTE: {
+                        uint8_t z;
+
+                        r = safe_atou8(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as byte (unsigned 8bit integer): %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_INT16: {
+                        int16_t z;
+
+                        r = safe_atoi16(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as signed 16bit integer: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_UINT16: {
+                        uint16_t z;
+
+                        r = safe_atou16(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as unsigned 16bit integer: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_INT32: {
+                        int32_t z;
+
+                        r = safe_atoi32(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as signed 32bit integer: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_UINT32: {
+                        uint32_t z;
+
+                        r = safe_atou32(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as unsigned 32bit integer: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_INT64: {
+                        int64_t z;
+
+                        r = safe_atoi64(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as signed 64bit integer: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_UINT64: {
+                        uint64_t z;
+
+                        r = safe_atou64(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as unsigned 64bit integer: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+
+                case SD_BUS_TYPE_DOUBLE: {
+                        double z;
+
+                        r = safe_atod(v, &z);
+                        if (r < 0) {
+                                log_error("Failed to parse as double precision floating point: %s", v);
+                                return r;
+                        }
+
+                        r = sd_bus_message_append_basic(m, t, &z);
+                        break;
+                }
+
+                case SD_BUS_TYPE_STRING:
+                case SD_BUS_TYPE_OBJECT_PATH:
+                case SD_BUS_TYPE_SIGNATURE:
+
+                        r = sd_bus_message_append_basic(m, t, v);
+                        break;
+
+                case SD_BUS_TYPE_ARRAY: {
+                        uint32_t n;
+                        size_t k;
+
+                        r = safe_atou32(v, &n);
+                        if (r < 0) {
+                                log_error("Failed to parse number of array entries: %s", v);
+                                return r;
+                        }
+
+                        r = signature_element_length(signature, &k);
+                        if (r < 0) {
+                                log_error("Invalid array signature.");
+                                return r;
+                        }
+
+                        {
+                                unsigned i;
+                                char s[k + 1];
+                                memcpy(s, signature, k);
+                                s[k] = 0;
+
+                                r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
+                                if (r < 0)
+                                        return bus_log_create_error(r);
+
+                                for (i = 0; i < n; i++) {
+                                        r = message_append_cmdline(m, s, &p);
+                                        if (r < 0)
+                                                return r;
+                                }
+                        }
+
+                        signature += k;
+
+                        r = sd_bus_message_close_container(m);
+                        break;
+                }
+
+                case SD_BUS_TYPE_VARIANT:
+                        r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, v);
+                        if (r < 0)
+                                return bus_log_create_error(r);
+
+                        r = message_append_cmdline(m, v, &p);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_message_close_container(m);
+                        break;
+
+                case SD_BUS_TYPE_STRUCT_BEGIN:
+                case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
+                        size_t k;
+
+                        signature--;
+                        p--;
+
+                        r = signature_element_length(signature, &k);
+                        if (r < 0) {
+                                log_error("Invalid struct/dict entry signature.");
+                                return r;
+                        }
+
+                        {
+                                char s[k-1];
+                                memcpy(s, signature + 1, k - 2);
+                                s[k - 2] = 0;
+
+                                r = sd_bus_message_open_container(m, t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
+                                if (r < 0)
+                                        return bus_log_create_error(r);
+
+                                r = message_append_cmdline(m, s, &p);
+                                if (r < 0)
+                                        return r;
+                        }
+
+                        signature += k;
+
+                        r = sd_bus_message_close_container(m);
+                        break;
+                }
+
+                case SD_BUS_TYPE_UNIX_FD:
+                        log_error("UNIX file descriptor not supported as type.");
+                        return -EINVAL;
+
+                default:
+                        log_error("Unknown signature type %c.", t);
+                        return -EINVAL;
+                }
+
+                if (r < 0)
+                        return bus_log_create_error(r);
+        }
+
+        *x = p;
+        return 0;
+}
+
+static int call(sd_bus *bus, char *argv[]) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        int r;
+
+        assert(bus);
+
+        if (strv_length(argv) < 5) {
+                log_error("Expects at least four arguments.");
+                return -EINVAL;
+        }
+
+        r = sd_bus_message_new_method_call(bus, &m, argv[1], argv[2], argv[3], argv[4]);
+        if (r < 0) {
+                log_error("Failed to prepare bus message: %s", strerror(-r));
+                return r;
+        }
+
+        if (!isempty(argv[5])) {
+                char **p;
+
+                p = argv+6;
+
+                r = message_append_cmdline(m, argv[5], &p);
+                if (r < 0)
+                        return r;
+
+                if (*p) {
+                        log_error("Too many parameters for signature.");
+                        return -EINVAL;
+                }
+        }
+
+        r = sd_bus_call(bus, m, 0, &error, &reply);
+        if (r < 0) {
+                log_error("%s", bus_error_message(&error, r));
+                return r;
+        }
+
+        r = sd_bus_message_is_empty(reply);
+        if (r < 0)
+                return bus_log_parse_error(r);
+        if (r == 0 && !arg_quiet) {
+                pager_open_if_enabled();
+                bus_message_dump(reply, stdout, false);
+        }
+
+        return 0;
+}
+
 static int help(void) {
         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
                "Introspect the bus.\n\n"
@@ -1156,13 +1461,16 @@ static int help(void) {
                "     --acquired           Only show acquired names\n"
                "     --activatable        Only show activatable names\n"
                "     --match=MATCH        Only show matching messages\n"
-               "     --list               Don't show tree, but simple object path list\n\n"
+               "     --list               Don't show tree, but simple object path list\n"
+               "     --quiet              Don't show method call reply\n\n"
                "Commands:\n"
                "  list                    List bus names\n"
                "  tree [SERVICE...]       Show object tree of service\n"
                "  monitor [SERVICE...]    Show bus traffic\n"
                "  capture [SERVICE...]    Capture bus traffic as pcap\n"
-               "  status NAME             Show name status\n"
+               "  status SERVICE          Show service name status\n"
+               "  call SERVICE PATH INTERFACE METHOD [SIGNATURE] [ARGUMENTS...]\n"
+               "                          Call a method\n"
                "  help                    Show this help\n"
                , program_invocation_short_name);
 
@@ -1204,6 +1512,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "machine",      required_argument, NULL, 'M'              },
                 { "size",         required_argument, NULL, ARG_SIZE         },
                 { "list",         no_argument,       NULL, ARG_LIST         },
+                { "quiet",        no_argument,       NULL, 'q'              },
                 {},
         };
 
@@ -1212,7 +1521,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "hH:M:q", options, NULL)) >= 0)
 
                 switch (c) {
 
@@ -1297,6 +1606,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_host = optarg;
                         break;
 
+                case 'q':
+                        arg_quiet = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -1326,6 +1639,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], "call"))
+                return call(bus, argv + optind);
+
         if (streq(argv[optind], "help"))
                 return help();
 
diff --git a/src/shared/util.c b/src/shared/util.c
index 2f4fa23..eeced47 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -363,6 +363,46 @@ int safe_atou8(const char *s, uint8_t *ret) {
         return 0;
 }
 
+int safe_atou16(const char *s, uint16_t *ret) {
+        char *x = NULL;
+        unsigned long l;
+
+        assert(s);
+        assert(ret);
+
+        errno = 0;
+        l = strtoul(s, &x, 0);
+
+        if (!x || x == s || *x || errno)
+                return errno > 0 ? -errno : -EINVAL;
+
+        if ((unsigned long) (uint16_t) l != l)
+                return -ERANGE;
+
+        *ret = (uint16_t) l;
+        return 0;
+}
+
+int safe_atoi16(const char *s, int16_t *ret) {
+        char *x = NULL;
+        long l;
+
+        assert(s);
+        assert(ret);
+
+        errno = 0;
+        l = strtol(s, &x, 0);
+
+        if (!x || x == s || *x || errno)
+                return errno > 0 ? -errno : -EINVAL;
+
+        if ((long) (int16_t) l != l)
+                return -ERANGE;
+
+        *ret = (int16_t) l;
+        return 0;
+}
+
 int safe_atollu(const char *s, long long unsigned *ret_llu) {
         char *x = NULL;
         unsigned long long l;
diff --git a/src/shared/util.h b/src/shared/util.h
index 04f2d8a..835fee4 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -245,6 +245,9 @@ static inline int safe_atoi64(const char *s, int64_t *ret_i) {
         return safe_atolli(s, (long long int*) ret_i);
 }
 
+int safe_atou16(const char *s, uint16_t *ret);
+int safe_atoi16(const char *s, int16_t *ret);
+
 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
 
 #define FOREACH_WORD(word, length, s, state)                            \

commit 8022212b3b8e553fd83b87575f3e730e56852d5d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Nov 14 13:05:01 2014 +0100

    sd-bus: add sd_bus_message_is_empty() for checking whether a message carries any body

diff --git a/src/libsystemd/libsystemd.sym.m4 b/src/libsystemd/libsystemd.sym.m4
index 0c02db7..7632942 100644
--- a/src/libsystemd/libsystemd.sym.m4
+++ b/src/libsystemd/libsystemd.sym.m4
@@ -254,6 +254,7 @@ global:
         sd_bus_message_get_realtime_usec;
         sd_bus_message_get_seqnum;
         sd_bus_message_get_creds;
+        sd_bus_message_is_empty;
         sd_bus_message_is_signal;
         sd_bus_message_is_method_call;
         sd_bus_message_is_method_error;
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
index 1a8c445..9d6647b 100644
--- a/src/libsystemd/sd-bus/bus-message.c
+++ b/src/libsystemd/sd-bus/bus-message.c
@@ -5380,6 +5380,12 @@ _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complet
         return strempty(c->signature);
 }
 
+_public_ int sd_bus_message_is_empty(sd_bus_message *m) {
+        assert_return(m, -EINVAL);
+
+        return isempty(m->root_container.signature);
+}
+
 _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
         bool done_something = false;
         int r;
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index 49d4907..58a8980 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -222,6 +222,7 @@ sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m); /* do not unref the r
 int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member);
 int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member);
 int sd_bus_message_is_method_error(sd_bus_message *m, const char *name);
+int sd_bus_message_is_empty(sd_bus_message *m);
 
 int sd_bus_message_set_expect_reply(sd_bus_message *m, int b);
 int sd_bus_message_set_auto_start(sd_bus_message *m, int b);

commit 34b950a8d8b83b3c101beba479a9ddae8c2ebb93
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Nov 14 12:55:18 2014 +0100

    man: hostnamectl understands -M too

diff --git a/man/hostnamectl.xml b/man/hostnamectl.xml
index ffae5e6..de15402 100644
--- a/man/hostnamectl.xml
+++ b/man/hostnamectl.xml
@@ -129,6 +129,7 @@
                         </varlistentry>
 
                         <xi:include href="user-system-options.xml" xpointer="host" />
+                        <xi:include href="user-system-options.xml" xpointer="machine" />
 
                         <xi:include href="standard-options.xml" xpointer="help" />
                         <xi:include href="standard-options.xml" xpointer="version" />



More information about the systemd-commits mailing list