Add gamemode_query_status and teach gamemoded '-s'

This allows the client to query the daemon about the status of gamemode.

	Returns the following:
		 0 if gamemode is inactive
		 1 if gamemode is active
		 2 if gamemode is active and this client is registered
		-1 if the query failed

	Passing -s to gamemoded will simply query and print the current status.

	Allows for more comprehensive testing when using 'gamemoded -r' as well as more reactionary program behaviour
This commit is contained in:
Marc Di Luzio
2018-04-25 14:39:05 +01:00
parent 2317bcde14
commit 3eea07255e
8 changed files with 140 additions and 4 deletions

View File

@@ -366,6 +366,40 @@ bool game_mode_context_unregister(GameModeContext *self, pid_t client)
return true;
}
int game_mode_context_query_status(GameModeContext *self, pid_t client)
{
GameModeClient *cl = NULL;
int ret = 0;
/*
* Check the current refcount on gamemode, this equates to whether gamemode is active or not,
* see game_mode_context_register and game_mode_context_unregister
*/
if (atomic_load_explicit(&self->refcount, memory_order_seq_cst)) {
ret++;
/* Check if the current client is registered */
/* Requires locking. */
pthread_rwlock_rdlock(&self->rwlock);
for (cl = self->client; cl; cl = cl->next) {
if (cl->pid != client) {
continue;
}
/* Found it */
ret++;
break;
}
/* Unlock here, potentially yielding */
pthread_rwlock_unlock(&self->rwlock);
}
return ret;
}
/**
* Construct a new GameModeClient for the given process ID
*