]> git.proxmox.com Git - mirror_corosync.git/blob - exec/cfg.c
config: Don't free pointers used by transports
[mirror_corosync.git] / exec / cfg.c
1 /*
2 * Copyright (c) 2005-2006 MontaVista Software, Inc.
3 * Copyright (c) 2006-2018 Red Hat, Inc.
4 *
5 * All rights reserved.
6 *
7 * Author: Steven Dake (sdake@redhat.com)
8 *
9 * This software licensed under BSD license, the text of which follows:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * - Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * - Neither the name of the MontaVista Software, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <config.h>
37
38 #include <sys/types.h>
39 #include <sys/uio.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <stddef.h>
49 #include <limits.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <assert.h>
53
54 #include <corosync/corotypes.h>
55 #include <qb/qbipc_common.h>
56 #include <corosync/cfg.h>
57 #include <qb/qblist.h>
58 #include <corosync/mar_gen.h>
59 #include <corosync/totem/totemip.h>
60 #include <corosync/totem/totem.h>
61 #include <corosync/ipc_cfg.h>
62 #include <corosync/logsys.h>
63 #include <corosync/coroapi.h>
64 #include <corosync/icmap.h>
65 #include <corosync/corodefs.h>
66
67 #include "totemconfig.h"
68 #include "service.h"
69 #include "main.h"
70
71 LOGSYS_DECLARE_SUBSYS ("CFG");
72
73 enum cfg_message_req_types {
74 MESSAGE_REQ_EXEC_CFG_RINGREENABLE = 0,
75 MESSAGE_REQ_EXEC_CFG_KILLNODE = 1,
76 MESSAGE_REQ_EXEC_CFG_SHUTDOWN = 2,
77 MESSAGE_REQ_EXEC_CFG_RELOAD_CONFIG = 3
78 };
79
80 #define DEFAULT_SHUTDOWN_TIMEOUT 5
81
82 static struct qb_list_head trackers_list;
83
84 /*
85 * Variables controlling a requested shutdown
86 */
87 static corosync_timer_handle_t shutdown_timer;
88 static struct cfg_info *shutdown_con;
89 static uint32_t shutdown_flags;
90 static int shutdown_yes;
91 static int shutdown_no;
92 static int shutdown_expected;
93
94 struct cfg_info
95 {
96 struct qb_list_head list;
97 void *conn;
98 void *tracker_conn;
99 enum {SHUTDOWN_REPLY_UNKNOWN, SHUTDOWN_REPLY_YES, SHUTDOWN_REPLY_NO} shutdown_reply;
100 };
101
102 static void cfg_confchg_fn (
103 enum totem_configuration_type configuration_type,
104 const unsigned int *member_list, size_t member_list_entries,
105 const unsigned int *left_list, size_t left_list_entries,
106 const unsigned int *joined_list, size_t joined_list_entries,
107 const struct memb_ring_id *ring_id);
108
109 static char *cfg_exec_init_fn (struct corosync_api_v1 *corosync_api_v1);
110
111 static struct corosync_api_v1 *api;
112
113 static int cfg_lib_init_fn (void *conn);
114
115 static int cfg_lib_exit_fn (void *conn);
116
117 static void message_handler_req_exec_cfg_ringreenable (
118 const void *message,
119 unsigned int nodeid);
120
121 static void message_handler_req_exec_cfg_killnode (
122 const void *message,
123 unsigned int nodeid);
124
125 static void message_handler_req_exec_cfg_shutdown (
126 const void *message,
127 unsigned int nodeid);
128
129 static void message_handler_req_exec_cfg_reload_config (
130 const void *message,
131 unsigned int nodeid);
132
133 static void exec_cfg_killnode_endian_convert (void *msg);
134
135 static void message_handler_req_lib_cfg_ringstatusget (
136 void *conn,
137 const void *msg);
138
139 static void message_handler_req_lib_cfg_ringreenable (
140 void *conn,
141 const void *msg);
142
143 static void message_handler_req_lib_cfg_killnode (
144 void *conn,
145 const void *msg);
146
147 static void message_handler_req_lib_cfg_tryshutdown (
148 void *conn,
149 const void *msg);
150
151 static void message_handler_req_lib_cfg_replytoshutdown (
152 void *conn,
153 const void *msg);
154
155 static void message_handler_req_lib_cfg_get_node_addrs (
156 void *conn,
157 const void *msg);
158
159 static void message_handler_req_lib_cfg_local_get (
160 void *conn,
161 const void *msg);
162
163 static void message_handler_req_lib_cfg_reload_config (
164 void *conn,
165 const void *msg);
166
167 static void message_handler_req_lib_cfg_reopen_log_files (
168 void *conn,
169 const void *msg);
170
171 /*
172 * Service Handler Definition
173 */
174 static struct corosync_lib_handler cfg_lib_engine[] =
175 {
176 { /* 0 */
177 .lib_handler_fn = message_handler_req_lib_cfg_ringstatusget,
178 .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
179 },
180 { /* 1 */
181 .lib_handler_fn = message_handler_req_lib_cfg_ringreenable,
182 .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
183 },
184 { /* 2 */
185 .lib_handler_fn = message_handler_req_lib_cfg_killnode,
186 .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
187 },
188 { /* 3 */
189 .lib_handler_fn = message_handler_req_lib_cfg_tryshutdown,
190 .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
191 },
192 { /* 4 */
193 .lib_handler_fn = message_handler_req_lib_cfg_replytoshutdown,
194 .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
195 },
196 { /* 5 */
197 .lib_handler_fn = message_handler_req_lib_cfg_get_node_addrs,
198 .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
199 },
200 { /* 6 */
201 .lib_handler_fn = message_handler_req_lib_cfg_local_get,
202 .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
203 },
204 { /* 7 */
205 .lib_handler_fn = message_handler_req_lib_cfg_reload_config,
206 .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
207 },
208 { /* 8 */
209 .lib_handler_fn = message_handler_req_lib_cfg_reopen_log_files,
210 .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
211 }
212 };
213
214 static struct corosync_exec_handler cfg_exec_engine[] =
215 {
216 { /* 0 */
217 .exec_handler_fn = message_handler_req_exec_cfg_ringreenable,
218 },
219 { /* 1 */
220 .exec_handler_fn = message_handler_req_exec_cfg_killnode,
221 .exec_endian_convert_fn = exec_cfg_killnode_endian_convert
222 },
223 { /* 2 */
224 .exec_handler_fn = message_handler_req_exec_cfg_shutdown,
225 },
226 { /* 3 */
227 .exec_handler_fn = message_handler_req_exec_cfg_reload_config,
228 }
229 };
230
231 /*
232 * Exports the interface for the service
233 */
234 struct corosync_service_engine cfg_service_engine = {
235 .name = "corosync configuration service",
236 .id = CFG_SERVICE,
237 .priority = 1,
238 .private_data_size = sizeof(struct cfg_info),
239 .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
240 .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
241 .lib_init_fn = cfg_lib_init_fn,
242 .lib_exit_fn = cfg_lib_exit_fn,
243 .lib_engine = cfg_lib_engine,
244 .lib_engine_count = sizeof (cfg_lib_engine) / sizeof (struct corosync_lib_handler),
245 .exec_init_fn = cfg_exec_init_fn,
246 .exec_engine = cfg_exec_engine,
247 .exec_engine_count = sizeof (cfg_exec_engine) / sizeof (struct corosync_exec_handler),
248 .confchg_fn = cfg_confchg_fn
249 };
250
251 struct corosync_service_engine *cfg_get_service_engine_ver0 (void)
252 {
253 return (&cfg_service_engine);
254 }
255
256 struct req_exec_cfg_ringreenable {
257 struct qb_ipc_request_header header __attribute__((aligned(8)));
258 mar_message_source_t source __attribute__((aligned(8)));
259 };
260
261 struct req_exec_cfg_reload_config {
262 struct qb_ipc_request_header header __attribute__((aligned(8)));
263 mar_message_source_t source __attribute__((aligned(8)));
264 };
265
266 struct req_exec_cfg_killnode {
267 struct qb_ipc_request_header header __attribute__((aligned(8)));
268 mar_uint32_t nodeid __attribute__((aligned(8)));
269 mar_name_t reason __attribute__((aligned(8)));
270 };
271
272 struct req_exec_cfg_shutdown {
273 struct qb_ipc_request_header header __attribute__((aligned(8)));
274 };
275
276 /* IMPL */
277
278 static char *cfg_exec_init_fn (
279 struct corosync_api_v1 *corosync_api_v1)
280 {
281 api = corosync_api_v1;
282
283 qb_list_init(&trackers_list);
284 return (NULL);
285 }
286
287 static void cfg_confchg_fn (
288 enum totem_configuration_type configuration_type,
289 const unsigned int *member_list, size_t member_list_entries,
290 const unsigned int *left_list, size_t left_list_entries,
291 const unsigned int *joined_list, size_t joined_list_entries,
292 const struct memb_ring_id *ring_id)
293 {
294 }
295
296 /*
297 * Tell other nodes we are shutting down
298 */
299 static int send_shutdown(void)
300 {
301 struct req_exec_cfg_shutdown req_exec_cfg_shutdown;
302 struct iovec iovec;
303
304 ENTER();
305 req_exec_cfg_shutdown.header.size =
306 sizeof (struct req_exec_cfg_shutdown);
307 req_exec_cfg_shutdown.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
308 MESSAGE_REQ_EXEC_CFG_SHUTDOWN);
309
310 iovec.iov_base = (char *)&req_exec_cfg_shutdown;
311 iovec.iov_len = sizeof (struct req_exec_cfg_shutdown);
312
313 assert (api->totem_mcast (&iovec, 1, TOTEM_SAFE) == 0);
314
315 LEAVE();
316 return 0;
317 }
318
319 static void send_test_shutdown(void *only_conn, void *exclude_conn, int status)
320 {
321 struct res_lib_cfg_testshutdown res_lib_cfg_testshutdown;
322 struct qb_list_head *iter;
323
324 ENTER();
325 res_lib_cfg_testshutdown.header.size = sizeof(struct res_lib_cfg_testshutdown);
326 res_lib_cfg_testshutdown.header.id = MESSAGE_RES_CFG_TESTSHUTDOWN;
327 res_lib_cfg_testshutdown.header.error = status;
328 res_lib_cfg_testshutdown.flags = shutdown_flags;
329
330 if (only_conn) {
331 TRACE1("sending testshutdown to only %p", only_conn);
332 api->ipc_dispatch_send(only_conn, &res_lib_cfg_testshutdown,
333 sizeof(res_lib_cfg_testshutdown));
334 } else {
335 qb_list_for_each(iter, &trackers_list) {
336 struct cfg_info *ci = qb_list_entry(iter, struct cfg_info, list);
337
338 if (ci->conn != exclude_conn) {
339 TRACE1("sending testshutdown to %p", ci->tracker_conn);
340 api->ipc_dispatch_send(ci->tracker_conn, &res_lib_cfg_testshutdown,
341 sizeof(res_lib_cfg_testshutdown));
342 }
343 }
344 }
345 LEAVE();
346 }
347
348 static void check_shutdown_status(void)
349 {
350 ENTER();
351
352 /*
353 * Shutdown client might have gone away
354 */
355 if (!shutdown_con) {
356 LEAVE();
357 return;
358 }
359
360 /*
361 * All replies safely gathered in ?
362 */
363 if (shutdown_yes + shutdown_no >= shutdown_expected) {
364 struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
365
366 api->timer_delete(shutdown_timer);
367
368 if (shutdown_yes >= shutdown_expected ||
369 shutdown_flags == CFG_SHUTDOWN_FLAG_REGARDLESS) {
370 TRACE1("shutdown confirmed");
371
372 res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
373 res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
374 res_lib_cfg_tryshutdown.header.error = CS_OK;
375
376 /*
377 * Tell originator that shutdown was confirmed
378 */
379 api->ipc_response_send(shutdown_con->conn, &res_lib_cfg_tryshutdown,
380 sizeof(res_lib_cfg_tryshutdown));
381 shutdown_con = NULL;
382
383 /*
384 * Tell other nodes we are going down
385 */
386 send_shutdown();
387
388 }
389 else {
390
391 TRACE1("shutdown cancelled");
392 res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
393 res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
394 res_lib_cfg_tryshutdown.header.error = CS_ERR_BUSY;
395
396 /*
397 * Tell originator that shutdown was cancelled
398 */
399 api->ipc_response_send(shutdown_con->conn, &res_lib_cfg_tryshutdown,
400 sizeof(res_lib_cfg_tryshutdown));
401 shutdown_con = NULL;
402 }
403
404 log_printf(LOGSYS_LEVEL_DEBUG, "shutdown decision is: (yes count: %d, no count: %d) flags=%x",
405 shutdown_yes, shutdown_no, shutdown_flags);
406 }
407 LEAVE();
408 }
409
410
411 /*
412 * Not all nodes responded to the shutdown (in time)
413 */
414 static void shutdown_timer_fn(void *arg)
415 {
416 ENTER();
417
418 /*
419 * Mark undecideds as "NO"
420 */
421 shutdown_no = shutdown_expected;
422 check_shutdown_status();
423
424 send_test_shutdown(NULL, NULL, CS_ERR_TIMEOUT);
425 LEAVE();
426 }
427
428 static void remove_ci_from_shutdown(struct cfg_info *ci)
429 {
430 ENTER();
431
432 /*
433 * If the controlling shutdown process has quit, then cancel the
434 * shutdown session
435 */
436 if (ci == shutdown_con) {
437 shutdown_con = NULL;
438 api->timer_delete(shutdown_timer);
439 }
440
441 if (!qb_list_empty(&ci->list)) {
442 qb_list_del(&ci->list);
443 qb_list_init(&ci->list);
444
445 /*
446 * Remove our option
447 */
448 if (shutdown_con) {
449 if (ci->shutdown_reply == SHUTDOWN_REPLY_YES)
450 shutdown_yes--;
451 if (ci->shutdown_reply == SHUTDOWN_REPLY_NO)
452 shutdown_no--;
453 }
454
455 /*
456 * If we are leaving, then that's an implicit YES to shutdown
457 */
458 ci->shutdown_reply = SHUTDOWN_REPLY_YES;
459 shutdown_yes++;
460
461 check_shutdown_status();
462 }
463 LEAVE();
464 }
465
466
467 int cfg_lib_exit_fn (void *conn)
468 {
469 struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
470
471 ENTER();
472 remove_ci_from_shutdown(ci);
473 LEAVE();
474 return (0);
475 }
476
477 static int cfg_lib_init_fn (void *conn)
478 {
479 struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
480
481 ENTER();
482 qb_list_init(&ci->list);
483 LEAVE();
484
485 return (0);
486 }
487
488 /*
489 * Executive message handlers
490 */
491 static void message_handler_req_exec_cfg_ringreenable (
492 const void *message,
493 unsigned int nodeid)
494 {
495 ENTER();
496
497 LEAVE();
498 }
499
500 static void exec_cfg_killnode_endian_convert (void *msg)
501 {
502 struct req_exec_cfg_killnode *req_exec_cfg_killnode =
503 (struct req_exec_cfg_killnode *)msg;
504 ENTER();
505
506 swab_mar_name_t(&req_exec_cfg_killnode->reason);
507 LEAVE();
508 }
509
510
511 static void message_handler_req_exec_cfg_killnode (
512 const void *message,
513 unsigned int nodeid)
514 {
515 const struct req_exec_cfg_killnode *req_exec_cfg_killnode = message;
516 cs_name_t reason;
517
518 ENTER();
519 log_printf(LOGSYS_LEVEL_DEBUG, "request to kill node " CS_PRI_NODE_ID " (us=" CS_PRI_NODE_ID ")",
520 req_exec_cfg_killnode->nodeid, api->totem_nodeid_get());
521 if (req_exec_cfg_killnode->nodeid == api->totem_nodeid_get()) {
522 marshall_from_mar_name_t(&reason, &req_exec_cfg_killnode->reason);
523 log_printf(LOGSYS_LEVEL_NOTICE, "Killed by node " CS_PRI_NODE_ID " : %s",
524 nodeid, reason.value);
525 corosync_fatal_error(COROSYNC_FATAL_ERROR_EXIT);
526 }
527 LEAVE();
528 }
529
530 /*
531 * Self shutdown
532 */
533 static void message_handler_req_exec_cfg_shutdown (
534 const void *message,
535 unsigned int nodeid)
536 {
537 ENTER();
538
539 log_printf(LOGSYS_LEVEL_NOTICE, "Node " CS_PRI_NODE_ID " was shut down by sysadmin", nodeid);
540 if (nodeid == api->totem_nodeid_get()) {
541 api->shutdown_request();
542 }
543 LEAVE();
544 }
545
546 /* strcmp replacement that can handle NULLs */
547 static int nullcheck_strcmp(const char* left, const char *right)
548 {
549 if (!left && right)
550 return -1;
551 if (left && !right)
552 return 1;
553
554 if (!left && !right)
555 return 0;
556
557 return strcmp(left, right);
558 }
559
560 /*
561 * If a key has changed value in the new file, then warn the user and remove it from the temp_map
562 */
563 static void delete_and_notify_if_changed(icmap_map_t temp_map, const char *key_name)
564 {
565 if (!(icmap_key_value_eq(temp_map, key_name, icmap_get_global_map(), key_name))) {
566 if (icmap_delete_r(temp_map, key_name) == CS_OK) {
567 log_printf(LOGSYS_LEVEL_NOTICE, "Modified entry '%s' in corosync.conf cannot be changed at run-time", key_name);
568 }
569 }
570 }
571 /*
572 * Remove any keys from the new config file that in the new corosync.conf but that
573 * cannot be changed at run time. A log message will be issued for each
574 * entry that the user wants to change but they cannot.
575 *
576 * Add more here as needed.
577 */
578 static void remove_ro_entries(icmap_map_t temp_map)
579 {
580 delete_and_notify_if_changed(temp_map, "totem.secauth");
581 delete_and_notify_if_changed(temp_map, "totem.crypto_hash");
582 delete_and_notify_if_changed(temp_map, "totem.crypto_cipher");
583 delete_and_notify_if_changed(temp_map, "totem.keyfile");
584 delete_and_notify_if_changed(temp_map, "totem.key");
585 delete_and_notify_if_changed(temp_map, "totem.version");
586 delete_and_notify_if_changed(temp_map, "totem.threads");
587 delete_and_notify_if_changed(temp_map, "totem.ip_version");
588 delete_and_notify_if_changed(temp_map, "totem.rrp_mode");
589 delete_and_notify_if_changed(temp_map, "totem.netmtu");
590 delete_and_notify_if_changed(temp_map, "totem.interface.ringnumber");
591 delete_and_notify_if_changed(temp_map, "totem.interface.bindnetaddr");
592 delete_and_notify_if_changed(temp_map, "totem.interface.mcastaddr");
593 delete_and_notify_if_changed(temp_map, "totem.interface.broadcast");
594 delete_and_notify_if_changed(temp_map, "totem.interface.mcastport");
595 delete_and_notify_if_changed(temp_map, "totem.interface.ttl");
596 delete_and_notify_if_changed(temp_map, "totem.vsftype");
597 delete_and_notify_if_changed(temp_map, "totem.transport");
598 delete_and_notify_if_changed(temp_map, "totem.cluster_name");
599 delete_and_notify_if_changed(temp_map, "quorum.provider");
600 delete_and_notify_if_changed(temp_map, "system.move_to_root_cgroup");
601 delete_and_notify_if_changed(temp_map, "system.sched_rr");
602 delete_and_notify_if_changed(temp_map, "system.priority");
603 delete_and_notify_if_changed(temp_map, "system.qb_ipc_type");
604 delete_and_notify_if_changed(temp_map, "system.state_dir");
605 }
606
607 /*
608 * Remove entries that exist in the global map, but not in the temp_map, this will
609 * cause delete notifications to be sent to any listeners.
610 *
611 * NOTE: This routine depends entirely on the keys returned by the iterators
612 * being in alpha-sorted order.
613 */
614 static void remove_deleted_entries(icmap_map_t temp_map, const char *prefix)
615 {
616 icmap_iter_t old_iter;
617 icmap_iter_t new_iter;
618 const char *old_key, *new_key;
619 int ret;
620
621 old_iter = icmap_iter_init(prefix);
622 new_iter = icmap_iter_init_r(temp_map, prefix);
623
624 old_key = icmap_iter_next(old_iter, NULL, NULL);
625 new_key = icmap_iter_next(new_iter, NULL, NULL);
626
627 while (old_key || new_key) {
628 ret = nullcheck_strcmp(old_key, new_key);
629 if ((ret < 0 && old_key) || !new_key) {
630 /*
631 * new_key is greater, a line (or more) has been deleted
632 * Continue until old is >= new
633 */
634 do {
635 /* Remove it from icmap & send notifications */
636 icmap_delete(old_key);
637
638 old_key = icmap_iter_next(old_iter, NULL, NULL);
639 ret = nullcheck_strcmp(old_key, new_key);
640 } while (ret < 0 && old_key);
641 }
642 else if ((ret > 0 && new_key) || !old_key) {
643 /*
644 * old_key is greater, a line (or more) has been added
645 * Continue until new is >= old
646 *
647 * we don't need to do anything special with this like tell
648 * icmap. That will happen when we copy the values over
649 */
650 do {
651 new_key = icmap_iter_next(new_iter, NULL, NULL);
652 ret = nullcheck_strcmp(old_key, new_key);
653 } while (ret > 0 && new_key);
654 }
655 if (ret == 0) {
656 new_key = icmap_iter_next(new_iter, NULL, NULL);
657 old_key = icmap_iter_next(old_iter, NULL, NULL);
658 }
659 }
660 icmap_iter_finalize(new_iter);
661 icmap_iter_finalize(old_iter);
662 }
663
664 /*
665 * Reload configuration file
666 */
667 static void message_handler_req_exec_cfg_reload_config (
668 const void *message,
669 unsigned int nodeid)
670 {
671 const struct req_exec_cfg_reload_config *req_exec_cfg_reload_config = message;
672 struct res_lib_cfg_reload_config res_lib_cfg_reload_config;
673 struct totem_config new_config;
674 icmap_map_t temp_map;
675 const char *error_string;
676 int res = CS_OK;
677
678 ENTER();
679
680 log_printf(LOGSYS_LEVEL_NOTICE, "Config reload requested by node " CS_PRI_NODE_ID, nodeid);
681
682 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
683 /*
684 * Set up a new hashtable as a staging area.
685 */
686 if ((res = icmap_init_r(&temp_map)) != CS_OK) {
687 log_printf(LOGSYS_LEVEL_ERROR, "Unable to create temporary icmap. config file reload cancelled\n");
688 goto reload_fini_nomap;
689 }
690
691 /*
692 * Load new config into the temporary map
693 */
694 res = coroparse_configparse(temp_map, &error_string);
695 if (res == -1) {
696 log_printf (LOGSYS_LEVEL_ERROR, "Unable to reload config file: %s", error_string);
697 res = CS_ERR_INVALID_PARAM;
698 goto reload_fini_nofree;
699 }
700
701 /* Signal start of the reload process */
702 icmap_set_uint8("config.reload_in_progress", 1);
703
704 /* Detect deleted entries and remove them from the main icmap hashtable */
705 remove_deleted_entries(temp_map, "logging.");
706 remove_deleted_entries(temp_map, "totem.");
707 remove_deleted_entries(temp_map, "nodelist.");
708 remove_deleted_entries(temp_map, "quorum.");
709 remove_deleted_entries(temp_map, "uidgid.config.");
710 remove_deleted_entries(temp_map, "nozzle.");
711
712 /* Remove entries that cannot be changed */
713 remove_ro_entries(temp_map);
714
715 /* Take a copy of the current setup so we can check what has changed */
716 memset(&new_config, 0, sizeof(new_config));
717 new_config.orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
718 assert(new_config.orig_interfaces != NULL);
719
720 totempg_get_config(&new_config);
721
722 new_config.interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
723 assert(new_config.interfaces != NULL);
724 memset(new_config.interfaces, 0, sizeof (struct totem_interface) * INTERFACE_MAX);
725
726 /* For UDP[U] the configuration on link0 is static (apart from the nodelist) and only read at
727 startup. So preserve it here */
728 if ( (new_config.transport_number == TOTEM_TRANSPORT_UDP) ||
729 (new_config.transport_number == TOTEM_TRANSPORT_UDPU)) {
730 memcpy(&new_config.interfaces[0], &new_config.orig_interfaces[0],
731 sizeof(struct totem_interface));
732 }
733
734 /* Calculate new node and interface definitions */
735 if (totemconfig_configure_new_params(&new_config, temp_map, &error_string) == -1) {
736 log_printf (LOGSYS_LEVEL_ERROR, "Cannot configure new interface definitions: %s\n", error_string);
737 res = CS_ERR_INVALID_PARAM;
738 goto reload_fini;
739 }
740
741 /* Read from temp_map into new_config */
742 totem_volatile_config_read(&new_config, temp_map, NULL);
743
744 /* Validate dynamic parameters */
745 if (totem_volatile_config_validate(&new_config, temp_map, &error_string) == -1) {
746 log_printf (LOGSYS_LEVEL_ERROR, "Configuration is not valid: %s\n", error_string);
747 res = CS_ERR_INVALID_PARAM;
748 goto reload_fini;
749 }
750
751 /*
752 * Copy new keys into live config.
753 */
754 if ( (res = icmap_copy_map(icmap_get_global_map(), temp_map)) != CS_OK) {
755 log_printf (LOGSYS_LEVEL_ERROR, "Error making new config live. cmap database may be inconsistent\n");
756 /* Return res from icmap */
757 goto reload_fini;
758 }
759
760 /* Copy into live system */
761 totempg_put_config(&new_config);
762 totemconfig_commit_new_params(&new_config, temp_map);
763 free(new_config.interfaces);
764
765 reload_fini:
766 /* All done - let clients know */
767 icmap_set_int32("config.reload_status", res);
768 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
769 icmap_set_uint8("config.reload_in_progress", 0);
770
771 /* Finished with the temporary storage */
772 free(new_config.orig_interfaces);
773
774 reload_fini_nofree:
775 icmap_fini_r(temp_map);
776
777 reload_fini_nomap:
778 /* All done, return result to the caller if it was on this system */
779 if (nodeid == api->totem_nodeid_get()) {
780 res_lib_cfg_reload_config.header.size = sizeof(res_lib_cfg_reload_config);
781 res_lib_cfg_reload_config.header.id = MESSAGE_RES_CFG_RELOAD_CONFIG;
782 res_lib_cfg_reload_config.header.error = res;
783 api->ipc_response_send(req_exec_cfg_reload_config->source.conn,
784 &res_lib_cfg_reload_config,
785 sizeof(res_lib_cfg_reload_config));
786 api->ipc_refcnt_dec(req_exec_cfg_reload_config->source.conn);;
787 }
788
789 LEAVE();
790 }
791
792 /*
793 * Library Interface Implementation
794 */
795 static void message_handler_req_lib_cfg_ringstatusget (
796 void *conn,
797 const void *msg)
798 {
799 struct res_lib_cfg_ringstatusget res_lib_cfg_ringstatusget;
800 struct totem_ip_address interfaces[INTERFACE_MAX];
801 unsigned int iface_count;
802 char **status;
803 const char *totem_ip_string;
804 char ifname[CFG_INTERFACE_NAME_MAX_LEN];
805 unsigned int iface_ids[INTERFACE_MAX];
806 unsigned int i;
807 cs_error_t res = CS_OK;
808
809 ENTER();
810
811 res_lib_cfg_ringstatusget.header.id = MESSAGE_RES_CFG_RINGSTATUSGET;
812 res_lib_cfg_ringstatusget.header.size = sizeof (struct res_lib_cfg_ringstatusget);
813
814 api->totem_ifaces_get (
815 api->totem_nodeid_get(),
816 iface_ids,
817 interfaces,
818 INTERFACE_MAX,
819 &status,
820 &iface_count);
821
822 assert(iface_count <= CFG_MAX_INTERFACES);
823
824 res_lib_cfg_ringstatusget.interface_count = iface_count;
825
826 for (i = 0; i < iface_count; i++) {
827 totem_ip_string
828 = (const char *)api->totem_ip_print (&interfaces[i]);
829
830 if (!totem_ip_string) {
831 totem_ip_string="";
832 }
833
834 /* Allow for i/f number at the start */
835 if (strlen(totem_ip_string) >= CFG_INTERFACE_NAME_MAX_LEN-3) {
836 log_printf(LOGSYS_LEVEL_ERROR, "String representation of interface %u is too long", i);
837 res = CS_ERR_NAME_TOO_LONG;
838 goto send_response;
839 }
840 snprintf(ifname, sizeof(ifname), "%d %s", iface_ids[i], totem_ip_string);
841
842 if (strlen(status[i]) >= CFG_INTERFACE_STATUS_MAX_LEN) {
843 log_printf(LOGSYS_LEVEL_ERROR, "Status string for interface %u is too long", i);
844 res = CS_ERR_NAME_TOO_LONG;
845 goto send_response;
846 }
847
848 strcpy ((char *)&res_lib_cfg_ringstatusget.interface_status[i],
849 status[i]);
850 strcpy ((char *)&res_lib_cfg_ringstatusget.interface_name[i],
851 ifname);
852 }
853
854 send_response:
855 res_lib_cfg_ringstatusget.header.error = res;
856 api->ipc_response_send (
857 conn,
858 &res_lib_cfg_ringstatusget,
859 sizeof (struct res_lib_cfg_ringstatusget));
860
861 LEAVE();
862 }
863
864 static void message_handler_req_lib_cfg_ringreenable (
865 void *conn,
866 const void *msg)
867 {
868 struct res_lib_cfg_ringreenable res_lib_cfg_ringreenable;
869 ENTER();
870
871 res_lib_cfg_ringreenable.header.id = MESSAGE_RES_CFG_RINGREENABLE;
872 res_lib_cfg_ringreenable.header.size = sizeof (struct res_lib_cfg_ringreenable);
873 res_lib_cfg_ringreenable.header.error = CS_ERR_NOT_SUPPORTED;
874 api->ipc_response_send (
875 conn, &res_lib_cfg_ringreenable,
876 sizeof (struct res_lib_cfg_ringreenable));
877
878 LEAVE();
879 }
880
881 static void message_handler_req_lib_cfg_killnode (
882 void *conn,
883 const void *msg)
884 {
885 const struct req_lib_cfg_killnode *req_lib_cfg_killnode = msg;
886 struct res_lib_cfg_killnode res_lib_cfg_killnode;
887 struct req_exec_cfg_killnode req_exec_cfg_killnode;
888 struct iovec iovec;
889
890 ENTER();
891 req_exec_cfg_killnode.header.size =
892 sizeof (struct req_exec_cfg_killnode);
893 req_exec_cfg_killnode.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
894 MESSAGE_REQ_EXEC_CFG_KILLNODE);
895 req_exec_cfg_killnode.nodeid = req_lib_cfg_killnode->nodeid;
896 marshall_to_mar_name_t(&req_exec_cfg_killnode.reason, &req_lib_cfg_killnode->reason);
897
898 iovec.iov_base = (char *)&req_exec_cfg_killnode;
899 iovec.iov_len = sizeof (struct req_exec_cfg_killnode);
900
901 (void)api->totem_mcast (&iovec, 1, TOTEM_SAFE);
902
903 res_lib_cfg_killnode.header.size = sizeof(struct res_lib_cfg_killnode);
904 res_lib_cfg_killnode.header.id = MESSAGE_RES_CFG_KILLNODE;
905 res_lib_cfg_killnode.header.error = CS_OK;
906
907 api->ipc_response_send(conn, &res_lib_cfg_killnode,
908 sizeof(res_lib_cfg_killnode));
909
910 LEAVE();
911 }
912
913
914 static void message_handler_req_lib_cfg_tryshutdown (
915 void *conn,
916 const void *msg)
917 {
918 struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
919 const struct req_lib_cfg_tryshutdown *req_lib_cfg_tryshutdown = msg;
920 struct qb_list_head *iter;
921
922 ENTER();
923
924 if (req_lib_cfg_tryshutdown->flags == CFG_SHUTDOWN_FLAG_IMMEDIATE) {
925 struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
926
927 /*
928 * Tell other nodes
929 */
930 send_shutdown();
931
932 res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
933 res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
934 res_lib_cfg_tryshutdown.header.error = CS_OK;
935 api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
936 sizeof(res_lib_cfg_tryshutdown));
937
938 LEAVE();
939 return;
940 }
941
942 /*
943 * Shutdown in progress, return an error
944 */
945 if (shutdown_con) {
946 struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
947
948 res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
949 res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
950 res_lib_cfg_tryshutdown.header.error = CS_ERR_EXIST;
951
952 api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
953 sizeof(res_lib_cfg_tryshutdown));
954
955
956 LEAVE();
957
958 return;
959 }
960
961 ci->conn = conn;
962 shutdown_con = (struct cfg_info *)api->ipc_private_data_get (conn);
963 shutdown_flags = req_lib_cfg_tryshutdown->flags;
964 shutdown_yes = 0;
965 shutdown_no = 0;
966
967 /*
968 * Count the number of listeners
969 */
970 shutdown_expected = 0;
971
972 qb_list_for_each(iter, &trackers_list) {
973 struct cfg_info *testci = qb_list_entry(iter, struct cfg_info, list);
974 /*
975 * It is assumed that we will allow shutdown
976 */
977 if (testci != ci) {
978 testci->shutdown_reply = SHUTDOWN_REPLY_UNKNOWN;
979 shutdown_expected++;
980 }
981 }
982
983 /*
984 * If no-one is listening for events then we can just go down now
985 */
986 if (shutdown_expected == 0) {
987 struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
988
989 res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
990 res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
991 res_lib_cfg_tryshutdown.header.error = CS_OK;
992
993 /*
994 * Tell originator that shutdown was confirmed
995 */
996 api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
997 sizeof(res_lib_cfg_tryshutdown));
998
999 send_shutdown();
1000 LEAVE();
1001 return;
1002 }
1003 else {
1004 unsigned int shutdown_timeout = DEFAULT_SHUTDOWN_TIMEOUT;
1005
1006 /*
1007 * Look for a shutdown timeout in configuration map
1008 */
1009 icmap_get_uint32("cfg.shutdown_timeout", &shutdown_timeout);
1010
1011 /*
1012 * Start the timer. If we don't get a full set of replies before this goes
1013 * off we'll cancel the shutdown
1014 */
1015 api->timer_add_duration((unsigned long long)shutdown_timeout*1000000000, NULL,
1016 shutdown_timer_fn, &shutdown_timer);
1017
1018 /*
1019 * Tell the users we would like to shut down
1020 */
1021 send_test_shutdown(NULL, conn, CS_OK);
1022 }
1023
1024 /*
1025 * We don't sent a reply to the caller here.
1026 * We send it when we know if we can shut down or not
1027 */
1028
1029 LEAVE();
1030 }
1031
1032 static void message_handler_req_lib_cfg_replytoshutdown (
1033 void *conn,
1034 const void *msg)
1035 {
1036 struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
1037 const struct req_lib_cfg_replytoshutdown *req_lib_cfg_replytoshutdown = msg;
1038 struct res_lib_cfg_replytoshutdown res_lib_cfg_replytoshutdown;
1039 int status = CS_OK;
1040
1041 ENTER();
1042 if (!shutdown_con) {
1043 status = CS_ERR_ACCESS;
1044 goto exit_fn;
1045 }
1046
1047 if (req_lib_cfg_replytoshutdown->response) {
1048 shutdown_yes++;
1049 ci->shutdown_reply = SHUTDOWN_REPLY_YES;
1050 }
1051 else {
1052 shutdown_no++;
1053 ci->shutdown_reply = SHUTDOWN_REPLY_NO;
1054 }
1055 check_shutdown_status();
1056
1057 exit_fn:
1058 res_lib_cfg_replytoshutdown.header.error = status;
1059 res_lib_cfg_replytoshutdown.header.id = MESSAGE_RES_CFG_REPLYTOSHUTDOWN;
1060 res_lib_cfg_replytoshutdown.header.size = sizeof(res_lib_cfg_replytoshutdown);
1061
1062 api->ipc_response_send(conn, &res_lib_cfg_replytoshutdown,
1063 sizeof(res_lib_cfg_replytoshutdown));
1064
1065 LEAVE();
1066 }
1067
1068 static void message_handler_req_lib_cfg_get_node_addrs (void *conn,
1069 const void *msg)
1070 {
1071 struct totem_ip_address node_ifs[INTERFACE_MAX];
1072 unsigned int iface_ids[INTERFACE_MAX];
1073 char buf[PIPE_BUF];
1074 char **status;
1075 unsigned int num_interfaces = 0;
1076 struct sockaddr_storage *ss;
1077 int ret = CS_OK;
1078 int i;
1079 int live_addrs = 0;
1080 const struct req_lib_cfg_get_node_addrs *req_lib_cfg_get_node_addrs = msg;
1081 struct res_lib_cfg_get_node_addrs *res_lib_cfg_get_node_addrs = (struct res_lib_cfg_get_node_addrs *)buf;
1082 unsigned int nodeid = req_lib_cfg_get_node_addrs->nodeid;
1083 char *addr_buf;
1084
1085 if (nodeid == 0)
1086 nodeid = api->totem_nodeid_get();
1087
1088 if (api->totem_ifaces_get(nodeid, iface_ids, node_ifs, INTERFACE_MAX, &status, &num_interfaces)) {
1089 ret = CS_ERR_EXIST;
1090 num_interfaces = 0;
1091 }
1092
1093 res_lib_cfg_get_node_addrs->header.size = sizeof(struct res_lib_cfg_get_node_addrs) + (num_interfaces * TOTEMIP_ADDRLEN);
1094 res_lib_cfg_get_node_addrs->header.id = MESSAGE_RES_CFG_GET_NODE_ADDRS;
1095 res_lib_cfg_get_node_addrs->header.error = ret;
1096 if (num_interfaces) {
1097 res_lib_cfg_get_node_addrs->family = node_ifs[0].family;
1098 for (i = 0, addr_buf = (char *)res_lib_cfg_get_node_addrs->addrs;
1099 i < num_interfaces; i++) {
1100 ss = (struct sockaddr_storage *)&node_ifs[i].addr;
1101 if (ss->ss_family) {
1102 memcpy(addr_buf, node_ifs[i].addr, TOTEMIP_ADDRLEN);
1103 live_addrs++;
1104 addr_buf += TOTEMIP_ADDRLEN;
1105 }
1106 }
1107 res_lib_cfg_get_node_addrs->num_addrs = live_addrs;
1108 } else {
1109 res_lib_cfg_get_node_addrs->header.error = CS_ERR_NOT_EXIST;
1110 }
1111 api->ipc_response_send(conn, res_lib_cfg_get_node_addrs, res_lib_cfg_get_node_addrs->header.size);
1112 }
1113
1114 static void message_handler_req_lib_cfg_local_get (void *conn, const void *msg)
1115 {
1116 struct res_lib_cfg_local_get res_lib_cfg_local_get;
1117
1118 res_lib_cfg_local_get.header.size = sizeof(res_lib_cfg_local_get);
1119 res_lib_cfg_local_get.header.id = MESSAGE_RES_CFG_LOCAL_GET;
1120 res_lib_cfg_local_get.header.error = CS_OK;
1121 res_lib_cfg_local_get.local_nodeid = api->totem_nodeid_get ();
1122
1123 api->ipc_response_send(conn, &res_lib_cfg_local_get,
1124 sizeof(res_lib_cfg_local_get));
1125 }
1126
1127 static void message_handler_req_lib_cfg_reload_config (void *conn, const void *msg)
1128 {
1129 struct req_exec_cfg_reload_config req_exec_cfg_reload_config;
1130 struct iovec iovec;
1131
1132 ENTER();
1133
1134 req_exec_cfg_reload_config.header.size =
1135 sizeof (struct req_exec_cfg_reload_config);
1136 req_exec_cfg_reload_config.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
1137 MESSAGE_REQ_EXEC_CFG_RELOAD_CONFIG);
1138 api->ipc_source_set (&req_exec_cfg_reload_config.source, conn);
1139 api->ipc_refcnt_inc(conn);
1140
1141 iovec.iov_base = (char *)&req_exec_cfg_reload_config;
1142 iovec.iov_len = sizeof (struct req_exec_cfg_reload_config);
1143
1144 assert (api->totem_mcast (&iovec, 1, TOTEM_SAFE) == 0);
1145
1146 LEAVE();
1147 }
1148
1149 static void message_handler_req_lib_cfg_reopen_log_files (void *conn, const void *msg)
1150 {
1151 struct res_lib_cfg_reopen_log_files res_lib_cfg_reopen_log_files;
1152 cs_error_t res;
1153
1154 ENTER();
1155
1156 log_printf(LOGSYS_LEVEL_DEBUG, "Reopening logging files\n");
1157
1158 res = logsys_reopen_log_files();
1159
1160 res_lib_cfg_reopen_log_files.header.size = sizeof(res_lib_cfg_reopen_log_files);
1161 res_lib_cfg_reopen_log_files.header.id = MESSAGE_RES_CFG_REOPEN_LOG_FILES;
1162 res_lib_cfg_reopen_log_files.header.error = res;
1163 api->ipc_response_send(conn,
1164 &res_lib_cfg_reopen_log_files,
1165 sizeof(res_lib_cfg_reopen_log_files));
1166
1167 LEAVE();
1168 }