client_impl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <dlfcn.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <systemd/sd-bus.h>
  33. #include <unistd.h>
  34. // Storage for error strings
  35. static char error_string[512] = { 0 };
  36. // Simple requestor function for a gamemode
  37. static int gamemode_request(const char *function, int arg)
  38. {
  39. sd_bus_message *msg = NULL;
  40. sd_bus *bus = NULL;
  41. sd_bus_error err;
  42. memset(&err, 0, sizeof(err));
  43. int result = -1;
  44. // Open the user bus
  45. int ret = sd_bus_open_user(&bus);
  46. if (ret < 0) {
  47. snprintf(error_string,
  48. sizeof(error_string),
  49. "Could not connect to bus: %s",
  50. strerror(-ret));
  51. } else {
  52. // Attempt to send the requested function
  53. ret = sd_bus_call_method(bus,
  54. "com.feralinteractive.GameMode",
  55. "/com/feralinteractive/GameMode",
  56. "com.feralinteractive.GameMode",
  57. function,
  58. &err,
  59. &msg,
  60. arg ? "ii" : "i",
  61. getpid(),
  62. arg);
  63. if (ret < 0) {
  64. snprintf(error_string,
  65. sizeof(error_string),
  66. "Could not call method %s on com.feralinteractive.GameMode\n"
  67. "\t%s\n"
  68. "\t%s\n"
  69. "\t%s\n",
  70. function,
  71. err.name,
  72. err.message,
  73. strerror(-ret));
  74. } else {
  75. // Read the reply
  76. ret = sd_bus_message_read(msg, "i", &result);
  77. if (ret < 0) {
  78. snprintf(error_string,
  79. sizeof(error_string),
  80. "Failure to parse response: %s",
  81. strerror(-ret));
  82. }
  83. }
  84. }
  85. return result;
  86. }
  87. // Get the error string
  88. extern const char *real_gamemode_error_string(void)
  89. {
  90. return error_string;
  91. }
  92. // Wrapper to call RegisterGame
  93. extern int real_gamemode_request_start(void)
  94. {
  95. return gamemode_request("RegisterGame", 0);
  96. }
  97. // Wrapper to call UnregisterGame
  98. extern int real_gamemode_request_end(void)
  99. {
  100. return gamemode_request("UnregisterGame", 0);
  101. }
  102. // Wrapper to call QueryStatus
  103. extern int real_gamemode_query_status(void)
  104. {
  105. return gamemode_request("QueryStatus", 0);
  106. }
  107. // Wrapper to call RegisterGameByPID
  108. extern int real_gamemode_request_start_for(pid_t pid)
  109. {
  110. return gamemode_request("RegisterGameByPID", pid);
  111. }
  112. // Wrapper to call UnregisterGameByPID
  113. extern int real_gamemode_request_end_for(pid_t pid)
  114. {
  115. return gamemode_request("UnregisterGameByPID", pid);
  116. }
  117. // Wrapper to call QueryStatusByPID
  118. extern int real_gamemode_query_status_for(pid_t pid)
  119. {
  120. return gamemode_request("QueryStatusByPID", pid);
  121. }