gamemode_client.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. Copyright (c) 2017-2018, 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. #ifndef CLIENT_GAMEMODE_H
  27. #define CLIENT_GAMEMODE_H
  28. /*
  29. * GameMode supports the following client functions
  30. * Requests are refcounted in the daemon
  31. *
  32. * int gamemode_request_start() - Request gamemode starts
  33. * 0 if the request was sent successfully
  34. * -1 if the request failed
  35. *
  36. * int gamemode_request_end() - Request gamemode ends
  37. * 0 if the request was sent successfully
  38. * -1 if the request failed
  39. *
  40. * int gamemode_query_status() - Query the current status of gamemode
  41. * 0 if gamemode is inactive
  42. * 1 if gamemode is active
  43. * 2 if gamemode is active and this client is registered
  44. * -1 if the query failed
  45. *
  46. * int gamemode_request_start_for(pid_t pid) - Request gamemode starts for another process
  47. * 0 if the request was sent successfully
  48. * -1 if the request failed
  49. * -2 if the request was rejected
  50. *
  51. * int gamemode_request_end_for(pid_t pid) - Request gamemode ends for another process
  52. * 0 if the request was sent successfully
  53. * -1 if the request failed
  54. * -2 if the request was rejected
  55. *
  56. * const char* gamemode_error_string() - Get an error string
  57. * returns a string describing any of the above errors
  58. */
  59. #include <stdbool.h>
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <unistd.h>
  63. #include <dlfcn.h>
  64. #include <errno.h>
  65. #include <string.h>
  66. #include <sys/types.h>
  67. static char internal_gamemode_client_error_string[512] = { 0 };
  68. /**
  69. * Load libgamemode dynamically to dislodge us from most dependencies.
  70. * This allows clients to link and/or use this regardless of runtime.
  71. * See SDL2 for an example of the reasoning behind this in terms of
  72. * dynamic versioning as well.
  73. */
  74. static volatile int internal_libgamemode_loaded = 1;
  75. /* Typedefs for the functions to load */
  76. typedef int (*api_call_return_int)(void);
  77. typedef const char *(*api_call_return_cstring)(void);
  78. typedef int (*api_call_pid_return_int)(pid_t);
  79. /* Storage for functors */
  80. static api_call_return_int REAL_internal_gamemode_request_start = NULL;
  81. static api_call_return_int REAL_internal_gamemode_request_end = NULL;
  82. static api_call_return_int REAL_internal_gamemode_query_status = NULL;
  83. static api_call_return_cstring REAL_internal_gamemode_error_string = NULL;
  84. static api_call_pid_return_int REAL_internal_gamemode_request_start_for = NULL;
  85. static api_call_pid_return_int REAL_internal_gamemode_request_end_for = NULL;
  86. /**
  87. * Internal helper to perform the symbol binding safely.
  88. *
  89. * Returns 0 on success and -1 on failure
  90. */
  91. __attribute__((always_inline)) static inline int internal_bind_libgamemode_symbol(
  92. void *handle, const char *name, void **out_func, size_t func_size, bool required)
  93. {
  94. void *symbol_lookup = NULL;
  95. char *dl_error = NULL;
  96. /* Safely look up the symbol */
  97. symbol_lookup = dlsym(handle, name);
  98. dl_error = dlerror();
  99. if (required && (dl_error || !symbol_lookup)) {
  100. snprintf(internal_gamemode_client_error_string,
  101. sizeof(internal_gamemode_client_error_string),
  102. "dlsym failed - %s",
  103. dl_error);
  104. return -1;
  105. }
  106. /* Have the symbol correctly, copy it to make it usable */
  107. memcpy(out_func, &symbol_lookup, func_size);
  108. return 0;
  109. }
  110. /**
  111. * Loads libgamemode and needed functions
  112. *
  113. * Returns 0 on success and -1 on failure
  114. */
  115. __attribute__((always_inline)) static inline int internal_load_libgamemode(void)
  116. {
  117. /* We start at 1, 0 is a success and -1 is a fail */
  118. if (internal_libgamemode_loaded != 1) {
  119. return internal_libgamemode_loaded;
  120. }
  121. /* Anonymous struct type to define our bindings */
  122. struct binding {
  123. const char *name;
  124. void **functor;
  125. size_t func_size;
  126. bool required;
  127. } bindings[] = {
  128. { "real_gamemode_request_start",
  129. (void **)&REAL_internal_gamemode_request_start,
  130. sizeof(REAL_internal_gamemode_request_start),
  131. true },
  132. { "real_gamemode_request_end",
  133. (void **)&REAL_internal_gamemode_request_end,
  134. sizeof(REAL_internal_gamemode_request_end),
  135. true },
  136. { "real_gamemode_query_status",
  137. (void **)&REAL_internal_gamemode_query_status,
  138. sizeof(REAL_internal_gamemode_query_status),
  139. false },
  140. { "real_gamemode_error_string",
  141. (void **)&REAL_internal_gamemode_error_string,
  142. sizeof(REAL_internal_gamemode_error_string),
  143. true },
  144. { "real_gamemode_request_start_for",
  145. (void **)&REAL_internal_gamemode_request_start_for,
  146. sizeof(REAL_internal_gamemode_request_start_for),
  147. false },
  148. { "real_gamemode_request_end_for",
  149. (void **)&REAL_internal_gamemode_request_end_for,
  150. sizeof(REAL_internal_gamemode_request_end_for),
  151. false },
  152. };
  153. void *libgamemode = NULL;
  154. /* Try and load libgamemode */
  155. libgamemode = dlopen("libgamemode.so.0", RTLD_NOW);
  156. if (!libgamemode) {
  157. /* Attempt to load unversioned library for compatibility with older
  158. * versions (as of writing, there are no ABI changes between the two -
  159. * this may need to change if ever ABI-breaking changes are made) */
  160. libgamemode = dlopen("libgamemode.so", RTLD_NOW);
  161. if (!libgamemode) {
  162. snprintf(internal_gamemode_client_error_string,
  163. sizeof(internal_gamemode_client_error_string),
  164. "dlopen failed - %s",
  165. dlerror());
  166. internal_libgamemode_loaded = -1;
  167. return -1;
  168. }
  169. }
  170. /* Attempt to bind all symbols */
  171. for (size_t i = 0; i < sizeof(bindings) / sizeof(bindings[0]); i++) {
  172. struct binding *binder = &bindings[i];
  173. if (internal_bind_libgamemode_symbol(libgamemode,
  174. binder->name,
  175. binder->functor,
  176. binder->func_size,
  177. binder->required)) {
  178. internal_libgamemode_loaded = -1;
  179. return -1;
  180. };
  181. }
  182. /* Success */
  183. internal_libgamemode_loaded = 0;
  184. return 0;
  185. }
  186. /**
  187. * Redirect to the real libgamemode
  188. */
  189. __attribute__((always_inline)) static inline const char *gamemode_error_string(void)
  190. {
  191. /* If we fail to load the system gamemode, or we have an error string already, return our error
  192. * string instead of diverting to the system version */
  193. if (internal_load_libgamemode() < 0 || internal_gamemode_client_error_string[0] != '\0') {
  194. return internal_gamemode_client_error_string;
  195. }
  196. return REAL_internal_gamemode_error_string();
  197. }
  198. /**
  199. * Redirect to the real libgamemod
  200. * Allow automatically requesting game mode
  201. * Also prints errors as they happen.
  202. */
  203. #ifdef GAMEMODE_AUTO
  204. __attribute__((constructor))
  205. #else
  206. __attribute__((always_inline)) static inline
  207. #endif
  208. int gamemode_request_start(void)
  209. {
  210. /* Need to load gamemode */
  211. if (internal_load_libgamemode() < 0) {
  212. #ifdef GAMEMODE_AUTO
  213. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  214. #endif
  215. return -1;
  216. }
  217. if (REAL_internal_gamemode_request_start() < 0) {
  218. #ifdef GAMEMODE_AUTO
  219. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  220. #endif
  221. return -1;
  222. }
  223. return 0;
  224. }
  225. /* Redirect to the real libgamemode */
  226. #ifdef GAMEMODE_AUTO
  227. __attribute__((destructor))
  228. #else
  229. __attribute__((always_inline)) static inline
  230. #endif
  231. int gamemode_request_end(void)
  232. {
  233. /* Need to load gamemode */
  234. if (internal_load_libgamemode() < 0) {
  235. #ifdef GAMEMODE_AUTO
  236. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  237. #endif
  238. return -1;
  239. }
  240. if (REAL_internal_gamemode_request_end() < 0) {
  241. #ifdef GAMEMODE_AUTO
  242. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  243. #endif
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. /* Redirect to the real libgamemode */
  249. __attribute__((always_inline)) static inline int gamemode_query_status(void)
  250. {
  251. /* Need to load gamemode */
  252. if (internal_load_libgamemode() < 0) {
  253. return -1;
  254. }
  255. if (REAL_internal_gamemode_query_status == NULL) {
  256. snprintf(internal_gamemode_client_error_string,
  257. sizeof(internal_gamemode_client_error_string),
  258. "gamemode_query_status missing (older host?)");
  259. return -1;
  260. }
  261. return REAL_internal_gamemode_query_status();
  262. }
  263. /* Redirect to the real libgamemode */
  264. __attribute__((always_inline)) static inline int gamemode_request_start_for(pid_t pid)
  265. {
  266. /* Need to load gamemode */
  267. if (internal_load_libgamemode() < 0) {
  268. return -1;
  269. }
  270. if (REAL_internal_gamemode_request_start_for == NULL) {
  271. snprintf(internal_gamemode_client_error_string,
  272. sizeof(internal_gamemode_client_error_string),
  273. "gamemode_request_start_for missing (older host?)");
  274. return -1;
  275. }
  276. return REAL_internal_gamemode_request_start_for(pid);
  277. }
  278. /* Redirect to the real libgamemode */
  279. __attribute__((always_inline)) static inline int gamemode_request_end_for(pid_t pid)
  280. {
  281. /* Need to load gamemode */
  282. if (internal_load_libgamemode() < 0) {
  283. return -1;
  284. }
  285. if (REAL_internal_gamemode_request_end_for == NULL) {
  286. snprintf(internal_gamemode_client_error_string,
  287. sizeof(internal_gamemode_client_error_string),
  288. "gamemode_request_end_for missing (older host?)");
  289. return -1;
  290. }
  291. return REAL_internal_gamemode_request_end_for(pid);
  292. }
  293. #endif // CLIENT_GAMEMODE_H