man-pages/man3/sd_bus_message_new_method_call.3.html
2021-03-31 01:06:50 +01:00

309 lines
7.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SD_BUS_MESSAGE_NEW_METHOD_CALL</TITLE>
</HEAD><BODY>
<H1>SD_BUS_MESSAGE_NEW_METHOD_CALL</H1>
Section: sd_bus_message_new_method_call (3)<BR>Updated: <BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
sd_bus_message_new_method_call, sd_bus_message_new_method_return - Create a method call message
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<P>
<B>
</B><PRE>
#include &lt;<A HREF="file:///usr/include/systemd/sd-bus.h">systemd/sd-bus.h</A>&gt;
</PRE>
<DL COMPACT>
<DT id="1">
<B>int sd_bus_message_new_method_call(sd_bus&nbsp;*</B><I>bus</I><B>, sd_bus_message&nbsp;**</B><I>m</I><B>, const&nbsp;char&nbsp;*</B><I>destination</I><B>, const&nbsp;char&nbsp;*</B><I>path</I><B>, const&nbsp;char&nbsp;*</B><I>interface</I><B>, const&nbsp;char&nbsp;*</B><I>member</I><B>);</B>
<DT id="2">
<B>int sd_bus_message_new_method_return(sd_bus_message&nbsp;*</B><I>call</I><B>, sd_bus_message&nbsp;**</B><I>m</I><B>);</B>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<P>
<DD>The
<B>sd_bus_message_new_method_call()</B>
function creates a new bus message object that encapsulates a D-Bus method call, and returns it in the
<I>m</I>
output parameter. The call will be made on the destination
<I>destination</I>, path
<I>path</I>, on the interface
<I>interface</I>, member
<I>member</I>.
<P>
Briefly, the
<I>destination</I>
is a dot-separated name that identifies a service connected to the bus. The
<I>path</I>
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
<I>interface</I>
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
<I>members</I>
and are identified by a simple name composed of ASCII letters, numbers, and underscores. See the
m[blue]<B>D-Bus Tutorial</B>m[]<FONT SIZE="-2">[1]</FONT>
for an in-depth explanation.
<P>
The
<I>destination</I>
parameter may be
<B>NULL</B>. The
<I>interface</I>
parameter may be
<B>NULL</B>, if the destination has only a single member with the given name and there is no ambiguity if the interface name is omitted.
<P>
The
<B>sd_bus_message_new_method_call()</B>
function creates a new bus message object that is a reply to the method call
<I>call</I>
and returns it in the
<I>m</I>
output parameter. The
<I>call</I>
parameter must be a method call message. The sender of
<I>call</I>
is used as the destination.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<P>
This function returns 0 if the message object was successfully created, and a negative errno-style error code otherwise.
<A NAME="lbAF">&nbsp;</A>
<H3>Errors</H3>
<P>
Returned errors may indicate the following problems:
<P>
<B>-EINVAL</B>
<DL COMPACT><DT id="3"><DD>
The output parameter
<I>m</I>
is
<B>NULL</B>.
<P>
The
<I>destination</I>
parameter is non-null and is not a valid D-Bus service name (&quot;org.somewhere.Something&quot;), the
<I>path</I>
parameter is not a valid D-Bus path (&quot;/an/object/path&quot;), the
<I>interface</I>
parameter is non-null and is not a valid D-Bus interface name (&quot;an.interface.name&quot;), or the
<I>member</I>
parameter is not a valid D-Bus member (&quot;Name&quot;).
<P>
The
<I>call</I>
parameter is not a method call object.
</DL>
<P>
<B>-ENOTCONN</B>
<DL COMPACT><DT id="4"><DD>
The bus parameter
<I>bus</I>
is
<B>NULL</B>
or the bus is not connected.
</DL>
<P>
<B>-ENOMEM</B>
<DL COMPACT><DT id="5"><DD>
Memory allocation failed.
</DL>
<P>
<B>-EPERM</B>
<DL COMPACT><DT id="6"><DD>
The
<I>call</I>
parameter is not sealed.
</DL>
<P>
<B>-EOPNOTSUPP</B>
<DL COMPACT><DT id="7"><DD>
The
<I>call</I>
message does not have a cookie.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>NOTES</H2>
<P>
These APIs are implemented as a shared library, which can be compiled and linked to with the
<B>libsystemd</B>&nbsp;<B><A HREF="/cgi-bin/man/man2html?1+pkg-config">pkg-config</A></B>(1)
file.
<A NAME="lbAH">&nbsp;</A>
<H2>EXAMPLES</H2>
<P>
<B>Example&nbsp;1.&nbsp;Make a call to a D-Bus method that takes a single parameter</B>
<P>
<DL COMPACT><DT id="8"><DD>
<PRE>
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/systemd/sd-bus.h">systemd/sd-bus.h</A>&gt;
#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 &quot;org.freedesktop.systemd1&quot;
#define PATH &quot;/org/freedesktop/systemd1&quot;
#define INTERFACE &quot;org.freedesktop.systemd1.Manager&quot;
#define MEMBER &quot;GetUnitByPID&quot;
static int log_error(int error, const char *message) {
fprintf(stderr, &quot;%s: %s\n&quot;, 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, &amp;m,
DESTINATION, PATH, INTERFACE, MEMBER);
if (r &lt; 0)
return log_error(r, &quot;Failed to create bus message&quot;);
r = sd_bus_message_append(m, &quot;u&quot;, (unsigned) getpid());
if (r &lt; 0)
return log_error(r, &quot;Failed to append to bus message&quot;);
r = sd_bus_call(bus, m, -1, &amp;error, &amp;reply);
if (r &lt; 0)
return log_error(r, &quot;Call failed&quot;);
const char *ans;
r = sd_bus_message_read(reply, &quot;o&quot;, &amp;ans);
if (r &lt; 0)
return log_error(r, &quot;Failed to read reply&quot;);
printf(&quot;Unit path is \&quot;%s\&quot;.\n&quot;, 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(&amp;bus);
if (r &lt; 0)
return log_error(r, &quot;Failed to acquire bus&quot;);
print_unit_path(bus);
}
</PRE>
</DL>
<P>
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.
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<P>
<B><A HREF="/cgi-bin/man/man2html?1+systemd">systemd</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?3+sd-bus">sd-bus</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sd_bus_path_encode">sd_bus_path_encode</A></B>(3)
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
<DL COMPACT>
<DT id="9"> 1.<DD>
D-Bus Tutorial
<DL COMPACT><DT id="10"><DD>
<A HREF="https://dbus.freedesktop.org/doc/dbus-tutorial.html#concepts">https://dbus.freedesktop.org/doc/dbus-tutorial.html#concepts</A>
</DL>
<P>
</DL>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="11"><A HREF="#lbAB">NAME</A><DD>
<DT id="12"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="13"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="14"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DL>
<DT id="15"><A HREF="#lbAF">Errors</A><DD>
</DL>
<DT id="16"><A HREF="#lbAG">NOTES</A><DD>
<DT id="17"><A HREF="#lbAH">EXAMPLES</A><DD>
<DT id="18"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="19"><A HREF="#lbAJ">NOTES</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:54 GMT, March 31, 2021
</BODY>
</HTML>