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:
Leonard
2018-04-16 18:21:35 +02:00
committed by Marc Di Luzio
parent 6b71edf740
commit 752d877196
8 changed files with 34 additions and 27 deletions

View File

@@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
/**
* 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 } };
int num = fetch_governors(governors);

View File

@@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
/**
* Helper to perform standard UNIX daemonization
*/
void daemonize(char *name)
void daemonize(const char *name)
{
/* Initial fork */
pid_t pid = fork();

View File

@@ -35,4 +35,4 @@ POSSIBILITY OF SUCH DAMAGE.
* Attempt daemonization of the process.
* If this fails, the process will exit
*/
void daemonize(char *name);
void daemonize(const char *name);

View File

@@ -77,7 +77,7 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
}
/* 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) {
continue;
}
@@ -95,7 +95,7 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
/**
* Return the current governor state
*/
const char *get_gov_state()
const char *get_gov_state(void)
{
/* Cached primary governor state */
static char governor[64] = { 0 };

View File

@@ -46,7 +46,7 @@ static const char *initial = NULL;
/**
* Cache the governor state as seen at startup
*/
void update_initial_gov_state()
void update_initial_gov_state(void)
{
initial = get_gov_state();
}
@@ -61,9 +61,9 @@ bool set_governors(const char *value)
int ret = 0;
int r = -1;
const char *govern = value ? value : initial;
char *exec_args[] = {
"/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", (char *)govern, NULL,
const char *const govern = value ? value : initial;
const char *const exec_args[] = {
"/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", govern, NULL,
};
LOG_MSG("Requesting update of governor policy to %s\n", govern);
@@ -73,7 +73,13 @@ bool set_governors(const char *value)
return false;
} else if (p == 0) {
/* 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));
exit(EXIT_FAILURE);
}
@@ -100,7 +106,7 @@ bool set_governors(const char *value)
/**
* Return the cached governor seen at startup
*/
const char *get_initial_governor()
const char *get_initial_governor(void)
{
return initial;
}

View File

@@ -47,7 +47,7 @@ void set_use_syslog(const char *name)
/**
* Simple getter for the syslog var
*/
bool get_use_syslog()
bool get_use_syslog(void)
{
return use_syslog;
}