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

@@ -28,6 +28,7 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "dbus_messaging.h"
#include "daemonize.h"
#include "gamemode.h"
@@ -38,11 +39,13 @@ POSSIBILITY OF SUCH DAMAGE.
#include <systemd/sd-bus.h>
// sd-bus tracker values
/* systemd dbus components */
static sd_bus *bus = NULL;
static sd_bus_slot *slot = NULL;
// Clean up any resources as needed
/**
* Clean up our private dbus state
*/
static void clean_up()
{
if (slot) {
@@ -55,10 +58,13 @@ static void clean_up()
bus = NULL;
}
// Callback for RegisterGame
/**
* Handles the RegisterGame D-BUS Method
*/
static int method_register_game(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
int pid = 0;
GameModeContext *context = userdata;
int ret = sd_bus_message_read(m, "i", &pid);
if (ret < 0) {
@@ -66,15 +72,18 @@ static int method_register_game(sd_bus_message *m, void *userdata, sd_bus_error
return ret;
}
register_game(pid);
game_mode_context_register(context, (pid_t)pid);
return sd_bus_reply_method_return(m, "i", 0);
}
// Callback for UnregisterGame
/**
* Handles the UnregisterGame D-BUS Method
*/
static int method_unregister_game(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
int pid = 0;
GameModeContext *context = userdata;
int ret = sd_bus_message_read(m, "i", &pid);
if (ret < 0) {
@@ -82,49 +91,49 @@ static int method_unregister_game(sd_bus_message *m, void *userdata, sd_bus_erro
return ret;
}
unregister_game(pid);
game_mode_context_unregister(context, (pid_t)pid);
return sd_bus_reply_method_return(m, "i", 0);
}
// Vtable for function dispatch
/**
* D-BUS vtable to dispatch virtual methods
*/
static const sd_bus_vtable gamemode_vtable[] =
{ SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END };
// Main loop, will not return until something request a quit
void run_dbus_main_loop(bool system_dbus)
/**
* Main process loop for the daemon. Run until quitting has been requested.
*/
void game_mode_context_loop(GameModeContext *context)
{
// Set up function to handle clean up of resources
/* Set up function to handle clean up of resources */
atexit(clean_up);
int ret = 0;
// Connec to the desired bus
if (system_dbus) {
ret = sd_bus_open_system(&bus);
} else {
ret = sd_bus_open_user(&bus);
}
/* Connect to the session bus */
ret = sd_bus_open_user(&bus);
if (ret < 0) {
FATAL_ERROR("Failed to connect to the bus: %s", strerror(-ret));
}
// Create the object to allow connections
/* Create the object to allow connections */
ret = sd_bus_add_object_vtable(bus,
&slot,
"/com/feralinteractive/GameMode",
"com.feralinteractive.GameMode",
gamemode_vtable,
NULL);
context);
if (ret < 0) {
FATAL_ERROR("Failed to install GameMode object: %s", strerror(-ret));
}
// Request our name
/* Request our name */
ret = sd_bus_request_name(bus, "com.feralinteractive.GameMode", 0);
if (ret < 0) {
FATAL_ERROR("Failed to acquire service name: %s", strerror(-ret));
@@ -132,19 +141,19 @@ void run_dbus_main_loop(bool system_dbus)
LOG_MSG("Successfully initialised bus with name [%s]...\n", "com.feralinteractive.GameMode");
// Now loop, waiting for callbacks
/* Now loop, waiting for callbacks */
for (;;) {
ret = sd_bus_process(bus, NULL);
if (ret < 0) {
FATAL_ERROR("Failure when processing the bus: %s", strerror(-ret));
}
// We're done processing
/* We're done processing */
if (ret > 0) {
continue;
}
// Wait for more
/* Wait for more */
ret = sd_bus_wait(bus, (uint64_t)-1);
if (ret < 0 && -ret != EINTR) {
FATAL_ERROR("Failure when waiting on bus: %s", strerror(-ret));