[systemd-commits] 6 commits - man/busctl.xml src/core src/libsystemd src/shared src/test

Lennart Poettering lennart at kemper.freedesktop.org
Thu Nov 20 15:32:12 PST 2014


 man/busctl.xml                      |  155 ++++++++++++-
 src/core/build.h                    |    2 
 src/libsystemd/sd-bus/bus-objects.c |   10 
 src/libsystemd/sd-bus/busctl.c      |  428 +++++++++++++++++++++++++++++-------
 src/shared/util.c                   |   39 ++-
 src/shared/util.h                   |    2 
 src/test/test-util.c                |   13 +
 7 files changed, 554 insertions(+), 95 deletions(-)

New commits:
commit 1fc5560911a7e9e8cf2993e17e1f0a001e148809
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Nov 20 23:12:29 2014 +0100

    busctl: show property values in "introspect" output, add "set-property" command, and support both a terse and a verbose output format

diff --git a/man/busctl.xml b/man/busctl.xml
index ccbd832..e9b758a 100644
--- a/man/busctl.xml
+++ b/man/busctl.xml
@@ -161,6 +161,17 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
         </listitem>
       </varlistentry>
 
+
+      <varlistentry>
+        <term><option>--verbose</option></term>
+
+        <listitem>
+          <para>When used with the <command>call</command> or
+          <command>get-property</command> command shows output in a
+          more verbose format.</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" />
@@ -238,21 +249,31 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
         <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 arguments,
-        individually formatted as textual parameters.</para></listitem>
+        string is required, followed by the arguments, individually
+        formatted as strings. For details on the formatting used, see
+        below. To suppress output of the returned data use the
+        <option>--quiet</option> option.</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>PROPERTY</replaceable></arg></arg></term>
-
-        <listitem><para>Retrieve the current value one or more object
-        properties. Takes a service name and object path. Optionally
-        takes an interface name and property name. If the property
-        name is omited, shows all properties on the selected
-        interface. If the interface is also omitted shows the
-        properties of all interfaces. Multiple properties may be
+        <term><command>get-property</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" rep="repeat"><replaceable>PROPERTY</replaceable></arg></term>
+
+        <listitem><para>Retrieve the current value of one or more
+        object properties. Takes a service name, object path,
+        interface name and property name. Multiple properties may be
         specified at once in which case their values will be shown one
-        after the other.</para></listitem>
+        after the other, separated by newlines. The output is by
+        default in terse format. Use <option>--verbose</option> for a
+        more elaborate output format.</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><command>set-property</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>PROPERTY</replaceable></arg> <arg choice="plain"><replaceable>SIGNATURE</replaceable></arg> <arg choice="plain" rep="repeat"><replaceable>ARGUMENT</replaceable></arg></term>
+
+        <listitem><para>Set the current value an object
+        property. Takes a service name, object path, interface name,
+        property name, property signature, followed by a list of
+        parameters formatted as strings.</para></listitem>
       </varlistentry>
 
       <varlistentry>
@@ -264,6 +285,118 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
   </refsect1>
 
   <refsect1>
+          <title>Parameter Formatting</title>
+
+          <para>The <command>call</command> and
+          <command>set-property</command> commands take a signature
+          string followed by a list of parameters formatted as string
+          (for details on D-Bus signature strings see the <ulink
+          url="http://dbus.freedesktop.org/doc/dbus-specification.html#type-system">Type
+          system chapter of the D-Bus specification</ulink>). For
+          simple types each parameter following the signature should
+          simply be the parameter's value formatted as
+          string. Positive boolean values may be formatted as
+          <literal>true</literal>, <literal>yes</literal>,
+          <literal>on</literal>, <literal>1</literal>; negative
+          boolean values may be specified as <literal>false</literal>,
+          <literal>no</literal>, <literal>off</literal>,
+          <literal>0</literal>. For arrays, a numeric argument for the
+          number of entries followed by the entries shall be
+          specified. For variants the signature of the contents shall
+          be specified, followed by the contents. For dictionaries and
+          structs the contents of them shall be directly
+          specified.</para>
+
+          <para>For example,
+          <programlisting>s jawoll</programlisting> is the formatting
+          of a single string <literal>jawoll</literal>.</para>
+
+          <para>
+          <programlisting>as 3 hello world foobar</programlisting>
+          is the formatting of a string array with three entries,
+          <literal>hello</literal>, <literal>world</literal> and
+          <literal>foobar</literal>.</para>
+
+          <para>
+          <programlisting>a{sv} 3 One s Eins Two u 2 Yes b true</programlisting>
+          is the formatting of a dictionary
+          array that maps strings to variants, consisting of three
+          entries. The string <literal>One</literal> is assigned the
+          string <literal>Eins</literal>. The string
+          <literal>Two</literal> is assigned the 32bit unsigned
+          integer 2. The string <literal>Yes</literal> is assigned a
+          positive boolean.</para>
+
+          <para>Note that the <command>call</command>,
+          <command>get-property</command>,
+          <command>introspect</command> commands will also generate
+          output in this format for the returned data. Since this
+          format is sometimes too terse to be easily understood, the
+          <command>call</command> and <command>get-property</command>
+          commands may generate a more verbose, multi-line output when
+          passed the <option>--verbose</option> option.</para>
+  </refsect1>
+
+  <refsect1>
+          <title>Examples</title>
+
+          <example>
+                  <title>Write and Read a Property</title>
+
+                  <para>The following two commands first write a
+                  property and then read it back. The property is
+                  found on the
+                  <literal>/org/freedesktop/systemd1</literal> object
+                  of the <literal>org.freedesktop.systemd1</literal>
+                  service. The name of the property is
+                  <literal>LogLevel</literal> on the
+                  <literal>org.freedesktop.systemd1.Manager</literal>
+                  interface. The property contains a single
+                  string:</para>
+
+                  <programlisting># busctl set-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager LogLevel s debug
+# busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager LogLevel
+s "debug"</programlisting>
+
+          </example>
+
+          <example>
+                  <title>Terse and Verbose Output</title>
+
+                  <para>The following two commands read a property that
+                  contains an array of strings, and first show it in
+                  terse format, followed by verbose format:</para>
+
+                  <programlisting>$ busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager Environment
+as 2 "LANG=en_US.UTF-8" "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
+$ busctl get-property --verbose org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager Environment
+ARRAY "s" {
+        STRING "LANG=en_US.UTF-8";
+        STRING "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin";
+};</programlisting>
+          </example>
+
+        <example>
+                  <title>Invoking a Method</title>
+
+                  <para>The following command invokes a the
+                  <literal>StartUnit</literal> method on the
+                  <literal>org.freedesktop.systemd1.Manager</literal>
+                  interface of the
+                  <literal>/org/freedesktop/systemd1</literal> object
+                  of the <literal>org.freedesktop.systemd1</literal>
+                  service, and passes it two strings
+                  <literal>cups.service</literal> and
+                  <literal>replace</literal>. As result of the method
+                  call a single object path parameter is received and
+                  shown:</para>
+
+                  <programlisting># busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StartUnit ss "cups.service" "replace"
+o "/org/freedesktop/systemd1/job/42684"</programlisting>
+        </example>
+  </refsect1>
+
+  <refsect1>
     <title>See Also</title>
 
     <para>
diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c
index 809940a..7f5124f 100644
--- a/src/libsystemd/sd-bus/busctl.c
+++ b/src/libsystemd/sd-bus/busctl.c
@@ -52,6 +52,7 @@ static bool arg_user = false;
 static size_t arg_snaplen = 4096;
 static bool arg_list = false;
 static bool arg_quiet = false;
+static bool arg_verbose = false;
 
 static void pager_open_if_enabled(void) {
 
@@ -475,12 +476,152 @@ static int tree(sd_bus *bus, char **argv) {
         return r;
 }
 
+static int format_cmdline(sd_bus_message *m, FILE *f, bool needs_space) {
+        int r;
+
+        for (;;) {
+                const char *contents = NULL;
+                char type;
+                union {
+                        uint8_t u8;
+                        uint16_t u16;
+                        int16_t s16;
+                        uint32_t u32;
+                        int32_t s32;
+                        uint64_t u64;
+                        int64_t s64;
+                        double d64;
+                        const char *string;
+                        int i;
+                } basic;
+
+                r = sd_bus_message_peek_type(m, &type, &contents);
+                if (r <= 0)
+                        return r;
+
+                if (bus_type_is_container(type) > 0) {
+
+                        r = sd_bus_message_enter_container(m, type, contents);
+                        if (r < 0)
+                                return r;
+
+                        if (type == SD_BUS_TYPE_ARRAY) {
+                                unsigned n = 0;
+
+                                /* count array entries */
+                                for (;;) {
+
+                                        r = sd_bus_message_skip(m, contents);
+                                        if (r < 0)
+                                                return r;
+                                        if (r == 0)
+                                                break;
+
+                                        n++;
+                                }
+
+                                r = sd_bus_message_rewind(m, false);
+                                if (r < 0)
+                                        return r;
+
+                                if (needs_space)
+                                        fputc(' ', f);
+
+                                fprintf(f, "%u", n);
+                        } else if (type == SD_BUS_TYPE_VARIANT) {
+
+                                if (needs_space)
+                                        fputc(' ', f);
+
+                                fprintf(f, "%s", contents);
+                        }
+
+                        r = format_cmdline(m, f, true);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_message_exit_container(m);
+                        if (r < 0)
+                                return r;
+
+                        continue;
+                }
+
+                r = sd_bus_message_read_basic(m, type, &basic);
+                if (r < 0)
+                        return r;
+
+                if (needs_space)
+                        fputc(' ', f);
+
+                switch (type) {
+                case SD_BUS_TYPE_BYTE:
+                        fprintf(f, "%u", basic.u8);
+                        break;
+
+                case SD_BUS_TYPE_BOOLEAN:
+                        fputs(true_false(basic.i), f);
+                        break;
+
+                case SD_BUS_TYPE_INT16:
+                        fprintf(f, "%i", basic.s16);
+                        break;
+
+                case SD_BUS_TYPE_UINT16:
+                        fprintf(f, "%u", basic.u16);
+                        break;
+
+                case SD_BUS_TYPE_INT32:
+                        fprintf(f, "%i", basic.s32);
+                        break;
+
+                case SD_BUS_TYPE_UINT32:
+                        fprintf(f, "%u", basic.u32);
+                        break;
+
+                case SD_BUS_TYPE_INT64:
+                        fprintf(f, "%" PRIi64, basic.s64);
+                        break;
+
+                case SD_BUS_TYPE_UINT64:
+                        fprintf(f, "%" PRIu64, basic.u64);
+                        break;
+
+                case SD_BUS_TYPE_DOUBLE:
+                        fprintf(f, "%g", basic.d64);
+                        break;
+
+                case SD_BUS_TYPE_STRING:
+                case SD_BUS_TYPE_OBJECT_PATH:
+                case SD_BUS_TYPE_SIGNATURE: {
+                        _cleanup_free_ char *b = NULL;
+
+                        b = cescape(basic.string);
+                        if (!b)
+                                return -ENOMEM;
+
+                        fprintf(f, "\"%s\"", b);
+                        break;
+                }
+
+                case SD_BUS_TYPE_UNIX_FD:
+                        fprintf(f, "%i", basic.i);
+                        break;
+
+                default:
+                        assert_not_reached("Unknown basic type.");
+                }
+
+        }
+}
+
 typedef struct Member {
         const char *type;
         char *interface;
         char *name;
         char *signature;
         char *result;
+        char *value;
         bool writable;
         uint64_t flags;
 } Member;
@@ -550,6 +691,7 @@ static void member_free(Member *m) {
         free(m->name);
         free(m->signature);
         free(m->result);
+        free(m->value);
         free(m);
 }
 
@@ -758,16 +900,93 @@ static int introspect(sd_bus *bus, char **argv) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
+        /* First, get list of all properties */
         r = parse_xml_introspect(argv[2], xml, &ops, members);
         if (r < 0)
                 return r;
 
+        /* Second, find the current values for them */
+        SET_FOREACH(m, members, i) {
+
+                if (!streq(m->type, "property"))
+                        continue;
+
+                if (m->value)
+                        continue;
+
+                r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", m->interface);
+                if (r < 0) {
+                        log_error("%s", bus_error_message(&error, r));
+                        return r;
+                }
+
+                r = sd_bus_message_enter_container(reply, 'a', "{sv}");
+                if (r < 0)
+                        return bus_log_parse_error(r);
+
+                for (;;) {
+                        Member *z;
+                        _cleanup_free_ char *buf = NULL;
+                        _cleanup_fclose_ FILE *mf = NULL;
+                        size_t sz = 0;
+                        const char *name;
+
+                        r = sd_bus_message_enter_container(reply, 'e', "sv");
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        if (r == 0)
+                                break;
+
+                        r = sd_bus_message_read(reply, "s", &name);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        r = sd_bus_message_enter_container(reply, 'v', NULL);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        mf = open_memstream(&buf, &sz);
+                        if (!mf)
+                                return log_oom();
+
+                        r = format_cmdline(reply, mf, false);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        fclose(mf);
+                        mf = NULL;
+
+                        z = set_get(members, &((Member) {
+                                                .type = "property",
+                                                .interface = m->interface,
+                                                .name = (char*) name }));
+                        if (z) {
+                                free(z->value);
+                                z->value = buf;
+                                buf = NULL;
+                        }
+
+                        r = sd_bus_message_exit_container(reply);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        r = sd_bus_message_exit_container(reply);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+                }
+
+                r = sd_bus_message_exit_container(reply);
+                if (r < 0)
+                        return bus_log_parse_error(r);
+        }
+
         pager_open_if_enabled();
 
         name_width = strlen("NAME");
         type_width = strlen("TYPE");
         signature_width = strlen("SIGNATURE");
-        result_width = strlen("RESULT");
+        result_width = strlen("RESULT/VALUE");
 
         sorted = newa(Member*, set_size(members));
 
@@ -782,27 +1001,45 @@ static int introspect(sd_bus *bus, char **argv) {
                         signature_width = MAX(signature_width, strlen(m->signature));
                 if (m->result)
                         result_width = MAX(result_width, strlen(m->result));
+                if (m->value)
+                        result_width = MAX(result_width, strlen(m->value));
 
                 sorted[k++] = m;
         }
 
+        if (result_width > 40)
+                result_width = 40;
+
         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");
+        if (arg_legend) {
+                printf("%-*s %-*s %-*s %-*s %s\n",
+                       (int) name_width, "NAME",
+                       (int) type_width, "TYPE",
+                       (int) signature_width, "SIGNATURE",
+                       (int) result_width, "RESULT/VALUE",
+                       "FLAGS");
+        }
 
         for (j = 0; j < k; j++) {
+                _cleanup_free_ char *ellipsized = NULL;
+                const char *rv;
                 bool is_interface;
 
                 m = sorted[j];
 
                 is_interface = streq(m->type, "interface");
 
+                if (m->value) {
+                        ellipsized = ellipsize(m->value, result_width, 100);
+                        if (!ellipsized)
+                                return log_oom();
+
+                        rv = ellipsized;
+                } else
+                        rv = strdash(m->result);
+
                 printf("%s%s%-*s%s %-*s %-*s %-*s%s%s%s%s%s%s\n",
                        is_interface ? ansi_highlight() : "",
                        is_interface ? "" : ".",
@@ -810,7 +1047,7 @@ static int introspect(sd_bus *bus, char **argv) {
                        is_interface ? ansi_highlight_off() : "",
                        (int) type_width, strdash(m->type),
                        (int) signature_width, strdash(m->signature),
-                       (int) result_width, strdash(m->result),
+                       (int) result_width, rv,
                        (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" : "",
@@ -1241,9 +1478,26 @@ static int call(sd_bus *bus, char *argv[]) {
         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, 0);
+
+                if (arg_verbose) {
+                        pager_open_if_enabled();
+
+                        r = bus_message_dump(reply, stdout, 0);
+                        if (r < 0)
+                                return r;
+                } else {
+
+                        fputs(sd_bus_message_get_signature(reply, true), stdout);
+                        fputc(' ', stdout);
+
+                        r = format_cmdline(reply, stdout, false);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        fputc('\n', stdout);
+                }
         }
 
         return 0;
@@ -1252,99 +1506,106 @@ static int call(sd_bus *bus, char *argv[]) {
 static int get_property(sd_bus *bus, char *argv[]) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         unsigned n;
+        char **i;
         int r;
 
         assert(bus);
 
         n = strv_length(argv);
-        if (n < 3) {
-                log_error("Expects at least three arguments.");
+        if (n < 5) {
+                log_error("Expects at least four arguments.");
                 return -EINVAL;
         }
 
-        if (n < 5) {
+        STRV_FOREACH(i, argv + 4) {
                 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-                bool not_first = false;
+                const char *contents = NULL;
+                char type;
 
-                r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", strempty(argv[3]));
+                r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "Get", &error, &reply, "ss", argv[3], *i);
                 if (r < 0) {
                         log_error("%s", bus_error_message(&error, r));
                         return r;
                 }
 
-                r = sd_bus_message_enter_container(reply, 'a', "{sv}");
+                r = sd_bus_message_peek_type(reply, &type, &contents);
                 if (r < 0)
                         return bus_log_parse_error(r);
 
-                for (;;) {
-                        const char *name;
-
-                        r = sd_bus_message_enter_container(reply, 'e', "sv");
-                        if (r < 0)
-                                return bus_log_parse_error(r);
-
-                        if (r == 0)
-                                break;
-
-                        r = sd_bus_message_read(reply, "s", &name);
-                        if (r < 0)
-                                return bus_log_parse_error(r);
-
-                        if (not_first)
-                                printf("\n");
-
-                        printf("Property %s:\n", name);
-
-                        r = sd_bus_message_enter_container(reply, 'v', NULL);
-                        if (r < 0)
-                                return bus_log_parse_error(r);
+                r = sd_bus_message_enter_container(reply, 'v', contents);
+                if (r < 0)
+                        return bus_log_parse_error(r);
 
+                if (arg_verbose)  {
                         pager_open_if_enabled();
-                        bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_SUBTREE_ONLY);
 
-                        r = sd_bus_message_exit_container(reply);
+                        r = bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_SUBTREE_ONLY);
                         if (r < 0)
-                                return bus_log_parse_error(r);
+                                return r;
+                } else {
+                        fputs(contents, stdout);
+                        fputc(' ', stdout);
 
-                        r = sd_bus_message_exit_container(reply);
+                        r = format_cmdline(reply, stdout, false);
                         if (r < 0)
                                 return bus_log_parse_error(r);
 
-                        not_first = true;
+                        fputc('\n', stdout);
                 }
 
                 r = sd_bus_message_exit_container(reply);
                 if (r < 0)
                         return bus_log_parse_error(r);
-        } else {
-                char **i;
+        }
 
-                STRV_FOREACH(i, argv + 4) {
-                        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        return 0;
+}
 
-                        r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "Get", &error, &reply, "ss", argv[3], *i);
-                        if (r < 0) {
-                                log_error("%s", bus_error_message(&error, r));
-                                return r;
-                        }
+static int set_property(sd_bus *bus, char *argv[]) {
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        unsigned n;
+        char **p;
+        int r;
 
-                        r = sd_bus_message_enter_container(reply, 'v', NULL);
-                        if (r < 0)
-                                return bus_log_parse_error(r);
+        assert(bus);
 
-                        if (i > argv + 4)
-                                printf("\n");
+        n = strv_length(argv);
+        if (n < 6) {
+                log_error("Expects at least five arguments.");
+                return -EINVAL;
+        }
 
-                        if (argv[5])
-                                printf("Property %s:\n", *i);
+        r = sd_bus_message_new_method_call(bus, &m, argv[1], argv[2], "org.freedesktop.DBus.Properties", "Set");
+        if (r < 0)
+                return bus_log_create_error(r);
 
-                        pager_open_if_enabled();
-                        bus_message_dump(reply, stdout, BUS_MESSAGE_DUMP_SUBTREE_ONLY);
+        r = sd_bus_message_append(m, "ss", argv[3], argv[4]);
+        if (r < 0)
+                return bus_log_create_error(r);
 
-                        r = sd_bus_message_exit_container(reply);
-                        if (r < 0)
-                                return bus_log_parse_error(r);
-                }
+        r = sd_bus_message_open_container(m, 'v', argv[5]);
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        p = argv+6;
+        r = message_append_cmdline(m, argv[5], &p);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_close_container(m);
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        if (*p) {
+                log_error("Too many parameters for signature.");
+                return -EINVAL;
+        }
+
+        r = sd_bus_call(bus, m, 0, &error, NULL);
+        if (r < 0) {
+                log_error("%s", bus_error_message(&error, r));
+                return r;
         }
 
         return 0;
@@ -1368,18 +1629,21 @@ static int help(void) {
                "     --activatable        Only show activatable names\n"
                "     --match=MATCH        Only show matching messages\n"
                "     --list               Don't show tree, but simple object path list\n"
-               "     --quiet              Don't show method call reply\n\n"
+               "     --quiet              Don't show method call reply\n"
+               "     --verbose            Show result values in long format\n\n"
                "Commands:\n"
                "  list                    List bus names\n"
                "  status SERVICE          Show service name status\n"
                "  monitor [SERVICE...]    Show bus traffic\n"
                "  capture [SERVICE...]    Capture bus traffic as pcap\n"
                "  tree [SERVICE...]       Show object tree of service\n"
-               "  introspect SERVICE PATH\n"
+               "  introspect SERVICE OBJECT\n"
                "  call SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]\n"
                "                          Call a method\n"
-               "  get-property SERVICE OBJECT [INTERFACE [PROPERTY...]]\n"
+               "  get-property SERVICE OBJECT INTERFACE PROPERTY...\n"
                "                          Get property value\n"
+               "  set-property SERVICE OBJECT INTERFACE PROPERTY SIGNATURE ARGUMENT...\n"
+               "                          Set property value\n"
                "  help                    Show this help\n"
                , program_invocation_short_name);
 
@@ -1402,6 +1666,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_ACTIVATABLE,
                 ARG_SIZE,
                 ARG_LIST,
+                ARG_VERBOSE,
         };
 
         static const struct option options[] = {
@@ -1422,6 +1687,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "size",         required_argument, NULL, ARG_SIZE         },
                 { "list",         no_argument,       NULL, ARG_LIST         },
                 { "quiet",        no_argument,       NULL, 'q'              },
+                { "verbose",      no_argument,       NULL, ARG_VERBOSE      },
                 {},
         };
 
@@ -1519,6 +1785,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_quiet = true;
                         break;
 
+                case ARG_VERBOSE:
+                        arg_verbose = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -1557,6 +1827,9 @@ static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
         if (streq(argv[optind], "get-property"))
                 return get_property(bus, argv + optind);
 
+        if (streq(argv[optind], "set-property"))
+                return set_property(bus, argv + optind);
+
         if (streq(argv[optind], "help"))
                 return help();
 

commit b18ec7e29f9756bb66f63a0fa02a6ceb40b38b03
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Nov 20 21:01:06 2014 +0100

    busctl: when --address is specified do not assume we connect to a full bus

diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c
index 4da8074..809940a 100644
--- a/src/libsystemd/sd-bus/busctl.c
+++ b/src/libsystemd/sd-bus/busctl.c
@@ -1612,6 +1612,12 @@ int main(int argc, char *argv[]) {
         if (arg_address)
                 r = sd_bus_set_address(bus, arg_address);
         else {
+                r = sd_bus_set_bus_client(bus, true);
+                if (r < 0) {
+                        log_error("Failed to set bus client: %s", strerror(-r));
+                        goto finish;
+                }
+
                 switch (arg_transport) {
 
                 case BUS_TRANSPORT_LOCAL:
@@ -1638,12 +1644,6 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = sd_bus_set_bus_client(bus, true);
-        if (r < 0) {
-                log_error("Failed to set bus client: %s", strerror(-r));
-                goto finish;
-        }
-
         r = sd_bus_start(bus);
         if (r < 0) {
                 log_error("Failed to connect to bus: %s", strerror(-r));

commit 79f34de9fb8e4faa6ed0847b386099a4ba100a8a
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Nov 20 20:59:57 2014 +0100

    busctl: use canned error message on parse failure

diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c
index 33be4e8..4da8074 100644
--- a/src/libsystemd/sd-bus/busctl.c
+++ b/src/libsystemd/sd-bus/busctl.c
@@ -35,6 +35,7 @@
 #include "bus-util.h"
 #include "bus-dump.h"
 #include "bus-signature.h"
+#include "bus-type.h"
 #include "busctl-introspect.h"
 
 static bool arg_no_pager = false;
@@ -1213,10 +1214,8 @@ static int call(sd_bus *bus, char *argv[]) {
         }
 
         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 (r < 0)
+                return bus_log_create_error(r);
 
         if (!isempty(argv[5])) {
                 char **p;

commit 0ca454d474a13f363dcd5ef8dc97ca158569d3ed
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Nov 20 20:58:39 2014 +0100

    sd-bus: create clean error when a property Set() call with incorrect signature is passed in

diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c
index 7981d65..6162d12 100644
--- a/src/libsystemd/sd-bus/bus-objects.c
+++ b/src/libsystemd/sd-bus/bus-objects.c
@@ -617,6 +617,9 @@ static int property_get_set_callbacks_run(
                         return r;
 
         } else {
+                const char *signature = NULL;
+                char type = 0;
+
                 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
                         return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
 
@@ -628,6 +631,13 @@ static int property_get_set_callbacks_run(
 
                 c->last_iteration = bus->iteration_counter;
 
+                r = sd_bus_message_peek_type(m, &type, &signature);
+                if (r < 0)
+                        return r;
+
+                if (type != 'v' || !streq(strempty(signature), strempty(c->vtable->x.property.signature)))
+                        return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Incorrect parameters for property '%s', expected '%s', got '%s'.", c->member, strempty(c->vtable->x.property.signature), strempty(signature));
+
                 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
                 if (r < 0)
                         return r;

commit 609c3029076da1ba423673161e5d0fc82ccca6b6
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Nov 20 20:58:02 2014 +0100

    build: avoid trailing whitespace in feature string

diff --git a/src/core/build.h b/src/core/build.h
index d5e5550..24873ab 100644
--- a/src/core/build.h
+++ b/src/core/build.h
@@ -154,4 +154,4 @@
         _BLKID_FEATURE_ " "                                             \
         _ELFUTILS_FEATURE_ " "                                          \
         _KMOD_FEATURE_ " "                                              \
-        _IDN_FEATURE_ " "
+        _IDN_FEATURE_

commit ad450c3e0418fe23e2896e581575cb06564ca29a
Author: Jakub Filak <jfilak at redhat.com>
Date:   Wed Nov 19 11:01:19 2014 +0100

    util: add functions getting proc cwd and root
    
    /proc/[pid]/cwd and /proc/[pid]/root are symliks to corresponding
    directories
    
    The added functions returns values of that symlinks.

diff --git a/src/shared/util.c b/src/shared/util.c
index eeced47..0166052 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -797,19 +797,30 @@ int get_process_capeff(pid_t pid, char **capeff) {
         return get_status_field(p, "\nCapEff:", capeff);
 }
 
+static int get_process_link_contents(const char *proc_file, char **name) {
+        int r;
+
+        assert(proc_file);
+        assert(name);
+
+        r = readlink_malloc(proc_file, name);
+        if (r < 0)
+                return r == -ENOENT ? -ESRCH : r;
+
+        return 0;
+}
+
 int get_process_exe(pid_t pid, char **name) {
         const char *p;
         char *d;
         int r;
 
         assert(pid >= 0);
-        assert(name);
 
         p = procfs_file_alloca(pid, "exe");
-
-        r = readlink_malloc(p, name);
+        r = get_process_link_contents(p, name);
         if (r < 0)
-                return r == -ENOENT ? -ESRCH : r;
+                return r;
 
         d = endswith(*name, " (deleted)");
         if (d)
@@ -861,6 +872,26 @@ int get_process_gid(pid_t pid, gid_t *gid) {
         return get_process_id(pid, "Gid:", gid);
 }
 
+int get_process_cwd(pid_t pid, char **cwd) {
+        const char *p;
+
+        assert(pid >= 0);
+
+        p = procfs_file_alloca(pid, "cwd");
+
+        return get_process_link_contents(p, cwd);
+}
+
+int get_process_root(pid_t pid, char **root) {
+        const char *p;
+
+        assert(pid >= 0);
+
+        p = procfs_file_alloca(pid, "root");
+
+        return get_process_link_contents(p, root);
+}
+
 char *strnappend(const char *s, const char *suffix, size_t b) {
         size_t a;
         char *r;
diff --git a/src/shared/util.h b/src/shared/util.h
index 835fee4..fc59481 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -295,6 +295,8 @@ int get_process_exe(pid_t pid, char **name);
 int get_process_uid(pid_t pid, uid_t *uid);
 int get_process_gid(pid_t pid, gid_t *gid);
 int get_process_capeff(pid_t pid, char **capeff);
+int get_process_cwd(pid_t pid, char **cwd);
+int get_process_root(pid_t pid, char **root);
 
 char hexchar(int x) _const_;
 int unhexchar(char c) _const_;
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 01e10c9..b33e15a 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -490,13 +490,14 @@ static void test_u64log2(void) {
 
 static void test_get_process_comm(void) {
         struct stat st;
-        _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
+        _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL;
         unsigned long long b;
         pid_t e;
         uid_t u;
         gid_t g;
         dev_t h;
         int r;
+        pid_t me;
 
         if (stat("/proc/1/comm", &st) == 0) {
                 assert_se(get_process_comm(1, &a) >= 0);
@@ -532,6 +533,16 @@ static void test_get_process_comm(void) {
         log_info("pid1 gid: "GID_FMT, g);
         assert_se(g == 0);
 
+        me = getpid();
+
+        r = get_process_cwd(me, &cwd);
+        assert_se(r >= 0 || r == -EACCES);
+        log_info("pid1 cwd: '%s'", cwd);
+
+        r = get_process_root(me, &root);
+        assert_se(r >= 0 || r == -EACCES);
+        log_info("pid1 root: '%s'", root);
+
         assert_se(get_ctty_devnr(1, &h) == -ENOENT);
 
         getenv_for_pid(1, "PATH", &i);



More information about the systemd-commits mailing list