mainconfig.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <corosync/saAis.h>
  44. #include <corosync/list.h>
  45. #include <corosync/totem/totem.h>
  46. #include <corosync/engine/logsys.h>
  47. #include "util.h"
  48. #include "mainconfig.h"
  49. #include "mempool.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. memset (main_config, 0, sizeof (struct main_config));
  102. objdb->object_find_create (
  103. OBJECT_PARENT_HANDLE,
  104. "logging",
  105. strlen ("logging"),
  106. &object_find_handle);
  107. main_config->logmode = LOG_MODE_THREADED | LOG_MODE_FORK;
  108. if (objdb->object_find_next (
  109. object_find_handle,
  110. &object_service_handle) == 0) {
  111. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  112. if (strcmp (value, "yes") == 0) {
  113. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  114. } else
  115. if (strcmp (value, "no") == 0) {
  116. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  117. }
  118. }
  119. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  120. if (strcmp (value, "yes") == 0) {
  121. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG;
  122. } else
  123. if (strcmp (value, "no") == 0) {
  124. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG;
  125. }
  126. }
  127. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  128. if (strcmp (value, "yes") == 0) {
  129. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  130. } else
  131. if (strcmp (value, "no") == 0) {
  132. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  133. }
  134. }
  135. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  136. /* todo change format string
  137. if (strcmp (value, "on") == 0) {
  138. main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
  139. } else
  140. if (strcmp (value, "off") == 0) {
  141. main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
  142. } else {
  143. goto parse_error;
  144. }
  145. */
  146. }
  147. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  148. main_config->logfile = strdup (value);
  149. }
  150. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  151. /* TODO
  152. if (strcmp (value, "on") == 0) {
  153. main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
  154. } else
  155. if (strcmp (value, "off") == 0) {
  156. main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
  157. } else {
  158. goto parse_error;
  159. }
  160. */
  161. }
  162. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  163. if (strcmp (value, "daemon") == 0) {
  164. main_config->syslog_facility = LOG_DAEMON;
  165. } else
  166. if (strcmp (value, "local0") == 0) {
  167. main_config->syslog_facility = LOG_LOCAL0;
  168. } else
  169. if (strcmp (value, "local1") == 0) {
  170. main_config->syslog_facility = LOG_LOCAL1;
  171. } else
  172. if (strcmp (value, "local2") == 0) {
  173. main_config->syslog_facility = LOG_LOCAL2;
  174. } else
  175. if (strcmp (value, "local3") == 0) {
  176. main_config->syslog_facility = LOG_LOCAL3;
  177. } else
  178. if (strcmp (value, "local4") == 0) {
  179. main_config->syslog_facility = LOG_LOCAL4;
  180. } else
  181. if (strcmp (value, "local5") == 0) {
  182. main_config->syslog_facility = LOG_LOCAL5;
  183. } else
  184. if (strcmp (value, "local6") == 0) {
  185. main_config->syslog_facility = LOG_LOCAL6;
  186. } else
  187. if (strcmp (value, "local7") == 0) {
  188. main_config->syslog_facility = LOG_LOCAL7;
  189. } else {
  190. error_reason = "unknown syslog facility specified";
  191. goto parse_error;
  192. }
  193. }
  194. objdb->object_find_create (
  195. object_service_handle,
  196. "logger_subsys",
  197. strlen ("logger_subsys"),
  198. &object_find_logsys_handle);
  199. while (objdb->object_find_next (
  200. object_find_logsys_handle,
  201. &object_logger_subsys_handle) == 0) {
  202. if (!objdb_get_string (objdb,
  203. object_logger_subsys_handle,
  204. "subsys", &value)) {
  205. strncpy (logsys_logger.subsys, value,
  206. sizeof (logsys_logger.subsys));
  207. }
  208. else {
  209. error_reason = "subsys required for logger directive";
  210. goto parse_error;
  211. }
  212. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  213. if (strcmp (value, "on") == 0) {
  214. logsys_logger.priority = LOG_LEVEL_DEBUG;
  215. } else
  216. if (strcmp (value, "off") == 0) {
  217. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  218. } else {
  219. goto parse_error;
  220. }
  221. }
  222. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  223. char *token = strtok (value, "|");
  224. while (token != NULL) {
  225. if (strcmp (token, "enter") == 0) {
  226. logsys_logger.tags |= LOGSYS_TAG_ENTER;
  227. } else if (strcmp (token, "leave") == 0) {
  228. logsys_logger.tags |= LOGSYS_TAG_LEAVE;
  229. } else if (strcmp (token, "trace1") == 0) {
  230. logsys_logger.tags |= LOGSYS_TAG_TRACE1;
  231. } else if (strcmp (token, "trace2") == 0) {
  232. logsys_logger.tags |= LOGSYS_TAG_TRACE2;
  233. } else if (strcmp (token, "trace3") == 0) {
  234. logsys_logger.tags |= LOGSYS_TAG_TRACE3;
  235. } else if (strcmp (token, "trace4") == 0) {
  236. logsys_logger.tags |= LOGSYS_TAG_TRACE4;
  237. } else if (strcmp (token, "trace5") == 0) {
  238. logsys_logger.tags |= LOGSYS_TAG_TRACE5;
  239. } else if (strcmp (token, "trace6") == 0) {
  240. logsys_logger.tags |= LOGSYS_TAG_TRACE6;
  241. } else if (strcmp (token, "trace7") == 0) {
  242. logsys_logger.tags |= LOGSYS_TAG_TRACE7;
  243. } else if (strcmp (token, "trace8") == 0) {
  244. logsys_logger.tags |= LOGSYS_TAG_TRACE8;
  245. } else {
  246. error_reason = "bad tags value";
  247. goto parse_error;
  248. }
  249. token = strtok(NULL, "|");
  250. }
  251. }
  252. /*
  253. * set individual logger configurations
  254. */
  255. logsys_config_subsys_set (
  256. logsys_logger.subsys,
  257. logsys_logger.tags,
  258. logsys_logger.priority);
  259. }
  260. objdb->object_find_destroy (object_find_logsys_handle);
  261. }
  262. objdb->object_find_destroy (object_find_handle);
  263. objdb->object_find_create (
  264. OBJECT_PARENT_HANDLE,
  265. "aisexec",
  266. strlen ("aisexec"),
  267. &object_find_handle);
  268. if (objdb->object_find_next (
  269. object_find_handle,
  270. &object_service_handle) == 0) {
  271. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  272. main_config->user = strdup(value);
  273. }
  274. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  275. main_config->group = strdup(value);
  276. }
  277. }
  278. objdb->object_find_destroy (object_find_handle);
  279. /* Default user/group */
  280. if (!main_config->user)
  281. main_config->user = "ais";
  282. if (!main_config->group)
  283. main_config->group = "ais";
  284. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  285. (main_config->logfile == NULL)) {
  286. error_reason = "logmode set to 'file' but no logfile specified";
  287. goto parse_error;
  288. }
  289. if (main_config->syslog_facility == 0)
  290. main_config->syslog_facility = LOG_DAEMON;
  291. return 0;
  292. parse_error:
  293. sprintf (error_string_response,
  294. "parse error in config: %s.\n",
  295. error_reason);
  296. *error_string = error_string_response;
  297. return (-1);
  298. }