mainconfig.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.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. int global_debug = 0;
  100. memset (main_config, 0, sizeof (struct main_config));
  101. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  102. main_config->logmode = LOG_MODE_FLUSH_AFTER_CONFIG;
  103. if (objdb->object_find (
  104. OBJECT_PARENT_HANDLE,
  105. "logging",
  106. strlen ("logging"),
  107. &object_service_handle) == 0) {
  108. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  109. if (strcmp (value, "yes") == 0) {
  110. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  111. } else
  112. if (strcmp (value, "no") == 0) {
  113. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  114. }
  115. }
  116. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  117. if (strcmp (value, "yes") == 0) {
  118. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG_THREADED;
  119. } else
  120. if (strcmp (value, "no") == 0) {
  121. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG_THREADED;
  122. }
  123. }
  124. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  125. if (strcmp (value, "yes") == 0) {
  126. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  127. } else
  128. if (strcmp (value, "no") == 0) {
  129. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  130. }
  131. }
  132. if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
  133. if (strcmp (value, "on") == 0) {
  134. global_debug = 1;
  135. } else
  136. if (strcmp (value, "off") == 0) {
  137. global_debug = 0;
  138. } else {
  139. goto parse_error;
  140. }
  141. }
  142. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  143. if (strcmp (value, "on") == 0) {
  144. main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
  145. } else
  146. if (strcmp (value, "off") == 0) {
  147. main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
  148. } else {
  149. goto parse_error;
  150. }
  151. }
  152. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  153. main_config->logfile = strdup (value);
  154. }
  155. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  156. if (strcmp (value, "on") == 0) {
  157. main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
  158. } else
  159. if (strcmp (value, "off") == 0) {
  160. main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
  161. } else {
  162. goto parse_error;
  163. }
  164. }
  165. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  166. main_config->syslog_facility = logsys_facility_id_get(value);
  167. if (main_config->syslog_facility < 0) {
  168. error_reason = "unknown syslog facility specified";
  169. goto parse_error;
  170. }
  171. }
  172. while (objdb->object_find (object_service_handle,
  173. "logger_subsys",
  174. strlen ("logger_subsys"),
  175. &object_logger_subsys_handle) == 0) {
  176. if (!objdb_get_string (objdb,
  177. object_logger_subsys_handle,
  178. "subsys", &value)) {
  179. strncpy (logsys_logger.subsys, value,
  180. sizeof (logsys_logger.subsys));
  181. }
  182. else {
  183. error_reason = "subsys required for logger directive";
  184. goto parse_error;
  185. }
  186. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  187. if (strcmp (value, "on") == 0) {
  188. logsys_logger.priority = LOG_LEVEL_DEBUG;
  189. } else
  190. if (strcmp (value, "off") == 0) {
  191. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  192. } else {
  193. goto parse_error;
  194. }
  195. }
  196. else {
  197. if (global_debug)
  198. logsys_logger.priority = LOG_LEVEL_DEBUG;
  199. }
  200. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  201. char *token = strtok (value, "|");
  202. while (token != NULL) {
  203. if (strcmp (token, "enter") == 0) {
  204. logsys_logger.tags |= LOGSYS_TAG_ENTER;
  205. } else if (strcmp (token, "leave") == 0) {
  206. logsys_logger.tags |= LOGSYS_TAG_LEAVE;
  207. } else if (strcmp (token, "trace1") == 0) {
  208. logsys_logger.tags |= LOGSYS_TAG_TRACE1;
  209. } else if (strcmp (token, "trace2") == 0) {
  210. logsys_logger.tags |= LOGSYS_TAG_TRACE2;
  211. } else if (strcmp (token, "trace3") == 0) {
  212. logsys_logger.tags |= LOGSYS_TAG_TRACE3;
  213. } else if (strcmp (token, "trace4") == 0) {
  214. logsys_logger.tags |= LOGSYS_TAG_TRACE4;
  215. } else if (strcmp (token, "trace5") == 0) {
  216. logsys_logger.tags |= LOGSYS_TAG_TRACE5;
  217. } else if (strcmp (token, "trace6") == 0) {
  218. logsys_logger.tags |= LOGSYS_TAG_TRACE6;
  219. } else if (strcmp (token, "trace7") == 0) {
  220. logsys_logger.tags |= LOGSYS_TAG_TRACE7;
  221. } else if (strcmp (token, "trace8") == 0) {
  222. logsys_logger.tags |= LOGSYS_TAG_TRACE8;
  223. } else {
  224. error_reason = "bad tags value";
  225. goto parse_error;
  226. }
  227. token = strtok(NULL, "|");
  228. }
  229. }
  230. /*
  231. * set individual logger configurations
  232. */
  233. logsys_config_subsys_set (
  234. logsys_logger.subsys,
  235. logsys_logger.tags,
  236. logsys_logger.priority);
  237. }
  238. }
  239. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  240. if (objdb->object_find (
  241. OBJECT_PARENT_HANDLE,
  242. "aisexec",
  243. strlen ("aisexec"),
  244. &object_service_handle) == 0) {
  245. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  246. main_config->user = strdup(value);
  247. }
  248. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  249. main_config->group = strdup(value);
  250. }
  251. }
  252. /* Default user/group */
  253. if (!main_config->user)
  254. main_config->user = "ais";
  255. if (!main_config->group)
  256. main_config->group = "ais";
  257. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  258. (main_config->logfile == NULL)) {
  259. error_reason = "logmode set to 'file' but no logfile specified";
  260. goto parse_error;
  261. }
  262. if (main_config->syslog_facility == 0)
  263. main_config->syslog_facility = LOG_DAEMON;
  264. return 0;
  265. parse_error:
  266. sprintf (error_string_response,
  267. "parse error in config: %s.\n",
  268. error_reason);
  269. *error_string = error_string_response;
  270. return (-1);
  271. }