mainconfig.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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 "print.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(struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  53. char *key, char **value)
  54. {
  55. int res;
  56. *value = NULL;
  57. if ( !(res = objdb->object_key_get (object_service_handle,
  58. key,
  59. strlen (key),
  60. (void *)value,
  61. NULL))) {
  62. if (*value)
  63. return 0;
  64. }
  65. return -1;
  66. }
  67. extern int openais_main_config_read (
  68. struct objdb_iface_ver0 *objdb,
  69. char **error_string,
  70. struct main_config *main_config)
  71. {
  72. unsigned int object_service_handle;
  73. char *value;
  74. char *error_reason = error_string_response;
  75. memset (main_config, 0, sizeof (struct main_config));
  76. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  77. if (objdb->object_find (
  78. OBJECT_PARENT_HANDLE,
  79. "logging",
  80. strlen ("logging"),
  81. &object_service_handle) == 0) {
  82. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  83. if (strcmp (value, "yes") == 0) {
  84. main_config->logmode |= LOG_MODE_FILE;
  85. } else
  86. if (strcmp (value, "no") == 0) {
  87. main_config->logmode &= ~LOG_MODE_FILE;
  88. }
  89. }
  90. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  91. if (strcmp (value, "yes") == 0) {
  92. main_config->logmode |= LOG_MODE_SYSLOG;
  93. } else
  94. if (strcmp (value, "no") == 0) {
  95. main_config->logmode &= ~LOG_MODE_SYSLOG;
  96. }
  97. }
  98. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  99. if (strcmp (value, "yes") == 0) {
  100. main_config->logmode |= LOG_MODE_STDERR;
  101. } else
  102. if (strcmp (value, "no") == 0) {
  103. main_config->logmode &= ~LOG_MODE_STDERR;
  104. }
  105. }
  106. if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
  107. if (strcmp (value, "on") == 0) {
  108. main_config->logmode |= LOG_MODE_DEBUG;
  109. } else
  110. if (strcmp (value, "off") == 0) {
  111. main_config->logmode &= ~LOG_MODE_DEBUG;
  112. } else {
  113. goto parse_error;
  114. }
  115. }
  116. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  117. if (strcmp (value, "on") == 0) {
  118. main_config->logmode |= LOG_MODE_TIMESTAMP;
  119. } else
  120. if (strcmp (value, "off") == 0) {
  121. main_config->logmode &= ~LOG_MODE_TIMESTAMP;
  122. } else {
  123. goto parse_error;
  124. }
  125. }
  126. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  127. main_config->logfile = strdup (value);
  128. }
  129. }
  130. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  131. if (objdb->object_find (
  132. OBJECT_PARENT_HANDLE,
  133. "aisexec",
  134. strlen ("aisexec"),
  135. &object_service_handle) == 0) {
  136. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  137. main_config->user = strdup(value);
  138. }
  139. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  140. main_config->group = strdup(value);
  141. }
  142. }
  143. /* Default user/group */
  144. if (!main_config->user)
  145. main_config->user = OPENAIS_USER;
  146. if (!main_config->group)
  147. main_config->group = OPENAIS_GROUP;
  148. if ((main_config->logmode & LOG_MODE_FILE) && main_config->logfile == 0) {
  149. error_reason = "logmode set to 'file' but no logfile specified";
  150. goto parse_error;
  151. }
  152. return 0;
  153. parse_error:
  154. sprintf (error_string_response,
  155. "parse error in config: %s.\n",
  156. error_reason);
  157. *error_string = error_string_response;
  158. return (-1);
  159. }