main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (c) 2017, 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 "daemonize.h"
  45. #include "dbus_messaging.h"
  46. #include "gamemode.h"
  47. #include "logging.h"
  48. #include <signal.h>
  49. #include <string.h>
  50. #include <unistd.h>
  51. static void sigint_handler(int signo)
  52. {
  53. LOG_MSG("Quitting by request...\n");
  54. /* Clean up nicely */
  55. game_mode_context_destroy(game_mode_context_instance());
  56. exit(EXIT_SUCCESS);
  57. }
  58. /**
  59. * Main bootstrap entry into gamemoded
  60. */
  61. int main(int argc, char *argv[])
  62. {
  63. GameModeContext *context = NULL;
  64. /* Gather command line options */
  65. bool daemon = false;
  66. bool use_syslog = false;
  67. int opt = 0;
  68. while ((opt = getopt(argc, argv, "dl")) != -1) {
  69. switch (opt) {
  70. case 'd':
  71. daemon = true;
  72. break;
  73. case 'l':
  74. use_syslog = true;
  75. break;
  76. default:
  77. fprintf(stderr, "Usage: %s [-d] [-l]\n", argv[0]);
  78. exit(EXIT_FAILURE);
  79. break;
  80. }
  81. }
  82. /* If syslog is requested, set it up with our process name */
  83. if (use_syslog) {
  84. set_use_syslog(argv[0]);
  85. }
  86. /* Daemonize ourselves first if asked */
  87. if (daemon) {
  88. daemonize(argv[0]);
  89. }
  90. /* Set up the game mode context */
  91. context = game_mode_context_instance();
  92. game_mode_context_init(context);
  93. /* Handle quits cleanly */
  94. if (signal(SIGINT, sigint_handler) == SIG_ERR) {
  95. FATAL_ERRORNO("Could not catch SIGINT");
  96. }
  97. /* Run the main dbus message loop */
  98. game_mode_context_loop(context);
  99. game_mode_context_destroy(context);
  100. /* Log we're finished */
  101. LOG_MSG("Quitting naturally...\n");
  102. }