20 {
21#ifdef HAVE_LIBSYSTEMD
22
23 if (g_inhibit_fd >= 0) {
24 log_debug("Keepawake already enabled");
25 return ASCIICHAT_OK;
26 }
27
28
29 if (sd_bus_default_system == NULL) {
30 log_dev("systemd not available, keepawake not supported");
31 return ASCIICHAT_OK;
32 }
33
34 sd_bus *bus = NULL;
35 sd_bus_message *reply = NULL;
36 sd_bus_error error = SD_BUS_ERROR_NULL;
37
38 if (sd_bus_default_system(&bus) < 0) {
39 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to connect to system bus");
40 }
41
42 if (!bus) {
43 return SET_ERRNO(ERROR_PLATFORM_INIT, "System bus is NULL");
44 }
45
46 int r = sd_bus_call_method(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager",
47 "Inhibit", &error, &reply, "ssss",
48 "sleep:idle",
49 "ascii-chat",
50 "Video/audio streaming",
51 "block"
52 );
53
54 if (r < 0) {
55 sd_bus_error_free(&error);
56 sd_bus_unref(bus);
57 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to inhibit sleep via systemd");
58 }
59
60 if (!reply) {
61 sd_bus_error_free(&error);
62 sd_bus_unref(bus);
63 return SET_ERRNO(ERROR_PLATFORM_INIT, "systemd inhibit reply is NULL");
64 }
65
66 if (sd_bus_message_read(reply, "h", &g_inhibit_fd) < 0) {
67 sd_bus_message_unref(reply);
68 sd_bus_error_free(&error);
69 sd_bus_unref(bus);
70 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to read inhibit fd from systemd reply");
71 }
72
73 sd_bus_message_unref(reply);
74 sd_bus_error_free(&error);
75 sd_bus_unref(bus);
76
77 log_debug("Keepawake enabled via systemd-inhibit (fd: %d)", g_inhibit_fd);
78 return ASCIICHAT_OK;
79
80#else
81
82 log_debug("Keepawake not implemented on this platform");
83 return ASCIICHAT_OK;
84#endif
85}