From 4f5791ebd03eaec1c7da0865a383175b05102712 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 19:47:29 +0200 Subject: Adding upstream version 2:4.17.12+dfsg. Signed-off-by: Daniel Baumann --- ctdb/.bzrignore | 24 + ctdb/.gitignore | 32 + ctdb/COPYING | 674 ++ ctdb/Makefile | 83 + ctdb/README | 8 + ctdb/client/client.h | 1416 +++++ ctdb/client/client_call.c | 184 + ctdb/client/client_connect.c | 532 ++ ctdb/client/client_control.c | 439 ++ ctdb/client/client_control_sync.c | 2676 ++++++++ ctdb/client/client_db.c | 2791 +++++++++ ctdb/client/client_event.c | 444 ++ ctdb/client/client_message.c | 607 ++ ctdb/client/client_message_sync.c | 197 + ctdb/client/client_private.h | 99 + ctdb/client/client_sync.h | 526 ++ ctdb/client/client_tunnel.c | 693 +++ ctdb/client/client_util.c | 137 + ctdb/cluster/cluster_conf.c | 182 + ctdb/cluster/cluster_conf.h | 36 + ctdb/common/cmdline.c | 598 ++ ctdb/common/cmdline.h | 163 + ctdb/common/comm.c | 427 ++ ctdb/common/comm.h | 101 + ctdb/common/common.h | 159 + ctdb/common/conf.c | 1391 +++++ ctdb/common/conf.h | 473 ++ ctdb/common/conf_tool.c | 321 + ctdb/common/conf_tool.h | 39 + ctdb/common/ctdb_io.c | 498 ++ ctdb/common/ctdb_ltdb.c | 430 ++ ctdb/common/ctdb_util.c | 674 ++ ctdb/common/db_hash.c | 295 + ctdb/common/db_hash.h | 174 + ctdb/common/event_script.c | 247 + ctdb/common/event_script.h | 72 + ctdb/common/hash_count.c | 219 + ctdb/common/hash_count.h | 94 + ctdb/common/line.c | 145 + ctdb/common/line.h | 62 + ctdb/common/logging.c | 745 +++ ctdb/common/logging.h | 51 + ctdb/common/logging_conf.c | 127 + ctdb/common/logging_conf.h | 36 + ctdb/common/path.c | 211 + ctdb/common/path.h | 39 + ctdb/common/path_tool.c | 384 ++ ctdb/common/path_tool.h | 38 + ctdb/common/pidfile.c | 85 + ctdb/common/pidfile.h | 51 + ctdb/common/pkt_read.c | 190 + ctdb/common/pkt_read.h | 98 + ctdb/common/pkt_write.c | 101 + ctdb/common/pkt_write.h | 79 + ctdb/common/rb_tree.c | 1101 ++++ ctdb/common/rb_tree.h | 90 + ctdb/common/reqid.c | 89 + ctdb/common/reqid.h | 89 + ctdb/common/run_event.c | 829 +++ ctdb/common/run_event.h | 150 + ctdb/common/run_proc.c | 503 ++ ctdb/common/run_proc.h | 100 + ctdb/common/sock_client.c | 334 + ctdb/common/sock_client.h | 129 + ctdb/common/sock_daemon.c | 1100 ++++ ctdb/common/sock_daemon.h | 283 + ctdb/common/sock_io.c | 328 + ctdb/common/sock_io.h | 39 + ctdb/common/srvid.c | 280 + ctdb/common/srvid.h | 121 + ctdb/common/system.c | 237 + ctdb/common/system.h | 37 + ctdb/common/system_socket.c | 1168 ++++ ctdb/common/system_socket.h | 46 + ctdb/common/tmon.c | 602 ++ ctdb/common/tmon.h | 218 + ctdb/common/tunable.c | 401 ++ ctdb/common/tunable.h | 35 + ctdb/config/README | 31 + ctdb/config/ctdb-crash-cleanup.sh | 27 + ctdb/config/ctdb.conf | 22 + ctdb/config/ctdb.init | 161 + ctdb/config/ctdb.sudoers | 3 + ctdb/config/ctdb.sysconfig | 11 + ctdb/config/ctdb.tunables | 2 + ctdb/config/debug-hung-script.sh | 61 + ctdb/config/debug_locks.sh | 218 + ctdb/config/events/README | 193 + ctdb/config/events/legacy/00.ctdb.script | 130 + ctdb/config/events/legacy/01.reclock.script | 34 + ctdb/config/events/legacy/05.system.script | 196 + ctdb/config/events/legacy/06.nfs.script | 39 + ctdb/config/events/legacy/10.interface.script | 262 + ctdb/config/events/legacy/11.natgw.script | 242 + ctdb/config/events/legacy/11.routing.script | 49 + ctdb/config/events/legacy/13.per_ip_routing.script | 438 ++ ctdb/config/events/legacy/20.multipathd.script | 83 + ctdb/config/events/legacy/31.clamd.script | 37 + ctdb/config/events/legacy/40.vsftpd.script | 56 + ctdb/config/events/legacy/41.httpd.script | 78 + ctdb/config/events/legacy/48.netbios.script | 64 + ctdb/config/events/legacy/49.winbind.script | 55 + ctdb/config/events/legacy/50.samba.script | 163 + ctdb/config/events/legacy/60.nfs.script | 299 + ctdb/config/events/legacy/70.iscsi.script | 87 + ctdb/config/events/legacy/91.lvs.script | 124 + ctdb/config/functions | 1037 ++++ ctdb/config/nfs-checks.d/00.portmapper.check | 2 + ctdb/config/nfs-checks.d/10.status.check | 7 + ctdb/config/nfs-checks.d/20.nfs.check | 7 + ctdb/config/nfs-checks.d/30.nlockmgr.check | 6 + ctdb/config/nfs-checks.d/40.mountd.check | 7 + ctdb/config/nfs-checks.d/50.rquotad.check | 7 + ctdb/config/nfs-checks.d/README | 31 + ctdb/config/nfs-linux-kernel-callout | 441 ++ ctdb/config/notification.README | 36 + ctdb/config/notify.sh | 19 + ctdb/config/script.options | 16 + ctdb/config/statd-callout | 254 + ctdb/configure | 22 + ctdb/configure.rpm | 20 + ctdb/database/database_conf.c | 165 + ctdb/database/database_conf.h | 35 + ctdb/doc/cluster_mutex_helper.txt | 80 + ctdb/doc/ctdb-etcd.7.xml | 119 + ctdb/doc/ctdb-script.options.5.xml | 1076 ++++ ctdb/doc/ctdb-statistics.7.xml | 689 +++ ctdb/doc/ctdb-tunables.7.xml | 772 +++ ctdb/doc/ctdb.1.xml | 1863 ++++++ ctdb/doc/ctdb.7.xml | 1182 ++++ ctdb/doc/ctdb.conf.5.xml | 652 ++ ctdb/doc/ctdb.sysconfig.5.xml | 240 + ctdb/doc/ctdb_diagnostics.1.xml | 128 + ctdb/doc/ctdb_mutex_ceph_rados_helper.7.xml | 96 + ctdb/doc/ctdbd.1.xml | 129 + ctdb/doc/examples/11.natgw.options | 25 + ctdb/doc/examples/20.nfs_ganesha.check | 8 + ctdb/doc/examples/91.lvs.options | 12 + ctdb/doc/examples/README | 15 + ctdb/doc/examples/config_migrate.sh | 741 +++ ctdb/doc/examples/config_migrate.test_input | 50 + ctdb/doc/examples/ctdb.conf | 73 + ctdb/doc/examples/ctdb.spec.in | 291 + ctdb/doc/examples/nfs-ganesha-callout | 352 ++ ctdb/doc/ltdbtool.1.xml | 300 + ctdb/doc/onnode.1.xml | 315 + ctdb/doc/ping_pong.1.xml | 164 + ctdb/doc/readonlyrecords.txt | 343 ++ ctdb/event/event.h | 55 + ctdb/event/event_client.c | 351 ++ ctdb/event/event_cmd.c | 358 ++ ctdb/event/event_conf.c | 85 + ctdb/event/event_conf.h | 31 + ctdb/event/event_config.c | 122 + ctdb/event/event_context.c | 472 ++ ctdb/event/event_daemon.c | 382 ++ ctdb/event/event_private.h | 103 + ctdb/event/event_protocol.c | 1123 ++++ ctdb/event/event_protocol.h | 100 + ctdb/event/event_protocol_api.h | 61 + ctdb/event/event_protocol_test.c | 412 ++ ctdb/event/event_protocol_util.c | 46 + ctdb/event/event_request.c | 217 + ctdb/event/event_tool.c | 792 +++ ctdb/event/event_tool.h | 38 + ctdb/failover/failover_conf.c | 53 + ctdb/failover/failover_conf.h | 31 + ctdb/ib/README.txt | 10 + ctdb/ib/ibw_ctdb.c | 185 + ctdb/ib/ibw_ctdb.h | 51 + ctdb/ib/ibw_ctdb_init.c | 255 + ctdb/ib/ibwrapper.c | 1361 ++++ ctdb/ib/ibwrapper.h | 218 + ctdb/ib/ibwrapper_internal.h | 126 + ctdb/ib/ibwrapper_test.c | 659 ++ ctdb/include/common/srvid.h | 1 + ctdb/include/ctdb_client.h | 289 + ctdb/include/ctdb_private.h | 1035 ++++ ctdb/include/ctdb_protocol.h | 301 + ctdb/include/public/README.txt | 6 + ctdb/include/public/util/README.txt | 6 + ctdb/protocol/protocol.h | 1074 ++++ ctdb/protocol/protocol_api.h | 682 ++ ctdb/protocol/protocol_basic.c | 400 ++ ctdb/protocol/protocol_basic.h | 86 + ctdb/protocol/protocol_call.c | 581 ++ ctdb/protocol/protocol_client.c | 2352 +++++++ ctdb/protocol/protocol_control.c | 1998 ++++++ ctdb/protocol/protocol_debug.c | 742 +++ ctdb/protocol/protocol_header.c | 169 + ctdb/protocol/protocol_keepalive.c | 95 + ctdb/protocol/protocol_message.c | 476 ++ ctdb/protocol/protocol_packet.c | 48 + ctdb/protocol/protocol_private.h | 300 + ctdb/protocol/protocol_sock.c | 81 + ctdb/protocol/protocol_tunnel.c | 114 + ctdb/protocol/protocol_types.c | 5348 ++++++++++++++++ ctdb/protocol/protocol_util.c | 755 +++ ctdb/protocol/protocol_util.h | 80 + ctdb/server/ctdb_banning.c | 146 + ctdb/server/ctdb_call.c | 2082 +++++++ ctdb/server/ctdb_client.c | 1709 +++++ ctdb/server/ctdb_cluster_mutex.c | 382 ++ ctdb/server/ctdb_cluster_mutex.h | 51 + ctdb/server/ctdb_config.c | 183 + ctdb/server/ctdb_config.h | 59 + ctdb/server/ctdb_control.c | 1089 ++++ ctdb/server/ctdb_daemon.c | 2248 +++++++ ctdb/server/ctdb_fork.c | 216 + ctdb/server/ctdb_freeze.c | 923 +++ ctdb/server/ctdb_keepalive.c | 234 + ctdb/server/ctdb_lock.c | 996 +++ ctdb/server/ctdb_lock_helper.c | 350 ++ ctdb/server/ctdb_logging.c | 174 + ctdb/server/ctdb_ltdb_server.c | 1663 +++++ ctdb/server/ctdb_monitor.c | 509 ++ ctdb/server/ctdb_mutex_fcntl_helper.c | 794 +++ ctdb/server/ctdb_persistent.c | 397 ++ ctdb/server/ctdb_recover.c | 1243 ++++ ctdb/server/ctdb_recoverd.c | 3286 ++++++++++ ctdb/server/ctdb_recovery_helper.c | 3200 ++++++++++ ctdb/server/ctdb_server.c | 608 ++ ctdb/server/ctdb_statistics.c | 93 + ctdb/server/ctdb_takeover.c | 2653 ++++++++ ctdb/server/ctdb_takeover_helper.c | 1276 ++++ ctdb/server/ctdb_traverse.c | 781 +++ ctdb/server/ctdb_tunables.c | 170 + ctdb/server/ctdb_tunnel.c | 141 + ctdb/server/ctdb_update_record.c | 372 ++ ctdb/server/ctdb_uptime.c | 55 + ctdb/server/ctdb_vacuum.c | 1990 ++++++ ctdb/server/ctdbd.c | 405 ++ ctdb/server/eventscript.c | 845 +++ ctdb/server/ipalloc.c | 284 + ctdb/server/ipalloc.h | 67 + ctdb/server/ipalloc_common.c | 192 + ctdb/server/ipalloc_deterministic.c | 63 + ctdb/server/ipalloc_lcp2.c | 525 ++ ctdb/server/ipalloc_nondeterministic.c | 150 + ctdb/server/ipalloc_private.h | 57 + ctdb/server/legacy_conf.c | 80 + ctdb/server/legacy_conf.h | 35 + ctdb/tcp/ctdb_tcp.h | 56 + ctdb/tcp/tcp_connect.c | 599 ++ ctdb/tcp/tcp_init.c | 215 + ctdb/tcp/tcp_io.c | 96 + .../CLUSTER/complex/11_ctdb_delip_removes_ip.sh | 43 + ctdb/tests/CLUSTER/complex/18_ctdb_reloadips.sh | 257 + .../tests/CLUSTER/complex/30_nfs_tickle_killtcp.sh | 57 + ctdb/tests/CLUSTER/complex/31_nfs_tickle.sh | 77 + ctdb/tests/CLUSTER/complex/32_cifs_tickle.sh | 69 + ctdb/tests/CLUSTER/complex/33_gratuitous_arp.sh | 74 + .../tests/CLUSTER/complex/34_nfs_tickle_restart.sh | 81 + ctdb/tests/CLUSTER/complex/36_smb_reset_server.sh | 78 + ctdb/tests/CLUSTER/complex/37_nfs_reset_server.sh | 78 + .../CLUSTER/complex/41_failover_ping_discrete.sh | 56 + .../CLUSTER/complex/42_failover_ssh_hostname.sh | 70 + .../tests/CLUSTER/complex/43_failover_nfs_basic.sh | 62 + .../CLUSTER/complex/44_failover_nfs_oneway.sh | 82 + ctdb/tests/CLUSTER/complex/45_failover_nfs_kill.sh | 69 + ctdb/tests/CLUSTER/complex/60_rogueip_releaseip.sh | 56 + ctdb/tests/CLUSTER/complex/61_rogueip_takeip.sh | 46 + ctdb/tests/CLUSTER/complex/README | 2 + ctdb/tests/CLUSTER/complex/scripts/local.bash | 289 + .../INTEGRATION/database/basics.001.attach.sh | 48 + .../INTEGRATION/database/basics.002.attach.sh | 116 + .../INTEGRATION/database/basics.003.detach.sh | 166 + ctdb/tests/INTEGRATION/database/basics.004.wipe.sh | 56 + .../database/basics.010.backup_restore.sh | 97 + ctdb/tests/INTEGRATION/database/fetch.001.ring.sh | 34 + .../INTEGRATION/database/fetch.002.ring-hotkeys.sh | 161 + .../INTEGRATION/database/readonly.001.basic.sh | 178 + .../INTEGRATION/database/recovery.001.volatile.sh | 118 + .../INTEGRATION/database/recovery.002.large.sh | 106 + .../database/recovery.003.no_resurrect.sh | 63 + .../database/recovery.010.persistent.sh | 103 + .../INTEGRATION/database/recovery.011.continue.sh | 73 + ctdb/tests/INTEGRATION/database/scripts/local.bash | 116 + .../INTEGRATION/database/transaction.001.ptrans.sh | 110 + .../INTEGRATION/database/transaction.002.loop.sh | 28 + .../database/transaction.003.loop_recovery.sh | 50 + .../database/transaction.004.update_record.sh | 80 + .../database/transaction.010.loop_recovery.sh | 51 + .../tests/INTEGRATION/database/traverse.001.one.sh | 116 + .../INTEGRATION/database/traverse.002.many.sh | 52 + ctdb/tests/INTEGRATION/database/vacuum.001.fast.sh | 159 + ctdb/tests/INTEGRATION/database/vacuum.002.full.sh | 96 + .../INTEGRATION/database/vacuum.003.recreate.sh | 139 + .../INTEGRATION/database/vacuum.030.locked.sh | 102 + .../INTEGRATION/database/vacuum.031.locked.sh | 114 + .../INTEGRATION/database/vacuum.032.locked.sh | 102 + .../INTEGRATION/database/vacuum.033.locked.sh | 117 + .../INTEGRATION/database/vacuum.034.locked.sh | 129 + ctdb/tests/INTEGRATION/failover/pubips.001.list.sh | 48 + .../tests/INTEGRATION/failover/pubips.010.addip.sh | 25 + .../tests/INTEGRATION/failover/pubips.011.delip.sh | 16 + .../INTEGRATION/failover/pubips.012.reloadips.sh | 117 + .../failover/pubips.013.failover_noop.sh | 44 + .../INTEGRATION/failover/pubips.014.iface_gc.sh | 51 + .../INTEGRATION/failover/pubips.020.moveip.sh | 76 + .../failover/pubips.030.disable_enable.sh | 23 + .../failover/pubips.032.stop_continue.sh | 21 + .../failover/pubips.040.NoIPTakeover.sh | 71 + .../INTEGRATION/failover/pubips.050.missing_ip.sh | 71 + ctdb/tests/INTEGRATION/simple/README | 2 + ctdb/tests/INTEGRATION/simple/basics.000.onnode.sh | 12 + .../INTEGRATION/simple/basics.001.listnodes.sh | 38 + .../INTEGRATION/simple/basics.002.tunables.sh | 67 + ctdb/tests/INTEGRATION/simple/basics.003.ping.sh | 34 + ctdb/tests/INTEGRATION/simple/basics.004.getpid.sh | 55 + .../simple/basics.005.process_exists.sh | 66 + .../INTEGRATION/simple/basics.010.statistics.sh | 17 + .../simple/basics.011.statistics_reset.sh | 62 + .../simple/cluster.001.stop_leader_yield.sh | 26 + .../simple/cluster.002.ban_leader_yield.sh | 26 + .../simple/cluster.003.capability_leader_yield.sh | 24 + .../cluster.006.stop_leader_yield_no_lock.sh | 30 + .../simple/cluster.007.ban_leader_yield_no_lock.sh | 30 + .../cluster.008.capability_leader_yield_no_lock.sh | 29 + .../INTEGRATION/simple/cluster.010.getrelock.sh | 24 + .../simple/cluster.012.reclock_command.sh | 20 + .../simple/cluster.015.reclock_remove_lock.sh | 80 + .../simple/cluster.016.reclock_move_lock_dir.sh | 92 + .../INTEGRATION/simple/cluster.020.message_ring.sh | 53 + .../INTEGRATION/simple/cluster.021.tunnel_ring.sh | 34 + .../cluster.030.node_stall_leader_timeout.sh | 48 + .../INTEGRATION/simple/cluster.090.unreachable.sh | 39 + .../simple/cluster.091.version_check.sh | 55 + .../tests/INTEGRATION/simple/debug.001.getdebug.sh | 42 + .../tests/INTEGRATION/simple/debug.002.setdebug.sh | 74 + .../INTEGRATION/simple/debug.003.dumpmemory.sh | 18 + .../simple/eventscripts.001.zero_scripts.sh | 16 + .../simple/eventscripts.090.debug_hung.sh | 76 + ctdb/tests/README | 145 + ctdb/tests/TODO | 4 + ctdb/tests/UNIT/cunit/cluster_mutex_001.sh | 66 + ctdb/tests/UNIT/cunit/cluster_mutex_002.sh | 132 + ctdb/tests/UNIT/cunit/cluster_mutex_003.sh | 75 + ctdb/tests/UNIT/cunit/cmdline_test_001.sh | 98 + ctdb/tests/UNIT/cunit/comm_test_001.sh | 13 + ctdb/tests/UNIT/cunit/comm_test_002.sh | 24 + ctdb/tests/UNIT/cunit/conf_test_001.sh | 196 + ctdb/tests/UNIT/cunit/config_test_001.sh | 115 + ctdb/tests/UNIT/cunit/config_test_002.sh | 65 + ctdb/tests/UNIT/cunit/config_test_003.sh | 52 + ctdb/tests/UNIT/cunit/config_test_004.sh | 144 + ctdb/tests/UNIT/cunit/config_test_005.sh | 97 + ctdb/tests/UNIT/cunit/config_test_006.sh | 56 + ctdb/tests/UNIT/cunit/config_test_007.sh | 24 + ctdb/tests/UNIT/cunit/ctdb_io_test_001.sh | 10 + ctdb/tests/UNIT/cunit/db_hash_test_001.sh | 7 + ctdb/tests/UNIT/cunit/event_protocol_test_001.sh | 7 + ctdb/tests/UNIT/cunit/event_script_test_001.sh | 127 + ctdb/tests/UNIT/cunit/hash_count_test_001.sh | 7 + ctdb/tests/UNIT/cunit/line_test_001.sh | 90 + ctdb/tests/UNIT/cunit/path_tests_001.sh | 62 + ctdb/tests/UNIT/cunit/pidfile_test_001.sh | 8 + ctdb/tests/UNIT/cunit/pkt_read_001.sh | 7 + ctdb/tests/UNIT/cunit/pkt_write_001.sh | 7 + ctdb/tests/UNIT/cunit/porting_tests_001.sh | 15 + ctdb/tests/UNIT/cunit/protocol_test_001.sh | 7 + ctdb/tests/UNIT/cunit/protocol_test_002.sh | 7 + ctdb/tests/UNIT/cunit/protocol_test_012.sh | 7 + ctdb/tests/UNIT/cunit/protocol_test_101.sh | 7 + ctdb/tests/UNIT/cunit/protocol_test_111.sh | 7 + ctdb/tests/UNIT/cunit/protocol_test_201.sh | 6 + ctdb/tests/UNIT/cunit/rb_test_001.sh | 31 + ctdb/tests/UNIT/cunit/reqid_test_001.sh | 13 + ctdb/tests/UNIT/cunit/run_event_001.sh | 137 + ctdb/tests/UNIT/cunit/run_proc_001.sh | 159 + ctdb/tests/UNIT/cunit/sock_daemon_test_001.sh | 135 + ctdb/tests/UNIT/cunit/sock_io_test_001.sh | 9 + ctdb/tests/UNIT/cunit/srvid_test_001.sh | 7 + ctdb/tests/UNIT/cunit/system_socket_test_001.sh | 6 + ctdb/tests/UNIT/cunit/system_socket_test_002.sh | 68 + ctdb/tests/UNIT/cunit/system_socket_test_003.sh | 42 + ctdb/tests/UNIT/cunit/tmon_test_001.sh | 195 + ctdb/tests/UNIT/cunit/tmon_test_002.sh | 142 + ctdb/tests/UNIT/cunit/tunable_test_001.sh | 312 + ctdb/tests/UNIT/eventd/README | 1 + ctdb/tests/UNIT/eventd/etc-ctdb/ctdb.conf | 6 + ctdb/tests/UNIT/eventd/etc-ctdb/debug-script.sh | 22 + .../eventd/etc-ctdb/events/data/03.notalink.script | 2 + ctdb/tests/UNIT/eventd/etc-ctdb/events/data/README | 1 + .../tests/UNIT/eventd/etc-ctdb/events/empty/README | 1 + .../eventd/etc-ctdb/events/multi/01.test.script | 11 + .../eventd/etc-ctdb/events/multi/02.test.script | 9 + .../eventd/etc-ctdb/events/multi/03.test.script | 9 + .../etc-ctdb/events/random/01.disabled.script | 3 + .../etc-ctdb/events/random/02.enabled.script | 12 + .../eventd/etc-ctdb/events/random/README.script | 1 + .../UNIT/eventd/etc-ctdb/events/random/a.script | 3 + .../etc-ctdb/share/events/data/01.dummy.script | 6 + .../etc-ctdb/share/events/data/02.disabled.script | 6 + .../UNIT/eventd/etc-ctdb/share/events/empty/README | 1 + .../share/events/random/01.disabled.script | 3 + .../etc-ctdb/share/events/random/02.enabled.script | 12 + .../etc-ctdb/share/events/random/README.script | 1 + .../eventd/etc-ctdb/share/events/random/a.script | 3 + ctdb/tests/UNIT/eventd/eventd_001.sh | 27 + ctdb/tests/UNIT/eventd/eventd_002.sh | 21 + ctdb/tests/UNIT/eventd/eventd_003.sh | 45 + ctdb/tests/UNIT/eventd/eventd_004.sh | 33 + ctdb/tests/UNIT/eventd/eventd_005.sh | 34 + ctdb/tests/UNIT/eventd/eventd_006.sh | 19 + ctdb/tests/UNIT/eventd/eventd_007.sh | 19 + ctdb/tests/UNIT/eventd/eventd_008.sh | 19 + ctdb/tests/UNIT/eventd/eventd_009.sh | 155 + ctdb/tests/UNIT/eventd/eventd_011.sh | 40 + ctdb/tests/UNIT/eventd/eventd_012.sh | 27 + ctdb/tests/UNIT/eventd/eventd_013.sh | 27 + ctdb/tests/UNIT/eventd/eventd_014.sh | 27 + ctdb/tests/UNIT/eventd/eventd_021.sh | 26 + ctdb/tests/UNIT/eventd/eventd_022.sh | 22 + ctdb/tests/UNIT/eventd/eventd_023.sh | 22 + ctdb/tests/UNIT/eventd/eventd_024.sh | 31 + ctdb/tests/UNIT/eventd/eventd_031.sh | 17 + ctdb/tests/UNIT/eventd/eventd_032.sh | 43 + ctdb/tests/UNIT/eventd/eventd_033.sh | 43 + ctdb/tests/UNIT/eventd/eventd_041.sh | 26 + ctdb/tests/UNIT/eventd/eventd_042.sh | 29 + ctdb/tests/UNIT/eventd/eventd_043.sh | 29 + ctdb/tests/UNIT/eventd/eventd_044.sh | 37 + ctdb/tests/UNIT/eventd/eventd_051.sh | 15 + ctdb/tests/UNIT/eventd/eventd_052.sh | 35 + ctdb/tests/UNIT/eventd/scripts/local.sh | 122 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.001.sh | 13 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.002.sh | 17 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.003.sh | 16 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.004.sh | 18 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.005.sh | 20 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.006.sh | 23 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.007.sh | 16 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.008.sh | 19 + ctdb/tests/UNIT/eventscripts/00.ctdb.init.009.sh | 51 + .../tests/UNIT/eventscripts/01.reclock.init.001.sh | 10 + .../tests/UNIT/eventscripts/01.reclock.init.002.sh | 10 + .../tests/UNIT/eventscripts/01.reclock.init.003.sh | 20 + .../UNIT/eventscripts/05.system.monitor.001.sh | 13 + .../UNIT/eventscripts/05.system.monitor.002.sh | 14 + .../UNIT/eventscripts/05.system.monitor.003.sh | 17 + .../UNIT/eventscripts/05.system.monitor.004.sh | 15 + .../UNIT/eventscripts/05.system.monitor.005.sh | 17 + .../UNIT/eventscripts/05.system.monitor.006.sh | 17 + .../UNIT/eventscripts/05.system.monitor.007.sh | 14 + .../UNIT/eventscripts/05.system.monitor.011.sh | 13 + .../UNIT/eventscripts/05.system.monitor.012.sh | 14 + .../UNIT/eventscripts/05.system.monitor.014.sh | 18 + .../UNIT/eventscripts/05.system.monitor.015.sh | 20 + .../UNIT/eventscripts/05.system.monitor.017.sh | 20 + .../UNIT/eventscripts/05.system.monitor.018.sh | 82 + .../UNIT/eventscripts/06.nfs.releaseip.001.sh | 12 + .../UNIT/eventscripts/06.nfs.releaseip.002.sh | 12 + ctdb/tests/UNIT/eventscripts/06.nfs.takeip.001.sh | 12 + ctdb/tests/UNIT/eventscripts/06.nfs.takeip.002.sh | 12 + ctdb/tests/UNIT/eventscripts/10.interface.010.sh | 23 + ctdb/tests/UNIT/eventscripts/10.interface.011.sh | 28 + ctdb/tests/UNIT/eventscripts/10.interface.012.sh | 31 + ctdb/tests/UNIT/eventscripts/10.interface.013.sh | 36 + .../UNIT/eventscripts/10.interface.init.001.sh | 13 + .../UNIT/eventscripts/10.interface.init.002.sh | 11 + .../UNIT/eventscripts/10.interface.init.021.sh | 11 + .../UNIT/eventscripts/10.interface.init.022.sh | 18 + .../UNIT/eventscripts/10.interface.init.023.sh | 23 + .../UNIT/eventscripts/10.interface.monitor.001.sh | 13 + .../UNIT/eventscripts/10.interface.monitor.002.sh | 11 + .../UNIT/eventscripts/10.interface.monitor.003.sh | 15 + .../UNIT/eventscripts/10.interface.monitor.004.sh | 15 + .../UNIT/eventscripts/10.interface.monitor.005.sh | 15 + .../UNIT/eventscripts/10.interface.monitor.006.sh | 15 + .../UNIT/eventscripts/10.interface.monitor.009.sh | 19 + .../UNIT/eventscripts/10.interface.monitor.010.sh | 25 + .../UNIT/eventscripts/10.interface.monitor.011.sh | 21 + .../UNIT/eventscripts/10.interface.monitor.012.sh | 29 + .../UNIT/eventscripts/10.interface.monitor.013.sh | 15 + .../UNIT/eventscripts/10.interface.monitor.014.sh | 16 + .../UNIT/eventscripts/10.interface.monitor.015.sh | 16 + .../UNIT/eventscripts/10.interface.monitor.016.sh | 20 + .../UNIT/eventscripts/10.interface.monitor.017.sh | 20 + .../UNIT/eventscripts/10.interface.monitor.018.sh | 20 + .../UNIT/eventscripts/10.interface.multi.001.sh | 14 + .../eventscripts/10.interface.releaseip.001.sh | 13 + .../eventscripts/10.interface.releaseip.002.sh | 14 + .../UNIT/eventscripts/10.interface.startup.001.sh | 13 + .../UNIT/eventscripts/10.interface.startup.002.sh | 11 + .../UNIT/eventscripts/10.interface.takeip.001.sh | 13 + .../UNIT/eventscripts/10.interface.takeip.002.sh | 13 + .../UNIT/eventscripts/10.interface.takeip.003.sh | 22 + ctdb/tests/UNIT/eventscripts/11.natgw.001.sh | 12 + ctdb/tests/UNIT/eventscripts/11.natgw.002.sh | 24 + ctdb/tests/UNIT/eventscripts/11.natgw.003.sh | 24 + ctdb/tests/UNIT/eventscripts/11.natgw.004.sh | 24 + ctdb/tests/UNIT/eventscripts/11.natgw.011.sh | 23 + ctdb/tests/UNIT/eventscripts/11.natgw.012.sh | 23 + ctdb/tests/UNIT/eventscripts/11.natgw.013.sh | 27 + ctdb/tests/UNIT/eventscripts/11.natgw.014.sh | 27 + ctdb/tests/UNIT/eventscripts/11.natgw.015.sh | 61 + ctdb/tests/UNIT/eventscripts/11.natgw.021.sh | 27 + ctdb/tests/UNIT/eventscripts/11.natgw.022.sh | 27 + ctdb/tests/UNIT/eventscripts/11.natgw.023.sh | 27 + ctdb/tests/UNIT/eventscripts/11.natgw.024.sh | 27 + ctdb/tests/UNIT/eventscripts/11.natgw.025.sh | 65 + ctdb/tests/UNIT/eventscripts/11.natgw.031.sh | 62 + ctdb/tests/UNIT/eventscripts/11.natgw.041.sh | 24 + ctdb/tests/UNIT/eventscripts/11.natgw.042.sh | 25 + ctdb/tests/UNIT/eventscripts/11.natgw.051.sh | 17 + ctdb/tests/UNIT/eventscripts/11.natgw.052.sh | 21 + ctdb/tests/UNIT/eventscripts/11.natgw.053.sh | 17 + ctdb/tests/UNIT/eventscripts/11.natgw.054.sh | 21 + .../UNIT/eventscripts/13.per_ip_routing.001.sh | 19 + .../UNIT/eventscripts/13.per_ip_routing.002.sh | 18 + .../UNIT/eventscripts/13.per_ip_routing.003.sh | 16 + .../UNIT/eventscripts/13.per_ip_routing.004.sh | 17 + .../UNIT/eventscripts/13.per_ip_routing.005.sh | 20 + .../UNIT/eventscripts/13.per_ip_routing.006.sh | 24 + .../UNIT/eventscripts/13.per_ip_routing.007.sh | 17 + .../UNIT/eventscripts/13.per_ip_routing.008.sh | 23 + .../UNIT/eventscripts/13.per_ip_routing.009.sh | 20 + .../UNIT/eventscripts/13.per_ip_routing.010.sh | 19 + .../UNIT/eventscripts/13.per_ip_routing.011.sh | 21 + .../UNIT/eventscripts/13.per_ip_routing.012.sh | 29 + .../UNIT/eventscripts/13.per_ip_routing.013.sh | 23 + .../UNIT/eventscripts/13.per_ip_routing.014.sh | 29 + .../UNIT/eventscripts/13.per_ip_routing.015.sh | 29 + .../UNIT/eventscripts/13.per_ip_routing.016.sh | 14 + .../UNIT/eventscripts/13.per_ip_routing.017.sh | 15 + .../UNIT/eventscripts/13.per_ip_routing.018.sh | 21 + .../UNIT/eventscripts/13.per_ip_routing.019.sh | 23 + .../UNIT/eventscripts/13.per_ip_routing.021.sh | 15 + .../UNIT/eventscripts/13.per_ip_routing.022.sh | 15 + .../UNIT/eventscripts/13.per_ip_routing.023.sh | 25 + .../UNIT/eventscripts/13.per_ip_routing.024.sh | 30 + .../UNIT/eventscripts/20.multipathd.monitor.001.sh | 11 + .../UNIT/eventscripts/20.multipathd.monitor.002.sh | 11 + .../UNIT/eventscripts/20.multipathd.monitor.003.sh | 14 + .../UNIT/eventscripts/20.multipathd.monitor.004.sh | 15 + .../UNIT/eventscripts/31.clamd.monitor.002.sh | 16 + .../UNIT/eventscripts/31.clamd.monitor.003.sh | 16 + .../UNIT/eventscripts/40.vsftpd.monitor.002.sh | 19 + .../UNIT/eventscripts/40.vsftpd.shutdown.002.sh | 12 + .../UNIT/eventscripts/40.vsftpd.startup.002.sh | 12 + .../UNIT/eventscripts/41.httpd.monitor.002.sh | 30 + .../UNIT/eventscripts/41.httpd.shutdown.002.sh | 12 + .../UNIT/eventscripts/41.httpd.startup.002.sh | 12 + .../UNIT/eventscripts/48.netbios.shutdown.011.sh | 14 + .../UNIT/eventscripts/48.netbios.startup.011.sh | 14 + .../UNIT/eventscripts/49.winbind.monitor.101.sh | 11 + .../UNIT/eventscripts/49.winbind.monitor.102.sh | 13 + .../UNIT/eventscripts/49.winbind.shutdown.002.sh | 12 + .../UNIT/eventscripts/49.winbind.startup.002.sh | 12 + .../UNIT/eventscripts/50.samba.monitor.101.sh | 11 + .../UNIT/eventscripts/50.samba.monitor.103.sh | 13 + .../UNIT/eventscripts/50.samba.monitor.104.sh | 13 + .../UNIT/eventscripts/50.samba.monitor.105.sh | 12 + .../UNIT/eventscripts/50.samba.monitor.106.sh | 15 + .../UNIT/eventscripts/50.samba.monitor.110.sh | 20 + .../UNIT/eventscripts/50.samba.monitor.111.sh | 25 + .../UNIT/eventscripts/50.samba.monitor.112.sh | 13 + .../UNIT/eventscripts/50.samba.monitor.113.sh | 16 + .../UNIT/eventscripts/50.samba.shutdown.001.sh | 12 + .../UNIT/eventscripts/50.samba.shutdown.002.sh | 15 + .../UNIT/eventscripts/50.samba.shutdown.011.sh | 14 + .../UNIT/eventscripts/50.samba.startup.011.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.101.sh | 11 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.102.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.103.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.104.sh | 17 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.105.sh | 10 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.106.sh | 11 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.107.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.108.sh | 12 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.109.sh | 12 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.111.sh | 13 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.112.sh | 13 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.113.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.114.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.121.sh | 15 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.122.sh | 18 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.131.sh | 11 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.132.sh | 15 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.141.sh | 13 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.142.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.143.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.144.sh | 13 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.151.sh | 13 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.152.sh | 15 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.153.sh | 15 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.161.sh | 12 + ctdb/tests/UNIT/eventscripts/60.nfs.monitor.162.sh | 15 + ctdb/tests/UNIT/eventscripts/60.nfs.multi.001.sh | 19 + ctdb/tests/UNIT/eventscripts/60.nfs.multi.002.sh | 17 + .../UNIT/eventscripts/60.nfs.releaseip.001.sh | 14 + .../UNIT/eventscripts/60.nfs.releaseip.002.sh | 12 + .../tests/UNIT/eventscripts/60.nfs.shutdown.001.sh | 14 + .../tests/UNIT/eventscripts/60.nfs.shutdown.002.sh | 12 + ctdb/tests/UNIT/eventscripts/60.nfs.startup.001.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.startup.002.sh | 12 + ctdb/tests/UNIT/eventscripts/60.nfs.takeip.001.sh | 14 + ctdb/tests/UNIT/eventscripts/60.nfs.takeip.002.sh | 12 + ctdb/tests/UNIT/eventscripts/91.lvs.001.sh | 54 + .../UNIT/eventscripts/91.lvs.ipreallocated.011.sh | 14 + .../UNIT/eventscripts/91.lvs.ipreallocated.012.sh | 17 + .../UNIT/eventscripts/91.lvs.ipreallocated.013.sh | 17 + .../UNIT/eventscripts/91.lvs.ipreallocated.014.sh | 27 + ctdb/tests/UNIT/eventscripts/91.lvs.monitor.001.sh | 11 + ctdb/tests/UNIT/eventscripts/91.lvs.monitor.002.sh | 14 + ctdb/tests/UNIT/eventscripts/91.lvs.monitor.003.sh | 19 + .../tests/UNIT/eventscripts/91.lvs.shutdown.001.sh | 11 + .../tests/UNIT/eventscripts/91.lvs.shutdown.002.sh | 18 + ctdb/tests/UNIT/eventscripts/91.lvs.startup.001.sh | 11 + ctdb/tests/UNIT/eventscripts/91.lvs.startup.002.sh | 14 + ctdb/tests/UNIT/eventscripts/README | 46 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.001.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.002.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.003.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.004.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.005.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.006.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.007.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.008.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.021.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.022.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.023.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.024.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.025.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.026.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.027.sh | 9 + ctdb/tests/UNIT/eventscripts/debug_locks.sh.028.sh | 9 + .../UNIT/eventscripts/etc-ctdb/public_addresses | 9 + ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local | 59 + ctdb/tests/UNIT/eventscripts/etc/init.d/nfs | 7 + ctdb/tests/UNIT/eventscripts/etc/init.d/nfslock | 7 + ctdb/tests/UNIT/eventscripts/etc/samba/smb.conf | 42 + ctdb/tests/UNIT/eventscripts/etc/sysconfig/nfs | 2 + ctdb/tests/UNIT/eventscripts/scripts/00.ctdb.sh | 29 + ctdb/tests/UNIT/eventscripts/scripts/01.reclock.sh | 16 + ctdb/tests/UNIT/eventscripts/scripts/05.system.sh | 46 + ctdb/tests/UNIT/eventscripts/scripts/06.nfs.sh | 4 + .../UNIT/eventscripts/scripts/10.interface.sh | 72 + ctdb/tests/UNIT/eventscripts/scripts/11.natgw.sh | 119 + .../UNIT/eventscripts/scripts/13.per_ip_routing.sh | 42 + .../UNIT/eventscripts/scripts/20.multipathd.sh | 24 + ctdb/tests/UNIT/eventscripts/scripts/31.clamd.sh | 8 + ctdb/tests/UNIT/eventscripts/scripts/40.vsftpd.sh | 12 + ctdb/tests/UNIT/eventscripts/scripts/41.httpd.sh | 14 + ctdb/tests/UNIT/eventscripts/scripts/48.netbios.sh | 21 + ctdb/tests/UNIT/eventscripts/scripts/49.winbind.sh | 26 + ctdb/tests/UNIT/eventscripts/scripts/50.samba.sh | 54 + ctdb/tests/UNIT/eventscripts/scripts/60.nfs.sh | 436 ++ ctdb/tests/UNIT/eventscripts/scripts/91.lvs.sh | 76 + .../tests/UNIT/eventscripts/scripts/debug_locks.sh | 255 + ctdb/tests/UNIT/eventscripts/scripts/local.sh | 550 ++ .../UNIT/eventscripts/scripts/statd-callout.sh | 65 + ctdb/tests/UNIT/eventscripts/statd-callout.001.sh | 13 + ctdb/tests/UNIT/eventscripts/statd-callout.002.sh | 14 + ctdb/tests/UNIT/eventscripts/statd-callout.003.sh | 16 + ctdb/tests/UNIT/eventscripts/statd-callout.004.sh | 17 + ctdb/tests/UNIT/eventscripts/statd-callout.005.sh | 25 + ctdb/tests/UNIT/eventscripts/statd-callout.006.sh | 27 + ctdb/tests/UNIT/eventscripts/statd-callout.007.sh | 14 + ctdb/tests/UNIT/eventscripts/stubs/ctdb | 496 ++ ctdb/tests/UNIT/eventscripts/stubs/ctdb-config | 2 + ctdb/tests/UNIT/eventscripts/stubs/ctdb_killtcp | 9 + ctdb/tests/UNIT/eventscripts/stubs/ctdb_lvs | 52 + ctdb/tests/UNIT/eventscripts/stubs/ctdb_natgw | 34 + ctdb/tests/UNIT/eventscripts/stubs/date | 7 + ctdb/tests/UNIT/eventscripts/stubs/df | 38 + ctdb/tests/UNIT/eventscripts/stubs/ethtool | 12 + ctdb/tests/UNIT/eventscripts/stubs/exportfs | 13 + ctdb/tests/UNIT/eventscripts/stubs/gstack | 19 + ctdb/tests/UNIT/eventscripts/stubs/id | 3 + ctdb/tests/UNIT/eventscripts/stubs/ip | 679 ++ ctdb/tests/UNIT/eventscripts/stubs/ip6tables | 5 + ctdb/tests/UNIT/eventscripts/stubs/iptables | 5 + ctdb/tests/UNIT/eventscripts/stubs/ipvsadm | 126 + ctdb/tests/UNIT/eventscripts/stubs/kill | 7 + ctdb/tests/UNIT/eventscripts/stubs/killall | 7 + ctdb/tests/UNIT/eventscripts/stubs/multipath | 36 + ctdb/tests/UNIT/eventscripts/stubs/net | 5 + .../tests/UNIT/eventscripts/stubs/nfs-fake-callout | 15 + ctdb/tests/UNIT/eventscripts/stubs/nfsconf | 5 + ctdb/tests/UNIT/eventscripts/stubs/pidof | 16 + ctdb/tests/UNIT/eventscripts/stubs/pkill | 7 + ctdb/tests/UNIT/eventscripts/stubs/ps | 48 + ctdb/tests/UNIT/eventscripts/stubs/rm | 6 + ctdb/tests/UNIT/eventscripts/stubs/rpc.lockd | 6 + ctdb/tests/UNIT/eventscripts/stubs/rpc.mountd | 6 + ctdb/tests/UNIT/eventscripts/stubs/rpc.rquotad | 6 + ctdb/tests/UNIT/eventscripts/stubs/rpc.statd | 6 + ctdb/tests/UNIT/eventscripts/stubs/rpcinfo | 73 + ctdb/tests/UNIT/eventscripts/stubs/service | 64 + ctdb/tests/UNIT/eventscripts/stubs/sleep | 9 + ctdb/tests/UNIT/eventscripts/stubs/smnotify | 38 + ctdb/tests/UNIT/eventscripts/stubs/ss | 193 + ctdb/tests/UNIT/eventscripts/stubs/stat | 71 + ctdb/tests/UNIT/eventscripts/stubs/tdb_mutex_check | 10 + ctdb/tests/UNIT/eventscripts/stubs/tdbdump | 9 + ctdb/tests/UNIT/eventscripts/stubs/tdbtool | 33 + ctdb/tests/UNIT/eventscripts/stubs/testparm | 85 + ctdb/tests/UNIT/eventscripts/stubs/timeout | 8 + ctdb/tests/UNIT/eventscripts/stubs/wbinfo | 7 + ctdb/tests/UNIT/onnode/0001.sh | 24 + ctdb/tests/UNIT/onnode/0002.sh | 16 + ctdb/tests/UNIT/onnode/0003.sh | 16 + ctdb/tests/UNIT/onnode/0004.sh | 16 + ctdb/tests/UNIT/onnode/0005.sh | 13 + ctdb/tests/UNIT/onnode/0006.sh | 15 + ctdb/tests/UNIT/onnode/0010.sh | 13 + ctdb/tests/UNIT/onnode/0011.sh | 13 + ctdb/tests/UNIT/onnode/0070.sh | 32 + ctdb/tests/UNIT/onnode/0071.sh | 29 + ctdb/tests/UNIT/onnode/0072.sh | 29 + ctdb/tests/UNIT/onnode/0075.sh | 29 + ctdb/tests/UNIT/onnode/etc-ctdb/nodes | 4 + ctdb/tests/UNIT/onnode/scripts/local.sh | 64 + ctdb/tests/UNIT/onnode/stubs/ctdb | 19 + ctdb/tests/UNIT/onnode/stubs/ssh | 2 + ctdb/tests/UNIT/shellcheck/base_scripts.sh | 12 + ctdb/tests/UNIT/shellcheck/ctdb_helpers.sh | 9 + ctdb/tests/UNIT/shellcheck/event_scripts.sh | 7 + ctdb/tests/UNIT/shellcheck/functions.sh | 7 + ctdb/tests/UNIT/shellcheck/init_script.sh | 19 + ctdb/tests/UNIT/shellcheck/scripts/local.sh | 33 + ctdb/tests/UNIT/shellcheck/tests.sh | 31 + ctdb/tests/UNIT/shellcheck/tools.sh | 9 + ctdb/tests/UNIT/takeover/README | 5 + ctdb/tests/UNIT/takeover/det.001.sh | 36 + ctdb/tests/UNIT/takeover/det.002.sh | 33 + ctdb/tests/UNIT/takeover/det.003.sh | 30 + ctdb/tests/UNIT/takeover/lcp2.001.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.002.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.003.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.004.sh | 37 + ctdb/tests/UNIT/takeover/lcp2.005.sh | 198 + ctdb/tests/UNIT/takeover/lcp2.006.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.007.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.008.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.009.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.010.sh | 32 + ctdb/tests/UNIT/takeover/lcp2.011.sh | 45 + ctdb/tests/UNIT/takeover/lcp2.012.sh | 33 + ctdb/tests/UNIT/takeover/lcp2.013.sh | 33 + ctdb/tests/UNIT/takeover/lcp2.014.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.015.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.016.sh | 31 + ctdb/tests/UNIT/takeover/lcp2.024.sh | 42 + ctdb/tests/UNIT/takeover/lcp2.025.sh | 33 + ctdb/tests/UNIT/takeover/lcp2.027.sh | 45 + ctdb/tests/UNIT/takeover/lcp2.028.sh | 45 + ctdb/tests/UNIT/takeover/lcp2.029.sh | 111 + ctdb/tests/UNIT/takeover/lcp2.030.sh | 1813 ++++++ ctdb/tests/UNIT/takeover/lcp2.031.sh | 143 + ctdb/tests/UNIT/takeover/lcp2.032.sh | 450 ++ ctdb/tests/UNIT/takeover/lcp2.033.sh | 74 + ctdb/tests/UNIT/takeover/lcp2.034.sh | 21 + ctdb/tests/UNIT/takeover/lcp2.035.sh | 1813 ++++++ ctdb/tests/UNIT/takeover/nondet.001.sh | 35 + ctdb/tests/UNIT/takeover/nondet.002.sh | 32 + ctdb/tests/UNIT/takeover/nondet.003.sh | 29 + ctdb/tests/UNIT/takeover/scripts/local.sh | 30 + ctdb/tests/UNIT/takeover_helper/000.sh | 22 + ctdb/tests/UNIT/takeover_helper/010.sh | 33 + ctdb/tests/UNIT/takeover_helper/011.sh | 33 + ctdb/tests/UNIT/takeover_helper/012.sh | 33 + ctdb/tests/UNIT/takeover_helper/013.sh | 33 + ctdb/tests/UNIT/takeover_helper/014.sh | 37 + ctdb/tests/UNIT/takeover_helper/016.sh | 36 + ctdb/tests/UNIT/takeover_helper/017.sh | 36 + ctdb/tests/UNIT/takeover_helper/018.sh | 34 + ctdb/tests/UNIT/takeover_helper/019.sh | 37 + ctdb/tests/UNIT/takeover_helper/021.sh | 39 + ctdb/tests/UNIT/takeover_helper/022.sh | 40 + ctdb/tests/UNIT/takeover_helper/023.sh | 41 + ctdb/tests/UNIT/takeover_helper/024.sh | 43 + ctdb/tests/UNIT/takeover_helper/025.sh | 37 + ctdb/tests/UNIT/takeover_helper/026.sh | 41 + ctdb/tests/UNIT/takeover_helper/027.sh | 33 + ctdb/tests/UNIT/takeover_helper/028.sh | 33 + ctdb/tests/UNIT/takeover_helper/030.sh | 35 + ctdb/tests/UNIT/takeover_helper/031.sh | 55 + ctdb/tests/UNIT/takeover_helper/110.sh | 29 + ctdb/tests/UNIT/takeover_helper/111.sh | 40 + ctdb/tests/UNIT/takeover_helper/120.sh | 40 + ctdb/tests/UNIT/takeover_helper/121.sh | 40 + ctdb/tests/UNIT/takeover_helper/122.sh | 40 + ctdb/tests/UNIT/takeover_helper/130.sh | 41 + ctdb/tests/UNIT/takeover_helper/131.sh | 40 + ctdb/tests/UNIT/takeover_helper/132.sh | 42 + ctdb/tests/UNIT/takeover_helper/140.sh | 33 + ctdb/tests/UNIT/takeover_helper/150.sh | 31 + ctdb/tests/UNIT/takeover_helper/160.sh | 31 + ctdb/tests/UNIT/takeover_helper/210.sh | 29 + ctdb/tests/UNIT/takeover_helper/211.sh | 40 + ctdb/tests/UNIT/takeover_helper/220.sh | 40 + ctdb/tests/UNIT/takeover_helper/230.sh | 41 + ctdb/tests/UNIT/takeover_helper/240.sh | 33 + ctdb/tests/UNIT/takeover_helper/250.sh | 31 + ctdb/tests/UNIT/takeover_helper/260.sh | 31 + ctdb/tests/UNIT/takeover_helper/scripts/local.sh | 108 + ctdb/tests/UNIT/tool/README | 17 + ctdb/tests/UNIT/tool/ctdb.attach.001.sh | 35 + ctdb/tests/UNIT/tool/ctdb.attach.002.sh | 35 + ctdb/tests/UNIT/tool/ctdb.attach.003.sh | 35 + ctdb/tests/UNIT/tool/ctdb.ban.001.sh | 35 + ctdb/tests/UNIT/tool/ctdb.ban.002.sh | 23 + ctdb/tests/UNIT/tool/ctdb.ban.003.sh | 23 + ctdb/tests/UNIT/tool/ctdb.catdb.001.sh | 80 + ctdb/tests/UNIT/tool/ctdb.catdb.002.sh | 86 + ctdb/tests/UNIT/tool/ctdb.cattdb.001.sh | 80 + ctdb/tests/UNIT/tool/ctdb.cattdb.002.sh | 86 + ctdb/tests/UNIT/tool/ctdb.continue.001.sh | 23 + ctdb/tests/UNIT/tool/ctdb.continue.002.sh | 23 + ctdb/tests/UNIT/tool/ctdb.continue.003.sh | 23 + ctdb/tests/UNIT/tool/ctdb.deletekey.001.sh | 34 + ctdb/tests/UNIT/tool/ctdb.disable.001.sh | 23 + ctdb/tests/UNIT/tool/ctdb.disable.002.sh | 23 + ctdb/tests/UNIT/tool/ctdb.disable.003.sh | 23 + ctdb/tests/UNIT/tool/ctdb.disable.004.sh | 15 + ctdb/tests/UNIT/tool/ctdb.enable.001.sh | 23 + ctdb/tests/UNIT/tool/ctdb.enable.002.sh | 23 + ctdb/tests/UNIT/tool/ctdb.enable.003.sh | 23 + ctdb/tests/UNIT/tool/ctdb.getcapabilities.001.sh | 19 + ctdb/tests/UNIT/tool/ctdb.getcapabilities.002.sh | 19 + ctdb/tests/UNIT/tool/ctdb.getcapabilities.003.sh | 28 + ctdb/tests/UNIT/tool/ctdb.getcapabilities.004.sh | 39 + ctdb/tests/UNIT/tool/ctdb.getdbmap.001.sh | 34 + ctdb/tests/UNIT/tool/ctdb.getdbseqnum.001.sh | 41 + ctdb/tests/UNIT/tool/ctdb.getdbseqnum.002.sh | 36 + ctdb/tests/UNIT/tool/ctdb.getdbstatus.001.sh | 108 + ctdb/tests/UNIT/tool/ctdb.getdbstatus.002.sh | 108 + ctdb/tests/UNIT/tool/ctdb.getpid.001.sh | 17 + ctdb/tests/UNIT/tool/ctdb.getpid.010.sh | 25 + ctdb/tests/UNIT/tool/ctdb.getreclock.001.sh | 16 + ctdb/tests/UNIT/tool/ctdb.getreclock.002.sh | 21 + ctdb/tests/UNIT/tool/ctdb.getvar.001.sh | 35 + ctdb/tests/UNIT/tool/ctdb.getvar.002.sh | 17 + ctdb/tests/UNIT/tool/ctdb.ifaces.001.sh | 24 + ctdb/tests/UNIT/tool/ctdb.ip.001.sh | 17 + ctdb/tests/UNIT/tool/ctdb.ip.002.sh | 17 + ctdb/tests/UNIT/tool/ctdb.ip.003.sh | 30 + ctdb/tests/UNIT/tool/ctdb.ip.004.sh | 29 + ctdb/tests/UNIT/tool/ctdb.ip.005.sh | 30 + ctdb/tests/UNIT/tool/ctdb.ip.006.sh | 30 + ctdb/tests/UNIT/tool/ctdb.ip.007.sh | 36 + ctdb/tests/UNIT/tool/ctdb.ipinfo.001.sh | 18 + ctdb/tests/UNIT/tool/ctdb.ipinfo.002.sh | 32 + ctdb/tests/UNIT/tool/ctdb.ipinfo.003.sh | 35 + ctdb/tests/UNIT/tool/ctdb.leader.001.sh | 16 + ctdb/tests/UNIT/tool/ctdb.leader.002.sh | 16 + ctdb/tests/UNIT/tool/ctdb.listnodes.001.sh | 20 + ctdb/tests/UNIT/tool/ctdb.listnodes.002.sh | 19 + ctdb/tests/UNIT/tool/ctdb.listvars.001.sh | 66 + ctdb/tests/UNIT/tool/ctdb.lvs.001.sh | 36 + ctdb/tests/UNIT/tool/ctdb.lvs.002.sh | 46 + ctdb/tests/UNIT/tool/ctdb.lvs.003.sh | 43 + ctdb/tests/UNIT/tool/ctdb.lvs.004.sh | 45 + ctdb/tests/UNIT/tool/ctdb.lvs.005.sh | 46 + ctdb/tests/UNIT/tool/ctdb.lvs.006.sh | 44 + ctdb/tests/UNIT/tool/ctdb.lvs.007.sh | 42 + ctdb/tests/UNIT/tool/ctdb.lvs.008.sh | 66 + ctdb/tests/UNIT/tool/ctdb.lvs.010.sh | 25 + ctdb/tests/UNIT/tool/ctdb.natgw.001.sh | 46 + ctdb/tests/UNIT/tool/ctdb.natgw.002.sh | 46 + ctdb/tests/UNIT/tool/ctdb.natgw.003.sh | 43 + ctdb/tests/UNIT/tool/ctdb.natgw.004.sh | 46 + ctdb/tests/UNIT/tool/ctdb.natgw.005.sh | 46 + ctdb/tests/UNIT/tool/ctdb.natgw.006.sh | 46 + ctdb/tests/UNIT/tool/ctdb.natgw.007.sh | 45 + ctdb/tests/UNIT/tool/ctdb.natgw.008.sh | 46 + ctdb/tests/UNIT/tool/ctdb.natgw.010.sh | 25 + ctdb/tests/UNIT/tool/ctdb.nodestatus.001.sh | 33 + ctdb/tests/UNIT/tool/ctdb.nodestatus.002.sh | 33 + ctdb/tests/UNIT/tool/ctdb.nodestatus.003.sh | 33 + ctdb/tests/UNIT/tool/ctdb.nodestatus.004.sh | 28 + ctdb/tests/UNIT/tool/ctdb.nodestatus.005.sh | 28 + ctdb/tests/UNIT/tool/ctdb.nodestatus.006.sh | 40 + ctdb/tests/UNIT/tool/ctdb.nodestatus.007.sh | 36 + ctdb/tests/UNIT/tool/ctdb.pdelete.001.sh | 27 + ctdb/tests/UNIT/tool/ctdb.ping.001.sh | 24 + ctdb/tests/UNIT/tool/ctdb.pnn.001.sh | 15 + ctdb/tests/UNIT/tool/ctdb.process-exists.001.sh | 28 + ctdb/tests/UNIT/tool/ctdb.process-exists.002.sh | 30 + ctdb/tests/UNIT/tool/ctdb.process-exists.003.sh | 30 + ctdb/tests/UNIT/tool/ctdb.pstore.001.sh | 24 + ctdb/tests/UNIT/tool/ctdb.ptrans.001.sh | 49 + ctdb/tests/UNIT/tool/ctdb.readkey.001.sh | 20 + ctdb/tests/UNIT/tool/ctdb.recover.001.sh | 22 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.001.sh | 24 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.002.sh | 30 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.003.sh | 29 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.011.sh | 25 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.012.sh | 24 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.013.sh | 26 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.014.sh | 24 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.015.sh | 26 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.016.sh | 24 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.017.sh | 26 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.018.sh | 29 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.019.sh | 28 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.020.sh | 28 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.021.sh | 26 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.023.sh | 24 + ctdb/tests/UNIT/tool/ctdb.reloadnodes.024.sh | 24 + ctdb/tests/UNIT/tool/ctdb.runstate.001.sh | 15 + ctdb/tests/UNIT/tool/ctdb.runstate.002.sh | 15 + ctdb/tests/UNIT/tool/ctdb.runstate.003.sh | 17 + ctdb/tests/UNIT/tool/ctdb.runstate.004.sh | 15 + ctdb/tests/UNIT/tool/ctdb.runstate.005.sh | 15 + ctdb/tests/UNIT/tool/ctdb.setdbreadonly.001.sh | 53 + ctdb/tests/UNIT/tool/ctdb.setdbreadonly.002.sh | 37 + ctdb/tests/UNIT/tool/ctdb.setdbreadonly.003.sh | 39 + ctdb/tests/UNIT/tool/ctdb.setdbreadonly.004.sh | 37 + ctdb/tests/UNIT/tool/ctdb.setdbreadonly.005.sh | 39 + ctdb/tests/UNIT/tool/ctdb.setdbsticky.001.sh | 53 + ctdb/tests/UNIT/tool/ctdb.setdbsticky.002.sh | 37 + ctdb/tests/UNIT/tool/ctdb.setdbsticky.003.sh | 39 + ctdb/tests/UNIT/tool/ctdb.setdbsticky.004.sh | 37 + ctdb/tests/UNIT/tool/ctdb.setdbsticky.005.sh | 39 + ctdb/tests/UNIT/tool/ctdb.setdebug.001.sh | 23 + ctdb/tests/UNIT/tool/ctdb.setdebug.002.sh | 23 + ctdb/tests/UNIT/tool/ctdb.setdebug.003.sh | 32 + ctdb/tests/UNIT/tool/ctdb.setifacelink.001.sh | 76 + ctdb/tests/UNIT/tool/ctdb.setifacelink.002.sh | 22 + ctdb/tests/UNIT/tool/ctdb.setvar.001.sh | 49 + ctdb/tests/UNIT/tool/ctdb.setvar.002.sh | 17 + ctdb/tests/UNIT/tool/ctdb.status.001.sh | 46 + ctdb/tests/UNIT/tool/ctdb.status.002.sh | 46 + ctdb/tests/UNIT/tool/ctdb.status.003.sh | 49 + ctdb/tests/UNIT/tool/ctdb.stop.001.sh | 23 + ctdb/tests/UNIT/tool/ctdb.stop.002.sh | 23 + ctdb/tests/UNIT/tool/ctdb.stop.003.sh | 23 + ctdb/tests/UNIT/tool/ctdb.unban.001.sh | 23 + ctdb/tests/UNIT/tool/ctdb.unban.002.sh | 34 + ctdb/tests/UNIT/tool/ctdb.unban.003.sh | 23 + ctdb/tests/UNIT/tool/ctdb.uptime.001.sh | 36 + ctdb/tests/UNIT/tool/ctdb.writekey.001.sh | 31 + ctdb/tests/UNIT/tool/scripts/local.sh | 112 + ctdb/tests/etc-ctdb/events/legacy/00.test.script | 30 + ctdb/tests/local_daemons.sh | 506 ++ ctdb/tests/run_cluster_tests.sh | 1 + ctdb/tests/run_tests.sh | 399 ++ ctdb/tests/scripts/cluster.bash | 18 + ctdb/tests/scripts/common.sh | 146 + ctdb/tests/scripts/integration.bash | 864 +++ ctdb/tests/scripts/integration_local_daemons.bash | 95 + ctdb/tests/scripts/integration_real_cluster.bash | 53 + ctdb/tests/scripts/script_install_paths.sh | 67 + ctdb/tests/scripts/test_wrap | 11 + ctdb/tests/scripts/unit.sh | 267 + ctdb/tests/src/cluster_mutex_test.c | 844 +++ ctdb/tests/src/cluster_wait.c | 346 ++ ctdb/tests/src/cluster_wait.h | 30 + ctdb/tests/src/cmdline_test.c | 480 ++ ctdb/tests/src/comm_client_test.c | 217 + ctdb/tests/src/comm_server_test.c | 292 + ctdb/tests/src/comm_test.c | 501 ++ ctdb/tests/src/conf_test.c | 513 ++ ctdb/tests/src/ctdb_io_test.c | 356 ++ ctdb/tests/src/ctdb_packet_parse.c | 136 + ctdb/tests/src/ctdb_takeover_tests.c | 278 + ctdb/tests/src/db_hash_test.c | 138 + ctdb/tests/src/db_test_tool.c | 792 +++ ctdb/tests/src/dummy_client.c | 163 + ctdb/tests/src/errcode.c | 189 + ctdb/tests/src/event_script_test.c | 120 + ctdb/tests/src/fake_ctdbd.c | 4781 ++++++++++++++ ctdb/tests/src/fetch_loop.c | 288 + ctdb/tests/src/fetch_loop_key.c | 217 + ctdb/tests/src/fetch_readonly.c | 166 + ctdb/tests/src/fetch_readonly_loop.c | 272 + ctdb/tests/src/fetch_ring.c | 398 ++ ctdb/tests/src/g_lock_loop.c | 270 + ctdb/tests/src/hash_count_test.c | 132 + ctdb/tests/src/ipalloc_read_known_ips.c | 179 + ctdb/tests/src/ipalloc_read_known_ips.h | 32 + ctdb/tests/src/line_test.c | 102 + ctdb/tests/src/lock_tdb.c | 60 + ctdb/tests/src/message_ring.c | 369 ++ ctdb/tests/src/pidfile_test.c | 242 + ctdb/tests/src/pkt_read_test.c | 249 + ctdb/tests/src/pkt_write_test.c | 359 ++ ctdb/tests/src/porting_tests.c | 262 + ctdb/tests/src/protocol_basic_test.c | 106 + ctdb/tests/src/protocol_common.c | 1260 ++++ ctdb/tests/src/protocol_common.h | 238 + ctdb/tests/src/protocol_common_basic.c | 305 + ctdb/tests/src/protocol_common_basic.h | 175 + ctdb/tests/src/protocol_common_ctdb.c | 1934 ++++++ ctdb/tests/src/protocol_common_ctdb.h | 101 + ctdb/tests/src/protocol_ctdb_compat_test.c | 1270 ++++ ctdb/tests/src/protocol_ctdb_test.c | 365 ++ ctdb/tests/src/protocol_types_compat_test.c | 2371 +++++++ ctdb/tests/src/protocol_types_test.c | 194 + ctdb/tests/src/protocol_util_test.c | 417 ++ ctdb/tests/src/rb_test.c | 336 + ctdb/tests/src/reqid_test.c | 89 + ctdb/tests/src/run_event_test.c | 251 + ctdb/tests/src/run_proc_test.c | 111 + ctdb/tests/src/sigcode.c | 120 + ctdb/tests/src/sock_daemon_test.c | 1980 ++++++ ctdb/tests/src/sock_io_test.c | 283 + ctdb/tests/src/srvid_test.c | 105 + ctdb/tests/src/system_socket_test.c | 266 + ctdb/tests/src/test_backtrace.c | 37 + ctdb/tests/src/test_backtrace.h | 25 + ctdb/tests/src/test_mutex_raw.c | 434 ++ ctdb/tests/src/test_options.c | 245 + ctdb/tests/src/test_options.h | 44 + ctdb/tests/src/tmon_ping_test.c | 381 ++ ctdb/tests/src/tmon_test.c | 406 ++ ctdb/tests/src/transaction_loop.c | 419 ++ ctdb/tests/src/tunable_test.c | 71 + ctdb/tests/src/tunnel_cmd.c | 199 + ctdb/tests/src/tunnel_test.c | 480 ++ ctdb/tests/src/update_record.c | 236 + ctdb/tests/src/update_record_persistent.c | 218 + ctdb/tests/test_check_tcp_ports.sh | 18 + ctdb/tools/ctdb.c | 6500 ++++++++++++++++++++ ctdb/tools/ctdb_diagnostics | 346 ++ ctdb/tools/ctdb_killtcp.c | 418 ++ ctdb/tools/ctdb_lvs | 204 + ctdb/tools/ctdb_natgw | 194 + ctdb/tools/ltdbtool.c | 395 ++ ctdb/tools/onnode | 342 + ctdb/utils/ceph/ctdb_mutex_ceph_rados_helper.c | 457 ++ ctdb/utils/ceph/test_ceph_rados_reclock.sh | 212 + ctdb/utils/etcd/ctdb_etcd_lock | 213 + ctdb/utils/nagios/README | 56 + ctdb/utils/nagios/check_ctdb | 279 + ctdb/utils/ping_pong/ping_pong.c | 303 + ctdb/utils/pmda/Install | 36 + ctdb/utils/pmda/README | 84 + ctdb/utils/pmda/Remove | 29 + ctdb/utils/pmda/domain.h | 19 + ctdb/utils/pmda/help | 106 + ctdb/utils/pmda/pmda_ctdb.c | 559 ++ ctdb/utils/pmda/pmns | 73 + ctdb/utils/pmda/root | 10 + ctdb/utils/scsi_io/scsi_io.c | 1152 ++++ ctdb/utils/smnotify/smnotify.c | 151 + ctdb/utils/smnotify/smnotify.x | 21 + ctdb/utils/tdb/tdb_mutex_check.c | 160 + ctdb/wscript | 1329 ++++ 1043 files changed, 178090 insertions(+) create mode 100644 ctdb/.bzrignore create mode 100644 ctdb/.gitignore create mode 100644 ctdb/COPYING create mode 100644 ctdb/Makefile create mode 100644 ctdb/README create mode 100644 ctdb/client/client.h create mode 100644 ctdb/client/client_call.c create mode 100644 ctdb/client/client_connect.c create mode 100644 ctdb/client/client_control.c create mode 100644 ctdb/client/client_control_sync.c create mode 100644 ctdb/client/client_db.c create mode 100644 ctdb/client/client_event.c create mode 100644 ctdb/client/client_message.c create mode 100644 ctdb/client/client_message_sync.c create mode 100644 ctdb/client/client_private.h create mode 100644 ctdb/client/client_sync.h create mode 100644 ctdb/client/client_tunnel.c create mode 100644 ctdb/client/client_util.c create mode 100644 ctdb/cluster/cluster_conf.c create mode 100644 ctdb/cluster/cluster_conf.h create mode 100644 ctdb/common/cmdline.c create mode 100644 ctdb/common/cmdline.h create mode 100644 ctdb/common/comm.c create mode 100644 ctdb/common/comm.h create mode 100644 ctdb/common/common.h create mode 100644 ctdb/common/conf.c create mode 100644 ctdb/common/conf.h create mode 100644 ctdb/common/conf_tool.c create mode 100644 ctdb/common/conf_tool.h create mode 100644 ctdb/common/ctdb_io.c create mode 100644 ctdb/common/ctdb_ltdb.c create mode 100644 ctdb/common/ctdb_util.c create mode 100644 ctdb/common/db_hash.c create mode 100644 ctdb/common/db_hash.h create mode 100644 ctdb/common/event_script.c create mode 100644 ctdb/common/event_script.h create mode 100644 ctdb/common/hash_count.c create mode 100644 ctdb/common/hash_count.h create mode 100644 ctdb/common/line.c create mode 100644 ctdb/common/line.h create mode 100644 ctdb/common/logging.c create mode 100644 ctdb/common/logging.h create mode 100644 ctdb/common/logging_conf.c create mode 100644 ctdb/common/logging_conf.h create mode 100644 ctdb/common/path.c create mode 100644 ctdb/common/path.h create mode 100644 ctdb/common/path_tool.c create mode 100644 ctdb/common/path_tool.h create mode 100644 ctdb/common/pidfile.c create mode 100644 ctdb/common/pidfile.h create mode 100644 ctdb/common/pkt_read.c create mode 100644 ctdb/common/pkt_read.h create mode 100644 ctdb/common/pkt_write.c create mode 100644 ctdb/common/pkt_write.h create mode 100644 ctdb/common/rb_tree.c create mode 100644 ctdb/common/rb_tree.h create mode 100644 ctdb/common/reqid.c create mode 100644 ctdb/common/reqid.h create mode 100644 ctdb/common/run_event.c create mode 100644 ctdb/common/run_event.h create mode 100644 ctdb/common/run_proc.c create mode 100644 ctdb/common/run_proc.h create mode 100644 ctdb/common/sock_client.c create mode 100644 ctdb/common/sock_client.h create mode 100644 ctdb/common/sock_daemon.c create mode 100644 ctdb/common/sock_daemon.h create mode 100644 ctdb/common/sock_io.c create mode 100644 ctdb/common/sock_io.h create mode 100644 ctdb/common/srvid.c create mode 100644 ctdb/common/srvid.h create mode 100644 ctdb/common/system.c create mode 100644 ctdb/common/system.h create mode 100644 ctdb/common/system_socket.c create mode 100644 ctdb/common/system_socket.h create mode 100644 ctdb/common/tmon.c create mode 100644 ctdb/common/tmon.h create mode 100644 ctdb/common/tunable.c create mode 100644 ctdb/common/tunable.h create mode 100644 ctdb/config/README create mode 100755 ctdb/config/ctdb-crash-cleanup.sh create mode 100644 ctdb/config/ctdb.conf create mode 100755 ctdb/config/ctdb.init create mode 100644 ctdb/config/ctdb.sudoers create mode 100644 ctdb/config/ctdb.sysconfig create mode 100644 ctdb/config/ctdb.tunables create mode 100755 ctdb/config/debug-hung-script.sh create mode 100755 ctdb/config/debug_locks.sh create mode 100644 ctdb/config/events/README create mode 100755 ctdb/config/events/legacy/00.ctdb.script create mode 100755 ctdb/config/events/legacy/01.reclock.script create mode 100755 ctdb/config/events/legacy/05.system.script create mode 100755 ctdb/config/events/legacy/06.nfs.script create mode 100755 ctdb/config/events/legacy/10.interface.script create mode 100755 ctdb/config/events/legacy/11.natgw.script create mode 100755 ctdb/config/events/legacy/11.routing.script create mode 100755 ctdb/config/events/legacy/13.per_ip_routing.script create mode 100755 ctdb/config/events/legacy/20.multipathd.script create mode 100755 ctdb/config/events/legacy/31.clamd.script create mode 100755 ctdb/config/events/legacy/40.vsftpd.script create mode 100755 ctdb/config/events/legacy/41.httpd.script create mode 100755 ctdb/config/events/legacy/48.netbios.script create mode 100755 ctdb/config/events/legacy/49.winbind.script create mode 100755 ctdb/config/events/legacy/50.samba.script create mode 100755 ctdb/config/events/legacy/60.nfs.script create mode 100755 ctdb/config/events/legacy/70.iscsi.script create mode 100755 ctdb/config/events/legacy/91.lvs.script create mode 100755 ctdb/config/functions create mode 100644 ctdb/config/nfs-checks.d/00.portmapper.check create mode 100644 ctdb/config/nfs-checks.d/10.status.check create mode 100644 ctdb/config/nfs-checks.d/20.nfs.check create mode 100644 ctdb/config/nfs-checks.d/30.nlockmgr.check create mode 100644 ctdb/config/nfs-checks.d/40.mountd.check create mode 100644 ctdb/config/nfs-checks.d/50.rquotad.check create mode 100644 ctdb/config/nfs-checks.d/README create mode 100755 ctdb/config/nfs-linux-kernel-callout create mode 100755 ctdb/config/notification.README create mode 100755 ctdb/config/notify.sh create mode 100644 ctdb/config/script.options create mode 100755 ctdb/config/statd-callout create mode 100755 ctdb/configure create mode 100755 ctdb/configure.rpm create mode 100644 ctdb/database/database_conf.c create mode 100644 ctdb/database/database_conf.h create mode 100644 ctdb/doc/cluster_mutex_helper.txt create mode 100644 ctdb/doc/ctdb-etcd.7.xml create mode 100644 ctdb/doc/ctdb-script.options.5.xml create mode 100644 ctdb/doc/ctdb-statistics.7.xml create mode 100644 ctdb/doc/ctdb-tunables.7.xml create mode 100644 ctdb/doc/ctdb.1.xml create mode 100644 ctdb/doc/ctdb.7.xml create mode 100644 ctdb/doc/ctdb.conf.5.xml create mode 100644 ctdb/doc/ctdb.sysconfig.5.xml create mode 100644 ctdb/doc/ctdb_diagnostics.1.xml create mode 100644 ctdb/doc/ctdb_mutex_ceph_rados_helper.7.xml create mode 100644 ctdb/doc/ctdbd.1.xml create mode 100644 ctdb/doc/examples/11.natgw.options create mode 100644 ctdb/doc/examples/20.nfs_ganesha.check create mode 100644 ctdb/doc/examples/91.lvs.options create mode 100644 ctdb/doc/examples/README create mode 100755 ctdb/doc/examples/config_migrate.sh create mode 100644 ctdb/doc/examples/config_migrate.test_input create mode 100644 ctdb/doc/examples/ctdb.conf create mode 100644 ctdb/doc/examples/ctdb.spec.in create mode 100755 ctdb/doc/examples/nfs-ganesha-callout create mode 100644 ctdb/doc/ltdbtool.1.xml create mode 100644 ctdb/doc/onnode.1.xml create mode 100644 ctdb/doc/ping_pong.1.xml create mode 100644 ctdb/doc/readonlyrecords.txt create mode 100644 ctdb/event/event.h create mode 100644 ctdb/event/event_client.c create mode 100644 ctdb/event/event_cmd.c create mode 100644 ctdb/event/event_conf.c create mode 100644 ctdb/event/event_conf.h create mode 100644 ctdb/event/event_config.c create mode 100644 ctdb/event/event_context.c create mode 100644 ctdb/event/event_daemon.c create mode 100644 ctdb/event/event_private.h create mode 100644 ctdb/event/event_protocol.c create mode 100644 ctdb/event/event_protocol.h create mode 100644 ctdb/event/event_protocol_api.h create mode 100644 ctdb/event/event_protocol_test.c create mode 100644 ctdb/event/event_protocol_util.c create mode 100644 ctdb/event/event_request.c create mode 100644 ctdb/event/event_tool.c create mode 100644 ctdb/event/event_tool.h create mode 100644 ctdb/failover/failover_conf.c create mode 100644 ctdb/failover/failover_conf.h create mode 100644 ctdb/ib/README.txt create mode 100644 ctdb/ib/ibw_ctdb.c create mode 100644 ctdb/ib/ibw_ctdb.h create mode 100644 ctdb/ib/ibw_ctdb_init.c create mode 100644 ctdb/ib/ibwrapper.c create mode 100644 ctdb/ib/ibwrapper.h create mode 100644 ctdb/ib/ibwrapper_internal.h create mode 100644 ctdb/ib/ibwrapper_test.c create mode 120000 ctdb/include/common/srvid.h create mode 100644 ctdb/include/ctdb_client.h create mode 100644 ctdb/include/ctdb_private.h create mode 100644 ctdb/include/ctdb_protocol.h create mode 100644 ctdb/include/public/README.txt create mode 100644 ctdb/include/public/util/README.txt create mode 100644 ctdb/protocol/protocol.h create mode 100644 ctdb/protocol/protocol_api.h create mode 100644 ctdb/protocol/protocol_basic.c create mode 100644 ctdb/protocol/protocol_basic.h create mode 100644 ctdb/protocol/protocol_call.c create mode 100644 ctdb/protocol/protocol_client.c create mode 100644 ctdb/protocol/protocol_control.c create mode 100644 ctdb/protocol/protocol_debug.c create mode 100644 ctdb/protocol/protocol_header.c create mode 100644 ctdb/protocol/protocol_keepalive.c create mode 100644 ctdb/protocol/protocol_message.c create mode 100644 ctdb/protocol/protocol_packet.c create mode 100644 ctdb/protocol/protocol_private.h create mode 100644 ctdb/protocol/protocol_sock.c create mode 100644 ctdb/protocol/protocol_tunnel.c create mode 100644 ctdb/protocol/protocol_types.c create mode 100644 ctdb/protocol/protocol_util.c create mode 100644 ctdb/protocol/protocol_util.h create mode 100644 ctdb/server/ctdb_banning.c create mode 100644 ctdb/server/ctdb_call.c create mode 100644 ctdb/server/ctdb_client.c create mode 100644 ctdb/server/ctdb_cluster_mutex.c create mode 100644 ctdb/server/ctdb_cluster_mutex.h create mode 100644 ctdb/server/ctdb_config.c create mode 100644 ctdb/server/ctdb_config.h create mode 100644 ctdb/server/ctdb_control.c create mode 100644 ctdb/server/ctdb_daemon.c create mode 100644 ctdb/server/ctdb_fork.c create mode 100644 ctdb/server/ctdb_freeze.c create mode 100644 ctdb/server/ctdb_keepalive.c create mode 100644 ctdb/server/ctdb_lock.c create mode 100644 ctdb/server/ctdb_lock_helper.c create mode 100644 ctdb/server/ctdb_logging.c create mode 100644 ctdb/server/ctdb_ltdb_server.c create mode 100644 ctdb/server/ctdb_monitor.c create mode 100644 ctdb/server/ctdb_mutex_fcntl_helper.c create mode 100644 ctdb/server/ctdb_persistent.c create mode 100644 ctdb/server/ctdb_recover.c create mode 100644 ctdb/server/ctdb_recoverd.c create mode 100644 ctdb/server/ctdb_recovery_helper.c create mode 100644 ctdb/server/ctdb_server.c create mode 100644 ctdb/server/ctdb_statistics.c create mode 100644 ctdb/server/ctdb_takeover.c create mode 100644 ctdb/server/ctdb_takeover_helper.c create mode 100644 ctdb/server/ctdb_traverse.c create mode 100644 ctdb/server/ctdb_tunables.c create mode 100644 ctdb/server/ctdb_tunnel.c create mode 100644 ctdb/server/ctdb_update_record.c create mode 100644 ctdb/server/ctdb_uptime.c create mode 100644 ctdb/server/ctdb_vacuum.c create mode 100644 ctdb/server/ctdbd.c create mode 100644 ctdb/server/eventscript.c create mode 100644 ctdb/server/ipalloc.c create mode 100644 ctdb/server/ipalloc.h create mode 100644 ctdb/server/ipalloc_common.c create mode 100644 ctdb/server/ipalloc_deterministic.c create mode 100644 ctdb/server/ipalloc_lcp2.c create mode 100644 ctdb/server/ipalloc_nondeterministic.c create mode 100644 ctdb/server/ipalloc_private.h create mode 100644 ctdb/server/legacy_conf.c create mode 100644 ctdb/server/legacy_conf.h create mode 100644 ctdb/tcp/ctdb_tcp.h create mode 100644 ctdb/tcp/tcp_connect.c create mode 100644 ctdb/tcp/tcp_init.c create mode 100644 ctdb/tcp/tcp_io.c create mode 100755 ctdb/tests/CLUSTER/complex/11_ctdb_delip_removes_ip.sh create mode 100755 ctdb/tests/CLUSTER/complex/18_ctdb_reloadips.sh create mode 100755 ctdb/tests/CLUSTER/complex/30_nfs_tickle_killtcp.sh create mode 100755 ctdb/tests/CLUSTER/complex/31_nfs_tickle.sh create mode 100755 ctdb/tests/CLUSTER/complex/32_cifs_tickle.sh create mode 100755 ctdb/tests/CLUSTER/complex/33_gratuitous_arp.sh create mode 100755 ctdb/tests/CLUSTER/complex/34_nfs_tickle_restart.sh create mode 100755 ctdb/tests/CLUSTER/complex/36_smb_reset_server.sh create mode 100755 ctdb/tests/CLUSTER/complex/37_nfs_reset_server.sh create mode 100755 ctdb/tests/CLUSTER/complex/41_failover_ping_discrete.sh create mode 100755 ctdb/tests/CLUSTER/complex/42_failover_ssh_hostname.sh create mode 100755 ctdb/tests/CLUSTER/complex/43_failover_nfs_basic.sh create mode 100755 ctdb/tests/CLUSTER/complex/44_failover_nfs_oneway.sh create mode 100755 ctdb/tests/CLUSTER/complex/45_failover_nfs_kill.sh create mode 100755 ctdb/tests/CLUSTER/complex/60_rogueip_releaseip.sh create mode 100755 ctdb/tests/CLUSTER/complex/61_rogueip_takeip.sh create mode 100644 ctdb/tests/CLUSTER/complex/README create mode 100644 ctdb/tests/CLUSTER/complex/scripts/local.bash create mode 100755 ctdb/tests/INTEGRATION/database/basics.001.attach.sh create mode 100755 ctdb/tests/INTEGRATION/database/basics.002.attach.sh create mode 100755 ctdb/tests/INTEGRATION/database/basics.003.detach.sh create mode 100755 ctdb/tests/INTEGRATION/database/basics.004.wipe.sh create mode 100755 ctdb/tests/INTEGRATION/database/basics.010.backup_restore.sh create mode 100755 ctdb/tests/INTEGRATION/database/fetch.001.ring.sh create mode 100755 ctdb/tests/INTEGRATION/database/fetch.002.ring-hotkeys.sh create mode 100755 ctdb/tests/INTEGRATION/database/readonly.001.basic.sh create mode 100755 ctdb/tests/INTEGRATION/database/recovery.001.volatile.sh create mode 100755 ctdb/tests/INTEGRATION/database/recovery.002.large.sh create mode 100755 ctdb/tests/INTEGRATION/database/recovery.003.no_resurrect.sh create mode 100755 ctdb/tests/INTEGRATION/database/recovery.010.persistent.sh create mode 100755 ctdb/tests/INTEGRATION/database/recovery.011.continue.sh create mode 100644 ctdb/tests/INTEGRATION/database/scripts/local.bash create mode 100755 ctdb/tests/INTEGRATION/database/transaction.001.ptrans.sh create mode 100755 ctdb/tests/INTEGRATION/database/transaction.002.loop.sh create mode 100755 ctdb/tests/INTEGRATION/database/transaction.003.loop_recovery.sh create mode 100755 ctdb/tests/INTEGRATION/database/transaction.004.update_record.sh create mode 100755 ctdb/tests/INTEGRATION/database/transaction.010.loop_recovery.sh create mode 100755 ctdb/tests/INTEGRATION/database/traverse.001.one.sh create mode 100755 ctdb/tests/INTEGRATION/database/traverse.002.many.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.001.fast.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.002.full.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.003.recreate.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.030.locked.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.031.locked.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.032.locked.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.033.locked.sh create mode 100755 ctdb/tests/INTEGRATION/database/vacuum.034.locked.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.001.list.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.010.addip.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.011.delip.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.012.reloadips.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.013.failover_noop.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.014.iface_gc.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.020.moveip.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.030.disable_enable.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.032.stop_continue.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.040.NoIPTakeover.sh create mode 100755 ctdb/tests/INTEGRATION/failover/pubips.050.missing_ip.sh create mode 100644 ctdb/tests/INTEGRATION/simple/README create mode 100755 ctdb/tests/INTEGRATION/simple/basics.000.onnode.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.001.listnodes.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.002.tunables.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.003.ping.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.004.getpid.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.005.process_exists.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.010.statistics.sh create mode 100755 ctdb/tests/INTEGRATION/simple/basics.011.statistics_reset.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.001.stop_leader_yield.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.002.ban_leader_yield.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.003.capability_leader_yield.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.006.stop_leader_yield_no_lock.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.007.ban_leader_yield_no_lock.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.008.capability_leader_yield_no_lock.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.010.getrelock.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.012.reclock_command.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.015.reclock_remove_lock.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.016.reclock_move_lock_dir.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.020.message_ring.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.021.tunnel_ring.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.030.node_stall_leader_timeout.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.090.unreachable.sh create mode 100755 ctdb/tests/INTEGRATION/simple/cluster.091.version_check.sh create mode 100755 ctdb/tests/INTEGRATION/simple/debug.001.getdebug.sh create mode 100755 ctdb/tests/INTEGRATION/simple/debug.002.setdebug.sh create mode 100755 ctdb/tests/INTEGRATION/simple/debug.003.dumpmemory.sh create mode 100755 ctdb/tests/INTEGRATION/simple/eventscripts.001.zero_scripts.sh create mode 100755 ctdb/tests/INTEGRATION/simple/eventscripts.090.debug_hung.sh create mode 100644 ctdb/tests/README create mode 100644 ctdb/tests/TODO create mode 100755 ctdb/tests/UNIT/cunit/cluster_mutex_001.sh create mode 100755 ctdb/tests/UNIT/cunit/cluster_mutex_002.sh create mode 100755 ctdb/tests/UNIT/cunit/cluster_mutex_003.sh create mode 100755 ctdb/tests/UNIT/cunit/cmdline_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/comm_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/comm_test_002.sh create mode 100755 ctdb/tests/UNIT/cunit/conf_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_002.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_003.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_004.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_005.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_006.sh create mode 100755 ctdb/tests/UNIT/cunit/config_test_007.sh create mode 100755 ctdb/tests/UNIT/cunit/ctdb_io_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/db_hash_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/event_protocol_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/event_script_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/hash_count_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/line_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/path_tests_001.sh create mode 100755 ctdb/tests/UNIT/cunit/pidfile_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/pkt_read_001.sh create mode 100755 ctdb/tests/UNIT/cunit/pkt_write_001.sh create mode 100755 ctdb/tests/UNIT/cunit/porting_tests_001.sh create mode 100755 ctdb/tests/UNIT/cunit/protocol_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/protocol_test_002.sh create mode 100755 ctdb/tests/UNIT/cunit/protocol_test_012.sh create mode 100755 ctdb/tests/UNIT/cunit/protocol_test_101.sh create mode 100755 ctdb/tests/UNIT/cunit/protocol_test_111.sh create mode 100755 ctdb/tests/UNIT/cunit/protocol_test_201.sh create mode 100755 ctdb/tests/UNIT/cunit/rb_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/reqid_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/run_event_001.sh create mode 100755 ctdb/tests/UNIT/cunit/run_proc_001.sh create mode 100755 ctdb/tests/UNIT/cunit/sock_daemon_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/sock_io_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/srvid_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/system_socket_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/system_socket_test_002.sh create mode 100755 ctdb/tests/UNIT/cunit/system_socket_test_003.sh create mode 100755 ctdb/tests/UNIT/cunit/tmon_test_001.sh create mode 100755 ctdb/tests/UNIT/cunit/tmon_test_002.sh create mode 100755 ctdb/tests/UNIT/cunit/tunable_test_001.sh create mode 100644 ctdb/tests/UNIT/eventd/README create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/ctdb.conf create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/debug-script.sh create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/events/data/03.notalink.script create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/events/data/README create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/events/empty/README create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/events/multi/01.test.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/events/multi/02.test.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/events/multi/03.test.script create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/events/random/01.disabled.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/events/random/02.enabled.script create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/events/random/README.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/events/random/a.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/data/01.dummy.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/data/02.disabled.script create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/empty/README create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/random/01.disabled.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/random/02.enabled.script create mode 100644 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/random/README.script create mode 100755 ctdb/tests/UNIT/eventd/etc-ctdb/share/events/random/a.script create mode 100755 ctdb/tests/UNIT/eventd/eventd_001.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_002.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_003.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_004.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_005.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_006.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_007.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_008.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_009.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_011.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_012.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_013.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_014.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_021.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_022.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_023.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_024.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_031.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_032.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_033.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_041.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_042.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_043.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_044.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_051.sh create mode 100755 ctdb/tests/UNIT/eventd/eventd_052.sh create mode 100644 ctdb/tests/UNIT/eventd/scripts/local.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.005.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.006.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.007.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.008.sh create mode 100755 ctdb/tests/UNIT/eventscripts/00.ctdb.init.009.sh create mode 100755 ctdb/tests/UNIT/eventscripts/01.reclock.init.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/01.reclock.init.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/01.reclock.init.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.005.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.006.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.007.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.012.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.014.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.015.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.017.sh create mode 100755 ctdb/tests/UNIT/eventscripts/05.system.monitor.018.sh create mode 100755 ctdb/tests/UNIT/eventscripts/06.nfs.releaseip.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/06.nfs.releaseip.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/06.nfs.takeip.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/06.nfs.takeip.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.010.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.012.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.013.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.init.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.init.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.init.021.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.init.022.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.init.023.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.005.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.006.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.009.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.010.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.012.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.013.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.014.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.015.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.016.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.017.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.monitor.018.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.multi.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.releaseip.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.releaseip.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.startup.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.startup.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.takeip.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.takeip.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/10.interface.takeip.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.012.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.013.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.014.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.015.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.021.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.022.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.023.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.024.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.025.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.031.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.041.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.042.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.051.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.052.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.053.sh create mode 100755 ctdb/tests/UNIT/eventscripts/11.natgw.054.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.005.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.006.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.007.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.008.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.009.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.010.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.012.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.013.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.014.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.015.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.016.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.017.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.018.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.019.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.021.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.022.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.023.sh create mode 100755 ctdb/tests/UNIT/eventscripts/13.per_ip_routing.024.sh create mode 100755 ctdb/tests/UNIT/eventscripts/20.multipathd.monitor.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/20.multipathd.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/20.multipathd.monitor.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/20.multipathd.monitor.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/31.clamd.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/31.clamd.monitor.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/40.vsftpd.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/40.vsftpd.shutdown.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/40.vsftpd.startup.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/41.httpd.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/41.httpd.shutdown.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/41.httpd.startup.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/48.netbios.shutdown.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/48.netbios.startup.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/49.winbind.monitor.101.sh create mode 100755 ctdb/tests/UNIT/eventscripts/49.winbind.monitor.102.sh create mode 100755 ctdb/tests/UNIT/eventscripts/49.winbind.shutdown.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/49.winbind.startup.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.101.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.103.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.104.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.105.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.106.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.110.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.111.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.112.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.monitor.113.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.shutdown.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.shutdown.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.shutdown.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/50.samba.startup.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.101.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.102.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.103.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.104.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.105.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.106.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.107.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.108.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.109.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.111.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.112.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.113.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.114.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.121.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.122.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.131.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.132.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.141.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.142.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.143.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.144.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.151.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.152.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.153.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.161.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.monitor.162.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.multi.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.multi.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.releaseip.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.releaseip.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.shutdown.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.shutdown.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.startup.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.startup.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.takeip.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/60.nfs.takeip.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.ipreallocated.011.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.ipreallocated.012.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.ipreallocated.013.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.ipreallocated.014.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.monitor.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.monitor.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.monitor.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.shutdown.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.shutdown.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.startup.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/91.lvs.startup.002.sh create mode 100644 ctdb/tests/UNIT/eventscripts/README create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.005.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.006.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.007.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.008.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.021.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.022.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.023.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.024.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.025.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.026.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.027.sh create mode 100755 ctdb/tests/UNIT/eventscripts/debug_locks.sh.028.sh create mode 100644 ctdb/tests/UNIT/eventscripts/etc-ctdb/public_addresses create mode 100755 ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local create mode 100755 ctdb/tests/UNIT/eventscripts/etc/init.d/nfs create mode 100755 ctdb/tests/UNIT/eventscripts/etc/init.d/nfslock create mode 100644 ctdb/tests/UNIT/eventscripts/etc/samba/smb.conf create mode 100644 ctdb/tests/UNIT/eventscripts/etc/sysconfig/nfs create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/00.ctdb.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/01.reclock.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/05.system.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/06.nfs.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/10.interface.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/11.natgw.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/13.per_ip_routing.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/20.multipathd.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/31.clamd.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/40.vsftpd.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/41.httpd.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/48.netbios.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/49.winbind.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/50.samba.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/60.nfs.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/91.lvs.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/debug_locks.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/local.sh create mode 100644 ctdb/tests/UNIT/eventscripts/scripts/statd-callout.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.001.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.002.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.003.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.004.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.005.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.006.sh create mode 100755 ctdb/tests/UNIT/eventscripts/statd-callout.007.sh create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ctdb create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ctdb-config create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ctdb_killtcp create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ctdb_lvs create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ctdb_natgw create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/date create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/df create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ethtool create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/exportfs create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/gstack create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/id create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ip create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ip6tables create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/iptables create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ipvsadm create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/kill create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/killall create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/multipath create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/net create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/nfs-fake-callout create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/nfsconf create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/pidof create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/pkill create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ps create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/rm create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/rpc.lockd create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/rpc.mountd create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/rpc.rquotad create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/rpc.statd create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/rpcinfo create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/service create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/sleep create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/smnotify create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/ss create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/stat create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/tdb_mutex_check create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/tdbdump create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/tdbtool create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/testparm create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/timeout create mode 100755 ctdb/tests/UNIT/eventscripts/stubs/wbinfo create mode 100755 ctdb/tests/UNIT/onnode/0001.sh create mode 100755 ctdb/tests/UNIT/onnode/0002.sh create mode 100755 ctdb/tests/UNIT/onnode/0003.sh create mode 100755 ctdb/tests/UNIT/onnode/0004.sh create mode 100755 ctdb/tests/UNIT/onnode/0005.sh create mode 100755 ctdb/tests/UNIT/onnode/0006.sh create mode 100755 ctdb/tests/UNIT/onnode/0010.sh create mode 100755 ctdb/tests/UNIT/onnode/0011.sh create mode 100755 ctdb/tests/UNIT/onnode/0070.sh create mode 100755 ctdb/tests/UNIT/onnode/0071.sh create mode 100755 ctdb/tests/UNIT/onnode/0072.sh create mode 100755 ctdb/tests/UNIT/onnode/0075.sh create mode 100644 ctdb/tests/UNIT/onnode/etc-ctdb/nodes create mode 100644 ctdb/tests/UNIT/onnode/scripts/local.sh create mode 100755 ctdb/tests/UNIT/onnode/stubs/ctdb create mode 100755 ctdb/tests/UNIT/onnode/stubs/ssh create mode 100755 ctdb/tests/UNIT/shellcheck/base_scripts.sh create mode 100755 ctdb/tests/UNIT/shellcheck/ctdb_helpers.sh create mode 100755 ctdb/tests/UNIT/shellcheck/event_scripts.sh create mode 100755 ctdb/tests/UNIT/shellcheck/functions.sh create mode 100755 ctdb/tests/UNIT/shellcheck/init_script.sh create mode 100644 ctdb/tests/UNIT/shellcheck/scripts/local.sh create mode 100755 ctdb/tests/UNIT/shellcheck/tests.sh create mode 100755 ctdb/tests/UNIT/shellcheck/tools.sh create mode 100644 ctdb/tests/UNIT/takeover/README create mode 100755 ctdb/tests/UNIT/takeover/det.001.sh create mode 100755 ctdb/tests/UNIT/takeover/det.002.sh create mode 100755 ctdb/tests/UNIT/takeover/det.003.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.001.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.002.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.003.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.004.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.005.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.006.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.007.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.008.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.009.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.010.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.011.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.012.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.013.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.014.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.015.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.016.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.024.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.025.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.027.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.028.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.029.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.030.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.031.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.032.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.033.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.034.sh create mode 100755 ctdb/tests/UNIT/takeover/lcp2.035.sh create mode 100755 ctdb/tests/UNIT/takeover/nondet.001.sh create mode 100755 ctdb/tests/UNIT/takeover/nondet.002.sh create mode 100755 ctdb/tests/UNIT/takeover/nondet.003.sh create mode 100644 ctdb/tests/UNIT/takeover/scripts/local.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/000.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/010.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/011.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/012.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/013.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/014.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/016.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/017.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/018.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/019.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/021.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/022.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/023.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/024.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/025.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/026.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/027.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/028.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/030.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/031.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/110.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/111.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/120.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/121.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/122.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/130.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/131.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/132.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/140.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/150.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/160.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/210.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/211.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/220.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/230.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/240.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/250.sh create mode 100755 ctdb/tests/UNIT/takeover_helper/260.sh create mode 100644 ctdb/tests/UNIT/takeover_helper/scripts/local.sh create mode 100644 ctdb/tests/UNIT/tool/README create mode 100755 ctdb/tests/UNIT/tool/ctdb.attach.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.attach.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.attach.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ban.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ban.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ban.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.catdb.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.catdb.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.cattdb.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.cattdb.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.continue.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.continue.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.continue.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.deletekey.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.disable.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.disable.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.disable.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.disable.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.enable.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.enable.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.enable.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getcapabilities.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getcapabilities.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getcapabilities.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getcapabilities.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getdbmap.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getdbseqnum.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getdbseqnum.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getdbstatus.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getdbstatus.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getpid.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getpid.010.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getreclock.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getreclock.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getvar.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.getvar.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ifaces.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.006.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ip.007.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ipinfo.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ipinfo.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ipinfo.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.leader.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.leader.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.listnodes.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.listnodes.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.listvars.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.006.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.007.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.008.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.lvs.010.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.006.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.007.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.008.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.natgw.010.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.006.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.nodestatus.007.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.pdelete.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ping.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.pnn.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.process-exists.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.process-exists.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.process-exists.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.pstore.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.ptrans.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.readkey.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.recover.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.011.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.012.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.013.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.014.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.015.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.016.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.017.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.018.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.019.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.020.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.021.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.023.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.reloadnodes.024.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.runstate.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.runstate.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.runstate.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.runstate.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.runstate.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbreadonly.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbreadonly.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbreadonly.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbreadonly.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbreadonly.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbsticky.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbsticky.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbsticky.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbsticky.004.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdbsticky.005.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdebug.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdebug.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setdebug.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setifacelink.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setifacelink.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setvar.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.setvar.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.status.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.status.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.status.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.stop.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.stop.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.stop.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.unban.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.unban.002.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.unban.003.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.uptime.001.sh create mode 100755 ctdb/tests/UNIT/tool/ctdb.writekey.001.sh create mode 100644 ctdb/tests/UNIT/tool/scripts/local.sh create mode 100755 ctdb/tests/etc-ctdb/events/legacy/00.test.script create mode 100755 ctdb/tests/local_daemons.sh create mode 120000 ctdb/tests/run_cluster_tests.sh create mode 100755 ctdb/tests/run_tests.sh create mode 100755 ctdb/tests/scripts/cluster.bash create mode 100644 ctdb/tests/scripts/common.sh create mode 100644 ctdb/tests/scripts/integration.bash create mode 100644 ctdb/tests/scripts/integration_local_daemons.bash create mode 100644 ctdb/tests/scripts/integration_real_cluster.bash create mode 100644 ctdb/tests/scripts/script_install_paths.sh create mode 100755 ctdb/tests/scripts/test_wrap create mode 100644 ctdb/tests/scripts/unit.sh create mode 100644 ctdb/tests/src/cluster_mutex_test.c create mode 100644 ctdb/tests/src/cluster_wait.c create mode 100644 ctdb/tests/src/cluster_wait.h create mode 100644 ctdb/tests/src/cmdline_test.c create mode 100644 ctdb/tests/src/comm_client_test.c create mode 100644 ctdb/tests/src/comm_server_test.c create mode 100644 ctdb/tests/src/comm_test.c create mode 100644 ctdb/tests/src/conf_test.c create mode 100644 ctdb/tests/src/ctdb_io_test.c create mode 100644 ctdb/tests/src/ctdb_packet_parse.c create mode 100644 ctdb/tests/src/ctdb_takeover_tests.c create mode 100644 ctdb/tests/src/db_hash_test.c create mode 100644 ctdb/tests/src/db_test_tool.c create mode 100644 ctdb/tests/src/dummy_client.c create mode 100644 ctdb/tests/src/errcode.c create mode 100644 ctdb/tests/src/event_script_test.c create mode 100644 ctdb/tests/src/fake_ctdbd.c create mode 100644 ctdb/tests/src/fetch_loop.c create mode 100644 ctdb/tests/src/fetch_loop_key.c create mode 100644 ctdb/tests/src/fetch_readonly.c create mode 100644 ctdb/tests/src/fetch_readonly_loop.c create mode 100644 ctdb/tests/src/fetch_ring.c create mode 100644 ctdb/tests/src/g_lock_loop.c create mode 100644 ctdb/tests/src/hash_count_test.c create mode 100644 ctdb/tests/src/ipalloc_read_known_ips.c create mode 100644 ctdb/tests/src/ipalloc_read_known_ips.h create mode 100644 ctdb/tests/src/line_test.c create mode 100644 ctdb/tests/src/lock_tdb.c create mode 100644 ctdb/tests/src/message_ring.c create mode 100644 ctdb/tests/src/pidfile_test.c create mode 100644 ctdb/tests/src/pkt_read_test.c create mode 100644 ctdb/tests/src/pkt_write_test.c create mode 100644 ctdb/tests/src/porting_tests.c create mode 100644 ctdb/tests/src/protocol_basic_test.c create mode 100644 ctdb/tests/src/protocol_common.c create mode 100644 ctdb/tests/src/protocol_common.h create mode 100644 ctdb/tests/src/protocol_common_basic.c create mode 100644 ctdb/tests/src/protocol_common_basic.h create mode 100644 ctdb/tests/src/protocol_common_ctdb.c create mode 100644 ctdb/tests/src/protocol_common_ctdb.h create mode 100644 ctdb/tests/src/protocol_ctdb_compat_test.c create mode 100644 ctdb/tests/src/protocol_ctdb_test.c create mode 100644 ctdb/tests/src/protocol_types_compat_test.c create mode 100644 ctdb/tests/src/protocol_types_test.c create mode 100644 ctdb/tests/src/protocol_util_test.c create mode 100644 ctdb/tests/src/rb_test.c create mode 100644 ctdb/tests/src/reqid_test.c create mode 100644 ctdb/tests/src/run_event_test.c create mode 100644 ctdb/tests/src/run_proc_test.c create mode 100644 ctdb/tests/src/sigcode.c create mode 100644 ctdb/tests/src/sock_daemon_test.c create mode 100644 ctdb/tests/src/sock_io_test.c create mode 100644 ctdb/tests/src/srvid_test.c create mode 100644 ctdb/tests/src/system_socket_test.c create mode 100644 ctdb/tests/src/test_backtrace.c create mode 100644 ctdb/tests/src/test_backtrace.h create mode 100644 ctdb/tests/src/test_mutex_raw.c create mode 100644 ctdb/tests/src/test_options.c create mode 100644 ctdb/tests/src/test_options.h create mode 100644 ctdb/tests/src/tmon_ping_test.c create mode 100644 ctdb/tests/src/tmon_test.c create mode 100644 ctdb/tests/src/transaction_loop.c create mode 100644 ctdb/tests/src/tunable_test.c create mode 100644 ctdb/tests/src/tunnel_cmd.c create mode 100644 ctdb/tests/src/tunnel_test.c create mode 100644 ctdb/tests/src/update_record.c create mode 100644 ctdb/tests/src/update_record_persistent.c create mode 100755 ctdb/tests/test_check_tcp_ports.sh create mode 100644 ctdb/tools/ctdb.c create mode 100755 ctdb/tools/ctdb_diagnostics create mode 100644 ctdb/tools/ctdb_killtcp.c create mode 100755 ctdb/tools/ctdb_lvs create mode 100755 ctdb/tools/ctdb_natgw create mode 100644 ctdb/tools/ltdbtool.c create mode 100755 ctdb/tools/onnode create mode 100644 ctdb/utils/ceph/ctdb_mutex_ceph_rados_helper.c create mode 100755 ctdb/utils/ceph/test_ceph_rados_reclock.sh create mode 100755 ctdb/utils/etcd/ctdb_etcd_lock create mode 100644 ctdb/utils/nagios/README create mode 100755 ctdb/utils/nagios/check_ctdb create mode 100644 ctdb/utils/ping_pong/ping_pong.c create mode 100644 ctdb/utils/pmda/Install create mode 100644 ctdb/utils/pmda/README create mode 100644 ctdb/utils/pmda/Remove create mode 100644 ctdb/utils/pmda/domain.h create mode 100644 ctdb/utils/pmda/help create mode 100644 ctdb/utils/pmda/pmda_ctdb.c create mode 100644 ctdb/utils/pmda/pmns create mode 100644 ctdb/utils/pmda/root create mode 100644 ctdb/utils/scsi_io/scsi_io.c create mode 100644 ctdb/utils/smnotify/smnotify.c create mode 100644 ctdb/utils/smnotify/smnotify.x create mode 100644 ctdb/utils/tdb/tdb_mutex_check.c create mode 100644 ctdb/wscript (limited to 'ctdb') diff --git a/ctdb/.bzrignore b/ctdb/.bzrignore new file mode 100644 index 0000000..6560aa4 --- /dev/null +++ b/ctdb/.bzrignore @@ -0,0 +1,24 @@ +config.status +Makefile +bin +config.log +push.sh +ctdb_test +config.cache +configure +config.h +config.h.in +nodes-ssh.txt +TAGS +ctdb-0 +ctdb-1 +ctdb-2 +ctdb-3 +nodes.txt +TAGS +web/packages +rec.lock +test.db +sock.1 +sock.3 +sock.4 diff --git a/ctdb/.gitignore b/ctdb/.gitignore new file mode 100644 index 0000000..f0534b3 --- /dev/null +++ b/ctdb/.gitignore @@ -0,0 +1,32 @@ +*.[oa] +*~ +*.swp +config.status +configure +ctdb.pc +publish*.sh +push*.sh +web/packages +TAGS +tags +bin +Makefile +config.h +config.h.in +config.log +utils/smnotify/gen_smnotify.c +utils/smnotify/gen_xdr.c +utils/smnotify/smnotify.h +nodes.txt +public_addresses.txt +rec.lock +test.db +tests/bin +tests/var +tests/takeover/ctdb_takeover.pyc +tests/eventscripts/var +tests/eventscripts/etc/iproute2 +tests/eventscripts/etc-ctdb/policy_routing +packaging/RPM/ctdb.spec +doc/*.[1-7] +doc/*.[1-7].html diff --git a/ctdb/COPYING b/ctdb/COPYING new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/ctdb/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/ctdb/Makefile b/ctdb/Makefile new file mode 100644 index 0000000..5dbc7ac --- /dev/null +++ b/ctdb/Makefile @@ -0,0 +1,83 @@ +# simple makefile wrapper to run waf + +WAF_BINARY=$(PYTHON) ../buildtools/bin/waf +WAF=PYTHONHASHSEED=1 WAF_MAKE=1 $(WAF_BINARY) + +all: + $(WAF) build + +install: + $(WAF) install + +uninstall: + $(WAF) uninstall + +test: FORCE + $(WAF) test $(TEST_OPTIONS) + +testenv: + $(WAF) test --testenv $(TEST_OPTIONS) + +autotest: + $(WAF) autotest $(TEST_OPTIONS) + +quicktest: + $(WAF) test --quick $(TEST_OPTIONS) + +show_version: + @touch .tmplock + @WAFLOCK=.tmplock $(WAF) show_version + +manpages: + touch .tmplock + WAFLOCK=.tmplock $(WAF) manpages + +dist: + touch .tmplock + WAFLOCK=.tmplock $(WAF) dist + +distcheck: + touch .tmplock + WAFLOCK=.tmplock $(WAF) distcheck + +rpm: + touch .tmplock + WAFLOCK=.tmplock $(WAF) rpm + +clean: + $(WAF) clean + +distclean: + $(WAF) distclean + +reconfigure: configure + $(WAF) reconfigure + +show_waf_options: + $(WAF) --help + +# some compatibility make targets +everything: all + +testsuite: all + +check: test + +torture: all + +# this should do an install as well, once install is finished +installcheck: test + +etags: + $(WAF) etags + +ctags: + touch .tmplock + WAFLOCK=.tmplock $(WAF) ctags + +pydoctor: + $(WAF) pydoctor + +bin/%:: FORCE + $(WAF) --targets=`basename $@` +FORCE: diff --git a/ctdb/README b/ctdb/README new file mode 100644 index 0000000..3099a6d --- /dev/null +++ b/ctdb/README @@ -0,0 +1,8 @@ +This is the release version of CTDB, a clustered implementation of TDB +database used by Samba and other projects to store temporary data. + +This software is freely distributable under the GNU public license, +a copy of which you should have received with this software (in a file +called COPYING). + +For documentation on CTDB, please visit CTDB website http://ctdb.samba.org. diff --git a/ctdb/client/client.h b/ctdb/client/client.h new file mode 100644 index 0000000..5f17403 --- /dev/null +++ b/ctdb/client/client.h @@ -0,0 +1,1416 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CLIENT_H__ +#define __CTDB_CLIENT_H__ + +#include +#include + +#include "protocol/protocol.h" +#include "common/srvid.h" + +/** + * @file client.h + * + * @brief Client api to talk to ctdb daemon + * + * This API allows one to connect to ctdb daemon, perform various database + * operations, send controls to ctdb daemon and send messages to other ctdb + * clients. + */ + +/** + * @brief The abstract context that holds client connection to ctdb daemon + */ +struct ctdb_client_context; + +/** + * @brief The abstract context that holds a tunnel endpoint + */ +struct ctdb_tunnel_context; + +/** + * @brief The abstract context that represents a clustered database + */ +struct ctdb_db_context; + +/** + * @brief The abstract context that represents a record from a distributed + * database + */ +struct ctdb_record_handle; + +/** + * @brief The abstract context that represents a transaction on a replicated + * database + */ +struct ctdb_transaction_handle; + +/** + * @brief Client callback function + * + * This function can be registered to be invoked in case of ctdb daemon going + * away. + */ +typedef void (*ctdb_client_callback_func_t)(void *private_data); + +/** + * @brief Tunnel callback function + * + * This function is registered when a tunnel endpoint is set up. When the + * tunnel endpoint receives a message, this function is invoked. + */ +typedef void (*ctdb_tunnel_callback_func_t)(struct ctdb_tunnel_context *tctx, + uint32_t srcnode, uint32_t reqid, + uint8_t *buf, size_t buflen, + void *private_data); + +/** + * @brief Async computation start to initialize a connection to ctdb daemon + * + * This returns a ctdb client context. Freeing this context will free the + * connection to ctdb daemon and any memory associated with it. + * + * If the connection to ctdb daemon is lost, the client will terminate + * automatically as the library will call exit(). If the client code + * wants to perform cleanup or wants to re-establish a new connection, + * the client should register a disconnect callback function. + * + * @see ctdb_client_set_disconnect_callback + * + * When a disconnect callback function is registered, client library will + * not call exit(). It is the responsibility of the client code to take + * appropriate action. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] sockpath Path to ctdb daemon unix domain socket + * @return new tevent request, NULL on failure + */ +struct tevent_req *ctdb_client_init_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + const char *sockpath); + +/** + * @brief Async computation end to initialize a connection to ctdb daemon + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc memory context + * @param[out] result The new ctdb client context + * @return true on success, false on failure + */ +bool ctdb_client_init_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_client_context **result); + +/** + * @brief Sync wrapper to initialize ctdb connection + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] sockpath Path to ctdb daemon unix domain socket + * @param[out] result The new ctdb client context + * @return 0 on succcess, errno on failure + */ +int ctdb_client_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + const char *sockpath, + struct ctdb_client_context **result); + +/** + * @brief Register a callback in case of client disconnection + * + * This allows client code to know if the connection to ctdb daemon is lost. + * This is useful if the client wants to re-establish a new connection to ctdb + * daemon. + * + * @param[in] client Client connection context + * @param[in] func Callback function + * @param[in] private_data private data for callback function + */ +void ctdb_client_set_disconnect_callback(struct ctdb_client_context *client, + ctdb_client_callback_func_t func, + void *private_data); + +/** + * @brief Get the node number of the current node + * + * @param[in] client Client connection context + * return node number on success, CTDB_UNKNOWN_PNN on error + */ +uint32_t ctdb_client_pnn(struct ctdb_client_context *client); + +/** + * @brief Client event loop waiting for a flag + * + * This can used to wait for asynchronous computations to complete. + * When this function is called, it will run tevent event loop and wait + * till the done flag is set to true. This function will block and will + * not return as long as the done flag is false. + * + * @param[in] ev Tevent context + * @param[in] done Boolean flag to indicate when to stop waiting + */ +void ctdb_client_wait(struct tevent_context *ev, bool *done); + +/** + * @brief Client event loop waiting for function to return true with timeout + * + * This can be used to wait for asynchronous computations to complete. + * When this function is called, it will run tevent event loop and wait + * till the done function returns true or if the timeout occurs. + * + * This function will return when either + * - done function returns true, or + * - timeout has occurred. + * + * @param[in] ev Tevent context + * @param[in] done_func Function flag to indicate when to stop waiting + * @param[in] private_data Passed to done function + * @param[in] timeout How long to wait + * @return 0 on success, ETIMEDOUT on timeout, and errno on failure + */ +int ctdb_client_wait_func_timeout(struct tevent_context *ev, + bool (*done_func)(void *private_data), + void *private_data, + struct timeval timeout); + +/** + * @brief Client event loop waiting for a flag with timeout + * + * This can be used to wait for asynchronous computations to complete. + * When this function is called, it will run tevent event loop and wait + * till the done flag is set to true or if the timeout occurs. + * + * This function will return when either + * - done flag is set to true, or + * - timeout has occurred. + * + * @param[in] ev Tevent context + * @param[in] done Boolean flag to indicate when to stop waiting + * @param[in] timeout How long to wait + * @return 0 on success, ETIMEDOUT on timeout, and errno on failure + */ +int ctdb_client_wait_timeout(struct tevent_context *ev, bool *done, + struct timeval timeout); + +/** + * @brief Async computation start to wait till recovery is completed + * + * CTDB daemon does not perform many operations while in recovery (especially + * database operations). This computation allows one to wait till ctdb daemon has + * finished recovery. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @return new tevent request, or NULL on failure + */ +struct tevent_req *ctdb_recovery_wait_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client); + +/** + * @brief Async computation end to wait till recovery is completed + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_recovery_wait_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper for ctdb_recovery_wait computation + * + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @return true on success, false on failure + */ +bool ctdb_recovery_wait(struct tevent_context *ev, + struct ctdb_client_context *client); + +/** + * @brief Async computation start to migrate a database record + * + * This sends a request to ctdb daemon to migrate a database record to + * the local node. CTDB daemon will locate the data master for the record + * and will migrate record (and the data master) to the current node. + * + * @see ctdb_fetch_lock_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] request CTDB request data + * @return a new tevent req, or NULL on failure + */ +struct tevent_req *ctdb_client_call_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_req_call *request); + +/** + * @brief Async computation end to migrate a database record + * + * @param[in] req Tevent request + * @param[in] mem_ctx Talloc memory context + * @param[out] reply CTDB reply data + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_client_call_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, + struct ctdb_reply_call **reply, int *perr); + + +/** + * @brief Async computation start to send a message to remote client(s) + * + * This sends a message to ctdb clients on a remote node. All the + * messages are associated with a specific SRVID. All the clients on the + * remote node listening to that SRVID, will get the message. + * + * Clients can register and deregister for messages for a SRVID using + * ctdb_client_set_message_handler() and ctdb_client_remove_message_handler(). + * + * @see ctdb_client_set_message_handler_send, + * ctdb_client_remove_message_handler_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] destnode Remote node id + * @param[in] message Message to send + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_client_message_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, + struct ctdb_req_message *message); + +/** + * @brief Async computation end to send a message to remote client(s) + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_client_message_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper to send a message to client(s) on remote node + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] destnode Node id + * @param[in] message Message to send + */ +int ctdb_client_message(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, struct ctdb_req_message *message); + +/** + * @brief Async computation start to send a message to multiple nodes + * + * This sends a message to ctdb clients on multiple remote nodes. All the + * messages are associated with a specific SRVID. All the clients on remote + * nodes listening to that SRVID, will get the message. + * + * Clients can register and deregister for messages for a SRVID using + * ctdb_client_set_message_handler() and ctdb_client_remove_message_handler(). + * + * @see ctdb_client_set_message_handler_send, + * ctdb_client_remove_message_handler_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] pnn_list List of node ids + * @param[in] count Number of node ids + * @param[in] message Message to send + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_client_message_multi_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct ctdb_req_message *message); + +/** + * @brief Async computation end to send a message to multiple nodes + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc memory context + * @param[out] perr_list The status from each node id + * @return true on success, false on failure + * + * If perr_list is not NULL, then the status (0 on success, errno on failure) + * of sending message to each of the node in the specified node list. The + * perr_list is an array of the same size as of pnn_list. + */ +bool ctdb_client_message_multi_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, int **perr_list); + +/** + * @brief Sync wrapper to send a message to multiple nodes + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] pnn_list List of node ids + * @param[in] count Number of node ids + * @param[in] message Message to send + * @param[out] perr_list The status from each node id + * @return 0 on success, errno on failure + */ +int ctdb_client_message_multi(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct ctdb_req_message *message, + int **perr_list); + +/** + * @brief Async computation start to receive messages for a SRVID + * + * This computation informs ctdb that the client is interested in all messages + * for a specific SRVID. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] srvid SRVID + * @param[in] handler Callback function to call when a message is received + * @param[in] private_data Private data for callback + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_client_set_message_handler_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, + srvid_handler_fn handler, + void *private_data); + +/** + * @brief Async computation end to receive messages for a SRVID + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_client_set_message_handler_recv(struct tevent_req *req, int *perr); + +/** + * Sync wrapper to receive messages for a SRVID + * + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] srvid SRVID + * @param[in] handler Callback function to call when a message is received + * @param[in] private_data Private data for callback + * @return 0 on success, errno on failure + */ +int ctdb_client_set_message_handler(struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, srvid_handler_fn handler, + void *private_data); + +/** + * @brief Async computation start to stop receiving messages for a SRVID + * + * This computation informs ctdb that the client is no longer interested in + * messages for a specific SRVID. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] srvid SRVID + * @param[in] private_data Private data used to register callback + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_client_remove_message_handler_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, + void *private_data); + +/** + * @brief Async computation end to stop receiving messages for a SRVID + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_client_remove_message_handler_recv(struct tevent_req *req, + int *perr); + +/** + * Sync wrapper to stop receiving messages for a SRVID + * + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] srvid SRVID + * @param[in] private_data Private data used to register callback + * @return 0 on success, errno on failure + */ +int ctdb_client_remove_message_handler(struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, void *private_data); + +/** + * @brief Async computation start to send a control to ctdb daemon + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] destnode Node id + * @param[in] timeout How long to wait + * @param[in] request Control request + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_client_control_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, + struct timeval timeout, + struct ctdb_req_control *request); + +/** + * @brief Async computation end to send a control to ctdb daemon + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc memory context + * @param[out] preply Control reply + * @return true on success, false on failure + */ +bool ctdb_client_control_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_reply_control **preply); + +/** + * @brief Sync wrapper to send a control to ctdb daemon + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] destnode Node id + * @param[in] timeout How long to wait + * @param[in] request Control request + * @param[out] preply Control reply + * @return 0 on success, errno on failure + */ +int ctdb_client_control(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, + struct timeval timeout, + struct ctdb_req_control *request, + struct ctdb_reply_control **preply); + +/** + * @brief Async computation start to send a control to multiple nodes + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] pnn_list List of node ids + * @param[in] count Number of node ids + * @param[in] timeout How long to wait + * @param[in] request Control request + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_client_control_multi_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct timeval timeout, + struct ctdb_req_control *request); + +/** + * @brief Async computation end to send a control to multiple nodes + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc memory context + * @param[out] perr_list Status from each node + * @param[out] preply Control reply from each node + * @return true on success, false on failure + */ +bool ctdb_client_control_multi_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, int **perr_list, + struct ctdb_reply_control ***preply); + +/** + * @brief Sync wrapper to send a control to multiple nodes + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] pnn_list List of node ids + * @param[in] count Number of node ids + * @param[in] timeout How long to wait + * @param[in] request Control request + * @param[out] perr_list Status from each node + * @param[out] preply Control reply from each node + * @return 0 on success, errno on failure + */ +int ctdb_client_control_multi(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct timeval timeout, + struct ctdb_req_control *request, + int **perr_list, + struct ctdb_reply_control ***preply); + +/** + * @brief Check err_list for errors + * + * This is a convenience function to parse the err_list returned from + * functions that send requests to multiple nodes. + * + * If status from any of the node is non-zero, then return first non-zero + * status. + * + * If status from all the nodes is 0, then return 0. + * + * @param[in] pnn_list List of node ids + * @param[in] count Number of node ids + * @param[in] err_list Status from each node + * @param[out] pnn Node id in case of failure + * @return 0 if no failures, status from first failure + */ +int ctdb_client_control_multi_error(uint32_t *pnn_list, int count, + int *err_list, uint32_t *pnn); + +/** + * @brief Async computation start to setup a tunnel endpoint + * + * This computation sets up a tunnel endpoint corresponding to a tunnel_id. + * A tunnel is a ctdb transport to deliver new protocol between endpoints. + * + * For two endpoints to communicate using new protocol, + * 1. Set up tunnel endpoints + * 2. Send requests + * 3. Send replies + * 4. Destroy tunnel endpoints + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] tunnel_id Unique tunnel id + * @param[in] callback Callback function to call when a message is received + * @param[in] private_data Private data for callback + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_tunnel_setup_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t tunnel_id, + ctdb_tunnel_callback_func_t callback, + void *private_data); + +/** + * @brief Async computation end to setup a tunnel + * + * @param[in] req Tevent request + * @param[in] perr errno in case of failure + * @param[out] result A new tunnel context + * @return true on success, false on failure + * + * Tunnel context should never be freed by user. + */ +bool ctdb_tunnel_setup_recv(struct tevent_req *req, int *perr, + struct ctdb_tunnel_context **result); + +/** + * @brief Sync wrapper for ctdb_tunnel_setup computation + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] tunnel_id Unique tunnel id + * @param[in] callback Callback function to call when a message is received + * @param[in] private_data Private data for callback + * @param[out] result A new tunnel context + * @return 0 on success, errno on failure + */ +int ctdb_tunnel_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, uint64_t tunnel_id, + ctdb_tunnel_callback_func_t callback, void *private_data, + struct ctdb_tunnel_context **result); + +/** + * @brief Async computation start to destroy a tunnel endpoint + * + * This computation destroys the tunnel endpoint. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] tctx Tunnel context + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_tunnel_destroy_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_tunnel_context *tctx); + +/** + * @brief Async computation end to destroy a tunnel endpoint + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_tunnel_destroy_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper for ctdb_tunnel_destroy computation + * + * @param[in] ev Tevent context + * @param[in] tctx Tunnel context + * @return 0 on success, errno on failure + */ +int ctdb_tunnel_destroy(struct tevent_context *ev, + struct ctdb_tunnel_context *tctx); + +/** + * @brief Async computation start to send a request via a tunnel + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] tctx Tunnel context + * @param[in] destnode PNN of destination + * @param[in] timeout How long to wait + * @param[in] buf Message to send + * @param[in] buflen Size of the message to send + * @param[in] wait_for_reply Whether to wait for reply + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_tunnel_request_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, + uint32_t destnode, + struct timeval timeout, + uint8_t *buf, size_t buflen, + bool wait_for_reply); + +/** + * @brief Async computation end to send a request via a tunnel + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc context + * @param[out] buf Reply data if expected + * @param[out] buflen Size of reply data if expected + * @return true on success, false on failure + */ +bool ctdb_tunnel_request_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, uint8_t **buf, + size_t *buflen); + +/** + * @brief Sync wrapper for ctdb_tunnel_request computation + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] tctx Tunnel context + * @param[in] destnode PNN of destination + * @param[in] timeout How long to wait + * @param[in] buf Message to send + * @param[in] buflen Size of the message to send + * @param[in] wait_for_reply Whether to wait for reply + * @return 0 on success, errno on failure + */ +int ctdb_tunnel_request(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, uint32_t destnode, + struct timeval timeout, uint8_t *buf, size_t buflen, + bool wait_for_reply); + +/** + * @brief Async computation start to send a reply via a tunnel + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] tctx Tunnel context + * @param[in] destnode PNN of destination + * @param[in] reqid Request id + * @param[in] timeout How long to wait + * @param[in] buf Reply data + * @param[in] buflen Size of reply data + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_tunnel_reply_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, + uint32_t destnode, uint32_t reqid, + struct timeval timeout, + uint8_t *buf, size_t buflen); + +/** + * @brief Async computation end to send a reply via a tunnel + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_tunnel_reply_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper for ctdb_tunnel_reply computation + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] tctx Tunnel context + * @param[in] destnode PNN of destination + * @param[in] reqid Request id + * @param[in] timeout How long to wait + * @param[in] buf Reply data + * @param[in] buflen Size of reply data + * @return 0 on success, errno on failure + */ +int ctdb_tunnel_reply(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, uint32_t destnode, + uint32_t reqid, struct timeval timeout, + uint8_t *buf, size_t buflen); + +/** + * @brief Async computation start to attach a database + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in[ client Client connection context + * @param[in] timeout How long to wait + * @param[in] db_name Name of the database + * @param[in] db_flags Database flags + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_attach_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + const char *db_name, uint8_t db_flags); + +/** + * @brief Async computation end to attach a database + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[out] result New database context + * @return true on success, false on failure + */ +bool ctdb_attach_recv(struct tevent_req *req, int *perr, + struct ctdb_db_context **result); + +/** + * @brief Sync wrapper to attach a database + * + * @param[in] ev Tevent context + * @param[in[ client Client connection context + * @param[in] timeout How long to wait + * @param[in] db_name Name of the database + * @param[in] db_flags Database flags + * @param[out] result New database context + * @return 0 on success, errno on failure + */ +int ctdb_attach(struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + const char *db_name, uint8_t db_flags, + struct ctdb_db_context **result); + +/** + * @brief Async computation start to detach a database + * + * Only volatile databases can be detached at runtime. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in[ client Client connection context + * @param[in] timeout How long to wait + * @param[in] db_id Database id + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_detach_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, uint32_t db_id); + +/** + * @brief Async computation end to detach a database + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_detach_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper to detach a database + * + * Only volatile databases can be detached at runtime. + * + * @param[in] ev Tevent context + * @param[in[ client Client connection context + * @param[in] timeout How long to wait + * @param[in] db_id Database id + * @return 0 on success, errno on failure + */ +int ctdb_detach(struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, uint32_t db_id); + + +/** + * @brief Get database id from database context + * + * @param[in] db Database context + * @return database id + */ +uint32_t ctdb_db_id(struct ctdb_db_context *db); + +/** + * @brief Traverse a database locally on the node + * + * This function traverses a database locally on the node and for each record + * calls the parser function. If the parser function returns 1, the traverse + * will terminate. If parser function returns 0, the traverse will continue + * till all records in database are parsed. + * + * This is useful for replicated databases, since each node has exactly the + * same records. + * + * @param[in] db Database context + * @param[in] readonly Is the traversal for reading or updating + * @param[in] extract_header Whether to extract ltdb header from record data + * @param[in] parser Record parsing function + * @param[in] private_data Private data for parser function + * @return 0 on success, non-zero return value from parser function + */ +int ctdb_db_traverse_local(struct ctdb_db_context *db, bool readonly, + bool extract_header, + ctdb_rec_parser_func_t parser, void *private_data); + +/** + * @brief Async computation start to a cluster-wide database traverse + * + * This function traverses a database on all the nodes and for each record + * calls the parser function. If the parser function returns 1, the traverse + * will terminate. If parser function returns 0, the traverse will continue + * till all records all on nodes are parsed. + * + * This is useful for distributed databases as the records are distributed + * among the cluster nodes. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] db Database context + * @param[in] destnode Node id + * @param[in] timeout How long to wait + * @param[in] parser Record parser function + * @param[in] private_data Private data for parser + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_db_traverse_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + uint32_t destnode, + struct timeval timeout, + ctdb_rec_parser_func_t parser, + void *private_data); + +/** + * @brief Async computation end to a cluster-wide database traverse + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_db_traverse_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper for a cluster-wide database traverse + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] db Database context + * @param[in] destnode Node id + * @param[in] timeout How long to wait + * @param[in] parser Record parser function + * @param[in] private_data Private data for parser + * @return 0 on success, errno on failure or non-zero status from parser + */ +int ctdb_db_traverse(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + uint32_t destnode, struct timeval timeout, + ctdb_rec_parser_func_t parser, void *private_data); + +/** + * @brief Fetch a record from a local database + * + * This function is primarily for internal use. + * Clients should use ctdb_fetch_lock() instead. + * + * @param[in] db Database context + * @param[in] key Record key + * @param[out] header Record header + * @param[in] mem_ctx Talloc memory context + * @param[out] data Record data + */ +int ctdb_ltdb_fetch(struct ctdb_db_context *db, TDB_DATA key, + struct ctdb_ltdb_header *header, + TALLOC_CTX *mem_ctx, TDB_DATA *data); + +/** + * @brief Async computation start to fetch a locked record + * + * This function is used to fetch a record from a distributed database. + * + * If the record is already available on the local node, then lock the + * record and return the record handle. + * + * If the record is not available on the local node, send a CTDB request to + * migrate the record. Once the record is migrated to the local node, lock + * the record and return the record handle. + * + * At the end of the computation, a record handle is returned which holds + * the record lock. When the record handle is freed, the record is unlocked. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client context + * @param[in] db Database context + * @param[in] key Record key + * @param[in] readonly Whether to request readonly copy of the record + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_fetch_lock_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + TDB_DATA key, bool readonly); + +/** + * @brief Async computation end to fetch a locked record + * + * @param[in] req Tevent request + * @param[out] header Record header + * @param[in] mem_ctx Talloc memory context + * @param[out] data Record data + * @param[out] perr errno in case of failure + * @return a new record handle, NULL on failure + */ +struct ctdb_record_handle *ctdb_fetch_lock_recv(struct tevent_req *req, + struct ctdb_ltdb_header *header, + TALLOC_CTX *mem_ctx, + TDB_DATA *data, int *perr); + +/** + * @brief Sync wrapper to fetch a locked record + * + * @see ctdb_fetch_lock_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client context + * @param[in] db Database context + * @param[in] key Record key + * @param[in] readonly Whether to request readonly copy of the record + * @param[out] header Record header + * @param[out] data Record data + * return 0 on success, errno on failure + */ +int ctdb_fetch_lock(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, TDB_DATA key, bool readonly, + struct ctdb_record_handle **out, + struct ctdb_ltdb_header *header, TDB_DATA *data); + +/** + * @brief Update a locked record + * + * This function is used to update a record in a distributed database. + * + * This function should NOT be used to store null data, instead use + * ctdb_delete_record(). + * + * @param[in] h Record handle + * @param[in] data New record data + * @return 0 on success, errno on failure + */ +int ctdb_store_record(struct ctdb_record_handle *h, TDB_DATA data); + +/** + * @brief Async computation start to delete a locked record + * + * This function is used to delete a record in a distributed database + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] h Record handle + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_delete_record_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_record_handle *h); + +/** + * @brief Async computation end to delete a locked record + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_delete_record_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper to delete a locked record + * + * @see ctdb_delete_record_send + * + * @param[in] h Record handle + * @return 0 on success, errno on failure + */ +int ctdb_delete_record(struct ctdb_record_handle *h); + +/** + * @brief Async computation start to get a global database lock + * + * Functions related to global locks are primarily used internally for + * implementing transaction api. + * + * Clients should use transaction api directly. + * @see ctdb_transaction_start_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client context + * @param[in] db Database context for g_lock.tdb + * @param[in] keyname Record key + * @param[in] sid Server id + * @param[in] readonly Lock type + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_g_lock_lock_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + const char *keyname, + struct ctdb_server_id *sid, + bool readonly); + +/** + * @brief Async computation end to get a global database lock + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_g_lock_lock_recv(struct tevent_req *req, int *perr); + +/** + * @brief Async computation start to release a global database lock + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] db Database context + * @param[in] keyname Record key + * @param[in] sid Server id + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_g_lock_unlock_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + const char *keyname, + struct ctdb_server_id sid); + +/** + * @brief Async computation end to release a global database lock + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_g_lock_unlock_recv(struct tevent_req *req, int *perr); + +/** + * @brief Async computation start to start a transaction + * + * This function is used to start a transaction on a replicated database. + * + * To perform any updates on a replicated database + * - start transaction + * - fetch record (ctdb_transaction_fetch_record) + * - store record (ctdb_transaction_store_record) + * - delete record (ctdb_transaction_delete_record) + * - commit transaction (ctdb_transaction_commit_send), or + * - cancel transaction (ctdb_transaction_cancel_send) + * + * Starting a transaction will return a transaction handle. This is used + * for updating records under a transaction. This handle is automatically + * freed once the transacion is committed or cancelled. + * + * Clients should NOT free the transaction handle. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] timeout How long to wait + * @param[in] db Database context + * @param[in] readonly Is transaction readonly + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_transaction_start_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + struct ctdb_db_context *db, + bool readonly); + +/** + * @brief Async computation end to start a transaction + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return a new transaction handle on success, NULL on failure + */ +struct ctdb_transaction_handle *ctdb_transaction_start_recv( + struct tevent_req *req, + int *perr); + +/** + * @brief Sync wrapper to start a transaction + * + * @see ctdb_transaction_start_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] timeout How long to wait + * @param[in] db Database context + * @param[in] readonly Is transaction readonly + * @param[out] result a new transaction handle + * @return 0 on success, errno on failure + */ +int ctdb_transaction_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + struct ctdb_db_context *db, bool readonly, + struct ctdb_transaction_handle **result); + +/** + * @brief Fetch a record under a transaction + * + * @see ctdb_transaction_start_send + * + * @param[in] h Transaction handle + * @param[in] key Record key + * @param[in] mem_ctx Talloc memory context + * @param[out] data Record data + * @return 0 on success, errno on failure + */ +int ctdb_transaction_fetch_record(struct ctdb_transaction_handle *h, + TDB_DATA key, + TALLOC_CTX *mem_ctx, TDB_DATA *data); + +/** + * @brief Store a record under a transaction + * + * @see ctdb_transaction_start_send + * + * @param[in] h Transaction handle + * @param[in] key Record key + * @param[in] data New record data + * @return 0 on success, errno on failure + */ +int ctdb_transaction_store_record(struct ctdb_transaction_handle *h, + TDB_DATA key, TDB_DATA data); + +/** + * @brief Delete a record under a transaction + * + * @see ctdb_transaction_start_send + * + * @param[in] h Transaction handle + * @param[in] key Record key + * @return 0 on success, errno on failure + */ +int ctdb_transaction_delete_record(struct ctdb_transaction_handle *h, + TDB_DATA key); + +/** + * @brief Async computation start to commit a transaction + * + * @see ctdb_transaction_start_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] timeout How long to wait + * @param[in] h Transaction handle + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_transaction_commit_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct timeval timeout, + struct ctdb_transaction_handle *h); + +/** + * @brief Async computation end to commit a transaction + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_transaction_commit_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper to commit a transaction + * + * @see ctdb_transaction_commit_send + * + * @param[in] h Transaction handle + * @return 0 on success, errno on failure + */ +int ctdb_transaction_commit(struct ctdb_transaction_handle *h); + +/** + * @brief Async computation start to cancel a transaction + * + * @see ctdb_transaction_start_send + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] timeout How long to wait + * @param[in] h Transaction handle + * @return a new tevent req on success, NULL on failure + */ +struct tevent_req *ctdb_transaction_cancel_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct timeval timeout, + struct ctdb_transaction_handle *h); + +/** + * @brief Async computation end to cancel a transaction + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool ctdb_transaction_cancel_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync wrapper to cancel a transaction + * + * @see ctdb_transaction_cancel_send + * + * @param[in] h Transaction handle + * @return 0 on success, errno on failure + */ +int ctdb_transaction_cancel(struct ctdb_transaction_handle *h); + +/** + * @brief Utility function to extract a list of node ids from nodemap + * + * @param[in] nodemap Node map + * @param[in] flags_mask Flags to match on + * @param[in] exclude_pnn Node id to exclude from the list + * @param[in] mem_ctx Talloc memory context + * @param[out] pnn_list List of node ids + * @return number of node ids on success, -1 on failure + */ +int list_of_nodes(struct ctdb_node_map *nodemap, + uint32_t flags_mask, uint32_t exclude_pnn, + TALLOC_CTX *mem_ctx, uint32_t **pnn_list); + +/** + * @brief Utility function to extract a list of node ids for active nodes + * + * @param[in] nodemap Node map + * @param[in] exclude_pnn Node id to exclude from the list + * @param[in] mem_ctx Talloc memory context + * @param[out] pnn_list List of node ids + * @return number of node ids on success, -1 on failure + */ +int list_of_active_nodes(struct ctdb_node_map *nodemap, uint32_t exclude_pnn, + TALLOC_CTX *mem_ctx, uint32_t **pnn_list); + +/** + * @brief Utility function to extract a list of node ids for connected nodes + * + * @param[in] nodemap Node map + * @param[in] exclude_pnn Node id to exclude from the list + * @param[in] mem_ctx Talloc memory context + * @param[out] pnn_list List of node ids + * @return number of node ids on success, -1 on failure + */ +int list_of_connected_nodes(struct ctdb_node_map *nodemap, + uint32_t exclude_pnn, + TALLOC_CTX *mem_ctx, uint32_t **pnn_list); + +/** + * @brief Construct a new server id + * + * @param[in] client Client connection context + * @param[in] task_id Task id + * @return a new server id + */ +struct ctdb_server_id ctdb_client_get_server_id( + struct ctdb_client_context *client, + uint32_t task_id); + +/** + * @brief Check if two server ids are the same + * + * @param[in] sid1 Server id 1 + * @param[in] sid2 Server id 2 + * @return true if the server ids are same, false otherwise + */ +bool ctdb_server_id_equal(struct ctdb_server_id *sid1, + struct ctdb_server_id *sid2); + +/** + * @brief Check if the process with server id exists + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client Client connection context + * @param[in] sid Server id + * @param[out] exists Boolean flag to indicate if the process exists + * @return 0 on success, errno on failure + */ +int ctdb_server_id_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_server_id *sid, bool *exists); + +#endif /* __CTDB_CLIENT_H__ */ diff --git a/ctdb/client/client_call.c b/ctdb/client/client_call.c new file mode 100644 index 0000000..088ba67 --- /dev/null +++ b/ctdb/client/client_call.c @@ -0,0 +1,184 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "common/reqid.h" +#include "common/srvid.h" +#include "common/comm.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" + +#include "client/client_private.h" +#include "client/client.h" + + +/* + * Handle REQ_CALL and REPLY_CALL + */ + +struct ctdb_client_call_state { + struct ctdb_client_context *client; + uint32_t reqid; + struct ctdb_reply_call *reply; + struct tevent_req *req; +}; + +static int ctdb_client_call_state_destructor( + struct ctdb_client_call_state *state); +static void ctdb_client_call_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_call_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_req_call *request) +{ + struct ctdb_req_header h; + struct tevent_req *req, *subreq; + struct ctdb_client_call_state *state; + uint32_t reqid; + uint8_t *buf; + size_t datalen, buflen; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_call_state); + if (req == NULL) { + return NULL; + } + + reqid = reqid_new(client->idr, state); + if (reqid == REQID_INVALID) { + talloc_free(req); + return NULL; + } + + state->client = client; + state->reqid = reqid; + state->req = req; + state->reply = talloc_zero(state, struct ctdb_reply_call); + if (tevent_req_nomem(state->reply, req)) { + return tevent_req_post(req, ev); + } + + talloc_set_destructor(state, ctdb_client_call_state_destructor); + + ctdb_req_header_fill(&h, 0, CTDB_REQ_CALL, CTDB_CURRENT_NODE, + client->pnn, reqid); + + datalen = ctdb_req_call_len(&h, request); + ret = ctdb_allocate_pkt(state, datalen, &buf, &buflen); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = ctdb_req_call_push(&h, request, buf, &buflen); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + subreq = comm_write_send(state, ev, client->comm, buf, buflen); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_client_call_done, req); + + return req; +} + +static int ctdb_client_call_state_destructor( + struct ctdb_client_call_state *state) +{ + reqid_remove(state->client->idr, state->reqid); + return 0; +} + +static void ctdb_client_call_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + bool status; + int ret; + + status = comm_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + /* wait for the reply */ +} + +void ctdb_client_reply_call(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid) +{ + struct ctdb_req_header h; + struct ctdb_client_call_state *state; + int ret; + + state = reqid_find(client->idr, reqid, struct ctdb_client_call_state); + if (state == NULL) { + return; + } + + if (reqid != state->reqid) { + return; + } + + ret = ctdb_reply_call_pull(buf, buflen, &h, state, state->reply); + if (ret != 0) { + tevent_req_error(state->req, ret); + return; + } + + tevent_req_done(state->req); +} + +bool ctdb_client_call_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, + struct ctdb_reply_call **reply, int *perr) +{ + struct ctdb_client_call_state *state = tevent_req_data( + req, struct ctdb_client_call_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + if (reply != NULL) { + *reply = talloc_steal(mem_ctx, state->reply); + } + + return true; +} diff --git a/ctdb/client/client_connect.c b/ctdb/client/client_connect.c new file mode 100644 index 0000000..a942871 --- /dev/null +++ b/ctdb/client/client_connect.c @@ -0,0 +1,532 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "common/reqid.h" +#include "common/srvid.h" +#include "common/comm.h" +#include "common/logging.h" + +#include "lib/util/tevent_unix.h" +#include "lib/util/debug.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" + +#include "client/client_private.h" +#include "client/client.h" +#include "client/client_sync.h" + +static void client_read_handler(uint8_t *buf, size_t buflen, + void *private_data); +static void client_dead_handler(void *private_data); + +struct ctdb_client_init_state { + struct ctdb_client_context *client; +}; + +static int ctdb_client_context_destructor(struct ctdb_client_context *client); +static void ctdb_client_init_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_init_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + const char *sockpath) +{ + struct tevent_req *req, *subreq; + struct ctdb_client_init_state *state; + struct ctdb_client_context *client; + struct ctdb_req_control request; + struct sockaddr_un addr; + size_t len; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_init_state); + if (req == NULL) { + return NULL; + } + + if (sockpath == NULL) { + D_ERR("socket path cannot be NULL\n"); + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + + client = talloc_zero(state, struct ctdb_client_context); + if (tevent_req_nomem(client, req)) { + return tevent_req_post(req, ev); + } + + ret = reqid_init(client, INT_MAX-200, &client->idr); + if (ret != 0) { + D_ERR("reqid_init() failed, ret=%d\n", ret); + talloc_free(client); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = srvid_init(client, &client->srv); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret)); + talloc_free(client); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = srvid_init(client, &client->tunnels); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret)); + talloc_free(client); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path)); + if (len != strlen(sockpath)) { + D_ERR("socket path too long, len=%zu\n", strlen(sockpath)); + talloc_free(client); + tevent_req_error(req, ENAMETOOLONG); + return tevent_req_post(req, ev); + } + + client->fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (client->fd == -1) { + ret = errno; + D_ERR("socket() failed, errno=%d\n", ret); + talloc_free(client); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = connect(client->fd, (struct sockaddr *)&addr, sizeof(addr)); + if (ret == -1) { + ret = errno; + DEBUG(DEBUG_ERR, ("connect() failed, errno=%d\n", ret)); + close(client->fd); + talloc_free(client); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = comm_setup(client, ev, client->fd, client_read_handler, client, + client_dead_handler, client, &client->comm); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("comm_setup() failed, ret=%d\n", ret)); + close(client->fd); + talloc_free(client); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + client->pnn = CTDB_UNKNOWN_PNN; + + talloc_set_destructor(client, ctdb_client_context_destructor); + + state->client = client; + + ctdb_req_control_get_pnn(&request); + subreq = ctdb_client_control_send(state, ev, client, + CTDB_CURRENT_NODE, + tevent_timeval_zero(), + &request); + if (tevent_req_nomem(subreq, req)) { + TALLOC_FREE(state->client); + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_client_init_done, req); + + return req; +} + +static int ctdb_client_context_destructor(struct ctdb_client_context *client) +{ + if (client->fd != -1) { + close(client->fd); + client->fd = -1; + } + return 0; +} + +static void ctdb_client_init_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_client_init_state *state = tevent_req_data( + req, struct ctdb_client_init_state); + struct ctdb_reply_control *reply; + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_get_pnn(reply, &state->client->pnn); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_client_init_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_client_context **result) +{ + struct ctdb_client_init_state *state = tevent_req_data( + req, struct ctdb_client_init_state); + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + *result = talloc_steal(mem_ctx, state->client); + return true; +} + + +int ctdb_client_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + const char *sockpath, struct ctdb_client_context **out) +{ + struct tevent_req *req; + int ret; + bool status; + + req = ctdb_client_init_send(mem_ctx, ev, sockpath); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_client_init_recv(req, &ret, mem_ctx, out); + TALLOC_FREE(req); + if (! status) { + return ret; + } + + return 0; +} + +static void client_read_handler(uint8_t *buf, size_t buflen, + void *private_data) +{ + struct ctdb_client_context *client = talloc_get_type_abort( + private_data, struct ctdb_client_context); + struct ctdb_req_header hdr; + size_t np; + int ret; + + ret = ctdb_req_header_pull(buf, buflen, &hdr, &np); + if (ret != 0) { + DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret)); + return; + } + + if (buflen != hdr.length) { + DEBUG(DEBUG_WARNING, ("packet size mismatch %zu != %d\n", + buflen, hdr.length)); + return; + } + + ret = ctdb_req_header_verify(&hdr, 0); + if (ret != 0) { + DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret)); + return; + } + + switch (hdr.operation) { + case CTDB_REPLY_CALL: + ctdb_client_reply_call(client, buf, buflen, hdr.reqid); + break; + + case CTDB_REQ_MESSAGE: + ctdb_client_req_message(client, buf, buflen, hdr.reqid); + break; + + case CTDB_REPLY_CONTROL: + ctdb_client_reply_control(client, buf, buflen, hdr.reqid); + break; + + case CTDB_REQ_TUNNEL: + ctdb_client_req_tunnel(client, buf, buflen, hdr.reqid); + break; + + default: + break; + } +} + +static void client_dead_handler(void *private_data) +{ + struct ctdb_client_context *client = talloc_get_type_abort( + private_data, struct ctdb_client_context); + ctdb_client_callback_func_t callback = client->callback; + void *callback_data = client->private_data; + + if (callback != NULL) { + callback(callback_data); + return; + } + + DEBUG(DEBUG_NOTICE, ("connection to daemon closed, exiting\n")); + exit(1); +} + +void ctdb_client_set_disconnect_callback(struct ctdb_client_context *client, + ctdb_client_callback_func_t callback, + void *private_data) +{ + client->callback = callback; + client->private_data = private_data; +} + +uint32_t ctdb_client_pnn(struct ctdb_client_context *client) +{ + return client->pnn; +} + +void ctdb_client_wait(struct tevent_context *ev, bool *done) +{ + while (! (*done)) { + tevent_loop_once(ev); + } +} + +static void ctdb_client_wait_timeout_handler(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval t, + void *private_data) +{ + bool *timed_out = (bool *)private_data; + + *timed_out = true; +} + +int ctdb_client_wait_func_timeout(struct tevent_context *ev, + bool (*done_func)(void *private_data), + void *private_data, + struct timeval timeout) +{ + TALLOC_CTX *mem_ctx; + struct tevent_timer *timer; + bool timed_out = false; + + mem_ctx = talloc_new(ev); + if (mem_ctx == NULL) { + return ENOMEM; + } + + timer = tevent_add_timer(ev, mem_ctx, timeout, + ctdb_client_wait_timeout_handler, + &timed_out); + if (timer == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + while (! (done_func(private_data)) && ! timed_out) { + tevent_loop_once(ev); + } + + talloc_free(mem_ctx); + + if (timed_out) { + return ETIMEDOUT; + } + + return 0; +} + +static bool client_wait_done(void *private_data) +{ + bool *done = (bool *)private_data; + + return *done; +} + +int ctdb_client_wait_timeout(struct tevent_context *ev, + bool *done, + struct timeval timeout) + +{ + int ret; + + ret = ctdb_client_wait_func_timeout(ev, + client_wait_done, + done, + timeout); + + return ret; +} + +struct ctdb_recovery_wait_state { + struct tevent_context *ev; + struct ctdb_client_context *client; +}; + +static void ctdb_recovery_wait_recmode(struct tevent_req *subreq); +static void ctdb_recovery_wait_retry(struct tevent_req *subreq); + +struct tevent_req *ctdb_recovery_wait_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client) +{ + struct tevent_req *req, *subreq; + struct ctdb_recovery_wait_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_recovery_wait_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->client = client; + + ctdb_req_control_get_recmode(&request); + subreq = ctdb_client_control_send(state, ev, client, client->pnn, + tevent_timeval_zero(), &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req); + + return req; +} + +static void ctdb_recovery_wait_recmode(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_recovery_wait_state *state = tevent_req_data( + req, struct ctdb_recovery_wait_state); + struct ctdb_reply_control *reply; + int recmode; + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_get_recmode(reply, &recmode); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + if (recmode == CTDB_RECOVERY_NORMAL) { + tevent_req_done(req); + return; + } + + subreq = tevent_wakeup_send(state, state->ev, + tevent_timeval_current_ofs(1, 0)); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_recovery_wait_retry, req); +} + +static void ctdb_recovery_wait_retry(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_recovery_wait_state *state = tevent_req_data( + req, struct ctdb_recovery_wait_state); + struct ctdb_req_control request; + bool status; + + status = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ENOMEM); + return; + } + + ctdb_req_control_get_recmode(&request); + subreq = ctdb_client_control_send(state, state->ev, state->client, + state->client->pnn, + tevent_timeval_zero(), &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req); +} + +bool ctdb_recovery_wait_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + +bool ctdb_recovery_wait(struct tevent_context *ev, + struct ctdb_client_context *client) +{ + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + bool status; + + mem_ctx = talloc_new(client); + if (mem_ctx == NULL) { + return false; + } + + req = ctdb_recovery_wait_send(mem_ctx, ev, client); + if (req == NULL) { + return false; + } + + tevent_req_poll(req, ev); + + status = ctdb_recovery_wait_recv(req, NULL); + + talloc_free(mem_ctx); + return status; +} diff --git a/ctdb/client/client_control.c b/ctdb/client/client_control.c new file mode 100644 index 0000000..ab0aac8 --- /dev/null +++ b/ctdb/client/client_control.c @@ -0,0 +1,439 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "common/reqid.h" +#include "common/srvid.h" +#include "common/comm.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" + +#include "client/client_private.h" +#include "client/client.h" + + +/* + * Handle REQ_CONTROL and REPLY_CONTROL + */ + +struct ctdb_client_control_state { + struct ctdb_client_context *client; + uint32_t opcode; + uint32_t flags; + uint32_t reqid; + struct ctdb_reply_control *reply; + struct tevent_req *req; +}; + +static int ctdb_client_control_state_destructor( + struct ctdb_client_control_state *state); +static void ctdb_client_control_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_control_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, + struct timeval timeout, + struct ctdb_req_control *request) +{ + struct ctdb_req_header h; + struct tevent_req *req, *subreq; + struct ctdb_client_control_state *state; + uint32_t reqid; + uint8_t *buf; + size_t datalen, buflen; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_control_state); + if (req == NULL) { + return NULL; + } + + reqid = reqid_new(client->idr, state); + if (reqid == REQID_INVALID) { + talloc_free(req); + return NULL; + } + + state->client = client; + state->flags = request->flags; + state->opcode = request->opcode; + state->reqid = reqid; + state->req = req; + state->reply = talloc_zero(state, struct ctdb_reply_control); + if (tevent_req_nomem(state->reply, req)) { + return tevent_req_post(req, ev); + } + state->reply->rdata.opcode = request->rdata.opcode; + + talloc_set_destructor(state, ctdb_client_control_state_destructor); + + ctdb_req_header_fill(&h, 0, CTDB_REQ_CONTROL, destnode, + client->pnn, reqid); + + datalen = ctdb_req_control_len(&h, request); + ret = ctdb_allocate_pkt(state, datalen, &buf, &buflen); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = ctdb_req_control_push(&h, request, buf, &buflen); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + if (!tevent_timeval_is_zero(&timeout)) { + if (!tevent_req_set_endtime(req, ev, timeout)) { + return tevent_req_post(req, ev); + } + } + + subreq = comm_write_send(state, ev, client->comm, buf, buflen); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_client_control_done, req); + + return req; +} + +static int ctdb_client_control_state_destructor( + struct ctdb_client_control_state *state) +{ + reqid_remove(state->client->idr, state->reqid); + return 0; +} + +static void ctdb_client_control_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_client_control_state *state = tevent_req_data( + req, struct ctdb_client_control_state); + bool status; + int ret; + + status = comm_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + /* Daemon will not reply, so we set status to 0 */ + if (state->flags & CTDB_CTRL_FLAG_NOREPLY) { + state->reply->status = 0; + tevent_req_done(req); + } + + /* wait for the reply or timeout */ +} + +void ctdb_client_reply_control(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid) +{ + struct ctdb_req_header h; + struct ctdb_client_control_state *state; + int ret; + + state = reqid_find(client->idr, reqid, + struct ctdb_client_control_state); + if (state == NULL) { + return; + } + + if (reqid != state->reqid) { + return; + } + + ret = ctdb_reply_control_pull(buf, buflen, state->opcode, &h, + state->reply, state->reply); + if (ret != 0) { + tevent_req_error(state->req, ret); + return; + } + + tevent_req_done(state->req); +} + +bool ctdb_client_control_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_reply_control **reply) +{ + struct ctdb_client_control_state *state = tevent_req_data( + req, struct ctdb_client_control_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + if (reply != NULL) { + *reply = talloc_steal(mem_ctx, state->reply); + } + + return true; +} + +/* + * Handle multiple nodes - there cannot be any return data + */ + +struct ctdb_client_control_multi_state { + uint32_t *pnn_list; + int count; + int done; + int err; + int *err_list; + struct ctdb_reply_control **reply; +}; + +struct control_index_state { + struct tevent_req *req; + int index; +}; + +static void ctdb_client_control_multi_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_control_multi_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct timeval timeout, + struct ctdb_req_control *request) +{ + struct tevent_req *req, *subreq; + struct ctdb_client_control_multi_state *state; + int i; + + if (pnn_list == NULL || count == 0) { + return NULL; + } + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_control_multi_state); + if (req == NULL) { + return NULL; + } + + state->pnn_list = pnn_list; + state->count = count; + state->done = 0; + state->err = 0; + state->err_list = talloc_zero_array(state, int, count); + if (tevent_req_nomem(state->err_list, req)) { + return tevent_req_post(req, ev); + } + state->reply = talloc_zero_array(state, struct ctdb_reply_control *, + count); + if (tevent_req_nomem(state->reply, req)) { + return tevent_req_post(req, ev); + } + + for (i=0; ireq = req; + substate->index = i; + + tevent_req_set_callback(subreq, ctdb_client_control_multi_done, + substate); + } + + return req; +} + +static void ctdb_client_control_multi_done(struct tevent_req *subreq) +{ + struct control_index_state *substate = tevent_req_callback_data( + subreq, struct control_index_state); + struct tevent_req *req = substate->req; + int idx = substate->index; + struct ctdb_client_control_multi_state *state = tevent_req_data( + req, struct ctdb_client_control_multi_state); + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state->reply, + &state->reply[idx]); + TALLOC_FREE(subreq); + if (! status) { + if (state->err == 0) { + state->err = ret; + state->err_list[idx] = state->err; + } + } else { + if (state->reply[idx]->status != 0) { + if (state->err == 0) { + state->err = state->reply[idx]->status; + state->err_list[idx] = state->err; + } + } + } + + state->done += 1; + + if (state->done == state->count) { + tevent_req_done(req); + } +} + +bool ctdb_client_control_multi_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, int **perr_list, + struct ctdb_reply_control ***preply) +{ + struct ctdb_client_control_multi_state *state = tevent_req_data( + req, struct ctdb_client_control_multi_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + if (perr_list != NULL) { + *perr_list = talloc_steal(mem_ctx, state->err_list); + } + return false; + } + + if (perr != NULL) { + *perr = state->err; + } + + if (perr_list != NULL) { + *perr_list = talloc_steal(mem_ctx, state->err_list); + } + + if (preply != NULL) { + *preply = talloc_steal(mem_ctx, state->reply); + } + + if (state->err != 0) { + return false; + } + + return true; +} + +int ctdb_client_control_multi_error(uint32_t *pnn_list, int count, + int *err_list, uint32_t *pnn) +{ + int ret = 0, i; + + for (i=0; i. +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "common/logging.h" + +#include "lib/util/debug.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" +#include "client/client_private.h" +#include "client/client.h" +#include "client/client_sync.h" + +int ctdb_ctrl_process_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + pid_t pid, int *status) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_process_exists(&request, pid); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control PROCESS_EXISTS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_process_exists(reply, status); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control PROCESS_EXISTS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_statistics(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_statistics **stats) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_statistics(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control STATISTICS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_statistics(reply, mem_ctx, stats); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control STATISTICS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_ping(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int *num_clients) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_ping(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control PING failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_ping(reply, num_clients); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control PING failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_getdbpath(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, + const char **db_path) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_getdbpath(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GETDBPATH failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_getdbpath(reply, mem_ctx, db_path); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GETDBPATH failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_getvnnmap(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_vnn_map **vnnmap) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_getvnnmap(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GETVNNMAP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_getvnnmap(reply, mem_ctx, vnnmap); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GETVNNMAP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_getdebug(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int *loglevel) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_debug(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DEBUG failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_debug(reply, loglevel); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DEBUG failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_setdebug(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int loglevel) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_debug(&request, loglevel); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_DEBUG failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_debug(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_DEBUG failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_dbmap(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_dbid_map **dbmap) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_dbmap(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DBMAP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_dbmap(reply, mem_ctx, dbmap); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DBMAP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_recmode(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int *recmode) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_recmode(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_RECMODE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_recmode(reply, recmode); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_RECMODE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_recmode(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int recmode) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_recmode(&request, recmode); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_RECMODE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_recmode(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_RECMODE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_statistics_reset(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_statistics_reset(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control STATISTICS_RESET failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_statistics_reset(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control STATISTICS_RESET failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_attach(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *db_name, uint32_t *db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_attach(&request, db_name); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_ATTACH failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_attach(reply, db_id); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_ATTACH failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_traverse_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_traverse_start *traverse) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_traverse_start(&request, traverse); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRAVERSE_START failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_traverse_start(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRAVERSE_START failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_register_srvid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t srvid) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_register_srvid(&request, srvid); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control REGISTER_SRVID failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_register_srvid(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control REGISTER_SRVID failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_deregister_srvid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t srvid) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_deregister_srvid(&request, srvid); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DEREGISTER_SRVID failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_deregister_srvid(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DEREGISTER_SRVID failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_dbname(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, const char **db_name) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_dbname(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DBNAME failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_dbname(reply, mem_ctx, db_name); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DBNAME failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_enable_seqnum(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_enable_seqnum(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control ENABLE_SEQNUM failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_enable_seqnum(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control ENABLE_SEQNUM failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_update_seqnum(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_update_seqnum(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control UPDATE_SEQNUM failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_update_seqnum(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control UPDATE_SEQNUM failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_dump_memory(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char **mem_str) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_dump_memory(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DUMP_MEMORY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_dump_memory(reply, mem_ctx, mem_str); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DUMP_MEMORY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_pid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + pid_t *pid) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_pid(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PID failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_pid(reply, pid); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PID failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_freeze(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int priority) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_freeze(&request, priority); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control FREEZE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_freeze(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control FREEZE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_pnn(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t *pnn) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_pnn(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PNN failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_pnn(reply, pnn); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PNN failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_shutdown(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_shutdown(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SHUTDOWN failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_shutdown(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SHUTDOWN failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_tcp_add(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_connection *conn) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_tcp_add(&request, conn); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TCP_ADD failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_tcp_add(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TCP_ADD failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_tcp_remove(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_connection *conn) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_tcp_remove(&request, conn); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TCP_REMOVE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_tcp_remove(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TCP_REMOVE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_tunable(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_tunable *tunable) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_tunable(&request, tunable); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_TUNABLE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_tunable(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_TUNABLE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_tunable(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *var, uint32_t *value) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_tunable(&request, var); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_TUNABLE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_tunable(reply, value); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_TUNABLE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_list_tunables(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_var_list **var_list) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_list_tunables(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control LIST_TUNABLES failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_list_tunables(reply, mem_ctx, var_list); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control LIST_TUNABLES failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_modify_flags(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t pnn, uint32_t old_flags, + uint32_t new_flags) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + struct ctdb_node_flag_change flag_change; + int ret; + + flag_change.pnn = pnn; + flag_change.old_flags = old_flags; + flag_change.new_flags = new_flags; + + ctdb_req_control_modify_flags(&request, &flag_change); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control MODIFY_FLAGS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_modify_flags(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control MODIFY_FLAGS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_all_tunables(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_tunable_list **tun_list) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_all_tunables(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_ALL_TUNABLES failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_all_tunables(reply, mem_ctx, tun_list); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_ALL_TUNABLES failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_tcp_tickle_list(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + ctdb_sock_addr *addr, + struct ctdb_tickle_list **tickles) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_tcp_tickle_list(&request, addr); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_TCP_TICKLE_LIST failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_tcp_tickle_list(reply, mem_ctx, tickles); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_TCP_TICKLE_LIST failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_tcp_tickle_list(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_tickle_list *tickles) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_tcp_tickle_list(&request, tickles); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_TCP_TICKLE_LIST failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_tcp_tickle_list(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_TCP_TICKLE_LIST failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_attach_persistent(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *db_name, uint32_t *db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_attach_persistent(&request, db_name); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_ATTACH_PERSISTENT failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_attach_persistent(reply, db_id); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_ATTACH_PERSISTENT failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_send_gratuitous_arp(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_addr_info *addr_info) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_send_gratuitous_arp(&request, addr_info); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SEND_GRATUITOUS_ARP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_send_gratuitous_arp(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SEND_GRATUITOUS_ARP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_wipe_database(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, uint32_t tid) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + struct ctdb_transdb transdb; + int ret; + + transdb.db_id = db_id; + transdb.tid = tid; + + ctdb_req_control_wipe_database(&request, &transdb); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control WIPE_DATABASE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_wipe_database(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control WIPE_DATABASE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_uptime(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_uptime **uptime) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_uptime(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control UPTIME failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_uptime(reply, mem_ctx, uptime); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control UPTIME failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_start_recovery(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_start_recovery(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control START_RECOVERY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_start_recovery(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control START_RECOVERY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_end_recovery(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_end_recovery(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control END_RECOVERY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_end_recovery(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control END_RECOVERY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_reload_nodes_file(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_reload_nodes_file(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control RELOAD_NODES_FILE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_reload_nodes_file(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control RELOAD_NODES_FILE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_add_public_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_addr_info *addr_info) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_add_public_ip(&request, addr_info); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control ADD_PUBLIC_IP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_add_public_ip(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control ADD_PUBLIC_IP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_del_public_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_addr_info *addr_info) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_del_public_ip(&request, addr_info); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DEL_PUBLIC_IP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_del_public_ip(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DEL_PUBLIC_IP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_capabilities(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t *caps) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_capabilities(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_CAPABILITIES failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_capabilities(reply, caps); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_CAPABILITIES failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_release_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_public_ip *pubip) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_release_ip(&request, pubip); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control RELEASE_IP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_release_ip(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control RELEASE_IP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_takeover_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_public_ip *pubip) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_takeover_ip(&request, pubip); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TAKEOVER_IP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_takeover_ip(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TAKEOVER_IP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_public_ips(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + bool available_only, + struct ctdb_public_ip_list **pubip_list) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_public_ips(&request, available_only); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PUBLIC_IPS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_public_ips(reply, mem_ctx, pubip_list); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PUBLIC_IPS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_nodemap(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_node_map **nodemap) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_nodemap(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_NODEMAP failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_nodemap(reply, mem_ctx, nodemap); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_NODEMAP failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_traverse_kill(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_traverse_start *traverse) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_traverse_kill(&request, traverse); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRAVERSE_KILL failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_traverse_kill(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRAVERSE_KILL failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_reclock_file(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char **reclock_file) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_reclock_file(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_RECLOCK_FILE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_reclock_file(reply, mem_ctx, reclock_file); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_RECLOCK_FILE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_stop_node(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_stop_node(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control STOP_NODE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_stop_node(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control STOP_NODE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_continue_node(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_continue_node(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control CONTINUE_NODE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_continue_node(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control CONTINUE_NODE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_lmasterrole(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t lmaster_role) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_lmasterrole(&request, lmaster_role); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_LMASTERROLE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_lmasterrole(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_LMASTERROLE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_recmasterrole(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t recmaster_role) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_recmasterrole(&request, recmaster_role); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_RECMASTERROLE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_recmasterrole(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_RECMASTERROLE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_ban_state(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_ban_state *ban_state) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_ban_state(&request, ban_state); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_BAN_STATE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_ban_state(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_BAN_STATE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_ban_state(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_ban_state **ban_state) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_ban_state(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_BAN_STATE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_ban_state(reply, mem_ctx, ban_state); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_BAN_STATE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_register_notify(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_notify_data *notify) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_register_notify(&request, notify); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control REGISTER_NOTIFY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_register_notify(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control REGISTER_NOTIFY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_deregister_notify(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t srvid) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_deregister_notify(&request, srvid); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DEREGISTER_NOTIFY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_deregister_notify(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DEREGISTER_NOTIFY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_trans3_commit(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_rec_buffer *recbuf) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_trans3_commit(&request, recbuf); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRANS3_COMMIT failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_trans3_commit(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRANS3_COMMIT failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_db_seqnum(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, uint64_t *seqnum) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_db_seqnum(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DB_SEQNUM failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_db_seqnum(reply, seqnum); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DB_SEQNUM failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_set_healthy(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_set_healthy(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_SET_HEALTHY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_set_healthy(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_SET_HEALTHY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_get_health(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, const char **reason) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_get_health(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_GET_HEALTH failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_get_health(reply, mem_ctx, reason); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_GET_HEALTH failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_public_ip_info(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + ctdb_sock_addr *addr, + struct ctdb_public_ip_info **ipinfo) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_public_ip_info(&request, addr); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PUBLIC_IP_INFO failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_public_ip_info(reply, mem_ctx, ipinfo); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_PUBLIC_IP_INFO failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_ifaces(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_iface_list **iface_list) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_ifaces(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_IFACES failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_ifaces(reply, mem_ctx, iface_list); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_IFACES failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_iface_link_state(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_iface *iface) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_iface_link_state(&request, iface); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_IFACE_LINK_STATE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_iface_link_state(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_IFACE_LINK_STATE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_tcp_add_delayed_update(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_connection *conn) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_tcp_add_delayed_update(&request, conn); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TCP_ADD_DELAYED_UPDATE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_tcp_add_delayed_update(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TCP_ADD_DELAYED_UPDATE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_stat_history(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_statistics_list **stats_list) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_stat_history(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_STAT_HISTORY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_stat_history(reply, mem_ctx, stats_list); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_STAT_HISTORY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_schedule_for_deletion(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_key_data *key) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_schedule_for_deletion(&request, key); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SCHEDULE_FOR_DELETION failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_schedule_for_deletion(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SCHEDULE_FOR_DELETION failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_db_readonly(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_db_readonly(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_DB_READONY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_db_readonly(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_DB_READONY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_traverse_start_ext(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_traverse_start_ext *traverse) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_traverse_start_ext(&request, traverse); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRAVERSE_START_EXT failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_traverse_start_ext(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TRAVERSE_START_EXT failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_db_statistics(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, + struct ctdb_db_statistics **dbstats) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_db_statistics(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DB_STATISTICS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_db_statistics(reply, mem_ctx, dbstats); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_DB_STATISTICS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_set_db_sticky(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_set_db_sticky(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_DB_STICKY failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_set_db_sticky(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control SET_DB_STICKY failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_reload_public_ips(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_reload_public_ips(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control RELOAD_PUBLIC_IPS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_reload_public_ips(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control RELOAD_PUBLIC_IPS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_ipreallocated(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_ipreallocated(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control IPREALLOCATED failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_ipreallocated(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control IPREALLOCATED failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_runstate(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + enum ctdb_runstate *runstate) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_runstate(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_RUNSTATE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_runstate(reply, runstate); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_RUNSTATE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_detach(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_detach(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_DETACH failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_detach(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_DETACH failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_get_nodes_file(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_node_map **nodemap) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_get_nodes_file(&request); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_NODES_FILE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_get_nodes_file(reply, mem_ctx, nodemap); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control GET_NODES_FILE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_freeze(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_freeze(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_FREEZE failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_freeze(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_FREEZE failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_thaw(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_thaw(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_THAW failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_thaw(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_THAW failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_transaction_start(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_transdb *transdb) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_transaction_start(&request, transdb); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_TRANSACTION_START failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_transaction_start(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_TRANSACTION_START failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_transaction_commit(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_transdb *transdb) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_transaction_commit(&request, transdb); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_TRANSACTION_COMMIT failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_transaction_commit(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_TRANSACTION_COMMIT failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_transaction_cancel(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_transaction_cancel(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_TRANSACTION_CANCEL failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_transaction_cancel(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_TRANSACTION_CANCEL failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_pull(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_pulldb_ext *pulldb, uint32_t *num_records) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_pull(&request, pulldb); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_PULL failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_pull(reply, num_records); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("Control DB_PULL failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_push_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_pulldb_ext *pulldb) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_push_start(&request, pulldb); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_PUSH_START failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_push_start(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_PUSH_START failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_push_confirm(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, uint32_t *num_records) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_push_confirm(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_PUSH_CONFIRM failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_push_confirm(reply, num_records); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_PUSH_CONFIRM failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_open_flags(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, int *tdb_flags) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_open_flags(&request, db_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_OPEN_FLAGS failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_open_flags(reply, tdb_flags); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_OPEN_FLAGS failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_db_attach_replicated(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *db_name, uint32_t *db_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_db_attach_replicated(&request, db_name); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_ATTACH_REPLICATED failed to node %u," + " ret=%d\n", destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_db_attach_replicated(reply, db_id); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control DB_ATTACH_REPLICATED failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_check_pid_srvid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_pid_srvid *pid_srvid, int *status) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_check_pid_srvid(&request, pid_srvid); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control CHECK_PID_SRVID failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_check_pid_srvid(reply, status); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control CHECK_PID_SRVID failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_tunnel_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t tunnel_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_tunnel_register(&request, tunnel_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TUNNEL_REGISTER failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_tunnel_register(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TUNNEL_REGISTER failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_tunnel_deregister(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t tunnel_id) +{ + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + int ret; + + ctdb_req_control_tunnel_deregister(&request, tunnel_id); + ret = ctdb_client_control(mem_ctx, ev, client, destnode, timeout, + &request, &reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TUNNEL_DEREGISTER failed to node %u, ret=%d\n", + destnode, ret)); + return ret; + } + + ret = ctdb_reply_control_tunnel_deregister(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Control TUNNEL_DEREGISTER failed, ret=%d\n", ret)); + return ret; + } + + return 0; +} + +int ctdb_ctrl_disable_node(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct timeval timeout) +{ + struct ctdb_req_control request = { + .opcode = 0, + }; + struct ctdb_reply_control *reply = NULL; + int ret; + + ctdb_req_control_disable_node(&request); + ret = ctdb_client_control(mem_ctx, + ev, + client, + destnode, + timeout, + &request, + &reply); + if (ret != 0) { + D_ERR("Control DISABLE_NODE failed to node %u, ret=%d\n", + destnode, + ret); + return ret; + } + + ret = ctdb_reply_control_disable_node(reply); + if (ret != 0) { + D_ERR("Control DISABLE_NODE failed, ret=%d\n", ret); + return ret; + } + + return 0; +} + +int ctdb_ctrl_enable_node(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct timeval timeout) +{ + struct ctdb_req_control request = { + .opcode = 0, + }; + struct ctdb_reply_control *reply = NULL; + int ret; + + ctdb_req_control_enable_node(&request); + ret = ctdb_client_control(mem_ctx, + ev, + client, + destnode, + timeout, + &request, + &reply); + if (ret != 0) { + D_ERR("Control ENABLE_NODE failed to node %u, ret=%d\n", + destnode, + ret); + return ret; + } + + ret = ctdb_reply_control_enable_node(reply); + if (ret != 0) { + D_ERR("Control ENABLE_NODE failed, ret=%d\n", ret); + return ret; + } + + return 0; +} diff --git a/ctdb/client/client_db.c b/ctdb/client/client_db.c new file mode 100644 index 0000000..0b06d6e --- /dev/null +++ b/ctdb/client/client_db.c @@ -0,0 +1,2791 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "common/logging.h" + +#include "lib/tdb_wrap/tdb_wrap.h" +#include "lib/util/tevent_unix.h" +#include "lib/util/dlinklist.h" +#include "lib/util/debug.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" +#include "client/client_private.h" +#include "client/client.h" + +struct tdb_context *client_db_tdb(struct ctdb_db_context *db) +{ + return db->ltdb->tdb; +} + +static struct ctdb_db_context *client_db_handle( + struct ctdb_client_context *client, + const char *db_name) +{ + struct ctdb_db_context *db; + + for (db = client->db; db != NULL; db = db->next) { + if (strcmp(db_name, db->db_name) == 0) { + return db; + } + } + + return NULL; +} + +static bool ctdb_db_persistent(struct ctdb_db_context *db) +{ + if (db->db_flags & CTDB_DB_FLAGS_PERSISTENT) { + return true; + } + return false; +} + +static bool ctdb_db_replicated(struct ctdb_db_context *db) +{ + if (db->db_flags & CTDB_DB_FLAGS_REPLICATED) { + return true; + } + return false; +} + +static bool ctdb_db_volatile(struct ctdb_db_context *db) +{ + if (db->db_flags & CTDB_DB_FLAGS_PERSISTENT || + db->db_flags & CTDB_DB_FLAGS_REPLICATED) { + return false; + } + return true; +} + +struct ctdb_set_db_flags_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct timeval timeout; + uint32_t db_id; + uint8_t db_flags; + bool readonly_done, sticky_done; + uint32_t *pnn_list; + int count; +}; + +static void ctdb_set_db_flags_nodemap_done(struct tevent_req *subreq); +static void ctdb_set_db_flags_readonly_done(struct tevent_req *subreq); +static void ctdb_set_db_flags_sticky_done(struct tevent_req *subreq); + +static struct tevent_req *ctdb_set_db_flags_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, struct timeval timeout, + uint32_t db_id, uint8_t db_flags) +{ + struct tevent_req *req, *subreq; + struct ctdb_set_db_flags_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_set_db_flags_state); + if (req == NULL) { + return NULL; + } + + if (! (db_flags & (CTDB_DB_FLAGS_READONLY | CTDB_DB_FLAGS_STICKY))) { + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + state->ev = ev; + state->client = client; + state->timeout = timeout; + state->db_id = db_id; + state->db_flags = db_flags; + + ctdb_req_control_get_nodemap(&request); + subreq = ctdb_client_control_send(state, ev, client, destnode, timeout, + &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_set_db_flags_nodemap_done, req); + + return req; +} + +static void ctdb_set_db_flags_nodemap_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_set_db_flags_state *state = tevent_req_data( + req, struct ctdb_set_db_flags_state); + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + struct ctdb_node_map *nodemap; + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("set_db_flags: 0x%08x GET_NODEMAP failed, ret=%d\n", + state->db_id, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_get_nodemap(reply, state, &nodemap); + talloc_free(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("set_db_flags: 0x%08x GET_NODEMAP parse failed, ret=%d\n", + state->db_id, ret)); + tevent_req_error(req, ret); + return; + } + + state->count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN, + state, &state->pnn_list); + talloc_free(nodemap); + if (state->count <= 0) { + DEBUG(DEBUG_ERR, + ("set_db_flags: 0x%08x no connected nodes, count=%d\n", + state->db_id, state->count)); + tevent_req_error(req, ENOMEM); + return; + } + + if (state->db_flags & CTDB_DB_FLAGS_READONLY) { + ctdb_req_control_set_db_readonly(&request, state->db_id); + subreq = ctdb_client_control_multi_send( + state, state->ev, state->client, + state->pnn_list, state->count, + state->timeout, &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, + ctdb_set_db_flags_readonly_done, req); + } else { + state->readonly_done = true; + } + + if (state->db_flags & CTDB_DB_FLAGS_STICKY) { + ctdb_req_control_set_db_sticky(&request, state->db_id); + subreq = ctdb_client_control_multi_send( + state, state->ev, state->client, + state->pnn_list, state->count, + state->timeout, &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_set_db_flags_sticky_done, + req); + } else { + state->sticky_done = true; + } +} + +static void ctdb_set_db_flags_readonly_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_set_db_flags_state *state = tevent_req_data( + req, struct ctdb_set_db_flags_state); + int ret; + bool status; + + status = ctdb_client_control_multi_recv(subreq, &ret, NULL, NULL, + NULL); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("set_db_flags: 0x%08x SET_DB_READONLY failed, ret=%d\n", + state->db_id, ret)); + tevent_req_error(req, ret); + return; + } + + state->readonly_done = true; + + if (state->readonly_done && state->sticky_done) { + tevent_req_done(req); + } +} + +static void ctdb_set_db_flags_sticky_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_set_db_flags_state *state = tevent_req_data( + req, struct ctdb_set_db_flags_state); + int ret; + bool status; + + status = ctdb_client_control_multi_recv(subreq, &ret, NULL, NULL, + NULL); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("set_db_flags: 0x%08x SET_DB_STICKY failed, ret=%d\n", + state->db_id, ret)); + tevent_req_error(req, ret); + return; + } + + state->sticky_done = true; + + if (state->readonly_done && state->sticky_done) { + tevent_req_done(req); + } +} + +static bool ctdb_set_db_flags_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + return true; +} + +struct ctdb_attach_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct timeval timeout; + uint32_t destnode; + uint8_t db_flags; + struct ctdb_db_context *db; +}; + +static void ctdb_attach_dbid_done(struct tevent_req *subreq); +static void ctdb_attach_dbpath_done(struct tevent_req *subreq); +static void ctdb_attach_health_done(struct tevent_req *subreq); +static void ctdb_attach_flags_done(struct tevent_req *subreq); +static void ctdb_attach_open_flags_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_attach_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + const char *db_name, uint8_t db_flags) +{ + struct tevent_req *req, *subreq; + struct ctdb_attach_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, struct ctdb_attach_state); + if (req == NULL) { + return NULL; + } + + state->db = client_db_handle(client, db_name); + if (state->db != NULL) { + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + state->ev = ev; + state->client = client; + state->timeout = timeout; + state->destnode = ctdb_client_pnn(client); + state->db_flags = db_flags; + + state->db = talloc_zero(client, struct ctdb_db_context); + if (tevent_req_nomem(state->db, req)) { + return tevent_req_post(req, ev); + } + + state->db->db_name = talloc_strdup(state->db, db_name); + if (tevent_req_nomem(state->db, req)) { + return tevent_req_post(req, ev); + } + + state->db->db_flags = db_flags; + + if (ctdb_db_persistent(state->db)) { + ctdb_req_control_db_attach_persistent(&request, + state->db->db_name); + } else if (ctdb_db_replicated(state->db)) { + ctdb_req_control_db_attach_replicated(&request, + state->db->db_name); + } else { + ctdb_req_control_db_attach(&request, state->db->db_name); + } + + subreq = ctdb_client_control_send(state, state->ev, state->client, + state->destnode, state->timeout, + &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_attach_dbid_done, req); + + return req; +} + +static void ctdb_attach_dbid_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_attach_state *state = tevent_req_data( + req, struct ctdb_attach_state); + struct ctdb_req_control request; + struct ctdb_reply_control *reply; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("attach: %s %s failed, ret=%d\n", + state->db->db_name, + (ctdb_db_persistent(state->db) + ? "DB_ATTACH_PERSISTENT" + : (ctdb_db_replicated(state->db) + ? "DB_ATTACH_REPLICATED" + : "DB_ATTACH")), + ret)); + tevent_req_error(req, ret); + return; + } + + if (ctdb_db_persistent(state->db)) { + ret = ctdb_reply_control_db_attach_persistent( + reply, &state->db->db_id); + } else if (ctdb_db_replicated(state->db)) { + ret = ctdb_reply_control_db_attach_replicated( + reply, &state->db->db_id); + } else { + ret = ctdb_reply_control_db_attach(reply, &state->db->db_id); + } + talloc_free(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("attach: %s failed to get db_id, ret=%d\n", + state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ctdb_req_control_getdbpath(&request, state->db->db_id); + subreq = ctdb_client_control_send(state, state->ev, state->client, + state->destnode, state->timeout, + &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_attach_dbpath_done, req); +} + +static void ctdb_attach_dbpath_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_attach_state *state = tevent_req_data( + req, struct ctdb_attach_state); + struct ctdb_reply_control *reply; + struct ctdb_req_control request; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("attach: %s GETDBPATH failed, ret=%d\n", + state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_getdbpath(reply, state->db, + &state->db->db_path); + talloc_free(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("attach: %s GETDBPATH parse failed, ret=%d\n", + state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ctdb_req_control_db_get_health(&request, state->db->db_id); + subreq = ctdb_client_control_send(state, state->ev, state->client, + state->destnode, state->timeout, + &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_attach_health_done, req); +} + +static void ctdb_attach_health_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_attach_state *state = tevent_req_data( + req, struct ctdb_attach_state); + struct ctdb_reply_control *reply; + const char *reason; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("attach: %s DB_GET_HEALTH failed, ret=%d\n", + state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_db_get_health(reply, state, &reason); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("attach: %s DB_GET_HEALTH parse failed, ret=%d\n", + state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + if (reason != NULL) { + /* Database unhealthy, avoid attach */ + DEBUG(DEBUG_ERR, ("attach: %s database unhealthy (%s)\n", + state->db->db_name, reason)); + tevent_req_error(req, EIO); + return; + } + + subreq = ctdb_set_db_flags_send(state, state->ev, state->client, + state->destnode, state->timeout, + state->db->db_id, state->db_flags); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_attach_flags_done, req); +} + +static void ctdb_attach_flags_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_attach_state *state = tevent_req_data( + req, struct ctdb_attach_state); + struct ctdb_req_control request; + bool status; + int ret; + + status = ctdb_set_db_flags_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("attach: %s set db flags 0x%08x failed\n", + state->db->db_name, state->db_flags)); + tevent_req_error(req, ret); + return; + } + + ctdb_req_control_db_open_flags(&request, state->db->db_id); + subreq = ctdb_client_control_send(state, state->ev, state->client, + state->destnode, state->timeout, + &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_attach_open_flags_done, req); +} + +static void ctdb_attach_open_flags_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_attach_state *state = tevent_req_data( + req, struct ctdb_attach_state); + struct ctdb_reply_control *reply; + bool status; + int ret, tdb_flags; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("attach: %s DB_OPEN_FLAGS failed, ret=%d\n", + state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_db_open_flags(reply, &tdb_flags); + talloc_free(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("attach: %s DB_OPEN_FLAGS parse failed," + " ret=%d\n", state->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + state->db->ltdb = tdb_wrap_open(state->db, state->db->db_path, 0, + tdb_flags, O_RDWR, 0); + if (tevent_req_nomem(state->db->ltdb, req)) { + DEBUG(DEBUG_ERR, ("attach: %s tdb_wrap_open failed\n", + state->db->db_name)); + return; + } + DLIST_ADD(state->client->db, state->db); + + tevent_req_done(req); +} + +bool ctdb_attach_recv(struct tevent_req *req, int *perr, + struct ctdb_db_context **out) +{ + struct ctdb_attach_state *state = tevent_req_data( + req, struct ctdb_attach_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + if (out != NULL) { + *out = state->db; + } + return true; +} + +int ctdb_attach(struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + const char *db_name, uint8_t db_flags, + struct ctdb_db_context **out) +{ + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + bool status; + int ret; + + mem_ctx = talloc_new(client); + if (mem_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_attach_send(mem_ctx, ev, client, timeout, + db_name, db_flags); + if (req == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_attach_recv(req, &ret, out); + if (! status) { + talloc_free(mem_ctx); + return ret; + } + + /* + ctdb_set_call(db, CTDB_NULL_FUNC, ctdb_null_func); + ctdb_set_call(db, CTDB_FETCH_FUNC, ctdb_fetch_func); + ctdb_set_call(db, CTDB_FETCH_WITH_HEADER_FUNC, ctdb_fetch_with_header_func); + */ + + talloc_free(mem_ctx); + return 0; +} + +struct ctdb_detach_state { + struct ctdb_client_context *client; + struct tevent_context *ev; + struct timeval timeout; + uint32_t db_id; + const char *db_name; +}; + +static void ctdb_detach_dbname_done(struct tevent_req *subreq); +static void ctdb_detach_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_detach_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, uint32_t db_id) +{ + struct tevent_req *req, *subreq; + struct ctdb_detach_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, struct ctdb_detach_state); + if (req == NULL) { + return NULL; + } + + state->client = client; + state->ev = ev; + state->timeout = timeout; + state->db_id = db_id; + + ctdb_req_control_get_dbname(&request, db_id); + subreq = ctdb_client_control_send(state, ev, client, + ctdb_client_pnn(client), timeout, + &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_detach_dbname_done, req); + + return req; +} + +static void ctdb_detach_dbname_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_detach_state *state = tevent_req_data( + req, struct ctdb_detach_state); + struct ctdb_reply_control *reply; + struct ctdb_req_control request; + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("detach: 0x%x GET_DBNAME failed, ret=%d\n", + state->db_id, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_get_dbname(reply, state, &state->db_name); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("detach: 0x%x GET_DBNAME failed, ret=%d\n", + state->db_id, ret)); + tevent_req_error(req, ret); + return; + } + + ctdb_req_control_db_detach(&request, state->db_id); + subreq = ctdb_client_control_send(state, state->ev, state->client, + ctdb_client_pnn(state->client), + state->timeout, &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_detach_done, req); + +} + +static void ctdb_detach_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_detach_state *state = tevent_req_data( + req, struct ctdb_detach_state); + struct ctdb_reply_control *reply; + struct ctdb_db_context *db; + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("detach: %s DB_DETACH failed, ret=%d\n", + state->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_db_detach(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("detach: %s DB_DETACH failed, ret=%d\n", + state->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + db = client_db_handle(state->client, state->db_name); + if (db != NULL) { + DLIST_REMOVE(state->client->db, db); + TALLOC_FREE(db); + } + + tevent_req_done(req); +} + +bool ctdb_detach_recv(struct tevent_req *req, int *perr) +{ + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + return true; +} + +int ctdb_detach(struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, uint32_t db_id) +{ + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + int ret; + bool status; + + mem_ctx = talloc_new(client); + if (mem_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_detach_send(mem_ctx, ev, client, timeout, db_id); + if (req == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_detach_recv(req, &ret); + if (! status) { + talloc_free(mem_ctx); + return ret; + } + + talloc_free(mem_ctx); + return 0; +} + +uint32_t ctdb_db_id(struct ctdb_db_context *db) +{ + return db->db_id; +} + +struct ctdb_db_traverse_local_state { + ctdb_rec_parser_func_t parser; + void *private_data; + bool extract_header; + int error; +}; + +static int ctdb_db_traverse_local_handler(struct tdb_context *tdb, + TDB_DATA key, TDB_DATA data, + void *private_data) +{ + struct ctdb_db_traverse_local_state *state = + (struct ctdb_db_traverse_local_state *)private_data; + int ret; + + if (state->extract_header) { + struct ctdb_ltdb_header header; + + ret = ctdb_ltdb_header_extract(&data, &header); + if (ret != 0) { + state->error = ret; + return 1; + } + + ret = state->parser(0, &header, key, data, state->private_data); + } else { + ret = state->parser(0, NULL, key, data, state->private_data); + } + + if (ret != 0) { + state->error = ret; + return 1; + } + + return 0; +} + +int ctdb_db_traverse_local(struct ctdb_db_context *db, bool readonly, + bool extract_header, + ctdb_rec_parser_func_t parser, void *private_data) +{ + struct ctdb_db_traverse_local_state state; + int ret; + + state.parser = parser; + state.private_data = private_data; + state.extract_header = extract_header; + state.error = 0; + + if (readonly) { + ret = tdb_traverse_read(client_db_tdb(db), + ctdb_db_traverse_local_handler, + &state); + } else { + ret = tdb_traverse(client_db_tdb(db), + ctdb_db_traverse_local_handler, &state); + } + + if (ret == -1) { + return EIO; + } + + return state.error; +} + +struct ctdb_db_traverse_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct ctdb_db_context *db; + uint32_t destnode; + uint64_t srvid; + struct timeval timeout; + ctdb_rec_parser_func_t parser; + void *private_data; + int result; +}; + +static void ctdb_db_traverse_handler_set(struct tevent_req *subreq); +static void ctdb_db_traverse_started(struct tevent_req *subreq); +static void ctdb_db_traverse_handler(uint64_t srvid, TDB_DATA data, + void *private_data); +static void ctdb_db_traverse_remove_handler(struct tevent_req *req); +static void ctdb_db_traverse_handler_removed(struct tevent_req *subreq); + +struct tevent_req *ctdb_db_traverse_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + uint32_t destnode, + struct timeval timeout, + ctdb_rec_parser_func_t parser, + void *private_data) +{ + struct tevent_req *req, *subreq; + struct ctdb_db_traverse_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_db_traverse_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->client = client; + state->db = db; + state->destnode = destnode; + state->srvid = CTDB_SRVID_CLIENT_RANGE | getpid(); + state->timeout = timeout; + state->parser = parser; + state->private_data = private_data; + + subreq = ctdb_client_set_message_handler_send(state, ev, client, + state->srvid, + ctdb_db_traverse_handler, + req); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_db_traverse_handler_set, req); + + return req; +} + +static void ctdb_db_traverse_handler_set(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_db_traverse_state *state = tevent_req_data( + req, struct ctdb_db_traverse_state); + struct ctdb_traverse_start_ext traverse; + struct ctdb_req_control request; + int ret = 0; + bool status; + + status = ctdb_client_set_message_handler_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + traverse = (struct ctdb_traverse_start_ext) { + .db_id = ctdb_db_id(state->db), + .reqid = 0, + .srvid = state->srvid, + .withemptyrecords = false, + }; + + ctdb_req_control_traverse_start_ext(&request, &traverse); + subreq = ctdb_client_control_send(state, state->ev, state->client, + state->destnode, state->timeout, + &request); + if (subreq == NULL) { + state->result = ENOMEM; + ctdb_db_traverse_remove_handler(req); + return; + } + tevent_req_set_callback(subreq, ctdb_db_traverse_started, req); +} + +static void ctdb_db_traverse_started(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_db_traverse_state *state = tevent_req_data( + req, struct ctdb_db_traverse_state); + struct ctdb_reply_control *reply; + int ret = 0; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("traverse: control failed, ret=%d\n", ret)); + state->result = ret; + ctdb_db_traverse_remove_handler(req); + return; + } + + ret = ctdb_reply_control_traverse_start_ext(reply); + talloc_free(reply); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("traverse: control reply failed, ret=%d\n", + ret)); + state->result = ret; + ctdb_db_traverse_remove_handler(req); + return; + } +} + +static void ctdb_db_traverse_handler(uint64_t srvid, TDB_DATA data, + void *private_data) +{ + struct tevent_req *req = talloc_get_type_abort( + private_data, struct tevent_req); + struct ctdb_db_traverse_state *state = tevent_req_data( + req, struct ctdb_db_traverse_state); + struct ctdb_rec_data *rec; + struct ctdb_ltdb_header header; + size_t np; + int ret; + + ret = ctdb_rec_data_pull(data.dptr, data.dsize, state, &rec, &np); + if (ret != 0) { + return; + } + + if (rec->key.dsize == 0 && rec->data.dsize == 0) { + talloc_free(rec); + ctdb_db_traverse_remove_handler(req); + return; + } + + ret = ctdb_ltdb_header_extract(&rec->data, &header); + if (ret != 0) { + talloc_free(rec); + return; + } + + if (rec->data.dsize == 0) { + talloc_free(rec); + return; + } + + ret = state->parser(rec->reqid, &header, rec->key, rec->data, + state->private_data); + talloc_free(rec); + if (ret != 0) { + state->result = ret; + ctdb_db_traverse_remove_handler(req); + } +} + +static void ctdb_db_traverse_remove_handler(struct tevent_req *req) +{ + struct ctdb_db_traverse_state *state = tevent_req_data( + req, struct ctdb_db_traverse_state); + struct tevent_req *subreq; + + subreq = ctdb_client_remove_message_handler_send(state, state->ev, + state->client, + state->srvid, req); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_db_traverse_handler_removed, req); +} + +static void ctdb_db_traverse_handler_removed(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_db_traverse_state *state = tevent_req_data( + req, struct ctdb_db_traverse_state); + int ret; + bool status; + + status = ctdb_client_remove_message_handler_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + if (state->result != 0) { + tevent_req_error(req, state->result); + return; + } + + tevent_req_done(req); +} + +bool ctdb_db_traverse_recv(struct tevent_req *req, int *perr) +{ + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + return true; +} + +int ctdb_db_traverse(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + uint32_t destnode, struct timeval timeout, + ctdb_rec_parser_func_t parser, void *private_data) +{ + struct tevent_req *req; + int ret = 0; + bool status; + + req = ctdb_db_traverse_send(mem_ctx, ev, client, db, destnode, + timeout, parser, private_data); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_db_traverse_recv(req, &ret); + if (! status) { + return ret; + } + + return 0; +} + +int ctdb_ltdb_fetch(struct ctdb_db_context *db, TDB_DATA key, + struct ctdb_ltdb_header *header, + TALLOC_CTX *mem_ctx, TDB_DATA *data) +{ + TDB_DATA rec; + size_t np; + int ret; + + rec = tdb_fetch(client_db_tdb(db), key); + if (rec.dsize < sizeof(struct ctdb_ltdb_header)) { + /* No record present */ + if (rec.dptr != NULL) { + free(rec.dptr); + } + + if (tdb_error(client_db_tdb(db)) != TDB_ERR_NOEXIST) { + return EIO; + } + + *header = (struct ctdb_ltdb_header) { + .dmaster = CTDB_UNKNOWN_PNN, + }; + + if (data != NULL) { + *data = tdb_null; + } + return 0; + } + + ret = ctdb_ltdb_header_pull(rec.dptr, rec.dsize, header, &np); + if (ret != 0) { + return ret; + } + + ret = 0; + if (data != NULL) { + data->dsize = rec.dsize - np; + data->dptr = talloc_memdup(mem_ctx, rec.dptr + np, + data->dsize); + if (data->dptr == NULL) { + ret = ENOMEM; + } + } + + free(rec.dptr); + return ret; +} + +/* + * Fetch a record from volatile database + * + * Steps: + * 1. Get a lock on the hash chain + * 2. If the record does not exist, migrate the record + * 3. If readonly=true and delegations do not exist, migrate the record. + * 4. If readonly=false and delegations exist, migrate the record. + * 5. If the local node is not dmaster, migrate the record. + * 6. Return record + */ + +struct ctdb_fetch_lock_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct ctdb_record_handle *h; + bool readonly; + uint32_t pnn; +}; + +static int ctdb_fetch_lock_check(struct tevent_req *req); +static void ctdb_fetch_lock_migrate(struct tevent_req *req); +static void ctdb_fetch_lock_migrate_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_fetch_lock_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + TDB_DATA key, bool readonly) +{ + struct ctdb_fetch_lock_state *state; + struct tevent_req *req; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct ctdb_fetch_lock_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->client = client; + + state->h = talloc_zero(db, struct ctdb_record_handle); + if (tevent_req_nomem(state->h, req)) { + return tevent_req_post(req, ev); + } + state->h->ev = ev; + state->h->client = client; + state->h->db = db; + state->h->key.dptr = talloc_memdup(state->h, key.dptr, key.dsize); + if (tevent_req_nomem(state->h->key.dptr, req)) { + return tevent_req_post(req, ev); + } + state->h->key.dsize = key.dsize; + state->h->readonly = false; + + state->readonly = readonly; + state->pnn = ctdb_client_pnn(client); + + /* Check that database is not persistent */ + if (! ctdb_db_volatile(db)) { + DEBUG(DEBUG_ERR, ("fetch_lock: %s database not volatile\n", + db->db_name)); + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + + ret = ctdb_fetch_lock_check(req); + if (ret == 0) { + tevent_req_done(req); + return tevent_req_post(req, ev); + } + if (ret != EAGAIN) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + return req; +} + +static int ctdb_fetch_lock_check(struct tevent_req *req) +{ + struct ctdb_fetch_lock_state *state = tevent_req_data( + req, struct ctdb_fetch_lock_state); + struct ctdb_record_handle *h = state->h; + struct ctdb_ltdb_header header; + TDB_DATA data = tdb_null; + size_t np; + int ret, err = 0; + bool do_migrate = false; + + ret = tdb_chainlock(client_db_tdb(h->db), h->key); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("fetch_lock: %s tdb_chainlock failed, %s\n", + h->db->db_name, tdb_errorstr(client_db_tdb(h->db)))); + err = EIO; + goto failed; + } + + data = tdb_fetch(client_db_tdb(h->db), h->key); + if (data.dptr == NULL) { + if (tdb_error(client_db_tdb(h->db)) == TDB_ERR_NOEXIST) { + goto migrate; + } else { + err = EIO; + goto failed; + } + } + + /* Got the record */ + ret = ctdb_ltdb_header_pull(data.dptr, data.dsize, &header, &np); + if (ret != 0) { + err = ret; + goto failed; + } + + if (! state->readonly) { + /* Read/write access */ + if (header.dmaster == state->pnn && + header.flags & CTDB_REC_RO_HAVE_DELEGATIONS) { + goto migrate; + } + + if (header.dmaster != state->pnn) { + goto migrate; + } + } else { + /* Readonly access */ + if (header.dmaster != state->pnn && + ! (header.flags & (CTDB_REC_RO_HAVE_READONLY | + CTDB_REC_RO_HAVE_DELEGATIONS))) { + goto migrate; + } + } + + /* We are the dmaster or readonly delegation */ + h->header = header; + h->data = data; + if (header.flags & (CTDB_REC_RO_HAVE_READONLY | + CTDB_REC_RO_HAVE_DELEGATIONS)) { + h->readonly = true; + } + return 0; + +migrate: + do_migrate = true; + err = EAGAIN; + +failed: + if (data.dptr != NULL) { + free(data.dptr); + } + ret = tdb_chainunlock(client_db_tdb(h->db), h->key); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("fetch_lock: %s tdb_chainunlock failed, %s\n", + h->db->db_name, tdb_errorstr(client_db_tdb(h->db)))); + return EIO; + } + + if (do_migrate) { + ctdb_fetch_lock_migrate(req); + } + return err; +} + +static void ctdb_fetch_lock_migrate(struct tevent_req *req) +{ + struct ctdb_fetch_lock_state *state = tevent_req_data( + req, struct ctdb_fetch_lock_state); + struct ctdb_req_call request; + struct tevent_req *subreq; + + ZERO_STRUCT(request); + request.flags = CTDB_IMMEDIATE_MIGRATION; + if (state->readonly) { + request.flags |= CTDB_WANT_READONLY; + } + request.db_id = state->h->db->db_id; + request.callid = CTDB_NULL_FUNC; + request.key = state->h->key; + request.calldata = tdb_null; + + subreq = ctdb_client_call_send(state, state->ev, state->client, + &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + + tevent_req_set_callback(subreq, ctdb_fetch_lock_migrate_done, req); +} + +static void ctdb_fetch_lock_migrate_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_fetch_lock_state *state = tevent_req_data( + req, struct ctdb_fetch_lock_state); + struct ctdb_reply_call *reply; + int ret; + bool status; + + status = ctdb_client_call_recv(subreq, state, &reply, &ret); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, ("fetch_lock: %s CALL failed, ret=%d\n", + state->h->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + if (reply->status != 0) { + tevent_req_error(req, EIO); + return; + } + talloc_free(reply); + + ret = ctdb_fetch_lock_check(req); + if (ret != 0) { + if (ret != EAGAIN) { + tevent_req_error(req, ret); + } + return; + } + + tevent_req_done(req); +} + +static int ctdb_record_handle_destructor(struct ctdb_record_handle *h) +{ + int ret; + + ret = tdb_chainunlock(client_db_tdb(h->db), h->key); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("fetch_lock: %s tdb_chainunlock failed, %s\n", + h->db->db_name, tdb_errorstr(client_db_tdb(h->db)))); + } + free(h->data.dptr); + return 0; +} + +struct ctdb_record_handle *ctdb_fetch_lock_recv(struct tevent_req *req, + struct ctdb_ltdb_header *header, + TALLOC_CTX *mem_ctx, + TDB_DATA *data, int *perr) +{ + struct ctdb_fetch_lock_state *state = tevent_req_data( + req, struct ctdb_fetch_lock_state); + struct ctdb_record_handle *h = state->h; + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + TALLOC_FREE(state->h); + *perr = err; + } + return NULL; + } + + if (header != NULL) { + *header = h->header; + } + if (data != NULL) { + size_t offset; + + offset = ctdb_ltdb_header_len(&h->header); + + data->dsize = h->data.dsize - offset; + if (data->dsize == 0) { + data->dptr = NULL; + } else { + data->dptr = talloc_memdup(mem_ctx, + h->data.dptr + offset, + data->dsize); + if (data->dptr == NULL) { + TALLOC_FREE(state->h); + if (perr != NULL) { + *perr = ENOMEM; + } + return NULL; + } + } + } + + talloc_set_destructor(h, ctdb_record_handle_destructor); + return h; +} + +int ctdb_fetch_lock(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, TDB_DATA key, bool readonly, + struct ctdb_record_handle **out, + struct ctdb_ltdb_header *header, TDB_DATA *data) +{ + struct tevent_req *req; + struct ctdb_record_handle *h; + int ret = 0; + + req = ctdb_fetch_lock_send(mem_ctx, ev, client, db, key, readonly); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + h = ctdb_fetch_lock_recv(req, header, mem_ctx, data, &ret); + if (h == NULL) { + return ret; + } + + *out = h; + return 0; +} + +int ctdb_store_record(struct ctdb_record_handle *h, TDB_DATA data) +{ + uint8_t header[sizeof(struct ctdb_ltdb_header)]; + TDB_DATA rec[2]; + size_t np; + int ret; + + /* Cannot modify the record if it was obtained as a readonly copy */ + if (h->readonly) { + return EINVAL; + } + + /* Check if the new data is same */ + if (h->data.dsize == data.dsize && + memcmp(h->data.dptr, data.dptr, data.dsize) == 0) { + /* No need to do anything */ + return 0; + } + + ctdb_ltdb_header_push(&h->header, header, &np); + + rec[0].dsize = np; + rec[0].dptr = header; + + rec[1].dsize = data.dsize; + rec[1].dptr = data.dptr; + + ret = tdb_storev(client_db_tdb(h->db), h->key, rec, 2, TDB_REPLACE); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("store_record: %s tdb_storev failed, %s\n", + h->db->db_name, tdb_errorstr(client_db_tdb(h->db)))); + return EIO; + } + + return 0; +} + +struct ctdb_delete_record_state { + struct ctdb_record_handle *h; +}; + +static void ctdb_delete_record_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_delete_record_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_record_handle *h) +{ + struct tevent_req *req, *subreq; + struct ctdb_delete_record_state *state; + struct ctdb_key_data key; + struct ctdb_req_control request; + uint8_t header[sizeof(struct ctdb_ltdb_header)]; + TDB_DATA rec; + size_t np; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_delete_record_state); + if (req == NULL) { + return NULL; + } + + state->h = h; + + /* Cannot delete the record if it was obtained as a readonly copy */ + if (h->readonly) { + DEBUG(DEBUG_ERR, ("fetch_lock delete: %s readonly record\n", + h->db->db_name)); + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + + ctdb_ltdb_header_push(&h->header, header, &np); + + rec.dsize = np; + rec.dptr = header; + + ret = tdb_store(client_db_tdb(h->db), h->key, rec, TDB_REPLACE); + if (ret != 0) { + D_ERR("fetch_lock delete: %s tdb_store failed, %s\n", + h->db->db_name, + tdb_errorstr(client_db_tdb(h->db))); + tevent_req_error(req, EIO); + return tevent_req_post(req, ev); + } + + key.db_id = h->db->db_id; + key.header = h->header; + key.key = h->key; + + ctdb_req_control_schedule_for_deletion(&request, &key); + subreq = ctdb_client_control_send(state, ev, h->client, + ctdb_client_pnn(h->client), + tevent_timeval_zero(), + &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_delete_record_done, req); + + return req; +} + +static void ctdb_delete_record_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_delete_record_state *state = tevent_req_data( + req, struct ctdb_delete_record_state); + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, NULL, NULL); + TALLOC_FREE(subreq); + if (! status) { + D_ERR("delete_record: %s SCHEDULE_FOR_DELETION failed, ret=%d\n", + state->h->db->db_name, + ret); + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_delete_record_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + + +int ctdb_delete_record(struct ctdb_record_handle *h) +{ + struct tevent_context *ev = h->ev; + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + int ret; + bool status; + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_delete_record_send(mem_ctx, ev, h); + if (req == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_delete_record_recv(req, &ret); + talloc_free(mem_ctx); + if (! status) { + return ret; + } + + return 0; +} + +/* + * Global lock functions + */ + +struct ctdb_g_lock_lock_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct ctdb_db_context *db; + TDB_DATA key; + struct ctdb_server_id my_sid; + enum ctdb_g_lock_type lock_type; + struct ctdb_record_handle *h; + /* state for verification of active locks */ + struct ctdb_g_lock_list *lock_list; + unsigned int current; +}; + +static void ctdb_g_lock_lock_fetched(struct tevent_req *subreq); +static void ctdb_g_lock_lock_process_locks(struct tevent_req *req); +static void ctdb_g_lock_lock_checked(struct tevent_req *subreq); +static int ctdb_g_lock_lock_update(struct tevent_req *req); +static void ctdb_g_lock_lock_retry(struct tevent_req *subreq); + +static bool ctdb_g_lock_conflicts(enum ctdb_g_lock_type l1, + enum ctdb_g_lock_type l2) +{ + if ((l1 == CTDB_G_LOCK_READ) && (l2 == CTDB_G_LOCK_READ)) { + return false; + } + return true; +} + +struct tevent_req *ctdb_g_lock_lock_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + const char *keyname, + struct ctdb_server_id *sid, + bool readonly) +{ + struct tevent_req *req, *subreq; + struct ctdb_g_lock_lock_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_g_lock_lock_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->client = client; + state->db = db; + state->key.dptr = discard_const(keyname); + state->key.dsize = strlen(keyname) + 1; + state->my_sid = *sid; + state->lock_type = (readonly ? CTDB_G_LOCK_READ : CTDB_G_LOCK_WRITE); + + subreq = ctdb_fetch_lock_send(state, ev, client, db, state->key, + false); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_g_lock_lock_fetched, req); + + return req; +} + +static void ctdb_g_lock_lock_fetched(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_g_lock_lock_state *state = tevent_req_data( + req, struct ctdb_g_lock_lock_state); + TDB_DATA data; + size_t np; + int ret = 0; + + state->h = ctdb_fetch_lock_recv(subreq, NULL, state, &data, &ret); + TALLOC_FREE(subreq); + if (state->h == NULL) { + DEBUG(DEBUG_ERR, ("g_lock_lock: %s fetch lock failed\n", + (char *)state->key.dptr)); + tevent_req_error(req, ret); + return; + } + + if (state->lock_list != NULL) { + TALLOC_FREE(state->lock_list); + state->current = 0; + } + + ret = ctdb_g_lock_list_pull(data.dptr, data.dsize, state, + &state->lock_list, &np); + talloc_free(data.dptr); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("g_lock_lock: %s invalid lock data\n", + (char *)state->key.dptr)); + tevent_req_error(req, ret); + return; + } + + ctdb_g_lock_lock_process_locks(req); +} + +static void ctdb_g_lock_lock_process_locks(struct tevent_req *req) +{ + struct ctdb_g_lock_lock_state *state = tevent_req_data( + req, struct ctdb_g_lock_lock_state); + struct tevent_req *subreq; + struct ctdb_g_lock *lock; + bool check_server = false; + int ret; + + while (state->current < state->lock_list->num) { + lock = &state->lock_list->lock[state->current]; + + /* We should not ask for the same lock more than once */ + if (ctdb_server_id_equal(&lock->sid, &state->my_sid)) { + DEBUG(DEBUG_ERR, ("g_lock_lock: %s deadlock\n", + (char *)state->key.dptr)); + tevent_req_error(req, EDEADLK); + return; + } + + if (ctdb_g_lock_conflicts(lock->type, state->lock_type)) { + check_server = true; + break; + } + + state->current += 1; + } + + if (check_server) { + struct ctdb_req_control request; + + ctdb_req_control_process_exists(&request, lock->sid.pid); + subreq = ctdb_client_control_send(state, state->ev, + state->client, + lock->sid.vnn, + tevent_timeval_zero(), + &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_g_lock_lock_checked, req); + return; + } + + /* There is no conflict, add ourself to the lock_list */ + state->lock_list->lock = talloc_realloc(state->lock_list, + state->lock_list->lock, + struct ctdb_g_lock, + state->lock_list->num + 1); + if (state->lock_list->lock == NULL) { + tevent_req_error(req, ENOMEM); + return; + } + + lock = &state->lock_list->lock[state->lock_list->num]; + lock->type = state->lock_type; + lock->sid = state->my_sid; + state->lock_list->num += 1; + + ret = ctdb_g_lock_lock_update(req); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + TALLOC_FREE(state->h); + tevent_req_done(req); +} + +static void ctdb_g_lock_lock_checked(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_g_lock_lock_state *state = tevent_req_data( + req, struct ctdb_g_lock_lock_state); + struct ctdb_reply_control *reply; + int ret, value; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("g_lock_lock: %s PROCESS_EXISTS failed, ret=%d\n", + (char *)state->key.dptr, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_process_exists(reply, &value); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + talloc_free(reply); + + if (value == 0) { + /* server process exists, need to retry */ + TALLOC_FREE(state->h); + subreq = tevent_wakeup_send(state, state->ev, + tevent_timeval_current_ofs(0,1000)); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_g_lock_lock_retry, req); + return; + } + + /* server process does not exist, remove conflicting entry */ + state->lock_list->lock[state->current] = + state->lock_list->lock[state->lock_list->num-1]; + state->lock_list->num -= 1; + + ret = ctdb_g_lock_lock_update(req); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + ctdb_g_lock_lock_process_locks(req); +} + +static int ctdb_g_lock_lock_update(struct tevent_req *req) +{ + struct ctdb_g_lock_lock_state *state = tevent_req_data( + req, struct ctdb_g_lock_lock_state); + TDB_DATA data; + size_t np; + int ret; + + data.dsize = ctdb_g_lock_list_len(state->lock_list); + data.dptr = talloc_size(state, data.dsize); + if (data.dptr == NULL) { + return ENOMEM; + } + + ctdb_g_lock_list_push(state->lock_list, data.dptr, &np); + ret = ctdb_store_record(state->h, data); + talloc_free(data.dptr); + return ret; +} + +static void ctdb_g_lock_lock_retry(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_g_lock_lock_state *state = tevent_req_data( + req, struct ctdb_g_lock_lock_state); + bool success; + + success = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (! success) { + tevent_req_error(req, ENOMEM); + return; + } + + subreq = ctdb_fetch_lock_send(state, state->ev, state->client, + state->db, state->key, false); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_g_lock_lock_fetched, req); +} + +bool ctdb_g_lock_lock_recv(struct tevent_req *req, int *perr) +{ + struct ctdb_g_lock_lock_state *state = tevent_req_data( + req, struct ctdb_g_lock_lock_state); + int err; + + TALLOC_FREE(state->h); + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + +struct ctdb_g_lock_unlock_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct ctdb_db_context *db; + TDB_DATA key; + struct ctdb_server_id my_sid; + struct ctdb_record_handle *h; + struct ctdb_g_lock_list *lock_list; +}; + +static void ctdb_g_lock_unlock_fetched(struct tevent_req *subreq); +static int ctdb_g_lock_unlock_update(struct tevent_req *req); +static void ctdb_g_lock_unlock_deleted(struct tevent_req *subreq); + +struct tevent_req *ctdb_g_lock_unlock_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_db_context *db, + const char *keyname, + struct ctdb_server_id sid) +{ + struct tevent_req *req, *subreq; + struct ctdb_g_lock_unlock_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_g_lock_unlock_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->client = client; + state->db = db; + state->key.dptr = discard_const(keyname); + state->key.dsize = strlen(keyname) + 1; + state->my_sid = sid; + + subreq = ctdb_fetch_lock_send(state, ev, client, db, state->key, + false); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_g_lock_unlock_fetched, req); + + return req; +} + +static void ctdb_g_lock_unlock_fetched(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_g_lock_unlock_state *state = tevent_req_data( + req, struct ctdb_g_lock_unlock_state); + TDB_DATA data; + size_t np; + int ret = 0; + + state->h = ctdb_fetch_lock_recv(subreq, NULL, state, &data, &ret); + TALLOC_FREE(subreq); + if (state->h == NULL) { + DEBUG(DEBUG_ERR, ("g_lock_unlock: %s fetch lock failed\n", + (char *)state->key.dptr)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_g_lock_list_pull(data.dptr, data.dsize, state, + &state->lock_list, &np); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("g_lock_unlock: %s invalid lock data\n", + (char *)state->key.dptr)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_g_lock_unlock_update(req); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + if (state->lock_list->num == 0) { + subreq = ctdb_delete_record_send(state, state->ev, state->h); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_g_lock_unlock_deleted, + req); + return; + } + + TALLOC_FREE(state->h); + tevent_req_done(req); +} + +static int ctdb_g_lock_unlock_update(struct tevent_req *req) +{ + struct ctdb_g_lock_unlock_state *state = tevent_req_data( + req, struct ctdb_g_lock_unlock_state); + struct ctdb_g_lock *lock; + unsigned int i; + int ret; + + for (i=0; ilock_list->num; i++) { + lock = &state->lock_list->lock[i]; + + if (ctdb_server_id_equal(&lock->sid, &state->my_sid)) { + break; + } + } + + if (i < state->lock_list->num) { + state->lock_list->lock[i] = + state->lock_list->lock[state->lock_list->num-1]; + state->lock_list->num -= 1; + } + + if (state->lock_list->num != 0) { + TDB_DATA data; + size_t np; + + data.dsize = ctdb_g_lock_list_len(state->lock_list); + data.dptr = talloc_size(state, data.dsize); + if (data.dptr == NULL) { + return ENOMEM; + } + + ctdb_g_lock_list_push(state->lock_list, data.dptr, &np); + ret = ctdb_store_record(state->h, data); + talloc_free(data.dptr); + if (ret != 0) { + return ret; + } + } + + return 0; +} + +static void ctdb_g_lock_unlock_deleted(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_g_lock_unlock_state *state = tevent_req_data( + req, struct ctdb_g_lock_unlock_state); + int ret; + bool status; + + status = ctdb_delete_record_recv(subreq, &ret); + if (! status) { + DEBUG(DEBUG_ERR, + ("g_lock_unlock %s delete record failed, ret=%d\n", + (char *)state->key.dptr, ret)); + tevent_req_error(req, ret); + return; + } + + TALLOC_FREE(state->h); + tevent_req_done(req); +} + +bool ctdb_g_lock_unlock_recv(struct tevent_req *req, int *perr) +{ + struct ctdb_g_lock_unlock_state *state = tevent_req_data( + req, struct ctdb_g_lock_unlock_state); + int err; + + TALLOC_FREE(state->h); + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + +/* + * Persistent database functions + */ +struct ctdb_transaction_start_state { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct timeval timeout; + struct ctdb_transaction_handle *h; + uint32_t destnode; +}; + +static void ctdb_transaction_g_lock_attached(struct tevent_req *subreq); +static void ctdb_transaction_g_lock_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_transaction_start_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + struct ctdb_db_context *db, + bool readonly) +{ + struct ctdb_transaction_start_state *state; + struct tevent_req *req, *subreq; + struct ctdb_transaction_handle *h; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_transaction_start_state); + if (req == NULL) { + return NULL; + } + + if (ctdb_db_volatile(db)) { + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + + state->ev = ev; + state->client = client; + state->destnode = ctdb_client_pnn(client); + + h = talloc_zero(db, struct ctdb_transaction_handle); + if (tevent_req_nomem(h, req)) { + return tevent_req_post(req, ev); + } + + h->ev = ev; + h->client = client; + h->db = db; + h->readonly = readonly; + h->updated = false; + + /* SRVID is unique for databases, so client can have transactions + * active for multiple databases */ + h->sid = ctdb_client_get_server_id(client, db->db_id); + + h->recbuf = ctdb_rec_buffer_init(h, db->db_id); + if (tevent_req_nomem(h->recbuf, req)) { + return tevent_req_post(req, ev); + } + + h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x", db->db_id); + if (tevent_req_nomem(h->lock_name, req)) { + return tevent_req_post(req, ev); + } + + state->h = h; + + subreq = ctdb_attach_send(state, ev, client, timeout, "g_lock.tdb", 0); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_transaction_g_lock_attached, req); + + return req; +} + +static void ctdb_transaction_g_lock_attached(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_transaction_start_state *state = tevent_req_data( + req, struct ctdb_transaction_start_state); + bool status; + int ret; + + status = ctdb_attach_recv(subreq, &ret, &state->h->db_g_lock); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("transaction_start: %s attach g_lock.tdb failed\n", + state->h->db->db_name)); + tevent_req_error(req, ret); + return; + } + + subreq = ctdb_g_lock_lock_send(state, state->ev, state->client, + state->h->db_g_lock, + state->h->lock_name, + &state->h->sid, state->h->readonly); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_transaction_g_lock_done, req); +} + +static void ctdb_transaction_g_lock_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_transaction_start_state *state = tevent_req_data( + req, struct ctdb_transaction_start_state); + int ret; + bool status; + + status = ctdb_g_lock_lock_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("transaction_start: %s g_lock lock failed, ret=%d\n", + state->h->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +struct ctdb_transaction_handle *ctdb_transaction_start_recv( + struct tevent_req *req, + int *perr) +{ + struct ctdb_transaction_start_state *state = tevent_req_data( + req, struct ctdb_transaction_start_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return NULL; + } + + return state->h; +} + +int ctdb_transaction_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct timeval timeout, + struct ctdb_db_context *db, bool readonly, + struct ctdb_transaction_handle **out) +{ + struct tevent_req *req; + struct ctdb_transaction_handle *h; + int ret = 0; + + req = ctdb_transaction_start_send(mem_ctx, ev, client, timeout, db, + readonly); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + h = ctdb_transaction_start_recv(req, &ret); + if (h == NULL) { + return ret; + } + + *out = h; + return 0; +} + +struct ctdb_transaction_record_fetch_state { + TDB_DATA key, data; + struct ctdb_ltdb_header header; + bool found; +}; + +static int ctdb_transaction_record_fetch_traverse( + uint32_t reqid, + struct ctdb_ltdb_header *nullheader, + TDB_DATA key, TDB_DATA data, + void *private_data) +{ + struct ctdb_transaction_record_fetch_state *state = + (struct ctdb_transaction_record_fetch_state *)private_data; + + if (state->key.dsize == key.dsize && + memcmp(state->key.dptr, key.dptr, key.dsize) == 0) { + int ret; + + ret = ctdb_ltdb_header_extract(&data, &state->header); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("record_fetch: Failed to extract header, " + "ret=%d\n", ret)); + return 1; + } + + state->data = data; + state->found = true; + } + + return 0; +} + +static int ctdb_transaction_record_fetch(struct ctdb_transaction_handle *h, + TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA *data) +{ + struct ctdb_transaction_record_fetch_state state; + int ret; + + state.key = key; + state.found = false; + + ret = ctdb_rec_buffer_traverse(h->recbuf, + ctdb_transaction_record_fetch_traverse, + &state); + if (ret != 0) { + return ret; + } + + if (state.found) { + if (header != NULL) { + *header = state.header; + } + if (data != NULL) { + *data = state.data; + } + return 0; + } + + return ENOENT; +} + +int ctdb_transaction_fetch_record(struct ctdb_transaction_handle *h, + TDB_DATA key, + TALLOC_CTX *mem_ctx, TDB_DATA *data) +{ + TDB_DATA tmp_data; + struct ctdb_ltdb_header header; + int ret; + + ret = ctdb_transaction_record_fetch(h, key, NULL, &tmp_data); + if (ret == 0) { + data->dptr = talloc_memdup(mem_ctx, tmp_data.dptr, + tmp_data.dsize); + if (data->dptr == NULL) { + return ENOMEM; + } + data->dsize = tmp_data.dsize; + return 0; + } + + ret = ctdb_ltdb_fetch(h->db, key, &header, mem_ctx, data); + if (ret != 0) { + return ret; + } + + ret = ctdb_rec_buffer_add(h, h->recbuf, 0, &header, key, *data); + if (ret != 0) { + return ret; + } + + return 0; +} + +int ctdb_transaction_store_record(struct ctdb_transaction_handle *h, + TDB_DATA key, TDB_DATA data) +{ + TALLOC_CTX *tmp_ctx; + struct ctdb_ltdb_header header; + TDB_DATA old_data; + int ret; + + if (h->readonly) { + return EINVAL; + } + + tmp_ctx = talloc_new(h); + if (tmp_ctx == NULL) { + return ENOMEM; + } + + ret = ctdb_transaction_record_fetch(h, key, &header, &old_data); + if (ret != 0) { + ret = ctdb_ltdb_fetch(h->db, key, &header, tmp_ctx, &old_data); + if (ret != 0) { + return ret; + } + } + + if (old_data.dsize == data.dsize && + memcmp(old_data.dptr, data.dptr, data.dsize) == 0) { + talloc_free(tmp_ctx); + return 0; + } + + header.dmaster = ctdb_client_pnn(h->client); + header.rsn += 1; + + ret = ctdb_rec_buffer_add(h, h->recbuf, 0, &header, key, data); + talloc_free(tmp_ctx); + if (ret != 0) { + return ret; + } + h->updated = true; + + return 0; +} + +int ctdb_transaction_delete_record(struct ctdb_transaction_handle *h, + TDB_DATA key) +{ + return ctdb_transaction_store_record(h, key, tdb_null); +} + +static int ctdb_transaction_fetch_db_seqnum(struct ctdb_transaction_handle *h, + uint64_t *seqnum) +{ + const char *keyname = CTDB_DB_SEQNUM_KEY; + TDB_DATA key, data; + struct ctdb_ltdb_header header; + int ret; + + key.dptr = discard_const(keyname); + key.dsize = strlen(keyname) + 1; + + ret = ctdb_ltdb_fetch(h->db, key, &header, h, &data); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("transaction_commit: %s seqnum fetch failed, ret=%d\n", + h->db->db_name, ret)); + return ret; + } + + if (data.dsize == 0) { + /* initial data */ + *seqnum = 0; + return 0; + } + + if (data.dsize != sizeof(uint64_t)) { + talloc_free(data.dptr); + return EINVAL; + } + + *seqnum = *(uint64_t *)data.dptr; + + talloc_free(data.dptr); + return 0; +} + +static int ctdb_transaction_store_db_seqnum(struct ctdb_transaction_handle *h, + uint64_t seqnum) +{ + const char *keyname = CTDB_DB_SEQNUM_KEY; + TDB_DATA key, data; + + key.dptr = discard_const(keyname); + key.dsize = strlen(keyname) + 1; + + data.dptr = (uint8_t *)&seqnum; + data.dsize = sizeof(seqnum); + + return ctdb_transaction_store_record(h, key, data); +} + +struct ctdb_transaction_commit_state { + struct tevent_context *ev; + struct timeval timeout; + struct ctdb_transaction_handle *h; + uint64_t seqnum; +}; + +static void ctdb_transaction_commit_done(struct tevent_req *subreq); +static void ctdb_transaction_commit_g_lock_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_transaction_commit_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct timeval timeout, + struct ctdb_transaction_handle *h) +{ + struct tevent_req *req, *subreq; + struct ctdb_transaction_commit_state *state; + struct ctdb_req_control request; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_transaction_commit_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->timeout = timeout; + state->h = h; + + ret = ctdb_transaction_fetch_db_seqnum(h, &state->seqnum); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = ctdb_transaction_store_db_seqnum(h, state->seqnum+1); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ctdb_req_control_trans3_commit(&request, h->recbuf); + subreq = ctdb_client_control_send(state, ev, h->client, + ctdb_client_pnn(h->client), + timeout, &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_transaction_commit_done, req); + + return req; +} + +static void ctdb_transaction_commit_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_transaction_commit_state *state = tevent_req_data( + req, struct ctdb_transaction_commit_state); + struct ctdb_transaction_handle *h = state->h; + struct ctdb_reply_control *reply; + uint64_t seqnum; + int ret; + bool status; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("transaction_commit: %s TRANS3_COMMIT failed, ret=%d\n", + h->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_trans3_commit(reply); + talloc_free(reply); + + if (ret != 0) { + /* Control failed due to recovery */ + + ret = ctdb_transaction_fetch_db_seqnum(h, &seqnum); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + if (seqnum == state->seqnum) { + struct ctdb_req_control request; + + /* try again */ + ctdb_req_control_trans3_commit(&request, + state->h->recbuf); + subreq = ctdb_client_control_send( + state, state->ev, state->h->client, + ctdb_client_pnn(state->h->client), + state->timeout, &request); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, + ctdb_transaction_commit_done, + req); + return; + } + + if (seqnum != state->seqnum + 1) { + DEBUG(DEBUG_ERR, + ("transaction_commit: %s seqnum mismatch " + "0x%"PRIx64" != 0x%"PRIx64" + 1\n", + state->h->db->db_name, seqnum, state->seqnum)); + tevent_req_error(req, EIO); + return; + } + } + + /* trans3_commit successful */ + subreq = ctdb_g_lock_unlock_send(state, state->ev, h->client, + h->db_g_lock, h->lock_name, h->sid); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, ctdb_transaction_commit_g_lock_done, + req); +} + +static void ctdb_transaction_commit_g_lock_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_transaction_commit_state *state = tevent_req_data( + req, struct ctdb_transaction_commit_state); + int ret; + bool status; + + status = ctdb_g_lock_unlock_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("transaction_commit: %s g_lock unlock failed, ret=%d\n", + state->h->db->db_name, ret)); + tevent_req_error(req, ret); + return; + } + + talloc_free(state->h); + tevent_req_done(req); +} + +bool ctdb_transaction_commit_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + +int ctdb_transaction_commit(struct ctdb_transaction_handle *h) +{ + struct tevent_context *ev = h->ev; + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + int ret; + bool status; + + if (h->readonly || ! h->updated) { + return ctdb_transaction_cancel(h); + } + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_transaction_commit_send(mem_ctx, ev, + tevent_timeval_zero(), h); + if (req == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_transaction_commit_recv(req, &ret); + if (! status) { + talloc_free(mem_ctx); + return ret; + } + + talloc_free(mem_ctx); + return 0; +} + +struct ctdb_transaction_cancel_state { + struct tevent_context *ev; + struct ctdb_transaction_handle *h; + struct timeval timeout; +}; + +static void ctdb_transaction_cancel_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_transaction_cancel_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct timeval timeout, + struct ctdb_transaction_handle *h) +{ + struct tevent_req *req, *subreq; + struct ctdb_transaction_cancel_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_transaction_cancel_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->h = h; + state->timeout = timeout; + + subreq = ctdb_g_lock_unlock_send(state, state->ev, state->h->client, + state->h->db_g_lock, + state->h->lock_name, state->h->sid); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_transaction_cancel_done, + req); + + return req; +} + +static void ctdb_transaction_cancel_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_transaction_cancel_state *state = tevent_req_data( + req, struct ctdb_transaction_cancel_state); + int ret; + bool status; + + status = ctdb_g_lock_unlock_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + DEBUG(DEBUG_ERR, + ("transaction_cancel: %s g_lock unlock failed, ret=%d\n", + state->h->db->db_name, ret)); + talloc_free(state->h); + tevent_req_error(req, ret); + return; + } + + talloc_free(state->h); + tevent_req_done(req); +} + +bool ctdb_transaction_cancel_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + +int ctdb_transaction_cancel(struct ctdb_transaction_handle *h) +{ + struct tevent_context *ev = h->ev; + struct tevent_req *req; + TALLOC_CTX *mem_ctx; + int ret; + bool status; + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + talloc_free(h); + return ENOMEM; + } + + req = ctdb_transaction_cancel_send(mem_ctx, ev, + tevent_timeval_zero(), h); + if (req == NULL) { + talloc_free(mem_ctx); + talloc_free(h); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_transaction_cancel_recv(req, &ret); + if (! status) { + talloc_free(mem_ctx); + return ret; + } + + talloc_free(mem_ctx); + return 0; +} + +/* + * TODO: + * + * In future Samba should register SERVER_ID. + * Make that structure same as struct srvid {}. + */ diff --git a/ctdb/client/client_event.c b/ctdb/client/client_event.c new file mode 100644 index 0000000..7111fe7 --- /dev/null +++ b/ctdb/client/client_event.c @@ -0,0 +1,444 @@ +/* + Eventd client api + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" + +#include +#include + +#include "lib/util/debug.h" +#include "lib/util/tevent_unix.h" + +#include "common/logging.h" +#include "common/sock_client.h" + +#include "protocol/protocol_api.h" + +#include "client/client_event.h" + +struct ctdb_event_context { + struct sock_client_context *sockc; +}; + +static int ctdb_event_msg_request_push(void *request_data, uint32_t reqid, + TALLOC_CTX *mem_ctx, + uint8_t **buf, size_t *buflen, + void *private_data) +{ + struct ctdb_event_request *request = + (struct ctdb_event_request *)request_data; + int ret; + + sock_packet_header_set_reqid(&request->header, reqid); + + *buflen = ctdb_event_request_len(request); + *buf = talloc_size(mem_ctx, *buflen); + if (*buf == NULL) { + return ENOMEM; + } + + ret = ctdb_event_request_push(request, *buf, buflen); + if (ret != 0) { + return ret; + } + + return 0; +} + +static int ctdb_event_msg_reply_pull(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, void **reply_data, + void *private_data) +{ + struct ctdb_event_reply *reply; + int ret; + + reply = talloc_zero(mem_ctx, struct ctdb_event_reply); + if (reply == NULL) { + return ENOMEM; + } + + ret = ctdb_event_reply_pull(buf, buflen, reply, reply); + if (ret != 0) { + talloc_free(reply); + return ret; + } + + *reply_data = reply; + return 0; +} + +static int ctdb_event_msg_reply_reqid(uint8_t *buf, size_t buflen, + uint32_t *reqid, void *private_data) +{ + struct sock_packet_header header; + size_t np; + int ret; + + ret = sock_packet_header_pull(buf, buflen, &header, &np); + if (ret != 0) { + return ret; + } + + *reqid = header.reqid; + return 0; +} + +struct sock_client_proto_funcs event_proto_funcs = { + .request_push = ctdb_event_msg_request_push, + .reply_pull = ctdb_event_msg_reply_pull, + .reply_reqid = ctdb_event_msg_reply_reqid, +}; + + +int ctdb_event_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + const char *sockpath, struct ctdb_event_context **out) +{ + struct ctdb_event_context *eclient; + int ret; + + eclient = talloc_zero(mem_ctx, struct ctdb_event_context); + if (eclient == NULL) { + DEBUG(DEBUG_ERR, (__location__ " memory allocation error\n")); + return ENOMEM; + } + + ret = sock_client_setup(eclient, ev, sockpath, + &event_proto_funcs, eclient, + &eclient->sockc); + if (ret != 0) { + talloc_free(eclient); + return ret; + } + + *out = eclient; + return 0; +} + +void ctdb_event_set_disconnect_callback(struct ctdb_event_context *eclient, + ctdb_client_callback_func_t callback, + void *private_data) +{ + sock_client_set_disconnect_callback(eclient->sockc, + callback, private_data); +} + +/* + * Handle eventd_request and eventd_reply + */ + +struct tevent_req *ctdb_event_msg_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request *request) +{ + struct tevent_req *req; + + req = sock_client_msg_send(mem_ctx, ev, eclient->sockc, + tevent_timeval_zero(), request); + return req; +} + +bool ctdb_event_msg_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **reply) +{ + void *reply_data; + bool status; + + status = sock_client_msg_recv(req, perr, mem_ctx, &reply_data); + + if (status && reply != NULL) { + *reply = talloc_get_type_abort( + reply_data, struct ctdb_event_reply); + } + + return status; +} + +/* + * Run an event + */ + +struct tevent_req *ctdb_event_run_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + enum ctdb_event event, + uint32_t timeout, const char *arg_str) +{ + struct ctdb_event_request request; + struct ctdb_event_request_run rdata; + + rdata.event = event; + rdata.timeout = timeout; + rdata.arg_str = arg_str; + + request.rdata.command = CTDB_EVENT_COMMAND_RUN; + request.rdata.data.run = &rdata; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_run_recv(struct tevent_req *req, int *perr, int *result) +{ + struct ctdb_event_reply *reply; + int ret; + bool status; + + status = ctdb_event_msg_recv(req, &ret, req, &reply); + if (! status) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (reply->rdata.command != CTDB_EVENT_COMMAND_RUN) { + if (perr != NULL) { + *perr = EPROTO; + } + talloc_free(reply); + return false; + } + + if (result != NULL) { + *result = reply->rdata.result; + } + + talloc_free(reply); + return true; +} + +/* + * Get event status + */ + +struct tevent_req *ctdb_event_status_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + enum ctdb_event event, + enum ctdb_event_status_state state) +{ + struct ctdb_event_request request; + struct ctdb_event_request_status rdata; + + rdata.event = event; + rdata.state = state; + + request.rdata.command = CTDB_EVENT_COMMAND_STATUS; + request.rdata.data.status = &rdata; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_status_recv(struct tevent_req *req, int *perr, + int32_t *result, int *event_status, + TALLOC_CTX *mem_ctx, + struct ctdb_script_list **script_list) +{ + struct ctdb_event_reply *reply; + int ret; + bool status; + + status = ctdb_event_msg_recv(req, &ret, req, &reply); + if (! status) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (reply->rdata.command != CTDB_EVENT_COMMAND_STATUS) { + if (perr != NULL) { + *perr = EPROTO; + } + talloc_free(reply); + return false; + } + + if (result != NULL) { + *result = reply->rdata.result; + } + if (event_status != NULL) { + *event_status = reply->rdata.data.status->status; + } + if (script_list != NULL) { + *script_list = talloc_steal(mem_ctx, + reply->rdata.data.status->script_list); + } + + talloc_free(reply); + return true; +} + +/* + * Get script list + */ + +struct tevent_req *ctdb_event_script_list_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient) +{ + struct ctdb_event_request request; + + request.rdata.command = CTDB_EVENT_COMMAND_SCRIPT_LIST; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_script_list_recv(struct tevent_req *req, int *perr, + int32_t *result, TALLOC_CTX *mem_ctx, + struct ctdb_script_list **script_list) +{ + struct ctdb_event_reply *reply; + int ret; + bool status; + + status = ctdb_event_msg_recv(req, &ret, req, &reply); + if (! status) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (reply->rdata.command != CTDB_EVENT_COMMAND_SCRIPT_LIST) { + if (perr != NULL) { + *perr = EPROTO; + } + talloc_free(reply); + return false; + } + + if (result != NULL) { + *result = reply->rdata.result; + } + if (script_list != NULL) { + *script_list = talloc_steal(mem_ctx, + reply->rdata.data.script_list->script_list); + } + + talloc_free(reply); + return true; +} + +/* + * Enable a script + */ + +struct tevent_req *ctdb_event_script_enable_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + const char *script_name) +{ + struct ctdb_event_request request; + struct ctdb_event_request_script_enable rdata; + + rdata.script_name = script_name; + + request.rdata.command = CTDB_EVENT_COMMAND_SCRIPT_ENABLE; + request.rdata.data.script_enable = &rdata; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_script_enable_recv(struct tevent_req *req, int *perr, + int *result) +{ + struct ctdb_event_reply *reply; + int ret; + bool status; + + status = ctdb_event_msg_recv(req, &ret, req, &reply); + if (! status) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (reply->rdata.command != CTDB_EVENT_COMMAND_SCRIPT_ENABLE) { + if (perr != NULL) { + *perr = EPROTO; + } + talloc_free(reply); + return false; + } + + if (result != NULL) { + *result = reply->rdata.result; + } + + talloc_free(reply); + return true; +} + +/* + * Disable a script + */ + +struct tevent_req *ctdb_event_script_disable_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + const char *script_name) +{ + struct ctdb_event_request request; + struct ctdb_event_request_script_disable rdata; + + rdata.script_name = script_name; + + request.rdata.command = CTDB_EVENT_COMMAND_SCRIPT_DISABLE; + request.rdata.data.script_disable = &rdata; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_script_disable_recv(struct tevent_req *req, int *perr, + int *result) +{ + struct ctdb_event_reply *reply; + int ret; + bool status; + + status = ctdb_event_msg_recv(req, &ret, req, &reply); + if (! status) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (reply->rdata.command != CTDB_EVENT_COMMAND_SCRIPT_DISABLE) { + if (perr != NULL) { + *perr = EPROTO; + } + talloc_free(reply); + return false; + } + + if (result != NULL) { + *result = reply->rdata.result; + } + + talloc_free(reply); + return true; +} diff --git a/ctdb/client/client_message.c b/ctdb/client/client_message.c new file mode 100644 index 0000000..c2e975a --- /dev/null +++ b/ctdb/client/client_message.c @@ -0,0 +1,607 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "common/reqid.h" +#include "common/srvid.h" +#include "common/comm.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" + +#include "client/client_private.h" +#include "client/client.h" + + +/* + * Handle REQ_MESSAGE + */ + +struct ctdb_client_message_state { + struct ctdb_client_context *client; + uint32_t reqid; +}; + +static int ctdb_client_message_state_destructor( + struct ctdb_client_message_state *state); +static void ctdb_client_message_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_message_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, + struct ctdb_req_message *message) +{ + struct tevent_req *req, *subreq; + struct ctdb_client_message_state *state; + struct ctdb_req_header h; + uint32_t reqid; + uint8_t *buf; + size_t datalen, buflen; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_message_state); + if (req == NULL) { + return NULL; + } + + reqid = reqid_new(client->idr, state); + if (reqid == REQID_INVALID) { + talloc_free(req); + return NULL; + } + + state->client = client; + state->reqid = reqid; + + talloc_set_destructor(state, ctdb_client_message_state_destructor); + + ctdb_req_header_fill(&h, 0, CTDB_REQ_MESSAGE, destnode, + client->pnn, reqid); + + datalen = ctdb_req_message_len(&h, message); + ret = ctdb_allocate_pkt(state, datalen, &buf, &buflen); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = ctdb_req_message_push(&h, message, buf, &buflen); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + subreq = comm_write_send(state, ev, client->comm, buf, buflen); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_client_message_done, req); + + return req; +} + +static int ctdb_client_message_state_destructor( + struct ctdb_client_message_state *state) +{ + reqid_remove(state->client->idr, state->reqid); + return 0; +} + +static void ctdb_client_message_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + int ret; + bool status; + + status = comm_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_client_message_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + return true; +} + +void ctdb_client_req_message(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid) +{ + struct ctdb_req_header h; + struct ctdb_req_message_data message; + TALLOC_CTX *tmp_ctx = talloc_new(client); + int ret; + + ret = ctdb_req_message_data_pull(buf, buflen, &h, tmp_ctx, &message); + if (ret != 0) { + return; + } + + srvid_dispatch(client->srv, message.srvid, CTDB_SRVID_ALL, + message.data); + talloc_free(tmp_ctx); +} + +/* + * Handle multiple nodes + */ + +struct ctdb_client_message_multi_state { + uint32_t *pnn_list; + int count; + int done; + int err; + int *err_list; +}; + +struct message_index_state { + struct tevent_req *req; + int index; +}; + +static void ctdb_client_message_multi_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_message_multi_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct ctdb_req_message *message) +{ + struct tevent_req *req, *subreq; + struct ctdb_client_message_multi_state *state; + int i; + + if (pnn_list == NULL || count == 0) { + return NULL; + } + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_message_multi_state); + if (req == NULL) { + return NULL; + } + + state->pnn_list = pnn_list; + state->count = count; + state->done = 0; + state->err = 0; + state->err_list = talloc_zero_array(state, int, count); + if (tevent_req_nomem(state->err_list, req)) { + return tevent_req_post(req, ev); + } + + for (i=0; ireq = req; + substate->index = i; + + tevent_req_set_callback(subreq, ctdb_client_message_multi_done, + substate); + } + + return req; +} + +static void ctdb_client_message_multi_done(struct tevent_req *subreq) +{ + struct message_index_state *substate = tevent_req_callback_data( + subreq, struct message_index_state); + struct tevent_req *req = substate->req; + int idx = substate->index; + struct ctdb_client_message_multi_state *state = tevent_req_data( + req, struct ctdb_client_message_multi_state); + bool status; + int ret; + + status = ctdb_client_message_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + if (state->err == 0) { + state->err = ret; + state->err_list[idx] = state->err; + } + } + + state->done += 1; + + if (state->done == state->count) { + tevent_req_done(req); + } +} + +bool ctdb_client_message_multi_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, int **perr_list) +{ + struct ctdb_client_message_multi_state *state = tevent_req_data( + req, struct ctdb_client_message_multi_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + if (perr_list != NULL) { + *perr_list = talloc_steal(mem_ctx, state->err_list); + } + return false; + } + + if (perr != NULL) { + *perr = state->err; + } + + if (perr_list != NULL) { + *perr_list = talloc_steal(mem_ctx, state->err_list); + } + + if (state->err != 0) { + return false; + } + + return true; +} + +/* + * sync version of message send + */ + +int ctdb_client_message(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t destnode, struct ctdb_req_message *message) +{ + TALLOC_CTX *tmp_ctx; + struct tevent_req *req; + int ret; + bool status; + + tmp_ctx = talloc_new(client); + if (tmp_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_client_message_send(tmp_ctx, ev, client, destnode, message); + if (req == NULL) { + talloc_free(tmp_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_client_message_recv(req, &ret); + if (! status) { + talloc_free(tmp_ctx); + return ret; + } + + talloc_free(tmp_ctx); + return 0; +} + +int ctdb_client_message_multi(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint32_t *pnn_list, int count, + struct ctdb_req_message *message, + int **perr_list) +{ + struct tevent_req *req; + bool status; + int ret; + + req = ctdb_client_message_multi_send(mem_ctx, ev, client, + pnn_list, count, + message); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_client_message_multi_recv(req, &ret, mem_ctx, perr_list); + if (! status) { + return ret; + } + + return 0; +} + +struct ctdb_client_set_message_handler_state { + struct ctdb_client_context *client; + uint64_t srvid; + srvid_handler_fn handler; + void *private_data; +}; + +static void ctdb_client_set_message_handler_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_set_message_handler_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, + srvid_handler_fn handler, + void *private_data) +{ + struct tevent_req *req, *subreq; + struct ctdb_client_set_message_handler_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_set_message_handler_state); + if (req == NULL) { + return NULL; + } + + state->client = client; + state->srvid = srvid; + state->handler = handler; + state->private_data = private_data; + + ctdb_req_control_register_srvid(&request, srvid); + subreq = ctdb_client_control_send(state, ev, client, client->pnn, + tevent_timeval_zero(), &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_client_set_message_handler_done, + req); + + return req; +} + +static void ctdb_client_set_message_handler_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_client_set_message_handler_state *state = tevent_req_data( + req, struct ctdb_client_set_message_handler_state); + struct ctdb_reply_control *reply; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_register_srvid(reply); + talloc_free(reply); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + ret = srvid_register(state->client->srv, state->client, state->srvid, + state->handler, state->private_data); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_client_set_message_handler_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + return true; +} + +struct ctdb_client_remove_message_handler_state { + struct ctdb_client_context *client; + uint64_t srvid; + void *private_data; +}; + +static void ctdb_client_remove_message_handler_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_client_remove_message_handler_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, + void *private_data) +{ + struct tevent_req *req, *subreq; + struct ctdb_client_remove_message_handler_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_client_remove_message_handler_state); + if (req == NULL) { + return NULL; + } + + state->client = client; + state->srvid = srvid; + state->private_data = private_data; + + ctdb_req_control_deregister_srvid(&request, srvid); + subreq = ctdb_client_control_send(state, ev, client, client->pnn, + tevent_timeval_zero(), &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, + ctdb_client_remove_message_handler_done, req); + + return req; +} + +static void ctdb_client_remove_message_handler_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_client_remove_message_handler_state *state = tevent_req_data( + req, struct ctdb_client_remove_message_handler_state); + struct ctdb_reply_control *reply; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_deregister_srvid(reply); + talloc_free(reply); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + ret = srvid_deregister(state->client->srv, state->srvid, + state->private_data); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_client_remove_message_handler_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + return true; +} + +int ctdb_client_set_message_handler(struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, srvid_handler_fn handler, + void *private_data) +{ + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + int ret; + bool status; + + mem_ctx = talloc_new(client); + if (mem_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_client_set_message_handler_send(mem_ctx, ev, client, + srvid, handler, + private_data); + if (req == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_client_set_message_handler_recv(req, &ret); + if (! status) { + talloc_free(mem_ctx); + return ret; + } + + talloc_free(mem_ctx); + return 0; +} + +int ctdb_client_remove_message_handler(struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t srvid, void *private_data) +{ + TALLOC_CTX *mem_ctx; + struct tevent_req *req; + int ret; + bool status; + + mem_ctx = talloc_new(client); + if (mem_ctx == NULL) { + return ENOMEM; + } + + req = ctdb_client_remove_message_handler_send(mem_ctx, ev, client, + srvid, private_data); + if (req == NULL) { + talloc_free(mem_ctx); + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_client_remove_message_handler_recv(req, &ret); + if (! status) { + talloc_free(mem_ctx); + return ret; + } + + talloc_free(mem_ctx); + return 0; +} diff --git a/ctdb/client/client_message_sync.c b/ctdb/client/client_message_sync.c new file mode 100644 index 0000000..f4d2441 --- /dev/null +++ b/ctdb/client/client_message_sync.c @@ -0,0 +1,197 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "common/logging.h" + +#include "lib/util/debug.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" +#include "client/client_private.h" +#include "client/client.h" +#include "client/client_sync.h" + +int ctdb_message_recd_update_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct ctdb_public_ip *pubip) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_RECD_UPDATE_IP; + message.data.pubip = pubip; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message RECD_UPDATE_IP failed to node %u\n", + destnode)); + } + + return ret; +} + +int ctdb_message_mem_dump(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct ctdb_srvid_message *msg) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_MEM_DUMP; + message.data.msg = msg; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message MEM_DUMP failed to node %u\n", destnode)); + } + + return ret; +} + +int ctdb_message_reload_nodes(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_RELOAD_NODES; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message RELOAD_NODES failed to node %u\n", destnode)); + } + + return ret; +} + +int ctdb_message_takeover_run(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct ctdb_srvid_message *msg) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_TAKEOVER_RUN; + message.data.msg = msg; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message TAKEOVER_RUN failed to node %u\n", destnode)); + } + + return ret; +} + +int ctdb_message_rebalance_node(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, uint32_t pnn) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_REBALANCE_NODE; + message.data.pnn = pnn; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message REBALANCE_NODE failed to node %u\n", + destnode)); + } + + return ret; +} + +int ctdb_message_disable_takeover_runs(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct ctdb_disable_message *disable) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_DISABLE_TAKEOVER_RUNS; + message.data.disable = disable; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message DISABLE_TAKEOVER_RUNS failed to node %u\n", + destnode)); + } + + return ret; +} + +int ctdb_message_disable_recoveries(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct ctdb_disable_message *disable) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_DISABLE_RECOVERIES; + message.data.disable = disable; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message DISABLE_RECOVERIES failed to node %u\n", + destnode)); + } + + return ret; +} + +int ctdb_message_disable_ip_check(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, uint32_t timeout) +{ + struct ctdb_req_message message; + int ret; + + message.srvid = CTDB_SRVID_DISABLE_IP_CHECK; + message.data.timeout = timeout; + + ret = ctdb_client_message(mem_ctx, ev, client, destnode, &message); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Message DISABLE_IP_CHECK failed to node %u\n", + destnode)); + } + + return ret; +} diff --git a/ctdb/client/client_private.h b/ctdb/client/client_private.h new file mode 100644 index 0000000..0bb2ad5 --- /dev/null +++ b/ctdb/client/client_private.h @@ -0,0 +1,99 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CLIENT_PRIVATE_H__ +#define __CTDB_CLIENT_PRIVATE_H__ + +#include "protocol/protocol.h" +#include "client/client.h" + +struct ctdb_db_context { + struct ctdb_db_context *prev, *next; + uint32_t db_id; + uint8_t db_flags; + const char *db_name; + const char *db_path; + struct tdb_wrap *ltdb; +}; + +struct ctdb_client_context { + struct reqid_context *idr; + struct srvid_context *srv; + struct srvid_context *tunnels; + struct comm_context *comm; + ctdb_client_callback_func_t callback; + void *private_data; + int fd; + uint32_t pnn; + struct ctdb_db_context *db; +}; + +struct ctdb_record_handle { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct ctdb_db_context *db; + struct ctdb_ltdb_header header; + TDB_DATA key; + TDB_DATA data; /* This is returned from tdb_fetch() */ + bool readonly; +}; + +struct ctdb_transaction_handle { + struct tevent_context *ev; + struct ctdb_client_context *client; + struct ctdb_db_context *db, *db_g_lock; + struct ctdb_rec_buffer *recbuf; + struct ctdb_server_id sid; + const char *lock_name; + bool readonly; + bool updated; +}; + +struct ctdb_tunnel_context { + struct ctdb_client_context *client; + uint64_t tunnel_id; + ctdb_tunnel_callback_func_t callback; + void *private_data; +}; + +/* From client_call.c */ + +void ctdb_client_reply_call(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid); + +/* From client_db.c */ + +struct tdb_context *client_db_tdb(struct ctdb_db_context *db); + +/* From client_message.c */ + +void ctdb_client_req_message(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid); + +/* From client_control.c */ + +void ctdb_client_reply_control(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid); + +/* From client_tunnel.c */ + +void ctdb_client_req_tunnel(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid); + +#endif /* __CTDB_CLIENT_PRIVATE_H__ */ diff --git a/ctdb/client/client_sync.h b/ctdb/client/client_sync.h new file mode 100644 index 0000000..5b0ff42 --- /dev/null +++ b/ctdb/client/client_sync.h @@ -0,0 +1,526 @@ +/* + CTDB client code - sync api + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CLIENT_SYNC_H__ +#define __CTDB_CLIENT_SYNC_H__ + +#include +#include + +/* from client/client_control_sync.c */ + +int ctdb_ctrl_process_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + pid_t pid, int *status); + +int ctdb_ctrl_statistics(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_statistics **stats); + +int ctdb_ctrl_ping(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int *num_clients); + +int ctdb_ctrl_getdbpath(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, const char **db_path); + +int ctdb_ctrl_getvnnmap(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_vnn_map **vnnmap); + +int ctdb_ctrl_getdebug(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int *loglevel); + +int ctdb_ctrl_setdebug(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int loglevel); + +int ctdb_ctrl_get_dbmap(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_dbid_map **dbmap); + +int ctdb_ctrl_get_recmode(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int *recmode); + +int ctdb_ctrl_set_recmode(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int recmode); + +int ctdb_ctrl_statistics_reset(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_db_attach(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *db_name, uint32_t *db_id); + +int ctdb_ctrl_traverse_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_traverse_start *traverse); + +int ctdb_ctrl_register_srvid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t srvid); + +int ctdb_ctrl_deregister_srvid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t srvid); + +int ctdb_ctrl_get_dbname(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, const char **db_name); + +int ctdb_ctrl_enable_seqnum(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_update_seqnum(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_dump_memory(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char **mem_str); + +int ctdb_ctrl_get_pid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + pid_t *pid); + +int ctdb_ctrl_freeze(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + int priority); + +int ctdb_ctrl_get_pnn(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t *pnn); + +int ctdb_ctrl_shutdown(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_tcp_add(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_connection *conn); + +int ctdb_ctrl_tcp_remove(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_connection *conn); + +int ctdb_ctrl_set_tunable(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_tunable *tunable); + +int ctdb_ctrl_get_tunable(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *var, uint32_t *value); + +int ctdb_ctrl_list_tunables(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_var_list **var_list); + +int ctdb_ctrl_modify_flags(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t pnn, uint32_t old_flags, + uint32_t new_flags); + +int ctdb_ctrl_get_all_tunables(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_tunable_list **tun_list); + +int ctdb_ctrl_get_tcp_tickle_list(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + ctdb_sock_addr *addr, + struct ctdb_tickle_list **tickles); + +int ctdb_ctrl_set_tcp_tickle_list(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_tickle_list *tickles); + +int ctdb_ctrl_db_attach_persistent(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *db_name, uint32_t *db_id); + +int ctdb_ctrl_send_gratuitous_arp(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_addr_info *addr_info); + +int ctdb_ctrl_wipe_database(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, uint32_t tid); + +int ctdb_ctrl_uptime(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_uptime **uptime); + +int ctdb_ctrl_start_recovery(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_end_recovery(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_reload_nodes_file(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_add_public_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_addr_info *addr_info); + +int ctdb_ctrl_del_public_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_addr_info *addr_info); + +int ctdb_ctrl_get_capabilities(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t *caps); + +int ctdb_ctrl_release_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_public_ip *pubip); + +int ctdb_ctrl_takeover_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_public_ip *pubip); + +int ctdb_ctrl_get_public_ips(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + bool available_only, + struct ctdb_public_ip_list **pubip_list); + +int ctdb_ctrl_get_nodemap(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_node_map **nodemap); + +int ctdb_ctrl_traverse_kill(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_traverse_start *traverse); + +int ctdb_ctrl_get_reclock_file(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char **reclock_file); + +int ctdb_ctrl_stop_node(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_continue_node(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_set_lmasterrole(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t lmaster_role); + +int ctdb_ctrl_set_recmasterrole(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t recmaster_role); + +int ctdb_ctrl_set_ban_state(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_ban_state *ban_state); + +int ctdb_ctrl_get_ban_state(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_ban_state **ban_state); + +int ctdb_ctrl_register_notify(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_notify_data *notify); + +int ctdb_ctrl_deregister_notify(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t srvid); + +int ctdb_ctrl_trans3_commit(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_rec_buffer *recbuf); + +int ctdb_ctrl_get_db_seqnum(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, uint64_t *seqnum); + +int ctdb_ctrl_db_set_healthy(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_db_get_health(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, const char **reason); + +int ctdb_ctrl_get_public_ip_info(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + ctdb_sock_addr *addr, + struct ctdb_public_ip_info **ipinfo); + +int ctdb_ctrl_get_ifaces(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_iface_list **iface_list); + +int ctdb_ctrl_set_iface_link_state(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_iface *iface); + +int ctdb_ctrl_tcp_add_delayed_update(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_connection *conn); + +int ctdb_ctrl_get_stat_history(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_statistics_list **stats_list); + +int ctdb_ctrl_schedule_for_deletion(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_key_data *key); + +int ctdb_ctrl_set_db_readonly(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_traverse_start_ext(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_traverse_start_ext *traverse); + +int ctdb_ctrl_get_db_statistics(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, + struct ctdb_db_statistics **dbstats); + +int ctdb_ctrl_set_db_sticky(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_reload_public_ips(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_ipreallocated(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout); + +int ctdb_ctrl_get_runstate(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + enum ctdb_runstate *runstate); + +int ctdb_ctrl_db_detach(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_get_nodes_file(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_node_map **nodemap); + +int ctdb_ctrl_db_freeze(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, uint32_t db_id); + +int ctdb_ctrl_db_thaw(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, uint32_t db_id); + +int ctdb_ctrl_db_transaction_start(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_transdb *transdb); + +int ctdb_ctrl_db_transaction_commit(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_transdb *transdb); + +int ctdb_ctrl_db_transaction_cancel(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id); + +int ctdb_ctrl_db_pull(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_pulldb_ext *pulldb, uint32_t *num_records); + +int ctdb_ctrl_db_push_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_pulldb_ext *pulldb); + +int ctdb_ctrl_db_push_confirm(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, uint32_t *num_records); + +int ctdb_ctrl_db_open_flags(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint32_t db_id, int *tdb_flags); + +int ctdb_ctrl_db_attach_replicated(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + const char *db_name, uint32_t *db_id); + +int ctdb_ctrl_check_pid_srvid(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + struct ctdb_pid_srvid *pid_srvid, int *status); + +int ctdb_ctrl_tunnel_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t tunnel_id); + +int ctdb_ctrl_tunnel_deregister(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct timeval timeout, + uint64_t tunnel_id); + +int ctdb_ctrl_disable_node(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct timeval timeout); + +int ctdb_ctrl_enable_node(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct timeval timeout); + +/* from client/client_message_sync.c */ + +int ctdb_message_recd_update_ip(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct ctdb_public_ip *pubip); + +int ctdb_message_mem_dump(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct ctdb_srvid_message *msg); + +int ctdb_message_reload_nodes(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode); + +int ctdb_message_takeover_run(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, struct ctdb_srvid_message *msg); + +int ctdb_message_rebalance_node(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, uint32_t pnn); + +int ctdb_message_disable_takeover_runs(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct ctdb_disable_message *disable); + +int ctdb_message_disable_recoveries(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, + struct ctdb_disable_message *disable); + +int ctdb_message_disable_ip_check(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + int destnode, uint32_t timeout); + +#endif /* __CTDB_CLIENT_SYNC_H__ */ diff --git a/ctdb/client/client_tunnel.c b/ctdb/client/client_tunnel.c new file mode 100644 index 0000000..13c35fb --- /dev/null +++ b/ctdb/client/client_tunnel.c @@ -0,0 +1,693 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" + +#include +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "common/reqid.h" +#include "common/srvid.h" +#include "common/comm.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" + +#include "client/client_private.h" +#include "client/client.h" + + +struct ctdb_tunnel_data { + struct ctdb_req_header hdr; + struct ctdb_req_tunnel *tunnel; + uint32_t reqid; +}; + +/* + * Tunnel setup and destroy + */ + +struct ctdb_tunnel_setup_state { + struct ctdb_client_context *client; + struct ctdb_tunnel_context *tctx; + uint64_t tunnel_id; +}; + +static void ctdb_tunnel_setup_register_done(struct tevent_req *subreq); +static void ctdb_tunnel_handler(uint64_t tunnel_id, TDB_DATA data, + void *private_data); + +struct tevent_req *ctdb_tunnel_setup_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_client_context *client, + uint64_t tunnel_id, + ctdb_tunnel_callback_func_t callback, + void *private_data) +{ + struct tevent_req *req, *subreq; + struct ctdb_tunnel_setup_state *state; + struct ctdb_tunnel_context *tctx; + struct ctdb_req_control request; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_tunnel_setup_state); + if (req == NULL) { + return NULL; + } + + tctx = talloc_zero(client, struct ctdb_tunnel_context); + if (tevent_req_nomem(tctx, req)) { + return tevent_req_post(req, ev); + } + + tctx->client = client; + tctx->tunnel_id = tunnel_id; + tctx->callback = callback; + tctx->private_data = private_data; + + state->client = client; + state->tunnel_id = tunnel_id; + state->tctx = tctx; + + ret = srvid_exists(client->tunnels, tunnel_id, NULL); + if (ret == 0) { + tevent_req_error(req, EEXIST); + return tevent_req_post(req, ev); + } + + ctdb_req_control_tunnel_register(&request, tunnel_id); + subreq = ctdb_client_control_send(state, ev, client, + ctdb_client_pnn(client), + tevent_timeval_zero(), + &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_tunnel_setup_register_done, req); + + return req; +} + +static void ctdb_tunnel_setup_register_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_tunnel_setup_state *state = tevent_req_data( + req, struct ctdb_tunnel_setup_state); + struct ctdb_reply_control *reply; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_tunnel_register(reply); + talloc_free(reply); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + ret = srvid_register(state->client->tunnels, state->client, + state->tunnel_id, + ctdb_tunnel_handler, state->tctx); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +static void ctdb_tunnel_handler(uint64_t tunnel_id, TDB_DATA data, + void *private_data) +{ + struct ctdb_tunnel_context *tctx = talloc_get_type_abort( + private_data, struct ctdb_tunnel_context); + struct ctdb_tunnel_data *tunnel_data; + + if (tctx->tunnel_id != tunnel_id) { + return; + } + + if (data.dsize != sizeof(struct ctdb_tunnel_data)) { + return; + } + + tunnel_data = (struct ctdb_tunnel_data *)data.dptr; + + tctx->callback(tctx, tunnel_data->hdr.srcnode, tunnel_data->reqid, + tunnel_data->tunnel->data.dptr, + tunnel_data->tunnel->data.dsize, tctx->private_data); +} + +bool ctdb_tunnel_setup_recv(struct tevent_req *req, int *perr, + struct ctdb_tunnel_context **result) +{ + struct ctdb_tunnel_setup_state *state = tevent_req_data( + req, struct ctdb_tunnel_setup_state); + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + *result = state->tctx; + return true; +} + +int ctdb_tunnel_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, uint64_t tunnel_id, + ctdb_tunnel_callback_func_t callback, void *private_data, + struct ctdb_tunnel_context **result) +{ + struct tevent_req *req; + int ret; + bool status; + + req = ctdb_tunnel_setup_send(mem_ctx, ev, client, tunnel_id, + callback, private_data); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_tunnel_setup_recv(req, &ret, result); + talloc_free(req); + if (! status) { + return ret; + } + + return 0; +} + +struct ctdb_tunnel_destroy_state { + struct ctdb_tunnel_context *tctx; +}; + +static void ctdb_tunnel_destroy_deregister_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_tunnel_destroy_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_tunnel_context *tctx) +{ + struct tevent_req *req, *subreq; + struct ctdb_tunnel_destroy_state *state; + struct ctdb_req_control request; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_tunnel_destroy_state); + if (req == NULL) { + return NULL; + } + + state->tctx = tctx; + + ctdb_req_control_tunnel_deregister(&request, tctx->tunnel_id); + subreq = ctdb_client_control_send(state, ev, tctx->client, + ctdb_client_pnn(tctx->client), + tevent_timeval_zero(), + &request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_tunnel_destroy_deregister_done, + req); + + return req; +} + +static void ctdb_tunnel_destroy_deregister_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_tunnel_destroy_state *state = tevent_req_data( + req, struct ctdb_tunnel_destroy_state); + struct ctdb_client_context *client = state->tctx->client; + struct ctdb_reply_control *reply; + bool status; + int ret; + + status = ctdb_client_control_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + ret = ctdb_reply_control_tunnel_deregister(reply); + talloc_free(reply); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + ret = srvid_deregister(client->tunnels, state->tctx->tunnel_id, + state->tctx); + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_tunnel_destroy_recv(struct tevent_req *req, int *perr) +{ + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + return true; +} + + +int ctdb_tunnel_destroy(struct tevent_context *ev, + struct ctdb_tunnel_context *tctx) +{ + struct tevent_req *req; + int ret; + bool status; + + req = ctdb_tunnel_destroy_send(ev, ev, tctx); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_tunnel_destroy_recv(req, &ret); + talloc_free(req); + if (! status) { + return ret; + } + + return 0; +} + +/* + * Callback when REQ_TUNNEL packet is received + */ + +static void ctdb_tunnel_request_reply(struct tevent_req *req, + struct ctdb_tunnel_data *tunnel_data); + +void ctdb_client_req_tunnel(struct ctdb_client_context *client, + uint8_t *buf, size_t buflen, uint32_t reqid) +{ + TALLOC_CTX *tmp_ctx = talloc_new(client); + struct ctdb_req_header h; + struct ctdb_req_tunnel *tunnel; + struct tevent_req *req; + struct ctdb_tunnel_data tunnel_data; + int ret; + + tunnel = talloc_zero(tmp_ctx, struct ctdb_req_tunnel); + if (tunnel == NULL) { + goto fail; + } + + ret = ctdb_req_tunnel_pull(buf, buflen, &h, tmp_ctx, tunnel); + if (ret != 0) { + goto fail; + } + + tunnel_data = (struct ctdb_tunnel_data) { + .hdr = h, + .tunnel = tunnel, + .reqid = reqid, + }; + + if (tunnel->flags & CTDB_TUNNEL_FLAG_REPLY) { + req = reqid_find(client->idr, reqid, struct tevent_req); + if (req == NULL) { + goto fail; + } + + ctdb_tunnel_request_reply(req, &tunnel_data); + + } else if (tunnel->flags & CTDB_TUNNEL_FLAG_REQUEST) { + + TDB_DATA data = { + .dsize = sizeof(struct ctdb_tunnel_data), + .dptr = (uint8_t *)&tunnel_data, + }; + + srvid_dispatch(client->tunnels, tunnel->tunnel_id, 0, data); + } + +fail: + TALLOC_FREE(tmp_ctx); +} + + +/* + * Send messages using tunnel + */ + +struct ctdb_tunnel_request_state { + struct ctdb_tunnel_context *tctx; + bool wait_for_reply; + uint32_t reqid; + struct ctdb_req_tunnel *tunnel; +}; + +static int ctdb_tunnel_request_state_destructor( + struct ctdb_tunnel_request_state *state); +static void ctdb_tunnel_request_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_tunnel_request_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, + uint32_t destnode, + struct timeval timeout, + uint8_t *buf, size_t buflen, + bool wait_for_reply) +{ + struct tevent_req *req, *subreq; + struct ctdb_tunnel_request_state *state; + struct ctdb_req_tunnel tunnel; + struct ctdb_req_header h; + uint8_t *pkt; + size_t datalen, pkt_len; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_tunnel_request_state); + if (req == NULL) { + return NULL; + } + + state->tctx = tctx; + state->wait_for_reply = wait_for_reply; + state->reqid = reqid_new(tctx->client->idr, req); + if (state->reqid == REQID_INVALID) { + talloc_free(req); + return NULL; + } + + talloc_set_destructor(state, ctdb_tunnel_request_state_destructor); + + tunnel = (struct ctdb_req_tunnel) { + .tunnel_id = state->tctx->tunnel_id, + .flags = CTDB_TUNNEL_FLAG_REQUEST, + .data = (TDB_DATA) { + .dptr = buf, + .dsize = buflen, + }, + }; + + if (destnode == CTDB_BROADCAST_ALL || + destnode == CTDB_BROADCAST_ACTIVE || + destnode == CTDB_BROADCAST_CONNECTED) { + state->wait_for_reply = false; + } + if (! state->wait_for_reply) { + tunnel.flags |= CTDB_TUNNEL_FLAG_NOREPLY; + } + + ctdb_req_header_fill(&h, 0, CTDB_REQ_TUNNEL, destnode, + ctdb_client_pnn(state->tctx->client), + state->reqid); + + datalen = ctdb_req_tunnel_len(&h, &tunnel); + ret = ctdb_allocate_pkt(state, datalen, &pkt, &pkt_len); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = ctdb_req_tunnel_push(&h, &tunnel, pkt, &pkt_len); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + if (!tevent_timeval_is_zero(&timeout)) { + if (!tevent_req_set_endtime(req, ev, timeout)) { + return tevent_req_post(req, ev); + } + } + + subreq = comm_write_send(state, ev, tctx->client->comm, + pkt, pkt_len); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_tunnel_request_done, req); + + return req; +} + +static int ctdb_tunnel_request_state_destructor( + struct ctdb_tunnel_request_state *state) +{ + reqid_remove(state->tctx->client->idr, state->reqid); + return 0; +} + +static void ctdb_tunnel_request_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_tunnel_request_state *state = tevent_req_data( + req, struct ctdb_tunnel_request_state); + int ret; + bool status; + + status = comm_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + if (! state->wait_for_reply) { + tevent_req_done(req); + } + + /* Wait for the reply or timeout */ +} + +static void ctdb_tunnel_request_reply(struct tevent_req *req, + struct ctdb_tunnel_data *tunnel_data) +{ + struct ctdb_tunnel_request_state *state = tevent_req_data( + req, struct ctdb_tunnel_request_state); + + if (tunnel_data->reqid != state->reqid) { + return; + } + + state->tunnel = talloc_steal(state, tunnel_data->tunnel); + tevent_req_done(req); +} + +bool ctdb_tunnel_request_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, uint8_t **buf, + size_t *buflen) +{ + struct ctdb_tunnel_request_state *state = tevent_req_data( + req, struct ctdb_tunnel_request_state); + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (state->wait_for_reply) { + if (buf != NULL) { + *buf = talloc_steal(mem_ctx, state->tunnel->data.dptr); + } + if (buflen != NULL) { + *buflen = state->tunnel->data.dsize; + } + } + + return true; +} + +int ctdb_tunnel_request(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, uint32_t destnode, + struct timeval timeout, uint8_t *buf, size_t buflen, + bool wait_for_reply) +{ + struct tevent_req *req; + int ret; + bool status; + + req = ctdb_tunnel_request_send(mem_ctx, ev, tctx, destnode, + timeout, buf, buflen, wait_for_reply); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_tunnel_request_recv(req, &ret, NULL, NULL, NULL); + talloc_free(req); + if (! status) { + return ret; + } + + return 0; +} + +struct ctdb_tunnel_reply_state { +}; + +static void ctdb_tunnel_reply_done(struct tevent_req *subreq); + +struct tevent_req *ctdb_tunnel_reply_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, + uint32_t destnode, uint32_t reqid, + struct timeval timeout, + uint8_t *buf, size_t buflen) +{ + struct tevent_req *req, *subreq; + struct ctdb_tunnel_reply_state *state; + struct ctdb_req_tunnel tunnel; + struct ctdb_req_header h; + uint8_t *pkt; + size_t datalen, pkt_len; + int ret; + + req = tevent_req_create(mem_ctx, &state, + struct ctdb_tunnel_reply_state); + if (req == NULL) { + return NULL; + } + + tunnel = (struct ctdb_req_tunnel) { + .tunnel_id = tctx->tunnel_id, + .flags = CTDB_TUNNEL_FLAG_REPLY, + .data = (TDB_DATA) { + .dptr = buf, + .dsize = buflen, + }, + }; + + ctdb_req_header_fill(&h, 0, CTDB_REQ_TUNNEL, destnode, + ctdb_client_pnn(tctx->client), reqid); + + datalen = ctdb_req_tunnel_len(&h, &tunnel); + ret = ctdb_allocate_pkt(state, datalen, &pkt, &pkt_len); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + ret = ctdb_req_tunnel_push(&h, &tunnel, pkt, &pkt_len); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + if (!tevent_timeval_is_zero(&timeout)) { + if (!tevent_req_set_endtime(req, ev, timeout)) { + return tevent_req_post(req, ev); + } + } + + subreq = comm_write_send(state, ev, tctx->client->comm, pkt, pkt_len); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_tunnel_reply_done, req); + + return req; +} + +static void ctdb_tunnel_reply_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + int ret; + bool status; + + status = comm_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool ctdb_tunnel_reply_recv(struct tevent_req *req, int *perr) +{ + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + return true; +} + +int ctdb_tunnel_reply(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_tunnel_context *tctx, uint32_t destnode, + uint32_t reqid, struct timeval timeout, + uint8_t *buf, size_t buflen) +{ + struct tevent_req *req; + int ret; + bool status; + + req = ctdb_tunnel_reply_send(mem_ctx, ev, tctx, destnode, reqid, + timeout, buf, buflen); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = ctdb_tunnel_reply_recv(req, &ret); + talloc_free(req); + if (! status) { + return ret; + } + + return 0; +} diff --git a/ctdb/client/client_util.c b/ctdb/client/client_util.c new file mode 100644 index 0000000..35323ff --- /dev/null +++ b/ctdb/client/client_util.c @@ -0,0 +1,137 @@ +/* + CTDB client code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "common/logging.h" + +#include "lib/util/debug.h" + +#include "protocol/protocol.h" +#include "protocol/protocol_api.h" +#include "client/client_private.h" +#include "client/client.h" +#include "client/client_sync.h" + +int list_of_nodes(struct ctdb_node_map *nodemap, + uint32_t flags_mask, uint32_t exclude_pnn, + TALLOC_CTX *mem_ctx, uint32_t **pnn_list) +{ + int num_nodes = 0; + uint32_t *list; + unsigned int i; + + /* Allocate the list of same number of nodes */ + list = talloc_array(mem_ctx, uint32_t, nodemap->num); + if (list == NULL) { + return -1; + } + + for (i=0; inum; i++) { + if (nodemap->node[i].flags & flags_mask) { + continue; + } + if (nodemap->node[i].pnn == exclude_pnn) { + continue; + } + list[num_nodes] = nodemap->node[i].pnn; + num_nodes++; + } + + *pnn_list = list; + return num_nodes; +} + +int list_of_active_nodes(struct ctdb_node_map *nodemap, uint32_t exclude_pnn, + TALLOC_CTX *mem_ctx, uint32_t **pnn_list) +{ + return list_of_nodes(nodemap, NODE_FLAGS_INACTIVE, exclude_pnn, + mem_ctx, pnn_list); +} + +int list_of_connected_nodes(struct ctdb_node_map *nodemap, + uint32_t exclude_pnn, + TALLOC_CTX *mem_ctx, uint32_t **pnn_list) +{ + return list_of_nodes(nodemap, NODE_FLAGS_DISCONNECTED, exclude_pnn, + mem_ctx, pnn_list); +} + +struct ctdb_server_id ctdb_client_get_server_id( + struct ctdb_client_context *client, + uint32_t task_id) +{ + struct ctdb_server_id sid; + + sid.pid = getpid(); + sid.task_id = task_id; + sid.vnn = ctdb_client_pnn(client); + sid.unique_id = task_id; + sid.unique_id = (sid.unique_id << 32) | sid.pid; + + return sid; +} + +bool ctdb_server_id_equal(struct ctdb_server_id *sid1, + struct ctdb_server_id *sid2) +{ + if (sid1->pid != sid2->pid) { + return false; + } + if (sid1->task_id != sid2->task_id) { + return false; + } + if (sid1->vnn != sid2->vnn) { + return false; + } + if (sid1->unique_id != sid2->unique_id) { + return false; + } + + return true; +} + +int ctdb_server_id_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct ctdb_client_context *client, + struct ctdb_server_id *sid, bool *exists) +{ + int result; + int ret; + + ret = ctdb_ctrl_process_exists(mem_ctx, ev, client, sid->vnn, + tevent_timeval_zero(), + sid->pid, &result); + if (ret != 0) { + return ret; + } + + if (result == 1) { + *exists = true; + } else { + *exists = false; + } + + return 0; +} diff --git a/ctdb/cluster/cluster_conf.c b/ctdb/cluster/cluster_conf.c new file mode 100644 index 0000000..bdd64ba --- /dev/null +++ b/ctdb/cluster/cluster_conf.c @@ -0,0 +1,182 @@ +/* + CTDB cluster config handling + + Copyright (C) Martin Schwenke 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" + +#include "lib/util/debug.h" + +#include "common/conf.h" + +#include "cluster_conf.h" + +#define CLUSTER_TRANSPORT_DEFAULT "tcp" + +/* + * Ideally this wants to be a void function but it also used directly + * as a validation function + */ +static bool check_static_string_change(const char *key, + const char *old_value, + const char *new_value, + enum conf_update_mode mode) +{ + if (mode == CONF_MODE_RELOAD) { + if (old_value == new_value) { + goto done; + } + + /* + * At this point old_value or new_value can not both + * NULL, so if one is NULL then they are different + */ + if (old_value == NULL || + new_value == NULL || + strcmp(old_value, new_value) != 0) { + D_WARNING("Ignoring update of [%s] -> %s\n", + CLUSTER_CONF_SECTION, + key); + } + } + +done: + return true; +} + +static bool validate_transport(const char *key, + const char *old_transport, + const char *new_transport, + enum conf_update_mode mode) +{ + /* Don't allow "ib" for now. It is broken! */ + if (strcmp(new_transport, CLUSTER_TRANSPORT_DEFAULT) != 0) { + D_ERR("Invalid value for [cluster] -> transport = %s\n", + new_transport); + return false; + } + + /* This sometimes warns but always returns true */ + return check_static_string_change(key, + old_transport, + new_transport, + mode); +} + +static bool validate_node_address(const char *key, + const char *old_node_address, + const char *new_node_address, + enum conf_update_mode mode) +{ + struct in_addr addr4; + struct in6_addr addr6; + int ret; + + if (new_node_address == NULL) { + goto good; + } + + ret = inet_pton(AF_INET, new_node_address, &addr4); + if (ret == 1) { + goto good; + } + + ret = inet_pton(AF_INET6, new_node_address, &addr6); + if (ret == 1) { + goto good; + } + + D_ERR("Invalid value for [cluster] -> node address = %s\n", + new_node_address); + return false; + +good: + /* This sometimes warns but always returns true */ + return check_static_string_change(key, + old_node_address, + new_node_address, + mode); +} + +static bool validate_recovery_lock(const char *key, + const char *old_reclock, + const char *new_reclock, + enum conf_update_mode mode) +{ + bool status; + + if (new_reclock != NULL) { + D_WARNING("Configuration option [%s] -> %s is deprecated\n", + CLUSTER_CONF_SECTION, + key); + } + + status = check_static_string_change(key, old_reclock, new_reclock, mode); + + return status; +} + +static bool validate_leader_timeout(const char *key, + int old_timeout, + int new_timeout, + enum conf_update_mode mode) +{ + if (new_timeout <= 0) { + D_ERR("Invalid value for [cluster] -> leader timeout = %d\n", + new_timeout); + return false; + } + + return true; +} + +void cluster_conf_init(struct conf_context *conf) +{ + conf_define_section(conf, CLUSTER_CONF_SECTION, NULL); + + conf_define_string(conf, + CLUSTER_CONF_SECTION, + CLUSTER_CONF_TRANSPORT, + CLUSTER_TRANSPORT_DEFAULT, + validate_transport); + conf_define_string(conf, + CLUSTER_CONF_SECTION, + CLUSTER_CONF_NODE_ADDRESS, + NULL, + validate_node_address); + conf_define_string(conf, + CLUSTER_CONF_SECTION, + CLUSTER_CONF_CLUSTER_LOCK, + NULL, + check_static_string_change); + conf_define_string(conf, + CLUSTER_CONF_SECTION, + CLUSTER_CONF_RECOVERY_LOCK, + NULL, + validate_recovery_lock); + conf_define_integer(conf, + CLUSTER_CONF_SECTION, + CLUSTER_CONF_LEADER_TIMEOUT, + 5, + validate_leader_timeout); + conf_define_boolean(conf, + CLUSTER_CONF_SECTION, + CLUSTER_CONF_LEADER_CAPABILITY, + true, + NULL); +} diff --git a/ctdb/cluster/cluster_conf.h b/ctdb/cluster/cluster_conf.h new file mode 100644 index 0000000..38c378f --- /dev/null +++ b/ctdb/cluster/cluster_conf.h @@ -0,0 +1,36 @@ +/* + CTDB cluster config handling + + Copyright (C) Martin Schwenke 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CLUSTER_CONF_H__ +#define __CTDB_CLUSTER_CONF_H__ + +#include "common/conf.h" + +#define CLUSTER_CONF_SECTION "cluster" + +#define CLUSTER_CONF_TRANSPORT "transport" +#define CLUSTER_CONF_NODE_ADDRESS "node address" +#define CLUSTER_CONF_CLUSTER_LOCK "cluster lock" +#define CLUSTER_CONF_RECOVERY_LOCK "recovery lock" +#define CLUSTER_CONF_LEADER_TIMEOUT "leader timeout" +#define CLUSTER_CONF_LEADER_CAPABILITY "leader capability" + +void cluster_conf_init(struct conf_context *conf); + +#endif /* __CTDB_CLUSTER_CONF_H__ */ diff --git a/ctdb/common/cmdline.c b/ctdb/common/cmdline.c new file mode 100644 index 0000000..ce368a9 --- /dev/null +++ b/ctdb/common/cmdline.c @@ -0,0 +1,598 @@ +/* + Command line processing + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include +#include +#include + +#include "lib/util/debug.h" + +#include "common/cmdline.h" + +#define CMDLINE_MAX_LEN 80 + +struct cmdline_section { + const char *name; + struct cmdline_command *commands; +}; + +struct cmdline_context { + const char *prog; + struct poptOption *options; + struct cmdline_section *section; + int num_sections; + size_t max_len; + poptContext pc; + int argc, arg0; + const char **argv; + struct cmdline_command *match_cmd; +}; + +static bool cmdline_show_help = false; + +static void cmdline_popt_help(poptContext pc, + enum poptCallbackReason reason, + struct poptOption *key, + const char *arg, + void *data) +{ + if (key->shortName == 'h') { + cmdline_show_help = true; + } +} + +struct poptOption cmdline_help_options[] = { + { NULL, '\0', POPT_ARG_CALLBACK, cmdline_popt_help, 0, NULL, NULL }, + { "help", 'h', 0, NULL, 'h', "Show this help message", NULL }, + POPT_TABLEEND +}; + +#define CMDLINE_HELP_OPTIONS \ + { NULL, '\0', POPT_ARG_INCLUDE_TABLE, cmdline_help_options, \ + 0, "Help Options:", NULL } + +static bool cmdline_option_check(struct poptOption *option) +{ + if (option->longName == NULL) { + D_ERR("Option has no long name\n"); + return false; + } + + if (option->argInfo != POPT_ARG_STRING && + option->argInfo != POPT_ARG_INT && + option->argInfo != POPT_ARG_LONG && + option->argInfo != POPT_ARG_VAL && + option->argInfo != POPT_ARG_FLOAT && + option->argInfo != POPT_ARG_DOUBLE) { + D_ERR("Option '%s' has unsupported type\n", option->longName); + return false; + } + + if (option->arg == NULL) { + D_ERR("Option '%s' has invalid arg\n", option->longName); + return false; + } + + if (option->descrip == NULL) { + D_ERR("Option '%s' has no help msg\n", option->longName); + return false; + } + + return true; +} + +static bool cmdline_options_check(struct poptOption *options) +{ + int i; + bool ok; + + if (options == NULL) { + return true; + } + + i = 0; + while (options[i].longName != NULL || options[i].shortName != '\0') { + ok = cmdline_option_check(&options[i]); + if (!ok) { + return false; + } + i++; + } + + return true; +} + +static int cmdline_options_define(TALLOC_CTX *mem_ctx, + struct poptOption *user_options, + struct poptOption **result) +{ + struct poptOption *options; + int count, i; + + count = (user_options == NULL ? 2 : 3); + + options = talloc_array(mem_ctx, struct poptOption, count); + if (options == NULL) { + return ENOMEM; + } + + i = 0; + options[i++] = (struct poptOption) CMDLINE_HELP_OPTIONS; + if (user_options != NULL) { + options[i++] = (struct poptOption) { + .argInfo = POPT_ARG_INCLUDE_TABLE, + .arg = user_options, + .descrip = "Options:", + }; + } + options[i++] = (struct poptOption) POPT_TABLEEND; + + *result = options; + return 0; +} + +static bool cmdline_command_check(struct cmdline_command *cmd, size_t *max_len) +{ + size_t len; + + if (cmd->name == NULL) { + return false; + } + + if (cmd->fn == NULL) { + D_ERR("Command '%s' has no implementation function\n", + cmd->name); + return false; + } + + if (cmd->msg_help == NULL) { + D_ERR("Command '%s' has no help msg\n", cmd->name); + return false; + } + + len = strlen(cmd->name); + if (cmd->msg_args != NULL) { + len += strlen(cmd->msg_args); + } + if (len > CMDLINE_MAX_LEN) { + D_ERR("Command '%s' is too long (%zu)\n", cmd->name, len); + return false; + } + + if (len > *max_len) { + *max_len = len; + } + + len = strlen(cmd->msg_help); + if (len > CMDLINE_MAX_LEN) { + D_ERR("Command '%s' help too long (%zu)\n", cmd->name, len); + return false; + } + + return true; +} + +static bool cmdline_commands_check(struct cmdline_command *commands, + size_t *max_len) +{ + int i; + bool ok; + + if (commands == NULL) { + return false; + } + + for (i=0; commands[i].name != NULL; i++) { + ok = cmdline_command_check(&commands[i], max_len); + if (!ok) { + return false; + } + } + + return true; +} + +static int cmdline_context_destructor(struct cmdline_context *cmdline); + +static int cmdline_section_add(struct cmdline_context *cmdline, + const char *name, + struct cmdline_command *commands) +{ + struct cmdline_section *section; + size_t max_len = 0; + bool ok; + + ok = cmdline_commands_check(commands, &max_len); + if (!ok) { + return EINVAL; + } + + section = talloc_realloc(cmdline, + cmdline->section, + struct cmdline_section, + cmdline->num_sections + 1); + if (section == NULL) { + return ENOMEM; + } + + section[cmdline->num_sections] = (struct cmdline_section) { + .name = name, + .commands = commands, + }; + + if (max_len > cmdline->max_len) { + cmdline->max_len = max_len; + } + + cmdline->section = section; + cmdline->num_sections += 1; + + return 0; +} + +int cmdline_init(TALLOC_CTX *mem_ctx, + const char *prog, + struct poptOption *options, + const char *name, + struct cmdline_command *commands, + struct cmdline_context **result) +{ + struct cmdline_context *cmdline; + int ret; + bool ok; + + if (prog == NULL) { + return EINVAL; + } + + ok = cmdline_options_check(options); + if (!ok) { + return EINVAL; + } + + cmdline = talloc_zero(mem_ctx, struct cmdline_context); + if (cmdline == NULL) { + return ENOMEM; + } + + cmdline->prog = talloc_strdup(cmdline, prog); + if (cmdline->prog == NULL) { + talloc_free(cmdline); + return ENOMEM; + } + + ret = cmdline_options_define(cmdline, options, &cmdline->options); + if (ret != 0) { + talloc_free(cmdline); + return ret; + } + + ret = cmdline_section_add(cmdline, name, commands); + if (ret != 0) { + talloc_free(cmdline); + return ret; + } + + cmdline->argc = 1; + cmdline->argv = talloc_array(cmdline, const char *, 2); + if (cmdline->argv == NULL) { + talloc_free(cmdline); + return ENOMEM; + } + cmdline->argv[0] = cmdline->prog; + cmdline->argv[1] = NULL; + + /* Dummy popt context for generating help */ + cmdline->pc = poptGetContext(cmdline->prog, + cmdline->argc, + cmdline->argv, + cmdline->options, + 0); + if (cmdline->pc == NULL) { + talloc_free(cmdline); + return ENOMEM; + } + + talloc_set_destructor(cmdline, cmdline_context_destructor); + + *result = cmdline; + return 0; +} + +static int cmdline_context_destructor(struct cmdline_context *cmdline) +{ + if (cmdline->pc != NULL) { + poptFreeContext(cmdline->pc); + } + + return 0; +} + +int cmdline_add(struct cmdline_context *cmdline, + const char *name, + struct cmdline_command *commands) +{ + return cmdline_section_add(cmdline, name, commands); +} + +static int cmdline_parse_options(struct cmdline_context *cmdline, + int argc, + const char **argv) +{ + int opt; + + if (cmdline->pc != NULL) { + poptFreeContext(cmdline->pc); + } + + cmdline->pc = poptGetContext(cmdline->prog, + argc, + argv, + cmdline->options, + 0); + if (cmdline->pc == NULL) { + return ENOMEM; + } + + while ((opt = poptGetNextOpt(cmdline->pc)) != -1) { + D_ERR("Invalid option %s: %s\n", + poptBadOption(cmdline->pc, 0), + poptStrerror(opt)); + return EINVAL; + } + + /* Set up remaining arguments for commands */ + cmdline->argc = 0; + cmdline->argv = poptGetArgs(cmdline->pc); + if (cmdline->argv != NULL) { + while (cmdline->argv[cmdline->argc] != NULL) { + cmdline->argc++; + } + } + + return 0; +} + +static int cmdline_match_section(struct cmdline_context *cmdline, + struct cmdline_section *section) +{ + int i; + + for (i=0; section->commands[i].name != NULL; i++) { + struct cmdline_command *cmd; + char name[CMDLINE_MAX_LEN+1]; + size_t len; + char *t, *str; + int n = 0; + bool match = false; + + cmd = §ion->commands[i]; + len = strlcpy(name, cmd->name, sizeof(name)); + if (len >= sizeof(name)) { + D_ERR("Skipping long command '%s'\n", cmd->name); + continue; + } + + str = name; + while ((t = strtok(str, " ")) != NULL) { + if (n >= cmdline->argc) { + match = false; + break; + } + if (cmdline->argv[n] == NULL) { + match = false; + break; + } + if (strcmp(cmdline->argv[n], t) == 0) { + match = true; + cmdline->arg0 = n+1; + } else { + match = false; + break; + } + + n += 1; + str = NULL; + } + + if (match) { + cmdline->match_cmd = cmd; + return 0; + } + } + + cmdline->match_cmd = NULL; + return ENOENT; +} + +static int cmdline_match(struct cmdline_context *cmdline) +{ + int i, ret = ENOENT; + + if (cmdline->argc == 0 || cmdline->argv == NULL) { + cmdline->match_cmd = NULL; + return EINVAL; + } + + for (i=0; inum_sections; i++) { + ret = cmdline_match_section(cmdline, &cmdline->section[i]); + if (ret == 0) { + break; + } + } + + return ret; +} + +int cmdline_parse(struct cmdline_context *cmdline, + int argc, + const char **argv, + bool parse_options) +{ + int ret; + + if (argc < 2) { + cmdline_usage(cmdline, NULL); + return EINVAL; + } + + cmdline_show_help = false; + + if (parse_options) { + ret = cmdline_parse_options(cmdline, argc, argv); + if (ret != 0) { + cmdline_usage(cmdline, NULL); + return ret; + } + } else { + cmdline->argc = argc; + cmdline->argv = argv; + } + + ret = cmdline_match(cmdline); + + if (ret != 0 || cmdline_show_help) { + const char *name = NULL; + + if (cmdline->match_cmd != NULL) { + name = cmdline->match_cmd->name; + } + + cmdline_usage(cmdline, name); + + if (cmdline_show_help) { + ret = EAGAIN; + } + } + + return ret; +} + +static void cmdline_usage_command(struct cmdline_context *cmdline, + struct cmdline_command *cmd, + bool print_all) +{ + size_t len; + + len = strlen(cmd->name); + + printf(" %s ", cmd->name); + if (print_all) { + printf("%-*s", + (int)(cmdline->max_len-len), + cmd->msg_args == NULL ? "" : cmd->msg_args); + } else { + printf("%s", cmd->msg_args == NULL ? "" : cmd->msg_args); + } + printf(" %s\n", cmd->msg_help); +} + +static void cmdline_usage_section(struct cmdline_context *cmdline, + struct cmdline_section *section) +{ + int i; + + printf("\n"); + + if (section->name != NULL) { + printf("%s ", section->name); + } + printf("Commands:\n"); + for (i=0; section->commands[i].name != NULL; i++) { + cmdline_usage_command(cmdline, §ion->commands[i], true); + + } +} + +static void cmdline_usage_full(struct cmdline_context *cmdline) +{ + int i; + + poptSetOtherOptionHelp(cmdline->pc, "[] []"); + poptPrintHelp(cmdline->pc, stdout, 0); + + for (i=0; inum_sections; i++) { + cmdline_usage_section(cmdline, &cmdline->section[i]); + } +} + +void cmdline_usage(struct cmdline_context *cmdline, const char *cmd_name) +{ + struct cmdline_command *cmd = NULL; + int i, j; + + if (cmd_name == NULL) { + cmdline_usage_full(cmdline); + return; + } + + for (j=0; jnum_sections; j++) { + struct cmdline_section *section = &cmdline->section[j]; + + for (i=0; section->commands[i].name != NULL; i++) { + if (strcmp(section->commands[i].name, cmd_name) == 0) { + cmd = §ion->commands[i]; + break; + } + } + } + + if (cmd == NULL) { + cmdline_usage_full(cmdline); + return; + } + + poptSetOtherOptionHelp(cmdline->pc, " []"); + poptPrintUsage(cmdline->pc, stdout, 0); + + printf("\n"); + cmdline_usage_command(cmdline, cmd, false); +} + +int cmdline_run(struct cmdline_context *cmdline, + void *private_data, + int *result) +{ + struct cmdline_command *cmd = cmdline->match_cmd; + TALLOC_CTX *tmp_ctx; + int ret; + + if (cmd == NULL) { + return ENOENT; + } + + tmp_ctx = talloc_new(cmdline); + if (tmp_ctx == NULL) { + return ENOMEM; + } + + ret = cmd->fn(tmp_ctx, + cmdline->argc - cmdline->arg0, + &cmdline->argv[cmdline->arg0], + private_data); + + talloc_free(tmp_ctx); + + if (result != NULL) { + *result = ret; + } + return 0; +} diff --git a/ctdb/common/cmdline.h b/ctdb/common/cmdline.h new file mode 100644 index 0000000..b9a128c --- /dev/null +++ b/ctdb/common/cmdline.h @@ -0,0 +1,163 @@ +/* + Command line processing + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CMDLINE_H__ +#define __CTDB_CMDLINE_H__ + +#include +#include + +/** + * @file cmdline.h + * + * @brief Command-line handling with options and commands + * + * This abstraction encapsulates the boiler-plate for parsing options, + * commands and arguments on command-line. + * + * Options handling is done using popt. + */ + +/** + * @brief Abstract data structure holding command-line configuration + */ +struct cmdline_context; + +/** + * @brief A command definition structure + * + * @name is the name of the command + * @fn is the implementation of the command + * @msg_help is the help message describing command + * @msg_args is the help message describing arguments + * + * A command name can be a single word or multiple words separated with spaces. + * + * An implementation function should return 0 on success and non-zero value + * on failure. This value is returned as result in @cmdline_run. + */ +struct cmdline_command { + const char *name; + int (*fn)(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data); + const char *msg_help; + const char *msg_args; +}; + +/** + * @brief convinience macro to define the end of commands list + * + * Here is an example of defining commands list. + * + * struct cmdline_command commands[] = { + * { "command1", command1_func, "Run command1", NULL }, + * { "command2", command2_func, "Run command2", "" }, + * CMDLINE_TABLEEND + * }; + */ +#define CMDLINE_TABLEEND { NULL, NULL, NULL, NULL } + +/** + * @brief Initialize cmdline abstraction + * + * If there are no options, options can be NULL. + * + * Help options (--help, -h) are automatically added to the options. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] prog Program name + * @param[in] options Command-line options + * @param[in] section Name of section grouping specified commands + * @param[in] commands Commands array + * @param[out] result New cmdline context + * @return 0 on success, errno on failure + * + * Freeing cmdline context will free up all the resources. + */ +int cmdline_init(TALLOC_CTX *mem_ctx, + const char *prog, + struct poptOption *options, + const char *section, + struct cmdline_command *commands, + struct cmdline_context **result); + + +/** + * @brief Add command line section/commands + * + * @param[in] cmdline Cmdline context + * @param[in] section Name of section grouping specified commands + * @param[in] commands Commands array + * @return 0 on success, errno on failure + */ +int cmdline_add(struct cmdline_context *cmdline, + const char *section, + struct cmdline_command *commands); + +/** + * @brief Parse command line options and commands/arguments + * + * This function parses the arguments to process options and commands. + * + * This function should be passed the arguments to main() and parse_options + * should be set to true. If cmdline is used for handling second-level + * commands, then parse_options should be set to false. + * + * If argv does not match any command, then ENOENT is returned. + * + * @param[in] cmdline Cmdline context + * @param[in] argc Number of arguments + * @param[in] argv Arguments array + * @param[in] parse_options Whether to parse for options + * @return 0 on success, errno on failure + */ +int cmdline_parse(struct cmdline_context *cmdline, + int argc, + const char **argv, + bool parse_options); + +/** + * @brief Excecute the function for the command matched by @cmdline_parse + * + * @param[in] cmdline Cmdline context + * @param[in] private_data Private data for implementation function + * @param[out] result Return value from the implementation function + * @return 0 on success, errno on failure + * + * If help options are specified, then detailed help will be printed and + * the return value will be EAGAIN. + */ +int cmdline_run(struct cmdline_context *cmdline, + void *private_data, + int *result); + +/** + * @brief Print usage help message to stdout + * + * @param[in] cmdline Cmdline context + * @param[in] command Command string + * + * If command is NULL, then full help is printed. + * If command is specified, then compact help is printed. + */ +void cmdline_usage(struct cmdline_context *cmdline, const char *command); + +#endif /* __CTDB_CMDLINE_H__ */ diff --git a/ctdb/common/comm.c b/ctdb/common/comm.c new file mode 100644 index 0000000..12f4970 --- /dev/null +++ b/ctdb/common/comm.c @@ -0,0 +1,427 @@ +/* + Communication endpoint implementation + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include + +#include "lib/util/blocking.h" +#include "lib/util/tevent_unix.h" + +#include "pkt_read.h" +#include "pkt_write.h" +#include "comm.h" + +/* + * Communication endpoint around a socket + */ + +#define SMALL_PKT_SIZE 1024 + +struct comm_context { + int fd; + comm_read_handler_fn read_handler; + void *read_private_data; + comm_dead_handler_fn dead_handler; + void *dead_private_data; + uint8_t small_pkt[SMALL_PKT_SIZE]; + struct tevent_req *read_req, *write_req; + struct tevent_fd *fde; + struct tevent_queue *queue; +}; + +static void comm_fd_handler(struct tevent_context *ev, + struct tevent_fd *fde, + uint16_t flags, void *private_data); +static struct tevent_req *comm_read_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct comm_context *comm, + uint8_t *buf, size_t buflen); +static void comm_read_failed(struct tevent_req *req); + + +int comm_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd, + comm_read_handler_fn read_handler, void *read_private_data, + comm_dead_handler_fn dead_handler, void *dead_private_data, + struct comm_context **result) +{ + struct comm_context *comm; + int ret; + + if (fd < 0) { + return EINVAL; + } + + if (dead_handler == NULL) { + return EINVAL; + } + + /* Socket queue relies on non-blocking sockets. */ + ret = set_blocking(fd, false); + if (ret == -1) { + return EIO; + } + + comm = talloc_zero(mem_ctx, struct comm_context); + if (comm == NULL) { + return ENOMEM; + } + + comm->fd = fd; + comm->read_handler = read_handler; + comm->read_private_data = read_private_data; + comm->dead_handler = dead_handler; + comm->dead_private_data = dead_private_data; + + comm->queue = tevent_queue_create(comm, "comm write queue"); + if (comm->queue == NULL) { + goto fail; + } + + /* Set up to write packets */ + comm->fde = tevent_add_fd(ev, comm, fd, TEVENT_FD_READ, + comm_fd_handler, comm); + if (comm->fde == NULL) { + goto fail; + } + + /* Set up to read packets */ + if (read_handler != NULL) { + struct tevent_req *req; + + req = comm_read_send(comm, ev, comm, comm->small_pkt, + SMALL_PKT_SIZE); + if (req == NULL) { + goto fail; + } + + tevent_req_set_callback(req, comm_read_failed, comm); + comm->read_req = req; + } + + *result = comm; + return 0; + +fail: + talloc_free(comm); + return ENOMEM; +} + + +/* + * Read packets + */ + +struct comm_read_state { + struct tevent_context *ev; + struct comm_context *comm; + uint8_t *buf; + size_t buflen; + struct tevent_req *subreq; +}; + +static ssize_t comm_read_more(uint8_t *buf, size_t buflen, void *private_data); +static void comm_read_done(struct tevent_req *subreq); + +static struct tevent_req *comm_read_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct comm_context *comm, + uint8_t *buf, size_t buflen) +{ + struct tevent_req *req, *subreq; + struct comm_read_state *state; + + req = tevent_req_create(mem_ctx, &state, struct comm_read_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->comm = comm; + state->buf = buf; + state->buflen = buflen; + + subreq = pkt_read_send(state, state->ev, comm->fd, sizeof(uint32_t), + state->buf, state->buflen, + comm_read_more, NULL); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + state->subreq = subreq; + + tevent_req_set_callback(subreq, comm_read_done, req); + return req; +} + +static ssize_t comm_read_more(uint8_t *buf, size_t buflen, void *private_data) +{ + uint32_t packet_len; + + if (buflen < sizeof(uint32_t)) { + return sizeof(uint32_t) - buflen; + } + + packet_len = *(uint32_t *)buf; + + return packet_len - buflen; +} + +static void comm_read_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct comm_read_state *state = tevent_req_data( + req, struct comm_read_state); + struct comm_context *comm = state->comm; + ssize_t nread; + uint8_t *buf; + bool free_buf; + int err = 0; + + nread = pkt_read_recv(subreq, state, &buf, &free_buf, &err); + TALLOC_FREE(subreq); + state->subreq = NULL; + if (nread == -1) { + tevent_req_error(req, err); + return; + } + + comm->read_handler(buf, nread, comm->read_private_data); + + if (free_buf) { + talloc_free(buf); + } + + subreq = pkt_read_send(state, state->ev, comm->fd, sizeof(uint32_t), + state->buf, state->buflen, + comm_read_more, NULL); + if (tevent_req_nomem(subreq, req)) { + return; + } + state->subreq = subreq; + + tevent_req_set_callback(subreq, comm_read_done, req); +} + +static void comm_read_recv(struct tevent_req *req, int *perr) +{ + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + } +} + +static void comm_read_failed(struct tevent_req *req) +{ + struct comm_context *comm = tevent_req_callback_data( + req, struct comm_context); + + comm_read_recv(req, NULL); + TALLOC_FREE(req); + comm->read_req = NULL; + if (comm->dead_handler != NULL) { + comm->dead_handler(comm->dead_private_data); + } +} + + +/* + * Write packets + */ + +struct comm_write_entry { + struct comm_context *comm; + struct tevent_queue_entry *qentry; + struct tevent_req *req; +}; + +struct comm_write_state { + struct tevent_context *ev; + struct comm_context *comm; + struct comm_write_entry *entry; + struct tevent_req *subreq; + uint8_t *buf; + size_t buflen, nwritten; +}; + +static int comm_write_entry_destructor(struct comm_write_entry *entry); +static void comm_write_trigger(struct tevent_req *req, void *private_data); +static void comm_write_done(struct tevent_req *subreq); + +struct tevent_req *comm_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct comm_context *comm, + uint8_t *buf, size_t buflen) +{ + struct tevent_req *req; + struct comm_write_state *state; + struct comm_write_entry *entry; + + req = tevent_req_create(mem_ctx, &state, struct comm_write_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->comm = comm; + state->buf = buf; + state->buflen = buflen; + + entry = talloc_zero(state, struct comm_write_entry); + if (tevent_req_nomem(entry, req)) { + return tevent_req_post(req, ev); + } + + entry->comm = comm; + entry->req = req; + entry->qentry = tevent_queue_add_entry(comm->queue, ev, req, + comm_write_trigger, NULL); + if (tevent_req_nomem(entry->qentry, req)) { + return tevent_req_post(req, ev); + } + + state->entry = entry; + talloc_set_destructor(entry, comm_write_entry_destructor); + + return req; +} + +static int comm_write_entry_destructor(struct comm_write_entry *entry) +{ + struct comm_context *comm = entry->comm; + + if (comm->write_req == entry->req) { + comm->write_req = NULL; + TEVENT_FD_NOT_WRITEABLE(comm->fde); + } + + TALLOC_FREE(entry->qentry); + return 0; +} + +static void comm_write_trigger(struct tevent_req *req, void *private_data) +{ + struct comm_write_state *state = tevent_req_data( + req, struct comm_write_state); + struct comm_context *comm = state->comm; + struct tevent_req *subreq; + + comm->write_req = req; + + subreq = pkt_write_send(state, state->ev, comm->fd, + state->buf, state->buflen); + if (tevent_req_nomem(subreq, req)) { + return; + } + + state->subreq = subreq; + tevent_req_set_callback(subreq, comm_write_done, req); + TEVENT_FD_WRITEABLE(comm->fde); +} + +static void comm_write_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct comm_write_state *state = tevent_req_data( + req, struct comm_write_state); + struct comm_context *comm = state->comm; + ssize_t nwritten; + int err = 0; + + TEVENT_FD_NOT_WRITEABLE(comm->fde); + nwritten = pkt_write_recv(subreq, &err); + TALLOC_FREE(subreq); + state->subreq = NULL; + comm->write_req = NULL; + if (nwritten == -1) { + if (err == EPIPE) { + comm->dead_handler(comm->dead_private_data); + } + tevent_req_error(req, err); + return; + } + + state->nwritten = nwritten; + state->entry->qentry = NULL; + TALLOC_FREE(state->entry); + tevent_req_done(req); +} + +bool comm_write_recv(struct tevent_req *req, int *perr) +{ + struct comm_write_state *state = tevent_req_data( + req, struct comm_write_state); + int err; + + if (tevent_req_is_unix_error(req, &err)) { + if (perr != NULL) { + *perr = err; + } + return false; + } + + if (state->nwritten != state->buflen) { + *perr = EIO; + return false; + } + + *perr = 0; + return true; +} + +static void comm_fd_handler(struct tevent_context *ev, + struct tevent_fd *fde, + uint16_t flags, void *private_data) +{ + struct comm_context *comm = talloc_get_type_abort( + private_data, struct comm_context); + + if (flags & TEVENT_FD_READ) { + struct comm_read_state *read_state; + + if (comm->read_req == NULL) { + /* This should never happen */ + abort(); + } + + read_state = tevent_req_data(comm->read_req, + struct comm_read_state); + pkt_read_handler(ev, fde, flags, read_state->subreq); + } + + if (flags & TEVENT_FD_WRITE) { + struct comm_write_state *write_state; + + if (comm->write_req == NULL) { + TEVENT_FD_NOT_WRITEABLE(comm->fde); + return; + } + + write_state = tevent_req_data(comm->write_req, + struct comm_write_state); + pkt_write_handler(ev, fde, flags, write_state->subreq); + } +} diff --git a/ctdb/common/comm.h b/ctdb/common/comm.h new file mode 100644 index 0000000..e11d38e --- /dev/null +++ b/ctdb/common/comm.h @@ -0,0 +1,101 @@ +/* + Communication endpoint API + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_COMM_H__ +#define __CTDB_COMM_H__ + +#include +#include + +/** + * @file comm.h + * + * @brief Communication over a socket or file descriptor + * + * This abstraction is a wrapper around a socket or file descriptor to + * send/receive complete packets. + */ + +/** + * @brief Packet handler function + * + * This function is registered while setting up communication endpoint. Any + * time packets are read, this function is called. + */ +typedef void (*comm_read_handler_fn)(uint8_t *buf, size_t buflen, + void *private_data); + +/** + * @brief Communication endpoint dead handler function + * + * This function is called when the communication endpoint is closed. + */ +typedef void (*comm_dead_handler_fn)(void *private_data); + +/** + * @brief Abstract struct to store communication endpoint details + */ +struct comm_context; + +/** + * @brief Initialize the communication endpoint + * + * This return a new communication context. Freeing this context will free all + * memory associated with it. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] fd The socket or file descriptor + * @param[in] read_handler The packet handler function + * @param[in] read_private_data Private data for read handler function + * @param[in] dead_handler The communication dead handler function + * @param[in] dead_private_data Private data for dead handler function + * @param[out] result The new comm_context structure + * @return 0 on success, errno on failure + */ +int comm_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd, + comm_read_handler_fn read_handler, void *read_private_data, + comm_dead_handler_fn dead_handler, void *dead_private_data, + struct comm_context **result); + +/** + * @brief Async computation start to send a packet + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] comm Communication context + * @param[in] buf The packet data + * @param[in] buflen The size of the packet + * @return new tevent request, or NULL on failure + */ +struct tevent_req *comm_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct comm_context *comm, + uint8_t *buf, size_t buflen); + +/** + * @brief Async computation end to send a packet + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool comm_write_recv(struct tevent_req *req, int *perr); + +#endif /* __CTDB_COMM_H__ */ diff --git a/ctdb/common/common.h b/ctdb/common/common.h new file mode 100644 index 0000000..c50b52a --- /dev/null +++ b/ctdb/common/common.h @@ -0,0 +1,159 @@ +/* + ctdb database library + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_COMMON_H__ +#define __CTDB_COMMON_H__ + +#include "lib/util/attr.h" + +/* From common/ctdb_io.c */ + +typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length, + void *private_data); + +uint32_t ctdb_queue_length(struct ctdb_queue *queue); + +int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length); + +int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd); + +struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb, + TALLOC_CTX *mem_ctx, int fd, int alignment, + ctdb_queue_cb_fn_t callback, + void *private_data, const char *fmt, ...) + PRINTF_ATTRIBUTE(7,8); + +/* From common/ctdb_ltdb.c */ + +int ctdb_db_tdb_flags(uint8_t db_flags, bool with_valgrind, bool with_mutex); + +struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, + const char *name); + +bool ctdb_db_persistent(struct ctdb_db_context *ctdb_db); +bool ctdb_db_replicated(struct ctdb_db_context *ctdb_db); +bool ctdb_db_volatile(struct ctdb_db_context *ctdb_db); + +bool ctdb_db_readonly(struct ctdb_db_context *ctdb_db); +void ctdb_db_set_readonly(struct ctdb_db_context *ctdb_db); +void ctdb_db_reset_readonly(struct ctdb_db_context *ctdb_db); + +bool ctdb_db_sticky(struct ctdb_db_context *ctdb_db); +void ctdb_db_set_sticky(struct ctdb_db_context *ctdb_db); + +uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key); + +int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, + TDB_DATA key, struct ctdb_ltdb_header *header, + TALLOC_CTX *mem_ctx, TDB_DATA *data); + +int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, + struct ctdb_ltdb_header *header, TDB_DATA data); + +int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key); + +int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key); + +int ctdb_ltdb_delete(struct ctdb_db_context *ctdb_db, TDB_DATA key); + +int ctdb_trackingdb_add_pnn(struct ctdb_context *ctdb, TDB_DATA *data, uint32_t pnn); + +typedef void (*ctdb_trackingdb_cb)(struct ctdb_context *ctdb, uint32_t pnn, + void *private_data); + +void ctdb_trackingdb_traverse(struct ctdb_context *ctdb, TDB_DATA data, + ctdb_trackingdb_cb cb, void *private_data); + +int ctdb_null_func(struct ctdb_call_info *call); + +int ctdb_fetch_func(struct ctdb_call_info *call); + +int ctdb_fetch_with_header_func(struct ctdb_call_info *call); + +/* from common/ctdb_util.c */ + +const char *ctdb_errstr(struct ctdb_context *ctdb); + +void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) + PRINTF_ATTRIBUTE(2,3); + +void ctdb_fatal(struct ctdb_context *ctdb, const char *msg) _NORETURN_; + +void ctdb_die(struct ctdb_context *ctdb, const char *msg) _NORETURN_; + +bool ctdb_set_helper(const char *type, char *helper, size_t size, + const char *envvar, + const char *dir, const char *file); + +int ctdb_parse_address(TALLOC_CTX *mem_ctx, const char *str, + ctdb_sock_addr *address); + +bool ctdb_same_address(ctdb_sock_addr *a1, ctdb_sock_addr *a2); + +uint32_t ctdb_hash(const TDB_DATA *key); + +struct ctdb_rec_data_old *ctdb_marshall_record(TALLOC_CTX *mem_ctx, + uint32_t reqid, + TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA data); + +struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx, + struct ctdb_marshall_buffer *m, + uint32_t db_id, + uint32_t reqid, + TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA data); + +TDB_DATA ctdb_marshall_finish(struct ctdb_marshall_buffer *m); + +struct ctdb_rec_data_old *ctdb_marshall_loop_next( + struct ctdb_marshall_buffer *m, + struct ctdb_rec_data_old *r, + uint32_t *reqid, + struct ctdb_ltdb_header *header, + TDB_DATA *key, TDB_DATA *data); + +void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip); + +bool ctdb_same_ip(const ctdb_sock_addr *tip1, const ctdb_sock_addr *tip2); + +bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2); + +char *ctdb_addr_to_str(ctdb_sock_addr *addr); + +unsigned ctdb_addr_to_port(ctdb_sock_addr *addr); + +struct ctdb_node_map_old *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx, + const char *nlist); + +struct ctdb_node_map_old *ctdb_node_list_to_map(struct ctdb_node **nodes, + uint32_t num_nodes, + TALLOC_CTX *mem_ctx); + +const char *runstate_to_string(enum ctdb_runstate runstate); + +enum ctdb_runstate runstate_from_string(const char *label); + +void ctdb_set_runstate(struct ctdb_context *ctdb, enum ctdb_runstate runstate); + +uint32_t *ctdb_key_to_idkey(TALLOC_CTX *mem_ctx, TDB_DATA key); + +#endif /* __CTDB_COMMON_H__ */ diff --git a/ctdb/common/conf.c b/ctdb/common/conf.c new file mode 100644 index 0000000..e849ff4 --- /dev/null +++ b/ctdb/common/conf.c @@ -0,0 +1,1391 @@ +/* + Configuration file handling on top of tini + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/locale.h" + +#include + +#include "lib/util/dlinklist.h" +#include "lib/util/tini.h" +#include "lib/util/debug.h" + +#include "common/conf.h" + +struct conf_value { + enum conf_type type; + union { + const char *string; + int integer; + bool boolean; + } data; +}; + +union conf_pointer { + const char **string; + int *integer; + bool *boolean; +}; + +struct conf_option { + struct conf_option *prev, *next; + + const char *name; + enum conf_type type; + void *validate; + + struct conf_value default_value; + bool default_set; + + struct conf_value *value, *new_value; + union conf_pointer ptr; + bool temporary_modified; +}; + +struct conf_section { + struct conf_section *prev, *next; + + const char *name; + conf_validate_section_fn validate; + struct conf_option *option; +}; + +struct conf_context { + const char *filename; + struct conf_section *section; + bool define_failed; + bool ignore_unknown; + bool reload; + bool validation_active; +}; + +/* + * Functions related to conf_value + */ + +static int string_to_string(TALLOC_CTX *mem_ctx, + const char *str, + const char **str_val) +{ + char *t; + + if (str == NULL) { + return EINVAL; + } + + t = talloc_strdup(mem_ctx, str); + if (t == NULL) { + return ENOMEM; + } + + *str_val = t; + return 0; +} + +static int string_to_integer(const char *str, int *int_val) +{ + long t; + char *endptr = NULL; + + if (str == NULL) { + return EINVAL; + } + + t = strtol(str, &endptr, 0); + if (*str != '\0' || endptr == NULL) { + if (t < 0 || t > INT_MAX) { + return EINVAL; + } + + *int_val = (int)t; + return 0; + } + + return EINVAL; +} + +static int string_to_boolean(const char *str, bool *bool_val) +{ + if (strcasecmp(str, "true") == 0 || strcasecmp(str, "yes") == 0) { + *bool_val = true; + return 0; + } + + if (strcasecmp(str, "false") == 0 || strcasecmp(str, "no") == 0) { + *bool_val = false; + return 0; + } + + return EINVAL; +} + +static int conf_value_from_string(TALLOC_CTX *mem_ctx, + const char *str, + struct conf_value *value) +{ + int ret; + + switch (value->type) { + case CONF_STRING: + ret = string_to_string(mem_ctx, str, &value->data.string); + break; + + case CONF_INTEGER: + ret = string_to_integer(str, &value->data.integer); + break; + + case CONF_BOOLEAN: + ret = string_to_boolean(str, &value->data.boolean); + break; + + default: + return EINVAL; + } + + return ret; +} + +static bool conf_value_compare(struct conf_value *old, struct conf_value *new) +{ + if (old == NULL || new == NULL) { + return false; + } + + if (old->type != new->type) { + return false; + } + + switch (old->type) { + case CONF_STRING: + if (old->data.string == NULL && new->data.string == NULL) { + return true; + } + if (old->data.string != NULL && new->data.string != NULL) { + if (strcmp(old->data.string, new->data.string) == 0) { + return true; + } + } + break; + + case CONF_INTEGER: + if (old->data.integer == new->data.integer) { + return true; + } + break; + + case CONF_BOOLEAN: + if (old->data.boolean == new->data.boolean) { + return true; + } + break; + } + + return false; +} + +static int conf_value_copy(TALLOC_CTX *mem_ctx, + struct conf_value *src, + struct conf_value *dst) +{ + if (src->type != dst->type) { + return EINVAL; + } + + switch (src->type) { + case CONF_STRING: + if (dst->data.string != NULL) { + talloc_free(discard_const(dst->data.string)); + } + if (src->data.string == NULL) { + dst->data.string = NULL; + } else { + dst->data.string = talloc_strdup( + mem_ctx, src->data.string); + if (dst->data.string == NULL) { + return ENOMEM; + } + } + break; + + case CONF_INTEGER: + dst->data.integer = src->data.integer; + break; + + case CONF_BOOLEAN: + dst->data.boolean = src->data.boolean; + break; + + default: + return EINVAL; + } + + return 0; +} + +static void conf_value_dump(const char *key, + struct conf_value *value, + bool is_default, + bool is_temporary, + FILE *fp) +{ + if ((value->type == CONF_STRING && value->data.string == NULL) || + is_default) { + fprintf(fp, "\t# %s = ", key); + } else { + fprintf(fp, "\t%s = ", key); + } + + switch (value->type) { + case CONF_STRING: + if (value->data.string != NULL) { + fprintf(fp, "%s", value->data.string); + } + break; + + case CONF_INTEGER: + fprintf(fp, "%d", value->data.integer); + break; + + case CONF_BOOLEAN: + fprintf(fp, "%s", (value->data.boolean ? "true" : "false")); + break; + } + + if (is_temporary) { + fprintf(fp, " # temporary"); + } + + fprintf(fp, "\n"); +} + +/* + * Functions related to conf_option + */ + +static struct conf_option *conf_option_find(struct conf_section *s, + const char *key) +{ + struct conf_option *opt; + + for (opt = s->option; opt != NULL; opt = opt->next) { + if (strcmp(opt->name, key) == 0) { + return opt; + } + } + + return NULL; +} + +static void conf_option_set_ptr_value(struct conf_option *opt) +{ + switch (opt->type) { + case CONF_STRING: + if (opt->ptr.string != NULL) { + *(opt->ptr.string) = opt->value->data.string; + } + break; + + case CONF_INTEGER: + if (opt->ptr.integer != NULL) { + *(opt->ptr.integer) = opt->value->data.integer; + } + break; + + case CONF_BOOLEAN: + if (opt->ptr.boolean != NULL) { + *(opt->ptr.boolean) = opt->value->data.boolean; + } + break; + } +} + +static void conf_option_default(struct conf_option *opt); + +static int conf_option_add(struct conf_section *s, + const char *key, + enum conf_type type, + void *validate, + struct conf_option **popt) +{ + struct conf_option *opt; + + opt = conf_option_find(s, key); + if (opt != NULL) { + D_ERR("conf: option \"%s\" already exists\n", key); + return EEXIST; + } + + opt = talloc_zero(s, struct conf_option); + if (opt == NULL) { + return ENOMEM; + } + + opt->name = talloc_strdup(opt, key); + if (opt->name == NULL) { + talloc_free(opt); + return ENOMEM; + } + + opt->type = type; + opt->validate = validate; + + DLIST_ADD_END(s->option, opt); + + if (popt != NULL) { + *popt = opt; + } + + return 0; +} + +static int conf_option_set_default(struct conf_option *opt, + struct conf_value *default_value) +{ + int ret; + + opt->default_value.type = opt->type; + + ret = conf_value_copy(opt, default_value, &opt->default_value); + if (ret != 0) { + return ret; + } + + opt->default_set = true; + opt->temporary_modified = false; + + return 0; +} + +static void conf_option_set_ptr(struct conf_option *opt, + union conf_pointer *ptr) +{ + opt->ptr = *ptr; +} + +static bool conf_option_validate_string(struct conf_option *opt, + struct conf_value *value, + enum conf_update_mode mode) +{ + conf_validate_string_option_fn validate = + (conf_validate_string_option_fn)opt->validate; + + return validate(opt->name, + opt->value->data.string, + value->data.string, + mode); +} + +static bool conf_option_validate_integer(struct conf_option *opt, + struct conf_value *value, + enum conf_update_mode mode) +{ + conf_validate_integer_option_fn validate = + (conf_validate_integer_option_fn)opt->validate; + + return validate(opt->name, + opt->value->data.integer, + value->data.integer, + mode); +} + +static bool conf_option_validate_boolean(struct conf_option *opt, + struct conf_value *value, + enum conf_update_mode mode) +{ + conf_validate_boolean_option_fn validate = + (conf_validate_boolean_option_fn)opt->validate; + + return validate(opt->name, + opt->value->data.boolean, + value->data.boolean, + mode); +} + +static bool conf_option_validate(struct conf_option *opt, + struct conf_value *value, + enum conf_update_mode mode) +{ + int ret; + + if (opt->validate == NULL) { + return true; + } + + switch (opt->type) { + case CONF_STRING: + ret = conf_option_validate_string(opt, value, mode); + break; + + case CONF_INTEGER: + ret = conf_option_validate_integer(opt, value, mode); + break; + + case CONF_BOOLEAN: + ret = conf_option_validate_boolean(opt, value, mode); + break; + + default: + ret = EINVAL; + } + + return ret; +} + +static bool conf_option_same_value(struct conf_option *opt, + struct conf_value *new_value) +{ + return conf_value_compare(opt->value, new_value); +} + +static int conf_option_new_value(struct conf_option *opt, + struct conf_value *new_value, + enum conf_update_mode mode) +{ + int ret; + bool ok; + + if (opt->new_value != &opt->default_value) { + TALLOC_FREE(opt->new_value); + } + + if (new_value == &opt->default_value) { + /* + * This happens only during load/reload. Set the value to + * default value, so if the config option is dropped from + * config file, then it get's reset to default. + */ + opt->new_value = &opt->default_value; + } else { + ok = conf_option_validate(opt, new_value, mode); + if (!ok) { + D_ERR("conf: validation for option \"%s\" failed\n", + opt->name); + return EINVAL; + } + + opt->new_value = talloc_zero(opt, struct conf_value); + if (opt->new_value == NULL) { + return ENOMEM; + } + + opt->new_value->type = opt->value->type; + ret = conf_value_copy(opt, new_value, opt->new_value); + if (ret != 0) { + return ret; + } + } + + conf_option_set_ptr_value(opt); + + if (new_value != &opt->default_value) { + if (mode == CONF_MODE_API) { + opt->temporary_modified = true; + } else { + opt->temporary_modified = false; + } + } + + return 0; +} + +static int conf_option_new_default_value(struct conf_option *opt, + enum conf_update_mode mode) +{ + return conf_option_new_value(opt, &opt->default_value, mode); +} + +static void conf_option_default(struct conf_option *opt) +{ + if (! opt->default_set) { + return; + } + + if (opt->value != &opt->default_value) { + TALLOC_FREE(opt->value); + } + + opt->value = &opt->default_value; + conf_option_set_ptr_value(opt); +} + +static void conf_option_reset(struct conf_option *opt) +{ + if (opt->new_value != &opt->default_value) { + TALLOC_FREE(opt->new_value); + } + + conf_option_set_ptr_value(opt); +} + +static void conf_option_update(struct conf_option *opt) +{ + if (opt->new_value == NULL) { + return; + } + + if (opt->value != &opt->default_value) { + TALLOC_FREE(opt->value); + } + + opt->value = opt->new_value; + opt->new_value = NULL; + + conf_option_set_ptr_value(opt); +} + +static void conf_option_reset_temporary(struct conf_option *opt) +{ + opt->temporary_modified = false; +} + +static bool conf_option_is_default(struct conf_option *opt) +{ + return (opt->value == &opt->default_value); +} + +static void conf_option_dump(struct conf_option *opt, FILE *fp) +{ + bool is_default; + + is_default = conf_option_is_default(opt); + + conf_value_dump(opt->name, + opt->value, + is_default, + opt->temporary_modified, + fp); +} + +/* + * Functions related to conf_section + */ + +static struct conf_section *conf_section_find(struct conf_context *conf, + const char *section) +{ + struct conf_section *s; + + for (s = conf->section; s != NULL; s = s->next) { + if (strcasecmp(s->name, section) == 0) { + return s; + } + } + + return NULL; +} + +static int conf_section_add(struct conf_context *conf, + const char *section, + conf_validate_section_fn validate) +{ + struct conf_section *s; + + s = conf_section_find(conf, section); + if (s != NULL) { + return EEXIST; + } + + s = talloc_zero(conf, struct conf_section); + if (s == NULL) { + return ENOMEM; + } + + s->name = talloc_strdup(s, section); + if (s->name == NULL) { + talloc_free(s); + return ENOMEM; + } + + s->validate = validate; + + DLIST_ADD_END(conf->section, s); + return 0; +} + +static bool conf_section_validate(struct conf_context *conf, + struct conf_section *s, + enum conf_update_mode mode) +{ + bool ok; + + if (s->validate == NULL) { + return true; + } + + ok = s->validate(conf, s->name, mode); + if (!ok) { + D_ERR("conf: validation for section [%s] failed\n", s->name); + } + + return ok; +} + +static void conf_section_dump(struct conf_section *s, FILE *fp) +{ + fprintf(fp, "[%s]\n", s->name); +} + +/* + * Functions related to conf_context + */ + +static void conf_all_default(struct conf_context *conf) +{ + struct conf_section *s; + struct conf_option *opt; + + for (s = conf->section; s != NULL; s = s->next) { + for (opt = s->option; opt != NULL; opt = opt->next) { + conf_option_default(opt); + } + } +} + +static int conf_all_temporary_default(struct conf_context *conf, + enum conf_update_mode mode) +{ + struct conf_section *s; + struct conf_option *opt; + int ret; + + for (s = conf->section; s != NULL; s = s->next) { + for (opt = s->option; opt != NULL; opt = opt->next) { + ret = conf_option_new_default_value(opt, mode); + if (ret != 0) { + return ret; + } + } + } + + return 0; +} + +static void conf_all_reset(struct conf_context *conf) +{ + struct conf_section *s; + struct conf_option *opt; + + for (s = conf->section; s != NULL; s = s->next) { + for (opt = s->option; opt != NULL; opt = opt->next) { + conf_option_reset(opt); + } + } +} + +static void conf_all_update(struct conf_context *conf) +{ + struct conf_section *s; + struct conf_option *opt; + + for (s = conf->section; s != NULL; s = s->next) { + for (opt = s->option; opt != NULL; opt = opt->next) { + conf_option_update(opt); + conf_option_reset_temporary(opt); + } + } +} + +/* + * API functions + */ + +int conf_init(TALLOC_CTX *mem_ctx, struct conf_context **result) +{ + struct conf_context *conf; + + conf = talloc_zero(mem_ctx, struct conf_context); + if (conf == NULL) { + return ENOMEM; + } + + conf->define_failed = false; + + *result = conf; + return 0; +} + +void conf_define_section(struct conf_context *conf, + const char *section, + conf_validate_section_fn validate) +{ + int ret; + + if (conf->define_failed) { + return; + } + + if (section == NULL) { + conf->define_failed = true; + return; + } + + ret = conf_section_add(conf, section, validate); + if (ret != 0) { + conf->define_failed = true; + return; + } +} + +static struct conf_option *conf_define(struct conf_context *conf, + const char *section, + const char *key, + enum conf_type type, + conf_validate_string_option_fn validate) +{ + struct conf_section *s; + struct conf_option *opt; + int ret; + + s = conf_section_find(conf, section); + if (s == NULL) { + D_ERR("conf: unknown section [%s]\n", section); + return NULL; + } + + if (key == NULL) { + D_ERR("conf: option name null in section [%s]\n", section); + return NULL; + } + + ret = conf_option_add(s, key, type, validate, &opt); + if (ret != 0) { + return NULL; + } + + return opt; +} + +static void conf_define_post(struct conf_context *conf, + struct conf_option *opt, + struct conf_value *default_value) +{ + int ret; + + ret = conf_option_set_default(opt, default_value); + if (ret != 0) { + conf->define_failed = true; + return; + } + + conf_option_default(opt); +} + +void conf_define_string(struct conf_context *conf, + const char *section, + const char *key, + const char *default_str_val, + conf_validate_string_option_fn validate) +{ + struct conf_option *opt; + struct conf_value default_value; + + if (! conf_valid(conf)) { + return; + } + + opt = conf_define(conf, section, key, CONF_STRING, validate); + if (opt == NULL) { + conf->define_failed = true; + return; + } + + default_value.type = CONF_STRING; + default_value.data.string = default_str_val; + + conf_define_post(conf, opt, &default_value); +} + +void conf_define_integer(struct conf_context *conf, + const char *section, + const char *key, + const int default_int_val, + conf_validate_integer_option_fn validate) +{ + struct conf_option *opt; + struct conf_value default_value; + + if (! conf_valid(conf)) { + return; + } + + opt = conf_define(conf, section, key, CONF_INTEGER, (void *)validate); + if (opt == NULL) { + conf->define_failed = true; + return; + } + + default_value.type = CONF_INTEGER; + default_value.data.integer = default_int_val; + + conf_define_post(conf, opt, &default_value); +} + + +void conf_define_boolean(struct conf_context *conf, + const char *section, + const char *key, + const bool default_bool_val, + conf_validate_boolean_option_fn validate) +{ + struct conf_option *opt; + struct conf_value default_value; + + if (! conf_valid(conf)) { + return; + } + + opt = conf_define(conf, section, key, CONF_BOOLEAN, (void *)validate); + if (opt == NULL) { + conf->define_failed = true; + return; + } + + default_value.type = CONF_BOOLEAN; + default_value.data.boolean = default_bool_val; + + conf_define_post(conf, opt, &default_value); +} + +static struct conf_option *_conf_option(struct conf_context *conf, + const char *section, + const char *key) +{ + struct conf_section *s; + struct conf_option *opt; + + s = conf_section_find(conf, section); + if (s == NULL) { + return NULL; + } + + opt = conf_option_find(s, key); + return opt; +} + +void conf_assign_string_pointer(struct conf_context *conf, + const char *section, + const char *key, + const char **str_ptr) +{ + struct conf_option *opt; + union conf_pointer ptr; + + opt = _conf_option(conf, section, key); + if (opt == NULL) { + D_ERR("conf: unknown option [%s] -> \"%s\"\n", section, key); + conf->define_failed = true; + return; + } + + if (opt->type != CONF_STRING) { + conf->define_failed = true; + return; + } + + ptr.string = str_ptr; + conf_option_set_ptr(opt, &ptr); + conf_option_set_ptr_value(opt); +} + +void conf_assign_integer_pointer(struct conf_context *conf, + const char *section, + const char *key, + int *int_ptr) +{ + struct conf_option *opt; + union conf_pointer ptr; + + opt = _conf_option(conf, section, key); + if (opt == NULL) { + D_ERR("conf: unknown option [%s] -> \"%s\"\n", section, key); + conf->define_failed = true; + return; + } + + if (opt->type != CONF_INTEGER) { + conf->define_failed = true; + return; + } + + ptr.integer = int_ptr; + conf_option_set_ptr(opt, &ptr); + conf_option_set_ptr_value(opt); +} + +void conf_assign_boolean_pointer(struct conf_context *conf, + const char *section, + const char *key, + bool *bool_ptr) +{ + struct conf_option *opt; + union conf_pointer ptr; + + opt = _conf_option(conf, section, key); + if (opt == NULL) { + D_ERR("conf: unknown option [%s] -> \"%s\"\n", section, key); + conf->define_failed = true; + return; + } + + if (opt->type != CONF_BOOLEAN) { + conf->define_failed = true; + return; + } + + ptr.boolean = bool_ptr; + conf_option_set_ptr(opt, &ptr); + conf_option_set_ptr_value(opt); +} + +bool conf_query(struct conf_context *conf, + const char *section, + const char *key, + enum conf_type *type) +{ + struct conf_section *s; + struct conf_option *opt; + + if (! conf_valid(conf)) { + return false; + } + + s = conf_section_find(conf, section); + if (s == NULL) { + return false; + } + + opt = conf_option_find(s, key); + if (opt == NULL) { + return false; + } + + if (type != NULL) { + *type = opt->type; + } + return true; +} + +bool conf_valid(struct conf_context *conf) +{ + if (conf->define_failed) { + return false; + } + + return true; +} + +void conf_set_defaults(struct conf_context *conf) +{ + conf_all_default(conf); +} + +struct conf_load_state { + struct conf_context *conf; + struct conf_section *s; + enum conf_update_mode mode; + int err; +}; + +static bool conf_load_section(const char *section, void *private_data); +static bool conf_load_option(const char *name, + const char *value_str, + void *private_data); + +static int conf_load_internal(struct conf_context *conf) +{ + struct conf_load_state state; + FILE *fp; + int ret; + bool ok; + + state = (struct conf_load_state) { + .conf = conf, + .mode = (conf->reload ? CONF_MODE_RELOAD : CONF_MODE_LOAD), + }; + + ret = conf_all_temporary_default(conf, state.mode); + if (ret != 0) { + return ret; + } + + fp = fopen(conf->filename, "r"); + if (fp == NULL) { + return errno; + } + + ok = tini_parse(fp, + false, + conf_load_section, + conf_load_option, + &state); + fclose(fp); + if (!ok) { + goto fail; + } + + /* Process the last section */ + if (state.s != NULL) { + ok = conf_section_validate(conf, state.s, state.mode); + if (!ok) { + state.err = EINVAL; + goto fail; + } + } + + if (state.err != 0) { + goto fail; + } + + conf_all_update(conf); + return 0; + +fail: + conf_all_reset(conf); + return state.err; +} + +static bool conf_load_section(const char *section, void *private_data) +{ + struct conf_load_state *state = + (struct conf_load_state *)private_data; + bool ok; + + if (state->s != NULL) { + ok = conf_section_validate(state->conf, state->s, state->mode); + if (!ok) { + state->err = EINVAL; + return true; + } + } + + state->s = conf_section_find(state->conf, section); + if (state->s == NULL) { + if (state->conf->ignore_unknown) { + D_DEBUG("conf: ignoring unknown section [%s]\n", + section); + } else { + D_ERR("conf: unknown section [%s]\n", section); + state->err = EINVAL; + return true; + } + } + + return true; +} + +static bool conf_load_option(const char *name, + const char *value_str, + void *private_data) +{ + struct conf_load_state *state = + (struct conf_load_state *)private_data; + struct conf_option *opt; + TALLOC_CTX *tmp_ctx; + struct conf_value value; + int ret; + bool ok; + + if (state->s == NULL) { + if (state->conf->ignore_unknown) { + D_DEBUG("conf: unknown section for option \"%s\"\n", + name); + return true; + } else { + D_ERR("conf: unknown section for option \"%s\"\n", + name); + state->err = EINVAL; + return true; + } + } + + opt = conf_option_find(state->s, name); + if (opt == NULL) { + if (state->conf->ignore_unknown) { + D_DEBUG("conf: unknown option [%s] -> \"%s\"\n", + state->s->name, + name); + return true; + } else { + D_ERR("conf: unknown option [%s] -> \"%s\"\n", + state->s->name, + name); + state->err = EINVAL; + return true; + } + } + + if (strlen(value_str) == 0) { + D_ERR("conf: empty value [%s] -> \"%s\"\n", + state->s->name, + name); + state->err = EINVAL; + return true; + } + + tmp_ctx = talloc_new(state->conf); + if (tmp_ctx == NULL) { + state->err = ENOMEM; + return false; + } + + value.type = opt->type; + ret = conf_value_from_string(tmp_ctx, value_str, &value); + if (ret != 0) { + D_ERR("conf: invalid value [%s] -> \"%s\" = \"%s\"\n", + state->s->name, + name, + value_str); + talloc_free(tmp_ctx); + state->err = ret; + return true; + } + + ok = conf_option_same_value(opt, &value); + if (ok) { + goto done; + } + + ret = conf_option_new_value(opt, &value, state->mode); + if (ret != 0) { + talloc_free(tmp_ctx); + state->err = ret; + return true; + } + +done: + talloc_free(tmp_ctx); + return true; + +} + +int conf_load(struct conf_context *conf, + const char *filename, + bool ignore_unknown) +{ + conf->filename = talloc_strdup(conf, filename); + if (conf->filename == NULL) { + return ENOMEM; + } + + conf->ignore_unknown = ignore_unknown; + + D_NOTICE("Reading config file %s\n", filename); + + return conf_load_internal(conf); +} + +int conf_reload(struct conf_context *conf) +{ + int ret; + + if (conf->filename == NULL) { + return EPERM; + } + + D_NOTICE("Re-reading config file %s\n", conf->filename); + + conf->reload = true; + ret = conf_load_internal(conf); + conf->reload = false; + + return ret; +} + +static int conf_set(struct conf_context *conf, + const char *section, + const char *key, + struct conf_value *value) +{ + struct conf_section *s; + struct conf_option *opt; + int ret; + bool ok; + + s = conf_section_find(conf, section); + if (s == NULL) { + return EINVAL; + } + + opt = conf_option_find(s, key); + if (opt == NULL) { + return EINVAL; + } + + if (opt->type != value->type) { + return EINVAL; + } + + ok = conf_option_same_value(opt, value); + if (ok) { + return 0; + } + + ret = conf_option_new_value(opt, value, CONF_MODE_API); + if (ret != 0) { + conf_option_reset(opt); + return ret; + } + + ok = conf_section_validate(conf, s, CONF_MODE_API); + if (!ok) { + conf_option_reset(opt); + return EINVAL; + } + + conf_option_update(opt); + return 0; +} + +int conf_set_string(struct conf_context *conf, + const char *section, + const char *key, + const char *str_val) +{ + struct conf_value value; + + value.type = CONF_STRING; + value.data.string = str_val; + + return conf_set(conf, section, key, &value); +} + +int conf_set_integer(struct conf_context *conf, + const char *section, + const char *key, + int int_val) +{ + struct conf_value value; + + value.type = CONF_INTEGER; + value.data.integer = int_val; + + return conf_set(conf, section, key, &value); +} + +int conf_set_boolean(struct conf_context *conf, + const char *section, + const char *key, + bool bool_val) +{ + struct conf_value value; + + value.type = CONF_BOOLEAN; + value.data.boolean = bool_val; + + return conf_set(conf, section, key, &value); +} + +static int conf_get(struct conf_context *conf, + const char *section, + const char *key, + enum conf_type type, + const struct conf_value **value, + bool *is_default) +{ + struct conf_section *s; + struct conf_option *opt; + + s = conf_section_find(conf, section); + if (s == NULL) { + return EINVAL; + } + + opt = conf_option_find(s, key); + if (opt == NULL) { + return EINVAL; + } + + if (opt->type != type) { + return EINVAL; + } + + *value = opt->value; + if (is_default != NULL) { + *is_default = conf_option_is_default(opt); + } + + return 0; +} + +int conf_get_string(struct conf_context *conf, + const char *section, + const char *key, + const char **str_val, + bool *is_default) +{ + const struct conf_value *value; + int ret; + + ret = conf_get(conf, section, key, CONF_STRING, &value, is_default); + if (ret != 0) { + return ret; + } + + *str_val = value->data.string; + return 0; +} + +int conf_get_integer(struct conf_context *conf, + const char *section, + const char *key, + int *int_val, + bool *is_default) +{ + const struct conf_value *value; + int ret; + + ret = conf_get(conf, section, key, CONF_INTEGER, &value, is_default); + if (ret != 0) { + return ret; + } + + *int_val = value->data.integer; + return 0; +} + +int conf_get_boolean(struct conf_context *conf, + const char *section, + const char *key, + bool *bool_val, + bool *is_default) +{ + const struct conf_value *value; + int ret; + + ret = conf_get(conf, section, key, CONF_BOOLEAN, &value, is_default); + if (ret != 0) { + return ret; + } + + *bool_val = value->data.boolean; + return 0; +} + +void conf_dump(struct conf_context *conf, FILE *fp) +{ + struct conf_section *s; + struct conf_option *opt; + + for (s = conf->section; s != NULL; s = s->next) { + conf_section_dump(s, fp); + for (opt = s->option; opt != NULL; opt = opt->next) { + conf_option_dump(opt, fp); + } + } +} diff --git a/ctdb/common/conf.h b/ctdb/common/conf.h new file mode 100644 index 0000000..6b152c1 --- /dev/null +++ b/ctdb/common/conf.h @@ -0,0 +1,473 @@ +/* + Configuration file handling on top of tini + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CONF_H__ +#define __CTDB_CONF_H__ + +#include +#include +#include + +/** + * @file conf.h + * + * @brief Configuration file handling with sections and key-value pairs + * + * CTDB settings can be written in a configuration file ctdb.conf (similar to + * samba's smb.conf). Various daemons and tools will consult the configuration + * file for runtime settings. + * + * The configuration will be organized in sections depending on various + * components. Each section will have various configuration options in the form + * of key-value pairs. + * + * [section1] + * key1 = value1 + * ... + * + * [section2] + * key2 = value2 + * ... + * + * ... + * + */ + +/** + * @brief Abstract data structure holding the configuration options + */ +struct conf_context; + +/** + * @brief configuration option update mode + * + * When a value of configuration option is changed, update mode is set + * appropriately. + * + * CONF_MODE_API - value modified using set functions + * CONF_MODE_LOAD - value modified via conf_load + * CONF_MODE_RELOAD - value modified via conf_reload + */ +enum conf_update_mode { + CONF_MODE_API, + CONF_MODE_LOAD, + CONF_MODE_RELOAD, +}; + +/** + * @brief configuration option type + */ +enum conf_type { + CONF_STRING, + CONF_INTEGER, + CONF_BOOLEAN, +}; + +/** + * @brief Configuration section validation function + * + * Check if all the configuration options are consistent with each-other + */ +typedef bool (*conf_validate_section_fn)(struct conf_context *conf, + const char *section, + enum conf_update_mode mode); + +/** + * @brief Configuration option validation function for string + * + * Check if a configuration option value is valid + */ +typedef bool (*conf_validate_string_option_fn)(const char *key, + const char *old_value, + const char *new_value, + enum conf_update_mode mode); + +/** + * @brief Configuration option validation function for integer + * + * Check if a configuration option value is valid + */ +typedef bool (*conf_validate_integer_option_fn)(const char *key, + int old_value, + int new_value, + enum conf_update_mode mode); + +/** + * @brief Configuration option validation function for boolean + * + * Check if a configuration option value is valid + */ +typedef bool (*conf_validate_boolean_option_fn)(const char *key, + bool old_value, + bool new_value, + enum conf_update_mode mode); + +/** + * @brief Initialize configuration option database + * + * This return a new configuration options context. Freeing this context will + * free up all the memory associated with the configuration options. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] result The new configuration options context + * @return 0 on success, errno on failure + */ +int conf_init(TALLOC_CTX *mem_ctx, struct conf_context **result); + +/** + * @brief Define a section for organizing configuration options + * + * This functions creates a section to organize configuration option. The + * section names are case-insensitive and are always stored in lower case. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] validate The validation function for configuration options + */ +void conf_define_section(struct conf_context *conf, + const char *section, + conf_validate_section_fn validate); + +/** + * @brief Define a configuration option which has a string value + * + * This functions adds a new configuration option organized under a given + * section. Configuration options are case-insensitive and are always stored + * in lower case. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[in] default_value The default value for the configuration option + * @param[in] validate The validation function for the configuration option + */ +void conf_define_string(struct conf_context *conf, + const char *section, + const char *key, + const char *default_value, + conf_validate_string_option_fn validate); + +/** + * @brief Define a configuration option which has an integer value + * + * This functions adds a new configuration option organized under a given + * section. Configuration options are case-insensitive and are always stored + * in lower case. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[in] default_value The default value for the configuration option + * @param[in] validate The validation function for the configuration option + */ +void conf_define_integer(struct conf_context *conf, + const char *section, + const char *key, + const int default_value, + conf_validate_integer_option_fn validate); + +/** + * @brief Define a configuration option which has an boolean value + * + * This functions adds a new configuration option organized under a given + * section. Configuration options are case-insensitive and are always stored + * in lower case. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[in] default_value The default value for the configuration option + * @param[in] validate The validation function for the configuration option + */ +void conf_define_boolean(struct conf_context *conf, + const char *section, + const char *key, + const bool default_value, + conf_validate_boolean_option_fn validate); + +/** + * @brief Assign user-accessible pointer for string option + * + * This pointer can be used for accessing the value of configuration option + * directly without requiring a function call. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[in] ptr User-accessible pointer to the value + */ +void conf_assign_string_pointer(struct conf_context *conf, + const char *section, + const char *key, + const char **ptr); + +/** + * @brief Assign user-accessible pointer for integer option + * + * This pointer can be used for accessing the value of configuration option + * directly without requiring a function call. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[in] ptr User-accessible pointer to the value + */ +void conf_assign_integer_pointer(struct conf_context *conf, + const char *section, + const char *key, + int *ptr); + +/** + * @brief Assign user-accessible pointer for boolean option + * + * This pointer can be used for accessing the value of configuration option + * directly without requiring a function call. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[in] ptr User-accessible pointer to the value + * @return true on success, false on failure + */ +void conf_assign_boolean_pointer(struct conf_context *conf, + const char *section, + const char *key, + bool *ptr); + +/** + * @brief Query a configuration option + * + * This function checks if a configuration option is defined or not. + * + * @param[in] conf The configuration options context + * @param[in] section The name of the section + * @param[in] key The name of the configuration option + * @param[out] type The type of the configuration option + * @return true on success, false if section/option is not defined + */ +bool conf_query(struct conf_context *conf, + const char *section, + const char *key, + enum conf_type *type); + +/** + * @brief Check if the defined configuration options are valid + * + * This function must be called after creating configuration options + * to confirm that all the option definitions are valid. + * + * @param[in] conf The configuration options context + * @return true on success, false on failure + */ +bool conf_valid(struct conf_context *conf); + +/** + * @brief Set the default values for all configuration options + * + * This function resets all the configuration options to their default values. + * + * @param[in] conf The connfiguration options context + */ +void conf_set_defaults(struct conf_context *conf); + +/** + * @brief Load the values for configuration option values from a file + * + * This function will update the values of the configuration options from those + * specified in a file. This function will fail in case it encounters an + * undefined option. Any sections which are not defined, will be ignored. + * + * This function will call validation function (if specified) before updating + * the value of a configuration option. After updating all the values for a + * section, the validation for section (if specified) will be called. If any + * of the validation functions return error, then all the configuration + * options will be reset to their previous values. + * + * @param[in] conf The configuration options context + * @param[in] filename The configuration file + * @param[in] skip_unknown Whether unknown config options should be ignored + * @return 0 on success, errno on failure + */ +int conf_load(struct conf_context *conf, + const char *filename, + bool ignore_unknown); + +/** + * @brief Reload the values for configuration options + * + * This function will re-load the values of the configuration options. This + * function can be called only after succesful call to conf_load(). + * + * @see conf_load + * + * @param[in] conf The configuration options context + * @return 0 on success, errno on failure. + */ +int conf_reload(struct conf_context *conf); + +/** + * @brief Set the string value of a configuration option + * + * This function can be used to update the value of a configuration option. + * This will call the validation function for that option (if defined) and + * the section validation function (if defined). + * + * If a user-defined storage pointer is provided, then the value of a + * configuration option should not be changed via that pointer. + * + * @param[in] conf The configuration options context + * @param[in] section The name of a section + * @param[in] key The name of a configuration option + * @param[in] str_val The string value + * @return 0 on success, errno in case of failure + */ +int conf_set_string(struct conf_context *conf, + const char *section, + const char *key, + const char *str_val); + +/** + * @brief Set the integer value of a configuration option + * + * This function can be used to update the value of a configuration option. + * This will call the validation function for that option (if defined) and + * the section validation function (if defined). + * + * If a user-defined storage pointer is provided, then the value of a + * configuration option should not be changed via that pointer. + * + * @param[in] conf The configuration options context + * @param[in] section The name of a section + * @param[in] key The name of a configuration option + * @param[in] int_val The integer value + * @return 0 on success, errno in case of failure + */ +int conf_set_integer(struct conf_context *conf, + const char *section, + const char *key, + int int_val); + +/** + * @brief Set the boolean value of a configuration option + * + * This function can be used to update the value of a configuration option. + * This will call the validation function for that option (if defined) and + * the section validation function (if defined). + * + * If a user-defined storage pointer is provided, then the value of a + * configuration option should not be changed via that pointer. + * + * @param[in] conf The configuration options context + * @param[in] section The name of a section + * @param[in] key The name of a configuration option + * @param[in] bool_val The boolean value + * @return 0 on success, errno in case of failure + */ +int conf_set_boolean(struct conf_context *conf, + const char *section, + const char *key, + bool bool_val); + +/** + * @brief Get the string value of a configuration option + * + * This function can be used to fetch the current value of a configuration + * option. + * + * If a user-defined storage pointer is provided, then the value of a + * configuration option can be accessed directly via that pointer. + * + * @param[in] conf The configuration options context + * @param[in] section The name of a section + * @param[in] key The name of a configuration option + * @param[out] str_val The string value of the configuration option + * @param[out] is_default True if the value is default value + * @return 0 on success, errno in case of failure + */ +int conf_get_string(struct conf_context *conf, + const char *section, + const char *key, + const char **str_val, + bool *is_default); + +/** + * @brief Get the integer value of a configuration option + * + * This function can be used to fetch the current value of a configuration + * option. + * + * If a user-defined storage pointer is provided, then the value of a + * configuration option can be accessed directly via that pointer. + * + * @param[in] conf The configuration options context + * @param[in] section The name of a section + * @param[in] key The name of a configuration option + * @param[out] int_val The integer value of the configuration option + * @param[out] is_default True if the value is default value + * @return 0 on success, errno in case of failure + */ +int conf_get_integer(struct conf_context *conf, + const char *section, + const char *key, + int *int_val, + bool *is_default); + +/** + * @brief Get the boolean value of a configuration option + * + * This function can be used to fetch the current value of a configuration + * option. + * + * If a user-defined storage pointer is provided, then the value of a + * configuration option can be accessed directly via that pointer. + * + * @param[in] conf The configuration options context + * @param[in] section The name of a section + * @param[in] key The name of a configuration option + * @param[out] bool_val The boolean value of the configuration option + * @param[out] is_default True if the value is default value + * @return 0 on success, errno in case of failure + */ +int conf_get_boolean(struct conf_context *conf, + const char *section, + const char *key, + bool *bool_val, + bool *is_default); + +/** + * @brief Dump the configuration in a file + * + * All the configuration options are dumped with their current values. + * If an option has a default value, then it is commented. + * + * Here is a sample output: + * + * [section1] + * key1 = value1 + * key2 = value2 + * # key3 = default_value3 + * [section2] + * key4 = value4 + * + * @param[in] conf The configuration options context + * @param[in] fp File pointer + */ +void conf_dump(struct conf_context *conf, FILE *fp); + +#endif /* __CTDB_CONF_H__ */ diff --git a/ctdb/common/conf_tool.c b/ctdb/common/conf_tool.c new file mode 100644 index 0000000..2d0543d --- /dev/null +++ b/ctdb/common/conf_tool.c @@ -0,0 +1,321 @@ +/* + Config options tool + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "lib/util/debug.h" + +#include "common/logging.h" +#include "common/cmdline.h" +#include "common/conf.h" +#include "common/path.h" + +#include "common/logging_conf.h" +#include "cluster/cluster_conf.h" +#include "database/database_conf.h" +#include "event/event_conf.h" +#include "failover/failover_conf.h" +#include "server/legacy_conf.h" + +#include "common/conf_tool.h" + +struct conf_tool_context { + struct cmdline_context *cmdline; + const char *conf_file; + struct conf_context *conf; +}; + +static int conf_tool_dump(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct conf_tool_context *ctx = talloc_get_type_abort( + private_data, struct conf_tool_context); + int ret; + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "dump"); + return EINVAL; + } + + ret = conf_load(ctx->conf, ctx->conf_file, true); + if (ret != 0 && ret != ENOENT) { + D_ERR("Failed to load config file %s\n", ctx->conf_file); + return ret; + } + + conf_dump(ctx->conf, stdout); + return 0; +} + +static int conf_tool_get(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct conf_tool_context *ctx = talloc_get_type_abort( + private_data, struct conf_tool_context); + const char *section, *option; + enum conf_type type; + int ret; + bool ok; + const char *s_val = NULL; + int i_val; + bool b_val; + + if (argc != 2) { + cmdline_usage(ctx->cmdline, "get"); + return EINVAL; + } + + section = argv[0]; + option = argv[1]; + + ok = conf_query(ctx->conf, section, option, &type); + if (!ok) { + D_ERR("Configuration option [%s] -> \"%s\" not defined\n", + section, option); + return ENOENT; + } + + ret = conf_load(ctx->conf, ctx->conf_file, true); + if (ret != 0 && ret != ENOENT) { + D_ERR("Failed to load config file %s\n", ctx->conf_file); + return ret; + } + + switch (type) { + case CONF_STRING: + ret = conf_get_string(ctx->conf, + section, + option, + &s_val, + NULL); + break; + + case CONF_INTEGER: + ret = conf_get_integer(ctx->conf, + section, + option, + &i_val, + NULL); + break; + + case CONF_BOOLEAN: + ret = conf_get_boolean(ctx->conf, + section, + option, + &b_val, + NULL); + break; + + default: + D_ERR("Unknown configuration option type\n"); + return EINVAL; + } + + if (ret != 0) { + D_ERR("Failed to get configuration option value\n"); + return ret; + } + + switch (type) { + case CONF_STRING: + printf("%s\n", s_val == NULL ? "" : s_val); + break; + + case CONF_INTEGER: + printf("%d\n", i_val); + break; + + case CONF_BOOLEAN: + printf("%s\n", b_val ? "true" : "false"); + break; + } + + return 0; +} + +static int conf_tool_validate(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct conf_tool_context *ctx = talloc_get_type_abort( + private_data, struct conf_tool_context); + int ret; + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "validate"); + return EINVAL; + } + + ret = conf_load(ctx->conf, ctx->conf_file, false); + if (ret != 0) { + D_ERR("Failed to load config file %s\n", ctx->conf_file); + return ret; + } + + return 0; +} + +struct cmdline_command conf_commands[] = { + { "dump", conf_tool_dump, + "Dump configuration", NULL }, + { "get", conf_tool_get, + "Get a config value", "
" }, + { "validate", conf_tool_validate, + "Validate configuration file", NULL }, + CMDLINE_TABLEEND +}; + +int conf_tool_init(TALLOC_CTX *mem_ctx, + const char *prog, + struct poptOption *options, + int argc, + const char **argv, + bool parse_options, + struct conf_tool_context **result) +{ + struct conf_tool_context *ctx; + int ret; + + ctx = talloc_zero(mem_ctx, struct conf_tool_context); + if (ctx == NULL) { + D_ERR("Memory allocation error\n"); + return ENOMEM; + } + + ret = cmdline_init(ctx, + prog, + options, + NULL, + conf_commands, + &ctx->cmdline); + if (ret != 0) { + D_ERR("Failed to initialize cmdline, ret=%d\n", ret); + talloc_free(ctx); + return ret; + } + + ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options); + if (ret != 0) { + cmdline_usage(ctx->cmdline, NULL); + talloc_free(ctx); + return ret; + } + + *result = ctx; + return 0; +} + +int conf_tool_run(struct conf_tool_context *ctx, int *result) +{ + int ret; + + ctx->conf_file = path_config(ctx); + if (ctx->conf_file == NULL) { + D_ERR("Memory allocation error\n"); + return ENOMEM; + } + + ret = conf_init(ctx, &ctx->conf); + if (ret != 0) { + D_ERR("Failed to initialize config\n"); + return ret; + } + + /* Call functions to initialize config sections/variables */ + logging_conf_init(ctx->conf, NULL); + cluster_conf_init(ctx->conf); + database_conf_init(ctx->conf); + event_conf_init(ctx->conf); + failover_conf_init(ctx->conf); + legacy_conf_init(ctx->conf); + + if (! conf_valid(ctx->conf)) { + D_ERR("Failed to define configuration options\n"); + return EINVAL; + } + + ret = cmdline_run(ctx->cmdline, ctx, result); + return ret; +} + +#ifdef CTDB_CONF_TOOL + +static struct { + const char *debug; +} conf_data = { + .debug = "ERROR", +}; + +struct poptOption conf_options[] = { + POPT_AUTOHELP + { "debug", 'd', POPT_ARG_STRING, &conf_data.debug, 0, + "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" }, + POPT_TABLEEND +}; + +int main(int argc, const char **argv) +{ + TALLOC_CTX *mem_ctx; + struct conf_tool_context *ctx; + int ret, result; + int level; + bool ok; + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + fprintf(stderr, "Memory allocation error\n"); + exit(1); + } + + ret = conf_tool_init(mem_ctx, + "ctdb-config", + conf_options, + argc, + argv, + true, + &ctx); + if (ret != 0) { + talloc_free(mem_ctx); + exit(1); + } + + setup_logging("ctdb-config", DEBUG_STDERR); + ok = debug_level_parse(conf_data.debug, &level); + if (!ok) { + level = DEBUG_ERR; + } + debuglevel_set(level); + + ret = conf_tool_run(ctx, &result); + if (ret != 0) { + result = 1; + } + + talloc_free(mem_ctx); + exit(result); +} + +#endif /* CTDB_CONF_TOOL */ diff --git a/ctdb/common/conf_tool.h b/ctdb/common/conf_tool.h new file mode 100644 index 0000000..c77419f --- /dev/null +++ b/ctdb/common/conf_tool.h @@ -0,0 +1,39 @@ +/* + Config options tool + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_CONF_TOOL_H__ +#define __CTDB_CONF_TOOL_H__ + +#include +#include +#include + +struct conf_tool_context; + +int conf_tool_init(TALLOC_CTX *mem_ctx, + const char *prog, + struct poptOption *options, + int argc, + const char **argv, + bool parse_options, + struct conf_tool_context **result); + +int conf_tool_run(struct conf_tool_context *ctx, int *result); + +#endif /* __CTDB_CONF_TOOL_H__ */ diff --git a/ctdb/common/ctdb_io.c b/ctdb/common/ctdb_io.c new file mode 100644 index 0000000..bf8bc73 --- /dev/null +++ b/ctdb/common/ctdb_io.c @@ -0,0 +1,498 @@ +/* + ctdb database library + Utility functions to read/write blobs of data from a file descriptor + and handle the case where we might need multiple read/writes to get all the + data. + + Copyright (C) Andrew Tridgell 2006 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "lib/util/dlinklist.h" +#include "lib/util/debug.h" +#include "lib/util/sys_rw.h" + +#include "ctdb_private.h" +#include "ctdb_client.h" + +#include "common/logging.h" +#include "common/common.h" + +/* structures for packet queueing - see common/ctdb_io.c */ +struct ctdb_buffer { + uint8_t *data; + uint32_t length; + uint32_t size; + uint32_t offset; +}; + +struct ctdb_queue_pkt { + struct ctdb_queue_pkt *next, *prev; + uint8_t *data; + uint32_t length; + uint32_t full_length; + uint8_t buf[]; +}; + +struct ctdb_queue { + struct ctdb_context *ctdb; + struct tevent_immediate *im; + struct ctdb_buffer buffer; /* input buffer */ + struct ctdb_queue_pkt *out_queue, *out_queue_tail; + uint32_t out_queue_length; + struct tevent_fd *fde; + int fd; + size_t alignment; + void *private_data; + ctdb_queue_cb_fn_t callback; + TALLOC_CTX *data_pool; + const char *name; + uint32_t buffer_size; +}; + + + +uint32_t ctdb_queue_length(struct ctdb_queue *queue) +{ + return queue->out_queue_length; +} + +static void queue_process(struct ctdb_queue *queue); + +static void queue_process_event(struct tevent_context *ev, struct tevent_immediate *im, + void *private_data) +{ + struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue); + + queue_process(queue); +} + +/* + * This function is used to process data in queue buffer. + * + * Queue callback function can end up freeing the queue, there should not be a + * loop processing packets from queue buffer. Instead set up a timed event for + * immediate run to process remaining packets from buffer. + */ +static void queue_process(struct ctdb_queue *queue) +{ + uint32_t pkt_size; + uint8_t *data = NULL; + + if (queue->buffer.length < sizeof(pkt_size)) { + return; + } + + /* Did we at least read the size into the buffer */ + pkt_size = *(uint32_t *)(queue->buffer.data + queue->buffer.offset); + if (pkt_size == 0) { + DEBUG(DEBUG_CRIT, ("Invalid packet of length 0\n")); + goto failed; + } + + /* the buffer doesn't contain the full packet, return to get the rest */ + if (queue->buffer.length < pkt_size) { + return; + } + + /* Extract complete packet */ + data = talloc_memdup(queue->data_pool, + queue->buffer.data + queue->buffer.offset, + pkt_size); + + if (data == NULL) { + D_ERR("read error alloc failed for %u\n", pkt_size); + return; + } + + queue->buffer.offset += pkt_size; + queue->buffer.length -= pkt_size; + + if (queue->buffer.offset < pkt_size || + queue->buffer.offset > queue->buffer.size) { + D_ERR("buffer offset overflow\n"); + TALLOC_FREE(queue->buffer.data); + memset(&queue->buffer, 0, sizeof(queue->buffer)); + goto failed; + } + + if (queue->buffer.length > 0) { + /* There is more data to be processed, schedule an event */ + tevent_schedule_immediate(queue->im, queue->ctdb->ev, + queue_process_event, queue); + } else { + if (queue->buffer.size > queue->buffer_size) { + TALLOC_FREE(queue->buffer.data); + queue->buffer.size = 0; + } + queue->buffer.offset = 0; + } + + /* It is the responsibility of the callback to free 'data' */ + queue->callback(data, pkt_size, queue->private_data); + return; + +failed: + queue->callback(NULL, 0, queue->private_data); +} + +/* + called when an incoming connection is readable + This function MUST be safe for reentry via the queue callback! +*/ +static void queue_io_read(struct ctdb_queue *queue) +{ + int num_ready = 0; + uint32_t pkt_size = 0; + uint32_t start_offset; + ssize_t nread; + uint8_t *data; + + /* check how much data is available on the socket for immediately + guaranteed nonblocking access. + as long as we are careful never to try to read more than this + we know all reads will be successful and will neither block + nor fail with a "data not available right now" error + */ + if (ioctl(queue->fd, FIONREAD, &num_ready) != 0) { + return; + } + if (num_ready == 0) { + /* the descriptor has been closed */ + goto failed; + } + + if (queue->buffer.data == NULL) { + /* starting fresh, allocate buf to read data */ + queue->buffer.data = talloc_size(queue, queue->buffer_size); + if (queue->buffer.data == NULL) { + DEBUG(DEBUG_ERR, ("read error alloc failed for %u\n", num_ready)); + goto failed; + } + queue->buffer.size = queue->buffer_size; + goto data_read; + } + + if (sizeof(pkt_size) > queue->buffer.length) { + /* data read is not sufficient to gather message size */ + goto buffer_shift; + } + + pkt_size = *(uint32_t *)(queue->buffer.data + queue->buffer.offset); + if (pkt_size > queue->buffer.size) { + data = talloc_realloc_size(queue, + queue->buffer.data, + pkt_size); + if (data == NULL) { + DBG_ERR("read error realloc failed for %u\n", pkt_size); + goto failed; + } + queue->buffer.data = data; + queue->buffer.size = pkt_size; + /* fall through here as we might need to move the data as well */ + } + +buffer_shift: + if (sizeof(pkt_size) > queue->buffer.size - queue->buffer.offset || + pkt_size > queue->buffer.size - queue->buffer.offset) { + /* Either the offset has progressed too far to host at least + * the size information or the remaining space in the buffer + * is not sufficient for the full message. + * Therefore, move the data and try again. + */ + memmove(queue->buffer.data, + queue->buffer.data + queue->buffer.offset, + queue->buffer.length); + queue->buffer.offset = 0; + } + +data_read: + start_offset = queue->buffer.length + queue->buffer.offset; + if (start_offset < queue->buffer.length) { + DBG_ERR("Buffer overflow\n"); + goto failed; + } + if (start_offset > queue->buffer.size) { + DBG_ERR("Buffer overflow\n"); + goto failed; + } + + num_ready = MIN(num_ready, queue->buffer.size - start_offset); + + if (num_ready > 0) { + nread = sys_read(queue->fd, + queue->buffer.data + + queue->buffer.offset + + queue->buffer.length, + num_ready); + if (nread <= 0) { + DEBUG(DEBUG_ERR, ("read error nread=%d\n", (int)nread)); + goto failed; + } + queue->buffer.length += nread; + } + + queue_process(queue); + return; + +failed: + queue->callback(NULL, 0, queue->private_data); +} + + +/* used when an event triggers a dead queue */ +static void queue_dead(struct tevent_context *ev, struct tevent_immediate *im, + void *private_data) +{ + struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue); + queue->callback(NULL, 0, queue->private_data); +} + + +/* + called when an incoming connection is writeable +*/ +static void queue_io_write(struct ctdb_queue *queue) +{ + while (queue->out_queue) { + struct ctdb_queue_pkt *pkt = queue->out_queue; + ssize_t n; + if (queue->ctdb->flags & CTDB_FLAG_TORTURE) { + n = write(queue->fd, pkt->data, 1); + } else { + n = write(queue->fd, pkt->data, pkt->length); + } + + if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) { + if (pkt->length != pkt->full_length) { + /* partial packet sent - we have to drop it */ + DLIST_REMOVE(queue->out_queue, pkt); + queue->out_queue_length--; + talloc_free(pkt); + } + TALLOC_FREE(queue->fde); + queue->fd = -1; + tevent_schedule_immediate(queue->im, queue->ctdb->ev, + queue_dead, queue); + return; + } + if (n <= 0) return; + + if (n != pkt->length) { + pkt->length -= n; + pkt->data += n; + return; + } + + DLIST_REMOVE(queue->out_queue, pkt); + queue->out_queue_length--; + talloc_free(pkt); + } + + TEVENT_FD_NOT_WRITEABLE(queue->fde); +} + +/* + called when an incoming connection is readable or writeable +*/ +static void queue_io_handler(struct tevent_context *ev, struct tevent_fd *fde, + uint16_t flags, void *private_data) +{ + struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue); + + if (flags & TEVENT_FD_READ) { + queue_io_read(queue); + } else { + queue_io_write(queue); + } +} + + +/* + queue a packet for sending +*/ +int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length) +{ + struct ctdb_req_header *hdr = (struct ctdb_req_header *)data; + struct ctdb_queue_pkt *pkt; + uint32_t length2, full_length; + + /* If the queue does not have valid fd, no point queueing a packet */ + if (queue->fd == -1) { + return 0; + } + + if (queue->alignment) { + /* enforce the length and alignment rules from the tcp packet allocator */ + length2 = (length+(queue->alignment-1)) & ~(queue->alignment-1); + *(uint32_t *)data = length2; + } else { + length2 = length; + } + + if (length2 != length) { + memset(data+length, 0, length2-length); + } + + full_length = length2; + + /* if the queue is empty then try an immediate write, avoiding + queue overhead. This relies on non-blocking sockets */ + if (queue->out_queue == NULL && queue->fd != -1 && + !(queue->ctdb->flags & CTDB_FLAG_TORTURE)) { + ssize_t n = write(queue->fd, data, length2); + if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) { + TALLOC_FREE(queue->fde); + queue->fd = -1; + tevent_schedule_immediate(queue->im, queue->ctdb->ev, + queue_dead, queue); + /* yes, we report success, as the dead node is + handled via a separate event */ + return 0; + } + if (n > 0) { + data += n; + length2 -= n; + } + if (length2 == 0) return 0; + } + + pkt = talloc_size( + queue, offsetof(struct ctdb_queue_pkt, buf) + length2); + CTDB_NO_MEMORY(queue->ctdb, pkt); + talloc_set_name_const(pkt, "struct ctdb_queue_pkt"); + + pkt->data = pkt->buf; + memcpy(pkt->data, data, length2); + + pkt->length = length2; + pkt->full_length = full_length; + + if (queue->out_queue == NULL && queue->fd != -1) { + TEVENT_FD_WRITEABLE(queue->fde); + } + + DLIST_ADD_END(queue->out_queue, pkt); + + queue->out_queue_length++; + + if (queue->ctdb->tunable.verbose_memory_names != 0) { + switch (hdr->operation) { + case CTDB_REQ_CONTROL: { + struct ctdb_req_control_old *c = (struct ctdb_req_control_old *)hdr; + talloc_set_name(pkt, "ctdb_queue_pkt: %s control opcode=%u srvid=%llu datalen=%u", + queue->name, (unsigned)c->opcode, (unsigned long long)c->srvid, (unsigned)c->datalen); + break; + } + case CTDB_REQ_MESSAGE: { + struct ctdb_req_message_old *m = (struct ctdb_req_message_old *)hdr; + talloc_set_name(pkt, "ctdb_queue_pkt: %s message srvid=%llu datalen=%u", + queue->name, (unsigned long long)m->srvid, (unsigned)m->datalen); + break; + } + default: + talloc_set_name(pkt, "ctdb_queue_pkt: %s operation=%u length=%u src=%u dest=%u", + queue->name, (unsigned)hdr->operation, (unsigned)hdr->length, + (unsigned)hdr->srcnode, (unsigned)hdr->destnode); + break; + } + } + + return 0; +} + + +/* + setup the fd used by the queue + */ +int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd) +{ + queue->fd = fd; + TALLOC_FREE(queue->fde); + + if (fd != -1) { + queue->fde = tevent_add_fd(queue->ctdb->ev, queue, fd, + TEVENT_FD_READ, + queue_io_handler, queue); + if (queue->fde == NULL) { + return -1; + } + tevent_fd_set_auto_close(queue->fde); + + if (queue->out_queue) { + TEVENT_FD_WRITEABLE(queue->fde); + } + } + + return 0; +} + +/* + setup a packet queue on a socket + */ +struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb, + TALLOC_CTX *mem_ctx, int fd, int alignment, + ctdb_queue_cb_fn_t callback, + void *private_data, const char *fmt, ...) +{ + struct ctdb_queue *queue; + va_list ap; + + queue = talloc_zero(mem_ctx, struct ctdb_queue); + CTDB_NO_MEMORY_NULL(ctdb, queue); + va_start(ap, fmt); + queue->name = talloc_vasprintf(mem_ctx, fmt, ap); + va_end(ap); + CTDB_NO_MEMORY_NULL(ctdb, queue->name); + + queue->im= tevent_create_immediate(queue); + CTDB_NO_MEMORY_NULL(ctdb, queue->im); + + queue->ctdb = ctdb; + queue->fd = fd; + queue->alignment = alignment; + queue->private_data = private_data; + queue->callback = callback; + if (fd != -1) { + if (ctdb_queue_set_fd(queue, fd) != 0) { + talloc_free(queue); + return NULL; + } + } + + queue->buffer_size = ctdb->tunable.queue_buffer_size; + /* In client code, ctdb->tunable is not initialized. + * This does not affect recovery daemon. + */ + if (queue->buffer_size == 0) { + queue->buffer_size = 1024; + } + + queue->data_pool = talloc_pool(queue, queue->buffer_size); + if (queue->data_pool == NULL) { + TALLOC_FREE(queue); + return NULL; + } + + return queue; +} diff --git a/ctdb/common/ctdb_ltdb.c b/ctdb/common/ctdb_ltdb.c new file mode 100644 index 0000000..0b79ab4 --- /dev/null +++ b/ctdb/common/ctdb_ltdb.c @@ -0,0 +1,430 @@ +/* + ctdb ltdb code + + Copyright (C) Andrew Tridgell 2006 + Copyright (C) Ronnie sahlberg 2011 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" + +#include + +#include "lib/tdb_wrap/tdb_wrap.h" +#include "lib/util/dlinklist.h" +#include "lib/util/debug.h" + +#include "ctdb_private.h" + +#include "common/common.h" +#include "common/logging.h" + + +/* + * Calculate tdb flags based on databse type + */ +int ctdb_db_tdb_flags(uint8_t db_flags, bool with_valgrind, bool with_mutex) +{ + int tdb_flags = 0; + + if (db_flags & CTDB_DB_FLAGS_PERSISTENT) { + tdb_flags = TDB_DEFAULT; + + } else if (db_flags & CTDB_DB_FLAGS_REPLICATED) { + tdb_flags = TDB_NOSYNC | + TDB_CLEAR_IF_FIRST | + TDB_INCOMPATIBLE_HASH; + + } else { + tdb_flags = TDB_NOSYNC | + TDB_CLEAR_IF_FIRST | + TDB_INCOMPATIBLE_HASH; + +#ifdef TDB_MUTEX_LOCKING + if (with_mutex && tdb_runtime_check_for_robust_mutexes()) { + tdb_flags |= TDB_MUTEX_LOCKING; + } +#endif + + } + + tdb_flags |= TDB_DISALLOW_NESTING; + if (with_valgrind) { + tdb_flags |= TDB_NOMMAP; + } + + return tdb_flags; +} + +/* + find an attached ctdb_db handle given a name + */ +struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name) +{ + struct ctdb_db_context *tmp_db; + for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) { + if (strcmp(name, tmp_db->db_name) == 0) { + return tmp_db; + } + } + return NULL; +} + +bool ctdb_db_persistent(struct ctdb_db_context *ctdb_db) +{ + if (ctdb_db->db_flags & CTDB_DB_FLAGS_PERSISTENT) { + return true; + } + return false; +} + +bool ctdb_db_replicated(struct ctdb_db_context *ctdb_db) +{ + if (ctdb_db->db_flags & CTDB_DB_FLAGS_REPLICATED) { + return true; + } + return false; +} + +bool ctdb_db_volatile(struct ctdb_db_context *ctdb_db) +{ + if ((ctdb_db->db_flags & CTDB_DB_FLAGS_PERSISTENT) || + (ctdb_db->db_flags & CTDB_DB_FLAGS_REPLICATED)) { + return false; + } + return true; +} + +bool ctdb_db_readonly(struct ctdb_db_context *ctdb_db) +{ + if (ctdb_db->db_flags & CTDB_DB_FLAGS_READONLY) { + return true; + } + return false; +} + +void ctdb_db_set_readonly(struct ctdb_db_context *ctdb_db) +{ + ctdb_db->db_flags |= CTDB_DB_FLAGS_READONLY; +} + +void ctdb_db_reset_readonly(struct ctdb_db_context *ctdb_db) +{ + ctdb_db->db_flags &= ~CTDB_DB_FLAGS_READONLY; +} + +bool ctdb_db_sticky(struct ctdb_db_context *ctdb_db) +{ + if (ctdb_db->db_flags & CTDB_DB_FLAGS_STICKY) { + return true; + } + return false; +} + +void ctdb_db_set_sticky(struct ctdb_db_context *ctdb_db) +{ + ctdb_db->db_flags |= CTDB_DB_FLAGS_STICKY; +} + +/* + return the lmaster given a key +*/ +uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key) +{ + uint32_t idx, lmaster; + + idx = ctdb_hash(key) % ctdb->vnn_map->size; + lmaster = ctdb->vnn_map->map[idx]; + + return lmaster; +} + + +/* + construct an initial header for a record with no ltdb header yet +*/ +static void ltdb_initial_header(struct ctdb_db_context *ctdb_db, + TDB_DATA key, + struct ctdb_ltdb_header *header) +{ + ZERO_STRUCTP(header); + /* initial dmaster is the lmaster */ + header->dmaster = ctdb_lmaster(ctdb_db->ctdb, &key); + header->flags = CTDB_REC_FLAG_AUTOMATIC; +} + +struct ctdb_ltdb_fetch_state { + struct ctdb_ltdb_header *header; + TALLOC_CTX *mem_ctx; + TDB_DATA *data; + int ret; + bool found; +}; + +static int ctdb_ltdb_fetch_fn(TDB_DATA key, TDB_DATA data, void *private_data) +{ + struct ctdb_ltdb_fetch_state *state = private_data; + struct ctdb_ltdb_header *header = state->header; + TDB_DATA *dstdata = state->data; + + if (data.dsize < sizeof(*header)) { + return 0; + } + + state->found = true; + memcpy(header, data.dptr, sizeof(*header)); + + if (dstdata != NULL) { + dstdata->dsize = data.dsize - sizeof(struct ctdb_ltdb_header); + dstdata->dptr = talloc_memdup( + state->mem_ctx, + data.dptr + sizeof(struct ctdb_ltdb_header), + dstdata->dsize); + if (dstdata->dptr == NULL) { + state->ret = -1; + } + } + + return 0; +} + +/* + fetch a record from the ltdb, separating out the header information + and returning the body of the record. A valid (initial) header is + returned if the record is not present +*/ +int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, + TDB_DATA key, struct ctdb_ltdb_header *header, + TALLOC_CTX *mem_ctx, TDB_DATA *data) +{ + struct ctdb_context *ctdb = ctdb_db->ctdb; + struct ctdb_ltdb_fetch_state state = { + .header = header, + .mem_ctx = mem_ctx, + .data = data, + .found = false, + }; + int ret; + + ret = tdb_parse_record( + ctdb_db->ltdb->tdb, key, ctdb_ltdb_fetch_fn, &state); + + if (ret == -1) { + enum TDB_ERROR err = tdb_error(ctdb_db->ltdb->tdb); + if (err != TDB_ERR_NOEXIST) { + return -1; + } + } + + if (state.ret != 0) { + DBG_DEBUG("ctdb_ltdb_fetch_fn failed\n"); + return state.ret; + } + + if (state.found) { + return 0; + } + + if (data != NULL) { + *data = tdb_null; + } + + if (ctdb->vnn_map == NULL) { + /* called from the client */ + header->dmaster = (uint32_t)-1; + return -1; + } + + ltdb_initial_header(ctdb_db, key, header); + if (ctdb_db_persistent(ctdb_db) || + header->dmaster == ctdb_db->ctdb->pnn) { + + ret = ctdb_ltdb_store(ctdb_db, key, header, tdb_null); + if (ret != 0) { + DBG_NOTICE("failed to store initial header\n"); + } + } + + return 0; +} + +/* + write a record to a normal database +*/ +int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, + struct ctdb_ltdb_header *header, TDB_DATA data) +{ + struct ctdb_context *ctdb = ctdb_db->ctdb; + TDB_DATA rec[2]; + uint32_t hsize = sizeof(struct ctdb_ltdb_header); + int ret; + + if (ctdb_db->ctdb_ltdb_store_fn) { + return ctdb_db->ctdb_ltdb_store_fn(ctdb_db, key, header, data); + } + + if (ctdb->flags & CTDB_FLAG_TORTURE) { + TDB_DATA old; + struct ctdb_ltdb_header *h2; + + old = tdb_fetch(ctdb_db->ltdb->tdb, key); + h2 = (struct ctdb_ltdb_header *)old.dptr; + if (old.dptr != NULL && old.dsize >= hsize && + h2->rsn > header->rsn) { + DEBUG(DEBUG_ERR, + ("RSN regression! %"PRIu64" %"PRIu64"\n", + h2->rsn, header->rsn)); + } + if (old.dptr != NULL) { + free(old.dptr); + } + } + + rec[0].dsize = hsize; + rec[0].dptr = (uint8_t *)header; + + rec[1].dsize = data.dsize; + rec[1].dptr = data.dptr; + + ret = tdb_storev(ctdb_db->ltdb->tdb, key, rec, 2, TDB_REPLACE); + if (ret != 0) { + DEBUG(DEBUG_ERR, (__location__ " Failed to store dynamic data\n")); + } + + return ret; +} + +/* + lock a record in the ltdb, given a key + */ +int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key) +{ + return tdb_chainlock(ctdb_db->ltdb->tdb, key); +} + +/* + unlock a record in the ltdb, given a key + */ +int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key) +{ + int ret = tdb_chainunlock(ctdb_db->ltdb->tdb, key); + if (ret != 0) { + DEBUG(DEBUG_ERR,("tdb_chainunlock failed on db %s [%s]\n", ctdb_db->db_name, tdb_errorstr(ctdb_db->ltdb->tdb))); + } + return ret; +} + + +/* + delete a record from a normal database +*/ +int ctdb_ltdb_delete(struct ctdb_db_context *ctdb_db, TDB_DATA key) +{ + if (! ctdb_db_volatile(ctdb_db)) { + DEBUG(DEBUG_WARNING, + ("Ignored deletion of empty record from " + "non-volatile database\n")); + return 0; + } + if (tdb_delete(ctdb_db->ltdb->tdb, key) != 0) { + DEBUG(DEBUG_ERR,("Failed to delete empty record.")); + return -1; + } + return 0; +} + +int ctdb_trackingdb_add_pnn(struct ctdb_context *ctdb, TDB_DATA *data, uint32_t pnn) +{ + unsigned int byte_pos = pnn / 8; + unsigned char bit_mask = 1 << (pnn % 8); + + if (byte_pos + 1 > data->dsize) { + char *buf; + + buf = malloc(byte_pos + 1); + memset(buf, 0, byte_pos + 1); + if (buf == NULL) { + DEBUG(DEBUG_ERR, ("Out of memory when allocating buffer of %d bytes for trackingdb\n", byte_pos + 1)); + return -1; + } + if (data->dptr != NULL) { + memcpy(buf, data->dptr, data->dsize); + free(data->dptr); + } + data->dptr = (uint8_t *)buf; + data->dsize = byte_pos + 1; + } + + data->dptr[byte_pos] |= bit_mask; + return 0; +} + +void ctdb_trackingdb_traverse(struct ctdb_context *ctdb, TDB_DATA data, ctdb_trackingdb_cb cb, void *private_data) +{ + unsigned int i; + + for(i = 0; i < data.dsize; i++) { + unsigned int j; + + for (j=0; j<8; j++) { + int mask = 1<reply_data = &call->record_data; + return 0; +} + +/* + this is a plain fetch procedure that all databases support + this returns the full record including the ltdb header +*/ +int ctdb_fetch_with_header_func(struct ctdb_call_info *call) +{ + call->reply_data = talloc(call, TDB_DATA); + if (call->reply_data == NULL) { + return -1; + } + call->reply_data->dsize = sizeof(struct ctdb_ltdb_header) + call->record_data.dsize; + call->reply_data->dptr = talloc_size(call->reply_data, call->reply_data->dsize); + if (call->reply_data->dptr == NULL) { + return -1; + } + memcpy(call->reply_data->dptr, call->header, sizeof(struct ctdb_ltdb_header)); + memcpy(&call->reply_data->dptr[sizeof(struct ctdb_ltdb_header)], call->record_data.dptr, call->record_data.dsize); + + return 0; +} + diff --git a/ctdb/common/ctdb_util.c b/ctdb/common/ctdb_util.c new file mode 100644 index 0000000..3f8fff9 --- /dev/null +++ b/ctdb/common/ctdb_util.c @@ -0,0 +1,674 @@ +/* + ctdb utility code + + Copyright (C) Andrew Tridgell 2006 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/filesys.h" +#include "system/wait.h" + +#include + +#include "lib/util/debug.h" +#include "lib/util/samba_util.h" + +#include "ctdb_private.h" + +#include "protocol/protocol_util.h" + +#include "common/reqid.h" +#include "common/system.h" +#include "common/common.h" +#include "common/logging.h" + +/* + return error string for last error +*/ +const char *ctdb_errstr(struct ctdb_context *ctdb) +{ + return ctdb->err_msg; +} + + +/* + remember an error message +*/ +void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) +{ + va_list ap; + talloc_free(ctdb->err_msg); + va_start(ap, fmt); + ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap); + DEBUG(DEBUG_ERR,("ctdb error: %s\n", ctdb->err_msg)); + va_end(ap); +} + +/* + a fatal internal error occurred - no hope for recovery +*/ +void ctdb_fatal(struct ctdb_context *ctdb, const char *msg) +{ + DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", msg)); + abort(); +} + +/* + like ctdb_fatal() but a core/backtrace would not be useful +*/ +void ctdb_die(struct ctdb_context *ctdb, const char *msg) +{ + DEBUG(DEBUG_ALERT,("ctdb exiting with error: %s\n", msg)); + exit(1); +} + +/* Set the path of a helper program from envvar, falling back to + * dir/file if envvar unset. type is a string to print in log + * messages. helper is assumed to point to a statically allocated + * array of size bytes, initialised to "". If file is NULL don't fall + * back if envvar is unset. If dir is NULL and envvar is unset (but + * file is not NULL) then this is an error. Returns true if helper is + * set, either previously or this time. */ +bool ctdb_set_helper(const char *type, char *helper, size_t size, + const char *envvar, + const char *dir, const char *file) +{ + const char *t; + struct stat st; + + if (helper[0] != '\0') { + /* Already set */ + return true; + } + + t = getenv(envvar); + if (t != NULL) { + if (strlen(t) >= size) { + DEBUG(DEBUG_ERR, + ("Unable to set %s - path too long\n", type)); + return false; + } + + strncpy(helper, t, size); + } else if (file == NULL) { + return false; + } else if (dir == NULL) { + DEBUG(DEBUG_ERR, + ("Unable to set %s - dir is NULL\n", type)); + return false; + } else { + int ret; + + ret = snprintf(helper, size, "%s/%s", dir, file); + if (ret < 0 || (size_t)ret >= size) { + DEBUG(DEBUG_ERR, + ("Unable to set %s - path too long\n", type)); + return false; + } + } + + if (stat(helper, &st) != 0) { + DEBUG(DEBUG_ERR, + ("Unable to set %s \"%s\" - %s\n", + type, helper, strerror(errno))); + return false; + } + if (!(st.st_mode & S_IXUSR)) { + DEBUG(DEBUG_ERR, + ("Unable to set %s \"%s\" - not executable\n", + type, helper)); + return false; + } + + DEBUG(DEBUG_NOTICE, + ("Set %s to \"%s\"\n", type, helper)); + return true; +} + +/* + parse a IP:port pair +*/ +int ctdb_parse_address(TALLOC_CTX *mem_ctx, const char *str, + ctdb_sock_addr *address) +{ + struct servent *se; + int port; + int ret; + + setservent(0); + se = getservbyname("ctdb", "tcp"); + endservent(); + + if (se == NULL) { + port = CTDB_PORT; + } else { + port = ntohs(se->s_port); + } + + ret = ctdb_sock_addr_from_string(str, address, false); + if (ret != 0) { + return -1; + } + ctdb_sock_addr_set_port(address, port); + + return 0; +} + + +/* + check if two addresses are the same +*/ +bool ctdb_same_address(ctdb_sock_addr *a1, ctdb_sock_addr *a2) +{ + return ctdb_same_ip(a1, a2) && + ctdb_addr_to_port(a1) == ctdb_addr_to_port(a2); +} + + +/* + hash function for mapping data to a VNN - taken from tdb +*/ +uint32_t ctdb_hash(const TDB_DATA *key) +{ + return tdb_jenkins_hash(discard_const(key)); +} + + +static uint32_t ctdb_marshall_record_size(TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA data) +{ + return offsetof(struct ctdb_rec_data_old, data) + key.dsize + + data.dsize + (header ? sizeof(*header) : 0); +} + +static void ctdb_marshall_record_copy(struct ctdb_rec_data_old *rec, + uint32_t reqid, + TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA data, + uint32_t length) +{ + uint32_t offset; + + rec->length = length; + rec->reqid = reqid; + rec->keylen = key.dsize; + memcpy(&rec->data[0], key.dptr, key.dsize); + offset = key.dsize; + + if (header) { + rec->datalen = data.dsize + sizeof(*header); + memcpy(&rec->data[offset], header, sizeof(*header)); + offset += sizeof(*header); + } else { + rec->datalen = data.dsize; + } + memcpy(&rec->data[offset], data.dptr, data.dsize); +} + +/* + form a ctdb_rec_data record from a key/data pair + + note that header may be NULL. If not NULL then it is included in the data portion + of the record + */ +struct ctdb_rec_data_old *ctdb_marshall_record(TALLOC_CTX *mem_ctx, + uint32_t reqid, + TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA data) +{ + size_t length; + struct ctdb_rec_data_old *d; + + length = ctdb_marshall_record_size(key, header, data); + + d = (struct ctdb_rec_data_old *)talloc_size(mem_ctx, length); + if (d == NULL) { + return NULL; + } + + ctdb_marshall_record_copy(d, reqid, key, header, data, length); + return d; +} + + +/* helper function for marshalling multiple records */ +struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx, + struct ctdb_marshall_buffer *m, + uint32_t db_id, + uint32_t reqid, + TDB_DATA key, + struct ctdb_ltdb_header *header, + TDB_DATA data) +{ + struct ctdb_rec_data_old *r; + struct ctdb_marshall_buffer *m2; + uint32_t length, offset; + + length = ctdb_marshall_record_size(key, header, data); + + if (m == NULL) { + offset = offsetof(struct ctdb_marshall_buffer, data); + m2 = talloc_zero_size(mem_ctx, offset + length); + } else { + offset = talloc_get_size(m); + m2 = talloc_realloc_size(mem_ctx, m, offset + length); + } + if (m2 == NULL) { + TALLOC_FREE(m); + return NULL; + } + + if (m == NULL) { + m2->db_id = db_id; + } + + r = (struct ctdb_rec_data_old *)((uint8_t *)m2 + offset); + ctdb_marshall_record_copy(r, reqid, key, header, data, length); + m2->count++; + + return m2; +} + +/* we've finished marshalling, return a data blob with the marshalled records */ +TDB_DATA ctdb_marshall_finish(struct ctdb_marshall_buffer *m) +{ + TDB_DATA data; + data.dptr = (uint8_t *)m; + data.dsize = talloc_get_size(m); + return data; +} + +/* + loop over a marshalling buffer + + - pass r==NULL to start + - loop the number of times indicated by m->count +*/ +struct ctdb_rec_data_old *ctdb_marshall_loop_next( + struct ctdb_marshall_buffer *m, + struct ctdb_rec_data_old *r, + uint32_t *reqid, + struct ctdb_ltdb_header *header, + TDB_DATA *key, TDB_DATA *data) +{ + if (r == NULL) { + r = (struct ctdb_rec_data_old *)&m->data[0]; + } else { + r = (struct ctdb_rec_data_old *)(r->length + (uint8_t *)r); + } + + if (reqid != NULL) { + *reqid = r->reqid; + } + + if (key != NULL) { + key->dptr = &r->data[0]; + key->dsize = r->keylen; + } + if (data != NULL) { + data->dptr = &r->data[r->keylen]; + data->dsize = r->datalen; + if (header != NULL) { + data->dptr += sizeof(*header); + data->dsize -= sizeof(*header); + } + } + + if (header != NULL) { + if (r->datalen < sizeof(*header)) { + return NULL; + } + memcpy(header, &r->data[r->keylen], sizeof(*header)); + } + + return r; +} + +/* + This is used to canonicalize a ctdb_sock_addr structure. +*/ +void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip) +{ + ZERO_STRUCTP(cip); + + if (ip->sa.sa_family == AF_INET6) { + const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff }; + if (memcmp(&ip->ip6.sin6_addr, prefix, sizeof(prefix)) == 0) { + /* Copy IPv4-mapped IPv6 addresses as IPv4 */ + cip->ip.sin_family = AF_INET; +#ifdef HAVE_SOCK_SIN_LEN + cip->ip.sin_len = sizeof(ctdb_sock_addr); +#endif + cip->ip.sin_port = ip->ip6.sin6_port; + memcpy(&cip->ip.sin_addr, + &ip->ip6.sin6_addr.s6_addr[12], + sizeof(cip->ip.sin_addr)); + } else { + cip->ip6.sin6_family = AF_INET6; +#ifdef HAVE_SOCK_SIN6_LEN + cip->ip6.sin6_len = sizeof(ctdb_sock_addr); +#endif + cip->ip6.sin6_port = ip->ip6.sin6_port; + memcpy(&cip->ip6.sin6_addr, + &ip->ip6.sin6_addr, + sizeof(cip->ip6.sin6_addr)); + } + + return; + } + + if (ip->sa.sa_family == AF_INET) { + cip->ip.sin_family = AF_INET; +#ifdef HAVE_SOCK_SIN_LEN + cip->ip.sin_len = sizeof(ctdb_sock_addr); +#endif + cip->ip.sin_port = ip->ip.sin_port; + memcpy(&cip->ip.sin_addr, + &ip->ip.sin_addr, + sizeof(ip->ip.sin_addr)); + + return; + } +} + +bool ctdb_same_ip(const ctdb_sock_addr *tip1, const ctdb_sock_addr *tip2) +{ + ctdb_sock_addr ip1, ip2; + + ctdb_canonicalize_ip(tip1, &ip1); + ctdb_canonicalize_ip(tip2, &ip2); + + if (ip1.sa.sa_family != ip2.sa.sa_family) { + return false; + } + + switch (ip1.sa.sa_family) { + case AF_INET: + return ip1.ip.sin_addr.s_addr == ip2.ip.sin_addr.s_addr; + case AF_INET6: + return !memcmp(&ip1.ip6.sin6_addr.s6_addr[0], + &ip2.ip6.sin6_addr.s6_addr[0], + 16); + default: + DEBUG(DEBUG_ERR, (__location__ " CRITICAL Can not compare sockaddr structures of type %u\n", ip1.sa.sa_family)); + return false; + } + + return true; +} + +/* + compare two ctdb_sock_addr structures + */ +bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2) +{ + return ctdb_same_ip(ip1, ip2) && ip1->ip.sin_port == ip2->ip.sin_port; +} + +char *ctdb_addr_to_str(ctdb_sock_addr *addr) +{ + static char cip[128] = ""; + + switch (addr->sa.sa_family) { + case AF_INET: + inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr, cip, sizeof(cip)); + break; + case AF_INET6: + inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr, cip, sizeof(cip)); + break; + default: + DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family %u\n", addr->sa.sa_family)); + } + + return cip; +} + +unsigned ctdb_addr_to_port(ctdb_sock_addr *addr) +{ + switch (addr->sa.sa_family) { + case AF_INET: + return ntohs(addr->ip.sin_port); + break; + case AF_INET6: + return ntohs(addr->ip6.sin6_port); + break; + default: + DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family %u\n", addr->sa.sa_family)); + } + + return 0; +} + +/* Add a node to a node map with given address and flags */ +static bool node_map_add(TALLOC_CTX *mem_ctx, + const char *nstr, uint32_t flags, + struct ctdb_node_map_old **node_map) +{ + ctdb_sock_addr addr; + uint32_t num; + size_t s; + struct ctdb_node_and_flags *n; + + /* Might as well do this before trying to allocate memory */ + if (ctdb_parse_address(mem_ctx, nstr, &addr) == -1) { + return false; + } + + num = (*node_map)->num + 1; + s = offsetof(struct ctdb_node_map_old, nodes) + + num * sizeof(struct ctdb_node_and_flags); + *node_map = talloc_realloc_size(mem_ctx, *node_map, s); + if (*node_map == NULL) { + DEBUG(DEBUG_ERR, (__location__ " Out of memory\n")); + return false; + } + + n = &(*node_map)->nodes[(*node_map)->num]; + n->addr = addr; + n->pnn = (*node_map)->num; + n->flags = flags; + + (*node_map)->num++; + + return true; +} + +/* Read a nodes file into a node map */ +struct ctdb_node_map_old *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx, + const char *nlist) +{ + char **lines; + int nlines; + int i; + struct ctdb_node_map_old *ret; + + /* Allocate node map header */ + ret = talloc_zero_size(mem_ctx, offsetof(struct ctdb_node_map_old, nodes)); + if (ret == NULL) { + DEBUG(DEBUG_ERR, (__location__ " Out of memory\n")); + return false; + } + + lines = file_lines_load(nlist, &nlines, 0, mem_ctx); + if (lines == NULL) { + DEBUG(DEBUG_ERR, ("Failed to read nodes file \"%s\"\n", nlist)); + return false; + } + while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) { + nlines--; + } + + for (i=0; i < nlines; i++) { + char *node; + uint32_t flags; + size_t len; + + node = lines[i]; + /* strip leading spaces */ + while((*node == ' ') || (*node == '\t')) { + node++; + } + + len = strlen(node); + + while ((len > 1) && + ((node[len-1] == ' ') || (node[len-1] == '\t'))) + { + node[len-1] = '\0'; + len--; + } + + if (len == 0) { + continue; + } + if (*node == '#') { + /* A "deleted" node is a node that is + commented out in the nodes file. This is + used instead of removing a line, which + would cause subsequent nodes to change + their PNN. */ + flags = NODE_FLAGS_DELETED; + node = discard_const("0.0.0.0"); + } else { + flags = 0; + } + if (!node_map_add(mem_ctx, node, flags, &ret)) { + talloc_free(lines); + TALLOC_FREE(ret); + return NULL; + } + } + + talloc_free(lines); + return ret; +} + +struct ctdb_node_map_old * +ctdb_node_list_to_map(struct ctdb_node **nodes, uint32_t num_nodes, + TALLOC_CTX *mem_ctx) +{ + uint32_t i; + size_t size; + struct ctdb_node_map_old *node_map; + + size = offsetof(struct ctdb_node_map_old, nodes) + + num_nodes * sizeof(struct ctdb_node_and_flags); + node_map = (struct ctdb_node_map_old *)talloc_zero_size(mem_ctx, size); + if (node_map == NULL) { + DEBUG(DEBUG_ERR, + (__location__ " Failed to allocate nodemap array\n")); + return NULL; + } + + node_map->num = num_nodes; + for (i=0; inodes[i].addr = nodes[i]->address; + node_map->nodes[i].pnn = nodes[i]->pnn; + node_map->nodes[i].flags = nodes[i]->flags; + } + + return node_map; +} + +const char *ctdb_eventscript_call_names[] = { + "init", + "setup", + "startup", + "startrecovery", + "recovered", + "takeip", + "releaseip", + "stopped", + "monitor", + "status", + "shutdown", + "reload", + "updateip", + "ipreallocated" +}; + +/* Runstate handling */ +static struct { + enum ctdb_runstate runstate; + const char * label; +} runstate_map[] = { + { CTDB_RUNSTATE_UNKNOWN, "UNKNOWN" }, + { CTDB_RUNSTATE_INIT, "INIT" }, + { CTDB_RUNSTATE_SETUP, "SETUP" }, + { CTDB_RUNSTATE_FIRST_RECOVERY, "FIRST_RECOVERY" }, + { CTDB_RUNSTATE_STARTUP, "STARTUP" }, + { CTDB_RUNSTATE_RUNNING, "RUNNING" }, + { CTDB_RUNSTATE_SHUTDOWN, "SHUTDOWN" }, + { -1, NULL }, +}; + +const char *runstate_to_string(enum ctdb_runstate runstate) +{ + int i; + for (i=0; runstate_map[i].label != NULL ; i++) { + if (runstate_map[i].runstate == runstate) { + return runstate_map[i].label; + } + } + + return runstate_map[0].label; +} + +enum ctdb_runstate runstate_from_string(const char *label) +{ + int i; + for (i=0; runstate_map[i].label != NULL; i++) { + if (strcasecmp(runstate_map[i].label, label) == 0) { + return runstate_map[i].runstate; + } + } + + return CTDB_RUNSTATE_UNKNOWN; +} + +void ctdb_set_runstate(struct ctdb_context *ctdb, enum ctdb_runstate runstate) +{ + DEBUG(DEBUG_NOTICE,("Set runstate to %s (%d)\n", + runstate_to_string(runstate), runstate)); + + if (runstate <= ctdb->runstate) { + ctdb_fatal(ctdb, "runstate must always increase"); + } + + ctdb->runstate = runstate; +} + +/* Convert arbitrary data to 4-byte boundary padded uint32 array */ +uint32_t *ctdb_key_to_idkey(TALLOC_CTX *mem_ctx, TDB_DATA key) +{ + uint32_t idkey_size, *k; + + idkey_size = 1 + (key.dsize + sizeof(uint32_t)-1) / sizeof(uint32_t); + + k = talloc_zero_array(mem_ctx, uint32_t, idkey_size); + if (k == NULL) { + return NULL; + } + + k[0] = idkey_size; + memcpy(&k[1], key.dptr, key.dsize); + + return k; +} diff --git a/ctdb/common/db_hash.c b/ctdb/common/db_hash.c new file mode 100644 index 0000000..8dd62c4 --- /dev/null +++ b/ctdb/common/db_hash.c @@ -0,0 +1,295 @@ +/* + Using tdb as a hash table + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include +#include + +#include "common/db_hash.h" + +struct db_hash_context { + struct tdb_context *db; +}; + + +static int db_hash_destructor(struct db_hash_context *dh) +{ + if (dh->db != NULL) { + tdb_close(dh->db); + dh->db = NULL; + } + return 0; +} + +int db_hash_init(TALLOC_CTX *mem_ctx, const char *name, int hash_size, + enum db_hash_type type, struct db_hash_context **result) +{ + struct db_hash_context *dh; + int tdb_flags = TDB_INTERNAL | TDB_DISALLOW_NESTING; + + dh = talloc_zero(mem_ctx, struct db_hash_context); + if (dh == NULL) { + return ENOMEM; + } + + if (type == DB_HASH_COMPLEX) { + tdb_flags |= TDB_INCOMPATIBLE_HASH; + } + + dh->db = tdb_open(name, hash_size, tdb_flags, O_RDWR|O_CREAT, 0); + if (dh->db == NULL) { + talloc_free(dh); + return ENOMEM; + } + + talloc_set_destructor(dh, db_hash_destructor); + *result = dh; + return 0; +} + +static int db_hash_map_tdb_error(struct db_hash_context *dh) +{ + enum TDB_ERROR tdb_err; + int ret; + + tdb_err = tdb_error(dh->db); + switch (tdb_err) { + case TDB_SUCCESS: + ret = 0; break; + case TDB_ERR_OOM: + ret = ENOMEM; break; + case TDB_ERR_EXISTS: + ret = EEXIST; break; + case TDB_ERR_NOEXIST: + ret = ENOENT; break; + case TDB_ERR_EINVAL: + ret = EINVAL; break; + default: + ret = EIO; break; + } + return ret; +} + +int db_hash_insert(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen) +{ + TDB_DATA key, data; + int ret; + + if (dh == NULL) { + return EINVAL; + } + + key.dptr = keybuf; + key.dsize = keylen; + + data.dptr = databuf; + data.dsize = datalen; + + ret = tdb_store(dh->db, key, data, TDB_INSERT); + if (ret != 0) { + ret = db_hash_map_tdb_error(dh); + } + return ret; +} + +int db_hash_add(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen) +{ + TDB_DATA key, data; + int ret; + + if (dh == NULL) { + return EINVAL; + } + + key.dptr = keybuf; + key.dsize = keylen; + + data.dptr = databuf; + data.dsize = datalen; + + ret = tdb_store(dh->db, key, data, TDB_REPLACE); + if (ret != 0) { + ret = db_hash_map_tdb_error(dh); + } + return ret; +} + +int db_hash_delete(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen) +{ + TDB_DATA key; + int ret; + + key.dptr = keybuf; + key.dsize = keylen; + + if (dh == NULL) { + return EINVAL; + } + + ret = tdb_delete(dh->db, key); + if (ret != 0) { + ret = db_hash_map_tdb_error(dh); + } + return ret; +} + +struct db_hash_fetch_state { + db_hash_record_parser_fn parser; + void *private_data; +}; + +static int db_hash_fetch_parser(TDB_DATA key, TDB_DATA data, void *private_data) +{ + struct db_hash_fetch_state *state = + (struct db_hash_fetch_state *)private_data; + int ret; + + ret = state->parser(key.dptr, key.dsize, data.dptr, data.dsize, + state->private_data); + return ret; +} + +int db_hash_fetch(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen, + db_hash_record_parser_fn parser, void *private_data) +{ + struct db_hash_fetch_state state; + TDB_DATA key; + int ret; + + if (dh == NULL || parser == NULL) { + return EINVAL; + } + + state.parser = parser; + state.private_data = private_data; + + key.dptr = keybuf; + key.dsize = keylen; + + ret = tdb_parse_record(dh->db, key, db_hash_fetch_parser, &state); + if (ret == -1) { + return ENOENT; + } + return ret; +} + +int db_hash_exists(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen) +{ + TDB_DATA key; + int ret; + + if (dh == NULL) { + return EINVAL; + } + + key.dptr = keybuf; + key.dsize = keylen; + + ret = tdb_exists(dh->db, key); + if (ret == 1) { + /* Key found */ + ret = 0; + } else { + ret = db_hash_map_tdb_error(dh); + if (ret == 0) { + ret = ENOENT; + } + } + return ret; +} + +struct db_hash_traverse_state { + db_hash_record_parser_fn parser; + void *private_data; +}; + +static int db_hash_traverse_parser(struct tdb_context *tdb, + TDB_DATA key, TDB_DATA data, + void *private_data) +{ + struct db_hash_traverse_state *state = + (struct db_hash_traverse_state *)private_data; + + return state->parser(key.dptr, key.dsize, data.dptr, data.dsize, + state->private_data); +} + +int db_hash_traverse(struct db_hash_context *dh, + db_hash_record_parser_fn parser, void *private_data, + int *count) +{ + struct db_hash_traverse_state state; + int ret; + + if (dh == NULL) { + return EINVAL; + } + + /* Special case, for counting records */ + if (parser == NULL) { + ret = tdb_traverse_read(dh->db, NULL, NULL); + } else { + state.parser = parser; + state.private_data = private_data; + + ret = tdb_traverse_read(dh->db, db_hash_traverse_parser, &state); + } + + if (ret == -1) { + ret = db_hash_map_tdb_error(dh); + } else { + if (count != NULL) { + *count = ret; + } + ret = 0; + } + + return ret; +} + +int db_hash_traverse_update(struct db_hash_context *dh, + db_hash_record_parser_fn parser, + void *private_data, int *count) +{ + struct db_hash_traverse_state state; + int ret; + + if (dh == NULL || parser == NULL) { + return EINVAL; + } + + state.parser = parser; + state.private_data = private_data; + + ret = tdb_traverse(dh->db, db_hash_traverse_parser, &state); + if (ret == -1) { + ret = db_hash_map_tdb_error(dh); + } else { + if (count != NULL) { + *count = ret; + } + ret = 0; + } + + return ret; +} diff --git a/ctdb/common/db_hash.h b/ctdb/common/db_hash.h new file mode 100644 index 0000000..67e2b85 --- /dev/null +++ b/ctdb/common/db_hash.h @@ -0,0 +1,174 @@ +/* + Using tdb as a hash table + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_DB_HASH_H__ +#define __CTDB_DB_HASH_H__ + +#include +#include + +/** + * @file db_hash.h + * + * @brief Use tdb database as a hash table + * + * This uses in-memory tdb databases to create a fixed sized hash table. + */ + +/** + * @brief Hash type to indicate the hashing function to use. + * + * DB_HASH_SIMPLE uses default hashing function + * DB_HASH_COMPLEX uses jenkins hashing function + */ +enum db_hash_type { + DB_HASH_SIMPLE, + DB_HASH_COMPLEX, +}; + +/** + * @brief Parser callback function called when fetching a record + * + * This function is called when fetching a record. This function should + * not modify key and data arguments. + * + * The function should return 0 on success and errno on error. + */ +typedef int (*db_hash_record_parser_fn)(uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen, + void *private_data); + +/** + * @brief Abstract structure representing tdb hash table + */ +struct db_hash_context; + +/** + * @brief Initialize tdb hash table + * + * This returns a new tdb hash table context which is a talloc context. Freeing + * this context will free all the memory associated with the hash table. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] name The name for the hash table + * @param[in] hash_size The size of the hash table + * @param[in] type The type of hashing function to use + * @param[out] result The new db_hash_context structure + * @return 0 on success, errno on failure + */ +int db_hash_init(TALLOC_CTX *mem_ctx, const char *name, int hash_size, + enum db_hash_type type, struct db_hash_context **result); + +/** + * @brief Insert a record into the hash table + * + * The key and data can be any binary data. Insert only if the record does not + * exist. If the record already exists, return error. + * + * @param[in] dh The tdb hash table context + * @param[in] keybuf The key buffer + * @param[in] keylen The key length + * @param[in] databuf The data buffer + * @param[in] datalen The data length + * @return 0 on success, errno on failure + */ +int db_hash_insert(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen); + +/** + * @brief Add a record into the hash table + * + * The key and data can be any binary data. If the record does not exist, + * insert the record. If the record already exists, replace the record. + * + * @param[in] dh The tdb hash table context + * @param[in] keybuf The key buffer + * @param[in] keylen The key length + * @param[in] databuf The data buffer + * @param[in] datalen The data length + * @return 0 on success, errno on failure + */ +int db_hash_add(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen); +/** + * @brief Delete a record from the hash table + * + * @param[in] dh The tdb hash table context + * @param[in] keybuf The key buffer + * @param[in] keylen The key length + * @return 0 on success, errno on failure + */ +int db_hash_delete(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen); + +/** + * @brief Fetch a record from the hash table + * + * The key and data can be any binary data. + * + * @param[in] dh The tdb hash table context + * @param[in] keybuf The key buffer + * @param[in] keylen The key length + * @param[in] parser Function called when the matching record is found + * @param[in] private_data Private data to parser function + * @return 0 on success, errno on failure + */ +int db_hash_fetch(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen, + db_hash_record_parser_fn parser, void *private_data); + +/** + * @brief Check if a record exists in the hash table + * + * @param[in] dh The tdb hash table context + * @param[in] keybuf The key buffer + * @param[in] keylen The key length + * @return 0 if the record exists, errno on failure + */ +int db_hash_exists(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen); + +/** + * @brief Traverse the database without modification + * + * The parser function should return non-zero value to stop traverse. + * + * @param[in] dh The tdb hash table context + * @param[in] parser Function called for each record + * @param[in] private_data Private data to parser function + * @param[out] count Number of records traversed + * @return 0 on success, errno on failure + */ +int db_hash_traverse(struct db_hash_context *dh, + db_hash_record_parser_fn parser, void *private_data, + int *count); + +/** + * @brief Traverse the database for modifications + * + * The parser function should return non-zero value to stop traverse. + * + * @param[in] dh The tdb hash table context + * @param[in] parser Function called for each record + * @param[in] private_data Private data to parser function + * @param[out] count Number of records traversed + * @return 0 on success, errno on failure + */ +int db_hash_traverse_update(struct db_hash_context *dh, + db_hash_record_parser_fn parser, + void *private_data, int *count); + +#endif /* __CTDB_DB_HASH_H__ */ diff --git a/ctdb/common/event_script.c b/ctdb/common/event_script.c new file mode 100644 index 0000000..edd607f --- /dev/null +++ b/ctdb/common/event_script.c @@ -0,0 +1,247 @@ +/* + Low level event script handling + + Copyright (C) Amitay Isaacs 2017 + Copyright (C) Martin Schwenke 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/dir.h" +#include "system/glob.h" + +#include + +#include "common/event_script.h" + +static int script_filter(const struct dirent *de) +{ + int ret; + + /* Match a script pattern */ + ret = fnmatch("[0-9][0-9].*.script", de->d_name, 0); + if (ret == 0) { + return 1; + } + + return 0; +} + +int event_script_get_list(TALLOC_CTX *mem_ctx, + const char *script_dir, + struct event_script_list **out) +{ + struct dirent **namelist = NULL; + struct event_script_list *script_list = NULL; + size_t ds_len; + int count, ret; + int i; + + count = scandir(script_dir, &namelist, script_filter, alphasort); + if (count == -1) { + ret = errno; + goto done; + } + + script_list = talloc_zero(mem_ctx, struct event_script_list); + if (script_list == NULL) { + goto nomem; + } + + if (count == 0) { + ret = 0; + *out = script_list; + goto done; + } + + script_list->num_scripts = count; + script_list->script = talloc_zero_array(script_list, + struct event_script *, + count); + if (script_list->script == NULL) { + goto nomem; + } + + ds_len = strlen(".script"); + for (i = 0; i < count; i++) { + struct event_script *s; + struct stat statbuf; + + s = talloc_zero(script_list->script, struct event_script); + if (s == NULL) { + goto nomem; + } + + script_list->script[i] = s; + + s->name = talloc_strndup(script_list->script, + namelist[i]->d_name, + strlen(namelist[i]->d_name) - ds_len); + if (s->name == NULL) { + goto nomem; + } + + s->path = talloc_asprintf(script_list->script, + "%s/%s", + script_dir, + namelist[i]->d_name); + if (s->path == NULL) { + goto nomem; + } + + ret = stat(s->path, &statbuf); + if (ret == 0) { + /* + * If ret != 0 this is either a dangling + * symlink or it has just disappeared. Either + * way, it isn't executable. See the note + * below about things that have disappeared. + */ + if (statbuf.st_mode & S_IXUSR) { + s->enabled = true; + } + } + } + + *out = script_list; + ret = 0; + goto done; + +nomem: + ret = ENOMEM; + talloc_free(script_list); + +done: + if (namelist != NULL && count != -1) { + for (i=0; i ds_len && + strcmp(&script_name[sn_len - ds_len], dot_script) == 0) { + script_file = script_name; + } else { + ret = snprintf(buf, sizeof(buf), "%s.script", script_name); + if (ret < 0 || (size_t)ret >= sizeof(buf)) { + return ENAMETOOLONG; + } + script_file = buf; + } + + dirp = opendir(script_dir); + if (dirp == NULL) { + return errno; + } + + found = false; + while ((de = readdir(dirp)) != NULL) { + if (strcmp(de->d_name, script_file) == 0) { + /* check for valid script names */ + ret = script_filter(de); + if (ret == 0) { + closedir(dirp); + return EINVAL; + } + + found = true; + found_inode = de->d_ino; + break; + } + } + closedir(dirp); + + if (! found) { + return ENOENT; + } + + ret = snprintf(filename, + sizeof(filename), + "%s/%s", + script_dir, + script_file); + if (ret < 0 || (size_t)ret >= sizeof(filename)) { + return ENAMETOOLONG; + } + + fd = open(filename, O_RDWR); + if (fd == -1) { + ret = errno; + goto done; + } + + ret = fstat(fd, &st); + if (ret != 0) { + ret = errno; + goto done; + } + + /* + * If the directory entry inode number doesn't match the one + * returned by fstat() then this is probably a symlink, so the + * caller should not be calling this function. Note that this + * is a cheap sanity check to catch most programming errors. + * This doesn't cost any extra system calls but can still miss + * the unlikely case where the symlink is to a file on a + * different filesystem with the same inode number as the + * symlink. + */ + if (found && found_inode != st.st_ino) { + ret = EINVAL; + goto done; + } + + if (enable) { + new_mode = st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH); + } else { + new_mode = st.st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH); + } + + ret = fchmod(fd, new_mode); + if (ret != 0) { + ret = errno; + goto done; + } + +done: + if (fd != -1) { + close(fd); + } + return ret; +} diff --git a/ctdb/common/event_script.h b/ctdb/common/event_script.h new file mode 100644 index 0000000..bf5a8fd --- /dev/null +++ b/ctdb/common/event_script.h @@ -0,0 +1,72 @@ +/* + Low level event script handling + + Copyright (C) Amitay Isaacs 2017 + Copyright (C) Martin Schwenke 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SCRIPT_H__ +#define __CTDB_SCRIPT_H__ + +#include "replace.h" +#include "system/filesys.h" + +#include + +/** + * @file script.h + * + * @brief Script listing and manipulation + */ + + +struct event_script { + char *name; + char *path; + bool enabled; +}; + +struct event_script_list { + unsigned int num_scripts; + struct event_script **script; +}; + + +/** + * @brief Retrieve a list of scripts + * + * @param[in] mem_ctx Talloc memory context + * @param[in] script_dir Directory containing scripts + * @param[out] out List of scripts + * @return 0 on success, errno on failure + */ +int event_script_get_list(TALLOC_CTX *mem_ctx, + const char *script_dir, + struct event_script_list **out); + +/** + * @brief Make a script executable or not executable + * + * @param[in] script_dir Directory containing script + * @param[in] script_name Name of the script to enable + * @param[in] executable True if script should be made executable + * @return 0 on success, errno on failure + */ +int event_script_chmod(const char *script_dir, + const char *script_name, + bool executable); + +#endif /* __CTDB_SCRIPT_H__ */ diff --git a/ctdb/common/hash_count.c b/ctdb/common/hash_count.c new file mode 100644 index 0000000..f845016 --- /dev/null +++ b/ctdb/common/hash_count.c @@ -0,0 +1,219 @@ +/* + Using hash table for counting events + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/time.h" + +#include + +#include "lib/util/time.h" + +#include "common/db_hash.h" +#include "common/hash_count.h" + +struct hash_count_value { + struct timeval update_time; + uint64_t counter; +}; + +struct hash_count_context { + struct db_hash_context *dh; + struct timeval update_interval; + hash_count_update_handler_fn handler; + void *private_data; +}; + +/* + * Initialise hash count map + */ +int hash_count_init(TALLOC_CTX *mem_ctx, struct timeval update_interval, + hash_count_update_handler_fn handler, void *private_data, + struct hash_count_context **result) +{ + struct hash_count_context *hcount; + int ret; + + if (handler == NULL) { + return EINVAL; + } + + hcount = talloc_zero(mem_ctx, struct hash_count_context); + if (hcount == NULL) { + return ENOMEM; + } + + ret = db_hash_init(hcount, "hash_count_db", 8192, DB_HASH_COMPLEX, + &hcount->dh); + if (ret != 0) { + talloc_free(hcount); + return ret; + } + + hcount->update_interval = update_interval; + hcount->handler = handler; + hcount->private_data = private_data; + + *result = hcount; + return 0; +} + +static int hash_count_fetch_parser(uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen, + void *private_data) +{ + struct hash_count_value *value = + (struct hash_count_value *)private_data; + + if (datalen != sizeof(struct hash_count_value)) { + return EIO; + } + + *value = *(struct hash_count_value *)databuf; + return 0; +} + +static int hash_count_fetch(struct hash_count_context *hcount, TDB_DATA key, + struct hash_count_value *value) +{ + return db_hash_fetch(hcount->dh, key.dptr, key.dsize, + hash_count_fetch_parser, value); +} + +static int hash_count_insert(struct hash_count_context *hcount, TDB_DATA key, + struct hash_count_value *value) +{ + return db_hash_insert(hcount->dh, key.dptr, key.dsize, + (uint8_t *)value, + sizeof(struct hash_count_value)); +} + +static int hash_count_update(struct hash_count_context *hcount, TDB_DATA key, + struct hash_count_value *value) +{ + return db_hash_add(hcount->dh, key.dptr, key.dsize, + (uint8_t *)value, sizeof(struct hash_count_value)); +} + +int hash_count_increment(struct hash_count_context *hcount, TDB_DATA key) +{ + struct hash_count_value value; + struct timeval current_time = timeval_current(); + int ret; + + if (hcount == NULL) { + return EINVAL; + } + + ret = hash_count_fetch(hcount, key, &value); + if (ret == 0) { + struct timeval tmp_t; + + tmp_t = timeval_sum(&value.update_time, + &hcount->update_interval); + if (timeval_compare(¤t_time, &tmp_t) < 0) { + value.counter += 1; + } else { + value.update_time = current_time; + value.counter = 1; + } + + hcount->handler(key, value.counter, hcount->private_data); + ret = hash_count_update(hcount, key, &value); + + } else if (ret == ENOENT) { + value.update_time = current_time; + value.counter = 1; + + hcount->handler(key, value.counter, hcount->private_data); + ret = hash_count_insert(hcount, key, &value); + } + + return ret; +} + +static struct timeval timeval_subtract(const struct timeval *tv1, + const struct timeval *tv2) +{ + struct timeval tv = *tv1; + const unsigned int million = 1000000; + + if (tv.tv_sec > 1) { + tv.tv_sec -= 1; + tv.tv_usec += million; + } else { + return tv; + } + + tv.tv_sec -= tv2->tv_sec; + tv.tv_usec -= tv2->tv_usec; + + tv.tv_sec += tv.tv_usec / million; + tv.tv_usec = tv.tv_usec % million; + + return tv; +} + +struct hash_count_expire_state { + struct db_hash_context *dh; + struct timeval last_time; + int count; +}; + +static int hash_count_expire_parser(uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen, + void *private_data) +{ + struct hash_count_expire_state *state = + (struct hash_count_expire_state *)private_data; + struct hash_count_value *value; + int ret = 0; + + if (datalen != sizeof(struct hash_count_value)) { + return EIO; + } + + value = (struct hash_count_value *)databuf; + if (timeval_compare(&value->update_time, &state->last_time) < 0) { + ret = db_hash_delete(state->dh, keybuf, keylen); + if (ret == 0) { + state->count += 1; + } + } + + return ret; +} + +void hash_count_expire(struct hash_count_context *hcount, int *delete_count) +{ + struct timeval current_time = timeval_current(); + struct hash_count_expire_state state; + + state.dh = hcount->dh; + state.last_time = timeval_subtract(¤t_time, + &hcount->update_interval); + state.count = 0; + + (void) db_hash_traverse_update(hcount->dh, hash_count_expire_parser, + &state, NULL); + + if (delete_count != NULL) { + *delete_count = state.count; + } +} diff --git a/ctdb/common/hash_count.h b/ctdb/common/hash_count.h new file mode 100644 index 0000000..f14c82c --- /dev/null +++ b/ctdb/common/hash_count.h @@ -0,0 +1,94 @@ +/* + Using hash table for counting events + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_HASH_COUNT_H__ +#define __CTDB_HASH_COUNT_H__ + +/** + * @file hash_count.h + * + * @brief Count key-based events for specified interval + * + * This can be used to measure the rate of events based on any interval. + * For example, number of occurrences per second. + */ + +/** + * @brief Handler callback function called when counter is incremented + * + * This function is called every time a counter is incremented for a key. + * The counter argument is the number of times the increment function is + * called during a count interval. + * + * This function should not modify key and data arguments. + */ +typedef void (*hash_count_update_handler_fn)(TDB_DATA key, uint64_t counter, + void *private_data); + +/** + * @brief Abstract structure representing hash based counting + */ +struct hash_count_context; + +/** + * @brief Initialize hash counting + * + * This return a new hash count context which is a talloc context. Freeing + * this context will free all the memory associated with hash count. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] count_interval The time interval for counting events + * @param[in] handler Function called when counter is incremented + * @param[in] private_data Private data to handler function + * @param[out] result The new hash_count structure + * @return 0 on success, errno on failure + */ +int hash_count_init(TALLOC_CTX *mem_ctx, struct timeval count_interval, + hash_count_update_handler_fn handler, void *private_data, + struct hash_count_context **result); + +/** + * @brief Increment a counter for a key + * + * First time this is called for a key, corresponding counter is set to 1 + * and the start time is noted. For all subsequent calls made during the + * count_interval (used in initializing the context) will increment + * corresponding counter for the key. After the count_interval has elapsed, + * the counter will be reset to 1. + * + * @param[in] hcount The hash count context + * @param[in] key The key for which counter is updated + * @return 0 on success, errno on failure + * + * This will result in a callback function being called. + */ +int hash_count_increment(struct hash_count_context *hcount, TDB_DATA key); + +/** + * @brief Remove keys for which count interval has elapsed + * + * This function is used to clean the database of keys for which there are + * no recent events. + * + * @param[in] hcount The hash count context + * @param[out] delete_count The number of keys deleted + */ +void hash_count_expire(struct hash_count_context *hcount, int *delete_count); + +#endif /* __CTDB_HASH_COUNT_H__ */ diff --git a/ctdb/common/line.c b/ctdb/common/line.c new file mode 100644 index 0000000..c4c6726 --- /dev/null +++ b/ctdb/common/line.c @@ -0,0 +1,145 @@ +/* + Line based I/O over fds + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "lib/util/sys_rw.h" + +#include "common/line.h" + +struct line_read_state { + line_process_fn_t callback; + void *private_data; + char *buf; + size_t hint, len, offset; + int num_lines; +}; + +static bool line_read_one(char *buf, size_t start, size_t len, size_t *pos) +{ + size_t i; + + for (i=start; ibuf, start, state->offset, &pos); + if (! ok) { + break; + } + + state->buf[pos] = '\0'; + state->num_lines += 1; + + ret = state->callback(state->buf + start, state->private_data); + if (ret != 0) { + return ret; + } + + start = pos+1; + } + + if (pos > 0) { + if (pos+1 < state->offset) { + memmove(state->buf, + state->buf + pos+1, + state->offset - (pos+1)); + } + state->offset -= (pos+1); + } + + return 0; +} + +int line_read(int fd, + size_t length, + TALLOC_CTX *mem_ctx, + line_process_fn_t callback, + void *private_data, + int *num_lines) +{ + struct line_read_state state; + + if (length < 32) { + length = 32; + } + + state = (struct line_read_state) { + .callback = callback, + .private_data = private_data, + .hint = length, + }; + + while (1) { + ssize_t n; + int ret; + + if (state.offset == state.len) { + state.len += state.hint; + state.buf = talloc_realloc_size(mem_ctx, + state.buf, + state.len); + if (state.buf == NULL) { + return ENOMEM; + } + } + + n = sys_read(fd, + state.buf + state.offset, + state.len - state.offset); + if (n < 0) { + return errno; + } + if (n == 0) { + break; + } + + state.offset += n; + + ret = line_read_process(&state); + if (ret != 0) { + if (num_lines != NULL) { + *num_lines = state.num_lines; + } + return ret; + } + } + + if (num_lines != NULL) { + *num_lines = state.num_lines; + } + return 0; +} diff --git a/ctdb/common/line.h b/ctdb/common/line.h new file mode 100644 index 0000000..6b67f1e --- /dev/null +++ b/ctdb/common/line.h @@ -0,0 +1,62 @@ +/* + Line based I/O over fds + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_LINE_H__ +#define __CTDB_LINE_H__ + +#include + +/** + * @file line.h + * + * @brief Line based I/O over pipes and sockets + */ + +/** + * @brief The callback routine called to process a line + * + * @param[in] line The line read + * @param[in] private_data Private data for callback + * @return 0 to continue processing lines, non-zero to stop reading + */ +typedef int (*line_process_fn_t)(char *line, void *private_data); + +/** + * @brief Read a line (terminated by \n or \0) + * + * If there is any read error on fd, then errno will be returned. + * If callback function returns a non-zero value, then that value will be + * returned. + * + * @param[in] fd The file descriptor + * @param[in] length The expected length of a line (this is only a hint) + * @param[in] mem_ctx Talloc memory context + * @param[in] callback Callback function called when a line is read + * @param[in] private_data Private data for callback + * @param[out] num_lines Number of lines read so far + * @return 0 on on success, errno on failure + */ +int line_read(int fd, + size_t length, + TALLOC_CTX *mem_ctx, + line_process_fn_t callback, + void *private_data, + int *num_lines); + +#endif /* __CTDB_LINE_H__ */ diff --git a/ctdb/common/logging.c b/ctdb/common/logging.c new file mode 100644 index 0000000..3aa5ca9 --- /dev/null +++ b/ctdb/common/logging.c @@ -0,0 +1,745 @@ +/* + Logging utilities + + Copyright (C) Andrew Tridgell 2008 + Copyright (C) Martin Schwenke 2014 + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" +#include "system/locale.h" +#include "system/time.h" +#include "system/filesys.h" +#include "system/syslog.h" +#include "system/dir.h" + +#include "lib/util/time_basic.h" +#include "lib/util/sys_rw.h" +#include "lib/util/debug.h" +#include "lib/util/blocking.h" +#include "lib/util/samba_util.h" /* get_myname() */ + +#include "common/logging.h" + +struct { + int log_level; + const char *log_string; +} log_string_map[] = { + { DEBUG_ERR, "ERROR" }, + { DEBUG_WARNING, "WARNING" }, + { 2, "WARNING" }, + { DEBUG_NOTICE, "NOTICE" }, + { 4, "NOTICE" }, + { DEBUG_INFO, "INFO" }, + { 6, "INFO" }, + { 7, "INFO" }, + { 8, "INFO" }, + { 9, "INFO" }, + { DEBUG_DEBUG, "DEBUG" }, +}; + +bool debug_level_parse(const char *log_string, int *log_level) +{ + size_t i; + + if (log_string == NULL) { + return false; + } + + if (isdigit(log_string[0])) { + int level = atoi(log_string); + + if (level >= 0 && (size_t)level < ARRAY_SIZE(log_string_map)) { + *log_level = level; + return true; + } + return false; + } + + for (i=0; i= ARRAY_SIZE(priority_map) || level < 0) { + priority = LOG_DEBUG; + } else { + priority = priority_map[level]; + } + return priority; +} + +struct syslog_log_state { + int fd; + const char *app_name; + const char *hostname; + int (*format)(int dbglevel, struct syslog_log_state *state, + const char *str, char *buf, int bsize); + /* RFC3164 says: The total length of the packet MUST be 1024 + bytes or less. */ + char buffer[1024]; + unsigned int dropped_count; +}; + +/* Format messages as per RFC3164 + * + * It appears that some syslog daemon implementations do not allow a + * hostname when messages are sent via a Unix domain socket, so omit + * it. Similarly, syslogd on FreeBSD does not understand the hostname + * part of the header, even when logging via UDP. Note that most + * implementations will log messages against "localhost" when logging + * via UDP. A timestamp could be sent but rsyslogd on Linux limits + * the timestamp logged to the precision that was received on + * /dev/log. It seems sane to send degenerate RFC3164 messages + * without a header at all, so that the daemon will generate high + * resolution timestamps if configured. + */ +static int format_rfc3164(int dbglevel, struct syslog_log_state *state, + const char *str, char *buf, int bsize) +{ + int pri; + int len; + + pri = LOG_DAEMON | debug_level_to_priority(dbglevel); + len = snprintf(buf, bsize, "<%d>%s[%u]: %s", + pri, state->app_name, getpid(), str); + buf[bsize-1] = '\0'; + len = MIN(len, bsize - 1); + + return len; +} + +/* Format messages as per RFC5424 + * + * <165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 + * myproc 8710 - - %% It's time to make the do-nuts. + */ +static int format_rfc5424(int dbglevel, struct syslog_log_state *state, + const char *str, char *buf, int bsize) +{ + int pri; + struct timeval tv; + struct timeval_buf tvbuf; + int len, s; + + /* Header */ + pri = LOG_DAEMON | debug_level_to_priority(dbglevel); + GetTimeOfDay(&tv); + len = snprintf(buf, bsize, + "<%d>1 %s %s %s %u - - ", + pri, timeval_str_buf(&tv, true, true, &tvbuf), + state->hostname, state->app_name, getpid()); + /* A truncated header is not useful... */ + if (len >= bsize) { + return -1; + } + + /* Message */ + s = snprintf(&buf[len], bsize - len, "%s", str); + buf[bsize-1] = '\0'; + len = MIN(len + s, bsize - 1); + + return len; +} + +static void syslog_log(void *private_data, int level, const char *msg) +{ + syslog(debug_level_to_priority(level), "%s", msg); +} + +static int syslog_log_sock_maybe(struct syslog_log_state *state, + int level, const char *msg) +{ + int n; + ssize_t ret; + + n = state->format(level, state, msg, state->buffer, + sizeof(state->buffer)); + if (n == -1) { + return E2BIG; + } + + do { + ret = write(state->fd, state->buffer, n); + } while (ret == -1 && errno == EINTR); + + if (ret == -1) { + return errno; + } + + return 0; + +} +static void syslog_log_sock(void *private_data, int level, const char *msg) +{ + struct syslog_log_state *state = talloc_get_type_abort( + private_data, struct syslog_log_state); + int ret; + + if (state->dropped_count > 0) { + char t[64] = { 0 }; + snprintf(t, sizeof(t), + "[Dropped %u log messages]\n", + state->dropped_count); + t[sizeof(t)-1] = '\0'; + ret = syslog_log_sock_maybe(state, level, t); + if (ret == EAGAIN || ret == EWOULDBLOCK) { + state->dropped_count++; + /* + * If above failed then actually drop the + * message that would be logged below, since + * it would have been dropped anyway and it is + * also likely to fail. Falling through and + * attempting to log the message also means + * that the dropped message count will be + * logged out of order. + */ + return; + } + if (ret != 0) { + /* Silent failure on any other error */ + return; + } + state->dropped_count = 0; + } + + ret = syslog_log_sock_maybe(state, level, msg); + if (ret == EAGAIN || ret == EWOULDBLOCK) { + state->dropped_count++; + } +} + +static int syslog_log_setup_syslog(TALLOC_CTX *mem_ctx, const char *app_name) +{ + openlog(app_name, LOG_PID, LOG_DAEMON); + + debug_set_callback(NULL, syslog_log); + + return 0; +} + +static int syslog_log_state_destructor(struct syslog_log_state *state) +{ + if (state->fd != -1) { + close(state->fd); + state->fd = -1; + } + return 0; +} + +static int syslog_log_setup_common(TALLOC_CTX *mem_ctx, const char *app_name, + struct syslog_log_state **result) +{ + struct syslog_log_state *state; + + state = talloc_zero(mem_ctx, struct syslog_log_state); + if (state == NULL) { + return ENOMEM; + } + + state->fd = -1; + state->app_name = app_name; + talloc_set_destructor(state, syslog_log_state_destructor); + + *result = state; + return 0; +} + +#ifdef _PATH_LOG +static int syslog_log_setup_nonblocking(TALLOC_CTX *mem_ctx, + const char *app_name) +{ + struct syslog_log_state *state = NULL; + struct sockaddr_un dest; + int ret; + + ret = syslog_log_setup_common(mem_ctx, app_name, &state); + if (ret != 0) { + return ret; + } + + state->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (state->fd == -1) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + dest.sun_family = AF_UNIX; + strncpy(dest.sun_path, _PATH_LOG, sizeof(dest.sun_path)-1); + ret = connect(state->fd, + (struct sockaddr *)&dest, sizeof(dest)); + if (ret == -1) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + ret = set_blocking(state->fd, false); + if (ret != 0) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + if (! set_close_on_exec(state->fd)) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + state->hostname = NULL; /* Make this explicit */ + state->format = format_rfc3164; + + debug_set_callback(state, syslog_log_sock); + + return 0; +} +#endif /* _PATH_LOG */ + +static int syslog_log_setup_udp(TALLOC_CTX *mem_ctx, const char *app_name, + bool rfc5424) +{ + struct syslog_log_state *state = NULL; + struct sockaddr_in dest; + int ret; + + ret = syslog_log_setup_common(mem_ctx, app_name, &state); + if (ret != 0) { + return ret; + } + + state->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (state->fd == -1) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + dest.sin_family = AF_INET; + dest.sin_port = htons(514); + dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + ret = connect(state->fd, + (struct sockaddr *)&dest, sizeof(dest)); + if (ret == -1) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + if (! set_close_on_exec(state->fd)) { + int save_errno = errno; + talloc_free(state); + return save_errno; + } + + state->hostname = get_myname(state); + if (state->hostname == NULL) { + /* Use a fallback instead of failing initialisation */ + state->hostname = "localhost"; + } + if (rfc5424) { + state->format = format_rfc5424; + } else { + state->format = format_rfc3164; + } + + debug_set_callback(state, syslog_log_sock); + + return 0; +} + +static bool syslog_log_validate(const char *option) +{ + if (option == NULL) { + return true; +#ifdef _PATH_LOG + } else if (strcmp(option, "nonblocking") == 0) { + return true; +#endif + } else if (strcmp(option, "udp") == 0) { + return true; + } else if (strcmp(option, "udp-rfc5424") == 0) { + return true; + } + + return false; +} + +static int syslog_log_setup(TALLOC_CTX *mem_ctx, const char *option, + const char *app_name) +{ + if (option == NULL) { + return syslog_log_setup_syslog(mem_ctx, app_name); +#ifdef _PATH_LOG + } else if (strcmp(option, "nonblocking") == 0) { + return syslog_log_setup_nonblocking(mem_ctx, app_name); +#endif + } else if (strcmp(option, "udp") == 0) { + return syslog_log_setup_udp(mem_ctx, app_name, false); + } else if (strcmp(option, "udp-rfc5424") == 0) { + return syslog_log_setup_udp(mem_ctx, app_name, true); + } + + return EINVAL; +} + +struct log_backend { + const char *name; + bool (*validate)(const char *option); + int (*setup)(TALLOC_CTX *mem_ctx, + const char *option, + const char *app_name); +}; + +static struct log_backend log_backend[] = { + { + .name = "file", + .validate = file_log_validate, + .setup = file_log_setup, + }, + { + .name = "syslog", + .validate = syslog_log_validate, + .setup = syslog_log_setup, + }, +}; + +static int log_backend_parse(TALLOC_CTX *mem_ctx, + const char *logging, + struct log_backend **backend, + char **backend_option) +{ + struct log_backend *b = NULL; + char *t, *name, *option; + size_t i; + + t = talloc_strdup(mem_ctx, logging); + if (t == NULL) { + return ENOMEM; + } + + name = strtok(t, ":"); + if (name == NULL) { + talloc_free(t); + return EINVAL; + } + option = strtok(NULL, ":"); + + for (i=0; ivalidate(option); + talloc_free(tmp_ctx); + return status; +} + +/* Initialise logging */ +int logging_init(TALLOC_CTX *mem_ctx, const char *logging, + const char *debug_level, const char *app_name) +{ + struct log_backend *backend = NULL; + char *option = NULL; + int level; + int ret; + + setup_logging(app_name, DEBUG_DEFAULT_STDERR); + + if (debug_level == NULL) { + debug_level = getenv("CTDB_DEBUGLEVEL"); + } + if (! debug_level_parse(debug_level, &level)) { + return EINVAL; + } + debuglevel_set(level); + + if (logging == NULL) { + logging = getenv("CTDB_LOGGING"); + } + if (logging == NULL || logging[0] == '\0') { + return EINVAL; + } + + ret = log_backend_parse(mem_ctx, logging, &backend, &option); + if (ret != 0) { + if (ret == ENOENT) { + fprintf(stderr, "Invalid logging option \'%s\'\n", + logging); + } + talloc_free(option); + return ret; + } + + ret = backend->setup(mem_ctx, option, app_name); + talloc_free(option); + return ret; +} + +bool logging_reopen_logs(void) +{ + bool status; + + status = reopen_logs_internal(); + + return status; +} + +struct logging_reopen_logs_data { + void (*hook)(void *private_data); + void *private_data; +}; + +static void logging_sig_hup_handler(struct tevent_context *ev, + struct tevent_signal *se, + int signum, + int count, + void *dont_care, + void *private_data) +{ + bool status; + + if (private_data != NULL) { + struct logging_reopen_logs_data *data = talloc_get_type_abort( + private_data, struct logging_reopen_logs_data); + + if (data->hook != NULL) { + data->hook(data->private_data); + } + } + + status = logging_reopen_logs(); + if (!status) { + D_WARNING("Failed to reopen logs\n"); + return; + } + + D_NOTICE("Reopened logs\n"); + +} + +bool logging_setup_sighup_handler(struct tevent_context *ev, + TALLOC_CTX *talloc_ctx, + void (*hook)(void *private_data), + void *private_data) +{ + struct logging_reopen_logs_data *data = NULL; + struct tevent_signal *se; + + if (hook != NULL) { + data = talloc(talloc_ctx, struct logging_reopen_logs_data); + if (data == NULL) { + return false; + } + + data->hook = hook; + data->private_data = private_data; + } + + + se = tevent_add_signal(ev, + talloc_ctx, + SIGHUP, + 0, + logging_sig_hup_handler, + data); + if (se == NULL) { + talloc_free(data); + return false; + } + + return true; +} diff --git a/ctdb/common/logging.h b/ctdb/common/logging.h new file mode 100644 index 0000000..542b4a3 --- /dev/null +++ b/ctdb/common/logging.h @@ -0,0 +1,51 @@ +/* + Logging utilities + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_LOGGING_H__ +#define __CTDB_LOGGING_H__ + +#include +#include +#include "lib/util/debug.h" + +#define DEBUG_ERR DBGLVL_ERR +#define DEBUG_WARNING DBGLVL_WARNING +#define DEBUG_NOTICE DBGLVL_NOTICE +#define DEBUG_INFO DBGLVL_INFO +#define DEBUG_DEBUG DBGLVL_DEBUG + +/* These are used in many places, so define them here to avoid churn */ +#define DEBUG_ALERT DEBUG_ERR +#define DEBUG_CRIT DEBUG_ERR + +bool debug_level_parse(const char *log_string, int *log_level); +const char *debug_level_to_string(int log_level); +int debug_level_from_string(const char *log_string); + +bool logging_validate(const char *logging); +int logging_init(TALLOC_CTX *mem_ctx, const char *logging, + const char *debuglevel, const char *app_name); + +bool logging_reopen_logs(void); +bool logging_setup_sighup_handler(struct tevent_context *ev, + TALLOC_CTX *talloc_ctx, + void (*hook)(void *private_data), + void *private_data); + +#endif /* __CTDB_LOGGING_H__ */ diff --git a/ctdb/common/logging_conf.c b/ctdb/common/logging_conf.c new file mode 100644 index 0000000..1cd929e --- /dev/null +++ b/ctdb/common/logging_conf.c @@ -0,0 +1,127 @@ +/* + CTDB logging config handling + + Copyright (C) Martin Schwenke 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "common/conf.h" +#include "common/logging.h" +#include "common/logging_conf.h" + +#define LOGGING_LOCATION_DEFAULT "file:" LOGDIR "/log.ctdb" +#define LOGGING_LOG_LEVEL_DEFAULT "ERROR" + +static bool logging_conf_validate_log_level(const char *key, + const char *old_loglevel, + const char *new_loglevel, + enum conf_update_mode mode) +{ + int log_level; + bool ok; + + ok = debug_level_parse(new_loglevel, &log_level); + if (!ok) { + return false; + } + + return true; +} + +static bool logging_conf_validate_location(const char *key, + const char *old_location, + const char *new_location, + enum conf_update_mode mode) +{ + bool ok; + + ok = logging_validate(new_location); + if (!ok) { + return false; + } + + if (mode == CONF_MODE_RELOAD && + strcmp(old_location, new_location) != 0) { + D_WARNING("Ignoring update of %s config option \"%s\"\n", + LOGGING_CONF_SECTION, key); + return false; + } + + return true; +} + +void logging_conf_init(struct conf_context *conf, + const char *default_log_level) +{ + const char *log_level; + + log_level = (default_log_level == NULL) ? + LOGGING_LOG_LEVEL_DEFAULT : + default_log_level; + + conf_define_section(conf, LOGGING_CONF_SECTION, NULL); + + conf_define_string(conf, + LOGGING_CONF_SECTION, + LOGGING_CONF_LOCATION, + LOGGING_LOCATION_DEFAULT, + logging_conf_validate_location); + + conf_define_string(conf, + LOGGING_CONF_SECTION, + LOGGING_CONF_LOG_LEVEL, + log_level, + logging_conf_validate_log_level); +} + +const char *logging_conf_location(struct conf_context *conf) +{ + const char *out = NULL; + int ret; + + ret = conf_get_string(conf, + LOGGING_CONF_SECTION, + LOGGING_CONF_LOCATION, + &out, + NULL); + if (ret != 0) { + /* Can't really happen, but return default */ + return LOGGING_LOCATION_DEFAULT; + } + + return out; +} + +const char *logging_conf_log_level(struct conf_context *conf) +{ + const char *out = NULL; + int ret; + + ret = conf_get_string(conf, + LOGGING_CONF_SECTION, + LOGGING_CONF_LOG_LEVEL, + &out, + NULL); + if (ret != 0) { + /* Can't really happen, but return default */ + return LOGGING_LOG_LEVEL_DEFAULT; + } + + return out; +} diff --git a/ctdb/common/logging_conf.h b/ctdb/common/logging_conf.h new file mode 100644 index 0000000..fab478d --- /dev/null +++ b/ctdb/common/logging_conf.h @@ -0,0 +1,36 @@ +/* + CTDB logging config handling + + Copyright (C) Martin Schwenke 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __LOGGING_CONF_H__ +#define __LOGGING_CONF_H__ + +#include "common/conf.h" + +#define LOGGING_CONF_SECTION "logging" + +#define LOGGING_CONF_LOCATION "location" +#define LOGGING_CONF_LOG_LEVEL "log level" + +void logging_conf_init(struct conf_context *conf, + const char *default_log_level); + +const char *logging_conf_location(struct conf_context *conf); +const char *logging_conf_log_level(struct conf_context *conf); + +#endif /* __LOGGING_CONF_H__ */ diff --git a/ctdb/common/path.c b/ctdb/common/path.c new file mode 100644 index 0000000..ea3b08f --- /dev/null +++ b/ctdb/common/path.c @@ -0,0 +1,211 @@ +/* + Construct runtime paths + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include "lib/util/debug.h" + +#include "common/path.h" + +#define CTDB_CONFIG_FILE "ctdb.conf" + +struct { + char *basedir; + char datadir[PATH_MAX]; + char etcdir[PATH_MAX]; + char rundir[PATH_MAX]; + char vardir[PATH_MAX]; + bool test_mode; + bool basedir_set; + bool datadir_set; + bool etcdir_set; + bool rundir_set; + bool vardir_set; +} ctdb_paths = { + .datadir = CTDB_DATADIR, + .etcdir = CTDB_ETCDIR, + .rundir = CTDB_RUNDIR, + .vardir = CTDB_VARDIR, +}; + +static void path_set_basedir(void) +{ + const char *t; + + t = getenv("CTDB_TEST_MODE"); + if (t == NULL) { + goto done; + } + + ctdb_paths.test_mode = true; + + ctdb_paths.basedir = getenv("CTDB_BASE"); + if (ctdb_paths.basedir == NULL) { + D_ERR("Broken CTDB setup, CTDB_BASE not set in test mode\n"); + abort(); + } + +done: + ctdb_paths.basedir_set = true; +} + +static bool path_construct(char *path, const char *subdir) +{ + char p[PATH_MAX]; + int len; + + if (! ctdb_paths.basedir_set) { + path_set_basedir(); + } + + if (! ctdb_paths.test_mode) { + return true; + } + + if (subdir == NULL) { + len = snprintf(p, sizeof(p), "%s", ctdb_paths.basedir); + } else { + len = snprintf(p, + sizeof(p), + "%s/%s", + ctdb_paths.basedir, + subdir); + } + + if ((size_t)len >= sizeof(p)) { + return false; + } + + strncpy(path, p, PATH_MAX); + return true; +} + +const char *path_datadir(void) +{ + bool ok; + + if (! ctdb_paths.datadir_set) { + ok = path_construct(ctdb_paths.datadir, "share"); + if (!ok) { + D_ERR("Failed to construct DATADIR\n"); + } else { + ctdb_paths.datadir_set = true; + } + } + + return ctdb_paths.datadir; +} + +const char *path_etcdir(void) +{ + bool ok; + + if (! ctdb_paths.etcdir_set) { + ok = path_construct(ctdb_paths.etcdir, NULL); + if (!ok) { + D_ERR("Failed to construct ETCDIR\n"); + } else { + ctdb_paths.etcdir_set = true; + } + } + + return ctdb_paths.etcdir; +} + +const char *path_rundir(void) +{ + bool ok; + + if (! ctdb_paths.rundir_set) { + ok = path_construct(ctdb_paths.rundir, "run"); + if (!ok) { + D_ERR("Failed to construct RUNDIR\n"); + } else { + ctdb_paths.rundir_set = true; + } + } + + return ctdb_paths.rundir; +} + +const char *path_vardir(void) +{ + bool ok; + + if (! ctdb_paths.vardir_set) { + ok = path_construct(ctdb_paths.vardir, "var"); + if (!ok) { + D_ERR("Failed to construct VARDIR\n"); + } else { + ctdb_paths.vardir_set = true; + } + } + + return ctdb_paths.vardir; +} + +char *path_datadir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_datadir(), path); +} + +char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_etcdir(), path); +} + +char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_rundir(), path); +} + +char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_vardir(), path); +} + +char *path_config(TALLOC_CTX *mem_ctx) +{ + return path_etcdir_append(mem_ctx, CTDB_CONFIG_FILE); +} + +char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon) +{ + if (strcmp(daemon, "ctdbd") == 0) { + const char *t = getenv("CTDB_SOCKET"); + + if (t != NULL) { + return talloc_strdup(mem_ctx, t); + } + } + + return talloc_asprintf(mem_ctx, + "%s/%s.socket", + path_rundir(), + daemon); +} + +char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon) +{ + return talloc_asprintf(mem_ctx, + "%s/%s.pid", + path_rundir(), + daemon); +} diff --git a/ctdb/common/path.h b/ctdb/common/path.h new file mode 100644 index 0000000..dcc6c20 --- /dev/null +++ b/ctdb/common/path.h @@ -0,0 +1,39 @@ +/* + Construct runtime paths + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_PATH_H__ +#define __CTDB_PATH_H__ + +#include + +const char *path_datadir(void); +const char *path_etcdir(void); +const char *path_rundir(void); +const char *path_vardir(void); + +char *path_datadir_append(TALLOC_CTX *mem_ctx, const char *path); +char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path); +char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path); +char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path); + +char *path_config(TALLOC_CTX *mem_ctx); +char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon); +char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon); + +#endif /* __CTDB_PATH_H__ */ diff --git a/ctdb/common/path_tool.c b/ctdb/common/path_tool.c new file mode 100644 index 0000000..44d29b6 --- /dev/null +++ b/ctdb/common/path_tool.c @@ -0,0 +1,384 @@ +/* + path tool + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "lib/util/debug.h" + +#include "common/logging.h" +#include "common/cmdline.h" +#include "common/path.h" +#include "common/path_tool.h" + +struct path_tool_context { + struct cmdline_context *cmdline; +}; + +static int path_tool_config(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "config"); + return EINVAL; + } + + printf("%s\n", path_config(mem_ctx)); + + return 0; +} + +static int path_tool_pidfile(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + char *p; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "pidfile"); + return EINVAL; + } + + p = path_pidfile(mem_ctx, argv[0]); + if (p == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + printf("%s\n", p); + + return 0; +} + +static int path_tool_socket(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + char *p; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "socket"); + return EINVAL; + } + + p = path_socket(mem_ctx, argv[0]); + if (p == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + printf("%s\n", p); + + return 0; +} + +static int path_tool_datadir(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "datadir"); + return EINVAL; + } + + printf("%s\n", path_datadir()); + + return 0; +} + +static int path_tool_datadir_append(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + char *p; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "datadir append"); + return EINVAL; + } + + p = path_datadir_append(mem_ctx, argv[0]); + if (p == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + printf("%s\n", p); + + return 0; +} + +static int path_tool_etcdir(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "etcdir"); + return EINVAL; + } + + printf("%s\n", path_etcdir()); + + return 0; +} + +static int path_tool_etcdir_append(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + char *p; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "etcdir append"); + return EINVAL; + } + + p = path_etcdir_append(mem_ctx, argv[0]); + if (p == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + printf("%s\n", p); + + return 0; +} + +static int path_tool_rundir(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "rundir"); + return EINVAL; + } + + printf("%s\n", path_rundir()); + + return 0; +} + +static int path_tool_rundir_append(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + char *p; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "rundir append"); + return EINVAL; + } + + p = path_rundir_append(mem_ctx, argv[0]); + if (p == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + printf("%s\n", p); + + return 0; +} + +static int path_tool_vardir(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + + if (argc != 0) { + cmdline_usage(ctx->cmdline, "vardir"); + return EINVAL; + } + + printf("%s\n", path_vardir()); + + return 0; +} + +static int path_tool_vardir_append(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct path_tool_context *ctx = talloc_get_type_abort( + private_data, struct path_tool_context); + char *p; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "vardir append"); + return EINVAL; + } + + p = path_vardir_append(mem_ctx, argv[0]); + if (p == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + printf("%s\n", p); + + return 0; +} + +struct cmdline_command path_commands[] = { + { "config", path_tool_config, + "Get path of CTDB config file", NULL }, + { "pidfile", path_tool_pidfile, + "Get path of CTDB daemon pidfile", "" }, + { "socket", path_tool_socket, + "Get path of CTDB daemon socket", "" }, + { "datadir append", path_tool_datadir_append, + "Get path relative to CTDB DATADIR", "" }, + { "datadir", path_tool_datadir, + "Get path of CTDB DATADIR", NULL }, + { "etcdir append", path_tool_etcdir_append, + "Get path relative to CTDB ETCDIR", "" }, + { "etcdir", path_tool_etcdir, + "Get path of CTDB ETCDIR", NULL }, + { "rundir append", path_tool_rundir_append, + "Get path relative to CTDB RUNDIR", "" }, + { "rundir", path_tool_rundir, + "Get path of CTDB RUNDIR", NULL }, + { "vardir append", path_tool_vardir_append, + "Get path relative to CTDB VARDIR", "" }, + { "vardir", path_tool_vardir, + "Get path of CTDB VARDIR", NULL }, + CMDLINE_TABLEEND +}; + +int path_tool_init(TALLOC_CTX *mem_ctx, + const char *prog, + struct poptOption *options, + int argc, + const char **argv, + bool parse_options, + struct path_tool_context **result) +{ + struct path_tool_context *ctx; + int ret; + + ctx = talloc_zero(mem_ctx, struct path_tool_context); + if (ctx == NULL) { + D_ERR("Memory allocation error\n"); + return ENOMEM; + } + + ret = cmdline_init(ctx, + prog, + options, + NULL, + path_commands, + &ctx->cmdline); + if (ret != 0) { + D_ERR("Failed to initialize cmdline, ret=%d\n", ret); + talloc_free(ctx); + return ret; + } + + ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options); + if (ret != 0) { + cmdline_usage(ctx->cmdline, NULL); + talloc_free(ctx); + return ret; + } + + *result = ctx; + return 0; +} + +int path_tool_run(struct path_tool_context *ctx, int *result) +{ + return cmdline_run(ctx->cmdline, ctx, result); +} + +#ifdef CTDB_PATH_TOOL + +int main(int argc, const char **argv) +{ + TALLOC_CTX *mem_ctx; + struct path_tool_context *ctx; + int ret, result; + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + fprintf(stderr, "Memory allocation error\n"); + exit(1); + } + + ret = path_tool_init(mem_ctx, + "ctdb-path", + NULL, + argc, + argv, + true, + &ctx); + if (ret != 0) { + talloc_free(mem_ctx); + exit(1); + } + + setup_logging("ctdb-path", DEBUG_STDERR); + debuglevel_set(DEBUG_ERR); + + ret = path_tool_run(ctx, &result); + if (ret != 0) { + result = 1; + } + + talloc_free(mem_ctx); + exit(result); +} + +#endif /* CTDB_PATH_TOOL */ diff --git a/ctdb/common/path_tool.h b/ctdb/common/path_tool.h new file mode 100644 index 0000000..bc6ea62 --- /dev/null +++ b/ctdb/common/path_tool.h @@ -0,0 +1,38 @@ +/* + path tool + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_PATH_TOOL__ +#define __CTDB_PATH_TOOL__ + +#include +#include + +struct path_tool_context; + +int path_tool_init(TALLOC_CTX *mem_ctx, + const char *prog, + struct poptOption *options, + int argc, + const char **argv, + bool parse_options, + struct path_tool_context **result); + +int path_tool_run(struct path_tool_context *ctx, int *result); + +#endif /* __CTDB_PATH_TOOL__ */ diff --git a/ctdb/common/pidfile.c b/ctdb/common/pidfile.c new file mode 100644 index 0000000..47589f4 --- /dev/null +++ b/ctdb/common/pidfile.c @@ -0,0 +1,85 @@ +/* + Create and remove pidfile + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include + +#include "lib/util/blocking.h" +#include "lib/util/pidfile.h" + +#include "common/pidfile.h" + +struct pidfile_context { + const char *pidfile; + int fd; + pid_t pid; +}; + +static int pidfile_context_destructor(struct pidfile_context *pid_ctx); + +int pidfile_context_create(TALLOC_CTX *mem_ctx, const char *pidfile, + struct pidfile_context **result) +{ + struct pidfile_context *pid_ctx; + int fd, ret = 0; + + pid_ctx = talloc_zero(mem_ctx, struct pidfile_context); + if (pid_ctx == NULL) { + return ENOMEM; + } + + pid_ctx->pidfile = talloc_strdup(pid_ctx, pidfile); + if (pid_ctx->pidfile == NULL) { + ret = ENOMEM; + goto fail; + } + + pid_ctx->pid = getpid(); + + ret = pidfile_path_create(pid_ctx->pidfile, &fd, NULL); + if (ret != 0) { + return ret; + } + + pid_ctx->fd = fd; + + talloc_set_destructor(pid_ctx, pidfile_context_destructor); + + *result = pid_ctx; + return 0; + +fail: + talloc_free(pid_ctx); + return ret; +} + +static int pidfile_context_destructor(struct pidfile_context *pid_ctx) +{ + if (getpid() != pid_ctx->pid) { + return 0; + } + + (void) unlink(pid_ctx->pidfile); + + pidfile_fd_close(pid_ctx->fd); + + return 0; +} diff --git a/ctdb/common/pidfile.h b/ctdb/common/pidfile.h new file mode 100644 index 0000000..bc4e3a7 --- /dev/null +++ b/ctdb/common/pidfile.h @@ -0,0 +1,51 @@ +/* + Create and remove pidfile + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_PIDFILE_H__ +#define __CTDB_PIDFILE_H__ + +#include + +/** + * @file pidfile.h + * + * @brief Routines to manage PID file + */ + +/** + * @brief Abstract struct to store pidfile details + */ +struct pidfile_context; + +/** + * @brief Create a PID file + * + * This creates a PID file, locks it, and writes PID. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] pidfile Path of PID file + * @param[out] result Pidfile context + * @return 0 on success, errno on failure + * + * Freeing the pidfile_context, will delete the pidfile. + */ +int pidfile_context_create(TALLOC_CTX *mem_ctx, const char *pidfile, + struct pidfile_context **result); + +#endif /* __CTDB_PIDFILE_H__ */ diff --git a/ctdb/common/pkt_read.c b/ctdb/common/pkt_read.c new file mode 100644 index 0000000..212ace5 --- /dev/null +++ b/ctdb/common/pkt_read.c @@ -0,0 +1,190 @@ +/* + Reading packets using fixed and dynamic buffer + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +/* This is similar to read_packet abstraction. The main different is that + * tevent fd event is created only once. + */ + +#include "replace.h" +#include "system/network.h" + +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "pkt_read.h" + +/* + * Read a packet using fixed buffer + */ + +struct pkt_read_state { + int fd; + uint8_t *buf; + size_t buflen; + size_t nread, total; + bool use_fixed; + ssize_t (*more)(uint8_t *buf, size_t buflen, void *private_data); + void *private_data; +}; + +struct tevent_req *pkt_read_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, size_t initial, + uint8_t *buf, size_t buflen, + ssize_t (*more)(uint8_t *buf, + size_t buflen, + void *private_data), + void *private_data) +{ + struct tevent_req *req; + struct pkt_read_state *state; + + req = tevent_req_create(mem_ctx, &state, struct pkt_read_state); + if (req == NULL) { + return NULL; + } + + state->fd = fd; + + if (buf == NULL || buflen == 0) { + state->use_fixed = false; + state->buf = talloc_array(state, uint8_t, initial); + if (state->buf == NULL) { + talloc_free(req); + return NULL; + } + state->buflen = initial; + } else { + state->use_fixed = true; + state->buf = buf; + state->buflen = buflen; + } + + state->nread = 0; + state->total = initial; + + state->more = more; + state->private_data = private_data; + + return req; +} + +void pkt_read_handler(struct tevent_context *ev, struct tevent_fd *fde, + uint16_t flags, struct tevent_req *req) +{ + struct pkt_read_state *state = tevent_req_data( + req, struct pkt_read_state); + ssize_t nread, more; + uint8_t *tmp; + + nread = read(state->fd, state->buf + state->nread, + state->total - state->nread); + if ((nread == -1) && (errno == EINTR)) { + /* retry */ + return; + } + if (nread == -1) { + tevent_req_error(req, errno); + return; + } + if (nread == 0) { + /* fd closed */ + tevent_req_error(req, EPIPE); + return; + } + + state->nread += nread; + if (state->nread < state->total) { + /* come back later */ + return; + } + + /* Check if "more" asks for more data */ + if (state->more == NULL) { + tevent_req_done(req); + return; + } + + more = state->more(state->buf, state->nread, state->private_data); + if (more == -1) { + /* invalid packet */ + tevent_req_error(req, EIO); + return; + } + if (more == 0) { + tevent_req_done(req); + return; + } + + if (state->total + more < state->total) { + /* int wrapped */ + tevent_req_error(req, EMSGSIZE); + return; + } + + if (state->total + more < state->buflen) { + /* continue using fixed buffer */ + state->total += more; + return; + } + + if (state->use_fixed) { + /* switch to dynamic buffer */ + tmp = talloc_array(state, uint8_t, state->total + more); + if (tevent_req_nomem(tmp, req)) { + return; + } + + memcpy(tmp, state->buf, state->total); + state->use_fixed = false; + } else { + tmp = talloc_realloc(state, state->buf, uint8_t, + state->total + more); + if (tevent_req_nomem(tmp, req)) { + return; + } + } + + state->buf = tmp; + state->buflen = state->total + more; + state->total += more; +} + +ssize_t pkt_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, + uint8_t **pbuf, bool *free_buf, int *perrno) +{ + struct pkt_read_state *state = tevent_req_data( + req, struct pkt_read_state); + + if (tevent_req_is_unix_error(req, perrno)) { + return -1; + } + + if (state->use_fixed) { + *pbuf = state->buf; + *free_buf = false; + } else { + *pbuf = talloc_steal(mem_ctx, state->buf); + *free_buf = true; + } + + return state->total; +} diff --git a/ctdb/common/pkt_read.h b/ctdb/common/pkt_read.h new file mode 100644 index 0000000..25d4a51 --- /dev/null +++ b/ctdb/common/pkt_read.h @@ -0,0 +1,98 @@ +/* + API for reading packets using fixed and dynamic buffer + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_PKT_READ_H__ +#define __CTDB_PKT_READ_H__ + +#include +#include + +/** + * @file pkt_read.h + * + * @brief Read a packet using fixed size buffer or allocated memory. + * + * CTDB communication uses lots of small packets. This abstraction avoids the + * need to allocate memory for small packets. Only if the received packet is + * larger than the fixed memory buffer, use talloc to allocate memory. + */ + +/** + * @brief Start async computation to read a packet + * + * This returns a tevent request to read a packet from given fd. The fd + * should be nonblocking. Freeing this request will free all the memory + * associated with the request. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] fd The non-blocking file/socket descriptor to read from + * @param[in] initial Initial amount of data to read + * @param[in] buf The static buffer to read data in + * @param[in] buflen The size of the static buffer + * @param[in] more The function to check if the bytes read forms a packet + * @param[in] private_data Private data to pass to more function + * @return new tevent request or NULL on failure + */ +struct tevent_req *pkt_read_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, size_t initial, + uint8_t *buf, size_t buflen, + ssize_t (*more)(uint8_t *buf, + size_t buflen, + void *private_data), + void *private_data); + +/** + * @brief Function to actually read data from the socket + * + * This function should be called, when tevent fd event is triggered. This + * function has the syntax of tevent_fd_handler_t. The private_data for this + * function is the tevent request created by pkt_read_send function. + * + * @param[in] ev Tevent context + * @param[in] fde Tevent fd context + * @param[in] flags Tevent fd flags + * @param[in] req The active tevent request + */ +void pkt_read_handler(struct tevent_context *ev, struct tevent_fd *fde, + uint16_t flags, struct tevent_req *req); + +/** + * @brief Retrieve a packet + * + * This function returns the pkt read from fd. + * + * @param[in] req Tevent request + * @param[in] mem_ctx Talloc memory context + * @param[out] pbuf The pointer to the buffer + * @param[out] free_buf Boolean to indicate that caller should free buffer + * @param[out] perrno errno in case of failure + * @return the size of the pkt, or -1 on failure + * + * If the pkt data is dynamically allocated, then it is moved under the + * specified talloc memory context and free_buf is set to true. It is the + * responsibility of the caller to the free the memory returned. + * + * If the pkt data is stored in the fixed buffer, then free_buf is set to false. + */ +ssize_t pkt_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, + uint8_t **pbuf, bool *free_buf, int *perrno); + +#endif /* __CTDB_PKT_READ_H__ */ diff --git a/ctdb/common/pkt_write.c b/ctdb/common/pkt_write.c new file mode 100644 index 0000000..b1c1730 --- /dev/null +++ b/ctdb/common/pkt_write.c @@ -0,0 +1,101 @@ +/* + Write a packet + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" + +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "pkt_write.h" + +/* + * Write a packet + */ + +struct pkt_write_state { + int fd; + uint8_t *buf; + size_t buflen, offset; +}; + +struct tevent_req *pkt_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, uint8_t *buf, size_t buflen) +{ + struct tevent_req *req; + struct pkt_write_state *state; + + req = tevent_req_create(mem_ctx, &state, struct pkt_write_state); + if (req == NULL) { + return NULL; + } + + state->fd = fd; + state->buf = buf; + state->buflen = buflen; + state->offset = 0; + + return req; +} + +void pkt_write_handler(struct tevent_context *ev, struct tevent_fd *fde, + uint16_t flags, struct tevent_req *req) +{ + struct pkt_write_state *state = tevent_req_data( + req, struct pkt_write_state); + ssize_t nwritten; + + nwritten = write(state->fd, state->buf + state->offset, + state->buflen - state->offset); + if ((nwritten == -1) && (errno == EINTR)) { + /* retry */ + return; + } + if (nwritten == -1) { + tevent_req_error(req, errno); + return; + } + if (nwritten == 0) { + /* retry */ + return; + } + + state->offset += nwritten; + if (state->offset < state->buflen) { + /* come back later */ + return; + } + + tevent_req_done(req); +} + +ssize_t pkt_write_recv(struct tevent_req *req, int *perrno) +{ + struct pkt_write_state *state = tevent_req_data( + req, struct pkt_write_state); + + if (tevent_req_is_unix_error(req, perrno)) { + return -1; + } + + return state->offset; +} diff --git a/ctdb/common/pkt_write.h b/ctdb/common/pkt_write.h new file mode 100644 index 0000000..19d8045 --- /dev/null +++ b/ctdb/common/pkt_write.h @@ -0,0 +1,79 @@ +/* + API for writing a packet + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_PKT_WRITE_H__ +#define __CTDB_PKT_WRITE_H__ + +#include +#include + +/** + * @file pkt_write.h + * + * @brief Write a packet. + * + * Write a complete packet with possibly multiple system calls. + */ + +/** + * @brief Start async computation to write a packet + * + * This returns a tevent request to write a packet to given fd. The fd + * should be nonblocking. Freeing this request will free all the memory + * associated with the request. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] fd The non-blocking file/socket descriptor to write to + * @param[in] buf The data + * @param[in] buflen The size of the data + * @return new tevent request or NULL on failure + */ +struct tevent_req *pkt_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, uint8_t *buf, size_t buflen); + +/** + * @brief Function to actually write data to the socket + * + * This function should be called, when tevent fd event is triggered + * for TEVENT_FD_WRITE event. This function has the syntax of + * tevent_fd_handler_t. The private_data for this function is the tevent + * request created by pkt_write_send function. + * + * @param[in] ev Tevent context + * @param[in] fde Tevent fd context + * @param[in] flags Tevent fd flags + * @param[in] req The active tevent request + */ +void pkt_write_handler(struct tevent_context *ev, struct tevent_fd *fde, + uint16_t flags, struct tevent_req *req); + +/** + * @brief Packet is sent + * + * This function returns the number of bytes written. + * + * @param[in] req Tevent request + * @param[out] perrno errno in case of failure + * @return the number of bytes written, or -1 on failure + */ +ssize_t pkt_write_recv(struct tevent_req *req, int *perrno); + +#endif /* __CTDB_PKT_WRITE_H__ */ diff --git a/ctdb/common/rb_tree.c b/ctdb/common/rb_tree.c new file mode 100644 index 0000000..0965623 --- /dev/null +++ b/ctdb/common/rb_tree.c @@ -0,0 +1,1101 @@ +/* + a talloc based red-black tree + + Copyright (C) Ronnie Sahlberg 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "lib/util/debug.h" + +#include "common/logging.h" +#include "common/rb_tree.h" + +#define NO_MEMORY_FATAL(p) do { if (!(p)) { \ + DEBUG(DEBUG_CRIT,("Out of memory for %s at %s\n", #p, __location__)); \ + exit(10); \ + }} while (0) + + +static void +tree_destructor_traverse_node(TALLOC_CTX *mem_ctx, trbt_node_t *node) +{ + talloc_set_destructor(node, NULL); + if (node->left) { + tree_destructor_traverse_node(mem_ctx, node->left); + } + if (node->right) { + tree_destructor_traverse_node(mem_ctx, node->right); + } + talloc_steal(mem_ctx, node); +} + +/* + destroy a tree and remove all its nodes + */ +static int tree_destructor(trbt_tree_t *tree) +{ + TALLOC_CTX *tmp_ctx; + trbt_node_t *node; + + if (tree == NULL) { + return 0; + } + + node=tree->root; + if (node == NULL) { + return 0; + } + + /* traverse the tree and remove the node destructor and steal + the node to the temporary context. + we don't want to use the existing destructor for the node + since that will remove the nodes one by one from the tree. + since the entire tree will be completely destroyed we don't care + if it is inconsistent or unbalanced while freeing the + individual nodes + */ + tmp_ctx = talloc_new(NULL); + tree_destructor_traverse_node(tmp_ctx, node); + talloc_free(tmp_ctx); + + return 0; +} + + +/* create a red black tree */ +trbt_tree_t * +trbt_create(TALLOC_CTX *memctx, uint32_t flags) +{ + trbt_tree_t *tree; + + tree = talloc_zero(memctx, trbt_tree_t); + NO_MEMORY_FATAL(tree); + + /* If the tree is freed, we must walk over all entries and steal the + node from the stored data pointer and release the node. + Note, when we free the tree we only free the tree and not any of + the data stored in the tree. + */ + talloc_set_destructor(tree, tree_destructor); + tree->flags = flags; + + return tree; +} + +static inline trbt_node_t * +trbt_parent(trbt_node_t *node) +{ + return node->parent; +} + +static inline trbt_node_t * +trbt_grandparent(trbt_node_t *node) +{ + trbt_node_t *parent; + + parent=trbt_parent(node); + if(parent){ + return parent->parent; + } + return NULL; +} + +static inline trbt_node_t * +trbt_uncle(trbt_node_t *node) +{ + trbt_node_t *parent, *grandparent; + + parent=trbt_parent(node); + if(!parent){ + return NULL; + } + grandparent=trbt_parent(parent); + if(!grandparent){ + return NULL; + } + if(parent==grandparent->left){ + return grandparent->right; + } + return grandparent->left; +} + + +static inline void trbt_insert_case1(trbt_tree_t *tree, trbt_node_t *node); +static inline void trbt_insert_case2(trbt_tree_t *tree, trbt_node_t *node); + +static inline void +trbt_rotate_left(trbt_node_t *node) +{ + trbt_tree_t *tree = node->tree; + + if(node->parent){ + if(node->parent->left==node){ + node->parent->left=node->right; + } else { + node->parent->right=node->right; + } + } else { + tree->root=node->right; + } + node->right->parent=node->parent; + node->parent=node->right; + node->right=node->right->left; + if(node->right){ + node->right->parent=node; + } + node->parent->left=node; +} + +static inline void +trbt_rotate_right(trbt_node_t *node) +{ + trbt_tree_t *tree = node->tree; + + if(node->parent){ + if(node->parent->left==node){ + node->parent->left=node->left; + } else { + node->parent->right=node->left; + } + } else { + tree->root=node->left; + } + node->left->parent=node->parent; + node->parent=node->left; + node->left=node->left->right; + if(node->left){ + node->left->parent=node; + } + node->parent->right=node; +} + +/* NULL nodes are black by definition */ +static inline int trbt_get_color(trbt_node_t *node) +{ + if (node==NULL) { + return TRBT_BLACK; + } + return node->rb_color; +} +static inline int trbt_get_color_left(trbt_node_t *node) +{ + if (node==NULL) { + return TRBT_BLACK; + } + if (node->left==NULL) { + return TRBT_BLACK; + } + return node->left->rb_color; +} +static inline int trbt_get_color_right(trbt_node_t *node) +{ + if (node==NULL) { + return TRBT_BLACK; + } + if (node->right==NULL) { + return TRBT_BLACK; + } + return node->right->rb_color; +} +/* setting a NULL node to black is a nop */ +static inline void trbt_set_color(trbt_node_t *node, int color) +{ + if (node == NULL) { + return; + } + node->rb_color = color; +} +static inline void trbt_set_color_left(trbt_node_t *node, int color) +{ + if (node == NULL || node->left == NULL) { + return; + } + node->left->rb_color = color; +} +static inline void trbt_set_color_right(trbt_node_t *node, int color) +{ + if (node == NULL || node->right == NULL) { + return; + } + node->right->rb_color = color; +} + +static inline void +trbt_insert_case5(trbt_tree_t *tree, trbt_node_t *node) +{ + trbt_node_t *grandparent; + trbt_node_t *parent; + + parent=trbt_parent(node); + grandparent=trbt_parent(parent); + parent->rb_color=TRBT_BLACK; + grandparent->rb_color=TRBT_RED; + if( (node==parent->left) && (parent==grandparent->left) ){ + trbt_rotate_right(grandparent); + } else { + trbt_rotate_left(grandparent); + } +} + +static inline void +trbt_insert_case4(trbt_tree_t *tree, trbt_node_t *node) +{ + trbt_node_t *grandparent; + trbt_node_t *parent; + + parent=trbt_parent(node); + grandparent=trbt_parent(parent); + if(!grandparent){ + return; + } + if( (node==parent->right) && (parent==grandparent->left) ){ + trbt_rotate_left(parent); + node=node->left; + } else if( (node==parent->left) && (parent==grandparent->right) ){ + trbt_rotate_right(parent); + node=node->right; + } + trbt_insert_case5(tree, node); +} + +static inline void +trbt_insert_case3(trbt_tree_t *tree, trbt_node_t *node) +{ + trbt_node_t *grandparent; + trbt_node_t *parent; + trbt_node_t *uncle; + + uncle=trbt_uncle(node); + if(uncle && (uncle->rb_color==TRBT_RED)){ + parent=trbt_parent(node); + parent->rb_color=TRBT_BLACK; + uncle->rb_color=TRBT_BLACK; + grandparent=trbt_grandparent(node); + grandparent->rb_color=TRBT_RED; + trbt_insert_case1(tree, grandparent); + } else { + trbt_insert_case4(tree, node); + } +} + +static inline void +trbt_insert_case2(trbt_tree_t *tree, trbt_node_t *node) +{ + trbt_node_t *parent; + + parent=trbt_parent(node); + /* parent is always non-NULL here */ + if(parent->rb_color==TRBT_BLACK){ + return; + } + trbt_insert_case3(tree, node); +} + +static inline void +trbt_insert_case1(trbt_tree_t *tree, trbt_node_t *node) +{ + trbt_node_t *parent; + + parent=trbt_parent(node); + if(!parent){ + node->rb_color=TRBT_BLACK; + return; + } + trbt_insert_case2(tree, node); +} + +static inline trbt_node_t * +trbt_sibling(trbt_node_t *node) +{ + trbt_node_t *parent; + + parent=trbt_parent(node); + if(!parent){ + return NULL; + } + + if (node == parent->left) { + return parent->right; + } else { + return parent->left; + } +} + +static inline void +trbt_delete_case6(trbt_node_t *node) +{ + trbt_node_t *sibling, *parent; + + sibling = trbt_sibling(node); + parent = trbt_parent(node); + + trbt_set_color(sibling, parent->rb_color); + trbt_set_color(parent, TRBT_BLACK); + if (node == parent->left) { + trbt_set_color_right(sibling, TRBT_BLACK); + trbt_rotate_left(parent); + } else { + trbt_set_color_left(sibling, TRBT_BLACK); + trbt_rotate_right(parent); + } +} + + +static inline void +trbt_delete_case5(trbt_node_t *node) +{ + trbt_node_t *parent, *sibling; + + parent = trbt_parent(node); + sibling = trbt_sibling(node); + if ( (node == parent->left) + &&(trbt_get_color(sibling) == TRBT_BLACK) + &&(trbt_get_color_left(sibling) == TRBT_RED) + &&(trbt_get_color_right(sibling) == TRBT_BLACK) ){ + trbt_set_color(sibling, TRBT_RED); + trbt_set_color_left(sibling, TRBT_BLACK); + trbt_rotate_right(sibling); + trbt_delete_case6(node); + return; + } + if ( (node == parent->right) + &&(trbt_get_color(sibling) == TRBT_BLACK) + &&(trbt_get_color_right(sibling) == TRBT_RED) + &&(trbt_get_color_left(sibling) == TRBT_BLACK) ){ + trbt_set_color(sibling, TRBT_RED); + trbt_set_color_right(sibling, TRBT_BLACK); + trbt_rotate_left(sibling); + trbt_delete_case6(node); + return; + } + + trbt_delete_case6(node); +} + +static inline void +trbt_delete_case4(trbt_node_t *node) +{ + trbt_node_t *sibling; + + sibling = trbt_sibling(node); + if ( (trbt_get_color(node->parent) == TRBT_RED) + &&(trbt_get_color(sibling) == TRBT_BLACK) + &&(trbt_get_color_left(sibling) == TRBT_BLACK) + &&(trbt_get_color_right(sibling) == TRBT_BLACK) ){ + trbt_set_color(sibling, TRBT_RED); + trbt_set_color(node->parent, TRBT_BLACK); + } else { + trbt_delete_case5(node); + } +} + +static void trbt_delete_case1(trbt_node_t *node); + +static inline void +trbt_delete_case3(trbt_node_t *node) +{ + trbt_node_t *sibling; + + sibling = trbt_sibling(node); + if ( (trbt_get_color(node->parent) == TRBT_BLACK) + &&(trbt_get_color(sibling) == TRBT_BLACK) + &&(trbt_get_color_left(sibling) == TRBT_BLACK) + &&(trbt_get_color_right(sibling) == TRBT_BLACK) ){ + trbt_set_color(sibling, TRBT_RED); + trbt_delete_case1(node->parent); + } else { + trbt_delete_case4(node); + } +} + +static inline void +trbt_delete_case2(trbt_node_t *node) +{ + trbt_node_t *sibling; + + sibling = trbt_sibling(node); + if (trbt_get_color(sibling) == TRBT_RED) { + trbt_set_color(node->parent, TRBT_RED); + trbt_set_color(sibling, TRBT_BLACK); + if (node == node->parent->left) { + trbt_rotate_left(node->parent); + } else { + trbt_rotate_right(node->parent); + } + } + trbt_delete_case3(node); +} + +static void +trbt_delete_case1(trbt_node_t *node) +{ + if (!node->parent) { + return; + } else { + trbt_delete_case2(node); + } +} + +static void +delete_node(trbt_node_t *node, bool from_destructor) +{ + trbt_node_t *parent, *child, dc; + trbt_node_t *temp = NULL; + + /* This node has two child nodes, then just copy the content + from the next smaller node with this node and delete the + predecessor instead. + The predecessor is guaranteed to have at most one child + node since its right arm must be NULL + (It must be NULL since we are its sucessor and we are above + it in the tree) + */ + if (node->left != NULL && node->right != NULL) { + /* This node has two children, just copy the data */ + /* find the predecessor */ + temp = node->left; + + while (temp->right != NULL) { + temp = temp->right; + } + + /* swap the predecessor data and key with the node to + be deleted. + */ + node->key32 = temp->key32; + node->data = temp->data; + /* now we let node hang off the new data */ + talloc_steal(node->data, node); + + temp->data = NULL; + temp->key32 = -1; + /* then delete the temp node. + this node is guaranteed to have at least one leaf + child */ + delete_node(temp, from_destructor); + goto finished; + } + + + /* There is at most one child to this node to be deleted */ + child = node->left; + if (node->right) { + child = node->right; + } + + /* If the node to be deleted did not have any child at all we + create a temporary dummy node for the child and mark it black. + Once the delete of the node is finished, we remove this dummy + node, which is simple to do since it is guaranteed that it will + still not have any children after the delete operation. + This is because we don't represent the leaf-nodes as actual nodes + in this implementation. + */ + if (!child) { + child = &dc; + child->tree = node->tree; + child->left=NULL; + child->right=NULL; + child->rb_color=TRBT_BLACK; + child->data=NULL; + } + + /* replace node with child */ + parent = trbt_parent(node); + if (parent) { + if (parent->left == node) { + parent->left = child; + } else { + parent->right = child; + } + } else { + node->tree->root = child; + } + child->parent = node->parent; + + + if (node->rb_color == TRBT_BLACK) { + if (trbt_get_color(child) == TRBT_RED) { + child->rb_color = TRBT_BLACK; + } else { + trbt_delete_case1(child); + } + } + + /* If we had to create a temporary dummy node to represent a black + leaf child we now has to delete it. + This is simple since this dummy node originally had no children + and we are guaranteed that it will also not have any children + after the node has been deleted and any possible rotations + have occurred. + + The only special case is if this was the last node of the tree + in which case we have to reset the root to NULL as well. + Othervise it is enough to just unlink the child from its new + parent. + */ + if (child == &dc) { + if (child->parent == NULL) { + node->tree->root = NULL; + } else if (child == child->parent->left) { + child->parent->left = NULL; + } else { + child->parent->right = NULL; + } + } + +finished: + if (!from_destructor) { + talloc_free(node); + } + + /* if we came from a destructor and temp!=NULL this means we + did the node-swap but now the tree still contains the old + node which was freed in the destructor. Not good. + */ + if (from_destructor && temp) { + temp->key32 = node->key32; + temp->rb_color = node->rb_color; + + temp->data = node->data; + talloc_steal(temp->data, temp); + + temp->parent = node->parent; + if (temp->parent) { + if (temp->parent->left == node) { + temp->parent->left = temp; + } else { + temp->parent->right = temp; + } + } + + temp->left = node->left; + if (temp->left) { + temp->left->parent = temp; + } + temp->right = node->right; + if (temp->right) { + temp->right->parent = temp; + } + + if (temp->tree->root == node) { + temp->tree->root = temp; + } + } + + if ( (node->tree->flags & TRBT_AUTOFREE) + && (node->tree->root == NULL) ) { + talloc_free(node->tree); + } + + return; +} + +/* + destroy a node and remove it from its tree + */ +static int node_destructor(trbt_node_t *node) +{ + delete_node(node, true); + + return 0; +} + +static inline trbt_node_t * +trbt_create_node(trbt_tree_t *tree, trbt_node_t *parent, uint32_t key, void *data) +{ + trbt_node_t *node; + + node=talloc_zero(tree, trbt_node_t); + NO_MEMORY_FATAL(node); + + node->tree=tree; + node->rb_color=TRBT_BLACK; + node->parent=parent; + node->left=NULL; + node->right=NULL; + node->key32=key; + node->data = data; + + /* let this node hang off data so that it is removed when + data is freed + */ + talloc_steal(data, node); + talloc_set_destructor(node, node_destructor); + + return node; +} + +/* insert a new node in the tree. + if there is already a node with a matching key in the tree + we replace it with the new data and return a pointer to the old data + in case the caller wants to take any special action + */ +void * +trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data) +{ + trbt_node_t *node; + + node=tree->root; + + /* is this the first node ?*/ + if(!node){ + node = trbt_create_node(tree, NULL, key, data); + + tree->root=node; + return NULL; + } + + /* it was not the new root so walk the tree until we find where to + * insert this new leaf. + */ + while(1){ + /* this node already exists, replace data and return the + old data + */ + if(key==node->key32){ + void *old_data; + + old_data = node->data; + node->data = data; + /* Let the node now be owned by the new data + so the node is freed when the enw data is released + */ + talloc_steal(node->data, node); + + return old_data; + } + if(keykey32) { + if(!node->left){ + /* new node to the left */ + trbt_node_t *new_node; + + new_node = trbt_create_node(tree, node, key, data); + node->left=new_node; + node=new_node; + + break; + } + node=node->left; + continue; + } + if(key>node->key32) { + if(!node->right){ + /* new node to the right */ + trbt_node_t *new_node; + + new_node = trbt_create_node(tree, node, key, data); + node->right=new_node; + node=new_node; + break; + } + node=node->right; + continue; + } + } + + /* node will now point to the newly created node */ + node->rb_color=TRBT_RED; + trbt_insert_case1(tree, node); + return NULL; +} + +void * +trbt_lookup32(trbt_tree_t *tree, uint32_t key) +{ + trbt_node_t *node; + + node=tree->root; + + while(node){ + if(key==node->key32){ + return node->data; + } + if(keykey32){ + node=node->left; + continue; + } + if(key>node->key32){ + node=node->right; + continue; + } + } + return NULL; +} + + +/* This deletes a node from the tree. + Note that this does not release the data that the node points to +*/ +void +trbt_delete32(trbt_tree_t *tree, uint32_t key) +{ + trbt_node_t *node; + + node=tree->root; + + while(node){ + if(key==node->key32){ + delete_node(node, false); + return; + } + if(keykey32){ + node=node->left; + continue; + } + if(key>node->key32){ + node=node->right; + continue; + } + } +} + + +void +trbt_insert32_callback(trbt_tree_t *tree, uint32_t key, void *(*callback)(void *param, void *data), void *param) +{ + trbt_node_t *node; + + node=tree->root; + + /* is this the first node ?*/ + if(!node){ + node = trbt_create_node(tree, NULL, key, + callback(param, NULL)); + + tree->root=node; + return; + } + + /* it was not the new root so walk the tree until we find where to + * insert this new leaf. + */ + while(1){ + /* this node already exists, replace it + */ + if(key==node->key32){ + node->data = callback(param, node->data); + talloc_steal(node->data, node); + + return; + } + if(keykey32) { + if(!node->left){ + /* new node to the left */ + trbt_node_t *new_node; + + new_node = trbt_create_node(tree, node, key, + callback(param, NULL)); + node->left=new_node; + node=new_node; + + break; + } + node=node->left; + continue; + } + if(key>node->key32) { + if(!node->right){ + /* new node to the right */ + trbt_node_t *new_node; + + new_node = trbt_create_node(tree, node, key, + callback(param, NULL)); + node->right=new_node; + node=new_node; + break; + } + node=node->right; + continue; + } + } + + /* node will now point to the newly created node */ + node->rb_color=TRBT_RED; + trbt_insert_case1(tree, node); + return; +} + + +struct trbt_array_param { + void *(*callback)(void *param, void *data); + void *param; + uint32_t keylen; + uint32_t *key; + trbt_tree_t *tree; +}; +static void *array_insert_callback(void *p, void *data) +{ + struct trbt_array_param *param = (struct trbt_array_param *)p; + trbt_tree_t *tree = NULL; + + + /* if keylen has reached 0 we are done and can call the users + callback function with the users parameters + */ + if (param->keylen == 0) { + return param->callback(param->param, data); + } + + + /* keylen is not zero yes so we must create/process more subtrees */ + /* if data is NULL this means we did not yet have a subtree here + and we must create one. + */ + if (data == NULL) { + /* create a new subtree and hang it off our current tree + set it to autofree so that the tree is freed when + the last node in it has been released. + */ + tree = trbt_create(param->tree, TRBT_AUTOFREE); + } else { + /* we already have a subtree for this path */ + tree = (trbt_tree_t *)data; + } + + trbt_insertarray32_callback(tree, param->keylen, param->key, param->callback, param->param); + + /* now return either the old tree we got in *data or the new tree + we created to our caller so he can update his pointer in his + tree to point to our subtree + */ + return tree; +} + + + +/* insert into the tree using an array of uint32 as a key */ +void +trbt_insertarray32_callback(trbt_tree_t *tree, uint32_t keylen, uint32_t *key, void *(*cb)(void *param, void *data), void *pm) +{ + struct trbt_array_param tap; + + /* keylen-1 and key[1] since the call to insert32 will consume the + first part of the key. + */ + tap.callback= cb; + tap.param = pm; + tap.keylen = keylen-1; + tap.key = &key[1]; + tap.tree = tree; + + trbt_insert32_callback(tree, key[0], array_insert_callback, &tap); +} + +/* lookup the tree using an array of uint32 as a key */ +void * +trbt_lookuparray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key) +{ + /* if keylen is 1 we can do a regular lookup and return this to the + user + */ + if (keylen == 1) { + return trbt_lookup32(tree, key[0]); + } + + /* we need to lookup the next subtree */ + tree = trbt_lookup32(tree, key[0]); + if (tree == NULL) { + /* the key does not exist, return NULL */ + return NULL; + } + + /* now lookup the next part of the key in our new tree */ + return trbt_lookuparray32(tree, keylen-1, &key[1]); +} + + +/* traverse a tree starting at node */ +static int +trbt_traversearray32_node(trbt_node_t *node, uint32_t keylen, + int (*callback)(void *param, void *data), + void *param) +{ + trbt_node_t *left = node->left; + trbt_node_t *right = node->right; + + if (left) { + int ret; + ret = trbt_traversearray32_node(left, keylen, callback, param); + if (ret != 0) { + return ret; + } + } + + /* this is the smallest node in this subtree + if keylen is 0 this means we can just call the callback + otherwise we must pull the next subtree and traverse that one as well + */ + if (keylen == 0) { + int ret; + + ret = callback(param, node->data); + if (ret != 0) { + return ret; + } + } else { + int ret; + + ret = trbt_traversearray32(node->data, keylen, callback, param); + if (ret != 0) { + return ret; + } + } + + if (right) { + int ret; + + ret = trbt_traversearray32_node(right, keylen, callback, param); + if (ret != 0) { + return ret; + } + } + + return 0; +} + + +/* traverse the tree using an array of uint32 as a key */ +int +trbt_traversearray32(trbt_tree_t *tree, uint32_t keylen, + int (*callback)(void *param, void *data), + void *param) +{ + trbt_node_t *node; + + if (tree == NULL) { + return 0; + } + + node=tree->root; + if (node == NULL) { + return 0; + } + + return trbt_traversearray32_node(node, keylen-1, callback, param); +} + + +/* this function will return the first node in a tree where + the key is an array of uint32_t +*/ +void * +trbt_findfirstarray32(trbt_tree_t *tree, uint32_t keylen) +{ + trbt_node_t *node; + + if (keylen < 1) { + return NULL; + } + + if (tree == NULL) { + return NULL; + } + + node=tree->root; + if (node == NULL) { + return NULL; + } + + while (node->left) { + node = node->left; + } + + /* we found our node so return the data */ + if (keylen == 1) { + return node->data; + } + + /* we are still traversing subtrees so find the first node in the + next level of trees + */ + return trbt_findfirstarray32(node->data, keylen-1); +} + + +#ifdef TEST_RB_TREE +static void printtree(trbt_node_t *node, int levels) +{ + int i; + if(node==NULL)return; + printtree(node->left, levels+1); + + for(i=0;ikey32,node->rb_color==TRBT_BLACK?"BLACK":"RED", node, node->parent, node->left, node->right); + + printtree(node->right, levels+1); + printf("\n"); +} + +void print_tree(trbt_tree_t *tree) +{ + if(tree->root==NULL){ + printf("tree is empty\n"); + return; + } + printf("---\n"); + printtree(tree->root->left, 1); + printf("root node key:%d COLOR:%s (node:%p left:%p right:%p)\n",tree->root->key32,tree->root->rb_color==TRBT_BLACK?"BLACK":"RED", tree->root, tree->root->left, tree->root->right); + printtree(tree->root->right, 1); + printf("===\n"); +} + +void +test_tree(void) +{ + trbt_tree_t *tree; + char *str; + int i, ret; + int NUM=15; + int cnt=0; + + tree=trbt_create(talloc_new(NULL), 0); +#if 0 + for(i=0;i<10;i++){ + printf("adding node %i\n",i); + trbt_insert32(tree, i, NULL); + print_tree(tree); + } + printf("deleting node %i\n",3); + trbt_delete32(tree, 3); + print_tree(tree); + for(i=0;i<10;i++){ + printf("deleting node %i\n",i); + trbt_delete32(tree, i); + print_tree(tree); + } +exit(0); +#endif + while(++cnt){ + int i; + printf("iteration : %d\n",cnt); + i=random()%20; + printf("adding node %i\n",i); + trbt_insert32(tree, i, NULL); + print_tree(tree); + + i=random()%20; + printf("deleting node %i\n",i); + trbt_delete32(tree, i); + print_tree(tree); + } + +} + +#endif /* TEST_RB_TREE */ diff --git a/ctdb/common/rb_tree.h b/ctdb/common/rb_tree.h new file mode 100644 index 0000000..b5ddbb2 --- /dev/null +++ b/ctdb/common/rb_tree.h @@ -0,0 +1,90 @@ +/* + a talloc based red-black tree + + Copyright (C) Ronnie Sahlberg 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef _RB_TREE_H +#define _RB_TREE_H + +#define TRBT_RED 0x00 +#define TRBT_BLACK 0x01 +typedef struct trbt_node { + struct trbt_tree *tree; + struct trbt_node *parent; + struct trbt_node *left; + struct trbt_node *right; + uint32_t rb_color; + uint32_t key32; + void *data; +} trbt_node_t; + +typedef struct trbt_tree { + trbt_node_t *root; +/* automatically free the tree when the last node has been deleted */ +#define TRBT_AUTOFREE 0x00000001 + uint32_t flags; +} trbt_tree_t; + + + +/* Create a RB tree */ +trbt_tree_t *trbt_create(TALLOC_CTX *memctx, uint32_t flags); + +/* Lookup a node in the tree and return a pointer to data or NULL */ +void *trbt_lookup32(trbt_tree_t *tree, uint32_t key); + +/* Insert a new node into the tree. If there was already a node with this + key the pointer to the previous data is returned. + The tree will talloc_steal() the data inserted into the tree . +*/ +void *trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data); + +/* Insert a new node into the tree. + If this is a new node: + callback is called with data==NULL and param=param + the returned value from the callback is talloc_stolen and inserted in the + tree. + If a node already exists for this key then: + callback is called with data==existing data and param=param + the returned calue is talloc_stolen and inserted in the tree +*/ +void trbt_insert32_callback(trbt_tree_t *tree, uint32_t key, void *(*callback)(void *param, void *data), void *param); + +/* Delete a node from the tree and free all data associated with it */ +void trbt_delete32(trbt_tree_t *tree, uint32_t key); + + +/* insert into the tree with a key based on an array of uint32 */ +void trbt_insertarray32_callback(trbt_tree_t *tree, uint32_t keylen, uint32_t *key, void *(*callback)(void *param, void *data), void *param); + +/* Lookup a node in the tree with a key based on an array of uint32 + and return a pointer to data or NULL */ +void *trbt_lookuparray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key); + +/* Traverse a tree with a key based on an array of uint32 + returns 0 if traverse completed + !0 if the traverse was aborted + + If the callback returns !0 the traverse will be aborted +*/ +int trbt_traversearray32(trbt_tree_t *tree, uint32_t keylen, int (*callback)(void *param, void *data), void *param); + +/* Lookup the first node in the tree with a key based on an array of uint32 + and return a pointer to data or NULL */ +void *trbt_findfirstarray32(trbt_tree_t *tree, uint32_t keylen); + +#endif /* _RB_TREE_H */ diff --git a/ctdb/common/reqid.c b/ctdb/common/reqid.c new file mode 100644 index 0000000..0e651cf --- /dev/null +++ b/ctdb/common/reqid.c @@ -0,0 +1,89 @@ +/* + ctdb request id handling code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "lib/util/idtree.h" +#include "reqid.h" + +struct reqid_context { + struct idr_context *idr; + uint32_t lastid; +}; + +int reqid_init(TALLOC_CTX *mem_ctx, int start_id, + struct reqid_context **result) +{ + struct reqid_context *reqid_ctx; + + reqid_ctx = talloc_zero(mem_ctx, struct reqid_context); + if (reqid_ctx == NULL) { + return ENOMEM; + } + + reqid_ctx->idr = idr_init(reqid_ctx); + if (reqid_ctx->idr == NULL) { + talloc_free(reqid_ctx); + return ENOMEM; + } + + if (start_id <= 0) { + start_id = 1; + } + reqid_ctx->lastid = start_id; + + *result = reqid_ctx; + return 0; +} + +uint32_t reqid_new(struct reqid_context *reqid_ctx, void *private_data) +{ + int id; + + id = idr_get_new_above(reqid_ctx->idr, private_data, + reqid_ctx->lastid+1, INT_MAX); + if (id < 0) { + /* reqid wrapped */ + id = idr_get_new(reqid_ctx->idr, private_data, INT_MAX); + } + if (id == -1) { + return REQID_INVALID; + } + + reqid_ctx->lastid = id; + return id; +} + +void *_reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid) +{ + return idr_find(reqid_ctx->idr, reqid); +} + +int reqid_remove(struct reqid_context *reqid_ctx, uint32_t reqid) +{ + int ret; + + ret = idr_remove(reqid_ctx->idr, reqid); + if (ret < 0) { + return ENOENT; + } + return 0; +} diff --git a/ctdb/common/reqid.h b/ctdb/common/reqid.h new file mode 100644 index 0000000..736e5b3 --- /dev/null +++ b/ctdb/common/reqid.h @@ -0,0 +1,89 @@ +/* + Request id database + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_REQID_H__ +#define __CTDB_REQID_H__ + +#include + +/** + * @file reqid.h + * + * @brief Request id database + * + * CTDB tracks messsages using request id. CTDB stores client state for each + * request id to process the replies correctly. + */ + +/** + * @brief Abstract struct to store request id database + */ +struct reqid_context; + +#define REQID_INVALID 0xffffffff + +/** + * @brief Initialize request id database + * + * This returns a new request id context. Freeing this context will free + * all the memory associated with request id database. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] start_id The initial id + * @param[out] result The new talloc_context structure + * @return 0 on success, errno on failure + */ +int reqid_init(TALLOC_CTX *mem_ctx, int start_id, + struct reqid_context **result); + +/** + * @brief Generate new request id and associate given data with the request id + * + * @param[in] reqid_ctx The request id context + * @param[in] private_data The state to associate with new request id + * @return new request id, REQID_INVALID on failure + */ +uint32_t reqid_new(struct reqid_context *reqid_ctx, void *private_data); + +#ifdef DOXYGEN +/** + * @brief Fetch the data associated with the request id + * + * @param[in] reqid_ctx The request id context + * @param[in] reqid The request id + * @param[in] type The data type of the stored data + * @return the data stored for the reqid, NULL on failure + */ +type *reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid, #type); +#else +void *_reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid); +#define reqid_find(ctx, reqid, type) \ + (type *)talloc_check_name(_reqid_find(ctx, reqid), #type) +#endif + +/** + * @brief Remove the data associated with the request id + * + * @param[in] reqid_ctx The request id context + * @param[in] reqid The request id + * @return 0 on success, errno on failure + */ +int reqid_remove(struct reqid_context *reqid_ctx, uint32_t reqid); + +#endif /* __CTDB_REQID_H__ */ diff --git a/ctdb/common/run_event.c b/ctdb/common/run_event.c new file mode 100644 index 0000000..d283664 --- /dev/null +++ b/ctdb/common/run_event.c @@ -0,0 +1,829 @@ +/* + Run scripts in a directory with specific event arguments + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/dir.h" +#include "system/glob.h" +#include "system/wait.h" + +#include +#include + +#include "lib/util/tevent_unix.h" +#include "lib/util/debug.h" + +#include "common/logging.h" +#include "common/run_proc.h" +#include "common/event_script.h" + +#include "common/run_event.h" + +/* + * Utility functions + */ + +static int get_script_list(TALLOC_CTX *mem_ctx, + const char *script_dir, + struct run_event_script_list **out) +{ + struct event_script_list *s_list; + struct run_event_script_list *script_list; + unsigned int i; + int ret; + + ret = event_script_get_list(mem_ctx, script_dir, &s_list); + if (ret != 0) { + if (ret == ENOENT) { + D_WARNING("event script dir %s removed\n", script_dir); + } else { + D_WARNING("failed to get script list for %s, ret=%d\n", + script_dir, ret); + } + return ret; + } + + if (s_list->num_scripts == 0) { + *out = NULL; + talloc_free(s_list); + return 0; + } + + script_list = talloc_zero(mem_ctx, struct run_event_script_list); + if (script_list == NULL) { + talloc_free(s_list); + return ENOMEM; + } + + script_list->num_scripts = s_list->num_scripts; + script_list->script = talloc_zero_array(script_list, + struct run_event_script, + script_list->num_scripts); + if (script_list->script == NULL) { + talloc_free(s_list); + talloc_free(script_list); + return ENOMEM; + } + + for (i = 0; i < s_list->num_scripts; i++) { + struct event_script *s = s_list->script[i]; + struct run_event_script *script = &script_list->script[i]; + + script->name = talloc_steal(script_list->script, s->name); + + if (! s->enabled) { + script->summary = -ENOEXEC; + } + } + + talloc_free(s_list); + *out = script_list; + return 0; +} + +static int script_args(TALLOC_CTX *mem_ctx, const char *event_str, + const char *arg_str, const char ***out) +{ + const char **argv; + size_t argc; + size_t len; + + /* Preallocate argv array to avoid reallocation. */ + len = 8; + argv = talloc_array(mem_ctx, const char *, len); + if (argv == NULL) { + return ENOMEM; + } + + argv[0] = NULL; /* script name */ + argv[1] = event_str; + argc = 2; + + if (arg_str != NULL) { + char *str, *t, *tok; + + str = talloc_strdup(argv, arg_str); + if (str == NULL) { + return ENOMEM; + } + + t = str; + while ((tok = strtok(t, " ")) != NULL) { + argv[argc] = talloc_strdup(argv, tok); + if (argv[argc] == NULL) { + talloc_free(argv); + return ENOMEM; + } + argc += 1; + if (argc >= len) { + argv = talloc_realloc(mem_ctx, argv, + const char *, len + 8); + if (argv == NULL) { + return ENOMEM; + } + len += 8; + } + t = NULL; + } + + talloc_free(str); + } + + argv[argc] = NULL; + /* argc += 1 */ + + *out = argv; + return 0; +} + +struct run_event_context { + struct run_proc_context *run_proc_ctx; + const char *script_dir; + const char *debug_prog; + bool debug_running; + + struct tevent_queue *queue; + struct tevent_req *current_req; + bool monitor_running; +}; + + +int run_event_init(TALLOC_CTX *mem_ctx, struct run_proc_context *run_proc_ctx, + const char *script_dir, const char *debug_prog, + struct run_event_context **out) +{ + struct run_event_context *run_ctx; + struct stat st; + int ret; + + run_ctx = talloc_zero(mem_ctx, struct run_event_context); + if (run_ctx == NULL) { + return ENOMEM; + } + + run_ctx->run_proc_ctx = run_proc_ctx; + + ret = stat(script_dir, &st); + if (ret != 0) { + ret = errno; + talloc_free(run_ctx); + return ret; + } + + if (! S_ISDIR(st.st_mode)) { + talloc_free(run_ctx); + return ENOTDIR; + } + + run_ctx->script_dir = talloc_strdup(run_ctx, script_dir); + if (run_ctx->script_dir == NULL) { + talloc_free(run_ctx); + return ENOMEM; + } + + if (debug_prog != NULL) { + run_ctx->debug_prog = talloc_strdup(run_ctx, debug_prog); + if (run_ctx->debug_prog == NULL) { + talloc_free(run_ctx); + return ENOMEM; + } + } + + run_ctx->debug_running = false; + + run_ctx->queue = tevent_queue_create(run_ctx, "run event queue"); + if (run_ctx->queue == NULL) { + talloc_free(run_ctx); + return ENOMEM; + } + + run_ctx->monitor_running = false; + + *out = run_ctx; + return 0; +} + +static struct run_proc_context * +run_event_run_proc_context(struct run_event_context *run_ctx) +{ + return run_ctx->run_proc_ctx; +} + +static const char *run_event_script_dir(struct run_event_context *run_ctx) +{ + return run_ctx->script_dir; +} + +static const char *run_event_debug_prog(struct run_event_context *run_ctx) +{ + return run_ctx->debug_prog; +} + +static struct tevent_queue *run_event_queue(struct run_event_context *run_ctx) +{ + return run_ctx->queue; +} + +static void run_event_start_running(struct run_event_context *run_ctx, + struct tevent_req *req, bool is_monitor) +{ + run_ctx->current_req = req; + run_ctx->monitor_running = is_monitor; +} + +static void run_event_stop_running(struct run_event_context *run_ctx) +{ + run_ctx->current_req = NULL; + run_ctx->monitor_running = false; +} + +static struct tevent_req *run_event_get_running( + struct run_event_context *run_ctx, + bool *is_monitor) +{ + *is_monitor = run_ctx->monitor_running; + return run_ctx->current_req; +} + +static int run_event_script_status(struct run_event_script *script) +{ + int ret; + + if (script->result.sig > 0) { + ret = -EINTR; + } else if (script->result.err > 0) { + if (script->result.err == EACCES) { + /* Map EACCESS to ENOEXEC */ + ret = -ENOEXEC; + } else { + ret = -script->result.err; + } + } else { + ret = script->result.status; + } + + return ret; +} + +int run_event_list(struct run_event_context *run_ctx, + TALLOC_CTX *mem_ctx, + struct run_event_script_list **output) +{ + struct event_script_list *s_list = NULL; + struct run_event_script_list *script_list = NULL; + unsigned int i; + int ret; + + ret = event_script_get_list(mem_ctx, + run_event_script_dir(run_ctx), + &s_list); + if (ret != 0) { + return ret; + } + + if (s_list->num_scripts == 0) { + *output = NULL; + talloc_free(s_list); + return 0; + } + + script_list = talloc_zero(mem_ctx, struct run_event_script_list); + if (script_list == NULL) { + return ENOMEM; + } + + script_list->num_scripts = s_list->num_scripts; + script_list->script = talloc_zero_array(script_list, + struct run_event_script, + script_list->num_scripts); + if (script_list->script == NULL) { + talloc_free(s_list); + talloc_free(script_list); + return ENOMEM; + } + + for (i=0; i < s_list->num_scripts; i++) { + struct event_script *s = s_list->script[i]; + struct run_event_script *script = &script_list->script[i]; + + script->name = talloc_steal(script_list->script, s->name); + + if (! s->enabled) { + script->summary = -ENOEXEC; + } + } + + + talloc_free(s_list); + *output = script_list; + return 0; +} + +int run_event_script_enable(struct run_event_context *run_ctx, + const char *script_name) +{ + return event_script_chmod(run_event_script_dir(run_ctx), + script_name, + true); +} + +int run_event_script_disable(struct run_event_context *run_ctx, + const char *script_name) +{ + return event_script_chmod(run_event_script_dir(run_ctx), + script_name, + false); +} + +/* + * Run debug program to diagnose hung scripts + */ + +static int debug_args(TALLOC_CTX *mem_ctx, const char *path, + const char *event_str, pid_t pid, const char ***out) +{ + const char **argv; + + argv = talloc_array(mem_ctx, const char *, 4); + if (argv == NULL) { + return ENOMEM; + } + + argv[0] = path; + argv[1] = talloc_asprintf(argv, "%d", pid); + argv[2] = event_str; + if (argv[1] == NULL) { + talloc_free(argv); + return ENOMEM; + } + argv[3] = NULL; + + *out = argv; + return 0; +} + +static void debug_log(int loglevel, const char *output, const char *log_prefix) +{ + char *line, *s; + + s = strdup(output); + if (s == NULL) { + DEBUG(loglevel, ("%s: %s\n", log_prefix, output)); + return; + } + + line = strtok(s, "\n"); + while (line != NULL) { + DEBUG(loglevel, ("%s: %s\n", log_prefix, line)); + line = strtok(NULL, "\n"); + } + free(s); +} + +struct run_debug_state { + struct run_event_context *run_ctx; + pid_t pid; +}; + +static void run_debug_done(struct tevent_req *subreq); + +static struct tevent_req *run_debug_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct run_event_context *run_ctx, + const char *event_str, pid_t pid) +{ + struct tevent_req *req, *subreq; + struct run_debug_state *state; + const char **argv; + const char *debug_prog; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct run_debug_state); + if (req == NULL) { + return NULL; + } + + state->run_ctx = run_ctx; + state->pid = pid; + + debug_prog = run_event_debug_prog(run_ctx); + if (debug_prog == NULL) { + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + if (run_ctx->debug_running) { + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + if (pid == -1) { + D_DEBUG("Event script terminated, nothing to debug\n"); + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + ret = debug_args(state, debug_prog, event_str, pid, &argv); + if (ret != 0) { + D_ERR("debug_args() failed\n"); + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + D_DEBUG("Running debug %s with args \"%s %s\"\n", + debug_prog, argv[1], argv[2]); + + subreq = run_proc_send(state, ev, run_event_run_proc_context(run_ctx), + debug_prog, argv, -1, tevent_timeval_zero()); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, run_debug_done, req); + + run_ctx->debug_running = true; + + talloc_free(argv); + return req; +} + +static void run_debug_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct run_debug_state *state = tevent_req_data( + req, struct run_debug_state); + char *output; + int ret; + bool status; + + state->run_ctx->debug_running = false; + + status = run_proc_recv(subreq, &ret, NULL, NULL, state, &output); + TALLOC_FREE(subreq); + if (! status) { + D_ERR("Running debug failed, ret=%d\n", ret); + } + + /* Log output */ + if (output != NULL) { + debug_log(DEBUG_ERR, output, "event_debug"); + talloc_free(output); + } + + kill(-state->pid, SIGTERM); + tevent_req_done(req); +} + +static bool run_debug_recv(struct tevent_req *req, int *perr) +{ + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + return true; +} + +/* + * Run a single event + */ + +struct run_event_state { + struct tevent_context *ev; + struct run_event_context *run_ctx; + const char *event_str; + const char *arg_str; + struct timeval timeout; + bool continue_on_failure; + + struct run_event_script_list *script_list; + const char **argv; + struct tevent_req *script_subreq; + unsigned int index; + bool cancelled; +}; + +static void run_event_cancel(struct tevent_req *req); +static void run_event_trigger(struct tevent_req *req, void *private_data); +static struct tevent_req *run_event_run_script(struct tevent_req *req); +static void run_event_next_script(struct tevent_req *subreq); +static void run_event_debug(struct tevent_req *req, pid_t pid); +static void run_event_debug_done(struct tevent_req *subreq); + +struct tevent_req *run_event_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct run_event_context *run_ctx, + const char *event_str, + const char *arg_str, + struct timeval timeout, + bool continue_on_failure) +{ + struct tevent_req *req, *current_req; + struct run_event_state *state; + bool monitor_running, status; + + req = tevent_req_create(mem_ctx, &state, struct run_event_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->run_ctx = run_ctx; + state->event_str = talloc_strdup(state, event_str); + if (tevent_req_nomem(state->event_str, req)) { + return tevent_req_post(req, ev); + } + if (arg_str != NULL) { + state->arg_str = talloc_strdup(state, arg_str); + if (tevent_req_nomem(state->arg_str, req)) { + return tevent_req_post(req, ev); + } + } + state->timeout = timeout; + state->continue_on_failure = continue_on_failure; + state->cancelled = false; + + state->script_list = talloc_zero(state, struct run_event_script_list); + if (tevent_req_nomem(state->script_list, req)) { + return tevent_req_post(req, ev); + } + + /* + * If monitor event is running, + * cancel the running monitor event and run new event + * + * If any other event is running, + * if new event is monitor, cancel that event + * else add new event to the queue + */ + + current_req = run_event_get_running(run_ctx, &monitor_running); + if (current_req != NULL) { + if (monitor_running) { + run_event_cancel(current_req); + } else if (strcmp(event_str, "monitor") == 0) { + state->script_list->summary = -ECANCELED; + tevent_req_done(req); + return tevent_req_post(req, ev); + } + } + + status = tevent_queue_add(run_event_queue(run_ctx), ev, req, + run_event_trigger, NULL); + if (! status) { + tevent_req_error(req, ENOMEM); + return tevent_req_post(req, ev); + } + + return req; +} + +static void run_event_cancel(struct tevent_req *req) +{ + struct run_event_state *state = tevent_req_data( + req, struct run_event_state); + + run_event_stop_running(state->run_ctx); + + state->script_list->summary = -ECANCELED; + state->cancelled = true; + + TALLOC_FREE(state->script_subreq); + + tevent_req_done(req); +} + +static void run_event_trigger(struct tevent_req *req, void *private_data) +{ + struct tevent_req *subreq; + struct run_event_state *state = tevent_req_data( + req, struct run_event_state); + struct run_event_script_list *script_list; + int ret; + bool is_monitor = false; + + D_DEBUG("Running event %s with args \"%s\"\n", state->event_str, + state->arg_str == NULL ? "(null)" : state->arg_str); + + ret = get_script_list(state, + run_event_script_dir(state->run_ctx), + &script_list); + if (ret != 0) { + D_ERR("get_script_list() failed, ret=%d\n", ret); + tevent_req_error(req, ret); + return; + } + + /* No scripts */ + if (script_list == NULL || script_list->num_scripts == 0) { + tevent_req_done(req); + return; + } + + talloc_free(state->script_list); + state->script_list = script_list; + + ret = script_args(state, state->event_str, state->arg_str, + &state->argv); + if (ret != 0) { + D_ERR("script_args() failed, ret=%d\n", ret); + tevent_req_error(req, ret); + return; + } + + state->index = 0; + + subreq = run_event_run_script(req); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, run_event_next_script, req); + + state->script_subreq = subreq; + + if (strcmp(state->event_str, "monitor") == 0) { + is_monitor = true; + } + run_event_start_running(state->run_ctx, req, is_monitor); +} + +static struct tevent_req *run_event_run_script(struct tevent_req *req) +{ + struct run_event_state *state = tevent_req_data( + req, struct run_event_state); + struct run_event_script *script; + struct tevent_req *subreq; + char *path; + + script = &state->script_list->script[state->index]; + + path = talloc_asprintf(state, "%s/%s.script", + run_event_script_dir(state->run_ctx), + script->name); + if (path == NULL) { + return NULL; + } + + state->argv[0] = script->name; + script->begin = tevent_timeval_current(); + + D_DEBUG("Running %s with args \"%s %s\"\n", + path, state->argv[0], state->argv[1]); + + subreq = run_proc_send(state, state->ev, + run_event_run_proc_context(state->run_ctx), + path, state->argv, -1, state->timeout); + + talloc_free(path); + + return subreq; +} + +static void run_event_next_script(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct run_event_state *state = tevent_req_data( + req, struct run_event_state); + struct run_event_script *script; + pid_t pid; + int ret; + bool status; + + script = &state->script_list->script[state->index]; + script->end = tevent_timeval_current(); + + status = run_proc_recv(subreq, &ret, &script->result, &pid, + state->script_list, &script->output); + TALLOC_FREE(subreq); + state->script_subreq = NULL; + if (! status) { + D_ERR("run_proc failed for %s, ret=%d\n", script->name, ret); + run_event_stop_running(state->run_ctx); + tevent_req_error(req, ret); + return; + } + + if (state->cancelled) { + return; + } + + /* Log output */ + if (script->output != NULL) { + debug_log(DEBUG_ERR, script->output, script->name); + } + + D_DEBUG("Script %s finished sig=%d, err=%d, status=%d\n", + script->name, script->result.sig, script->result.err, + script->result.status); + + + /* If a script fails, stop running */ + script->summary = run_event_script_status(script); + if (script->summary != 0 && script->summary != -ENOEXEC) { + state->script_list->summary = script->summary; + + if (! state->continue_on_failure) { + state->script_list->num_scripts = state->index + 1; + + if (script->summary == -ETIMEDOUT && pid != -1) { + run_event_debug(req, pid); + } + D_NOTICE("%s event %s\n", state->event_str, + (script->summary == -ETIMEDOUT) ? + "timed out" : + "failed"); + run_event_stop_running(state->run_ctx); + tevent_req_done(req); + return; + } + } + + state->index += 1; + + /* All scripts executed */ + if (state->index >= state->script_list->num_scripts) { + run_event_stop_running(state->run_ctx); + tevent_req_done(req); + return; + } + + subreq = run_event_run_script(req); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, run_event_next_script, req); + + state->script_subreq = subreq; +} + +static void run_event_debug(struct tevent_req *req, pid_t pid) +{ + struct run_event_state *state = tevent_req_data( + req, struct run_event_state); + struct tevent_req *subreq; + + /* Debug script is run with ectx as the memory context */ + subreq = run_debug_send(state->run_ctx, state->ev, state->run_ctx, + state->event_str, pid); + if (subreq == NULL) { + /* If run debug fails, it's not an error */ + D_NOTICE("Failed to run event debug\n"); + return; + } + tevent_req_set_callback(subreq, run_event_debug_done, NULL); +} + +static void run_event_debug_done(struct tevent_req *subreq) +{ + int ret = 0; + bool status; + + status = run_debug_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + D_NOTICE("run_debug() failed, ret=%d\n", ret); + } +} + +bool run_event_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct run_event_script_list **script_list) +{ + struct run_event_state *state = tevent_req_data( + req, struct run_event_state); + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (script_list != NULL) { + *script_list = talloc_steal(mem_ctx, state->script_list); + } + return true; +} + diff --git a/ctdb/common/run_event.h b/ctdb/common/run_event.h new file mode 100644 index 0000000..f53bca3 --- /dev/null +++ b/ctdb/common/run_event.h @@ -0,0 +1,150 @@ +/* + Run scripts in a directory with specific event arguments + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_RUN_EVENT_H__ +#define __CTDB_RUN_EVENT_H__ + +#include +#include + +#include "common/run_proc.h" + +/** + * @file run_event.h + * + * @brief Run scripts in a directory with specific event arguments. + * + * This abstraction allows one to execute multiple scripts in a directory + * (specified by script_dir) with given event and arguments. + * + * At one time, only one event can be run. Multiple run_event calls + * will cause events to be queued up. They will be run sequentially. + * + * A "monitor" event is special and has special semantics. + * + * If a monitor event is running and another event is scheduled, the + * currently running monitor event is cancelled. + * + * If an event (not monitor) is running and monitor event is scheduled, + * then the monior event will be cancelled immediately. + */ + +/** + * @brief The run process context + */ +struct run_event_context; + +struct run_event_script { + char *name; + struct timeval begin, end; + struct run_proc_result result; + int summary; + char *output; +}; + +struct run_event_script_list { + uint32_t num_scripts; + struct run_event_script *script; + int summary; +}; + + +/** + * @brief Initialize the context for running events + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] script_dir Directory containing script to run + * @param[in] debug_prog Path of a program to run if a script hangs + * @param[out] result New run_event context + * @return 0 on success, errno on error + */ +int run_event_init(TALLOC_CTX *mem_ctx, struct run_proc_context *run_proc_ctx, + const char *script_dir, const char *debug_prog, + struct run_event_context **result); + +/** + * @brief Get a list of scripts + * + * @param[in] run_ctx Run_event context + * @param[in] mem_ctx Talloc memory context + * @param[out] output List of valid scripts + * @return 0 on success, errno on failure + */ +int run_event_list(struct run_event_context *run_ctx, + TALLOC_CTX *mem_ctx, + struct run_event_script_list **output); + +/** + * @brief Enable a script + * + * @param[in] run_ctx Run_event context + * @param[in] script_name Name of the script to enable + * @return 0 on success, errno on failure + */ +int run_event_script_enable(struct run_event_context *run_ctx, + const char *script_name); + +/** + * @brief Disable a script + * + * @param[in] run_ctx Run_event context + * @param[in] script_name Name of the script to disable + * @return 0 on success, errno on failure + */ +int run_event_script_disable(struct run_event_context *run_ctx, + const char *script_name); + +/** + * @brief Async computation start to run an event + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] run_ctx Run_event context + * @param[in] event_str The event argument to the script + * @param[in] arg_str Event arguments to the script + * @param[in] timeout How long to wait for execution + * @param[in] continue_on_failure Whether to continue to run events on failure + * @return new tevent request, or NULL on failure + * + * arg_str contains optional arguments for an event. + */ +struct tevent_req *run_event_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct run_event_context *run_ctx, + const char *event_str, + const char *arg_str, + struct timeval timeout, + bool continue_on_failure); + +/** + * @brief Async computation end to run an event + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc memory context + * @param[out] output List of scripts executed and their status + * @return true on success, false on failure + */ +bool run_event_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, + struct run_event_script_list **output); + +#endif /* __CTDB_RUN_EVENT_H__ */ + diff --git a/ctdb/common/run_proc.c b/ctdb/common/run_proc.c new file mode 100644 index 0000000..84bc343 --- /dev/null +++ b/ctdb/common/run_proc.c @@ -0,0 +1,503 @@ +/* + Run a child process and collect the output + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/wait.h" + +#include +#include + +#include "lib/util/tevent_unix.h" +#include "lib/util/sys_rw.h" +#include "lib/util/blocking.h" +#include "lib/util/dlinklist.h" + +#include "common/run_proc.h" + +/* + * Process abstraction + */ + +struct run_proc_context; + +struct proc_context { + struct proc_context *prev, *next; + + pid_t pid; + + int fd; + struct tevent_fd *fde; + + char *output; + struct run_proc_result result; + + struct tevent_req *req; +}; + +static int proc_destructor(struct proc_context *proc); + +static struct proc_context *proc_new(TALLOC_CTX *mem_ctx, + struct run_proc_context *run_ctx) +{ + struct proc_context *proc; + + proc = talloc_zero(mem_ctx, struct proc_context); + if (proc == NULL) { + return NULL; + } + + proc->pid = -1; + proc->fd = -1; + + talloc_set_destructor(proc, proc_destructor); + + return proc; +} + +static void run_proc_kill(struct tevent_req *req); + +static int proc_destructor(struct proc_context *proc) +{ + if (proc->req != NULL) { + run_proc_kill(proc->req); + } + + talloc_free(proc->fde); + if (proc->pid != -1) { + kill(-proc->pid, SIGKILL); + } + + return 0; +} + +static void proc_read_handler(struct tevent_context *ev, + struct tevent_fd *fde, uint16_t flags, + void *private_data); + +static int proc_start(struct proc_context *proc, struct tevent_context *ev, + const char *path, const char **argv, int stdin_fd) +{ + int fd[2]; + int ret; + + ret = pipe(fd); + if (ret != 0) { + return ret; + } + + proc->pid = fork(); + if (proc->pid == -1) { + ret = errno; + close(fd[0]); + close(fd[1]); + return ret; + } + + if (proc->pid == 0) { + close(fd[0]); + + ret = dup2(fd[1], STDOUT_FILENO); + if (ret == -1) { + exit(64 + errno); + } + ret = dup2(fd[1], STDERR_FILENO); + if (ret == -1) { + exit(64 + errno); + } + + close(fd[1]); + + if (stdin_fd != -1) { + ret = dup2(stdin_fd, STDIN_FILENO); + if (ret == -1) { + exit(64 + errno); + } + } + + ret = setpgid(0, 0); + if (ret != 0) { + exit(64 + errno); + } + + ret = execv(path, discard_const(argv)); + if (ret != 0) { + exit(64 + errno); + } + + exit(64 + ENOEXEC); + } + + close(fd[1]); + + proc->fd = fd[0]; + proc->fde = tevent_add_fd(ev, proc, fd[0], TEVENT_FD_READ, + proc_read_handler, proc); + if (proc->fde == NULL) { + close(fd[0]); + return ENOMEM; + } + + tevent_fd_set_auto_close(proc->fde); + + return 0; +} + +static void proc_read_handler(struct tevent_context *ev, + struct tevent_fd *fde, uint16_t flags, + void *private_data) +{ + struct proc_context *proc = talloc_get_type_abort( + private_data, struct proc_context); + size_t offset; + ssize_t nread; + int len = 0; + int ret; + + ret = ioctl(proc->fd, FIONREAD, &len); + if (ret != 0) { + goto fail; + } + + if (len == 0) { + /* pipe closed */ + goto close; + } + + offset = (proc->output == NULL) ? 0 : strlen(proc->output); + + proc->output = talloc_realloc(proc, proc->output, char, offset+len+1); + if (proc->output == NULL) { + goto fail; + } + + nread = sys_read(proc->fd, proc->output + offset, len); + if (nread == -1) { + goto fail; + } + proc->output[offset+nread] = '\0'; + return; + +fail: + if (proc->pid != -1) { + kill(-proc->pid, SIGKILL); + proc->pid = -1; + } +close: + TALLOC_FREE(proc->fde); + proc->fd = -1; +} + + +/* + * Run proc abstraction + */ + +struct run_proc_context { + struct tevent_context *ev; + struct tevent_signal *se; + struct proc_context *plist; +}; + +static void run_proc_signal_handler(struct tevent_context *ev, + struct tevent_signal *se, + int signum, int count, void *siginfo, + void *private_data); +static int run_proc_context_destructor(struct run_proc_context *run_ctx); +static void run_proc_done(struct tevent_req *req); + +int run_proc_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct run_proc_context **result) +{ + struct run_proc_context *run_ctx; + + run_ctx = talloc_zero(mem_ctx, struct run_proc_context); + if (run_ctx == NULL) { + return ENOMEM; + } + + run_ctx->ev = ev; + run_ctx->se = tevent_add_signal(ev, run_ctx, SIGCHLD, 0, + run_proc_signal_handler, run_ctx); + if (run_ctx->se == NULL) { + talloc_free(run_ctx); + return ENOMEM; + } + + talloc_set_destructor(run_ctx, run_proc_context_destructor); + + *result = run_ctx; + return 0; +} + +static void run_proc_signal_handler(struct tevent_context *ev, + struct tevent_signal *se, + int signum, int count, void *siginfo, + void *private_data) +{ + struct run_proc_context *run_ctx = talloc_get_type_abort( + private_data, struct run_proc_context); + struct proc_context *proc; + pid_t pid = -1; + int status; + +again: + pid = waitpid(-1, &status, WNOHANG); + if (pid == -1) { + return; + } + + if (pid == 0) { + return; + } + + for (proc = run_ctx->plist; proc != NULL; proc = proc->next) { + if (proc->pid == pid) { + break; + } + } + + if (proc == NULL) { + /* unknown process */ + goto again; + } + + /* Mark the process as terminated */ + proc->pid = -1; + + /* Update process status */ + if (WIFEXITED(status)) { + int pstatus = WEXITSTATUS(status); + if (WIFSIGNALED(status)) { + proc->result.sig = WTERMSIG(status); + } else if (pstatus >= 64 && pstatus < 255) { + proc->result.err = pstatus-64; + } else { + proc->result.status = pstatus; + } + } else if (WIFSIGNALED(status)) { + proc->result.sig = WTERMSIG(status); + } + + /* Confirm that all data has been read from the pipe */ + if (proc->fd != -1) { + proc_read_handler(ev, proc->fde, 0, proc); + TALLOC_FREE(proc->fde); + proc->fd = -1; + } + + DLIST_REMOVE(run_ctx->plist, proc); + + /* Active run_proc request */ + if (proc->req != NULL) { + run_proc_done(proc->req); + } else { + talloc_free(proc); + } + + goto again; +} + +static int run_proc_context_destructor(struct run_proc_context *run_ctx) +{ + struct proc_context *proc; + + /* Get rid of signal handler */ + TALLOC_FREE(run_ctx->se); + + /* Kill any pending processes */ + while ((proc = run_ctx->plist) != NULL) { + DLIST_REMOVE(run_ctx->plist, proc); + talloc_free(proc); + } + + return 0; +} + +struct run_proc_state { + struct tevent_context *ev; + struct run_proc_context *run_ctx; + struct proc_context *proc; + + struct run_proc_result result; + char *output; + pid_t pid; +}; + +static int run_proc_state_destructor(struct run_proc_state *state); +static void run_proc_timedout(struct tevent_req *subreq); + +struct tevent_req *run_proc_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct run_proc_context *run_ctx, + const char *path, const char **argv, + int stdin_fd, struct timeval timeout) +{ + struct tevent_req *req; + struct run_proc_state *state; + struct stat st; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct run_proc_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->run_ctx = run_ctx; + state->pid = -1; + + ret = stat(path, &st); + if (ret != 0) { + state->result.err = errno; + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + if (! (st.st_mode & S_IXUSR)) { + state->result.err = EACCES; + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + state->proc = proc_new(run_ctx, run_ctx); + if (tevent_req_nomem(state->proc, req)) { + return tevent_req_post(req, ev); + } + + state->proc->req = req; + DLIST_ADD(run_ctx->plist, state->proc); + + ret = proc_start(state->proc, ev, path, argv, stdin_fd); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + talloc_set_destructor(state, run_proc_state_destructor); + + if (! tevent_timeval_is_zero(&timeout)) { + struct tevent_req *subreq; + + subreq = tevent_wakeup_send(state, ev, timeout); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, run_proc_timedout, req); + } + + return req; +} + +static int run_proc_state_destructor(struct run_proc_state *state) +{ + /* Do not get rid of the child process if timeout has occurred */ + if ((state->proc != NULL) && (state->proc->req != NULL)) { + state->proc->req = NULL; + DLIST_REMOVE(state->run_ctx->plist, state->proc); + TALLOC_FREE(state->proc); + } + + return 0; +} + +static void run_proc_done(struct tevent_req *req) +{ + struct run_proc_state *state = tevent_req_data( + req, struct run_proc_state); + + state->proc->req = NULL; + + state->result = state->proc->result; + if (state->proc->output != NULL) { + state->output = talloc_move(state, &state->proc->output); + } + talloc_steal(state, state->proc); + + tevent_req_done(req); +} + +static void run_proc_kill(struct tevent_req *req) +{ + struct run_proc_state *state = tevent_req_data( + req, struct run_proc_state); + + state->proc->req = NULL; + state->proc = NULL; + + state->result.sig = SIGKILL; + + tevent_req_done(req); +} + +static void run_proc_timedout(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct run_proc_state *state = tevent_req_data( + req, struct run_proc_state); + bool status; + + state->proc->req = NULL; + + status = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, EIO); + return; + } + + state->result.err = ETIMEDOUT; + if (state->proc->output != NULL) { + state->output = talloc_move(state, &state->proc->output); + } + state->pid = state->proc->pid; + + tevent_req_done(req); +} + +bool run_proc_recv(struct tevent_req *req, int *perr, + struct run_proc_result *result, pid_t *pid, + TALLOC_CTX *mem_ctx, char **output) +{ + struct run_proc_state *state = tevent_req_data( + req, struct run_proc_state); + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (result != NULL) { + *result = state->result; + } + + if (pid != NULL) { + *pid = state->pid; + } + + if (output != NULL) { + *output = talloc_move(mem_ctx, &state->output); + } + + return true; +} diff --git a/ctdb/common/run_proc.h b/ctdb/common/run_proc.h new file mode 100644 index 0000000..7b06dad --- /dev/null +++ b/ctdb/common/run_proc.h @@ -0,0 +1,100 @@ +/* + Run a child process and collect the output + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_RUN_PROC_H__ +#define __CTDB_RUN_PROC_H__ + +#include +#include + +/** + * @file run_proc.h + * + * @brief Run a process and capture the output + * + * This abstraction allows one to execute scripts with argumunts. + */ + +/** + * @brief The run process context + */ +struct run_proc_context; + +/** + * @brief The exit status structure + * + * If the process is terminated due to a signal, sig is set. + * If the process is terminated due to an error, err is set. + * If the process terminates normally, status is set. + */ +struct run_proc_result { + int sig; + int err; + int status; +}; + +/** + * @brief Initialize the context for running processes + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[out] result New run_proc context + * @return 0 on success, errno on error + */ +int run_proc_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + struct run_proc_context **result); + +/** + * @brief Async computation start to run an executable + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] run_ctx Run_proc context + * @param[in] prog The path to the executable + * @param[in] argv Arguments to the executable + * @param[in] stdin_fd Assign stdin_fd as stdin for the process, -1 if not + * @param[in] timeout How long to wait for execution + * @return new tevent request, or NULL on failure + * + * argv must include program name as argv[0] and must be null terminated. + */ +struct tevent_req *run_proc_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct run_proc_context *run_ctx, + const char *prog, const char **argv, + int stdin_fd, struct timeval timeout); + +/** + * @brief Async computation end to run an executable + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[out] result The exit status of the executable + * @param[out] pid The pid of the child process (still running) + * @param[in] mem_ctx Talloc memory context + * @param[out] output The output from the executable (stdio + stderr) + * @return true on success, false on failure + * + * The returned pid is -1 if the process has terminated. + */ +bool run_proc_recv(struct tevent_req *req, int *perr, + struct run_proc_result *result, pid_t *pid, + TALLOC_CTX *mem_ctx, char **output); + +#endif /* __CTDB_RUN_PROC_H__ */ diff --git a/ctdb/common/sock_client.c b/ctdb/common/sock_client.c new file mode 100644 index 0000000..75f471f --- /dev/null +++ b/ctdb/common/sock_client.c @@ -0,0 +1,334 @@ +/* + A client based on unix domain socket + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/network.h" + +#include +#include + +#include "lib/util/debug.h" +#include "lib/util/time.h" +#include "lib/util/tevent_unix.h" + +#include "common/logging.h" +#include "common/reqid.h" +#include "common/comm.h" +#include "common/sock_client.h" + +struct sock_client_context { + struct sock_client_proto_funcs *funcs; + void *private_data; + + void (*disconnect_callback)(void *private_data); + void *disconnect_data; + + int fd; + struct comm_context *comm; + struct reqid_context *idr; +}; + +/* + * connect to a unix domain socket + */ + +static int socket_connect(const char *sockpath) +{ + struct sockaddr_un addr; + size_t len; + int fd, ret; + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + + len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path)); + if (len >= sizeof(addr.sun_path)) { + D_ERR("socket path too long: %s\n", sockpath); + return -1; + } + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) { + D_ERR("socket create failed - %s\n", sockpath); + return -1; + } + + ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); + if (ret != 0) { + D_ERR("socket connect failed - %s\n", sockpath); + close(fd); + return -1; + } + + return fd; +} + +/* + * Socket client + */ + +static int sock_client_context_destructor(struct sock_client_context *sockc); +static void sock_client_read_handler(uint8_t *buf, size_t buflen, + void *private_data); +static void sock_client_dead_handler(void *private_data); + +static void sock_client_msg_reply(struct sock_client_context *sockc, + uint8_t *buf, size_t buflen); + +int sock_client_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + const char *sockpath, + struct sock_client_proto_funcs *funcs, + void *private_data, + struct sock_client_context **result) +{ + struct sock_client_context *sockc; + int ret; + + if (sockpath == NULL) { + return EINVAL; + } + + if (funcs == NULL || funcs->request_push == NULL || + funcs->reply_pull == NULL || funcs->reply_reqid == NULL) { + return EINVAL; + } + + sockc = talloc_zero(mem_ctx, struct sock_client_context); + if (sockc == NULL) { + return ENOMEM; + } + + sockc->funcs = funcs; + sockc->private_data = private_data; + + sockc->fd = socket_connect(sockpath); + if (sockc->fd == -1) { + talloc_free(sockc); + return EIO; + } + + ret = comm_setup(sockc, ev, sockc->fd, + sock_client_read_handler, sockc, + sock_client_dead_handler, sockc, + &sockc->comm); + if (ret != 0) { + D_ERR("comm_setup() failed, ret=%d\n", ret); + close(sockc->fd); + talloc_free(sockc); + return ret; + } + + ret = reqid_init(sockc, INT_MAX-200, &sockc->idr); + if (ret != 0) { + D_ERR("reqid_init() failed, ret=%d\n", ret); + close(sockc->fd); + talloc_free(sockc); + return ret; + } + + talloc_set_destructor(sockc, sock_client_context_destructor); + + *result = sockc; + return 0; +} + +static int sock_client_context_destructor(struct sock_client_context *sockc) +{ + TALLOC_FREE(sockc->comm); + if (sockc->fd != -1) { + close(sockc->fd); + sockc->fd = -1; + } + return 0; +} + + +static void sock_client_read_handler(uint8_t *buf, size_t buflen, + void *private_data) +{ + struct sock_client_context *sockc = talloc_get_type_abort( + private_data, struct sock_client_context); + + sock_client_msg_reply(sockc, buf, buflen); +} + +static void sock_client_dead_handler(void *private_data) +{ + struct sock_client_context *sockc = talloc_get_type_abort( + private_data, struct sock_client_context); + + if (sockc->disconnect_callback != NULL) { + sockc->disconnect_callback(sockc->disconnect_data); + talloc_free(sockc); + return; + } + + D_NOTICE("connection to daemon closed, exiting\n"); + exit(1); +} + +void sock_client_set_disconnect_callback(struct sock_client_context *sockc, + sock_client_callback_func_t callback, + void *private_data) +{ + sockc->disconnect_callback = callback; + sockc->disconnect_data = private_data; +} + + +struct sock_client_msg_state { + struct sock_client_context *sockc; + uint32_t reqid; + struct tevent_req *req; + void *reply; +}; + +static int sock_client_msg_state_destructor( + struct sock_client_msg_state *state); +static void sock_client_msg_done(struct tevent_req *subreq); + +struct tevent_req *sock_client_msg_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_client_context *sockc, + struct timeval timeout, + void *request) +{ + struct tevent_req *req, *subreq; + struct sock_client_msg_state *state; + uint8_t *buf; + size_t buflen; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct sock_client_msg_state); + if (req == NULL) { + return NULL; + } + + state->sockc = sockc; + + state->reqid = reqid_new(sockc->idr, state); + if (state->reqid == REQID_INVALID) { + talloc_free(req); + return NULL; + } + + state->req = req; + + talloc_set_destructor(state, sock_client_msg_state_destructor); + + ret = sockc->funcs->request_push(request, state->reqid, state, + &buf, &buflen, sockc->private_data); + if (ret != 0) { + tevent_req_error(req, ret); + return tevent_req_post(req, ev); + } + + subreq = comm_write_send(state, ev, sockc->comm, buf, buflen); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, sock_client_msg_done, req); + + if (! timeval_is_zero(&timeout)) { + if (!tevent_req_set_endtime(req, ev, timeout)) { + return tevent_req_post(req, ev); + } + } + + return req; +} + +static int sock_client_msg_state_destructor( + struct sock_client_msg_state *state) +{ + reqid_remove(state->sockc->idr, state->reqid); + return 0; +} + +static void sock_client_msg_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + int ret; + bool status; + + status = comm_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, ret); + return; + } + + /* wait for the reply or timeout */ +} + +static void sock_client_msg_reply(struct sock_client_context *sockc, + uint8_t *buf, size_t buflen) +{ + struct sock_client_msg_state *state; + uint32_t reqid; + int ret; + + ret = sockc->funcs->reply_reqid(buf, buflen, &reqid, + sockc->private_data); + if (ret != 0) { + D_WARNING("Invalid packet received, ret=%d\n", ret); + return; + } + + state = reqid_find(sockc->idr, reqid, struct sock_client_msg_state); + if (state == NULL) { + return; + } + + if (reqid != state->reqid) { + return; + } + + ret = sockc->funcs->reply_pull(buf, buflen, state, &state->reply, + sockc->private_data); + if (ret != 0) { + tevent_req_error(state->req, ret); + return; + } + + tevent_req_done(state->req); +} + +bool sock_client_msg_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, void *reply) +{ + struct sock_client_msg_state *state = tevent_req_data( + req, struct sock_client_msg_state); + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (reply != NULL) { + *(void **)reply = talloc_steal(mem_ctx, state->reply); + } + + return true; +} diff --git a/ctdb/common/sock_client.h b/ctdb/common/sock_client.h new file mode 100644 index 0000000..d661c85 --- /dev/null +++ b/ctdb/common/sock_client.h @@ -0,0 +1,129 @@ +/* + A client based on unix domain socket + + Copyright (C) Amitay Isaacs 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SOCK_CLIENT_H__ +#define __CTDB_SOCK_CLIENT_H__ + +#include +#include + +/** + * @file sock_client.h + * + * @brief A framework for a client based on unix-domain sockets. + * + * This abstraction allows one to build clients that communicate using + * unix-domain sockets. It takes care of the common boilerplate. + */ + +/** + * @brief The abstract socket daemon context + */ +struct sock_client_context; + +/** + * @brief callback function + * + * This function can be registered to be called in case daemon goes away. + */ +typedef void (*sock_client_callback_func_t)(void *private_data); + +/** + * @brief Protocol marshalling functions + * + * The typical protocol packet will have a header and a payload. + * Header will contain at least 2 fields: length and reqid + * + * request_push() is called when the request packet needs to be marshalled + * + * reply_pull() is called to unmarshall data into a reply packet + * + * reply_reqid() is called to extract request id from a reply packet + */ +struct sock_client_proto_funcs { + int (*request_push)(void *request, uint32_t reqid, + TALLOC_CTX *mem_ctx, + uint8_t **buf, size_t *buflen, + void *private_data); + + int (*reply_pull)(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, void **reply, + void *private_data); + + int (*reply_reqid)(uint8_t *buf, size_t buflen, + uint32_t *reqid, void *private_data); +}; + +/** + * @brief Create a new socket client + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] sockpath Unix domain socket path + * @param[in] funcs Protocol marshalling functions + * @param[in] private_data Private data for protocol functions + * @param[out] result New socket client context + * @return 0 on success, errno on failure + */ +int sock_client_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, + const char *sockpath, + struct sock_client_proto_funcs *funcs, + void *private_data, + struct sock_client_context **result); + +/** + * @brief Register a callback in case of client disconnection + * + * @param[in] sockc Socket client context + * @param[in] callback Callback function + * @param[in] private_data Private data for callback function + */ +void sock_client_set_disconnect_callback(struct sock_client_context *sockc, + sock_client_callback_func_t callback, + void *private_data); + +/** + * @brief Async computation to send data to the daemon + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] sockc The socket client context + * @param[in] timeout How long to wait for + * @param[in] request Requeset packet to be sent + * @return new tevent request, or NULL on failure + */ +struct tevent_req *sock_client_msg_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_client_context *sockc, + struct timeval timeout, + void *request); + +/** + * @brief Async computation end to send data to the daemon + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @param[in] mem_ctx Talloc memory context + * @param[out] reply Reply received from server + * @return true on success, false on failure + */ +bool sock_client_msg_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, void *reply); + +#endif /* __CTDB_SOCK_CLIENT_H__ */ diff --git a/ctdb/common/sock_daemon.c b/ctdb/common/sock_daemon.c new file mode 100644 index 0000000..e31a364 --- /dev/null +++ b/ctdb/common/sock_daemon.c @@ -0,0 +1,1100 @@ +/* + A server based on unix domain socket + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/network.h" +#include "system/wait.h" + +#include +#include + +#include "lib/async_req/async_sock.h" +#include "lib/util/debug.h" +#include "lib/util/blocking.h" +#include "lib/util/dlinklist.h" +#include "lib/util/tevent_unix.h" +#include "lib/util/become_daemon.h" +#include "lib/util/sys_rw.h" + +#include "common/logging.h" +#include "common/reqid.h" +#include "common/comm.h" +#include "common/pidfile.h" +#include "common/system.h" +#include "common/sock_daemon.h" + +struct sock_socket { + struct sock_socket *prev, *next; + + const char *sockpath; + struct sock_socket_funcs *funcs; + void *private_data; + + int fd; + struct tevent_req *req; +}; + +struct sock_client { + struct sock_client *prev, *next; + + struct tevent_req *req; + struct sock_client_context *client_ctx; +}; + +struct sock_client_context { + struct tevent_context *ev; + struct sock_socket *sock; + int fd; + struct comm_context *comm; + + struct sock_client *client; +}; + +struct sock_daemon_context { + struct sock_daemon_funcs *funcs; + void *private_data; + + struct pidfile_context *pid_ctx; + struct sock_socket *socket_list; + int startup_fd; +}; + +/* + * Process a single client + */ + +static void sock_client_read_handler(uint8_t *buf, size_t buflen, + void *private_data); +static void sock_client_read_done(struct tevent_req *subreq); +static void sock_client_dead_handler(void *private_data); +static int sock_client_context_destructor( + struct sock_client_context *client_ctx); + +static int sock_client_context_init(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_socket *sock, + int client_fd, + struct sock_client *client, + struct sock_client_context **result) +{ + struct sock_client_context *client_ctx; + int ret; + + client_ctx = talloc_zero(mem_ctx, struct sock_client_context); + if (client_ctx == NULL) { + return ENOMEM; + } + + client_ctx->ev = ev; + client_ctx->sock = sock; + client_ctx->fd = client_fd; + client_ctx->client = client; + + ret = comm_setup(client_ctx, ev, client_fd, + sock_client_read_handler, client_ctx, + sock_client_dead_handler, client_ctx, + &client_ctx->comm); + if (ret != 0) { + talloc_free(client_ctx); + return ret; + } + + if (sock->funcs->connect != NULL) { + pid_t pid; + bool status; + + (void) ctdb_get_peer_pid(client_fd, &pid); + + status = sock->funcs->connect(client_ctx, + pid, + sock->private_data); + if (! status) { + talloc_free(client_ctx); + close(client_fd); + return 0; + } + } + + talloc_set_destructor(client_ctx, sock_client_context_destructor); + + *result = client_ctx; + return 0; +} + +static void sock_client_read_handler(uint8_t *buf, size_t buflen, + void *private_data) +{ + struct sock_client_context *client_ctx = talloc_get_type_abort( + private_data, struct sock_client_context); + struct sock_socket *sock = client_ctx->sock; + struct tevent_req *subreq; + + subreq = sock->funcs->read_send(client_ctx, client_ctx->ev, + client_ctx, buf, buflen, + sock->private_data); + if (subreq == NULL) { + talloc_free(client_ctx); + return; + } + tevent_req_set_callback(subreq, sock_client_read_done, client_ctx); +} + +static void sock_client_read_done(struct tevent_req *subreq) +{ + struct sock_client_context *client_ctx = tevent_req_callback_data( + subreq, struct sock_client_context); + struct sock_socket *sock = client_ctx->sock; + int ret; + bool status; + + status = sock->funcs->read_recv(subreq, &ret); + if (! status) { + D_ERR("client read failed with ret=%d\n", ret); + talloc_free(client_ctx); + } +} + +static void sock_client_dead_handler(void *private_data) +{ + struct sock_client_context *client_ctx = talloc_get_type_abort( + private_data, struct sock_client_context); + struct sock_socket *sock = client_ctx->sock; + + if (sock->funcs->disconnect != NULL) { + sock->funcs->disconnect(client_ctx, sock->private_data); + } + + talloc_free(client_ctx); +} + +static int sock_client_context_destructor( + struct sock_client_context *client_ctx) +{ + TALLOC_FREE(client_ctx->client); + TALLOC_FREE(client_ctx->comm); + if (client_ctx->fd != -1) { + close(client_ctx->fd); + client_ctx->fd = -1; + } + + return 0; +} + +/* + * Process a single listening socket + */ + +static int socket_setup(const char *sockpath, bool remove_before_use) +{ + struct sockaddr_un addr; + size_t len; + int ret, fd; + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + + len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path)); + if (len >= sizeof(addr.sun_path)) { + D_ERR("socket path too long: %s\n", sockpath); + return -1; + } + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) { + D_ERR("socket create failed - %s\n", sockpath); + return -1; + } + + ret = set_blocking(fd, false); + if (ret != 0) { + D_ERR("socket set nonblocking failed - %s\n", sockpath); + close(fd); + return -1; + } + + if (remove_before_use) { + unlink(sockpath); + } + + ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); + if (ret != 0) { + D_ERR("socket bind failed - %s\n", sockpath); + close(fd); + return -1; + } + + ret = listen(fd, 10); + if (ret != 0) { + D_ERR("socket listen failed - %s\n", sockpath); + close(fd); + return -1; + } + + D_NOTICE("listening on %s\n", sockpath); + + return fd; +} + +static int sock_socket_destructor(struct sock_socket *sock); + +static int sock_socket_init(TALLOC_CTX *mem_ctx, const char *sockpath, + struct sock_socket_funcs *funcs, + void *private_data, + struct sock_socket **result) +{ + struct sock_socket *sock; + + if (funcs == NULL) { + return EINVAL; + } + if (funcs->read_send == NULL || funcs->read_recv == NULL) { + return EINVAL; + } + + sock = talloc_zero(mem_ctx, struct sock_socket); + if (sock == NULL) { + return ENOMEM; + } + + sock->sockpath = talloc_strdup(sock, sockpath); + if (sock->sockpath == NULL) { + talloc_free(sock); + return ENOMEM; + } + sock->funcs = funcs; + sock->private_data = private_data; + sock->fd = -1; + + talloc_set_destructor(sock, sock_socket_destructor); + + *result = sock; + return 0; +} + +static int sock_socket_destructor(struct sock_socket *sock) +{ + TALLOC_FREE(sock->req); + + if (sock->fd != -1) { + close(sock->fd); + sock->fd = -1; + } + + unlink(sock->sockpath); + return 0; +} + + +struct sock_socket_start_state { + struct tevent_context *ev; + struct sock_socket *sock; + + struct sock_client *client_list; +}; + +static int sock_socket_start_state_destructor( + struct sock_socket_start_state *state); +static void sock_socket_start_new_client(struct tevent_req *subreq); +static int sock_socket_start_client_destructor(struct sock_client *client); + +static struct tevent_req *sock_socket_start_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_socket *sock, + bool remove_before_use) +{ + struct tevent_req *req, *subreq; + struct sock_socket_start_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct sock_socket_start_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->sock = sock; + + sock->fd = socket_setup(sock->sockpath, remove_before_use); + if (sock->fd == -1) { + tevent_req_error(req, EIO); + return tevent_req_post(req, ev); + } + + talloc_set_destructor(state, sock_socket_start_state_destructor); + + subreq = accept_send(state, ev, sock->fd); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, sock_socket_start_new_client, req); + + sock->req = req; + + return req; +} + +static int sock_socket_start_state_destructor( + struct sock_socket_start_state *state) +{ + struct sock_client *client; + + while ((client = state->client_list) != NULL) { + talloc_free(client); + } + + return 0; +} + +static void sock_socket_start_new_client(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_socket_start_state *state = tevent_req_data( + req, struct sock_socket_start_state); + struct sock_client *client; + int client_fd, ret; + + client_fd = accept_recv(subreq, NULL, NULL, &ret); + TALLOC_FREE(subreq); + if (client_fd == -1) { + D_ERR("failed to accept new connection\n"); + } + + subreq = accept_send(state, state->ev, state->sock->fd); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, sock_socket_start_new_client, req); + + if (client_fd == -1) { + return; + } + + client = talloc_zero(state, struct sock_client); + if (tevent_req_nomem(client, req)) { + close(client_fd); + return; + } + + client->req = req; + + ret = sock_client_context_init(client, state->ev, state->sock, + client_fd, client, &client->client_ctx); + if (ret != 0) { + talloc_free(client); + return; + } + + talloc_set_destructor(client, sock_socket_start_client_destructor); + DLIST_ADD(state->client_list, client); +} + +static int sock_socket_start_client_destructor(struct sock_client *client) +{ + struct sock_socket_start_state *state = tevent_req_data( + client->req, struct sock_socket_start_state); + + DLIST_REMOVE(state->client_list, client); + TALLOC_FREE(client->client_ctx); + + return 0; +} + +static bool sock_socket_start_recv(struct tevent_req *req, int *perr, + TALLOC_CTX *mem_ctx, const char **sockpath) +{ + struct sock_socket_start_state *state = tevent_req_data( + req, struct sock_socket_start_state); + int ret; + + state->sock->req = NULL; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + if (sockpath != NULL) { + *sockpath = talloc_steal(mem_ctx, state->sock->sockpath); + } + + return true; +} + +/* + * Send message to a client + */ + +struct tevent_req *sock_socket_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_client_context *client_ctx, + uint8_t *buf, size_t buflen) +{ + struct tevent_req *req; + + req = comm_write_send(mem_ctx, ev, client_ctx->comm, buf, buflen); + + return req; +} + +bool sock_socket_write_recv(struct tevent_req *req, int *perr) +{ + int ret; + bool status; + + status = comm_write_recv(req, &ret); + if (! status) { + if (perr != NULL) { + *perr = ret; + } + } + + return status; +} + +/* + * Socket daemon + */ + +int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name, + const char *logging, const char *debug_level, + struct sock_daemon_funcs *funcs, + void *private_data, + struct sock_daemon_context **out) +{ + struct sock_daemon_context *sockd; + int ret; + + sockd = talloc_zero(mem_ctx, struct sock_daemon_context); + if (sockd == NULL) { + return ENOMEM; + } + + sockd->funcs = funcs; + sockd->private_data = private_data; + sockd->startup_fd = -1; + + ret = logging_init(sockd, logging, debug_level, daemon_name); + if (ret != 0) { + fprintf(stderr, + "Failed to initialize logging, logging=%s, debug=%s\n", + logging, debug_level); + return ret; + } + + *out = sockd; + return 0; +} + +int sock_daemon_add_unix(struct sock_daemon_context *sockd, + const char *sockpath, + struct sock_socket_funcs *funcs, + void *private_data) +{ + struct sock_socket *sock; + int ret; + + ret = sock_socket_init(sockd, sockpath, funcs, private_data, &sock); + if (ret != 0) { + return ret; + } + + + DLIST_ADD(sockd->socket_list, sock); + return 0; +} + +bool sock_daemon_set_startup_fd(struct sock_daemon_context *sockd, int fd) +{ + if (! set_close_on_exec(fd)) { + D_ERR("Failed to set close-on-exec on startup fd\n"); + return false; + } + + sockd->startup_fd = fd; + return true; +} + +/* + * Run socket daemon + */ + +struct sock_daemon_run_state { + struct tevent_context *ev; + struct sock_daemon_context *sockd; + pid_t pid_watch; + + int fd; + int exit_code; +}; + +static void sock_daemon_run_started(struct tevent_req *subreq); +static void sock_daemon_run_startup_done(struct tevent_req *subreq); +static void sock_daemon_run_signal_handler(struct tevent_context *ev, + struct tevent_signal *se, + int signum, int count, void *siginfo, + void *private_data); +static void sock_daemon_run_reconfigure(struct tevent_req *req); +static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq); +static void sock_daemon_run_reopen_logs(struct tevent_req *req); +static void sock_daemon_run_reopen_logs_done(struct tevent_req *subreq); +static void sock_daemon_run_shutdown(struct tevent_req *req); +static void sock_daemon_run_shutdown_done(struct tevent_req *subreq); +static void sock_daemon_run_exit(struct tevent_req *req); +static bool sock_daemon_run_socket_listen(struct tevent_req *req); +static void sock_daemon_run_socket_fail(struct tevent_req *subreq); +static void sock_daemon_run_watch_pid(struct tevent_req *subreq); +static void sock_daemon_run_wait(struct tevent_req *req); +static void sock_daemon_run_wait_done(struct tevent_req *subreq); +static void sock_daemon_startup_notify(struct sock_daemon_context *sockd); + +struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_daemon_context *sockd, + const char *pidfile, + bool do_fork, bool create_session, + pid_t pid_watch) +{ + struct tevent_req *req, *subreq; + struct sock_daemon_run_state *state; + struct tevent_signal *se; + + req = tevent_req_create(mem_ctx, &state, + struct sock_daemon_run_state); + if (req == NULL) { + return NULL; + } + + become_daemon(do_fork, !create_session, false); + + if (pidfile != NULL) { + int ret = pidfile_context_create(sockd, pidfile, + &sockd->pid_ctx); + if (ret != 0) { + tevent_req_error(req, EEXIST); + return tevent_req_post(req, ev); + } + } + + state->ev = ev; + state->sockd = sockd; + state->pid_watch = pid_watch; + state->fd = -1; + + subreq = tevent_wakeup_send(state, ev, + tevent_timeval_current_ofs(0, 0)); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, sock_daemon_run_started, req); + + se = tevent_add_signal(ev, state, SIGHUP, 0, + sock_daemon_run_signal_handler, req); + if (tevent_req_nomem(se, req)) { + return tevent_req_post(req, ev); + } + + se = tevent_add_signal(ev, state, SIGUSR1, 0, + sock_daemon_run_signal_handler, req); + if (tevent_req_nomem(se, req)) { + return tevent_req_post(req, ev); + } + + se = tevent_add_signal(ev, state, SIGINT, 0, + sock_daemon_run_signal_handler, req); + if (tevent_req_nomem(se, req)) { + return tevent_req_post(req, ev); + } + + se = tevent_add_signal(ev, state, SIGTERM, 0, + sock_daemon_run_signal_handler, req); + if (tevent_req_nomem(se, req)) { + return tevent_req_post(req, ev); + } + + if (pid_watch > 1) { + subreq = tevent_wakeup_send(state, ev, + tevent_timeval_current_ofs(1,0)); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, sock_daemon_run_watch_pid, + req); + } + + return req; +} + +static void sock_daemon_run_started(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + bool status; + + status = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, EIO); + return; + } + + D_NOTICE("daemon started, pid=%u\n", getpid()); + + if (sockd->funcs != NULL && sockd->funcs->startup_send != NULL && + sockd->funcs->startup_recv != NULL) { + subreq = sockd->funcs->startup_send(state, state->ev, + sockd->private_data); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, sock_daemon_run_startup_done, + req); + return; + } + + if (sockd->funcs != NULL && sockd->funcs->startup != NULL) { + int ret; + + ret = sockd->funcs->startup(sockd->private_data); + if (ret != 0) { + D_ERR("startup failed, ret=%d\n", ret); + tevent_req_error(req, EIO); + return; + } + + D_NOTICE("startup completed successfully\n"); + } + + status = sock_daemon_run_socket_listen(req); + if (! status) { + return; + } + sock_daemon_run_wait(req); + + sock_daemon_startup_notify(sockd); +} + +static void sock_daemon_run_startup_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + int ret; + bool status; + + status = sockd->funcs->startup_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + D_ERR("startup failed, ret=%d\n", ret); + tevent_req_error(req, EIO); + return; + } + + D_NOTICE("startup completed successfully\n"); + + status = sock_daemon_run_socket_listen(req); + if (! status) { + return; + } + sock_daemon_run_wait(req); + + sock_daemon_startup_notify(sockd); +} + +static void sock_daemon_run_signal_handler(struct tevent_context *ev, + struct tevent_signal *se, + int signum, int count, void *siginfo, + void *private_data) +{ + struct tevent_req *req = talloc_get_type_abort( + private_data, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + + D_NOTICE("Received signal %d\n", signum); + + if (signum == SIGUSR1) { + sock_daemon_run_reconfigure(req); + return; + } + + if (signum == SIGHUP) { + sock_daemon_run_reopen_logs(req); + return; + } + + if (signum == SIGINT || signum == SIGTERM) { + state->exit_code = EINTR; + sock_daemon_run_shutdown(req); + } +} + +static void sock_daemon_run_reconfigure(struct tevent_req *req) +{ + struct tevent_req *subreq; + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + + if (sockd->funcs != NULL && sockd->funcs->reconfigure_send != NULL && + sockd->funcs->reconfigure_recv != NULL) { + subreq = sockd->funcs->reconfigure_send(state, state->ev, + sockd->private_data); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, + sock_daemon_run_reconfigure_done, req); + return; + } + + if (sockd->funcs != NULL && sockd->funcs->reconfigure != NULL) { + int ret; + + ret = sockd->funcs->reconfigure(sockd->private_data); + if (ret != 0) { + D_ERR("reconfigure failed, ret=%d\n", ret); + return; + } + + D_NOTICE("reconfigure completed successfully\n"); + } +} + +static void sock_daemon_run_reconfigure_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + int ret; + bool status; + + status = sockd->funcs->reconfigure_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + D_ERR("reconfigure failed, ret=%d\n", ret); + return; + } + + D_NOTICE("reconfigure completed successfully\n"); +} + +static void sock_daemon_run_reopen_logs(struct tevent_req *req) +{ + struct tevent_req *subreq; + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + + if (sockd->funcs != NULL && sockd->funcs->reopen_logs_send != NULL && + sockd->funcs->reopen_logs_recv != NULL) { + subreq = sockd->funcs->reopen_logs_send(state, state->ev, + sockd->private_data); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, + sock_daemon_run_reopen_logs_done, req); + return; + } + + if (sockd->funcs != NULL && sockd->funcs->reopen_logs != NULL) { + int ret; + + ret = sockd->funcs->reopen_logs(sockd->private_data); + if (ret != 0) { + D_ERR("reopen logs, ret=%d\n", ret); + return; + } + + D_NOTICE("reopen logs completed successfully\n"); + } +} + +static void sock_daemon_run_reopen_logs_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + int ret; + bool status; + + status = sockd->funcs->reopen_logs_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + D_ERR("reopen logs failed, ret=%d\n", ret); + return; + } + + D_NOTICE("reopen logs completed successfully\n"); +} + +static void sock_daemon_run_shutdown(struct tevent_req *req) +{ + struct tevent_req *subreq; + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + struct sock_socket *sock; + + D_NOTICE("Shutting down\n"); + + while ((sock = sockd->socket_list) != NULL) { + DLIST_REMOVE(sockd->socket_list, sock); + TALLOC_FREE(sock); + } + + if (sockd->funcs != NULL && sockd->funcs->shutdown_send != NULL && + sockd->funcs->shutdown_recv != NULL) { + subreq = sockd->funcs->shutdown_send(state, state->ev, + sockd->private_data); + if (subreq == NULL) { + sock_daemon_run_exit(req); + return; + } + tevent_req_set_callback(subreq, sock_daemon_run_shutdown_done, + req); + return; + } + + if (sockd->funcs != NULL && sockd->funcs->shutdown != NULL) { + sockd->funcs->shutdown(sockd->private_data); + } + + sock_daemon_run_exit(req); +} + +static void sock_daemon_run_shutdown_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + + sockd->funcs->shutdown_recv(subreq); + TALLOC_FREE(subreq); + + sock_daemon_run_exit(req); +} + +static void sock_daemon_run_exit(struct tevent_req *req) +{ + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + + TALLOC_FREE(sockd->pid_ctx); + + if (state->exit_code == 0) { + tevent_req_done(req); + } else { + tevent_req_error(req, state->exit_code); + } +} + +static bool sock_daemon_run_socket_listen(struct tevent_req *req) +{ + struct tevent_req *subreq; + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + struct sock_socket *sock; + bool remove_before_use = false; + + if (sockd->pid_ctx != NULL) { + remove_before_use = true; + } + for (sock = sockd->socket_list; sock != NULL; sock = sock->next) { + subreq = sock_socket_start_send(state, state->ev, sock, + remove_before_use); + if (tevent_req_nomem(subreq, req)) { + return false; + } + tevent_req_set_callback(subreq, sock_daemon_run_socket_fail, + req); + } + + return true; +} + +static void sock_daemon_run_socket_fail(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + const char *sockpath = "INVALID"; + int ret = 0; + bool status; + + status = sock_socket_start_recv(subreq, &ret, state, &sockpath); + TALLOC_FREE(subreq); + if (! status) { + D_ERR("socket %s closed unexpectedly\n", sockpath); + state->exit_code = ret; + } else { + state->exit_code = 0; + } + + sock_daemon_run_shutdown(req); +} + +static void sock_daemon_run_watch_pid(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + int ret; + bool status; + + status = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (! status) { + tevent_req_error(req, EIO); + return; + } + + ret = kill(state->pid_watch, 0); + if (ret == -1) { + if (errno == ESRCH) { + D_ERR("PID %d gone away, exiting\n", state->pid_watch); + state->exit_code = ESRCH; + sock_daemon_run_shutdown(req); + return; + } else { + D_ERR("Failed to check PID status %d, ret=%d\n", + state->pid_watch, errno); + } + } + + subreq = tevent_wakeup_send(state, state->ev, + tevent_timeval_current_ofs(5,0)); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, sock_daemon_run_watch_pid, req); +} + +static void sock_daemon_run_wait(struct tevent_req *req) +{ + struct tevent_req *subreq; + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + + if (sockd->funcs != NULL && sockd->funcs->wait_send != NULL && + sockd->funcs->wait_recv != NULL) { + subreq = sockd->funcs->wait_send(state, state->ev, + sockd->private_data); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, sock_daemon_run_wait_done, + req); + } +} + +static void sock_daemon_run_wait_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct sock_daemon_run_state *state = tevent_req_data( + req, struct sock_daemon_run_state); + struct sock_daemon_context *sockd = state->sockd; + int ret = 0; + bool status; + + status = sockd->funcs->wait_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (! status) { + state->exit_code = ret; + } else { + state->exit_code = 0; + } + + sock_daemon_run_shutdown(req); +} + +static void sock_daemon_startup_notify(struct sock_daemon_context *sockd) +{ + if (sockd->startup_fd != -1) { + unsigned int zero = 0; + ssize_t num; + + num = sys_write(sockd->startup_fd, &zero, sizeof(zero)); + if (num != sizeof(zero)) { + D_WARNING("Failed to write zero to pipe FD\n"); + } + } +} + +bool sock_daemon_run_recv(struct tevent_req *req, int *perr) +{ + int ret; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + return true; +} + +int sock_daemon_run(struct tevent_context *ev, + struct sock_daemon_context *sockd, + const char *pidfile, + bool do_fork, bool create_session, + pid_t pid_watch) +{ + struct tevent_req *req; + int ret; + bool status; + + req = sock_daemon_run_send(ev, ev, sockd, + pidfile, do_fork, create_session, pid_watch); + if (req == NULL) { + return ENOMEM; + } + + tevent_req_poll(req, ev); + + status = sock_daemon_run_recv(req, &ret); + TALLOC_FREE(req); + if (! status) { + return ret; + } + + return 0; +} diff --git a/ctdb/common/sock_daemon.h b/ctdb/common/sock_daemon.h new file mode 100644 index 0000000..7653d2c --- /dev/null +++ b/ctdb/common/sock_daemon.h @@ -0,0 +1,283 @@ +/* + A server based on unix domain socket + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SOCK_DAEMON_H__ +#define __CTDB_SOCK_DAEMON_H__ + +#include +#include + +#include "common/logging.h" + +/** + * @file sock_daemon.h + * + * @brief A framework for a server based on unix-domain sockets. + * + * This abstraction allows one to build simple servers that communicate using + * unix-domain sockets. It takes care of the common boilerplate. + */ + +/** + * @brief The abstract socket daemon context + */ +struct sock_daemon_context; + +/** + * @brief The abstract socket client context + */ +struct sock_client_context; + +/** + * @brief The callback routines called during daemon life cycle + * + * startup() is called when the daemon starts running + * either via sock_daemon_run() or via sock_daemon_run_send() + * startup() should return 0 for success, non-zero value on failure + * On failure, sock_daemon_run() will return error. + * + * startup_send()/startup_recv() is the async version of startup() + * + * reconfigure() is called when the daemon receives SIGUSR1 + * reconfigure() should return 0 for success, non-zero value on failure + * On failure, sock_daemon_run() will continue to run. + * + * reconfigure_send()/reconfigure_recv() is the async version of reconfigure() + * + * reopen_logs() is called when the daemon receives SIGHUP + * reopen_logs() should return 0 for success, non-zero value on failure + * On failure, sock_daemon_run() will continue to run. + * + * reopen_logs_send()/reopen_logs_recv() is the async version of reopen_logs() + * + * shutdown() is called when process receives SIGINT or SIGTERM or + * when wait computation has finished + * + * shutdown_send()/shutdown_recv() is the async version of shutdown() + * + * Please note that only one (sync or async) version of these functions + * will be called. If both versions are defined, then only async function + * will be called. + * + * wait_send() starts the async computation to keep running the daemon + * wait_recv() ends the async computation to keep running the daemon + * + * If wait_send()/wait_recv() is NULL, then daemon will keep running forever. + * If wait_send() returns req, then when req is over, daemon will shutdown. + */ +struct sock_daemon_funcs { + int (*startup)(void *private_data); + + struct tevent_req * (*startup_send)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + void *private_data); + bool (*startup_recv)(struct tevent_req *req, int *perr); + + int (*reconfigure)(void *private_data); + + struct tevent_req * (*reconfigure_send)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + void *private_data); + bool (*reconfigure_recv)(struct tevent_req *req, int *perr); + + int (*reopen_logs)(void *private_data); + + struct tevent_req * (*reopen_logs_send)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + void *private_data); + bool (*reopen_logs_recv)(struct tevent_req *req, int *perr); + + void (*shutdown)(void *private_data); + + struct tevent_req * (*shutdown_send)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + void *private_data); + void (*shutdown_recv)(struct tevent_req *req); + + struct tevent_req * (*wait_send)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + void *private_data); + bool (*wait_recv)(struct tevent_req *req, int *perr); +}; + +/** + * @brief The callback routines called for an unix-domain socket + * + * connect() is called when there is a new connection + * + * @param[in] client The new socket client context + * @param[in] pid The pid of the new client process, or -1 if unknown + * @param[in] private_data Private data set with the socket + * @return true if connection should be accepted, false otherwise + * + * + * disconnect() is called when client closes connection + * + * @param[in] client The socket client context + * @param[in] private_data Private data associated with the socket + * + * + * read_send() starts the async computation to process data on the socket + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client The socket client context + * @param[in] buf Data received from the client + * @param[in] buflen Length of the data + * @param[i] private_data Private data associatedwith the socket + * @return new tevent reques, or NULL on failure + * + * + * read_recv() ends the async computation to process data on the socket + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + * + */ +struct sock_socket_funcs { + bool (*connect)(struct sock_client_context *client, + pid_t pid, + void *private_data); + void (*disconnect)(struct sock_client_context *client, + void *private_data); + + struct tevent_req * (*read_send)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_client_context *client, + uint8_t *buf, size_t buflen, + void *private_data); + bool (*read_recv)(struct tevent_req *req, int *perr); +}; + +/** + * @brief Async computation to send data to the client + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] client The socket client context + * @param[in] buf Data to be sent to the client + * @param[in] buflen Length of the data + * @return new tevent request, or NULL on failure + */ +struct tevent_req *sock_socket_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_client_context *client, + uint8_t *buf, size_t buflen); + +/** + * @brief Async computation end to send data to client + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool sock_socket_write_recv(struct tevent_req *req, int *perr); + +/** + * @brief Create a new socket daemon + * + * @param[in] mem_ctx Talloc memory context + * @param[in] daemon_name Name of the daemon, used for logging + * @param[in] logging Logging setup string + * @param[in] debug_level Debug level to log at + * @param[in] funcs Socket daemon callback routines + * @param[in] private_data Private data associated with callback routines + * @param[out] result New socket daemon context + * @return 0 on success, errno on failure + */ +int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name, + const char *logging, const char *debug_level, + struct sock_daemon_funcs *funcs, + void *private_data, + struct sock_daemon_context **result); + +/** + * @brief Create and listen to the unix domain socket + * + * @param[in] sockd Socket daemon context + * @param[in] sockpath Unix domain socket path + * @param[in] funcs socket callback routines + * @param[in] private_data Private data associated with callback routines + * @return 0 on success, errno on failure + */ +int sock_daemon_add_unix(struct sock_daemon_context *sockd, + const char *sockpath, + struct sock_socket_funcs *funcs, + void *private_data); + +/** + * @brief Set file descriptor for indicating startup success + * + * On successful completion, 0 (unsigned int) will be written to the fd. + * + * @param[in] sockd Socket daemon context + * @param[in] fd File descriptor + * @return true on success, false on error + */ +bool sock_daemon_set_startup_fd(struct sock_daemon_context *sockd, int fd); + +/** + * @brief Async computation start to run a socket daemon + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] sockd The socket daemon context + * @param[in] pidfile PID file to create, NULL if no PID file required + * @param[in] do_fork Whether the daemon should fork on startup + * @param[in] create_session Whether the daemon should create a new session + * @param[in] pid_watch PID to watch. If PID goes away, shutdown. + * @return new tevent request, NULL on failure + */ +struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_daemon_context *sockd, + const char *pidfile, + bool do_fork, bool create_session, + pid_t pid_watch); + +/** + * @brief Async computation end to run a socket daemon + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool sock_daemon_run_recv(struct tevent_req *req, int *perr); + +/** + * @brief Sync way to start a daemon + * + * @param[in] ev Tevent context + * @param[in] sockd The socket daemon context + * @param[in] pidfile PID file to create, NULL if no PID file required + * @param[in] do_fork Whether the daemon should fork on startup + * @param[in] create_session Whether the daemon should create a new session + * @param[in] pid_watch PID to watch. If PID goes away, shutdown. + * @return 0 on success, errno on failure + * + * This call will return only on shutdown of the daemon + */ +int sock_daemon_run(struct tevent_context *ev, + struct sock_daemon_context *sockd, + const char *pidfile, + bool do_fork, bool create_session, + pid_t pid_watch); + +#endif /* __CTDB_SOCK_DAEMON_H__ */ diff --git a/ctdb/common/sock_io.c b/ctdb/common/sock_io.c new file mode 100644 index 0000000..81e82c5 --- /dev/null +++ b/ctdb/common/sock_io.c @@ -0,0 +1,328 @@ +/* + Generic Unix-domain Socket I/O + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/network.h" + +#include +#include + +#include "lib/util/sys_rw.h" +#include "lib/util/debug.h" +#include "lib/util/blocking.h" + +#include "common/logging.h" +#include "common/sock_io.h" + +bool sock_clean(const char *sockpath) +{ + int ret; + + ret = unlink(sockpath); + if (ret == 0) { + D_WARNING("Removed stale socket %s\n", sockpath); + } else if (errno != ENOENT) { + D_ERR("Failed to remove stale socket %s\n", sockpath); + return false; + } + + return true; +} + +int sock_connect(const char *sockpath) +{ + struct sockaddr_un addr; + size_t len; + int fd, ret; + + if (sockpath == NULL) { + D_ERR("Invalid socket path\n"); + return -1; + } + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path)); + if (len >= sizeof(addr.sun_path)) { + D_ERR("Socket path too long, len=%zu\n", strlen(sockpath)); + return -1; + } + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) { + D_ERR("socket() failed, errno=%d\n", errno); + return -1; + } + + ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); + if (ret == -1) { + D_ERR("connect() failed, errno=%d\n", errno); + close(fd); + return -1; + } + + return fd; +} + +struct sock_queue { + struct tevent_context *ev; + sock_queue_callback_fn_t callback; + void *private_data; + int fd; + + struct tevent_immediate *im; + struct tevent_queue *queue; + struct tevent_fd *fde; + uint8_t *buf; + size_t buflen, begin, end; +}; + +/* + * The reserved talloc headers, SOCK_QUEUE_OBJ_COUNT, + * and the pre-allocated pool-memory SOCK_QUEUE_POOL_SIZE, + * are used for the sub-objects queue->im, queue->queue, queue->fde + * and queue->buf. + * If the memory allocating sub-objects of struct sock_queue change, + * those values need to be adjusted. + */ +#define SOCK_QUEUE_OBJ_COUNT 4 +#define SOCK_QUEUE_POOL_SIZE 2048 + +static bool sock_queue_set_fd(struct sock_queue *queue, int fd); +static void sock_queue_handler(struct tevent_context *ev, + struct tevent_fd *fde, uint16_t flags, + void *private_data); +static void sock_queue_process(struct sock_queue *queue); +static void sock_queue_process_event(struct tevent_context *ev, + struct tevent_immediate *im, + void *private_data); + +struct sock_queue *sock_queue_setup(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, + sock_queue_callback_fn_t callback, + void *private_data) +{ + struct sock_queue *queue; + + queue = talloc_pooled_object(mem_ctx, struct sock_queue, + SOCK_QUEUE_OBJ_COUNT, SOCK_QUEUE_POOL_SIZE); + if (queue == NULL) { + return NULL; + } + memset(queue, 0, sizeof(struct sock_queue)); + + queue->ev = ev; + queue->callback = callback; + queue->private_data = private_data; + + queue->im = tevent_create_immediate(queue); + if (queue->im == NULL) { + talloc_free(queue); + return NULL; + } + + queue->queue = tevent_queue_create(queue, "out-queue"); + if (queue->queue == NULL) { + talloc_free(queue); + return NULL; + } + + if (! sock_queue_set_fd(queue, fd)) { + talloc_free(queue); + return NULL; + } + + return queue; +} + +static bool sock_queue_set_fd(struct sock_queue *queue, int fd) +{ + TALLOC_FREE(queue->fde); + queue->fd = fd; + + if (fd != -1) { + int ret; + + ret = set_blocking(fd, false); + if (ret != 0) { + return false; + } + + queue->fde = tevent_add_fd(queue->ev, queue, fd, + TEVENT_FD_READ, + sock_queue_handler, queue); + if (queue->fde == NULL) { + return false; + } + tevent_fd_set_auto_close(queue->fde); + } + + return true; +} + +static void sock_queue_handler(struct tevent_context *ev, + struct tevent_fd *fde, uint16_t flags, + void *private_data) +{ + struct sock_queue *queue = talloc_get_type_abort( + private_data, struct sock_queue); + int ret, num_ready; + ssize_t nread; + + ret = ioctl(queue->fd, FIONREAD, &num_ready); + if (ret != 0) { + /* Ignore */ + return; + } + + if (num_ready == 0) { + /* descriptor has been closed */ + goto fail; + } + + if ((size_t)num_ready > queue->buflen - queue->end) { + queue->buf = talloc_realloc_size(queue, queue->buf, + queue->end + num_ready); + if (queue->buf == NULL) { + goto fail; + } + queue->buflen = queue->end + num_ready; + } + + nread = sys_read(queue->fd, queue->buf + queue->end, num_ready); + if (nread < 0) { + goto fail; + } + queue->end += nread; + + sock_queue_process(queue); + return; + +fail: + queue->callback(NULL, 0, queue->private_data); +} + +static void sock_queue_process(struct sock_queue *queue) +{ + uint32_t pkt_size; + + if ((queue->end - queue->begin) < sizeof(uint32_t)) { + /* not enough data */ + return; + } + + pkt_size = *(uint32_t *)(queue->buf + queue->begin); + if (pkt_size == 0) { + D_ERR("Invalid packet of length 0\n"); + queue->callback(NULL, 0, queue->private_data); + return; + } + + if ((queue->end - queue->begin) < pkt_size) { + /* not enough data */ + return; + } + + queue->callback(queue->buf + queue->begin, pkt_size, + queue->private_data); + queue->begin += pkt_size; + + if (queue->begin < queue->end) { + /* more data to be processed */ + tevent_schedule_immediate(queue->im, queue->ev, + sock_queue_process_event, queue); + } else { + TALLOC_FREE(queue->buf); + queue->buflen = 0; + queue->begin = 0; + queue->end = 0; + } +} + +static void sock_queue_process_event(struct tevent_context *ev, + struct tevent_immediate *im, + void *private_data) +{ + struct sock_queue *queue = talloc_get_type_abort( + private_data, struct sock_queue); + + sock_queue_process(queue); +} + +struct sock_queue_write_state { + uint8_t *pkt; + uint32_t pkt_size; +}; + +static void sock_queue_trigger(struct tevent_req *req, void *private_data); + +int sock_queue_write(struct sock_queue *queue, uint8_t *buf, size_t buflen) +{ + struct tevent_req *req; + struct sock_queue_write_state *state; + struct tevent_queue_entry *qentry; + + if (buflen >= INT32_MAX) { + return -1; + } + + req = tevent_req_create(queue, &state, struct sock_queue_write_state); + if (req == NULL) { + return -1; + } + + state->pkt = buf; + state->pkt_size = (uint32_t)buflen; + + qentry = tevent_queue_add_entry(queue->queue, queue->ev, req, + sock_queue_trigger, queue); + if (qentry == NULL) { + talloc_free(req); + return -1; + } + + return 0; +} + +static void sock_queue_trigger(struct tevent_req *req, void *private_data) +{ + struct sock_queue *queue = talloc_get_type_abort( + private_data, struct sock_queue); + struct sock_queue_write_state *state = tevent_req_data( + req, struct sock_queue_write_state); + size_t offset = 0; + + do { + ssize_t nwritten; + + nwritten = sys_write(queue->fd, state->pkt + offset, + state->pkt_size - offset); + if (nwritten < 0) { + queue->callback(NULL, 0, queue->private_data); + return; + } + offset += nwritten; + + } while (offset < state->pkt_size); + + tevent_req_done(req); + talloc_free(req); +} diff --git a/ctdb/common/sock_io.h b/ctdb/common/sock_io.h new file mode 100644 index 0000000..8b6e4eb --- /dev/null +++ b/ctdb/common/sock_io.h @@ -0,0 +1,39 @@ +/* + Generic Socket I/O + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SOCK_IO_H__ +#define __CTDB_SOCK_IO_H__ + +typedef void (*sock_queue_callback_fn_t)(uint8_t *buf, size_t buflen, + void *private_data); + +struct sock_queue; + +bool sock_clean(const char *sockpath); +int sock_connect(const char *sockpath); + +struct sock_queue *sock_queue_setup(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, + sock_queue_callback_fn_t callback, + void *private_data); + +int sock_queue_write(struct sock_queue *queue, uint8_t *buf, size_t buflen); + +#endif /* __CTDB_SOCK_IO_H__ */ diff --git a/ctdb/common/srvid.c b/ctdb/common/srvid.c new file mode 100644 index 0000000..3304994 --- /dev/null +++ b/ctdb/common/srvid.c @@ -0,0 +1,280 @@ +/* + Message handler database based on srvid + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include + +#include "lib/util/dlinklist.h" +#include "common/db_hash.h" +#include "common/srvid.h" + +struct srvid_handler_list; + +struct srvid_context { + struct db_hash_context *dh; + struct srvid_handler_list *list; +}; + +struct srvid_handler { + struct srvid_handler *prev, *next; + struct srvid_handler_list *list; + srvid_handler_fn handler; + void *private_data; +}; + +struct srvid_handler_list { + struct srvid_handler_list *prev, *next; + struct srvid_context *srv; + uint64_t srvid; + struct srvid_handler *h; +}; + + +/* + * Initialise message srvid context and database + */ +int srvid_init(TALLOC_CTX *mem_ctx, struct srvid_context **result) +{ + struct srvid_context *srv; + int ret; + + srv = talloc_zero(mem_ctx, struct srvid_context); + if (srv == NULL) { + return ENOMEM; + } + + ret = db_hash_init(srv, "messagedb", 8192, DB_HASH_SIMPLE, &srv->dh); + if (ret != 0) { + talloc_free(srv); + return ret; + } + + *result = srv; + return 0; +} + +/* + * Wrapper functions to insert/delete/fetch srvid_hander_list + */ + +static int srvid_insert(struct srvid_context *srv, uint64_t srvid, + struct srvid_handler_list *list) +{ + return db_hash_insert(srv->dh, (uint8_t *)&srvid, sizeof(uint64_t), + (uint8_t *)&list, sizeof(list)); +} + +static int srvid_delete(struct srvid_context *srv, uint64_t srvid) +{ + return db_hash_delete(srv->dh, (uint8_t *)&srvid, sizeof(uint64_t)); +} + +static int srvid_fetch_parser(uint8_t *keybuf, size_t keylen, + uint8_t *databuf, size_t datalen, + void *private_data) +{ + struct srvid_handler_list **list = + (struct srvid_handler_list **)private_data; + + if (datalen != sizeof(*list)) { + return EIO; + } + + *list = *(struct srvid_handler_list **)databuf; + return 0; +} + +static int srvid_fetch(struct srvid_context *srv, uint64_t srvid, + struct srvid_handler_list **list) +{ + return db_hash_fetch(srv->dh, (uint8_t *)&srvid, sizeof(uint64_t), + srvid_fetch_parser, list); +} + +/* + * When a handler is freed, remove it from the list + */ +static int srvid_handler_destructor(struct srvid_handler *h) +{ + struct srvid_handler_list *list = h->list; + + DLIST_REMOVE(list->h, h); + if (list->h == NULL) { + talloc_free(list); + } + return 0; +} + +/* + * When a list is freed, remove all handlers and remove db entry + */ +static int srvid_handler_list_destructor(struct srvid_handler_list *list) +{ + struct srvid_handler *h; + + while (list->h != NULL) { + h = list->h; + DLIST_REMOVE(list->h, h); + TALLOC_FREE(h); + } + + srvid_delete(list->srv, list->srvid); + DLIST_REMOVE(list->srv->list, list); + return 0; +} + +/* + * Register a message handler + */ +int srvid_register(struct srvid_context *srv, TALLOC_CTX *mem_ctx, + uint64_t srvid, srvid_handler_fn handler, + void *private_data) +{ + struct srvid_handler_list *list; + struct srvid_handler *h; + int ret; + + if (srv == NULL) { + return EINVAL; + } + + h = talloc_zero(mem_ctx, struct srvid_handler); + if (h == NULL) { + return ENOMEM; + } + + h->handler = handler; + h->private_data = private_data; + + ret = srvid_fetch(srv, srvid, &list); + if (ret != 0) { + /* srvid not yet registered */ + list = talloc_zero(srv, struct srvid_handler_list); + if (list == NULL) { + talloc_free(h); + return ENOMEM; + } + + list->srv = srv; + list->srvid = srvid; + + ret = srvid_insert(srv, srvid, list); + if (ret != 0) { + talloc_free(h); + talloc_free(list); + return ret; + } + + DLIST_ADD(srv->list, list); + talloc_set_destructor(list, srvid_handler_list_destructor); + } + + h->list = list; + DLIST_ADD(list->h, h); + talloc_set_destructor(h, srvid_handler_destructor); + return 0; +} + +/* + * Deregister a message handler + */ +int srvid_deregister(struct srvid_context *srv, uint64_t srvid, + void *private_data) +{ + struct srvid_handler_list *list; + struct srvid_handler *h; + int ret; + + ret = srvid_fetch(srv, srvid, &list); + if (ret != 0) { + return ret; + } + + for (h = list->h; h != NULL; h = h->next) { + if (h->private_data == private_data) { + talloc_free(h); + return 0; + } + } + + return ENOENT; +} + +/* + * Check if a message handler exists + */ +int srvid_exists(struct srvid_context *srv, uint64_t srvid, void *private_data) +{ + struct srvid_handler_list *list; + struct srvid_handler *h; + int ret; + + ret = srvid_fetch(srv, srvid, &list); + if (ret != 0) { + return ret; + } + if (list->h == NULL) { + return ENOENT; + } + + if (private_data != NULL) { + for (h = list->h; h != NULL; h = h->next) { + if (h->private_data == private_data) { + return 0; + } + } + + return ENOENT; + } + + return 0; +} + +/* + * Send a message to registered srvid and srvid_all + */ +int srvid_dispatch(struct srvid_context *srv, uint64_t srvid, + uint64_t srvid_all, TDB_DATA data) +{ + struct srvid_handler_list *list; + struct srvid_handler *h; + int ret; + + ret = srvid_fetch(srv, srvid, &list); + if (ret == 0) { + for (h = list->h; h != NULL; h = h->next) { + h->handler(srvid, data, h->private_data); + } + } + + if (srvid_all == 0) { + return ret; + } + + ret = srvid_fetch(srv, srvid_all, &list); + if (ret == 0) { + for (h = list->h; h != NULL; h = h->next) { + h->handler(srvid, data, h->private_data); + } + } + + return ret; +} diff --git a/ctdb/common/srvid.h b/ctdb/common/srvid.h new file mode 100644 index 0000000..c0c2b30 --- /dev/null +++ b/ctdb/common/srvid.h @@ -0,0 +1,121 @@ +/* + Message handler database based on srvid + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SRVID_H__ +#define __CTDB_SRVID_H__ + +#include +#include + +/** + * @file srvid.h + * + * @brief Database of message handlers based on srvid + * + * CTDB can be used to send messages between clients across nodes using + * CTDB_REQ_MESSAGE. Clients register for messages based on srvid. CTDB itself + * uses a small set of srvid messages. A large range (2^56) of srvid messages + * is reserved for Samba. + */ + +/** + * @brief Message handler function + * + * To receive messages for a specific srvid, register a message handler function + * for the srvid. + */ +typedef void (*srvid_handler_fn)(uint64_t srvid, TDB_DATA data, + void *private_data); + +/** + * @brief Abstract struct to store srvid message handler database + */ +struct srvid_context; + +/** + * @brief Initialize srvid message handler database + * + * This returns a new srvid message handler database context. Freeing + * this context will free all the memory associated with the hash table. + * + * @param[in] mem_ctx Talloc memory context + * @param[out] result The new db_hash_context structure + * @return 0 on success, errno on failure + */ +int srvid_init(TALLOC_CTX *mem_ctx, struct srvid_context **result); + +/** + * @brief Register a message handler for a srvid + * + * The message handler is allocated using the specified talloc context. Freeing + * this talloc context, removes the message handler. + * + * @param[in] srv The srvid message handler database context + * @param[in] mem_ctx Talloc memory context for message handler + * @param[in] srvid The srvid + * @param[in] handler The message handler function for srvid + * @param[in] private_data Private data for message handler function + * @return 0 on success, errno on failure + */ +int srvid_register(struct srvid_context *srv, TALLOC_CTX *mem_ctx, + uint64_t srvid, srvid_handler_fn handler, + void *private_data); + +/** + * @brief Unregister a message handler for a srvid + * + * @param[in] srv The srvid message handler database context + * @param[in] srvid The srvid + * @param[in] private_data Private data of message handler function + * @return 0 on success, errno on failure + */ +int srvid_deregister(struct srvid_context *srv, uint64_t srvid, + void *private_data); + +/** + * @brief Check if any message handler is registered for srvid + * + * If private_data is NULL, then check if there is any registration + * for * specified srvid. If private_data is not NULL, then check for + * registration that matches the specified private data. + * + * @param[in] srv The srvid message handler database context + * @param[in] srvid The srvid + * @param[in] private_data Private data + * @return 0 on success, errno on failure + */ +int srvid_exists(struct srvid_context *srv, uint64_t srvid, + void *private_data); + +/** + * @brief Call message handlers for given srvid + * + * @param[in] srv The srvid message handler database context + * @param[in] srvid The srvid + * @param[in] srvid_all The srvid that gets all messages + * @param[in] data The data passed to each message handler + * @return 0 on success, errno on failure + * + * If srvid_all passed is 0, the message is not sent to message handlers + * registered with special srvid to receive all messages. + */ +int srvid_dispatch(struct srvid_context *srv, uint64_t srvid, + uint64_t srvid_all, TDB_DATA data); + +#endif /* __CTDB_SRVID_H__ */ diff --git a/ctdb/common/system.c b/ctdb/common/system.c new file mode 100644 index 0000000..08dc682 --- /dev/null +++ b/ctdb/common/system.c @@ -0,0 +1,237 @@ +/* + common system utilities + + Copyright (C) Amitay Isaacs 2014 + Copyright (C) Martin Schwenke 2014 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/shmem.h" +#include "system/network.h" + +#include +#include + +#include "lib/util/debug.h" + +#include "protocol/protocol.h" + +#include "common/logging.h" +#include "common/system.h" + +#ifdef HAVE_SCHED_H +#include +#endif + +#ifdef HAVE_PROCINFO_H +#include +#endif + +/* + if possible, make this task real time + */ +bool set_scheduler(void) +{ +#ifdef _AIX_ +#ifdef HAVE_THREAD_SETSCHED + struct thrdentry64 te; + tid64_t ti; + + ti = 0ULL; + if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) { + DEBUG(DEBUG_ERR, ("Unable to get thread information\n")); + return false; + } + + if (thread_setsched(te.ti_tid, 0, SCHED_RR) == -1) { + DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_RR (%s)\n", + strerror(errno))); + return false; + } else { + return true; + } +#endif +#else /* no AIX */ +#ifdef HAVE_SCHED_SETSCHEDULER + struct sched_param p; + + p.sched_priority = 1; + + if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) { + DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n", + strerror(errno))); + return false; + } else { + return true; + } +#endif +#endif + DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n")); + return false; +} + +/* + reset scheduler from real-time to normal scheduling + */ +void reset_scheduler(void) +{ +#ifdef _AIX_ +#ifdef HAVE_THREAD_SETSCHED + struct thrdentry64 te; + tid64_t ti; + + ti = 0ULL; + if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) { + DEBUG(DEBUG_ERR, ("Unable to get thread information\n")); + } + if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) { + DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n")); + } +#endif +#else /* no AIX */ +#ifdef HAVE_SCHED_SETSCHEDULER + struct sched_param p; + + p.sched_priority = 0; + if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) { + DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n")); + } +#endif +#endif +} + +/* we don't lock future pages here; it would increase the chance that + * we'd fail to mmap later on. */ +void lockdown_memory(bool valgrinding) +{ +#if defined(HAVE_MLOCKALL) && !defined(_AIX_) + /* Extra stack, please! */ + char dummy[10000]; + memset(dummy, 0, sizeof(dummy)); + + if (valgrinding) { + return; + } + + /* Ignore when running in local daemons mode */ + if (getuid() != 0) { + return; + } + + /* Avoid compiler optimizing out dummy. */ + mlock(dummy, sizeof(dummy)); + if (mlockall(MCL_CURRENT) != 0) { + DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n", + strerror(errno))); + } +#endif +} + +void ctdb_wait_for_process_to_exit(pid_t pid) +{ + while (kill(pid, 0) == 0 || errno != ESRCH) { + sleep(5); + } +} + +#ifdef HAVE_IF_NAMEINDEX + +bool ctdb_sys_check_iface_exists(const char *iface) +{ + struct if_nameindex *ifnis, *ifni; + bool found = false; + + ifnis = if_nameindex(); + if (ifnis == NULL) { + DBG_ERR("Failed to retrieve inteface list\n"); + return false; + } + + for (ifni = ifnis; + ifni->if_index != 0 || ifni->if_name != NULL; + ifni++) { + int cmp = strcmp(iface, ifni->if_name); + if (cmp == 0) { + found = true; + goto done; + } + } + +done: + if_freenameindex(ifnis); + + return found; +} + +#else /* HAVE_IF_NAMEINDEX */ + +bool ctdb_sys_check_iface_exists(const char *iface) +{ + /* Not implemented: Interface always considered present */ + return true; +} + +#endif /* HAVE_IF_NAMEINDEX */ + +#ifdef HAVE_PEERCRED + +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) +{ + struct ucred cr; + socklen_t crl = sizeof(struct ucred); + int ret; + + ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl); + if (ret == 0) { + *peer_pid = cr.pid; + } else { + *peer_pid = -1; + } + return ret; +} + +#else /* HAVE_PEERCRED */ + +#ifdef _AIX_ + +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) +{ + struct peercred_struct cr; + socklen_t crl = sizeof(struct peercred_struct); + int ret; + + ret = getsockopt(fd, SOL_SOCKET, SO_PEERID, &cr, &crl); + if (ret == 0) { + *peer_pid = cr.pid; + } else { + *peer_pid = -1; + } + return ret; +} + +#else /* _AIX_ */ + +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) +{ + /* Not implemented */ + *peer_pid = -1; + return ENOSYS; +} + +#endif /* _AIX_ */ + +#endif /* HAVE_PEERCRED */ diff --git a/ctdb/common/system.h b/ctdb/common/system.h new file mode 100644 index 0000000..042e7cc --- /dev/null +++ b/ctdb/common/system.h @@ -0,0 +1,37 @@ +/* + System specific code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SYSTEM_H__ +#define __CTDB_SYSTEM_H__ + +#include + +/* From system_util.c */ + +bool set_scheduler(void); +void reset_scheduler(void); + +void lockdown_memory(bool valgrinding); + +void ctdb_wait_for_process_to_exit(pid_t pid); + +bool ctdb_sys_check_iface_exists(const char *iface); +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid); + +#endif /* __CTDB_SYSTEM_H__ */ diff --git a/ctdb/common/system_socket.c b/ctdb/common/system_socket.c new file mode 100644 index 0000000..273b9c3 --- /dev/null +++ b/ctdb/common/system_socket.c @@ -0,0 +1,1168 @@ +/* + ctdb system specific code to manage raw sockets on linux + + Copyright (C) Ronnie Sahlberg 2007 + Copyright (C) Andrew Tridgell 2007 + Copyright (C) Marc Dequènes (Duck) 2009 + Copyright (C) Volker Lendecke 2012 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +/* + * Use BSD struct tcphdr field names for portability. Modern glibc + * makes them available by default via but older glibc + * requires __FAVOR_BSD to be defined. + * + * __FAVOR_BSD is normally defined in if _DEFAULT_SOURCE + * (new) or _BSD_SOURCE (now deprecated) is set and _GNU_SOURCE is not + * set. Including "replace.h" above causes to be + * indirectly included and this will not set __FAVOR_BSD because + * _GNU_SOURCE is set in Samba's "config.h" (which is included by + * "replace.h"). + * + * Therefore, set __FAVOR_BSD by hand below. + */ +#define __FAVOR_BSD 1 +#include "system/network.h" + +#ifdef HAVE_NETINET_IF_ETHER_H +#include +#endif +#ifdef HAVE_NETINET_IP6_H +#include +#endif +#ifdef HAVE_NETINET_ICMP6_H +#include +#endif +#ifdef HAVE_LINUX_IF_PACKET_H +#include +#endif + +#ifndef ETHERTYPE_IP6 +#define ETHERTYPE_IP6 0x86dd +#endif + +#include "lib/util/debug.h" +#include "lib/util/blocking.h" + +#include "protocol/protocol.h" + +#include "common/logging.h" +#include "common/system_socket.h" + +/* + uint16 checksum for n bytes + */ +static uint32_t uint16_checksum(uint8_t *data, size_t n) +{ + uint32_t sum=0; + uint16_t value; + + while (n>=2) { + memcpy(&value, data, 2); + sum += (uint32_t)ntohs(value); + data += 2; + n -= 2; + } + if (n == 1) { + sum += (uint32_t)ntohs(*data); + } + return sum; +} + +/* + * See if the given IP is currently on an interface + */ +bool ctdb_sys_have_ip(ctdb_sock_addr *_addr) +{ + int s; + int ret; + ctdb_sock_addr __addr = *_addr; + ctdb_sock_addr *addr = &__addr; + socklen_t addrlen = 0; + + switch (addr->sa.sa_family) { + case AF_INET: + addr->ip.sin_port = 0; + addrlen = sizeof(struct sockaddr_in); + break; + case AF_INET6: + addr->ip6.sin6_port = 0; + addrlen = sizeof(struct sockaddr_in6); + break; + } + + s = socket(addr->sa.sa_family, SOCK_STREAM, IPPROTO_TCP); + if (s == -1) { + return false; + } + + ret = bind(s, (struct sockaddr *)addr, addrlen); + + close(s); + return ret == 0; +} + +/* + * simple TCP checksum - assumes data is multiple of 2 bytes long + */ +static uint16_t ip_checksum(uint8_t *data, size_t n, struct ip *ip) +{ + uint32_t sum = uint16_checksum(data, n); + uint16_t sum2; + + sum += uint16_checksum((uint8_t *)&ip->ip_src, sizeof(ip->ip_src)); + sum += uint16_checksum((uint8_t *)&ip->ip_dst, sizeof(ip->ip_dst)); + sum += ip->ip_p + n; + sum = (sum & 0xFFFF) + (sum >> 16); + sum = (sum & 0xFFFF) + (sum >> 16); + sum2 = htons(sum); + sum2 = ~sum2; + if (sum2 == 0) { + return 0xFFFF; + } + return sum2; +} + +static uint16_t ip6_checksum(uint8_t *data, size_t n, struct ip6_hdr *ip6) +{ + uint16_t phdr[3]; + uint32_t sum = 0; + uint16_t sum2; + uint32_t len; + + sum += uint16_checksum((uint8_t *)&ip6->ip6_src, 16); + sum += uint16_checksum((uint8_t *)&ip6->ip6_dst, 16); + + len = htonl(n); + phdr[0] = len & UINT16_MAX; + phdr[1] = (len >> 16) & UINT16_MAX; + /* ip6_nxt is only 8 bits, so fits comfortably into a uint16_t */ + phdr[2] = htons(ip6->ip6_nxt); + sum += uint16_checksum((uint8_t *)phdr, sizeof(phdr)); + + sum += uint16_checksum(data, n); + + sum = (sum & 0xFFFF) + (sum >> 16); + sum = (sum & 0xFFFF) + (sum >> 16); + sum2 = htons(sum); + sum2 = ~sum2; + if (sum2 == 0) { + return 0xFFFF; + } + return sum2; +} + +/* + * Send gratuitous ARP request/reply or IPv6 neighbor advertisement + */ + +#ifdef HAVE_PACKETSOCKET + +/* + * Create IPv4 ARP requests/replies or IPv6 neighbour advertisement + * packets + */ + +#define ARP_STRUCT_SIZE sizeof(struct ether_header) + \ + sizeof(struct ether_arp) + +#define IP6_NA_STRUCT_SIZE sizeof(struct ether_header) + \ + sizeof(struct ip6_hdr) + \ + sizeof(struct nd_neighbor_advert) + \ + sizeof(struct nd_opt_hdr) + \ + sizeof(struct ether_addr) + +#define ARP_BUFFER_SIZE MAX(ARP_STRUCT_SIZE, 64) + +#define IP6_NA_BUFFER_SIZE MAX(IP6_NA_STRUCT_SIZE, 64) + +static int arp_build(uint8_t *buffer, + size_t buflen, + const struct sockaddr_in *addr, + const struct ether_addr *hwaddr, + bool reply, + struct ether_addr **ether_dhost, + size_t *len) +{ + size_t l = ARP_BUFFER_SIZE; + struct ether_header *eh; + struct ether_arp *ea; + struct arphdr *ah; + + if (addr->sin_family != AF_INET) { + return EINVAL; + } + + if (buflen < l) { + return EMSGSIZE; + } + + memset(buffer, 0 , l); + + eh = (struct ether_header *)buffer; + memset(eh->ether_dhost, 0xff, ETH_ALEN); + memcpy(eh->ether_shost, hwaddr, ETH_ALEN); + eh->ether_type = htons(ETHERTYPE_ARP); + + ea = (struct ether_arp *)(buffer + sizeof(struct ether_header)); + ah = &ea->ea_hdr; + ah->ar_hrd = htons(ARPHRD_ETHER); + ah->ar_pro = htons(ETH_P_IP); + ah->ar_hln = ETH_ALEN; + ah->ar_pln = sizeof(ea->arp_spa); + + if (! reply) { + ah->ar_op = htons(ARPOP_REQUEST); + memcpy(ea->arp_sha, hwaddr, ETH_ALEN); + memcpy(ea->arp_spa, &addr->sin_addr, sizeof(ea->arp_spa)); + memset(ea->arp_tha, 0, ETH_ALEN); + memcpy(ea->arp_tpa, &addr->sin_addr, sizeof(ea->arp_tpa)); + } else { + ah->ar_op = htons(ARPOP_REPLY); + memcpy(ea->arp_sha, hwaddr, ETH_ALEN); + memcpy(ea->arp_spa, &addr->sin_addr, sizeof(ea->arp_spa)); + memcpy(ea->arp_tha, hwaddr, ETH_ALEN); + memcpy(ea->arp_tpa, &addr->sin_addr, sizeof(ea->arp_tpa)); + } + + *ether_dhost = (struct ether_addr *)eh->ether_dhost; + *len = l; + return 0; +} + +static int ip6_na_build(uint8_t *buffer, + size_t buflen, + const struct sockaddr_in6 *addr, + const struct ether_addr *hwaddr, + struct ether_addr **ether_dhost, + size_t *len) +{ + size_t l = IP6_NA_BUFFER_SIZE; + struct ether_header *eh; + struct ip6_hdr *ip6; + struct nd_neighbor_advert *nd_na; + struct nd_opt_hdr *nd_oh; + struct ether_addr *ea; + int ret; + + if (addr->sin6_family != AF_INET6) { + return EINVAL; + } + + if (buflen < l) { + return EMSGSIZE; + } + + memset(buffer, 0 , l); + + eh = (struct ether_header *)buffer; + /* + * Ethernet multicast: 33:33:00:00:00:01 (see RFC2464, + * section 7) - note memset 0 above! + */ + eh->ether_dhost[0] = 0x33; + eh->ether_dhost[1] = 0x33; + eh->ether_dhost[5] = 0x01; + memcpy(eh->ether_shost, hwaddr, ETH_ALEN); + eh->ether_type = htons(ETHERTYPE_IP6); + + ip6 = (struct ip6_hdr *)(buffer + sizeof(struct ether_header)); + ip6->ip6_vfc = 6 << 4; + ip6->ip6_plen = htons(sizeof(struct nd_neighbor_advert) + + sizeof(struct nd_opt_hdr) + + ETH_ALEN); + ip6->ip6_nxt = IPPROTO_ICMPV6; + ip6->ip6_hlim = 255; + ip6->ip6_src = addr->sin6_addr; + /* all-nodes multicast */ + + ret = inet_pton(AF_INET6, "ff02::1", &ip6->ip6_dst); + if (ret != 1) { + return EIO; + } + + nd_na = (struct nd_neighbor_advert *)(buffer + + sizeof(struct ether_header) + + sizeof(struct ip6_hdr)); + nd_na->nd_na_type = ND_NEIGHBOR_ADVERT; + nd_na->nd_na_code = 0; + nd_na->nd_na_flags_reserved = ND_NA_FLAG_OVERRIDE; + nd_na->nd_na_target = addr->sin6_addr; + + /* Option: Target link-layer address */ + nd_oh = (struct nd_opt_hdr *)(buffer + + sizeof(struct ether_header) + + sizeof(struct ip6_hdr) + + sizeof(struct nd_neighbor_advert)); + nd_oh->nd_opt_type = ND_OPT_TARGET_LINKADDR; + nd_oh->nd_opt_len = 1; /* multiple of 8 octets */ + + ea = (struct ether_addr *)(buffer + + sizeof(struct ether_header) + + sizeof(struct ip6_hdr) + + sizeof(struct nd_neighbor_advert) + + sizeof(struct nd_opt_hdr)); + memcpy(ea, hwaddr, ETH_ALEN); + + nd_na->nd_na_cksum = ip6_checksum((uint8_t *)nd_na, + ntohs(ip6->ip6_plen), + ip6); + + *ether_dhost = (struct ether_addr *)eh->ether_dhost; + *len = l; + return 0; +} + +int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface) +{ + int s; + struct sockaddr_ll sall = {0}; + struct ifreq if_hwaddr = { + .ifr_ifru = { + .ifru_flags = 0 + }, + }; + uint8_t buffer[MAX(ARP_BUFFER_SIZE, IP6_NA_BUFFER_SIZE)]; + struct ifreq ifr = { + .ifr_ifru = { + .ifru_flags = 0 + }, + }; + struct ether_addr *hwaddr = NULL; + struct ether_addr *ether_dhost = NULL; + size_t len = 0; + int ret = 0; + + s = socket(AF_PACKET, SOCK_RAW, 0); + if (s == -1) { + ret = errno; + DBG_ERR("Failed to open raw socket\n"); + return ret; + } + DBG_DEBUG("Created SOCKET FD:%d for sending arp\n", s); + + /* Find interface */ + strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { + ret = errno; + DBG_ERR("Interface '%s' not found\n", iface); + goto fail; + } + + /* Get MAC address */ + strlcpy(if_hwaddr.ifr_name, iface, sizeof(if_hwaddr.ifr_name)); + ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr); + if ( ret < 0 ) { + ret = errno; + DBG_ERR("ioctl failed\n"); + goto fail; + } + if (ARPHRD_LOOPBACK == if_hwaddr.ifr_hwaddr.sa_family) { + ret = 0; + D_DEBUG("Ignoring loopback arp request\n"); + goto fail; + } + if (if_hwaddr.ifr_hwaddr.sa_family != ARPHRD_ETHER) { + ret = EINVAL; + DBG_ERR("Not an ethernet address family (0x%x)\n", + if_hwaddr.ifr_hwaddr.sa_family); + goto fail;; + } + + /* Set up most of destination address structure */ + sall.sll_family = AF_PACKET; + sall.sll_halen = sizeof(struct ether_addr); + sall.sll_protocol = htons(ETH_P_ALL); + sall.sll_ifindex = ifr.ifr_ifindex; + + /* For clarity */ + hwaddr = (struct ether_addr *)if_hwaddr.ifr_hwaddr.sa_data; + + switch (addr->ip.sin_family) { + case AF_INET: + /* Send gratuitous ARP */ + ret = arp_build(buffer, + sizeof(buffer), + &addr->ip, + hwaddr, + false, + ðer_dhost, + &len); + if (ret != 0) { + DBG_ERR("Failed to build ARP request\n"); + goto fail; + } + + memcpy(&sall.sll_addr[0], ether_dhost, sall.sll_halen); + + ret = sendto(s, + buffer, + len, + 0, + (struct sockaddr *)&sall, + sizeof(sall)); + if (ret < 0 ) { + ret = errno; + DBG_ERR("Failed sendto\n"); + goto fail; + } + + /* Send unsolicited ARP reply */ + ret = arp_build(buffer, + sizeof(buffer), + &addr->ip, + hwaddr, + true, + ðer_dhost, + &len); + if (ret != 0) { + DBG_ERR("Failed to build ARP reply\n"); + goto fail; + } + + memcpy(&sall.sll_addr[0], ether_dhost, sall.sll_halen); + + ret = sendto(s, + buffer, + len, + 0, + (struct sockaddr *)&sall, + sizeof(sall)); + if (ret < 0 ) { + ret = errno; + DBG_ERR("Failed sendto\n"); + goto fail; + } + + close(s); + break; + + case AF_INET6: + ret = ip6_na_build(buffer, + sizeof(buffer), + &addr->ip6, + hwaddr, + ðer_dhost, + &len); + if (ret != 0) { + DBG_ERR("Failed to build IPv6 neighbor advertisement\n"); + goto fail; + } + + memcpy(&sall.sll_addr[0], ether_dhost, sall.sll_halen); + + ret = sendto(s, + buffer, + len, + 0, + (struct sockaddr *)&sall, + sizeof(sall)); + if (ret < 0 ) { + ret = errno; + DBG_ERR("Failed sendto\n"); + goto fail; + } + + close(s); + break; + + default: + ret = EINVAL; + DBG_ERR("Not an ipv4/ipv6 address (family is %u)\n", + addr->ip.sin_family); + goto fail; + } + + return 0; + +fail: + close(s); + return ret; +} + +#else /* HAVE_PACKETSOCKET */ + +int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface) +{ + /* Not implemented */ + return ENOSYS; +} + +#endif /* HAVE_PACKETSOCKET */ + + +#define IP4_TCP_BUFFER_SIZE sizeof(struct ip) + \ + sizeof(struct tcphdr) + +#define IP6_TCP_BUFFER_SIZE sizeof(struct ip6_hdr) + \ + sizeof(struct tcphdr) + +static int tcp4_build(uint8_t *buf, + size_t buflen, + const struct sockaddr_in *src, + const struct sockaddr_in *dst, + uint32_t seq, + uint32_t ack, + int rst, + size_t *len) +{ + size_t l = IP4_TCP_BUFFER_SIZE; + struct { + struct ip ip; + struct tcphdr tcp; + } *ip4pkt; + + if (l != sizeof(*ip4pkt)) { + return EMSGSIZE; + } + + if (buflen < l) { + return EMSGSIZE; + } + + ip4pkt = (void *)buf; + memset(ip4pkt, 0, l); + + ip4pkt->ip.ip_v = 4; + ip4pkt->ip.ip_hl = sizeof(ip4pkt->ip)/sizeof(uint32_t); + ip4pkt->ip.ip_len = htons(sizeof(ip4pkt)); + ip4pkt->ip.ip_ttl = 255; + ip4pkt->ip.ip_p = IPPROTO_TCP; + ip4pkt->ip.ip_src.s_addr = src->sin_addr.s_addr; + ip4pkt->ip.ip_dst.s_addr = dst->sin_addr.s_addr; + ip4pkt->ip.ip_sum = 0; + + ip4pkt->tcp.th_sport = src->sin_port; + ip4pkt->tcp.th_dport = dst->sin_port; + ip4pkt->tcp.th_seq = seq; + ip4pkt->tcp.th_ack = ack; + ip4pkt->tcp.th_flags = 0; + ip4pkt->tcp.th_flags |= TH_ACK; + if (rst) { + ip4pkt->tcp.th_flags |= TH_RST; + } + ip4pkt->tcp.th_off = sizeof(ip4pkt->tcp)/sizeof(uint32_t); + /* this makes it easier to spot in a sniffer */ + ip4pkt->tcp.th_win = htons(1234); + ip4pkt->tcp.th_sum = ip_checksum((uint8_t *)&ip4pkt->tcp, + sizeof(ip4pkt->tcp), + &ip4pkt->ip); + + *len = l; + return 0; +} + +static int tcp6_build(uint8_t *buf, + size_t buflen, + const struct sockaddr_in6 *src, + const struct sockaddr_in6 *dst, + uint32_t seq, + uint32_t ack, + int rst, + size_t *len) +{ + size_t l = IP6_TCP_BUFFER_SIZE; + struct { + struct ip6_hdr ip6; + struct tcphdr tcp; + } *ip6pkt; + + if (l != sizeof(*ip6pkt)) { + return EMSGSIZE; + } + + if (buflen < l) { + return EMSGSIZE; + } + + ip6pkt = (void *)buf; + memset(ip6pkt, 0, l); + + ip6pkt->ip6.ip6_vfc = 6 << 4; + ip6pkt->ip6.ip6_plen = htons(sizeof(struct tcphdr)); + ip6pkt->ip6.ip6_nxt = IPPROTO_TCP; + ip6pkt->ip6.ip6_hlim = 64; + ip6pkt->ip6.ip6_src = src->sin6_addr; + ip6pkt->ip6.ip6_dst = dst->sin6_addr; + + ip6pkt->tcp.th_sport = src->sin6_port; + ip6pkt->tcp.th_dport = dst->sin6_port; + ip6pkt->tcp.th_seq = seq; + ip6pkt->tcp.th_ack = ack; + ip6pkt->tcp.th_flags = 0; + ip6pkt->tcp.th_flags |= TH_ACK; + if (rst) { + ip6pkt->tcp.th_flags |= TH_RST; + } + ip6pkt->tcp.th_off = sizeof(ip6pkt->tcp)/sizeof(uint32_t); + /* this makes it easier to spot in a sniffer */ + ip6pkt->tcp.th_win = htons(1234); + ip6pkt->tcp.th_sum = ip6_checksum((uint8_t *)&ip6pkt->tcp, + sizeof(ip6pkt->tcp), + &ip6pkt->ip6); + + *len = l; + return 0; +} + +/* + * Send tcp segment from the specified IP/port to the specified + * destination IP/port. + * + * This is used to trigger the receiving host into sending its own ACK, + * which should trigger early detection of TCP reset by the client + * after IP takeover + * + * This can also be used to send RST segments (if rst is true) and also + * if correct seq and ack numbers are provided. + */ +int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, + const ctdb_sock_addr *src, + uint32_t seq, + uint32_t ack, + int rst) +{ + uint8_t buf[MAX(IP4_TCP_BUFFER_SIZE, IP6_TCP_BUFFER_SIZE)]; + size_t len = 0; + int ret; + int s; + uint32_t one = 1; + struct sockaddr_in6 tmpdest = { 0 }; + int saved_errno; + + switch (src->ip.sin_family) { + case AF_INET: + ret = tcp4_build(buf, + sizeof(buf), + &src->ip, + &dest->ip, + seq, + ack, + rst, + &len); + if (ret != 0) { + DBG_ERR("Failed to build TCP packet (%d)\n", ret); + return ret; + } + + /* open a raw socket to send this segment from */ + s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + if (s == -1) { + DBG_ERR("Failed to open raw socket (%s)\n", + strerror(errno)); + return -1; + } + + ret = setsockopt(s, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)); + if (ret != 0) { + DBG_ERR("Failed to setup IP headers (%s)\n", + strerror(errno)); + close(s); + return -1; + } + + ret = sendto(s, + buf, + len, + 0, + (const struct sockaddr *)&dest->ip, + sizeof(dest->ip)); + saved_errno = errno; + close(s); + if (ret == -1) { + D_ERR("Failed sendto (%s)\n", strerror(saved_errno)); + return -1; + } + if ((size_t)ret != len) { + DBG_ERR("Failed sendto - didn't send full packet\n"); + return -1; + } + break; + + case AF_INET6: + ret = tcp6_build(buf, + sizeof(buf), + &src->ip6, + &dest->ip6, + seq, + ack, + rst, + &len); + if (ret != 0) { + DBG_ERR("Failed to build TCP packet (%d)\n", ret); + return ret; + } + + s = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW); + if (s == -1) { + DBG_ERR("Failed to open sending socket\n"); + return -1; + + } + /* + * sendto() on an IPv6 raw socket requires the port to + * be either 0 or a protocol value + */ + tmpdest = dest->ip6; + tmpdest.sin6_port = 0; + + ret = sendto(s, + buf, + len, + 0, + (const struct sockaddr *)&tmpdest, + sizeof(tmpdest)); + saved_errno = errno; + close(s); + if (ret == -1) { + D_ERR("Failed sendto (%s)\n", strerror(saved_errno)); + return -1; + } + if ((size_t)ret != len) { + DBG_ERR("Failed sendto - didn't send full packet\n"); + return -1; + } + break; + + default: + DBG_ERR("Not an ipv4/v6 address\n"); + return -1; + } + + return 0; +} + +static int tcp4_extract(const uint8_t *ip_pkt, + size_t pktlen, + struct sockaddr_in *src, + struct sockaddr_in *dst, + uint32_t *ack_seq, + uint32_t *seq, + int *rst, + uint16_t *window) +{ + const struct ip *ip; + const struct tcphdr *tcp; + + if (pktlen < sizeof(struct ip)) { + return EMSGSIZE; + } + + ip = (const struct ip *)ip_pkt; + + /* IPv4 only */ + if (ip->ip_v != 4) { + return ENOMSG; + } + /* Don't look at fragments */ + if ((ntohs(ip->ip_off)&0x1fff) != 0) { + return ENOMSG; + } + /* TCP only */ + if (ip->ip_p != IPPROTO_TCP) { + return ENOMSG; + } + + /* Ensure there is enough of the packet to gather required fields */ + if (pktlen < + (ip->ip_hl * sizeof(uint32_t)) + offsetof(struct tcphdr, th_sum)) { + return EMSGSIZE; + } + + tcp = (const struct tcphdr *)(ip_pkt + (ip->ip_hl * sizeof(uint32_t))); + + src->sin_family = AF_INET; + src->sin_addr.s_addr = ip->ip_src.s_addr; + src->sin_port = tcp->th_sport; + + dst->sin_family = AF_INET; + dst->sin_addr.s_addr = ip->ip_dst.s_addr; + dst->sin_port = tcp->th_dport; + + *ack_seq = tcp->th_ack; + *seq = tcp->th_seq; + if (window != NULL) { + *window = tcp->th_win; + } + if (rst != NULL) { + *rst = tcp->th_flags & TH_RST; + } + + return 0; +} + +static int tcp6_extract(const uint8_t *ip_pkt, + size_t pktlen, + struct sockaddr_in6 *src, + struct sockaddr_in6 *dst, + uint32_t *ack_seq, + uint32_t *seq, + int *rst, + uint16_t *window) +{ + const struct ip6_hdr *ip6; + const struct tcphdr *tcp; + + /* Ensure there is enough of the packet to gather required fields */ + if (pktlen < sizeof(struct ip6_hdr) + offsetof(struct tcphdr, th_sum)) { + return EMSGSIZE; + } + + ip6 = (const struct ip6_hdr *)ip_pkt; + + /* IPv6 only */ + if ((ip6->ip6_vfc >> 4) != 6){ + return ENOMSG; + } + + /* TCP only */ + if (ip6->ip6_nxt != IPPROTO_TCP) { + return ENOMSG; + } + + tcp = (const struct tcphdr *)(ip_pkt + sizeof(struct ip6_hdr)); + + src->sin6_family = AF_INET6; + src->sin6_port = tcp->th_sport; + src->sin6_addr = ip6->ip6_src; + + dst->sin6_family = AF_INET6; + dst->sin6_port = tcp->th_dport; + dst->sin6_addr = ip6->ip6_dst; + + *ack_seq = tcp->th_ack; + *seq = tcp->th_seq; + if (window != NULL) { + *window = tcp->th_win; + } + if (rst != NULL) { + *rst = tcp->th_flags & TH_RST; + } + + return 0; +} + +/* + * Packet capture + * + * If AF_PACKET is available then use a raw socket otherwise use pcap. + * wscript has checked to make sure that pcap is available if needed. + */ + +#if defined(HAVE_AF_PACKET) && !defined(ENABLE_PCAP) + +/* + * This function is used to open a raw socket to capture from + */ +int ctdb_sys_open_capture_socket(const char *iface, void **private_data) +{ + int s, ret; + + /* Open a socket to capture all traffic */ + s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); + if (s == -1) { + DBG_ERR("Failed to open raw socket\n"); + return -1; + } + + DBG_DEBUG("Opened raw socket for TCP tickle capture (fd=%d)\n", s); + + ret = set_blocking(s, false); + if (ret != 0) { + DBG_ERR("Failed to set socket non-blocking (%s)\n", + strerror(errno)); + close(s); + return -1; + } + + set_close_on_exec(s); + + return s; +} + +/* + * This function is used to do any additional cleanup required when closing + * a capture socket. + * Note that the socket itself is closed automatically in the caller. + */ +int ctdb_sys_close_capture_socket(void *private_data) +{ + return 0; +} + + +/* + * called when the raw socket becomes readable + */ +int ctdb_sys_read_tcp_packet(int s, void *private_data, + ctdb_sock_addr *src, + ctdb_sock_addr *dst, + uint32_t *ack_seq, + uint32_t *seq, + int *rst, + uint16_t *window) +{ + ssize_t nread; + uint8_t pkt[100]; /* Large enough for simple ACK/RST packets */ + struct ether_header *eth; + int ret; + + nread = recv(s, pkt, sizeof(pkt), MSG_TRUNC); + if (nread == -1) { + return errno; + } + if ((size_t)nread < sizeof(*eth)) { + return EMSGSIZE; + } + + ZERO_STRUCTP(src); + ZERO_STRUCTP(dst); + + /* Ethernet */ + eth = (struct ether_header *)pkt; + + /* we want either IPv4 or IPv6 */ + if (ntohs(eth->ether_type) == ETHERTYPE_IP) { + ret = tcp4_extract(pkt + sizeof(struct ether_header), + (size_t)nread - sizeof(struct ether_header), + &src->ip, + &dst->ip, + ack_seq, + seq, + rst, + window); + return ret; + + } else if (ntohs(eth->ether_type) == ETHERTYPE_IP6) { + ret = tcp6_extract(pkt + sizeof(struct ether_header), + (size_t)nread - sizeof(struct ether_header), + &src->ip6, + &dst->ip6, + ack_seq, + seq, + rst, + window); + return ret; + } + + return ENOMSG; +} + +#else /* defined(HAVE_AF_PACKET) && !defined(ENABLE_PCAP) */ + +#include + +/* + * Assume this exists if pcap.h exists - it has been around for a + * while + */ +#include + +int ctdb_sys_open_capture_socket(const char *iface, void **private_data) +{ + char errbuf[PCAP_ERRBUF_SIZE]; + pcap_t *pt; + int pcap_packet_type; + const char *t = NULL; + int fd; + int ret; + + pt = pcap_create(iface, errbuf); + if (pt == NULL) { + DBG_ERR("Failed to open pcap capture device %s (%s)\n", + iface, + errbuf); + return -1; + } + /* + * pcap isn't very clear about defaults... + */ + ret = pcap_set_snaplen(pt, 100); + if (ret < 0) { + DBG_ERR("Failed to set snaplen for pcap capture\n"); + goto fail; + } + ret = pcap_set_promisc(pt, 0); + if (ret < 0) { + DBG_ERR("Failed to unset promiscuous mode for pcap capture\n"); + goto fail; + } + ret = pcap_set_timeout(pt, 0); + if (ret < 0) { + DBG_ERR("Failed to set timeout for pcap capture\n"); + goto fail; + } +#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE + ret = pcap_set_immediate_mode(pt, 1); + if (ret < 0) { + DBG_ERR("Failed to set immediate mode for pcap capture\n"); + goto fail; + } +#endif + ret = pcap_activate(pt); + if (ret < 0) { + DBG_ERR("Failed to activate pcap capture\n"); + goto fail; + } + + pcap_packet_type = pcap_datalink(pt); + switch (pcap_packet_type) { + case DLT_EN10MB: + t = "DLT_EN10MB"; + break; + case DLT_LINUX_SLL: + t = "DLT_LINUX_SLL"; + break; +#ifdef DLT_LINUX_SLL2 + case DLT_LINUX_SLL2: + t = "DLT_LINUX_SLL2"; + break; +#endif /* DLT_LINUX_SLL2 */ + default: + DBG_ERR("Unknown pcap packet type %d\n", pcap_packet_type); + goto fail; + } + + fd = pcap_get_selectable_fd(pt); + DBG_DEBUG("Opened pcap capture for TCP tickle (type=%s, fd=%d)\n", + t, + fd); + + *((pcap_t **)private_data) = pt; + return fd; + +fail: + pcap_close(pt); + return -1; +} + +int ctdb_sys_close_capture_socket(void *private_data) +{ + pcap_t *pt = (pcap_t *)private_data; + pcap_close(pt); + return 0; +} + +int ctdb_sys_read_tcp_packet(int s, + void *private_data, + ctdb_sock_addr *src, + ctdb_sock_addr *dst, + uint32_t *ack_seq, + uint32_t *seq, + int *rst, + uint16_t *window) +{ + int ret; + struct pcap_pkthdr pkthdr; + const u_char *buffer; + pcap_t *pt = (pcap_t *)private_data; + int pcap_packet_type; + uint16_t ether_type; + size_t ll_hdr_len; + + buffer=pcap_next(pt, &pkthdr); + if (buffer==NULL) { + return ENOMSG; + } + + ZERO_STRUCTP(src); + ZERO_STRUCTP(dst); + + pcap_packet_type = pcap_datalink(pt); + switch (pcap_packet_type) { + case DLT_EN10MB: { + const struct ether_header *eth = + (const struct ether_header *)buffer; + ether_type = ntohs(eth->ether_type); + ll_hdr_len = sizeof(struct ether_header); + break; + } + case DLT_LINUX_SLL: { + const struct sll_header *sll = + (const struct sll_header *)buffer; + uint16_t arphrd_type = ntohs(sll->sll_hatype); + switch (arphrd_type) { + case ARPHRD_ETHER: + case ARPHRD_INFINIBAND: + break; + default: + DBG_DEBUG("SLL: Unknown arphrd_type %"PRIu16"\n", + arphrd_type); + return EPROTONOSUPPORT; + } + ether_type = ntohs(sll->sll_protocol); + ll_hdr_len = SLL_HDR_LEN; + break; + } +#ifdef DLT_LINUX_SLL2 + case DLT_LINUX_SLL2: { + const struct sll2_header *sll2 = + (const struct sll2_header *)buffer; + uint16_t arphrd_type = ntohs(sll2->sll2_hatype); + switch (arphrd_type) { + case ARPHRD_ETHER: + case ARPHRD_INFINIBAND: + break; + default: + DBG_DEBUG("SLL2: Unknown arphrd_type %"PRIu16"\n", + arphrd_type); + return EPROTONOSUPPORT; + } + ether_type = ntohs(sll2->sll2_protocol); + ll_hdr_len = SLL2_HDR_LEN; + break; + } +#endif /* DLT_LINUX_SLL2 */ + default: + DBG_DEBUG("Unknown pcap packet type %d\n", pcap_packet_type); + return EPROTONOSUPPORT; + } + + switch (ether_type) { + case ETHERTYPE_IP: + ret = tcp4_extract(buffer + ll_hdr_len, + (size_t)pkthdr.caplen - ll_hdr_len, + &src->ip, + &dst->ip, + ack_seq, + seq, + rst, + window); + break; + case ETHERTYPE_IP6: + ret = tcp6_extract(buffer + ll_hdr_len, + (size_t)pkthdr.caplen - ll_hdr_len, + &src->ip6, + &dst->ip6, + ack_seq, + seq, + rst, + window); + break; + case ETHERTYPE_ARP: + /* Silently ignore ARP packets */ + return EPROTO; + default: + DBG_DEBUG("Unknown ether type %"PRIu16"\n", ether_type); + return EPROTO; + } + + return ret; +} + +#endif /* defined(HAVE_AF_PACKET) && !defined(ENABLE_PCAP) */ diff --git a/ctdb/common/system_socket.h b/ctdb/common/system_socket.h new file mode 100644 index 0000000..065c53c --- /dev/null +++ b/ctdb/common/system_socket.h @@ -0,0 +1,46 @@ +/* + System specific network code + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_SYSTEM_SOCKET_H__ +#define __CTDB_SYSTEM_SOCKET_H__ + +#include "protocol/protocol.h" + +bool ctdb_sys_have_ip(ctdb_sock_addr *addr); + +int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface); + +int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, + const ctdb_sock_addr *src, + uint32_t seq, + uint32_t ack, + int rst); + +int ctdb_sys_open_capture_socket(const char *iface, void **private_data); +int ctdb_sys_close_capture_socket(void *private_data); +int ctdb_sys_read_tcp_packet(int s, + void *private_data, + ctdb_sock_addr *src, + ctdb_sock_addr *dst, + uint32_t *ack_seq, + uint32_t *seq, + int *rst, + uint16_t *window); + +#endif /* __CTDB_SYSTEM_SOCKET_H__ */ diff --git a/ctdb/common/tmon.c b/ctdb/common/tmon.c new file mode 100644 index 0000000..04bad1f --- /dev/null +++ b/ctdb/common/tmon.c @@ -0,0 +1,602 @@ +/* + Trivial FD monitoring + + Copyright (C) Martin Schwenke & Amitay Isaacs, DataDirect Networks 2022 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "lib/util/blocking.h" +#include "lib/util/sys_rw.h" +#include "lib/util/tevent_unix.h" +#include "lib/util/util.h" +#include "lib/util/smb_strtox.h" + +#include "lib/async_req/async_sock.h" + +#include "common/tmon.h" + + +enum tmon_message_type { + TMON_MSG_EXIT = 1, + TMON_MSG_ERRNO, + TMON_MSG_PING, + TMON_MSG_ASCII, + TMON_MSG_CUSTOM, +}; + +struct tmon_pkt { + enum tmon_message_type type; + uint16_t val; +}; + +struct tmon_buf { + uint8_t data[4]; +}; + +static void tmon_packet_push(struct tmon_pkt *pkt, + struct tmon_buf *buf) +{ + uint16_t type_n, val_n; + + type_n = htons(pkt->type); + val_n = htons(pkt->val); + memcpy(&buf->data[0], &type_n, 2); + memcpy(&buf->data[2], &val_n, 2); +} + +static void tmon_packet_pull(struct tmon_buf *buf, + struct tmon_pkt *pkt) +{ + uint16_t type_n, val_n; + + memcpy(&type_n, &buf->data[0], 2); + memcpy(&val_n, &buf->data[2], 2); + + pkt->type = ntohs(type_n); + pkt->val = ntohs(val_n); +} + +static int tmon_packet_write(int fd, struct tmon_pkt *pkt) +{ + struct tmon_buf buf; + ssize_t n; + + tmon_packet_push(pkt, &buf); + + n = sys_write(fd, &buf.data[0], sizeof(buf.data)); + if (n == -1) { + return errno; + } + return 0; +} + +bool tmon_set_exit(struct tmon_pkt *pkt) +{ + *pkt = (struct tmon_pkt) { + .type = TMON_MSG_EXIT, + }; + + return true; +} + +bool tmon_set_errno(struct tmon_pkt *pkt, int err) +{ + if (err <= 0 || err > UINT16_MAX) { + return false; + } + + *pkt = (struct tmon_pkt) { + .type = TMON_MSG_ERRNO, + .val = (uint16_t)err, + }; + + return true; +} + +bool tmon_set_ping(struct tmon_pkt *pkt) +{ + *pkt = (struct tmon_pkt) { + .type = TMON_MSG_PING, + }; + + return true; +} + +bool tmon_set_ascii(struct tmon_pkt *pkt, char c) +{ + if (!isascii(c)) { + return false; + } + + *pkt = (struct tmon_pkt) { + .type = TMON_MSG_ASCII, + .val = (uint16_t)c, + }; + + return true; +} + +bool tmon_set_custom(struct tmon_pkt *pkt, uint16_t val) +{ + *pkt = (struct tmon_pkt) { + .type = TMON_MSG_CUSTOM, + .val = val, + }; + + return true; +} + +static bool tmon_parse_exit(struct tmon_pkt *pkt) +{ + if (pkt->type != TMON_MSG_EXIT) { + return false; + } + if (pkt->val != 0) { + return false; + } + + return true; +} + +static bool tmon_parse_errno(struct tmon_pkt *pkt, int *err) +{ + if (pkt->type != TMON_MSG_ERRNO) { + return false; + } + *err= (int)pkt->val; + + return true; +} + +bool tmon_parse_ping(struct tmon_pkt *pkt) +{ + if (pkt->type != TMON_MSG_PING) { + return false; + } + if (pkt->val != 0) { + return false; + } + + return true; +} + +bool tmon_parse_ascii(struct tmon_pkt *pkt, char *c) +{ + if (pkt->type != TMON_MSG_ASCII) { + return false; + } + if (!isascii((int)pkt->val)) { + return false; + } + *c = (char)pkt->val; + + return true; +} + +bool tmon_parse_custom(struct tmon_pkt *pkt, uint16_t *val) +{ + if (pkt->type != TMON_MSG_CUSTOM) { + return false; + } + *val = pkt->val; + + return true; +} + +struct tmon_state { + int fd; + int direction; + struct tevent_context *ev; + bool monitor_close; + unsigned long write_interval; + unsigned long read_timeout; + struct tmon_actions actions; + struct tevent_timer *timer; + void *private_data; +}; + +static void tmon_readable(struct tevent_req *subreq); +static bool tmon_set_timeout(struct tevent_req *req, + struct tevent_context *ev); +static void tmon_timedout(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval now, + void *private_data); +static void tmon_write_loop(struct tevent_req *subreq); + +struct tevent_req *tmon_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, + int direction, + unsigned long read_timeout, + unsigned long write_interval, + struct tmon_actions *actions, + void *private_data) +{ + struct tevent_req *req, *subreq; + struct tmon_state *state; + bool status; + + req = tevent_req_create(mem_ctx, &state, struct tmon_state); + if (req == NULL) { + return NULL; + } + + if (actions != NULL) { + /* If FD isn't readable then read actions are invalid */ + if (!(direction & TMON_FD_READ) && + (actions->timeout_callback != NULL || + actions->read_callback != NULL || + read_timeout != 0)) { + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + /* If FD isn't writeable then write actions are invalid */ + if (!(direction & TMON_FD_WRITE) && + (actions->write_callback != NULL || + write_interval != 0)) { + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + /* Can't specify write interval without a callback */ + if (state->write_interval != 0 && + state->actions.write_callback == NULL) { + tevent_req_error(req, EINVAL); + return tevent_req_post(req, ev); + } + } + + state->fd = fd; + state->direction = direction; + state->ev = ev; + state->write_interval = write_interval; + state->read_timeout = read_timeout; + state->private_data = private_data; + + if (actions != NULL) { + state->actions = *actions; + } + + status = set_close_on_exec(fd); + if (!status) { + tevent_req_error(req, errno); + return tevent_req_post(req, ev); + } + + if (direction & TMON_FD_READ) { + subreq = wait_for_read_send(state, ev, fd, true); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, tmon_readable, req); + } + + if (state->read_timeout != 0) { + status = tmon_set_timeout(req, state->ev); + if (!status) { + tevent_req_error(req, ENOMEM); + return tevent_req_post(req, ev); + } + } + + if (state->write_interval != 0) { + subreq = tevent_wakeup_send( + state, + state->ev, + tevent_timeval_current_ofs(state->write_interval, 0)); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, state->ev); + } + tevent_req_set_callback(subreq, tmon_write_loop, req); + } + + return req; +} + +static void tmon_readable(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct tmon_state *state = tevent_req_data( req, struct tmon_state); + struct tmon_buf buf; + struct tmon_pkt pkt; + ssize_t nread; + bool status; + int err; + int ret; + + status = wait_for_read_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (!status) { + if (ret == EPIPE && state->actions.close_callback != NULL) { + ret = state->actions.close_callback(state->private_data); + if (ret == TMON_STATUS_EXIT) { + ret = 0; + } + } + if (ret == 0) { + tevent_req_done(req); + } else { + tevent_req_error(req, ret); + } + return; + } + + nread = sys_read(state->fd, buf.data, sizeof(buf.data)); + if (nread == -1) { + tevent_req_error(req, errno); + return; + } + if (nread == 0) { + /* Can't happen, treat like EPIPE, above */ + tevent_req_error(req, EPIPE); + return; + } + if (nread != sizeof(buf.data)) { + tevent_req_error(req, EPROTO); + return; + } + + tmon_packet_pull(&buf, &pkt); + + switch (pkt.type) { + case TMON_MSG_EXIT: + status = tmon_parse_exit(&pkt); + if (!status) { + tevent_req_error(req, EPROTO); + return; + } + tevent_req_done(req); + return; + case TMON_MSG_ERRNO: + status = tmon_parse_errno(&pkt, &err); + if (!status) { + err = EPROTO; + } + tevent_req_error(req, err); + return; + default: + break; + } + + if (state->actions.read_callback == NULL) { + /* Shouldn't happen, other end should not write */ + tevent_req_error(req, EIO); + return; + } + ret = state->actions.read_callback(state->private_data, &pkt); + if (ret == TMON_STATUS_EXIT) { + tevent_req_done(req); + return; + } + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + subreq = wait_for_read_send(state, state->ev, state->fd, true); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, tmon_readable, req); + + /* Reset read timeout */ + if (state->read_timeout != 0) { + status = tmon_set_timeout(req, state->ev); + if (!status) { + tevent_req_error(req, ENOMEM); + return; + } + } +} + +static bool tmon_set_timeout(struct tevent_req *req, + struct tevent_context *ev) +{ + struct tmon_state *state = tevent_req_data( + req, struct tmon_state); + struct timeval endtime = + tevent_timeval_current_ofs(state->read_timeout, 0); + + TALLOC_FREE(state->timer); + + state->timer = tevent_add_timer(ev, req, endtime, tmon_timedout, req); + if (tevent_req_nomem(state->timer, req)) { + return false; + } + + return true; +} + +static void tmon_timedout(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval now, + void *private_data) +{ + struct tevent_req *req = talloc_get_type_abort( + private_data, struct tevent_req); + struct tmon_state *state = tevent_req_data(req, struct tmon_state); + int ret; + + TALLOC_FREE(state->timer); + + if (state->actions.timeout_callback != NULL) { + ret = state->actions.timeout_callback(state->private_data); + if (ret == TMON_STATUS_EXIT) { + ret = 0; + } + } else { + ret = ETIMEDOUT; + } + + if (ret == 0) { + tevent_req_done(req); + } else { + tevent_req_error(req, ret); + } +} + +static void tmon_write_loop(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct tmon_state *state = tevent_req_data( + req, struct tmon_state); + struct tmon_pkt pkt; + int ret; + bool status; + + status = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (!status) { + /* Ignore error */ + } + + ret = state->actions.write_callback(state->private_data, &pkt); + if (ret == TMON_STATUS_EXIT) { + tevent_req_done(req); + return; + } + if (ret == TMON_STATUS_SKIP) { + goto done; + } + if (ret != 0) { + tevent_req_error(req, ret); + return; + } + + status = tmon_write(req, &pkt); + if (!status) { + return; + } + +done: + subreq = tevent_wakeup_send( + state, + state->ev, + tevent_timeval_current_ofs(state->write_interval, 0)); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, tmon_write_loop, req); +} + +bool tmon_write(struct tevent_req *req, struct tmon_pkt *pkt) +{ + struct tmon_state *state = tevent_req_data( + req, struct tmon_state); + int ret; + + if (state->fd == -1) { + return false; + } + + if (!(state->direction & TMON_FD_WRITE)) { + tevent_req_error(req, EINVAL); + return false; + } + + ret = tmon_packet_write(state->fd, pkt); + if (ret != 0) { + if (ret == EPIPE && state->actions.close_callback != NULL) { + ret = state->actions.close_callback(state->private_data); + if (ret == TMON_STATUS_EXIT) { + ret = 0; + } + } + + if (ret == 0) { + tevent_req_done(req); + } else { + tevent_req_error(req, ret); + } + state->fd = -1; + return false; + } + + return true; +} + +bool tmon_recv(struct tevent_req *req, int *perr) +{ + if (tevent_req_is_unix_error(req, perr)) { + return false; + } + + return true; +} + +static int ping_writer(void *private_data, struct tmon_pkt *pkt) +{ + tmon_set_ping(pkt); + + return 0; +} + +static int ping_reader(void *private_data, struct tmon_pkt *pkt) +{ + bool status; + + /* Only expect pings */ + status = tmon_parse_ping(pkt); + if (!status) { + return EPROTO; + } + + return 0; +} + +struct tevent_req *tmon_ping_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, + int direction, + unsigned long timeout, + unsigned long interval) +{ + struct tevent_req *req; + struct tmon_actions actions = { + .write_callback = NULL, + }; + + if ((direction & TMON_FD_WRITE) && interval != 0) { + actions.write_callback = ping_writer; + } + if ((direction & TMON_FD_READ) && timeout != 0) { + actions.read_callback = ping_reader; + } + + req = tmon_send(mem_ctx, + ev, + fd, + direction, + timeout, + interval, + &actions, + NULL); + return req; +} + +bool tmon_ping_recv(struct tevent_req *req, int *perr) +{ + bool status; + + status = tmon_recv(req, perr); + + return status; +} diff --git a/ctdb/common/tmon.h b/ctdb/common/tmon.h new file mode 100644 index 0000000..1d315a9 --- /dev/null +++ b/ctdb/common/tmon.h @@ -0,0 +1,218 @@ +/* + Trivial FD monitoring + + Copyright (C) Martin Schwenke & Amitay Isaacs, DataDirect Networks 2022 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_TMON_H__ +#define __CTDB_TMON_H__ + +#include +#include + +/** + * @file tmon.h + * + * @brief Interprocess file descriptor (pipe and socketpair) monitoring + * + * Assumes 2 processes connected by a pipe(2) or a socketpair(2). A + * simple protocol is defined to allow sending various types of status + * information. When a pipe(2) is used the reader can monitor for + * close and read packets, while the sender can write packets. When a + * socketpair(2) is used then both ends can monitor for close, and + * read and write packets. A read timeout can be specified, + * terminating the computation if no packets are received. + * + * A simplified interface is provided to monitor for close and allow + * sending/monitoring of one-way ping packets. A ping timeout occurs + * when one end is expecting pings but none are received during the + * timeout interval - no response is sent to pings, they merely reset + * a timer on the receiving end. + */ + +struct tmon_pkt; + +struct tmon_actions { + int (*write_callback)(void *private_data, struct tmon_pkt *pkt); + int (*timeout_callback)(void *private_data); + int (*read_callback)(void *private_data, struct tmon_pkt *pkt); + int (*close_callback)(void *private_data); +}; + +/* + * Return value from write_callback() and read_callback() to cause the + * computation to exit successfully. For consistency this can also be + * used with timeout_callback() and close_callback(). + */ +#define TMON_STATUS_EXIT (-1) + +/* Return value from write_callback() to skip write */ +#define TMON_STATUS_SKIP (-2) + +/* For direction, below */ +#define TMON_FD_READ 0x1 +#define TMON_FD_WRITE 0x2 +#define TMON_FD_BOTH (TMON_FD_READ | TMON_FD_WRITE) + +/** + * @brief Async computation to start FD monitoring + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] fd File descriptor for "this" end of pipe/socketpair + * @param[in] direction Read, write or both - for sanity checking + * @param[in] read_timeout Seconds to trigger timeout when no packets received + * @param[in] write_interval Seconds to trigger write_callback + * @param[in] actions struct containing callbacks + * @param[in] private_data Passed to callbacks + * @return new tevent request or NULL on failure + * + * @note read_timeout implies monitor_close + * + * @note The computation will complete when: + * + * - The writing end closes (e.g. writer process terminates) - EPIPE + * - read_timeout is non-zero and timeout occurs - ETIMEDOUT + * - Packets received with no read_callback defined - EIO + * - Invalid or unexpected packet received - EPROTO + * - File descriptor readable but no bytes to read - error: EPIPE + * - Invalid combination of direction, callbacks, timeouts: EINVAL + * - An unexpected error occurs - other + * + * @note action callbacks return an int that can be used to trigger + * other errors or override an error. For example: + * + * - write_callback() can return non-zero errno, causing an error + * - close_callback() can return zero, overriding the default EPIPE error + * - timeout_callback() can return something other than ETIMEDOUT + * - read_callback() can return EPROTO for unexpected packet types + * + * Reading of exit and errno packets is handled internally (read + * callback is never called). Write callback can return special + * value TMON_STATUS_SKIP to avoid sending any data. + */ +struct tevent_req *tmon_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, + int direction, + unsigned long read_timeout, + unsigned long write_interval, + struct tmon_actions *actions, + void *private_data); + +/** + * @brief Async computation to end FD monitoring + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool tmon_recv(struct tevent_req *req, int *perr); + +/** + * @brief Fill in an exit packet + * + * @param[in,out] pkt An exit packet + * @return true on success, false on failure + */ +bool tmon_set_exit(struct tmon_pkt *pkt); +/** + * @brief Fill in an errno packet + * + * @param[in,out] pkt An errno packet + * @param[in] err An errno to send in packet + * @return true on success, false on failure + */ +bool tmon_set_errno(struct tmon_pkt *pkt, int err); +/** + * @brief Fill in a ping packet + * + * @param[in,out] pkt A ping packet + * @return true on success, false on failure + */ +bool tmon_set_ping(struct tmon_pkt *pkt); +/** + * @brief Fill in an ASCII packet + * + * @param[in,out] pkt An ASCII packet + * @param[in] c An ASCII character to send in packet + * @return true on success, false on failure + */ +bool tmon_set_ascii(struct tmon_pkt *pkt, char c); +/** + * @brief Fill in a custom packet + * + * @param[in,out] pkt A custom packet + * @param[in] val A uint16_t to send in a custom packet + * @return true on success, false on failure + */ +bool tmon_set_custom(struct tmon_pkt *pkt, uint16_t val); + +/** + * @brief Validate a ping packet + * + * @param[in] pkt A ping packet + * @return true on success, false on failure + */ +bool tmon_parse_ping(struct tmon_pkt *pkt); + +/** + * @brief Validate ASCII packet and parse out character + * + * @param[in] pkt An ASCII packet + * @param[out] c An ASCII character value from packet + * @return true on success, false on failure + */ +bool tmon_parse_ascii(struct tmon_pkt *pkt, char *c); + +/** + * @brief Validate custom packet and parse out value + * + * @param[in] pkt A custom packet + * @param[out] val A uint16_t value from packet + * @return true on success, false on failure + */ +bool tmon_parse_custom(struct tmon_pkt *pkt, uint16_t *val); + +/** + * @brief Write a packet + * + * @param[in] req Tevent request created by tmon_send + * @param[in] pkt Packet to write + * @return true on sucess, false on failure + */ +bool tmon_write(struct tevent_req *req, struct tmon_pkt *pkt); + +/** + * @brief Async computation to start ping monitoring + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] fd File descriptor for "this" end of pipe/socketpair + * @param[in] direction Read, write or both - for sanity checking + * @param[in] timeout Timeout for pings on receiving end + * @param[in] interval Send a ping packet every interval seconds + */ +struct tevent_req *tmon_ping_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int fd, + int direction, + unsigned long timeout, + unsigned long interval); + +bool tmon_ping_recv(struct tevent_req *req, int *perr); + +#endif /* __CTDB_TMON_H__ */ diff --git a/ctdb/common/tunable.c b/ctdb/common/tunable.c new file mode 100644 index 0000000..f366f23 --- /dev/null +++ b/ctdb/common/tunable.c @@ -0,0 +1,401 @@ +/* + Tunables utilities + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/locale.h" +#include "system/network.h" + +#include + +#include "lib/util/debug.h" +#include "lib/util/smb_strtox.h" +#include "lib/util/tini.h" + +#include "protocol/protocol.h" + +#include "common/tunable.h" + +static struct { + const char *label; + uint32_t value; + bool obsolete; + size_t offset; +} tunable_map[] = { + { "MaxRedirectCount", 3, true, + offsetof(struct ctdb_tunable_list, max_redirect_count) }, + { "SeqnumInterval", 1000, false, + offsetof(struct ctdb_tunable_list, seqnum_interval) }, + { "ControlTimeout", 60, false, + offsetof(struct ctdb_tunable_list, control_timeout) }, + { "TraverseTimeout", 20, false, + offsetof(struct ctdb_tunable_list, traverse_timeout) }, + { "KeepaliveInterval", 5, false, + offsetof(struct ctdb_tunable_list, keepalive_interval) }, + { "KeepaliveLimit", 5, false, + offsetof(struct ctdb_tunable_list, keepalive_limit) }, + { "RecoverTimeout", 30, false, + offsetof(struct ctdb_tunable_list, recover_timeout) }, + { "RecoverInterval", 1, false, + offsetof(struct ctdb_tunable_list, recover_interval) }, + { "ElectionTimeout", 3, false, + offsetof(struct ctdb_tunable_list, election_timeout) }, + { "TakeoverTimeout", 9, false, + offsetof(struct ctdb_tunable_list, takeover_timeout) }, + { "MonitorInterval", 15, false, + offsetof(struct ctdb_tunable_list, monitor_interval) }, + { "TickleUpdateInterval", 20, false, + offsetof(struct ctdb_tunable_list, tickle_update_interval) }, + { "EventScriptTimeout", 30, false, + offsetof(struct ctdb_tunable_list, script_timeout) }, + { "MonitorTimeoutCount", 20, false, + offsetof(struct ctdb_tunable_list, monitor_timeout_count) }, + { "EventScriptUnhealthyOnTimeout", 0, true, + offsetof(struct ctdb_tunable_list, script_unhealthy_on_timeout) }, + { "RecoveryGracePeriod", 120, false, + offsetof(struct ctdb_tunable_list, recovery_grace_period) }, + { "RecoveryBanPeriod", 300, false, + offsetof(struct ctdb_tunable_list, recovery_ban_period) }, + { "DatabaseHashSize", 100001, false, + offsetof(struct ctdb_tunable_list, database_hash_size) }, + { "DatabaseMaxDead", 5, false, + offsetof(struct ctdb_tunable_list, database_max_dead) }, + { "RerecoveryTimeout", 10, false, + offsetof(struct ctdb_tunable_list, rerecovery_timeout) }, + { "EnableBans", 1, false, + offsetof(struct ctdb_tunable_list, enable_bans) }, + { "DeterministicIPs", 0, true, + offsetof(struct ctdb_tunable_list, deterministic_public_ips) }, + { "LCP2PublicIPs", 1, true, + offsetof(struct ctdb_tunable_list, lcp2_public_ip_assignment) }, + { "ReclockPingPeriod", 60, true, + offsetof(struct ctdb_tunable_list, reclock_ping_period) }, + { "NoIPFailback", 0, false, + offsetof(struct ctdb_tunable_list, no_ip_failback) }, + { "DisableIPFailover", 0, true, + offsetof(struct ctdb_tunable_list, disable_ip_failover) }, + { "VerboseMemoryNames", 0, false, + offsetof(struct ctdb_tunable_list, verbose_memory_names) }, + { "RecdPingTimeout", 60, false, + offsetof(struct ctdb_tunable_list, recd_ping_timeout) }, + { "RecdFailCount", 10, false, + offsetof(struct ctdb_tunable_list, recd_ping_failcount) }, + { "LogLatencyMs", 0, false, + offsetof(struct ctdb_tunable_list, log_latency_ms) }, + { "RecLockLatencyMs", 1000, false, + offsetof(struct ctdb_tunable_list, reclock_latency_ms) }, + { "RecoveryDropAllIPs", 120, false, + offsetof(struct ctdb_tunable_list, recovery_drop_all_ips) }, + { "VerifyRecoveryLock", 1, true, + offsetof(struct ctdb_tunable_list, verify_recovery_lock) }, + { "VacuumInterval", 10, false, + offsetof(struct ctdb_tunable_list, vacuum_interval) }, + { "VacuumMaxRunTime", 120, false, + offsetof(struct ctdb_tunable_list, vacuum_max_run_time) }, + { "RepackLimit", 10*1000, false, + offsetof(struct ctdb_tunable_list, repack_limit) }, + { "VacuumLimit", 5*1000, true, + offsetof(struct ctdb_tunable_list, vacuum_limit) }, + { "VacuumFastPathCount", 60, false, + offsetof(struct ctdb_tunable_list, vacuum_fast_path_count) }, + { "MaxQueueDropMsg", 1000*1000, false, + offsetof(struct ctdb_tunable_list, max_queue_depth_drop_msg) }, + { "AllowUnhealthyDBRead", 0, false, + offsetof(struct ctdb_tunable_list, allow_unhealthy_db_read) }, + { "StatHistoryInterval", 1, false, + offsetof(struct ctdb_tunable_list, stat_history_interval) }, + { "DeferredAttachTO", 120, false, + offsetof(struct ctdb_tunable_list, deferred_attach_timeout) }, + { "AllowClientDBAttach", 1, false, + offsetof(struct ctdb_tunable_list, allow_client_db_attach) }, + { "RecoverPDBBySeqNum", 1, true, + offsetof(struct ctdb_tunable_list, recover_pdb_by_seqnum) }, + { "DeferredRebalanceOnNodeAdd", 300, true, + offsetof(struct ctdb_tunable_list, deferred_rebalance_on_node_add) }, + { "FetchCollapse", 1, false, + offsetof(struct ctdb_tunable_list, fetch_collapse) }, + { "HopcountMakeSticky", 50, false, + offsetof(struct ctdb_tunable_list, hopcount_make_sticky) }, + { "StickyDuration", 600, false, + offsetof(struct ctdb_tunable_list, sticky_duration) }, + { "StickyPindown", 200, false, + offsetof(struct ctdb_tunable_list, sticky_pindown) }, + { "NoIPTakeover", 0, false, + offsetof(struct ctdb_tunable_list, no_ip_takeover) }, + { "DBRecordCountWarn", 100*1000, false, + offsetof(struct ctdb_tunable_list, db_record_count_warn) }, + { "DBRecordSizeWarn", 10*1000*1000, false, + offsetof(struct ctdb_tunable_list, db_record_size_warn) }, + { "DBSizeWarn", 100*1000*1000, false, + offsetof(struct ctdb_tunable_list, db_size_warn) }, + { "PullDBPreallocation", 10*1024*1024, false, + offsetof(struct ctdb_tunable_list, pulldb_preallocation_size) }, + { "NoIPHostOnAllDisabled", 1, true, + offsetof(struct ctdb_tunable_list, no_ip_host_on_all_disabled) }, + { "Samba3AvoidDeadlocks", 0, true, + offsetof(struct ctdb_tunable_list, samba3_hack) }, + { "TDBMutexEnabled", 1, true, + offsetof(struct ctdb_tunable_list, mutex_enabled) }, + { "LockProcessesPerDB", 200, false, + offsetof(struct ctdb_tunable_list, lock_processes_per_db) }, + { "RecBufferSizeLimit", 1000*1000, false, + offsetof(struct ctdb_tunable_list, rec_buffer_size_limit) }, + { "QueueBufferSize", 1024, false, + offsetof(struct ctdb_tunable_list, queue_buffer_size) }, + { "IPAllocAlgorithm", 2, false, + offsetof(struct ctdb_tunable_list, ip_alloc_algorithm) }, + { "AllowMixedVersions", 0, false, + offsetof(struct ctdb_tunable_list, allow_mixed_versions) }, + { .obsolete = true, } +}; + +void ctdb_tunable_set_defaults(struct ctdb_tunable_list *tun_list) +{ + int i; + + for (i=0; tunable_map[i].label != NULL; i++) { + size_t offset = tunable_map[i].offset; + uint32_t value = tunable_map[i].value; + uint32_t *value_ptr; + + value_ptr = (uint32_t *)((uint8_t *)tun_list + offset); + *value_ptr = value; + } +} + +bool ctdb_tunable_get_value(struct ctdb_tunable_list *tun_list, + const char *tunable_str, uint32_t *value) +{ + int i; + + for (i=0; tunable_map[i].label != NULL; i++) { + if (strcasecmp(tunable_map[i].label, tunable_str) == 0) { + uint32_t *value_ptr; + + value_ptr = (uint32_t *)((uint8_t *)tun_list + + tunable_map[i].offset); + *value = *value_ptr; + return true; + } + } + + return false; +} + +bool ctdb_tunable_set_value(struct ctdb_tunable_list *tun_list, + const char *tunable_str, uint32_t value, + bool *obsolete) +{ + int i; + + for (i=0; tunable_map[i].label != NULL; i++) { + if (strcasecmp(tunable_map[i].label, tunable_str) == 0) { + uint32_t *value_ptr; + + value_ptr = (uint32_t *)((uint8_t *)tun_list + + tunable_map[i].offset); + *value_ptr = value; + if (obsolete != NULL) { + *obsolete = tunable_map[i].obsolete; + } + return true; + } + } + + return false; +} + +struct ctdb_var_list *ctdb_tunable_names(TALLOC_CTX *mem_ctx) +{ + struct ctdb_var_list *list; + int i; + + list = talloc_zero(mem_ctx, struct ctdb_var_list); + if (list == NULL) { + return NULL; + } + + for (i=0; tunable_map[i].label != NULL; i++) { + if (tunable_map[i].obsolete) { + continue; + } + + list->var = talloc_realloc(list, list->var, const char *, + list->count + 1); + if (list->var == NULL) { + goto fail; + } + + list->var[list->count] = talloc_strdup(list, + tunable_map[i].label); + if (list->var[list->count] == NULL) { + goto fail; + } + + list->count += 1; + } + + return list; + +fail: + TALLOC_FREE(list); + return NULL; +} + +char *ctdb_tunable_names_to_string(TALLOC_CTX *mem_ctx) +{ + char *str = NULL; + int i; + + str = talloc_strdup(mem_ctx, ":"); + if (str == NULL) { + return NULL; + } + + for (i=0; tunable_map[i].label != NULL; i++) { + if (tunable_map[i].obsolete) { + continue; + } + + str = talloc_asprintf_append(str, "%s:", + tunable_map[i].label); + if (str == NULL) { + return NULL; + } + } + + /* Remove the last ':' */ + str[strlen(str)-1] = '\0'; + + return str; +} + +struct tunable_load_state { + struct ctdb_tunable_list *tun_list; + bool status; + const char *func; +}; + +static bool tunable_section(const char *section, void *private_data) +{ + struct tunable_load_state *state = + (struct tunable_load_state *)private_data; + + D_ERR("%s: Invalid line for section [%s] - sections not supported \n", + state->func, + section); + state->status = false; + + return true; +} + +static bool tunable_option(const char *name, + const char *value, + void *private_data) +{ + struct tunable_load_state *state = + (struct tunable_load_state *)private_data; + unsigned long num; + bool obsolete; + bool ok; + int ret; + + if (value[0] == '\0') { + D_ERR("%s: Invalid line containing \"%s\"\n", state->func, name); + state->status = false; + return true; + } + + num = smb_strtoul(value, NULL, 0, &ret, SMB_STR_FULL_STR_CONV); + if (ret != 0) { + D_ERR("%s: Invalid value \"%s\" for tunable \"%s\"\n", + state->func, + value, + name); + state->status = false; + return true; + } + + ok = ctdb_tunable_set_value(state->tun_list, + name, + (uint32_t)num, + &obsolete); + if (!ok) { + D_ERR("%s: Unknown tunable \"%s\"\n", state->func, name); + state->status = false; + return true; + } + if (obsolete) { + D_ERR("%s: Obsolete tunable \"%s\"\n", state->func, name); + state->status = false; + return true; + } + + return true; +} + +bool ctdb_tunable_load_file(TALLOC_CTX *mem_ctx, + struct ctdb_tunable_list *tun_list, + const char *file) +{ + struct tunable_load_state state = { + .tun_list = tun_list, + .status = true, + .func = __FUNCTION__, + }; + FILE *fp; + bool status; + + ctdb_tunable_set_defaults(tun_list); + + fp = fopen(file, "r"); + if (fp == NULL) { + if (errno == ENOENT) { + /* Doesn't need to exist */ + return true; + } + + DBG_ERR("Failed to open %s\n", file); + return false; + } + + D_NOTICE("Loading tunables from %s\n", file); + /* + * allow_empty_value=true is somewhat counter-intuitive. + * However, if allow_empty_value=false then a tunable with no + * equals or value is regarded as empty and is simply ignored. + * Use true so an "empty value" can be caught in + * tunable_option(). + * + * tunable_section() and tunable_option() return true while + * setting state.status=false, allowing all possible errors + * with tunables and values to be reported. This helps to + * avoid a potential game of whack-a-mole in a well-formed + * file with multiple minor errors. + */ + status = tini_parse(fp, true, tunable_section, tunable_option, &state); + + fclose(fp); + + if (!status) { + DBG_ERR("Syntax error\n"); + } + + return status && state.status; +} diff --git a/ctdb/common/tunable.h b/ctdb/common/tunable.h new file mode 100644 index 0000000..89f99f1 --- /dev/null +++ b/ctdb/common/tunable.h @@ -0,0 +1,35 @@ +/* + Tunable utilities + + Copyright (C) Amitay Isaacs 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_TUNABLE_H__ +#define __CTDB_TUNABLE_H__ + +void ctdb_tunable_set_defaults(struct ctdb_tunable_list *tun_list); +bool ctdb_tunable_get_value(struct ctdb_tunable_list *tun_list, + const char *tunable_str, uint32_t *value); +bool ctdb_tunable_set_value(struct ctdb_tunable_list *tun_list, + const char *tunable_str, uint32_t value, + bool *obsolete); +struct ctdb_var_list *ctdb_tunable_names(TALLOC_CTX *mem_ctx); +char *ctdb_tunable_names_to_string(TALLOC_CTX *mem_ctx); +bool ctdb_tunable_load_file(TALLOC_CTX *mem_ctx, + struct ctdb_tunable_list *tun_list, + const char *file); + +#endif /* __CTDB_TUNABLE_H__ */ diff --git a/ctdb/config/README b/ctdb/config/README new file mode 100644 index 0000000..d28f4f0 --- /dev/null +++ b/ctdb/config/README @@ -0,0 +1,31 @@ +This directory contains run-time support scripts for CTDB. + +Selected highlights: + + ctdb.init + + An initscript for starting ctdbd at boot time. + + events/ + + Eventscripts. See events/README for more details. + + functions + + Support functions, sourced by eventscripts and other scripts. + + statd-callout + + rpc.statd high-availability callout to support lock migration on + failover. + +Notes: + +* All of these scripts are written in POSIX Bourne shell. Please + avoid bash-isms, including the use of "local" variables (which are + not available in POSIX shell). + +* Do not use absolute paths for commands. Unit tests attempt to + replace many commands with stubs and can not do this if commands are + specified with absolute paths. The functions file controls $PATH so + absolute paths should not be required. diff --git a/ctdb/config/ctdb-crash-cleanup.sh b/ctdb/config/ctdb-crash-cleanup.sh new file mode 100755 index 0000000..95cfd75 --- /dev/null +++ b/ctdb/config/ctdb-crash-cleanup.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# +# This script can be called from a cronjob to automatically drop/release +# all public ip addresses if CTDBD has crashed or stopped running. +# + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && echo "$PWD") + +. "${CTDB_BASE}/functions" + +# If ctdb is running, just exit +if service ctdb status >/dev/null 2>&1 ; then + exit 0 +fi + +load_script_options "failover" "11.natgw" + +if [ ! -f "$CTDB_BASE/public_addresses" ] ; then + die "No public addresses file found. Can't clean up." +fi + +drop_all_public_ips 2>&1 | script_log "ctdb-crash-cleanup.sh" + +if [ -n "$CTDB_NATGW_PUBLIC_IP" ] ; then + drop_ip "$CTDB_NATGW_PUBLIC_IP" "ctdb-crash-cleanup.sh" +fi diff --git a/ctdb/config/ctdb.conf b/ctdb/config/ctdb.conf new file mode 100644 index 0000000..8e1b376 --- /dev/null +++ b/ctdb/config/ctdb.conf @@ -0,0 +1,22 @@ +# See ctdb.conf(5) for documentation +# +# See ctdb-script.options(5) for documentation about event script +# options + +[logging] + # Enable logging to syslog + # location = syslog + + # Default log level + # log level = NOTICE + +[cluster] + # Shared cluster lock file to avoid split brain. Daemon + # default is no cluster lock. Do NOT run CTDB without a + # cluster lock file unless you know exactly what you are + # doing. + # + # Please see the CLUSTER LOCK section in ctdb(7) for more + # details. + # + # cluster lock = !/bin/false CLUSTER LOCK NOT CONFIGURED diff --git a/ctdb/config/ctdb.init b/ctdb/config/ctdb.init new file mode 100755 index 0000000..6a7f781 --- /dev/null +++ b/ctdb/config/ctdb.init @@ -0,0 +1,161 @@ +#!/bin/sh + +# Start and stop CTDB (Clustered TDB daemon) +# +# chkconfig: - 90 01 +# +# description: Starts and stops CTDB +# pidfile: /var/run/ctdb/ctdbd.pid +# config: /etc/sysconfig/ctdb + +### BEGIN INIT INFO +# Provides: ctdb +# Required-Start: $local_fs $syslog $network $remote_fs +# Required-Stop: $local_fs $syslog $network $remote_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: start and stop ctdb service +# Description: Start and stop CTDB (Clustered TDB daemon) +### END INIT INFO + +# Source function library. +if [ -f /etc/init.d/functions ] ; then + # Red Hat + . /etc/init.d/functions +elif [ -f /etc/rc.d/init.d/functions ] ; then + # Red Hat + . /etc/rc.d/init.d/functions +elif [ -f /etc/rc.status ] ; then + # SUSE + . /etc/rc.status + rc_reset + LC_ALL=en_US.UTF-8 +elif [ -f /lib/lsb/init-functions ] ; then + # Debian + . /lib/lsb/init-functions +fi + +# Avoid using root's TMPDIR +unset TMPDIR + +[ -n "$CTDB_BASE" ] || export CTDB_BASE="/etc/ctdb" + +. "${CTDB_BASE}/functions" + +load_system_config "network" + +# check networking is up (for redhat) +if [ "$NETWORKING" = "no" ] ; then + exit 0 +fi + +load_system_config "ctdb" + +detect_init_style +export CTDB_INIT_STYLE + +ctdbd="${CTDBD:-/usr/sbin/ctdbd}" +ctdb="${CTDB:-/usr/bin/ctdb}" +pidfile="/var/run/ctdb/ctdbd.pid" + +############################################################ + +start() +{ + printf "Starting ctdbd service: " + + case "$CTDB_INIT_STYLE" in + suse) + startproc "$ctdbd" + rc_status -v + ;; + redhat) + daemon --pidfile "$pidfile" "$ctdbd" + RETVAL=$? + echo + [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1 + return $RETVAL + ;; + debian) + eval start-stop-daemon --start --quiet --background --exec "$ctdbd" + ;; + esac +} + +stop() +{ + printf "Shutting down ctdbd service: " + + case "$CTDB_INIT_STYLE" in + suse) + "$ctdb" "shutdown" + rc_status -v + ;; + redhat) + "$ctdb" "shutdown" + RETVAL=$? + # Common idiom in Red Hat init scripts - success() always + # succeeds so this does behave like if-then-else + # shellcheck disable=SC2015 + [ $RETVAL -eq 0 ] && success || failure + echo "" + [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb + return $RETVAL + ;; + debian) + "$ctdb" "shutdown" + log_end_msg $? + ;; + esac +} + +restart() +{ + stop + start +} + +check_status () +{ + case "$CTDB_INIT_STYLE" in + suse) + checkproc -p "$pidfile" "$ctdbd" + rc_status -v + ;; + redhat) + status -p "$pidfile" -l "ctdb" "$ctdbd" + ;; + debian) + status_of_proc -p "$pidfile" "$ctdbd" "ctdb" + ;; + esac +} + +############################################################ + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart|reload|force-reload) + restart + ;; + status) + check_status + ;; + condrestart|try-restart) + if check_status >/dev/null ; then + restart + fi + ;; + cron) + # used from cron to auto-restart ctdb + check_status >/dev/null 2>&1 || restart + ;; + *) + echo "Usage: $0 {start|stop|restart|reload|force-reload|status|cron|condrestart|try-restart}" + exit 1 +esac diff --git a/ctdb/config/ctdb.sudoers b/ctdb/config/ctdb.sudoers new file mode 100644 index 0000000..1c23818 --- /dev/null +++ b/ctdb/config/ctdb.sudoers @@ -0,0 +1,3 @@ +Defaults!/usr/local/etc/ctdb/statd-callout !requiretty + +rpcuser ALL=(ALL) NOPASSWD: /usr/local/etc/ctdb/statd-callout diff --git a/ctdb/config/ctdb.sysconfig b/ctdb/config/ctdb.sysconfig new file mode 100644 index 0000000..fc57929 --- /dev/null +++ b/ctdb/config/ctdb.sysconfig @@ -0,0 +1,11 @@ +# If using SYSV init, install this as /etc/sysconfig/ctdb, +# /etc/default/ctdb or similar + +# Allow 1M open files +ulimit -n 1048576 + +# Allow core files to be created +ulimit -c unlimited + +# Useful if default detection doesn't work +# CTDB_INIT_STYLE=debian diff --git a/ctdb/config/ctdb.tunables b/ctdb/config/ctdb.tunables new file mode 100644 index 0000000..b99e5cd --- /dev/null +++ b/ctdb/config/ctdb.tunables @@ -0,0 +1,2 @@ +# Set some CTDB tunable variables during CTDB startup? +# MutexEnabled=0 diff --git a/ctdb/config/debug-hung-script.sh b/ctdb/config/debug-hung-script.sh new file mode 100755 index 0000000..c1ac0f1 --- /dev/null +++ b/ctdb/config/debug-hung-script.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +# This script only works on Linux. Please modify (and submit patches) +# for other operating systems. + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && echo "$PWD") + +. "${CTDB_BASE}/functions" + +load_script_options + +# Testing hook +if [ -n "$CTDB_DEBUG_HUNG_SCRIPT_LOGFILE" ] ; then + tmp="${CTDB_DEBUG_HUNG_SCRIPT_LOGFILE}.part" + exec >>"$tmp" 2>&1 +fi + +( + # No use running several of these in parallel if, say, "releaseip" + # event hangs for multiple IPs. In that case the output would be + # interleaved in the log and would just be confusing. + flock --wait 2 9 || exit 1 + + echo "===== Start of hung script debug for PID=\"$1\", event=\"$2\" =====" + + echo "pstree -p -a ${1}:" + out=$(pstree -p -a "$1") + echo "$out" + + # Check for processes matching a regular expression and print + # stack staces. This could help confirm that certain processes + # are stuck in certain places such as the cluster filesystem. The + # regexp must separate items with "|" and must not contain + # parentheses. The default pattern can be replaced for testing. + default_pat='exportfs|rpcinfo' + pat="${CTDB_DEBUG_HUNG_SCRIPT_STACKPAT:-${default_pat}}" + echo "$out" | + sed -r -n "s@.*-(.*(${pat}).*),([0-9]*).*@\\3 \\1@p" | + while read pid name ; do + trace=$(cat "/proc/${pid}/stack" 2>/dev/null) + # No! Checking the exit code afterwards is actually clearer... + # shellcheck disable=SC2181 + if [ $? -eq 0 ] ; then + echo "---- Stack trace of interesting process ${pid}[${name}] ----" + echo "$trace" + fi + done + + if [ "$2" != "init" ] ; then + echo "---- ctdb scriptstatus ${2}: ----" + $CTDB scriptstatus "$2" + fi + + echo "===== End of hung script debug for PID=\"$1\", event=\"$2\" =====" + + if [ -n "$CTDB_DEBUG_HUNG_SCRIPT_LOGFILE" ] ; then + mv "$tmp" "$CTDB_DEBUG_HUNG_SCRIPT_LOGFILE" + fi + +) 9>"${CTDB_SCRIPT_VARDIR}/debug-hung-script.lock" diff --git a/ctdb/config/debug_locks.sh b/ctdb/config/debug_locks.sh new file mode 100755 index 0000000..6c730ee --- /dev/null +++ b/ctdb/config/debug_locks.sh @@ -0,0 +1,218 @@ +#!/bin/sh + +# This script attempts to find processes holding locks on a particular +# CTDB database and dumps a stack trace for each such processe. +# +# There are 2 cases: +# +# * Samba is configured to use fcntl locks +# +# In this case /proc/locks is parsed to find potential lock holders +# +# * Samba is configured to use POSIX robust mutexes +# +# In this case the helper program tdb_mutex_check is used to find +# potential lock holders. +# +# This helper program uses a private glibc struct field, so is +# neither portable nor supported. If this field is not available +# then the helper is not built. Unexpected changes in internal +# glibc structures may cause unexpected results, including crashes. +# Bug reports for this helper program are not accepted without an +# accompanying patch. + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && echo "$PWD") + +. "${CTDB_BASE}/functions" + +if [ $# -ne 4 ] ; then + die "usage: $0 { DB | RECORD } { FCNTL | MUTEX }" +fi + +lock_helper_pid="$1" +# lock_scope is unused for now +# shellcheck disable=SC2034 +lock_scope="$2" +tdb_path="$3" +lock_type="$4" + +# type is at least mentioned in POSIX and more is portable than which(1) +# shellcheck disable=SC2039 +if ! type gstack >/dev/null 2>&1 ; then + gstack () + { + _pid="$1" + + gdb -batch --quiet -nx "/proc/${_pid}/exe" "$_pid" \ + -ex "thread apply all bt" 2>/dev/null | + grep '^\(#\|Thread \)' + } +fi + +# Load/cache database options from configuration file +ctdb_get_db_options + +dump_stack () +{ + _pid="$1" + + echo "----- Stack trace for PID=${_pid} -----" + _state=$(ps -p "$_pid" -o state= | cut -c 1) + if [ "$_state" = "D" ] ; then + # Don't run gstack on a process in D state since + # gstack will hang until the process exits D state. + # Although it is possible for a process to transition + # to D state after this check, it is unlikely because + # if a process is stuck in D state then it is probably + # the reason why this script was called. Note that a + # kernel stack almost certainly won't help diagnose a + # deadlock... but it will probably give us someone to + # blame! + echo "----- Process in D state, printing kernel stack only" + get_proc "${_pid}/stack" + else + gstack "$_pid" + fi +} + +dump_stacks () +{ + _pids="$1" + + # Use word splitting to squash whitespace + # shellcheck disable=SC2086 + _pids=$(echo $_pids | tr ' ' '\n' | sort -u) + + for _pid in $_pids; do + dump_stack "$_pid" + done +} + +get_tdb_file_id () +{ + if ! _device_inode=$(stat -c "%d:%i" "$tdb_path" 2>/dev/null) ; then + die "Unable to stat \"${tdb_path}\"" + fi + _device="${_device_inode%%:*}" + _device_major=$((_device >> 8)) + _device_minor=$((_device & 0xff)) + _inode="${_device_inode#*:}" + printf '%02x:%02x:%u\n' "$_device_major" "$_device_minor" "$_inode" +} + +debug_via_proc_locks () +{ + # Get file ID to match relevant column in /proc/locks + _file_id=$(get_tdb_file_id) + + # Log information from /proc/locks about the waiting process + _tdb=$(basename "$tdb_path") + _comm=$(ps -p "$lock_helper_pid" -o comm=) + _out=$(get_proc "locks" | + awk -v pid="$lock_helper_pid" \ + -v file_id="$_file_id" \ + -v file="$_tdb" \ + -v comm="$_comm" \ + '$2 == "->" && + $3 == "POSIX" && + $4 == "ADVISORY" && + $5 == "WRITE" && + $6 == pid && + $7 == file_id { print $6, comm, file, $8, $9 }') + if [ -n "$_out" ] ; then + echo "Waiter:" + echo "$_out" + fi + + # Parse /proc/locks and find process holding locks on $tdb_path + # extract following information + # pid process_name tdb_name offsets + _out=$(get_proc "locks" | + awk -v pid="$lock_helper_pid" \ + -v file_id="$_file_id" \ + -v file="$_tdb" \ + '$2 == "POSIX" && + $3 == "ADVISORY" && + $4 == "WRITE" && + $5 != pid && + $6 == file_id { print $5, file, $7, $8 }' | + while read -r _pid _rest ; do + _pname=$(ps -p "$_pid" -o comm=) + echo "$_pid $_pname $_rest" + done) + + if [ -z "$_out" ]; then + return + fi + + # Log information about locks + echo "Lock holders:" + echo "$_out" + + _pids=$(echo "$_out" | awk '{ print $1 }') + + lock_holder_pids="${lock_holder_pids:+${lock_holder_pids} }${_pids}" +} + +debug_via_tdb_mutex () +{ + _helper="${CTDB_HELPER_BINDIR}/tdb_mutex_check" + if [ ! -x "$_helper" ] ; then + # Mutex helper not available - not supported? + # Avoid not found error... + return + fi + + # Helper should always succeed + if ! _t=$("$_helper" "$tdb_path") ; then + return + fi + + _out=$(echo "$_t" | sed -n -e 's#^\[\(.*\)\] pid=\(.*\)#\2 \1#p') + + if [ -z "$_out" ]; then + if [ -n "$_t" ] ; then + echo "$_t" | grep -F 'trylock failed' + fi + return + fi + + # Get process names, append $tdb_path + _out=$(echo "$_out" | + while read -r _pid _rest ; do + _pname=$(ps -p "$_pid" -o comm=) + _tdb=$(basename "$tdb_path") + echo "${_pid} ${_pname} ${_tdb} ${_rest}" + done) + + # Log information about locks + echo "Lock holders:" + echo "$_out" + + # Get PIDs of processes that are holding locks + _pids=$(echo "$_out" | + awk -v pid="$lock_helper_pid" '$1 != pid {print $1}') + + lock_holder_pids="${lock_holder_pids:+${lock_holder_pids} }${_pids}" +} + +( + flock -n 9 || exit 1 + + echo "===== Start of debug locks PID=$$ =====" + + lock_holder_pids="" + + debug_via_proc_locks + + if [ "$lock_type" = "MUTEX" ] ; then + debug_via_tdb_mutex + fi + + dump_stacks "$lock_holder_pids" + + echo "===== End of debug locks PID=$$ =====" +)9>"${CTDB_SCRIPT_VARDIR}/debug_locks.lock" | script_log "ctdbd-lock" + +exit 0 diff --git a/ctdb/config/events/README b/ctdb/config/events/README new file mode 100644 index 0000000..766cb4f --- /dev/null +++ b/ctdb/config/events/README @@ -0,0 +1,193 @@ +The events/ directory contains event scripts used by CTDB. Event +scripts are triggered on certain events, such as startup, monitoring +or public IP allocation. Scripts may be specific to services, +networking or internal CTDB operations. + +Scripts are divided into subdirectories for different CTDB components. +Right now the only component is "legacy". + +All event scripts start with the prefix 'NN.' where N is a digit. The +event scripts are run in sequence based on NN. Thus 10.interface will +be run before 60.nfs. It is recommended to keep each NN unique. +However, scripts with the same NN prefix will be executed in +alphanumeric sort order. + +As a special case, any eventscript that ends with a '~' character will be +ignored since this is a common postfix that some editors will append to +older versions of a file. Similarly, any eventscript with multiple '.'s +will be ignored as package managers can create copies with additional +suffix starting with '.' (e.g. .rpmnew, .dpkg-dist). + +Only executable event scripts are run by CTDB. Any event script that +does not have execute permission is ignored. + +The eventscripts are called with varying number of arguments. The +first argument is the event name and the rest of the arguments depend +on the event name. + +Event scripts must return 0 for success and non-zero for failure. + +Output of event scripts is logged. On failure the output of the +failing event script is included in the output of "ctdb scriptstatus". + +The following events are supported (with arguments shown): + +init + + This event is triggered once when CTDB is starting up. This + event is used to do some basic cleanup and initialisation. + + During the "init" event CTDB is not listening on its Unix + domain socket, so the "ctdb" CLI will not work. + + Failure of this event will cause CTDB to terminate. + + Example: 00.ctdb creates $CTDB_SCRIPT_VARDIR + +setup + + This event is triggered once, after the "init" event has + completed. + + For this and any subsequent events the CTDB Unix domain socket + is available, so the "ctdb" CLI will work. + + Failure of this event will cause CTDB to terminate. + + Example: 00.ctdb processes tunables defined in ctdb.tunables. + +startup + + This event is triggered after the "setup" event has completed + and CTDB has finished its initial database recovery. + + This event starts all services that are managed by CTDB. Each + service that is managed by CTDB should implement this event + and use it to (re)start the service. + + If the "startup" event fails then CTDB will retry it until it + succeeds. There is no limit on the number of retries. + + Example: 50.samba uses this event to start the Samba daemon. + +shutdown + + This event is triggered when CTDB is shutting down. + + This event shuts down all services that are managed by CTDB. + Each service that is managed by CTDB should implement this + event and use it to stop the service. + + Example: 50.samba uses this event to shut down the Samba + daemon. + +monitor + + This event is run periodically. The interval between + successive "monitor" events is configured using the + MonitorInterval tunable, which defaults to 15 seconds. + + This event is triggered by CTDB to continuously monitor that + all managed services are healthy. If all event scripts + complete then the monitor event successfully then the node is + marked HEALTHY. If any event script fails then no subsequent + scripts will be run for that event and the node is marked + UNHEALTHY. + + Each service that is managed by CTDB should implement this + event and use it to monitor the service. + + Example: 10.interface checks that each configured interface + for public IP addresses has a physical link established. + +startrecovery + + This event is triggered every time a database recovery process + is started. + + This is rarely used. + +recovered + + This event is triggered every time a database recovery process + is completed. + + This is rarely used. + +takeip + + This event is triggered for each public IP address taken by a + node during IP address (re)assignment. Multiple "takeip" + events can be run in parallel if multiple IP addresses are + being assigned. + + Example: In 10.interface the "ip" command (from the Linux + iproute2 package) is used to add the specified public IP + address to the specified interface. The "ip" command can + safely be run concurrently. However, the "iptables" command + cannot be run concurrently so a wrapper is used to serialise + runs using exclusive locking. + + If substantial work is required to reconfigure a service when + a public IP address is taken over it can be better to defer + service reconfiguration to the "ipreallocated" event, after + all IP addresses have been assigned. + + Example: 60.nfs uses ctdb_service_set_reconfigure() to flag + that public IP addresses have changed so that service + reconfiguration will occur in the "ipreallocated" event. + +releaseip + + This event is triggered for each public IP address released by + a node during IP address (re)assignment. Multiple "releaseip" + events can be run in parallel if multiple IP addresses are + being unassigned. + + In all other regards, this event is analogous to the "takeip" + event above. + +updateip + + This event is triggered for each public IP address moved + between interfaces on a node during IP address (re)assignment. + Multiple "updateip" events can be run in parallel if multiple + IP addresses are being moved. + + This event is only used if multiple interfaces are capable of + hosting an IP address, as specified in the public addresses + configuration file. + + This event is similar to the "takeip" event above. + +ipreallocated + + This event is triggered on all nodes as the last step of + public IP address (re)assignment. It is unconditionally + triggered after any "releaseip", "takeip" and "updateip" + events, even though these events may not run on some nodes if + there are no relevant changes. That is, the "ipreallocated" + event is triggered unconditionally, even on nodes where public + IP addresses assignements have not changed. + + This event is used to reconfigure services. + + Since "ipreallocated" is always run, this allows + reconfiguration to depend on the states of other nodes rather + that just IP addresses. + + Example: 11.natgw recalculates the NAT gateway master and + updates the relevant network configuration on each node if the + NAT gateway master has changed. + +Additional notes for "takeip", "releaseip", "updateip", +"ipreallocated": + +* Failure of any of these events causes IP allocation to be retried. + +* An event script can use ctdb_service_set_reconfigure() in "takeip", + "releaseip" or "updateip" events to flag that its service needs to + be reconfigured. The "ipreallocated" event can then use + ctdb_service_needs_reconfigure() to test if there were public IPs + changes to determine what type of reconfiguration (if any) is + needed. diff --git a/ctdb/config/events/legacy/00.ctdb.script b/ctdb/config/events/legacy/00.ctdb.script new file mode 100755 index 0000000..81c16af --- /dev/null +++ b/ctdb/config/events/legacy/00.ctdb.script @@ -0,0 +1,130 @@ +#!/bin/sh + +# Event script for ctdb-specific setup and other things that don't fit +# elsewhere. + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +load_script_options + +############################################################ + +# type is commonly supported and more portable than which(1) +# shellcheck disable=SC2039 +select_tdb_checker () +{ + # Find the best TDB consistency check available. + use_tdb_tool_check=false + type tdbtool >/dev/null 2>&1 && found_tdbtool=true + type tdbdump >/dev/null 2>&1 && found_tdbdump=true + + if $found_tdbtool && echo "help" | tdbtool | grep -q check ; then + use_tdb_tool_check=true + elif $found_tdbtool && $found_tdbdump ; then + cat </dev/null | + grep -q "Database integrity is OK" ; then + return 0 + else + return 1 + fi + else + timeout 10 tdbdump "$_db" >/dev/null 2>/dev/null + return $? + fi +} + +check_persistent_databases () +{ + _dir="${CTDB_DBDIR_PERSISTENT:-${CTDB_VARDIR}/persistent}" + [ -d "$_dir" ] || return 0 + + for _db in "$_dir/"*.tdb.*[0-9] ; do + [ -r "$_db" ] || continue + check_tdb "$_db" || \ + die "Persistent database $_db is corrupted! CTDB will not start." + done +} + +check_non_persistent_databases () +{ + _dir="${CTDB_DBDIR:-${CTDB_VARDIR}}" + [ -d "$_dir" ] || return 0 + + for _db in "${_dir}/"*.tdb.*[0-9] ; do + [ -r "$_db" ] || continue + check_tdb "$_db" || { + _backup="${_db}.$(date +'%Y%m%d.%H%M%S').corrupt" + cat <= threshold %d%%\n' \ + "$_thing" \ + "$_usage" \ + "$_unhealthy_threshold" + # Only run unhealthy callout if passing the + # unhealthy threshold. That is, if the + # previous usage was below the threshold. + if [ "$_prev" -lt "$_unhealthy_threshold" ]; then + eval "$_unhealthy_callout" + fi + echo "$_usage" >"$_cache" + exit 1 + fi + fi + + if validate_percentage "$_warn_threshold" "$_thing"; then + if [ "$_usage" -ge "$_warn_threshold" ]; then + if [ "$_usage" = "$_prev" ]; then + return + fi + printf 'WARNING: %s utilization %d%% >= threshold %d%%\n' \ + "$_thing" \ + "$_usage" \ + "$_warn_threshold" + echo "$_usage" >"$_cache" + else + if [ ! -r "$_cache" ]; then + return + fi + printf 'NOTICE: %s utilization %d%% < threshold %d%%\n' \ + "$_thing" \ + "$_usage" \ + "$_warn_threshold" + rm -f "$_cache" + fi + fi +} + +set_monitor_filsystem_usage_defaults() +{ + _fs_defaults_cache="${script_state_dir}/cache_filsystem_usage_defaults" + + if [ ! -r "$_fs_defaults_cache" ]; then + # Determine filesystem for each database directory, generate + # an entry to warn at 90%, de-duplicate entries, put all items + # on 1 line (so the read below gets everything) + for _t in "${CTDB_DBDIR:-${CTDB_VARDIR}}" \ + "${CTDB_DBDIR_PERSISTENT:-${CTDB_VARDIR}/persistent}" \ + "${CTDB_DBDIR_STATE:-${CTDB_VARDIR}/state}"; do + df -kP "$_t" | awk 'NR == 2 { printf "%s:90\n", $6 }' + done | sort -u | xargs >"$_fs_defaults_cache" + fi + + read -r CTDB_MONITOR_FILESYSTEM_USAGE <"$_fs_defaults_cache" +} + +monitor_filesystem_usage() +{ + if [ -z "$CTDB_MONITOR_FILESYSTEM_USAGE" ]; then + set_monitor_filsystem_usage_defaults + fi + + # Check each specified filesystem, specified in format + # :[:fs_unhealthy_threshold] + for _fs in $CTDB_MONITOR_FILESYSTEM_USAGE; do + _fs_mount="${_fs%%:*}" + _fs_thresholds="${_fs#*:}" + + if [ ! -d "$_fs_mount" ]; then + echo "WARNING: Directory ${_fs_mount} does not exist" + continue + fi + + # Get current utilization + _fs_usage=$(df -kP "$_fs_mount" | + sed -n -e 's@.*[[:space:]]\([[:digit:]]*\)%.*@\1@p') + if [ -z "$_fs_usage" ]; then + printf 'WARNING: Unable to get FS utilization for %s\n' \ + "$_fs_mount" + continue + fi + + check_thresholds "Filesystem ${_fs_mount}" \ + "$_fs_thresholds" \ + "$_fs_usage" + done +} + +dump_memory_info() +{ + get_proc "meminfo" + ps auxfww + set_proc "sysrq-trigger" "m" +} + +monitor_memory_usage() +{ + # Defaults + if [ -z "$CTDB_MONITOR_MEMORY_USAGE" ]; then + CTDB_MONITOR_MEMORY_USAGE=80 + fi + + _meminfo=$(get_proc "meminfo") + # Intentional word splitting here + # shellcheck disable=SC2046 + set -- $(echo "$_meminfo" | awk ' +$1 == "MemAvailable:" { memavail += $2 } +$1 == "MemFree:" { memfree += $2 } +$1 == "Cached:" { memfree += $2 } +$1 == "Buffers:" { memfree += $2 } +$1 == "MemTotal:" { memtotal = $2 } +$1 == "SwapFree:" { swapfree = $2 } +$1 == "SwapTotal:" { swaptotal = $2 } +END { + if (memavail != 0) { memfree = memavail ; } + if (memtotal + swaptotal != 0) { + usedtotal = memtotal - memfree + swaptotal - swapfree + print int(usedtotal / (memtotal + swaptotal) * 100) + } else { + print 0 + } +}') + _mem_usage="$1" + + check_thresholds "System memory" \ + "$CTDB_MONITOR_MEMORY_USAGE" \ + "$_mem_usage" \ + dump_memory_info +} + +case "$1" in +monitor) + # Load/cache database options from configuration file + ctdb_get_db_options + + rc=0 + monitor_filesystem_usage || rc=$? + monitor_memory_usage || rc=$? + exit $rc + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/06.nfs.script b/ctdb/config/events/legacy/06.nfs.script new file mode 100755 index 0000000..b937d43 --- /dev/null +++ b/ctdb/config/events/legacy/06.nfs.script @@ -0,0 +1,39 @@ +#!/bin/sh +# script to manage nfs in a clustered environment + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +service_name="nfs" + +load_script_options "service" "60.nfs" + +ctdb_setup_state_dir "service" "$service_name" + +###################################################################### + +nfs_callout_pre () +{ + _event="$1" + shift + + nfs_callout "${_event}-pre" "$@" +} + +###################################################################### + +# script_state_dir set by ctdb_setup_state_dir() +# shellcheck disable=SC2154 +nfs_callout_init "$script_state_dir" + +case "$1" in +takeip) + nfs_callout_pre "$@" + ;; + +releaseip) + nfs_callout_pre "$@" + ;; +esac diff --git a/ctdb/config/events/legacy/10.interface.script b/ctdb/config/events/legacy/10.interface.script new file mode 100755 index 0000000..7dfacd3 --- /dev/null +++ b/ctdb/config/events/legacy/10.interface.script @@ -0,0 +1,262 @@ +#!/bin/sh + +################################# +# interface event script for ctdb +# this adds/removes IPs from your +# public interface + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +load_script_options + +ctdb_public_addresses="${CTDB_BASE}/public_addresses" + +if [ ! -f "$ctdb_public_addresses" ]; then + if [ "$1" = "init" ] ; then + echo "No public addresses file found" + fi + exit 0 +fi + +# This sets $all_interfaces as a side-effect. +get_all_interfaces () +{ + # Get all the interfaces listed in the public_addresses file + all_interfaces=$(sed -e '/^#.*/d' \ + -e 's/^[^\t ]*[\t ]*//' \ + -e 's/,/ /g' \ + -e 's/[\t ]*$//' "$ctdb_public_addresses") + + # Get the interfaces for which CTDB has public IPs configured. + # That is, for all but the 1st line, get the 1st field. + ctdb_ifaces=$($CTDB -X ifaces | sed -e '1d' -e 's@^|@@' -e 's@|.*@@') + + # Add $ctdb_ifaces and make $all_interfaces unique + # Use word splitting to squash whitespace + # shellcheck disable=SC2086 + all_interfaces=$(echo $all_interfaces $ctdb_ifaces | tr ' ' '\n' | sort -u) +} + +monitor_interfaces() +{ + get_all_interfaces + + down_interfaces_found=false + up_interfaces_found=false + + # Note that this loop must not exit early. It must process + # all interfaces so that the correct state for each interface + # is set in CTDB using setifacelink. + for _iface in $all_interfaces ; do + if interface_monitor "$_iface" ; then + up_interfaces_found=true + $CTDB setifacelink "$_iface" up >/dev/null 2>&1 + else + down_interfaces_found=true + $CTDB setifacelink "$_iface" down >/dev/null 2>&1 + fi + done + + if ! $down_interfaces_found ; then + return 0 + fi + + if ! $up_interfaces_found ; then + return 1 + fi + + if [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" != "yes" ]; then + return 1 + fi + + return 0 +} + +# Sets: iface, ip, maskbits +get_iface_ip_maskbits () +{ + _iface_in="$1" + ip="$2" + _maskbits_in="$3" + + # Intentional word splitting here + # shellcheck disable=SC2046 + set -- $(ip_maskbits_iface "$ip") + if [ -n "$1" ] ; then + maskbits="$1" + iface="$2" + + if [ "$iface" != "$_iface_in" ] ; then + printf \ + 'WARNING: Public IP %s hosted on interface %s but VNN says %s\n' \ + "$ip" "$iface" "$_iface_in" + fi + if [ "$maskbits" != "$_maskbits_in" ] ; then + printf \ + 'WARNING: Public IP %s has %s bit netmask but VNN says %s\n' \ + "$ip" "$maskbits" "$_maskbits_in" + fi + else + die "ERROR: Unable to determine interface for IP ${ip}" + fi +} + +ip_block () +{ + _ip="$1" + _iface="$2" + + case "$_ip" in + *:*) _family="inet6" ;; + *) _family="inet" ;; + esac + + # Extra delete copes with previously killed script + iptables_wrapper "$_family" \ + -D INPUT -i "$_iface" -d "$_ip" -j DROP 2>/dev/null + iptables_wrapper "$_family" \ + -I INPUT -i "$_iface" -d "$_ip" -j DROP +} + +ip_unblock () +{ + _ip="$1" + _iface="$2" + + case "$_ip" in + *:*) _family="inet6" ;; + *) _family="inet" ;; + esac + + iptables_wrapper "$_family" \ + -D INPUT -i "$_iface" -d "$_ip" -j DROP 2>/dev/null +} + +ctdb_check_args "$@" + +case "$1" in +init) + # make sure that we only respond to ARP messages from the NIC where + # a particular ip address is associated. + get_proc sys/net/ipv4/conf/all/arp_filter >/dev/null 2>&1 && { + set_proc sys/net/ipv4/conf/all/arp_filter 1 + } + + _promote="sys/net/ipv4/conf/all/promote_secondaries" + get_proc "$_promote" >/dev/null 2>&1 || \ + die "Public IPs only supported if promote_secondaries is available" + + # make sure we drop any ips that might still be held if + # previous instance of ctdb got killed with -9 or similar + drop_all_public_ips + ;; + +startup) + monitor_interfaces + ;; + +shutdown) + drop_all_public_ips + ;; + +takeip) + iface=$2 + ip=$3 + maskbits=$4 + + add_ip_to_iface "$iface" "$ip" "$maskbits" || { + exit 1; + } + + # In case a previous "releaseip" for this IP was killed... + ip_unblock "$ip" "$iface" + + flush_route_cache + ;; + +releaseip) + # releasing an IP is a bit more complex than it seems. Once the IP + # is released, any open tcp connections to that IP on this host will end + # up being stuck. Some of them (such as NFS connections) will be unkillable + # so we need to use the killtcp ctdb function to kill them off. We also + # need to make sure that no new connections get established while we are + # doing this! So what we do is this: + # 1) firewall this IP, so no new external packets arrive for it + # 2) find existing connections, and kill them + # 3) remove the IP from the interface + # 4) remove the firewall rule + shift + get_iface_ip_maskbits "$@" + + ip_block "$ip" "$iface" + + kill_tcp_connections "$iface" "$ip" + + delete_ip_from_iface "$iface" "$ip" "$maskbits" || { + ip_unblock "$ip" "$iface" + exit 1 + } + + ip_unblock "$ip" "$iface" + + flush_route_cache + ;; + +updateip) + # moving an IP is a bit more complex than it seems. + # First we drop all traffic on the old interface. + # Then we try to add the ip to the new interface and before + # we finally remove it from the old interface. + # + # 1) firewall this IP, so no new external packets arrive for it + # 2) remove the IP from the old interface (and new interface, to be sure) + # 3) add the IP to the new interface + # 4) remove the firewall rule + # 5) use ctdb gratarp to propagate the new mac address + # 6) use netstat -tn to find existing connections, and tickle them + _oiface=$2 + niface=$3 + _ip=$4 + _maskbits=$5 + + get_iface_ip_maskbits "$_oiface" "$_ip" "$_maskbits" + oiface="$iface" + + # Could check maskbits too. However, that should never change + # so we want to notice if it does. + if [ "$oiface" = "$niface" ] ; then + echo "Redundant \"updateip\" - ${ip} already on ${niface}" + exit 0 + fi + + ip_block "$ip" "$oiface" + + delete_ip_from_iface "$oiface" "$ip" "$maskbits" 2>/dev/null + delete_ip_from_iface "$niface" "$ip" "$maskbits" 2>/dev/null + + add_ip_to_iface "$niface" "$ip" "$maskbits" || { + ip_unblock "$ip" "$oiface" + exit 1 + } + + ip_unblock "$ip" "$oiface" + + flush_route_cache + + # propagate the new mac address + $CTDB gratarp "$ip" "$niface" + + # tickle all existing connections, so that dropped packets + # are retransmited and the tcp streams work + tickle_tcp_connections "$ip" + ;; + +monitor) + monitor_interfaces || exit 1 + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/11.natgw.script b/ctdb/config/events/legacy/11.natgw.script new file mode 100755 index 0000000..fb93dea --- /dev/null +++ b/ctdb/config/events/legacy/11.natgw.script @@ -0,0 +1,242 @@ +#!/bin/sh +# Script to set up one of the nodes as a NAT gateway for all other nodes. +# This is used to ensure that all nodes in the cluster can still originate +# traffic to the external network even if there are no public addresses +# available. +# + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +service_name="natgw" + +load_script_options + +[ -n "$CTDB_NATGW_NODES" ] || exit 0 +export CTDB_NATGW_NODES + +ctdb_setup_state_dir "failover" "$service_name" + +# script_state_dir set by ctdb_setup_state_dir() +# shellcheck disable=SC2154 +natgw_cfg_new="${script_state_dir}/cfg_new" +natgw_cfg_old="${script_state_dir}/cfg_old" +natgw_leader_old="${script_state_dir}/leader_old" + +ctdb_natgw_follower_only () +{ + _ip_address=$(ctdb_get_ip_address) + + awk -v my_ip="$_ip_address" \ + '$1 == my_ip { if ($2 ~ "follower-only") { exit 0 } else { exit 1 } }' \ + "$CTDB_NATGW_NODES" +} + +natgw_check_config () +{ + [ -r "$CTDB_NATGW_NODES" ] || \ + die "error: CTDB_NATGW_NODES=${CTDB_NATGW_NODES} unreadable" + if ! ctdb_natgw_follower_only ; then + [ -n "$CTDB_NATGW_PUBLIC_IP" ] || \ + die "Invalid configuration: CTDB_NATGW_PUBLIC_IP not set" + [ -n "$CTDB_NATGW_PUBLIC_IFACE" ] || \ + die "Invalid configuration: CTDB_NATGW_PUBLIC_IFACE not set" + fi + [ -n "$CTDB_NATGW_PRIVATE_NETWORK" ] || \ + die "Invalid configuration: CTDB_NATGW_PRIVATE_NETWORK not set" + + # The default is to create a single default route + [ -n "$CTDB_NATGW_STATIC_ROUTES" ] || CTDB_NATGW_STATIC_ROUTES="0.0.0.0/0" +} + +natgw_write_config () +{ + _f="$1" + + cat >"$_f" </dev/null 2>&1 ; then + return 1 + fi + + echo "NAT gateway configuration has changed" + return 0 +} + +_natgw_clear () +{ + _ip="${CTDB_NATGW_PUBLIC_IP%/*}" + _maskbits="${CTDB_NATGW_PUBLIC_IP#*/}" + + delete_ip_from_iface \ + "$CTDB_NATGW_PUBLIC_IFACE" "$_ip" "$_maskbits" >/dev/null 2>&1 + for _net_gw in $CTDB_NATGW_STATIC_ROUTES ; do + _net="${_net_gw%@*}" + ip route del "$_net" metric 10 >/dev/null 2>/dev/null + done + + # Delete the masquerading setup from a previous iteration where we + # were the NAT-GW + iptables -D POSTROUTING -t nat \ + -s "$CTDB_NATGW_PRIVATE_NETWORK" ! -d "$CTDB_NATGW_PRIVATE_NETWORK" \ + -j MASQUERADE >/dev/null 2>/dev/null + + iptables -D INPUT -p tcp --syn -d "${_ip}/32" -j REJECT 2>/dev/null +} + +natgw_clear () +{ + if [ -r "$natgw_cfg_old" ] ; then + (. "$natgw_cfg_old" ; _natgw_clear) + else + _natgw_clear + fi +} + +natgw_set_leader () +{ + set_proc sys/net/ipv4/ip_forward 1 + iptables -A POSTROUTING -t nat \ + -s "$CTDB_NATGW_PRIVATE_NETWORK" ! -d "$CTDB_NATGW_PRIVATE_NETWORK" \ + -j MASQUERADE + + # block all incoming connections to the NATGW IP address + ctdb_natgw_public_ip_host="${CTDB_NATGW_PUBLIC_IP%/*}/32" + iptables -D INPUT -p tcp --syn \ + -d "$ctdb_natgw_public_ip_host" -j REJECT 2>/dev/null + iptables -I INPUT -p tcp --syn \ + -d "$ctdb_natgw_public_ip_host" -j REJECT 2>/dev/null + + ip addr add "$CTDB_NATGW_PUBLIC_IP" dev "$CTDB_NATGW_PUBLIC_IFACE" + for _net_gw in $CTDB_NATGW_STATIC_ROUTES ; do + _net="${_net_gw%@*}" + if [ "$_net" != "$_net_gw" ] ; then + _gw="${_net_gw#*@}" + else + _gw="$CTDB_NATGW_DEFAULT_GATEWAY" + fi + + [ -n "$_gw" ] || continue + ip route add "$_net" metric 10 via "$_gw" + done +} + +natgw_set_follower () +{ + _natgwip="$1" + + for _net_gw in $CTDB_NATGW_STATIC_ROUTES ; do + _net="${_net_gw%@*}" + ip route add "$_net" via "$_natgwip" metric 10 + done +} + +natgw_ensure_leader () +{ + # Intentional word splitting here + # shellcheck disable=SC2046 + set -- $("${CTDB_HELPER_BINDIR}/ctdb_natgw" leader) + natgwleader="${1:--1}" # Default is -1, for failure above + natgwip="$2" + + if [ "$natgwleader" = "-1" ]; then + # Fail... + die "There is no NATGW leader node" + fi +} + +natgw_leader_has_changed () +{ + if [ -r "$natgw_leader_old" ] ; then + read _old_natgwleader <"$natgw_leader_old" + else + _old_natgwleader="" + fi + [ "$_old_natgwleader" != "$natgwleader" ] +} + +natgw_save_state () +{ + echo "$natgwleader" >"$natgw_leader_old" + # Created by natgw_config_has_changed() + mv "$natgw_cfg_new" "$natgw_cfg_old" +} + + +case "$1" in +setup) + natgw_check_config + ;; + +startup) + natgw_check_config + + # Error if CTDB_NATGW_PUBLIC_IP is listed in public addresses + ip_pat=$(echo "$CTDB_NATGW_PUBLIC_IP" | sed -e 's@\.@\\.@g') + ctdb_public_addresses="${CTDB_BASE}/public_addresses" + if grep -q "^${ip_pat}[[:space:]]" "$ctdb_public_addresses" ; then + die "ERROR: CTDB_NATGW_PUBLIC_IP same as a public address" + fi + + # do not send out arp requests from loopback addresses + set_proc sys/net/ipv4/conf/all/arp_announce 2 + ;; + +updatenatgw|ipreallocated) + natgw_check_config + + natgw_ensure_leader + + natgw_config_has_changed || natgw_leader_has_changed || exit 0 + + natgw_clear + + pnn=$(ctdb_get_pnn) + if [ "$pnn" = "$natgwleader" ]; then + natgw_set_leader + else + natgw_set_follower "$natgwip" + fi + + # flush our route cache + set_proc sys/net/ipv4/route/flush 1 + + # Only update saved state when NATGW successfully updated + natgw_save_state + ;; + +shutdown|removenatgw) + natgw_check_config + natgw_clear + ;; + +monitor) + natgw_check_config + + if [ -n "$CTDB_NATGW_PUBLIC_IFACE" ] ; then + interface_monitor "$CTDB_NATGW_PUBLIC_IFACE" || exit 1 + fi + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/11.routing.script b/ctdb/config/events/legacy/11.routing.script new file mode 100755 index 0000000..7ba7f3b --- /dev/null +++ b/ctdb/config/events/legacy/11.routing.script @@ -0,0 +1,49 @@ +#!/bin/sh + +# Attempt to add a set of static routes. +# +# Do this in "ipreallocated" rather than just "startup" because some +# of the routes might be missing because the corresponding interface +# has not previously had any IPs assigned or IPs were previously +# released and corresponding routes were dropped. +# +# Addition of some routes might fail, errors go to /dev/null. +# +# Routes to add are defined in $CTDB_BASE/static-routes. Syntax is: +# +# IFACE NET/MASK GATEWAY +# +# Example: +# +# bond1 10.3.3.0/24 10.0.0.1 + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +load_script_options + +[ -f "${CTDB_BASE}/static-routes" ] || { + exit 0 +} + +case "$1" in +ipreallocated) + while read iface dest gw; do + ip route add "$dest" via "$gw" dev "$iface" >/dev/null 2>&1 + done <"${CTDB_BASE}/static-routes" + ;; + +updateip) + oiface=$2 + niface=$3 + while read iface dest gw; do + if [ "$niface" = "$iface" ] || [ "$oiface" = "$iface" ] ; then + ip route add "$dest" via "$gw" dev "$iface" >/dev/null 2>&1 + fi + done <"${CTDB_BASE}/static-routes" + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/13.per_ip_routing.script b/ctdb/config/events/legacy/13.per_ip_routing.script new file mode 100755 index 0000000..d7949c6 --- /dev/null +++ b/ctdb/config/events/legacy/13.per_ip_routing.script @@ -0,0 +1,438 @@ +#!/bin/sh + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +load_script_options + +service_name="per_ip_routing" + +# Do nothing if unconfigured +[ -n "$CTDB_PER_IP_ROUTING_CONF" ] || exit 0 + +table_id_prefix="ctdb." + +[ -n "$CTDB_PER_IP_ROUTING_RULE_PREF" ] || \ + die "error: CTDB_PER_IP_ROUTING_RULE_PREF not configured" + +[ "$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" -lt "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] 2>/dev/null || \ + die "error: CTDB_PER_IP_ROUTING_TABLE_ID_LOW[$CTDB_PER_IP_ROUTING_TABLE_ID_LOW] and/or CTDB_PER_IP_ROUTING_TABLE_ID_HIGH[$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH] improperly configured" + +if [ "$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" -le 253 ] && \ + [ 255 -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] ; then + die "error: range CTDB_PER_IP_ROUTING_TABLE_ID_LOW[$CTDB_PER_IP_ROUTING_TABLE_ID_LOW]..CTDB_PER_IP_ROUTING_TABLE_ID_HIGH[$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH] must not include 253-255" +fi + +have_link_local_config () +{ + [ "$CTDB_PER_IP_ROUTING_CONF" = "__auto_link_local__" ] +} + +if ! have_link_local_config && [ ! -r "$CTDB_PER_IP_ROUTING_CONF" ] ; then + die "error: CTDB_PER_IP_ROUTING_CONF=$CTDB_PER_IP_ROUTING_CONF file not found" +fi + +ctdb_setup_state_dir "failover" "$service_name" + +###################################################################### + +ipv4_is_valid_addr() +{ + _ip="$1" + + _count=0 + # Get the shell to break up the address into 1 word per octet + # Intentional word splitting here + # shellcheck disable=SC2086 + for _o in $(export IFS="." ; echo $_ip) ; do + # The 2>/dev/null stops output from failures where an "octet" + # is not numeric. The test will still fail. + if ! [ 0 -le $_o ] && [ $_o -le 255 ] 2>/dev/null ; then + return 1 + fi + _count=$((_count + 1)) + done + + # A valid IPv4 address has 4 octets + [ $_count -eq 4 ] +} + +ensure_ipv4_is_valid_addr () +{ + _event="$1" + _ip="$2" + + ipv4_is_valid_addr "$_ip" || { + echo "$0: $_event not an ipv4 address skipping IP:$_ip" + exit 0 + } +} + +ipv4_host_addr_to_net () +{ + _host="$1" + _maskbits="$2" + + # Convert the host address to an unsigned long by splitting out + # the octets and doing the math. + _host_ul=0 + # Intentional word splitting here + # shellcheck disable=SC2086 + for _o in $(export IFS="." ; echo $_host) ; do + _host_ul=$(( (_host_ul << 8) + _o)) # work around Emacs color bug + done + + # Calculate the mask and apply it. + _mask_ul=$(( 0xffffffff << (32 - _maskbits) )) + _net_ul=$(( _host_ul & _mask_ul )) + + # Now convert to a network address one byte at a time. + _net="" + for _o in $(seq 1 4) ; do + _net="$((_net_ul & 255))${_net:+.}${_net}" + _net_ul=$((_net_ul >> 8)) + done + + echo "${_net}/${_maskbits}" +} + +###################################################################### + +ensure_rt_tables () +{ + rt_tables="$CTDB_SYS_ETCDIR/iproute2/rt_tables" + # script_state_dir set by ctdb_setup_state_dir() + # shellcheck disable=SC2154 + rt_tables_lock="${script_state_dir}/rt_tables_lock" + + # This file should always exist. Even if this didn't exist on the + # system, adding a route will have created it. What if we startup + # and immediately shutdown? Let's be sure. + if [ ! -f "$rt_tables" ] ; then + mkdir -p "${rt_tables%/*}" # dirname + touch "$rt_tables" + fi +} + +# Setup a table id to use for the given IP. We don't need to know it, +# it just needs to exist in /etc/iproute2/rt_tables. Fail if no free +# table id could be found in the configured range. +ensure_table_id_for_ip () +{ + _ip=$1 + + ensure_rt_tables + + # Maintain a table id for each IP address we've ever seen in + # rt_tables. We use a "ctdb." prefix on the label. + _label="${table_id_prefix}${_ip}" + + # This finds either the table id corresponding to the label or a + # new unused one (that is greater than all the used ones in the + # range). + ( + # Note that die() just gets us out of the subshell... + flock --timeout 30 9 || \ + die "ensure_table_id_for_ip: failed to lock file $rt_tables" + + _new="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" + while read _t _l ; do + # Skip comments + case "$_t" in + \#*) continue ;; + esac + # Found existing: done + if [ "$_l" = "$_label" ] ; then + return 0 + fi + # Potentially update the new table id to be used. The + # redirect stops error spam for a non-numeric value. + if [ "$_new" -le "$_t" ] && \ + [ "$_t" -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] \ + 2>/dev/null ; then + _new=$((_t + 1)) + fi + done <"$rt_tables" + + # If the new table id is legal then add it to the file and + # print it. + if [ "$_new" -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] ; then + printf '%d\t%s\n' "$_new" "$_label" >>"$rt_tables" + return 0 + else + return 1 + fi + ) 9>"$rt_tables_lock" +} + +# Clean up all the table ids that we might own. +clean_up_table_ids () +{ + ensure_rt_tables + + ( + # Note that die() just gets us out of the subshell... + flock --timeout 30 9 || \ + die "clean_up_table_ids: failed to lock file $rt_tables" + + # Delete any items from the file that have a table id in our + # range or a label matching our label. Preserve comments. + _tmp="${rt_tables}.$$.ctdb" + awk -v min="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" \ + -v max="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" \ + -v pre="$table_id_prefix" \ + '/^#/ || + !(min <= $1 && $1 <= max) && + !(index($2, pre) == 1) { + print $0 }' "$rt_tables" >"$_tmp" + + mv "$_tmp" "$rt_tables" + ) 9>"$rt_tables_lock" +} + +###################################################################### + +# This prints the config for an IP, which is either relevant entries +# from the config file or, if set to the magic link local value, some +# link local routing config for the IP. +get_config_for_ip () +{ + _ip="$1" + + if have_link_local_config ; then + # When parsing public_addresses also split on '/'. This means + # that we get the maskbits as item #2 without further parsing. + while IFS="/$IFS" read _i _maskbits _x ; do + if [ "$_ip" = "$_i" ] ; then + printf "%s" "$_ip "; ipv4_host_addr_to_net "$_ip" "$_maskbits" + fi + done <"${CTDB_BASE}/public_addresses" + else + while read _i _rest ; do + if [ "$_ip" = "$_i" ] ; then + printf '%s\t%s\n' "$_ip" "$_rest" + fi + done <"$CTDB_PER_IP_ROUTING_CONF" + fi +} + +ip_has_configuration () +{ + _ip="$1" + + _conf=$(get_config_for_ip "$_ip") + [ -n "$_conf" ] +} + +add_routing_for_ip () +{ + _iface="$1" + _ip="$2" + + # Do nothing if no config for this IP. + ip_has_configuration "$_ip" || return 0 + + ensure_table_id_for_ip "$_ip" || \ + die "add_routing_for_ip: out of table ids in range $CTDB_PER_IP_ROUTING_TABLE_ID_LOW - $CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" + + _pref="$CTDB_PER_IP_ROUTING_RULE_PREF" + _table_id="${table_id_prefix}${_ip}" + + del_routing_for_ip "$_ip" >/dev/null 2>&1 + + ip rule add from "$_ip" pref "$_pref" table "$_table_id" || \ + die "add_routing_for_ip: failed to add rule for $_ip" + + # Add routes to table for any lines matching the IP. + get_config_for_ip "$_ip" | + while read _i _dest _gw ; do + _r="$_dest ${_gw:+via} $_gw dev $_iface table $_table_id" + # Intentionally unquoted multi-word value here + # shellcheck disable=SC2086 + ip route add $_r || \ + die "add_routing_for_ip: failed to add route: $_r" + done +} + +del_routing_for_ip () +{ + _ip="$1" + + _pref="$CTDB_PER_IP_ROUTING_RULE_PREF" + _table_id="${table_id_prefix}${_ip}" + + # Do this unconditionally since we own any matching table ids. + # However, print a meaningful message if something goes wrong. + _cmd="ip rule del from $_ip pref $_pref table $_table_id" + _out=$($_cmd 2>&1) || \ + cat <&1 | sed -e 's@^.@ &@' +} + +###################################################################### + +flush_rules_and_routes () +{ + ip rule show | + while read _p _x _i _x _t ; do + # Remove trailing colon after priority/preference. + _p="${_p%:}" + # Only remove rules that match our priority/preference. + [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue + + echo "Removing ip rule for public address $_i for routing table $_t" + ip rule del from "$_i" table "$_t" pref "$_p" + ip route flush table "$_t" 2>/dev/null + done +} + +# Add any missing routes. Some might have gone missing if, for +# example, all IPs on the network were removed (possibly if the +# primary was removed). If $1 is "force" then (re-)add all the +# routes. +add_missing_routes () +{ + $CTDB ip -v -X | { + read _x # skip header line + + # Read the rest of the lines. We're only interested in the + # "IP" and "ActiveInterface" columns. The latter is only set + # for addresses local to this node, making it easy to skip + # non-local addresses. For each IP local address we check if + # the relevant routing table is populated and populate it if + # not. + while IFS="|" read _x _ip _x _iface _x ; do + [ -n "$_iface" ] || continue + + _table_id="${table_id_prefix}${_ip}" + if [ -z "$(ip route show table "$_table_id" 2>/dev/null)" ] || \ + [ "$1" = "force" ] ; then + add_routing_for_ip "$_iface" "$_ip" + fi + done + } || exit $? +} + +# Remove rules/routes for addresses that we're not hosting. If a +# releaseip event failed in an earlier script then we might not have +# had a chance to remove the corresponding rules/routes. +remove_bogus_routes () +{ + # Get a IPs current hosted by this node, each anchored with '@'. + _ips=$($CTDB ip -v -X | awk -F'|' 'NR > 1 && $4 != "" {printf "@%s@\n", $2}') + + # x is intentionally ignored + # shellcheck disable=SC2034 + ip rule show | + while read _p _x _i _x _t ; do + # Remove trailing colon after priority/preference. + _p="${_p%:}" + # Only remove rules that match our priority/preference. + [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue + # Only remove rules for which we don't have an IP. This could + # be done with grep, but let's do it with shell prefix removal + # to avoid unnecessary processes. This falls through if + # "@${_i}@" isn't present in $_ips. + [ "$_ips" = "${_ips#*@"${_i}"@}" ] || continue + + echo "Removing ip rule/routes for unhosted public address $_i" + del_routing_for_ip "$_i" + done +} + +###################################################################### + +ctdb_check_args "$@" + +case "$1" in +startup) + flush_rules_and_routes + + # make sure that we only respond to ARP messages from the NIC + # where a particular ip address is associated. + get_proc sys/net/ipv4/conf/all/arp_filter >/dev/null 2>&1 && { + set_proc sys/net/ipv4/conf/all/arp_filter 1 + } + ;; + +shutdown) + flush_rules_and_routes + clean_up_table_ids + ;; + +takeip) + iface=$2 + ip=$3 + # maskbits included here so argument order is obvious + # shellcheck disable=SC2034 + maskbits=$4 + + ensure_ipv4_is_valid_addr "$1" "$ip" + add_routing_for_ip "$iface" "$ip" + + # flush our route cache + set_proc sys/net/ipv4/route/flush 1 + + $CTDB gratarp "$ip" "$iface" + ;; + +updateip) + # oiface, maskbits included here so argument order is obvious + # shellcheck disable=SC2034 + oiface=$2 + niface=$3 + ip=$4 + # shellcheck disable=SC2034 + maskbits=$5 + + ensure_ipv4_is_valid_addr "$1" "$ip" + add_routing_for_ip "$niface" "$ip" + + # flush our route cache + set_proc sys/net/ipv4/route/flush 1 + + $CTDB gratarp "$ip" "$niface" + tickle_tcp_connections "$ip" + ;; + +releaseip) + iface=$2 + ip=$3 + # maskbits included here so argument order is obvious + # shellcheck disable=SC2034 + maskbits=$4 + + ensure_ipv4_is_valid_addr "$1" "$ip" + del_routing_for_ip "$ip" + ;; + +ipreallocated) + add_missing_routes + remove_bogus_routes + ;; + +reconfigure) + echo "Reconfiguring service \"${service_name}\"..." + + add_missing_routes "force" + remove_bogus_routes + + # flush our route cache + set_proc sys/net/ipv4/route/flush 1 + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/20.multipathd.script b/ctdb/config/events/legacy/20.multipathd.script new file mode 100755 index 0000000..a420251 --- /dev/null +++ b/ctdb/config/events/legacy/20.multipathd.script @@ -0,0 +1,83 @@ +#!/bin/sh +# ctdb event script for monitoring the multipath daemon +# +# Configure monitporing of multipath devices by listing the device serials +# in /etc/ctdb/multipathd : +# CTDB_MONITOR_MPDEVICES="device1 device2 ..." +# + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +service_name="multipathd" + +load_script_options + +[ -n "$CTDB_MONITOR_MPDEVICES" ] || exit 0 + +ctdb_setup_state_dir "service" "$service_name" + +# script_state_dir set by ctdb_setup_state_dir() +# shellcheck disable=SC2154 +multipath_fail="${script_state_dir}/fail" + +multipathd_check_background() +{ + for _device in $CTDB_MONITOR_MPDEVICES; do + # Check multipath knows about the device + _out=$(multipath -ll "$_device") + if [ -z "$_out" ] ; then + echo "ERROR: device \"${_device}\" not known to multipathd" \ + >"$multipath_fail" + exit 1 + fi + + # Check for at least 1 active path + if ! echo "$_out" | grep 'prio=.* status=active' >/dev/null 2>&1 ; then + echo "ERROR: multipath device \"${_device}\" has no active paths" \ + >"$multipath_fail" + exit 1 + fi + done + exit 0 +} + +multipathd_check() +{ + # Run the actual check in the background since the call to + # multipath may block + multipathd_check_background /dev/null 2>&1 & + _pid="$!" + _timeleft=10 + + while [ $_timeleft -gt 0 ]; do + _timeleft=$((_timeleft - 1)) + + # see if the process still exists + kill -0 $_pid >/dev/null 2>&1 || { + if wait $_pid ; then + return 0 + else + cat "$multipath_fail" + rm -f "$multipath_fail" + return 1 + fi + } + sleep 1 + done + + echo "ERROR: callout to multipath checks hung" + # If hung then this probably won't work, but worth trying... + kill -9 $_pid >/dev/null 2>&1 + return 1 +} + +case "$1" in +monitor) + multipathd_check || die "multipath monitoring failed" + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/31.clamd.script b/ctdb/config/events/legacy/31.clamd.script new file mode 100755 index 0000000..5d60fe3 --- /dev/null +++ b/ctdb/config/events/legacy/31.clamd.script @@ -0,0 +1,37 @@ +#!/bin/sh +# event script to manage clamd in a cluster environment + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +detect_init_style + +case $CTDB_INIT_STYLE in +redhat) + service_name="clamd" + ;; +*) + service_name="clamav" + ;; +esac + +load_script_options + +case "$1" in +startup) + service "$service_name" stop > /dev/null 2>&1 + service "$service_name" start || exit $? + ;; + +shutdown) + service "$service_name"_stop + ;; + +monitor) + ctdb_check_unix_socket "$CTDB_CLAMD_SOCKET" || exit $? + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/40.vsftpd.script b/ctdb/config/events/legacy/40.vsftpd.script new file mode 100755 index 0000000..19d4007 --- /dev/null +++ b/ctdb/config/events/legacy/40.vsftpd.script @@ -0,0 +1,56 @@ +#!/bin/sh +# event strict to manage vsftpd in a cluster environment + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +service_name="vsftpd" + +service_reconfigure () +{ + service $service_name restart +} + +load_script_options + +ctdb_setup_state_dir "service" "$service_name" + +case "$1" in +startup) + service "$service_name" stop > /dev/null 2>&1 + service "$service_name" start + ctdb_counter_init + ;; + +shutdown) + service "$service_name" stop + ;; + +takeip|releaseip) + ctdb_service_set_reconfigure + ;; + +ipreallocated) + if ctdb_service_needs_reconfigure ; then + ctdb_service_reconfigure + fi + ;; + +monitor) + if ctdb_check_tcp_ports 21 ; then + ctdb_counter_init + else + ctdb_counter_incr + num_fails=$(ctdb_counter_get) + if [ "$num_fails" -ge 2 ] ; then + die "ERROR: ${num_fails} consecutive failures for vsftpd, marking node unhealthy" + elif [ "$num_fails" -eq 1 ] ; then + echo "WARNING: vsftpd not listening but less than 2 consecutive failures, not unhealthy yet" + fi + fi + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/41.httpd.script b/ctdb/config/events/legacy/41.httpd.script new file mode 100755 index 0000000..dd90aed --- /dev/null +++ b/ctdb/config/events/legacy/41.httpd.script @@ -0,0 +1,78 @@ +#!/bin/sh +# event script to manage httpd in a cluster environment + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +detect_init_style + +case $CTDB_INIT_STYLE in +redhat) + service_name="httpd" + ;; +suse|debian|*) + service_name="apache2" + ;; +esac + +load_script_options + +ctdb_setup_state_dir "service" "$service_name" + +# RHEL5 sometimes use a SIGKILL to terminate httpd, which then leaks +# semaphores. This is a hack to clean them up. +cleanup_httpd_semaphore_leak() { + killall -q -0 "$service_name" || + for i in $(ipcs -s | awk '$3 == "apache" { print $2 }') ; do + ipcrm -s "$i" + done +} + +########## + +service_start () +{ + cleanup_httpd_semaphore_leak + service $service_name start +} +service_stop () +{ + service $service_name stop + killall -q -9 $service_name || true +} + +case "$1" in +startup) + service_start + ctdb_counter_init + ;; + +shutdown) + service_stop + ;; + +monitor) + if ctdb_check_tcp_ports 80 >/dev/null 2>/dev/null ; then + ctdb_counter_init + else + ctdb_counter_incr + num_fails=$(ctdb_counter_get) + if [ "$num_fails" -eq 2 ] ; then + echo "HTTPD is not running. Trying to restart HTTPD." + service_stop + service_start + exit 0 + elif [ "$num_fails" -ge 5 ] ; then + echo "HTTPD is not running. Trying to restart HTTPD." + service_stop + service_start + exit 1 + fi + fi + ;; +esac + +exit 0 + diff --git a/ctdb/config/events/legacy/48.netbios.script b/ctdb/config/events/legacy/48.netbios.script new file mode 100755 index 0000000..4320447 --- /dev/null +++ b/ctdb/config/events/legacy/48.netbios.script @@ -0,0 +1,64 @@ +#!/bin/sh +# ctdb event script for Netbios Name Services + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +detect_init_style + +case $CTDB_INIT_STYLE in + suse) + CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb} + ;; + debian) + CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmbd} + ;; + *) + # Use redhat style as default: + CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb} + ;; +esac + +service_name="netbios" + +load_script_options + +ctdb_setup_state_dir "service" "$service_name" + +service_start () +{ + # make sure nmbd is not already started + service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1 + killall -0 -q nmbd && { + sleep 1 + # make absolutely sure nmbd is dead + killall -q -9 nmbd + } + + # start Samba nmbd service. Start it reniced, as under very heavy load + # the number of smbd processes will mean that it leaves few cycles + # for anything else + nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd" +} + +service_stop () +{ + service "$CTDB_SERVICE_NMB" stop +} + +########################### + +case "$1" in +startup) + service_start + ;; + +shutdown) + service_stop + ;; + +esac + +exit 0 diff --git a/ctdb/config/events/legacy/49.winbind.script b/ctdb/config/events/legacy/49.winbind.script new file mode 100755 index 0000000..852b541 --- /dev/null +++ b/ctdb/config/events/legacy/49.winbind.script @@ -0,0 +1,55 @@ +#!/bin/sh +# ctdb event script for winbind + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind} + +# service_name is used by various functions +# shellcheck disable=SC2034 +service_name="winbind" + +load_script_options + +service_start () +{ + service "$CTDB_SERVICE_WINBIND" stop >/dev/null 2>&1 + killall -0 -q winbindd && { + sleep 1 + # make absolutely sure winbindd is dead + killall -q -9 winbindd + } + + service "$CTDB_SERVICE_WINBIND" start || \ + die "Failed to start winbind" +} + +service_stop () +{ + service "$CTDB_SERVICE_WINBIND" stop +} + +########################### + +case "$1" in +startup) + service_start + ;; + +shutdown) + service_stop + ;; + +monitor) + if ! out=$(wbinfo -p 2>&1) ; then + echo "ERROR: wbinfo -p returned error" + echo "$out" + exit 1 + fi + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/50.samba.script b/ctdb/config/events/legacy/50.samba.script new file mode 100755 index 0000000..81c6e7f --- /dev/null +++ b/ctdb/config/events/legacy/50.samba.script @@ -0,0 +1,163 @@ +#!/bin/sh +# ctdb event script for Samba + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +detect_init_style + +case $CTDB_INIT_STYLE in + suse) + CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb} + ;; + debian) + CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smbd} + ;; + *) + # Use redhat style as default: + CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb} + ;; +esac + +service_name="samba" + +load_script_options + +ctdb_setup_state_dir "service" "$service_name" + +service_start () +{ + # make sure samba is not already started + service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1 + killall -0 -q smbd && { + sleep 1 + # make absolutely sure samba is dead + killall -q -9 smbd + } + # start Samba service. Start it reniced, as under very heavy load + # the number of smbd processes will mean that it leaves few cycles + # for anything else + nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba" +} + +service_stop () +{ + service "$CTDB_SERVICE_SMB" stop + program_stack_traces "smbd" 5 +} + +###################################################################### +# Show the testparm output using a cached smb.conf to avoid delays due +# to registry access. + +# script_state_dir set by ctdb_setup_state_dir() +# shellcheck disable=SC2154 +smbconf_cache="$script_state_dir/smb.conf.cache" + +testparm_foreground_update () +{ + _timeout="$1" + + # No need to remove these temporary files, since there are only 2 + # of them. + _out="${smbconf_cache}.out" + _err="${smbconf_cache}.err" + + timeout "$_timeout" testparm -v -s >"$_out" 2>"$_err" + case $? in + 0) : ;; + 124) + if [ -f "$smbconf_cache" ] ; then + echo "WARNING: smb.conf cache update timed out - using old cache file" + return 1 + else + echo "ERROR: smb.conf cache create failed - testparm command timed out" + exit 1 + fi + ;; + *) + if [ -f "$smbconf_cache" ] ; then + echo "WARNING: smb.conf cache update failed - using old cache file" + cat "$_err" + return 1 + else + echo "ERROR: smb.conf cache create failed - testparm failed with:" + cat "$_err" + exit 1 + fi + esac + + # Only using $$ here to avoid a collision. This is written into + # CTDB's own state directory so there is no real need for a secure + # temporary file. + _tmpfile="${smbconf_cache}.$$" + # Patterns to exclude... + _pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+=' + grep -Ev "$_pat" <"$_out" >"$_tmpfile" + mv "$_tmpfile" "$smbconf_cache" # atomic + + return 0 +} + +testparm_background_update () +{ + _timeout="$1" + + testparm_foreground_update "$_timeout" >/dev/null 2>&1 /dev/null +} + +list_samba_shares () +{ + testparm_cat | + sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' | + sed -e 's/"//g' +} + +list_samba_ports () +{ + testparm_cat --parameter-name="smb ports" | + sed -e 's@,@ @g' +} + +########################### + +case "$1" in +startup) + service_start + ;; + +shutdown) + service_stop + ;; + +monitor) + testparm_foreground_update 10 + ret=$? + + smb_ports="$CTDB_SAMBA_CHECK_PORTS" + if [ -z "$smb_ports" ] ; then + smb_ports=$(list_samba_ports) + [ -n "$smb_ports" ] || die "Failed to set smb ports" + fi + # Intentionally unquoted multi-word value here + # shellcheck disable=SC2086 + ctdb_check_tcp_ports $smb_ports || exit $? + + if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then + list_samba_shares | ctdb_check_directories || exit $? + fi + + if [ $ret -ne 0 ] ; then + testparm_background_update 10 + fi + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/60.nfs.script b/ctdb/config/events/legacy/60.nfs.script new file mode 100755 index 0000000..8e496f7 --- /dev/null +++ b/ctdb/config/events/legacy/60.nfs.script @@ -0,0 +1,299 @@ +#!/bin/sh +# script to manage nfs in a clustered environment + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +service_name="nfs" + +load_system_config "nfs" + +load_script_options + +ctdb_setup_state_dir "service" "$service_name" + +###################################################################### + +service_reconfigure () +{ + # Restart lock manager, notify clients + if [ -x "${CTDB_BASE}/statd-callout" ] ; then + "${CTDB_BASE}/statd-callout" notify & + fi >/dev/null 2>&1 +} + +###################################################################### + +###################################################### +# Check the health of NFS services +# +# Use .check files in $CTDB_NFS_CHECKS_DIR. +# Default is "${CTDB_BASE}/nfs-checks.d/" +###################################################### +nfs_check_services () +{ + _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}" + + # Files must end with .check - avoids editor backups, RPM fu, ... + for _f in "$_dir"/[0-9][0-9].*.check ; do + [ -r "$_f" ] || continue + + _t="${_f%.check}" + _progname="${_t##*/[0-9][0-9].}" + + nfs_check_service "$_progname" <"$_f" + done +} + +###################################################### +# Check the health of an NFS service +# +# $1 - progname, passed to rpcinfo (looked up in /etc/rpc) +# +# Reads variables from stdin +# +# Variables are: +# +# * family - "tcp" or "udp" or space separated list +# default: tcp, not used with "service_check_cmd" +# * version - optional, RPC service version number +# default is to omit to check for any version, +# not used with "service_check_cmd" +# * unhealthy_after - number of check fails before unhealthy +# default: 1 +# * restart_every - number of check fails before restart +# default: 0, meaning no restart +# * service_stop_cmd - command to stop service +# default: no default, must be provided if +# restart_every > 0 +# * service_start_cmd - command to start service +# default: no default, must be provided if +# restart_every > 0 +# * service_check_cmd - command to check health of service +# default is to check RPC service using rpcinfo +# * service_debug_cmd - command to debug a service after trying to stop it; +# for example, it can be useful to print stack +# traces of threads that have not exited, since +# they may be stuck doing I/O; +# no default, see also function program_stack_traces() +# +# Quoting in values is not preserved +# +###################################################### +nfs_check_service () +{ + _progname="$1" + + # This sub-shell is created to intentionally limit the scope of + # variable values read from the .check files. + # shellcheck disable=SC2030 + ( + # Subshell to restrict scope variables... + + # Defaults + family="tcp" + version="" + unhealthy_after=1 + restart_every=0 + service_stop_cmd="" + service_start_cmd="" + service_check_cmd="" + service_debug_cmd="" + + # Eval line-by-line. Expands variable references in values. + # Also allows variable name checking, which seems useful. + while read _line ; do + case "$_line" in + \#*|"") : ;; # Ignore comments, blank lines + + family=*|version=*|\ + unhealthy_after=*|restart_every=*|\ + service_stop_cmd=*|service_start_cmd=*|\ + service_check_cmd=*|service_debug_cmd=*) + + eval "$_line" + ;; + *) + echo "ERROR: Unknown variable for ${_progname}: ${_line}" + exit 1 + esac + done + + _ok=false + if [ -n "$service_check_cmd" ] ; then + # Using eval means variables can contain semicolon separated commands + if eval "$service_check_cmd" ; then + _ok=true + else + _err="monitoring service \"${_progname}\" failed" + fi + else + if nfs_check_rpcinfo \ + "$_progname" "$version" "$family" >/dev/null ; then + _ok=true + else + _err="$ctdb_check_rpc_out" + fi + fi + + if $_ok ; then + if [ $unhealthy_after -ne 1 ] || [ $restart_every -ne 0 ] ; then + ctdb_counter_init "$_progname" + fi + exit 0 + fi + + ctdb_counter_incr "$_progname" + _failcount=$(ctdb_counter_get "$_progname") + + _unhealthy=false + if [ "$unhealthy_after" -gt 0 ] ; then + if [ "$_failcount" -ge "$unhealthy_after" ] ; then + _unhealthy=true + echo "ERROR: $_err" + fi + fi + + if [ "$restart_every" -gt 0 ] ; then + if [ $((_failcount % restart_every)) -eq 0 ] ; then + if ! $_unhealthy ; then + echo "WARNING: $_err" + fi + nfs_restart_service + fi + fi + + if $_unhealthy ; then + exit 1 + fi + + return 0 + ) || exit 1 +} + +# Uses: service_stop_cmd, service_start_cmd, service_debug_cmd +# This function is called within the sub-shell that shellcheck thinks +# loses the above variable values. +# shellcheck disable=SC2031 +nfs_restart_service () +{ + if [ -z "$service_stop_cmd" ] || [ -z "$service_start_cmd" ] ; then + die "ERROR: Can not restart service \"${_progname}\" without corresponding service_start_cmd/service_stop_cmd settings" + fi + + echo "Trying to restart service \"${_progname}\"..." + # Using eval means variables can contain semicolon separated commands + eval "$service_stop_cmd" + if [ -n "$service_debug_cmd" ] ; then + eval "$service_debug_cmd" + fi + background_with_logging eval "$service_start_cmd" +} + +###################################################### +# Check an RPC service with rpcinfo +###################################################### +ctdb_check_rpc () +{ + _progname="$1" # passed to rpcinfo (looked up in /etc/rpc) + _version="$2" # optional, not passed if empty/unset + _family="${3:-tcp}" # optional, default is "tcp" + + case "$_family" in + tcp6|udp6) + _localhost="${CTDB_RPCINFO_LOCALHOST6:-::1}" + ;; + *) + _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}" + esac + + # $_version is not quoted because it is optional + # shellcheck disable=SC2086 + if ! ctdb_check_rpc_out=$(rpcinfo -T "$_family" "$_localhost" \ + "$_progname" $_version 2>&1) ; then + ctdb_check_rpc_out="$_progname failed RPC check: +$ctdb_check_rpc_out" + echo "$ctdb_check_rpc_out" + return 1 + fi +} + +nfs_check_rpcinfo () +{ + _progname="$1" # passed to rpcinfo (looked up in /etc/rpc) + _versions="$2" # optional, space separated, not passed if empty/unset + _families="${3:-tcp}" # optional, space separated, default is "tcp" + + for _family in $_families ; do + if [ -n "$_versions" ] ; then + for _version in $_versions ; do + ctdb_check_rpc "$_progname" "$_version" "$_family" || return $? + done + else + ctdb_check_rpc "$_progname" "" "$_family" || return $? + fi + done +} + +################################################################## +# use statd-callout to update NFS lock info +################################################################## +nfs_update_lock_info () +{ + if [ -x "$CTDB_BASE/statd-callout" ] ; then + "$CTDB_BASE/statd-callout" update + fi +} + +###################################################################### + +# script_state_dir set by ctdb_setup_state_dir() +# shellcheck disable=SC2154 +nfs_callout_init "$script_state_dir" + +case "$1" in +startup) + nfs_callout "$@" || exit $? + ;; + +shutdown) + nfs_callout "$@" || exit $? + ;; + +takeip) + nfs_callout "$@" || exit $? + ctdb_service_set_reconfigure + ;; + +releaseip) + nfs_callout "$@" || exit $? + ctdb_service_set_reconfigure + ;; + +ipreallocated) + if ctdb_service_needs_reconfigure ; then + ctdb_service_reconfigure + fi + ;; + +monitor) + nfs_callout "monitor-pre" || exit $? + + # Check that directories for shares actually exist + if [ "$CTDB_NFS_SKIP_SHARE_CHECK" != "yes" ] ; then + nfs_callout "monitor-list-shares" | ctdb_check_directories || \ + exit $? + fi + + update_tickles 2049 + nfs_update_lock_info + + nfs_check_services + + nfs_callout "monitor-post" || exit $? + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/70.iscsi.script b/ctdb/config/events/legacy/70.iscsi.script new file mode 100755 index 0000000..e74651d --- /dev/null +++ b/ctdb/config/events/legacy/70.iscsi.script @@ -0,0 +1,87 @@ +#!/bin/sh + +# CTDB event script for TGTD based iSCSI + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +# service_name is used by various functions +# shellcheck disable=SC2034 +service_name="iscsi" + +load_script_options + +[ -z "$CTDB_START_ISCSI_SCRIPTS" ] && { + echo "No iscsi start script directory found" + exit 0 +} + +case "$1" in +ipreallocated) + all_ips=$($CTDB -X ip | tail -n +2) + + # Block the iSCSI port. Only block for the address families + # we have configured. This copes with, for example, ip6tables + # being unavailable on an IPv4-only system. + have_ipv4=false + have_ipv6=false + # x is intentionally ignored + # shellcheck disable=SC2034 + while IFS='|' read x ip pnn x ; do + case "$ip" in + *:*) have_ipv6=true ;; + *) have_ipv4=true ;; + esac + done </dev/null 2>/dev/null + + pnn=$(ctdb_get_pnn) + [ -n "$pnn" ] || die "Failed to get node pnn" + + # Start iSCSI daemon + tgtd >/dev/null 2>&1 + + # Run a script for each currently hosted public IP address + ips=$(echo "$all_ips" | awk -F'|' -v pnn="$pnn" '$3 == pnn {print $2}') + for ip in $ips ; do + script="${CTDB_START_ISCSI_SCRIPTS}/${ip}.sh" + if [ -x "$script" ] ; then + echo "Starting iSCSI service for public address ${ip}" + "$script" + fi + done + + # Unblock iSCSI port. These can be unconditional (compared to + # blocking above), since errors are redirected. + while iptables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do + : + done + while ip6tables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do + : + done + + ;; + +shutdown) + # Shutdown iSCSI daemon when ctdb goes down + killall -9 tgtd >/dev/null 2>&1 + ;; + +monitor) + ctdb_check_tcp_ports 3260 || exit $? + ;; +esac + +exit 0 diff --git a/ctdb/config/events/legacy/91.lvs.script b/ctdb/config/events/legacy/91.lvs.script new file mode 100755 index 0000000..8855068 --- /dev/null +++ b/ctdb/config/events/legacy/91.lvs.script @@ -0,0 +1,124 @@ +#!/bin/sh +# script to manage the lvs ip multiplexer for a single public address cluster + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD") + +. "${CTDB_BASE}/functions" + +load_script_options + +[ -n "$CTDB_LVS_NODES" ] || exit 0 +export CTDB_LVS_NODES + +# type is commonly supported and more portable than which(1) +# shellcheck disable=SC2039 +if ! type ipvsadm >/dev/null 2>&1 ; then + echo "LVS configured but ipvsadm not found" + exit 0 +fi + + +lvs_follower_only () +{ + _ip_address=$(ctdb_get_ip_address) + awk -v my_ip="$_ip_address" \ + '$1 == my_ip { if ($2 ~ "follower-only") { exit 0 } else { exit 1 } }' \ + "$CTDB_LVS_NODES" +} + +lvs_check_config () +{ + [ -r "$CTDB_LVS_NODES" ] || \ + die "error: CTDB_LVS_NODES=${CTDB_LVS_NODES} unreadable" + [ -n "$CTDB_LVS_PUBLIC_IP" ] || \ + die "Invalid configuration: CTDB_LVS_PUBLIC_IP not set" + if ! lvs_follower_only ; then + [ -n "$CTDB_LVS_PUBLIC_IFACE" ] || \ + die "Invalid configuration: CTDB_LVS_PUBLIC_IFACE not set" + fi +} + +case "$1" in +setup) + lvs_check_config + ;; +startup) + lvs_check_config + + ipvsadm -D -t "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1 + ipvsadm -D -u "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1 + + ip addr add "${CTDB_LVS_PUBLIC_IP}/32" dev lo scope host + + # do not respond to ARPs that are for ip addresses with scope 'host' + set_proc_maybe sys/net/ipv4/conf/all/arp_ignore 3 + # do not send out arp requests from loopback addresses + set_proc_maybe sys/net/ipv4/conf/all/arp_announce 2 + ;; + +shutdown) + lvs_check_config + + ipvsadm -D -t "$CTDB_LVS_PUBLIC_IP" + ipvsadm -D -u "$CTDB_LVS_PUBLIC_IP" + + ip addr del "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1 + + flush_route_cache + ;; + +ipreallocated) + lvs_check_config + + # Kill connections + ipvsadm -D -t "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1 + ipvsadm -D -u "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1 + kill_tcp_connections_local_only \ + "$CTDB_LVS_PUBLIC_IFACE" "$CTDB_LVS_PUBLIC_IP" + + pnn=$(ctdb_get_pnn) + lvsleader=$("${CTDB_HELPER_BINDIR}/ctdb_lvs" leader) + if [ "$pnn" != "$lvsleader" ] ; then + # This node is not the LVS leader so change the IP address + # to have scope "host" so this node won't respond to ARPs + ip addr del "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1 + ip addr add "${CTDB_LVS_PUBLIC_IP}/32" dev lo scope host + exit 0 + fi + + # Change the scope so this node starts responding to ARPs + ip addr del "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1 + ip addr add "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1 + + ipvsadm -A -t "$CTDB_LVS_PUBLIC_IP" -p 1999999 -s lc + ipvsadm -A -u "$CTDB_LVS_PUBLIC_IP" -p 1999999 -s lc + + # Add all nodes (except this node) as LVS servers + "${CTDB_HELPER_BINDIR}/ctdb_lvs" list | + awk -v pnn="$pnn" '$1 != pnn { print $2 }' | + while read ip ; do + ipvsadm -a -t "$CTDB_LVS_PUBLIC_IP" -r "$ip" -g + ipvsadm -a -u "$CTDB_LVS_PUBLIC_IP" -r "$ip" -g + done + + # Add localhost too... + ipvsadm -a -t "$CTDB_LVS_PUBLIC_IP" -r 127.0.0.1 + ipvsadm -a -u "$CTDB_LVS_PUBLIC_IP" -r 127.0.0.1 + + $CTDB gratarp \ + "$CTDB_LVS_PUBLIC_IP" "$CTDB_LVS_PUBLIC_IFACE" >/dev/null 2>&1 + + flush_route_cache + ;; + +monitor) + lvs_check_config + + if [ -n "$CTDB_LVS_PUBLIC_IFACE" ] ; then + interface_monitor "$CTDB_LVS_PUBLIC_IFACE" || exit 1 + fi + ;; +esac + +exit 0 diff --git a/ctdb/config/functions b/ctdb/config/functions new file mode 100755 index 0000000..725993c --- /dev/null +++ b/ctdb/config/functions @@ -0,0 +1,1037 @@ +# Hey Emacs, this is a -*- shell-script -*- !!! + +# utility functions for ctdb event scripts + +if [ -z "$CTDB_BASE" ] ; then + echo 'CTDB_BASE unset in CTDB functions file' + exit 1 +fi +export CTDB_BASE + +# CTDB_VARDIR is used elsewhere +# shellcheck disable=SC2034 +CTDB_VARDIR="/usr/local/var/lib/ctdb" + +CTDB="${CTDB:-/usr/local/bin/ctdb}" + +# Only (and always) override these variables in test code + +if [ -z "$CTDB_SCRIPT_VARDIR" ] ; then + CTDB_SCRIPT_VARDIR="/usr/local/var/lib/ctdb/scripts" +fi + +if [ -z "$CTDB_SYS_ETCDIR" ] ; then + CTDB_SYS_ETCDIR="/etc" +fi + +if [ -z "$CTDB_HELPER_BINDIR" ] ; then + CTDB_HELPER_BINDIR="/usr/local/libexec/ctdb" +fi + +####################################### +# pull in a system config file, if any + +load_system_config () +{ + for _i ; do + + if [ -f "${CTDB_SYS_ETCDIR}/sysconfig/${_i}" ]; then + . "${CTDB_SYS_ETCDIR}/sysconfig/${_i}" + return + elif [ -f "${CTDB_SYS_ETCDIR}/default/${_i}" ]; then + . "${CTDB_SYS_ETCDIR}/default/${_i}" + return + fi + done +} + +# load_script_options [ component script ] +# script is an event script name relative to a component +# component is currently ignored +load_script_options () +{ + if [ $# -eq 2 ] ; then + _script="$2" + elif [ $# -eq 0 ] ; then + _script="" + else + die "usage: load_script_options [ component script ]" + fi + + _options="${CTDB_BASE}/script.options" + + if [ -r "$_options" ] ; then + . "$_options" + fi + + if [ -n "$_script" ] ; then + _s="${CTDB_BASE}/events/legacy/${_script}" + else + _s="${0%.script}" + fi + _options="${_s}.options" + + if [ -r "$_options" ] ; then + . "$_options" + fi +} + +############################################################## + +die () +{ + _msg="$1" + _rc="${2:-1}" + + echo "$_msg" >&2 + exit "$_rc" +} + +# Log given message or stdin to either syslog or a CTDB log file +# $1 is the tag passed to logger if syslog is in use. +script_log () +{ + _tag="$1" ; shift + + case "$CTDB_LOGGING" in + file:*|"") + if [ -n "$CTDB_LOGGING" ] ; then + _file="${CTDB_LOGGING#file:}" + else + _file="/usr/local/var/log/log.ctdb" + fi + { + if [ -n "$*" ] ; then + echo "$*" + else + cat + fi + } >>"$_file" + ;; + *) + # Handle all syslog:* variants here too. There's no tool to do + # the lossy things, so just use logger. + logger -t "ctdbd: ${_tag}" "$@" + ;; + esac +} + +# When things are run in the background in an eventscript then logging +# output might get lost. This is the "solution". :-) +background_with_logging () +{ + ( + "$@" 2>&1 "$_pnn_file" + fi + + cat "$_pnn_file" +} + +# Cached retrieval of private IP address from local node. This never +# changes. +ctdb_get_ip_address () +{ + _ip_addr_file="${CTDB_SCRIPT_VARDIR}/my-ip-address" + if [ ! -f "$_ip_addr_file" ] ; then + $CTDB -X nodestatus | + awk -F '|' 'NR == 2 { print $3 }' >"$_ip_addr_file" + fi + + cat "$_ip_addr_file" +} + +# Cached retrieval of database options for use by event scripts. +# +# If the variables are already set then they should not be overwritten +# - this should only happen during event script testing. +ctdb_get_db_options () +{ + _db_opts_file="${CTDB_SCRIPT_VARDIR}/db_options.cache" + + if [ ! -f "$_db_opts_file" ] ; then + { + ctdb_translate_option "database" \ + "volatile database directory" \ + "CTDB_DBDIR" + ctdb_translate_option "database" \ + "persistent database directory" \ + "CTDB_DBDIR_PERSISTENT" + ctdb_translate_option "database" \ + "state database directory" \ + "CTDB_DBDIR_STATE" + } >"$_db_opts_file" + fi + + . "$_db_opts_file" +} + +ctdb_translate_option () +{ + _section="$1" + _opt="$2" + _variable="$3" + + # ctdb-config already prints an error if something goes wrong + _t=$("${CTDB_HELPER_BINDIR}/ctdb-config" get "$_section" "$_opt") || \ + exit $? + echo "${_variable}=\"${_t}\"" +} + +###################################################### +# wrapper around /proc/ settings to allow them to be hooked +# for testing +# 1st arg is relative path under /proc/, 2nd arg is value to set +set_proc () +{ + echo "$2" >"/proc/$1" +} + +set_proc_maybe () +{ + if [ -w "/proc/$1" ] ; then + set_proc "$1" "$2" + fi +} + +###################################################### +# wrapper around getting file contents from /proc/ to allow +# this to be hooked for testing +# 1st arg is relative path under /proc/ +get_proc () +{ + cat "/proc/$1" +} + +###################################################### +# Print up to $_max kernel stack traces for processes named $_program +program_stack_traces () +{ + _prog="$1" + _max="${2:-1}" + + _count=1 + for _pid in $(pidof "$_prog") ; do + [ "$_count" -le "$_max" ] || break + + # Do this first to avoid racing with process exit + _stack=$(get_proc "${_pid}/stack" 2>/dev/null) + if [ -n "$_stack" ] ; then + echo "Stack trace for ${_prog}[${_pid}]:" + echo "$_stack" + _count=$((_count + 1)) + fi + done +} + +###################################################### +# Ensure $service_name is set +assert_service_name () +{ + # service_name is set by the event script + # shellcheck disable=SC2154 + [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set" +} + +###################################################### +# check a set of directories is available +# return 1 on a missing directory +# directories are read from stdin +###################################################### +ctdb_check_directories_probe() +{ + while IFS="" read d ; do + case "$d" in + *%*) + continue + ;; + *) + [ -d "${d}/." ] || return 1 + esac + done +} + +###################################################### +# check a set of directories is available +# directories are read from stdin +###################################################### +ctdb_check_directories() +{ + ctdb_check_directories_probe || { + echo "ERROR: $service_name directory \"$d\" not available" + exit 1 + } +} + +###################################################### +# check a set of tcp ports +# usage: ctdb_check_tcp_ports +###################################################### + +# Check whether something is listening on all of the given TCP ports +# using the "ctdb checktcpport" command. +ctdb_check_tcp_ports() +{ + if [ -z "$1" ] ; then + echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified" + exit 1 + fi + + for _p ; do # process each function argument (port) + _cmd="$CTDB checktcpport $_p" + _out=$($_cmd 2>&1) + _ret=$? + case "$_ret" in + 0) + echo "$service_name not listening on TCP port $_p" + return 1 + ;; + 98) + # Couldn't bind, something already listening, next port + continue + ;; + *) + echo "unexpected error (${_ret}) running \"${_cmd}\"" + if [ -n "$_out" ] ; then + echo "$_out" + fi + return $_ret + ;; + esac + done + + # All ports listening + return 0 +} + +###################################################### +# check a unix socket +# usage: ctdb_check_unix_socket SOCKPATH +###################################################### +ctdb_check_unix_socket() +{ + _sockpath="$1" + + if [ -z "$_sockpath" ] ; then + echo "ERROR: ctdb_check_unix_socket() requires socket path" + return 1 + fi + + _out=$(ss -l -x "src ${_sockpath}" | tail -n +2) + if [ -z "$_out" ] ; then + echo "ERROR: ${service_name} not listening on ${_sockpath}" + return 1 + fi +} + +################################################ +# kill off any TCP connections with the given IP +################################################ +kill_tcp_connections () +{ + _iface="$1" + _ip="$2" + + _oneway=false + if [ "$3" = "oneway" ] ; then + _oneway=true + fi + + get_tcp_connections_for_ip "$_ip" | { + _killcount=0 + _connections="" + _nl=" +" + while read _dst _src; do + _destport="${_dst##*:}" + __oneway=$_oneway + case $_destport in + # we only do one-way killtcp for CIFS + 139|445) __oneway=true ;; + esac + + _connections="${_connections}${_nl}${_src} ${_dst}" + if ! $__oneway ; then + _connections="${_connections}${_nl}${_dst} ${_src}" + fi + + _killcount=$((_killcount + 1)) + done + + if [ $_killcount -eq 0 ] ; then + return + fi + + if [ -n "$CTDB_KILLTCP_DEBUGLEVEL" ]; then + _debuglevel="$CTDB_KILLTCP_DEBUGLEVEL" + else + _debuglevel="$CTDB_DEBUGLEVEL" + fi + echo "$_connections" | \ + CTDB_DEBUGLEVEL="$_debuglevel" \ + "${CTDB_HELPER_BINDIR}/ctdb_killtcp" "$_iface" || { + echo "Failed to kill TCP connections" + return + } + + _connections=$(get_tcp_connections_for_ip "$_ip") + if [ -z "$_connections" ] ; then + _remaining=0 + else + _remaining=$(echo "$_connections" | wc -l) + fi + + _actually_killed=$((_killcount - _remaining)) + + _t="${_actually_killed}/${_killcount}" + echo "Killed ${_t} TCP connections to released IP $_ip" + + if [ -n "$_connections" ] ; then + echo "Remaining connections:" + echo "$_connections" | sed -e 's|^| |' + fi + } +} + +################################################################## +# kill off the local end for any TCP connections with the given IP +################################################################## +kill_tcp_connections_local_only () +{ + kill_tcp_connections "$@" "oneway" +} + +################################################################## +# tickle any TCP connections with the given IP +################################################################## +tickle_tcp_connections () +{ + _ip="$1" + + # Get connections, both directions + _conns=$(get_tcp_connections_for_ip "$_ip" | \ + awk '{ print $1, $2 ; print $2, $1 }') + + echo "$_conns" | awk '{ print "Tickle TCP connection", $1, $2 }' + echo "$_conns" | ctdb tickle +} + +get_tcp_connections_for_ip () +{ + _ip="$1" + + ss -tn state established "src [$_ip]" | awk 'NR > 1 {print $3, $4}' +} + +######################################################## + +add_ip_to_iface () +{ + _iface=$1 + _ip=$2 + _maskbits=$3 + + # Ensure interface is up + ip link set "$_iface" up || \ + die "Failed to bringup interface $_iface" + + # Only need to define broadcast for IPv4 + case "$_ip" in + *:*) _bcast="" ;; + *) _bcast="brd +" ;; + esac + + # Intentionally unquoted multi-word value here + # shellcheck disable=SC2086 + ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || { + echo "Failed to add $_ip/$_maskbits on dev $_iface" + return 1 + } + + # Wait 5 seconds for IPv6 addresses to stop being tentative... + if [ -z "$_bcast" ] ; then + for _x in $(seq 1 10) ; do + ip addr show to "${_ip}/128" | grep -q "tentative" || break + sleep 0.5 + done + + # If the address was a duplicate then it won't be on the + # interface so flag an error. + _t=$(ip addr show to "${_ip}/128") + case "$_t" in + "") + echo "Failed to add $_ip/$_maskbits on dev $_iface" + return 1 + ;; + *tentative*|*dadfailed*) + echo "Failed to add $_ip/$_maskbits on dev $_iface" + ip addr del "$_ip/$_maskbits" dev "$_iface" + return 1 + ;; + esac + fi +} + +delete_ip_from_iface() +{ + _iface=$1 + _ip=$2 + _maskbits=$3 + + # This could be set globally for all interfaces but it is probably + # better to avoid surprises, so limit it the interfaces where CTDB + # has public IP addresses. There isn't anywhere else convenient + # to do this so just set it each time. This is much cheaper than + # remembering and re-adding secondaries. + set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1 + + ip addr del "$_ip/$_maskbits" dev "$_iface" || { + echo "Failed to del $_ip on dev $_iface" + return 1 + } +} + +# If the given IP is hosted then print 2 items: maskbits and iface +ip_maskbits_iface () +{ + _addr="$1" + + case "$_addr" in + *:*) _bits=128 ;; + *) _bits=32 ;; + esac + ip addr show to "${_addr}/${_bits}" 2>/dev/null | \ + awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; + sub("@.*", "", iface) } + $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); + print mask, iface }' +} + +drop_ip () +{ + _addr="${1%/*}" # Remove optional maskbits + + # Intentional word splitting here + # shellcheck disable=SC2046 + set -- $(ip_maskbits_iface "$_addr") + if [ -n "$1" ] ; then + _maskbits="$1" + _iface="$2" + echo "Removing public address $_addr/$_maskbits from device $_iface" + delete_ip_from_iface "$_iface" "$_addr" "$_maskbits" >/dev/null 2>&1 + fi +} + +drop_all_public_ips () +{ + # _x is intentionally ignored + # shellcheck disable=SC2034 + while read _ip _x ; do + case "$_ip" in + \#*) continue ;; + esac + drop_ip "$_ip" + done <"${CTDB_BASE}/public_addresses" +} + +flush_route_cache () +{ + set_proc_maybe sys/net/ipv4/route/flush 1 + set_proc_maybe sys/net/ipv6/route/flush 1 +} + +######################################################## +# Interface monitoring + +# If the interface is a virtual one (e.g. VLAN) then get the +# underlying interface +interface_get_real () +{ + # Output of "ip link show " + _iface_info="$1" + + # Extract the full interface description to see if it is a VLAN + _t=$(echo "$_iface_info" | + awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; + print iface }') + case "$_t" in + *@*) + # VLAN: use the underlying interface, after the '@' + echo "${_t##*@}" + ;; + *) + # Not a regular VLAN. For backward compatibility, assume + # there is some other sort of VLAN that doesn't have the + # '@' in the output and only use what is before a '.'. If + # there is no '.' then this will be the whole interface + # name. + echo "${_t%%.*}" + esac +} + +# Check whether an interface is operational +interface_monitor () +{ + _iface="$1" + + _iface_info=$(ip link show "$_iface" 2>&1) || { + echo "ERROR: Monitored interface ${_iface} does not exist" + return 1 + } + + + # If the interface is a virtual one (e.g. VLAN) then get the + # underlying interface. + _realiface=$(interface_get_real "$_iface_info") + + if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null) ; then + # This is a bond: various monitoring strategies + echo "$_bi" | grep -q 'Currently Active Slave: None' && { + echo "ERROR: No active slaves for bond device ${_realiface}" + return 1 + } + echo "$_bi" | grep -q '^MII Status: up' || { + echo "ERROR: public network interface ${_realiface} is down" + return 1 + } + echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && { + # This works around a bug in the driver where the + # overall bond status can be up but none of the actual + # physical interfaces have a link. + echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || { + echo "ERROR: No active slaves for 802.ad bond device ${_realiface}" + return 1 + } + } + + return 0 + else + # Not a bond + case "$_iface" in + lo*) + # loopback is always working + return 0 + ;; + ib*) + # we don't know how to test ib links + return 0 + ;; + *) + ethtool "$_iface" | grep -q 'Link detected: yes' || { + # On some systems, this is not successful when a + # cable is plugged but the interface has not been + # brought up previously. Bring the interface up + # and try again... + ip link set "$_iface" up + ethtool "$_iface" | grep -q 'Link detected: yes' || { + echo "ERROR: No link on the public network interface ${_iface}" + return 1 + } + } + return 0 + ;; + esac + fi +} + +######################################################## +# Simple counters +_ctdb_counter_common () +{ + [ $# -le 1 ] || die "usage: _ctdb_counter_common [name]" + + if [ $# -eq 1 ] ; then + _counter_name="${1}.failcount" + else + _counter_name="failcount" + fi + + if [ -z "$script_state_dir" ] ; then + die "ctdb_counter_* functions need ctdb_setup_state_dir()" + fi + + _counter_file="${script_state_dir}/${_counter_name}" +} +# Some code passes an argument +# shellcheck disable=SC2120 +ctdb_counter_init () { + _ctdb_counter_common "$1" + + : >"$_counter_file" +} +ctdb_counter_incr () { + _ctdb_counter_common "$1" + + # unary counting using newlines! + echo >>"$_counter_file" +} +ctdb_counter_get () { + _ctdb_counter_common "$1" + # unary counting! + _val=$(wc -c < "$_counter_file" 2>/dev/null || echo 0) + # Strip leading spaces from ouput of wc (on freebsd) + # shellcheck disable=SC2086 + echo $_val +} + +######################################################## + +# ctdb_setup_state_dir +# Sets/creates script_state_dir) +ctdb_setup_state_dir () +{ + [ $# -eq 2 ] || die "usage: ctdb_setup_state_dir " + + _type="$1" + _name="$2" + + script_state_dir="${CTDB_SCRIPT_VARDIR}/${_type}/${_name}" + + mkdir -p "$script_state_dir" || \ + die "Error creating script state dir \"${script_state_dir}\"" +} + +################################################################## +# Reconfigure a service on demand + +_ctdb_service_reconfigure_common () +{ + if [ -z "$script_state_dir" ] ; then + die "ctdb_service_*_reconfigure() needs ctdb_setup_state_dir()" + fi + + _ctdb_service_reconfigure_flag="${script_state_dir}/need_reconfigure" +} + +ctdb_service_needs_reconfigure () +{ + _ctdb_service_reconfigure_common + [ -e "$_ctdb_service_reconfigure_flag" ] +} + +ctdb_service_set_reconfigure () +{ + _ctdb_service_reconfigure_common + : >"$_ctdb_service_reconfigure_flag" +} + +ctdb_service_unset_reconfigure () +{ + _ctdb_service_reconfigure_common + rm -f "$_ctdb_service_reconfigure_flag" +} + +ctdb_service_reconfigure () +{ + echo "Reconfiguring service \"${service_name}\"..." + ctdb_service_unset_reconfigure + service_reconfigure || return $? + # Intentionally have this use $service_name as default + # shellcheck disable=SC2119 + ctdb_counter_init +} + +# Default service_reconfigure() function does nothing. +service_reconfigure () +{ + : +} + +# Default service_start() and service_stop() functions. + +# These may be overridden in an eventscript. +service_start () +{ + service "$service_name" start +} + +service_stop () +{ + service "$service_name" stop +} + +################################################################## + +# This exists only for backward compatibility with 3rd party scripts +# that call it +ctdb_standard_event_handler () +{ + : +} + +iptables_wrapper () +{ + _family="$1" ; shift + if [ "$_family" = "inet6" ] ; then + _iptables_cmd="ip6tables" + else + _iptables_cmd="iptables" + fi + + # iptables doesn't like being re-entered, so flock-wrap it. + flock -w 30 "${CTDB_SCRIPT_VARDIR}/iptables.flock" "$_iptables_cmd" "$@" +} + +# AIX (and perhaps others?) doesn't have mktemp +# type is commonly supported and more portable than which(1) +# shellcheck disable=SC2039 +if ! type mktemp >/dev/null 2>&1 ; then + mktemp () + { + _dir=false + if [ "$1" = "-d" ] ; then + _dir=true + shift + fi + _d="${TMPDIR:-/tmp}" + _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \ + cksum | \ + awk '{print $1}') + _t="${_d}/tmp.${_hex10}" + ( + umask 077 + if $_dir ; then + mkdir "$_t" + else + : >"$_t" + fi + ) + echo "$_t" + } +fi + +###################################################################### +# NFS callout handling + +nfs_callout_init () +{ + _state_dir="$1" + + if [ -z "$CTDB_NFS_CALLOUT" ] ; then + CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-linux-kernel-callout" + fi + # Always export, for statd callout + export CTDB_NFS_CALLOUT + + # If the callout wants to use this then it must create it + export CTDB_NFS_CALLOUT_STATE_DIR="${_state_dir}/callout-state" + + # Export, if set, for use by clustered NFS callouts + if [ -n "$CTDB_NFS_STATE_FS_TYPE" ] ; then + export CTDB_NFS_STATE_FS_TYPE + fi + if [ -n "$CTDB_NFS_STATE_MNT" ] ; then + export CTDB_NFS_STATE_MNT + fi + + nfs_callout_cache="${_state_dir}/nfs_callout_cache" + nfs_callout_cache_callout="${nfs_callout_cache}/CTDB_NFS_CALLOUT" + nfs_callout_cache_ops="${nfs_callout_cache}/ops" +} + +nfs_callout_register () +{ + mkdir -p "$nfs_callout_cache_ops" + rm -f "$nfs_callout_cache_ops"/* + + echo "$CTDB_NFS_CALLOUT" >"$nfs_callout_cache_callout" + + _t=$("$CTDB_NFS_CALLOUT" "register") + if [ -n "$_t" ] ; then + echo "$_t" | + while IFS="" read _op ; do + touch "${nfs_callout_cache_ops}/${_op}" + done + else + touch "${nfs_callout_cache_ops}/ALL" + fi +} + +nfs_callout () +{ + # Re-run registration if $CTDB_NFS_CALLOUT has changed + _prev="" + if [ -r "$nfs_callout_cache_callout" ] ; then + read _prev <"$nfs_callout_cache_callout" + fi + if [ "$CTDB_NFS_CALLOUT" != "$_prev" ] ; then + nfs_callout_register + fi + + # Run the operation if it is registered... + if [ -e "${nfs_callout_cache_ops}/${1}" ] || \ + [ -e "${nfs_callout_cache_ops}/ALL" ]; then + "$CTDB_NFS_CALLOUT" "$@" + fi +} + +######################################################## +# tickle handling +######################################################## + +update_tickles () +{ + _port="$1" + + tickledir="${CTDB_SCRIPT_VARDIR}/tickles" + mkdir -p "$tickledir" + + # What public IPs do I hold? + _pnn=$(ctdb_get_pnn) + _ips=$($CTDB -X ip | awk -F'|' -v pnn="$_pnn" '$3 == pnn {print $2}') + + # IPs and port as ss filters + _ip_filter="" + for _ip in $_ips ; do + _ip_filter="${_ip_filter}${_ip_filter:+ || }src [${_ip}]" + done + _port_filter="sport == :${_port}" + + # Record connections to our public IPs in a temporary file. + # This temporary file is in CTDB's private state directory and + # $$ is used to avoid a very rare race involving CTDB's script + # debugging. No security issue, nothing to see here... + _my_connections="${tickledir}/${_port}.connections.$$" + # Parentheses are needed around the filters for precedence but + # the parentheses can't be empty! + # + # Recent versions of ss print square brackets around IPv6 + # addresses. While it is desirable to update CTDB's address + # parsing and printing code, something needs to be done here + # for backward compatibility, so just delete the brackets. + ss -tn state established \ + "${_ip_filter:+( ${_ip_filter} )}" \ + "${_port_filter:+( ${_port_filter} )}" | + awk 'NR > 1 {print $4, $3}' | + tr -d '][' | + sort >"$_my_connections" + + # Record our current tickles in a temporary file + _my_tickles="${tickledir}/${_port}.tickles.$$" + for _i in $_ips ; do + $CTDB -X gettickles "$_i" "$_port" | + awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }' + done | + sort >"$_my_tickles" + + # Add tickles for connections that we haven't already got tickles for + comm -23 "$_my_connections" "$_my_tickles" | \ + $CTDB addtickle + + # Remove tickles for connections that are no longer there + comm -13 "$_my_connections" "$_my_tickles" | \ + $CTDB deltickle + + rm -f "$_my_connections" "$_my_tickles" + + # Remove stale files from killed scripts + # Files can't have spaces in name, more portable than -print0/-0 + # shellcheck disable=SC2038 + (cd "$tickledir" && find . -type f -mmin +10 | xargs -r rm) +} + +######################################################## +# load a site local config file +######################################################## + +[ -x "${CTDB_BASE}/rc.local" ] && { + . "${CTDB_BASE}/rc.local" +} + +[ -d "${CTDB_BASE}/rc.local.d" ] && { + for i in "${CTDB_BASE}/rc.local.d"/* ; do + [ -x "$i" ] && . "$i" + done +} + +script_name="${0##*/}" # basename diff --git a/ctdb/config/nfs-checks.d/00.portmapper.check b/ctdb/config/nfs-checks.d/00.portmapper.check new file mode 100644 index 0000000..24def35 --- /dev/null +++ b/ctdb/config/nfs-checks.d/00.portmapper.check @@ -0,0 +1,2 @@ +# portmapper +unhealthy_after=1 diff --git a/ctdb/config/nfs-checks.d/10.status.check b/ctdb/config/nfs-checks.d/10.status.check new file mode 100644 index 0000000..b8ce1e0 --- /dev/null +++ b/ctdb/config/nfs-checks.d/10.status.check @@ -0,0 +1,7 @@ +# status +version="1" +restart_every=2 +unhealthy_after=6 +service_stop_cmd="$CTDB_NFS_CALLOUT stop status" +service_start_cmd="$CTDB_NFS_CALLOUT start status" +service_debug_cmd="program_stack_traces rpc.statd 5" diff --git a/ctdb/config/nfs-checks.d/20.nfs.check b/ctdb/config/nfs-checks.d/20.nfs.check new file mode 100644 index 0000000..dad1cdc --- /dev/null +++ b/ctdb/config/nfs-checks.d/20.nfs.check @@ -0,0 +1,7 @@ +# nfs +version="3" +restart_every=10 +unhealthy_after=2 +service_stop_cmd="$CTDB_NFS_CALLOUT stop nfs" +service_start_cmd="$CTDB_NFS_CALLOUT start nfs" +service_debug_cmd="program_stack_traces nfsd 5" diff --git a/ctdb/config/nfs-checks.d/30.nlockmgr.check b/ctdb/config/nfs-checks.d/30.nlockmgr.check new file mode 100644 index 0000000..6660ca0 --- /dev/null +++ b/ctdb/config/nfs-checks.d/30.nlockmgr.check @@ -0,0 +1,6 @@ +# nlockmgr +version="4" +restart_every=2 +unhealthy_after=6 +service_stop_cmd="$CTDB_NFS_CALLOUT stop nlockmgr" +service_start_cmd="$CTDB_NFS_CALLOUT start nlockmgr" diff --git a/ctdb/config/nfs-checks.d/40.mountd.check b/ctdb/config/nfs-checks.d/40.mountd.check new file mode 100644 index 0000000..bfe4c27 --- /dev/null +++ b/ctdb/config/nfs-checks.d/40.mountd.check @@ -0,0 +1,7 @@ +# mountd +version="1" +restart_every=2 +unhealthy_after=6 +service_stop_cmd="$CTDB_NFS_CALLOUT stop mountd" +service_start_cmd="$CTDB_NFS_CALLOUT start mountd" +service_debug_cmd="program_stack_traces rpc.mountd 5" diff --git a/ctdb/config/nfs-checks.d/50.rquotad.check b/ctdb/config/nfs-checks.d/50.rquotad.check new file mode 100644 index 0000000..98bd8d9 --- /dev/null +++ b/ctdb/config/nfs-checks.d/50.rquotad.check @@ -0,0 +1,7 @@ +# rquotad +version="1" +restart_every=2 +unhealthy_after=6 +service_stop_cmd="$CTDB_NFS_CALLOUT stop rquotad" +service_start_cmd="$CTDB_NFS_CALLOUT start rquotad" +service_debug_cmd="program_stack_traces rpc.rquotad 5" diff --git a/ctdb/config/nfs-checks.d/README b/ctdb/config/nfs-checks.d/README new file mode 100644 index 0000000..044067a --- /dev/null +++ b/ctdb/config/nfs-checks.d/README @@ -0,0 +1,31 @@ +NFS check configuration files. + +Files are named NN.RPCSERVICE.check. Files without a .check suffix +are ignored. + +Supported variables are: + +* family - "tcp" or "udp" or space separated list + default: tcp, not used with "service_check_cmd" +* version - optional, RPC service version number + default is to omit to check for any version, + not used with "service_check_cmd" +* unhealthy_after - number of check fails before unhealthy + default: 1 +* restart_every - number of check fails before restart + default: 0, meaning no restart +* service_stop_cmd - command to stop service + default: no default, must be provided if + restart_every > 0 +* service_start_cmd - command to start service + default: no default, must be provided if + restart_every > 0 +* service_check_cmd - command to check health of service + default is to check RPC service using rpcinfo +* service_debug_cmd - command to debug a service after trying to stop it; + for example, it can be useful to print stack + traces of threads that have not exited, since + they may be stuck doing I/O; + no default, see also function program_stack_traces() + +Quoting inside values is not preserved. diff --git a/ctdb/config/nfs-linux-kernel-callout b/ctdb/config/nfs-linux-kernel-callout new file mode 100755 index 0000000..f2f3e38 --- /dev/null +++ b/ctdb/config/nfs-linux-kernel-callout @@ -0,0 +1,441 @@ +#!/bin/sh + +# Exit on 1st error +set -e + +# NFS exports file. Some code below keeps a cache of output derived +# from exportfs(8). When this file is updated the cache is invalid +# and needs to be regenerated. +# +# To change the file, edit the default value below. Do not set +# CTDB_NFS_EXPORTS_FILE - it isn't a configuration variable, just a +# hook for testing. +nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/var/lib/nfs/etab}" + +# As above, edit the default value below. CTDB_NFS_DISTRO_STYLE is a +# test variable only. +nfs_distro_style="${CTDB_NFS_DISTRO_STYLE:-systemd-redhat}" + +# As above, edit the default value below. CTDB_SYS_ETCDIR is a +# test variable only. +etc_dir="${CTDB_SYS_ETCDIR:-/etc}" + +# A value of "AUTO" for any service means that service is usually +# automatically started and stopped by one of the other services. +# Such services will still be restarted by hand on failure, if +# configured to do so. This allows services that should not be +# running to be set to "". + +case "$nfs_distro_style" in +systemd-*) + # Defaults + nfs_service="nfs-server" + nfs_lock_service="rpc-statd" + nfs_mountd_service="nfs-mountd" + nfs_status_service="rpc-statd" + nfs_rquotad_service="rpc-rquotad" + nfs_config="${etc_dir}/sysconfig/nfs" + nfs_rquotad_config="" # Not use with systemd, restart via service + + case "$nfs_distro_style" in + *-redhat | *-suse) + : # Defaults only + ;; + *-debian) + nfs_rquotad_service="quotarpc" + ;; + *) + echo "Internal error" + exit 1 + ;; + esac + ;; + +sysvinit-*) + # Defaults + nfs_service="nfs" + nfs_lock_service="AUTO" + nfs_mountd_service="AUTO" + nfs_status_service="AUTO" + nfs_rquotad_service="AUTO" + nfs_config="${etc_dir}/sysconfig/nfs" + nfs_rquotad_config="$nfs_config" + + case "$nfs_distro_style" in + *-redhat) + nfs_lock_service="nfslock" + ;; + *-suse) + nfs_service="nfsserver" + ;; + *-debian) + nfs_service="nfs-kernel-server" + nfs_config="${etc_dir}/default/nfs-kernel-server" + nfs_rquotad_config="${etc_dir}/default/quota" + ;; + *) + echo "Internal error" + exit 1 + ;; + esac + ;; + +*) + echo "Internal error" + exit 1 + ;; +esac + +# Override for unit testing +if [ -z "$PROCFS_PATH" ]; then + PROCFS_PATH="/proc" +fi + +################################################## + +usage() +{ + _c=$(basename "$0") + cat <"${PROCFS_PATH}/fs/nfsd/threads" + nfs_service_stop >/dev/null 2>&1 || true + pkill -9 nfsd + ;; + nlockmgr) + if service_is_defined "$nfs_lock_service" ; then + service "$nfs_lock_service" stop >/dev/null 2>&1 || true + else + service "$nfs_service" stop >/dev/null 2>&1 || true + fi + ;; + mountd) + service_or_manual_stop "$_rpc_service" "$nfs_mountd_service" + ;; + rquotad) + service_or_manual_stop "$_rpc_service" "$nfs_rquotad_service" + ;; + status) + service_or_manual_stop "$_rpc_service" "$nfs_status_service" + ;; + *) + usage + ;; + esac +} + +manual_start() +{ + case "$1" in + mountd) + nfs_load_config + if [ -z "$RPCMOUNTDOPTS" ]; then + RPCMOUNTDOPTS="${MOUNTD_PORT:+-p }$MOUNTD_PORT" + fi + # shellcheck disable=SC2086 + rpc.mountd $RPCMOUNTDOPTS + ;; + rquotad) + nfs_load_config "$nfs_rquotad_config" + if [ -z "$RPCRQUOTADOPTS" ]; then + RPCRQUOTADOPTS="${RQUOTAD_PORT:+-p }$RQUOTAD_PORT" + fi + # shellcheck disable=SC2086 + rpc.rquotad $RPCRQUOTADOPTS + ;; + status) + nfs_load_config + # Red Hat uses STATDARG, Debian uses STATDOPTS + opts="${STATDARG:-${STATDOPTS:-''}}" + if [ -z "$opts" ]; then + # shellcheck disable=SC2086 + set -- \ + ${STATD_HA_CALLOUT:+-H} $STATD_HA_CALLOUT \ + ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \ + ${STATD_PORT:+-p} $STATD_PORT \ + ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT + opts="$*" + fi + # shellcheck disable=SC2086 + rpc.statd $opts + ;; + *) + echo "$0: Internal error - invalid call to manual_start()" + exit 1 + ;; + esac +} + +service_or_manual_start() +{ + _rpc_service="$1" + _system_service="$2" + + if service_is_defined "$_system_service"; then + service "$_system_service" start + elif service_is_auto_started "$_system_service"; then + manual_start "$_rpc_service" + fi +} + +service_start() +{ + _rpc_service="$1" + + case "$_rpc_service" in + nfs) + nfs_service_start + ;; + nlockmgr) + if service_is_defined "$nfs_lock_service" ; then + service "$nfs_lock_service" start + else + service "$nfs_service" start + fi + ;; + mountd) + service_or_manual_start "$_rpc_service" "$nfs_mountd_service" + ;; + rquotad) + service_or_manual_start "$_rpc_service" "$nfs_rquotad_service" + ;; + status) + service_or_manual_start "$_rpc_service" "$nfs_status_service" + ;; + *) + usage + ;; + esac +} + +################################################## +# service init startup and final shutdown + +nfs_shutdown() +{ + nfs_service_stop +} + +nfs_startup() +{ + nfs_service_stop || true + nfs_service_start + _f="${PROCFS_PATH}/sys/net/ipv4/tcp_tw_recycle" + if [ -f "$_f" ]; then + echo 1 >"$_f" + fi +} + +################################################## +# monitor-post support + +nfs_check_thread_count() +{ + # Load NFS configuration to get desired number of threads. + nfs_load_config + + # If $RPCNFSDCOUNT/$USE_KERNEL_NFSD_NUMBER isn't set then we could + # guess the default from the initscript. However, let's just + # assume that those using the default don't care about the number + # of threads and that they have switched on this feature in error. + _configured_threads="${RPCNFSDCOUNT:-${USE_KERNEL_NFSD_NUMBER}}" + if [ -z "$_configured_threads" ] && type nfsconf >/dev/null 2>&1; then + _configured_threads=$(nfsconf --get nfsd threads) || true + fi + [ -n "$_configured_threads" ] || return 0 + + _threads_file="${PROCFS_PATH}/fs/nfsd/threads" + + # nfsd should be running the configured number of threads. If + # there are a different number of threads then tell nfsd the + # correct number. + read -r _running_threads <"$_threads_file" || { + echo "WARNING: Reading \"${_threads_file}\" unexpectedly failed" + exit 0 + } + + # Intentionally not arithmetic comparison - avoids extra errors + # when above read fails in an unexpected way... + if [ "$_running_threads" != "$_configured_threads" ]; then + echo "Attempting to correct number of nfsd threads from ${_running_threads} to ${_configured_threads}" + echo "$_configured_threads" >"$_threads_file" + fi +} + +################################################## +# list share directories + +nfs_monitor_list_shares() +{ + _cache_file="${CTDB_NFS_CALLOUT_STATE_DIR}/list_shares_cache" + # -nt operator is well supported in Linux: dash, bash, ksh, ... + # shellcheck disable=SC2039,SC3013 + if [ ! -r "$nfs_exports_file" ] || [ ! -r "$_cache_file" ] || + [ "$nfs_exports_file" -nt "$_cache_file" ]; then + mkdir -p "$CTDB_NFS_CALLOUT_STATE_DIR" + # We could just use the contents of $nfs_exports_file. + # However, let's regard that file as internal to NFS and use + # exportfs, which is the public API. + if ! _exports=$(exportfs -v); then + echo "WARNING: failed to run exportfs to list NFS shares" >&2 + return + fi + + echo "$_exports" | + grep '^/' | + sed -e 's@[[:space:]][[:space:]]*[^[:space:]()][^[:space:]()]*([^[:space:]()][^[:space:]()]*)$@@' | + sort -u >"$_cache_file" + fi + + cat "$_cache_file" +} + +################################################## + +nfs_register() +{ + cat </dev/null 2>&1 + ;; + healthy) + mail -s "$(hostname) is HEALTHY" foo@example.com /dev/null 2>&1 + ;; + esac diff --git a/ctdb/config/notify.sh b/ctdb/config/notify.sh new file mode 100755 index 0000000..db69afc --- /dev/null +++ b/ctdb/config/notify.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +# This is script is invoked from ctdb when certain events happen. See +# /etc/ctdb/events/notification/README for more details. + +d=$(dirname "$0") +nd="${d}/events/notification" + +ok=true + +for i in "${nd}/"*.script ; do + # Files must be executable + [ -x "$i" ] || continue + + # Flag failures + "$i" "$1" || ok=false +done + +$ok diff --git a/ctdb/config/script.options b/ctdb/config/script.options new file mode 100644 index 0000000..79e82af --- /dev/null +++ b/ctdb/config/script.options @@ -0,0 +1,16 @@ +# For now, use script.options to demonstrate these options. See +# *.options examples for more specific examples. + +# +# Samba configuration +# + +# 50.samba.options +# CTDB_SAMBA_SKIP_SHARE_CHECK=yes + +# +# NFS configuration +# + +# 60.nfs.options +CTDB_RPCINFO_LOCALHOST="127.0.0.1" diff --git a/ctdb/config/statd-callout b/ctdb/config/statd-callout new file mode 100755 index 0000000..38c155e --- /dev/null +++ b/ctdb/config/statd-callout @@ -0,0 +1,254 @@ +#!/bin/sh + +# This must run as root as CTDB tool commands need to access CTDB socket +[ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@" + +# statd must be configured to use this script as its high availability call-out. +# +# In most Linux versions this can be done using something like the following... +# +# /etc/sysconfig/nfs (Red Hat) or /etc/default/nfs-common (Debian): +# NFS_HOSTNAME=myhostname +# STATD_HOSTNAME="${NFS_HOSTNAME} -H /etc/ctdb/statd-callout" +# +# Newer Red Hat Linux variants instead use /etc/nfs.conf: +# [statd] +# name = myhostname +# ha-callout = /etc/ctdb/statd-callout + +[ -n "$CTDB_BASE" ] || \ + CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && echo "$PWD") + +. "${CTDB_BASE}/functions" + +# Overwrite this so we get some logging +die () +{ + script_log "statd-callout" "$@" + exit 1 +} + +# Try different variables to find config file for NFS_HOSTNAME +load_system_config "nfs" "nfs-common" + +# If NFS_HOSTNAME not set then try to pull it out of /etc/nfs.conf +if [ -z "$NFS_HOSTNAME" ]; then + if type nfsconf >/dev/null 2>&1; then + NFS_HOSTNAME=$(nfsconf --get statd name) + elif type git >/dev/null 2>&1; then + # git to the rescue! + NFS_HOSTNAME=$(git config --file=/etc/nfs.conf statd.name) + fi +fi + +[ -n "$NFS_HOSTNAME" ] || \ + die "NFS_HOSTNAME is not configured. statd-callout failed" + +############################################################ + +ctdb_setup_state_dir "service" "nfs" + +# script_state_dir set by ctdb_setup_state_dir() +# shellcheck disable=SC2154 +d="${script_state_dir}/statd-callout" + +mkdir -p "$d" || die "Failed to create directory \"${d}\"" +cd "$d" || die "Failed to change directory to \"${d}\"" + +pnn=$(ctdb_get_pnn) + +############################################################ + +send_notifies () +{ + _smnotify="${CTDB_HELPER_BINDIR}/smnotify" + + # State must monotonically increase, across the entire + # cluster. Use seconds since epoch and hope the time is in + # sync across nodes. Even numbers mean service is shut down, + # odd numbers mean service is started. + + # Intentionally round to an even number + # shellcheck disable=SC2017 + _state_even=$(( $(date '+%s') / 2 * 2)) + + _prev="" + while read _sip _cip ; do + # NOTE: Consider optimising smnotify to read all the + # data from stdin and then run it in the background. + + # Reset stateval for each serverip + if [ "$_sip" != "$_prev" ] ; then + _stateval="$_state_even" + fi + + # Send notifies for server shutdown + "$_smnotify" --client="$_cip" --ip="$_sip" \ + --server="$_sip" --stateval="$_stateval" + "$_smnotify" --client="$_cip" --ip="$_sip" \ + --server="$NFS_HOSTNAME" --stateval="$_stateval" + + # Send notifies for server startup + _stateval=$((_stateval + 1)) + "$_smnotify" --client="$_cip" --ip="$_sip" \ + --server="$_sip" --stateval="$_stateval" + "$_smnotify" --client="$_cip" --ip="$_sip" \ + --server="$NFS_HOSTNAME" --stateval="$_stateval" + done +} + +delete_records () +{ + while read _sip _cip ; do + _key="statd-state@${_sip}@${_cip}" + echo "\"${_key}\" \"\"" + done | $CTDB ptrans "ctdb.tdb" +} + +############################################################ + +case "$1" in + # Keep a single file to keep track of the last "add-client" or + # "del-client'. These get pushed to ctdb.tdb during "update", + # which will generally be run once each "monitor" cycle. In this + # way we avoid scalability problems with flood of persistent + # transactions after a "notify" when all the clients re-take their + # locks. + + add-client) + # statd does not tell us to which IP the client connected so + # we must add it to all the IPs that we serve + cip="$2" + date=$(date '+%s') + # x is intentionally ignored + # shellcheck disable=SC2034 + $CTDB ip -X | + tail -n +2 | + while IFS="|" read x sip node x ; do + [ "$node" = "$pnn" ] || continue # not us + key="statd-state@${sip}@${cip}" + echo "\"${key}\" \"${date}\"" >"$key" + done + ;; + + del-client) + # statd does not tell us from which IP the client disconnected + # so we must add it to all the IPs that we serve + cip="$2" + # x is intentionally ignored + # shellcheck disable=SC2034 + $CTDB ip -X | + tail -n +2 | + while IFS="|" read x sip node x ; do + [ "$node" = "$pnn" ] || continue # not us + key="statd-state@${sip}@${cip}" + echo "\"${key}\" \"\"" >"$key" + done + ;; + + update) + files=$(echo statd-state@*) + if [ "$files" = "statd-state@*" ] ; then + # No files! + exit 0 + fi + # Filter out lines for any IP addresses that are not currently + # hosted public IP addresses. + ctdb_ips=$($CTDB ip | tail -n +2) + sed_expr=$(echo "$ctdb_ips" | + awk -v pnn="$pnn" 'pnn == $2 { + ip = $1; gsub(/\./, "\\.", ip); + printf "/statd-state@%s@/p\n", ip }') + # Intentional multi-word expansion for multiple files + # shellcheck disable=SC2086 + items=$(sed -n "$sed_expr" $files) + if [ -n "$items" ] ; then + if echo "$items" | $CTDB ptrans "ctdb.tdb" ; then + # shellcheck disable=SC2086 + rm $files + fi + fi + ;; + + notify) + # we must restart the lockmanager (on all nodes) so that we get + # a clusterwide grace period (so other clients don't take out + # conflicting locks through other nodes before all locks have been + # reclaimed) + + # we need these settings to make sure that no tcp connections survive + # across a very fast failover/failback + #echo 10 > /proc/sys/net/ipv4/tcp_fin_timeout + #echo 0 > /proc/sys/net/ipv4/tcp_max_tw_buckets + #echo 0 > /proc/sys/net/ipv4/tcp_max_orphans + + # Delete the notification list for statd, we don't want it to + # ping any clients + rm -f /var/lib/nfs/statd/sm/* + rm -f /var/lib/nfs/statd/sm.bak/* + + # We must also let some time pass between stopping and + # restarting the lock manager. Otherwise there is a window + # where the lock manager will respond "strangely" immediately + # after restarting it, which causes clients to fail to reclaim + # their locks. + nfs_callout_init + "$CTDB_NFS_CALLOUT" "stop" "nlockmgr" >/dev/null 2>&1 + sleep 2 + "$CTDB_NFS_CALLOUT" "start" "nlockmgr" >/dev/null 2>&1 + + # we now need to send out additional statd notifications to ensure + # that clients understand that the lockmanager has restarted. + # we have three cases: + # 1, clients that ignore the ip address the stat notification came from + # and ONLY care about the 'name' in the notify packet. + # these clients ONLY work with lock failover IFF that name + # can be resolved into an ipaddress that matches the one used + # to mount the share. (==linux clients) + # This is handled when starting lockmanager above, but those + # packets are sent from the "wrong" ip address, something linux + # clients are ok with, buth other clients will barf at. + # 2, Some clients only accept statd packets IFF they come from the + # 'correct' ip address. + # 2a,Send out the notification using the 'correct' ip address and also + # specify the 'correct' hostname in the statd packet. + # Some clients require both the correct source address and also the + # correct name. (these clients also ONLY work if the ip addresses + # used to map the share can be resolved into the name returned in + # the notify packet.) + # 2b,Other clients require that the source ip address of the notify + # packet matches the ip address used to take out the lock. + # I.e. that the correct source address is used. + # These clients also require that the statd notify packet contains + # the name as the ip address used when the lock was taken out. + # + # Both 2a and 2b are commonly used in lockmanagers since they maximize + # probability that the client will accept the statd notify packet and + # not just ignore it. + # For all IPs we serve, collect info and push to the config database + + # Construct a sed expression to take catdb output and produce pairs of: + # server-IP client-IP + # but only for the server-IPs that are hosted on this node. + ctdb_all_ips=$($CTDB ip all | tail -n +2) + sed_expr=$(echo "$ctdb_all_ips" | + awk -v pnn="$pnn" 'pnn == $2 { + ip = $1; gsub(/\./, "\\.", ip); + printf "s/^key.*=.*statd-state@\\(%s\\)@\\([^\"]*\\).*/\\1 \\2/p\n", ip }') + + statd_state=$($CTDB catdb ctdb.tdb | sed -n "$sed_expr" | sort) + [ -n "$statd_state" ] || exit 0 + + echo "$statd_state" | send_notifies + echo "$statd_state" | delete_records + + # Remove any stale touch files (i.e. for IPs not currently + # hosted on this node and created since the last "update"). + # There's nothing else we can do with them at this stage. + echo "$ctdb_all_ips" | + awk -v pnn="$pnn" 'pnn != $2 { print $1 }' | + while read sip ; do + rm -f "statd-state@${sip}@"* + done + ;; +esac diff --git a/ctdb/configure b/ctdb/configure new file mode 100755 index 0000000..48b786b --- /dev/null +++ b/ctdb/configure @@ -0,0 +1,22 @@ +#!/bin/sh + +PREVPATH=`dirname $0` + +WAF=buildtools/bin/waf +[ -x "$WAF" ] || WAF=../buildtools/bin/waf + +# using JOBS=1 gives maximum compatibility with +# systems like AIX which have broken threading in python +JOBS=1 +export JOBS + +# Make sure we don't have any library preloaded. +unset LD_PRELOAD + +# Make sure we get stable hashes +PYTHONHASHSEED=1 +export PYTHONHASHSEED + +cd . || exit 1 +$PYTHON $WAF configure "$@" || exit 1 +cd $PREVPATH diff --git a/ctdb/configure.rpm b/ctdb/configure.rpm new file mode 100755 index 0000000..e8ec3b2 --- /dev/null +++ b/ctdb/configure.rpm @@ -0,0 +1,20 @@ +#!/bin/sh + +if gcc -dM -E - . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/dir.h" + +#include "lib/util/debug.h" +#include "lib/util/samba_util.h" + +#include "common/conf.h" +#include "common/path.h" + +#include "database_conf.h" + +#define DATABASE_CONF_VOLATILE_DB_DIR_DEFAULT CTDB_VARDIR "/volatile" +#define DATABASE_CONF_PERSISTENT_DB_DIR_DEFAULT CTDB_VARDIR "/persistent" +#define DATABASE_CONF_STATE_DB_DIR_DEFAULT CTDB_VARDIR "/state" + +static bool check_static_string_change(const char *key, + const char *old_value, + const char *new_value, + enum conf_update_mode mode) +{ + if (mode == CONF_MODE_RELOAD) { + if (strcmp(old_value, new_value) != 0) { + D_WARNING("Ignoring update of [%s] -> %s\n", + DATABASE_CONF_SECTION, + key); + } + } + + return true; +} + +static bool check_static_boolean_change(const char *key, + bool old_value, + bool new_value, + enum conf_update_mode mode) +{ + if (mode == CONF_MODE_RELOAD || CONF_MODE_API) { + if (old_value != new_value) { + D_WARNING("Ignoring update of [%s] -> %s\n", + DATABASE_CONF_SECTION, + key); + } + } + + return true; +} + +static bool database_conf_validate_lock_debug_script(const char *key, + const char *old_script, + const char *new_script, + enum conf_update_mode mode) +{ + char script[PATH_MAX]; + char script_path[PATH_MAX]; + struct stat st; + size_t len; + int ret; + + if (new_script == NULL) { + return true; + } + + len = strlcpy(script, new_script, sizeof(script)); + if (len >= sizeof(script)) { + D_ERR("lock debug script name too long\n"); + return false; + } + + ret = snprintf(script_path, + sizeof(script_path), + "%s/%s", + path_etcdir(), + basename(script)); + if (ret < 0 || (size_t)ret >= sizeof(script_path)) { + D_ERR("lock debug script path too long\n"); + return false; + } + + ret = stat(script_path, &st); + if (ret == -1) { + D_ERR("lock debug script %s does not exist\n", script_path); + return false; + } + + if (! S_ISREG(st.st_mode)) { + D_ERR("lock debug script %s is not a file\n", script_path); + return false; + } + if (! (st.st_mode & S_IXUSR)) { + D_ERR("lock debug script %s is not executable\n", script_path); + return false; + } + + return true; +} + +static bool database_conf_validate_db_dir(const char *key, + const char *old_dir, + const char *new_dir, + enum conf_update_mode mode) +{ + if (! directory_exist(new_dir)) { + D_ERR("%s \"%s\" does not exist\n", key, new_dir); + return false; + } + + /* This sometimes warns but always returns true */ + return check_static_string_change(key, old_dir, new_dir, mode); +} + +void database_conf_init(struct conf_context *conf) +{ + conf_define_section(conf, DATABASE_CONF_SECTION, NULL); + + conf_define_string(conf, + DATABASE_CONF_SECTION, + DATABASE_CONF_VOLATILE_DB_DIR, + DATABASE_CONF_VOLATILE_DB_DIR_DEFAULT, + database_conf_validate_db_dir); + conf_define_string(conf, + DATABASE_CONF_SECTION, + DATABASE_CONF_PERSISTENT_DB_DIR, + DATABASE_CONF_PERSISTENT_DB_DIR_DEFAULT, + database_conf_validate_db_dir); + conf_define_string(conf, + DATABASE_CONF_SECTION, + DATABASE_CONF_STATE_DB_DIR, + DATABASE_CONF_STATE_DB_DIR_DEFAULT, + database_conf_validate_db_dir); + conf_define_string(conf, + DATABASE_CONF_SECTION, + DATABASE_CONF_LOCK_DEBUG_SCRIPT, + NULL, + database_conf_validate_lock_debug_script); + conf_define_boolean(conf, + DATABASE_CONF_SECTION, + DATABASE_CONF_TDB_MUTEXES, + true, + check_static_boolean_change); +} diff --git a/ctdb/database/database_conf.h b/ctdb/database/database_conf.h new file mode 100644 index 0000000..6fa579c --- /dev/null +++ b/ctdb/database/database_conf.h @@ -0,0 +1,35 @@ +/* + CTDB database config handling + + Copyright (C) Martin Schwenke 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_DATABASE_CONF_H__ +#define __CTDB_DATABASE_CONF_H__ + +#include "common/conf.h" + +#define DATABASE_CONF_SECTION "database" + +#define DATABASE_CONF_VOLATILE_DB_DIR "volatile database directory" +#define DATABASE_CONF_PERSISTENT_DB_DIR "persistent database directory" +#define DATABASE_CONF_STATE_DB_DIR "state database directory" +#define DATABASE_CONF_LOCK_DEBUG_SCRIPT "lock debug script" +#define DATABASE_CONF_TDB_MUTEXES "tdb mutexes" + +void database_conf_init(struct conf_context *conf); + +#endif /* __CTDB_DATABASE_CONF_H__ */ diff --git a/ctdb/doc/cluster_mutex_helper.txt b/ctdb/doc/cluster_mutex_helper.txt new file mode 100644 index 0000000..4ee018f --- /dev/null +++ b/ctdb/doc/cluster_mutex_helper.txt @@ -0,0 +1,80 @@ +Writing CTDB cluster mutex helpers +================================== + +CTDB uses cluster-wide mutexes to protect against a "split brain", +which could occur if the cluster becomes partitioned due to network +failure or similar. + +CTDB uses a cluster-wide mutex for its "cluster lock", which is used +to ensure that only one database recovery can happen at a time. For +an overview of cluster lock configuration see the CLUSTER LOCK +section in ctdb(7). CTDB tries to ensure correct operation of the +cluster lock by attempting to take the cluster lock when CTDB knows +that it should already be held. + +By default, CTDB uses a supplied mutex helper that uses a fcntl(2) +lock on a specified file in the cluster filesystem. + +However, a user supplied mutex helper can be used as an alternative. +The rest of this document describes the API for mutex helpers. + +A mutex helper is an external executable +---------------------------------------- + +A mutex helper is an external executable that can be run by CTDB. +There are no CTDB-specific compilation dependencies. This means that +a helper could easily be scripted around existing commands. Mutex +helpers are run relatively rarely and are not time critical. +Therefore, reliability is preferred over high performance. + +Taking a mutex with a helper +---------------------------- + +1. Helper is executed with helper-specific arguments + +2. Helper attempts to take mutex + +3. On success, the helper writes ASCII 0 to standard output + +4. Helper stays running, holding mutex, awaiting termination by CTDB + +5. When a helper receives SIGTERM it must release any mutex it is + holding and then exit. + +Status codes +------------ + +CTDB ignores the exit code of a helper. Instead, CTDB reacts to a +single ASCII character that is sent to it via a helper's standard +output. + +Valid status codes are: + +0 - The helper took the mutex and is holding it, awaiting termination. + +1 - The helper was unable to take the mutex due to contention. + +2 - The helper took too long to take the mutex. + + Helpers do not need to implement this status code. CTDB + already implements any required timeout handling. + +3 - An unexpected error occurred. + +If a 0 status code is sent then it the helper should periodically +check if the (original) parent processes still exists while awaiting +termination. If the parent process disappears then the helper should +release the mutex and exit. This avoids stale mutexes. Note that a +helper should never wait for parent process ID 1! + +If a non-0 status code is sent then the helper can exit immediately. +However, if the helper does not exit then it must terminate if it +receives SIGTERM. + +Logging +------- + +Anything written to standard error by a helper is incorporated into +CTDB's logs. A helper should generally only output to stderr for +unexpected errors and avoid output to stderr on success or on mutex +contention. diff --git a/ctdb/doc/ctdb-etcd.7.xml b/ctdb/doc/ctdb-etcd.7.xml new file mode 100644 index 0000000..f84989f --- /dev/null +++ b/ctdb/doc/ctdb-etcd.7.xml @@ -0,0 +1,119 @@ + + + + + + + + This documentation was written by + Jose A. Rivera + + + + + 2016 + Jose A. Rivera + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + + ctdb-etcd + 7 + ctdb + CTDB - clustered TDB database + + + + ctdb-etcd + CTDB etcd integration + + + + + ctdb_etcd_lock + + + + + DESCRIPTION + + ctdb_etcd_lock is intended to be run as a mutex helper for CTDB. It + will try to connect to an existing etcd cluster and grab a lock in that + cluster to function as CTDB's cluster lock. Please see + ctdb/doc/cluster_mutex_helper.txt for details on + the mutex helper API. To use this, include the following line in + the [cluster] section of + ctdb.conf + 5: + + +cluster lock = !/usr/local/usr/libexec/ctdb/ctdb_etcd_lock + + + You can also pass "-v", "-vv", or "-vvv" to include verbose output in + the CTDB log. Additional "v"s indicate increases in verbosity. + + + This mutex helper expects the system Python interpreter to have access + to the etcd Python module. It also expects an etcd cluster to be + configured and running. To integrate with this, there is an optional + config file of the following format: + + +key = value + + + The following configuration parameters (and their defaults) are defined + for use by ctdb_etcd_lock: + + +port = 2379 # connecting port for the etcd cluster +lock_ttl = 9 # seconds for TTL +refresh = 2 # seconds between attempts to maintain lock +locks_dir = _ctdb # where to store CTDB locks in etcd + # The final etcd directory for any given lock looks like: + # /_locks/{locks_dir}/{netbios name}/ + + + In addition, any keyword parameter that can be used to configure an + etcd client may be specified and modified here. For more documentation + on these parameters, see here: https://github.com/jplana/python-etcd/ + + + + + SEE ALSO + + ctdb + 7, + + ctdbd + 1, + + + + + + + diff --git a/ctdb/doc/ctdb-script.options.5.xml b/ctdb/doc/ctdb-script.options.5.xml new file mode 100644 index 0000000..700d1f7 --- /dev/null +++ b/ctdb/doc/ctdb-script.options.5.xml @@ -0,0 +1,1076 @@ + + + + + + + ctdb-script.options + 5 + ctdb + CTDB - clustered TDB database + + + + ctdb-script.options + CTDB scripts configuration files + + + + DESCRIPTION + + + Each CTDB script has 2 possible locations for its configuration options: + + + + + + + /usr/local/etc/ctdb/script.options + + + + This is a catch-all global file for general purpose + scripts and for options that are used in multiple event + scripts. + + + + + + + SCRIPT.options + + + + That is, options for + SCRIPT are + placed in a file alongside the script, with a ".script" + suffix added. This style is usually recommended for event + scripts. + + + + Options in this script-specific file override those in + the global file. + + + + + + + + These files should include simple shell-style variable + assignments and shell-style comments. + + + + + + NETWORK CONFIGURATION + + + 10.interface + + + This event script handles monitoring of interfaces using by + public IP addresses. + + + + + + + CTDB_PARTIALLY_ONLINE_INTERFACES=yes|no + + + + Whether one or more offline interfaces should cause a + monitor event to fail if there are other interfaces that + are up. If this is "yes" and a node has some interfaces + that are down then ctdb status will + display the node as "PARTIALLYONLINE". + + + + Note that CTDB_PARTIALLY_ONLINE_INTERFACES=yes is not + generally compatible with NAT gateway or LVS. NAT + gateway relies on the interface configured by + CTDB_NATGW_PUBLIC_IFACE to be up and LVS replies on + CTDB_LVS_PUBLIC_IFACE to be up. CTDB does not check if + these options are set in an incompatible way so care is + needed to understand the interaction. + + + + Default is "no". + + + + + + + + + 11.natgw + + + Provides CTDB's NAT gateway functionality. + + + + NAT gateway is used to configure fallback routing for nodes + when they do not host any public IP addresses. For example, + it allows unhealthy nodes to reliably communicate with + external infrastructure. One node in a NAT gateway group will + be designated as the NAT gateway leader node and other (follower) + nodes will be configured with fallback routes via the NAT + gateway leader node. For more information, see the + NAT GATEWAY section in + ctdb + 7. + + + + + + CTDB_NATGW_DEFAULT_GATEWAY=IPADDR + + + IPADDR is an alternate network gateway to use on the NAT + gateway leader node. If set, a fallback default route + is added via this network gateway. + + + No default. Setting this variable is optional - if not + set that no route is created on the NAT gateway leader + node. + + + + + + CTDB_NATGW_NODES=FILENAME + + + FILENAME contains the list of nodes that belong to the + same NAT gateway group. + + + File format: + +IPADDR follower-only + + + + IPADDR is the private IP address of each node in the NAT + gateway group. + + + If "follower-only" is specified then the corresponding node + can not be the NAT gateway leader node. In this case + CTDB_NATGW_PUBLIC_IFACE and + CTDB_NATGW_PUBLIC_IP are optional and + unused. + + + No default, usually + /usr/local/etc/ctdb/natgw_nodes when enabled. + + + + + + CTDB_NATGW_PRIVATE_NETWORK=IPADDR/MASK + + + IPADDR/MASK is the private sub-network that is + internally routed via the NAT gateway leader node. This + is usually the private network that is used for node + addresses. + + + No default. + + + + + + CTDB_NATGW_PUBLIC_IFACE=IFACE + + + IFACE is the network interface on which the + CTDB_NATGW_PUBLIC_IP will be configured. + + + No default. + + + + + + CTDB_NATGW_PUBLIC_IP=IPADDR/MASK + + + IPADDR/MASK indicates the IP address that is used for + outgoing traffic (originating from + CTDB_NATGW_PRIVATE_NETWORK) on the NAT gateway leader + node. This must not be a + configured public IP address. + + + No default. + + + + + + CTDB_NATGW_STATIC_ROUTES=IPADDR/MASK[@GATEWAY] ... + + + Each IPADDR/MASK identifies a network or host to which + NATGW should create a fallback route, instead of + creating a single default route. This can be used when + there is already a default route, via an interface that + can not reach required infrastructure, that overrides + the NAT gateway default route. + + + If GATEWAY is specified then the corresponding route on + the NATGW leader node will be via GATEWAY. Such routes + are created even if + CTDB_NATGW_DEFAULT_GATEWAY is not + specified. If GATEWAY is not specified for some + networks then routes are only created on the NATGW + leader node for those networks if + CTDB_NATGW_DEFAULT_GATEWAY is + specified. + + + This should be used with care to avoid causing traffic + to unnecessarily double-hop through the NAT gateway + leader, even when a node is hosting public IP addresses. + Each specified network or host should probably have a + corresponding automatically created link route or static + route to avoid this. + + + No default. + + + + + + + + Example + +CTDB_NATGW_NODES=/usr/local/etc/ctdb/natgw_nodes +CTDB_NATGW_PRIVATE_NETWORK=192.168.1.0/24 +CTDB_NATGW_DEFAULT_GATEWAY=10.0.0.1 +CTDB_NATGW_PUBLIC_IP=10.0.0.227/24 +CTDB_NATGW_PUBLIC_IFACE=eth0 + + + + A variation that ensures that infrastructure (ADS, DNS, ...) + directly attached to the public network (10.0.0.0/24) is + always reachable would look like this: + + +CTDB_NATGW_NODES=/usr/local/etc/ctdb/natgw_nodes +CTDB_NATGW_PRIVATE_NETWORK=192.168.1.0/24 +CTDB_NATGW_PUBLIC_IP=10.0.0.227/24 +CTDB_NATGW_PUBLIC_IFACE=eth0 +CTDB_NATGW_STATIC_ROUTES=10.0.0.0/24 + + + Note that CTDB_NATGW_DEFAULT_GATEWAY is + not specified. + + + + + + + 13.per_ip_routing + + + Provides CTDB's policy routing functionality. + + + + A node running CTDB may be a component of a complex network + topology. In particular, public addresses may be spread + across several different networks (or VLANs) and it may not be + possible to route packets from these public addresses via the + system's default route. Therefore, CTDB has support for + policy routing via the 13.per_ip_routing + eventscript. This allows routing to be specified for packets + sourced from each public address. The routes are added and + removed as CTDB moves public addresses between nodes. + + + + For more information, see the POLICY + ROUTING section in + ctdb + 7. + + + + + CTDB_PER_IP_ROUTING_CONF=FILENAME + + + FILENAME contains elements for constructing the desired + routes for each source address. + + + + The special FILENAME value + __auto_link_local__ indicates that no + configuration file is provided and that CTDB should + generate reasonable link-local routes for each public IP + address. + + + + File format: + + IPADDR DEST-IPADDR/MASK GATEWAY-IPADDR + + + + + No default, usually + /usr/local/etc/ctdb/policy_routing + when enabled. + + + + + + + CTDB_PER_IP_ROUTING_RULE_PREF=NUM + + + + NUM sets the priority (or preference) for the routing + rules that are added by CTDB. + + + + This should be (strictly) greater than 0 and (strictly) + less than 32766. A priority of 100 is recommended, unless + this conflicts with a priority already in use on the + system. See + ip + 8, for more details. + + + + + + + CTDB_PER_IP_ROUTING_TABLE_ID_LOW=LOW-NUM, + CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=HIGH-NUM + + + + CTDB determines a unique routing table number to use for + the routing related to each public address. LOW-NUM and + HIGH-NUM indicate the minimum and maximum routing table + numbers that are used. + + + + ip + 8 uses some + reserved routing table numbers below 255. Therefore, + CTDB_PER_IP_ROUTING_TABLE_ID_LOW should be (strictly) + greater than 255. + + + + CTDB uses the standard file + /etc/iproute2/rt_tables to maintain + a mapping between the routing table numbers and labels. + The label for a public address + ADDR will look like + ctdb.addr. This means that + the associated rules and routes are easy to read (and + manipulate). + + + + No default, usually 1000 and 9000. + + + + + + + Example + +CTDB_PER_IP_ROUTING_CONF=/usr/local/etc/ctdb/policy_routing +CTDB_PER_IP_ROUTING_RULE_PREF=100 +CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000 +CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=9000 + + + + + + + 91.lvs + + + Provides CTDB's LVS functionality. + + + + For a general description see the LVS + section in ctdb + 7. + + + + + + + CTDB_LVS_NODES=FILENAME + + + + FILENAME contains the list of nodes that belong to the + same LVS group. + + + File format: + +IPADDR follower-only + + + + IPADDR is the private IP address of each node in the LVS + group. + + + If "follower-only" is specified then the corresponding node + can not be the LVS leader node. In this case + CTDB_LVS_PUBLIC_IFACE and + CTDB_LVS_PUBLIC_IP are optional and + unused. + + + No default, usually + /usr/local/etc/ctdb/lvs_nodes when enabled. + + + + + + + CTDB_LVS_PUBLIC_IFACE=INTERFACE + + + + INTERFACE is the network interface that clients will use + to connection to CTDB_LVS_PUBLIC_IP. + This is optional for follower-only nodes. + No default. + + + + + + + CTDB_LVS_PUBLIC_IP=IPADDR + + + + CTDB_LVS_PUBLIC_IP is the LVS public address. No + default. + + + + + + + + + + + SERVICE CONFIGURATION + + + CTDB can be configured to manage and/or monitor various NAS (and + other) services via its eventscripts. + + + + In the simplest case CTDB will manage a service. This means the + service will be started and stopped along with CTDB, CTDB will + monitor the service and CTDB will do any required + reconfiguration of the service when public IP addresses are + failed over. + + + + 20.multipathd + + + Provides CTDB's Linux multipathd service management. + + + + It can monitor multipath devices to ensure that active paths + are available. + + + + + + CTDB_MONITOR_MPDEVICES=MP-DEVICE-LIST + + + + MP-DEVICE-LIST is a list of multipath devices for CTDB to monitor? + + + No default. + + + + + + + + 31.clamd + + + This event script provide CTDB's ClamAV anti-virus service + management. + + + + This eventscript is not enabled by default. Use ctdb + enablescript to enable it. + + + + + + + CTDB_CLAMD_SOCKET=FILENAME + + + + FILENAME is the socket to monitor ClamAV. + + + No default. + + + + + + + + + + 48.netbios + + + Provides CTDB's NetBIOS service management. + + + + + + CTDB_SERVICE_NMB=SERVICE + + + + Distribution specific SERVICE for managing nmbd. + + + Default is distribution-dependant. + + + + + + + + + + 49.winbind + + + Provides CTDB's Samba winbind service management. + + + + + + + CTDB_SERVICE_WINBIND=SERVICE + + + + Distribution specific SERVICE for managing winbindd. + + + Default is "winbind". + + + + + + + + + + 50.samba + + + Provides the core of CTDB's Samba file service management. + + + + + + + CTDB_SAMBA_CHECK_PORTS=PORT-LIST + + + + When monitoring Samba, check TCP ports in + space-separated PORT-LIST. + + + Default is to monitor ports that Samba is configured to listen on. + + + + + + + CTDB_SAMBA_SKIP_SHARE_CHECK=yes|no + + + + As part of monitoring, should CTDB skip the check for + the existence of each directory configured as share in + Samba. This may be desirable if there is a large number + of shares. + + + Default is no. + + + + + + + CTDB_SERVICE_SMB=SERVICE + + + + Distribution specific SERVICE for managing smbd. + + + Default is distribution-dependant. + + + + + + + + + + 60.nfs + + + This event script (along with 06.nfs) provides CTDB's NFS + service management. + + + + This includes parameters for the kernel NFS server. + Alternative NFS subsystems (such as NFS-Ganesha) + can be integrated using CTDB_NFS_CALLOUT. + + + + + + + CTDB_NFS_CALLOUT=COMMAND + + + + COMMAND specifies the path to a callout to handle + interactions with the configured NFS system, including + startup, shutdown, monitoring. + + + Default is the included + nfs-linux-kernel-callout. + + + + + + + CTDB_NFS_CHECKS_DIR=DIRECTORY + + + + Specifies the path to a DIRECTORY containing files that + describe how to monitor the responsiveness of NFS RPC + services. See the README file for this directory for an + explanation of the contents of these "check" files. + + + CTDB_NFS_CHECKS_DIR can be used to point to different + sets of checks for different NFS servers. + + + One way of using this is to have it point to, say, + /usr/local/etc/ctdb/nfs-checks-enabled.d + and populate it with symbolic links to the desired check + files. This avoids duplication and is upgrade-safe. + + + Default is + /usr/local/etc/ctdb/nfs-checks.d, + which contains NFS RPC checks suitable for Linux kernel + NFS. + + + + + + + CTDB_NFS_SKIP_SHARE_CHECK=yes|no + + + + As part of monitoring, should CTDB skip the check for + the existence of each directory exported via NFS. This + may be desirable if there is a large number of exports. + + + Default is no. + + + + + + + CTDB_RPCINFO_LOCALHOST=IPADDR|HOSTNAME + + + + IPADDR or HOSTNAME indicates the address that + rpcinfo should connect to when doing + rpcinfo check on IPv4 RPC service during + monitoring. Optimally this would be "localhost". + However, this can add some performance overheads. + + + Default is "127.0.0.1". + + + + + + + CTDB_RPCINFO_LOCALHOST6=IPADDR|HOSTNAME + + + + IPADDR or HOSTNAME indicates the address that + rpcinfo should connect to when doing + rpcinfo check on IPv6 RPC service + during monitoring. Optimally this would be "localhost6" + (or similar). However, this can add some performance + overheads. + + + Default is "::1". + + + + + + + CTDB_NFS_STATE_FS_TYPE=TYPE + + + + The type of filesystem used for a clustered NFS' shared + state. No default. + + + + + + + CTDB_NFS_STATE_MNT=DIR + + + + The directory where a clustered NFS' shared state will be + located. No default. + + + + + + + + + + 70.iscsi + + + Provides CTDB's Linux iSCSI tgtd service management. + + + + + + + CTDB_START_ISCSI_SCRIPTS=DIRECTORY + + + + DIRECTORY on shared storage containing scripts to start + tgtd for each public IP address. + + + No default. + + + + + + + + + + + DATABASE SETUP + + + + CTDB checks the consistency of databases during startup. + + + + 00.ctdb + + + + + CTDB_MAX_CORRUPT_DB_BACKUPS=NUM + + + NUM is the maximum number of volatile TDB database + backups to be kept (for each database) when a corrupt + database is found during startup. Volatile TDBs are + zeroed during startup so backups are needed to debug + any corruption that occurs before a restart. + + + Default is 10. + + + + + + + + + + + SYSTEM RESOURCE MONITORING + + + + 05.system + + + + Provides CTDB's filesystem and memory usage monitoring. + + + + CTDB can experience seemingly random (performance and other) + issues if system resources become too constrained. Options in + this section can be enabled to allow certain system resources + to be checked. They allows warnings to be logged and nodes to + be marked unhealthy when system resource usage reaches the + configured thresholds. + + + + Some checks are enabled by default. It is recommended that + these checks remain enabled or are augmented by extra checks. + There is no supported way of completely disabling the checks. + + + + + + + CTDB_MONITOR_FILESYSTEM_USAGE=FS-LIMIT-LIST + + + + FS-LIMIT-LIST is a space-separated list of + FILESYSTEM:WARN_LIMIT:UNHEALTHY_LIMIT + triples indicating that warnings should be logged if the + space used on FILESYSTEM reaches WARN_LIMIT%. If usage + reaches UNHEALTHY_LIMIT then the node should be flagged + unhealthy. Either WARN_LIMIT or UNHEALTHY_LIMIT may be + left blank, meaning that check will be omitted. + + + + Default is to warn for each filesystem containing a + database directory + (volatile database directory, + persistent database directory, + state database directory) + with a threshold of 90%. + + + + + + + CTDB_MONITOR_MEMORY_USAGE=MEM-LIMITS + + + + MEM-LIMITS takes the form + WARN_LIMIT:UNHEALTHY_LIMIT + indicating that warnings should be logged if memory + usage reaches WARN_LIMIT%. If usage reaches + UNHEALTHY_LIMIT then the node should be flagged + unhealthy. Either WARN_LIMIT or UNHEALTHY_LIMIT may be + left blank, meaning that check will be omitted. + + + Default is 80, so warnings will be logged when memory + usage reaches 80%. + + + + + + + + + + + + EVENT SCRIPT DEBUGGING + + + + debug-hung-script.sh + + + + + + CTDB_DEBUG_HUNG_SCRIPT_STACKPAT=REGEXP + + + REGEXP specifies interesting processes for which stack + traces should be logged when debugging hung eventscripts + and those processes are matched in pstree output. + REGEXP is an extended regexp so choices are separated by + pipes ('|'). However, REGEXP should not contain + parentheses. See also the ctdb.conf + 5 + [event] "debug script" option. + + + Default is "exportfs|rpcinfo". + + + + + + + + + + + FILES + + + /usr/local/etc/ctdb/script.options + + + + + SEE ALSO + + ctdbd + 1, + + ctdb + 7, + + + + + + + + + This documentation was written by + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ctdb-statistics.7.xml b/ctdb/doc/ctdb-statistics.7.xml new file mode 100644 index 0000000..387852d --- /dev/null +++ b/ctdb/doc/ctdb-statistics.7.xml @@ -0,0 +1,689 @@ + + + + + + + ctdb-statistics + 7 + ctdb + CTDB - clustered TDB database + + + + ctdb-statistics + CTDB statistics output + + + + OVERALL STATISTICS + + + CTDB maintains information about various messages communicated + and some of the important operations per node. See the + ctdb + 1 commands + statistics and statisticsreset + for displaying statistics. + + + + Example: ctdb statistics + +CTDB version 1 +Current time of statistics : Fri Sep 12 13:32:32 2014 +Statistics collected since : (000 01:49:20) Fri Sep 12 11:43:12 2014 + num_clients 6 + frozen 0 + recovering 0 + num_recoveries 2 + client_packets_sent 281293 + client_packets_recv 296317 + node_packets_sent 452387 + node_packets_recv 182394 + keepalive_packets_sent 3927 + keepalive_packets_recv 3928 + node + req_call 48605 + reply_call 1 + req_dmaster 23404 + reply_dmaster 24917 + reply_error 0 + req_message 958 + req_control 197513 + reply_control 153705 + client + req_call 130866 + req_message 770 + req_control 168921 + timeouts + call 0 + control 0 + traverse 0 + locks + num_calls 220 + num_current 0 + num_pending 0 + num_failed 0 + total_calls 130866 + pending_calls 0 + childwrite_calls 1 + pending_childwrite_calls 0 + memory_used 334490 + max_hop_count 18 + total_ro_delegations 2 + total_ro_revokes 2 + hop_count_buckets: 42816 5464 26 1 0 0 0 0 0 0 0 0 0 0 0 0 + lock_buckets: 9 165 14 15 7 2 2 0 0 0 0 0 0 0 0 0 + locks_latency MIN/AVG/MAX 0.000685/0.160302/6.369342 sec out of 214 + reclock_ctdbd MIN/AVG/MAX 0.004940/0.004969/0.004998 sec out of 2 + reclock_recd MIN/AVG/MAX 0.000000/0.000000/0.000000 sec out of 0 + call_latency MIN/AVG/MAX 0.000006/0.000719/4.562991 sec out of 126626 + childwrite_latency MIN/AVG/MAX 0.014527/0.014527/0.014527 sec out of 1 + + + + + CTDB version + + Version of the ctdb protocol used by the node. + + + + + Current time of statistics + + Time when the statistics are generated. + + + This is useful when collecting statistics output periodically + for post-processing. + + + + + Statistics collected since + + Time when ctdb was started or the last time statistics was reset. + The output shows the duration and the timestamp. + + + + + num_clients + + Number of processes currently connected to CTDB's unix socket. + This includes recovery daemon, ctdb tool and samba processes + (smbd, winbindd). + + + + + frozen + + 1 if the databases are currently frozen, 0 otherwise. + + + + + recovering + + 1 if recovery is active, 0 otherwise. + + + + + num_recoveries + + Number of recoveries since the start of ctdb or since the last + statistics reset. + + + + + client_packets_sent + + Number of packets sent to client processes via unix domain socket. + + + + + client_packets_recv + + Number of packets received from client processes via unix domain socket. + + + + + node_packets_sent + + Number of packets sent to the other nodes in the cluster via TCP. + + + + + node_packets_recv + + Number of packets received from the other nodes in the cluster via TCP. + + + + + keepalive_packets_sent + + Number of keepalive messages sent to other nodes. + + + CTDB periodically sends keepalive messages to other nodes. + See KeepaliveInterval tunable in + ctdb-tunables + 7 for more details. + + + + + keepalive_packets_recv + + Number of keepalive messages received from other nodes. + + + + + node + + This section lists various types of messages processed which + originated from other nodes via TCP. + + + + req_call + + Number of REQ_CALL messages from the other nodes. + + + + + reply_call + + Number of REPLY_CALL messages from the other nodes. + + + + + req_dmaster + + Number of REQ_DMASTER messages from the other nodes. + + + + + reply_dmaster + + Number of REPLY_DMASTER messages from the other nodes. + + + + + reply_error + + Number of REPLY_ERROR messages from the other nodes. + + + + + req_message + + Number of REQ_MESSAGE messages from the other nodes. + + + + + req_control + + Number of REQ_CONTROL messages from the other nodes. + + + + + reply_control + + Number of REPLY_CONTROL messages from the other nodes. + + + + + req_tunnel + + Number of REQ_TUNNEL messages from the other nodes. + + + + + + + client + + This section lists various types of messages processed which + originated from clients via unix domain socket. + + + + req_call + + Number of REQ_CALL messages from the clients. + + + + + req_message + + Number of REQ_MESSAGE messages from the clients. + + + + + req_control + + Number of REQ_CONTROL messages from the clients. + + + + + req_tunnel + + Number of REQ_TUNNEL messages from the clients. + + + + + + + timeouts + + This section lists timeouts occurred when sending various messages. + + + + call + + Number of timeouts for REQ_CALL messages. + + + + + control + + Number of timeouts for REQ_CONTROL messages. + + + + + traverse + + Number of timeouts for database traverse operations. + + + + + + locks + + This section lists locking statistics. + + + + num_calls + + Number of completed lock calls. This includes database locks + and record locks. + + + + + num_current + + Number of scheduled lock calls. This includes database locks + and record locks. + + + + + num_pending + + Number of queued lock calls. This includes database locks and + record locks. + + + + + num_failed + + Number of failed lock calls. This includes database locks and + record locks. + + + + + + + total_calls + + Number of req_call messages processed from clients. This number + should be same as client --> req_call. + + + + + pending_calls + + Number of req_call messages which are currently being processed. + This number indicates the number of record migrations in flight. + + + + + childwrite_calls + + Number of record update calls. Record update calls are used to + update a record under a transaction. + + + + + pending_childwrite_calls + + Number of record update calls currently active. + + + + + memory_used + + The amount of memory in bytes currently used by CTDB using + talloc. This includes all the memory used for CTDB's internal + data structures. This does not include the memory mapped TDB + databases. + + + + + max_hop_count + + The maximum number of hops required for a record migration request + to obtain the record. High numbers indicate record contention. + + + + + total_ro_delegations + + Number of readonly delegations created. + + + + + total_ro_revokes + + Number of readonly delegations that were revoked. The difference + between total_ro_revokes and total_ro_delegations gives the + number of currently active readonly delegations. + + + + + hop_count_buckets + + Distribution of migration requests based on hop counts values. + Buckets are 0, < 2, < 4, < 8, + < 16, < 32, < 64, < 128, + < 256, < 512, < 1024, < 2048, + < 4096, < 8192, < 16384, ≥ 16384. + + + + + lock_buckets + + Distribution of record lock requests based on time required to + obtain locks. Buckets are < 1ms, < 10ms, + < 100ms, < 1s, < 2s, < 4s, + < 8s, < 16s, < 32s, < 64s, + ≥ 64s. + + + + + locks_latency + + The minimum, the average and the maximum time (in seconds) + required to obtain record locks. + + + + + reclock_ctdbd + + The minimum, the average and the maximum time (in seconds) + required to check if recovery lock is still held by recovery + daemon when recovery mode is changed. This check is done in ctdb daemon. + + + + + reclock_recd + + The minimum, the average and the maximum time (in seconds) + required to check if recovery lock is still held by recovery + daemon during recovery. This check is done in recovery daemon. + + + + + call_latency + + The minimum, the average and the maximum time (in seconds) required + to process a REQ_CALL message from client. This includes the time + required to migrate a record from remote node, if the record is + not available on the local node. + + + + + childwrite_latency + Default: 0 + + The minimum, the average and the maximum time (in seconds) + required to update records under a transaction. + + + + + + DATABASE STATISTICS + + + CTDB maintains per database statistics about important operations. + See the ctdb + 1 command + dbstatistics for displaying database statistics. + + + + Example: ctdb dbstatistics notify_index.tdb + +DB Statistics: notify_index.tdb + ro_delegations 0 + ro_revokes 0 + locks + total 131 + failed 0 + current 0 + pending 0 + hop_count_buckets: 9890 5454 26 1 0 0 0 0 0 0 0 0 0 0 0 0 + lock_buckets: 4 117 10 0 0 0 0 0 0 0 0 0 0 0 0 0 + locks_latency MIN/AVG/MAX 0.000683/0.004198/0.014730 sec out of 131 + Num Hot Keys: 3 + Count:7 Key:2f636c75737465726673 + Count:18 Key:2f636c757374657266732f64617461 + Count:7 Key:2f636c757374657266732f646174612f636c69656e7473 + + + + + DB Statistics + + Name of the database. + + + + + ro_delegations + + Number of readonly delegations created in the database. + + + + + ro_revokes + + Number of readonly delegations revoked. The difference in + ro_delegations and ro_revokes indicates the currently active + readonly delegations. + + + + + locks + + This section lists locking statistics. + + + + total + + Number of completed lock calls. This includes database locks + and record locks. + + + + + failed + + Number of failed lock calls. This includes database locks and + record locks. + + + + + current + + Number of scheduled lock calls. This includes database locks + and record locks. + + + + + pending + + Number of queued lock calls. This includes database locks and + record locks. + + + + + + + hop_count_buckets + + Distribution of migration requests based on hop counts values. + Buckets are 0, < 2, < 4, < 8, + < 16, < 32, < 64, < 128, + < 256, < 512, < 1024, < 2048, + < 4096, < 8192, < 16384, ≥ 16384. + + + + + lock_buckets + + Distribution of record lock requests based on time required to + obtain locks. Buckets are < 1ms, < 10ms, + < 100ms, < 1s, < 2s, < 4s, + < 8s, < 16s, < 32s, < 64s, + ≥ 64s. + + + + + locks_latency + + The minimum, the average and the maximum time (in seconds) + required to obtain record locks. + + + + + Num Hot Keys + + Number of contended records determined by hop count. CTDB keeps + track of top 10 hot records and the output shows hex encoded + keys for the hot records. + + + + + + SEE ALSO + + ctdb + 1, + + ctdbd + 1, + + ctdb-tunables + 7, + + + + + + + + + This documentation was written by + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + + diff --git a/ctdb/doc/ctdb-tunables.7.xml b/ctdb/doc/ctdb-tunables.7.xml new file mode 100644 index 0000000..700709e --- /dev/null +++ b/ctdb/doc/ctdb-tunables.7.xml @@ -0,0 +1,772 @@ + + + + + + + ctdb-tunables + 7 + ctdb + CTDB - clustered TDB database + + + + ctdb-tunables + CTDB tunable configuration variables + + + + DESCRIPTION + + + CTDB's behaviour can be configured by setting run-time tunable + variables. This lists and describes all tunables. See the + ctdb + 1 + listvars, setvar and + getvar commands for more details. + + + + Unless otherwise stated, tunables should be set to the same + value on all nodes. Setting tunables to different values across + nodes may produce unexpected results. Future releases may set + (some or most) tunables globally across the cluster but doing so + is currently a manual process. + + + + Tunables can be set at startup from the + /usr/local/etc/ctdb/ctdb.tunables + configuration file. + + +TUNABLE=VALUE + + + Comment lines beginning with '#' are permitted. Whitespace may + be used for formatting/alignment. VALUE must be a non-negative + integer and must be the last thing on a line (i.e. no trailing + garbage, trailing comments are not permitted). + + + + For example: + + +MonitorInterval=20 + + + + + The available tunable variables are listed alphabetically below. + + + + AllowClientDBAttach + Default: 1 + + When set to 0, clients are not allowed to attach to any databases. + This can be used to temporarily block any new processes from + attaching to and accessing the databases. This is mainly used + for detaching a volatile database using 'ctdb detach'. + + + + + AllowMixedVersions + Default: 0 + + CTDB will not allow incompatible versions to co-exist in + a cluster. If a version mismatch is found, then losing CTDB + will shutdown. To disable the incompatible version check, + set this tunable to 1. + + + For version checking, CTDB uses major and minor version. + For example, CTDB 4.6.1 and CTDB 4.6.2 are matching versions; + CTDB 4.5.x and CTDB 4.6.y do not match. + + + CTDB with version check support will lose to CTDB without + version check support. Between two different CTDB versions with + version check support, one running for less time will lose. + If the running time for both CTDB versions with version check + support is equal (to seconds), then the older version will lose. + The losing CTDB daemon will shutdown. + + + + + AllowUnhealthyDBRead + Default: 0 + + When set to 1, ctdb allows database traverses to read unhealthy + databases. By default, ctdb does not allow reading records from + unhealthy databases. + + + + + ControlTimeout + Default: 60 + + This is the default setting for timeout for when sending a + control message to either the local or a remote ctdb daemon. + + + + + DatabaseHashSize + Default: 100001 + + Number of the hash chains for the local store of the tdbs that + ctdb manages. + + + + + DatabaseMaxDead + Default: 5 + + Maximum number of dead records per hash chain for the tdb databses + managed by ctdb. + + + + + DBRecordCountWarn + Default: 100000 + + When set to non-zero, ctdb will log a warning during recovery if + a database has more than this many records. This will produce a + warning if a database grows uncontrollably with orphaned records. + + + + + DBRecordSizeWarn + Default: 10000000 + + When set to non-zero, ctdb will log a warning during recovery + if a single record is bigger than this size. This will produce + a warning if a database record grows uncontrollably. + + + + + DBSizeWarn + Default: 1000000000 + + When set to non-zero, ctdb will log a warning during recovery if + a database size is bigger than this. This will produce a warning + if a database grows uncontrollably. + + + + + DeferredAttachTO + Default: 120 + + When databases are frozen we do not allow clients to attach to + the databases. Instead of returning an error immediately to the + client, the attach request from the client is deferred until + the database becomes available again at which stage we respond + to the client. + + + This timeout controls how long we will defer the request from the + client before timing it out and returning an error to the client. + + + + + ElectionTimeout + Default: 3 + + The number of seconds to wait for the election of recovery + master to complete. If the election is not completed during this + interval, then that round of election fails and ctdb starts a + new election. + + + + + EnableBans + Default: 1 + + This parameter allows ctdb to ban a node if the node is misbehaving. + + + When set to 0, this disables banning completely in the cluster + and thus nodes can not get banned, even it they break. Don't + set to 0 unless you know what you are doing. + + + + + EventScriptTimeout + Default: 30 + + Maximum time in seconds to allow an event to run before timing + out. This is the total time for all enabled scripts that are + run for an event, not just a single event script. + + + Note that timeouts are ignored for some events ("takeip", + "releaseip", "startrecovery", "recovered") and converted to + success. The logic here is that the callers of these events + implement their own additional timeout. + + + + + FetchCollapse + Default: 1 + + This parameter is used to avoid multiple migration requests for + the same record from a single node. All the record requests for + the same record are queued up and processed when the record is + migrated to the current node. + + + When many clients across many nodes try to access the same record + at the same time this can lead to a fetch storm where the record + becomes very active and bounces between nodes very fast. This + leads to high CPU utilization of the ctdbd daemon, trying to + bounce that record around very fast, and poor performance. + This can improve performance and reduce CPU utilization for + certain workloads. + + + + + HopcountMakeSticky + Default: 50 + + For database(s) marked STICKY (using 'ctdb setdbsticky'), + any record that is migrating so fast that hopcount + exceeds this limit is marked as STICKY record for + StickyDuration seconds. This means that + after each migration the sticky record will be kept on the node + StickyPindownmilliseconds and prevented from + being migrated off the node. + + + This will improve performance for certain workloads, such as + locking.tdb if many clients are opening/closing the same file + concurrently. + + + + + IPAllocAlgorithm + Default: 2 + + Selects the algorithm that CTDB should use when doing public + IP address allocation. Meaningful values are: + + + + 0 + + + Deterministic IP address allocation. + + + This is a simple and fast option. However, it can cause + unnecessary address movement during fail-over because + each address has a "home" node. Works badly when some + nodes do not have any addresses defined. Should be used + with care when addresses are defined across multiple + networks. + + + + + 1 + + + Non-deterministic IP address allocation. + + + This is a relatively fast option that attempts to do a + minimise unnecessary address movements. Addresses do + not have a "home" node. Rebalancing is limited but it + usually adequate. Works badly when addresses are + defined across multiple networks. + + + + + 2 + + + LCP2 IP address allocation. + + + Uses a heuristic to assign addresses defined across + multiple networks, usually balancing addresses on each + network evenly across nodes. Addresses do not have a + "home" node. Minimises unnecessary address movements. + The algorithm is complex, so is slower than other + choices for a large number of addresses. However, it + can calculate an optimal assignment of 900 addresses in + under 10 seconds on modern hardware. + + + + + + If the specified value is not one of these then the default + will be used. + + + + + KeepaliveInterval + Default: 5 + + How often in seconds should the nodes send keep-alive packets to + each other. + + + + + KeepaliveLimit + Default: 5 + + After how many keepalive intervals without any traffic should + a node wait until marking the peer as DISCONNECTED. + + + If a node has hung, it can take + KeepaliveInterval * + (KeepaliveLimit + 1) seconds before + ctdb determines that the node is DISCONNECTED and performs + a recovery. This limit should not be set too high to enable + early detection and avoid any application timeouts (e.g. SMB1) + to kick in before the fail over is completed. + + + + + LockProcessesPerDB + Default: 200 + + This is the maximum number of lock helper processes ctdb will + create for obtaining record locks. When ctdb cannot get a record + lock without blocking, it creates a helper process that waits + for the lock to be obtained. + + + + + LogLatencyMs + Default: 0 + + When set to non-zero, ctdb will log if certains operations + take longer than this value, in milliseconds, to complete. + These operations include "process a record request from client", + "take a record or database lock", "update a persistent database + record" and "vacuum a database". + + + + + MaxQueueDropMsg + Default: 1000000 + + This is the maximum number of messages to be queued up for + a client before ctdb will treat the client as hung and will + terminate the client connection. + + + + + MonitorInterval + Default: 15 + + How often should ctdb run the 'monitor' event in seconds to check + for a node's health. + + + + + MonitorTimeoutCount + Default: 20 + + How many 'monitor' events in a row need to timeout before a node + is flagged as UNHEALTHY. This setting is useful if scripts can + not be written so that they do not hang for benign reasons. + + + + + NoIPFailback + Default: 0 + + When set to 1, ctdb will not perform failback of IP addresses + when a node becomes healthy. When a node becomes UNHEALTHY, + ctdb WILL perform failover of public IP addresses, but when the + node becomes HEALTHY again, ctdb will not fail the addresses back. + + + Use with caution! Normally when a node becomes available to the + cluster ctdb will try to reassign public IP addresses onto the + new node as a way to distribute the workload evenly across the + clusternode. Ctdb tries to make sure that all running nodes have + approximately the same number of public addresses it hosts. + + + When you enable this tunable, ctdb will no longer attempt to + rebalance the cluster by failing IP addresses back to the new + nodes. An unbalanced cluster will therefore remain unbalanced + until there is manual intervention from the administrator. When + this parameter is set, you can manually fail public IP addresses + over to the new node(s) using the 'ctdb moveip' command. + + + + + NoIPTakeover + Default: 0 + + When set to 1, ctdb will not allow IP addresses to be failed + over to other nodes. Any IP addresses already hosted on + healthy nodes will remain. Any IP addresses hosted on + unhealthy nodes will be released by unhealthy nodes and will + become un-hosted. + + + + + PullDBPreallocation + Default: 10*1024*1024 + + This is the size of a record buffer to pre-allocate for sending + reply to PULLDB control. Usually record buffer starts with size + of the first record and gets reallocated every time a new record + is added to the record buffer. For a large number of records, + this can be very inefficient to grow the record buffer one record + at a time. + + + + + QueueBufferSize + Default: 1024 + + This is the maximum amount of data (in bytes) ctdb will read + from a socket at a time. + + + For a busy setup, if ctdb is not able to process the TCP sockets + fast enough (large amount of data in Recv-Q for tcp sockets), + then this tunable value should be increased. However, large + values can keep ctdb busy processing packets and prevent ctdb + from handling other events. + + + + + RecBufferSizeLimit + Default: 1000000 + + This is the limit on the size of the record buffer to be sent + in various controls. This limit is used by new controls used + for recovery and controls used in vacuuming. + + + + + RecdFailCount + Default: 10 + + If the recovery daemon has failed to ping the main daemon for + this many consecutive intervals, the main daemon will consider + the recovery daemon as hung and will try to restart it to recover. + + + + + RecdPingTimeout + Default: 60 + + If the main daemon has not heard a "ping" from the recovery daemon + for this many seconds, the main daemon will log a message that + the recovery daemon is potentially hung. This also increments a + counter which is checked against RecdFailCount + for detection of hung recovery daemon. + + + + + RecLockLatencyMs + Default: 1000 + + When using a reclock file for split brain prevention, if set + to non-zero this tunable will make the recovery daemon log a + message if the fcntl() call to lock/testlock the recovery file + takes longer than this number of milliseconds. + + + + + RecoverInterval + Default: 1 + + How frequently in seconds should the recovery daemon perform the + consistency checks to determine if it should perform a recovery. + + + + + RecoverTimeout + Default: 120 + + This is the default setting for timeouts for controls when sent + from the recovery daemon. We allow longer control timeouts from + the recovery daemon than from normal use since the recovery + daemon often use controls that can take a lot longer than normal + controls. + + + + + RecoveryBanPeriod + Default: 300 + + The duration in seconds for which a node is banned if the node + fails during recovery. After this time has elapsed the node will + automatically get unbanned and will attempt to rejoin the cluster. + + + A node usually gets banned due to real problems with the node. + Don't set this value too small. Otherwise, a problematic node + will try to re-join cluster too soon causing unnecessary recoveries. + + + + + RecoveryDropAllIPs + Default: 120 + + If a node is stuck in recovery, or stopped, or banned, for this + many seconds, then ctdb will release all public addresses on + that node. + + + + + RecoveryGracePeriod + Default: 120 + + During recoveries, if a node has not caused recovery failures + during the last grace period in seconds, any records of + transgressions that the node has caused recovery failures will be + forgiven. This resets the ban-counter back to zero for that node. + + + + + RepackLimit + Default: 10000 + + During vacuuming, if the number of freelist records are more than + RepackLimit, then the database is repacked + to get rid of the freelist records to avoid fragmentation. + + + + + RerecoveryTimeout + Default: 10 + + Once a recovery has completed, no additional recoveries are + permitted until this timeout in seconds has expired. + + + + + SeqnumInterval + Default: 1000 + + Some databases have seqnum tracking enabled, so that samba will + be able to detect asynchronously when there has been updates + to the database. Every time a database is updated its sequence + number is increased. + + + This tunable is used to specify in milliseconds how frequently + ctdb will send out updates to remote nodes to inform them that + the sequence number is increased. + + + + + StatHistoryInterval + Default: 1 + + Granularity of the statistics collected in the statistics + history. This is reported by 'ctdb stats' command. + + + + + StickyDuration + Default: 600 + + Once a record has been marked STICKY, this is the duration in + seconds, the record will be flagged as a STICKY record. + + + + + StickyPindown + Default: 200 + + Once a STICKY record has been migrated onto a node, it will be + pinned down on that node for this number of milliseconds. Any + request from other nodes to migrate the record off the node will + be deferred. + + + + + TakeoverTimeout + Default: 9 + + This is the duration in seconds in which ctdb tries to complete IP + failover. + + + + + TickleUpdateInterval + Default: 20 + + Every TickleUpdateInterval seconds, ctdb + synchronizes the client connection information across nodes. + + + + + TraverseTimeout + Default: 20 + + This is the duration in seconds for which a database traverse + is allowed to run. If the traverse does not complete during + this interval, ctdb will abort the traverse. + + + + + VacuumFastPathCount + Default: 60 + + During a vacuuming run, ctdb usually processes only the records + marked for deletion also called the fast path vacuuming. After + finishing VacuumFastPathCount number of fast + path vacuuming runs, ctdb will trigger a scan of complete database + for any empty records that need to be deleted. + + + + + VacuumInterval + Default: 10 + + Periodic interval in seconds when vacuuming is triggered for + volatile databases. + + + + + VacuumMaxRunTime + Default: 120 + + The maximum time in seconds for which the vacuuming process is + allowed to run. If vacuuming process takes longer than this + value, then the vacuuming process is terminated. + + + + + VerboseMemoryNames + Default: 0 + + When set to non-zero, ctdb assigns verbose names for some of + the talloc allocated memory objects. These names are visible + in the talloc memory report generated by 'ctdb dumpmemory'. + + + + + + + FILES> + + + /usr/local/etc/ctdb/ctdb.tunables + + + + + SEE ALSO + + ctdb + 1, + + ctdbd + 1, + + ctdb.conf + 5, + + ctdb + 7, + + + + + + + + + This documentation was written by + Ronnie Sahlberg, + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ctdb.1.xml b/ctdb/doc/ctdb.1.xml new file mode 100644 index 0000000..6f9a176 --- /dev/null +++ b/ctdb/doc/ctdb.1.xml @@ -0,0 +1,1863 @@ + + + + + + + + This documentation was written by + Ronnie Sahlberg, + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + + ctdb + 1 + ctdb + CTDB - clustered TDB database + + + + ctdb + CTDB management utility + + + + + ctdb + OPTION + COMMAND + COMMAND-ARGS + + + + + DESCRIPTION + + ctdb is a utility to view and manage a CTDB cluster. + + + + The following terms are used when referring to nodes in a + cluster: + + + PNN + + + Physical Node Number. The physical node number is an + integer that describes the node in the cluster. The + first node has physical node number 0. in a cluster. + + + + + PNN-LIST + + + This is either a single PNN, a comma-separate list of PNNs + or "all". + + + + + + + + Commands that reference a database use the following terms: + + + DB + + + This is either a database name, such as + locking.tdb or a database ID such + as "0x42fe72c5". + + + + + DB-LIST + + + A space separated list of at least one + DB. + + + + + + + + + OPTIONS + + + -n PNN + + + The node specified by PNN should be queried for the + requested information. Default is to query the daemon + running on the local host. + + + + + -Y + + + Produce output in machine readable form for easier parsing + by scripts. This uses a field delimiter of ':'. Not all + commands support this option. + + + + + -x SEPARATOR + + + Use SEPARATOR to delimit fields in machine readable output. + This implies -Y. + + + + + -X + + + Produce output in machine readable form for easier parsing + by scripts. This uses a field delimiter of '|'. Not all + commands support this option. + + + This is equivalent to "-x|" and avoids some shell quoting + issues. + + + + + -t TIMEOUT + + + Indicates that ctdb should wait up to TIMEOUT seconds for + a response to most commands sent to the CTDB daemon. The + default is 10 seconds. + + + + + -T TIMELIMIT + + + Indicates that TIMELIMIT is the maximum run time (in + seconds) for the ctdb command. When TIMELIMIT is exceeded + the ctdb command will terminate with an error. The default + is 120 seconds. + + + + + -? --help + + + Print some help text to the screen. + + + + + --usage + + + Print usage information to the screen. + + + + + -d --debug=DEBUGLEVEL + + + Change the debug level for the command. Default is NOTICE. + + + + + + + + + ADMINISTRATIVE COMMANDS + + These are commands used to monitor and administer a CTDB cluster. + + + + pnn + + This command displays the PNN of the current node. + + + + + status + + This command shows the current status of all CTDB nodes based + on information from the queried node. + + + + Note: If the queried node is INACTIVE then the status + might not be current. + + + + Node status + + This includes the number of physical nodes and the status of + each node. See ctdb + 7 for information + about node states. + + + + + Generation + + The generation id is a number that indicates the current generation + of a cluster instance. Each time a cluster goes through a + reconfiguration or a recovery its generation id will be changed. + + + This number does not have any particular meaning other than + to keep track of when a cluster has gone through a + recovery. It is a random number that represents the current + instance of a ctdb cluster and its databases. The CTDB + daemon uses this number internally to be able to tell when + commands to operate on the cluster and the databases was + issued in a different generation of the cluster, to ensure + that commands that operate on the databases will not survive + across a cluster database recovery. After a recovery, all + old outstanding commands will automatically become invalid. + + + Sometimes this number will be shown as "INVALID". This only means that + the ctdbd daemon has started but it has not yet merged with the cluster through a recovery. + All nodes start with generation "INVALID" and are not assigned a real + generation id until they have successfully been merged with a cluster + through a recovery. + + + + + Virtual Node Number (VNN) map + + Consists of the number of virtual nodes and mapping from + virtual node numbers to physical node numbers. Only nodes + that are participating in the VNN map can become lmaster for + database records. + + + + + Recovery mode + + This is the current recovery mode of the cluster. There are two possible modes: + + + NORMAL - The cluster is fully operational. + + + RECOVERY - The cluster databases have all been frozen, pausing all services while the cluster awaits a recovery process to complete. A recovery process should finish within seconds. If a cluster is stuck in the RECOVERY state this would indicate a cluster malfunction which needs to be investigated. + + + Once the leader detects an inconsistency, for example a node + becomes disconnected/connected, the recovery daemon will trigger a + cluster recovery process, where all databases are remerged across the + cluster. When this process starts, the leader will first + "freeze" all databases to prevent applications such as samba from + accessing the databases and it will also mark the recovery mode as + RECOVERY. + + + When the CTDB daemon starts up, it will start in RECOVERY + mode. Once the node has been merged into a cluster and all + databases have been recovered, the node mode will change into + NORMAL mode and the databases will be "thawed", allowing samba + to access the databases again. + + + + Leader + + This is the cluster node that is currently designated as the + leader. This node is responsible of monitoring the + consistency of the cluster and to perform the actual + recovery process when reqired. + + + Only one node at a time can be the designated leader. Which + node is designated the leader is decided by an election + process in the recovery daemons running on each node. + + + + + Example + +# ctdb status +Number of nodes:4 +pnn:0 192.168.2.200 OK (THIS NODE) +pnn:1 192.168.2.201 OK +pnn:2 192.168.2.202 OK +pnn:3 192.168.2.203 OK +Generation:1362079228 +Size:4 +hash:0 lmaster:0 +hash:1 lmaster:1 +hash:2 lmaster:2 +hash:3 lmaster:3 +Recovery mode:NORMAL (0) +Leader:0 + + + + + + nodestatus <optional><parameter>PNN-LIST</parameter></optional> + + This command is similar to the status + command. It displays the "node status" subset of output. The + main differences are: + + + + + + The exit code is the bitwise-OR of the flags for each + specified node, while ctdb status exits + with 0 if it was able to retrieve status for all nodes. + + + + + + ctdb status provides status information + for all nodes. ctdb nodestatus + defaults to providing status for only the current node. + If PNN-LIST is provided then status is given for + the indicated node(s). + + + + + + A common invocation in scripts is ctdb nodestatus + all to check whether all nodes in a cluster are + healthy. + + + + Example + +# ctdb nodestatus +pnn:0 10.0.0.30 OK (THIS NODE) + +# ctdb nodestatus all +Number of nodes:2 +pnn:0 10.0.0.30 OK (THIS NODE) +pnn:1 10.0.0.31 OK + + + + + + leader + + This command shows the pnn of the node which is currently the leader. + + + + Note: If the queried node is INACTIVE then the status + might not be current. + + + + + uptime + + This command shows the uptime for the ctdb daemon. When the last recovery or ip-failover completed and how long it took. If the "duration" is shown as a negative number, this indicates that there is a recovery/failover in progress and it started that many seconds ago. + + + + Example + +# ctdb uptime +Current time of node : Thu Oct 29 10:38:54 2009 +Ctdbd start time : (000 16:54:28) Wed Oct 28 17:44:26 2009 +Time of last recovery/failover: (000 16:53:31) Wed Oct 28 17:45:23 2009 +Duration of last recovery/failover: 2.248552 seconds + + + + + + listnodes + + This command shows lists the ip addresses of all the nodes in the cluster. + + + + Example + +# ctdb listnodes +192.168.2.200 +192.168.2.201 +192.168.2.202 +192.168.2.203 + + + + + + natgw {leader|list|status} + + This command shows different aspects of NAT gateway status. + For an overview of CTDB's NAT gateway functionality please see + the NAT GATEWAY section in + ctdb + 7. + + + + + leader + + + Show the PNN and private IP address of the current NAT + gateway leader node. + + + Example output: + + +1 192.168.2.201 + + + + + list + + + List the private IP addresses of nodes in the current + NAT gateway group, annotating the leader node. + + + Example output: + + +192.168.2.200 +192.168.2.201 LEADER +192.168.2.202 +192.168.2.203 + + + + + status + + + List the nodes in the current NAT gateway group and + their status. + + + Example output: + + +pnn:0 192.168.2.200 UNHEALTHY (THIS NODE) +pnn:1 192.168.2.201 OK +pnn:2 192.168.2.202 OK +pnn:3 192.168.2.203 OK + + + + + + + + ping + + This command will "ping" specified CTDB nodes in the cluster + to verify that they are running. + + + Example + +# ctdb ping +response from 0 time=0.000054 sec (3 clients) + + + + + + ifaces + + This command will display the list of network interfaces, which could + host public addresses, along with their status. + + + Example + +# ctdb ifaces +Interfaces on node 0 +name:eth5 link:up references:2 +name:eth4 link:down references:0 +name:eth3 link:up references:1 +name:eth2 link:up references:1 + +# ctdb -X ifaces +|Name|LinkStatus|References| +|eth5|1|2| +|eth4|0|0| +|eth3|1|1| +|eth2|1|1| + + + + + + ip + + This command will display the list of public addresses that are provided by the cluster and which physical node is currently serving this ip. By default this command will ONLY show those public addresses that are known to the node itself. To see the full list of all public ips across the cluster you must use "ctdb ip all". + + + Example + +# ctdb ip -v +Public IPs on node 0 +172.31.91.82 node[1] active[] available[eth2,eth3] configured[eth2,eth3] +172.31.91.83 node[0] active[eth3] available[eth2,eth3] configured[eth2,eth3] +172.31.91.84 node[1] active[] available[eth2,eth3] configured[eth2,eth3] +172.31.91.85 node[0] active[eth2] available[eth2,eth3] configured[eth2,eth3] +172.31.92.82 node[1] active[] available[eth5] configured[eth4,eth5] +172.31.92.83 node[0] active[eth5] available[eth5] configured[eth4,eth5] +172.31.92.84 node[1] active[] available[eth5] configured[eth4,eth5] +172.31.92.85 node[0] active[eth5] available[eth5] configured[eth4,eth5] + +# ctdb -X ip -v +|Public IP|Node|ActiveInterface|AvailableInterfaces|ConfiguredInterfaces| +|172.31.91.82|1||eth2,eth3|eth2,eth3| +|172.31.91.83|0|eth3|eth2,eth3|eth2,eth3| +|172.31.91.84|1||eth2,eth3|eth2,eth3| +|172.31.91.85|0|eth2|eth2,eth3|eth2,eth3| +|172.31.92.82|1||eth5|eth4,eth5| +|172.31.92.83|0|eth5|eth5|eth4,eth5| +|172.31.92.84|1||eth5|eth4,eth5| +|172.31.92.85|0|eth5|eth5|eth4,eth5| + + + + + + ipinfo <parameter>IP</parameter> + + This command will display details about the specified public addresses. + + + Example + +# ctdb ipinfo 172.31.92.85 +Public IP[172.31.92.85] info on node 0 +IP:172.31.92.85 +CurrentNode:0 +NumInterfaces:2 +Interface[1]: Name:eth4 Link:down References:0 +Interface[2]: Name:eth5 Link:up References:2 (active) + + + + + + event run|status|script list|script enable|script disable + + This command is used to control event daemon and to inspect + status of various events. + + + + The commands below require a component to be specified. In + the current version the only valid component is + legacy. + + + + + run TIMEOUT COMPONENT EVENT ARGUMENTS + + + This command can be used to manually run specified EVENT + in COMPONENT with optional ARGUMENTS. The event will be + allowed to run a maximum of TIMEOUT seconds. If TIMEOUT + is 0, then there is no time limit for running the event. + + + + + + status COMPONENT EVENT + + + This command displays the last execution status of the + specified EVENT in COMPONENT. + + + The command will terminate with the exit status + corresponding to the overall status of event that is + displayed. + + + The output is the list of event scripts executed. + Each line shows the name, status, duration and start time + for each script. + + + Example + + +# ctdb event status legacy monitor +00.ctdb OK 0.014 Sat Dec 17 19:39:11 2016 +01.reclock OK 0.013 Sat Dec 17 19:39:11 2016 +05.system OK 0.029 Sat Dec 17 19:39:11 2016 +06.nfs OK 0.014 Sat Dec 17 19:39:11 2016 +10.interface OK 0.037 Sat Dec 17 19:39:11 2016 +11.natgw OK 0.011 Sat Dec 17 19:39:11 2016 +11.routing OK 0.007 Sat Dec 17 19:39:11 2016 +13.per_ip_routing OK 0.007 Sat Dec 17 19:39:11 2016 +20.multipathd OK 0.007 Sat Dec 17 19:39:11 2016 +31.clamd OK 0.007 Sat Dec 17 19:39:11 2016 +40.vsftpd OK 0.013 Sat Dec 17 19:39:11 2016 +41.httpd OK 0.018 Sat Dec 17 19:39:11 2016 +49.winbind OK 0.023 Sat Dec 17 19:39:11 2016 +50.samba OK 0.100 Sat Dec 17 19:39:12 2016 +60.nfs OK 0.376 Sat Dec 17 19:39:12 2016 +70.iscsi OK 0.009 Sat Dec 17 19:39:12 2016 +91.lvs OK 0.007 Sat Dec 17 19:39:12 2016 + + + + + + script list COMPONENT + + + List the available event scripts in COMPONENT. Enabled + scripts are flagged with a '*'. + + + Generally, event scripts are provided by CTDB. However, + local or 3rd party event scripts may also be available. + These are shown in a separate section after those + provided by CTDB. + + + Example + + +# ctdb event script list legacy +* 00.ctdb +* 01.reclock +* 05.system +* 06.nfs +* 10.interface + 11.natgw + 11.routing + 13.per_ip_routing + 20.multipathd + 31.clamd + 40.vsftpd + 41.httpd +* 49.winbind +* 50.samba +* 60.nfs + 70.iscsi + 91.lvs + +* 02.local + + + + + + script enable COMPONENT SCRIPT + + + Enable the specified event SCRIPT in COMPONENT. Only + enabled scripts will be executed when running any event. + + + + + + script disable COMPONENT SCRIPT + + + Disable the specified event SCRIPT in COMPONENT. This + will prevent the script from executing when running any + event. + + + + + + + + scriptstatus + + This command displays which event scripts where run in the previous + monitoring cycle and the result of each script. If a script + failed with an error, causing the node to become unhealthy, + the output from that script is also shown. + + + This command is deprecated. It's provided for backward + compatibility. In place of ctdb scriptstatus, + use ctdb event status. + + + Example + +# ctdb scriptstatus +00.ctdb OK 0.011 Sat Dec 17 19:40:46 2016 +01.reclock OK 0.010 Sat Dec 17 19:40:46 2016 +05.system OK 0.030 Sat Dec 17 19:40:46 2016 +06.nfs OK 0.014 Sat Dec 17 19:40:46 2016 +10.interface OK 0.041 Sat Dec 17 19:40:46 2016 +11.natgw OK 0.008 Sat Dec 17 19:40:46 2016 +11.routing OK 0.007 Sat Dec 17 19:40:46 2016 +13.per_ip_routing OK 0.007 Sat Dec 17 19:40:46 2016 +20.multipathd OK 0.007 Sat Dec 17 19:40:46 2016 +31.clamd OK 0.007 Sat Dec 17 19:40:46 2016 +40.vsftpd OK 0.013 Sat Dec 17 19:40:46 2016 +41.httpd OK 0.015 Sat Dec 17 19:40:46 2016 +49.winbind OK 0.022 Sat Dec 17 19:40:46 2016 +50.samba ERROR 0.077 Sat Dec 17 19:40:46 2016 + OUTPUT: ERROR: samba tcp port 445 is not responding + + + + + + listvars + + List all tuneable variables, except the values of the obsolete tunables + like VacuumMinInterval. The obsolete tunables can be retrieved only + explicitly with the "ctdb getvar" command. + + + Example + +# ctdb listvars +SeqnumInterval = 1000 +ControlTimeout = 60 +TraverseTimeout = 20 +KeepaliveInterval = 5 +KeepaliveLimit = 5 +RecoverTimeout = 120 +RecoverInterval = 1 +ElectionTimeout = 3 +TakeoverTimeout = 9 +MonitorInterval = 15 +TickleUpdateInterval = 20 +EventScriptTimeout = 30 +MonitorTimeoutCount = 20 +RecoveryGracePeriod = 120 +RecoveryBanPeriod = 300 +DatabaseHashSize = 100001 +DatabaseMaxDead = 5 +RerecoveryTimeout = 10 +EnableBans = 1 +NoIPFailback = 0 +VerboseMemoryNames = 0 +RecdPingTimeout = 60 +RecdFailCount = 10 +LogLatencyMs = 0 +RecLockLatencyMs = 1000 +RecoveryDropAllIPs = 120 +VacuumInterval = 10 +VacuumMaxRunTime = 120 +RepackLimit = 10000 +VacuumFastPathCount = 60 +MaxQueueDropMsg = 1000000 +AllowUnhealthyDBRead = 0 +StatHistoryInterval = 1 +DeferredAttachTO = 120 +AllowClientDBAttach = 1 +RecoverPDBBySeqNum = 1 +DeferredRebalanceOnNodeAdd = 300 +FetchCollapse = 1 +HopcountMakeSticky = 50 +StickyDuration = 600 +StickyPindown = 200 +NoIPTakeover = 0 +DBRecordCountWarn = 100000 +DBRecordSizeWarn = 10000000 +DBSizeWarn = 100000000 +PullDBPreallocation = 10485760 +LockProcessesPerDB = 200 +RecBufferSizeLimit = 1000000 +QueueBufferSize = 1024 +IPAllocAlgorithm = 2 + + + + + + getvar <parameter>NAME</parameter> + + Get the runtime value of a tuneable variable. + + + Example + +# ctdb getvar MonitorInterval +MonitorInterval = 15 + + + + + + setvar <parameter>NAME</parameter> <parameter>VALUE</parameter> + + Set the runtime value of a tuneable variable. + + + Example + +# ctdb setvar MonitorInterval 20 + + + + + + lvs {leader|list|status} + + This command shows different aspects of LVS status. For an + overview of CTDB's LVS functionality please see the + LVS section in + ctdb + 7. + + + + + leader + + + Shows the PNN of the current LVS leader node. + + + Example output: + + +2 + + + + + list + + + Lists the currently usable LVS nodes. + + + Example output: + + +2 10.0.0.13 +3 10.0.0.14 + + + + + status + + + List the nodes in the current LVS group and their status. + + + Example output: + + +pnn:0 10.0.0.11 UNHEALTHY (THIS NODE) +pnn:1 10.0.0.12 UNHEALTHY +pnn:2 10.0.0.13 OK +pnn:3 10.0.0.14 OK + + + + + + + + + + getcapabilities + + + This command shows the capabilities of the current node. See + the CAPABILITIES section in + ctdb + 7 for more details. + + + + Example output: + + +LEADER: YES +LMASTER: YES + + + + + + statistics + + Collect statistics from the CTDB daemon about + how many calls it has served. Information about + various fields in statistics can be found in + ctdb-statistics + 7. + + + Example + +# ctdb statistics +CTDB version 1 +Current time of statistics : Tue Mar 8 15:18:51 2016 +Statistics collected since : (003 21:31:32) Fri Mar 4 17:47:19 2016 + num_clients 9 + frozen 0 + recovering 0 + num_recoveries 2 + client_packets_sent 8170534 + client_packets_recv 7166132 + node_packets_sent 16549998 + node_packets_recv 5244418 + keepalive_packets_sent 201969 + keepalive_packets_recv 201969 + node + req_call 26 + reply_call 0 + req_dmaster 9 + reply_dmaster 12 + reply_error 0 + req_message 1339231 + req_control 8177506 + reply_control 6831284 + client + req_call 15 + req_message 334809 + req_control 6831308 + timeouts + call 0 + control 0 + traverse 0 + locks + num_calls 8 + num_current 0 + num_pending 0 + num_failed 0 + total_calls 15 + pending_calls 0 + childwrite_calls 0 + pending_childwrite_calls 0 + memory_used 394879 + max_hop_count 1 + total_ro_delegations 0 + total_ro_revokes 0 + hop_count_buckets: 8 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + lock_buckets: 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 + locks_latency MIN/AVG/MAX 0.010005/0.010418/0.011010 sec out of 8 + reclock_ctdbd MIN/AVG/MAX 0.002538/0.002538/0.002538 sec out of 1 + reclock_recd MIN/AVG/MAX 0.000000/0.000000/0.000000 sec out of 0 + call_latency MIN/AVG/MAX 0.000044/0.002142/0.011702 sec out of 15 + childwrite_latency MIN/AVG/MAX 0.000000/0.000000/0.000000 sec out of 0 + + + + + + statisticsreset + + This command is used to clear all statistics counters in a node. + + + Example: ctdb statisticsreset + + + + + dbstatistics <parameter>DB</parameter> + + Display statistics about the database DB. Information + about various fields in dbstatistics can be found in + ctdb-statistics + 7. + + + Example + +# ctdb dbstatistics locking.tdb +DB Statistics: locking.tdb + ro_delegations 0 + ro_revokes 0 + locks + total 14356 + failed 0 + current 0 + pending 0 + hop_count_buckets: 28087 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 + lock_buckets: 0 14188 38 76 32 19 3 0 0 0 0 0 0 0 0 0 + locks_latency MIN/AVG/MAX 0.001066/0.012686/4.202292 sec out of 14356 + vacuum_latency MIN/AVG/MAX 0.000472/0.002207/15.243570 sec out of 224530 + Num Hot Keys: 1 + Count:8 Key:ff5bd7cb3ee3822edc1f0000000000000000000000000000 + + + + + + getreclock + + Show details of the recovery lock, if any. + + + + Example output: + + + /clusterfs/.ctdb/recovery.lock + + + + + + getdebug + + Get the current debug level for the node. the debug level controls what information is written to the log file. + + + The debug levels are mapped to the corresponding syslog levels. + When a debug level is set, only those messages at that level and higher + levels will be printed. + + + The list of debug levels from highest to lowest are : + + + ERROR WARNING NOTICE INFO DEBUG + + + + + setdebug <parameter>DEBUGLEVEL</parameter> + + Set the debug level of a node. This controls what information will be logged. + + + The debuglevel is one of ERROR WARNING NOTICE INFO DEBUG + + + + + getpid + + This command will return the process id of the ctdb daemon. + + + + + disable + + This command is used to administratively disable a node in the cluster. + A disabled node will still participate in the cluster and host + clustered TDB records but its public ip address has been taken over by + a different node and it no longer hosts any services. + + + + + enable + + Re-enable a node that has been administratively disabled. + + + + + stop + + This command is used to administratively STOP a node in the cluster. + A STOPPED node is connected to the cluster but will not host any + public ip addresse, nor does it participate in the VNNMAP. + The difference between a DISABLED node and a STOPPED node is that + a STOPPED node does not host any parts of the database which means + that a recovery is required to stop/continue nodes. + + + + + continue + + Re-start a node that has been administratively stopped. + + + + + addip <parameter>IPADDR</parameter>/<parameter>mask</parameter> <parameter>IFACE</parameter> + + This command is used to add a new public ip to a node + during runtime. It should be followed by a ctdb + ipreallocate. This allows public addresses to be + added to a cluster without having to restart the ctdb daemons. + + + Note that this only updates the runtime instance of ctdb. Any + changes will be lost next time ctdb is restarted and the public + addresses file is re-read. If you want this change to be + permanent you must also update the public addresses file manually. + + + + + delip <parameter>IPADDR</parameter> + + This command flags IPADDR for deletion from a node at runtime. + It should be followed by a ctdb + ipreallocate. If IPADDR is currently hosted by the + node it is being removed from, this ensures that the IP will + first be failed over to another node, if possible, and that it + is then actually removed. + + + Note that this only updates the runtime instance of CTDB. Any + changes will be lost next time CTDB is restarted and the + public addresses file is re-read. If you want this change to + be permanent you must also update the public addresses file + manually. + + + + + moveip <parameter>IPADDR</parameter> <parameter>PNN</parameter> + + This command can be used to manually fail a public ip address to a + specific node. + + + In order to manually override the "automatic" distribution of public + ip addresses that ctdb normally provides, this command only works + when you have changed the tunables for the daemon to: + + + IPAllocAlgorithm != 0 + + + NoIPFailback = 1 + + + + + shutdown + + This command will shutdown a specific CTDB daemon. + + + + + setlmasterrole on|off + + This command is used to enable/disable the LMASTER capability for a node at runtime. This capability determines whether or not a node can be used as an LMASTER for records in the database. A node that does not have the LMASTER capability will not show up in the vnnmap. + + + + Nodes will by default have this capability, but it can be stripped off nodes by the setting in the sysconfig file or by using this command. + + + Once this setting has been enabled/disabled, you need to perform a recovery for it to take effect. + + + See also "ctdb getcapabilities" + + + + + setleaderrole on|off + + This command is used to enable/disable the LEADER capability + for a node at runtime. This capability determines whether or + not a node can be elected leader of the cluster. A node that + does not have the LEADER capability can not be elected + leader. If the current leader has this capability removed then + an election will occur. + + + + Nodes have this capability enabled by default, but it can be + removed via the cluster:leader capability + configuration setting or by using this command. + + + See also "ctdb getcapabilities" + + + + + reloadnodes + + This command is used when adding new nodes, or removing + existing nodes from an existing cluster. + + + Procedure to add nodes: + + + + + To expand an existing cluster, first ensure with + ctdb status that all nodes are up and + running and that they are all healthy. Do not try to + expand a cluster unless it is completely healthy! + + + + + On all nodes, edit /usr/local/etc/ctdb/nodes + and add the new nodes at the end of this + file. + + + + + Verify that all the nodes have identical + /usr/local/etc/ctdb/nodes files after adding + the new nodes. + + + + + Run ctdb reloadnodes to force all nodes + to reload the nodes file. + + + + + Use ctdb status on all nodes and verify + that they now show the additional nodes. + + + + + Install and configure the new node and bring it online. + + + + + Procedure to remove nodes: + + + + + To remove nodes from an existing cluster, first ensure + with ctdb status that all nodes, except + the node to be deleted, are up and running and that they + are all healthy. Do not try to remove nodes from a + cluster unless the cluster is completely healthy! + + + + + Shutdown and power off the node to be removed. + + + + + On all other nodes, edit the + /usr/local/etc/ctdb/nodes file and + comment out the nodes to be removed. + Do not delete the lines for the deleted + nodes, just comment them out by adding a '#' at + the beginning of the lines. + + + + + Run ctdb reloadnodes to force all nodes + to reload the nodes file. + + + + + Use ctdb status on all nodes and verify + that the deleted nodes are no longer listed. + + + + + + + + + reloadips + <optional><parameter>PNN-LIST</parameter></optional> + + + This command reloads the public addresses configuration file + on the specified nodes. When it completes addresses will be + reconfigured and reassigned across the cluster as necessary. + + + + This command is currently unable to make changes to the + netmask or interfaces associated with existing addresses. + Such changes must be made in 2 steps by deleting addresses in + question and re-adding then. Unfortunately this will disrupt + connections to the changed addresses. + + + + + getdbmap + + This command lists all clustered TDB databases that the CTDB + daemon has attached to. Some databases are flagged as PERSISTENT, + this means that the database stores data persistently and the + data will remain across reboots. One example of such a database + is secrets.tdb where information about how the cluster was joined + to the domain is stored. Some database are flagged as REPLICATED, + this means that the data in that database is replicated across all + the nodes. But the data will not remain across reboots. This + type of database is used by CTDB to store it's internal state. + + + If a PERSISTENT database is not in a healthy state the database + is flagged as UNHEALTHY. If there's at least one completely + healthy node running in the cluster, it's possible that the + content is restored by a recovery run automatically. Otherwise an + administrator needs to analyze the problem. + + + See also "ctdb getdbstatus", "ctdb backupdb", "ctdb restoredb", + "ctdb dumpbackup", "ctdb wipedb", "ctdb setvar AllowUnhealthyDBRead 1" + and (if samba or tdb-utils are installed) "tdbtool check". + + + Most databases are not persistent and only store the state + information that the currently running samba daemons need. These + databases are always wiped when ctdb/samba starts and when a + node is rebooted. + + + + Example + +# ctdb getdbmap +Number of databases:10 +dbid:0x435d3410 name:notify.tdb path:/usr/local/var/lib/ctdb/notify.tdb.0 +dbid:0x42fe72c5 name:locking.tdb path:/usr/local/var/lib/ctdb/locking.tdb.0 +dbid:0x1421fb78 name:brlock.tdb path:/usr/local/var/lib/ctdb/brlock.tdb.0 +dbid:0x17055d90 name:connections.tdb path:/usr/local/var/lib/ctdb/connections.tdb.0 +dbid:0xc0bdde6a name:sessionid.tdb path:/usr/local/var/lib/ctdb/sessionid.tdb.0 +dbid:0x122224da name:test.tdb path:/usr/local/var/lib/ctdb/test.tdb.0 +dbid:0x2672a57f name:idmap2.tdb path:/usr/local/var/lib/ctdb/persistent/idmap2.tdb.0 PERSISTENT +dbid:0xb775fff6 name:secrets.tdb path:/usr/local/var/lib/ctdb/persistent/secrets.tdb.0 PERSISTENT +dbid:0xe98e08b6 name:group_mapping.tdb path:/usr/local/var/lib/ctdb/persistent/group_mapping.tdb.0 PERSISTENT +dbid:0x7bbbd26c name:passdb.tdb path:/usr/local/var/lib/ctdb/persistent/passdb.tdb.0 PERSISTENT + +# ctdb getdbmap # example for unhealthy database +Number of databases:1 +dbid:0xb775fff6 name:secrets.tdb path:/usr/local/var/lib/ctdb/persistent/secrets.tdb.0 PERSISTENT UNHEALTHY + +# ctdb -X getdbmap +|ID|Name|Path|Persistent|Unhealthy| +|0x7bbbd26c|passdb.tdb|/usr/local/var/lib/ctdb/persistent/passdb.tdb.0|1|0| + + + + + + + backupdb + <parameter>DB</parameter> + <parameter>FILE</parameter> + + + Copy the contents of database DB to FILE. FILE can later be + read back using restoredb. This is mainly + useful for backing up persistent databases such as + secrets.tdb and similar. + + + + + + restoredb + <parameter>FILE</parameter> + <optional><parameter>DB</parameter></optional> + + + This command restores a persistent database that was + previously backed up using backupdb. By default the data will + be restored back into the same database as it was created + from. By specifying dbname you can restore the data into a + different database. + + + + + setdbreadonly <parameter>DB</parameter> + + This command will enable the read-only record support for a + database. This is an experimental feature to improve + performance for contended records primarily in locking.tdb and + brlock.tdb. When enabling this feature you must set it on all + nodes in the cluster. + + + + + setdbsticky <parameter>DB</parameter> + + This command will enable the sticky record support for the + specified database. This is an experimental feature to + improve performance for contended records primarily in + locking.tdb and brlock.tdb. When enabling this feature you + must set it on all nodes in the cluster. + + + + + + + INTERNAL COMMANDS + + + Internal commands are used by CTDB's scripts and are not + required for managing a CTDB cluster. Their parameters and + behaviour are subject to change. + + + + gettickles <parameter>IPADDR</parameter> + + Show TCP connections that are registered with CTDB to be + "tickled" if there is a failover. + + + + + gratarp <parameter>IPADDR</parameter> <parameter>INTERFACE</parameter> + + Send out a gratuitous ARP for the specified interface through + the specified interface. This command is mainly used by the + ctdb eventscripts. + + + + + + pdelete <parameter>DB</parameter> <parameter>KEY</parameter> + + + Delete KEY from DB. + + + + + + pfetch <parameter>DB</parameter> <parameter>KEY</parameter> + + + Print the value associated with KEY in DB. + + + + + + pstore + <parameter>DB</parameter> + <parameter>KEY</parameter> + <parameter>FILE</parameter> + + + Store KEY in DB with contents of FILE as the associated value. + + + + + + ptrans + <parameter>DB</parameter> + <optional><parameter>FILE</parameter></optional> + + + Read a list of key-value pairs, one per line from FILE, and + store them in DB using a single transaction. An empty value + is equivalent to deleting the given key. + + + The key and value should be separated by spaces or tabs. Each + key/value should be a printable string enclosed in + double-quotes. + + + + + runstate [setup|first_recovery|startup|running] + + Print the runstate of the specified node. Runstates are used + to serialise important state transitions in CTDB, particularly + during startup. + + + If one or more optional runstate arguments are specified then + the node must be in one of these runstates for the command to + succeed. + + + Example + +# ctdb runstate +RUNNING + + + + + + setifacelink <parameter>IFACE</parameter> up|down + + Set the internal state of network interface IFACE. This is + typically used in the 10.interface script + in the "monitor" event. + + + Example: ctdb setifacelink eth0 up + + + + + tickle + + Read a list of TCP connections, one per line, from standard + input and send a TCP tickle to the source host for each + connection. A connection is specified as: + + + SRC-IPADDR:SRC-PORT DST-IPADDR:DST-PORT + + + A single connection can be specified on the command-line + rather than on standard input. + + + A TCP tickle is a TCP ACK packet with an invalid sequence and + acknowledge number and will when received by the source host + result in it sending an immediate correct ACK back to the + other end. + + + TCP tickles are useful to "tickle" clients after a IP failover has + occurred since this will make the client immediately recognize the + TCP connection has been disrupted and that the client will need + to reestablish. This greatly speeds up the time it takes for a client + to detect and reestablish after an IP failover in the ctdb cluster. + + + + + version + + Display the CTDB version. + + + + + + + DEBUGGING COMMANDS + + These commands are primarily used for CTDB development and testing and + should not be used for normal administration. + + + + + OPTIONS + + + --print-emptyrecords + + + This enables printing of empty records when dumping databases + with the catdb, cattbd and dumpdbbackup commands. Records with + empty data segment are considered deleted by ctdb and cleaned + by the vacuuming mechanism, so this switch can come in handy for + debugging the vacuuming behaviour. + + + + + --print-datasize + + + This lets database dumps (catdb, cattdb, dumpdbbackup) print the + size of the record data instead of dumping the data contents. + + + + + --print-lmaster + + + This lets catdb print the lmaster for each record. + + + + + --print-hash + + + This lets database dumps (catdb, cattdb, dumpdbbackup) print the + hash for each record. + + + + + --print-recordflags + + + This lets catdb and dumpdbbackup print the + record flags for each record. Note that cattdb always + prints the flags. + + + + + + + + + process-exists <parameter>PID</parameter> <parameter>[SRVID]</parameter> + + This command checks if a specific process exists on the CTDB + host. This is mainly used by Samba to check if remote instances + of samba are still running or not. When the optional SRVID + argument is specified, the command check if a specific process + exists on the CTDB host and has registered for specified SRVID. + + + + + getdbstatus <parameter>DB</parameter> + + This command displays more details about a database. + + + Example + +# ctdb getdbstatus test.tdb.0 +dbid: 0x122224da +name: test.tdb +path: /usr/local/var/lib/ctdb/test.tdb.0 +PERSISTENT: no +HEALTH: OK + +# ctdb getdbstatus registry.tdb # with a corrupted TDB +dbid: 0xf2a58948 +name: registry.tdb +path: /usr/local/var/lib/ctdb/persistent/registry.tdb.0 +PERSISTENT: yes +HEALTH: NO-HEALTHY-NODES - ERROR - Backup of corrupted TDB in '/usr/local/var/lib/ctdb/persistent/registry.tdb.0.corrupted.20091208091949.0Z' + + + + + + catdb <parameter>DB</parameter> + + Print a dump of the clustered TDB database DB. + + + + + cattdb <parameter>DB</parameter> + + Print a dump of the contents of the local TDB database DB. + + + + + dumpdbbackup <parameter>FILE</parameter> + + Print a dump of the contents from database backup FILE, + similar to catdb. + + + + + wipedb <parameter>DB</parameter> + + Remove all contents of database DB. + + + + + recover + + This command will trigger the recovery daemon to do a cluster + recovery. + + + + + ipreallocate, sync + + This command will force the leader to perform a full ip + reallocation process and redistribute all ip addresses. This + is useful to "reset" the allocations back to its default state + if they have been changed using the "moveip" command. While a + "recover" will also perform this reallocation, a recovery is + much more hevyweight since it will also rebuild all the + databases. + + + + + attach <parameter>DBNAME</parameter> [persistent|replicated] + + Create a new CTDB database called DBNAME and attach to it on + all nodes. + + + + + detach <parameter>DB-LIST</parameter> + + Detach specified non-persistent database(s) from the cluster. This + command will disconnect specified database(s) on all nodes in + the cluster. This command should only be used when none of the + specified database(s) are in use. + + + All nodes should be active and tunable AllowClientDBAccess should + be disabled on all nodes before detaching databases. + + + + + dumpmemory + + This is a debugging command. This command will make the ctdb + daemon to write a fill memory allocation map to standard output. + + + + + rddumpmemory + + This is a debugging command. This command will dump the talloc memory + allocation tree for the recovery daemon to standard output. + + + + + ban <parameter>BANTIME</parameter> + + Administratively ban a node for BANTIME seconds. The node + will be unbanned after BANTIME seconds have elapsed. + + + A banned node does not participate in the cluster. It does + not host any records for the clustered TDB and does not host + any public IP addresses. + + + Nodes are automatically banned if they misbehave. For + example, a node may be banned if it causes too many cluster + recoveries. + + + To administratively exclude a node from a cluster use the + stop command. + + + + + unban + + This command is used to unban a node that has either been + administratively banned using the ban command or has been + automatically banned. + + + + + + + + + SEE ALSO + + ctdbd + 1, + + onnode + 1, + + ctdb + 7, + + ctdb-statistics + 7, + + ctdb-tunables + 7, + + + + + + + diff --git a/ctdb/doc/ctdb.7.xml b/ctdb/doc/ctdb.7.xml new file mode 100644 index 0000000..351f3e8 --- /dev/null +++ b/ctdb/doc/ctdb.7.xml @@ -0,0 +1,1182 @@ + + + + + + ctdb + 7 + ctdb + CTDB - clustered TDB database + + + + + ctdb + Clustered TDB + + + + DESCRIPTION + + + CTDB is a clustered database component in clustered Samba that + provides a high-availability load-sharing CIFS server cluster. + + + + The main functions of CTDB are: + + + + + + Provide a clustered version of the TDB database with automatic + rebuild/recovery of the databases upon node failures. + + + + + + Monitor nodes in the cluster and services running on each node. + + + + + + Manage a pool of public IP addresses that are used to provide + services to clients. Alternatively, CTDB can be used with + LVS. + + + + + + Combined with a cluster filesystem CTDB provides a full + high-availablity (HA) environment for services such as clustered + Samba, NFS and other services. + + + + In addition to the CTDB manual pages there is much more + information available at + . + + + + + ANATOMY OF A CTDB CLUSTER + + + A CTDB cluster is a collection of nodes with 2 or more network + interfaces. All nodes provide network (usually file/NAS) services + to clients. Data served by file services is stored on shared + storage (usually a cluster filesystem) that is accessible by all + nodes. + + + CTDB provides an "all active" cluster, where services are load + balanced across all nodes. + + + + + Cluster leader + + + CTDB uses a cluster leader and follower + model of cluster management. All nodes in a cluster elect one + node to be the leader. The leader node coordinates privileged + operations such as database recovery and IP address failover. + + + + CTDB previously referred to the leader as the recovery + master or recmaster. References + to these terms may still be found in documentation and code. + + + + + Cluster Lock + + + CTDB uses a cluster lock to assert its privileged role in the + cluster. This node takes the cluster lock when it becomes + leader and holds the lock until it is no longer leader. The + cluster lock helps CTDB to avoid a + split brain, where a cluster becomes + partitioned and each partition attempts to operate + independently. Issues that can result from a split brain + include file data corruption, because file locking metadata may + not be tracked correctly. + + + + CTDB previously referred to the cluster lock as the + recovery lock. The abbreviation + reclock is still used - just "clock" would + be confusing. + + + + CTDB is unable configure a default cluster + lock, because this would depend on factors such as + cluster filesystem mountpoints. However, running CTDB + without a cluster lock is not recommended as there + will be no split brain protection. + + + + When a cluster lock is configured it is used as the election + mechanism. Nodes race to take the cluster lock and the winner + is the cluster leader. This avoids problems when a node wins an + election but is unable to take the lock - this can occur if a + cluster becomes partitioned (for example, due to a communication + failure) and a different leader is elected by the nodes in each + partition, or if the cluster filesystem has a high failover + latency. + + + + By default, the cluster lock is implemented using a file + (specified by cluster lock in the + [cluster] section of + ctdb.conf + 5) residing in shared + storage (usually) on a cluster filesystem. To support a + cluster lock the cluster filesystem must support lock + coherence. See + ping_pong + 1 for more details. + + + + The cluster lock can also be implemented using an arbitrary + cluster mutex helper (or call-out). This is indicated by using + an exclamation point ('!') as the first character of the + cluster lock parameter. For example, a + value of !/usr/local/bin/myhelper cluster + would run the given helper with the specified arguments. The + helper will continue to run as long as it holds its mutex. See + ctdb/doc/cluster_mutex_helper.txt in the + source tree, and related code, for clues about writing helpers. + + + + When a file is specified for the cluster + lock parameter (i.e. no leading '!') the file lock + is implemented by a default helper + (/usr/local/libexec/ctdb/ctdb_mutex_fcntl_helper). + This helper has arguments as follows: + + + +ctdb_mutex_fcntl_helper FILE RECHECK-INTERVAL + + + ctdb_mutex_fcntl_helper will take a lock on + FILE and then check every RECHECK-INTERVAL seconds to ensure + that FILE still exists and that its inode number is unchanged + from when the lock was taken. The default value for + RECHECK-INTERVAL is 5. + + + + CTDB does sanity checks to ensure that the cluster lock is held + as expected. + + + + + Private vs Public addresses + + + Each node in a CTDB cluster has multiple IP addresses assigned + to it: + + + + + A single private IP address that is used for communication + between nodes. + + + + + One or more public IP addresses that are used to provide + NAS or other services. + + + + + + + Private address + + + Each node is configured with a unique, permanently assigned + private address. This address is configured by the operating + system. This address uniquely identifies a physical node in + the cluster and is the address that CTDB daemons will use to + communicate with the CTDB daemons on other nodes. + + + + Private addresses are listed in the file + /usr/local/etc/ctdb/nodes). This file + contains the list of private addresses for all nodes in the + cluster, one per line. This file must be the same on all nodes + in the cluster. + + + + Some users like to put this configuration file in their + cluster filesystem. A symbolic link should be used in this + case. + + + + Private addresses should not be used by clients to connect to + services provided by the cluster. + + + It is strongly recommended that the private addresses are + configured on a private network that is separate from client + networks. This is because the CTDB protocol is both + unauthenticated and unencrypted. If clients share the private + network then steps need to be taken to stop injection of + packets to relevant ports on the private addresses. It is + also likely that CTDB protocol traffic between nodes could + leak sensitive information if it can be intercepted. + + + + Example /usr/local/etc/ctdb/nodes for a four node + cluster: + + +192.168.1.1 +192.168.1.2 +192.168.1.3 +192.168.1.4 + + + + + Public addresses + + + Public addresses are used to provide services to clients. + Public addresses are not configured at the operating system + level and are not permanently associated with a particular + node. Instead, they are managed by CTDB and are assigned to + interfaces on physical nodes at runtime. + + + The CTDB cluster will assign/reassign these public addresses + across the available healthy nodes in the cluster. When one + node fails, its public addresses will be taken over by one or + more other nodes in the cluster. This ensures that services + provided by all public addresses are always available to + clients, as long as there are nodes available capable of + hosting this address. + + + + The public address configuration is stored in + /usr/local/etc/ctdb/public_addresses on + each node. This file contains a list of the public addresses + that the node is capable of hosting, one per line. Each entry + also contains the netmask and the interface to which the + address should be assigned. If this file is missing then no + public addresses are configured. + + + + Some users who have the same public addresses on all nodes + like to put this configuration file in their cluster + filesystem. A symbolic link should be used in this case. + + + + Example /usr/local/etc/ctdb/public_addresses for a + node that can host 4 public addresses, on 2 different + interfaces: + + +10.1.1.1/24 eth1 +10.1.1.2/24 eth1 +10.1.2.1/24 eth2 +10.1.2.2/24 eth2 + + + + In many cases the public addresses file will be the same on + all nodes. However, it is possible to use different public + address configurations on different nodes. + + + + Example: 4 nodes partitioned into two subgroups: + + +Node 0:/usr/local/etc/ctdb/public_addresses + 10.1.1.1/24 eth1 + 10.1.1.2/24 eth1 + +Node 1:/usr/local/etc/ctdb/public_addresses + 10.1.1.1/24 eth1 + 10.1.1.2/24 eth1 + +Node 2:/usr/local/etc/ctdb/public_addresses + 10.1.2.1/24 eth2 + 10.1.2.2/24 eth2 + +Node 3:/usr/local/etc/ctdb/public_addresses + 10.1.2.1/24 eth2 + 10.1.2.2/24 eth2 + + + In this example nodes 0 and 1 host two public addresses on the + 10.1.1.x network while nodes 2 and 3 host two public addresses + for the 10.1.2.x network. + + + Public address 10.1.1.1 can be hosted by either of nodes 0 or + 1 and will be available to clients as long as at least one of + these two nodes are available. + + + If both nodes 0 and 1 become unavailable then public address + 10.1.1.1 also becomes unavailable. 10.1.1.1 can not be failed + over to nodes 2 or 3 since these nodes do not have this public + address configured. + + + The ctdb ip command can be used to view the + current assignment of public addresses to physical nodes. + + + + + + + Node status + + + The current status of each node in the cluster can be viewed by the + ctdb status command. + + + + A node can be in one of the following states: + + + + + OK + + + This node is healthy and fully functional. It hosts public + addresses to provide services. + + + + + + DISCONNECTED + + + This node is not reachable by other nodes via the private + network. It is not currently participating in the cluster. + It does not host public addresses to + provide services. It might be shut down. + + + + + + DISABLED + + + This node has been administratively disabled. This node is + partially functional and participates in the cluster. + However, it does not host public + addresses to provide services. + + + + + + UNHEALTHY + + + A service provided by this node has failed a health check + and should be investigated. This node is partially + functional and participates in the cluster. However, it + does not host public addresses to + provide services. Unhealthy nodes should be investigated + and may require an administrative action to rectify. + + + + + + BANNED + + + CTDB is not behaving as designed on this node. For example, + it may have failed too many recovery attempts. Such nodes + are banned from participating in the cluster for a + configurable time period before they attempt to rejoin the + cluster. A banned node does not host + public addresses to provide services. All banned nodes + should be investigated and may require an administrative + action to rectify. + + + + + + STOPPED + + + This node has been administratively exclude from the + cluster. A stopped node does no participate in the cluster + and does not host public addresses to + provide services. This state can be used while performing + maintenance on a node. + + + + + + PARTIALLYONLINE + + + A node that is partially online participates in a cluster + like a healthy (OK) node. Some interfaces to serve public + addresses are down, but at least one interface is up. See + also ctdb ifaces. + + + + + + + + + CAPABILITIES + + + Cluster nodes can have several different capabilities enabled. + These are listed below. + + + + + + LEADER + + + Indicates that a node can become the CTDB cluster leader. + The current leader is decided via an + election held by all active nodes with this capability. + + + Default is YES. + + + + + + LMASTER + + + Indicates that a node can be the location master (LMASTER) + for database records. The LMASTER always knows which node + has the latest copy of a record in a volatile database. + + + Default is YES. + + + + + + + + The LEADER and LMASTER capabilities can be disabled when CTDB + is used to create a cluster spanning across WAN links. In this + case CTDB acts as a WAN accelerator. + + + + + + LVS + + + LVS is a mode where CTDB presents one single IP address for the + entire cluster. This is an alternative to using public IP + addresses and round-robin DNS to loadbalance clients across the + cluster. + + + + This is similar to using a layer-4 loadbalancing switch but with + some restrictions. + + + + One extra LVS public address is assigned on the public network + to each LVS group. Each LVS group is a set of nodes in the + cluster that presents the same LVS address public address to the + outside world. Normally there would only be one LVS group + spanning an entire cluster, but in situations where one CTDB + cluster spans multiple physical sites it might be useful to have + one LVS group for each site. There can be multiple LVS groups + in a cluster but each node can only be member of one LVS group. + + + + Client access to the cluster is load-balanced across the HEALTHY + nodes in an LVS group. If no HEALTHY nodes exists then all + nodes in the group are used, regardless of health status. CTDB + will, however never load-balance LVS traffic to nodes that are + BANNED, STOPPED, DISABLED or DISCONNECTED. The ctdb + lvs command is used to show which nodes are currently + load-balanced across. + + + + In each LVS group, one of the nodes is selected by CTDB to be + the LVS leader. This node receives all traffic from clients + coming in to the LVS public address and multiplexes it across + the internal network to one of the nodes that LVS is using. + When responding to the client, that node will send the data back + directly to the client, bypassing the LVS leader node. The + command ctdb lvs leader will show which node + is the current LVS leader. + + + + The path used for a client I/O is: + + + + Client sends request packet to LVS leader. + + + + + LVS leader passes the request on to one node across the + internal network. + + + + + Selected node processes the request. + + + + + Node responds back to client. + + + + + + + This means that all incoming traffic to the cluster will pass + through one physical node, which limits scalability. You can + send more data to the LVS address that one physical node can + multiplex. This means that you should not use LVS if your I/O + pattern is write-intensive since you will be limited in the + available network bandwidth that node can handle. LVS does work + very well for read-intensive workloads where only smallish READ + requests are going through the LVS leader bottleneck and the + majority of the traffic volume (the data in the read replies) + goes straight from the processing node back to the clients. For + read-intensive i/o patterns you can achieve very high throughput + rates in this mode. + + + + Note: you can use LVS and public addresses at the same time. + + + + If you use LVS, you must have a permanent address configured for + the public interface on each node. This address must be routable + and the cluster nodes must be configured so that all traffic + back to client hosts are routed through this interface. This is + also required in order to allow samba/winbind on the node to + talk to the domain controller. This LVS IP address can not be + used to initiate outgoing traffic. + + + Make sure that the domain controller and the clients are + reachable from a node before you enable + LVS. Also ensure that outgoing traffic to these hosts is routed + out through the configured public interface. + + + + Configuration + + + To activate LVS on a CTDB node you must specify the + CTDB_LVS_PUBLIC_IFACE, + CTDB_LVS_PUBLIC_IP and + CTDB_LVS_NODES configuration variables. + CTDB_LVS_NODES specifies a file containing + the private address of all nodes in the current node's LVS + group. + + + + Example: + +CTDB_LVS_PUBLIC_IFACE=eth1 +CTDB_LVS_PUBLIC_IP=10.1.1.237 +CTDB_LVS_NODES=/usr/local/etc/ctdb/lvs_nodes + + + + + Example /usr/local/etc/ctdb/lvs_nodes: + + +192.168.1.2 +192.168.1.3 +192.168.1.4 + + + + Normally any node in an LVS group can act as the LVS leader. + Nodes that are highly loaded due to other demands maybe + flagged with the "follower-only" option in the + CTDB_LVS_NODES file to limit the LVS + functionality of those nodes. + + + + LVS nodes file that excludes 192.168.1.4 from being + the LVS leader node: + + +192.168.1.2 +192.168.1.3 +192.168.1.4 follower-only + + + + + + + TRACKING AND RESETTING TCP CONNECTIONS + + + CTDB tracks TCP connections from clients to public IP addresses, + on known ports. When an IP address moves from one node to + another, all existing TCP connections to that IP address are + reset. The node taking over this IP address will also send + gratuitous ARPs (for IPv4, or neighbour advertisement, for + IPv6). This allows clients to reconnect quickly, rather than + waiting for TCP timeouts, which can be very long. + + + + It is important that established TCP connections do not survive + a release and take of a public IP address on the same node. + Such connections can get out of sync with sequence and ACK + numbers, potentially causing a disruptive ACK storm. + + + + + + NAT GATEWAY + + + NAT gateway (NATGW) is an optional feature that is used to + configure fallback routing for nodes. This allows cluster nodes + to connect to external services (e.g. DNS, AD, NIS and LDAP) + when they do not host any public addresses (e.g. when they are + unhealthy). + + + This also applies to node startup because CTDB marks nodes as + UNHEALTHY until they have passed a "monitor" event. In this + context, NAT gateway helps to avoid a "chicken and egg" + situation where a node needs to access an external service to + become healthy. + + + Another way of solving this type of problem is to assign an + extra static IP address to a public interface on every node. + This is simpler but it uses an extra IP address per node, while + NAT gateway generally uses only one extra IP address. + + + + Operation + + + One extra NATGW public address is assigned on the public + network to each NATGW group. Each NATGW group is a set of + nodes in the cluster that shares the same NATGW address to + talk to the outside world. Normally there would only be one + NATGW group spanning an entire cluster, but in situations + where one CTDB cluster spans multiple physical sites it might + be useful to have one NATGW group for each site. + + + There can be multiple NATGW groups in a cluster but each node + can only be member of one NATGW group. + + + In each NATGW group, one of the nodes is selected by CTDB to + be the NATGW leader and the other nodes are consider to be + NATGW followers. NATGW followers establish a fallback default route + to the NATGW leader via the private network. When a NATGW + follower hosts no public IP addresses then it will use this route + for outbound connections. The NATGW leader hosts the NATGW + public IP address and routes outgoing connections from + follower nodes via this IP address. It also establishes a + fallback default route. + + + + + Configuration + + + NATGW is usually configured similar to the following example configuration: + + +CTDB_NATGW_NODES=/usr/local/etc/ctdb/natgw_nodes +CTDB_NATGW_PRIVATE_NETWORK=192.168.1.0/24 +CTDB_NATGW_PUBLIC_IP=10.0.0.227/24 +CTDB_NATGW_PUBLIC_IFACE=eth0 +CTDB_NATGW_DEFAULT_GATEWAY=10.0.0.1 + + + + Normally any node in a NATGW group can act as the NATGW + leader. Some configurations may have special nodes that lack + connectivity to a public network. In such cases, those nodes + can be flagged with the "follower-only" option in the + CTDB_NATGW_NODES file to limit the NATGW + functionality of those nodes. + + + + See the NAT GATEWAY section in + ctdb-script.options + 5 for more details of + NATGW configuration. + + + + + + Implementation details + + + When the NATGW functionality is used, one of the nodes is + selected to act as a NAT gateway for all the other nodes in + the group when they need to communicate with the external + services. The NATGW leader is selected to be a node that is + most likely to have usable networks. + + + + The NATGW leader hosts the NATGW public IP address + CTDB_NATGW_PUBLIC_IP on the configured public + interfaces CTDB_NATGW_PUBLIC_IFACE and acts as + a router, masquerading outgoing connections from follower nodes + via this IP address. If + CTDB_NATGW_DEFAULT_GATEWAY is set then it + also establishes a fallback default route to the configured + this gateway with a metric of 10. A metric 10 route is used + so it can co-exist with other default routes that may be + available. + + + + A NATGW follower establishes its fallback default route to the + NATGW leader via the private network + CTDB_NATGW_PRIVATE_NETWORKwith a metric of 10. + This route is used for outbound connections when no other + default route is available because the node hosts no public + addresses. A metric 10 routes is used so that it can co-exist + with other default routes that may be available when the node + is hosting public addresses. + + + + CTDB_NATGW_STATIC_ROUTES can be used to + have NATGW create more specific routes instead of just default + routes. + + + + This is implemented in the 11.natgw + eventscript. Please see the eventscript file and the + NAT GATEWAY section in + ctdb-script.options + 5 for more details. + + + + + + + POLICY ROUTING + + + Policy routing is an optional CTDB feature to support complex + network topologies. Public addresses may be spread across + several different networks (or VLANs) and it may not be possible + to route packets from these public addresses via the system's + default route. Therefore, CTDB has support for policy routing + via the 13.per_ip_routing eventscript. + This allows routing to be specified for packets sourced from + each public address. The routes are added and removed as CTDB + moves public addresses between nodes. + + + + Configuration variables + + + There are 4 configuration variables related to policy routing: + CTDB_PER_IP_ROUTING_CONF, + CTDB_PER_IP_ROUTING_RULE_PREF, + CTDB_PER_IP_ROUTING_TABLE_ID_LOW, + CTDB_PER_IP_ROUTING_TABLE_ID_HIGH. See the + POLICY ROUTING section in + ctdb-script.options + 5 for more details. + + + + + Configuration + + + The format of each line of + CTDB_PER_IP_ROUTING_CONF is: + + + +<public_address> <network> [ <gateway> ] + + + + Leading whitespace is ignored and arbitrary whitespace may be + used as a separator. Lines that have a "public address" item + that doesn't match an actual public address are ignored. This + means that comment lines can be added using a leading + character such as '#', since this will never match an IP + address. + + + + A line without a gateway indicates a link local route. + + + + For example, consider the configuration line: + + + + 192.168.1.99 192.168.1.0/24 + + + + If the corresponding public_addresses line is: + + + + 192.168.1.99/24 eth2,eth3 + + + + CTDB_PER_IP_ROUTING_RULE_PREF is 100, and + CTDB adds the address to eth2 then the following routing + information is added: + + + + ip rule add from 192.168.1.99 pref 100 table ctdb.192.168.1.99 + ip route add 192.168.1.0/24 dev eth2 table ctdb.192.168.1.99 + + + + This causes traffic from 192.168.1.99 to 192.168.1.0/24 go via + eth2. + + + + The ip rule command will show (something + like - depending on other public addresses and other routes on + the system): + + + + 0: from all lookup local + 100: from 192.168.1.99 lookup ctdb.192.168.1.99 + 32766: from all lookup main + 32767: from all lookup default + + + + ip route show table ctdb.192.168.1.99 will show: + + + + 192.168.1.0/24 dev eth2 scope link + + + + The usual use for a line containing a gateway is to add a + default route corresponding to a particular source address. + Consider this line of configuration: + + + + 192.168.1.99 0.0.0.0/0 192.168.1.1 + + + + In the situation described above this will cause an extra + routing command to be executed: + + + + ip route add 0.0.0.0/0 via 192.168.1.1 dev eth2 table ctdb.192.168.1.99 + + + + With both configuration lines, ip route show table + ctdb.192.168.1.99 will show: + + + + 192.168.1.0/24 dev eth2 scope link + default via 192.168.1.1 dev eth2 + + + + + Sample configuration + + + Here is a more complete example configuration. + + + +/usr/local/etc/ctdb/public_addresses: + + 192.168.1.98 eth2,eth3 + 192.168.1.99 eth2,eth3 + +/usr/local/etc/ctdb/policy_routing: + + 192.168.1.98 192.168.1.0/24 + 192.168.1.98 192.168.200.0/24 192.168.1.254 + 192.168.1.98 0.0.0.0/0 192.168.1.1 + 192.168.1.99 192.168.1.0/24 + 192.168.1.99 192.168.200.0/24 192.168.1.254 + 192.168.1.99 0.0.0.0/0 192.168.1.1 + + + + The routes local packets as expected, the default route is as + previously discussed, but packets to 192.168.200.0/24 are + routed via the alternate gateway 192.168.1.254. + + + + + + + NOTIFICATIONS + + + When certain state changes occur in CTDB, it can be configured + to perform arbitrary actions via notifications. For example, + sending SNMP traps or emails when a node becomes unhealthy or + similar. + + + + The notification mechanism runs all executable files ending in + ".script" in + /usr/local/etc/ctdb/events/notification/, + ignoring any failures and continuing to run all files. + + + + CTDB currently generates notifications after CTDB changes to + these states: + + + + init + setup + startup + healthy + unhealthy + + + + + + LOG LEVELS + + + Valid log levels, in increasing order of verbosity, are: + + + + ERROR + WARNING + NOTICE + INFO + DEBUG + + + + + + REMOTE CLUSTER NODES + +It is possible to have a CTDB cluster that spans across a WAN link. +For example where you have a CTDB cluster in your datacentre but you also +want to have one additional CTDB node located at a remote branch site. +This is similar to how a WAN accelerator works but with the difference +that while a WAN-accelerator often acts as a Proxy or a MitM, in +the ctdb remote cluster node configuration the Samba instance at the remote site +IS the genuine server, not a proxy and not a MitM, and thus provides 100% +correct CIFS semantics to clients. + + + + See the cluster as one single multihomed samba server where one of + the NICs (the remote node) is very far away. + + + + NOTE: This does require that the cluster filesystem you use can cope + with WAN-link latencies. Not all cluster filesystems can handle + WAN-link latencies! Whether this will provide very good WAN-accelerator + performance or it will perform very poorly depends entirely + on how optimized your cluster filesystem is in handling high latency + for data and metadata operations. + + + + To activate a node as being a remote cluster node you need to + set the following two parameters in + /usr/local/etc/ctdb/ctdb.conf for the remote node: + +[legacy] + lmaster capability = false + leader capability = false + + + + + Verify with the command "ctdb getcapabilities" that that node no longer + has the leader or the lmaster capabilities. + + + + + + + SEE ALSO + + + ctdb + 1, + + ctdbd + 1, + + ctdb_diagnostics + 1, + + ltdbtool + 1, + + onnode + 1, + + ping_pong + 1, + + ctdb.conf + 5, + + ctdb-script.options + 5, + + ctdb.sysconfig + 5, + + ctdb-statistics + 7, + + ctdb-tunables + 7, + + , + + + + + + + + + This documentation was written by + Ronnie Sahlberg, + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ctdb.conf.5.xml b/ctdb/doc/ctdb.conf.5.xml new file mode 100644 index 0000000..87a7ea5 --- /dev/null +++ b/ctdb/doc/ctdb.conf.5.xml @@ -0,0 +1,652 @@ + + + + + + + ctdb.conf + 5 + ctdb + CTDB - clustered TDB database + + + + ctdb.conf + CTDB configuration file + + + + DESCRIPTION + + + This file contains CTDB configuration options that affect the + operation of CTDB daemons and command-line tools. The default + location of this file is + /usr/local/etc/ctdb/ctdb.conf. + + + + Note that this is a Samba-style configuration file, so it has a + very different syntax to previous CTDB configuration files. + + + + For event script options please see + ctdb-script.options + 5. + + + + Configuration options are grouped into several sections below. + There are only a few options in each section, allowing them to + be ordered (approximately) in decreasing order of importance. + + + + + + + LOGGING CONFIGURATION + + + + Options in this section control CTDB's logging. They are valid + within the logging section of file, + indicated by [logging]. + + + + + + log level = LOGLEVEL + + + LOGLEVEL is a string that controls the verbosity of + ctdbd's logging. See the LOG + LEVELS section in + ctdb + 7 for more details. + + + Default: NOTICE + + + + + + location = STRING + + + STRING specifies where ctdbd will write its log. + + + Valid values are: + + + + file:FILENAME + + + FILENAME where ctdbd will write its log. This is usually + /usr/local/var/log/log.ctdb. + + + + + syslog:METHOD + + + CTDB will log to syslog. By default this will use + the syslog(3) API. + + + If METHOD is specified then it specifies an + extension that causes logging to be done in a + non-blocking fashion. This can be useful under + heavy loads that might cause the syslog daemon to + dequeue messages too slowly, which would otherwise + cause CTDB to block when logging. METHOD must be + one of: + + + + nonblocking + + + CTDB will log to syslog via + /dev/log in non-blocking + mode. + + + + + udp + + + CTDB will log to syslog via UDP to + localhost:514. The syslog daemon must be + configured to listen on (at least) + localhost:514. Most implementations will log + the messages against hostname "localhost" - + this is a limit of the implementation for + compatibility with more syslog daemon + implementations. + + + + + udp-rfc5424 + + + As with "udp" but messages are sent in RFC5424 + format. This method will log the correct + hostname but is not as widely implemented in + syslog daemons. + + + + + + + + + Default: + file:/usr/local/var/log/log.ctdb + + + + + + + + + + CLUSTER CONFIGURATION + + + + Options in this section affect the CTDB cluster setup. They + are valid within the cluster section of + file, indicated by [cluster]. + + + + + + cluster lock = LOCK + + + LOCK specifies the cluster-wide mutex used to detect and + prevent a partitioned cluster (or "split brain"). + + + For information about the cluster lock please see the + CLUSTER LOCK section in + ctdb + 7. + + + Default: NONE. However, uses of a cluster lock is + strongly recommended. + + + + + + leader capability = true|false + + + Indicates whether a node can become the leader + for the cluster. If this is set to + false then the node will not be able to + become the leader for the cluster. This feature + is primarily used for making a cluster span across a WAN + link and use CTDB as a WAN-accelerator. + + + Please see the REMOTE CLUSTER NODES + section in + ctdb + 7 for more + information. + + + Default: true + + + + + + leader timeout = SECONDS + + + Number of SECONDS without a leader broadcast before a node + triggers an election. + + + Default: 5 + + + + + + node address = IPADDR + + + IPADDR is the private IP address that ctdbd will bind to. + + + This option is only required when automatic address + detection can not be used. This can be the case when + running multiple ctdbd daemons/nodes on the same physical + host (usually for testing), using InfiniBand for the + private network or on Linux when sysctl + net.ipv4.ip_nonlocal_bind=1. + + + Default: CTDB selects the first address from the nodes + list that it can bind to. See also the PRIVATE + ADDRESS section in + ctdb + 7. + + + + + + transport = tcp|ib + + + This option specifies which transport to use for ctdbd + internode communications on the private network. + + + ib means InfiniBand. The InfiniBand + support is not regularly tested. If it is known to be + broken then it may be disabled so that a value of + ib is considered invalid. + + + Default: tcp + + + + + + + + + + DATABASE CONFIGURATION + + + + Options in this section affect the CTDB database setup. They + are valid within the database section of + file, indicated by [database]. + + + + + + volatile database directory = DIRECTORY + + + DIRECTORY on local storage where CTDB keeps a local copy + of volatile TDB databases. This directory is local for + each node and should not be stored on the shared cluster + filesystem. + + + Mounting a tmpfs (or similar memory filesystem) on this + directory can provide a significant performance + improvement when there is I/O contention on the local + disk. + + + Default: /usr/local/var/lib/ctdb/volatile + + + + + + persistent database directory=DIRECTORY + + + DIRECTORY on local storage where CTDB keeps a local copy + of persistent TDB databases. This directory is local for + each node and should not be stored on the shared cluster + filesystem. + + + Default: /usr/local/var/lib/ctdb/persistent + + + + + + state database directory = DIRECTORY + + + DIRECTORY on local storage where CTDB keeps a local copy + of internal state TDB databases. This directory is local + for each node and should not be stored on the shared + cluster filesystem. + + + Default: /usr/local/var/lib/ctdb/state + + + + + + tdb mutexes = true|false + + + This parameter enables TDB_MUTEX_LOCKING feature on + volatile databases if the robust mutexes are + supported. This optimizes the record locking using robust + mutexes and is much more efficient that using posix locks. + + + If robust mutexes are unreliable on the platform being + used then they can be disabled by setting this to + false. + + + + + + lock debug script = FILENAME + + + FILENAME is a script used by CTDB's database locking code + to attempt to provide debugging information when CTDB is + unable to lock an entire database or a record. + + + This script should be a bare filename relative to the CTDB + configuration directory + (/usr/local/etc/ctdb/). Any + directory prefix is ignored and the path is calculated + relative to this directory. + + + CTDB provides a lock debugging script and installs it as + /usr/local/etc/ctdb/debug_locks.sh. + + + Default: NONE + + + + + + + + + + EVENT HANDLING CONFIGURATION + + + + Options in this section affect CTDB event handling. They are + valid within the event section of file, + indicated by [event]. + + + + + + debug script = FILENAME + + + FILENAME is a script used by CTDB's event handling code to + attempt to provide debugging information when an event + times out. + + + This script should be a bare filename relative to the CTDB + configuration directory + (/usr/local/etc/ctdb/). Any + directory prefix is ignored and the path is calculated + relative to this directory. + + + CTDB provides a script for debugging timed out event + scripts and installs it as + /usr/local/etc/ctdb/debug-hung-script.sh. + + + Default: NONE + + + + + + + + + + FAILOVER CONFIGURATION + + + + Options in this section affect CTDB failover. They are + valid within the failover section of file, + indicated by [failover]. + + + + + + disabled = true|false + + + If set to true then public IP failover + is disabled. + + + Default: false + + + + + + + + + + LEGACY CONFIGURATION + + + + Options in this section affect legacy CTDB setup. They are valid + within the legacy section of file, + indicated by [legacy]. + + + + + + ctdb start as stopped = true|false + + + If set to true CTDB starts in the + STOPPED state. + + + To allow the node to take part in the cluster it must be + manually continued with the ctdb + continue command. + + + Please see the NODE STATES section + in ctdb + 7 for more + information about the STOPPED state. + + + Default: false + + + + + + start as disabled = true|false + + + If set to true CTDB starts in the + DISABLED state. + + + To allow the node to host public IP addresses and + services, it must be manually enabled using the + ctdb enable command. + + + Please see the NODE STATES section + in ctdb + 7 for more + information about the DISABLED state. + + + Default: false + + + + + + realtime scheduling = true|false + + + Usually CTDB runs with real-time priority. This helps it + to perform effectively on a busy system, such as when + there are thousands of Samba clients. If you are running + CTDB on a platform that does not support real-time + priority, you can set this to false. + + + Default: true + + + + + + lmaster capability = true|false + + + Indicates whether a node can become a location master for + records in a database. If this is set to + false then the node will not be part of + the vnnmap. This feature is primarily used for making a + cluster span across a WAN link and use CTDB as a + WAN-accelerator. + + + Please see the REMOTE CLUSTER NODES + section in + ctdb + 7 for more + information. + + + Default: true + + + + + + script log level = LOGLEVEL + + + This option sets the debug level of event script output to + LOGLEVEL. + + + See the DEBUG LEVELS section in + ctdb + 7 for more + information. + + + Default: ERROR + + + + + + + + + + FILES + + + /usr/local/etc/ctdb/ctdb.conf + + + + + SEE ALSO + + ctdbd + 1, + + onnode + 1, + + ctdb.sysconfig + 5, + + ctdb-script.options + 5, + + ctdb + 7, + + ctdb-tunables + 7, + + + + + + + + + This documentation was written by + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ctdb.sysconfig.5.xml b/ctdb/doc/ctdb.sysconfig.5.xml new file mode 100644 index 0000000..2618934 --- /dev/null +++ b/ctdb/doc/ctdb.sysconfig.5.xml @@ -0,0 +1,240 @@ + + + + + + + ctdb.sysconfig + 5 + ctdb + CTDB - clustered TDB database + + + + ctdb.sysconfig + CTDB daemon configuration file + + + + DESCRIPTION + + + This file contains configuration that affects the operation of + CTDB. This is a distribution-specific service configuration + file such as /etc/sysconfig/ctdb (Red Hat) + or /etc/default/ctdb (Debian) and is a + shell script (see + sh + 1). + + + + + + + GLOBAL CONFIGURATION + + + + + + CTDB_INIT_STYLE=debian|redhat|suse + + + This is the init style used by the Linux distribution (or + other operating system) being used. This is usually + determined dynamically by checking the system. This + variable is used by the initscript to determine which init + system primitives to use. It is also used by some + eventscripts to choose the name of initscripts for certain + services, since these can vary between distributions. + + + If using CTDB's event scripts are unable to determine an + appropriate default then this option can also be placed in + a relevant + ctdb-script.options + 5 file. + + + Default: NONE. Guessed, based on features of + distribution. + + + + + + + + + + RESOURCE LIMITS + + + + + Maximum number of open files + + + + CTDB can use a lot of file descriptors, especially when used + with Samba. If there are thousands of smbd processes + connected to CTDB when this can mean that thousands of file + descriptors are used. For CTDB, it is often necessary to + increase limit on the maximum number of open files. + + + + The maximum number of open files should be configured using an + operating system mechanism. + + + + + + + systemd + + + + The + LimitNOFILE= + option can be used in a unit/service file increase the + maximum number of open files. See + systemd.exec + 5 for details. + + + + + + + SYSV init + + + + Use a command like ulimit -n + to increase the maximum + number of open files. This command can be put in the + relevant distribution-specific service configuration file. + + + + + + + + + + + Allowing core dumps + + + + Many distributions do not allow core dump files to be + generated by default. To assist with debugging, core files + can be enabled. This should be configured using an operating + system mechanism. + + + + + + + systemd + + + + The LimitCORE=0|unlimited option can + be used in a unit/service file. 0 + disallows core files, unlimited + allows them. maximum number of open files. See + systemd.exec + 5 for details. + + + + + + + SYSV init + + + + Use a command like ulimit -c 0|unlimited + to disable or enable core files as required. This + command can be put in the relevant distribution-specific + service configuration file. + + + + + + + + + + + + FILES + + + /etc/sysconfig/ctdb + /etc/default/ctdb + /usr/local/etc/ctdb/script.options + + + + + SEE ALSO + + ctdbd + 1, + + ctdb-script.options + 5, + + ctdb + 7, + + + + + + + + + This documentation was written by + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ctdb_diagnostics.1.xml b/ctdb/doc/ctdb_diagnostics.1.xml new file mode 100644 index 0000000..274c70d --- /dev/null +++ b/ctdb/doc/ctdb_diagnostics.1.xml @@ -0,0 +1,128 @@ + + + + + + ctdb_diagnostics + 1 + ctdb + CTDB - clustered TDB database + + + + ctdb_diagnostics + dump diagnostic information about CTDB/Samba installation + + + + + ctdb_diagnostics + OPTIONS + ... + + + + + DESCRIPTION + + ctdb_diagnostics is used to dump diagnostic information about a + clustered Samba installation. This includes configuration + files, output of relevant commands and logs. This information + can be used to check the correctness of the configuration and to + diagnose problems. + + + + + OPTIONS + + + + + -n <nodes> + + + Comma separated list of nodes to operate on + + + + + + -c + + + Ignore comment lines (starting with '#') in file comparisons + + + + + + -w + + + Ignore whitespace in file comparisons + + + + + + --no-ads + + + Do not use commands that assume an Active Directory Server + + + + + + + + + + SEE ALSO + + ctdb + 1, + ctdb + 7, + + + + + + + + This documentation was written by Martijn van Brummelen + + + + + 2015 + Martijn van Brummelen + + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + + diff --git a/ctdb/doc/ctdb_mutex_ceph_rados_helper.7.xml b/ctdb/doc/ctdb_mutex_ceph_rados_helper.7.xml new file mode 100644 index 0000000..dd3dbab --- /dev/null +++ b/ctdb/doc/ctdb_mutex_ceph_rados_helper.7.xml @@ -0,0 +1,96 @@ + + + + + + Ceph RADOS Mutex + 7 + ctdb + CTDB - clustered TDB database + + + + ctdb_mutex_ceph_rados_helper + Ceph RADOS cluster mutex helper + + + + DESCRIPTION + + ctdb_mutex_ceph_rados_helper can be used as a cluster lock provider + for CTDB. When configured, split brain avoidance during CTDB recovery + will be handled using locks against an object located in a Ceph RADOS + pool. + To enable this functionality, include the following line in the + [cluster] section of + ctdb.conf + 5: + + +cluster lock = !ctdb_mutex_ceph_rados_helper [Cluster] [User] [Pool] [Object] + +Cluster: Ceph cluster name (e.g. ceph) +User: Ceph cluster user name (e.g. client.admin) +Pool: Ceph RADOS pool name +Object: Ceph RADOS object name + + + The Ceph cluster Cluster must be up and running, + with a configuration, and keyring file for User + located in a librados default search path (e.g. /etc/ceph/). + Pool must already exist. + + + For informational purposes, ctdb_mutex_ceph_rados_helper will also + register the cluster lock holder in Ceph Manager's service map. + + + + + SEE ALSO + + ctdb + 7, + + ctdbd + 1, + + + + + + + + + This documentation was written by David Disseldorp + + + + + 2016 + David Disseldorp + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ctdbd.1.xml b/ctdb/doc/ctdbd.1.xml new file mode 100644 index 0000000..314964e --- /dev/null +++ b/ctdb/doc/ctdbd.1.xml @@ -0,0 +1,129 @@ + + + + + + + ctdbd + 1 + ctdb + CTDB - clustered TDB database + + + + ctdbd + The CTDB cluster daemon + + + + + ctdbd + OPTION + + + + + DESCRIPTION + + ctdbd is the main CTDB daemon. + + + + See ctdb + 7 for an overview of CTDB. + + + + + GENERAL OPTIONS + + + + -i, --interactive + + + Enable interactive mode. This will make ctdbd run in the + foreground and not detach from the terminal. In this mode + ctdbd will log to stderr. + + + By default ctdbd will detach itself and run in the + background as a daemon, logging to the configured + destination. + + + + + + -?, --help + + + Display a summary of options. + + + + + + + + + SEE ALSO + + ctdb + 1, + + onnode + 1, + + ctdb.conf + 5, + + ctdb + 7, + + ctdb-tunables + 7, + + + + + + + + + This documentation was written by + Ronnie Sahlberg, + Amitay Isaacs, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/examples/11.natgw.options b/ctdb/doc/examples/11.natgw.options new file mode 100644 index 0000000..e2460cb --- /dev/null +++ b/ctdb/doc/examples/11.natgw.options @@ -0,0 +1,25 @@ +# NAT gateway configuration +# +# See ctdb.conf for main cluster configuration details +# +# Cluster provides file services on following IP addresses +# +# 10.1.1.101 - 10.1.1.106 +# +# When a node is not hosting any IPs, it cannot connect to network +# infrastructure (e.g. DNS, Active Directory, ...). +# +# Using NAT gateway feature of CTDB allows a node not hosting IPs to connect +# to network infrastructure using the additional CTDB_NATGW_PUBLIC_IP. + +# ---------- /etc/ctdb/natgw_nodes ---------- +# 192.168.1.1 +# 192.168.1.2 +# 192.168.1.3 +# ---------- /etc/ctdb/natgw_nodes ---------- +# +CTDB_NATGW_PUBLIC_IP=10.1.1.121/24 +CTDB_NATGW_PUBLIC_IFACE=eth1 +CTDB_NATGW_DEFAULT_GATEWAY=10.1.1.254 +CTDB_NATGW_PRIVATE_NETWORK=192.168.1.0/24 +CTDB_NATGW_NODES=/etc/ctdb/natgw_nodes diff --git a/ctdb/doc/examples/20.nfs_ganesha.check b/ctdb/doc/examples/20.nfs_ganesha.check new file mode 100644 index 0000000..3288f16 --- /dev/null +++ b/ctdb/doc/examples/20.nfs_ganesha.check @@ -0,0 +1,8 @@ +# nfs_ganesha +restart_every=2 +unhealthy_after=6 +service_stop_cmd="$CTDB_NFS_CALLOUT stop nfs" +service_start_cmd="$CTDB_NFS_CALLOUT start nfs" +service_check_cmd="$CTDB_NFS_CALLOUT check nfs" +# Ganesha initscript restarts rpc.statd and stack trace is desirable! +service_debug_cmd="program_stack_traces status 5" diff --git a/ctdb/doc/examples/91.lvs.options b/ctdb/doc/examples/91.lvs.options new file mode 100644 index 0000000..adb3660 --- /dev/null +++ b/ctdb/doc/examples/91.lvs.options @@ -0,0 +1,12 @@ +# LVS configuration +# +# ---------- /etc/ctdb/lvs_nodes ---------- +# 192.168.1.1 +# 192.168.1.2 +# 192.168.1.3 +# ---------- /etc/ctdb/lvs_nodes ---------- +# +CTDB_LVS_NODES=/etc/ctdb/lvs_nodes + +CTDB_LVS_PUBLIC_IP=10.1.1.101 +CTDB_LVS_PUBLIC_IFACE=eth1 diff --git a/ctdb/doc/examples/README b/ctdb/doc/examples/README new file mode 100644 index 0000000..a4ea222 --- /dev/null +++ b/ctdb/doc/examples/README @@ -0,0 +1,15 @@ +This directory includes... + +Sample CTDB configuration files: + + o ctdb.conf - Main configuration file + o 11.natgw.options - Options for the 11.natgw event script + o 91.lvs.options - Options for the 91.lvs event script + +Sample 60.nfs configuration for NFS ganesha - callout script and +.check file + + o nfs-ganesha-callout + o 20.nfs_ganesha.check + +See the comment at the top of nfs-ganesha-callout for instructions. diff --git a/ctdb/doc/examples/config_migrate.sh b/ctdb/doc/examples/config_migrate.sh new file mode 100755 index 0000000..874e96c --- /dev/null +++ b/ctdb/doc/examples/config_migrate.sh @@ -0,0 +1,741 @@ +#!/bin/sh + +# config_migrate.sh - migrate old ctdbd.conf file to new configuration files +# +# Input files are old-style CTDB configuration files, including: +# +# /etc/ctdb/ctdbd.conf +# /usr/local/etc/ctdb/ctdbd.conf +# /etc/sysconfig/ctdb +# /etc/defaults/ctdb +# +# These files are sourced by this script. They used to be sourced by +# ctdbd_wrapper, so this should not be too surprising. +# +# By default, the output directory is the given configuration +# directory. An alternate output directory can be specified if this +# isn't desired. +# +# The output directory will contain the following if non-empty: +# +# * ctdb.conf (may be empty) +# * script.options +# * ctdb.tunables +# * ctdb.sysconfig - consider installing as /etc/sysconfig/ctdb, +# /etc/default/ctdb, or similar +# * commands.sh - consider running commands in this files +# * README.warn - warnings about removed/invalid configuration options + +usage () +{ + cat <] [-o ] ... +EOF + exit 1 +} + +config_dir="" +out_dir="" +force=false + +while getopts "d:fho:?" opt ; do + case "$opt" in + d) config_dir="$OPTARG" ;; + f) force=true ;; + o) out_dir="$OPTARG" ;; + \?|h) usage ;; + esac +done +shift $((OPTIND - 1)) + +if [ $# -lt 1 ] ; then + usage +fi + +if [ -z "$config_dir" ] ; then + echo "Assuming \"/etc/ctdb\" as ctdb configuration directory" + echo "If that's not correct, please specify config dir with -d" + echo + config_dir="/etc/ctdb" +else + echo "Using \"$config_dir\" as ctdb configuration directory" + echo +fi + +if [ -z "$out_dir" ] ; then + echo "No output directory specified, using \"$config_dir\"" + echo + out_dir="$config_dir" +fi + +############################################################ + +# +# Output file handling +# + +out_file_check_and_create () +{ + _out_file="$1" + + if [ -f "$_out_file" ] ; then + if ! $force ; then + echo "Not overwriting existing file: ${_out_file}" >&2 + return 1 + fi + mv -v "$_out_file" "${_out_file}.convertsave" + fi + + touch "$_out_file" + + return 0 +} + +out_file_remove_if_empty () +{ + _out_file="$1" + + if [ ! -s "$_out_file" ] ; then + rm "$_out_file" + fi +} + +############################################################ + +# +# Option/tunable/service conversion and validity checking +# +# This is basically the data that drives most of the rest of the +# script +# + +# Convert a ctdbd.conf opt+val into a ctdb.conf section+opt+val +# +# If opt is matched and val is empty then output is printed, allowing +# this function to be reused to check if opt is valid. +# +# Note that for boolean options, the expected value and the new value +# form part of the data. +get_ctdb_conf_option () +{ + _opt="$1" + _val="$2" + + awk -v opt="${_opt}" -v val="${_val}" \ + '$3 == opt { + if (!$4 || !val || val == $4) { + if ($5) { + print $1, $2, $5 + } else { + print $1, $2, val + } + } + }' </dev/null +} + +############################################################ + +# +# Utilities +# + +# List all options starting with "CTDB_" set in given configuration files +list_options () +{ + set | + sed -n 's|^\(CTDB_[^=]*\)=\(.*\)|\1 \2|p' | + while read -r _var _val ; do + # Strip quotes from value + _val=$(echo "$_val" | sed -e "s|^'||" -e "s|'\$||") + + echo "${_var} ${_val}" + done +} + +# List all tunables set in the given configuration files +list_tunables () +{ + list_options | + while read -r _opt _val ; do + case "$_opt" in + CTDB_SET_*) echo "${_opt#CTDB_SET_} ${_val}" ;; + esac + done +} + +# List all managed services according to the given configuration files +list_managed_services () +{ + # + # CTDB_MANAGES_="yes" + # + list_options | + while read -r _opt _val ; do + case "$_opt" in + CTDB_MANAGES_*) : ;; + *) continue ;; + esac + + if [ "$_val" != "yes" ] ; then + continue + fi + + # Trim and downcase + echo "${_opt#CTDB_MANAGES_}" | tr '[:upper:]' '[:lower:]' + done + + # + # CTDB_MANAGED_SERVICES + # + for _service in $CTDB_MANAGED_SERVICES ; do + echo "$_service" + done +} + +############################################################ + +# +# Print warnings for removed and unknown options +# + + +# Print a warning as a bullet list item +# +# Arguments after the 1st are printed as a subsequent paragraph. +warn () +{ + bullet="$1" ; shift + + printf '* %s\n\n' "$bullet" + + if [ $# -gt 0 ] ; then + printf ' %s\n\n' "$*" + fi +} + +warn_about_CTDB_DBDIR_tmpfs_yes () +{ + if $ctdb_dbdir_tmpfs_magic ; then + warn "Option \"CTDB_DBDIR=tmpfs\" is no longer available:" \ + "Permanently mount a tmpfs filesystem on the volatile" \ + "database directory" + fi +} + +warn_about_unknown_managed_services () +{ + list_managed_services | + while read -r _s ; do + if check_valid_service "$_s" ; then + continue + fi + warn "Unknown service \"${_s}\" marked as managed" \ + "If this is a 3rd party service, please enable it manually" + done +} + +warn_about_removed_and_unknown_options () +{ + list_options | + while read -r _opt _val ; do + if check_ctdb_conf_option "$_opt" ; then + continue + fi + + if check_valid_script_option "$_opt" ; then + continue + fi + + case "$_opt" in + CTDB_MANAGED_SERVICES|\ + CTDB_MANAGES_*|\ + CTDB_SET_*|\ + CTDB_NODES|\ + CTDB_PUBLIC_ADDRESSES|\ + CTDB_MAX_OPEN_FILES|\ + CTDB_SUPPRESS_COREFILE) + # Handled elsewhere + continue + ;; + esac + + if check_removed_option "$_opt" ; then + warn "Option \"${_opt}\" is no longer available" \ + "Please see the WHATSNEW.txt" + continue + fi + + warn "Option \"${_opt}\" is unknown" + done +} + +warn_about_removed_and_unknown_tunables () +{ + list_tunables | + while read -r _var _val ; do + if check_valid_tunable "$_var" ; then + continue + fi + + if check_removed_tunable "$_var" ; then + warn "Tunable \"${_var}\" is no longer available" \ + "Please see the WHATSNEW.txt" + continue + fi + + warn "Tunable \"${_var}\" is unknown" + done +} + +############################################################ + +# +# Top-level file builders +# + +build_ctdb_conf () +{ + _out_file="$1" + + out_file_check_and_create "$_out_file" || return + + list_options | + while read -r _opt _val ; do + case "$_opt" in + CTDB_SET_*) + _opt="${_opt#CTDB_SET_}" + _out=$(get_ctdb_conf_tunable_option "$_opt" "$_val") + ;; + *) + _out=$(get_ctdb_conf_option "$_opt" "$_val") + esac + if [ -z "$_out" ] ; then + continue + fi + + # $_out is section and key, replace dashes with spaces + # Intentional word splitting + # shellcheck disable=SC2086 + set -- $_out + _section=$(echo "$1" | sed -e 's|-| |g') + _key=$(echo "$2" | sed -e 's|-| |g') + _newval="$3" + + if ! grep -Fqx "[${_section}]" "$_out_file" ; then + # Add blank line if file is not empty + if [ -s "$_out_file" ] ; then + echo >>"$_out_file" + fi + # Create section at end of file + echo "[${_section}]" >>"$_out_file" + fi + + # Must escape leading TAB or sed eats it + sed -i -e "/\\[${_section}\\]/a\ +\\ ${_key} = ${_newval} +" "$_out_file" + + done + +} + +build_script_options () +{ + _out_file="$1" + + out_file_check_and_create "$_out_file" || return + + list_options | + while read -r _var _val ; do + if check_valid_script_option "$_var" ; then + echo "${_var}=${_val}" + fi + done >>"$_out_file" + + out_file_remove_if_empty "$_out_file" +} + +build_ctdb_tunables () +{ + _out_file="$1" + + out_file_check_and_create "$_out_file" || return + + list_tunables | + while read -r _var _val ; do + if check_ctdb_conf_tunable_option "$_var" ; then + continue + fi + if ! check_valid_tunable "$_var" ; then + continue + fi + echo "${_var}=${_val}" + done >>"$_out_file" + + out_file_remove_if_empty "$_out_file" +} + +build_ctdb_sysconfig () +{ + _out_file="$1" + + out_file_check_and_create "$_out_file" || return + + if [ -n "$CTDB_SUPPRESS_COREFILE" ] ; then + if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ] ; then + echo "ulimit -c 0" + else + echo "ulimit -c unlimited" + fi >>"$_out_file" + fi + + if [ -n "$CTDB_MAX_OPEN_FILES" ] ; then + echo "ulimit -n ${CTDB_MAX_OPEN_FILES}" >>"$_out_file" + fi + + out_file_remove_if_empty "$_out_file" +} + +build_commands_sh () +{ + _out_file="$1" + + out_file_check_and_create "$_out_file" || return + + # + # Enable script for managed services + # + list_managed_services | + while read -r _service ; do + print_event_script_enable_command "$_service" + done >>"$_out_file" + + # + # CTDB_NODES no longer available + # + if [ -n "$CTDB_NODES" ] ; then + if [ "$CTDB_NODES" = "${config_dir}/nodes" ] ; then + cat <>"$_out_file" + fi + + # + # CTDB_PUBLIC_ADDRESSES no longer available + # + if [ -n "$CTDB_PUBLIC_ADDRESSES" ] ; then + _pa="public_addresses" + if [ "$CTDB_PUBLIC_ADDRESSES" = "${config_dir}/${_pa}" ] ; then + cat <>"$_out_file" + fi + + out_file_remove_if_empty "$_out_file" +} + +build_README_warn () +{ + _out_file="$1" + + out_file_check_and_create "$_out_file" || return + + { + warn_about_CTDB_DBDIR_tmpfs_yes + warn_about_unknown_managed_services + warn_about_removed_and_unknown_options + warn_about_removed_and_unknown_tunables + } >>"$_out_file" + + out_file_remove_if_empty "$_out_file" +} + +############################################################ + +mkdir -p "$out_dir" || exit 1 + +# Source the input files +for i ; do + # Unknown non-constant source + # shellcheck disable=SC1090 + . "$i" +done + +# Special case +ctdb_dbdir_tmpfs_magic=false +if [ "$CTDB_DBDIR" = "tmpfs" ] ; then + ctdb_dbdir_tmpfs_magic=true + unset CTDB_DBDIR +fi + +build_ctdb_conf "${out_dir}/ctdb.conf" +build_script_options "${out_dir}/script.options" +build_ctdb_tunables "${out_dir}/ctdb.tunables" +build_ctdb_sysconfig "${out_dir}/ctdb.sysconfig" +build_commands_sh "${out_dir}/commands.sh" +build_README_warn "${out_dir}/README.warn" diff --git a/ctdb/doc/examples/config_migrate.test_input b/ctdb/doc/examples/config_migrate.test_input new file mode 100644 index 0000000..e823fd4 --- /dev/null +++ b/ctdb/doc/examples/config_migrate.test_input @@ -0,0 +1,50 @@ +CTDB_BASE="/etc/ctdb" + +CTDB_RECOVERY_LOCK="/some/place/on/shared/storage" + +CTDB_NODES="/etc/ctdb/nodes" + +CTDB_PUBLIC_ADDRESSES="/clusterfs/.ctdb/public_addresses" + +CTDB_SOCKET="/var/run/ctdb.sock" + +CTDB_MANAGES_SAMBA="yes" +CTDB_MANAGES_WINBIND="yes" +CTDB_MANAGES_NFS="yes" +CTDB_MANAGED_SERVICES="vsftpd" +CTDB_MANAGES_FOOBAR="yes" + +CTDB_MAX_OPEN_FILES=10000 + +CTDB_LOGGING="syslog" +CTDB_DEBUGLEVEL="ERROR" + +CTDB_SAMBA_CHECK_PORTS="445" +CTDB_SAMBA_SKIP_SHARE_CHECK="yes" + +CTDB_START_AS_STOPPED="yes" +CTDB_CAPABILITY_RECMASTER="no" +CTDB_CAPABILITY_LMASTER="yes" + +CTDB_NOSETSCHED="yes" + +CTDB_FOO="bar" + +CTDB_NATGW_PUBLIC_IP=10.1.1.121/24 +CTDB_NATGW_PUBLIC_IFACE=eth1 +CTDB_NATGW_DEFAULT_GATEWAY=10.1.1.254 +CTDB_NATGW_PRIVATE_NETWORK=192.168.1.0/24 +CTDB_NATGW_NODES=/etc/ctdb/natgw_nodes + +CTDB_SET_TDBMutexEnabled=0 +CTDB_SET_IPALLOCALGORITHM=0 +CTDB_SET_noiphostonalldisabled=0 +CTDB_SET_DisableIpFailover=1 +CTDB_SET_foobar=0 + +CTDB_SUPPRESS_COREFILE="yes" +CTDB_MAX_OPEN_FILES="1048576" + +CTDB_NOTIFY_SCRIPT=/etc/ctdb/notify-custom.sh + +CTDB_DBDIR=tmpfs diff --git a/ctdb/doc/examples/ctdb.conf b/ctdb/doc/examples/ctdb.conf new file mode 100644 index 0000000..3a8ccc6 --- /dev/null +++ b/ctdb/doc/examples/ctdb.conf @@ -0,0 +1,73 @@ +# +# CTDB configuration for simple cluster +# +# This is the sample configuration for a 3-node CTDB cluster providing file +# services via Samba and NFS. +# +# Cluster details: +# +# internal network (192.168.1.0/24) +# -------+----------------------+-----------------------+---------- +# | | | +# | | | +# eth0 | 192.168.1.1 eth0 | 192.168.1.2 eth0 | 192.168.1.3 +# +-----+-----+ +-----+-----+ +-----+-----+ +# | | | | | | +# | Node 1 | | Node 2 | | Node 3 | +# | | | | | | +# +-----+-----+ +-----+-----+ +-----+-----+ +# eth1 | 10.1.1.1 eth1 | 10.1.1.2 eth1 | 10.1.1.3 +# | | | +# | | | +# -------+----------------------+-----------------------+---------- +# public network (10.1.1.0/24) +# +# +# Storage details: +# +# Each node has a shared storage - /shared +# +# +# Service details: +# +# Cluster provides file services on following IP addresses +# +# 10.1.1.101 - 10.1.1.106 +# +# Each node also has a fixed IP address on public network. This is used to +# communicate to network infrastructure (e.g. DNS, Active Directory, ...). +# Make sure that file services are not available on these fixed IP addresses +# (e.g. network filtering, using cluster hostname instead of IPs) + +[logging] + # Enable logging to syslog + location = syslog + + # Default log level + log level = NOTICE + +[cluster] + cluster lock = /shared/cluster.lock + +# +# Nodes configuration +# +# ---------- /etc/ctdb/nodes ---------- +# 192.168.1.1 +# 192.168.1.2 +# 192.168.1.3 +# ---------- /etc/ctdb/nodes ---------- +# +# +# Public addresses configuration +# +# ---------- /etc/ctdb/public_addresses ---------- +# 10.1.1.101/24 eth1 +# 10.1.1.102/24 eth1 +# 10.1.1.103/24 eth1 +# 10.1.1.104/24 eth1 +# 10.1.1.105/24 eth1 +# 10.1.1.106/24 eth1 +# ---------- /etc/ctdb/public_addresses ---------- +# +# Event script configuration - see *.options examples diff --git a/ctdb/doc/examples/ctdb.spec.in b/ctdb/doc/examples/ctdb.spec.in new file mode 100644 index 0000000..addb7e1 --- /dev/null +++ b/ctdb/doc/examples/ctdb.spec.in @@ -0,0 +1,291 @@ +%define with_systemd %{?_with_systemd: 1} %{?!_with_systemd: 0} +%define initdir %{_sysconfdir}/init.d +Name: ctdb +Summary: Clustered TDB +Vendor: Samba Team +Packager: Samba Team +Version: @VERSION@ +Release: @RELEASE@ +Epoch: 0 +License: GNU GPL version 3 +Group: System Environment/Daemons +URL: http://ctdb.samba.org/ + +Source: ctdb-%{version}.tar.gz + +# Packages +Requires: coreutils, sed, gawk, iptables, iproute, procps, ethtool, sudo +# Commands - package name might vary +Requires: /usr/bin/killall, /bin/kill, /bin/ss + +Provides: ctdb = %{version} + +Prefix: /usr +BuildRoot: %{_tmppath}/%{name}-%{version}-root + +# Allow build with system libraries +# To enable, run rpmbuild with, +# "--with system_talloc" +# "--with system_tdb" +# "--with system_tevent" +#%define with_included_talloc %{?_with_system_talloc: 0} %{?!_with_system_talloc: 1} +#%define with_included_tdb %{?_with_system_tdb: 0} %{?!_with_system_tdb: 1} +#%define with_included_tevent %{?_with_system_tevent: 0} %{?!_with_system_tevent: 1} + +%define with_included_talloc 0 +%define with_included_tevent 0 +%define with_included_tdb 0 + +# Required minimum library versions when building with system libraries +%define libtalloc_version 2.0.8 +%define libtdb_version 1.3.11 +%define libtevent_version 0.9.16 + +%if ! %with_included_talloc +BuildRequires: libtalloc-devel >= %{libtalloc_version} +Requires: libtalloc >= %{libtalloc_version} +%endif +%if ! %with_included_tdb +BuildRequires: libtdb-devel >= %{libtdb_version} +Requires: libtdb >= %{libtdb_version} +%endif +%if ! %with_included_tevent +BuildRequires: libtevent-devel >= %{libtevent_version} +Requires: libtevent >= %{libtevent_version} +%endif + +# To build the ctdb-pcp-pmda package, run rpmbuild with "--with pmda" +%define with_pcp_pmda %{?_with_pmda: 1} %{?!_with_pmda: 0} +%if %with_pcp_pmda +BuildRequires: pcp-libs-devel +%endif + +%if %{with_systemd} +BuildRequires: systemd-units +%endif + +%description +ctdb is the clustered database used by samba + +####################################################################### + + + +%prep +%setup -q +# setup the init script and sysconfig file +%setup -T -D -n ctdb-%{version} -q + +%build + +## check for ccache +if ccache -h >/dev/null 2>&1 ; then + CC="ccache gcc" +else + CC="gcc" +fi + +export CC + +CFLAGS="$RPM_OPT_FLAGS $EXTRA -D_GNU_SOURCE" \ +./configure \ + --builtin-libraries=replace,popt \ + --bundled-libraries=!talloc,!tevent,!tdb \ + --minimum-library-version=talloc:%libtalloc_version,tdb:%libtdb_version,tevent:%libtevent_version \ +%if %with_pcp_pmda + --enable-pmda \ +%endif + --prefix=%{_prefix} \ + --includedir=%{_includedir}/ctdb \ + --libdir=%{_libdir} \ + --libexecdir=%{_libexecdir} \ + --sysconfdir=%{_sysconfdir} \ + --mandir=%{_mandir} \ + --localstatedir=%{_localstatedir} + +make -j + +%install +# Clean up in case there is trash left from a previous build +rm -rf $RPM_BUILD_ROOT + +# Create the target build directory hierarchy +mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/sudoers.d + +DESTDIR=$RPM_BUILD_ROOT make -j install + +install -m644 config/ctdb.conf $RPM_BUILD_ROOT%{_sysconfdir}/ctdb +install -m644 config/ctdb.tunables $RPM_BUILD_ROOT%{_sysconfdir}/ctdb +install -m644 config/script.options $RPM_BUILD_ROOT%{_sysconfdir}/ctdb + +mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig +install -m644 config/ctdb.sysconfig $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/ctdb + +%if %{with_systemd} +mkdir -p $RPM_BUILD_ROOT%{_unitdir} +install -m 755 config/ctdb.service $RPM_BUILD_ROOT%{_unitdir} +%else +mkdir -p $RPM_BUILD_ROOT%{initdir} +install -m755 config/ctdb.init $RPM_BUILD_ROOT%{initdir}/ctdb +%endif + +# This is a hack. All documents should be installed in /usr/share/doc. +cp config/events/README README.eventscripts +rm -f $RPM_BUILD_ROOT%{_sysconfdir}/ctdb/nfs-checks.d/README +cp config/nfs-checks.d/README README.nfs-checks.d +cp config/notification.README README.notification + +# Remove "*.old" files +find $RPM_BUILD_ROOT -name "*.old" -exec rm -f {} \; + +%clean +rm -rf $RPM_BUILD_ROOT + +%post + +# If mandatory 00.ctdb event script is not enabled then enable it and +# some other scripts. The assumption here is that this is a +# first-time install or an upgrade to a version that requires event +# scripts to be enabled via symlinks. +required_script="00.ctdb" +required_path="%{_sysconfdir}/ctdb/events/legacy/${required_script}.script" +if [ ! -L "$required_path" ] && [ ! -e "$required_path" ] ; then + default_scripts="${required_script} + 01.reclock + 05.system + 10.interface + " + for t in $default_scripts ; do + tgt="%{_datadir}/ctdb/events/legacy/${t}.script" + name="%{_sysconfdir}/ctdb/events/legacy/${t}.script" + # Directory is created via install and files + ln -s "$tgt" "$name" + done +fi + +%preun + +# Uninstall, not upgrade. Clean up by removing any remaining links. +if [ "$1" = "0" ] ; then + for i in "%{_sysconfdir}/ctdb/events/legacy/"*.script ; do + if [ -L "$i" ] ; then + rm -f "$i" + fi + done +fi + +%files +%defattr(-,root,root) + +%config(noreplace) %{_sysconfdir}/ctdb/ctdb.conf +%config(noreplace) %{_sysconfdir}/ctdb/ctdb.tunables +%config(noreplace) %{_sysconfdir}/ctdb/script.options +%{_sysconfdir}/ctdb/notify.sh +%config(noreplace) %{_sysconfdir}/ctdb/debug-hung-script.sh +%config(noreplace) %{_sysconfdir}/ctdb/ctdb-crash-cleanup.sh +%config(noreplace) %{_sysconfdir}/ctdb/debug_locks.sh + +%config(noreplace, missingok) %{_sysconfdir}/sysconfig/ctdb + +%if %{with_systemd} +%{_unitdir}/ctdb.service +%else +%attr(755,root,root) %{initdir}/ctdb +%endif + +%doc README COPYING +%doc README.eventscripts README.notification +%doc doc/recovery-process.txt +%doc doc/cluster_mutex_helper.txt +%doc doc/*.html +%doc doc/examples +%{_sysconfdir}/sudoers.d/ctdb +%dir %{_sysconfdir}/ctdb +%{_sysconfdir}/ctdb/functions +%dir %{_sysconfdir}/ctdb/events +%{_sysconfdir}/ctdb/events/* +%dir %{_sysconfdir}/ctdb/nfs-checks.d +%config(noreplace) %{_sysconfdir}/ctdb/nfs-checks.d/00.portmapper.check +%config(noreplace) %{_sysconfdir}/ctdb/nfs-checks.d/10.status.check +%config(noreplace) %{_sysconfdir}/ctdb/nfs-checks.d/20.nfs.check +%config(noreplace) %{_sysconfdir}/ctdb/nfs-checks.d/30.nlockmgr.check +%config(noreplace) %{_sysconfdir}/ctdb/nfs-checks.d/40.mountd.check +%config(noreplace) %{_sysconfdir}/ctdb/nfs-checks.d/50.rquotad.check +%{_sysconfdir}/ctdb/statd-callout +%{_sysconfdir}/ctdb/nfs-linux-kernel-callout +%{_sbindir}/ctdbd +%{_bindir}/ctdb +%{_bindir}/ping_pong +%{_bindir}/ltdbtool +%{_bindir}/ctdb_diagnostics +%{_bindir}/onnode +%dir %{_libexecdir}/ctdb +%{_libexecdir}/ctdb/* +%dir %{_libdir}/ctdb +%{_libexecdir}/tdb_mutex_check +%{_libdir}/ctdb/lib* +%dir %{_datadir}/ctdb/events +%{_datadir}/ctdb/events/* +%{_mandir}/man1/ctdb.1.gz +%{_mandir}/man1/ctdb_diagnostics.1.gz +%{_mandir}/man1/ctdbd.1.gz +%{_mandir}/man1/onnode.1.gz +%{_mandir}/man1/ltdbtool.1.gz +%{_mandir}/man1/ping_pong.1.gz +%{_mandir}/man5/ctdb.conf.5.gz +%{_mandir}/man5/ctdb-script.options.5.gz +%{_mandir}/man5/ctdb.sysconfig.5.gz +%{_mandir}/man7/ctdb.7.gz +%{_mandir}/man7/ctdb-statistics.7.gz +%{_mandir}/man7/ctdb-tunables.7.gz +%attr(0700,root,root) %dir %{_localstatedir}/lib/ctdb +%attr(0700,root,root) %dir %{_localstatedir}/lib/ctdb/* +%attr(0700,root,root) %dir %{_localstatedir}/run/ctdb + + +%package devel +Summary: CTDB development libraries +Group: Development/Libraries + +%description devel +development libraries for ctdb + +%files devel +%defattr(-,root,root) + +%package tests +Summary: CTDB test suite +Group: Development/Tools +Requires: ctdb = %{version} +Requires: nc, tcpdump + +%description tests +test suite for ctdb + +%files tests +%defattr(-,root,root) +%dir %{_datadir}/%{name}/tests +%{_datadir}/%{name}/tests/* +%dir %{_libexecdir}/%{name}/tests +%{_libexecdir}/%{name}/tests/* +%{_bindir}/ctdb_run_tests +%{_bindir}/ctdb_run_cluster_tests +%{_bindir}/ctdb_local_daemons +%doc tests/README + +%if %with_pcp_pmda + +%package pcp-pmda +Summary: CTDB PCP pmda support +Group: Development/Tools +Requires: ctdb = %{version} +Requires: pcp-libs + +%description pcp-pmda +Performance Co-Pilot (PCP) support for CTDB + +%files pcp-pmda +%dir %{_localstatedir}/lib/pcp/pmdas/ctdb +%{_localstatedir}/lib/pcp/pmdas/ctdb/* + +%endif diff --git a/ctdb/doc/examples/nfs-ganesha-callout b/ctdb/doc/examples/nfs-ganesha-callout new file mode 100755 index 0000000..7c7b074 --- /dev/null +++ b/ctdb/doc/examples/nfs-ganesha-callout @@ -0,0 +1,352 @@ +#!/bin/sh + +# This is an example CTDB NFS callout script for Ganesha. It is based +# on the last version of 60.ganesha shipped with CTDB. As such, it +# does not try to monitor RPC services that were not monitored by +# 60.ganesha - this might be a useful improvement. It has also not +# been properly tested. + +# You should check your version of NFS Ganesha to see if it ships with +# a newer callout. + +# To use this: +# +# * Set CTDB_NFS_CALLOUT in your CTDB configuration to point to (a +# copy of) this script, making sure it is executable. +# +# * Create a new directory alongside the nfs-checks.d directory, for +# example nfs-checks-ganesha.d. Install 20.nfs-ganesha.check in +# this directory. Symlink to any other check files from +# nfs-checks.d that should still be used, such as +# 00.portmapper.check. Set CTDB_NFS_CHECKS_DIR to point to this new +# directory of check files. +# +# * It is recommended, but not required, to install the grace_period +# script (usually shipped in a utils package for NFS-Ganesha) to +# /usr/bin/grace_period + +# I (Martin Schwenke) hereby relicense all of my contributions to this +# callout (and, previously, to 60.ganesha) to a license compatible +# with NFS Ganesha (right now this is LGPLv3, but I'm flexible). +# There may be other contributions to be considered for relicensing, +# particularly those in commit 28cbe527d47822f870e8252495ab2a1c8fddd12f. + +###################################################################### + +# Exit on 1st error +set -e + +# Filesystem type and mount point for the (typically clustered) +# volume that will contain the NFS-Ganesha state. +state_fs="${CTDB_NFS_STATE_FS_TYPE:-gpfs}" +state_dir="${CTDB_NFS_STATE_MNT}" # No sane default. + +# To change the following, edit the default values below. Do not set +# these - they aren't configuration variables, just hooks for testing. +nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/etc/ganesha/ganesha.conf}" +nfs_service="${CTDB_NFS_SERVICE:-nfs-ganesha}" +ganesha_rec_subdir=${CTDB_GANESHA_REC_SUBDIR:-.ganesha} +procfs=${PROCFS_PATH:-/proc} + +case "$state_fs" in +gpfs) + GANRECDIR="/var/lib/nfs/ganesha" + ;; +glusterfs) + if [ -z "${state_dir}" ]; then + echo "CTDB_NFS_STATE_MNT not defined for GlusterFS" + exit 1 + fi + host=$(hostname) + NODESTATEDIR="$state_dir/nfs-ganesha/$host" + GANSTATEDIR="$state_dir/nfs-ganesha/.noderefs" + NODESTATELN="$GANSTATEDIR/$host" + ;; +esac + + +################################################## + +usage () +{ + _c=$(basename "$0") + cat < +check_ln () +{ + if [ ! -L "${2}" ] ; then + rm -vrf "${2}" + else + _t=$(readlink "${2}") + if [ "$_t" != "${1}" ] ; then + rm -v "${2}" + fi + fi + # This is not an "else". It also re-creates the link if it was + # removed above! + if [ ! -e "${2}" ]; then + ln -sfv "${1}" "${2}" + fi +} + +# Return 'active' if the shared filesystem is accessible. +get_cluster_fs_state () +{ + case $state_fs in + gpfs) + /usr/lpp/mmfs/bin/mmgetstate | awk 'NR == 4 { print $3 }' + ;; + glusterfs) + # Since we're past create_ganesha_recdirs(), we're active. + echo "active" + ;; + *) + echo "File system $state_fs not supported" + exit 1 + ;; + esac +} + +create_ganesha_recdirs () +{ + if ! _mounts=$(mount | grep "$state_fs"); then + echo "Failed to find mounts of type $state_fs" + exit 1 + fi + if [ -z "$_mounts" ]; then + echo "startup $state_fs not ready" + exit 0 + fi + + case $state_fs in + gpfs) + _mntpt=$(echo "$_mounts" | sort | awk 'NR == 1 {print $3}') + _link_dst="${_mntpt}/${ganesha_rec_subdir}" + mkdir -vp "$_link_dst" + check_ln "$_link_dst" "$GANRECDIR" + ;; + glusterfs) + [ -d /var/lib/nfs.backup ] || \ + mv /var/lib/nfs /var/lib/nfs.backup + check_ln "$NODESTATEDIR" /var/lib/nfs + + mkdir -p "${NODESTATEDIR}/ganesha/v4recov" + mkdir -p "${NODESTATEDIR}/ganesha/v4old" + mkdir -p "${NODESTATEDIR}/statd/sm" + mkdir -p "${NODESTATEDIR}/statd/sm.bak" + touch "${NODESTATEDIR}/state" + touch "${NODESTATEDIR}/statd/state" + + mkdir -p "$GANSTATEDIR" + check_ln "$NODESTATEDIR" "$NODESTATELN" + for _dir in "${GANSTATEDIR}/"* ; do + # Handle no directories case + if [ ! -d "$_dir" ] ; then + break + fi + + _node="${_dir##*/}" # basename + if [ "${_node}" != "${host}" ]; then + check_ln "${GANSTATEDIR}/${_node}/ganesha" \ + "${NODESTATEDIR}/ganesha/${_node}" + check_ln "${GANSTATEDIR}/${_node}/statd" \ + "${NODESTATEDIR}/statd/${_node}" + fi + done + ;; + esac +} + +service_check () +{ + create_ganesha_recdirs + + # Always succeed if cluster filesystem is not active + _cluster_fs_state=$(get_cluster_fs_state) + if [ "$_cluster_fs_state" != "active" ] ; then + return 0 + fi + + # Check that NFS Ganesha is running, according to PID file + _pidfile="/var/run/ganesha.pid" + _ganesha="/usr/bin/ganesha.nfsd" + if ! { read -r _pid < "$_pidfile" && \ + grep "$_ganesha" "${procfs}/${_pid}/cmdline" ; } >/dev/null 2>&1 + then + + echo "ERROR: NFS Ganesha not running according to PID file" + return 1 + fi + + return 0 +} + +#------------------------------------------------- + +nfs_releaseip () +{ + if [ -x "/usr/bin/grace_period" ]; then + /usr/bin/grace_period "2:${2}" + else + dbus-send --print-reply --system --dest=org.ganesha.nfsd \ + /org/ganesha/nfsd/admin org.ganesha.nfsd.admin.grace \ + string:"2:${2}" + fi +} + +nfs_takeip () +{ + case $state_fs in + glusterfs) + check_ln "$NODESTATEDIR" "${GANSTATEDIR}/${2}" + ;; + esac + if [ -x "/usr/bin/grace_period" ]; then + /usr/bin/grace_period "5:${2}" + else + dbus-send --print-reply --system --dest=org.ganesha.nfsd \ + /org/ganesha/nfsd/admin org.ganesha.nfsd.admin.grace \ + string:"5:${2}" + fi +} + +################################################## +# service init startup and final shutdown + +nfs_shutdown () +{ + basic_stop "nfs" +} + +nfs_startup () +{ + basic_stop "nfs" || true + + create_ganesha_recdirs + + basic_start "nfs" + _f="${procfs}/sys/net/ipv4/tcp_tw_recycle" + if [ -f "$_f" ] ; then + echo 1 >"$_f" + fi +} + +################################################## +# list share directories + +nfs_monitor_list_shares () +{ + grep Path "$nfs_exports_file" | + cut -f2 -d\" | + sort -u +} + +################################################## + +nfs_register () +{ + cat < + + + + + ltdbtool + 1 + ctdb + CTDB - clustered TDB database + + + + ltdbtool + manipulate CTDB's local TDB files + + + + + ltdbtool + OPTION + COMMAND + COMMAND-ARGS + + + + + DESCRIPTION + + + ltdbtool is a utility to manipulate CTDB's local TDB databases + (LTDBs) without connecting to a CTDB daemon. + + + + It can be used to: + + + + + + dump the contents of a LTDB, optionally printing the CTDB + record header information, + + + + + convert between an LTDB and a non-clustered tdb + by adding or removing CTDB headers and + + + + convert between 64 and 32 bit LTDBs where the CTDB record + headers differ by 4 bytes of padding. + + + + + + + OPTIONS + + + -e + + + Dump empty records. These are normally excluded. + + + + + -p + + + Dump with header information, similar to "ctdb catdb". + + + + + + + -s + + 0 + 32 + 64 + + + + + Specify how to determine the CTDB record header size + for the input database: + + 0 + + no CTDB header + + + 32 + + CTDB header size of a 32 bit system (20 bytes) + + + 64 + + CTDB header size of a 64 bit system (24 bytes) + + + + The default is 32 or 64 depending on the system architecture. + + + + + + + -o + + 0 + 32 + 64 + + + + + Specify how to determine the CTDB record header size + for the output database, see -s. + + + + + -S SIZE + + + Explicitly specify the CTDB record header SIZE of the + input database in bytes. + + + + + -O SIZE + + + Explicitly specify the CTDB record header SIZE for the + output database in bytes. + + + + + -h + + + Print help text. + + + + + + + + COMMANDS + + + help + + + Print help text. + + + + + dump IDB + + + Dump the contents of an LTDB input file IDB to standard + output in a human-readable format. + + + + + + + convert IDB ODB + + + + Copy an LTDB input file IDB to output file ODB, optionally + adding or removing CTDB headers. + + + + + + + + EXAMPLES + + + Print a local tdb in "tdbdump" style: + + + ltdbtool dump idmap2.tdb.0 + + + + Print a local tdb with header information similar to "ctdb catdb": + + + ltdbtool dump -p idmap2.tdb.0 + + + + Strip the CTDB headers from records: + + + ltdbtool convert -o0 idmap2.tdb.0 idmap.tdb + + + + Strip 64 bit CTDB headers from records, running on i386: + + + ltdbtool convert -s64 -o0 idmap2.tdb.0 idmap.tdb + + + + Strip the CTDB headers from records by piping through tdbrestore: + + + ltdbtool dump idmap2.tdb.0 | tdbrestore idmap.tdb + + + + Convert a local tdb from a 64 bit system for usage on a 32 bit system: + + + ltdbtool convert -s64 -o32 idmap2.tdb.0 idmap2.tdb.1 + + + + Add a default header: + + + ltdbtool convert -s0 idmap.tdb idmap2.tdb.0 + + + + SEE ALSO + + ctdb + 1, + + tdbdump + 1, + + tdbrestore + 1, + + ctdb + 7, + + + + + + + + + This documentation was written by Gregor Beck + + + + + 2011 + Gregor Beck + Michael Adam + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/onnode.1.xml b/ctdb/doc/onnode.1.xml new file mode 100644 index 0000000..f7da04a --- /dev/null +++ b/ctdb/doc/onnode.1.xml @@ -0,0 +1,315 @@ + + + + + + onnode + 1 + ctdb + CTDB - clustered TDB database + + + + onnode + run commands on CTDB cluster nodes + + + + + onnode + OPTION + NODES + COMMAND + + + + + DESCRIPTION + + onnode is a utility to run commands on a specific node of a CTDB + cluster, or on all nodes. + + + NODES specifies which node(s) to run + a command on. See section NODES + SPECIFICATION for details. + + + COMMAND can be any shell command. The + onnode utility uses ssh or rsh to connect to the remote nodes + and run the command. + + + + + OPTIONS + + + -c + + + Execute COMMAND in the current working directory on the + specified nodes. + + + + + -f FILENAME + + + Specify an alternative nodes FILENAME to use instead of + the default. See the discussion of + /usr/local/etc/ctdb/nodes in the + FILES section for more details. + + + + + -i + + + Keep standard input open, allowing data to be piped to + onnode. Normally onnode closes stdin to avoid surprises + when scripting. Note that this option is ignored when + using or if ONNODE_SSH + is set to anything other than "ssh". + + + + + -n + + + Allow nodes to be specified by name rather than node + numbers. These nodes don't need to be listed in the nodes + file. You can avoid the nodes file entirely by combining + this with -f /dev/null. + + + + + -p + + + Run COMMAND in parallel on the specified nodes. The + default is to run COMMAND sequentially on each node. + + + + + -P + + + Push files to nodes. Names of files to push are specified + rather than the usual command. Quoting is fragile/broken + - filenames with whitespace in them are not supported. + + + + + -q + + + Do not print node addresses. Normally, onnode prints + informational node addresses if more than one node is + specified. This overrides -v. + + + + + -v + + + Print node addresses even if only one node is specified. + Normally, onnode prints informational node addresses when + more than one node is specified. + + + + + -h, --help + + + Show a short usage guide. + + + + + + + + NODES SPECIFICATION + + + Nodes can be specified via numeric node numbers (from 0 to N-1) + or mnemonics. Multiple nodes are specified using lists of + nodes, separated by commas, and ranges of numeric node numbers, + separated by dashes. If nodes are specified multiple times then + the command will be executed multiple times on those nodes. The + order of nodes is significant. + + + + The following mnemonics are available: + + + + all + + + All nodes. + + + + any + + + A node where ctdbd is running. This semi-random but + there is a bias towards choosing a low numbered node. + + + + ok | healthy + + + All nodes that are not disconnected, banned, disabled or + unhealthy. + + + + con | connected + + + All nodes that are not disconnected. + + + + + + + + EXAMPLES + + + The following command would show the process ID of ctdbd on all nodes + + + onnode all ctdb getpid + + + + The following command would show the last 5 lines of log on each + node, preceded by the node's hostname + + + onnode all "hostname; tail -5 /usr/local/var/log/log.ctdb" + + + + The following command would restart the ctdb service on all + nodes, in parallel. + + + onnode -p all service ctdb restart + + + + The following command would run ./foo in the current working + directory, in parallel, on nodes 0, 2, 3 and 4. + + + onnode -c -p 0,2-4 ./foo + + + + + FILES + + + /usr/local/etc/ctdb/nodes + + + Default file containing a list of each node's IP address + or hostname. + + + As above, a file specified via the + is given precedence. If a + relative path is specified and no corresponding file + exists relative to the current directory then the file is + also searched for in the CTDB configuration directory. + + + Otherwise the default is + /usr/local/etc/ctdb/nodes. + + + + + + /usr/local/etc/ctdb/onnode.conf + + + If this file exists it is sourced by onnode. The main + purpose is to allow the administrator to set + ONNODE_SSH to something other than "ssh". + In this case the -t option is ignored. + + + + + + + + SEE ALSO + + + ctdb + 7, + + + + + + + + + This documentation was written by + Andrew Tridgell, + Martin Schwenke + + + + + 2007 + Andrew Tridgell + Ronnie Sahlberg + + + 2008 + Martin Schwenke + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/ping_pong.1.xml b/ctdb/doc/ping_pong.1.xml new file mode 100644 index 0000000..47e90e8 --- /dev/null +++ b/ctdb/doc/ping_pong.1.xml @@ -0,0 +1,164 @@ + + + + + + ping_pong + 1 + ctdb + CTDB - clustered TDB database + + + + ping_pong + measures the ping-pong byte range lock latency + + + + + ping_pong + + -r + -w + -rw + + -m + -c + FILENAME + NUM-LOCKS + + + + + DESCRIPTION + + ping_pong measures the byte range lock latency. It is especially + useful on a cluster of nodes sharing a common lock manager as it + will give some indication of the lock manager's performance + under stress. + + + + FILENAME is a file on shared storage to use for byte range + locking tests. + + + + NUM-LOCKS is the number of byte range locks, so needs to be + (strictly) greater than the number of nodes in the cluster. + + + + + OPTIONS + + + + -r + + + test read performance + + + + + + -w + + + test write performance + + + + + + -m + + + use mmap + + + + + + -c + + + validate the locks + + + + + + + + + EXAMPLES + + Testing lock coherence + + + ping_pong test.dat N + + + + Testing lock coherence with lock validation + + + ping_pong -c test.dat N + + + + Testing IO coherence + + + ping_pong -rw test.dat N + + + + + SEE ALSO + + ctdb + 7, + + + + + + + + + + This documentation was written by Mathieu Parent + + + + + 2002 + Andrew Tridgell + + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more details. + + + You should have received a copy of the GNU General Public + License along with this program; if not, see + . + + + + + diff --git a/ctdb/doc/readonlyrecords.txt b/ctdb/doc/readonlyrecords.txt new file mode 100644 index 0000000..cc6d7ee --- /dev/null +++ b/ctdb/doc/readonlyrecords.txt @@ -0,0 +1,343 @@ +Read-Only locks in CTDB +======================= + +Problem +======= +CTDB currently only supports exclusive Read-Write locks for clients(samba) accessing the +TDB databases. +This mostly works well but when very many clients are accessing the same file, +at the same time, this causes the exclusive lock as well as the record itself to +rapidly bounce between nodes and acts as a scalability limitation. + +This primarily affects locking.tdb and brlock.tdb, two databases where record access is +read-mostly and where writes are semi-rare. + +For the common case, if CTDB provided shared non-exclusive Read-Only lock semantics +this would greatly improve scaling for these workloads. + + +Desired properties +================== +We can not make backward incompatible changes the ctdb_ltdb header for the records. + +A Read-Only lock enabled ctdb demon must be able to interoperate with a non-Read-Only +lock enbled daemon. + +Getting a Read-Only lock should not be slower than getting a Read-Write lock. + +When revoking Read-Only locks for a record, this should involve only those nodes that +currently hold a Read-Only lock and should avoid broadcasting opportunistic revocations. +(must track which nodes are delegated to) + +When a Read-Write lock is requested, if there are Read-Only locks delegated to other +nodes, the DMASTER will defer the record migration until all read-only locks are first +revoked (synchronous revoke). + +Due to the cost of revoking Read-Only locks has on getting a Read-Write lock, the +implementation should try to avoid creating Read-Only locks unless it has indication +that there is contention. This may mean that even if client requests a Read-Only lock +we might still provide a full Read-Write lock in order to avoid the cost of revoking +the locks in some cases. + +Read-Only locks require additional state to be stored in a separate database, containing +information about which nodes have have been delegated Read-Only locks. +This database should be kept at minimal size. + +Read-Only locks should not significantly complicate the normal record +create/migration/deletion cycle for normal records. + +Read-Only locks should not complicate the recovery process. + +Read-Only locks should not complicate the vacuuming process. + +We should avoid forking new child processes as far as possible from the main daemon. + +Client-side implementation, samba, libctdb, others, should have minimal impact when +Read-Only locks are implemented. +Client-side implementation must be possible with only minor conditionals added to the +existing lock-check-fetch-unlock loop that clients use today for Read-Write locks. So +that clients only need one single loop that can handle both Read-Write locking as well +as Read-Only locking. Clients should not need two nearly identical loops. + + +Implementation +============== + +Four new flags are allocated in the ctdb_ltdb record header. +HAVE_DELEGATIONS, HAVE_READONLY_LOCK, REVOKING_READONLY and REVOKE_COMPLETE + +HAVE_DELEGATIONS is a flag that can only be set on the node that is currently the +DMASTER for the record. When set, this flag indicates that there are Read-Only locks +delegated to other nodes in the cluster for this record. + +HAVE_READONLY is a flag that is only set on nodes that are NOT the DMASTER for the +record. If set this flag indicates that this record contains an up-to-date Read-Only +version of this record. A client that only needs to read, but not to write, the record +can safely use the content of this record as is regardless of the value of the DMASTER +field of the record. + +REVOKING_READONLY is a flag that is used while a set of read only delegations are being +revoked. +This flag is only set when HAVE_DELEGATIONS is also set, and is cleared at the same time +as HAVE_DELEGATIONS is cleared. +Normal operations is that first the HAVE_DELEGATIONS flag is set when the first +delegation is generated. When the delegations are about to be revoked, the +REVOKING_READONLY flag is set too. +Once all delegations are revoked, both flags are cleared at the same time. +While REVOKING_READONLY is set, any requests for the record, either normal request or +request for readonly will be deferred. +Deferred requests are linked on a list for deferred requests until the time that the +revokation is completed. +This flags is set by the main ctdb daemon when it starts revoking this record. + +REVOKE_COMPLETE +The actual revoke of records is done by a child process, spawned from the main ctdb +daemon when it starts the process to revoke the records. +Once the child process has finished revoking all delegations it will set the flag +REVOKE_COMPLETE for this record to signal to the main daemon that the record has been +successfully revoked. +At this stage the child process will also trigger an event in the main daemon that +revoke is complete and that the main daemon should start re-processing all deferred +requests. + + + +Once the revoke process is completed there will be at least one deferred request to +access this record. That is the initical call to for an exclusive fetch_lock() that +triggered the revoke process to be started. +In addition to this deferred request there may also be additional requests that have +also become deferred while the revoke was in process. These can be either exclusive +fetch_locks() or they can be readonly lock requests. +Once the revoke is completed the main daemon will reprocess all exclusive fetch_lock() +requests immediately and respond to these clients. +Any requests for readadonly lock requests will be deferred for an additional period of +time before they are re-processed. +This is to allow the client that needs a fetch_lock() to update the record to get some +time to access and work on the record without having to compete with the possibly +very many readonly requests. + + + + + +The ctdb_db structure is expanded so that it contains one extra TDB database for each +normal, non-persistent datbase. +This new database is used for tracking delegations for the records. +A record in the normal database that has "HAVE_DELEGATION" set will always have a +corresponding record at the same key. This record contains the set of all nodes that +the record is delegated to. +This tracking database is lockless, using TDB_NOLOCK, and is only ever accessed by +the main ctdbd daemon. +The lockless nature and the fact that no other process ever access this TDB means we +are guaranteed non-blocking access to records in the tracking database. + +The ctdb_call PDU is allocated with a new flag WANT_READONLY and possibly also a new +callid: CTDB_FETCH_WITH_HEADER_FUNC. +This new function returns not only the record, as CTDB_FETCH_FUNC does, but also +returns the full ctdb_ltdb record HEADER prepended to the record. +This function is optional, clients that do not care what the header is can continue +using just CTDB_FETCH_FUNC + + +This flag is used to requesting a read-only record from the DMASTER/LMASTER. +If the record does not yet exist, this is a returned as an error to the client and the +client will retry the request loop. + +A new control is added to make remote nodes remove the HAVE_READONLY_LOCK from a record +and to invalidate any deferred readonly copies from the databases. + + + +Client implementation +===================== +Clients today use a loop for record fetch lock that looks like this + try_again: + lock record in tdb + + if record does not exist in tdb, + unlock record + ask ctdb to migrate record onto the node + goto try_again + + if record dmaster != this node pnn + unlock record + ask ctdb to migrate record onto the node + goto try_again + + finished: + +where we basically spin, until the record is migrated onto the node and we have managed +to pin it down. + +This will change to instead to something like + + try_again: + lock record in tdb + + if record does not exist in tdb, + unlock record + ask ctdb to migrate record onto the node + goto try_again + + if record dmaster == current node pnn + goto finished + + if read-only lock + if HAVE_READONLY_LOCK or HAVE_DELEGATIONS is set + goto finished + else + unlock record + ask ctdb for read-only copy (WANT_READONLY[|WITH_HEADER]) + if failed to get read-only copy (*A) + ask ctdb to migrate the record onto the node + goto try_again + lock record in tdb + goto finished + + unlock record + ask ctdb to migrate record onto the node + goto try_again + + finished: + +If the record does not yet exist in the local TDB, we always perform a full fetch for a +Read-Write lock even if only a Read-Only lock was requested. +This means that for first access we always grab a Read-Write lock and thus upgrade any +requests for Read-Only locks into a Read-Write request. +This creates the record, migrates it onto the node and makes the local node become +the DMASTER for the record. + +Future reference to this same record by the local samba daemons will still access/lock +the record locally without triggereing a Read-Only delegation to be created since the +record is already hosted on the local node as DMASTER. + +Only if the record is contended, i.e. it has been created an migrated onto the node but +we are no longer the DMASTER for this record, only for this case will we create a +Read-Only delegation. +This heuristics provide a mechanism where we will not create Read-Only delegations until +we have some indication that the record may be contended. + +This avoids creating and revoking Read-Only delegations when only a single client is +repeatedly accessing the same set of records. +This also aims to limit the size of the tracking tdb. + + +Server implementation +===================== +When receiving a ctdb_call with the WANT_READONLY flag: + +If this is the LMASTER for the record and the record does not yet exist, LMASTER will +return an error back to the client (*A above) and the client will try to recover. +In particular, LMASTER will not create a new record for this case. + +If this is the LMASTER for the record and the record exists, the PDU will be forwarded to +the DMASTER for the record. + +If this node is not the DMASTER for this record, we forward the PDU back to the +LMASTER. Just as we always do today. + +If this is the DMASTER for the record, we need to create a Read-Only delegation. +This is done by + lock record + increase the RSN by one for this record + set the HAVE_DELEGATIONS flag for the record + write the updated record to the TDB + create/update the tracking TDB nd add this new node to the set of delegations + send a modified copy of the record back to the requesting client. + modifications are that RSN is decremented by one, so delegated records are "older" than on the DMASTER, + it has HAVE_DELEGATIONS flag stripped off, and has HAVE_READONLY_LOCK added. + unlock record + +Important to note is that this does not trigger a record migration. + + +When receiving a ctdb_call without the WANT_READONLY flag: + +If this is the DMASTER for the this might trigger a migration. If there exists +delegations we must first revoke these before allowing the Read-Write request from +proceeding. So, +IF the record has HAVE_DELEGATIONS set, we create a child process and defer processing +of this PDU until the child process has completed. + +From the child process we will call out to all nodes that have delegations for this +record and tell them to invalidate this record by clearing the HAVE_READONLY_LOCK from +the record. +Once all delegated nodes respond back, the child process signals back to the main daemon +the revoke has completed. (child process may not access the tracking tdb since it is +lockless) + +Main process is triggered to re-process the PDU once the child process has finished. +Main daemon deletes the corresponding record in the tracking database, clears the +HAVE_DELEGATIONS flag for the record and then proceeds to perform the migration as usual. + +When receiving a ctdb_call without the flag we want all delegations to be revoked, +so we must take care that the delegations are revoked unconditionally before we even +check if we are already the DMASTER (in which case the ctdb_call would normally just +be no-op (*B below)) + + + +Recovery process changes +======================== +A recovery implicitly clears/revokes any read only records and delegations from all +databases. + +During delegations of Read-Only locks, this is done in such way that delegated records +will have a RSN smaller than the DMASTER. This guarantees that read-only copies always +have a RSN that is smaller than the DMASTER. + +During recoveries we do not need to take any special action other than always picking +the copy of the record that has the highest RSN, which is what we already do today. + +During the recovery process, we strip all flags off all records while writing the new +content of the database during the PUSH_DB control. + +During processing of the PUSH_DB control and once the new database has been written we +then also wipe the tracking database. + +This makes changes to the recovery process minimal and nonintrusive. + + + +Vacuuming process +================= +Vacuuming needs only minimal changes. + + +When vacuuming runs, it will do a fetch_lock to migrate any remote records back onto the +LMASTER before the record can be purged. This will automatically force all delegations +for that record to be revoked before the migration is copied back onto the LMASTER. +This handles the case where LMASTER is not the DMASTER for the record that will be +purged. +The migration in this case does force any delegations to be revoked before the +vacuuming takes place. + +Missing is the case when delegations exist and the LMASTER is also the DMASTER. +For this case we need to change the vacuuming to unconditionally always try to do a +fetch_lock when HAVE_DELEGATIONS is set, even if the record is already stored locally. +(*B) +This fetch lock will not cause any migrations by the ctdb daemon, but since it does +not have the WANT_READONLY this will still force the delegations to be revoked but no +migration will trigger. + + +Traversal process +================= +Traversal process is changed to ignore any records with the HAVE_READONLY_LOCK + + +Forward/Backward Compatibility +============================== +Non-readonly locking daemons must be able to interoperate with readonly locking enabled daemons. + +Non-readonly enabled daemons fetching records from Readonly enabled daemons: +Non-readonly enabled daemons do not know, and never set the WANT_READONLY flag so these daemons will always request a full migration for a full fetch-lock for all records. Thus a request from a non-readonly enabled daemon will always cause any existing delegations to be immediately revoked. Access will work but performance may be harmed since there will be a lot of revoking of delegations. + +Readonly enabled daemons fetching records with WANT_READONLY from non-readonly enabled daemons: +Non-readonly enabled daemons ingore the WANT_READONLY flag and never return delegations. They always return a full record migration. +Full record migration is allowed by the protocol, even if the originator only requests the 'hint' WANT_READONLY, +so this access also interoperates between daemons with different capabilities. + + + + diff --git a/ctdb/event/event.h b/ctdb/event/event.h new file mode 100644 index 0000000..13617c0 --- /dev/null +++ b/ctdb/event/event.h @@ -0,0 +1,55 @@ +/* + CTDB event daemon client + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_EVENT_H__ +#define __CTDB_EVENT_H__ + +#include "event/event_protocol.h" + +struct ctdb_event_context; + +int ctdb_event_init(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context **result); + +struct tevent_req *ctdb_event_run_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request_run *run); +bool ctdb_event_run_recv(struct tevent_req *req, int *perr, int *result); + +struct tevent_req *ctdb_event_status_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request_status *status); +bool ctdb_event_status_recv(struct tevent_req *req, + int *perr, + int *result, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply_status **status); + +struct tevent_req *ctdb_event_script_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request_script *script); +bool ctdb_event_script_recv(struct tevent_req *req, int *perr, int *result); + +#endif /* __CTDB_EVENT_H__ */ diff --git a/ctdb/event/event_client.c b/ctdb/event/event_client.c new file mode 100644 index 0000000..d0bee0d --- /dev/null +++ b/ctdb/event/event_client.c @@ -0,0 +1,351 @@ +/* + CTDB event daemon client + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include +#include + +#include "lib/util/debug.h" +#include "lib/util/tevent_unix.h" + +#include "common/logging.h" +#include "common/path.h" +#include "common/sock_client.h" + +#include "event/event_protocol_api.h" +#include "event/event.h" + +struct ctdb_event_context { + char *socket; + struct sock_client_context *sockc; +}; + +struct event_request { + struct ctdb_event_header header; + struct ctdb_event_request *request; +}; + +struct event_reply { + struct ctdb_event_header header; + struct ctdb_event_reply *reply; +}; + +static int event_request_push(void *request_data, + uint32_t reqid, + TALLOC_CTX *mem_ctx, + uint8_t **buf, + size_t *buflen, + void *private_data) +{ + struct event_request *r = (struct event_request *)request_data; + int ret; + + r->header.reqid = reqid; + + *buflen = ctdb_event_request_len(&r->header, r->request); + *buf = talloc_size(mem_ctx, *buflen); + if (*buf == NULL) { + return ENOMEM; + } + + ret = ctdb_event_request_push(&r->header, r->request, *buf, buflen); + if (ret != 0) { + talloc_free(*buf); + return ret; + } + + return 0; +} + +static int event_reply_pull(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + void **reply_data, + void *private_data) +{ + struct event_reply *r; + int ret; + + r = talloc_zero(mem_ctx, struct event_reply); + if (r == NULL) { + return ENOMEM; + } + + ret = ctdb_event_reply_pull(buf, buflen, &r->header, r, &r->reply); + if (ret != 0) { + talloc_free(r); + return ret; + } + + *reply_data = r; + + return 0; +} + +static int event_reply_reqid(uint8_t *buf, + size_t buflen, + uint32_t *reqid, + void *private_data) +{ + struct ctdb_event_header header; + int ret; + + ret = ctdb_event_header_extract(buf, buflen, &header); + if (ret != 0) { + return ret; + } + + *reqid = header.reqid; + return 0; +} + +struct sock_client_proto_funcs event_proto_funcs = { + .request_push = event_request_push, + .reply_pull = event_reply_pull, + .reply_reqid = event_reply_reqid, +}; + +int ctdb_event_init(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context **result) +{ + struct ctdb_event_context *eclient; + int ret; + + eclient = talloc_zero(mem_ctx, struct ctdb_event_context); + if (eclient == NULL) { + return ENOMEM; + } + + eclient->socket = path_socket(eclient, "eventd"); + if (eclient->socket == NULL) { + talloc_free(eclient); + return ENOMEM; + } + + ret = sock_client_setup(eclient, + ev, + eclient->socket, + &event_proto_funcs, + eclient, + &eclient->sockc); + if (ret != 0) { + talloc_free(eclient); + return ret; + } + + *result = eclient; + return 0; +} + +/* + * Handle request and reply + */ + +struct ctdb_event_msg_state { + struct event_request e_request; + struct event_reply *e_reply; +}; + +static void ctdb_event_msg_done(struct tevent_req *subreq); + +static struct tevent_req *ctdb_event_msg_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request *request) +{ + struct tevent_req *req, *subreq; + struct ctdb_event_msg_state *state; + + req = tevent_req_create(mem_ctx, &state, struct ctdb_event_msg_state); + if (req == NULL) { + return NULL; + } + + state->e_request.request = request; + + subreq = sock_client_msg_send(mem_ctx, + ev, + eclient->sockc, + tevent_timeval_zero(), + &state->e_request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, ctdb_event_msg_done, req); + + return req; +} + +static void ctdb_event_msg_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct ctdb_event_msg_state *state = tevent_req_data( + req, struct ctdb_event_msg_state); + int ret = 0; + bool ok; + + ok = sock_client_msg_recv(subreq, &ret, state, &state->e_reply); + TALLOC_FREE(subreq); + if (!ok) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +static bool ctdb_event_msg_recv(struct tevent_req *req, + int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **reply) +{ + struct ctdb_event_msg_state *state = tevent_req_data( + req, struct ctdb_event_msg_state); + int ret = 0; + + if (tevent_req_is_unix_error(req, &ret)) { + if (perr != NULL) { + *perr = ret; + } + return false; + } + + *reply = talloc_steal(mem_ctx, state->e_reply->reply); + + return true; +} + +/* + * API functions + */ + +struct tevent_req *ctdb_event_run_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request_run *run) +{ + struct ctdb_event_request request; + + request.cmd = CTDB_EVENT_CMD_RUN; + request.data.run = run; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_run_recv(struct tevent_req *req, int *perr, int *result) +{ + struct ctdb_event_reply *reply = NULL; + bool ok; + + ok = ctdb_event_msg_recv(req, perr, req, &reply); + if (!ok) { + return false; + } + + if (reply->cmd != CTDB_EVENT_CMD_RUN) { + *result = EPROTO; + } else { + *result = reply->result; + } + + talloc_free(reply); + return true; +} + +struct tevent_req *ctdb_event_status_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request_status *status) +{ + struct ctdb_event_request request; + + request.cmd = CTDB_EVENT_CMD_STATUS; + request.data.status = status; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_status_recv(struct tevent_req *req, + int *perr, + int *result, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply_status **status) +{ + struct ctdb_event_reply *reply = NULL; + bool ok; + + ok = ctdb_event_msg_recv(req, perr, req, &reply); + if (!ok) { + return false; + } + + if (reply->cmd != CTDB_EVENT_CMD_STATUS) { + *result = EPROTO; + } else { + *result = reply->result; + } + + if (reply->result == 0) { + *status = talloc_steal(mem_ctx, reply->data.status); + } else { + *status = NULL; + } + + talloc_free(reply); + return true; +} + +struct tevent_req *ctdb_event_script_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct ctdb_event_context *eclient, + struct ctdb_event_request_script *script) +{ + struct ctdb_event_request request; + + request.cmd = CTDB_EVENT_CMD_SCRIPT; + request.data.script = script; + + return ctdb_event_msg_send(mem_ctx, ev, eclient, &request); +} + +bool ctdb_event_script_recv(struct tevent_req *req, int *perr, int *result) +{ + struct ctdb_event_reply *reply = NULL; + bool ok; + + ok = ctdb_event_msg_recv(req, perr, req, &reply); + if (!ok) { + return false; + } + + if (reply->cmd != CTDB_EVENT_CMD_SCRIPT) { + *result = EPROTO; + } else { + *result = reply->result; + } + + talloc_free(reply); + return true; +} diff --git a/ctdb/event/event_cmd.c b/ctdb/event/event_cmd.c new file mode 100644 index 0000000..db39e4d --- /dev/null +++ b/ctdb/event/event_cmd.c @@ -0,0 +1,358 @@ +/* + CTDB event daemon - command handling + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include +#include + +#include "lib/util/debug.h" +#include "lib/util/tevent_unix.h" + +#include "common/logging.h" + +#include "event/event_private.h" + +struct event_cmd_state { + struct event_context *eventd; + struct ctdb_event_request *request; + struct ctdb_event_reply *reply; +}; + +/* + * CTDB_EVENT_CMD_RUN + */ + +static void event_cmd_run_done(struct tevent_req *subreq); + +static struct tevent_req *event_cmd_run_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + struct ctdb_event_request *request, + struct ctdb_event_reply *reply) +{ + struct tevent_req *req, *subreq; + struct event_cmd_state *state; + struct run_event_context *run_ctx; + struct ctdb_event_request_run *rdata; + int ret; + bool continue_on_failure = false; + + req = tevent_req_create(mem_ctx, &state, struct event_cmd_state); + if (req == NULL) { + return NULL; + } + + state->eventd = eventd; + state->request = request; + state->reply = reply; + + rdata = request->data.run; + + ret = eventd_run_ctx(eventd, rdata->component, &run_ctx); + if (ret != 0) { + state->reply->result = ret; + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + if (rdata->flags & CTDB_EVENT_RUN_ALL) { + continue_on_failure = true; + } + + subreq = run_event_send(state, + ev, + run_ctx, + rdata->event, + rdata->args, + tevent_timeval_current_ofs(rdata->timeout,0), + continue_on_failure); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, event_cmd_run_done, req); + + return req; +} + +static void event_cmd_run_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct event_cmd_state *state = tevent_req_data( + req, struct event_cmd_state); + struct run_event_script_list *script_list = NULL; + struct ctdb_event_request_run *rdata; + int ret; + bool ok; + + ok = run_event_recv(subreq, &ret, state, &script_list); + TALLOC_FREE(subreq); + if (!ok) { + state->reply->result = ret; + goto done; + } + + if (script_list == NULL) { + state->reply->result = EIO; + goto done; + } + + if (script_list->summary == -ECANCELED) { + state->reply->result = ECANCELED; + goto done; + } + + rdata = state->request->data.run; + ret = eventd_set_event_result(state->eventd, + rdata->component, + rdata->event, + script_list); + if (ret != 0) { + state->reply->result = ret; + goto done; + } + + if (script_list->summary == -ETIMEDOUT) { + state->reply->result = ETIMEDOUT; + } else if (script_list->summary != 0) { + state->reply->result = ENOEXEC; + } + +done: + tevent_req_done(req); +} + +/* + * CTDB_EVENT_CMD_STATUS + */ + +static struct tevent_req *event_cmd_status_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + struct ctdb_event_request *request, + struct ctdb_event_reply *reply) +{ + struct tevent_req *req; + struct event_cmd_state *state; + struct ctdb_event_request_run *rdata; + struct run_event_script_list *script_list; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct event_cmd_state); + if (req == NULL) { + return NULL; + } + + reply->data.status = talloc_zero(reply, + struct ctdb_event_reply_status); + if (tevent_req_nomem(reply->data.status, req)) { + reply->result = ENOMEM; + goto done; + } + + rdata = request->data.run; + + ret = eventd_get_event_result(eventd, + rdata->component, + rdata->event, + &script_list); + if (ret != 0) { + reply->result = ret; + goto done; + } + + reply->data.status->script_list = eventd_script_list(reply, + script_list); + if (reply->data.status->script_list == NULL) { + reply->result = ENOMEM; + goto done; + } + reply->data.status->summary = script_list->summary; + + reply->result = 0; + +done: + tevent_req_done(req); + return tevent_req_post(req, ev); +} + +/* + * CTDB_EVENT_CMD_SCRIPT + */ + +static struct tevent_req *event_cmd_script_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + struct ctdb_event_request *request, + struct ctdb_event_reply *reply) +{ + struct tevent_req *req; + struct event_cmd_state *state; + struct run_event_context *run_ctx; + struct ctdb_event_request_script *rdata; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct event_cmd_state); + if (req == NULL) { + return NULL; + } + + rdata = request->data.script; + + ret = eventd_run_ctx(eventd, rdata->component, &run_ctx); + if (ret != 0) { + reply->result = ret; + goto done; + } + + if (rdata->action == CTDB_EVENT_SCRIPT_DISABLE) { + ret = run_event_script_disable(run_ctx, rdata->script); + } else if (rdata->action == CTDB_EVENT_SCRIPT_ENABLE) { + ret = run_event_script_enable(run_ctx, rdata->script); + } else { + D_ERR("Invalid action specified\n"); + reply->result = EPROTO; + goto done; + } + + if (ret != 0) { + reply->result = ret; + goto done; + } + + reply->result = 0; + +done: + tevent_req_done(req); + return tevent_req_post(req, ev); +} + +static bool event_cmd_recv(struct tevent_req *req, int *perr) +{ + if (tevent_req_is_unix_error(req, perr)) { + return false; + } + + return true; +} + + +struct event_cmd_dispatch_state { + struct ctdb_event_reply *reply; +}; + +static void event_cmd_dispatch_done(struct tevent_req *subreq); + +struct tevent_req *event_cmd_dispatch_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + struct ctdb_event_request *request) +{ + struct tevent_req *req, *subreq; + struct event_cmd_dispatch_state *state; + + req = tevent_req_create(mem_ctx, + &state, + struct event_cmd_dispatch_state); + if (req == NULL) { + return NULL; + } + + state->reply = talloc_zero(state, struct ctdb_event_reply); + if (tevent_req_nomem(state->reply, req)) { + return tevent_req_post(req, ev); + } + + state->reply->cmd = request->cmd; + + switch (request->cmd) { + case CTDB_EVENT_CMD_RUN: + subreq = event_cmd_run_send(state, + ev, + eventd, + request, + state->reply); + break; + + case CTDB_EVENT_CMD_STATUS: + subreq = event_cmd_status_send(state, + ev, + eventd, + request, + state->reply); + break; + + case CTDB_EVENT_CMD_SCRIPT: + subreq = event_cmd_script_send(state, + ev, + eventd, + request, + state->reply); + break; + + default: + state->reply->result = EPROTO; + tevent_req_done(req); + return tevent_req_post(req, ev); + } + + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, event_cmd_dispatch_done, req); + + return req; +} + +static void event_cmd_dispatch_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + int ret; + bool ok; + + ok = event_cmd_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (!ok) { + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool event_cmd_dispatch_recv(struct tevent_req *req, + int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **reply) +{ + struct event_cmd_dispatch_state *state = tevent_req_data( + req, struct event_cmd_dispatch_state); + + if (tevent_req_is_unix_error(req, perr)) { + return false; + } + + *reply = talloc_steal(mem_ctx, state->reply); + return true; +} diff --git a/ctdb/event/event_conf.c b/ctdb/event/event_conf.c new file mode 100644 index 0000000..61ecf8e --- /dev/null +++ b/ctdb/event/event_conf.c @@ -0,0 +1,85 @@ +/* + CTDB event daemon + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/dir.h" + +#include "lib/util/debug.h" + +#include "common/conf.h" +#include "common/path.h" + +#include "event/event_conf.h" + +static bool event_conf_validate_debug_script(const char *key, + const char *old_script, + const char *new_script, + enum conf_update_mode mode) +{ + char script[PATH_MAX]; + char script_path[PATH_MAX]; + struct stat st; + size_t len; + int ret; + + len = strlcpy(script, new_script, sizeof(script)); + if (len >= sizeof(script)) { + D_ERR("debug script name too long\n"); + return false; + } + + ret = snprintf(script_path, + sizeof(script_path), + "%s/%s", + path_etcdir(), + basename(script)); + if (ret < 0 || (size_t)ret >= sizeof(script_path)) { + D_ERR("debug script path too long\n"); + return false; + } + + ret = stat(script_path, &st); + if (ret == -1) { + D_ERR("debug script %s does not exist\n", script_path); + return false; + } + + if (! S_ISREG(st.st_mode)) { + D_ERR("debug script %s is not a file\n", script_path); + return false; + } + if (! (st.st_mode & S_IXUSR)) { + D_ERR("debug script %s is not executable\n", script_path); + return false; + } + + return true; +} + +void event_conf_init(struct conf_context *conf) +{ + conf_define_section(conf, EVENT_CONF_SECTION, NULL); + + conf_define_string(conf, + EVENT_CONF_SECTION, + EVENT_CONF_DEBUG_SCRIPT, + NULL, + event_conf_validate_debug_script); +} diff --git a/ctdb/event/event_conf.h b/ctdb/event/event_conf.h new file mode 100644 index 0000000..964a18a --- /dev/null +++ b/ctdb/event/event_conf.h @@ -0,0 +1,31 @@ +/* + CTDB event daemon + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_EVENT_CONF_H__ +#define __CTDB_EVENT_CONF_H__ + +#include "common/conf.h" + +#define EVENT_CONF_SECTION "event" + +#define EVENT_CONF_DEBUG_SCRIPT "debug script" + +void event_conf_init(struct conf_context *conf); + +#endif /* __CTDB_EVENT_CONF_H__ */ diff --git a/ctdb/event/event_config.c b/ctdb/event/event_config.c new file mode 100644 index 0000000..d282622 --- /dev/null +++ b/ctdb/event/event_config.c @@ -0,0 +1,122 @@ +/* + CTDB event daemon - config handling + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "common/conf.h" +#include "common/logging_conf.h" +#include "common/path.h" + +#include "event/event_private.h" +#include "event/event_conf.h" + +struct event_config { + char *config_file; + struct conf_context *conf; + + const char *logging_location; + const char *logging_loglevel; + const char *debug_script; +}; + +int event_config_init(TALLOC_CTX *mem_ctx, struct event_config **result) +{ + struct event_config *config; + int ret; + bool ok; + + config = talloc_zero(mem_ctx, struct event_config); + if (config == NULL) { + return ENOMEM; + } + + config->config_file = path_config(config); + if (config->config_file == NULL) { + talloc_free(config); + return ENOMEM; + } + + ret = conf_init(config, &config->conf); + if (ret != 0) { + talloc_free(config); + return ret; + } + + logging_conf_init(config->conf, NULL); + + conf_assign_string_pointer(config->conf, + LOGGING_CONF_SECTION, + LOGGING_CONF_LOCATION, + &config->logging_location); + conf_assign_string_pointer(config->conf, + LOGGING_CONF_SECTION, + LOGGING_CONF_LOG_LEVEL, + &config->logging_loglevel); + + event_conf_init(config->conf); + + conf_assign_string_pointer(config->conf, + EVENT_CONF_SECTION, + EVENT_CONF_DEBUG_SCRIPT, + &config->debug_script); + + ok = conf_valid(config->conf); + if (!ok) { + talloc_free(config); + return EINVAL; + } + + ret = conf_load(config->conf, config->config_file, true); + if (ret != 0 && ret != ENOENT) { + talloc_free(config); + return ret; + } + + *result = config; + return 0; +} + +const char *event_config_log_location(struct event_config *config) +{ + return config->logging_location; +} + +const char *event_config_log_level(struct event_config *config) +{ + return config->logging_loglevel; +} + +const char *event_config_debug_script(struct event_config *config) +{ + return config->debug_script; +} + +int event_config_reload(struct event_config *config) +{ + int ret; + + ret = conf_reload(config->conf); + if (ret != 0 && ret != ENOENT) { + return ret; + } + + return 0; +} diff --git a/ctdb/event/event_context.c b/ctdb/event/event_context.c new file mode 100644 index 0000000..79bcd83 --- /dev/null +++ b/ctdb/event/event_context.c @@ -0,0 +1,472 @@ +/* + CTDB event daemon - daemon state + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/dir.h" + +#include +#include + +#include "lib/util/debug.h" +#include "lib/util/dlinklist.h" + +#include "common/logging.h" +#include "common/run_event.h" +#include "common/path.h" + +#include "event/event_private.h" + +struct event_event { + struct event_event *prev, *next; + + const char *name; + struct run_event_script_list *script_list; +}; + +struct event_component { + struct event_component *prev, *next; + + /* component state */ + const char *name; + const char *path; + struct run_event_context *run_ctx; + + /* events list */ + struct event_event *event; +}; + +struct event_client { + struct event_client *prev, *next; + + struct sock_client_context *client; +}; + +struct event_context { + struct tevent_context *ev; + struct event_config *config; + struct run_proc_context *run_proc_ctx; + + const char *script_dir; + const char *debug_script; + + /* component list */ + struct event_component *component; + + /* client list */ + struct event_client *client; +}; + +/* + * event_event functions + */ + +static struct event_event *eventd_event_find(struct event_component *comp, + const char *event_name) +{ + struct event_event *event; + + if (event_name == NULL) { + return NULL; + } + + for (event = comp->event; event != NULL; event = event->next) { + if (strcmp(event->name, event_name) == 0) { + return event; + } + } + + return NULL; +} + +static int eventd_event_add(struct event_component *comp, + const char *event_name, + struct event_event **result) +{ + struct event_event *event; + + if (event_name == NULL) { + return EINVAL; + } + + event = eventd_event_find(comp, event_name); + if (event != NULL) { + goto done; + } + + event = talloc_zero(comp, struct event_event); + if (event == NULL) { + return ENOMEM; + } + + event->name = talloc_strdup(event, event_name); + if (event->name == NULL) { + talloc_free(event); + return ENOMEM; + } + + DLIST_ADD_END(comp->event, event); + +done: + if (result != NULL) { + *result = event; + } + return 0; +} + +static int eventd_event_set(struct event_component *comp, + const char *event_name, + struct run_event_script_list *script_list) +{ + struct event_event *event = NULL; + int ret; + + ret = eventd_event_add(comp, event_name, &event); + if (ret != 0) { + return ret; + } + + TALLOC_FREE(event->script_list); + if (script_list != NULL) { + event->script_list = talloc_steal(event, script_list); + } + + return 0; +} + +static int eventd_event_get(struct event_component *comp, + const char *event_name, + struct run_event_script_list **result) +{ + struct event_event *event; + + event = eventd_event_find(comp, event_name); + if (event == NULL) { + return EINVAL; + } + + *result = event->script_list; + return 0; +} + +/* + * event_component functions + */ + +static struct event_component *eventd_component_find( + struct event_context *eventd, + const char *comp_name) +{ + struct event_component *comp; + + if (comp_name == NULL) { + return NULL; + } + + for (comp = eventd->component; comp != NULL; comp = comp->next) { + if (strcmp(comp->name, comp_name) == 0) { + return comp; + } + } + + return NULL; +} + +static int eventd_component_add(struct event_context *eventd, + const char *comp_name, + struct event_component **result) +{ + struct event_component *comp; + int ret; + + if (comp_name == NULL) { + return EINVAL; + } + + comp = eventd_component_find(eventd, comp_name); + if (comp != NULL) { + goto done; + } + + comp = talloc_zero(eventd, struct event_component); + if (comp == NULL) { + return ENOMEM; + } + + comp->name = talloc_strdup(comp, comp_name); + if (comp->name == NULL) { + talloc_free(comp); + return ENOMEM; + } + + comp->path = talloc_asprintf(comp, + "%s/%s", + eventd->script_dir, + comp_name); + if (comp->path == NULL) { + talloc_free(comp); + return ENOMEM; + } + + ret = run_event_init(eventd, + eventd->run_proc_ctx, + comp->path, + eventd->debug_script, + &comp->run_ctx); + if (ret != 0) { + talloc_free(comp); + return ret; + } + + DLIST_ADD_END(eventd->component, comp); + +done: + if (result != NULL) { + *result = comp; + } + return 0; +} + +/* + * event_client functions + */ + +static struct event_client *eventd_client_find( + struct event_context *eventd, + struct sock_client_context *client) +{ + struct event_client *e; + + for (e = eventd->client; e != NULL; e = e->next) { + if (e->client == client) { + return e; + } + } + + return NULL; +} + +int eventd_client_add(struct event_context *eventd, + struct sock_client_context *client) +{ + struct event_client *e; + + e = talloc_zero(eventd, struct event_client); + if (e == NULL) { + return ENOMEM; + } + + e->client = client; + + DLIST_ADD_END(eventd->client, e); + + return 0; +} + +void eventd_client_del(struct event_context *eventd, + struct sock_client_context *client) +{ + struct event_client *e; + + e = eventd_client_find(eventd, client); + if (e == NULL) { + return; + } + + DLIST_REMOVE(eventd->client, e); + + talloc_free(e); +} + +bool eventd_client_exists(struct event_context *eventd, + struct sock_client_context *client) +{ + struct event_client *e; + + e = eventd_client_find(eventd, client); + if (e == NULL) { + return false; + } + + return true; +} + +/* public functions */ + +int event_context_init(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_config *config, + struct event_context **result) +{ + struct event_context *eventd; + const char *debug_script; + int ret; + + eventd = talloc_zero(mem_ctx, struct event_context); + if (eventd == NULL) { + return ENOMEM; + } + + eventd->ev = ev; + eventd->config = config; + + ret = run_proc_init(eventd, ev, &eventd->run_proc_ctx); + if (ret != 0) { + talloc_free(eventd); + return ret; + } + + eventd->script_dir = path_etcdir_append(eventd, "events"); + if (eventd->script_dir == NULL) { + talloc_free(eventd); + return ENOMEM; + } + + /* FIXME + status = directory_exist(eventd->script_dir); + if (! status) { + talloc_free(eventd); + return EINVAL; + } + */ + + debug_script = event_config_debug_script(config); + if (debug_script != NULL) { + eventd->debug_script = path_etcdir_append(eventd, + debug_script); + if (eventd->debug_script == NULL) { + D_WARNING("Failed to set debug script to %s\n", + debug_script); + } + } + + *result = eventd; + return 0; +} + +struct event_config *eventd_config(struct event_context *eventd) +{ + return eventd->config; +} + +int eventd_run_ctx(struct event_context *eventd, + const char *comp_name, + struct run_event_context **result) +{ + struct event_component *comp; + int ret; + + ret = eventd_component_add(eventd, comp_name, &comp); + if (ret != 0) { + return ret; + } + + *result = comp->run_ctx; + return 0; +} + +int eventd_set_event_result(struct event_context *eventd, + const char *comp_name, + const char *event_name, + struct run_event_script_list *script_list) +{ + struct event_component *comp; + + comp = eventd_component_find(eventd, comp_name); + if (comp == NULL) { + return ENOENT; + } + + return eventd_event_set(comp, event_name, script_list); +} + +int eventd_get_event_result(struct event_context *eventd, + const char *comp_name, + const char *event_name, + struct run_event_script_list **result) +{ + struct event_component *comp; + int ret; + + ret = eventd_component_add(eventd, comp_name, &comp); + if (ret != 0) { + return ret; + } + + return eventd_event_get(comp, event_name, result); +} + +struct ctdb_event_script_list *eventd_script_list( + TALLOC_CTX *mem_ctx, + struct run_event_script_list *script_list) +{ + struct ctdb_event_script_list *value; + int num_scripts = 0; + int i; + + value = talloc_zero(mem_ctx, struct ctdb_event_script_list); + if (value == NULL) { + return NULL; + } + + if (script_list != NULL) { + num_scripts = script_list->num_scripts; + } + + if (num_scripts <= 0) { + return value; + } + + value->script = talloc_array(value, + struct ctdb_event_script, + num_scripts); + if (value->script == NULL) { + goto fail; + } + + for (i=0; iscript[i]; + struct ctdb_event_script *escript = &value->script[i]; + + escript->name = talloc_strdup(value, rscript->name); + if (escript->name == NULL) { + goto fail; + } + + escript->begin = rscript->begin; + escript->end = rscript->end; + escript->result = rscript->summary; + + if (rscript->output == NULL) { + escript->output = NULL; + continue; + } + + escript->output = talloc_strdup(value, rscript->output); + if (escript->output == NULL) { + goto fail; + } + } + value->num_scripts = num_scripts; + + return value; + +fail: + talloc_free(value); + return NULL; +} diff --git a/ctdb/event/event_daemon.c b/ctdb/event/event_daemon.c new file mode 100644 index 0000000..d96ff6f --- /dev/null +++ b/ctdb/event/event_daemon.c @@ -0,0 +1,382 @@ +/* + CTDB event daemon + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include +#include +#include + +#include "lib/util/tevent_unix.h" + +#include "common/logging.h" +#include "common/path.h" +#include "common/sock_daemon.h" + +#include "event/event_private.h" + +struct event_daemon_state { + TALLOC_CTX *mem_ctx; + char *socket; + char *pidfile; + struct tevent_context *ev; + struct event_config *config; + struct sock_daemon_context *sockd; + struct event_context *eventd; +}; + +static int event_daemon_startup(void *private_data) +{ + struct event_daemon_state *e_state = talloc_get_type_abort( + private_data, struct event_daemon_state); + int ret; + + ret = event_context_init(e_state, + e_state->ev, + e_state->config, + &e_state->eventd); + if (ret != 0) { + D_ERR("Failed to initialize event context\n"); + return ret; + } + + return 0; +} + +static int event_daemon_reconfigure(void *private_data) +{ + struct event_daemon_state *e_state = talloc_get_type_abort( + private_data, struct event_daemon_state); + int ret; + + ret = event_config_reload(e_state->config); + if (ret != 0) { + D_WARNING("Configuration reload failed\n"); + } + + return 0; +} + +static int event_daemon_reopen_logs(void *private_data) +{ + bool status; + + status = logging_reopen_logs(); + + return status ? 0 : 1; +} + +static void event_daemon_shutdown(void *private_data) +{ + struct event_daemon_state *e_state = talloc_get_type_abort( + private_data, struct event_daemon_state); + + TALLOC_FREE(e_state->eventd); +} + +static bool event_client_connect(struct sock_client_context *client, + pid_t pid, + void *private_data) +{ + struct event_daemon_state *e_state = talloc_get_type_abort( + private_data, struct event_daemon_state); + int ret; + + ret = eventd_client_add(e_state->eventd, client); + if (ret != 0) { + D_ERR("Failed to register client, ret=%d\n", ret); + return false; + } + + return true; +} + +static void event_client_disconnect(struct sock_client_context *client, + void *private_data) +{ + struct event_daemon_state *e_state = talloc_get_type_abort( + private_data, struct event_daemon_state); + + eventd_client_del(e_state->eventd, client); +} + +struct event_client_state { + struct tevent_context *ev; + struct event_context *eventd; + struct sock_client_context *client; + uint8_t *buf; + size_t buflen; +}; + +static void event_client_request_done(struct tevent_req *subreq); +static void event_client_reply_done(struct tevent_req *subreq); + +static struct tevent_req *event_client_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sock_client_context *client, + uint8_t *buf, + size_t buflen, + void *private_data) +{ + struct event_daemon_state *e_state = talloc_get_type_abort( + private_data, struct event_daemon_state); + struct tevent_req *req, *subreq; + struct event_client_state *state; + + req = tevent_req_create(mem_ctx, &state, struct event_client_state); + if (req == NULL) { + return NULL; + } + + state->ev = ev; + state->eventd = e_state->eventd; + state->client = client; + + subreq = event_pkt_send(state, ev, e_state->eventd, buf, buflen); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, event_client_request_done, req); + + return req; +} + +static void event_client_request_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct event_client_state *state = tevent_req_data( + req, struct event_client_state); + int ret = 0; + bool ok; + + ok = event_pkt_recv(subreq, &ret, state, &state->buf, &state->buflen); + TALLOC_FREE(subreq); + if (!ok) { + tevent_req_error(req, ret); + return; + } + + ok = eventd_client_exists(state->eventd, state->client); + if (!ok) { + /* Client has already disconnected */ + talloc_free(state->buf); + tevent_req_done(req); + return; + } + + subreq = sock_socket_write_send(state, + state->ev, + state->client, + state->buf, + state->buflen); + if (tevent_req_nomem(subreq, req)) { + talloc_free(state->buf); + return; + } + tevent_req_set_callback(subreq, event_client_reply_done, req); +} + +static void event_client_reply_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct event_client_state *state = tevent_req_data( + req, struct event_client_state); + int ret = 0; + bool ok; + + talloc_free(state->buf); + + ok = sock_socket_write_recv(subreq, &ret); + TALLOC_FREE(subreq); + if (!ok) { + D_ERR("Sending reply failed\n"); + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +static bool event_client_recv(struct tevent_req *req, int *perr) +{ + if (tevent_req_is_unix_error(req, perr)) { + return false; + } + + return true; +} + +static struct { + int pid; + int startup_fd; +} options = { + .pid = -1, + .startup_fd = -1, +}; + +struct poptOption cmdline_options[] = { + POPT_AUTOHELP + { "pid", 'P', POPT_ARG_INT, &options.pid, 0, + "pid to wait for", "PID" }, + { "startup-fd", 'S', POPT_ARG_INT, &options.startup_fd, 0, + "file descriptor to notify of successful start", "FD" }, + POPT_TABLEEND +}; + +int main(int argc, const char **argv) +{ + poptContext pc; + struct event_daemon_state *e_state; + struct sock_daemon_funcs daemon_funcs; + struct sock_socket_funcs socket_funcs; + const char *log_location = "file:"; + const char *log_level = "NOTICE"; + const char *t; + int interactive = 0; + int opt, ret; + bool ok; + + pc = poptGetContext(argv[0], + argc, + argv, + cmdline_options, + 0); + while ((opt = poptGetNextOpt(pc)) != -1) { + D_ERR("Invalid options %s: %s\n", + poptBadOption(pc, 0), + poptStrerror(opt)); + exit(1); + } + + t = getenv("CTDB_INTERACTIVE"); + if (t != NULL) { + interactive = 1; + } + + e_state = talloc_zero(NULL, struct event_daemon_state); + if (e_state == NULL) { + D_ERR("Memory allocation error\n"); + ret = 1; + goto fail; + } + + e_state->mem_ctx = talloc_new(e_state); + if (e_state->mem_ctx == NULL) { + D_ERR("Memory allocation error\n"); + ret = 1; + goto fail; + } + + e_state->socket = path_socket(e_state, "eventd"); + if (e_state->socket == NULL) { + D_ERR("Memory allocation error\n"); + ret = 1; + goto fail; + } + + e_state->pidfile = path_pidfile(e_state, "eventd"); + if (e_state->pidfile == NULL) { + D_ERR("Memory allocation error\n"); + ret = 1; + goto fail; + } + + ret = event_config_init(e_state, &e_state->config); + if (ret != 0) { + D_ERR("Failed to initialize event config\n"); + goto fail; + } + + e_state->ev = tevent_context_init(e_state->mem_ctx); + if (e_state->ev == NULL) { + D_ERR("Failed to initialize tevent\n"); + ret = 1; + goto fail; + } + + daemon_funcs = (struct sock_daemon_funcs) { + .startup = event_daemon_startup, + .reconfigure = event_daemon_reconfigure, + .reopen_logs = event_daemon_reopen_logs, + .shutdown = event_daemon_shutdown, + }; + + if (interactive == 0) { + log_location = event_config_log_location(e_state->config); + log_level = event_config_log_level(e_state->config); + } + + ret = sock_daemon_setup(e_state->mem_ctx, + "ctdb-eventd", + log_location, + log_level, + &daemon_funcs, + e_state, + &e_state->sockd); + if (ret != 0) { + D_ERR("Failed to setup sock daemon\n"); + goto fail; + } + + socket_funcs = (struct sock_socket_funcs) { + .connect = event_client_connect, + .disconnect = event_client_disconnect, + .read_send = event_client_send, + .read_recv = event_client_recv, + }; + + ret = sock_daemon_add_unix(e_state->sockd, + e_state->socket, + &socket_funcs, + e_state); + if (ret != 0) { + D_ERR("Failed to setup socket %s\n", e_state->socket); + goto fail; + } + + if (options.startup_fd != -1) { + ok = sock_daemon_set_startup_fd(e_state->sockd, + options.startup_fd); + if (!ok) { + goto fail; + } + } + + ret = sock_daemon_run(e_state->ev, + e_state->sockd, + e_state->pidfile, + false, + false, + options.pid); + if (ret == EINTR) { + ret = 0; + } + + if (t != NULL) { + talloc_report_full(e_state->mem_ctx, stderr); + } + +fail: + talloc_free(e_state); + (void)poptFreeContext(pc); + exit(ret); +} diff --git a/ctdb/event/event_private.h b/ctdb/event/event_private.h new file mode 100644 index 0000000..0cc8d80 --- /dev/null +++ b/ctdb/event/event_private.h @@ -0,0 +1,103 @@ +/* + CTDB event daemon + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_EVENT_PRIVATE_H__ +#define __CTDB_EVENT_PRIVATE_H__ + +#include +#include + +#include "common/run_event.h" +#include "common/sock_daemon.h" + +#include "event/event_protocol.h" + +struct event_config; +struct event_context; + +/* From event/event_cmd.c */ + +struct tevent_req *event_cmd_dispatch_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + struct ctdb_event_request *request); +bool event_cmd_dispatch_recv(struct tevent_req *req, + int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **reply); + +/* From event/event_config.c */ + +int event_config_init(TALLOC_CTX *mem_ctx, struct event_config **result); + +const char *event_config_log_location(struct event_config *config); +const char *event_config_log_level(struct event_config *config); +const char *event_config_debug_script(struct event_config *config); + +int event_config_reload(struct event_config *config); + +/* From event/event_context.c */ + +int eventd_client_add(struct event_context *eventd, + struct sock_client_context *client); +void eventd_client_del(struct event_context *eventd, + struct sock_client_context *client); +bool eventd_client_exists(struct event_context *eventd, + struct sock_client_context *client); + +int event_context_init(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_config *config, + struct event_context **result); + +struct event_config *eventd_config(struct event_context *eventd); +int eventd_run_ctx(struct event_context *eventd, + const char *comp_name, + struct run_event_context **result); + +int eventd_set_event_result(struct event_context *eventd, + const char *comp_name, + const char *event_name, + struct run_event_script_list *script_list); +int eventd_get_event_result(struct event_context *eventd, + const char *comp_name, + const char *event_name, + struct run_event_script_list **result); + +struct ctdb_event_script_list *eventd_script_list( + TALLOC_CTX *mem_ctx, + struct run_event_script_list *script_list); + + +/* From event/event_request.c */ + +struct tevent_req *event_pkt_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + uint8_t *buf, + size_t buflen); + +bool event_pkt_recv(struct tevent_req *req, + int *perr, + TALLOC_CTX *mem_ctx, + uint8_t **buf, + size_t *buflen); + +#endif /* __CTDB_EVENT_PRIVATE_H__ */ diff --git a/ctdb/event/event_protocol.c b/ctdb/event/event_protocol.c new file mode 100644 index 0000000..baa9e1e --- /dev/null +++ b/ctdb/event/event_protocol.c @@ -0,0 +1,1123 @@ +/* + CTDB event daemon protocol + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include + +#include "protocol/protocol_basic.h" + +#include "event_protocol.h" +#include "event_protocol_api.h" + +static size_t ctdb_event_script_action_len(enum ctdb_event_script_action in) +{ + uint32_t u32 = in; + + return ctdb_uint32_len(&u32); +} + +static void ctdb_event_script_action_push(enum ctdb_event_script_action in, + uint8_t *buf, + size_t *npush) +{ + uint32_t u32 = in; + + ctdb_uint32_push(&u32, buf, npush); +} + +static int ctdb_event_script_action_pull(uint8_t *buf, + size_t buflen, + enum ctdb_event_script_action *out, + size_t *npull) +{ + enum ctdb_event_script_action value; + uint32_t u32; + size_t np; + int ret; + + ret = ctdb_uint32_pull(buf, buflen, &u32, &np); + if (ret != 0) { + return ret; + } + + switch (u32) { + case 0: + value = CTDB_EVENT_SCRIPT_DISABLE; + break; + + case 1: + value = CTDB_EVENT_SCRIPT_ENABLE; + break; + + default: + return EINVAL; + } + + *out = value; + *npull = np; + + return 0; +} + +static size_t ctdb_event_command_len(enum ctdb_event_command in) +{ + uint32_t u32 = in; + + return ctdb_uint32_len(&u32); +} + +static void ctdb_event_command_push(enum ctdb_event_command in, + uint8_t *buf, + size_t *npush) +{ + uint32_t u32 = in; + + ctdb_uint32_push(&u32, buf, npush); +} + +static int ctdb_event_command_pull(uint8_t *buf, + size_t buflen, + enum ctdb_event_command *out, + size_t *npull) +{ + enum ctdb_event_command value; + uint32_t u32; + size_t np; + int ret; + + ret = ctdb_uint32_pull(buf, buflen, &u32, &np); + if (ret != 0) { + return ret; + } + + switch (u32) { + case 1: + value = CTDB_EVENT_CMD_RUN; + break; + + case 2: + value = CTDB_EVENT_CMD_STATUS; + break; + + case 3: + value = CTDB_EVENT_CMD_SCRIPT; + break; + + default: + return EINVAL; + } + + *out = value; + *npull = np; + + return 0; +} + +static size_t ctdb_event_script_len(struct ctdb_event_script *in) +{ + return ctdb_stringn_len(&in->name) + + ctdb_timeval_len(&in->begin) + + ctdb_timeval_len(&in->end) + + ctdb_int32_len(&in->result) + + ctdb_stringn_len(&in->output); +} + +static void ctdb_event_script_push(struct ctdb_event_script *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_stringn_push(&in->name, buf+offset, &np); + offset += np; + + ctdb_timeval_push(&in->begin, buf+offset, &np); + offset += np; + + ctdb_timeval_push(&in->end, buf+offset, &np); + offset += np; + + ctdb_int32_push(&in->result, buf+offset, &np); + offset += np; + + ctdb_stringn_push(&in->output, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_event_script_pull_elems(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_script *value, + size_t *npull) +{ + size_t offset = 0, np; + int ret; + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + mem_ctx, + &value->name, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_timeval_pull(buf+offset, + buflen-offset, + &value->begin, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_timeval_pull(buf+offset, + buflen-offset, + &value->end, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_int32_pull(buf+offset, + buflen-offset, + &value->result, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + mem_ctx, + &value->output, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + *npull = offset; + + return 0; +} + +#ifdef EVENT_PROTOCOL_TEST +static int ctdb_event_script_pull(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_script **out, + size_t *npull) +{ + struct ctdb_event_script *value; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_script); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_event_script_pull_elems(buf, buflen, value, value, npull); + if (ret != 0) { + talloc_free(value); + return ret; + } + + *out = value; + + return 0; +} +#endif + +static size_t ctdb_event_script_list_len(struct ctdb_event_script_list *in) +{ + size_t len; + int i; + + len = ctdb_int32_len(&in->num_scripts); + + for (i=0; inum_scripts; i++) { + len += ctdb_event_script_len(&in->script[i]); + } + + return len; +} + +static void ctdb_event_script_list_push(struct ctdb_event_script_list *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + int i; + + ctdb_int32_push(&in->num_scripts, buf+offset, &np); + offset += np; + + for (i=0; inum_scripts; i++) { + ctdb_event_script_push(&in->script[i], buf+offset, &np); + offset += np; + } + + *npush = offset; +} + +static int ctdb_event_script_list_pull(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_script_list **out, + size_t *npull) +{ + struct ctdb_event_script_list *value = NULL; + size_t offset = 0, np; + int num_scripts; + int ret, i; + + ret = ctdb_int32_pull(buf+offset, buflen-offset, &num_scripts, &np); + if (ret != 0) { + return ret; + } + offset += np; + + if (num_scripts < 0) { + return EINVAL; + } + + value = talloc_zero(mem_ctx, struct ctdb_event_script_list); + if (value == NULL) { + return ENOMEM; + } + + value->num_scripts = num_scripts; + if (num_scripts == 0) { + goto done; + } + + value->script = talloc_array(value, struct ctdb_event_script, + num_scripts); + if (value->script == NULL) { + ret = ENOMEM; + goto fail; + } + + for (i=0; iscript[i], + &np); + if (ret != 0) { + goto fail; + } + offset += np; + } + +done: + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +static size_t ctdb_event_request_run_len(struct ctdb_event_request_run *in) +{ + return ctdb_stringn_len(&in->component) + + ctdb_stringn_len(&in->event) + + ctdb_stringn_len(&in->args) + + ctdb_uint32_len(&in->timeout) + + ctdb_uint32_len(&in->flags); +} + +static void ctdb_event_request_run_push(struct ctdb_event_request_run *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_stringn_push(&in->component, buf+offset, &np); + offset += np; + + ctdb_stringn_push(&in->event, buf+offset, &np); + offset += np; + + ctdb_stringn_push(&in->args, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->timeout, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->flags, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_event_request_run_pull(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_request_run **out, + size_t *npull) +{ + struct ctdb_event_request_run *value; + size_t offset = 0, np; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_request_run); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->component, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->event, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->args, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, + buflen-offset, + &value->timeout, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, + buflen-offset, + &value->flags, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +static size_t ctdb_event_request_status_len( + struct ctdb_event_request_status *in) +{ + return ctdb_stringn_len(&in->component) + + ctdb_stringn_len(&in->event); +} + +static void ctdb_event_request_status_push( + struct ctdb_event_request_status *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_stringn_push(&in->component, buf+offset, &np); + offset += np; + + ctdb_stringn_push(&in->event, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_event_request_status_pull( + uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_request_status **out, + size_t *npull) +{ + struct ctdb_event_request_status *value; + size_t offset = 0, np; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_request_status); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->component, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->event, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +static size_t ctdb_event_request_script_len( + struct ctdb_event_request_script *in) +{ + return ctdb_stringn_len(&in->component) + + ctdb_stringn_len(&in->script) + + ctdb_event_script_action_len(in->action); +} + +static void ctdb_event_request_script_push( + struct ctdb_event_request_script *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_stringn_push(&in->component, buf+offset, &np); + offset += np; + + ctdb_stringn_push(&in->script, buf+offset, &np); + offset += np; + + ctdb_event_script_action_push(in->action, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_event_request_script_pull( + uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_request_script **out, + size_t *npull) +{ + struct ctdb_event_request_script *value; + size_t offset = 0, np; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_request_script); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->component, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_stringn_pull(buf+offset, + buflen-offset, + value, + &value->script, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_event_script_action_pull(buf+offset, + buflen-offset, + &value->action, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +static size_t ctdb_event_reply_status_len( + struct ctdb_event_reply_status *in) +{ + return ctdb_int32_len(&in->summary) + + ctdb_event_script_list_len(in->script_list); +} + +static void ctdb_event_reply_status_push( + struct ctdb_event_reply_status *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_int32_push(&in->summary, buf+offset, &np); + offset += np; + + ctdb_event_script_list_push(in->script_list, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_event_reply_status_pull( + uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply_status **out, + size_t *npull) +{ + struct ctdb_event_reply_status *value; + size_t offset = 0, np; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_reply_status); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_int32_pull(buf+offset, buflen-offset, &value->summary, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_event_script_list_pull(buf+offset, + buflen-offset, + value, + &value->script_list, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +static size_t ctdb_event_header_len(struct ctdb_event_header *in) +{ + return ctdb_uint32_len(&in->length) + + ctdb_uint32_len(&in->version) + + ctdb_uint32_len(&in->reqid); +} + +static void ctdb_event_header_push(struct ctdb_event_header *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_uint32_push(&in->length, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->version, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->reqid, buf+offset, &np); + offset += np; + + *npush = offset; +} + +static int ctdb_event_header_pull(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *value, + size_t *npull) +{ + size_t offset = 0, np; + int ret; + + ret = ctdb_uint32_pull(buf+offset, + buflen-offset, + &value->length, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, + buflen-offset, + &value->version, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, + buflen-offset, + &value->reqid, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + *npull = offset; + + return 0; +} + +int ctdb_event_header_extract(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *value) +{ + size_t np; + + return ctdb_event_header_pull(buf, buflen, value, &np); +} + +static size_t ctdb_event_request_data_len(struct ctdb_event_request *in) +{ + size_t len; + + len = ctdb_event_command_len(in->cmd); + + switch (in->cmd) { + case CTDB_EVENT_CMD_RUN: + len += ctdb_event_request_run_len(in->data.run); + break; + + case CTDB_EVENT_CMD_STATUS: + len += ctdb_event_request_status_len(in->data.status); + break; + + case CTDB_EVENT_CMD_SCRIPT: + len += ctdb_event_request_script_len(in->data.script); + break; + + default: + break; + } + + return len; +} + +static void ctdb_event_request_data_push(struct ctdb_event_request *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_event_command_push(in->cmd, buf+offset, &np); + offset += np; + + switch (in->cmd) { + case CTDB_EVENT_CMD_RUN: + ctdb_event_request_run_push(in->data.run, buf+offset, &np); + break; + + case CTDB_EVENT_CMD_STATUS: + ctdb_event_request_status_push(in->data.status, + buf+offset, + &np); + break; + + case CTDB_EVENT_CMD_SCRIPT: + ctdb_event_request_script_push(in->data.script, + buf+offset, + &np); + break; + default: + np = 0; + break; + } + offset += np; + + *npush = offset; +} + +static int ctdb_event_request_data_pull(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_request **out, + size_t *npull) +{ + struct ctdb_event_request *value; + size_t offset = 0, np; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_request); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_event_command_pull(buf+offset, + buflen-offset, + &value->cmd, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + switch (value->cmd) { + case CTDB_EVENT_CMD_RUN: + ret = ctdb_event_request_run_pull(buf+offset, + buflen-offset, + value, + &value->data.run, + &np); + break; + + case CTDB_EVENT_CMD_STATUS: + ret = ctdb_event_request_status_pull(buf+offset, + buflen-offset, + value, + &value->data.status, + &np); + break; + + case CTDB_EVENT_CMD_SCRIPT: + ret = ctdb_event_request_script_pull(buf+offset, + buflen-offset, + value, + &value->data.script, + &np); + break; + + default: + np = 0; + break; + } + + if (ret != 0) { + goto fail; + } + offset += np; + + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +static size_t ctdb_event_reply_data_len(struct ctdb_event_reply *in) +{ + size_t len; + + len = ctdb_event_command_len(in->cmd) + + ctdb_int32_len(&in->result); + + if (in->result != 0) { + goto done; + } + + switch (in->cmd) { + case CTDB_EVENT_CMD_STATUS: + len += ctdb_event_reply_status_len(in->data.status); + break; + + default: + break; + } + +done: + return len; +} + +static void ctdb_event_reply_data_push(struct ctdb_event_reply *in, + uint8_t *buf, + size_t *npush) +{ + size_t offset = 0, np; + + ctdb_event_command_push(in->cmd, buf+offset, &np); + offset += np; + + ctdb_int32_push(&in->result, buf+offset, &np); + offset += np; + + if (in->result != 0) { + goto done; + } + + switch (in->cmd) { + case CTDB_EVENT_CMD_STATUS: + ctdb_event_reply_status_push(in->data.status, buf+offset, &np); + break; + + default: + np = 0; + break; + } + offset += np; + +done: + *npush = offset; +} + +static int ctdb_event_reply_data_pull(uint8_t *buf, + size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **out, + size_t *npull) +{ + struct ctdb_event_reply *value; + size_t offset = 0, np; + int ret; + + value = talloc(mem_ctx, struct ctdb_event_reply); + if (value == NULL) { + return ENOMEM; + } + + ret = ctdb_event_command_pull(buf+offset, + buflen-offset, + &value->cmd, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_int32_pull(buf+offset, buflen-offset, &value->result, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + if (value->result != 0) { + goto done; + } + + switch (value->cmd) { + case CTDB_EVENT_CMD_STATUS: + ret = ctdb_event_reply_status_pull(buf+offset, + buflen-offset, + value, + &value->data.status, + &np); + break; + + default: + np = 0; + break; + } + + if (ret != 0) { + goto fail; + } + offset += np; + +done: + *out = value; + *npull = offset; + + return 0; + +fail: + talloc_free(value); + return ret; +} + +size_t ctdb_event_request_len(struct ctdb_event_header *h, + struct ctdb_event_request *in) +{ + return ctdb_event_header_len(h) + + ctdb_event_request_data_len(in); +} + +int ctdb_event_request_push(struct ctdb_event_header *h, + struct ctdb_event_request *in, + uint8_t *buf, + size_t *buflen) +{ + size_t len, offset = 0, np; + + len = ctdb_event_request_len(h, in); + if (*buflen < len) { + *buflen = len; + return EMSGSIZE; + } + + h->length = *buflen; + + ctdb_event_header_push(h, buf+offset, &np); + offset += np; + + ctdb_event_request_data_push(in, buf+offset, &np); + offset += np; + + if (offset > *buflen) { + return EMSGSIZE; + } + + return 0; +} + +int ctdb_event_request_pull(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *h, + TALLOC_CTX *mem_ctx, + struct ctdb_event_request **out) +{ + size_t offset = 0, np; + int ret; + + ret = ctdb_event_header_pull(buf+offset, buflen-offset, h, &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_event_request_data_pull(buf+offset, + buflen-offset, + mem_ctx, + out, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + if (offset > buflen) { + return EMSGSIZE; + } + + return 0; +} + +size_t ctdb_event_reply_len(struct ctdb_event_header *h, + struct ctdb_event_reply *in) +{ + return ctdb_event_header_len(h) + + ctdb_event_reply_data_len(in); +} + +int ctdb_event_reply_push(struct ctdb_event_header *h, + struct ctdb_event_reply *in, + uint8_t *buf, + size_t *buflen) +{ + size_t len, offset = 0, np; + + len = ctdb_event_reply_len(h, in); + if (*buflen < len) { + *buflen = len; + return EMSGSIZE; + } + + h->length = *buflen; + + ctdb_event_header_push(h, buf+offset, &np); + offset += np; + + ctdb_event_reply_data_push(in, buf+offset, &np); + offset += np; + + if (offset > *buflen) { + return EMSGSIZE; + } + + return 0; +} + +int ctdb_event_reply_pull(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *h, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **out) +{ + size_t offset = 0, np; + int ret; + + ret = ctdb_event_header_pull(buf+offset, buflen-offset, h, &np); + if (ret != 0) { + return ret; + } + offset += np; + + ret = ctdb_event_reply_data_pull(buf+offset, + buflen-offset, + mem_ctx, + out, + &np); + if (ret != 0) { + return ret; + } + offset += np; + + if (offset > buflen) { + return EMSGSIZE; + } + + return 0; +} diff --git a/ctdb/event/event_protocol.h b/ctdb/event/event_protocol.h new file mode 100644 index 0000000..e7680fc --- /dev/null +++ b/ctdb/event/event_protocol.h @@ -0,0 +1,100 @@ +/* + CTDB event daemon protocol + Based on eventd code + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_EVENT_PROTOCOL_H__ +#define __CTDB_EVENT_PROTOCOL_H__ + +#define CTDB_EVENT_PROTOCOL_VERSION 1 + +enum ctdb_event_script_action { + CTDB_EVENT_SCRIPT_DISABLE = 0, + CTDB_EVENT_SCRIPT_ENABLE = 1, +}; + +enum ctdb_event_command { + CTDB_EVENT_CMD_RUN = 1, + CTDB_EVENT_CMD_STATUS = 2, + CTDB_EVENT_CMD_SCRIPT = 3, + CTDB_EVENT_CMD_MAX = 4, +}; + +struct ctdb_event_script { + const char *name; + struct timeval begin; + struct timeval end; + int result; + const char *output; +}; + +struct ctdb_event_script_list { + int num_scripts; + struct ctdb_event_script *script; +}; + +#define CTDB_EVENT_RUN_ALL 1 + +struct ctdb_event_request_run { + const char *component; + const char *event; + const char *args; + uint32_t timeout; + uint32_t flags; +}; + +struct ctdb_event_request_status { + const char *component; + const char *event; +}; + +struct ctdb_event_request_script { + const char *component; + const char *script; + enum ctdb_event_script_action action; +}; + +struct ctdb_event_reply_status { + int32_t summary; + struct ctdb_event_script_list *script_list; +}; + +struct ctdb_event_header { + uint32_t length; + uint32_t version; + uint32_t reqid; +}; + +struct ctdb_event_request { + enum ctdb_event_command cmd; + union { + struct ctdb_event_request_run *run; + struct ctdb_event_request_status *status; + struct ctdb_event_request_script *script; + } data; +}; + +struct ctdb_event_reply { + enum ctdb_event_command cmd; + int32_t result; + union { + struct ctdb_event_reply_status *status; + } data; +}; + +#endif /* __CTDB_EVENT_PROTOCOL_H__ */ diff --git a/ctdb/event/event_protocol_api.h b/ctdb/event/event_protocol_api.h new file mode 100644 index 0000000..e2ab439 --- /dev/null +++ b/ctdb/event/event_protocol_api.h @@ -0,0 +1,61 @@ +/* + CTDB event daemon protocol + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#ifndef __CTDB_EVENT_PROTOCOL_API_H__ +#define __CTDB_EVENT_PROTOCOL_API_H__ + +#include + +#include "event/event_protocol.h" + +/* From event/event_protocol.c */ + +int ctdb_event_header_extract(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *h); + +size_t ctdb_event_request_len(struct ctdb_event_header *h, + struct ctdb_event_request *in); +int ctdb_event_request_push(struct ctdb_event_header *h, + struct ctdb_event_request *in, + uint8_t *buf, + size_t *buflen); +int ctdb_event_request_pull(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *h, + TALLOC_CTX *mem_ctx, + struct ctdb_event_request **out); + +size_t ctdb_event_reply_len(struct ctdb_event_header *h, + struct ctdb_event_reply *in); +int ctdb_event_reply_push(struct ctdb_event_header *h, + struct ctdb_event_reply *in, + uint8_t *buf, + size_t *buflen); +int ctdb_event_reply_pull(uint8_t *buf, + size_t buflen, + struct ctdb_event_header *h, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **out); + +/* From event/event_protocol_util.c */ + +const char *ctdb_event_command_to_string(enum ctdb_event_command cmd); + +#endif /* __CTDB_EVENT_PROTOCOL_API_H__ */ diff --git a/ctdb/event/event_protocol_test.c b/ctdb/event/event_protocol_test.c new file mode 100644 index 0000000..8f34fa5 --- /dev/null +++ b/ctdb/event/event_protocol_test.c @@ -0,0 +1,412 @@ +/* + CTDB event daemon - protocol test + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include +#include + +#define EVENT_PROTOCOL_TEST +#include "event/event_protocol.c" + +#include "tests/src/protocol_common_basic.h" + +/* + * Functions to fill and verify event protocol structures + */ + +static void fill_ctdb_event_script(TALLOC_CTX *mem_ctx, + struct ctdb_event_script *p) +{ + fill_ctdb_stringn(mem_ctx, &p->name); + fill_ctdb_timeval(&p->begin); + fill_ctdb_timeval(&p->end); + p->result = rand32i(); + fill_ctdb_stringn(mem_ctx, &p->output); +} + +static void verify_ctdb_event_script(struct ctdb_event_script *p1, + struct ctdb_event_script *p2) +{ + verify_ctdb_stringn(&p1->name, &p2->name); + verify_ctdb_timeval(&p1->begin, &p2->begin); + verify_ctdb_timeval(&p1->end, &p2->end); + assert(p1->result == p2->result); + verify_ctdb_stringn(&p1->output, &p2->output); +} + +static void fill_ctdb_event_script_list(TALLOC_CTX *mem_ctx, + struct ctdb_event_script_list *p) +{ + int i; + + p->num_scripts = rand_int(32); + if (p->num_scripts > 0) { + p->script = talloc_array(mem_ctx, + struct ctdb_event_script, + p->num_scripts); + assert(p->script != NULL); + + for (i=0; inum_scripts; i++) { + fill_ctdb_event_script(mem_ctx, &p->script[i]); + } + } else { + p->script = NULL; + } +} + +static void verify_ctdb_event_script_list(struct ctdb_event_script_list *p1, + struct ctdb_event_script_list *p2) +{ + int i; + + assert(p1->num_scripts == p2->num_scripts); + for (i=0; inum_scripts; i++) { + verify_ctdb_event_script(&p1->script[i], &p2->script[i]); + } +} + +static void fill_ctdb_event_request_run(TALLOC_CTX *mem_ctx, + struct ctdb_event_request_run *p) +{ + fill_ctdb_stringn(mem_ctx, &p->component); + fill_ctdb_stringn(mem_ctx, &p->event); + fill_ctdb_stringn(mem_ctx, &p->args); + p->timeout = rand32(); + p->flags = rand32(); +} + +static void verify_ctdb_event_request_run(struct ctdb_event_request_run *p1, + struct ctdb_event_request_run *p2) +{ + verify_ctdb_stringn(&p1->component, &p2->component); + verify_ctdb_stringn(&p1->event, &p2->event); + verify_ctdb_stringn(&p1->args, &p2->args); + assert(p1->timeout == p2->timeout); + assert(p1->flags == p2->flags); +} + +static void fill_ctdb_event_request_status(TALLOC_CTX *mem_ctx, + struct ctdb_event_request_status *p) +{ + fill_ctdb_stringn(mem_ctx, &p->component); + fill_ctdb_stringn(mem_ctx, &p->event); +} + +static void verify_ctdb_event_request_status( + struct ctdb_event_request_status *p1, + struct ctdb_event_request_status *p2) +{ + verify_ctdb_stringn(&p1->component, &p2->component); + verify_ctdb_stringn(&p1->event, &p2->event); +} + +static void fill_ctdb_event_request_script(TALLOC_CTX *mem_ctx, + struct ctdb_event_request_script *p) +{ + fill_ctdb_stringn(mem_ctx, &p->component); + fill_ctdb_stringn(mem_ctx, &p->script); + if (rand_int(1) == 0) { + p->action = CTDB_EVENT_SCRIPT_DISABLE; + } else { + p->action = CTDB_EVENT_SCRIPT_ENABLE; + } +} + +static void fill_ctdb_event_reply_status(TALLOC_CTX *mem_ctx, + struct ctdb_event_reply_status *p) +{ + p->summary = rand32i(); + p->script_list = talloc(mem_ctx, struct ctdb_event_script_list); + assert(p->script_list != NULL); + + fill_ctdb_event_script_list(mem_ctx, p->script_list); +} + +static void verify_ctdb_event_reply_status(struct ctdb_event_reply_status *p1, + struct ctdb_event_reply_status *p2) +{ + assert(p1->summary == p2->summary); + verify_ctdb_event_script_list(p1->script_list, p2->script_list); +} + +static void verify_ctdb_event_request_script( + struct ctdb_event_request_script *p1, + struct ctdb_event_request_script *p2) +{ + verify_ctdb_stringn(&p1->component, &p2->component); + verify_ctdb_stringn(&p1->script, &p2->script); + assert(p1->action == p2->action); +} + +static void fill_ctdb_event_request_data(TALLOC_CTX *mem_ctx, + struct ctdb_event_request *p, + uint32_t cmd) +{ + p->cmd = cmd; + + switch (cmd) { + case CTDB_EVENT_CMD_RUN: + p->data.run = talloc(mem_ctx, struct ctdb_event_request_run); + assert(p->data.run != NULL); + + fill_ctdb_event_request_run(mem_ctx, p->data.run); + break; + + case CTDB_EVENT_CMD_STATUS: + p->data.status = talloc(mem_ctx, + struct ctdb_event_request_status); + assert(p->data.status != NULL); + + fill_ctdb_event_request_status(mem_ctx, p->data.status); + break; + + case CTDB_EVENT_CMD_SCRIPT: + p->data.script = talloc(mem_ctx, + struct ctdb_event_request_script); + assert(p->data.script != NULL); + + fill_ctdb_event_request_script(mem_ctx, p->data.script); + break; + + default: + assert(cmd > 0 && cmd < CTDB_EVENT_CMD_MAX); + } +} + +static void verify_ctdb_event_request_data(struct ctdb_event_request *p1, + struct ctdb_event_request *p2) +{ + assert(p1->cmd == p2->cmd); + + switch (p1->cmd) { + case CTDB_EVENT_CMD_RUN: + verify_ctdb_event_request_run(p1->data.run, p2->data.run); + break; + + case CTDB_EVENT_CMD_STATUS: + verify_ctdb_event_request_status(p1->data.status, + p2->data.status); + break; + + case CTDB_EVENT_CMD_SCRIPT: + verify_ctdb_event_request_script(p1->data.script, + p2->data.script); + break; + + default: + assert(p1->cmd > 0 && p1->cmd < CTDB_EVENT_CMD_MAX); + } +} + +static void fill_ctdb_event_reply_data(TALLOC_CTX *mem_ctx, + struct ctdb_event_reply *p, + uint32_t cmd) +{ + p->cmd = cmd; + p->result = rand32i(); + + if (p->result != 0) { + return; + } + + switch (cmd) { + case CTDB_EVENT_CMD_STATUS: + p->data.status = talloc(mem_ctx, + struct ctdb_event_reply_status); + assert(p->data.status != NULL); + + fill_ctdb_event_reply_status(mem_ctx, p->data.status); + break; + + default: + assert(cmd > 0 && cmd < CTDB_EVENT_CMD_MAX); + } +} + +static void verify_ctdb_event_reply_data(struct ctdb_event_reply *p1, + struct ctdb_event_reply *p2) +{ + assert(p1->cmd == p2->cmd); + assert(p1->result == p2->result); + + if (p1->result != 0) { + return; + } + + switch (p1->cmd) { + case CTDB_EVENT_CMD_STATUS: + verify_ctdb_event_reply_status(p1->data.status, + p2->data.status); + break; + + default: + assert(p1->cmd > 0 && p1->cmd < CTDB_EVENT_CMD_MAX); + } +} + +static void fill_ctdb_event_header(struct ctdb_event_header *p) +{ + p->length = 0; /* updated by push functions */ + p->version = 0; /* updated by push functions */ + p->reqid = rand32(); +} + +static void verify_ctdb_event_header(struct ctdb_event_header *p1, + struct ctdb_event_header *p2) +{ + assert(p1->length == p2->length); + assert(p1->version == p2->version); + assert(p1->reqid == p2->reqid); +} + +static void fill_ctdb_event_request(TALLOC_CTX *mem_ctx, + struct ctdb_event_request *p, + uint32_t cmd) +{ + fill_ctdb_event_request_data(mem_ctx, p, cmd); +} + +static void verify_ctdb_event_request(struct ctdb_event_request *p1, + struct ctdb_event_request *p2) +{ + verify_ctdb_event_request_data(p1, p2); +} + +static void fill_ctdb_event_reply(TALLOC_CTX *mem_ctx, + struct ctdb_event_reply *p, + uint32_t cmd) +{ + fill_ctdb_event_reply_data(mem_ctx, p, cmd); +} + +static void verify_ctdb_event_reply(struct ctdb_event_reply *p1, + struct ctdb_event_reply *p2) +{ + verify_ctdb_event_reply_data(p1, p2); +} + +#define EVENT_PROTOCOL1_TEST(TYPE, NAME) \ +static void TEST_FUNC(NAME)(uint32_t cmd) \ +{ \ + TALLOC_CTX *mem_ctx; \ + TYPE c1, *c2; \ + uint8_t *buf; \ + size_t buflen, np; \ + int ret; \ +\ + protocol_test_iterate_tag("%s %u\n", #NAME, cmd); \ + mem_ctx = talloc_new(NULL); \ + assert(mem_ctx != NULL); \ + FILL_FUNC(NAME)(mem_ctx, &c1, cmd); \ + buflen = LEN_FUNC(NAME)(&c1); \ + buf = talloc_size(mem_ctx, buflen); \ + assert(buf != NULL); \ + np = 0; \ + PUSH_FUNC(NAME)(&c1, buf, &np); \ + assert(np == buflen); \ + np = 0; \ + ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2, &np); \ + assert(ret == 0); \ + assert(np == buflen); \ + VERIFY_FUNC(NAME)(&c1, c2); \ + talloc_free(mem_ctx); \ +} + +#define EVENT_PROTOCOL2_TEST(TYPE, NAME) \ +static void TEST_FUNC(NAME)(uint32_t cmd) \ +{ \ + TALLOC_CTX *mem_ctx; \ + struct ctdb_event_header h1, h2; \ + TYPE c1, *c2; \ + uint8_t *buf; \ + size_t buflen, len; \ + int ret; \ +\ + protocol_test_iterate_tag("%s %u\n", #NAME, cmd); \ + mem_ctx = talloc_new(NULL); \ + assert(mem_ctx != NULL); \ + fill_ctdb_event_header(&h1); \ + FILL_FUNC(NAME)(mem_ctx, &c1, cmd); \ + buflen = LEN_FUNC(NAME)(&h1, &c1); \ + buf = talloc_size(mem_ctx, buflen); \ + assert(buf != NULL); \ + len = 0; \ + ret = PUSH_FUNC(NAME)(&h1, &c1, buf, &len); \ + assert(ret == EMSGSIZE); \ + assert(len == buflen); \ + ret = PUSH_FUNC(NAME)(&h1, &c1, buf, &buflen); \ + assert(ret == 0); \ + ret = PULL_FUNC(NAME)(buf, buflen, &h2, mem_ctx, &c2); \ + assert(ret == 0); \ + verify_ctdb_event_header(&h1, &h2); \ + VERIFY_FUNC(NAME)(&c1, c2); \ + talloc_free(mem_ctx); \ +} + +PROTOCOL_TYPE3_TEST(struct ctdb_event_script, ctdb_event_script); +PROTOCOL_TYPE3_TEST(struct ctdb_event_script_list, ctdb_event_script_list); + +PROTOCOL_TYPE3_TEST(struct ctdb_event_request_run, ctdb_event_request_run); +PROTOCOL_TYPE3_TEST(struct ctdb_event_request_status, + ctdb_event_request_status); +PROTOCOL_TYPE3_TEST(struct ctdb_event_request_script, + ctdb_event_request_script); + +PROTOCOL_TYPE3_TEST(struct ctdb_event_reply_status, ctdb_event_reply_status); + +EVENT_PROTOCOL1_TEST(struct ctdb_event_request, ctdb_event_request_data); +EVENT_PROTOCOL1_TEST(struct ctdb_event_reply, ctdb_event_reply_data); + +EVENT_PROTOCOL2_TEST(struct ctdb_event_request, ctdb_event_request); +EVENT_PROTOCOL2_TEST(struct ctdb_event_reply, ctdb_event_reply); + +static void event_protocol_test(void) +{ + uint32_t cmd; + + TEST_FUNC(ctdb_event_script)(); + TEST_FUNC(ctdb_event_script_list)(); + + TEST_FUNC(ctdb_event_request_run)(); + TEST_FUNC(ctdb_event_request_status)(); + TEST_FUNC(ctdb_event_request_script)(); + + TEST_FUNC(ctdb_event_reply_status)(); + + for (cmd=1; cmd. +*/ + +#include "replace.h" + +#include "event/event_protocol.h" +#include "event/event_protocol_api.h" + +static struct { + enum ctdb_event_command command; + const char *label; +} event_command_map[] = { + { CTDB_EVENT_CMD_RUN, "RUN" }, + { CTDB_EVENT_CMD_STATUS, "STATUS" }, + { CTDB_EVENT_CMD_SCRIPT, "SCRIPT" }, + { CTDB_EVENT_CMD_MAX, NULL }, +}; + +const char *ctdb_event_command_to_string(enum ctdb_event_command command) +{ + int i; + + for (i=0; event_command_map[i].label != NULL; i++) { + if (event_command_map[i].command == command) { + return event_command_map[i].label; + } + } + + return "UNKNOWN"; +} diff --git a/ctdb/event/event_request.c b/ctdb/event/event_request.c new file mode 100644 index 0000000..303e735 --- /dev/null +++ b/ctdb/event/event_request.c @@ -0,0 +1,217 @@ +/* + CTDB event daemon - handle requests + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" + +#include "lib/util/debug.h" +#include "lib/util/tevent_unix.h" + +#include "common/logging.h" + +#include "event/event_private.h" +#include "event/event_protocol_api.h" + +struct event_request_state { + struct ctdb_event_request *request; + struct ctdb_event_reply *reply; +}; + +static void event_request_done(struct tevent_req *subreq); + +static struct tevent_req *event_request_send( + TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + struct ctdb_event_header *header, + struct ctdb_event_request *request) +{ + struct tevent_req *req, *subreq; + struct event_request_state *state; + + req = tevent_req_create(mem_ctx, &state, struct event_request_state); + if (req == NULL) { + return NULL; + } + + state->request = request; + + subreq = event_cmd_dispatch_send(state, ev, eventd, request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, event_request_done, req); + + return req; +} + +static void event_request_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct event_request_state *state = tevent_req_data( + req, struct event_request_state); + int ret; + bool ok; + + ok = event_cmd_dispatch_recv(subreq, &ret, state, &state->reply); + TALLOC_FREE(subreq); + if (!ok) { + D_ERR("Command %s failed, ret=%d\n", + ctdb_event_command_to_string(state->request->cmd), ret); + + state->reply = talloc_zero(state, struct ctdb_event_reply); + if (tevent_req_nomem(state->reply, req)) { + return; + } + + state->reply->cmd = state->request->cmd; + state->reply->result = EIO; + } + + tevent_req_done(req); +} + +static bool event_request_recv(struct tevent_req *req, + int *perr, + TALLOC_CTX *mem_ctx, + struct ctdb_event_reply **reply) +{ + struct event_request_state *state = tevent_req_data( + req, struct event_request_state); + + if (tevent_req_is_unix_error(req, perr)) { + return false; + } + + *reply = talloc_steal(mem_ctx, state->reply); + + return true; +} + +struct event_pkt_state { + struct ctdb_event_header header; + struct ctdb_event_request *request; + uint8_t *buf; + size_t buflen; +}; + +static void event_pkt_done(struct tevent_req *subreq); + +struct tevent_req *event_pkt_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct event_context *eventd, + uint8_t *buf, + size_t buflen) +{ + struct tevent_req *req, *subreq; + struct event_pkt_state *state; + int ret; + + req = tevent_req_create(mem_ctx, &state, struct event_pkt_state); + if (req == NULL) { + return NULL; + } + + ret = ctdb_event_request_pull(buf, + buflen, + &state->header, + state, + &state->request); + if (ret != 0) { + /* Ignore invalid packets */ + D_ERR("Invalid packet received, buflen=%zu\n", buflen); + tevent_req_error(req, EPROTO); + return tevent_req_post(req, ev); + } + + subreq = event_request_send(state, + ev, + eventd, + &state->header, + state->request); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, event_pkt_done, req); + + return req; +} + +static void event_pkt_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct event_pkt_state *state = tevent_req_data( + req, struct event_pkt_state); + struct ctdb_event_header header; + struct ctdb_event_reply *reply; + int ret; + bool ok; + + ok = event_request_recv(subreq, &ret, state, &reply); + TALLOC_FREE(subreq); + TALLOC_FREE(state->request); + if (!ok) { + tevent_req_error(req, ret); + return; + } + + header = (struct ctdb_event_header) { + .reqid = state->header.reqid, + }; + + state->buflen = ctdb_event_reply_len(&header, reply); + state->buf = talloc_zero_size(state, state->buflen); + if (tevent_req_nomem(state->buf, req)) { + talloc_free(reply); + return; + } + + ret = ctdb_event_reply_push(&header, + reply, + state->buf, + &state->buflen); + talloc_free(reply); + if (ret != 0) { + talloc_free(state->buf); + tevent_req_error(req, ret); + return; + } + + tevent_req_done(req); +} + +bool event_pkt_recv(struct tevent_req *req, + int *perr, + TALLOC_CTX *mem_ctx, + uint8_t **buf, + size_t *buflen) +{ + struct event_pkt_state *state = tevent_req_data( + req, struct event_pkt_state); + + if (tevent_req_is_unix_error(req, perr)) { + return false; + } + + *buf = talloc_steal(mem_ctx, state->buf); + *buflen = state->buflen; + + return true; +} diff --git a/ctdb/event/event_tool.c b/ctdb/event/event_tool.c new file mode 100644 index 0000000..46dc25e --- /dev/null +++ b/ctdb/event/event_tool.c @@ -0,0 +1,792 @@ +/* + CTDB event daemon utility code + + Copyright (C) Amitay Isaacs 2018 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/filesys.h" +#include "system/time.h" + +#include +#include +#include + +#include "lib/util/debug.h" + +#include "common/cmdline.h" +#include "common/logging.h" +#include "common/path.h" +#include "common/event_script.h" + +#include "event/event_protocol_api.h" +#include "event/event.h" +#include "event/event_tool.h" + +struct event_tool_context { + struct cmdline_context *cmdline; + struct tevent_context *ev; + struct ctdb_event_context *eclient; +}; + +static int compact_args(TALLOC_CTX *mem_ctx, + const char **argv, + int argc, + int from, + const char **result) +{ + char *arg_str; + int i; + + if (argc <= from) { + *result = NULL; + return 0; + } + + arg_str = talloc_strdup(mem_ctx, argv[from]); + if (arg_str == NULL) { + return ENOMEM; + } + + for (i = from+1; i < argc; i++) { + arg_str = talloc_asprintf_append(arg_str, " %s", argv[i]); + if (arg_str == NULL) { + return ENOMEM; + } + } + + *result = arg_str; + return 0; +} + +static int event_command_run(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct event_tool_context *ctx = talloc_get_type_abort( + private_data, struct event_tool_context); + struct tevent_req *req; + struct ctdb_event_request_run request_run; + const char *arg_str = NULL; + const char *t; + int timeout, ret = 0, result = 0; + bool ok; + + if (argc < 3) { + cmdline_usage(ctx->cmdline, "run"); + return 1; + } + + ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient); + if (ret != 0) { + D_ERR("Failed to initialize event client, ret=%d\n", ret); + return ret; + } + + timeout = atoi(argv[0]); + if (timeout < 0) { + timeout = 0; + } + + ret = compact_args(mem_ctx, argv, argc, 3, &arg_str); + if (ret != 0) { + D_ERR("Memory allocation error\n"); + return 1; + } + + request_run.component = argv[1]; + request_run.event = argv[2]; + request_run.args = arg_str; + request_run.timeout = timeout; + request_run.flags = 0; + + t = getenv("CTDB_TEST_MODE"); + if (t != NULL) { + t = getenv("CTDB_EVENT_RUN_ALL"); + if (t != NULL) { + request_run.flags = CTDB_EVENT_RUN_ALL; + } + } + + req = ctdb_event_run_send(mem_ctx, + ctx->ev, + ctx->eclient, + &request_run); + if (req == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + tevent_req_poll(req, ctx->ev); + + ok = ctdb_event_run_recv(req, &ret, &result); + if (!ok) { + D_ERR("Failed to run event %s in %s, ret=%d\n", + argv[2], + argv[1], + ret); + return 1; + } + + D_NOTICE("Command run finished with result=%d\n", result); + + if (result == ENOENT) { + printf("Event dir for %s does not exist\n", argv[1]); + } else if (result == ETIMEDOUT) { + printf("Event %s in %s timed out\n", argv[2], argv[1]); + } else if (result == ECANCELED) { + printf("Event %s in %s got cancelled\n", argv[2], argv[1]); + } else if (result == ENOEXEC) { + printf("Event %s in %s failed\n", argv[2], argv[1]); + } else if (result != 0) { + printf("Failed to run event %s in %s, result=%d\n", + argv[2], + argv[1], + result); + } + + ret = (result < 0) ? -result : result; + return ret; +} + +static double timeval_delta(struct timeval *tv2, struct timeval *tv) +{ + return (tv2->tv_sec - tv->tv_sec) + + (tv2->tv_usec - tv->tv_usec) * 1.0e-6; +} + +static void print_status_one(struct ctdb_event_script *script) +{ + if (script->result == -ETIMEDOUT) { + printf("%-20s %-10s %s", + script->name, + "TIMEDOUT", + ctime(&script->begin.tv_sec)); + } else if (script->result == -ENOEXEC) { + printf("%-20s %-10s\n", script->name, "DISABLED"); + } else if (script->result < 0) { + printf("%-20s %-10s (%s)\n", + script->name, + "CANNOT RUN", + strerror(-script->result)); + } else if (script->result == 0) { + printf("%-20s %-10s %.3lf %s", + script->name, + "OK", + timeval_delta(&script->end, &script->begin), + ctime(&script->begin.tv_sec)); + } else { + printf("%-20s %-10s %.3lf %s", + script->name, + "ERROR", + timeval_delta(&script->end, &script->begin), + ctime(&script->begin.tv_sec)); + } + + if (script->result != 0 && script->result != -ENOEXEC) { + printf(" OUTPUT: %s\n", + script->output == NULL ? "" : script->output); + } +} + +static void print_status(const char *component, + const char *event, + int result, + struct ctdb_event_reply_status *status) +{ + int i; + + if (result != 0) { + if (result == ENOENT) { + printf("Event dir for %s does not exist\n", component); + } else if (result == EINVAL) { + printf("Event %s has never run in %s\n", + event, + component); + } else { + printf("Unknown error (%d) for event %s in %s\n", + result, + event, + component); + } + return; + } + + for (i=0; iscript_list->num_scripts; i++) { + print_status_one(&status->script_list->script[i]); + } +} + +static int event_command_status(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct event_tool_context *ctx = talloc_get_type_abort( + private_data, struct event_tool_context); + struct tevent_req *req; + struct ctdb_event_request_status request_status; + struct ctdb_event_reply_status *reply_status; + int ret = 0, result = 0; + bool ok; + + if (argc != 2) { + cmdline_usage(ctx->cmdline, "status"); + return 1; + } + + ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient); + if (ret != 0) { + D_ERR("Failed to initialize event client, ret=%d\n", ret); + return ret; + } + + request_status.component = argv[0]; + request_status.event = argv[1]; + + req = ctdb_event_status_send(mem_ctx, + ctx->ev, + ctx->eclient, + &request_status); + if (req == NULL) { + D_ERR("Memory allocation error\n"); + return 1; + } + + tevent_req_poll(req, ctx->ev); + + ok = ctdb_event_status_recv(req, + &ret, + &result, + mem_ctx, + &reply_status); + if (!ok) { + D_ERR("Failed to get status for event %s in %s, ret=%d\n", + argv[1], + argv[0], + ret); + return 1; + } + + D_NOTICE("Command status finished with result=%d\n", result); + + print_status(argv[0], argv[1], result, reply_status); + + if (reply_status == NULL) { + ret = result; + } else { + ret = reply_status->summary; + ret = (ret < 0) ? -ret : ret; + } + return ret; +} + +#define EVENT_SCRIPT_DISABLED ' ' +#define EVENT_SCRIPT_ENABLED '*' + +static int event_command_script_list(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct event_tool_context *ctx = talloc_get_type_abort( + private_data, struct event_tool_context); + char *subdir = NULL; + char *data_dir = NULL; + char *etc_dir = NULL; + char *t = NULL; + struct event_script_list *data_list = NULL; + struct event_script_list *etc_list = NULL; + unsigned int i, j, matched; + int ret = 0; + + if (argc != 1) { + cmdline_usage(ctx->cmdline, "script list"); + return 1; + } + + subdir = talloc_asprintf(mem_ctx, "events/%s", argv[0]); + if (subdir == NULL) { + return ENOMEM; + } + + data_dir = path_datadir_append(mem_ctx, subdir); + if (data_dir == NULL) { + return ENOMEM; + } + + t = talloc_size(mem_ctx, PATH_MAX); + if (t == NULL) { + return ENOMEM; + } + + data_dir = realpath(data_dir, t); + if (data_dir == NULL) { + if (errno != ENOENT) { + return errno; + } + D_ERR("Command script list finished with result=%d\n", ENOENT); + return ENOENT; + } + + etc_dir = path_etcdir_append(mem_ctx, subdir); + if (etc_dir == NULL) { + return ENOMEM; + } + + /* + * Ignore error on ENOENT for cut down (e.g. fixed/embedded) + * installs that don't use symlinks but just populate etc_dir + * directly + */ + ret = event_script_get_list(mem_ctx, data_dir, &data_list); + if (ret != 0 && ret != ENOENT) { + D_ERR("Command script list finished with result=%d\n", ret); + goto done; + } + + ret = event_script_get_list(mem_ctx, etc_dir, &etc_list); + if (ret != 0) { + D_ERR("Command script list finished with result=%d\n", ret); + goto done; + } + + D_NOTICE("Command script list finished with result=%d\n", ret); + + if (data_list == NULL) { + goto list_enabled_only; + } + + /* + * First list scripts provided by CTDB. Flag those that are + * enabled via a symlink and arrange for them to be excluded + * from the subsequent list of local scripts. + * + * Both lists are sorted, so walk the list of enabled scripts + * only once in this pass. + */ + j = 0; + matched = 0; + for (i = 0; i < data_list->num_scripts; i++) { + struct event_script *d = data_list->script[i]; + char flag = EVENT_SCRIPT_DISABLED; + char buf[PATH_MAX]; + ssize_t len; + + /* Check to see if this script is enabled */ + while (j < etc_list->num_scripts) { + struct event_script *e = etc_list->script[j]; + + ret = strcmp(e->name, d->name); + + if (ret > 0) { + /* + * Enabled name is greater, so needs + * to be considered later: done + */ + break; + } + + if (ret < 0) { + /* Enabled name is less: next */ + j++; + continue; + } + + len = readlink(e->path, buf, sizeof(buf)); + if (len == -1 || (size_t)len >= sizeof(buf)) { + /* + * Not a link? Disappeared? Invalid + * link target? Something else? + * + * Doesn't match provided script: next, done + */ + j++; + break; + } + + /* readlink() does not NUL-terminate */ + buf[len] = '\0'; + + ret = strcmp(buf, d->path); + if (ret != 0) { + /* Enabled link doesn't match: next, done */ + j++; + break; + } + + /* + * Enabled script's symlink matches our + * script: flag our script as enabled + * + * Also clear the enabled script so it can be + * trivially skipped in the next pass + */ + flag = EVENT_SCRIPT_ENABLED; + TALLOC_FREE(etc_list->script[j]); + j++; + matched++; + break; + } + + printf("%c %s\n", flag, d->name); + } + + /* Print blank line if both provided and local lists are being printed */ + if (data_list->num_scripts > 0 && matched != etc_list->num_scripts) { + printf("\n"); + } + +list_enabled_only: + + /* Now print details of local scripts, after a blank line */ + for (j = 0; j < etc_list->num_scripts; j++) { + struct event_script *e = etc_list->script[j]; + char flag = EVENT_SCRIPT_DISABLED; + + if (e == NULL) { + /* Matched in previous pass: next */ + continue; + } + + /* Script is local: if executable then flag as enabled */ + if (e->enabled) { + flag = EVENT_SCRIPT_ENABLED; + } + + printf("%c %s\n", flag, e->name); + } + + ret = 0; + +done: + talloc_free(subdir); + talloc_free(data_dir); + talloc_free(etc_dir); + talloc_free(data_list); + talloc_free(etc_list); + + return ret; +} + +static int event_command_script(TALLOC_CTX *mem_ctx, + struct event_tool_context *ctx, + const char *component, + const char *script, + bool enable) +{ + char *subdir, *etc_dir; + int result = 0; + + subdir = talloc_asprintf(mem_ctx, "events/%s", component); + if (subdir == NULL) { + return ENOMEM; + } + + etc_dir = path_etcdir_append(mem_ctx, subdir); + if (etc_dir == NULL) { + return ENOMEM; + } + + if (enable) { + result = event_script_chmod(etc_dir, script, true); + } else { + result = event_script_chmod(etc_dir, script, false); + } + + talloc_free(subdir); + talloc_free(etc_dir); + + D_NOTICE("Command script finished with result=%d\n", result); + + if (result == EINVAL) { + printf("Script %s is invalid in %s\n", script, component); + } else if (result == ENOENT) { + printf("Script %s does not exist in %s\n", script, component); + } + + return result; +} + +static int event_command_script_enable(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct event_tool_context *ctx = talloc_get_type_abort( + private_data, struct event_tool_context); + struct stat statbuf; + char *script, *etc_script; + int ret; + + if (argc != 2) { + cmdline_usage(ctx->cmdline, "script enable"); + return 1; + } + + script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]); + if (script == NULL) { + return ENOMEM; + } + + etc_script = path_etcdir_append(mem_ctx, script); + if (etc_script == NULL) { + return ENOMEM; + } + + ret = lstat(etc_script, &statbuf); + if (ret == 0) { + if (S_ISLNK(statbuf.st_mode)) { + /* Link already exists */ + return 0; + } else if (S_ISREG(statbuf.st_mode)) { + return event_command_script(mem_ctx, + ctx, + argv[0], + argv[1], + true); + } + + printf("Script %s is not a file or a link\n", etc_script); + return EINVAL; + } else { + if (errno == ENOENT) { + char *t; + char *data_script; + + data_script = path_datadir_append(mem_ctx, script); + if (data_script == NULL) { + return ENOMEM; + } + + t = talloc_size(mem_ctx, PATH_MAX); + if (t == NULL) { + return ENOMEM; + } + + data_script = realpath(data_script, t); + if (data_script == NULL) { + if (errno != ENOENT) { + return errno; + } + printf("Script %s does not exist in %s\n", + argv[1], + argv[0]); + return ENOENT; + } + + ret = stat(data_script, &statbuf); + if (ret != 0) { + printf("Script %s does not exist in %s\n", + argv[1], argv[0]); + return ENOENT; + } + + ret = symlink(data_script, etc_script); + if (ret != 0) { + printf("Failed to create symlink %s\n", + etc_script); + return EIO; + } + + return 0; + } + + printf("Script %s does not exist\n", etc_script); + return EINVAL; + } +} + +static int event_command_script_disable(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + struct event_tool_context *ctx = talloc_get_type_abort( + private_data, struct event_tool_context); + struct stat statbuf; + char *script, *etc_script; + int ret; + + + if (argc != 2) { + cmdline_usage(ctx->cmdline, "script disable"); + return 1; + } + + script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]); + if (script == NULL) { + return ENOMEM; + } + + etc_script = path_etcdir_append(mem_ctx, script); + if (etc_script == NULL) { + return ENOMEM; + } + + ret = lstat(etc_script, &statbuf); + if (ret == 0) { + if (S_ISLNK(statbuf.st_mode)) { + /* Link exists */ + ret = unlink(etc_script); + if (ret != 0) { + printf("Failed to remove symlink %s\n", + etc_script); + return EIO; + } + + return 0; + } else if (S_ISREG(statbuf.st_mode)) { + return event_command_script(mem_ctx, + ctx, + argv[0], + argv[1], + false); + } + + printf("Script %s is not a file or a link\n", etc_script); + return EINVAL; + } + + return 0; +} + +struct cmdline_command event_commands[] = { + { "run", event_command_run, + "Run an event", " " }, + { "status", event_command_status, + "Get status of an event", " " }, + { "script list", event_command_script_list, + "List event scripts", "" }, + { "script enable", event_command_script_enable, + "Enable an event script", "