man: add sd_bus_message_new_call(3)

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-07-26 20:00:36 +02:00
parent e8b84fcca6
commit cfe8ee463d
6 changed files with 206 additions and 2 deletions

64
man/print-unit-path.c Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <systemd/sd-bus.h>
#define _cleanup_(f) __attribute__((cleanup(f)))
/* This is equivalent to:
* busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 \
* org.freedesktop.systemd1.Manager GetUnitByPID $$
*
* Compile with 'cc -lsystemd print-unit-path.c'
*/
#define DESTINATION "org.freedesktop.systemd1"
#define PATH "/org/freedesktop/systemd1"
#define INTERFACE "org.freedesktop.systemd1.Manager"
#define MEMBER "GetUnitByPID"
static int log_error(int error, const char *message) {
fprintf(stderr, "%s: %s\n", message, strerror(-error));
return error;
}
static int print_unit_path(sd_bus *bus) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
int r;
r = sd_bus_message_new_method_call(bus, &m,
DESTINATION, PATH, INTERFACE, MEMBER);
if (r < 0)
return log_error(r, "Failed to create bus message");
r = sd_bus_message_append(m, "u", (unsigned) getpid());
if (r < 0)
return log_error(r, "Failed to append to bus message");
r = sd_bus_call(bus, m, -1, &error, &reply);
if (r < 0)
return log_error(r, "Call failed");
const char *ans;
r = sd_bus_message_read(reply, "o", &ans);
if (r < 0)
return log_error(r, "Failed to read reply");
printf("Unit path is \"%s\".\n", ans);
return 0;
}
int main(int argc, char **argv) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
r = sd_bus_open_system(&bus);
if (r < 0)
return log_error(r, "Failed to acquire bus");
print_unit_path(bus);
}

View File

@ -210,6 +210,7 @@ manpages = [
'3',
['sd_bus_message_get_realtime_usec', 'sd_bus_message_get_seqnum'],
''],
['sd_bus_message_new_method_call', '3', [], ''],
['sd_bus_message_new_method_error',
'3',
['sd_bus_message_new_method_errno',

View File

@ -60,6 +60,7 @@
<citerefentry><refentrytitle>sd_bus_message_copy</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_get_cookie</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_get_monotonic_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_new_method_call</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_new_method_error</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_new_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_read_basic</refentrytitle><manvolnum>3</manvolnum></citerefentry>,

View File

@ -0,0 +1,134 @@
<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1+ -->
<refentry id="sd_bus_message_new_method_call"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_message_new_method_call</title>
<productname>systemd</productname>
</refentryinfo>
<refmeta>
<refentrytitle>sd_bus_message_new_method_call</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_bus_message_new_method_call</refname>
<refpurpose>Create a method call message</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>int sd_bus_message_new_method_call</funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
<paramdef>sd_bus_message **<parameter>m</parameter></paramdef>
<paramdef>const char *<parameter>destination</parameter></paramdef>
<paramdef>const char *<parameter>path</parameter></paramdef>
<paramdef>const char *<parameter>interface</parameter></paramdef>
<paramdef>const char *<parameter>member</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>The <function>sd_bus_message_new_method_call()</function> function creates a new bus
message object that encapsulates a D-Bus method call, and returns it in the
<parameter>m</parameter> output parameter. The call will be made on the destination
<parameter>destination</parameter>, path <parameter>path</parameter>, on the interface
<parameter>interface</parameter>, member <parameter>member</parameter>.</para>
<para>Briefly, the <emphasis>destination</emphasis> is a dot-separated name that identifies a
service connected to the bus. The <emphasis>path</emphasis> is a slash-separated identifier of
an object within the destination that resembles a file system path. The meaning of this path is
defined by the destination. The <emphasis>interface</emphasis> is a dot-separated name that
resembles a Java interface name that identifies a group of methods and signals supported by the
object identified by path. Methods and signals are collectively called
<emphasis>members</emphasis> and are identified by a simple name composed of ASCII letters,
numbers, and underscores. See the <ulink
url="https://dbus.freedesktop.org/doc/dbus-tutorial.html#concepts">D-Bus Tutorial</ulink> for an
in-depth explanation.</para>
<para>The <parameter>destination</parameter> parameter may be <constant>NULL</constant>. The
<parameter>interface</parameter> parameter may be <constant>NULL</constant>, if the destination
has only a single member with the given name and there is no ambiguity if the interface name is
omitted.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>This function returns 0 if the message object was successfully created, and a negative
errno-style error code otherwise.</para>
</refsect1>
<refsect1 id='errors'>
<title>Errors</title>
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><constant>-EINVAL</constant></term>
<listitem><para>The output parameter <parameter>m</parameter> is
<constant>NULL</constant>.</para>
<para>The <parameter>destination</parameter> parameter is non-null and is not a valid D-Bus
service name (<literal>org.somewhere.Something</literal>), the <parameter>path</parameter>
parameter is not a valid D-Bus path (<literal>/an/object/path</literal>), the
<parameter>interface</parameter> parameter is non-null and is not a valid D-Bus interface
name (<literal>an.interface.name</literal>), or the <parameter>member</parameter> parameter
is not a valid D-Bus member (<literal>Name</literal>).</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOTCONN</constant></term>
<listitem><para>The bus parameter <parameter>bus</parameter> is <constant>NULL</constant> or
the bus is not connected.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOMEM</constant></term>
<listitem><para>Memory allocation failed.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>Examples</title>
<example>
<title>Make a call to a D-Bus method that takes a single parameter</title>
<programlisting><xi:include href="print-unit-path.c" parse="text" /></programlisting>
<para>This defines a minimally useful program that will open a connection to the bus, create a
message object, send it, wait for the reply, and finally extract and print the answer. It does
error handling and proper memory management.</para>
</example>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_path_encode</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>

View File

@ -44,7 +44,11 @@
object that encapsulates a D-Bus signal, and returns it in the <parameter>m</parameter> output
parameter. The signal will be sent to path <parameter>path</parameter>, on the interface
<parameter>interface</parameter>, member <parameter>member</parameter>. When this message is
sent, no reply is expected.</para>
sent, no reply is expected. See
<citerefentry><refentrytitle>sd_bus_message_new_call</refentrytitle><manvolnum>1</manvolnum></citerefentry>
for a short description of the meaning of the <parameter>path</parameter>,
<parameter>interface</parameter>, and <parameter>member</parameter> parameters.
</para>
</refsect1>
<refsect1>

View File

@ -80,7 +80,7 @@
always be <constant>NUL</constant>-terminated. The returned string
will be the concatenation of the bus path prefix plus an escaped
version of the external identifier string. This operation may be
reversed with <function>sd_bus_decode()</function>. It is
reversed with <function>sd_bus_path_decode()</function>. It is
recommended to only use external identifiers that generally
require little escaping to be turned into valid bus path
identifiers (for example, by sticking to a 7-bit ASCII character