daemon_config.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Copyright (c) 2017-2018, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include "daemon_config.h"
  28. #include "logging.h"
  29. /* Ben Hoyt's inih library */
  30. #include "ini.h"
  31. #include <linux/limits.h>
  32. #include <pthread.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. /* Name and possible location of the config file */
  36. #define CONFIG_NAME "gamemode.ini"
  37. #define CONFIG_DIR "/usr/share/gamemode/"
  38. /* Maximum values in a config list */
  39. #define MAX_LIST_VALUES 32
  40. /*
  41. * Maximum length of values in a config list
  42. * In practice inih has a INI_MAX_LINE value of 200, so this should never get hit
  43. */
  44. #define MAX_LIST_VALUE_LENGTH 256
  45. /* Default value for the reaper frequency */
  46. #define DEFAULT_REAPER_FREQ 5
  47. /**
  48. * The config holds various details as needed
  49. * and a rwlock to allow config_reload to be called
  50. */
  51. struct GameModeConfig {
  52. pthread_rwlock_t rwlock;
  53. int inotfd;
  54. int inotwd;
  55. char whitelist[MAX_LIST_VALUES][MAX_LIST_VALUE_LENGTH];
  56. char blacklist[MAX_LIST_VALUES][MAX_LIST_VALUE_LENGTH];
  57. long reaper_frequency;
  58. };
  59. /*
  60. * Add values to a char list
  61. */
  62. static bool append_value_to_list(const char *list_name, const char *value,
  63. char list[MAX_LIST_VALUES][MAX_LIST_VALUE_LENGTH])
  64. {
  65. unsigned int i = 0;
  66. while (*list[i] && ++i < MAX_LIST_VALUES)
  67. ;
  68. if (i < MAX_LIST_VALUES) {
  69. strncpy(list[i], value, MAX_LIST_VALUE_LENGTH);
  70. if (list[i][MAX_LIST_VALUE_LENGTH - 1] != '\0') {
  71. LOG_ERROR("Config: Could not add [%s] to [%s], exceeds length limit of %d\n",
  72. value,
  73. list_name,
  74. MAX_LIST_VALUE_LENGTH);
  75. memset(list[i], 0, sizeof(list[i]));
  76. return false;
  77. }
  78. } else {
  79. LOG_ERROR("Config: Could not add [%s] to [%s], exceeds number of %d\n",
  80. value,
  81. list_name,
  82. MAX_LIST_VALUES);
  83. return false;
  84. }
  85. return true;
  86. }
  87. /*
  88. * Get a positive long value from a string
  89. */
  90. static bool get_long_value(const char *value_name, const char *value, long *output)
  91. {
  92. char *end = NULL;
  93. long config_value = strtol(value, &end, 10);
  94. if (errno == ERANGE) {
  95. LOG_ERROR("Config: %s overflowed, given [%s]\n", value_name, value);
  96. return false;
  97. } else if (config_value <= 0 || !(*value != '\0' && end && *end == '\0')) {
  98. LOG_ERROR("Config: %s was invalid, given [%s]\n", value_name, value);
  99. return false;
  100. } else {
  101. *output = config_value;
  102. }
  103. return true;
  104. }
  105. /*
  106. * Handler for the inih callback
  107. */
  108. static int inih_handler(void *user, const char *section, const char *name, const char *value)
  109. {
  110. GameModeConfig *self = (GameModeConfig *)user;
  111. bool valid = false;
  112. if (strcmp(section, "filter") == 0) {
  113. /* Filter subsection */
  114. if (strcmp(name, "whitelist") == 0) {
  115. valid = append_value_to_list(name, value, self->whitelist);
  116. } else if (strcmp(name, "blacklist") == 0) {
  117. valid = append_value_to_list(name, value, self->blacklist);
  118. }
  119. } else if (strcmp(section, "general") == 0) {
  120. /* General subsection */
  121. if (strcmp(name, "reaper_freq") == 0) {
  122. valid = get_long_value(name, value, &self->reaper_frequency);
  123. }
  124. }
  125. if (!valid) {
  126. /* Simply ignore the value, but with a log */
  127. LOG_MSG("Config: Value ignored [%s] %s=%s\n", section, name, value);
  128. }
  129. return 1;
  130. }
  131. /*
  132. * Load the config file
  133. */
  134. static void load_config_file(GameModeConfig *self)
  135. {
  136. /* Take the write lock for the internal data */
  137. pthread_rwlock_wrlock(&self->rwlock);
  138. /* Clear our config values */
  139. memset(self->whitelist, 0, sizeof(self->whitelist));
  140. memset(self->blacklist, 0, sizeof(self->blacklist));
  141. self->reaper_frequency = DEFAULT_REAPER_FREQ;
  142. /* try locally first */
  143. FILE *f = fopen(CONFIG_NAME, "r");
  144. if (!f) {
  145. f = fopen(CONFIG_DIR CONFIG_NAME, "r");
  146. if (!f) {
  147. /* Failure here isn't fatal */
  148. LOG_ERROR("Note: No config file found [%s] in working directory or in [%s]\n",
  149. CONFIG_NAME,
  150. CONFIG_DIR);
  151. }
  152. }
  153. if (f) {
  154. int error = ini_parse_file(f, inih_handler, (void *)self);
  155. /* Failure here isn't fatal */
  156. if (error) {
  157. LOG_MSG("Failed to parse config file - error on line %d!\n", error);
  158. }
  159. fclose(f);
  160. }
  161. /* Release the lock */
  162. pthread_rwlock_unlock(&self->rwlock);
  163. }
  164. /*
  165. * Create a context object
  166. */
  167. GameModeConfig *config_create(void)
  168. {
  169. GameModeConfig *newconfig = (GameModeConfig *)malloc(sizeof(GameModeConfig));
  170. return newconfig;
  171. }
  172. /*
  173. * Initialise the config
  174. */
  175. void config_init(GameModeConfig *self)
  176. {
  177. pthread_rwlock_init(&self->rwlock, NULL);
  178. /* load the initial config */
  179. load_config_file(self);
  180. }
  181. /*
  182. * Re-load the config file
  183. */
  184. void config_reload(GameModeConfig *self)
  185. {
  186. load_config_file(self);
  187. }
  188. /*
  189. * Destroy the config
  190. */
  191. void config_destroy(GameModeConfig *self)
  192. {
  193. pthread_rwlock_destroy(&self->rwlock);
  194. /* Finally, free the memory */
  195. free(self);
  196. }
  197. /*
  198. * Checks if the client is whitelisted
  199. */
  200. bool config_get_client_whitelisted(GameModeConfig *self, const char *client)
  201. {
  202. /* Take the read lock for the internal data */
  203. pthread_rwlock_rdlock(&self->rwlock);
  204. /* If the whitelist is empty then everything passes */
  205. bool found = true;
  206. if (self->whitelist[0][0]) {
  207. /*
  208. * Check if the value is found in our whitelist
  209. * Currently is a simple strstr check, but could be modified for wildcards etc.
  210. */
  211. found = false;
  212. for (unsigned int i = 0; i < MAX_LIST_VALUES && self->whitelist[i][0]; i++) {
  213. if (strstr(client, self->whitelist[i])) {
  214. found = true;
  215. }
  216. }
  217. }
  218. /* release the lock */
  219. pthread_rwlock_unlock(&self->rwlock);
  220. return found;
  221. }
  222. /*
  223. * Checks if the client is blacklisted
  224. */
  225. bool config_get_client_blacklisted(GameModeConfig *self, const char *client)
  226. {
  227. /* Take the read lock for the internal data */
  228. pthread_rwlock_rdlock(&self->rwlock);
  229. /*
  230. * Check if the value is found in our whitelist
  231. * Currently is a simple strstr check, but could be modified for wildcards etc.
  232. */
  233. bool found = false;
  234. for (unsigned int i = 0; i < MAX_LIST_VALUES && self->blacklist[i][0]; i++) {
  235. if (strstr(client, self->blacklist[i])) {
  236. found = true;
  237. }
  238. }
  239. /* release the lock */
  240. pthread_rwlock_unlock(&self->rwlock);
  241. return found;
  242. }
  243. /*
  244. * Gets the reaper frequency
  245. */
  246. long config_get_reaper_thread_frequency(GameModeConfig *self)
  247. {
  248. long value;
  249. /* Take the read lock */
  250. pthread_rwlock_rdlock(&self->rwlock);
  251. value = self->reaper_frequency;
  252. /* release the lock */
  253. pthread_rwlock_unlock(&self->rwlock);
  254. return value;
  255. }