mainconfig.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. while ( objdb->object_find (object_service_handle,
  157. "logger",
  158. strlen ("logger"),
  159. &object_logger_handle) == 0) {
  160. main_config->logger =
  161. realloc(main_config->logger,
  162. sizeof(struct logger_config) *
  163. (main_config->loggers + 1));
  164. i = main_config->loggers;
  165. main_config->loggers++;
  166. memset(&main_config->logger[i], 0, sizeof(struct logger_config));
  167. if (!objdb_get_string (objdb, object_logger_handle, "ident", &value)) {
  168. main_config->logger[i].ident = value;
  169. }
  170. else {
  171. error_reason = "ident required for logger directive";
  172. goto parse_error;
  173. }
  174. if (!objdb_get_string (objdb, object_logger_handle, "debug", &value)) {
  175. if (strcmp (value, "on") == 0) {
  176. main_config->logger[i].level = LOG_LEVEL_DEBUG;
  177. } else
  178. if (strcmp (value, "off") == 0) {
  179. main_config->logger[i].level &= ~LOG_LEVEL_DEBUG;
  180. } else {
  181. goto parse_error;
  182. }
  183. }
  184. if (!objdb_get_string (objdb, object_logger_handle, "tags", &value)) {
  185. char *token = strtok (value, "|");
  186. while (token != NULL) {
  187. if (strcmp (token, "enter") == 0) {
  188. main_config->logger[i].tags |= TAG_ENTER;
  189. } else if (strcmp (token, "leave") == 0) {
  190. main_config->logger[i].tags |= TAG_LEAVE;
  191. } else if (strcmp (token, "trace1") == 0) {
  192. main_config->logger[i].tags |= TAG_TRACE1;
  193. } else if (strcmp (token, "trace2") == 0) {
  194. main_config->logger[i].tags |= TAG_TRACE2;
  195. } else if (strcmp (token, "trace3") == 0) {
  196. main_config->logger[i].tags |= TAG_TRACE3;
  197. } else if (strcmp (token, "trace4") == 0) {
  198. main_config->logger[i].tags |= TAG_TRACE4;
  199. } else if (strcmp (token, "trace5") == 0) {
  200. main_config->logger[i].tags |= TAG_TRACE5;
  201. } else if (strcmp (token, "trace6") == 0) {
  202. main_config->logger[i].tags |= TAG_TRACE6;
  203. } else if (strcmp (token, "trace7") == 0) {
  204. main_config->logger[i].tags |= TAG_TRACE7;
  205. } else if (strcmp (token, "trace8") == 0) {
  206. main_config->logger[i].tags |= TAG_TRACE8;
  207. } else {
  208. error_reason = "bad tags value";
  209. goto parse_error;
  210. }
  211. token = strtok(NULL, "|");
  212. }
  213. }
  214. }
  215. }
  216. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  217. if (objdb->object_find (
  218. OBJECT_PARENT_HANDLE,
  219. "aisexec",
  220. strlen ("aisexec"),
  221. &object_service_handle) == 0) {
  222. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  223. main_config->user = strdup(value);
  224. }
  225. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  226. main_config->group = strdup(value);
  227. }
  228. }
  229. /* Default user/group */
  230. if (!main_config->user)
  231. main_config->user = "ais";
  232. if (!main_config->group)
  233. main_config->group = "ais";
  234. if ((main_config->logmode & LOG_MODE_FILE) && main_config->logfile == 0) {
  235. error_reason = "logmode set to 'file' but no logfile specified";
  236. goto parse_error;
  237. }
  238. return 0;
  239. parse_error:
  240. sprintf (error_string_response,
  241. "parse error in config: %s.\n",
  242. error_reason);
  243. *error_string = error_string_response;
  244. return (-1);
  245. }