123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #define _GNU_SOURCE
- #include "daemon_config.h"
- #include "logging.h"
- #include "ini.h"
- #include <linux/limits.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <string.h>
- #define CONFIG_NAME "gamemode.ini"
- #define CONFIG_DIR "/usr/share/gamemode/"
- #define MAX_LIST_VALUES 32
- #define MAX_LIST_VALUE_LENGTH 256
- struct GameModeConfig {
- pthread_rwlock_t rwlock;
- int inotfd;
- int inotwd;
- char whitelist[MAX_LIST_VALUES][MAX_LIST_VALUE_LENGTH];
- char blacklist[MAX_LIST_VALUES][MAX_LIST_VALUE_LENGTH];
- };
- static int inih_handler(void *user, const char *section, const char *name, const char *value)
- {
- GameModeConfig *self = (GameModeConfig *)user;
- bool valid = false;
-
- if (strcmp(section, "filter") == 0) {
- if (strcmp(name, "whitelist") == 0) {
- valid = true;
- unsigned int i = 0;
- while (*self->whitelist[i] && ++i < MAX_LIST_VALUES)
- ;
- if (i < MAX_LIST_VALUES) {
- strncpy(self->whitelist[i], value, MAX_LIST_VALUE_LENGTH);
- } else {
- LOG_MSG("Could not add [%s] to the whitelist, exceeds limit of %d\n",
- value,
- MAX_LIST_VALUES);
- }
- } else if (strcmp(name, "blacklist") == 0) {
- valid = true;
- unsigned int i = 0;
- while (*self->blacklist[i] && ++i < MAX_LIST_VALUES)
- ;
- if (i < MAX_LIST_VALUES) {
- strncpy(self->blacklist[i], value, MAX_LIST_VALUE_LENGTH);
- } else {
- LOG_MSG("Could not add [%s] to the blacklist, exceeds limit of %d\n",
- value,
- MAX_LIST_VALUES);
- }
- }
- }
- if (!valid) {
-
- LOG_MSG("Unknown value in config file [%s] %s=%s\n", section, name, value);
- }
- return 1;
- }
- static void load_config_file(GameModeConfig *self)
- {
-
- pthread_rwlock_wrlock(&self->rwlock);
-
- memset(self->whitelist, 0, sizeof(self->whitelist));
- memset(self->blacklist, 0, sizeof(self->blacklist));
-
- FILE *f = fopen(CONFIG_NAME, "r");
- if (!f) {
- f = fopen(CONFIG_DIR CONFIG_NAME, "r");
- if (!f) {
-
- LOG_ERROR("Note: No config file found [%s] in working directory or in [%s]\n",
- CONFIG_NAME,
- CONFIG_DIR);
- }
- }
- if (f) {
- int error = ini_parse_file(f, inih_handler, (void *)self);
-
- if (error) {
- LOG_MSG("Failed to parse config file - error on line %d!\n", error);
- }
- fclose(f);
- }
-
- pthread_rwlock_unlock(&self->rwlock);
- }
- GameModeConfig *config_create(void)
- {
- GameModeConfig *newconfig = (GameModeConfig *)malloc(sizeof(GameModeConfig));
- return newconfig;
- }
- void config_init(GameModeConfig *self)
- {
- pthread_rwlock_init(&self->rwlock, NULL);
-
- load_config_file(self);
- }
- void config_reload(GameModeConfig *self)
- {
- load_config_file(self);
- }
- void config_destroy(GameModeConfig *self)
- {
- pthread_rwlock_destroy(&self->rwlock);
-
- free(self);
- }
- bool config_get_client_whitelisted(GameModeConfig *self, const char *client)
- {
-
- pthread_rwlock_rdlock(&self->rwlock);
-
- bool found = true;
- if (self->whitelist[0][0]) {
-
- found = false;
- for (unsigned int i = 0; i < MAX_LIST_VALUES && self->whitelist[i][0]; i++) {
- if (strstr(client, self->whitelist[i])) {
- found = true;
- }
- }
- }
-
- pthread_rwlock_unlock(&self->rwlock);
- return found;
- }
- bool config_get_client_blacklisted(GameModeConfig *self, const char *client)
- {
-
- pthread_rwlock_rdlock(&self->rwlock);
-
- bool found = false;
- for (unsigned int i = 0; i < MAX_LIST_VALUES && self->blacklist[i][0]; i++) {
- if (strstr(client, self->blacklist[i])) {
- found = true;
- }
- }
-
- pthread_rwlock_unlock(&self->rwlock);
- return found;
- }
|