mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-12-07 16:14:28 +01:00
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:
committed by
Marc Di Luzio
parent
400dcb9c53
commit
68e326de60
@@ -31,118 +31,15 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include "governors-query.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <linux/limits.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define MAX_GOVERNORS 128
|
||||
#define MAX_GOVERNOR_LENGTH PATH_MAX + 1
|
||||
|
||||
// Governers are located at /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
|
||||
static int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
|
||||
{
|
||||
const char *cpu_base_path = "/sys/devices/system/cpu/";
|
||||
DIR *dir = opendir(cpu_base_path);
|
||||
if (!dir) {
|
||||
FATAL_ERRORNO("cpu device path not found");
|
||||
}
|
||||
|
||||
int num_governors = 0;
|
||||
|
||||
// Explore the directory
|
||||
struct dirent *ent;
|
||||
while ((ent = readdir(dir)) && num_governors < MAX_GOVERNORS) {
|
||||
// CPU directories all start with "cpu"
|
||||
if (strncmp(ent->d_name, "cpu", 3) == 0) {
|
||||
// Check if this matches "cpu\d+"
|
||||
const int len = strlen(ent->d_name);
|
||||
if (len > 3 && len < 5 && isdigit(ent->d_name[3])) {
|
||||
// Construct the full path
|
||||
char path[PATH_MAX] = {};
|
||||
snprintf(path,
|
||||
sizeof(path),
|
||||
"%s%s/cpufreq/scaling_governor",
|
||||
cpu_base_path,
|
||||
ent->d_name);
|
||||
|
||||
// Get the real path to the file
|
||||
// Traditionally cpufreq symlinks to a policy directory that can be
|
||||
// shared So let's prevent duplicates
|
||||
char fullpath[PATH_MAX] = {};
|
||||
const char *ptr = realpath(path, fullpath);
|
||||
if (fullpath != ptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only add if unique
|
||||
for (int i = 0; i < num_governors; i++) {
|
||||
if (strncmp(fullpath, governors[i], MAX_GOVERNOR_LENGTH) == 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
|
||||
num_governors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
return num_governors;
|
||||
}
|
||||
|
||||
// Get the current governor state
|
||||
const char *get_gov_state()
|
||||
{
|
||||
// To be returned
|
||||
static char governor[64] = { 0 };
|
||||
|
||||
// State of all the overnors
|
||||
char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
|
||||
int num = fetch_governors(governors);
|
||||
|
||||
// Check the list
|
||||
for (int i = 0; i < num; i++) {
|
||||
const char *gov = governors[i];
|
||||
|
||||
FILE *f = fopen(gov, "r");
|
||||
if (!f) {
|
||||
LOG_ERROR("Failed to open file for read %s\n", gov);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Pull out the file contents
|
||||
fseek(f, 0, SEEK_END);
|
||||
int length = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
char contents[length];
|
||||
|
||||
if (fread(contents, 1, length, f) > 0) {
|
||||
// Files have a newline
|
||||
strtok(contents, "\n");
|
||||
if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
|
||||
// Don't handle the mixed case, this shouldn't ever happen
|
||||
// But it is a clear sign we shouldn't carry on
|
||||
LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
|
||||
return "malformed";
|
||||
}
|
||||
|
||||
strncpy(governor, contents, sizeof(governor));
|
||||
} else {
|
||||
LOG_ERROR("Failed to read contents of %s\n", gov);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
return governor;
|
||||
}
|
||||
|
||||
// Sets all governors to a value
|
||||
/**
|
||||
* Sets all governors to a value
|
||||
*/
|
||||
void set_gov_state(const char *value)
|
||||
{
|
||||
char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
|
||||
@@ -162,20 +59,31 @@ void set_gov_state(const char *value)
|
||||
}
|
||||
}
|
||||
|
||||
// Main entry point
|
||||
/**
|
||||
* Main entry point, dispatch to the appropriate helper
|
||||
*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: cpugovctl [get] [set VALUE]\n");
|
||||
exit(EXIT_FAILURE);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (strncmp(argv[1], "get", 3) == 0) {
|
||||
printf("%s", get_gov_state());
|
||||
} else if (strncmp(argv[1], "set", 3) == 0) {
|
||||
const char *value = argv[2];
|
||||
|
||||
/* Must be root to set the state */
|
||||
if (geteuid() != 0) {
|
||||
fprintf(stderr, "This program must be run as root\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
set_gov_state(value);
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user