1 /* SPDX-License-Identifier: MIT-0 */
3 /* This is equivalent to:
4 * busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 \
5 * org.freedesktop.systemd1.Manager GetUnitByPID $$
7 * Compile with 'cc print-unit-path.c -lsystemd'
12 #include <sys/types.h>
15 #include <systemd/sd-bus.h>
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"
23 static int log_error(int error
, const char *message
) {
25 fprintf(stderr
, "%s: %m\n", message
);
29 int 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
, *m
= NULL
;
35 r
= sd_bus_open_system(&bus
);
37 return log_error(r
, "Failed to acquire bus");
39 r
= sd_bus_message_new_method_call(bus
, &m
,
40 DESTINATION
, PATH
, INTERFACE
, MEMBER
);
42 return log_error(r
, "Failed to create bus message");
44 r
= sd_bus_message_append(m
, "u", (unsigned) getpid());
46 return log_error(r
, "Failed to append to bus message");
48 r
= sd_bus_call(bus
, m
, -1, &error
, &reply
);
50 return log_error(r
, MEMBER
" call failed");
53 r
= sd_bus_message_read(reply
, "o", &ans
);
55 return log_error(r
, "Failed to read reply");
57 printf("Unit path is \"%s\".\n", ans
);