Implement game_mode_query_status_for as well

This commit is contained in:
Marc Di Luzio
2019-02-09 16:09:11 +00:00
parent 1b96111afc
commit c2f7e971c6
5 changed files with 99 additions and 0 deletions

View File

@@ -162,6 +162,27 @@ static int method_unregister_game_by_pid(sd_bus_message *m, void *userdata,
return sd_bus_reply_method_return(m, "i", reply);
}
/**
* Handles the QueryStatus D-BUS Method
*/
static int method_query_status_for(sd_bus_message *m, void *userdata,
__attribute__((unused)) sd_bus_error *ret_error)
{
int callerpid = 0;
int gamepid = 0;
GameModeContext *context = userdata;
int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
if (ret < 0) {
LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
return ret;
}
int status = game_mode_context_query_status_for(context, (pid_t)callerpid, (pid_t)gamepid);
return sd_bus_reply_method_return(m, "i", status);
}
/**
* D-BUS vtable to dispatch virtual methods
*/
@@ -174,6 +195,8 @@ static const sd_bus_vtable gamemode_vtable[] =
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("QueryStatusFor", "ii", "i", method_query_status_for,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END };
/**

View File

@@ -568,6 +568,38 @@ error_cleanup:
return status;
}
/**
* Request status on behalf of caller
*/
int game_mode_context_query_status_for(GameModeContext *self, pid_t callerpid, pid_t gamepid)
{
int status = 0;
/* Lookup the executable first */
char *executable = game_mode_context_find_exe(callerpid);
if (!executable) {
status = -1;
goto error_cleanup;
}
/* Check our blacklist and whitelist */
if (!config_get_supervisor_whitelisted(self->config, executable)) {
LOG_MSG("Supervisor [%s] was rejected (not in whitelist)\n", executable);
status = -2;
goto error_cleanup;
} else if (config_get_supervisor_blacklisted(self->config, executable)) {
LOG_MSG("Supervisor [%s] was rejected (in blacklist)\n", executable);
status = -2;
goto error_cleanup;
}
/* Checks cleared, call the original query */
return game_mode_context_query_status(self, gamepid);
error_cleanup:
free(executable);
return status;
}
/**
* Construct a new GameModeClient for the given process ID
*

View File

@@ -111,6 +111,17 @@ int game_mode_context_register_by_pid(GameModeContext *self, pid_t callerpid, pi
*/
int game_mode_context_unregister_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid);
/**
* Query the current status of gamemode for another process
*
* @param pid Process ID for the remote client
* @returns Positive if gamemode is active
* 1 if gamemode is active but the client is not registered
* 2 if gamemode is active and the client is registered
* -2 if this supervisor was rejected
*/
int game_mode_context_query_status_for(GameModeContext *self, pid_t callerpid, pid_t gamepid);
/**
* Query the config of a gamemode context
*