mainconfig.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. typedef enum {
  52. MAIN_HEAD,
  53. MAIN_LOGGING,
  54. MAIN_EVENT,
  55. MAIN_AMF,
  56. MAIN_COMPONENTS
  57. } main_parse_t;
  58. /* This just makes the code below a little neater */
  59. static inline int objdb_get_string(struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  60. char *key, char **value)
  61. {
  62. int res;
  63. *value = NULL;
  64. if ( !(res = objdb->object_key_get (object_service_handle,
  65. key,
  66. strlen (key),
  67. (void *)value,
  68. NULL))) {
  69. if (*value)
  70. return 0;
  71. }
  72. return -1;
  73. }
  74. extern int openais_main_config_read (
  75. struct objdb_iface_ver0 *objdb,
  76. char **error_string,
  77. struct main_config *main_config)
  78. {
  79. unsigned int object_service_handle;
  80. char *value;
  81. char *error_reason = error_string_response;
  82. memset (main_config, 0, sizeof (struct main_config));
  83. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  84. if (objdb->object_find (
  85. OBJECT_PARENT_HANDLE,
  86. "logging",
  87. strlen ("logging"),
  88. &object_service_handle) == 0) {
  89. if (!objdb_get_string (objdb,object_service_handle, "logoutput", &value)) {
  90. if (strcmp (value, "file") == 0) {
  91. main_config->logmode |= LOG_MODE_FILE;
  92. } else
  93. if (strcmp (value, "syslog") == 0) {
  94. main_config->logmode |= LOG_MODE_SYSLOG;
  95. } else
  96. if (strcmp (value, "stderr") == 0) {
  97. main_config->logmode |= LOG_MODE_STDERR;
  98. } else {
  99. goto parse_error;
  100. }
  101. }
  102. if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
  103. if (strcmp (value, "on") == 0) {
  104. main_config->logmode |= LOG_MODE_DEBUG;
  105. } else
  106. if (strcmp (value, "off") == 0) {
  107. main_config->logmode &= ~LOG_MODE_DEBUG;
  108. } else {
  109. goto parse_error;
  110. }
  111. }
  112. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  113. if (strcmp (value, "on") == 0) {
  114. main_config->logmode |= LOG_MODE_TIMESTAMP;
  115. } else
  116. if (strcmp (value, "off") == 0) {
  117. main_config->logmode &= ~LOG_MODE_TIMESTAMP;
  118. } else {
  119. goto parse_error;
  120. }
  121. }
  122. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  123. main_config->logfile = strdup (value);
  124. }
  125. }
  126. if (objdb->object_find (
  127. OBJECT_PARENT_HANDLE,
  128. "aisexec",
  129. strlen ("aisexec"),
  130. &object_service_handle) == 0) {
  131. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  132. main_config->user = strdup(value);
  133. }
  134. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  135. main_config->group = strdup(value);
  136. }
  137. }
  138. /* Default user/group */
  139. if (!main_config->user)
  140. main_config->user = OPENAIS_USER;
  141. if (!main_config->group)
  142. main_config->group = OPENAIS_GROUP;
  143. if ((main_config->logmode & LOG_MODE_FILE) && main_config->logfile == 0) {
  144. error_reason = "logmode set to 'file' but no logfile specified";
  145. goto parse_error;
  146. }
  147. return 0;
  148. parse_error:
  149. sprintf (error_string_response,
  150. "parse error in config: %s.\n",
  151. error_reason);
  152. *error_string = error_string_response;
  153. return (-1);
  154. }