123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- /*
- Copyright (c) 2017, Feral Interactive
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of Feral Interactive nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- */
- #define _GNU_SOURCE
- #include "gamemode.h"
- #include "governors.h"
- #include "logging.h"
- #include <linux/limits.h>
- #include <pthread.h>
- #include <signal.h>
- #include <stdatomic.h>
- #include <string.h>
- /**
- * The GameModeClient encapsulates the remote connection, providing a list
- * form to contain the pid and credentials.
- */
- typedef struct GameModeClient {
- pid_t pid; /**< Process ID */
- struct GameModeClient *next; /**<Next client in the list */
- char *executable; /**<Process executable */
- } GameModeClient;
- struct GameModeContext {
- pthread_rwlock_t rwlock; /**<Guard access to the client list */
- _Atomic int refcount; /**<Allow cycling the game mode */
- GameModeClient *client; /**<Pointer to first client */
- bool performance_mode; /**<Only updates when we can */
- /* Reaper control */
- struct {
- pthread_t thread;
- bool running;
- pthread_mutex_t mutex;
- pthread_cond_t condition;
- } reaper;
- };
- static GameModeContext instance = { 0 };
- /* Maximum number of concurrent processes we'll sanely support */
- #define MAX_GAMES 256
- /* How often our reaper thread will run */
- #define SLEEP_INTERVAL 5
- /**
- * Protect against signals
- */
- static volatile bool had_context_init = false;
- static GameModeClient *game_mode_client_new(pid_t pid);
- static void game_mode_client_free(GameModeClient *client);
- static bool game_mode_context_has_client(GameModeContext *self, pid_t client);
- static int game_mode_context_num_clients(GameModeContext *self);
- static void *game_mode_context_reaper(void *userdata);
- static void game_mode_context_enter(GameModeContext *self);
- static void game_mode_context_leave(GameModeContext *self);
- static char *game_mode_context_find_exe(pid_t pid);
- void game_mode_context_init(GameModeContext *self)
- {
- if (had_context_init) {
- LOG_ERROR("Context already initialised\n");
- return;
- }
- had_context_init = true;
- self->refcount = ATOMIC_VAR_INIT(0);
- /* Read current governer state before setting up any message handling */
- update_initial_gov_state();
- LOG_MSG("governor is set to [%s]\n", get_initial_governor());
- pthread_mutex_init(&self->reaper.mutex, NULL);
- pthread_cond_init(&self->reaper.condition, NULL);
- /* Get the reaper thread going */
- self->reaper.running = true;
- if (pthread_create(&self->reaper.thread, NULL, game_mode_context_reaper, self) != 0) {
- FATAL_ERROR("Couldn't construct a new thread");
- }
- }
- void game_mode_context_destroy(GameModeContext *self)
- {
- if (!had_context_init) {
- return;
- }
- /* Leave game mode now */
- if (game_mode_context_num_clients(self) > 0) {
- game_mode_context_leave(self);
- }
- had_context_init = false;
- game_mode_client_free(self->client);
- self->reaper.running = false;
- /* We might be stuck waiting, so wake it up again */
- pthread_mutex_lock(&self->reaper.mutex);
- pthread_cond_signal(&self->reaper.condition);
- pthread_mutex_unlock(&self->reaper.mutex);
- /* Join the thread as soon as possible */
- pthread_join(self->reaper.thread, NULL);
- pthread_cond_destroy(&self->reaper.condition);
- pthread_mutex_destroy(&self->reaper.mutex);
- pthread_rwlock_destroy(&self->rwlock);
- }
- /**
- * Pivot into game mode.
- *
- * This is only possible after game_mode_context_init has made a GameModeContext
- * usable, and should always be followed by a game_mode_context_leave.
- */
- static void game_mode_context_enter(GameModeContext *self)
- {
- LOG_MSG("Entering Game Mode...\n");
- if (!self->performance_mode && set_governors("performance")) {
- self->performance_mode = true;
- }
- }
- /**
- * Pivot out of game mode.
- *
- * Should only be called after both init and game_mode_context_enter have
- * been performed.
- */
- static void game_mode_context_leave(GameModeContext *self)
- {
- LOG_MSG("Leaving Game Mode...\n");
- if (self->performance_mode && set_governors(NULL)) {
- self->performance_mode = false;
- }
- }
- /**
- * Automatically expire all dead processes
- *
- * This has to take special care to ensure thread safety and ensuring that our
- * pointer is never cached incorrectly.
- */
- static void game_mode_context_auto_expire(GameModeContext *self)
- {
- bool removing = true;
- while (removing) {
- pthread_rwlock_rdlock(&self->rwlock);
- removing = false;
- /* Each time we hit an expired game, start the loop back */
- for (GameModeClient *client = self->client; client; client = client->next) {
- if (kill(client->pid, 0) != 0) {
- LOG_MSG("Removing expired game [%i]...\n", client->pid);
- pthread_rwlock_unlock(&self->rwlock);
- game_mode_context_unregister(self, client->pid);
- removing = true;
- break;
- }
- }
- if (!removing) {
- pthread_rwlock_unlock(&self->rwlock);
- break;
- }
- }
- }
- /**
- * Determine if the client is already known to the context
- */
- static bool game_mode_context_has_client(GameModeContext *self, pid_t client)
- {
- bool found = false;
- pthread_rwlock_rdlock(&self->rwlock);
- /* Walk all clients and find a matching pid */
- for (GameModeClient *cl = self->client; cl; cl = cl->next) {
- if (cl->pid == client) {
- found = true;
- break;
- }
- }
- pthread_rwlock_unlock(&self->rwlock);
- return found;
- }
- /**
- * Helper to grab the current number of clients we know about
- */
- static int game_mode_context_num_clients(GameModeContext *self)
- {
- return atomic_load(&self->refcount);
- }
- bool game_mode_context_register(GameModeContext *self, pid_t client)
- {
- /* Construct a new client if we can */
- GameModeClient *cl = NULL;
- cl = game_mode_client_new(client);
- if (!cl) {
- fputs("OOM\n", stderr);
- return false;
- }
- cl->executable = game_mode_context_find_exe(client);
- if (game_mode_context_has_client(self, client)) {
- LOG_ERROR("Addition requested for already known process [%d]\n", client);
- return false;
- }
- /* Cap the total number of active clients */
- if (game_mode_context_num_clients(self) + 1 > MAX_GAMES) {
- LOG_ERROR("Max games (%d) reached, not registering %d\n", MAX_GAMES, client);
- return false;
- }
- /* Begin a write lock now to insert our new client at list start */
- pthread_rwlock_wrlock(&self->rwlock);
- LOG_MSG("Adding game: %d [%s]\n", client, cl->executable);
- /* Update the list */
- cl->next = self->client;
- self->client = cl;
- pthread_rwlock_unlock(&self->rwlock);
- /* First add, init */
- if (atomic_fetch_add_explicit(&self->refcount, 1, memory_order_seq_cst) == 0) {
- game_mode_context_enter(self);
- }
- return true;
- }
- bool game_mode_context_unregister(GameModeContext *self, pid_t client)
- {
- GameModeClient *cl = NULL;
- GameModeClient *prev = NULL;
- bool found = false;
- /* Requires locking. */
- pthread_rwlock_wrlock(&self->rwlock);
- for (prev = cl = self->client; cl; cl = cl->next) {
- if (cl->pid != client) {
- prev = cl;
- continue;
- }
- LOG_MSG("Removing game: %d [%s]\n", client, cl->executable);
- /* Found it */
- found = true;
- prev->next = cl->next;
- if (cl == self->client) {
- self->client = cl->next;
- }
- cl->next = NULL;
- game_mode_client_free(cl);
- break;
- }
- /* Unlock here, potentially yielding */
- pthread_rwlock_unlock(&self->rwlock);
- if (!found) {
- LOG_ERROR("Removal requested for unknown process [%d]\n", client);
- return false;
- }
- /* When we hit bottom then end the game mode */
- if (atomic_fetch_sub_explicit(&self->refcount, 1, memory_order_seq_cst) == 1) {
- game_mode_context_leave(self);
- }
- return true;
- }
- /**
- * Construct a new GameModeClient for the given process ID
- *
- * This is deliberately OOM safe
- */
- static GameModeClient *game_mode_client_new(pid_t pid)
- {
- GameModeClient c = {
- .next = NULL,
- .pid = pid,
- };
- GameModeClient *ret = NULL;
- ret = calloc(1, sizeof(struct GameModeClient));
- if (!ret) {
- return NULL;
- }
- *ret = c;
- return ret;
- }
- /**
- * Free a client and the next element in the list.
- */
- static void game_mode_client_free(GameModeClient *client)
- {
- if (!client) {
- return;
- }
- if (client->next) {
- game_mode_client_free(client->next);
- }
- if (client->executable) {
- free(client->executable);
- }
- free(client);
- }
- /**
- * We continuously run until told otherwise.
- */
- static void *game_mode_context_reaper(void *userdata)
- {
- /* Stack, not allocated, won't disappear. */
- GameModeContext *self = userdata;
- struct timespec ts = { 0, 0 };
- ts.tv_sec = time(NULL) + SLEEP_INTERVAL;
- while (self->reaper.running) {
- /* Wait for condition */
- pthread_mutex_lock(&self->reaper.mutex);
- pthread_cond_timedwait(&self->reaper.condition, &self->reaper.mutex, &ts);
- pthread_mutex_unlock(&self->reaper.mutex);
- /* Highly possible the main thread woke us up to exit */
- if (!self->reaper.running) {
- return NULL;
- }
- /* Expire remaining entries */
- game_mode_context_auto_expire(self);
- ts.tv_sec = time(NULL) + SLEEP_INTERVAL;
- }
- return NULL;
- }
- GameModeContext *game_mode_context_instance()
- {
- return &instance;
- }
- /**
- * Attempt to locate the exe for the process.
- * We might run into issues if the process is running under an odd umask.
- */
- static char *game_mode_context_find_exe(pid_t pid)
- {
- static char proc_path[PATH_MAX] = { 0 };
- if (snprintf(proc_path, sizeof(proc_path), "/proc/%d/exe", pid) < 0) {
- LOG_ERROR("Unable to find executable for PID %d: %s\n", pid, strerror(errno));
- return NULL;
- }
- /* Allocate the realpath if possible */
- return realpath(proc_path, NULL);
- }
|