]> git.proxmox.com Git - mirror_corosync.git/blame - exec/coroparse.c
*Dispatch returns CS_ERR_BAD_HANDLE only on first hdb_get
[mirror_corosync.git] / exec / coroparse.c
CommitLineData
2f552719 1/*
23c64c64 2 * Copyright (c) 2006, 2009 Red Hat, Inc.
2f552719
SD
3 *
4 * All rights reserved.
5 *
6 * Author: Patrick Caulfield (pcaulfie@redhat.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 */
031c02f5
FDN
34
35#include <config.h>
36
2f552719
SD
37#include <sys/types.h>
38#include <sys/uio.h>
39#include <sys/socket.h>
40#include <sys/un.h>
41#include <netinet/in.h>
42#include <arpa/inet.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <stdlib.h>
46#include <stdio.h>
47#include <errno.h>
2f552719
SD
48#include <string.h>
49
e1f53138
SD
50#include <corosync/lcr/lcr_comp.h>
51#include <corosync/engine/objdb.h>
52#include <corosync/engine/config.h>
53
2f552719
SD
54#include "util.h"
55
56static int read_config_file_into_objdb(
57 struct objdb_iface_ver0 *objdb,
23c64c64 58 const char **error_string);
2f552719
SD
59static char error_string_response[512];
60
61
23c64c64
JM
62static int aisparser_readconfig (struct objdb_iface_ver0 *objdb,
63 const char **error_string)
2f552719
SD
64{
65 if (read_config_file_into_objdb(objdb, error_string)) {
66 return -1;
67 }
68
69 return 0;
70}
71
72
73static char *remove_whitespace(char *string)
74{
75 char *start = string+strspn(string, " \t");
76 char *end = start+(strlen(start))-1;
77
78 while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
79 end--;
80 if (end != start)
81 *(end+1) = '\0';
82
83 return start;
84}
85
86static int parse_section(FILE *fp,
87 struct objdb_iface_ver0 *objdb,
58ab8d4a 88 hdb_handle_t parent_handle,
23c64c64 89 const char **error_string)
2f552719
SD
90{
91 char line[512];
92 int i;
93 char *loc;
94
94b64352
JF
95 while (fgets (line, sizeof (line), fp)) {
96 if (strlen(line) > 0) {
97 if (line[strlen(line) - 1] == '\n')
98 line[strlen(line) - 1] = '\0';
99 if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
100 line[strlen(line) - 1] = '\0';
101 }
2f552719
SD
102 /*
103 * Clear out white space and tabs
104 */
105 for (i = strlen (line) - 1; i > -1; i--) {
106 if (line[i] == '\t' || line[i] == ' ') {
107 line[i] = '\0';
108 } else {
109 break;
110 }
111 }
112 /*
113 * Clear out comments and empty lines
114 */
115 if (line[0] == '#' || line[0] == '\0') {
116 continue;
117 }
118
119 /* New section ? */
283011c6 120 if ((loc = strchr_rs (line, '{'))) {
58ab8d4a 121 hdb_handle_t new_parent;
2f552719
SD
122 char *section = remove_whitespace(line);
123
124 loc--;
125 *loc = '\0';
126 objdb->object_create (parent_handle, &new_parent,
127 section, strlen (section));
128 if (parse_section(fp, objdb, new_parent, error_string))
129 return -1;
130 }
131
132 /* New key/value */
283011c6 133 if ((loc = strchr_rs (line, ':'))) {
2f552719
SD
134 char *key;
135 char *value;
136
137 *(loc-1) = '\0';
138 key = remove_whitespace(line);
139 value = remove_whitespace(loc);
140 objdb->object_key_create (parent_handle, key,
141 strlen (key),
142 value, strlen (value) + 1);
143 }
144
283011c6 145 if ((loc = strchr_rs (line, '}'))) {
2f552719
SD
146 return 0;
147 }
148 }
149
150 if (parent_handle != OBJECT_PARENT_HANDLE) {
151 *error_string = "Missing closing brace";
152 return -1;
153 }
154
155 return 0;
156}
157
158
159
160/* Read config file and load into objdb */
161static int read_config_file_into_objdb(
162 struct objdb_iface_ver0 *objdb,
23c64c64 163 const char **error_string)
2f552719
SD
164{
165 FILE *fp;
23c64c64 166 const char *filename;
2f552719
SD
167 char *error_reason = error_string_response;
168 int res;
169
170 filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
171 if (!filename)
20e0336d 172 filename = SYSCONFDIR "/corosync.conf";
2f552719
SD
173
174 fp = fopen (filename, "r");
23c64c64 175 if (fp == NULL) {
870046d0
SD
176 snprintf (error_reason, sizeof(error_string_response),
177 "Can't read file %s reason = (%s)\n",
2f552719
SD
178 filename, strerror (errno));
179 *error_string = error_reason;
180 return -1;
181 }
182
183 res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string);
184
185 fclose(fp);
186
94b64352
JF
187 if (res == 0) {
188 snprintf (error_reason, sizeof(error_string_response),
870046d0 189 "Successfully read main configuration file '%s'.\n", filename);
94b64352
JF
190 *error_string = error_reason;
191 }
2f552719
SD
192
193 return res;
194}
195
196/*
197 * Dynamic Loader definition
198 */
199
200struct config_iface_ver0 aisparser_iface_ver0 = {
201 .config_readconfig = aisparser_readconfig
202};
203
dd3991c0 204struct lcr_iface corosync_aisparser_ver0[1] = {
2f552719 205 {
273d4d61 206 .name = "corosync_parser",
2f552719
SD
207 .version = 0,
208 .versions_replace = 0,
209 .versions_replace_count = 0,
210 .dependencies = 0,
211 .dependency_count = 0,
212 .constructor = NULL,
213 .destructor = NULL,
214 .interfaces = NULL,
215 }
216};
217
dd3991c0 218struct corosync_service_handler *aisparser_get_handler_ver0 (void);
2f552719
SD
219
220struct lcr_comp aisparser_comp_ver0 = {
221 .iface_count = 1,
dd3991c0 222 .ifaces = corosync_aisparser_ver0
2f552719
SD
223};
224
225
226__attribute__ ((constructor)) static void aisparser_comp_register (void) {
dd3991c0 227 lcr_interfaces_set (&corosync_aisparser_ver0[0], &aisparser_iface_ver0);
2f552719
SD
228 lcr_component_register (&aisparser_comp_ver0);
229}