Patch to remove segfault on the exiting of a service.
[openais.git] / exec / mainconfig.c
blobe0e350a3c4edec3ae9338f9a530f599ba5c66fb7
1 /*
2 * Copyright (c) 2002-2005 MontaVista Software, Inc.
4 * All rights reserved.
6 * Author: Steven Dake (sdake@mvista.com)
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
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.
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.
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>
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 #include "service.h"
52 static char error_string_response[512];
54 /* This just makes the code below a little neater */
55 static inline int objdb_get_string (
56 struct objdb_iface_ver0 *objdb,
57 unsigned int object_service_handle,
58 char *key, char **value)
60 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))) {
69 if (*value) {
70 return 0;
73 return -1;
76 static inline void objdb_get_int (
77 struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
78 char *key, unsigned int *intvalue)
80 char *value = NULL;
82 if (!objdb->object_key_get (object_service_handle,
83 key,
84 strlen (key),
85 (void *)&value,
86 NULL)) {
88 if (value) {
89 *intvalue = atoi(value);
94 static struct logsys_config_struct {
95 char subsys[6];
96 unsigned int priority;
97 unsigned int tags;
98 } logsys_logger;
100 int openais_main_config_read (
101 struct objdb_iface_ver0 *objdb,
102 char **error_string,
103 struct main_config *main_config)
105 unsigned int object_service_handle;
106 unsigned int object_logger_subsys_handle;
107 char *value;
108 char *error_reason = error_string_response;
110 memset (main_config, 0, sizeof (struct main_config));
112 objdb->object_find_reset (OBJECT_PARENT_HANDLE);
114 main_config->logmode = LOG_MODE_FLUSH_AFTER_CONFIG;
115 if (objdb->object_find (
116 OBJECT_PARENT_HANDLE,
117 "logging",
118 strlen ("logging"),
119 &object_service_handle) == 0) {
121 if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
122 if (strcmp (value, "yes") == 0) {
123 main_config->logmode |= LOG_MODE_OUTPUT_FILE;
124 } else
125 if (strcmp (value, "no") == 0) {
126 main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
129 if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
130 if (strcmp (value, "yes") == 0) {
131 main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG_THREADED;
132 } else
133 if (strcmp (value, "no") == 0) {
134 main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG_THREADED;
137 if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
138 if (strcmp (value, "yes") == 0) {
139 main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
140 } else
141 if (strcmp (value, "no") == 0) {
142 main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
146 if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
147 if (strcmp (value, "on") == 0) {
148 main_config->logmode |= LOG_MODE_DISPLAY_DEBUG;
149 } else
150 if (strcmp (value, "off") == 0) {
151 main_config->logmode &= ~LOG_MODE_DISPLAY_DEBUG;
152 } else {
153 goto parse_error;
156 if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
157 if (strcmp (value, "on") == 0) {
158 main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
159 } else
160 if (strcmp (value, "off") == 0) {
161 main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
162 } else {
163 goto parse_error;
166 if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
167 main_config->logfile = strdup (value);
170 if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
171 if (strcmp (value, "on") == 0) {
172 main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
173 } else
174 if (strcmp (value, "off") == 0) {
175 main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
176 } else {
177 goto parse_error;
181 if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
182 if (strcmp (value, "daemon") == 0) {
183 main_config->syslog_facility = LOG_DAEMON;
184 } else
185 if (strcmp (value, "local0") == 0) {
186 main_config->syslog_facility = LOG_LOCAL0;
187 } else
188 if (strcmp (value, "local1") == 0) {
189 main_config->syslog_facility = LOG_LOCAL1;
190 } else
191 if (strcmp (value, "local2") == 0) {
192 main_config->syslog_facility = LOG_LOCAL2;
193 } else
194 if (strcmp (value, "local3") == 0) {
195 main_config->syslog_facility = LOG_LOCAL3;
196 } else
197 if (strcmp (value, "local4") == 0) {
198 main_config->syslog_facility = LOG_LOCAL4;
199 } else
200 if (strcmp (value, "local5") == 0) {
201 main_config->syslog_facility = LOG_LOCAL5;
202 } else
203 if (strcmp (value, "local6") == 0) {
204 main_config->syslog_facility = LOG_LOCAL6;
205 } else
206 if (strcmp (value, "local7") == 0) {
207 main_config->syslog_facility = LOG_LOCAL7;
208 } else {
209 error_reason = "unknown syslog facility specified";
210 goto parse_error;
214 while (objdb->object_find (object_service_handle,
215 "logger_subsys",
216 strlen ("logger_subsys"),
217 &object_logger_subsys_handle) == 0) {
219 if (!objdb_get_string (objdb,
220 object_logger_subsys_handle,
221 "subsys", &value)) {
223 strncpy (logsys_logger.subsys, value,
224 sizeof (logsys_logger.subsys));
226 else {
227 error_reason = "subsys required for logger directive";
228 goto parse_error;
230 if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
231 if (strcmp (value, "on") == 0) {
232 logsys_logger.priority = LOG_LEVEL_DEBUG;
233 } else
234 if (strcmp (value, "off") == 0) {
235 logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
236 } else {
237 goto parse_error;
240 if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
241 char *token = strtok (value, "|");
243 while (token != NULL) {
244 if (strcmp (token, "enter") == 0) {
245 logsys_logger.tags |= LOGSYS_TAG_ENTER;
246 } else if (strcmp (token, "leave") == 0) {
247 logsys_logger.tags |= LOGSYS_TAG_LEAVE;
248 } else if (strcmp (token, "trace1") == 0) {
249 logsys_logger.tags |= LOGSYS_TAG_TRACE1;
250 } else if (strcmp (token, "trace2") == 0) {
251 logsys_logger.tags |= LOGSYS_TAG_TRACE2;
252 } else if (strcmp (token, "trace3") == 0) {
253 logsys_logger.tags |= LOGSYS_TAG_TRACE3;
254 } else if (strcmp (token, "trace4") == 0) {
255 logsys_logger.tags |= LOGSYS_TAG_TRACE4;
256 } else if (strcmp (token, "trace5") == 0) {
257 logsys_logger.tags |= LOGSYS_TAG_TRACE5;
258 } else if (strcmp (token, "trace6") == 0) {
259 logsys_logger.tags |= LOGSYS_TAG_TRACE6;
260 } else if (strcmp (token, "trace7") == 0) {
261 logsys_logger.tags |= LOGSYS_TAG_TRACE7;
262 } else if (strcmp (token, "trace8") == 0) {
263 logsys_logger.tags |= LOGSYS_TAG_TRACE8;
264 } else {
265 error_reason = "bad tags value";
266 goto parse_error;
269 token = strtok(NULL, "|");
273 * set individual logger configurations
275 logsys_config_subsys_set (
276 logsys_logger.subsys,
277 logsys_logger.tags,
278 logsys_logger.priority);
283 objdb->object_find_reset (OBJECT_PARENT_HANDLE);
284 if (objdb->object_find (
285 OBJECT_PARENT_HANDLE,
286 "aisexec",
287 strlen ("aisexec"),
288 &object_service_handle) == 0) {
290 if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
291 main_config->user = strdup(value);
293 if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
294 main_config->group = strdup(value);
298 /* Default user/group */
299 if (!main_config->user)
300 main_config->user = "ais";
302 if (!main_config->group)
303 main_config->group = "ais";
305 if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
306 (main_config->logfile == NULL)) {
307 error_reason = "logmode set to 'file' but no logfile specified";
308 goto parse_error;
311 if (main_config->syslog_facility == 0)
312 main_config->syslog_facility = LOG_DAEMON;
314 return 0;
316 parse_error:
317 sprintf (error_string_response,
318 "parse error in config: %s.\n",
319 error_reason);
321 *error_string = error_string_response;
322 return (-1);