daemon_config.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <pwd.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <sys/types.h>
  37. /* Name and possible location of the config file */
  38. #define CONFIG_NAME "gamemode.ini"
  39. /* Default value for the reaper frequency */
  40. #define DEFAULT_REAPER_FREQ 5
  41. /**
  42. * The config holds various details as needed
  43. * and a rwlock to allow config_reload to be called
  44. */
  45. struct GameModeConfig {
  46. pthread_rwlock_t rwlock;
  47. int inotfd;
  48. int inotwd;
  49. char whitelist[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
  50. char blacklist[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
  51. char startscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
  52. char endscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
  53. long reaper_frequency;
  54. };
  55. /*
  56. * Add values to a char list
  57. */
  58. static bool append_value_to_list(const char *list_name, const char *value,
  59. char list[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
  60. {
  61. unsigned int i = 0;
  62. while (*list[i] && ++i < CONFIG_LIST_MAX)
  63. ;
  64. if (i < CONFIG_LIST_MAX) {
  65. strncpy(list[i], value, CONFIG_VALUE_MAX);
  66. if (list[i][CONFIG_VALUE_MAX - 1] != '\0') {
  67. LOG_ERROR("Config: Could not add [%s] to [%s], exceeds length limit of %d\n",
  68. value,
  69. list_name,
  70. CONFIG_VALUE_MAX);
  71. memset(list[i], 0, sizeof(list[i]));
  72. return false;
  73. }
  74. } else {
  75. LOG_ERROR("Config: Could not add [%s] to [%s], exceeds number of %d\n",
  76. value,
  77. list_name,
  78. CONFIG_LIST_MAX);
  79. return false;
  80. }
  81. return true;
  82. }
  83. /*
  84. * Get a positive long value from a string
  85. */
  86. static bool get_long_value(const char *value_name, const char *value, long *output)
  87. {
  88. char *end = NULL;
  89. long config_value = strtol(value, &end, 10);
  90. if (errno == ERANGE) {
  91. LOG_ERROR("Config: %s overflowed, given [%s]\n", value_name, value);
  92. return false;
  93. } else if (config_value <= 0 || !(*value != '\0' && end && *end == '\0')) {
  94. LOG_ERROR("Config: %s was invalid, given [%s]\n", value_name, value);
  95. return false;
  96. } else {
  97. *output = config_value;
  98. }
  99. return true;
  100. }
  101. /*
  102. * Handler for the inih callback
  103. */
  104. static int inih_handler(void *user, const char *section, const char *name, const char *value)
  105. {
  106. GameModeConfig *self = (GameModeConfig *)user;
  107. bool valid = false;
  108. if (strcmp(section, "filter") == 0) {
  109. /* Filter subsection */
  110. if (strcmp(name, "whitelist") == 0) {
  111. valid = append_value_to_list(name, value, self->whitelist);
  112. } else if (strcmp(name, "blacklist") == 0) {
  113. valid = append_value_to_list(name, value, self->blacklist);
  114. }
  115. } else if (strcmp(section, "general") == 0) {
  116. /* General subsection */
  117. if (strcmp(name, "reaper_freq") == 0) {
  118. valid = get_long_value(name, value, &self->reaper_frequency);
  119. }
  120. } else if (strcmp(section, "custom") == 0) {
  121. /* Custom subsection */
  122. if (strcmp(name, "start") == 0) {
  123. valid = append_value_to_list(name, value, self->startscripts);
  124. } else if (strcmp(name, "end") == 0) {
  125. valid = append_value_to_list(name, value, self->endscripts);
  126. }
  127. }
  128. if (!valid) {
  129. /* Simply ignore the value, but with a log */
  130. LOG_MSG("Config: Value ignored [%s] %s=%s\n", section, name, value);
  131. }
  132. return 1;
  133. }
  134. /*
  135. * Load the config file
  136. */
  137. static void load_config_files(GameModeConfig *self)
  138. {
  139. /* grab the current dir */
  140. char *config_location_local = get_current_dir_name();
  141. /* Get home config location */
  142. char *config_location_home = NULL;
  143. const char *cfg = getenv("XDG_CONFIG_HOME");
  144. if (cfg) {
  145. config_location_home = realpath(cfg, NULL);
  146. } else {
  147. cfg = getenv("HOME");
  148. if (cfg) {
  149. char *cfg_full = NULL;
  150. if (asprintf(&cfg_full, "%s/.config", cfg) > 0) {
  151. config_location_home = realpath(cfg_full, NULL);
  152. free(cfg_full);
  153. }
  154. } else {
  155. struct passwd *p = getpwuid(getuid());
  156. if (p) {
  157. config_location_home = realpath(p->pw_dir, NULL);
  158. }
  159. }
  160. }
  161. /* Take the write lock for the internal data */
  162. pthread_rwlock_wrlock(&self->rwlock);
  163. /* Clear our config values */
  164. memset(self->whitelist, 0, sizeof(self->whitelist));
  165. memset(self->blacklist, 0, sizeof(self->blacklist));
  166. memset(self->startscripts, 0, sizeof(self->startscripts));
  167. memset(self->endscripts, 0, sizeof(self->endscripts));
  168. self->reaper_frequency = DEFAULT_REAPER_FREQ;
  169. /*
  170. * Locations to load, in order
  171. * Arrays merge and values overwrite
  172. */
  173. const char *locations[] = {
  174. "/usr/share/gamemode", /* shipped default config */
  175. "/etc", /* administrator config */
  176. config_location_home, /* user defined config eg. $XDG_CONFIG_HOME or $HOME/.config/ */
  177. config_location_local /* local data eg. $PWD */
  178. };
  179. /* Load each file in order and overwrite values */
  180. for (unsigned int i = 0; i < sizeof(locations) / sizeof(locations[0]); i++) {
  181. char *path = NULL;
  182. if (locations[i] && asprintf(&path, "%s/" CONFIG_NAME, locations[i]) > 0) {
  183. FILE *f = fopen(path, "r");
  184. if (f) {
  185. LOG_MSG("Loading config file [%s]\n", path);
  186. int error = ini_parse_file(f, inih_handler, (void *)self);
  187. /* Failure here isn't fatal */
  188. if (error) {
  189. LOG_MSG("Failed to parse config file - error on line %d!\n", error);
  190. }
  191. }
  192. free(path);
  193. }
  194. }
  195. /* clean up memory */
  196. free(config_location_home);
  197. free(config_location_local);
  198. /* Release the lock */
  199. pthread_rwlock_unlock(&self->rwlock);
  200. }
  201. /*
  202. * Create a context object
  203. */
  204. GameModeConfig *config_create(void)
  205. {
  206. GameModeConfig *newconfig = (GameModeConfig *)malloc(sizeof(GameModeConfig));
  207. return newconfig;
  208. }
  209. /*
  210. * Initialise the config
  211. */
  212. void config_init(GameModeConfig *self)
  213. {
  214. pthread_rwlock_init(&self->rwlock, NULL);
  215. /* load the initial config */
  216. load_config_files(self);
  217. }
  218. /*
  219. * Re-load the config file
  220. */
  221. void config_reload(GameModeConfig *self)
  222. {
  223. load_config_files(self);
  224. }
  225. /*
  226. * Destroy the config
  227. */
  228. void config_destroy(GameModeConfig *self)
  229. {
  230. pthread_rwlock_destroy(&self->rwlock);
  231. /* Finally, free the memory */
  232. free(self);
  233. }
  234. /*
  235. * Checks if the client is whitelisted
  236. */
  237. bool config_get_client_whitelisted(GameModeConfig *self, const char *client)
  238. {
  239. /* Take the read lock for the internal data */
  240. pthread_rwlock_rdlock(&self->rwlock);
  241. /* If the whitelist is empty then everything passes */
  242. bool found = true;
  243. if (self->whitelist[0][0]) {
  244. /*
  245. * Check if the value is found in our whitelist
  246. * Currently is a simple strstr check, but could be modified for wildcards etc.
  247. */
  248. found = false;
  249. for (unsigned int i = 0; i < CONFIG_LIST_MAX && self->whitelist[i][0]; i++) {
  250. if (strstr(client, self->whitelist[i])) {
  251. found = true;
  252. }
  253. }
  254. }
  255. /* release the lock */
  256. pthread_rwlock_unlock(&self->rwlock);
  257. return found;
  258. }
  259. /*
  260. * Checks if the client is blacklisted
  261. */
  262. bool config_get_client_blacklisted(GameModeConfig *self, const char *client)
  263. {
  264. /* Take the read lock for the internal data */
  265. pthread_rwlock_rdlock(&self->rwlock);
  266. /*
  267. * Check if the value is found in our whitelist
  268. * Currently is a simple strstr check, but could be modified for wildcards etc.
  269. */
  270. bool found = false;
  271. for (unsigned int i = 0; i < CONFIG_LIST_MAX && self->blacklist[i][0]; i++) {
  272. if (strstr(client, self->blacklist[i])) {
  273. found = true;
  274. }
  275. }
  276. /* release the lock */
  277. pthread_rwlock_unlock(&self->rwlock);
  278. return found;
  279. }
  280. /*
  281. * Gets the reaper frequency
  282. */
  283. long config_get_reaper_thread_frequency(GameModeConfig *self)
  284. {
  285. long value;
  286. /* Take the read lock */
  287. pthread_rwlock_rdlock(&self->rwlock);
  288. value = self->reaper_frequency;
  289. /* release the lock */
  290. pthread_rwlock_unlock(&self->rwlock);
  291. return value;
  292. }
  293. /*
  294. * Get a set of scripts to call when gamemode starts
  295. */
  296. void config_get_gamemode_start_scripts(GameModeConfig *self,
  297. char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
  298. {
  299. /* Take the read lock */
  300. pthread_rwlock_rdlock(&self->rwlock);
  301. memcpy(scripts, self->startscripts, sizeof(self->startscripts));
  302. /* release the lock */
  303. pthread_rwlock_unlock(&self->rwlock);
  304. }
  305. /*
  306. * Get a set of scripts to call when gamemode ends
  307. */
  308. void config_get_gamemode_end_scripts(GameModeConfig *self,
  309. char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
  310. {
  311. /* Take the read lock */
  312. pthread_rwlock_rdlock(&self->rwlock);
  313. memcpy(scripts, self->endscripts, sizeof(self->startscripts));
  314. /* release the lock */
  315. pthread_rwlock_unlock(&self->rwlock);
  316. }