mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-06-06 07:37:21 +02:00
Minor C cleanup (#27)
* Minor C cleanup - some symbols can be made static: 1. set_gov_state 2. everything in gamemode_client.h - daemonize() can also take a const char*, since the name is only passed to printf() or syslog() - prevent shadowing of variables - use explicit (void) as parameter-list more consistently - use some more const. Move cast to more appropriate place and document that execv() behaves as if args where of type const *char and we trust on that. - example: Just use main(void), which is also an acceptable ISO-C decl - example: Use stderr for errors * Fix -Wold-style-declaration issue
This commit is contained in:
parent
6b71edf740
commit
752d877196
@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
/**
|
/**
|
||||||
* Sets all governors to a value
|
* Sets all governors to a value
|
||||||
*/
|
*/
|
||||||
void set_gov_state(const char *value)
|
static void set_gov_state(const char *value)
|
||||||
{
|
{
|
||||||
char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
|
char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
|
||||||
int num = fetch_governors(governors);
|
int num = fetch_governors(governors);
|
||||||
|
@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
/**
|
/**
|
||||||
* Helper to perform standard UNIX daemonization
|
* Helper to perform standard UNIX daemonization
|
||||||
*/
|
*/
|
||||||
void daemonize(char *name)
|
void daemonize(const char *name)
|
||||||
{
|
{
|
||||||
/* Initial fork */
|
/* Initial fork */
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
@ -35,4 +35,4 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
* Attempt daemonization of the process.
|
* Attempt daemonization of the process.
|
||||||
* If this fails, the process will exit
|
* If this fails, the process will exit
|
||||||
*/
|
*/
|
||||||
void daemonize(char *name);
|
void daemonize(const char *name);
|
||||||
|
@ -77,7 +77,7 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Only add this governor if it is unique */
|
/* Only add this governor if it is unique */
|
||||||
for (int i = 0; i < num_governors; i++) {
|
for (int j = 0; j < num_governors; j++) {
|
||||||
if (strncmp(fullpath, governors[i], MAX_GOVERNOR_LENGTH) == 0) {
|
if (strncmp(fullpath, governors[i], MAX_GOVERNOR_LENGTH) == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -95,7 +95,7 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
|
|||||||
/**
|
/**
|
||||||
* Return the current governor state
|
* Return the current governor state
|
||||||
*/
|
*/
|
||||||
const char *get_gov_state()
|
const char *get_gov_state(void)
|
||||||
{
|
{
|
||||||
/* Cached primary governor state */
|
/* Cached primary governor state */
|
||||||
static char governor[64] = { 0 };
|
static char governor[64] = { 0 };
|
||||||
|
@ -46,7 +46,7 @@ static const char *initial = NULL;
|
|||||||
/**
|
/**
|
||||||
* Cache the governor state as seen at startup
|
* Cache the governor state as seen at startup
|
||||||
*/
|
*/
|
||||||
void update_initial_gov_state()
|
void update_initial_gov_state(void)
|
||||||
{
|
{
|
||||||
initial = get_gov_state();
|
initial = get_gov_state();
|
||||||
}
|
}
|
||||||
@ -61,9 +61,9 @@ bool set_governors(const char *value)
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
int r = -1;
|
int r = -1;
|
||||||
|
|
||||||
const char *govern = value ? value : initial;
|
const char *const govern = value ? value : initial;
|
||||||
char *exec_args[] = {
|
const char *const exec_args[] = {
|
||||||
"/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", (char *)govern, NULL,
|
"/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", govern, NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
LOG_MSG("Requesting update of governor policy to %s\n", govern);
|
LOG_MSG("Requesting update of governor policy to %s\n", govern);
|
||||||
@ -73,7 +73,13 @@ bool set_governors(const char *value)
|
|||||||
return false;
|
return false;
|
||||||
} else if (p == 0) {
|
} else if (p == 0) {
|
||||||
/* Execute the command */
|
/* Execute the command */
|
||||||
if ((r = execv(exec_args[0], exec_args)) != 0) {
|
/* Note about cast:
|
||||||
|
* The statement about argv[] and envp[] being constants is
|
||||||
|
* included to make explicit to future writers of language
|
||||||
|
* bindings that these objects are completely constant.
|
||||||
|
* http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
|
||||||
|
*/
|
||||||
|
if ((r = execv(exec_args[0], (char *const *)exec_args)) != 0) {
|
||||||
LOG_ERROR("Failed to execute cpugovctl helper: %s %s\n", exec_args[1], strerror(errno));
|
LOG_ERROR("Failed to execute cpugovctl helper: %s %s\n", exec_args[1], strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -100,7 +106,7 @@ bool set_governors(const char *value)
|
|||||||
/**
|
/**
|
||||||
* Return the cached governor seen at startup
|
* Return the cached governor seen at startup
|
||||||
*/
|
*/
|
||||||
const char *get_initial_governor()
|
const char *get_initial_governor(void)
|
||||||
{
|
{
|
||||||
return initial;
|
return initial;
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ void set_use_syslog(const char *name)
|
|||||||
/**
|
/**
|
||||||
* Simple getter for the syslog var
|
* Simple getter for the syslog var
|
||||||
*/
|
*/
|
||||||
bool get_use_syslog()
|
bool get_use_syslog(void)
|
||||||
{
|
{
|
||||||
return use_syslog;
|
return use_syslog;
|
||||||
}
|
}
|
||||||
|
@ -34,11 +34,11 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(__attribute__((unused)) int argc, __attribute__((unused)) char **argv)
|
int main(void)
|
||||||
{
|
{
|
||||||
/* 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());
|
fprintf(stderr, "Failed to request gamemode start: %s...\n", gamemode_error_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Simulate running a game */
|
/* Simulate running a game */
|
||||||
@ -46,6 +46,6 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char **argv)
|
|||||||
|
|
||||||
/* 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());
|
fprintf(stderr, "Failed to request gamemode end: %s...\n", gamemode_error_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char _client_error_string[512] = { 0 };
|
static char _client_error_string[512] = { 0 };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load libgamemode dynamically to dislodge us from most dependencies.
|
* Load libgamemode dynamically to dislodge us from most dependencies.
|
||||||
@ -46,7 +46,7 @@ char _client_error_string[512] = { 0 };
|
|||||||
* See SDL2 for an example of the reasoning behind this in terms of
|
* See SDL2 for an example of the reasoning behind this in terms of
|
||||||
* dynamic versioning as well.
|
* dynamic versioning as well.
|
||||||
*/
|
*/
|
||||||
volatile int _libgamemode_loaded = 1;
|
static volatile int _libgamemode_loaded = 1;
|
||||||
|
|
||||||
/* Typedefs for the functions to load */
|
/* Typedefs for the functions to load */
|
||||||
typedef int (*_gamemode_request_start)(void);
|
typedef int (*_gamemode_request_start)(void);
|
||||||
@ -54,18 +54,19 @@ typedef int (*_gamemode_request_end)(void);
|
|||||||
typedef const char *(*_gamemode_error_string)(void);
|
typedef const char *(*_gamemode_error_string)(void);
|
||||||
|
|
||||||
/* Storage for functors */
|
/* Storage for functors */
|
||||||
_gamemode_request_start _REAL_gamemode_request_start = NULL;
|
static _gamemode_request_start _REAL_gamemode_request_start = NULL;
|
||||||
_gamemode_request_end _REAL_gamemode_request_end = NULL;
|
static _gamemode_request_end _REAL_gamemode_request_end = NULL;
|
||||||
_gamemode_error_string _REAL_gamemode_error_string = NULL;
|
static _gamemode_error_string _REAL_gamemode_error_string = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal helper to perform the symbol binding safely.
|
* Internal helper to perform the symbol binding safely.
|
||||||
*
|
*
|
||||||
* Returns 0 on success and -1 on failure
|
* Returns 0 on success and -1 on failure
|
||||||
*/
|
*/
|
||||||
__attribute__((always_inline)) inline int _bind_libgamemode_symbol(void *handle, const char *name,
|
__attribute__((always_inline)) static inline int _bind_libgamemode_symbol(void *handle,
|
||||||
void **out_func,
|
const char *name,
|
||||||
size_t func_size)
|
void **out_func,
|
||||||
|
size_t func_size)
|
||||||
{
|
{
|
||||||
void *symbol_lookup = NULL;
|
void *symbol_lookup = NULL;
|
||||||
char *dl_error = NULL;
|
char *dl_error = NULL;
|
||||||
@ -88,7 +89,7 @@ __attribute__((always_inline)) inline int _bind_libgamemode_symbol(void *handle,
|
|||||||
*
|
*
|
||||||
* Returns 0 on success and -1 on failure
|
* Returns 0 on success and -1 on failure
|
||||||
*/
|
*/
|
||||||
__attribute__((always_inline)) inline int _load_libgamemode(void)
|
__attribute__((always_inline)) static inline int _load_libgamemode(void)
|
||||||
{
|
{
|
||||||
/* 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) {
|
||||||
@ -146,7 +147,7 @@ __attribute__((always_inline)) inline int _load_libgamemode(void)
|
|||||||
/**
|
/**
|
||||||
* Redirect to the real libgamemode
|
* Redirect to the real libgamemode
|
||||||
*/
|
*/
|
||||||
__attribute__((always_inline)) inline const char *gamemode_error_string(void)
|
__attribute__((always_inline)) static inline const char *gamemode_error_string(void)
|
||||||
{
|
{
|
||||||
/* 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) {
|
||||||
@ -164,7 +165,7 @@ __attribute__((always_inline)) inline const char *gamemode_error_string(void)
|
|||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
__attribute__((constructor))
|
__attribute__((constructor))
|
||||||
#else
|
#else
|
||||||
__attribute__((always_inline)) inline
|
__attribute__((always_inline)) static inline
|
||||||
#endif
|
#endif
|
||||||
int gamemode_request_start(void)
|
int gamemode_request_start(void)
|
||||||
{
|
{
|
||||||
@ -190,7 +191,7 @@ int gamemode_request_start(void)
|
|||||||
#ifdef GAMEMODE_AUTO
|
#ifdef GAMEMODE_AUTO
|
||||||
__attribute__((destructor))
|
__attribute__((destructor))
|
||||||
#else
|
#else
|
||||||
__attribute__((always_inline)) inline
|
__attribute__((always_inline)) static inline
|
||||||
#endif
|
#endif
|
||||||
int gamemode_request_end(void)
|
int gamemode_request_end(void)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user