mainconfig.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <assert.h>
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include "../include/saAis.h"
  44. #include "../include/list.h"
  45. #include "util.h"
  46. #include "mainconfig.h"
  47. #include "mempool.h"
  48. #include "logsys.h"
  49. #include "totem.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 corosync_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. unsigned int object_find_handle;
  100. unsigned int object_find_logsys_handle;
  101. int global_debug = 0;
  102. memset (main_config, 0, sizeof (struct main_config));
  103. objdb->object_find_create (
  104. OBJECT_PARENT_HANDLE,
  105. "logging",
  106. strlen ("logging"),
  107. &object_find_handle);
  108. main_config->logmode = LOG_MODE_FLUSH_AFTER_CONFIG;
  109. if (objdb->object_find_next (
  110. object_find_handle,
  111. &object_service_handle) == 0) {
  112. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  113. if (strcmp (value, "yes") == 0) {
  114. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  115. } else
  116. if (strcmp (value, "no") == 0) {
  117. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  118. }
  119. }
  120. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  121. if (strcmp (value, "yes") == 0) {
  122. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG_THREADED;
  123. } else
  124. if (strcmp (value, "no") == 0) {
  125. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG_THREADED;
  126. }
  127. }
  128. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  129. if (strcmp (value, "yes") == 0) {
  130. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  131. } else
  132. if (strcmp (value, "no") == 0) {
  133. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  134. }
  135. }
  136. if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
  137. if (strcmp (value, "on") == 0) {
  138. global_debug = 1;
  139. } else
  140. if (strcmp (value, "off") == 0) {
  141. global_debug = 0;
  142. } else {
  143. goto parse_error;
  144. }
  145. }
  146. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  147. if (strcmp (value, "on") == 0) {
  148. main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
  149. } else
  150. if (strcmp (value, "off") == 0) {
  151. main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
  152. } else {
  153. goto parse_error;
  154. }
  155. }
  156. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  157. main_config->logfile = strdup (value);
  158. }
  159. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  160. if (strcmp (value, "on") == 0) {
  161. main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
  162. } else
  163. if (strcmp (value, "off") == 0) {
  164. main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
  165. } else {
  166. goto parse_error;
  167. }
  168. }
  169. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  170. if (strcmp (value, "daemon") == 0) {
  171. main_config->syslog_facility = LOG_DAEMON;
  172. } else
  173. if (strcmp (value, "local0") == 0) {
  174. main_config->syslog_facility = LOG_LOCAL0;
  175. } else
  176. if (strcmp (value, "local1") == 0) {
  177. main_config->syslog_facility = LOG_LOCAL1;
  178. } else
  179. if (strcmp (value, "local2") == 0) {
  180. main_config->syslog_facility = LOG_LOCAL2;
  181. } else
  182. if (strcmp (value, "local3") == 0) {
  183. main_config->syslog_facility = LOG_LOCAL3;
  184. } else
  185. if (strcmp (value, "local4") == 0) {
  186. main_config->syslog_facility = LOG_LOCAL4;
  187. } else
  188. if (strcmp (value, "local5") == 0) {
  189. main_config->syslog_facility = LOG_LOCAL5;
  190. } else
  191. if (strcmp (value, "local6") == 0) {
  192. main_config->syslog_facility = LOG_LOCAL6;
  193. } else
  194. if (strcmp (value, "local7") == 0) {
  195. main_config->syslog_facility = LOG_LOCAL7;
  196. } else {
  197. error_reason = "unknown syslog facility specified";
  198. goto parse_error;
  199. }
  200. }
  201. objdb->object_find_create (
  202. object_service_handle,
  203. "logger_subsys",
  204. strlen ("logger_subsys"),
  205. &object_find_logsys_handle);
  206. while (objdb->object_find_next (
  207. object_find_logsys_handle,
  208. &object_logger_subsys_handle) == 0) {
  209. if (!objdb_get_string (objdb,
  210. object_logger_subsys_handle,
  211. "subsys", &value)) {
  212. strncpy (logsys_logger.subsys, value,
  213. sizeof (logsys_logger.subsys));
  214. }
  215. else {
  216. error_reason = "subsys required for logger directive";
  217. goto parse_error;
  218. }
  219. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  220. if (strcmp (value, "on") == 0) {
  221. logsys_logger.priority = LOG_LEVEL_DEBUG;
  222. } else
  223. if (strcmp (value, "off") == 0) {
  224. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  225. } else {
  226. goto parse_error;
  227. }
  228. }
  229. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  230. char *token = strtok (value, "|");
  231. while (token != NULL) {
  232. if (strcmp (token, "enter") == 0) {
  233. logsys_logger.tags |= LOGSYS_TAG_ENTER;
  234. } else if (strcmp (token, "leave") == 0) {
  235. logsys_logger.tags |= LOGSYS_TAG_LEAVE;
  236. } else if (strcmp (token, "trace1") == 0) {
  237. logsys_logger.tags |= LOGSYS_TAG_TRACE1;
  238. } else if (strcmp (token, "trace2") == 0) {
  239. logsys_logger.tags |= LOGSYS_TAG_TRACE2;
  240. } else if (strcmp (token, "trace3") == 0) {
  241. logsys_logger.tags |= LOGSYS_TAG_TRACE3;
  242. } else if (strcmp (token, "trace4") == 0) {
  243. logsys_logger.tags |= LOGSYS_TAG_TRACE4;
  244. } else if (strcmp (token, "trace5") == 0) {
  245. logsys_logger.tags |= LOGSYS_TAG_TRACE5;
  246. } else if (strcmp (token, "trace6") == 0) {
  247. logsys_logger.tags |= LOGSYS_TAG_TRACE6;
  248. } else if (strcmp (token, "trace7") == 0) {
  249. logsys_logger.tags |= LOGSYS_TAG_TRACE7;
  250. } else if (strcmp (token, "trace8") == 0) {
  251. logsys_logger.tags |= LOGSYS_TAG_TRACE8;
  252. } else {
  253. error_reason = "bad tags value";
  254. goto parse_error;
  255. }
  256. token = strtok(NULL, "|");
  257. }
  258. }
  259. /*
  260. * set individual logger configurations
  261. */
  262. logsys_config_subsys_set (
  263. logsys_logger.subsys,
  264. logsys_logger.tags,
  265. logsys_logger.priority);
  266. }
  267. objdb->object_find_destroy (object_find_logsys_handle);
  268. }
  269. objdb->object_find_destroy (object_find_handle);
  270. objdb->object_find_create (
  271. OBJECT_PARENT_HANDLE,
  272. "aisexec",
  273. strlen ("aisexec"),
  274. &object_find_handle);
  275. if (objdb->object_find_next (
  276. object_find_handle,
  277. &object_service_handle) == 0) {
  278. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  279. main_config->user = strdup(value);
  280. }
  281. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  282. main_config->group = strdup(value);
  283. }
  284. }
  285. objdb->object_find_destroy (object_find_handle);
  286. /* Default user/group */
  287. if (!main_config->user)
  288. main_config->user = "ais";
  289. if (!main_config->group)
  290. main_config->group = "ais";
  291. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  292. (main_config->logfile == NULL)) {
  293. error_reason = "logmode set to 'file' but no logfile specified";
  294. goto parse_error;
  295. }
  296. if (main_config->syslog_facility == 0)
  297. main_config->syslog_facility = LOG_DAEMON;
  298. return 0;
  299. parse_error:
  300. sprintf (error_string_response,
  301. "parse error in config: %s.\n",
  302. error_reason);
  303. *error_string = error_string_response;
  304. return (-1);
  305. }