| From 21aa7e46e66dee529053f0570511a13a0d4201ff Mon Sep 17 00:00:00 2001 |
| From: Chris Morin <cmtm@google.com> |
| Date: Fri, 14 Jun 2019 13:16:10 -0700 |
| Subject: [PATCH] dbus-send: add --sender option |
| |
| Clients listening for a signal can match against the 'sender', expecting |
| it to come from a connection with a specific name. With this change, |
| dbus-send can send signals to them. |
| --- |
| doc/dbus-send.1.xml.in | 7 +++++++ |
| tools/dbus-send.c | 32 ++++++++++++++++++++++++++++++++ |
| 2 files changed, 39 insertions(+) |
| |
| diff --git a/doc/dbus-send.1.xml.in b/doc/dbus-send.1.xml.in |
| index 67b6dfd2..8c1bbeaa 100644 |
| --- a/doc/dbus-send.1.xml.in |
| +++ b/doc/dbus-send.1.xml.in |
| @@ -22,6 +22,7 @@ |
| <cmdsynopsis> |
| <command>dbus-send</command> |
| <group choice='opt'><arg choice='plain'>--system </arg><arg choice='plain'>--session </arg><arg choice='plain'>--address=<replaceable>ADDRESS</replaceable></arg></group> |
| + <arg choice='opt'>--sender=<replaceable>NAME</replaceable></arg> |
| <arg choice='opt'>--dest=<replaceable>NAME</replaceable></arg> |
| <arg choice='opt'><arg choice='plain'>--print-reply </arg><arg choice='opt'><replaceable>=literal</replaceable></arg></arg> |
| <arg choice='opt'>--reply-timeout=<replaceable>MSEC</replaceable></arg> |
| @@ -140,6 +141,12 @@ The default is implementation‐defined, typically 25 seconds.</para> |
| <term><option>--address=</option><replaceable>ADDRESS</replaceable></term> |
| <listitem> |
| <para>Send to <replaceable>ADDRESS</replaceable>.</para> |
| + </listitem> |
| + </varlistentry> |
| + <varlistentry> |
| + <term><option>--sender=</option><replaceable>NAME</replaceable></term> |
| + <listitem> |
| +<para>Request ownership of name <replaceable>NAME</replaceable> before sending message.</para> |
| </listitem> |
| </varlistentry> |
| <varlistentry> |
| diff --git a/tools/dbus-send.c b/tools/dbus-send.c |
| index fb6a9e6e..bf1e584c 100644 |
| --- a/tools/dbus-send.c |
| +++ b/tools/dbus-send.c |
| @@ -337,6 +337,7 @@ main (int argc, char *argv[]) |
| int message_type = DBUS_MESSAGE_TYPE_SIGNAL; |
| const char *type_str = NULL; |
| const char *address = NULL; |
| + const char *sender = NULL; |
| int is_bus = FALSE; |
| int session_or_system = FALSE; |
| int fixed = 0; |
| @@ -387,6 +388,16 @@ main (int argc, char *argv[]) |
| usage (1); |
| } |
| } |
| + else if (strstr (arg, "--sender=") == arg) |
| + { |
| + sender = strchr (arg, '=') + 1; |
| + |
| + if (sender[0] == '\0') |
| + { |
| + fprintf (stderr, "\"--sender=\" requires a NAME\n"); |
| + usage (1); |
| + } |
| + } |
| else if (strncmp (arg, "--print-reply", 13) == 0) |
| { |
| print_reply = TRUE; |
| @@ -497,6 +508,27 @@ main (int argc, char *argv[]) |
| } |
| } |
| |
| + if (sender != NULL) |
| + { |
| + switch(dbus_bus_request_name (connection, sender, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error)) |
| + { |
| + case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: |
| + /* success */ |
| + break; |
| + case DBUS_REQUEST_NAME_REPLY_EXISTS: |
| + fprintf (stderr, "Requested name \"%s\" already has owner\n", sender); |
| + exit (1); |
| + case -1: |
| + fprintf (stderr, "Failed to request sender name \"%s\": %s\n", sender, error.message); |
| + dbus_error_free(&error); |
| + exit (1); |
| + default: |
| + /* This should be unreachable if the bus is compliant */ |
| + fprintf (stderr, "Request of sender name failed unexpectedly\n"); |
| + exit (1); |
| + } |
| + } |
| + |
| if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL) |
| { |
| char *last_dot; |
| -- |
| 2.22.0.410.gd8fdbe21b5-goog |
| |