]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/print-unit-path-call-method.c
update TODO
[thirdparty/systemd.git] / man / print-unit-path-call-method.c
CommitLineData
ce199d12
LB
1/* SPDX-License-Identifier: MIT-0 */
2
3/* This is equivalent to:
4 * busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 \
5 * org.freedesktop.systemd1.Manager GetUnitByPID $$
6 *
7 * Compile with 'cc print-unit-path-call-method.c -lsystemd'
8 */
9
10#include <errno.h>
11#include <stdio.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include <systemd/sd-bus.h>
16
17#define _cleanup_(f) __attribute__((cleanup(f)))
18#define DESTINATION "org.freedesktop.systemd1"
19#define PATH "/org/freedesktop/systemd1"
20#define INTERFACE "org.freedesktop.systemd1.Manager"
21#define MEMBER "GetUnitByPID"
22
23static int log_error(int error, const char *message) {
24 errno = -error;
25 fprintf(stderr, "%s: %m\n", message);
26 return error;
27}
28
29int main(int argc, char **argv) {
30 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
31 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
32 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
33 int r;
34
35 r = sd_bus_open_system(&bus);
36 if (r < 0)
37 return log_error(r, "Failed to acquire bus");
38
39 r = sd_bus_call_method(bus, DESTINATION, PATH, INTERFACE, MEMBER, &error, &reply, "u", (unsigned) getpid());
40 if (r < 0)
41 return log_error(r, MEMBER " call failed");
42
43 const char *ans;
44 r = sd_bus_message_read(reply, "o", &ans);
45 if (r < 0)
46 return log_error(r, "Failed to read reply");
47
48 printf("Unit path is \"%s\".\n", ans);
49
50 return 0;
51}