main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /**
  27. * Simple daemon to allow user space programs to control the CPU governors
  28. *
  29. * The main process is responsible for bootstrapping the D-BUS daemon, caching
  30. * the initial governor settings, and then responding to requests over D-BUS.
  31. *
  32. * Clients register their pid(s) with the service, which are routinely checked
  33. * to see if they've expired. Once we reach our first actively registered client
  34. * we put the system into "game mode", i.e. move the CPU governor into a performance
  35. * mode.
  36. *
  37. * Upon exit, or when all clients have stopped running, we put the system back
  38. * into the default governor policy, which is invariably powersave or similar
  39. * on laptops. This ensures that the system is obtaining the maximum performance
  40. * whilst gaming, and allowed to sanely return to idle once the workload is
  41. * complete.
  42. */
  43. #define _GNU_SOURCE
  44. #include "config.h"
  45. #include "daemonize.h"
  46. #include "dbus_messaging.h"
  47. #include "gamemode.h"
  48. #include "gamemode_client.h"
  49. #include "logging.h"
  50. #include <signal.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <systemd/sd-daemon.h>
  54. #include <unistd.h>
  55. #define USAGE_TEXT \
  56. "Usage: %s [-d] [-l] [-r] [-t] [-h] [-v]\n\n" \
  57. " -d daemonize self after launch\n" \
  58. " -l log to syslog\n" \
  59. " -r request gamemode and pause\n" \
  60. " -t run tests\n" \
  61. " -h print this help\n" \
  62. " -v print version\n" \
  63. "\n" \
  64. "See man page for more information.\n"
  65. #define VERSION_TEXT "gamemode version: v" GAMEMODE_VERSION "\n"
  66. static void sigint_handler(__attribute__((unused)) int signo)
  67. {
  68. LOG_MSG("Quitting by request...\n");
  69. sd_notify(0, "STATUS=GameMode is quitting by request...\n");
  70. /* Clean up nicely */
  71. game_mode_context_destroy(game_mode_context_instance());
  72. _Exit(EXIT_SUCCESS);
  73. }
  74. static void sigint_handler_noexit(__attribute__((unused)) int signo)
  75. {
  76. LOG_MSG("Quitting by request...\n");
  77. }
  78. /**
  79. * Main bootstrap entry into gamemoded
  80. */
  81. int main(int argc, char *argv[])
  82. {
  83. GameModeContext *context = NULL;
  84. /* Gather command line options */
  85. bool daemon = false;
  86. bool use_syslog = false;
  87. int opt = 0;
  88. int status;
  89. while ((opt = getopt(argc, argv, "dlsrtvh")) != -1) {
  90. switch (opt) {
  91. case 'd':
  92. daemon = true;
  93. break;
  94. case 'l':
  95. use_syslog = true;
  96. break;
  97. case 's': {
  98. if ((status = gamemode_query_status()) < 0) {
  99. LOG_ERROR("gamemode status request failed: %s\n", gamemode_error_string());
  100. exit(EXIT_FAILURE);
  101. } else if (status > 0) {
  102. LOG_MSG("gamemode is active\n");
  103. } else {
  104. LOG_MSG("gamemode is inactive\n");
  105. }
  106. exit(EXIT_SUCCESS);
  107. break;
  108. }
  109. case 'r':
  110. if (gamemode_request_start() < 0) {
  111. LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
  112. exit(EXIT_FAILURE);
  113. }
  114. if ((status = gamemode_query_status()) == 2) {
  115. LOG_MSG("gamemode request succeeded and is active\n");
  116. } else if (status == 1) {
  117. LOG_ERROR("gamemode request succeeded and is active but registration failed\n");
  118. exit(EXIT_FAILURE);
  119. } else {
  120. LOG_ERROR("gamemode request succeeded but is not active\n");
  121. exit(EXIT_FAILURE);
  122. }
  123. // Simply pause and wait a SIGINT
  124. if (signal(SIGINT, sigint_handler_noexit) == SIG_ERR) {
  125. FATAL_ERRORNO("Could not catch SIGINT");
  126. }
  127. pause();
  128. // Explicitly clean up
  129. if (gamemode_request_end() < 0) {
  130. LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
  131. exit(EXIT_FAILURE);
  132. }
  133. exit(EXIT_SUCCESS);
  134. break;
  135. case 't':
  136. status = game_mode_run_client_tests();
  137. exit(status);
  138. break;
  139. case 'v':
  140. LOG_MSG(VERSION_TEXT);
  141. exit(EXIT_SUCCESS);
  142. break;
  143. case 'h':
  144. LOG_MSG(USAGE_TEXT, argv[0]);
  145. exit(EXIT_SUCCESS);
  146. break;
  147. default:
  148. fprintf(stderr, USAGE_TEXT, argv[0]);
  149. exit(EXIT_FAILURE);
  150. break;
  151. }
  152. }
  153. /* If syslog is requested, set it up with our process name */
  154. if (use_syslog) {
  155. set_use_syslog(argv[0]);
  156. }
  157. /* Daemonize ourselves first if asked */
  158. if (daemon) {
  159. daemonize(argv[0]);
  160. }
  161. /* Log a version message on startup */
  162. LOG_MSG("v%s\n", GAMEMODE_VERSION);
  163. /* Set up the game mode context */
  164. context = game_mode_context_instance();
  165. game_mode_context_init(context);
  166. /* Handle quits cleanly */
  167. if (signal(SIGINT, sigint_handler) == SIG_ERR) {
  168. FATAL_ERRORNO("Could not catch SIGINT");
  169. }
  170. if (signal(SIGTERM, sigint_handler) == SIG_ERR) {
  171. FATAL_ERRORNO("Could not catch SIGTERM");
  172. }
  173. /* Run the main dbus message loop */
  174. game_mode_context_loop(context);
  175. game_mode_context_destroy(context);
  176. /* Log we're finished */
  177. LOG_MSG("Quitting naturally...\n");
  178. sd_notify(0, "STATUS=GameMode is quitting naturally...\n");
  179. }