mainconfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/corotypes.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. static struct objdb_iface_ver0 *global_objdb;
  52. static void add_logsys_config_notification(
  53. struct objdb_iface_ver0 *objdb,
  54. struct main_config *main_config);
  55. /* This just makes the code below a little neater */
  56. static inline int objdb_get_string (
  57. struct objdb_iface_ver0 *objdb,
  58. unsigned int object_service_handle,
  59. char *key, char **value)
  60. {
  61. int res;
  62. *value = NULL;
  63. if ( !(res = objdb->object_key_get (object_service_handle,
  64. key,
  65. strlen (key),
  66. (void *)value,
  67. NULL))) {
  68. if (*value) {
  69. return 0;
  70. }
  71. }
  72. return -1;
  73. }
  74. static inline void objdb_get_int (
  75. struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  76. char *key, unsigned int *intvalue)
  77. {
  78. char *value = NULL;
  79. if (!objdb->object_key_get (object_service_handle,
  80. key,
  81. strlen (key),
  82. (void *)&value,
  83. NULL)) {
  84. if (value) {
  85. *intvalue = atoi(value);
  86. }
  87. }
  88. }
  89. static struct logsys_config_struct {
  90. char subsys[6];
  91. unsigned int priority;
  92. unsigned int tags;
  93. } logsys_logger;
  94. int corosync_main_config_read_logging (
  95. struct objdb_iface_ver0 *objdb,
  96. char **error_string,
  97. struct main_config *main_config)
  98. {
  99. unsigned int object_service_handle;
  100. unsigned int object_logger_subsys_handle;
  101. char *value;
  102. char *error_reason = error_string_response;
  103. unsigned int object_find_handle;
  104. unsigned int object_find_logsys_handle;
  105. objdb->object_find_create (
  106. OBJECT_PARENT_HANDLE,
  107. "logging",
  108. strlen ("logging"),
  109. &object_find_handle);
  110. main_config->logmode = LOG_MODE_THREADED | LOG_MODE_FORK;
  111. if (objdb->object_find_next (
  112. object_find_handle,
  113. &object_service_handle) == 0) {
  114. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  115. if (strcmp (value, "yes") == 0) {
  116. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  117. } else
  118. if (strcmp (value, "no") == 0) {
  119. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  120. }
  121. }
  122. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  123. if (strcmp (value, "yes") == 0) {
  124. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG;
  125. } else
  126. if (strcmp (value, "no") == 0) {
  127. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG;
  128. }
  129. }
  130. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  131. if (strcmp (value, "yes") == 0) {
  132. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  133. } else
  134. if (strcmp (value, "no") == 0) {
  135. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  136. }
  137. }
  138. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  139. /* todo change format string
  140. if (strcmp (value, "on") == 0) {
  141. main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
  142. } else
  143. if (strcmp (value, "off") == 0) {
  144. main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
  145. } else {
  146. goto parse_error;
  147. }
  148. */
  149. }
  150. /* free old string on reload */
  151. if (main_config->logfile) {
  152. free(main_config->logfile);
  153. main_config->logfile = NULL;
  154. }
  155. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  156. main_config->logfile = strdup (value);
  157. }
  158. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  159. /* TODO
  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. }
  170. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  171. if (strcmp (value, "daemon") == 0) {
  172. main_config->syslog_facility = LOG_DAEMON;
  173. } else
  174. if (strcmp (value, "local0") == 0) {
  175. main_config->syslog_facility = LOG_LOCAL0;
  176. } else
  177. if (strcmp (value, "local1") == 0) {
  178. main_config->syslog_facility = LOG_LOCAL1;
  179. } else
  180. if (strcmp (value, "local2") == 0) {
  181. main_config->syslog_facility = LOG_LOCAL2;
  182. } else
  183. if (strcmp (value, "local3") == 0) {
  184. main_config->syslog_facility = LOG_LOCAL3;
  185. } else
  186. if (strcmp (value, "local4") == 0) {
  187. main_config->syslog_facility = LOG_LOCAL4;
  188. } else
  189. if (strcmp (value, "local5") == 0) {
  190. main_config->syslog_facility = LOG_LOCAL5;
  191. } else
  192. if (strcmp (value, "local6") == 0) {
  193. main_config->syslog_facility = LOG_LOCAL6;
  194. } else
  195. if (strcmp (value, "local7") == 0) {
  196. main_config->syslog_facility = LOG_LOCAL7;
  197. } else {
  198. error_reason = "unknown syslog facility specified";
  199. goto parse_error;
  200. }
  201. }
  202. objdb->object_find_create (
  203. object_service_handle,
  204. "logger_subsys",
  205. strlen ("logger_subsys"),
  206. &object_find_logsys_handle);
  207. while (objdb->object_find_next (
  208. object_find_logsys_handle,
  209. &object_logger_subsys_handle) == 0) {
  210. if (!objdb_get_string (objdb,
  211. object_logger_subsys_handle,
  212. "subsys", &value)) {
  213. strncpy (logsys_logger.subsys, value,
  214. sizeof (logsys_logger.subsys));
  215. }
  216. else {
  217. error_reason = "subsys required for logger directive";
  218. goto parse_error;
  219. }
  220. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  221. if (strcmp (value, "on") == 0) {
  222. logsys_logger.priority = LOG_LEVEL_DEBUG;
  223. } else
  224. if (strcmp (value, "off") == 0) {
  225. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  226. } else {
  227. goto parse_error;
  228. }
  229. }
  230. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  231. char *token = strtok (value, "|");
  232. while (token != NULL) {
  233. if (strcmp (token, "enter") == 0) {
  234. logsys_logger.tags |= LOGSYS_TAG_ENTER;
  235. } else if (strcmp (token, "leave") == 0) {
  236. logsys_logger.tags |= LOGSYS_TAG_LEAVE;
  237. } else if (strcmp (token, "trace1") == 0) {
  238. logsys_logger.tags |= LOGSYS_TAG_TRACE1;
  239. } else if (strcmp (token, "trace2") == 0) {
  240. logsys_logger.tags |= LOGSYS_TAG_TRACE2;
  241. } else if (strcmp (token, "trace3") == 0) {
  242. logsys_logger.tags |= LOGSYS_TAG_TRACE3;
  243. } else if (strcmp (token, "trace4") == 0) {
  244. logsys_logger.tags |= LOGSYS_TAG_TRACE4;
  245. } else if (strcmp (token, "trace5") == 0) {
  246. logsys_logger.tags |= LOGSYS_TAG_TRACE5;
  247. } else if (strcmp (token, "trace6") == 0) {
  248. logsys_logger.tags |= LOGSYS_TAG_TRACE6;
  249. } else if (strcmp (token, "trace7") == 0) {
  250. logsys_logger.tags |= LOGSYS_TAG_TRACE7;
  251. } else if (strcmp (token, "trace8") == 0) {
  252. logsys_logger.tags |= LOGSYS_TAG_TRACE8;
  253. } else {
  254. error_reason = "bad tags value";
  255. goto parse_error;
  256. }
  257. token = strtok(NULL, "|");
  258. }
  259. }
  260. /*
  261. * set individual logger configurations
  262. */
  263. logsys_config_subsys_set (
  264. logsys_logger.subsys,
  265. logsys_logger.tags,
  266. logsys_logger.priority);
  267. }
  268. objdb->object_find_destroy (object_find_logsys_handle);
  269. }
  270. objdb->object_find_destroy (object_find_handle);
  271. return 0;
  272. parse_error:
  273. sprintf (error_string_response,
  274. "parse error in config: %s.\n",
  275. error_reason);
  276. *error_string = error_string_response;
  277. return (-1);
  278. }
  279. int corosync_main_config_read (
  280. struct objdb_iface_ver0 *objdb,
  281. char **error_string,
  282. struct main_config *main_config)
  283. {
  284. unsigned int object_service_handle;
  285. char *value;
  286. char *error_reason = error_string_response;
  287. unsigned int object_find_handle;
  288. memset (main_config, 0, sizeof (struct main_config));
  289. corosync_main_config_read_logging(objdb, error_string, main_config);
  290. objdb->object_find_create (
  291. OBJECT_PARENT_HANDLE,
  292. "aisexec",
  293. strlen ("aisexec"),
  294. &object_find_handle);
  295. if (objdb->object_find_next (
  296. object_find_handle,
  297. &object_service_handle) == 0) {
  298. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  299. main_config->user = strdup(value);
  300. }
  301. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  302. main_config->group = strdup(value);
  303. }
  304. }
  305. objdb->object_find_destroy (object_find_handle);
  306. /* Default user/group */
  307. if (!main_config->user)
  308. main_config->user = "ais";
  309. if (!main_config->group)
  310. main_config->group = "ais";
  311. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  312. (main_config->logfile == NULL)) {
  313. error_reason = "logmode set to 'file' but no logfile specified";
  314. goto parse_error;
  315. }
  316. if (main_config->syslog_facility == 0)
  317. main_config->syslog_facility = LOG_DAEMON;
  318. add_logsys_config_notification(objdb, main_config);
  319. return 0;
  320. parse_error:
  321. sprintf (error_string_response,
  322. "parse error in config: %s.\n",
  323. error_reason);
  324. *error_string = error_string_response;
  325. return (-1);
  326. }
  327. static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  328. void *priv_data_pt)
  329. {
  330. struct main_config *main_config = priv_data_pt;
  331. char *error_string;
  332. if (type == OBJDB_RELOAD_NOTIFY_END) {
  333. /*
  334. * Reload the logsys configuration
  335. */
  336. corosync_main_config_read_logging(global_objdb,
  337. &error_string,
  338. main_config);
  339. }
  340. }
  341. static void add_logsys_config_notification(
  342. struct objdb_iface_ver0 *objdb,
  343. struct main_config *main_config)
  344. {
  345. global_objdb = objdb;
  346. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  347. 1,
  348. NULL,
  349. NULL,
  350. NULL,
  351. main_objdb_reload_notify,
  352. main_config);
  353. }