dbus_messaging.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "dbus_messaging.h"
  27. #include "logging.h"
  28. #include "gamemode.h"
  29. #include "governors.h"
  30. #include "daemonize.h"
  31. #include <stdlib.h>
  32. #include <systemd/sd-bus.h>
  33. // sd-bus tracker values
  34. static sd_bus* bus = NULL;
  35. static sd_bus_slot* slot = NULL;
  36. // Clean up any resources as needed
  37. static void clean_up()
  38. {
  39. if( slot )
  40. sd_bus_slot_unref( slot );
  41. slot = NULL;
  42. if( bus )
  43. sd_bus_unref( bus );
  44. bus = NULL;
  45. }
  46. // Callback for RegisterGame
  47. static int method_register_game( sd_bus_message *m,
  48. void *userdata,
  49. sd_bus_error *ret_error )
  50. {
  51. int pid = 0;
  52. int ret = sd_bus_message_read( m, "i", &pid );
  53. if( ret < 0 )
  54. {
  55. LOG_ERROR( "Failed to parse input parameters: %s\n", strerror(-ret) );
  56. return ret;
  57. }
  58. register_game( pid );
  59. return sd_bus_reply_method_return( m, "i", 0 );
  60. }
  61. // Callback for UnregisterGame
  62. static int method_unregister_game( sd_bus_message *m,
  63. void *userdata,
  64. sd_bus_error *ret_error )
  65. {
  66. int pid = 0;
  67. int ret = sd_bus_message_read( m, "i", &pid );
  68. if( ret < 0 )
  69. {
  70. LOG_ERROR( "Failed to parse input parameters: %s\n", strerror(-ret) );
  71. return ret;
  72. }
  73. unregister_game( pid );
  74. return sd_bus_reply_method_return( m, "i", 0 );
  75. }
  76. // Vtable for function dispatch
  77. static const sd_bus_vtable gamemode_vtable[] = {
  78. SD_BUS_VTABLE_START( 0 ),
  79. SD_BUS_METHOD( "RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED ),
  80. SD_BUS_METHOD( "UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED ),
  81. SD_BUS_VTABLE_END
  82. };
  83. // Main loop, will not return until something request a quit
  84. void run_dbus_main_loop( bool system_dbus )
  85. {
  86. // Set up function to handle clean up of resources
  87. atexit( clean_up );
  88. int ret = 0;
  89. // Connec to the desired bus
  90. if( system_dbus )
  91. ret = sd_bus_open_system( &bus );
  92. else
  93. ret = sd_bus_open_user( &bus );
  94. if( ret < 0 )
  95. FATAL_ERROR( "Failed to connect to the bus: %s", strerror(-ret) );
  96. // Create the object to allow connections
  97. ret = sd_bus_add_object_vtable( bus,
  98. &slot,
  99. "/com/feralinteractive/GameMode",
  100. "com.feralinteractive.GameMode",
  101. gamemode_vtable,
  102. NULL );
  103. if( ret < 0 )
  104. FATAL_ERROR( "Failed to install GameMode object: %s", strerror(-ret) );
  105. // Request our name
  106. ret = sd_bus_request_name( bus, "com.feralinteractive.GameMode", 0 );
  107. if( ret < 0 )
  108. FATAL_ERROR( "Failed to acquire service name: %s", strerror(-ret) );
  109. LOG_MSG( "Successfully initialised bus with name [%s]...\n", "com.feralinteractive.GameMode" );
  110. // Now loop, waiting for callbacks
  111. for(;;)
  112. {
  113. ret = sd_bus_process( bus, NULL );
  114. if( ret < 0 )
  115. FATAL_ERROR( "Failure when processing the bus: %s", strerror(-ret) );
  116. // We're done processing
  117. if( ret > 0 )
  118. continue;
  119. // Wait for more
  120. ret = sd_bus_wait( bus, (uint64_t)-1 );
  121. if( ret < 0 && -ret != EINTR )
  122. FATAL_ERROR( "Failure when waiting on bus: %s", strerror(-ret) );
  123. }
  124. }