Transform into a full D-BUS service with Polkit support

Primarily we convert the service into a thread safe one that isn't reliant
on signaling for control flow, eliminating data race conditions. We also
enable interleaving by separating game mode pivoting from explicit client
registration.

The static pid list is now converted into a dynamic list that is OOM safe
to store all registered clients (with a reasonable upper limit of 256 clients)
to better handle cases where LD_PRELOAD is used for a large process group.
Additionally we begin storing some metadata on the connected clients such
as their executable path, which will enable us to perform some basic
whitelisting in future.

The cpugovctl binary is now moved into the libexecdir as an explicit helper
of the D-BUS service, using the shared library to merge some code back into
the daemon. This saves having to execute a process to query the state of the
governors, as we don't need a privileged client to do this.

In order to sanely set the governors, we require that the binary is running
as euid 0, and execute this using `pkexec`. A PolKit policy definition is
provided which allows active/logged in users to execute this helper through
a path whitelist. As such we can convert the daemon into user-mode only, with
the privileged helper being dispatched exclusively via polkit. This removes
the need for a setuid helper or having a system mode daemon.

Lastly we clean up the codebase a bit to be consistent with modern C code
conventions, using pragmas where available. The library component still uses
the older ifdef approach to support older compilers, but the daemon portion
uses the directive to simplify intent and speed up compilation. Additionally
we move all comments to C style comments for consistency, instead of mixing
in C++ style single line comments, in order to establish a formal coding
style.

The net result is a more robust service which can be D-BUS activated when
clients need it, that can perform scaling automatically without harassing
the user with authentication popups.

Signed-off-by: Ikey Doherty <ikey@solus-project.com>
This commit is contained in:
Ikey Doherty
2018-01-15 20:55:32 +00:00
committed by Marc Di Luzio
parent 400dcb9c53
commit 68e326de60
24 changed files with 860 additions and 379 deletions

View File

@@ -29,7 +29,23 @@ POSSIBILITY OF SUCH DAMAGE.
*/
// Simple daemon to allow user space programs to control the CPU governors
/**
* Simple daemon to allow user space programs to control the CPU governors
*
* The main process is responsible for bootstrapping the D-BUS daemon, caching
* the initial governor settings, and then responding to requests over D-BUS.
*
* Clients register their pid(s) with the service, which are routinely checked
* to see if they've expired. Once we reach our first actively registered client
* we put the system into "game mode", i.e. move the CPU governor into a performance
* mode.
*
* Upon exit, or when all clients have stopped running, we put the system back
* into the default governor policy, which is invariably powersave or similar
* on laptops. This ensures that the system is obtaining the maximum performance
* whilst gaming, and allowed to sanely return to idle once the workload is
* complete.
*/
#define _GNU_SOURCE
@@ -46,59 +62,62 @@ static void sigint_handler(int signo)
{
LOG_MSG("Quitting by request...\n");
// Terminate the game mode
term_game_mode();
/* Clean up nicely */
game_mode_context_destroy(game_mode_context_instance());
exit(EXIT_SUCCESS);
}
// Main entry point
/**
* Main bootstrap entry into gamemoded
*/
int main(int argc, char *argv[])
{
// Gather command line options
GameModeContext *context = NULL;
/* Gather command line options */
bool daemon = false;
bool system_dbus = false;
bool use_syslog = false;
int opt = 0;
while ((opt = getopt(argc, argv, "dsl")) != -1) {
while ((opt = getopt(argc, argv, "dl")) != -1) {
switch (opt) {
case 'd':
daemon = true;
break;
case 's':
system_dbus = true;
break;
case 'l':
use_syslog = true;
break;
default:
fprintf(stderr, "Usage: %s [-d] [-s] [-l]\n", argv[0]);
fprintf(stderr, "Usage: %s [-d] [-l]\n", argv[0]);
exit(EXIT_FAILURE);
break;
}
}
// Use syslog if requested
/* If syslog is requested, set it up with our process name */
if (use_syslog) {
set_use_syslog(argv[0]);
}
// Daemonize ourselves first if asked
/* Daemonize ourselves first if asked */
if (daemon) {
daemonize(argv[0]);
}
// Set up the game mode
init_game_mode();
/* Set up the game mode context */
context = game_mode_context_instance();
game_mode_context_init(context);
// Set up the SIGINT handler
/* Handle quits cleanly */
if (signal(SIGINT, sigint_handler) == SIG_ERR) {
FATAL_ERRORNO("Could not catch SIGINT");
}
// Run the main dbus message loop
run_dbus_main_loop(system_dbus);
/* Run the main dbus message loop */
game_mode_context_loop(context);
// Log we're finished
game_mode_context_destroy(context);
/* Log we're finished */
LOG_MSG("Quitting naturally...\n");
}