| 1 |
'\" t |
| 2 |
.TH "SD_BUS_CALL_METHOD" "3" "" "systemd 261~rc3" "sd_bus_call_method" |
| 3 |
.\" ----------------------------------------------------------------- |
| 4 |
.\" * Define some portability stuff |
| 5 |
.\" ----------------------------------------------------------------- |
| 6 |
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 |
.\" http://bugs.debian.org/507673 |
| 8 |
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html |
| 9 |
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 |
.ie \n(.g .ds Aq \(aq |
| 11 |
.el .ds Aq ' |
| 12 |
.\" ----------------------------------------------------------------- |
| 13 |
.\" * set default formatting |
| 14 |
.\" ----------------------------------------------------------------- |
| 15 |
.\" disable hyphenation |
| 16 |
.nh |
| 17 |
.\" disable justification (adjust text to left margin only) |
| 18 |
.ad l |
| 19 |
.\" ----------------------------------------------------------------- |
| 20 |
.\" * MAIN CONTENT STARTS HERE * |
| 21 |
.\" ----------------------------------------------------------------- |
| 22 |
.SH "NAME" |
| 23 |
sd_bus_call_method, sd_bus_call_methodv, sd_bus_call_method_async, sd_bus_call_method_asyncv \- Initialize a bus message object and invoke the corresponding D\-Bus method call |
| 24 |
.SH "SYNOPSIS" |
| 25 |
.sp |
| 26 |
.ft B |
| 27 |
.nf |
| 28 |
#include <systemd/sd\-bus\&.h> |
| 29 |
.fi |
| 30 |
.ft |
| 31 |
.HP \w'typedef\ int\ (*sd_bus_message_handler_t)('u |
| 32 |
.BI "typedef int (*sd_bus_message_handler_t)(sd_bus_message\ *" "m" ", void\ *" "userdata" ", sd_bus_error\ *" "ret_error" ");" |
| 33 |
.HP \w'int\ sd_bus_call_method('u |
| 34 |
.BI "int sd_bus_call_method(sd_bus\ *" "bus" ", const\ char\ *" "destination" ", const\ char\ *" "path" ", const\ char\ *" "interface" ", const\ char\ *" "member" ", sd_bus_error\ *" "ret_error" ", sd_bus_message\ **" "reply" ", const\ char\ *" "types" ", \&.\&.\&.);" |
| 35 |
.HP \w'int\ sd_bus_call_methodv('u |
| 36 |
.BI "int sd_bus_call_methodv(sd_bus\ *" "bus" ", const\ char\ *" "destination" ", const\ char\ *" "path" ", const\ char\ *" "interface" ", const\ char\ *" "member" ", sd_bus_error\ *" "ret_error" ", sd_bus_message\ **" "reply" ", const\ char\ *" "types" ", va_list\ " "ap" ");" |
| 37 |
.HP \w'int\ sd_bus_call_method_async('u |
| 38 |
.BI "int sd_bus_call_method_async(sd_bus\ *" "bus" ", sd_bus_slot\ **" "slot" ", const\ char\ *" "destination" ", const\ char\ *" "path" ", const\ char\ *" "interface" ", const\ char\ *" "member" ", sd_bus_message_handler_t\ " "callback" ", void\ *" "userdata" ", const\ char\ *" "types" ", \&.\&.\&.);" |
| 39 |
.HP \w'int\ sd_bus_call_method_asyncv('u |
| 40 |
.BI "int sd_bus_call_method_asyncv(sd_bus\ *" "bus" ", sd_bus_slot\ **" "slot" ", const\ char\ *" "destination" ", const\ char\ *" "path" ", const\ char\ *" "interface" ", const\ char\ *" "member" ", sd_bus_message_handler_t\ " "callback" ", void\ *" "userdata" ", const\ char\ *" "types" ", va_list\ " "ap" ");" |
| 41 |
.SH "DESCRIPTION" |
| 42 |
.PP |
| 43 |
\fBsd_bus_call_method()\fR |
| 44 |
is a convenience function for initializing a bus message object and calling the corresponding D\-Bus method\&. It combines the |
| 45 |
\fBsd_bus_message_new_method_call\fR(3), |
| 46 |
\fBsd_bus_message_append\fR(3) |
| 47 |
and |
| 48 |
\fBsd_bus_call\fR(3) |
| 49 |
functions into a single function call\&. |
| 50 |
.PP |
| 51 |
\fBsd_bus_call_method_async()\fR |
| 52 |
is a convenience function for initializing a bus message object and calling the corresponding D\-Bus method asynchronously\&. It combines the |
| 53 |
\fBsd_bus_message_new_method_call\fR(3), |
| 54 |
\fBsd_bus_message_append\fR(3) |
| 55 |
and |
| 56 |
\fBsd_bus_call_async\fR(3) |
| 57 |
functions into a single function call\&. |
| 58 |
.SH "RETURN VALUE" |
| 59 |
.PP |
| 60 |
On success, these functions return a non\-negative integer\&. On failure, they return a negative errno\-style error code\&. |
| 61 |
.SS "Errors" |
| 62 |
.PP |
| 63 |
See the man pages of |
| 64 |
\fBsd_bus_message_new_method_call\fR(3), |
| 65 |
\fBsd_bus_message_append\fR(3), |
| 66 |
\fBsd_bus_call\fR(3) |
| 67 |
and |
| 68 |
\fBsd_bus_call_async\fR(3) |
| 69 |
for a list of possible errors\&. |
| 70 |
.SH "NOTES" |
| 71 |
.PP |
| 72 |
Functions described here are available as a shared library, which can be compiled against and linked to with the |
| 73 |
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1) |
| 74 |
file\&. |
| 75 |
.PP |
| 76 |
The code described here uses |
| 77 |
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call |
| 78 |
\fBsetenv\fR(3) |
| 79 |
from a parallel thread\&. It is recommended to only do calls to |
| 80 |
\fBsetenv()\fR |
| 81 |
from an early phase of the program when no other threads have been started\&. |
| 82 |
.SH "EXAMPLES" |
| 83 |
.PP |
| 84 |
\fBExample\ \&1.\ \&Make a call to a D\-Bus method that takes a single parameter\fR |
| 85 |
.sp |
| 86 |
.if n \{\ |
| 87 |
.RS 4 |
| 88 |
.\} |
| 89 |
.nf |
| 90 |
/* SPDX\-License\-Identifier: MIT\-0 */ |
| 91 |
|
| 92 |
/* This is equivalent to: |
| 93 |
* busctl call org\&.freedesktop\&.systemd1 /org/freedesktop/systemd1 \e |
| 94 |
* org\&.freedesktop\&.systemd1\&.Manager GetUnitByPID $$ |
| 95 |
* |
| 96 |
* Compile with \*(Aqcc print\-unit\-path\-call\-method\&.c \-lsystemd\*(Aq |
| 97 |
*/ |
| 98 |
|
| 99 |
#include <errno\&.h> |
| 100 |
#include <stdio\&.h> |
| 101 |
#include <sys/types\&.h> |
| 102 |
#include <unistd\&.h> |
| 103 |
|
| 104 |
#include <systemd/sd\-bus\&.h> |
| 105 |
|
| 106 |
#define _cleanup_(f) __attribute__((cleanup(f))) |
| 107 |
#define DESTINATION "org\&.freedesktop\&.systemd1" |
| 108 |
#define PATH "/org/freedesktop/systemd1" |
| 109 |
#define INTERFACE "org\&.freedesktop\&.systemd1\&.Manager" |
| 110 |
#define MEMBER "GetUnitByPID" |
| 111 |
|
| 112 |
static int log_error(int error, const char *message) { |
| 113 |
fprintf(stderr, "%s: %s\en", message, strerror(\-error)); |
| 114 |
return error; |
| 115 |
} |
| 116 |
|
| 117 |
int main(int argc, char **argv) { |
| 118 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 119 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 120 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 121 |
int r; |
| 122 |
|
| 123 |
r = sd_bus_open_system(&bus); |
| 124 |
if (r < 0) |
| 125 |
return log_error(r, "Failed to acquire bus"); |
| 126 |
|
| 127 |
r = sd_bus_call_method(bus, DESTINATION, PATH, INTERFACE, MEMBER, &error, &reply, "u", (unsigned) getpid()); |
| 128 |
if (r < 0) |
| 129 |
return log_error(r, MEMBER " call failed"); |
| 130 |
|
| 131 |
const char *ans; |
| 132 |
r = sd_bus_message_read(reply, "o", &ans); |
| 133 |
if (r < 0) |
| 134 |
return log_error(r, "Failed to read reply"); |
| 135 |
|
| 136 |
printf("Unit path is \e"%s\e"\&.\en", ans); |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
.fi |
| 141 |
.if n \{\ |
| 142 |
.RE |
| 143 |
.\} |
| 144 |
.PP |
| 145 |
This defines a minimally useful program that will open a connection to the bus, call a method, wait for the reply, and finally extract and print the answer\&. It does error handling and proper memory management\&. |
| 146 |
.SH "HISTORY" |
| 147 |
.PP |
| 148 |
\fBsd_bus_call_method()\fR, and |
| 149 |
\fBsd_bus_call_method_async()\fR |
| 150 |
were added in version 221\&. |
| 151 |
.PP |
| 152 |
\fBsd_bus_call_methodv()\fR, |
| 153 |
\fBsd_bus_call_method_asyncv()\fR |
| 154 |
were added in version 246\&. |
| 155 |
.SH "SEE ALSO" |
| 156 |
.PP |
| 157 |
\fBsystemd\fR(1), \fBsd-bus\fR(3), \fBsd_bus_message_new_method_call\fR(3), \fBsd_bus_message_append\fR(3), \fBsd_bus_call\fR(3), \fBsd_bus_set_property\fR(3), \fBsd_bus_emit_signal\fR(3) |