From 208f37b7d1831979a1b81db7c757a555d64b0be3 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Tue, 2 Jul 2019 17:25:50 +0200 Subject: [PATCH] daemon: method to list all registered clients Return an array of pid_t elements containing the process ids of all registered clients. Memory ownership is transferred to the client and must be freed. --- daemon/gamemode-context.c | 26 ++++++++++++++++++++++++++ daemon/gamemode.h | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/daemon/gamemode-context.c b/daemon/gamemode-context.c index d53b0f8..000f472 100644 --- a/daemon/gamemode-context.c +++ b/daemon/gamemode-context.c @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include /* TODO: Move usage to gamemode-dbus.c */ #include @@ -332,6 +333,31 @@ int game_mode_context_num_clients(GameModeContext *self) return atomic_load(&self->refcount); } +pid_t *game_mode_context_list_clients(GameModeContext *self, unsigned int *count) +{ + pid_t *res = NULL; + unsigned int i = 0; + unsigned int n; + + pthread_rwlock_rdlock(&self->rwlock); + n = (unsigned int)atomic_load(&self->refcount); + + if (n > 0) + res = (pid_t *)malloc(n * sizeof(pid_t)); + + for (GameModeClient *cl = self->client; cl; cl = cl->next) { + assert(n > i); + + res[i] = cl->pid; + i++; + } + + *count = i; + + pthread_rwlock_unlock(&self->rwlock); + return res; +} + static int game_mode_apply_client_optimisations(GameModeContext *self, pid_t client) { /* Store current renice and apply */ diff --git a/daemon/gamemode.h b/daemon/gamemode.h index 1fa45ef..7ea1256 100644 --- a/daemon/gamemode.h +++ b/daemon/gamemode.h @@ -95,6 +95,14 @@ void game_mode_context_destroy(GameModeContext *self); */ int game_mode_context_num_clients(GameModeContext *self); +/** + * List the currently active clients. + * @param out holds the number of active clients. + * + * @returns A array of pid_t or NULL if there are no active clients. + */ +pid_t *game_mode_context_list_clients(GameModeContext *self, unsigned int *count); + /** * Register a new game client with the context *