client_impl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Copyright (c) 2017-2019, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include <dbus/dbus.h>
  28. #include <dlfcn.h>
  29. #include <errno.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #include <unistd.h>
  36. // For developmental purposes
  37. #define DO_TRACE 0
  38. // D-Bus name, path, iface
  39. #define DAEMON_DBUS_NAME "com.feralinteractive.GameMode"
  40. #define DAEMON_DBUS_PATH "/com/feralinteractive/GameMode"
  41. #define DAEMON_DBUS_IFACE "com.feralinteractive.GameMode"
  42. #define PORTAL_DBUS_NAME "org.freedesktop.portal.Desktop"
  43. #define PORTAL_DBUS_PATH "/org/freedesktop/portal/desktop"
  44. #define PORTAL_DBUS_IFACE "org.freedesktop.portal.GameMode"
  45. // Cleanup macros
  46. #define _cleanup_(x) __attribute__((cleanup(x)))
  47. #define _cleanup_bus_ _cleanup_(hop_off_the_bus)
  48. #define _cleanup_msg_ _cleanup_(cleanup_msg)
  49. #define _cleanup_dpc_ _cleanup_(cleanup_pending_call)
  50. #ifdef NDEBUG
  51. #define DEBUG(...)
  52. #define LOG_ERROR
  53. #else
  54. #define DEBUG(...) fprintf(stderr, __VA_ARGS__)
  55. #define LOG_ERROR fprintf(stderr, "ERROR: %s \n", error_string)
  56. #endif
  57. #if DO_TRACE
  58. #define TRACE(...) fprintf(stderr, __VA_ARGS__)
  59. #else
  60. #define TRACE(...)
  61. #endif
  62. // Prototypes
  63. static int log_error(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
  64. // Storage for error strings
  65. static char error_string[512] = { 0 };
  66. // Helper to check if we are running inside a flatpak
  67. static int in_flatpak(void)
  68. {
  69. struct stat sb;
  70. int r;
  71. r = lstat("/.flatpak-info", &sb);
  72. return r == 0 && sb.st_size > 0;
  73. }
  74. static int log_error(const char *fmt, ...)
  75. {
  76. va_list args;
  77. int n;
  78. va_start(args, fmt);
  79. n = vsnprintf(error_string, sizeof(error_string), fmt, args);
  80. va_end(args);
  81. if (n < 0)
  82. DEBUG("Failed to format error string");
  83. else if ((size_t)n >= sizeof(error_string))
  84. DEBUG("Error log overflow");
  85. TRACE("ERROR: %s \n", error_string);
  86. return -1;
  87. }
  88. static void hop_off_the_bus(DBusConnection **bus)
  89. {
  90. if (bus == NULL)
  91. return;
  92. dbus_connection_unref(*bus);
  93. }
  94. static DBusConnection *hop_on_the_bus(void)
  95. {
  96. DBusConnection *bus;
  97. DBusError err;
  98. dbus_error_init(&err);
  99. bus = dbus_bus_get(DBUS_BUS_SESSION, &err);
  100. if (bus == NULL) {
  101. log_error("Could not connect to bus: %s", err.message);
  102. dbus_error_free(&err);
  103. }
  104. return bus;
  105. }
  106. /* cleanup functions */
  107. static void cleanup_msg(DBusMessage **msg)
  108. {
  109. if (msg == NULL)
  110. return;
  111. dbus_message_unref(*msg);
  112. }
  113. static void cleanup_pending_call(DBusPendingCall **call)
  114. {
  115. if (call == NULL)
  116. return;
  117. dbus_pending_call_unref(*call);
  118. }
  119. /* internal API */
  120. static int gamemode_request(const char *method, pid_t for_pid)
  121. {
  122. _cleanup_bus_ DBusConnection *bus = NULL;
  123. _cleanup_msg_ DBusMessage *msg = NULL;
  124. _cleanup_dpc_ DBusPendingCall *call = NULL;
  125. DBusMessageIter iter;
  126. DBusError err;
  127. dbus_int32_t pid;
  128. int native;
  129. int res = -1;
  130. native = !in_flatpak();
  131. pid = (dbus_int32_t)getpid();
  132. TRACE("GM: [%d] request '%s' received (for pid: %d) [portal: %s]\n",
  133. (int)pid,
  134. method,
  135. (int)for_pid,
  136. (native ? "n" : "y"));
  137. bus = hop_on_the_bus();
  138. if (bus == NULL)
  139. return -1;
  140. // If we are inside a flatpak we need to talk to the portal instead
  141. const char *dest = native ? DAEMON_DBUS_NAME : PORTAL_DBUS_NAME;
  142. const char *path = native ? DAEMON_DBUS_PATH : PORTAL_DBUS_PATH;
  143. const char *iface = native ? DAEMON_DBUS_IFACE : PORTAL_DBUS_IFACE;
  144. msg = dbus_message_new_method_call(dest, path, iface, method);
  145. if (!msg)
  146. return log_error("Could not create dbus message");
  147. dbus_message_iter_init_append(msg, &iter);
  148. dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &pid);
  149. if (for_pid != 0) {
  150. dbus_int32_t p = (dbus_int32_t)for_pid;
  151. dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &p);
  152. }
  153. dbus_connection_send_with_reply(bus, msg, &call, -1);
  154. dbus_connection_flush(bus);
  155. dbus_message_unref(msg);
  156. msg = NULL;
  157. dbus_pending_call_block(call);
  158. msg = dbus_pending_call_steal_reply(call);
  159. if (msg == NULL)
  160. return log_error("Did not receive a reply");
  161. dbus_error_init(&err);
  162. if (dbus_set_error_from_message(&err, msg))
  163. log_error("Could not call method '%s' on '%s': %s", method, dest, err.message);
  164. else if (!dbus_message_iter_init(msg, &iter) ||
  165. dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32)
  166. log_error("Failed to parse response");
  167. else
  168. dbus_message_iter_get_basic(&iter, &res);
  169. TRACE("GM: [%d] request '%s' done: %d\n", (int)pid, method, res);
  170. if (dbus_error_is_set(&err))
  171. dbus_error_free(&err);
  172. return res;
  173. }
  174. // Get the error string
  175. extern const char *real_gamemode_error_string(void)
  176. {
  177. return error_string;
  178. }
  179. // Wrapper to call RegisterGame
  180. extern int real_gamemode_request_start(void)
  181. {
  182. return gamemode_request("RegisterGame", 0);
  183. }
  184. // Wrapper to call UnregisterGame
  185. extern int real_gamemode_request_end(void)
  186. {
  187. return gamemode_request("UnregisterGame", 0);
  188. }
  189. // Wrapper to call QueryStatus
  190. extern int real_gamemode_query_status(void)
  191. {
  192. return gamemode_request("QueryStatus", 0);
  193. }
  194. // Wrapper to call RegisterGameByPID
  195. extern int real_gamemode_request_start_for(pid_t pid)
  196. {
  197. return gamemode_request("RegisterGameByPID", pid);
  198. }
  199. // Wrapper to call UnregisterGameByPID
  200. extern int real_gamemode_request_end_for(pid_t pid)
  201. {
  202. return gamemode_request("UnregisterGameByPID", pid);
  203. }
  204. // Wrapper to call QueryStatusByPID
  205. extern int real_gamemode_query_status_for(pid_t pid)
  206. {
  207. return gamemode_request("QueryStatusByPID", pid);
  208. }