mainconfig.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include "../include/saAis.h"
  43. #include "../include/list.h"
  44. #include "util.h"
  45. #include "mainconfig.h"
  46. #include "mempool.h"
  47. #include "print.h"
  48. #include "totem.h"
  49. #include "service.h"
  50. static char error_string_response[512];
  51. /* This just makes the code below a little neater */
  52. static inline int objdb_get_string(struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  53. char *key, char **value)
  54. {
  55. int res;
  56. *value = NULL;
  57. if ( !(res = objdb->object_key_get (object_service_handle,
  58. key,
  59. strlen (key),
  60. (void *)value,
  61. NULL))) {
  62. if (*value)
  63. return 0;
  64. }
  65. return -1;
  66. }
  67. static inline void objdb_get_int (
  68. struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  69. char *key, unsigned int *intvalue)
  70. {
  71. char *value = NULL;
  72. if (!objdb->object_key_get (object_service_handle,
  73. key,
  74. strlen (key),
  75. (void *)&value,
  76. NULL)) {
  77. if (value) {
  78. *intvalue = atoi(value);
  79. }
  80. }
  81. }
  82. int openais_main_config_read (
  83. struct objdb_iface_ver0 *objdb,
  84. char **error_string,
  85. struct main_config *main_config)
  86. {
  87. unsigned int object_service_handle;
  88. unsigned int object_logger_handle;
  89. char *value;
  90. char *error_reason = error_string_response;
  91. int i;
  92. memset (main_config, 0, sizeof (struct main_config));
  93. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  94. if (objdb->object_find (
  95. OBJECT_PARENT_HANDLE,
  96. "logging",
  97. strlen ("logging"),
  98. &object_service_handle) == 0) {
  99. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  100. if (strcmp (value, "yes") == 0) {
  101. main_config->logmode |= LOG_MODE_FILE;
  102. } else
  103. if (strcmp (value, "no") == 0) {
  104. main_config->logmode &= ~LOG_MODE_FILE;
  105. }
  106. }
  107. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  108. if (strcmp (value, "yes") == 0) {
  109. main_config->logmode |= LOG_MODE_SYSLOG;
  110. } else
  111. if (strcmp (value, "no") == 0) {
  112. main_config->logmode &= ~LOG_MODE_SYSLOG;
  113. }
  114. }
  115. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  116. if (strcmp (value, "yes") == 0) {
  117. main_config->logmode |= LOG_MODE_STDERR;
  118. } else
  119. if (strcmp (value, "no") == 0) {
  120. main_config->logmode &= ~LOG_MODE_STDERR;
  121. }
  122. }
  123. if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
  124. if (strcmp (value, "on") == 0) {
  125. main_config->logmode |= LOG_MODE_DEBUG;
  126. } else
  127. if (strcmp (value, "off") == 0) {
  128. main_config->logmode &= ~LOG_MODE_DEBUG;
  129. } else {
  130. goto parse_error;
  131. }
  132. }
  133. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  134. if (strcmp (value, "on") == 0) {
  135. main_config->logmode |= LOG_MODE_TIMESTAMP;
  136. } else
  137. if (strcmp (value, "off") == 0) {
  138. main_config->logmode &= ~LOG_MODE_TIMESTAMP;
  139. } else {
  140. goto parse_error;
  141. }
  142. }
  143. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  144. main_config->logfile = strdup (value);
  145. }
  146. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  147. if (strcmp (value, "on") == 0) {
  148. main_config->logmode |= LOG_MODE_FILELINE;
  149. } else
  150. if (strcmp (value, "off") == 0) {
  151. main_config->logmode &= ~LOG_MODE_FILELINE;
  152. } else {
  153. goto parse_error;
  154. }
  155. }
  156. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  157. if (strcmp (value, "daemon") == 0) {
  158. main_config->syslog_facility = LOG_DAEMON;
  159. } else
  160. if (strcmp (value, "local0") == 0) {
  161. main_config->syslog_facility = LOG_LOCAL0;
  162. } else
  163. if (strcmp (value, "local1") == 0) {
  164. main_config->syslog_facility = LOG_LOCAL1;
  165. } else
  166. if (strcmp (value, "local2") == 0) {
  167. main_config->syslog_facility = LOG_LOCAL2;
  168. } else
  169. if (strcmp (value, "local3") == 0) {
  170. main_config->syslog_facility = LOG_LOCAL3;
  171. } else
  172. if (strcmp (value, "local4") == 0) {
  173. main_config->syslog_facility = LOG_LOCAL4;
  174. } else
  175. if (strcmp (value, "local5") == 0) {
  176. main_config->syslog_facility = LOG_LOCAL5;
  177. } else
  178. if (strcmp (value, "local6") == 0) {
  179. main_config->syslog_facility = LOG_LOCAL6;
  180. } else
  181. if (strcmp (value, "local7") == 0) {
  182. main_config->syslog_facility = LOG_LOCAL7;
  183. } else {
  184. error_reason = "unknown syslog facility specified";
  185. goto parse_error;
  186. }
  187. }
  188. while ( objdb->object_find (object_service_handle,
  189. "logger",
  190. strlen ("logger"),
  191. &object_logger_handle) == 0) {
  192. struct logger_config *logger_tmp;
  193. logger_tmp = realloc (main_config->logger,
  194. sizeof(struct logger_config) * (main_config->loggers + 1));
  195. if (logger_tmp == NULL) {
  196. error_reason = "no more memory";
  197. goto other_error;
  198. }
  199. main_config->logger = logger_tmp;
  200. i = main_config->loggers;
  201. main_config->loggers++;
  202. memset(&main_config->logger[i], 0, sizeof(struct logger_config));
  203. if (!objdb_get_string (objdb, object_logger_handle, "ident", &value)) {
  204. main_config->logger[i].ident = value;
  205. }
  206. else {
  207. error_reason = "ident required for logger directive";
  208. goto parse_error;
  209. }
  210. if (!objdb_get_string (objdb, object_logger_handle, "debug", &value)) {
  211. if (strcmp (value, "on") == 0) {
  212. main_config->logger[i].level = LOG_LEVEL_DEBUG;
  213. } else
  214. if (strcmp (value, "off") == 0) {
  215. main_config->logger[i].level &= ~LOG_LEVEL_DEBUG;
  216. } else {
  217. goto parse_error;
  218. }
  219. }
  220. if (!objdb_get_string (objdb, object_logger_handle, "tags", &value)) {
  221. char *token = strtok (value, "|");
  222. while (token != NULL) {
  223. if (strcmp (token, "enter") == 0) {
  224. main_config->logger[i].tags |= TAG_ENTER;
  225. } else if (strcmp (token, "leave") == 0) {
  226. main_config->logger[i].tags |= TAG_LEAVE;
  227. } else if (strcmp (token, "trace1") == 0) {
  228. main_config->logger[i].tags |= TAG_TRACE1;
  229. } else if (strcmp (token, "trace2") == 0) {
  230. main_config->logger[i].tags |= TAG_TRACE2;
  231. } else if (strcmp (token, "trace3") == 0) {
  232. main_config->logger[i].tags |= TAG_TRACE3;
  233. } else if (strcmp (token, "trace4") == 0) {
  234. main_config->logger[i].tags |= TAG_TRACE4;
  235. } else if (strcmp (token, "trace5") == 0) {
  236. main_config->logger[i].tags |= TAG_TRACE5;
  237. } else if (strcmp (token, "trace6") == 0) {
  238. main_config->logger[i].tags |= TAG_TRACE6;
  239. } else if (strcmp (token, "trace7") == 0) {
  240. main_config->logger[i].tags |= TAG_TRACE7;
  241. } else if (strcmp (token, "trace8") == 0) {
  242. main_config->logger[i].tags |= TAG_TRACE8;
  243. } else {
  244. error_reason = "bad tags value";
  245. goto parse_error;
  246. }
  247. token = strtok(NULL, "|");
  248. }
  249. }
  250. }
  251. }
  252. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  253. if (objdb->object_find (
  254. OBJECT_PARENT_HANDLE,
  255. "aisexec",
  256. strlen ("aisexec"),
  257. &object_service_handle) == 0) {
  258. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  259. main_config->user = strdup(value);
  260. }
  261. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  262. main_config->group = strdup(value);
  263. }
  264. }
  265. /* Default user/group */
  266. if (!main_config->user)
  267. main_config->user = "ais";
  268. if (!main_config->group)
  269. main_config->group = "ais";
  270. if ((main_config->logmode & LOG_MODE_FILE) && main_config->logfile == 0) {
  271. error_reason = "logmode set to 'file' but no logfile specified";
  272. goto parse_error;
  273. }
  274. if (!main_config->syslog_facility)
  275. main_config->syslog_facility = LOG_DAEMON;
  276. return 0;
  277. other_error:
  278. sprintf (error_string_response,
  279. "error parsing config: %s.\n",
  280. error_reason);
  281. *error_string = error_string_response;
  282. return -1;
  283. parse_error:
  284. sprintf (error_string_response,
  285. "parse error in config: %s.\n",
  286. error_reason);
  287. *error_string = error_string_response;
  288. return (-1);
  289. }