mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-06-07 08:07:20 +02:00
Apply clang format to files
Also add brackets for all scopes at the same time
This commit is contained in:
parent
ee1c51d0b0
commit
2bbaab129b
@ -30,9 +30,9 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
*/
|
*/
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#define MAX_GOVERNORS 128
|
#define MAX_GOVERNORS 128
|
||||||
#define MAX_GOVERNOR_LENGTH PATH_MAX + 1
|
#define MAX_GOVERNOR_LENGTH PATH_MAX + 1
|
||||||
@ -42,41 +42,43 @@ static int fetch_governors( char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] )
|
|||||||
{
|
{
|
||||||
const char *cpu_base_path = "/sys/devices/system/cpu/";
|
const char *cpu_base_path = "/sys/devices/system/cpu/";
|
||||||
DIR *dir = opendir(cpu_base_path);
|
DIR *dir = opendir(cpu_base_path);
|
||||||
if( !dir )
|
if (!dir) {
|
||||||
FATAL_ERRORNO("cpu device path not found");
|
FATAL_ERRORNO("cpu device path not found");
|
||||||
|
}
|
||||||
|
|
||||||
int num_governors = 0;
|
int num_governors = 0;
|
||||||
|
|
||||||
// Explore the directory
|
// Explore the directory
|
||||||
struct dirent *ent;
|
struct dirent *ent;
|
||||||
while( ( ent = readdir(dir) ) && num_governors < MAX_GOVERNORS )
|
while ((ent = readdir(dir)) && num_governors < MAX_GOVERNORS) {
|
||||||
{
|
|
||||||
// CPU directories all start with "cpu"
|
// CPU directories all start with "cpu"
|
||||||
if( strncmp( ent->d_name, "cpu", 3 ) == 0 )
|
if (strncmp(ent->d_name, "cpu", 3) == 0) {
|
||||||
{
|
|
||||||
// Check if this matches "cpu\d+"
|
// Check if this matches "cpu\d+"
|
||||||
const int len = strlen(ent->d_name);
|
const int len = strlen(ent->d_name);
|
||||||
if( len > 3 && len < 5
|
if (len > 3 && len < 5 && isdigit(ent->d_name[3])) {
|
||||||
&& isdigit( ent->d_name[3] ) )
|
|
||||||
{
|
|
||||||
// Construct the full path
|
// Construct the full path
|
||||||
char path[PATH_MAX] = {};
|
char path[PATH_MAX] = {};
|
||||||
snprintf( path, sizeof(path), "%s%s/cpufreq/scaling_governor", cpu_base_path, ent->d_name );
|
snprintf(path,
|
||||||
|
sizeof(path),
|
||||||
|
"%s%s/cpufreq/scaling_governor",
|
||||||
|
cpu_base_path,
|
||||||
|
ent->d_name);
|
||||||
|
|
||||||
// Get the real path to the file
|
// Get the real path to the file
|
||||||
// Traditionally cpufreq symlinks to a policy directory that can be shared
|
// Traditionally cpufreq symlinks to a policy directory that can be
|
||||||
// So let's prevent duplicates
|
// shared So let's prevent duplicates
|
||||||
char fullpath[PATH_MAX] = {};
|
char fullpath[PATH_MAX] = {};
|
||||||
const char *ptr = realpath(path, fullpath);
|
const char *ptr = realpath(path, fullpath);
|
||||||
if( fullpath != ptr )
|
if (fullpath != ptr) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Only add if unique
|
// Only add if unique
|
||||||
for( int i = 0; i < num_governors; i++ )
|
for (int i = 0; i < num_governors; i++) {
|
||||||
{
|
if (strncmp(fullpath, governors[i], MAX_GOVERNOR_LENGTH) == 0) {
|
||||||
if( strncmp( fullpath, governors[i], MAX_GOVERNOR_LENGTH ) == 0 )
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
|
strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
|
||||||
num_governors++;
|
num_governors++;
|
||||||
@ -99,13 +101,11 @@ const char* get_gov_state()
|
|||||||
int num = fetch_governors(governors);
|
int num = fetch_governors(governors);
|
||||||
|
|
||||||
// Check the list
|
// Check the list
|
||||||
for( int i = 0; i < num; i++ )
|
for (int i = 0; i < num; i++) {
|
||||||
{
|
|
||||||
const char *gov = governors[i];
|
const char *gov = governors[i];
|
||||||
|
|
||||||
FILE *f = fopen(gov, "r");
|
FILE *f = fopen(gov, "r");
|
||||||
if( !f )
|
if (!f) {
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to open file for read %s\n", gov);
|
LOG_ERROR("Failed to open file for read %s\n", gov);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -117,12 +117,10 @@ const char* get_gov_state()
|
|||||||
|
|
||||||
char contents[length];
|
char contents[length];
|
||||||
|
|
||||||
if( fread(contents, 1, length, f) > 0 )
|
if (fread(contents, 1, length, f) > 0) {
|
||||||
{
|
|
||||||
// Files have a newline
|
// Files have a newline
|
||||||
strtok(contents, "\n");
|
strtok(contents, "\n");
|
||||||
if( strlen(governor) > 0 && strncmp( governor, contents, 64 ) != 0 )
|
if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
|
||||||
{
|
|
||||||
// Don't handle the mixed case, this shouldn't ever happen
|
// Don't handle the mixed case, this shouldn't ever happen
|
||||||
// But it is a clear sign we shouldn't carry on
|
// But it is a clear sign we shouldn't carry on
|
||||||
LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
|
LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
|
||||||
@ -130,9 +128,7 @@ const char* get_gov_state()
|
|||||||
}
|
}
|
||||||
|
|
||||||
strncpy(governor, contents, sizeof(governor));
|
strncpy(governor, contents, sizeof(governor));
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to read contents of %s\n", gov);
|
LOG_ERROR("Failed to read contents of %s\n", gov);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,12 +145,10 @@ void set_gov_state( const char* value )
|
|||||||
int num = fetch_governors(governors);
|
int num = fetch_governors(governors);
|
||||||
|
|
||||||
LOG_MSG("Setting governors to %s\n", value);
|
LOG_MSG("Setting governors to %s\n", value);
|
||||||
for( int i = 0; i < num; i++ )
|
for (int i = 0; i < num; i++) {
|
||||||
{
|
|
||||||
const char *gov = governors[i];
|
const char *gov = governors[i];
|
||||||
FILE *f = fopen(gov, "w");
|
FILE *f = fopen(gov, "w");
|
||||||
if( !f )
|
if (!f) {
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to open file for write %s\n", gov);
|
LOG_ERROR("Failed to open file for write %s\n", gov);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -167,24 +161,17 @@ void set_gov_state( const char* value )
|
|||||||
// Main entry point
|
// Main entry point
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if( argc < 2 )
|
if (argc < 2) {
|
||||||
{
|
|
||||||
fprintf(stderr, "usage: cpugovctl [get] [set VALUE]\n");
|
fprintf(stderr, "usage: cpugovctl [get] [set VALUE]\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if( strncmp( argv[1], "get", 3 ) == 0 )
|
if (strncmp(argv[1], "get", 3) == 0) {
|
||||||
{
|
|
||||||
printf("%s", get_gov_state());
|
printf("%s", get_gov_state());
|
||||||
}
|
} else if (strncmp(argv[1], "set", 3) == 0) {
|
||||||
else if( strncmp( argv[1], "set", 3 ) == 0 )
|
|
||||||
{
|
|
||||||
const char *value = argv[2];
|
const char *value = argv[2];
|
||||||
set_gov_state(value);
|
set_gov_state(value);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,28 +41,31 @@ void daemonize( char* name )
|
|||||||
{
|
{
|
||||||
// Fork once
|
// Fork once
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if( pid < 0 )
|
if (pid < 0) {
|
||||||
FATAL_ERRORNO("Failed to fork");
|
FATAL_ERRORNO("Failed to fork");
|
||||||
|
}
|
||||||
|
|
||||||
if( pid != 0 )
|
if (pid != 0) {
|
||||||
{
|
|
||||||
LOG_MSG("Daemon launched...\n");
|
LOG_MSG("Daemon launched...\n");
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fork a second time
|
// Fork a second time
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if( pid < 0 )
|
if (pid < 0) {
|
||||||
FATAL_ERRORNO("Failed to fork");
|
FATAL_ERRORNO("Failed to fork");
|
||||||
else if( pid > 0 )
|
} else if (pid > 0) {
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
// Continue to set up as a daemon
|
// Continue to set up as a daemon
|
||||||
umask(0);
|
umask(0);
|
||||||
if ( setsid() < 0 )
|
if (setsid() < 0) {
|
||||||
FATAL_ERRORNO("Failed to create process group\n");
|
FATAL_ERRORNO("Failed to create process group\n");
|
||||||
if ( chdir( "/" ) < 0 )
|
}
|
||||||
|
if (chdir("/") < 0) {
|
||||||
FATAL_ERRORNO("Failed to change to root directory\n");
|
FATAL_ERRORNO("Failed to change to root directory\n");
|
||||||
|
}
|
||||||
close(STDIN_FILENO);
|
close(STDIN_FILENO);
|
||||||
close(STDOUT_FILENO);
|
close(STDOUT_FILENO);
|
||||||
close(STDERR_FILENO);
|
close(STDERR_FILENO);
|
||||||
|
@ -29,10 +29,10 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
#include "dbus_messaging.h"
|
#include "dbus_messaging.h"
|
||||||
#include "logging.h"
|
#include "daemonize.h"
|
||||||
#include "gamemode.h"
|
#include "gamemode.h"
|
||||||
#include "governors.h"
|
#include "governors.h"
|
||||||
#include "daemonize.h"
|
#include "logging.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -45,24 +45,23 @@ static sd_bus_slot* slot = NULL;
|
|||||||
// Clean up any resources as needed
|
// Clean up any resources as needed
|
||||||
static void clean_up()
|
static void clean_up()
|
||||||
{
|
{
|
||||||
if( slot )
|
if (slot) {
|
||||||
sd_bus_slot_unref(slot);
|
sd_bus_slot_unref(slot);
|
||||||
|
}
|
||||||
slot = NULL;
|
slot = NULL;
|
||||||
if( bus )
|
if (bus) {
|
||||||
sd_bus_unref(bus);
|
sd_bus_unref(bus);
|
||||||
|
}
|
||||||
bus = NULL;
|
bus = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callback for RegisterGame
|
// Callback for RegisterGame
|
||||||
static int method_register_game( sd_bus_message *m,
|
static int method_register_game(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
|
||||||
void *userdata,
|
|
||||||
sd_bus_error *ret_error )
|
|
||||||
{
|
{
|
||||||
int pid = 0;
|
int pid = 0;
|
||||||
|
|
||||||
int ret = sd_bus_message_read(m, "i", &pid);
|
int ret = sd_bus_message_read(m, "i", &pid);
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
|
LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -73,15 +72,12 @@ static int method_register_game( sd_bus_message *m,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Callback for UnregisterGame
|
// Callback for UnregisterGame
|
||||||
static int method_unregister_game( sd_bus_message *m,
|
static int method_unregister_game(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
|
||||||
void *userdata,
|
|
||||||
sd_bus_error *ret_error )
|
|
||||||
{
|
{
|
||||||
int pid = 0;
|
int pid = 0;
|
||||||
|
|
||||||
int ret = sd_bus_message_read(m, "i", &pid);
|
int ret = sd_bus_message_read(m, "i", &pid);
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
{
|
|
||||||
LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
|
LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -92,12 +88,11 @@ static int method_unregister_game( sd_bus_message *m,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Vtable for function dispatch
|
// Vtable for function dispatch
|
||||||
static const sd_bus_vtable gamemode_vtable[] = {
|
static const sd_bus_vtable gamemode_vtable[] =
|
||||||
SD_BUS_VTABLE_START( 0 ),
|
{ SD_BUS_VTABLE_START(0),
|
||||||
SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
|
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_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
|
||||||
SD_BUS_VTABLE_END
|
SD_BUS_VTABLE_END };
|
||||||
};
|
|
||||||
|
|
||||||
// Main loop, will not return until something request a quit
|
// Main loop, will not return until something request a quit
|
||||||
void run_dbus_main_loop(bool system_dbus)
|
void run_dbus_main_loop(bool system_dbus)
|
||||||
@ -107,13 +102,15 @@ void run_dbus_main_loop( bool system_dbus )
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
// Connec to the desired bus
|
// Connec to the desired bus
|
||||||
if( system_dbus )
|
if (system_dbus) {
|
||||||
ret = sd_bus_open_system(&bus);
|
ret = sd_bus_open_system(&bus);
|
||||||
else
|
} else {
|
||||||
ret = sd_bus_open_user(&bus);
|
ret = sd_bus_open_user(&bus);
|
||||||
|
}
|
||||||
|
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
FATAL_ERROR("Failed to connect to the bus: %s", strerror(-ret));
|
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,
|
ret = sd_bus_add_object_vtable(bus,
|
||||||
@ -123,32 +120,34 @@ void run_dbus_main_loop( bool system_dbus )
|
|||||||
gamemode_vtable,
|
gamemode_vtable,
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
FATAL_ERROR("Failed to install GameMode object: %s", strerror(-ret));
|
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);
|
ret = sd_bus_request_name(bus, "com.feralinteractive.GameMode", 0);
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
FATAL_ERROR("Failed to acquire service name: %s", strerror(-ret));
|
FATAL_ERROR("Failed to acquire service name: %s", strerror(-ret));
|
||||||
|
}
|
||||||
|
|
||||||
LOG_MSG("Successfully initialised bus with name [%s]...\n", "com.feralinteractive.GameMode");
|
LOG_MSG("Successfully initialised bus with name [%s]...\n", "com.feralinteractive.GameMode");
|
||||||
|
|
||||||
// Now loop, waiting for callbacks
|
// Now loop, waiting for callbacks
|
||||||
for(;;)
|
for (;;) {
|
||||||
{
|
|
||||||
ret = sd_bus_process(bus, NULL);
|
ret = sd_bus_process(bus, NULL);
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
FATAL_ERROR("Failure when processing the bus: %s", strerror(-ret));
|
FATAL_ERROR("Failure when processing the bus: %s", strerror(-ret));
|
||||||
|
}
|
||||||
|
|
||||||
// We're done processing
|
// We're done processing
|
||||||
if( ret > 0 )
|
if (ret > 0) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for more
|
// Wait for more
|
||||||
ret = sd_bus_wait(bus, (uint64_t)-1);
|
ret = sd_bus_wait(bus, (uint64_t)-1);
|
||||||
if( ret < 0 && -ret != EINTR )
|
if (ret < 0 && -ret != EINTR) {
|
||||||
FATAL_ERROR("Failure when waiting on bus: %s", strerror(-ret));
|
FATAL_ERROR("Failure when waiting on bus: %s", strerror(-ret));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
#include "gamemode.h"
|
#include "gamemode.h"
|
||||||
#include "logging.h"
|
|
||||||
#include "governors.h"
|
#include "governors.h"
|
||||||
|
#include "logging.h"
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -68,30 +68,30 @@ static void leave_game_mode()
|
|||||||
static void alarm_handler(int sig)
|
static void alarm_handler(int sig)
|
||||||
{
|
{
|
||||||
// Quick return if no games, and don't register another callback
|
// Quick return if no games, and don't register another callback
|
||||||
if( num_games == 0 )
|
if (num_games == 0) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if games are alive at all
|
// Check if games are alive at all
|
||||||
for( int i = 0; i < num_games; )
|
for (int i = 0; i < num_games;) {
|
||||||
{
|
|
||||||
int game = game_pids[i];
|
int game = game_pids[i];
|
||||||
|
|
||||||
if( kill( game, 0 ) != 0 )
|
if (kill(game, 0) != 0) {
|
||||||
{
|
|
||||||
LOG_MSG("Removing expired game [%i]...\n", game);
|
LOG_MSG("Removing expired game [%i]...\n", game);
|
||||||
memmove(&game_pids[i], &game_pids[i + 1], MAX_GAMES - (i + 1));
|
memmove(&game_pids[i], &game_pids[i + 1], MAX_GAMES - (i + 1));
|
||||||
num_games--;
|
num_games--;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Either trigger another alarm, or reset the governors
|
// Either trigger another alarm, or reset the governors
|
||||||
if( num_games )
|
if (num_games) {
|
||||||
start_alarm_timer();
|
start_alarm_timer();
|
||||||
else
|
} else {
|
||||||
leave_game_mode();
|
leave_game_mode();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Call to trigger starting the alarm timer for pid checks
|
// Call to trigger starting the alarm timer for pid checks
|
||||||
static void start_alarm_timer()
|
static void start_alarm_timer()
|
||||||
@ -112,27 +112,25 @@ void init_game_mode()
|
|||||||
// Called on exit to clean up the governors if appropriate
|
// Called on exit to clean up the governors if appropriate
|
||||||
void term_game_mode()
|
void term_game_mode()
|
||||||
{
|
{
|
||||||
if( num_games )
|
if (num_games) {
|
||||||
leave_game_mode();
|
leave_game_mode();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Register a game pid with the game mode
|
// Register a game pid with the game mode
|
||||||
// Will trigger enter game mode if appropriate
|
// Will trigger enter game mode if appropriate
|
||||||
void register_game(int pid)
|
void register_game(int pid)
|
||||||
{
|
{
|
||||||
// Check for duplicates
|
// Check for duplicates
|
||||||
for( int i = 0; i < num_games; i++ )
|
for (int i = 0; i < num_games; i++) {
|
||||||
{
|
if (game_pids[i] == pid) {
|
||||||
if( game_pids[i] == pid )
|
|
||||||
{
|
|
||||||
LOG_ERROR("Addition requested for already known process [%i]\n", pid);
|
LOG_ERROR("Addition requested for already known process [%i]\n", pid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check we've not already hit max
|
// Check we've not already hit max
|
||||||
if( num_games == MAX_GAMES )
|
if (num_games == MAX_GAMES) {
|
||||||
{
|
|
||||||
LOG_ERROR("Max games (%i) reached, could not add [%i]\n", MAX_GAMES, pid);
|
LOG_ERROR("Max games (%i) reached, could not add [%i]\n", MAX_GAMES, pid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -142,9 +140,10 @@ void register_game( int pid )
|
|||||||
game_pids[num_games] = pid;
|
game_pids[num_games] = pid;
|
||||||
num_games++;
|
num_games++;
|
||||||
|
|
||||||
if( num_games == 1 )
|
if (num_games == 1) {
|
||||||
enter_game_mode();
|
enter_game_mode();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remove a game from game mode
|
// Remove a game from game mode
|
||||||
// Will exit game mode if appropriate
|
// Will exit game mode if appropriate
|
||||||
@ -153,10 +152,8 @@ void unregister_game( int pid )
|
|||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
// Check list even contains this entry
|
// Check list even contains this entry
|
||||||
for( int i = 0; i < num_games; i++ )
|
for (int i = 0; i < num_games; i++) {
|
||||||
{
|
if (game_pids[i] == pid) {
|
||||||
if( game_pids[i] == pid )
|
|
||||||
{
|
|
||||||
LOG_MSG("Removing game: %i\n", pid);
|
LOG_MSG("Removing game: %i\n", pid);
|
||||||
memmove(&game_pids[i], &game_pids[i + 1], MAX_GAMES - (i + 1));
|
memmove(&game_pids[i], &game_pids[i + 1], MAX_GAMES - (i + 1));
|
||||||
num_games--;
|
num_games--;
|
||||||
@ -164,14 +161,13 @@ void unregister_game( int pid )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !found )
|
if (!found) {
|
||||||
{
|
|
||||||
LOG_ERROR("Removal requested for unknown process [%i]\n", pid);
|
LOG_ERROR("Removal requested for unknown process [%i]\n", pid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leave game mode if needed
|
// Leave game mode if needed
|
||||||
if( num_games == 0 )
|
if (num_games == 0) {
|
||||||
leave_game_mode();
|
leave_game_mode();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -31,9 +31,9 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include "governors.h"
|
#include "governors.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
|
#include <linux/limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <linux/limits.h>
|
|
||||||
|
|
||||||
static char initial[32];
|
static char initial[32];
|
||||||
|
|
||||||
@ -43,11 +43,13 @@ void update_initial_gov_state()
|
|||||||
static char *command = "cpugovctl get";
|
static char *command = "cpugovctl get";
|
||||||
|
|
||||||
FILE *f = popen(command, "r");
|
FILE *f = popen(command, "r");
|
||||||
if( !f )
|
if (!f) {
|
||||||
FATAL_ERRORNO("Failed to launch \"%s\" script", command);
|
FATAL_ERRORNO("Failed to launch \"%s\" script", command);
|
||||||
|
}
|
||||||
|
|
||||||
if( !fgets( initial, sizeof(initial)-1, f ) )
|
if (!fgets(initial, sizeof(initial) - 1, f)) {
|
||||||
FATAL_ERROR("Failed to get output from \"%s\"", command);
|
FATAL_ERROR("Failed to get output from \"%s\"", command);
|
||||||
|
}
|
||||||
|
|
||||||
pclose(f);
|
pclose(f);
|
||||||
|
|
||||||
@ -64,8 +66,9 @@ void set_governors( const char* value )
|
|||||||
snprintf(command, sizeof(command), "cpugovctl set %s", newval);
|
snprintf(command, sizeof(command), "cpugovctl set %s", newval);
|
||||||
|
|
||||||
FILE *f = popen(command, "r");
|
FILE *f = popen(command, "r");
|
||||||
if( !f )
|
if (!f) {
|
||||||
FATAL_ERRORNO("Failed to launch %s script", command);
|
FATAL_ERRORNO("Failed to launch %s script", command);
|
||||||
|
}
|
||||||
|
|
||||||
pclose(f);
|
pclose(f);
|
||||||
}
|
}
|
||||||
@ -75,4 +78,3 @@ const char* get_initial_governor()
|
|||||||
{
|
{
|
||||||
return initial;
|
return initial;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,25 +32,47 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#define _LOGGING_GAMEMODE_H_
|
#define _LOGGING_GAMEMODE_H_
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
// Logging helpers
|
// Logging helpers
|
||||||
#define PLOG_MSG(msg, ...) printf(msg, ##__VA_ARGS__)
|
#define PLOG_MSG(msg, ...) printf(msg, ##__VA_ARGS__)
|
||||||
#define SYSLOG_MSG(msg, ...) syslog(LOG_INFO, msg, ##__VA_ARGS__)
|
#define SYSLOG_MSG(msg, ...) syslog(LOG_INFO, msg, ##__VA_ARGS__)
|
||||||
#define LOG_MSG( msg, ... ) do { if( get_use_syslog() ) SYSLOG_MSG( msg, ##__VA_ARGS__ ); else PLOG_MSG( msg, ##__VA_ARGS__ ); } while(0)
|
#define LOG_MSG(msg, ...) \
|
||||||
|
do { \
|
||||||
|
if (get_use_syslog()) { \
|
||||||
|
SYSLOG_MSG(msg, ##__VA_ARGS__); \
|
||||||
|
} else { \
|
||||||
|
PLOG_MSG(msg, ##__VA_ARGS__); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#define PLOG_ERROR(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__)
|
#define PLOG_ERROR(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__)
|
||||||
#define SYSLOG_ERROR(msg, ...) syslog(LOG_ERR, msg, ##__VA_ARGS__)
|
#define SYSLOG_ERROR(msg, ...) syslog(LOG_ERR, msg, ##__VA_ARGS__)
|
||||||
#define LOG_ERROR( msg, ... ) do { if( get_use_syslog() ) SYSLOG_MSG( msg, ##__VA_ARGS__ ); else PLOG_MSG( msg, ##__VA_ARGS__ ); } while(0)
|
#define LOG_ERROR(msg, ...) \
|
||||||
|
do { \
|
||||||
|
if (get_use_syslog()) { \
|
||||||
|
SYSLOG_MSG(msg, ##__VA_ARGS__); \
|
||||||
|
} else { \
|
||||||
|
PLOG_MSG(msg, ##__VA_ARGS__); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
// Fatal errors trigger an exit
|
// Fatal errors trigger an exit
|
||||||
#define FATAL_ERRORNO( msg, ... ) do { LOG_ERROR( msg " (%s)\n", ##__VA_ARGS__, strerror(errno) ); exit(EXIT_FAILURE); } while(0)
|
#define FATAL_ERRORNO(msg, ...) \
|
||||||
#define FATAL_ERROR( msg, ... ) do { LOG_ERROR( msg, ##__VA_ARGS__ ); exit(EXIT_FAILURE); } while(0)
|
do { \
|
||||||
|
LOG_ERROR(msg " (%s)\n", ##__VA_ARGS__, strerror(errno)); \
|
||||||
|
exit(EXIT_FAILURE); \
|
||||||
|
} while (0)
|
||||||
|
#define FATAL_ERROR(msg, ...) \
|
||||||
|
do { \
|
||||||
|
LOG_ERROR(msg, ##__VA_ARGS__); \
|
||||||
|
exit(EXIT_FAILURE); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
// Control if we want to use the system logger
|
// Control if we want to use the system logger
|
||||||
void set_use_syslog(const char *name);
|
void set_use_syslog(const char *name);
|
||||||
|
@ -29,14 +29,14 @@ 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
|
||||||
#include "gamemode.h"
|
|
||||||
#include "dbus_messaging.h"
|
|
||||||
#include "logging.h"
|
|
||||||
#include "daemonize.h"
|
#include "daemonize.h"
|
||||||
|
#include "dbus_messaging.h"
|
||||||
|
#include "gamemode.h"
|
||||||
|
#include "logging.h"
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
static void sigint_handler(int signo)
|
static void sigint_handler(int signo)
|
||||||
{
|
{
|
||||||
@ -56,10 +56,8 @@ int main( int argc, char *argv[] )
|
|||||||
bool system_dbus = false;
|
bool system_dbus = false;
|
||||||
bool use_syslog = false;
|
bool use_syslog = false;
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
while( ( opt = getopt( argc, argv, "dsl") ) != -1 )
|
while ((opt = getopt(argc, argv, "dsl")) != -1) {
|
||||||
{
|
switch (opt) {
|
||||||
switch( opt )
|
|
||||||
{
|
|
||||||
case 'd':
|
case 'd':
|
||||||
daemon = true;
|
daemon = true;
|
||||||
break;
|
break;
|
||||||
@ -77,19 +75,22 @@ int main( int argc, char *argv[] )
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use syslog if requested
|
// Use syslog if requested
|
||||||
if( use_syslog )
|
if (use_syslog) {
|
||||||
set_use_syslog(argv[0]);
|
set_use_syslog(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
// Daemonize ourselves first if asked
|
// Daemonize ourselves first if asked
|
||||||
if ( daemon )
|
if (daemon) {
|
||||||
daemonize(argv[0]);
|
daemonize(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
// Set up the game mode
|
// Set up the game mode
|
||||||
init_game_mode();
|
init_game_mode();
|
||||||
|
|
||||||
// Set up the SIGINT handler
|
// Set up the SIGINT handler
|
||||||
if( signal( SIGINT, sigint_handler ) == SIG_ERR )
|
if (signal(SIGINT, sigint_handler) == SIG_ERR) {
|
||||||
FATAL_ERRORNO("Could not catch SIGINT");
|
FATAL_ERRORNO("Could not catch SIGINT");
|
||||||
|
}
|
||||||
|
|
||||||
// Run the main dbus message loop
|
// Run the main dbus message loop
|
||||||
run_dbus_main_loop(system_dbus);
|
run_dbus_main_loop(system_dbus);
|
||||||
|
@ -30,19 +30,21 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
*/
|
*/
|
||||||
#include "gamemode_client.h"
|
#include "gamemode_client.h"
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(){
|
int main()
|
||||||
|
{
|
||||||
// Request we start game mode
|
// Request we start game mode
|
||||||
if( gamemode_request_start() != 0 )
|
if (gamemode_request_start() != 0) {
|
||||||
printf("Failed to request gamemode start: %s...\n", gamemode_error_string());
|
printf("Failed to request gamemode start: %s...\n", gamemode_error_string());
|
||||||
|
}
|
||||||
|
|
||||||
// Simulate running a game
|
// Simulate running a game
|
||||||
sleep(10);
|
sleep(10);
|
||||||
|
|
||||||
// Request we end game mode (optional)
|
// Request we end game mode (optional)
|
||||||
if( gamemode_request_end() != 0 )
|
if (gamemode_request_end() != 0) {
|
||||||
printf("Failed to request gamemode end: %s...\n", gamemode_error_string());
|
printf("Failed to request gamemode end: %s...\n", gamemode_error_string());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -28,13 +28,13 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|||||||
POSSIBILITY OF SUCH DAMAGE.
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <systemd/sd-bus.h>
|
#include <systemd/sd-bus.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
// Storage for error strings
|
// Storage for error strings
|
||||||
static char error_string[512] = {};
|
static char error_string[512] = {};
|
||||||
@ -49,10 +49,12 @@ static int gamemode_request( const char* function )
|
|||||||
|
|
||||||
// Open the user bus
|
// Open the user bus
|
||||||
int ret = sd_bus_open_user(&bus);
|
int ret = sd_bus_open_user(&bus);
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
snprintf( error_string, sizeof(error_string), "Could not connect to bus: %s", strerror(-ret) );
|
snprintf(error_string,
|
||||||
else
|
sizeof(error_string),
|
||||||
{
|
"Could not connect to bus: %s",
|
||||||
|
strerror(-ret));
|
||||||
|
} else {
|
||||||
// Attempt to send the requested function
|
// Attempt to send the requested function
|
||||||
ret = sd_bus_call_method(bus,
|
ret = sd_bus_call_method(bus,
|
||||||
"com.feralinteractive.GameMode",
|
"com.feralinteractive.GameMode",
|
||||||
@ -63,14 +65,20 @@ static int gamemode_request( const char* function )
|
|||||||
&msg,
|
&msg,
|
||||||
"i",
|
"i",
|
||||||
getpid());
|
getpid());
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
snprintf( error_string, sizeof(error_string), "Could not call method on bus: %s", strerror(-ret) );
|
snprintf(error_string,
|
||||||
else
|
sizeof(error_string),
|
||||||
{
|
"Could not call method on bus: %s",
|
||||||
|
strerror(-ret));
|
||||||
|
} else {
|
||||||
// Read the reply
|
// Read the reply
|
||||||
ret = sd_bus_message_read(msg, "i", &result);
|
ret = sd_bus_message_read(msg, "i", &result);
|
||||||
if( ret < 0 )
|
if (ret < 0) {
|
||||||
snprintf( error_string, sizeof(error_string), "Failure to parse response: %s", strerror(-ret) );
|
snprintf(error_string,
|
||||||
|
sizeof(error_string),
|
||||||
|
"Failure to parse response: %s",
|
||||||
|
strerror(-ret));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,4 +102,3 @@ extern int real_gamemode_request_end()
|
|||||||
{
|
{
|
||||||
return gamemode_request("UnregisterGame");
|
return gamemode_request("UnregisterGame");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,33 +58,41 @@ _gamemode_error_string _REAL_gamemode_error_string = NULL;
|
|||||||
|
|
||||||
// Loads libgamemode and needed functions
|
// Loads libgamemode and needed functions
|
||||||
// returns 0 on success and -1 on failure
|
// returns 0 on success and -1 on failure
|
||||||
__attribute__((always_inline))
|
__attribute__((always_inline)) inline int _load_libgamemode()
|
||||||
inline int _load_libgamemode()
|
|
||||||
{
|
{
|
||||||
// We start at 1, 0 is a success and -1 is a fail
|
// We start at 1, 0 is a success and -1 is a fail
|
||||||
if ( _libgamemode_loaded != 1 )
|
if (_libgamemode_loaded != 1) {
|
||||||
return _libgamemode_loaded;
|
return _libgamemode_loaded;
|
||||||
|
}
|
||||||
|
|
||||||
void *libgamemode = NULL;
|
void *libgamemode = NULL;
|
||||||
|
|
||||||
// Try and load libgamemode
|
// Try and load libgamemode
|
||||||
libgamemode = dlopen("libgamemode.so", RTLD_NOW);
|
libgamemode = dlopen("libgamemode.so", RTLD_NOW);
|
||||||
if( !libgamemode )
|
if (!libgamemode) {
|
||||||
snprintf( _client_error_string, sizeof(_client_error_string), "dylopen failed - %s", dlerror() );
|
snprintf(_client_error_string,
|
||||||
else
|
sizeof(_client_error_string),
|
||||||
{
|
"dylopen failed - %s",
|
||||||
_REAL_gamemode_request_start = (_gamemode_request_start)dlsym( libgamemode, "real_gamemode_request_start" );
|
dlerror());
|
||||||
_REAL_gamemode_request_end = (_gamemode_request_end) dlsym( libgamemode, "real_gamemode_request_end" );
|
} else {
|
||||||
_REAL_gamemode_error_string = (_gamemode_error_string) dlsym( libgamemode, "real_gamemode_error_string" );
|
_REAL_gamemode_request_start =
|
||||||
|
(_gamemode_request_start)dlsym(libgamemode, "real_gamemode_request_start");
|
||||||
|
_REAL_gamemode_request_end =
|
||||||
|
(_gamemode_request_end)dlsym(libgamemode, "real_gamemode_request_end");
|
||||||
|
_REAL_gamemode_error_string =
|
||||||
|
(_gamemode_error_string)dlsym(libgamemode, "real_gamemode_error_string");
|
||||||
|
|
||||||
// Verify we have the functions we want
|
// Verify we have the functions we want
|
||||||
if( _REAL_gamemode_request_start && _REAL_gamemode_request_end && _REAL_gamemode_error_string )
|
if (_REAL_gamemode_request_start && _REAL_gamemode_request_end &&
|
||||||
{
|
_REAL_gamemode_error_string) {
|
||||||
_libgamemode_loaded = 0;
|
_libgamemode_loaded = 0;
|
||||||
return 0;
|
return 0;
|
||||||
|
} else {
|
||||||
|
snprintf(_client_error_string,
|
||||||
|
sizeof(_client_error_string),
|
||||||
|
"dlsym failed - %s",
|
||||||
|
dlerror());
|
||||||
}
|
}
|
||||||
else
|
|
||||||
snprintf( _client_error_string, sizeof(_client_error_string), "dlsym failed - %s", dlerror() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_libgamemode_loaded = -1;
|
_libgamemode_loaded = -1;
|
||||||
@ -92,12 +100,12 @@ inline int _load_libgamemode()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Redirect to the real libgamemode
|
// Redirect to the real libgamemode
|
||||||
__attribute__((always_inline))
|
__attribute__((always_inline)) inline const char *gamemode_error_string()
|
||||||
inline const char* gamemode_error_string()
|
|
||||||
{
|
{
|
||||||
// If we fail to load the system gamemode, return our error string
|
// If we fail to load the system gamemode, return our error string
|
||||||
if( _load_libgamemode() < 0 )
|
if (_load_libgamemode() < 0) {
|
||||||
return _client_error_string;
|
return _client_error_string;
|
||||||
|
}
|
||||||
|
|
||||||
return _REAL_gamemode_error_string();
|
return _REAL_gamemode_error_string();
|
||||||
}
|
}
|
||||||
@ -108,22 +116,19 @@ inline const char* gamemode_error_string()
|
|||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
__attribute__((constructor))
|
__attribute__((constructor))
|
||||||
#else
|
#else
|
||||||
__attribute__((always_inline))
|
__attribute__((always_inline)) inline
|
||||||
inline
|
|
||||||
#endif
|
#endif
|
||||||
int gamemode_request_start()
|
int gamemode_request_start()
|
||||||
{
|
{
|
||||||
// Need to load gamemode
|
// Need to load gamemode
|
||||||
if( _load_libgamemode() < 0 )
|
if (_load_libgamemode() < 0) {
|
||||||
{
|
|
||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( _REAL_gamemode_request_start() < 0 )
|
if (_REAL_gamemode_request_start() < 0) {
|
||||||
{
|
|
||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
||||||
#endif
|
#endif
|
||||||
@ -137,22 +142,19 @@ int gamemode_request_start()
|
|||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
__attribute__((destructor))
|
__attribute__((destructor))
|
||||||
#else
|
#else
|
||||||
__attribute__((always_inline))
|
__attribute__((always_inline)) inline
|
||||||
inline
|
|
||||||
#endif
|
#endif
|
||||||
int gamemode_request_end()
|
int gamemode_request_end()
|
||||||
{
|
{
|
||||||
// Need to load gamemode
|
// Need to load gamemode
|
||||||
if( _load_libgamemode() < 0 )
|
if (_load_libgamemode() < 0) {
|
||||||
{
|
|
||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( _REAL_gamemode_request_end() < 0 )
|
if (_REAL_gamemode_request_end() < 0) {
|
||||||
{
|
|
||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user