mainconfig.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. 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. main_config->syslog_facility = logsys_facility_id_get(value);
  166. if (main_config->syslog_facility < 0) {
  167. error_reason = "unknown syslog facility specified";
  168. goto parse_error;
  169. }
  170. }
  171. while (objdb->object_find (object_service_handle,
  172. "logger_subsys",
  173. strlen ("logger_subsys"),
  174. &object_logger_subsys_handle) == 0) {
  175. if (!objdb_get_string (objdb,
  176. object_logger_subsys_handle,
  177. "subsys", &value)) {
  178. strncpy (logsys_logger.subsys, value,
  179. sizeof (logsys_logger.subsys));
  180. }
  181. else {
  182. error_reason = "subsys required for logger directive";
  183. goto parse_error;
  184. }
  185. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  186. if (strcmp (value, "on") == 0) {
  187. logsys_logger.priority = LOG_LEVEL_DEBUG;
  188. } else
  189. if (strcmp (value, "off") == 0) {
  190. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  191. } else {
  192. goto parse_error;
  193. }
  194. }
  195. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  196. char *token = strtok (value, "|");
  197. while (token != NULL) {
  198. if (strcmp (token, "enter") == 0) {
  199. logsys_logger.tags |= LOGSYS_TAG_ENTER;
  200. } else if (strcmp (token, "leave") == 0) {
  201. logsys_logger.tags |= LOGSYS_TAG_LEAVE;
  202. } else if (strcmp (token, "trace1") == 0) {
  203. logsys_logger.tags |= LOGSYS_TAG_TRACE1;
  204. } else if (strcmp (token, "trace2") == 0) {
  205. logsys_logger.tags |= LOGSYS_TAG_TRACE2;
  206. } else if (strcmp (token, "trace3") == 0) {
  207. logsys_logger.tags |= LOGSYS_TAG_TRACE3;
  208. } else if (strcmp (token, "trace4") == 0) {
  209. logsys_logger.tags |= LOGSYS_TAG_TRACE4;
  210. } else if (strcmp (token, "trace5") == 0) {
  211. logsys_logger.tags |= LOGSYS_TAG_TRACE5;
  212. } else if (strcmp (token, "trace6") == 0) {
  213. logsys_logger.tags |= LOGSYS_TAG_TRACE6;
  214. } else if (strcmp (token, "trace7") == 0) {
  215. logsys_logger.tags |= LOGSYS_TAG_TRACE7;
  216. } else if (strcmp (token, "trace8") == 0) {
  217. logsys_logger.tags |= LOGSYS_TAG_TRACE8;
  218. } else {
  219. error_reason = "bad tags value";
  220. goto parse_error;
  221. }
  222. token = strtok(NULL, "|");
  223. }
  224. }
  225. /*
  226. * set individual logger configurations
  227. */
  228. logsys_config_subsys_set (
  229. logsys_logger.subsys,
  230. logsys_logger.tags,
  231. logsys_logger.priority);
  232. }
  233. }
  234. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  235. if (objdb->object_find (
  236. OBJECT_PARENT_HANDLE,
  237. "aisexec",
  238. strlen ("aisexec"),
  239. &object_service_handle) == 0) {
  240. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  241. main_config->user = strdup(value);
  242. }
  243. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  244. main_config->group = strdup(value);
  245. }
  246. }
  247. /* Default user/group */
  248. if (!main_config->user)
  249. main_config->user = "ais";
  250. if (!main_config->group)
  251. main_config->group = "ais";
  252. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  253. (main_config->logfile == NULL)) {
  254. error_reason = "logmode set to 'file' but no logfile specified";
  255. goto parse_error;
  256. }
  257. if (main_config->syslog_facility == 0)
  258. main_config->syslog_facility = LOG_DAEMON;
  259. return 0;
  260. parse_error:
  261. sprintf (error_string_response,
  262. "parse error in config: %s.\n",
  263. error_reason);
  264. *error_string = error_string_response;
  265. return (-1);
  266. }