From c5f58c56d0159e6fdef2213707818df2e8a72f01 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Fri, 8 Feb 2019 18:15:43 +0000 Subject: [PATCH] Hook up interfaces for functions to register another process --- daemon/dbus_messaging.c | 46 +++++++++++++++++++++++++++++++++++++++++ daemon/gamemode.c | 22 ++++++++++++++++++++ daemon/gamemode.h | 22 ++++++++++++++++++++ lib/client_impl.c | 27 +++++++++++++++++------- 4 files changed, 110 insertions(+), 7 deletions(-) diff --git a/daemon/dbus_messaging.c b/daemon/dbus_messaging.c index f777612..157b4b3 100644 --- a/daemon/dbus_messaging.c +++ b/daemon/dbus_messaging.c @@ -120,6 +120,48 @@ static int method_query_status(sd_bus_message *m, void *userdata, return sd_bus_reply_method_return(m, "i", status); } +/** + * Handles the RegisterGameByPID D-BUS Method + */ +static int method_register_game_by_pid(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 reply = game_mode_context_register_by_pid(context, (pid_t)callerpid, (pid_t)gamepid); + + return sd_bus_reply_method_return(m, "i", reply); +} + +/** + * Handles the UnregisterGameByPID D-BUS Method + */ +static int method_unregister_game_by_pid(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 reply = game_mode_context_unregister_by_pid(context, (pid_t)callerpid, (pid_t)gamepid); + + return sd_bus_reply_method_return(m, "i", reply); +} + /** * D-BUS vtable to dispatch virtual methods */ @@ -128,6 +170,10 @@ static const sd_bus_vtable gamemode_vtable[] = 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("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid, + SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid, + SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END }; /** diff --git a/daemon/gamemode.c b/daemon/gamemode.c index e366ff0..aff402d 100644 --- a/daemon/gamemode.c +++ b/daemon/gamemode.c @@ -502,6 +502,28 @@ int game_mode_context_query_status(GameModeContext *self, pid_t client) return ret; } +/** + * Stub to register on behalf of caller + */ +int game_mode_context_register_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid) +{ + (void)self; + (void)callerpid; + (void)gamepid; + return 0; +} + +/** + * Stub to unregister on behalf of caller + */ +int game_mode_context_unregister_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid) +{ + (void)self; + (void)callerpid; + (void)gamepid; + return 0; +} + /** * Construct a new GameModeClient for the given process ID * diff --git a/daemon/gamemode.h b/daemon/gamemode.h index 6e9124f..b092a2f 100644 --- a/daemon/gamemode.h +++ b/daemon/gamemode.h @@ -89,6 +89,28 @@ bool game_mode_context_unregister(GameModeContext *self, pid_t pid); */ int game_mode_context_query_status(GameModeContext *self, pid_t pid); +/** + * Register a new game client with the context on behalf of another process + * + * @param callerpid Process ID for the remote client making the request + * @param gamepid Process ID for the process to be registered + * @returns 0 if the request was accepted and the client could be registered + * -1 if the request was accepted but the client could not be registered + * -2 if the request was rejected + */ +int game_mode_context_register_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid); + +/** + * Unregister an existing remote game client from the context on behalf of another process + * + * @param callerpid Process ID for the remote client making the request + * @param gamepid Process ID for the process to be registered + * @returns 0 if the request was accepted and the client existed + * -1 if the request was accepted but the client did not exist + * -2 if the request was rejected + */ +int game_mode_context_unregister_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid); + /** * Query the config of a gamemode context * diff --git a/lib/client_impl.c b/lib/client_impl.c index 00eeed3..029d694 100644 --- a/lib/client_impl.c +++ b/lib/client_impl.c @@ -43,7 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. static char error_string[512] = { 0 }; // Simple requestor function for a gamemode -static int gamemode_request(const char *function) +static int gamemode_request(const char *function, int arg) { sd_bus_message *msg = NULL; sd_bus *bus = NULL; @@ -66,8 +66,9 @@ static int gamemode_request(const char *function) function, NULL, &msg, - "i", - getpid()); + arg ? "ii" : "i", + getpid(), + arg); if (ret < 0) { snprintf(error_string, sizeof(error_string), @@ -97,17 +98,29 @@ extern const char *real_gamemode_error_string(void) // Wrapper to call RegisterGame extern int real_gamemode_request_start(void) { - return gamemode_request("RegisterGame"); + return gamemode_request("RegisterGame", 0); } // Wrapper to call UnregisterGame extern int real_gamemode_request_end(void) { - return gamemode_request("UnregisterGame"); + return gamemode_request("UnregisterGame", 0); } -// Wrapper to call UnregisterGame +// Wrapper to call QueryStatus extern int real_gamemode_query_status(void) { - return gamemode_request("QueryStatus"); + return gamemode_request("QueryStatus", 0); +} + +// Wrapper to call RegisterGameByPID +extern int real_gamemode_register_start_for(pid_t pid) +{ + return gamemode_request("RegisterGameByPID", pid); +} + +// Wrapper to call UnregisterGameByPID +extern int real_gamemode_register_end_for(pid_t pid) +{ + return gamemode_request("UnregisterGameByPID", pid); }