1SD_BUS_CALL_METHOD(3)         sd_bus_call_method         SD_BUS_CALL_METHOD(3)
2
3
4

NAME

6       sd_bus_call_method, sd_bus_call_methodv, sd_bus_call_method_async,
7       sd_bus_call_method_asyncv - Initialize a bus message object and invoke
8       the corresponding D-Bus method call
9

SYNOPSIS

11       #include <systemd/sd-bus.h>
12
13       typedef int (*sd_bus_message_handler_t)(sd_bus_message *m,
14                                               void *userdata,
15                                               sd_bus_error *ret_error);
16
17       int sd_bus_call_method(sd_bus *bus, const char *destination,
18                              const char *path, const char *interface,
19                              const char *member, sd_bus_error *ret_error,
20                              sd_bus_message **reply, const char *types, ...);
21
22       int sd_bus_call_methodv(sd_bus *bus, const char *destination,
23                               const char *path, const char *interface,
24                               const char *member, sd_bus_error *ret_error,
25                               sd_bus_message **reply, const char *types,
26                               va_list ap);
27
28       int sd_bus_call_method_async(sd_bus *bus, sd_bus_slot **slot,
29                                    const char *destination, const char *path,
30                                    const char *interface, const char *member,
31                                    sd_bus_message_handler_t callback,
32                                    void *userdata, const char *types, ...);
33
34       int sd_bus_call_method_asyncv(sd_bus *bus, sd_bus_slot **slot,
35                                     const char *destination,
36                                     const char *path, const char *interface,
37                                     const char *member,
38                                     sd_bus_message_handler_t callback,
39                                     void *userdata, const char *types,
40                                     va_list ap);
41

DESCRIPTION

43       sd_bus_call_method() is a convenience function for initializing a bus
44       message object and calling the corresponding D-Bus method. It combines
45       the sd_bus_message_new_method_call(3), sd_bus_message_append(3) and
46       sd_bus_call(3) functions into a single function call.
47
48       sd_bus_call_method_async() is a convenience function for initializing a
49       bus message object and calling the corresponding D-Bus method
50       asynchronously. It combines the sd_bus_message_new_method_call(3),
51       sd_bus_message_append(3) and sd_bus_call_async(3) functions into a
52       single function call.
53

RETURN VALUE

55       On success, these functions return a non-negative integer. On failure,
56       they return a negative errno-style error code.
57
58   Errors
59       See the man pages of sd_bus_message_new_method_call(3),
60       sd_bus_message_append(3), sd_bus_call(3) and sd_bus_call_async(3) for a
61       list of possible errors.
62

NOTES

64       These APIs are implemented as a shared library, which can be compiled
65       and linked to with the libsystemd pkg-config(1) file.
66

EXAMPLES

68       Example 1. Make a call to a D-Bus method that takes a single parameter
69
70           /* SPDX-License-Identifier: MIT-0 */
71
72           /* This is equivalent to:
73            * busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 \
74            *       org.freedesktop.systemd1.Manager GetUnitByPID $$
75            *
76            * Compile with 'cc print-unit-path-call-method.c -lsystemd'
77            */
78
79           #include <errno.h>
80           #include <stdio.h>
81           #include <sys/types.h>
82           #include <unistd.h>
83
84           #include <systemd/sd-bus.h>
85
86           #define _cleanup_(f) __attribute__((cleanup(f)))
87           #define DESTINATION "org.freedesktop.systemd1"
88           #define PATH        "/org/freedesktop/systemd1"
89           #define INTERFACE   "org.freedesktop.systemd1.Manager"
90           #define MEMBER      "GetUnitByPID"
91
92           static int log_error(int error, const char *message) {
93             errno = -error;
94             fprintf(stderr, "%s: %m\n", message);
95             return error;
96           }
97
98           int main(int argc, char **argv) {
99             _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
100             _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
101             _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
102             int r;
103
104             r = sd_bus_open_system(&bus);
105             if (r < 0)
106               return log_error(r, "Failed to acquire bus");
107
108             r = sd_bus_call_method(bus, DESTINATION, PATH, INTERFACE, MEMBER, &error, &reply, "u", (unsigned) getpid());
109             if (r < 0)
110               return log_error(r, MEMBER " call failed");
111
112             const char *ans;
113             r = sd_bus_message_read(reply, "o", &ans);
114             if (r < 0)
115               return log_error(r, "Failed to read reply");
116
117             printf("Unit path is \"%s\".\n", ans);
118
119             return 0;
120           }
121
122       This defines a minimally useful program that will open a connection to
123       the bus, call a method, wait for the reply, and finally extract and
124       print the answer. It does error handling and proper memory management.
125

SEE ALSO

127       systemd(1), sd-bus(3), sd_bus_message_new_method_call(3),
128       sd_bus_message_append(3), sd_bus_call(3), sd_bus_set_property(3),
129       sd_bus_emit_signal(3)
130
131
132
133systemd 253                                              SD_BUS_CALL_METHOD(3)
Impressum