This source file includes following definitions.
- create_request_adv
- create_reply_adv
- pcmk__message_name
- pcmk__register_handlers
- pcmk__process_request
- pcmk__reset_request
- add_message_xml
- get_message_xml
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <sys/types.h>
14
15 #include <glib.h>
16 #include <libxml/tree.h>
17
18 #include <crm/common/xml.h>
19 #include <crm/common/xml_internal.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 xmlNode *
40 create_request_adv(const char *task, xmlNode *msg_data,
41 const char *host_to, const char *sys_to,
42 const char *sys_from, const char *uuid_from,
43 const char *origin)
44 {
45 static uint ref_counter = 0;
46
47 char *true_from = NULL;
48 xmlNode *request = NULL;
49 char *reference = crm_strdup_printf("%s-%s-%lld-%u",
50 (task? task : "_empty_"),
51 (sys_from? sys_from : "_empty_"),
52 (long long) time(NULL), ref_counter++);
53
54 if (uuid_from != NULL) {
55 true_from = crm_strdup_printf("%s_%s", uuid_from,
56 (sys_from? sys_from : "none"));
57 } else if (sys_from != NULL) {
58 true_from = strdup(sys_from);
59 } else {
60 crm_err("Cannot create IPC request: No originating system specified");
61 }
62
63
64 request = pcmk__xe_create(NULL, __func__);
65 crm_xml_add(request, PCMK_XA_ORIGIN, origin);
66 crm_xml_add(request, PCMK__XA_T, PCMK__VALUE_CRMD);
67 crm_xml_add(request, PCMK_XA_VERSION, CRM_FEATURE_SET);
68 crm_xml_add(request, PCMK__XA_SUBT, PCMK__VALUE_REQUEST);
69 crm_xml_add(request, PCMK_XA_REFERENCE, reference);
70 crm_xml_add(request, PCMK__XA_CRM_TASK, task);
71 crm_xml_add(request, PCMK__XA_CRM_SYS_TO, sys_to);
72 crm_xml_add(request, PCMK__XA_CRM_SYS_FROM, true_from);
73
74
75 if (host_to != NULL && strlen(host_to) > 0) {
76 crm_xml_add(request, PCMK__XA_CRM_HOST_TO, host_to);
77 }
78
79 if (msg_data != NULL) {
80 xmlNode *wrapper = pcmk__xe_create(request, PCMK__XE_CRM_XML);
81
82 pcmk__xml_copy(wrapper, msg_data);
83 }
84 free(reference);
85 free(true_from);
86
87 return request;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 xmlNode *
104 create_reply_adv(const xmlNode *original_request, xmlNode *xml_response_data,
105 const char *origin)
106 {
107 xmlNode *reply = NULL;
108
109 const char *host_from = crm_element_value(original_request, PCMK__XA_SRC);
110 const char *sys_from = crm_element_value(original_request,
111 PCMK__XA_CRM_SYS_FROM);
112 const char *sys_to = crm_element_value(original_request,
113 PCMK__XA_CRM_SYS_TO);
114 const char *type = crm_element_value(original_request, PCMK__XA_SUBT);
115 const char *operation = crm_element_value(original_request,
116 PCMK__XA_CRM_TASK);
117 const char *crm_msg_reference = crm_element_value(original_request,
118 PCMK_XA_REFERENCE);
119
120 if (type == NULL) {
121 crm_err("Cannot create new_message, no message type in original message");
122 CRM_ASSERT(type != NULL);
123 return NULL;
124 }
125
126 if (strcmp(type, PCMK__VALUE_REQUEST) != 0) {
127
128
129
130 crm_trace("Creating a reply for a non-request original message");
131 }
132
133 reply = pcmk__xe_create(NULL, __func__);
134 crm_xml_add(reply, PCMK_XA_ORIGIN, origin);
135 crm_xml_add(reply, PCMK__XA_T, PCMK__VALUE_CRMD);
136 crm_xml_add(reply, PCMK_XA_VERSION, CRM_FEATURE_SET);
137 crm_xml_add(reply, PCMK__XA_SUBT, PCMK__VALUE_RESPONSE);
138 crm_xml_add(reply, PCMK_XA_REFERENCE, crm_msg_reference);
139 crm_xml_add(reply, PCMK__XA_CRM_TASK, operation);
140
141
142 crm_xml_add(reply, PCMK__XA_CRM_SYS_TO, sys_from);
143 crm_xml_add(reply, PCMK__XA_CRM_SYS_FROM, sys_to);
144
145
146 if (host_from != NULL && strlen(host_from) > 0) {
147 crm_xml_add(reply, PCMK__XA_CRM_HOST_TO, host_from);
148 }
149
150 if (xml_response_data != NULL) {
151 xmlNode *wrapper = pcmk__xe_create(reply, PCMK__XE_CRM_XML);
152
153 pcmk__xml_copy(wrapper, xml_response_data);
154 }
155
156 return reply;
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170 const char *
171 pcmk__message_name(const char *name)
172 {
173 if (name == NULL) {
174 return "unknown";
175
176 } else if (!strcmp(name, "pacemaker-attrd")) {
177 return "attrd";
178
179 } else if (!strcmp(name, "pacemaker-based")) {
180 return CRM_SYSTEM_CIB;
181
182 } else if (!strcmp(name, "pacemaker-controld")) {
183 return CRM_SYSTEM_CRMD;
184
185 } else if (!strcmp(name, "pacemaker-execd")) {
186 return CRM_SYSTEM_LRMD;
187
188 } else if (!strcmp(name, "pacemaker-fenced")) {
189 return "stonith-ng";
190
191 } else if (!strcmp(name, "pacemaker-schedulerd")) {
192 return CRM_SYSTEM_PENGINE;
193
194 } else {
195 return name;
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 GHashTable *
213 pcmk__register_handlers(const pcmk__server_command_t handlers[])
214 {
215 GHashTable *commands = g_hash_table_new(g_str_hash, g_str_equal);
216
217 if (handlers != NULL) {
218 int i;
219
220 for (i = 0; handlers[i].command != NULL; ++i) {
221 g_hash_table_insert(commands, (gpointer) handlers[i].command,
222 handlers[i].handler);
223 }
224 if (handlers[i].handler != NULL) {
225
226 g_hash_table_insert(commands, (gpointer) "", handlers[i].handler);
227 }
228 }
229 return commands;
230 }
231
232
233
234
235
236
237
238
239
240
241 xmlNode *
242 pcmk__process_request(pcmk__request_t *request, GHashTable *handlers)
243 {
244 xmlNode *(*handler)(pcmk__request_t *request) = NULL;
245
246 CRM_CHECK((request != NULL) && (request->op != NULL) && (handlers != NULL),
247 return NULL);
248
249 if (pcmk_is_set(request->flags, pcmk__request_sync)
250 && (request->ipc_client != NULL)) {
251 CRM_CHECK(request->ipc_client->request_id == request->ipc_id,
252 return NULL);
253 }
254
255 handler = g_hash_table_lookup(handlers, request->op);
256 if (handler == NULL) {
257 handler = g_hash_table_lookup(handlers, "");
258 if (handler == NULL) {
259 crm_info("Ignoring %s request from %s %s with no handler",
260 request->op, pcmk__request_origin_type(request),
261 pcmk__request_origin(request));
262 return NULL;
263 }
264 }
265
266 return (*handler)(request);
267 }
268
269
270
271
272
273
274
275 void
276 pcmk__reset_request(pcmk__request_t *request)
277 {
278 free(request->op);
279 request->op = NULL;
280
281 pcmk__reset_result(&(request->result));
282 }
283
284
285
286
287 #include <crm/common/xml_compat.h>
288
289 gboolean
290 add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
291 {
292 xmlNode *holder = pcmk__xe_create(msg, field);
293
294 pcmk__xml_copy(holder, xml);
295 return TRUE;
296 }
297
298 xmlNode *
299 get_message_xml(const xmlNode *msg, const char *field)
300 {
301 xmlNode *child = pcmk__xe_first_child(msg, field, NULL, NULL);
302
303 return pcmk__xe_first_child(child, NULL, NULL, NULL);
304 }
305
306
307