mainconfig.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "logsys.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 (
  53. struct objdb_iface_ver0 *objdb,
  54. unsigned int object_service_handle,
  55. char *key, char **value)
  56. {
  57. int res;
  58. *value = NULL;
  59. if ( !(res = objdb->object_key_get (object_service_handle,
  60. key,
  61. strlen (key),
  62. (void *)value,
  63. NULL))) {
  64. if (*value) {
  65. return 0;
  66. }
  67. }
  68. return -1;
  69. }
  70. static inline void objdb_get_int (
  71. struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  72. char *key, unsigned int *intvalue)
  73. {
  74. char *value = NULL;
  75. if (!objdb->object_key_get (object_service_handle,
  76. key,
  77. strlen (key),
  78. (void *)&value,
  79. NULL)) {
  80. if (value) {
  81. *intvalue = atoi(value);
  82. }
  83. }
  84. }
  85. static struct logsys_config_struct {
  86. char subsys[6];
  87. unsigned int priority;
  88. unsigned int tags;
  89. } logsys_logger;
  90. int openais_main_config_read (
  91. struct objdb_iface_ver0 *objdb,
  92. char **error_string,
  93. struct main_config *main_config)
  94. {
  95. unsigned int object_service_handle;
  96. unsigned int object_logger_subsys_handle;
  97. char *value;
  98. char *error_reason = error_string_response;
  99. memset (main_config, 0, sizeof (struct main_config));
  100. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  101. main_config->logmode = LOG_MODE_FLUSH_AFTER_CONFIG;
  102. if (objdb->object_find (
  103. OBJECT_PARENT_HANDLE,
  104. "logging",
  105. strlen ("logging"),
  106. &object_service_handle) == 0) {
  107. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  108. if (strcmp (value, "yes") == 0) {
  109. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  110. } else
  111. if (strcmp (value, "no") == 0) {
  112. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  113. }
  114. }
  115. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  116. if (strcmp (value, "yes") == 0) {
  117. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG_THREADED;
  118. } else
  119. if (strcmp (value, "no") == 0) {
  120. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG_THREADED;
  121. }
  122. }
  123. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  124. if (strcmp (value, "yes") == 0) {
  125. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  126. } else
  127. if (strcmp (value, "no") == 0) {
  128. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  129. }
  130. }
  131. if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
  132. if (strcmp (value, "on") == 0) {
  133. main_config->logmode |= LOG_MODE_DISPLAY_DEBUG;
  134. } else
  135. if (strcmp (value, "off") == 0) {
  136. main_config->logmode &= ~LOG_MODE_DISPLAY_DEBUG;
  137. } else {
  138. goto parse_error;
  139. }
  140. }
  141. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  142. if (strcmp (value, "on") == 0) {
  143. main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
  144. } else
  145. if (strcmp (value, "off") == 0) {
  146. main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
  147. } else {
  148. goto parse_error;
  149. }
  150. }
  151. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  152. main_config->logfile = strdup (value);
  153. }
  154. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  155. if (strcmp (value, "on") == 0) {
  156. main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
  157. } else
  158. if (strcmp (value, "off") == 0) {
  159. main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
  160. } else {
  161. goto parse_error;
  162. }
  163. }
  164. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  165. if (strcmp (value, "daemon") == 0) {
  166. main_config->syslog_facility = LOG_DAEMON;
  167. } else
  168. if (strcmp (value, "local0") == 0) {
  169. main_config->syslog_facility = LOG_LOCAL0;
  170. } else
  171. if (strcmp (value, "local1") == 0) {
  172. main_config->syslog_facility = LOG_LOCAL1;
  173. } else
  174. if (strcmp (value, "local2") == 0) {
  175. main_config->syslog_facility = LOG_LOCAL2;
  176. } else
  177. if (strcmp (value, "local3") == 0) {
  178. main_config->syslog_facility = LOG_LOCAL3;
  179. } else
  180. if (strcmp (value, "local4") == 0) {
  181. main_config->syslog_facility = LOG_LOCAL4;
  182. } else
  183. if (strcmp (value, "local5") == 0) {
  184. main_config->syslog_facility = LOG_LOCAL5;
  185. } else
  186. if (strcmp (value, "local6") == 0) {
  187. main_config->syslog_facility = LOG_LOCAL6;
  188. } else
  189. if (strcmp (value, "local7") == 0) {
  190. main_config->syslog_facility = LOG_LOCAL7;
  191. } else {
  192. error_reason = "unknown syslog facility specified";
  193. goto parse_error;
  194. }
  195. }
  196. while (objdb->object_find (object_service_handle,
  197. "logger_subsys",
  198. strlen ("logger_subsys"),
  199. &object_logger_subsys_handle) == 0) {
  200. if (!objdb_get_string (objdb,
  201. object_logger_subsys_handle,
  202. "subsys", &value)) {
  203. strncpy (logsys_logger.subsys, value,
  204. sizeof (logsys_logger.subsys));
  205. }
  206. else {
  207. error_reason = "subsys required for logger directive";
  208. goto parse_error;
  209. }
  210. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  211. if (strcmp (value, "on") == 0) {
  212. logsys_logger.priority = LOG_LEVEL_DEBUG;
  213. } else
  214. if (strcmp (value, "off") == 0) {
  215. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  216. } else {
  217. goto parse_error;
  218. }
  219. }
  220. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  221. char *token = strtok (value, "|");
  222. while (token != NULL) {
  223. if (strcmp (token, "enter") == 0) {
  224. logsys_logger.tags |= LOGSYS_TAG_ENTER;
  225. } else if (strcmp (token, "leave") == 0) {
  226. logsys_logger.tags |= LOGSYS_TAG_LEAVE;
  227. } else if (strcmp (token, "trace1") == 0) {
  228. logsys_logger.tags |= LOGSYS_TAG_TRACE1;
  229. } else if (strcmp (token, "trace2") == 0) {
  230. logsys_logger.tags |= LOGSYS_TAG_TRACE2;
  231. } else if (strcmp (token, "trace3") == 0) {
  232. logsys_logger.tags |= LOGSYS_TAG_TRACE3;
  233. } else if (strcmp (token, "trace4") == 0) {
  234. logsys_logger.tags |= LOGSYS_TAG_TRACE4;
  235. } else if (strcmp (token, "trace5") == 0) {
  236. logsys_logger.tags |= LOGSYS_TAG_TRACE5;
  237. } else if (strcmp (token, "trace6") == 0) {
  238. logsys_logger.tags |= LOGSYS_TAG_TRACE6;
  239. } else if (strcmp (token, "trace7") == 0) {
  240. logsys_logger.tags |= LOGSYS_TAG_TRACE7;
  241. } else if (strcmp (token, "trace8") == 0) {
  242. logsys_logger.tags |= LOGSYS_TAG_TRACE8;
  243. } else {
  244. error_reason = "bad tags value";
  245. goto parse_error;
  246. }
  247. token = strtok(NULL, "|");
  248. }
  249. }
  250. /*
  251. * set individual logger configurations
  252. */
  253. logsys_config_subsys_set (
  254. logsys_logger.subsys,
  255. logsys_logger.tags,
  256. logsys_logger.priority);
  257. }
  258. }
  259. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  260. if (objdb->object_find (
  261. OBJECT_PARENT_HANDLE,
  262. "aisexec",
  263. strlen ("aisexec"),
  264. &object_service_handle) == 0) {
  265. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  266. main_config->user = strdup(value);
  267. }
  268. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  269. main_config->group = strdup(value);
  270. }
  271. }
  272. /* Default user/group */
  273. if (!main_config->user)
  274. main_config->user = "ais";
  275. if (!main_config->group)
  276. main_config->group = "ais";
  277. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  278. (main_config->logfile == NULL)) {
  279. error_reason = "logmode set to 'file' but no logfile specified";
  280. goto parse_error;
  281. }
  282. if (main_config->syslog_facility == 0)
  283. main_config->syslog_facility = LOG_DAEMON;
  284. return 0;
  285. parse_error:
  286. sprintf (error_string_response,
  287. "parse error in config: %s.\n",
  288. error_reason);
  289. *error_string = error_string_response;
  290. return (-1);
  291. }